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 static void update_status(struct discover_client *client,
116 struct boot_status *status)
118 if (client->ops.update_status)
119 client->ops.update_status(status, client->ops.cb_arg);
122 static void update_sysinfo(struct discover_client *client,
123 struct system_info *sysinfo)
125 if (client->ops.update_sysinfo)
126 client->ops.update_sysinfo(sysinfo, client->ops.cb_arg);
129 static void update_config(struct discover_client *client,
130 struct config *config)
132 if (client->ops.update_config)
133 client->ops.update_config(config, client->ops.cb_arg);
136 static int discover_client_process(void *arg)
138 struct discover_client *client = arg;
139 struct pb_protocol_message *message;
140 struct system_info *sysinfo;
141 struct boot_status *status;
142 struct boot_option *opt;
143 struct config *config;
149 /* We use a temporary context for processing one message; persistent
150 * data is re-parented to the client in the callbacks. */
151 ctx = talloc_new(client);
153 message = pb_protocol_read_message(ctx, client->fd);
158 switch (message->action) {
159 case PB_PROTOCOL_ACTION_DEVICE_ADD:
160 dev = talloc_zero(ctx, struct device);
161 list_init(&dev->boot_options);
163 rc = pb_protocol_deserialise_device(dev, message);
165 pb_log("%s: no device?\n", __func__);
169 device_add(client, dev);
171 case PB_PROTOCOL_ACTION_BOOT_OPTION_ADD:
172 opt = talloc_zero(ctx, struct boot_option);
174 rc = pb_protocol_deserialise_boot_option(opt, message);
176 pb_log("%s: no boot_option?\n", __func__);
180 boot_option_add(client, opt);
182 case PB_PROTOCOL_ACTION_DEVICE_REMOVE:
183 dev_id = pb_protocol_deserialise_string(ctx, message);
185 pb_log("%s: no device id?\n", __func__);
188 device_remove(client, dev_id);
190 case PB_PROTOCOL_ACTION_STATUS:
191 status = talloc_zero(ctx, struct boot_status);
193 rc = pb_protocol_deserialise_boot_status(status, message);
195 pb_log("%s: invalid status message?\n", __func__);
198 update_status(client, status);
200 case PB_PROTOCOL_ACTION_SYSTEM_INFO:
201 sysinfo = talloc_zero(ctx, struct system_info);
203 rc = pb_protocol_deserialise_system_info(sysinfo, message);
205 pb_log("%s: invalid sysinfo message?\n", __func__);
208 update_sysinfo(client, sysinfo);
210 case PB_PROTOCOL_ACTION_CONFIG:
211 config = talloc_zero(ctx, struct config);
213 rc = pb_protocol_deserialise_config(config, message);
215 pb_log("%s: invalid config message?\n", __func__);
218 update_config(client, config);
221 pb_log("%s: unknown action %d\n", __func__, message->action);
229 struct discover_client* discover_client_init(struct waitset *waitset,
230 const struct discover_client_ops *ops, void *cb_arg)
232 struct discover_client *client;
233 struct sockaddr_un addr;
235 client = talloc(NULL, struct discover_client);
239 memcpy(&client->ops, ops, sizeof(client->ops));
240 client->ops.cb_arg = cb_arg;
242 client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
243 if (client->fd < 0) {
244 pb_log("%s: socket: %s\n", __func__, strerror(errno));
248 talloc_set_destructor(client, discover_client_destructor);
250 client->n_devices = 0;
251 client->devices = NULL;
253 addr.sun_family = AF_UNIX;
254 strcpy(addr.sun_path, PB_SOCKET_PATH);
256 if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
257 pb_log("%s: connect: %s\n", __func__, strerror(errno));
261 waiter_register_io(waitset, client->fd, WAIT_IN,
262 discover_client_process, client);
271 /* accessors for discovered devices */
272 int discover_client_device_count(struct discover_client *client)
274 return client->n_devices;
277 struct device *discover_client_get_device(struct discover_client *client,
280 if (index < 0 || index >= client->n_devices)
283 return client->devices[index];
286 static void create_boot_command(struct boot_command *command,
287 const struct device *device __attribute__((unused)),
288 const struct boot_option *boot_option,
289 const struct pb_boot_data *data)
291 command->option_id = boot_option ? boot_option->id : NULL;
292 command->boot_image_file = data->image;
293 command->initrd_file = data->initrd;
294 command->dtb_file = data->dtb;
295 command->boot_args = data->args;
298 int discover_client_boot(struct discover_client *client,
299 const struct device *device,
300 const struct boot_option *boot_option,
301 const struct pb_boot_data *data)
303 struct pb_protocol_message *message;
304 struct boot_command boot_command;
307 create_boot_command(&boot_command, device, boot_option, data);
309 len = pb_protocol_boot_len(&boot_command);
311 message = pb_protocol_create_message(client,
312 PB_PROTOCOL_ACTION_BOOT, len);
317 pb_protocol_serialise_boot_command(&boot_command,
318 message->payload, len);
320 rc = pb_protocol_write_message(client->fd, message);
325 int discover_client_cancel_default(struct discover_client *client)
327 struct pb_protocol_message *message;
329 message = pb_protocol_create_message(client,
330 PB_PROTOCOL_ACTION_CANCEL_DEFAULT, 0);
335 return pb_protocol_write_message(client->fd, message);
338 int discover_client_send_reinit(struct discover_client *client)
340 struct pb_protocol_message *message;
342 message = pb_protocol_create_message(client,
343 PB_PROTOCOL_ACTION_REINIT, 0);
348 return pb_protocol_write_message(client->fd, message);
351 int discover_client_send_config(struct discover_client *client,
352 struct config *config)
354 struct pb_protocol_message *message;
357 len = pb_protocol_config_len(config);
359 message = pb_protocol_create_message(client,
360 PB_PROTOCOL_ACTION_CONFIG, len);
364 pb_protocol_serialise_config(config, message->payload, len);
366 return pb_protocol_write_message(client->fd, message);