2 * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/socket.h>
30 #include "thread-menu.h"
31 #include "thread-type.h"
32 #include "thread-log.h"
33 #include "thread-util.h"
35 static char g_name[NETWORK_DATA_SIZE + 1] = {"Thread-test"};
36 static char g_key[NETWORK_DATA_SIZE + 1];
37 static char g_pskc[NETWORK_DATA_SIZE + 1];
38 static char g_str_channel[NETWORK_DATA_SIZE + 1] = {"07fff800"};
39 static char g_str_extended_panid[NETWORK_DATA_SIZE + 1] = {"18446744073709551615"}; /* UINT64_MAX */
40 static char g_str_panid[NETWORK_DATA_SIZE + 1] = {"65535"}; /* UINT16_MAX */
41 static char g_str_buf_length[NETWORK_DATA_SIZE + 1];
42 static char g_str_tlvs_buffer[2*NETWORK_DATA_SIZE + 1];
43 static thread_network_h g_network;
45 static char g_str_ipaddr_type[NETWORK_DATA_SIZE + 1] = "0";
46 static char g_str_ipaddr[NETWORK_DATA_SIZE + 1];
48 static uint8_t g_ipv6_address[THREAD_IPV6_ADDRESS_SIZE];
50 static int run_thread_network_destroy_operational_network(MManager *mm, struct menu_data *menu)
53 thread_instance_h g_instance = mm->t_instance;
54 if (g_instance == NULL)
57 int ret = thread_unset_operational_network(g_instance);
58 if (ret == THREAD_ERROR_NONE)
59 msg("thread_unset_operational_network success");
61 msg("thread_unset_operational_network failed");
63 ret = thread_network_destroy(g_network);
64 if (ret == THREAD_ERROR_NONE)
65 msg("thread_network_destroy success");
67 msg("thread_network_destroy failed");
73 static int run_thread_network_create_operational_network(MManager *mm, struct menu_data *menu)
77 uint64_t extended_panid = 0;
79 thread_instance_h g_instance = mm->t_instance;
80 if (g_instance == NULL)
83 sscanf(g_str_channel, "%x", &channel);
84 sscanf(g_str_extended_panid, "%zu", (size_t *)&extended_panid);
85 sscanf(g_str_panid, "%hu", &panid);
87 int ret = thread_network_create(&g_network);
88 if (ret == THREAD_ERROR_NONE)
89 msg("thread_network_create success");
91 msg("thread_network_create failed");
93 ret = thread_network_set_name(g_network, g_name);
94 if (ret == THREAD_ERROR_NONE)
95 msg("thread_network_set_name success");
97 msg("thread_network_set_name failed");
99 ret = thread_network_set_key(g_network, g_key);
100 if (ret == THREAD_ERROR_NONE)
101 msg("thread_network_set_key success");
103 msg("thread_network_set_key failed");
105 ret = thread_network_set_pskc(g_network, g_pskc);
106 if (ret == THREAD_ERROR_NONE)
107 msg("thread_network_set_pskc success");
109 msg("thread_network_set_pskc failed");
111 ret = thread_network_set_channel(g_network, channel);
112 if (ret == THREAD_ERROR_NONE)
113 msg("thread_network_set_channel success");
115 msg("thread_network_set_channel failed");
117 ret = thread_network_set_extended_panid(g_network, extended_panid);
118 if (ret == THREAD_ERROR_NONE)
119 msg("thread_network_set_extended_panid success");
121 msg("thread_network_set_extended_panid failed");
123 ret = thread_network_set_panid(g_network, panid);
124 if (ret == THREAD_ERROR_NONE)
125 msg("thread_network_set_panid success");
127 msg("thread_network_set_panid failed");
129 ret = thread_set_operational_network(g_instance, g_network);
130 if (ret == THREAD_ERROR_NONE)
131 msg("thread_set_operational_network success");
133 msg("thread_set_operational_network failed");
139 static int run_thread_network_set_active_dataset_tlvs(MManager *mm, struct menu_data *menu)
142 thread_instance_h g_instance = mm->t_instance;
143 if (g_instance == NULL)
147 uint8_t tlvs_buffer[1024];
149 sscanf(g_str_buf_length, "%d", &buf_length);
150 msg("Set active dataset tlvs size: %d :: %s",
151 buf_length, g_str_tlvs_buffer);
153 if (!buf_length || buf_length > (MENU_DATA_SIZE + 1)) {
154 msg("Invalid Input");
157 for (int i = 0; i < buf_length; i++) {
159 memcpy(subbuff, &g_str_tlvs_buffer[2*i], 2);
161 tlvs_buffer[i] = (uint8_t)strtol(subbuff, NULL, 16);
164 int ret = thread_network_set_active_dataset_tlvs(
165 g_instance, tlvs_buffer, buf_length);
166 if (ret == THREAD_ERROR_NONE)
167 msg("thread_network_set_active_dataset_tlvs success");
169 msg("thread_network_set_active_dataset_tlvs failed");
175 static int run_thread_network_get_active_dataset_tlvs(MManager *mm, struct menu_data *menu)
179 uint8_t *tlvs_buffer = NULL;
183 thread_instance_h g_instance = mm->t_instance;
184 if (g_instance == NULL)
187 int ret = thread_network_get_active_dataset_tlvs(g_instance, &tlvs_buffer, &buf_length);
188 if (ret == THREAD_ERROR_NONE) {
189 msg("thread_network_get_active_dataset_tlvs success");
191 for (int i = 0; i < buf_length; i++)
192 snprintf(buf + i*2, 3, "%2.2x", tlvs_buffer[i]);
194 msg("Active dataset tlvs size: %d :: %s", buf_length, buf);
196 msg("thread_network_get_active_dataset_tlvs failed");
206 static int run_thread_network_attach(MManager *mm, struct menu_data *menu)
209 thread_instance_h g_instance = mm->t_instance;
210 if (g_instance == NULL)
213 int ret = thread_network_attach(g_instance);
214 if (ret == THREAD_ERROR_NONE)
215 msg("thread_network_attach success");
217 msg("thread_network_attach failed");
223 static int run_thread_network_detach(MManager *mm, struct menu_data *menu)
226 thread_instance_h g_instance = mm->t_instance;
227 if (g_instance == NULL)
230 int ret = thread_network_detach(g_instance);
231 if (ret == THREAD_ERROR_NONE)
232 msg("thread_network_detach success");
234 msg("thread_network_detach failed");
240 static void __get_ipaddr_callback(int index, char* ipaddr,
241 thread_ipaddr_type_e ipaddr_type, void *user_data) {
245 msg("IP address type: %s", _thread_ipaddr_type_to_string(ipaddr_type));
246 msg("IP address count: %d, ipv6 address: %s", index, ipaddr);
251 static int run_thread_get_ipaddr(MManager *mm, struct menu_data *menu)
254 thread_instance_h g_instance = mm->t_instance;
255 if (g_instance == NULL)
258 int type = atoi(g_str_ipaddr_type);
259 thread_ipaddr_type_e ipaddr_type;
262 ipaddr_type = THREAD_IPADDR_TYPE_ALL;
265 ipaddr_type = THREAD_IPADDR_TYPE_LINK_LOCAL;
268 ipaddr_type = THREAD_IPADDR_TYPE_RLOC;
271 ipaddr_type = THREAD_IPADDR_TYPE_MLEID;
277 int ret = thread_get_ipaddr(g_instance, __get_ipaddr_callback, ipaddr_type, NULL);
278 if (ret == THREAD_ERROR_NONE)
279 msg("thread_get_ipaddr success");
281 msg("thread_get_ipaddr failed");
287 static int run_thread_add_ipaddr(MManager *mm, struct menu_data *menu)
290 thread_instance_h g_instance = mm->t_instance;
291 if (g_instance == NULL)
294 int ret = _preprocess_ipv6_prefix(g_str_ipaddr, g_ipv6_address);
295 retv_if(ret != THREAD_ERROR_NONE, ret);
297 ret = thread_add_ipaddr(g_instance, g_ipv6_address);
298 if (ret == THREAD_ERROR_NONE)
299 msg("thread_add_ipaddr success");
301 msg("thread_add_ipaddr failed");
307 static int run_thread_remove_ipaddr(MManager *mm, struct menu_data *menu)
310 thread_instance_h g_instance = mm->t_instance;
311 if (g_instance == NULL)
314 int ret = _preprocess_ipv6_prefix(g_str_ipaddr, g_ipv6_address);
315 retv_if(ret != THREAD_ERROR_NONE, ret);
317 ret = thread_remove_ipaddr(g_instance, g_ipv6_address);
318 if (ret == THREAD_ERROR_NONE)
319 msg("thread_remove_ipaddr success");
321 msg("thread_remove_ipaddr failed");
327 static struct menu_data menu_thread_network_set_active_dataset_tlvs[] = {
328 { "1", "Tlvs_buffer len", NULL, NULL, g_str_buf_length},
329 { "2", "Tlvs_buffer", NULL, NULL, g_str_tlvs_buffer},
330 { "3", "run", NULL, run_thread_network_set_active_dataset_tlvs, NULL},
334 static struct menu_data menu_thread_network_create_operational_network[] = {
335 { "1", "Network Name", NULL, NULL, g_name},
336 { "2", "Network Key", NULL, NULL, g_key},
337 { "3", "Network pskc", NULL, NULL, g_pskc},
338 { "4", "Network Channel", NULL, NULL, g_str_channel},
339 { "5", "Network Extended Panid", NULL, NULL, g_str_extended_panid},
340 { "6", "Network panid", NULL, NULL, g_str_panid},
341 { "7", "run", NULL, run_thread_network_create_operational_network, NULL},
345 static struct menu_data menu_thread_get_ipaddr[] = {
346 { "1", "Node Addr Type: ALL > 0, LINK LOCAL > 1, RLOC > 2, MLEID > 3",
347 NULL, NULL, g_str_ipaddr_type},
348 { "2", "run", NULL, run_thread_get_ipaddr, NULL},
352 static struct menu_data menu_thread_add_ipaddr[] = {
353 { "1", "Type IPv6 address", NULL, NULL, g_str_ipaddr},
354 { "2", "run", NULL, run_thread_add_ipaddr, NULL},
358 static struct menu_data menu_thread_remove_ipaddr[] = {
359 { "1", "Type IPv6 address", NULL, NULL, g_str_ipaddr},
360 { "2", "run", NULL, run_thread_remove_ipaddr, NULL},
364 struct menu_data menu_thread_network[] = {
365 { "1", "thread_network_create_operational_network",
366 menu_thread_network_create_operational_network, NULL, NULL},
367 { "2", "thread_network_destroy_operational_network",
368 NULL, run_thread_network_destroy_operational_network, NULL },
369 { "3", "thread_network_set_active_dataset_tlvs",
370 menu_thread_network_set_active_dataset_tlvs, NULL, NULL},
371 { "4", "thread_network_get_active_dataset_tlvs",
372 NULL, run_thread_network_get_active_dataset_tlvs, NULL },
373 { "5", "thread_network_attach",
374 NULL, run_thread_network_attach, NULL},
375 { "6", "thread_network_detach",
376 NULL, run_thread_network_detach, NULL},
377 { "7", "thread_get_ipaddr",
378 menu_thread_get_ipaddr, NULL, NULL},
379 { "8", "thread_add_ipaddr",
380 menu_thread_add_ipaddr, NULL, NULL},
381 { "9", "thread_remove_ipaddr",
382 menu_thread_remove_ipaddr, NULL, NULL},