]> git.ozlabs.org Git - petitboot/blob - ui/common/discover-client.c
Remove union in pb_opt_data
[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/log.h>
14
15 #include "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         int n_devices;
22         struct device **devices;
23 };
24
25 static int discover_client_destructor(void *arg)
26 {
27         struct discover_client *client = arg;
28
29         if (client->fd >= 0)
30                 close(client->fd);
31
32         return 0;
33 }
34
35 struct discover_client* discover_client_init(
36         const struct discover_client_ops *ops, void *cb_arg)
37 {
38         struct discover_client *client;
39         struct sockaddr_un addr;
40
41         client = talloc(NULL, struct discover_client);
42         if (!client)
43                 return NULL;
44
45         memcpy(&client->ops, ops, sizeof(client->ops));
46         client->ops.cb_arg = cb_arg;
47
48         client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
49         if (client->fd < 0) {
50                 pb_log("%s: socket: %s\n", __func__, strerror(errno));
51                 goto out_err;
52         }
53
54         talloc_set_destructor(client, discover_client_destructor);
55
56         client->n_devices = 0;
57         client->devices = NULL;
58
59         addr.sun_family = AF_UNIX;
60         strcpy(addr.sun_path, PB_SOCKET_PATH);
61
62         if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
63                 pb_log("%s: connect: %s\n", __func__, strerror(errno));
64                 goto out_err;
65         }
66
67         return client;
68
69 out_err:
70         talloc_free(client);
71         return NULL;
72 }
73
74 int discover_client_get_fd(const struct discover_client *client)
75 {
76         return client->fd;
77 }
78
79 void discover_client_destroy(struct discover_client *client)
80 {
81         talloc_free(client);
82 }
83
84 static void device_add(struct discover_client *client, struct device *device)
85 {
86         client->n_devices++;
87         client->devices = talloc_realloc(client, client->devices,
88                         struct device *, client->n_devices);
89
90         client->devices[client->n_devices - 1] = device;
91         talloc_steal(client, device);
92
93         client->ops.device_add(device, client->ops.cb_arg);
94 }
95
96 static void device_remove(struct discover_client *client, const char *id)
97 {
98         struct device *device = NULL;
99         int i;
100
101         for (i = 0; i < client->n_devices; i++) {
102                 if (!strcmp(client->devices[i]->id, id)) {
103                         device = client->devices[i];
104                         break;
105                 }
106         }
107
108         if (!device)
109                 return;
110
111         /* remove the device from the client's device array */
112         client->n_devices--;
113         memmove(&client->devices[i], &client->devices[i+1],
114                         (client->n_devices - i) * sizeof(client->devices[0]));
115         client->devices = talloc_realloc(client, client->devices,
116                         struct device *, client->n_devices);
117
118         /* notify the UI */
119         client->ops.device_remove(device, client->ops.cb_arg);
120
121         talloc_free(device);
122 }
123
124 int discover_client_process(struct discover_client *client)
125 {
126         struct pb_protocol_message *message;
127         struct device *dev;
128         char *dev_id;
129
130         message = pb_protocol_read_message(client, client->fd);
131
132         if (!message)
133                 return -1;
134
135         switch (message->action) {
136         case PB_PROTOCOL_ACTION_ADD:
137                 dev = pb_protocol_deserialise_device(client, message);
138                 if (!dev) {
139                         pb_log("%s: no device?\n", __func__);
140                         return 0;
141                 }
142
143                 device_add(client, dev);
144                 break;
145         case PB_PROTOCOL_ACTION_REMOVE:
146                 dev_id = pb_protocol_deserialise_string(client, message);
147                 if (!dev_id) {
148                         pb_log("%s: no device id?\n", __func__);
149                         return 0;
150                 }
151                 device_remove(client, dev_id);
152                 break;
153         default:
154                 pb_log("%s: unknown action %d\n", __func__, message->action);
155         }
156
157
158         return 0;
159 }
160
161 /* accessors for discovered devices */
162 int discover_client_device_count(struct discover_client *client)
163 {
164         return client->n_devices;
165 }
166
167 struct device *discover_client_get_device(struct discover_client *client,
168                 int index)
169 {
170         if (index < 0 || index >= client->n_devices)
171                 return NULL;
172
173         return client->devices[index];
174 }