9 #include <sys/socket.h>
11 #include <asm/byteorder.h>
13 #include <talloc/talloc.h>
16 #include "discover-client.h"
17 #include "pb-protocol/pb-protocol.h"
19 struct discover_client {
21 struct discover_client_ops ops;
23 struct device **devices;
26 static int discover_client_destructor(void *arg)
28 struct discover_client *client = arg;
36 void discover_client_destroy(struct discover_client *client)
41 static struct device *find_device(struct discover_client *client,
46 for (i = 0; i < client->n_devices; i++) {
47 struct device *dev = client->devices[i];
48 if (!strcmp(dev->id, id))
55 static void device_add(struct discover_client *client, struct device *device)
58 client->devices = talloc_realloc(client, client->devices,
59 struct device *, client->n_devices);
61 client->devices[client->n_devices - 1] = device;
62 talloc_steal(client, device);
63 list_init(&device->boot_options);
65 if (client->ops.device_add)
66 client->ops.device_add(device, client->ops.cb_arg);
69 static void boot_option_add(struct discover_client *client,
70 struct boot_option *opt)
74 dev = find_device(client, opt->device_id);
76 /* we require that devices are already present before any boot options
80 talloc_steal(dev, opt);
81 list_add(&dev->boot_options, &opt->list);
83 if (client->ops.boot_option_add)
84 client->ops.boot_option_add(dev, opt, client->ops.cb_arg);
87 static void device_remove(struct discover_client *client, const char *id)
89 struct device *device = NULL;
92 for (i = 0; i < client->n_devices; i++) {
93 if (!strcmp(client->devices[i]->id, id)) {
94 device = client->devices[i];
102 /* remove the device from the client's device array */
104 memmove(&client->devices[i], &client->devices[i+1],
105 (client->n_devices - i) * sizeof(client->devices[0]));
106 client->devices = talloc_realloc(client, client->devices,
107 struct device *, client->n_devices);
110 client->ops.device_remove(device, client->ops.cb_arg);
115 void discover_client_enumerate(struct discover_client *client)
117 struct boot_option *opt;
118 struct device *device;
121 for (i = 0; i < client->n_devices; i++) {
122 device = client->devices[i];
123 if (client->ops.device_add)
124 client->ops.device_add(device, client->ops.cb_arg);
126 list_for_each_entry(&device->boot_options, opt, list)
127 if (client->ops.boot_option_add)
128 client->ops.boot_option_add(device, opt,
133 static void update_status(struct discover_client *client,
134 struct boot_status *status)
136 if (client->ops.update_status)
137 client->ops.update_status(status, client->ops.cb_arg);
140 static void update_sysinfo(struct discover_client *client,
141 struct system_info *sysinfo)
143 if (client->ops.update_sysinfo)
144 client->ops.update_sysinfo(sysinfo, client->ops.cb_arg);
147 static void update_config(struct discover_client *client,
148 struct config *config)
150 if (client->ops.update_config)
151 client->ops.update_config(config, client->ops.cb_arg);
154 static int discover_client_process(void *arg)
156 struct discover_client *client = arg;
157 struct pb_protocol_message *message;
158 struct system_info *sysinfo;
159 struct boot_status *status;
160 struct boot_option *opt;
161 struct config *config;
167 /* We use a temporary context for processing one message; persistent
168 * data is re-parented to the client in the callbacks. */
169 ctx = talloc_new(client);
171 message = pb_protocol_read_message(ctx, client->fd);
176 switch (message->action) {
177 case PB_PROTOCOL_ACTION_DEVICE_ADD:
178 dev = talloc_zero(ctx, struct device);
179 list_init(&dev->boot_options);
181 rc = pb_protocol_deserialise_device(dev, message);
183 pb_log("%s: no device?\n", __func__);
187 device_add(client, dev);
189 case PB_PROTOCOL_ACTION_BOOT_OPTION_ADD:
190 opt = talloc_zero(ctx, struct boot_option);
192 rc = pb_protocol_deserialise_boot_option(opt, message);
194 pb_log("%s: no boot_option?\n", __func__);
198 boot_option_add(client, opt);
200 case PB_PROTOCOL_ACTION_DEVICE_REMOVE:
201 dev_id = pb_protocol_deserialise_string(ctx, message);
203 pb_log("%s: no device id?\n", __func__);
206 device_remove(client, dev_id);
208 case PB_PROTOCOL_ACTION_STATUS:
209 status = talloc_zero(ctx, struct boot_status);
211 rc = pb_protocol_deserialise_boot_status(status, message);
213 pb_log("%s: invalid status message?\n", __func__);
216 update_status(client, status);
218 case PB_PROTOCOL_ACTION_SYSTEM_INFO:
219 sysinfo = talloc_zero(ctx, struct system_info);
221 rc = pb_protocol_deserialise_system_info(sysinfo, message);
223 pb_log("%s: invalid sysinfo message?\n", __func__);
226 update_sysinfo(client, sysinfo);
228 case PB_PROTOCOL_ACTION_CONFIG:
229 config = talloc_zero(ctx, struct config);
231 rc = pb_protocol_deserialise_config(config, message);
233 pb_log("%s: invalid config message?\n", __func__);
236 update_config(client, config);
239 pb_log("%s: unknown action %d\n", __func__, message->action);
247 struct discover_client* discover_client_init(struct waitset *waitset,
248 const struct discover_client_ops *ops, void *cb_arg)
250 struct discover_client *client;
251 struct sockaddr_un addr;
253 client = talloc(NULL, struct discover_client);
257 memcpy(&client->ops, ops, sizeof(client->ops));
258 client->ops.cb_arg = cb_arg;
260 client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
261 if (client->fd < 0) {
262 pb_log("%s: socket: %s\n", __func__, strerror(errno));
266 talloc_set_destructor(client, discover_client_destructor);
268 client->n_devices = 0;
269 client->devices = NULL;
271 addr.sun_family = AF_UNIX;
272 strcpy(addr.sun_path, PB_SOCKET_PATH);
274 if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
275 pb_log("%s: connect: %s\n", __func__, strerror(errno));
279 waiter_register_io(waitset, client->fd, WAIT_IN,
280 discover_client_process, client);
289 /* accessors for discovered devices */
290 int discover_client_device_count(struct discover_client *client)
292 return client->n_devices;
295 struct device *discover_client_get_device(struct discover_client *client,
298 if (index < 0 || index >= client->n_devices)
301 return client->devices[index];
304 static void create_boot_command(struct boot_command *command,
305 const struct device *device __attribute__((unused)),
306 const struct boot_option *boot_option,
307 const struct pb_boot_data *data)
309 command->option_id = boot_option ? boot_option->id : NULL;
310 command->boot_image_file = data->image;
311 command->initrd_file = data->initrd;
312 command->dtb_file = data->dtb;
313 command->boot_args = data->args;
316 int discover_client_boot(struct discover_client *client,
317 const struct device *device,
318 const struct boot_option *boot_option,
319 const struct pb_boot_data *data)
321 struct pb_protocol_message *message;
322 struct boot_command boot_command;
325 create_boot_command(&boot_command, device, boot_option, data);
327 len = pb_protocol_boot_len(&boot_command);
329 message = pb_protocol_create_message(client,
330 PB_PROTOCOL_ACTION_BOOT, len);
335 pb_protocol_serialise_boot_command(&boot_command,
336 message->payload, len);
338 rc = pb_protocol_write_message(client->fd, message);
343 int discover_client_cancel_default(struct discover_client *client)
345 struct pb_protocol_message *message;
347 message = pb_protocol_create_message(client,
348 PB_PROTOCOL_ACTION_CANCEL_DEFAULT, 0);
353 return pb_protocol_write_message(client->fd, message);
356 int discover_client_send_reinit(struct discover_client *client)
358 struct pb_protocol_message *message;
360 message = pb_protocol_create_message(client,
361 PB_PROTOCOL_ACTION_REINIT, 0);
366 return pb_protocol_write_message(client->fd, message);
369 int discover_client_send_config(struct discover_client *client,
370 struct config *config)
372 struct pb_protocol_message *message;
375 len = pb_protocol_config_len(config);
377 message = pb_protocol_create_message(client,
378 PB_PROTOCOL_ACTION_CONFIG, len);
382 pb_protocol_serialise_config(config, message->payload, len);
384 return pb_protocol_write_message(client->fd, message);
387 int discover_client_send_url(struct discover_client *client,
390 struct pb_protocol_message *message;
393 len = pb_protocol_url_len(url);
395 message = pb_protocol_create_message(client,
396 PB_PROTOCOL_ACTION_ADD_URL, len);
400 pb_protocol_serialise_url(url, message->payload, len);
402 return pb_protocol_write_message(client->fd, message);