#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; if (recv(sock, &length, sizeof(uint8_t), 0) < sizeof(uint8_t)) { return -1; } if (length >= 128) { return -1; } if (recv(sock, buffer, length, 0) < 0) { return -1; } (*buffer)[length] = '\0'; return length; } /* Helper function */ int send_string(int sock, char *buffer) { ssize_t sent = 0; size_t length = 0; uint8_t length8 = 0; /* Protocol allows strings of length < 128 */ if (length >= 128) { return 1; } /* bitmasking is unnecessary by now aside from clarity */ length8 = length8 & 0xFF; if (send(sock, &length8, sizeof(length8), 0) < sizeof(length8)) { return 1; } sent = send(sock, buffer, length8, 0); if (sent < 0) { perror("send"); return 1; } else if (sent < length8) { return 1; } return 0; } /* Helper function */ int send_packet_uuid(int sock, uint32_t packet_type, uuid_t uuid) { return (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type) || send(sock, uuid, sizeof(uuid_t), 0) != 16); } /** End helper functions ***********/ int alarmd_register(int sock, char *desc, uuid_t *uuid) { uint8_t length = 0; uint32_t packet_type = 0; packet_type = ALARMD_PACKET_TYPE_REGISTER; if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) { perror("send"); return 1; } length = strlen(desc); if (send(sock, &length, sizeof(length), 0) != sizeof(length)) { perror("send"); return 1; } if (send(sock, desc, strlen(desc), 0) != strlen(desc)) { perror("send"); return 1; } if (recv(sock, uuid, sizeof(*uuid), 0) != sizeof(*uuid)) { perror("recv"); return 1; } return 0; } int alarmd_deregister(int sock, uuid_t uuid) { return send_packet_uuid(sock, ALARMD_PACKET_TYPE_DEREGISTER, uuid); } int alarmd_raise(int sock, uuid_t uuid) { return send_packet_uuid(sock, ALARMD_PACKET_TYPE_RAISE, uuid); } int alarmd_clear(int sock, uuid_t uuid) { return send_packet_uuid(sock, ALARMD_PACKET_TYPE_CLEAR, uuid); }