b099b59128b7bbec99f34845a708d89dc45ea9a7
[petitboot] / ui / test / discover-test.c
1
2 #include <stdio.h>
3
4 #include "ui/common/discover-client.h"
5
6 static const char *device_type_string(enum device_type type)
7 {
8         switch (type) {
9         case DEVICE_TYPE_DISK:
10                 return "disk";
11         case DEVICE_TYPE_USB:
12                 return "usb";
13         case DEVICE_TYPE_NETWORK:
14                 return "network";
15         case DEVICE_TYPE_OPTICAL:
16                 return "optical";
17         case DEVICE_TYPE_ANY:
18                 return "any";
19         case DEVICE_TYPE_UNKNOWN:
20                 return "unknown";
21         }
22         return "invalid";
23 }
24
25 static int print_device_add(struct device *device,
26         void __attribute__((unused)) *arg)
27 {
28         struct boot_option *opt;
29
30         printf("new device:\n");
31         printf("\tid:   %s\n", device->id);
32         printf("\ttype: %s\n", device_type_string(device->type));
33         printf("\tname: %s\n", device->name);
34         printf("\tdesc: %s\n", device->description);
35         printf("\ticon: %s\n", device->icon_file);
36
37         printf("\tboot options:\n");
38         list_for_each_entry(&device->boot_options, opt, list) {
39                 printf("\t\tid:   %s\n", opt->id);
40                 printf("\t\tname: %s\n", opt->name);
41                 printf("\t\tdesc: %s\n", opt->description);
42                 printf("\t\ticon: %s\n", opt->icon_file);
43                 printf("\t\tboot: %s\n", opt->boot_image_file);
44                 printf("\t\tinit: %s\n", opt->initrd_file);
45                 printf("\t\tdtb:  %s\n", opt->dtb_file);
46                 printf("\t\targs: %s\n", opt->boot_args);
47         }
48
49         return 0;
50 }
51
52 static int print_boot_option_add(struct device *dev,
53                 struct boot_option *opt,
54                 void __attribute__((unused)) *arg)
55 {
56         printf("new boot option (dev: %s):\n", dev->id);
57         printf("\tdev id: %s\n", opt->device_id);
58         printf("\tid:     %s\n", opt->id);
59         printf("\tname:   %s\n", opt->name);
60         printf("\tdesc:   %s\n", opt->description);
61         printf("\ticon:   %s\n", opt->icon_file);
62         printf("\tboot:   %s\n", opt->boot_image_file);
63         printf("\tinit:   %s\n", opt->initrd_file);
64         printf("\targs:   %s\n", opt->boot_args);
65         printf("\tdefault:%d\n", opt->is_default);
66
67         return 0;
68 }
69
70 static void print_device_remove(struct device *device,
71         void __attribute__((unused)) *arg)
72 {
73         printf("removed device:\n");
74         printf("\tid:   %s\n", device->id);
75         printf("\tname: %s\n", device->name);
76 }
77
78 static void print_status(struct boot_status *status,
79         void __attribute__((unused)) *arg)
80 {
81         printf("status:\n");
82         printf("\ttype:     %d\n", status->type);
83         printf("\tmessage:  %s\n", status->message);
84         printf("\tdetail:   %s\n", status->detail);
85         printf("\tprogress: %d\n", status->progress);
86
87 }
88
89 static void print_sysinfo(struct system_info *sysinfo,
90         void __attribute__((unused)) *arg)
91 {
92         unsigned int i;
93
94         printf("sysinfo:\n");
95         printf("\ttype:     %s\n", sysinfo->type);
96         printf("\tid:       %s\n", sysinfo->identifier);
97
98         if (sysinfo->n_interfaces == 0)
99                 printf("\tno interfaces.\n");
100         else
101                 printf("\tinterfaces:\n");
102
103         for (i = 0; i < sysinfo->n_interfaces; i++) {
104                 struct interface_info *if_info = sysinfo->interfaces[i];
105                 uint8_t *m = if_info->hwaddr;
106
107                 printf("\t\tname:   %s\n", if_info->name);
108
109                 if (if_info->hwaddr_size == 6)
110                         printf("\t\tmac:    %02x:%02x:%02x:%02x:%02x:%02x\n",
111                                         m[0], m[1], m[2], m[3], m[4], m[5]);
112                 else
113                         printf("\t\tmac:    unknown hwaddr size %d\n",
114                                         if_info->hwaddr_size);
115         }
116 }
117
118 static struct discover_client_ops client_ops = {
119         .device_add = print_device_add,
120         .boot_option_add = print_boot_option_add,
121         .device_remove = print_device_remove,
122         .update_status = print_status,
123         .update_sysinfo = print_sysinfo,
124 };
125
126 int main(void)
127 {
128         struct discover_client *client;
129         struct waitset *waitset;
130
131         waitset = waitset_create(NULL);
132
133         client = discover_client_init(waitset, &client_ops, NULL);
134         if (!client)
135                 return -1;
136
137         for (;;) {
138                 int rc;
139
140                 rc = waiter_poll(waitset);
141                 if (rc)
142                         break;
143         }
144
145         return 0;
146 }