Support NAN discovery 07/261707/6
authorCheoleun Moon <chleun.moon@samsung.com>
Fri, 23 Jul 2021 04:08:35 +0000 (13:08 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Mon, 30 Aug 2021 07:45:36 +0000 (16:45 +0900)
Change-Id: Id4d0fed38d50ec94206d59a47322af7d1c8e09e5

CMakeLists.txt
include/vine.h
plugins/nan/nan-plugin.cpp
src/include/vine-disc-plugin.h
src/vine-disc.cpp
src/vine-session.cpp
tests/vine-test/vine-test-glib.cpp

index c9dfd61699179d7515a6c3aad9902e8a274a2526..60f50dec5522f889c25d54f3b9a203d9e40e0aa9 100755 (executable)
@@ -96,6 +96,8 @@ IF(NAN_SUPPORT)
        IF(NOT TIZEN_OS OR NOT USE_EVENT_LOOP_EXTERNAL_GLIB)
                MESSAGE("NAN is supported in Tizen with external glib loop")
                SET(NAN_SUPPORT OFF)
+       ELSE(NOT TIZEN_OS OR NOT USE_EVENT_LOOP_EXTERNAL_GLIB)
+               ADD_DEFINITIONS("-DNAN_SUPPORT")
        ENDIF(NOT TIZEN_OS OR NOT USE_EVENT_LOOP_EXTERNAL_GLIB)
 ENDIF(NAN_SUPPORT)
 
index 0544f7734aa50faa515b08997cffd836dfd57bee..e68d34719cf188591026dddf76607d19e4e1683f 100755 (executable)
@@ -268,6 +268,7 @@ typedef void *vine_security_h;
 typedef enum {
        VINE_DISCOVERY_METHOD_DNS_SD = 0,
        VINE_DISCOVERY_METHOD_BLE,
+       VINE_DISCOVERY_METHOD_NAN,
 } vine_discovery_method_e;
 
 /**
index 073986031b78017a5511a654df238d9dab6e629c..9f2f9a6712e45f9247c58aee2e57f989db117294 100755 (executable)
@@ -16,6 +16,7 @@
 */
 
 #include <string>
+#include <map>
 
 #include <wifi-aware.h>
 #include <netinet/in.h>
@@ -34,8 +35,10 @@ typedef struct {
        wifi_aware_publish_h publish_config;
        wifi_aware_subscribe_h subscribe_config;
        wifi_aware_session_h session;
-       //char service_type[NAN_SERVICE_TYPE_LEN + 1];
+       std::map<string, wifi_aware_peer_h> peer_map;   // <mac, peer>
+
        char service_name[VINE_MAX_NAN_SERVICE_NAME_LEN + 1];
+       int port;
 } vine_nan_s;
 
 static bool __is_nan_enabled = false;
@@ -63,6 +66,7 @@ vine_disc_error nan_resolve_ip(void *plugin_handle,
        VINE_LOGD("type[%s] name[%s]", service_type, service_name);
 
        vine_nan_s *nan_handle = (vine_nan_s *)plugin_handle;
+
        return VINE_DISC_ERROR_NONE;
 }
 
@@ -84,6 +88,8 @@ vine_disc_error nan_init(void **plugin_handle, void *disc_handle)
        handle->subscribe_config = nullptr;
        handle->session = nullptr;
 
+       handle->port = -1;
+
        // NAN will be enabled when publish() or subscribe()
        // because enable function works asynchronously.
 
@@ -97,11 +103,49 @@ void nan_deinit(void *plugin_handle)
 
        // TODO
        // Disable NAN here.
-       // However, we have to check if NAN is used for data path
+       // However, we have to check if NAN is used for data path or not
+       // , and if NAN data path is broken or not when NAN is disabled.
 
        delete nan_handle;
 }
 
+static bool __is_interest_message(const unsigned char *message, size_t len)
+{
+       // TODO: We have to determine message format.
+       return true;
+}
+
+static void __open_nan_data_path(wifi_aware_session_h session,
+       wifi_aware_peer_h peer, vine_nan_s *nan_handle)
+{
+       VINE_LOGD("Open NAN data path. session[%p]", session);
+
+       wifi_aware_data_path_h ndp;
+       int ret = wifi_aware_data_path_create(session, peer, &ndp);
+
+       // TODO
+       // Need to keep ndp
+}
+
+static void __received_cb(wifi_aware_session_h session, wifi_aware_peer_h peer,
+       const unsigned char *message, size_t len, void *user_data)
+{
+       RET_IF(!user_data, "user_data is NULL");
+       VINE_LOGD("NAN message is received. session[%p], peer[%p], nan_handle[%p]",
+               session, peer, user_data);
+
+       if (!__is_interest_message(message, len)) {
+               VINE_LOGD("This message will be not used");
+               return;
+       }
+
+       vine_nan_s *nan_handle = (vine_nan_s *)user_data;
+       __open_nan_data_path(session, peer, nan_handle);
+
+       // TODO
+       // Need to keep peer
+}
+
 static void __published_cb(wifi_aware_session_h session,
        wifi_aware_error_e error, void *user_data)
 {
@@ -120,14 +164,25 @@ static void __publish(vine_nan_s *nan_handle)
        int ret = wifi_aware_session_create(WIFI_AWARE_SESSION_PUBLISH, &session);
        RET_IF(ret != WIFI_AWARE_ERROR_NONE, "wifi_aware_session_create() fails");
 
+       ret = wifi_aware_session_set_message_received_cb(session, __received_cb, nan_handle);
+       if (ret != WIFI_AWARE_ERROR_NONE) {
+               VINE_LOGE("wifi_aware_session_publish() fails");
+               goto ERR;
+       }
+
+       VINE_LOGD("Publish a NAN service");
+
        ret = wifi_aware_session_publish(session, nan_handle->publish_config,
                __published_cb, nan_handle);
        if (ret != WIFI_AWARE_ERROR_NONE) {
                VINE_LOGE("wifi_aware_session_publish() fails");
-               wifi_aware_session_destroy(session);
+               goto ERR;
        }
 
        nan_handle->session = session;
+       return;
+ERR:
+       wifi_aware_session_destroy(session);
 }
 
 static void __subscribed_cb(wifi_aware_session_h session,
@@ -150,6 +205,8 @@ static void __subscribe(vine_nan_s *nan_handle)
        int ret = wifi_aware_session_create(WIFI_AWARE_SESSION_SUBSCRIBE, &session);
        RET_IF(ret != WIFI_AWARE_ERROR_NONE, "wifi_aware_session_create() fails");
 
+       VINE_LOGD("Subscribe a NAN service");
+
        ret = wifi_aware_session_subscribe(session, nan_handle->subscribe_config,
                __subscribed_cb, nan_handle);
        if (ret != WIFI_AWARE_ERROR_NONE) {
@@ -173,6 +230,7 @@ static void __start_session(vine_nan_s *nan_handle)
 static void __stop_session(vine_nan_s *nan_handle)
 {
        if (nan_handle->session) {
+               VINE_LOGD("Stop NAN session");
                wifi_aware_session_stop(nan_handle->session);
                wifi_aware_session_destroy(nan_handle->session);
        }
@@ -181,6 +239,8 @@ static void __stop_session(vine_nan_s *nan_handle)
 static void __enabled_cb(wifi_aware_error_e error, void *user_data)
 {
        RET_IF(!user_data, "nan_handle is NULL");
+
+       VINE_LOGD("NAN is enabled. nan_handle[%p]", user_data);
        __start_session((vine_nan_s *)user_data);
 }
 
@@ -233,13 +293,10 @@ vine_disc_error nan_publish(void *plugin_handle, const char *service_type,
                "Too long service name");
        RET_VAL_IF(__check_attr_len(attributes) == false, VINE_DISC_ERROR_INVALID_PARAMETER,
                "Too long attributes");
-       for (const auto &kv : attributes) {
-               auto key = kv.first.c_str();
-               auto val = kv.second.c_str();
-       }
 
        vine_nan_s *nan_handle = (vine_nan_s *)plugin_handle;
-       VINE_LOGD("Publish a service. plugin_handle[%p]\n", plugin_handle);
+       VINE_LOGD("Publish. plugin_handle[%p], service_type[%s], service_name[%s], port[%d]",
+               plugin_handle, service_type, service_name, port);
 
        strncpy(nan_handle->service_name, service_name, VINE_MAX_NAN_SERVICE_NAME_LEN);
 
@@ -263,8 +320,8 @@ vine_disc_error nan_publish(void *plugin_handle, const char *service_type,
        }
 
        __fill_specific_info(info, service_name, attributes);
-
        nan_handle->publish_config = config;
+       nan_handle->port = port;        // port will be set for NAN data path
 
        ret = wifi_aware_enable(__enabled_cb, nan_handle);
        if (ret != WIFI_AWARE_ERROR_NONE) {
@@ -283,6 +340,7 @@ vine_disc_error nan_stop_publish(void *plugin_handle)
 {
        RET_VAL_IF(!plugin_handle, VINE_DISC_ERROR_INVALID_PARAMETER, "plugin_handle is NULL");
 
+       VINE_LOGD("Stop publish. plugin_handle[%p]", plugin_handle);
        vine_nan_s *nan_handle = (vine_nan_s *)plugin_handle;
        __stop_session(nan_handle);
 
@@ -301,7 +359,8 @@ vine_disc_error nan_subscribe(void *plugin_handle,
                "Too long service type");
 
        vine_nan_s *nan_handle = (vine_nan_s *)plugin_handle;
-       VINE_LOGD("Publish a service. plugin_handle[%p]\n", plugin_handle);
+       VINE_LOGD("Subscribe. plugin_handle[%p], service_type[%s]",
+               plugin_handle, service_type);
 
        wifi_aware_subscribe_h config = nullptr;
        int ret = wifi_aware_subscribe_create(&config);
@@ -339,6 +398,7 @@ vine_disc_error nan_stop_subscribe(void *plugin_handle)
 {
        RET_VAL_IF(!plugin_handle, VINE_DISC_ERROR_INVALID_PARAMETER, "plugin_handle is NULL");
 
+       VINE_LOGD("Stop subscribe. plugin_handle[%p]", plugin_handle);
        vine_nan_s *nan_handle = (vine_nan_s *)plugin_handle;
        __stop_session(nan_handle);
 
index fd8330e73dbfd4de18377e292c4070d4da5c2b9f..cf36d202964e5b2b6d1f4908180ca75bece0e7c8 100755 (executable)
@@ -23,6 +23,7 @@
 
 #define DNS_SD_PLUGIN_PATH "libvine-plugin-dns-sd.so"
 #define BLE_PLUGIN_PATH "libvine-plugin-ble.so"
+#define NAN_PLUGIN_PATH "libvine-plugin-nan.so"
 
 using namespace std;
 
index b6ddf2457b600c9789e0c0989664c2e6d2464489..77083a22e3be23a979b2077640384de835580d30 100755 (executable)
@@ -33,6 +33,9 @@ static struct {
        [VINE_DISCOVERY_METHOD_DNS_SD] = {"DNS-SD", DNS_SD_PLUGIN_PATH},
 #ifdef BT_SUPPORT
        [VINE_DISCOVERY_METHOD_BLE] = {"BLE", BLE_PLUGIN_PATH},
+#endif
+#ifdef NAN_SUPPORT
+       [VINE_DISCOVERY_METHOD_NAN] = {"NAN", NAN_PLUGIN_PATH},
 #endif
        {NULL, NULL},
 };
@@ -48,6 +51,8 @@ static struct {
                {NULL, NULL, NULL, NULL, NULL}, NULL},
        {{NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
                {NULL, NULL, NULL, NULL, NULL}, NULL},
+       {{NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
+               {NULL, NULL, NULL, NULL, NULL}, NULL},
 };
 
 typedef struct {
@@ -168,7 +173,6 @@ static void __invoke_ip_resolved_user_cb(void *event, void *user_data)
                        disc_handle->ip_resolved_cb_data);
 }
 
-
 static void __free_pub_event(void *data)
 {
        VINE_LOGD("Free pub_event[%p]", data);
index 6ca5d536a68879f4decbc0b1ef2353fd39f7360b..8652e96d9d3b6dd033be1118f8c37f674a4e6f26 100755 (executable)
@@ -169,6 +169,9 @@ static bool __check_disc_method(vine_discovery_method_e method)
        bool ret = (method == VINE_DISCOVERY_METHOD_DNS_SD);
 #ifdef BT_SUPPORT
        ret = ret || (method == VINE_DISCOVERY_METHOD_BLE);
+#endif
+#ifdef NAN_SUPPORT
+       ret = ret || (method == VINE_DISCOVERY_METHOD_NAN);
 #endif
        return ret;
 }
index bfb0deb077664d64cf6708dae2d107fb4812a9e8..b973466aa76bbf791caf573a5de5da05969ef050 100755 (executable)
@@ -55,6 +55,8 @@ static vine_dp_h g_client_dp = NULL;
 static vine_dp_h g_pubsub_dp = NULL;
 static std::list<vine_dp_h> g_datapath_list;
 static std::list<vine_service_h> g_service_list;
+static int g_disc_method = 0;
+static int g_dp_method = 0;
 
 static bool test_get_user_string(const char *msg, char *buf, int buf_size)
 {
@@ -83,6 +85,29 @@ static void __quit()
        exit(1);
 }
 
+static void __select_method()
+{
+       if (g_session) {
+               _test_print_error("Session is already running");
+               return;
+       }
+
+       printf(" >> Select method (0-Default, 1-BLE, 2-NAN): ");
+       if (scanf(" %d", &g_disc_method) < 1) {
+               _test_print_error("Scan failed");
+               return;
+       }
+
+       if (g_disc_method < 0 || g_disc_method > 2) {
+               _test_print_error("Invalid method");
+               g_disc_method = 0;
+               return;
+       }
+
+       if (g_disc_method == 2)
+               g_dp_method = 0;
+}
+
 static void __init()
 {
        PRINT_IF_ERROR(vine_initialize(), "vine_initialize()");
@@ -236,6 +261,11 @@ static void __create_session()
                return;
        }
 
+       ret = vine_session_set_discovery_method(g_session, (vine_discovery_method_e)g_disc_method);
+       if (ret != VINE_ERROR_NONE) {
+               PRINT_IF_ERROR(ret,  "vine_session_set_discovery_method");
+               return;
+       }
        __set_callbacks();
        printf("Session Created\n");
 }
@@ -538,6 +568,7 @@ static void __open_server()
        }
 
        vine_dp_create(g_session, VINE_DP_TYPE_SERVER, &g_server_dp);
+       vine_dp_set_method(g_server_dp, (vine_dp_method_e)g_dp_method);
        vine_dp_set_accepted_cb(g_server_dp, __accepted_cb, NULL);
        vine_dp_set_terminated_cb(g_server_dp, __terminated_cb, NULL);
        vine_dp_set_address_family(g_server_dp, (vine_address_family_e)addr_family);
@@ -581,6 +612,7 @@ static void __connect_server()
        }
 
        vine_dp_create(g_session, VINE_DP_TYPE_CLIENT, &g_client_dp);
+       vine_dp_set_method(g_client_dp, (vine_dp_method_e)g_dp_method);
        vine_dp_set_remote_ip(g_client_dp,
                        addr_type ? VINE_ADDRESS_FAMILY_IPV6 : VINE_ADDRESS_FAMILY_IPV4, ip);
        PRINT_RESULT(vine_dp_set_remote_port(g_client_dp, port), "vine_dp_set_remote_port");
@@ -616,6 +648,7 @@ static void __join_service()
        }
 
        vine_dp_create(g_session, VINE_DP_TYPE_PUBSUB, &g_pubsub_dp);
+       vine_dp_set_method(g_pubsub_dp, (vine_dp_method_e)g_dp_method);
        vine_dp_set_address_family(g_pubsub_dp, (vine_address_family_e)addr_family);
        vine_dp_set_port(g_pubsub_dp, port);
        vine_dp_set_topic(g_pubsub_dp, topic);
@@ -685,6 +718,7 @@ static void __close_data_path()
 
 enum {
        CMD_QUIT = 0,
+       CMD_SELECT_METHOD,
        CMD_CREATE_SESSION,
        CMD_DESTROY_SESSION,
        CMD_CREATE_SERVICE,
@@ -709,6 +743,7 @@ static struct {
        void (*func)(void);
 } __menus[] = {
        [CMD_QUIT] = {"quit", __quit},
+       [CMD_SELECT_METHOD] = {"Select method", __select_method},
        [CMD_CREATE_SESSION] = {"Create session", __create_session},
        [CMD_DESTROY_SESSION] = {"Destroy session", __destroy_session},
        [CMD_CREATE_SERVICE] = {"Create service", __create_service},