112fa65782e88e910e2ba3c11a03e1bb637b6ed2
[petitboot] / ui / common / discover-client.c
1
2 #include <errno.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <stdint.h>
7
8 #include <sys/socket.h>
9 #include <sys/un.h>
10 #include <asm/byteorder.h>
11
12 #include <talloc/talloc.h>
13 #include <log.h>
14
15 #include "ui/common/discover-client.h"
16 #include "pb-protocol/pb-protocol.h"
17
18 struct discover_client {
19         int fd;
20         struct discover_client_ops ops;
21 };
22
23 static int discover_client_destructor(void *arg)
24 {
25         struct discover_client *client = arg;
26
27         if (client->fd >= 0)
28                 close(client->fd);
29
30         return 0;
31 }
32
33 struct discover_client* discover_client_init(struct discover_client_ops *ops)
34 {
35         struct discover_client *client;
36         struct sockaddr_un addr;
37
38         client = talloc(NULL, struct discover_client);
39         if (!client)
40                 return NULL;
41
42         memcpy(&client->ops, ops, sizeof(client->ops));
43
44         client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
45         if (!client->fd < 0) {
46                 pb_log("%s: socket: %s\n", __func__, strerror(errno));
47                 goto out_err;
48         }
49
50         talloc_set_destructor(client, discover_client_destructor);
51
52         addr.sun_family = AF_UNIX;
53         strcpy(addr.sun_path, PB_SOCKET_PATH);
54
55         if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
56                 pb_log("%s: connect: %s\n", __func__, strerror(errno));
57                 goto out_err;
58         }
59
60         return client;
61
62 out_err:
63         talloc_free(client);
64         return NULL;
65 }
66
67 int discover_client_get_fd(struct discover_client *client)
68 {
69         return client->fd;
70 }
71
72 void discover_client_destroy(struct discover_client *client)
73 {
74         talloc_free(client);
75 }
76
77 int discover_client_process(struct discover_client *client)
78 {
79         struct pb_protocol_message *message;
80         struct device *dev;
81         char *dev_id;
82
83         message = pb_protocol_read_message(client, client->fd);
84
85         if (!message)
86                 return -1;
87
88         switch (message->action) {
89         case PB_PROTOCOL_ACTION_ADD:
90                 dev = pb_protocol_deserialise_device(client, message);
91                 if (!dev) {
92                         pb_log("%s: no device?\n", __func__);
93                         return 0;
94                 }
95                 client->ops.add_device(dev, client->ops.cb_arg);
96                 talloc_free(dev);
97                 break;
98         case PB_PROTOCOL_ACTION_REMOVE:
99                 dev_id = pb_protocol_deserialise_string(client, message);
100                 if (!dev_id) {
101                         pb_log("%s: no device id?\n", __func__);
102                         return 0;
103                 }
104                 client->ops.remove_device(dev_id, client->ops.cb_arg);
105                 break;
106         default:
107                 pb_log("%s: unknown action %d\n", __func__, message->action);
108         }
109
110
111         return 0;
112 }