8 #include <sys/socket.h>
10 #include <asm/byteorder.h>
12 #include <talloc/talloc.h>
15 #include "ui/common/discover-client.h"
16 #include "pb-protocol/pb-protocol.h"
18 struct discover_client {
20 struct discover_client_ops ops;
22 struct device **devices;
25 static int discover_client_destructor(void *arg)
27 struct discover_client *client = arg;
35 struct discover_client* discover_client_init(
36 const struct discover_client_ops *ops, void *cb_arg)
38 struct discover_client *client;
39 struct sockaddr_un addr;
41 client = talloc(NULL, struct discover_client);
45 memcpy(&client->ops, ops, sizeof(client->ops));
46 client->ops.cb_arg = cb_arg;
48 client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
49 if (!client->fd < 0) {
50 pb_log("%s: socket: %s\n", __func__, strerror(errno));
54 talloc_set_destructor(client, discover_client_destructor);
56 client->n_devices = 0;
57 client->devices = NULL;
59 addr.sun_family = AF_UNIX;
60 strcpy(addr.sun_path, PB_SOCKET_PATH);
62 if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
63 pb_log("%s: connect: %s\n", __func__, strerror(errno));
74 int discover_client_get_fd(const struct discover_client *client)
79 void discover_client_destroy(struct discover_client *client)
84 static void device_add(struct discover_client *client, struct device *device)
87 client->devices = talloc_realloc(client, client->devices,
88 struct device *, client->n_devices);
90 client->devices[client->n_devices - 1] = device;
91 talloc_steal(client, device);
93 client->ops.device_add(device, client->ops.cb_arg);
96 static void device_remove(struct discover_client *client, const char *id)
98 struct device *device = NULL;
101 for (i = 0; i < client->n_devices; i++) {
102 if (!strcmp(client->devices[i]->id, id)) {
103 device = client->devices[i];
111 /* remove the device from the client's device array */
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);
119 client->ops.device_remove(device, client->ops.cb_arg);
124 int discover_client_process(struct discover_client *client)
126 struct pb_protocol_message *message;
130 message = pb_protocol_read_message(client, client->fd);
135 switch (message->action) {
136 case PB_PROTOCOL_ACTION_ADD:
137 dev = pb_protocol_deserialise_device(client, message);
139 pb_log("%s: no device?\n", __func__);
143 device_add(client, dev);
145 case PB_PROTOCOL_ACTION_REMOVE:
146 dev_id = pb_protocol_deserialise_string(client, message);
148 pb_log("%s: no device id?\n", __func__);
151 device_remove(client, dev_id);
154 pb_log("%s: unknown action %d\n", __func__, message->action);
161 /* accessors for discovered devices */
162 int discover_client_device_count(struct discover_client *client)
164 return client->n_devices;
167 struct device *discover_client_get_device(struct discover_client *client,
170 if (index < 0 || index >= client->n_devices)
173 return client->devices[index];