Add thread commissioning scenarios to thread-test 14/271714/3
authorRohit Singh <rohit.singh@samsung.com>
Tue, 15 Feb 2022 17:39:38 +0000 (23:09 +0530)
committerRohit Singh <rohit.singh@samsung.com>
Wed, 2 Mar 2022 06:28:40 +0000 (11:58 +0530)
Change-Id: I9d327ac92bc1f2ed4c8c7e5b93a006aa8b90f01b
Signed-off-by: Rohit Singh <rohit.singh@samsung.com>
include/thread.h
src/thread-core.c
tests/CMakeLists.txt
tests/thread-main.c
tests/thread-network.c
tests/thread-scenarios.c [new file with mode: 0644]

index 86c4772..06b2ce1 100644 (file)
@@ -808,7 +808,7 @@ int thread_srp_server_get_registered_service(thread_instance_h instance, const c
 
 /**
  * @ingroup CAPI_NETWORK_THREAD_SRP_MODULE
- * @brief Get registered service detail from the SRP server
+ * @brief Get node ip address of the thread network node
  * @since_tizen 7.0
  *
  * @return 0 on success, otherwise a negative error value.
@@ -817,12 +817,30 @@ int thread_srp_server_get_registered_service(thread_instance_h instance, const c
  * @retval #THREAD_ERROR_OPERATION_FAILED  Operation failed
  * @retval #THREAD_ERROR_NOT_SUPPORTED  Not supported
  *
- * @pre thread API must be initialized with thread_initialize().
- * @see thread_srp_server_start()
+ * @pre thread API must be initialized with thread_initialize() and
+ * enabled with thread_enable().
+ * @see thread_initialize() and thread_enable()
  */
 int thread_get_ipaddr(thread_instance_h instance, thread_ipaddr_foreach_cb callback,
                thread_ipaddr_type_e ipaddr_type, void *user_data);
 
+/**
+ * @ingroup CAPI_NETWORK_THREAD_SRP_MODULE
+ * @brief Up the netwotk interface for the thread node.
+ * @since_tizen 7.0
+ *
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #THREAD_ERROR_NONE  Successful
+ * @retval #THREAD_ERROR_NOT_INITIALIZED  Not initialized
+ * @retval #THREAD_ERROR_OPERATION_FAILED  Operation failed
+ * @retval #THREAD_ERROR_NOT_SUPPORTED  Not supported
+ *
+ * @pre thread API must be initialized with thread_initialize() and
+ * enabled with thread_enable().
+ * @see thread_initialize() and thread_enable()
+ */
+int thread_ifconfig_up(thread_instance_h instance);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
index e3ebad6..3eb8896 100644 (file)
@@ -183,7 +183,7 @@ int thread_disable(thread_instance_h instance)
        return THREAD_ERROR_NONE;
 }
 
-static int __thread_ifconfig_up(thread_instance_h instance)
+int thread_ifconfig_up(thread_instance_h instance)
 {
        FUNC_ENTRY;
        THREAD_VALIDATE_INPUT_PARAMETER(instance);
@@ -212,7 +212,7 @@ int thread_start(thread_instance_h instance)
        retv_if(current_instance->is_thread_started, THREAD_ERROR_ALREADY_DONE);
 
        /* ifconfig up */
-       ret = __thread_ifconfig_up(instance);
+       ret = thread_ifconfig_up(instance);
        if (ret != THREAD_ERROR_NONE)
                THREAD_DBG("Thread: ifconfig up failed");
 
index 2ea7876..ad99a19 100755 (executable)
@@ -22,6 +22,7 @@ SET(TEST_SRCS
        thread-network.c
        thread-br.c
        thread-srp.c
+       thread-scenarios.c
 )
 
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
index 049d514..1d6204a 100755 (executable)
@@ -37,6 +37,7 @@ extern struct menu_data menu_thread_commissioner[];
 extern struct menu_data menu_thread_network[];
 extern struct menu_data menu_thread_br[];
 extern struct menu_data menu_thread_srp[];
+extern struct menu_data menu_thread_scenarios[];
 
 static struct menu_data menu_main[] = {
        { "1", "Core", menu_thread_core, NULL, NULL},
@@ -45,6 +46,7 @@ static struct menu_data menu_main[] = {
        { "4", "Network", menu_thread_network, NULL, NULL},
        { "5", "Border Router", menu_thread_br, NULL, NULL},
        { "6", "SRP", menu_thread_srp, NULL, NULL},
+       { "7", "Scenarios", menu_thread_scenarios, NULL, NULL},
        { NULL, NULL, },
 };
 
index 2ad3fb3..c7a96da 100644 (file)
@@ -156,6 +156,7 @@ OUT:
        FUNC_EXIT;
        return RET_SUCCESS;
 }
+
 static void __get_ipaddr_callback(int index, char* ipaddr,
                        thread_ipaddr_type_e ipaddr_type, void *user_data) {
        FUNC_ENTRY;
@@ -166,6 +167,7 @@ static void __get_ipaddr_callback(int index, char* ipaddr,
 
        FUNC_EXIT;
 }
+
 static int run_thread_get_ipaddr(MManager *mm, struct menu_data *menu)
 {
        FUNC_ENTRY;
diff --git a/tests/thread-scenarios.c b/tests/thread-scenarios.c
new file mode 100644 (file)
index 0000000..116e627
--- /dev/null
@@ -0,0 +1,233 @@
+/*
+ * 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, },
+};