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;
87 message = pb_protocol_read_message(client, client->fd);
92 switch (message->action) {
93 case PB_PROTOCOL_ACTION_ADD:
94 dev = pb_protocol_deserialise_device(client, message);
96 pb_log("%s: no device?\n", __func__);
100 device_add(client, dev);
102 case PB_PROTOCOL_ACTION_REMOVE:
103 dev_id = pb_protocol_deserialise_string(client, message);
105 pb_log("%s: no device id?\n", __func__);
108 device_remove(client, dev_id);
111 pb_log("%s: unknown action %d\n", __func__, message->action);
118 struct discover_client* discover_client_init(struct waitset *waitset,
119 const struct discover_client_ops *ops, void *cb_arg)
121 struct discover_client *client;
122 struct sockaddr_un addr;
124 client = talloc(NULL, struct discover_client);
128 memcpy(&client->ops, ops, sizeof(client->ops));
129 client->ops.cb_arg = cb_arg;
131 client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
132 if (client->fd < 0) {
133 pb_log("%s: socket: %s\n", __func__, strerror(errno));
137 talloc_set_destructor(client, discover_client_destructor);
139 client->n_devices = 0;
140 client->devices = NULL;
142 addr.sun_family = AF_UNIX;
143 strcpy(addr.sun_path, PB_SOCKET_PATH);
145 if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
146 pb_log("%s: connect: %s\n", __func__, strerror(errno));
150 waiter_register(waitset, client->fd, WAIT_IN, discover_client_process,
160 /* accessors for discovered devices */
161 int discover_client_device_count(struct discover_client *client)
163 return client->n_devices;
166 struct device *discover_client_get_device(struct discover_client *client,
169 if (index < 0 || index >= client->n_devices)
172 return client->devices[index];
175 static void create_boot_command(struct boot_command *command,
176 const struct device *device __attribute__((unused)),
177 const struct boot_option *boot_option,
178 const struct pb_boot_data *data)
181 command->option_id = boot_option->id;
182 command->boot_image_file = data->image;
183 command->initrd_file = data->initrd;
184 command->boot_args = data->args;
187 int discover_client_boot(struct discover_client *client,
188 const struct device *device,
189 const struct boot_option *boot_option,
190 const struct pb_boot_data *data)
192 struct pb_protocol_message *message;
193 struct boot_command boot_command;
196 create_boot_command(&boot_command, device, boot_option, data);
198 len = pb_protocol_boot_len(&boot_command);
200 message = pb_protocol_create_message(client,
201 PB_PROTOCOL_ACTION_BOOT, len);
206 pb_protocol_serialise_boot_command(&boot_command,
207 message->payload, len);
209 rc = pb_protocol_write_message(client->fd, message);