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);
64 if (client->ops.device_add)
65 client->ops.device_add(device, client->ops.cb_arg);
68 static void boot_option_add(struct discover_client *client,
69 struct boot_option *opt)
73 dev = find_device(client, opt->device_id);
75 /* we require that devices are already present before any boot options
79 talloc_steal(dev, opt);
81 if (client->ops.boot_option_add)
82 client->ops.boot_option_add(dev, opt, client->ops.cb_arg);
85 static void device_remove(struct discover_client *client, const char *id)
87 struct device *device = NULL;
90 for (i = 0; i < client->n_devices; i++) {
91 if (!strcmp(client->devices[i]->id, id)) {
92 device = client->devices[i];
100 /* remove the device from the client's device array */
102 memmove(&client->devices[i], &client->devices[i+1],
103 (client->n_devices - i) * sizeof(client->devices[0]));
104 client->devices = talloc_realloc(client, client->devices,
105 struct device *, client->n_devices);
108 client->ops.device_remove(device, client->ops.cb_arg);
113 static void update_status(struct discover_client *client,
114 struct boot_status *status)
116 if (client->ops.update_status)
117 client->ops.update_status(status, client->ops.cb_arg);
121 static void update_sysinfo(struct discover_client *client,
122 struct system_info *sysinfo)
124 if (client->ops.update_sysinfo)
125 client->ops.update_sysinfo(sysinfo, client->ops.cb_arg);
126 talloc_free(sysinfo);
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;
148 message = pb_protocol_read_message(client, client->fd);
153 switch (message->action) {
154 case PB_PROTOCOL_ACTION_DEVICE_ADD:
155 dev = talloc_zero(client, struct device);
156 list_init(&dev->boot_options);
158 rc = pb_protocol_deserialise_device(dev, message);
160 pb_log("%s: no device?\n", __func__);
164 device_add(client, dev);
166 case PB_PROTOCOL_ACTION_BOOT_OPTION_ADD:
167 opt = talloc_zero(client, struct boot_option);
169 rc = pb_protocol_deserialise_boot_option(opt, message);
171 pb_log("%s: no boot_option?\n", __func__);
175 boot_option_add(client, opt);
177 case PB_PROTOCOL_ACTION_DEVICE_REMOVE:
178 dev_id = pb_protocol_deserialise_string(client, message);
180 pb_log("%s: no device id?\n", __func__);
183 device_remove(client, dev_id);
185 case PB_PROTOCOL_ACTION_STATUS:
186 status = talloc_zero(client, struct boot_status);
188 rc = pb_protocol_deserialise_boot_status(status, message);
190 pb_log("%s: invalid status message?\n", __func__);
193 update_status(client, status);
195 case PB_PROTOCOL_ACTION_SYSTEM_INFO:
196 sysinfo = talloc_zero(client, struct system_info);
198 rc = pb_protocol_deserialise_system_info(sysinfo, message);
200 pb_log("%s: invalid sysinfo message?\n", __func__);
203 update_sysinfo(client, sysinfo);
205 case PB_PROTOCOL_ACTION_CONFIG:
206 config = talloc_zero(ctx, struct config);
208 rc = pb_protocol_deserialise_config(config, message);
210 pb_log("%s: invalid config message?\n", __func__);
213 update_config(client, config);
216 pb_log("%s: unknown action %d\n", __func__, message->action);
223 struct discover_client* discover_client_init(struct waitset *waitset,
224 const struct discover_client_ops *ops, void *cb_arg)
226 struct discover_client *client;
227 struct sockaddr_un addr;
229 client = talloc(NULL, struct discover_client);
233 memcpy(&client->ops, ops, sizeof(client->ops));
234 client->ops.cb_arg = cb_arg;
236 client->fd = socket(AF_UNIX, SOCK_STREAM, 0);
237 if (client->fd < 0) {
238 pb_log("%s: socket: %s\n", __func__, strerror(errno));
242 talloc_set_destructor(client, discover_client_destructor);
244 client->n_devices = 0;
245 client->devices = NULL;
247 addr.sun_family = AF_UNIX;
248 strcpy(addr.sun_path, PB_SOCKET_PATH);
250 if (connect(client->fd, (struct sockaddr *)&addr, sizeof(addr))) {
251 pb_log("%s: connect: %s\n", __func__, strerror(errno));
255 waiter_register_io(waitset, client->fd, WAIT_IN,
256 discover_client_process, client);
265 /* accessors for discovered devices */
266 int discover_client_device_count(struct discover_client *client)
268 return client->n_devices;
271 struct device *discover_client_get_device(struct discover_client *client,
274 if (index < 0 || index >= client->n_devices)
277 return client->devices[index];
280 static void create_boot_command(struct boot_command *command,
281 const struct device *device __attribute__((unused)),
282 const struct boot_option *boot_option,
283 const struct pb_boot_data *data)
285 command->option_id = boot_option ? boot_option->id : NULL;
286 command->boot_image_file = data->image;
287 command->initrd_file = data->initrd;
288 command->dtb_file = data->dtb;
289 command->boot_args = data->args;
292 int discover_client_boot(struct discover_client *client,
293 const struct device *device,
294 const struct boot_option *boot_option,
295 const struct pb_boot_data *data)
297 struct pb_protocol_message *message;
298 struct boot_command boot_command;
301 create_boot_command(&boot_command, device, boot_option, data);
303 len = pb_protocol_boot_len(&boot_command);
305 message = pb_protocol_create_message(client,
306 PB_PROTOCOL_ACTION_BOOT, len);
311 pb_protocol_serialise_boot_command(&boot_command,
312 message->payload, len);
314 rc = pb_protocol_write_message(client->fd, message);
319 int discover_client_cancel_default(struct discover_client *client)
321 struct pb_protocol_message *message;
323 message = pb_protocol_create_message(client,
324 PB_PROTOCOL_ACTION_CANCEL_DEFAULT, 0);
329 return pb_protocol_write_message(client->fd, message);