Add getting global ipaddr function 95/281195/3 accepted/tizen_7.0_unified_hotfix accepted/tizen_unified_riscv tizen_7.0_hotfix accepted/tizen/7.0/unified/20221110.062459 accepted/tizen/7.0/unified/hotfix/20221116.104849 accepted/tizen/unified/20220919.090142 accepted/tizen/unified/riscv/20230724.093720 tizen_7.0_m2_release
authorWootak Jung <wootak.jung@samsung.com>
Wed, 14 Sep 2022 06:36:08 +0000 (15:36 +0900)
committerWootak Jung <wootak.jung@samsung.com>
Fri, 16 Sep 2022 00:07:03 +0000 (09:07 +0900)
Change-Id: I0813aa9e6f757966ed78da1e1a134d2cf4e15a24

include/thread-socket-handler.h
include/thread.h
src/thread-network.c
tests/thread-test/thread-network.c
tests/unittest/thread-unittest-network.cpp
tests/unittest/thread-unittest-srp.cpp

index 97ef619..93866eb 100644 (file)
@@ -46,6 +46,7 @@ extern "C" {
 #define THREAD_SRP_CLIENT_REMOVE_HOST_CMD "srp client host remove"
 #define THREAD_SRP_CLIENT_STOP_CMD "srp client stop"
 #define THREAD_SRP_CLIENT_START_CMD "srp client autostart enable"
+#define THREAD_IPADDR_V "ipaddr -v"
 
 int _thread_socket_client_init(int *session_fd, const char *if_name);
 
index 62c4deb..32b5ddd 100644 (file)
@@ -1589,6 +1589,23 @@ int thread_get_ipaddr(thread_instance_h instance, thread_ipaddr_foreach_cb callb
 
 /**
  * @ingroup CAPI_NETWORK_THREAD_SRP_MODULE
+ * @brief Get node global ip address of the thread network 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_get_global_ipaddr(thread_instance_h instance, const char **ipaddr);
+
+/**
+ * @ingroup CAPI_NETWORK_THREAD_SRP_MODULE
  * @brief Up the netwotk interface for the thread node.
  * @since_tizen 7.0
  *
index 8ff70c8..2832d0b 100644 (file)
@@ -689,3 +689,53 @@ int thread_remove_ipaddr(thread_instance_h instance, const uint8_t *ipv6_address
        return ret;
 }
 
+int thread_get_global_ipaddr(thread_instance_h instance, const char **ipaddr)
+{
+       FUNC_ENTRY;
+       THREAD_CHECK_SUPPORTED_FEATURE(THREAD_FEATURE_COMMON);
+       THREAD_CHECK_INIT_STATUS();
+       THREAD_VALIDATE_INPUT_PARAMETER(instance);
+
+       int ret = THREAD_ERROR_NONE;
+       char buffer[THREAD_MAX_BUFFER_SIZE];
+       int session_fd = _thread_get_socket_fd();
+       const char *msg = THREAD_IPADDR_V;
+       char *ret_ptr, *next_ptr;
+
+       ret = _thread_socket_client_write(session_fd, msg, strlen(msg));
+       if (ret != THREAD_ERROR_NONE) {
+               THREAD_DBG("Failed to execute command %s", msg);
+               return ret;
+       }
+       THREAD_DBG("Executed command '%s' with size %zu", msg, strlen(msg));
+
+       /* Check response */
+       ret = _thread_socket_client_read(session_fd, buffer);
+       if (ret != THREAD_ERROR_NONE && ret != THREAD_ERROR_ALREADY_DONE) {
+               THREAD_DBG("Socket response failed..");
+               return ret;
+       }
+
+       /* result is like below:
+        * > ipaddr -v
+        * fdd6:54c2:ee47:f2d2:0:ff:fe00:9400 origin:thread
+        * fd0b:8d9c:ac8c:1:2c82:12dd:825b:70c4 origin:slaac
+        * fdd6:54c2:ee47:f2d2:b267:e8fa:5bd6:2891 origin:thread
+        * fe80:0:0:0:5857:6a63:d5f1:91b6 origin:thread
+        * Done
+        */
+       ret_ptr = strtok_r(buffer, "\n", &next_ptr);
+       while (ret_ptr) {
+               if (strstr(ret_ptr, "origin:slaac")) {
+                       strtok_r(ret_ptr, " ", &next_ptr);
+                       THREAD_DBG("Global Ipaddr: %s", ret_ptr);
+                       *ipaddr = g_strdup(ret_ptr);
+                       return THREAD_ERROR_NONE;
+               }
+               ret_ptr = strtok_r(NULL, "\n", &next_ptr);
+       }
+
+       FUNC_EXIT;
+       return ret;
+}
+
index a0f1228..014844e 100644 (file)
@@ -324,6 +324,26 @@ OUT:
        return RET_SUCCESS;
 }
 
+static int run_thread_get_global_ipaddr(MManager *mm, struct menu_data *menu)
+{
+       FUNC_ENTRY;
+       thread_instance_h g_instance = mm->t_instance;
+       const char *ipaddr;
+       if (g_instance == NULL)
+               goto OUT;
+
+       int ret = thread_get_global_ipaddr(g_instance, &ipaddr);
+       if (ret == THREAD_ERROR_NONE) {
+               msg("thread_get_ipaddr_global success");
+               msg("ipaddr: %s", ipaddr);
+       } else {
+               msg("thread_get_ipaddr_global failed");
+       }
+OUT:
+       FUNC_EXIT;
+       return RET_SUCCESS;
+}
+
 static struct menu_data menu_thread_network_set_active_dataset_tlvs[] = {
        { "1", "Tlvs_buffer len", NULL, NULL, g_str_buf_length},
        { "2", "Tlvs_buffer", NULL, NULL, g_str_tlvs_buffer},
@@ -380,6 +400,8 @@ struct menu_data menu_thread_network[] = {
                menu_thread_add_ipaddr, NULL, NULL},
        { "9", "thread_remove_ipaddr",
                menu_thread_remove_ipaddr, NULL, NULL},
+       { "10", "thread_get_global_ipaddr",
+               NULL, run_thread_get_global_ipaddr, NULL},
        { NULL, NULL, },
 };
 
index 8509c3f..205bcd6 100644 (file)
@@ -50,6 +50,7 @@ public:
        uint8_t tlvsBuffer[NETWORK_TLVS_SIZE];
        int tlvsBufferLength;
        uint8_t ipv6Address[THREAD_IPV6_ADDRESS_SIZE];
+       const char *globalIpaddr;
 
 public:
        static void getIpAddrCallback(int index, char* ipaddr,
@@ -264,6 +265,27 @@ TEST_F(ThreadNetworkTest, RemoveIpAddrErrorNone)
                thread_remove_ipaddr(instance, ipv6Address));
 }
 
+TEST_F(ThreadNetworkTest, GetGlobalIpAddrNotInitialized)
+{
+       EXPECT_EQ(THREAD_ERROR_NONE, thread_deinitialize());
+       EXPECT_EQ(THREAD_ERROR_NOT_INITIALIZED,
+               thread_get_global_ipaddr(instance, &globalIpaddr));
+}
+
+TEST_F(ThreadNetworkTest, GetGlobalIpAddrInvalidParameter)
+{
+       EXPECT_EQ(THREAD_ERROR_INVALID_PARAMETER,
+               thread_get_global_ipaddr(instance, nullptr));
+}
+
+TEST_F(ThreadNetworkTest, GetGlobalIpAddrErrorNone)
+{
+       EXPECT_EQ(THREAD_ERROR_NONE, thread_enable(&instance));
+       EXPECT_EQ(THREAD_ERROR_NONE,
+               thread_get_global_ipaddr(instance, &globalIpaddr));
+       g_free((char *)globalIpaddr);
+}
+
 TEST_F(ThreadNetworkTest, GetPanIdNotInitialized)
 {
        uint16_t panid;
index 991ccc5..23a6447 100644 (file)
@@ -26,7 +26,7 @@ public:
 
        const char *serviceName;
        const char *address;
-       uint64_t port;
+       uint16_t port;
        bool isDeleted;
 
        thread_instance_h instance;