aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/libalarm.c76
1 files changed, 26 insertions, 50 deletions
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 <stdio.h>
#include <stdint.h>
#include <sys/socket.h>
+#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
-#include <arpa/inet.h>
#include <sys/types.h>
+#include <uuid/uuid.h>
+#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);
}