Add a conditional statement to check operational network set 41/273141/1
authorhyunuk.tak <hyunuk.tak@samsung.com>
Wed, 30 Mar 2022 06:00:31 +0000 (15:00 +0900)
committerhyunuk.tak <hyunuk.tak@samsung.com>
Thu, 31 Mar 2022 01:00:41 +0000 (10:00 +0900)
Change-Id: Ifffc42f29ce339dd9425b06e971cbd14e9d4752c
Signed-off-by: hyunuk.tak <hyunuk.tak@samsung.com>
include/thread-private.h
src/thread-network.c
tests/unittest/thread-unittest-network.cpp

index c6ae2e1..ccc9062 100755 (executable)
@@ -195,6 +195,7 @@ typedef struct {
  */
 typedef struct {
        bool is_network_active;
+       bool is_operational_network_set;
        char name[THREAD_NETWORK_NAME_MAX + 1];
        char key[THREAD_NETWORK_KEY_STRING_MAX + 1];
        char pskc[THREAD_NETWORK_PSKC_STRING_MAX + 1];
index da5b674..888fda9 100644 (file)
@@ -68,6 +68,7 @@ int thread_network_create(thread_network_h *network)
        }
 
        new_network->is_network_active = FALSE;
+       new_network->is_operational_network_set = FALSE;
        (void)g_strlcpy(new_network->name,
                THREAD_NETWORK_DEFAULT_NAME, THREAD_NETWORK_NAME_MAX + 1);
        (void)g_strlcpy(new_network->key,
@@ -92,10 +93,15 @@ int thread_network_destroy(thread_network_h network)
 
        thread_network_s *current_network = network;
 
+       if (current_network->is_operational_network_set) {
+               THREAD_DBG("Thread operational network set, can't be destroyed:: \
+                                               first unset the operational network");
+               return THREAD_ERROR_OPERATION_FAILED;
+       }
+
        if (current_network->is_network_active) {
                THREAD_DBG("Thread network active, can't be destroyed:: \
-                                               First Reset the Network");
-               FUNC_EXIT;
+                                               first reset the network");
                return THREAD_ERROR_OPERATION_FAILED;
        }
 
@@ -223,7 +229,9 @@ int thread_set_operational_network(thread_instance_h instance, thread_network_h
        THREAD_VALIDATE_INPUT_PARAMETER(network);
 
        thread_instance_s *current_instance = instance;
+       thread_network_s *current_network = network;
        current_instance->network = network;
+       current_network->is_operational_network_set = TRUE;
 
        FUNC_EXIT;
        return THREAD_ERROR_NONE;
@@ -237,8 +245,21 @@ int thread_unset_operational_network(thread_instance_h instance)
        THREAD_VALIDATE_INPUT_PARAMETER(instance);
 
        thread_instance_s *current_instance = instance;
+       thread_network_s *current_network = current_instance->network;
+
+       if (!current_network)
+               goto exit;
+
+       if (current_network->is_network_active) {
+               THREAD_DBG("Thread network active, can't be destroyed:: \
+                                               first reset the network");
+               return THREAD_ERROR_OPERATION_FAILED;
+       }
+
+       current_network->is_operational_network_set = FALSE;
        current_instance->network = NULL;
 
+exit:
        FUNC_EXIT;
        return THREAD_ERROR_NONE;
 }
index bf2550b..dbe3e14 100644 (file)
@@ -79,8 +79,8 @@ protected:
                {
                        if (network)
                        {
-                               thread_network_destroy(network);
                                thread_unset_operational_network(instance);
+                               thread_network_destroy(network);
                        }
                        thread_disable(instance);
                }
@@ -479,5 +479,7 @@ TEST_F(ThreadNetworkTest, UnsetOperationalNetworkErrorNone)
        EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
        EXPECT_EQ(THREAD_ERROR_NONE, thread_network_create(&network));
        EXPECT_EQ(THREAD_ERROR_NONE,
+               thread_set_operational_network(instance, network));
+       EXPECT_EQ(THREAD_ERROR_NONE,
                thread_unset_operational_network(instance));
 }
\ No newline at end of file