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;
23 static int discover_client_destructor(void *arg)
25 struct discover_client *client = arg;
33 struct discover_client* discover_client_init(struct discover_client_ops *ops)
35 struct discover_client *client;
36 struct sockaddr_un addr;
38 client = talloc(NULL, struct discover_client);
42 memcpy(&client->ops, ops, sizeof(client->ops));
44 client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
45 if (!client->fd < 0) {
46 pb_log("%s: socket: %s\n", __func__, strerror(errno));
50 talloc_set_destructor(client, discover_client_destructor);
52 addr.sun_family = AF_UNIX;
53 strcpy(addr.sun_path, PB_SOCKET_PATH);
55 if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
56 pb_log("%s: connect: %s\n", __func__, strerror(errno));
67 int discover_client_get_fd(struct discover_client *client)
72 void discover_client_destroy(struct discover_client *client)
77 int discover_client_process(struct discover_client *client)
79 struct pb_protocol_message *message;
83 message = pb_protocol_read_message(client, client->fd);
88 switch (message->action) {
89 case PB_PROTOCOL_ACTION_ADD:
90 dev = pb_protocol_deserialise_device(client, message);
92 pb_log("%s: no device?\n", __func__);
95 client->ops.add_device(dev);
98 case PB_PROTOCOL_ACTION_REMOVE:
99 dev_id = pb_protocol_deserialise_string(client, message);
101 pb_log("%s: no device id?\n", __func__);
104 client->ops.remove_device(dev_id);
107 pb_log("%s: unknown action %d\n", __func__, message->action);