--- /dev/null
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/time.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include "thread.h"
+#include "thread-menu.h"
+#include "thread-type.h"
+#include "thread-log.h"
+#include "thread-util.h"
+
+static char g_name[MENU_DATA_SIZE + 1] = {"Thread-test"};
+static char g_key[MENU_DATA_SIZE + 1];
+static char g_pskc[MENU_DATA_SIZE + 1];
+static char g_str_channel[MENU_DATA_SIZE + 1] = {"07fff800"};
+static char g_str_extended_panid[MENU_DATA_SIZE + 1] = {"18446744073709551615"}; /* UINT64_MAX */
+static char g_str_panid[MENU_DATA_SIZE + 1] = {"65535"}; /* UINT16_MAX */
+static thread_network_h g_network;
+
+static char g_joiner_uuid[MENU_DATA_SIZE + 1];
+static char g_joiner_passphrase[MENU_DATA_SIZE + 1];
+
+uint32_t g_channel;
+uint64_t g_extended_panid;
+uint16_t g_panid;
+
+static char g_prov_url[MENU_DATA_SIZE + 1];
+static char g_vendor_name[MENU_DATA_SIZE + 1];
+static char g_vendor_model[MENU_DATA_SIZE + 1];
+static char g_vendor_sw_version[MENU_DATA_SIZE + 1];
+static char g_vendor_data[MENU_DATA_SIZE + 1];
+
+void _thread_joiner_start_callback(int result, void* user_data)
+{
+ msg("Thread joiner start callback, result: %s",
+ _thread_get_error_message(result));
+
+ return;
+}
+
+static int run_thread_scenario_startup(MManager *mm, struct menu_data *menu)
+{
+ FUNC_ENTRY;
+ int ret = THREAD_ERROR_NONE;
+ msg(" ### thread_scenario_startup ###");
+
+ thread_instance_h *g_instance = &(mm->t_instance);
+
+ /* Initialize thread */
+ ret = thread_initialize();
+ msg(" - thread_initialize() ret: [0x%X] [%s]", ret, _thread_get_error_message(ret));
+ retv_if(!(ret == THREAD_ERROR_NONE || ret == THREAD_ERROR_ALREADY_DONE), RET_SUCCESS);
+
+ /* thread enable */
+ ret = thread_enable(g_instance);
+ msg(" - thread_enable() ret: [0x%X] [%s]", ret, _thread_get_error_message(ret));
+ retv_if(!(ret == THREAD_ERROR_NONE || ret == THREAD_ERROR_ALREADY_DONE
+ || ret == THREAD_ERROR_ALREADY_REGISTERED), RET_SUCCESS);
+
+ FUNC_EXIT;
+ return RET_SUCCESS;
+}
+
+static int run_thread_scenario_cleanup(MManager *mm, struct menu_data *menu)
+{
+ FUNC_ENTRY;
+ int ret = THREAD_ERROR_NONE;
+ msg(" ### thread_scenario_cleanup ###");
+
+ thread_instance_h *g_instance = &(mm->t_instance);
+
+ /* thread disable */
+ ret = thread_disable(g_instance);
+ msg(" - thread_disable() ret: [0x%X] [%s]", ret, _thread_get_error_message(ret));
+ retv_if(!(ret == THREAD_ERROR_NONE || ret == THREAD_ERROR_ALREADY_DONE
+ || ret == THREAD_ERROR_ALREADY_REGISTERED), RET_SUCCESS);
+
+ /* De-initialize thread */
+ ret = thread_deinitialize();
+ msg(" - thread_deinitialize() ret: [0x%X] [%s]", ret, _thread_get_error_message(ret));
+ retv_if(!(ret == THREAD_ERROR_NONE || ret == THREAD_ERROR_ALREADY_DONE), RET_SUCCESS);
+
+ FUNC_EXIT;
+ return RET_SUCCESS;
+}
+static int run_thread_joiner_join_network(MManager *mm, struct menu_data *menu)
+{
+ FUNC_ENTRY;
+ thread_instance_h g_instance = mm->t_instance;
+ if (g_instance == NULL)
+ goto OUT;
+ int ret = THREAD_ERROR_NONE;
+
+ /*factoryreset */
+ ret = thread_factoryreset(g_instance);
+ if (ret != THREAD_ERROR_NONE) {
+ msg("Failed: %s", _thread_get_error_message(ret));
+ goto OUT;
+ }
+
+ /*thread ifconfig up */
+ ret = thread_ifconfig_up(g_instance);
+ if (ret != THREAD_ERROR_NONE) {
+ msg("Failed: %s", _thread_get_error_message(ret));
+ goto OUT;
+ }
+
+ /*Joiner start*/
+ ret = thread_joiner_start(g_instance, g_joiner_passphrase, g_prov_url,
+ g_vendor_name, g_vendor_model, g_vendor_sw_version,
+ g_vendor_data, _thread_joiner_start_callback, NULL);
+ if (ret != THREAD_ERROR_NONE) {
+ msg("Failed: %s", _thread_get_error_message(ret));
+ goto OUT;
+ }
+
+ msg("thread_joiner_join_network success");
+OUT:
+ FUNC_EXIT;
+ return RET_SUCCESS;
+}
+
+static int run_thread_start_leader_as_commissioner(MManager *mm, struct menu_data *menu)
+{
+ FUNC_ENTRY;
+ thread_instance_h g_instance = mm->t_instance;
+ if (g_instance == NULL)
+ goto OUT;
+
+ sscanf(g_str_channel, "%x", &g_channel);
+ sscanf(g_str_extended_panid, "%llu", &g_extended_panid);
+ sscanf(g_str_panid, "%hu", &g_panid);
+
+ int ret = THREAD_ERROR_NONE;
+
+ /*factoryreset */
+ ret = thread_factoryreset(g_instance);
+ if (ret != THREAD_ERROR_NONE) {
+ msg("Failed: %s", _thread_get_error_message(ret));
+ goto OUT;
+ }
+
+ /* create operational_network */
+ ret = thread_network_create_operational_network(g_instance, g_name,
+ g_key, g_pskc, g_channel, g_extended_panid,
+ g_panid, &g_network);
+ if (ret != THREAD_ERROR_NONE) {
+ msg("Failed: %s", _thread_get_error_message(ret));
+ goto OUT;
+ }
+
+ /*attach operational network */
+ ret = thread_network_attach(g_instance);
+ if (ret != THREAD_ERROR_NONE) {
+ msg("Failed: %s", _thread_get_error_message(ret));
+ goto OUT;
+ }
+
+ /*thread ifconfig up */
+ ret = thread_ifconfig_up(g_instance);
+ if (ret != THREAD_ERROR_NONE) {
+ msg("Failed: %s", _thread_get_error_message(ret));
+ goto OUT;
+ }
+
+ /* thread commissioner start*/
+ ret = thread_commissioner_start(g_instance);
+ if (ret != THREAD_ERROR_NONE) {
+ msg("Failed: %s", _thread_get_error_message(ret));
+ goto OUT;
+ }
+
+ /* thread commissioner set commissioning data*/
+ ret = thread_commissioner_set_commisioning_data(g_instance, g_joiner_uuid,
+ g_joiner_passphrase);
+ if (ret != THREAD_ERROR_NONE) {
+ msg("Failed: %s", _thread_get_error_message(ret));
+ goto OUT;
+ }
+
+ msg("thread_commissioner_set_commisioning_data success");
+OUT:
+ FUNC_EXIT;
+ return RET_SUCCESS;
+}
+
+static struct menu_data menu_thread_joiner_join_network[] = {
+ { "1", "Type Joiner Passphrase", NULL, NULL, g_joiner_passphrase},
+ { "2", "run", NULL, run_thread_joiner_join_network, NULL},
+ { NULL, NULL, },
+};
+
+static struct menu_data menu_thread_start_leader_as_commissioner[] = {
+ { "1", "Joiner UUID", NULL, NULL, g_joiner_uuid},
+ { "2", "Joiner Passphrase", NULL, NULL, g_joiner_passphrase},
+ { "3", "run", NULL, run_thread_start_leader_as_commissioner, NULL},
+ { NULL, NULL, },
+};
+
+struct menu_data menu_thread_scenarios[] = {
+ { "1", "thread_scenario_startup",
+ NULL, run_thread_scenario_startup, NULL },
+ { "2", "thread_scenario_cleanup",
+ NULL, run_thread_scenario_cleanup, NULL },
+ { "3", "thread_start_leader_as_commissioner",
+ menu_thread_start_leader_as_commissioner, NULL, NULL},
+ { "4", "thread_joiner_join_network",
+ menu_thread_joiner_join_network, NULL, NULL },
+ { NULL, NULL, },
+};