aboutsummaryrefslogtreecommitdiff
path: root/alarmd
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2018-06-27 21:49:48 +1200
committerDavid Phillips <david@sighup.nz>2018-06-27 21:49:48 +1200
commit6b59b063ecf29868bc6ea8c3734a671d1206ff1b (patch)
tree7d1a0d23e7586fc8c6d0f592c4ae3311799b73be /alarmd
parent7ad723a8fb605f0bcab8a3ed912bef5f2d335f2c (diff)
downloadalarmd-6b59b063ecf29868bc6ea8c3734a671d1206ff1b.tar.xz
Refactor libalarm
Diffstat (limited to 'alarmd')
-rw-r--r--alarmd/alarmd.c35
1 files changed, 21 insertions, 14 deletions
diff --git a/alarmd/alarmd.c b/alarmd/alarmd.c
index 93336b8..59942eb 100644
--- a/alarmd/alarmd.c
+++ b/alarmd/alarmd.c
@@ -15,7 +15,7 @@
struct alarm {
UT_hash_handle hh;
- char uuid[16];
+ uuid_t uuid;
char *name;
bool is_raised;
// time_t last_change;
@@ -53,7 +53,7 @@ int handle_register(int c_sock)
//uuid_unparse(uuid, uuid_str);
//fprintf(stderr, "Registering alarm %s with uuid %s\n", buffer, uuid_str);
- if (send(c_sock, uuid, sizeof(uuid), 0) != sizeof(uuid)) {
+ if (send(c_sock, uuid, sizeof(uuid_t), 0) != sizeof(uuid_t)) {
perror("send");
return 1;
}
@@ -75,17 +75,19 @@ int handle_register(int c_sock)
int handle_deregister(int c_sock)
{
- unsigned char buffer[128];
+ uuid_t uuid;
struct alarm *target = NULL;
char uuid_str[37];
- /* FIXME 16 magic */
- if (recv(c_sock, &buffer, 16, 0) < 16) {
+
+ if (recv(c_sock, &uuid, sizeof(uuid_t), 0) < sizeof(uuid_t)) {
return 1;
}
- uuid_unparse(buffer, uuid_str);
+
+ uuid_unparse(uuid, uuid_str);
fprintf(stderr, "Deregistering alarm %s\n", uuid_str);
+
/* LOCK */
- HASH_FIND(hh, alarms, buffer, 16, target);
+ HASH_FIND(hh, alarms, uuid, sizeof(uuid_t), target);
if (target != NULL) {
HASH_DEL(alarms, target);
free(target);
@@ -97,18 +99,17 @@ int handle_deregister(int c_sock)
int handle_set_raised_generic(int c_sock, int shall_raise)
{
- unsigned char buffer[128];
+ uuid_t uuid;
+ size_t nread = 0;
struct alarm *target = NULL;
- char uuid_str[37];
- /* FIXME 16 magic */
- if (recv(c_sock, &buffer, 16, 0) < 16) {
+
+ if ((nread = recv(c_sock, &uuid, 16, 0)) < 16) {
+ fprintf(stderr, "received not enough - %d\n", nread);
return 1;
}
- uuid_unparse(buffer, uuid_str);
- fprintf(stderr, "%sing alarm %s\n", shall_raise ? "Rais" : "Clear", uuid_str);
/* LOCK */
- HASH_FIND(hh, alarms, buffer, 16, target);
+ HASH_FIND(hh, alarms, uuid, sizeof(uuid_t), target);
if (target != NULL) {
target->is_raised = shall_raise;
}
@@ -206,6 +207,7 @@ int main(int argc, char **argv)
perror("recv");
return 1;
} else if (nread == 0) {
+ printf("Zschero\n");
break;
} else if (nread < sizeof(packet_type)) {
fprintf(stderr, "Packet type too short\n");
@@ -216,26 +218,31 @@ int main(int argc, char **argv)
switch(packet_type) {
case ALARMD_PACKET_TYPE_REGISTER:
if (handle_register(c_sock)) {
+ printf("reg fail\n");
close(c_sock);
}
break;
case ALARMD_PACKET_TYPE_DEREGISTER:
if (handle_deregister(c_sock)) {
+ printf("rereg fail\n");
close(c_sock);
}
break;
case ALARMD_PACKET_TYPE_RAISE:
if (handle_raise(c_sock)) {
+ printf("raise fail\n");
close(c_sock);
}
break;
case ALARMD_PACKET_TYPE_CLEAR:
if (handle_clear(c_sock)) {
+ printf("clear fail\n");
close(c_sock);
}
break;
case ALARMD_PACKET_TYPE_QUERY:
if (handle_query(c_sock)) {
+ printf("query fail\n");
close(c_sock);
}
break;