8 #include <sys/socket.h>
10 #include <asm/byteorder.h>
12 #include <talloc/talloc.h>
15 #include "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 void discover_client_destroy(struct discover_client *client)
40 static void device_add(struct discover_client *client, struct device *device)
43 client->devices = talloc_realloc(client, client->devices,
44 struct device *, client->n_devices);
46 client->devices[client->n_devices - 1] = device;
47 talloc_steal(client, device);
49 client->ops.device_add(device, client->ops.cb_arg);
52 static void device_remove(struct discover_client *client, const char *id)
54 struct device *device = NULL;
57 for (i = 0; i < client->n_devices; i++) {
58 if (!strcmp(client->devices[i]->id, id)) {
59 device = client->devices[i];
67 /* remove the device from the client's device array */
69 memmove(&client->devices[i], &client->devices[i+1],
70 (client->n_devices - i) * sizeof(client->devices[0]));
71 client->devices = talloc_realloc(client, client->devices,
72 struct device *, client->n_devices);
75 client->ops.device_remove(device, client->ops.cb_arg);
80 static int discover_client_process(void *arg)
82 struct discover_client *client = arg;
83 struct pb_protocol_message *message;
88 message = pb_protocol_read_message(client, client->fd);
93 switch (message->action) {
94 case PB_PROTOCOL_ACTION_ADD:
95 dev = talloc(client, struct device);
97 rc = pb_protocol_deserialise_device(dev, message);
99 pb_log("%s: no device?\n", __func__);
103 device_add(client, dev);
105 case PB_PROTOCOL_ACTION_REMOVE:
106 dev_id = pb_protocol_deserialise_string(client, message);
108 pb_log("%s: no device id?\n", __func__);
111 device_remove(client, dev_id);
114 pb_log("%s: unknown action %d\n", __func__, message->action);
121 struct discover_client* discover_client_init(struct waitset *waitset,
122 const struct discover_client_ops *ops, void *cb_arg)
124 struct discover_client *client;
125 struct sockaddr_un addr;
127 client = talloc(NULL, struct discover_client);
131 memcpy(&client->ops, ops, sizeof(client->ops));
132 client->ops.cb_arg = cb_arg;
134 client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
135 if (client->fd < 0) {
136 pb_log("%s: socket: %s\n", __func__, strerror(errno));
140 talloc_set_destructor(client, discover_client_destructor);
142 client->n_devices = 0;
143 client->devices = NULL;
145 addr.sun_family = AF_UNIX;
146 strcpy(addr.sun_path, PB_SOCKET_PATH);
148 if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
149 pb_log("%s: connect: %s\n", __func__, strerror(errno));
153 waiter_register(waitset, client->fd, WAIT_IN, discover_client_process,
163 /* accessors for discovered devices */
164 int discover_client_device_count(struct discover_client *client)
166 return client->n_devices;
169 struct device *discover_client_get_device(struct discover_client *client,
172 if (index < 0 || index >= client->n_devices)
175 return client->devices[index];
178 static void create_boot_command(struct boot_command *command,
179 const struct device *device __attribute__((unused)),
180 const struct boot_option *boot_option,
181 const struct pb_boot_data *data)
184 command->option_id = boot_option->id;
185 command->boot_image_file = data->image;
186 command->initrd_file = data->initrd;
187 command->boot_args = data->args;
190 int discover_client_boot(struct discover_client *client,
191 const struct device *device,
192 const struct boot_option *boot_option,
193 const struct pb_boot_data *data)
195 struct pb_protocol_message *message;
196 struct boot_command boot_command;
199 create_boot_command(&boot_command, device, boot_option, data);
201 len = pb_protocol_boot_len(&boot_command);
203 message = pb_protocol_create_message(client,
204 PB_PROTOCOL_ACTION_BOOT, len);
209 pb_protocol_serialise_boot_command(&boot_command,
210 message->payload, len);
212 rc = pb_protocol_write_message(client->fd, message);