From: Cheoleun Moon Date: Fri, 23 Jul 2021 04:08:35 +0000 (+0900) Subject: Support NAN discovery X-Git-Tag: submit/tizen/20210831.025107~7 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=176f50b6bace73ba225011e4ce3f23bb0aedb989;p=platform%2Fcore%2Fapi%2Fvine.git Support NAN discovery Change-Id: Id4d0fed38d50ec94206d59a47322af7d1c8e09e5 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index c9dfd61..60f50de 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/include/vine.h b/include/vine.h index 0544f77..e68d347 100755 --- a/include/vine.h +++ b/include/vine.h @@ -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; /** diff --git a/plugins/nan/nan-plugin.cpp b/plugins/nan/nan-plugin.cpp index 0739860..9f2f9a6 100755 --- a/plugins/nan/nan-plugin.cpp +++ b/plugins/nan/nan-plugin.cpp @@ -16,6 +16,7 @@ */ #include +#include #include #include @@ -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 peer_map; // + 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); diff --git a/src/include/vine-disc-plugin.h b/src/include/vine-disc-plugin.h index fd8330e..cf36d20 100755 --- a/src/include/vine-disc-plugin.h +++ b/src/include/vine-disc-plugin.h @@ -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; diff --git a/src/vine-disc.cpp b/src/vine-disc.cpp index b6ddf24..77083a2 100755 --- a/src/vine-disc.cpp +++ b/src/vine-disc.cpp @@ -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); diff --git a/src/vine-session.cpp b/src/vine-session.cpp index 6ca5d53..8652e96 100755 --- a/src/vine-session.cpp +++ b/src/vine-session.cpp @@ -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; } diff --git a/tests/vine-test/vine-test-glib.cpp b/tests/vine-test/vine-test-glib.cpp index bfb0deb..b973466 100755 --- a/tests/vine-test/vine-test-glib.cpp +++ b/tests/vine-test/vine-test-glib.cpp @@ -55,6 +55,8 @@ static vine_dp_h g_client_dp = NULL; static vine_dp_h g_pubsub_dp = NULL; static std::list g_datapath_list; static std::list 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},