1 #include "devices.h"
2
3 #include "debug.h"
4
5 #ifdef ENABLE_MULTIDEVICE
6
realloc_devices_list(uint16_t new_size)7 static bool realloc_devices_list(uint16_t new_size) {
8 if (new_size == 0) {
9 if (devices) {
10 free(devices);
11 devices = NULL;
12 }
13 return 0;
14 }
15
16 if (new_size == self.device_list_size) {
17 return 1;
18 }
19
20 UTOX_DEVICE *tmp = realloc(devices, sizeof(UTOX_DEVICE) * new_size);
21 if (!tmp) {
22 LOG_ERR("Devices", "couldn't realloc for new_size %u" , new_size);
23 return 0;
24 }
25
26 devices = tmp;
27 self.device_list_size = new_size;
28 return 1;
29 }
30
utox_devices_init(void)31 void utox_devices_init(void) {
32 if (devices) {
33 LOG_FATAL_ERR(EXIT_FAILURE, "Devices", "Unable to init base devices, *devices exists");
34 }
35
36 devices = calloc(self.device_list_count, sizeof(UTOX_DEVICE));
37 self.device_list_size = self.device_list_count;
38
39 if (!devices) {
40 LOG_FATAL_ERR(EXIT_MALLOC, "Devices", "Unable to init base devices, *devices is null");
41 }
42 };
43
utox_devices_decon(void)44 void utox_devices_decon(void) {
45 if (devices) {
46 free(devices);
47 devices = NULL;
48 }
49 }
50
utox_device_init(Tox * tox,uint16_t dev_num)51 void utox_device_init(Tox *tox, uint16_t dev_num) {
52 if (dev_num >= self.device_list_size) {
53 if (!realloc_devices_list(dev_num + 1)) {
54 LOG_ERR("Devices", "ERROR, unable to realloc for a new device");
55 return;
56 }
57 }
58
59 if (!devices) {
60 LOG_ERR("Devices", "devices is null");
61 return;
62 }
63
64 TOX_ERR_DEVICE_GET error = 0;
65
66 tox_self_get_device(tox, dev_num, devices[dev_num].name, &devices[dev_num].status, devices[dev_num].pubkey, &error);
67
68 if (error) {
69 LOG_ERR("Devices", "Error getting device info dev_num %u error %u" , dev_num, error);
70 }
71
72 cid_to_string(devices[dev_num].pubkey_hex, devices[dev_num].pubkey);
73 }
74
devices_self_add_submit(uint8_t * name,size_t length,uint8_t id[TOX_ADDRESS_SIZE])75 static void devices_self_add_submit(uint8_t *name, size_t length, uint8_t id[TOX_ADDRESS_SIZE]) {
76 if (length >= UINT16_MAX) { /* Max size of postmsg */
77 LOG_ERR("Devices", "Name length > UINT16_MAX");
78 /* TODO send error to GUI */
79 return;
80 }
81
82 uint8_t *data = malloc(length * sizeof(uint8_t) + sizeof(id[0]) * TOX_ADDRESS_SIZE);
83 if (!data) {
84 LOG_ERR("Devices", "Could not alloc space (%uB) for new device (%.s)",
85 length * sizeof(uint8_t) + sizeof(id[0]) * TOX_ADDRESS_SIZE,
86 length, name);
87 return;
88 }
89
90 memcpy(data, id, TOX_ADDRESS_SIZE);
91 memcpy(data + TOX_ADDRESS_SIZE, name, length * sizeof(uint8_t));
92
93 postmessage_toxcore(TOX_SELF_NEW_DEVICE, length, 0, data);
94 }
95
delete_this_device(void)96 static void delete_this_device(void) { LOG_ERR("Devices", "Delete button pressed"); }
97
devices_update_list(void)98 void devices_update_list(void) {}
99
devices_update_ui(void)100 void devices_update_ui(void) {
101 if (!devices) {
102 panel_settings_devices.child = calloc(3, sizeof(void *));
103 if (!panel_settings_devices.child) {
104 LOG_FATAL_ERR(EXIT_MALLOC, "Devices", "Could not alloc memory");
105 }
106 panel_settings_devices.child[0] = (void *)&button_add_new_device_to_self;
107 panel_settings_devices.child[1] = (void *)&edit_add_new_device_to_self;
108 panel_settings_devices.child[2] = NULL;
109 return;
110 }
111
112 if (!panel_settings_devices.child) {
113 panel_settings_devices.child = calloc(3 + self.device_list_count * 2, sizeof(void *));
114 if (!panel_settings_devices.child) {
115 LOG_FATAL_ERR(EXIT_MALLOC, "Devices", "Could not alloc memory");
116 }
117 panel_settings_devices.child[0] = (void *)&button_add_new_device_to_self;
118 panel_settings_devices.child[1] = (void *)&edit_add_new_device_to_self;
119 } else {
120 panel_settings_devices.child =
121 realloc(panel_settings_devices.child, (3 + self.device_list_count * 2) * sizeof(void *));
122 if (!panel_settings_devices.child) {
123 LOG_FATAL_ERR(EXIT_MALLOC, "Devices", "Could not alloc memory");
124 }
125 }
126
127 uint16_t i;
128 for (i = 0; i < self.device_list_count; ++i) {
129 EDIT * edit = calloc(1, sizeof(EDIT));
130 BUTTON *dele = calloc(1, sizeof(BUTTON));
131
132 if (!edit || !dele) {
133 LOG_FATAL_ERR(EXIT_MALLOC, "Devices", "Can't malloc for an extra device");
134 }
135
136 PANEL p_edit =
137 {
138 .type = PANEL_EDIT,
139 .x = SCALE(10),
140 .y = SCALE(95) + (i * SCALE(27)),
141 .width = SCALE(-25) - BM_SBUTTON_WIDTH,
142 .height = SCALE(24),
143 },
144
145 b_delete = {
146 .type = PANEL_BUTTON,
147 .x = SCALE(-10) - BM_SBUTTON_WIDTH,
148 .y = SCALE(95) + (i * SCALE(29)),
149 .width = BM_SBUTTON_WIDTH,
150 .height = BM_SBUTTON_HEIGHT,
151 };
152
153 edit->panel = p_edit;
154 edit->length = TOX_PUBLIC_KEY_SIZE * 2,
155 edit->data_size = TOX_PUBLIC_KEY_SIZE * 2,
156 edit->data = devices[i].pubkey_hex, edit->readonly = 1, edit->noborder = 0, edit->select_completely = 1,
157
158 dele->panel = b_delete;
159 dele->bm_fill = BM_SBUTTON;
160 dele->update = button_setcolors_success, dele->on_mup = delete_this_device,
161 dele->button_text.i18nal = STR_DELETE;
162
163 panel_settings_devices.child[(i * 2) + 2] = (void *)edit;
164 panel_settings_devices.child[(i * 2) + 3] = (void *)dele;
165 }
166 panel_settings_devices.child[(i * 2) + 2] = NULL;
167 }
168
devices_self_add(uint8_t * device,size_t length)169 void devices_self_add(uint8_t *device, size_t length) {
170 uint8_t name_cleaned[length];
171 uint16_t length_cleaned = 0;
172
173 unsigned int i;
174 for (i = 0; i < length; ++i) {
175 if (device[i] != ' ') {
176 name_cleaned[length_cleaned] = device[i];
177 ++length_cleaned;
178 }
179 }
180
181 if (!length_cleaned) {
182 addfriend_status = ADDF_NONAME;
183 return;
184 }
185
186 uint8_t id[TOX_ADDRESS_SIZE];
187
188 if (length_cleaned == TOX_ADDRESS_SIZE * 2 && string_to_id(id, name_cleaned)) {
189 /* TODO, names! */
190 devices_self_add_submit((uint8_t *)"Default device name", 19, id);
191 } else {
192 LOG_ERR("Devices", "error trying to add this device");
193 }
194 }
195
196 #endif
197