Add support to add and remove ipaddr for thread node 15/271715/5
authorRohit Singh <rohit.singh@samsung.com>
Thu, 24 Feb 2022 08:31:46 +0000 (14:01 +0530)
committerRohit Singh <rohit.singh@samsung.com>
Wed, 2 Mar 2022 06:28:55 +0000 (11:58 +0530)
This patch adds support to add and remove ipv6 address for the thread network:
- thread_add_ipaddr
- thread_remove_ipaddr

Change-Id: Ie12902e3f707938af7b856258bee8a5bd67cce99
Signed-off-by: Rohit Singh <rohit.singh@samsung.com>
include/thread.h
src/thread-network.c
tests/thread-network.c

index 06b2ce154246af78415b148a57113230dee7f62a..f430ea6e9eef5cb1544899ebf98fa986821dc033 100644 (file)
@@ -841,6 +841,44 @@ int thread_get_ipaddr(thread_instance_h instance, thread_ipaddr_foreach_cb callb
  */
 int thread_ifconfig_up(thread_instance_h instance);
 
+/**
+ * @ingroup CAPI_NETWORK_THREAD_SRP_MODULE
+ * @brief Add Ipv6 address to the thread node.
+ * @since_tizen 7.0
+ *
+ * @param[in] ipv6_address Byte list
+ *
+ * @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_add_ipaddr(thread_instance_h instance, const uint8_t *ipv6_address);
+
+/**
+ * @ingroup CAPI_NETWORK_THREAD_SRP_MODULE
+ * @brief Remove Ipv6 address from the thread node.
+ * @since_tizen 7.0
+ *
+ * @param[in] ipv6_address Byte list
+ *
+ * @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_remove_ipaddr(thread_instance_h instance, const uint8_t *ipv6_address);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
index 5cec98c43e2550b8b8e169247c71492bb9d0f767..b48e1952da67cbf158d75aa1403d86933c328ce8 100644 (file)
@@ -412,3 +412,83 @@ int thread_get_ipaddr(thread_instance_h instance, thread_ipaddr_foreach_cb callb
        FUNC_EXIT;
        return ret;
 }
+
+static bool __is_valid_ipv6(const uint8_t* ipv6_address)
+{
+       FUNC_ENTRY;
+
+       int index = 0;
+       while (index < THREAD_IPV6_ADDRESS_SIZE) {
+               char buffer[THREAD_NETWORK_BUFFER_MAX];
+               snprintf(buffer, THREAD_NETWORK_BUFFER_MAX, "%02x", ipv6_address[index]);
+               if ((buffer[0] < '0' || buffer[0] > '9') &&
+                               (buffer[0] < 'a' ||  buffer[0] > 'f'))
+                       return FALSE;
+               if ((buffer[1] < '0' || buffer[1] > '9') &&
+                               (buffer[1] < 'a' ||  buffer[1] > 'f'))
+                       return FALSE;
+
+               index++;
+       }
+       THREAD_DBG("DEBUG: NETWORK MESSAGE -> return true");
+
+       FUNC_EXIT;
+       return TRUE;
+}
+
+int thread_add_ipaddr(thread_instance_h instance, const uint8_t *ipv6_address)
+{
+       FUNC_ENTRY;
+       THREAD_CHECK_SUPPORTED_FEATURE(THREAD_FEATURE_COMMON);
+       THREAD_CHECK_INIT_STATUS();
+       THREAD_VALIDATE_INPUT_PARAMETER(instance);
+       THREAD_VALIDATE_INPUT_PARAMETER(ipv6_address);
+
+       int ret = THREAD_ERROR_NONE;
+       char msg[THREAD_NETWORK_BUFFER_MAX];
+
+       retv_if(!__is_valid_ipv6(ipv6_address), THREAD_ERROR_INVALID_PARAMETER);
+
+       snprintf(msg, THREAD_NETWORK_BUFFER_MAX,
+               "ipaddr add %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
+               ipv6_address[0], ipv6_address[1], ipv6_address[2], ipv6_address[3],
+               ipv6_address[4], ipv6_address[5], ipv6_address[6], ipv6_address[7],
+               ipv6_address[8], ipv6_address[9], ipv6_address[10], ipv6_address[11],
+               ipv6_address[12], ipv6_address[13], ipv6_address[14], ipv6_address[15]);
+
+       ret = _thread_socket_client_execute(_thread_get_socket_fd(), msg, strlen(msg));
+       retv_if(ret != THREAD_ERROR_NONE, ret);
+       THREAD_DBG("Successfully added address");
+
+       FUNC_EXIT;
+       return ret;
+}
+
+int thread_remove_ipaddr(thread_instance_h instance, const uint8_t *ipv6_address)
+{
+       FUNC_ENTRY;
+       THREAD_CHECK_SUPPORTED_FEATURE(THREAD_FEATURE_COMMON);
+       THREAD_CHECK_INIT_STATUS();
+       THREAD_VALIDATE_INPUT_PARAMETER(instance);
+       THREAD_VALIDATE_INPUT_PARAMETER(ipv6_address);
+
+       int ret = THREAD_ERROR_NONE;
+       char msg[THREAD_NETWORK_BUFFER_MAX];
+
+       retv_if(!__is_valid_ipv6(ipv6_address), THREAD_ERROR_INVALID_PARAMETER);
+
+       snprintf(msg, THREAD_BORDER_ROUTER_BUFFER_MAX,
+               "ipaddr del %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
+               ipv6_address[0], ipv6_address[1], ipv6_address[2], ipv6_address[3],
+               ipv6_address[4], ipv6_address[5], ipv6_address[6], ipv6_address[7],
+               ipv6_address[8], ipv6_address[9], ipv6_address[10], ipv6_address[11],
+               ipv6_address[12], ipv6_address[13], ipv6_address[14], ipv6_address[15]);
+
+       ret = _thread_socket_client_execute(_thread_get_socket_fd(), msg, strlen(msg));
+       retv_if(ret != THREAD_ERROR_NONE, ret);
+       THREAD_DBG("Successfully removed address");
+
+       FUNC_EXIT;
+       return ret;
+}
+
index c7a96da2038cff2ab3301bf3cf56c73310e0c372..90f730a51ffb76be7eb6703f9657773e7cadf7f9 100644 (file)
@@ -47,6 +47,9 @@ uint64_t g_extended_panid;
 uint16_t g_panid;
 
 static char g_str_ipaddr_type[MENU_DATA_SIZE + 1] = "0";
+static char g_str_ipaddr[MENU_DATA_SIZE + 1];
+
+uint8_t g_ipv6_address[THREAD_IPV6_ADDRESS_SIZE];
 
 static int run_thread_network_destroy_operational_network(MManager *mm, struct menu_data *menu)
 {
@@ -204,6 +207,46 @@ OUT:
        return RET_SUCCESS;
 }
 
+static int run_thread_add_ipaddr(MManager *mm, struct menu_data *menu)
+{
+       FUNC_ENTRY;
+       thread_instance_h g_instance = mm->t_instance;
+       if (g_instance == NULL)
+               goto OUT;
+
+       int ret = _preprocess_ipv6_prefix(g_str_ipaddr, g_ipv6_address);
+       retv_if(ret != THREAD_ERROR_NONE, ret);
+
+       ret = thread_add_ipaddr(g_instance, g_ipv6_address);
+       if (ret == THREAD_ERROR_NONE)
+               msg("thread_add_ipaddr success");
+       else
+               msg("thread_add_ipaddr failed");
+OUT:
+       FUNC_EXIT;
+       return RET_SUCCESS;
+}
+
+static int run_thread_remove_ipaddr(MManager *mm, struct menu_data *menu)
+{
+       FUNC_ENTRY;
+       thread_instance_h g_instance = mm->t_instance;
+       if (g_instance == NULL)
+               goto OUT;
+
+       int ret = _preprocess_ipv6_prefix(g_str_ipaddr, g_ipv6_address);
+       retv_if(ret != THREAD_ERROR_NONE, ret);
+
+       ret = thread_remove_ipaddr(g_instance, g_ipv6_address);
+       if (ret == THREAD_ERROR_NONE)
+               msg("thread_remove_ipaddr success");
+       else
+               msg("thread_remove_ipaddr 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},
@@ -229,6 +272,18 @@ static struct menu_data menu_thread_get_ipaddr[] = {
        { NULL, NULL, },
 };
 
+static struct menu_data menu_thread_add_ipaddr[] = {
+       { "1", "Type IPv6 address", NULL, NULL, g_str_ipaddr},
+       { "2", "run", NULL, run_thread_add_ipaddr, NULL},
+       { NULL, NULL, },
+};
+
+static struct menu_data menu_thread_remove_ipaddr[] = {
+       { "1", "Type IPv6 address", NULL, NULL, g_str_ipaddr},
+       { "2", "run", NULL, run_thread_remove_ipaddr, NULL},
+       { NULL, NULL, },
+};
+
 struct menu_data menu_thread_network[] = {
        { "1", "thread_network_create_operational_network",
                menu_thread_network_create_operational_network, NULL, NULL},
@@ -242,5 +297,10 @@ struct menu_data menu_thread_network[] = {
                NULL, run_thread_network_attach, NULL},
        { "6", "thread_get_ipaddr",
                menu_thread_get_ipaddr, NULL, NULL},
+       { "7", "thread_add_ipaddr",
+               menu_thread_add_ipaddr, NULL, NULL},
+       { "8", "thread_remove_ipaddr",
+               menu_thread_remove_ipaddr, NULL, NULL},
        { NULL, NULL, },
 };
+