*/
#include <string>
+#include <map>
#include <wifi-aware.h>
#include <netinet/in.h>
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;
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;
}
handle->subscribe_config = nullptr;
handle->session = nullptr;
+ handle->port = -1;
+
// NAN will be enabled when publish() or subscribe()
// because enable function works asynchronously.
// 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)
{
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,
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) {
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);
}
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);
}
"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);
}
__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) {
{
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);
"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);
{
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);
[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},
};
{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 {
disc_handle->ip_resolved_cb_data);
}
-
static void __free_pub_event(void *data)
{
VINE_LOGD("Free pub_event[%p]", data);
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)
{
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()");
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");
}
}
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);
}
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");
}
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);
enum {
CMD_QUIT = 0,
+ CMD_SELECT_METHOD,
CMD_CREATE_SESSION,
CMD_DESTROY_SESSION,
CMD_CREATE_SERVICE,
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},