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(
34 const struct discover_client_ops *ops)
36 struct discover_client *client;
37 struct sockaddr_un addr;
39 client = talloc(NULL, struct discover_client);
43 memcpy(&client->ops, ops, sizeof(client->ops));
45 client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
46 if (!client->fd < 0) {
47 pb_log("%s: socket: %s\n", __func__, strerror(errno));
51 talloc_set_destructor(client, discover_client_destructor);
53 addr.sun_family = AF_UNIX;
54 strcpy(addr.sun_path, PB_SOCKET_PATH);
56 if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
57 pb_log("%s: connect: %s\n", __func__, strerror(errno));
68 int discover_client_get_fd(const struct discover_client *client)
73 void discover_client_destroy(struct discover_client *client)
78 int discover_client_process(struct discover_client *client)
80 struct pb_protocol_message *message;
84 message = pb_protocol_read_message(client, client->fd);
89 switch (message->action) {
90 case PB_PROTOCOL_ACTION_ADD:
91 dev = pb_protocol_deserialise_device(client, message);
93 pb_log("%s: no device?\n", __func__);
96 client->ops.add_device(dev, client->ops.cb_arg);
99 case PB_PROTOCOL_ACTION_REMOVE:
100 dev_id = pb_protocol_deserialise_string(client, message);
102 pb_log("%s: no device id?\n", __func__);
105 client->ops.remove_device(dev_id, client->ops.cb_arg);
108 pb_log("%s: unknown action %d\n", __func__, message->action);