9 #include <sys/socket.h>
11 #include <asm/byteorder.h>
13 #include <pb-config/pb-config.h>
14 #include <talloc/talloc.h>
15 #include <waiter/waiter.h>
18 #include "pb-protocol/pb-protocol.h"
19 #include "list/list.h"
21 #include "device-handler.h"
22 #include "discover-server.h"
26 struct discover_server {
28 struct waitset *waitset;
29 struct waiter *waiter;
31 struct device_handler *device_handler;
35 struct discover_server *server;
36 struct list_item list;
37 struct waiter *waiter;
43 static int server_destructor(void *arg)
45 struct discover_server *server = arg;
48 waiter_remove(server->waiter);
50 if (server->socket >= 0)
51 close(server->socket);
56 static int client_destructor(void *arg)
58 struct client *client = arg;
64 waiter_remove(client->waiter);
66 list_remove(&client->list);
72 static void print_clients(struct discover_server *server)
73 __attribute__((unused));
75 static void print_clients(struct discover_server *server)
77 struct client *client;
79 pb_debug("current clients [%p,%p,%p]:\n",
80 &server->clients.head,
81 server->clients.head.prev,
82 server->clients.head.next);
83 list_for_each_entry(&server->clients, client, list)
84 pb_debug("\t[%p,%p,%p] client: %d\n", &client->list,
85 client->list.prev, client->list.next,
89 static int client_write_message(
90 struct discover_server *server __attribute__((unused)),
91 struct client *client, struct pb_protocol_message *message)
95 if (client->remote_closed)
98 rc = pb_protocol_write_message(client->fd, message);
100 client->remote_closed = true;
105 static int write_device_add_message(struct discover_server *server,
106 struct client *client, const struct device *dev)
108 struct pb_protocol_message *message;
111 len = pb_protocol_device_len(dev);
113 message = pb_protocol_create_message(client,
114 PB_PROTOCOL_ACTION_DEVICE_ADD, len);
118 pb_protocol_serialise_device(dev, message->payload, len);
120 return client_write_message(server, client, message);
123 static int write_boot_option_add_message(struct discover_server *server,
124 struct client *client, const struct boot_option *opt)
126 struct pb_protocol_message *message;
129 len = pb_protocol_boot_option_len(opt);
131 message = pb_protocol_create_message(client,
132 PB_PROTOCOL_ACTION_BOOT_OPTION_ADD, len);
136 pb_protocol_serialise_boot_option(opt, message->payload, len);
138 return client_write_message(server, client, message);
141 static int write_device_remove_message(struct discover_server *server,
142 struct client *client, char *dev_id)
144 struct pb_protocol_message *message;
147 len = strlen(dev_id) + sizeof(uint32_t);
149 message = pb_protocol_create_message(client,
150 PB_PROTOCOL_ACTION_DEVICE_REMOVE, len);
154 pb_protocol_serialise_string(message->payload, dev_id);
156 return client_write_message(server, client, message);
159 static int write_boot_status_message(struct discover_server *server,
160 struct client *client, const struct boot_status *status)
162 struct pb_protocol_message *message;
165 len = pb_protocol_boot_status_len(status);
167 message = pb_protocol_create_message(client,
168 PB_PROTOCOL_ACTION_STATUS, len);
172 pb_protocol_serialise_boot_status(status, message->payload, len);
174 return client_write_message(server, client, message);
177 static int write_system_info_message(struct discover_server *server,
178 struct client *client, const struct system_info *sysinfo)
180 struct pb_protocol_message *message;
183 len = pb_protocol_system_info_len(sysinfo);
185 message = pb_protocol_create_message(client,
186 PB_PROTOCOL_ACTION_SYSTEM_INFO, len);
190 pb_protocol_serialise_system_info(sysinfo, message->payload, len);
192 return client_write_message(server, client, message);
195 static int write_config_message(struct discover_server *server,
196 struct client *client, const struct config *config)
198 struct pb_protocol_message *message;
201 len = pb_protocol_config_len(config);
203 message = pb_protocol_create_message(client,
204 PB_PROTOCOL_ACTION_CONFIG, len);
208 pb_protocol_serialise_config(config, message->payload, len);
210 return client_write_message(server, client, message);
213 static int discover_server_process_message(void *arg)
215 struct pb_protocol_message *message;
216 struct boot_command *boot_command;
217 struct client *client = arg;
218 struct config *config;
222 message = pb_protocol_read_message(client, client->fd);
230 switch (message->action) {
231 case PB_PROTOCOL_ACTION_BOOT:
232 boot_command = talloc(client, struct boot_command);
234 rc = pb_protocol_deserialise_boot_command(boot_command,
237 pb_log("%s: no boot command?", __func__);
241 device_handler_boot(client->server->device_handler,
245 case PB_PROTOCOL_ACTION_CANCEL_DEFAULT:
246 device_handler_cancel_default(client->server->device_handler);
249 case PB_PROTOCOL_ACTION_REINIT:
250 device_handler_reinit(client->server->device_handler);
253 case PB_PROTOCOL_ACTION_CONFIG:
254 config = talloc_zero(client, struct config);
256 rc = pb_protocol_deserialise_config(config, message);
258 pb_log("%s: no config?", __func__);
262 device_handler_update_config(client->server->device_handler,
266 case PB_PROTOCOL_ACTION_ADD_URL:
267 url = pb_protocol_deserialise_string((void *) client, message);
269 device_handler_process_url(client->server->device_handler,
274 pb_log("%s: invalid action %d\n", __func__, message->action);
282 static int discover_server_process_connection(void *arg)
284 struct discover_server *server = arg;
285 int fd, rc, i, n_devices;
286 struct client *client;
288 /* accept the incoming connection */
289 fd = accept(server->socket, NULL, 0);
291 pb_log("accept: %s\n", strerror(errno));
295 /* add to our list of clients */
296 client = talloc_zero(server, struct client);
297 list_add(&server->clients, &client->list);
299 talloc_set_destructor(client, client_destructor);
302 client->server = server;
303 client->waiter = waiter_register_io(server->waitset, client->fd,
304 WAIT_IN, discover_server_process_message,
307 /* send sysinfo to client */
308 rc = write_system_info_message(server, client, system_info_get());
312 /* send config to client */
313 rc = write_config_message(server, client, config_get());
317 /* send existing devices to client */
318 n_devices = device_handler_get_device_count(server->device_handler);
319 for (i = 0; i < n_devices; i++) {
320 const struct discover_boot_option *opt;
321 const struct discover_device *device;
323 device = device_handler_get_device(server->device_handler, i);
324 rc = write_device_add_message(server, client, device->device);
328 list_for_each_entry(&device->boot_options, opt, list) {
329 rc = write_boot_option_add_message(server, client,
339 void discover_server_notify_device_add(struct discover_server *server,
340 struct device *device)
342 struct client *client;
344 list_for_each_entry(&server->clients, client, list)
345 write_device_add_message(server, client, device);
349 void discover_server_notify_boot_option_add(struct discover_server *server,
350 struct boot_option *boot_option)
352 struct client *client;
354 list_for_each_entry(&server->clients, client, list)
355 write_boot_option_add_message(server, client, boot_option);
358 void discover_server_notify_device_remove(struct discover_server *server,
359 struct device *device)
361 struct client *client;
363 list_for_each_entry(&server->clients, client, list)
364 write_device_remove_message(server, client, device->id);
368 void discover_server_notify_boot_status(struct discover_server *server,
369 struct boot_status *status)
371 struct client *client;
373 list_for_each_entry(&server->clients, client, list)
374 write_boot_status_message(server, client, status);
377 void discover_server_notify_system_info(struct discover_server *server,
378 const struct system_info *sysinfo)
380 struct client *client;
382 list_for_each_entry(&server->clients, client, list)
383 write_system_info_message(server, client, sysinfo);
386 void discover_server_notify_config(struct discover_server *server,
387 const struct config *config)
389 struct client *client;
391 list_for_each_entry(&server->clients, client, list)
392 write_config_message(server, client, config);
395 void discover_server_set_device_source(struct discover_server *server,
396 struct device_handler *handler)
398 server->device_handler = handler;
401 struct discover_server *discover_server_init(struct waitset *waitset)
403 struct discover_server *server;
404 struct sockaddr_un addr;
406 server = talloc(NULL, struct discover_server);
410 server->waiter = NULL;
411 server->waitset = waitset;
412 list_init(&server->clients);
414 unlink(PB_SOCKET_PATH);
416 server->socket = socket(AF_UNIX, SOCK_STREAM, 0);
417 if (server->socket < 0) {
418 pb_log("error creating server socket: %s\n", strerror(errno));
422 talloc_set_destructor(server, server_destructor);
424 addr.sun_family = AF_UNIX;
425 strcpy(addr.sun_path, PB_SOCKET_PATH);
427 if (bind(server->socket, (struct sockaddr *)&addr, sizeof(addr))) {
428 pb_log("error binding server socket: %s\n", strerror(errno));
432 if (listen(server->socket, 8)) {
433 pb_log("server socket listen: %s\n", strerror(errno));
437 server->waiter = waiter_register_io(server->waitset, server->socket,
438 WAIT_IN, discover_server_process_connection, server);
447 void discover_server_destroy(struct discover_server *server)