From 6b59b063ecf29868bc6ea8c3734a671d1206ff1b Mon Sep 17 00:00:00 2001 From: David Phillips Date: Wed, 27 Jun 2018 21:49:48 +1200 Subject: Refactor libalarm --- lib/libalarm.c | 76 ++++++++++++++++++++-------------------------------------- 1 file changed, 26 insertions(+), 50 deletions(-) (limited to 'lib') diff --git a/lib/libalarm.c b/lib/libalarm.c index fb96e37..6140580 100644 --- a/lib/libalarm.c +++ b/lib/libalarm.c @@ -1,13 +1,15 @@ #include #include #include +#include #include #include -#include #include +#include +#include "alarmd_proto.h" - +/* Helper function */ int8_t recv_string(int sock, char (*buffer)[128]) { uint8_t length = 0; @@ -24,6 +26,7 @@ int8_t recv_string(int sock, char (*buffer)[128]) return length; } +/* Helper function */ int send_string(int sock, char *buffer) { ssize_t sent = 0; @@ -51,9 +54,21 @@ int send_string(int sock, char *buffer) return 0; } -#include "alarmd_proto.h" +/* Helper function */ +int send_packet_uuid(int sock, uint32_t packet_type, uuid_t uuid) +{ + uint32_t packet_type_n = 0; + + packet_type_n = htonl(packet_type); + + return (send(sock, &packet_type_n, sizeof(packet_type_n), 0) != sizeof(packet_type_n) + || send(sock, uuid, 16, 0) != 16); +} + +/** End helper functions ***********/ + -int alarmd_register(int sock, char *desc, char (*uuid)[16]) +int alarmd_register(int sock, char *desc, uuid_t *uuid) { uint8_t length = 0; uint32_t packet_type = 0; @@ -74,63 +89,24 @@ int alarmd_register(int sock, char *desc, char (*uuid)[16]) perror("send"); return 1; } - if (recv(sock, uuid, 16, 0) != 16) { + if (recv(sock, uuid, sizeof(*uuid), 0) != sizeof(*uuid)) { perror("recv"); return 1; } return 0; } -int alarmd_deregister(int sock, char uuid[16]) +int alarmd_deregister(int sock, uuid_t uuid) { - uint32_t packet_type = 0; - - packet_type = htonl(ALARMD_PACKET_TYPE_DEREGISTER); - if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) { - perror("send"); - return 1; - } - - /* FIXME 16 magic */ - if (send(sock, uuid, 16, 0) != 16) { - perror("send"); - return 1; - } - return 0; + return send_packet_uuid(sock, ALARMD_PACKET_TYPE_DEREGISTER, uuid); } -int alarmd_raise(int sock, char uuid[16]) +int alarmd_raise(int sock, uuid_t uuid) { - uint32_t packet_type = 0; - - packet_type = htonl(ALARMD_PACKET_TYPE_RAISE); - if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) { - perror("send"); - return 1; - } - - /* FIXME 16 magic */ - if (send(sock, uuid, 16, 0) != 16) { - perror("send"); - return 1; - } - return 0; + return send_packet_uuid(sock, ALARMD_PACKET_TYPE_RAISE, uuid); } -int alarmd_clear(int sock, char uuid[16]) +int alarmd_clear(int sock, uuid_t uuid) { - uint32_t packet_type = 0; - - packet_type = htonl(ALARMD_PACKET_TYPE_CLEAR); - if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) { - perror("send"); - return 1; - } - - /* FIXME 16 magic */ - if (send(sock, uuid, 16, 0) != 16) { - perror("send"); - return 1; - } - return 0; + return send_packet_uuid(sock, ALARMD_PACKET_TYPE_CLEAR, uuid); } -- cgit v1.1