From 0c41ab9a379dc3269b628794a477228153b0198a Mon Sep 17 00:00:00 2001 From: Seonah Moon Date: Tue, 6 Jul 2021 19:11:22 +0900 Subject: [PATCH] Add new API to set DP method Change-Id: I92440476858e246d859bb31958e65e0bacff7330 --- include/vine.h | 28 ++++- plugins/ble-gatt/ble-gatt-plugin.cpp | 2 +- src/include/vine-data-path-plugin.h | 8 +- src/include/vine-data-path-state.h | 29 +++-- src/include/vine-data-path.h | 12 +- src/include/vine-dp.h | 7 ++ src/vine-data-path-state.cpp | 99 ++++++++-------- src/vine-data-path.cpp | 213 +++++++++++++++++++++++------------ src/vine-dp.cpp | 74 +++++++++++- src/vine.cpp | 8 ++ tests/unittest/vine-unittest-dp.cpp | 14 +++ tool/tool_run.cpp | 3 + 12 files changed, 350 insertions(+), 147 deletions(-) diff --git a/include/vine.h b/include/vine.h index aa9d10b..c9a663f 100755 --- a/include/vine.h +++ b/include/vine.h @@ -271,6 +271,16 @@ typedef enum { } vine_discovery_method_e; /** + * @brief Enumeration for data path method + * @since_tizen 6.5 + */ +typedef enum { + VINE_DP_METHOD_DEFAULT = 0, + VINE_DP_METHOD_BLE_GATT, + VINE_DP_METHOD_UNKNOWN +} vine_dp_method_e; + +/** * @brief Enumeration for address family * @since_tizen 6.5 */ @@ -868,6 +878,20 @@ int vine_dp_create(vine_session_h session, vine_dp_type_e type, vine_dp_h *dp); int vine_dp_destroy(vine_dp_h dp); /** + * @brief Sets the data path method + * @remarks Default method is websocket + * @since_tizen 6.5 + * @param[in] dp The data path handle + * @param[in] method The data path method + * @return 0 on success, otherwise a negative error value + * @retval #VINE_ERROR_NONE Successful + * @retval #VINE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #VINE_ERROR_NOT_SUPPORTED Not supported + * @see vine_dp_create() + */ +int vine_dp_set_method(vine_dp_h dp, vine_dp_method_e method); + +/** * @brief Gets the Id for DP_TYPE_PUBSUB. * @remarks This should be called after completing vine_dp_open() * @since_tizen 6.5 @@ -1153,7 +1177,9 @@ typedef void (*vine_dp_opened_cb)(vine_dp_h dp, vine_error_e result, void *user_ /** * @brief Opens a datapath. - * @remarks This works depending on the type of datapath differently. + * @remarks This works depending on the type of datapath differently. \ + * If you want to open VINE_DP_SERVER with BLE GATT, \ + * BLE service should be registered before. * If @a dp is a server, a listen port is opened. * If @a dp is a client, it tries to connect to a server. * If @a dp is for Publish-Subscribe, it joins to a service which has the same topic. diff --git a/plugins/ble-gatt/ble-gatt-plugin.cpp b/plugins/ble-gatt/ble-gatt-plugin.cpp index 19898d0..dccb252 100755 --- a/plugins/ble-gatt/ble-gatt-plugin.cpp +++ b/plugins/ble-gatt/ble-gatt-plugin.cpp @@ -304,7 +304,7 @@ int gatt_create(vine_dp_plugin_h *handle, void *plugin_data, void *user) RET_VAL_IF(user == NULL, VINE_DATA_PATH_ERROR_INVALID_PARAMETER, "user is NULL"); vine_gatt_s *gatt = _create_gatt(); - RET_VAL_IF(gatt == NULL, VINE_DATA_PATH_ERROR_OUT_OF_MEMORY, "Out of memory"); + RET_VAL_IF(gatt == NULL, VINE_DATA_PATH_ERROR_OPERATION_FAILED, "Failed to create GATT handle"); gatt->user = user; *handle = gatt; diff --git a/src/include/vine-data-path-plugin.h b/src/include/vine-data-path-plugin.h index 4a451d0..e74ad5c 100755 --- a/src/include/vine-data-path-plugin.h +++ b/src/include/vine-data-path-plugin.h @@ -17,12 +17,8 @@ #ifndef __VINE_DATA_PATH_PLUGIN_H__ #define __VINE_DATA_PATH_PLUGIN_H__ -#ifdef USE_LIBWEBSOCKETS -#define LIBWEBSOCKETS_PLUGIN_PATH "libvine-plugin-libwebsockets.so" -#define DATA_PATH_PLUGIN_PATH LIBWEBSOCKETS_PLUGIN_PATH -#else -#define DATA_PATH_PLUGIN_PATH "" -#endif +#define LWS_PLUGIN_PATH "libvine-plugin-libwebsockets.so" +#define BLE_GATT_PLUGIN_PATH "libvine-plugin-ble-gatt.so" #define VINE_DATA_PATH_MAX_BUFFER_SIZE 131072 // 128 * 1024 bytes diff --git a/src/include/vine-data-path-state.h b/src/include/vine-data-path-state.h index 4147ecb..7ac17ba 100644 --- a/src/include/vine-data-path-state.h +++ b/src/include/vine-data-path-state.h @@ -34,7 +34,7 @@ typedef void *vine_data_path_h; class VineDataPathState { public: - VineDataPathState(vine_data_path_h dp, vine_dp_plugin_h plugin); + VineDataPathState(vine_data_path_h dp, vine_dp_plugin_h plugin, vine_dp_plugin_fn fn); virtual ~VineDataPathState(); virtual void enter() = 0; @@ -51,13 +51,14 @@ public: protected: vine_data_path_h __dp; vine_dp_plugin_h __plugin; + vine_dp_plugin_fn __fn; }; // Vine Datapath is in the VineDefaultState when initialized class VineDefaultState : public VineDataPathState { public: - VineDefaultState(vine_data_path_h dp, vine_dp_plugin_h plugin); + VineDefaultState(vine_data_path_h dp, vine_dp_plugin_h plugin, vine_dp_plugin_fn fn); ~VineDefaultState(); void enter(); @@ -78,7 +79,7 @@ public: class VineAcceptedState : public VineDataPathState { public: - VineAcceptedState(vine_data_path_h dp, vine_dp_plugin_h plugin); + VineAcceptedState(vine_data_path_h dp, vine_dp_plugin_h plugin, vine_dp_plugin_fn fn); ~VineAcceptedState(); void enter(); @@ -107,7 +108,7 @@ private: class VineConnectedState : public VineDataPathState { public: - VineConnectedState(vine_data_path_h dp, vine_dp_plugin_h plugin); + VineConnectedState(vine_data_path_h dp, vine_dp_plugin_h plugin, vine_dp_plugin_fn fn); ~VineConnectedState(); void enter(); @@ -143,7 +144,7 @@ private: class VineAuthRequestedState : public VineDataPathState { public: - VineAuthRequestedState(vine_data_path_h dp, vine_dp_plugin_h plugin); + VineAuthRequestedState(vine_data_path_h dp, vine_dp_plugin_h plugin, vine_dp_plugin_fn fn); ~VineAuthRequestedState(); void enter(); @@ -178,7 +179,7 @@ private: class VineAuthRespondedState : public VineDataPathState { public: - VineAuthRespondedState(vine_data_path_h dp, vine_dp_plugin_h plugin); + VineAuthRespondedState(vine_data_path_h dp, vine_dp_plugin_h plugin, vine_dp_plugin_fn fn); ~VineAuthRespondedState(); void enter(); @@ -207,7 +208,7 @@ private: class VineAuthFailedState : public VineDataPathState { public: - VineAuthFailedState(vine_data_path_h dp, vine_dp_plugin_h plugin); + VineAuthFailedState(vine_data_path_h dp, vine_dp_plugin_h plugin, vine_dp_plugin_fn fn); ~VineAuthFailedState(); void enter(); @@ -226,7 +227,7 @@ public: class VineEstablishedState : public VineDataPathState { public: - VineEstablishedState(vine_data_path_h dp, vine_dp_plugin_h plugin); + VineEstablishedState(vine_data_path_h dp, vine_dp_plugin_h plugin, vine_dp_plugin_fn fn); ~VineEstablishedState(); void enter(); @@ -248,7 +249,11 @@ void vine_dp_state_set_state_changed_notifier(state_changed_notifier notifier); void vine_dp_state_set_established_notifier(established_notifier notifier); void vine_dp_state_set_data_received_notifier(data_received_notifier notifier); -VineDataPathState *vine_get_default_state(vine_data_path_h dp, vine_dp_plugin_h plugin); -void start_default_state(vine_data_path_h dp, vine_dp_plugin_h plugin, VineDataPathState *state); -void start_connected_state(vine_data_path_h dp, vine_dp_plugin_h plugin, VineDataPathState *state); -void start_accepted_state(vine_data_path_h dp, vine_dp_plugin_h plugin, VineDataPathState *state); +VineDataPathState *vine_get_default_state(vine_data_path_h dp, + vine_dp_plugin_h plugin, vine_dp_plugin_fn fn); +void start_default_state(vine_data_path_h dp, vine_dp_plugin_h plugin, vine_dp_plugin_fn fn, + VineDataPathState *state); +void start_connected_state(vine_data_path_h dp, vine_dp_plugin_h plugin, vine_dp_plugin_fn fn, + VineDataPathState *state); +void start_accepted_state(vine_data_path_h dp, vine_dp_plugin_h plugin, vine_dp_plugin_fn fn, + VineDataPathState *state); diff --git a/src/include/vine-data-path.h b/src/include/vine-data-path.h index 8a64d34..775cb21 100755 --- a/src/include/vine-data-path.h +++ b/src/include/vine-data-path.h @@ -18,6 +18,7 @@ #define __VINE_DATA_PATH_H__ #include "vine.h" +#include "vine-data-path.h" #include "vine-data-path-state.h" #include "vine-event-loop.h" @@ -31,18 +32,25 @@ typedef void (*vine_data_path_accepted_cb)(vine_data_path_h datapath, void *user typedef void (*vine_data_path_connected_cb)(vine_data_path_h datapath, int result, void *user_data); typedef void (*vine_data_path_terminated_cb)(vine_data_path_h datapath, void *user_data); +typedef enum { + VINE_DATA_PATH_METHOD_LWS = 0, // default + VINE_DATA_PATH_METHOD_BLE_GATT, + VINE_DATA_PATH_METHOD_UNKNOWN +} vine_data_path_method_e; int vine_data_path_init(void); int vine_data_path_deinit(void); -int vine_data_path_open(vine_address_family_e addr_family, int port, +int vine_data_path_open(vine_data_path_method_e method, + vine_address_family_e addr_family, int port, const char *iface_name, int max_conn, vine_security_h security, const char *host_name, vine_data_path_opened_cb opened_cb, void *opened_cb_data, vine_data_path_accepted_cb accepted_cb, void *accepted_cb_data, vine_data_path_h *opened_datapath, vine_event_queue_h event_queue); -int vine_data_path_connect(vine_address_family_e addr_family, +int vine_data_path_connect(vine_data_path_method_e method, + vine_address_family_e addr_family, const char *ip, int port, const char *iface_name, vine_security_h security, const char *host_name, const char *token, vine_data_path_connected_cb callback, void *user_data, diff --git a/src/include/vine-dp.h b/src/include/vine-dp.h index eb7eb43..498aefd 100644 --- a/src/include/vine-dp.h +++ b/src/include/vine-dp.h @@ -47,6 +47,7 @@ public: virtual int send(unsigned char *buf, size_t len) = 0; virtual int recv(unsigned char *buf, size_t buf_len, size_t *read_len) = 0; + virtual int set_method(vine_dp_method_e method) = 0; virtual int get_id(char **id) = 0; virtual int set_addr_family(vine_address_family_e addr_family) = 0; virtual int set_remote_ip(vine_address_family_e, const std::string &ip) = 0; @@ -80,6 +81,8 @@ public: void invoke_received_cb(size_t received_len); protected: + vine_data_path_method_e mMethod; + void *mEventQueue; void *mSecurity; std::string mIfaceName; @@ -105,6 +108,7 @@ public: virtual int send(unsigned char *buf, size_t len); virtual int recv(unsigned char *buf, size_t buf_len, size_t *read_len); + virtual int set_method(vine_dp_method_e method); virtual int get_id(char **id); virtual int set_addr_family(vine_address_family_e addr_family); virtual int set_remote_ip(vine_address_family_e, const std::string &ip); @@ -149,6 +153,7 @@ public: virtual int send(unsigned char *buf, size_t len); virtual int recv(unsigned char *buf, size_t buf_len, size_t *read_len); + virtual int set_method(vine_dp_method_e method); virtual int get_id(char **id); virtual int set_addr_family(vine_address_family_e addr_family); virtual int set_remote_ip(vine_address_family_e, const std::string &ip); @@ -203,6 +208,7 @@ public: virtual int send(unsigned char *buf, size_t len); virtual int recv(unsigned char *buf, size_t buf_len, size_t *read_len); + virtual int set_method(vine_dp_method_e method); virtual int get_id(char **id); virtual int set_addr_family(vine_address_family_e addr_family); virtual int set_remote_ip(vine_address_family_e, const std::string &ip); @@ -277,6 +283,7 @@ private: int _vine_dp_create(vine_session_h session, vine_dp_type_e type, vine_dp_h *dp); int _vine_dp_destroy(vine_dp_h dp); +int _vine_dp_set_method(vine_dp_h dp, vine_dp_method_e method); int _vine_dp_get_id(vine_dp_h dp, char **id); int _vine_dp_set_iface_name(vine_dp_h dp, const char *iface_name); int _vine_dp_set_addr_family(vine_dp_h dp, vine_address_family_e addr_family); diff --git a/src/vine-data-path-state.cpp b/src/vine-data-path-state.cpp index 2c62af2..c7f1bf7 100644 --- a/src/vine-data-path-state.cpp +++ b/src/vine-data-path-state.cpp @@ -22,8 +22,6 @@ #include "vine-log.h" #include "vine-utils.h" -extern vine_dp_plugin_fn g_dp_plugin_fn; - static state_changed_notifier __set_state; static established_notifier __established; static data_received_notifier __data_received; @@ -37,15 +35,17 @@ static void state_transition(vine_data_path_h dp, new_state->enter(); } -VineDataPathState::VineDataPathState(vine_data_path_h dp, vine_dp_plugin_h plugin) - : __dp(dp), __plugin(plugin) +VineDataPathState::VineDataPathState(vine_data_path_h dp, + vine_dp_plugin_h plugin, vine_dp_plugin_fn fn) + : __dp(dp), __plugin(plugin), __fn(fn) {} VineDataPathState::~VineDataPathState() {} -VineDefaultState::VineDefaultState(vine_data_path_h dp, vine_dp_plugin_h plugin) - : VineDataPathState(dp, plugin) +VineDefaultState::VineDefaultState(vine_data_path_h dp, + vine_dp_plugin_h plugin, vine_dp_plugin_fn fn) + : VineDataPathState(dp, plugin, fn) { } @@ -78,8 +78,9 @@ void VineDefaultState::received_cb(size_t bytes) void VineDefaultState::written_cb(size_t bytes) {} -VineAcceptedState::VineAcceptedState(vine_data_path_h dp, vine_dp_plugin_h plugin) - : VineDataPathState(dp, plugin), __sent_frame(0), __accept(0) +VineAcceptedState::VineAcceptedState(vine_data_path_h dp, + vine_dp_plugin_h plugin, vine_dp_plugin_fn fn) + : VineDataPathState(dp, plugin, fn), __sent_frame(0), __accept(0) { } @@ -142,13 +143,13 @@ void VineAcceptedState::written_cb(size_t bytes) VINE_LOGD("+"); switch (__sent_frame) { case AUTH_OP_REQUEST: - state_transition(__dp, this, new VineAuthRequestedState(__dp, __plugin)); + state_transition(__dp, this, new VineAuthRequestedState(__dp, __plugin, __fn)); break; case AUTH_OP_CONFIRM: if (__accept == 1) - state_transition(__dp, this, new VineEstablishedState(__dp, __plugin)); + state_transition(__dp, this, new VineEstablishedState(__dp, __plugin, __fn)); else - state_transition(__dp, this, new VineAuthFailedState(__dp, __plugin)); + state_transition(__dp, this, new VineAuthFailedState(__dp, __plugin, __fn)); break; default: VINE_LOGE("Invalid Frame %d", __sent_frame); @@ -158,7 +159,7 @@ void VineAcceptedState::written_cb(size_t bytes) void VineAcceptedState::handle_failure(std::string msg) { VINE_LOGE("Failure %s", msg.c_str()); - state_transition(__dp, this, new VineAuthFailedState(__dp, __plugin)); + state_transition(__dp, this, new VineAuthFailedState(__dp, __plugin, __fn)); } void VineAcceptedState::send_auth_request() @@ -175,7 +176,7 @@ void VineAcceptedState::send_auth_request() return; } - g_dp_plugin_fn.write(__plugin, frame, size); + __fn.write(__plugin, frame, size); release_auth_frame(frame); } @@ -195,12 +196,13 @@ void VineAcceptedState::send_auth_confirm(int accept) return; } - g_dp_plugin_fn.write(__plugin, frame, size); + __fn.write(__plugin, frame, size); release_auth_frame(frame); } -VineConnectedState::VineConnectedState(vine_data_path_h dp, vine_dp_plugin_h plugin) - : VineDataPathState(dp, plugin), __sent_frame(0) +VineConnectedState::VineConnectedState(vine_data_path_h dp, + vine_dp_plugin_h plugin, vine_dp_plugin_fn fn) + : VineDataPathState(dp, plugin, fn), __sent_frame(0) { } @@ -248,7 +250,7 @@ void VineConnectedState::written_cb(size_t bytes) VINE_LOGD("+"); switch (__sent_frame) { case AUTH_OP_RESPONSE: - state_transition(__dp, this, new VineAuthRespondedState(__dp, __plugin)); + state_transition(__dp, this, new VineAuthRespondedState(__dp, __plugin, __fn)); break; default: VINE_LOGE("Invalid Frame %d", __sent_frame); @@ -258,13 +260,13 @@ void VineConnectedState::written_cb(size_t bytes) void VineConnectedState::handle_failure(std::string msg) { VINE_LOGE("Failure %s", msg.c_str()); - state_transition(__dp, this, new VineAuthFailedState(__dp, __plugin)); + state_transition(__dp, this, new VineAuthFailedState(__dp, __plugin, __fn)); } void VineConnectedState::read_data(size_t bytes) { unsigned char *buf = (unsigned char *)malloc(bytes); - size_t read_size = g_dp_plugin_fn.read(__plugin, buf, bytes); + size_t read_size = __fn.read(__plugin, buf, bytes); if (bytes != read_size) { // TODO: Check it handle_failure("Wrong size"); @@ -345,7 +347,7 @@ void VineConnectedState::recv_auth_confirm(unsigned char ver, return; } - state_transition(__dp, this, new VineEstablishedState(__dp, __plugin)); + state_transition(__dp, this, new VineEstablishedState(__dp, __plugin, __fn)); } void VineConnectedState::send_auth_response() @@ -369,15 +371,16 @@ void VineConnectedState::send_auth_response() return; } - g_dp_plugin_fn.write(__plugin, frame, size); + __fn.write(__plugin, frame, size); release_auth_frame(frame); free(psk); __sent_frame = AUTH_OP_RESPONSE; } -VineAuthRequestedState::VineAuthRequestedState(vine_data_path_h dp, vine_dp_plugin_h plugin) - : VineDataPathState(dp, plugin), __sent_frame(0), __accept(0) +VineAuthRequestedState::VineAuthRequestedState(vine_data_path_h dp, + vine_dp_plugin_h plugin, vine_dp_plugin_fn fn) + : VineDataPathState(dp, plugin, fn), __sent_frame(0), __accept(0) { } @@ -423,7 +426,7 @@ void VineAuthRequestedState::written_cb(size_t bytes) switch (__sent_frame) { case AUTH_OP_CONFIRM: if (__accept == 1) - state_transition(__dp, this, new VineEstablishedState(__dp, __plugin)); + state_transition(__dp, this, new VineEstablishedState(__dp, __plugin, __fn)); else handle_failure("Not Accepted"); break; @@ -435,13 +438,13 @@ void VineAuthRequestedState::written_cb(size_t bytes) void VineAuthRequestedState::handle_failure(std::string msg) { VINE_LOGE("Failure %s", msg.c_str()); - state_transition(__dp, this, new VineAuthFailedState(__dp, __plugin)); + state_transition(__dp, this, new VineAuthFailedState(__dp, __plugin, __fn)); } void VineAuthRequestedState::read_data(size_t bytes) { unsigned char *buf = (unsigned char *)malloc(bytes); - size_t read_size = g_dp_plugin_fn.read(__plugin, buf, bytes); + size_t read_size = __fn.read(__plugin, buf, bytes); if (bytes != read_size) { // TODO: Check it @@ -533,15 +536,16 @@ void VineAuthRequestedState::send_auth_confirm(int accept) return; } - g_dp_plugin_fn.write(__plugin, frame, size); + __fn.write(__plugin, frame, size); release_auth_frame(frame); __sent_frame = AUTH_OP_CONFIRM; __accept = accept; } -VineAuthRespondedState::VineAuthRespondedState(vine_data_path_h dp, vine_dp_plugin_h plugin) - : VineDataPathState(dp, plugin) +VineAuthRespondedState::VineAuthRespondedState(vine_data_path_h dp, + vine_dp_plugin_h plugin, vine_dp_plugin_fn fn) + : VineDataPathState(dp, plugin, fn) { } @@ -589,13 +593,13 @@ void VineAuthRespondedState::written_cb(size_t bytes) void VineAuthRespondedState::handle_failure(std::string msg) { VINE_LOGE("Failure %s", msg.c_str()); - state_transition(__dp, this, new VineAuthFailedState(__dp, __plugin)); + state_transition(__dp, this, new VineAuthFailedState(__dp, __plugin, __fn)); } void VineAuthRespondedState::read_data(size_t bytes) { unsigned char *buf = (unsigned char *)malloc(bytes); - size_t read_size = g_dp_plugin_fn.read(__plugin, buf, bytes); + size_t read_size = __fn.read(__plugin, buf, bytes); if (bytes != read_size) { // TODO: Check it @@ -646,11 +650,12 @@ void VineAuthRespondedState::recv_auth_confirm(unsigned char ver, } __timer.stop(); - state_transition(__dp, this, new VineEstablishedState(__dp, __plugin)); + state_transition(__dp, this, new VineEstablishedState(__dp, __plugin, __fn)); } -VineAuthFailedState::VineAuthFailedState(vine_data_path_h dp, vine_dp_plugin_h plugin) - : VineDataPathState(dp, plugin) +VineAuthFailedState::VineAuthFailedState(vine_data_path_h dp, + vine_dp_plugin_h plugin, vine_dp_plugin_fn fn) + : VineDataPathState(dp, plugin, fn) { } @@ -687,8 +692,9 @@ void VineAuthFailedState::written_cb(size_t bytes) VINE_LOGD("+"); } -VineEstablishedState::VineEstablishedState(vine_data_path_h dp, vine_dp_plugin_h plugin) - : VineDataPathState(dp, plugin) +VineEstablishedState::VineEstablishedState(vine_data_path_h dp, + vine_dp_plugin_h plugin, vine_dp_plugin_fn fn) + : VineDataPathState(dp, plugin, fn) { } @@ -708,14 +714,14 @@ void VineEstablishedState::exit() int VineEstablishedState::read(unsigned char *buf, size_t buf_len, size_t *read_len) { - size_t bytes = g_dp_plugin_fn.read(__plugin, buf, buf_len); + size_t bytes = __fn.read(__plugin, buf, buf_len); *read_len = bytes; return VINE_ERROR_NONE; } int VineEstablishedState::write(unsigned char *buf, size_t len) { - g_dp_plugin_fn.write(__plugin, buf, len); + __fn.write(__plugin, buf, len); return VINE_ERROR_NONE; } @@ -748,28 +754,29 @@ void vine_dp_state_set_data_received_notifier(data_received_notifier notifier) __data_received = notifier; } -VineDataPathState *vine_get_default_state(vine_data_path_h dp, vine_dp_plugin_h plugin) +VineDataPathState *vine_get_default_state(vine_data_path_h dp, + vine_dp_plugin_h plugin, vine_dp_plugin_fn fn) { - return new VineDefaultState(dp, plugin); + return new VineDefaultState(dp, plugin, fn); } -void start_default_state(vine_data_path_h dp, vine_dp_plugin_h plugin, +void start_default_state(vine_data_path_h dp, vine_dp_plugin_h plugin, vine_dp_plugin_fn fn, VineDataPathState *state) { VINE_LOGD("Default"); - state_transition(dp, state, new VineDefaultState(dp, plugin)); + state_transition(dp, state, new VineDefaultState(dp, plugin, fn)); } -void start_connected_state(vine_data_path_h dp, vine_dp_plugin_h plugin, +void start_connected_state(vine_data_path_h dp, vine_dp_plugin_h plugin, vine_dp_plugin_fn fn, VineDataPathState *state) { VINE_LOGD("Connected"); - state_transition(dp, state, new VineConnectedState(dp, plugin)); + state_transition(dp, state, new VineConnectedState(dp, plugin, fn)); } -void start_accepted_state(vine_data_path_h dp, vine_dp_plugin_h plugin, +void start_accepted_state(vine_data_path_h dp, vine_dp_plugin_h plugin, vine_dp_plugin_fn fn, VineDataPathState *state) { VINE_LOGD("Accepted"); - state_transition(dp, state, new VineAcceptedState(dp, plugin)); + state_transition(dp, state, new VineAcceptedState(dp, plugin, fn)); } diff --git a/src/vine-data-path.cpp b/src/vine-data-path.cpp index 6360779..e2fa7f5 100755 --- a/src/vine-data-path.cpp +++ b/src/vine-data-path.cpp @@ -25,7 +25,34 @@ #include "vine-log.h" #include "vine-utils.h" +static struct { + const char *name; + const char *path; +} __vine_data_path_plugins_info[] = { + [VINE_DATA_PATH_METHOD_LWS] = {"Websocket", LWS_PLUGIN_PATH}, +#ifdef BT_SUPPORT + [VINE_DATA_PATH_METHOD_BLE_GATT] = {"BLE GATT", BLE_GATT_PLUGIN_PATH}, +#endif + {NULL, NULL}, +}; + +struct { + vine_dp_plugin_fn fn; + vine_dp_plugin_callbacks callbacks; + void (*init)(vine_dp_plugin_fn *fn); +} __vine_data_path_plugins[] = { + {{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, 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 { + vine_data_path_method_e method; + vine_dp_plugin_fn *plugin_fn; + vine_address_family_e addr_family; char *addr; int port; @@ -68,31 +95,8 @@ typedef struct { vine_data_path_s *connected_dp; } vine_dp_accepted_event; -vine_dp_plugin_fn g_dp_plugin_fn = { - .init = NULL, - .deinit = NULL, - .register_callbacks = NULL, - .process_event = NULL, - .create = NULL, - .destroy = NULL, - .open = NULL, - .connect = NULL, - .read = NULL, - .write = NULL -}; - -static vine_dp_plugin_callbacks g_dp_plugin_cbs = { - .pollfd_cb = NULL, - .opened_cb = NULL, - .accepted_cb = NULL, - .connected_cb = NULL, - .received_cb = NULL, - .written_cb = NULL, - .terminated_cb = NULL, -}; - -static void (*__init_plugin)(vine_dp_plugin_fn *fn); -static vine_data_path_s *_vine_data_path_create(vine_address_family_e addr_family, +static vine_data_path_s *_vine_data_path_create(vine_data_path_method_e method, + vine_address_family_e addr_family, const char *addr, int port, vine_security_h security, void *plugin_data, vine_event_queue_h event_queue); @@ -136,7 +140,10 @@ static vine_error_e __convert_address_family(vine_address_family_e addr_family, void __vine_dp_poll_handler(int fd, int events, void *user_data) { - g_dp_plugin_fn.process_event(fd, events); + // TODO: Only LWS plugin supports polling functions. + // But, we need to modify pollfd_cb to pass datapath handle as the user_data + if (__vine_data_path_plugins[VINE_DATA_PATH_METHOD_LWS].fn.process_event) + __vine_data_path_plugins[VINE_DATA_PATH_METHOD_LWS].fn.process_event(fd, events); } void __vine_dp_op_handler(int fd, int events, void *user_data) @@ -148,7 +155,10 @@ void __vine_dp_op_handler(int fd, int events, void *user_data) VINE_LOGE("Read error(%d)", errno); return; } - g_dp_plugin_fn.process_event(fd, events); + // TODO: Only LWS plugin supports polling functions. + // But, we need to modify pollfd_cb to pass datapath handle as the user_data + if (__vine_data_path_plugins[VINE_DATA_PATH_METHOD_LWS].fn.process_event) + __vine_data_path_plugins[VINE_DATA_PATH_METHOD_LWS].fn.process_event(fd, events); } static void __pollfd_cb(vine_data_path_pollfd_op_e op, int fd, int events) @@ -261,8 +271,10 @@ static vine_address_family_e __convert_addr_family(vine_dp_addr_family_e addr_fa return VINE_ADDRESS_FAMILY_IPV4; case VINE_DP_IPV6: return VINE_ADDRESS_FAMILY_IPV6; + case VINE_DP_NOT_IP: + default: + return VINE_ADDRESS_FAMILY_DEFAULT; } - return VINE_ADDRESS_FAMILY_DEFAULT; } static void __accepted_cb(vine_dp_addr_family_e addr_family, char *addr, @@ -273,7 +285,8 @@ static void __accepted_cb(vine_dp_addr_family_e addr_family, char *addr, vine_data_path_s *listen_dp = (vine_data_path_s *)user_data; VINE_LOGE("listen_dp[%p], security[%p]", listen_dp, listen_dp->security); - vine_data_path_s *connected_dp = _vine_data_path_create(__convert_addr_family(addr_family), + vine_data_path_s *connected_dp = _vine_data_path_create(listen_dp->method, + __convert_addr_family(addr_family), addr, port, listen_dp->security, plugin_data, listen_dp->event_queue); RET_IF(connected_dp == NULL, "Out of memory"); @@ -282,7 +295,8 @@ static void __accepted_cb(vine_dp_addr_family_e addr_family, char *addr, VINE_LOGD("Start Authentication"); connected_dp->established = notify_accepted; - start_accepted_state(connected_dp, connected_dp->plugin_handle, connected_dp->state); + start_accepted_state(connected_dp, connected_dp->plugin_handle, *connected_dp->plugin_fn, + connected_dp->state); } static void __invoke_connected_user_cb(void *event, void *user_data) @@ -328,7 +342,7 @@ static void __connected_cb(int result, void *user_data) VINE_LOGD("Start Authentication"); dp->established = notify_connected; - start_connected_state(dp, dp->plugin_handle, dp->state); + start_connected_state(dp, dp->plugin_handle, *dp->plugin_fn, dp->state); } static void __invoke_received_user_cb(void *event, void *user_data) @@ -385,7 +399,7 @@ static void __invoke_terminated_user_cb(void *event, void *user_data) VINE_LOGD("user callback is invoked by event queue."); - start_default_state(dp, dp->plugin_handle, dp->state); + start_default_state(dp, dp->plugin_handle, *dp->plugin_fn, dp->state); if (dp->terminated_cb) dp->terminated_cb(dp, dp->terminated_cb_data); } @@ -400,17 +414,23 @@ static void __terminated_cb(void *user_data) static int _load_data_path_plugins(void) { - void *handle = dlopen(DATA_PATH_PLUGIN_PATH, RTLD_LAZY | RTLD_NODELETE); - if (handle) { - __init_plugin = (void (*)(vine_dp_plugin_fn *))dlsym(handle, "vine_data_path_plugin_init"); - dlclose(handle); - VINE_LOGI("Loaded %s", DATA_PATH_PLUGIN_PATH); - } else { - VINE_LOGE("%s doesn't exist", DATA_PATH_PLUGIN_PATH); - return VINE_ERROR_OPERATION_FAILED; + int loaded = 0; + + for (int i = 0; __vine_data_path_plugins_info[i].path; ++i) { + void *handle = dlopen(__vine_data_path_plugins_info[i].path, RTLD_LAZY | RTLD_NODELETE); + if (handle) { + __vine_data_path_plugins[i].init = + (void (*)(vine_dp_plugin_fn *))dlsym(handle, "vine_data_path_plugin_init"); + dlclose(handle); + ++loaded; + VINE_LOGI("Loaded %s", __vine_data_path_plugins_info[i].path); + } else { + VINE_LOGE("%s doesn't exist", __vine_data_path_plugins_info[i].path); + } } - return VINE_ERROR_NONE; + VINE_LOGI("%d plugins are loaded.", loaded); + return (loaded > 0 ? VINE_ERROR_NONE : VINE_ERROR_OPERATION_FAILED); } int vine_data_path_set_received_cb( @@ -464,15 +484,20 @@ int vine_data_path_close(vine_data_path_h datapath) RET_VAL_IF(datapath == NULL, VINE_ERROR_INVALID_PARAMETER, "datapath is NULL"); vine_data_path_s *dp = (vine_data_path_s *)datapath; - g_dp_plugin_fn.close(dp->plugin_handle); + RET_VAL_IF(!dp->plugin_fn || dp->plugin_fn->close, + VINE_ERROR_INVALID_PARAMETER, "plugin_fn is NULL"); + dp->plugin_fn->close(dp->plugin_handle); return VINE_ERROR_NONE; } -static vine_data_path_s *_vine_data_path_create(vine_address_family_e addr_family, +static vine_data_path_s *_vine_data_path_create(vine_data_path_method_e method, + vine_address_family_e addr_family, const char *addr, int port, vine_security_h security, void *plugin_data, vine_event_queue_h event_queue) { + RET_VAL_IF(method < VINE_DATA_PATH_METHOD_LWS || method >= VINE_DATA_PATH_METHOD_UNKNOWN, + NULL, "Unknown method."); RET_VAL_IF(addr == NULL, NULL, "addr is NULL"); int ret = VINE_ERROR_NONE; @@ -481,6 +506,13 @@ static vine_data_path_s *_vine_data_path_create(vine_address_family_e addr_famil dp = (vine_data_path_s *)calloc(1, sizeof(vine_data_path_s)); RET_VAL_IF(dp == NULL, NULL, "Out of memory"); + dp->method = method; + dp->plugin_fn = &__vine_data_path_plugins[method].fn; + if (!dp->plugin_fn || !dp->plugin_fn->create) { + free(dp); + return NULL; + } + dp->addr_family = addr_family; dp->addr = STRDUP(addr); if (dp->addr == NULL) { @@ -505,7 +537,7 @@ static vine_data_path_s *_vine_data_path_create(vine_address_family_e addr_famil dp->security = NULL; } - ret = g_dp_plugin_fn.create(&dp->plugin_handle, plugin_data, dp); + ret = dp->plugin_fn->create(&dp->plugin_handle, plugin_data, dp); if (ret != VINE_DATA_PATH_ERROR_NONE) { free(dp->addr); _vine_security_destroy(dp->security); @@ -514,8 +546,8 @@ static vine_data_path_s *_vine_data_path_create(vine_address_family_e addr_famil return NULL; } - dp->state = vine_get_default_state(dp, dp->plugin_handle); - VINE_LOGD("datapath[%p] is created.", dp); + dp->state = vine_get_default_state(dp, dp->plugin_handle, *dp->plugin_fn); + VINE_LOGD("datapath[%p] is created. method[%d]", dp, method); return dp; } @@ -524,7 +556,9 @@ int vine_data_path_destroy(vine_data_path_h datapath) RET_VAL_IF(datapath == NULL, VINE_ERROR_INVALID_PARAMETER, "datapath is NULL"); vine_data_path_s *dp = (vine_data_path_s *)datapath; - g_dp_plugin_fn.destroy(dp->plugin_handle); + if (dp->plugin_fn && dp->plugin_fn->destroy) + dp->plugin_fn->destroy(dp->plugin_handle); + dp->plugin_handle = NULL; free(dp->addr); dp->addr = NULL; @@ -582,26 +616,46 @@ static void __data_received(vine_data_path_h dp, size_t bytes) notify_data_received(dp, bytes); } +static int __init_plugins(void) +{ + vine_dp_plugin_callbacks callbacks = { + .pollfd_cb = __pollfd_cb, + .opened_cb = __opened_cb, + .accepted_cb = __accepted_cb, + .connected_cb = __connected_cb, + .received_cb = __received_cb, + .written_cb = __written_cb, + .terminated_cb = __terminated_cb, + }; + + for (int i = 0; __vine_data_path_plugins[i].init; ++i) { + __vine_data_path_plugins[i].init(&__vine_data_path_plugins[i].fn); + if (__vine_data_path_plugins[i].fn.init() != VINE_ERROR_NONE) + continue; + + __vine_data_path_plugins[i].callbacks = callbacks; + __vine_data_path_plugins[i].fn.register_callbacks(__vine_data_path_plugins[i].callbacks); + } + + return VINE_ERROR_NONE; +} + +static void __deinit_plugins(void) +{ + for (int i = 0; __vine_data_path_plugins[i].fn.deinit; ++i) + __vine_data_path_plugins[i].fn.deinit(); +} + int vine_data_path_init(void) { int ret = _load_data_path_plugins(); RET_VAL_IF(ret != VINE_ERROR_NONE, ret, "Fail to load plugins"); - __init_plugin(&g_dp_plugin_fn); - ret = g_dp_plugin_fn.init(); + ret = __init_plugins(); RET_VAL_IF(ret != VINE_DATA_PATH_ERROR_NONE, __convert_data_path_error_to_vine_error((vine_data_path_error)ret), "Filed to init dp-plugin"); - g_dp_plugin_cbs.pollfd_cb = __pollfd_cb; - g_dp_plugin_cbs.opened_cb = __opened_cb; - g_dp_plugin_cbs.accepted_cb = __accepted_cb; - g_dp_plugin_cbs.connected_cb = __connected_cb; - g_dp_plugin_cbs.received_cb = __received_cb; - g_dp_plugin_cbs.written_cb = __written_cb; - g_dp_plugin_cbs.terminated_cb = __terminated_cb; - g_dp_plugin_fn.register_callbacks(g_dp_plugin_cbs); - vine_dp_state_set_state_changed_notifier(__set_state); vine_dp_state_set_established_notifier(__established); vine_dp_state_set_data_received_notifier(__data_received); @@ -610,8 +664,7 @@ int vine_data_path_init(void) int vine_data_path_deinit(void) { - g_dp_plugin_fn.deinit(); - + __deinit_plugins(); return VINE_ERROR_NONE; } @@ -671,7 +724,8 @@ static void _destroy_security_info(vine_dp_ssl *dest) free(dest->key_path); } -int vine_data_path_open(vine_address_family_e addr_family, int port, const char *iface_name, +int vine_data_path_open(vine_data_path_method_e method, + vine_address_family_e addr_family, int port, const char *iface_name, int max_conn, vine_security_h security, const char *host_name, vine_data_path_opened_cb opened_cb, void *opened_cb_data, vine_data_path_accepted_cb accepted_cb, void *accepted_cb_data, @@ -686,8 +740,10 @@ int vine_data_path_open(vine_address_family_e addr_family, int port, const char } vine_data_path_s *dp = - _vine_data_path_create(addr_family, "", port, security, NULL, event_queue); + _vine_data_path_create(method, addr_family, "", port, security, NULL, event_queue); RET_VAL_IF(dp == NULL, VINE_ERROR_OUT_OF_MEMORY, "Out of memory"); + RET_VAL_IF(dp->plugin_fn == NULL, VINE_ERROR_INVALID_PARAMETER, "Invalid parameter"); + RET_VAL_IF(dp->plugin_fn->open == NULL, VINE_ERROR_INVALID_PARAMETER, "Invalid parameter"); vine_dp_ssl ssl = {false, VINE_DP_TLS_VERSION_DEFAULT, 0, NULL, NULL, NULL}; _extract_security_info(security, &ssl); @@ -698,15 +754,15 @@ int vine_data_path_open(vine_address_family_e addr_family, int port, const char dp->accepted_cb_data = accepted_cb_data; // optional - if (host_name) { - ret = g_dp_plugin_fn.set_host_name(dp->plugin_handle, host_name); + if (host_name && dp->plugin_fn->set_host_name) { + ret = dp->plugin_fn->set_host_name(dp->plugin_handle, host_name); if (ret != VINE_DATA_PATH_ERROR_NONE) { vine_data_path_destroy(dp); return __convert_data_path_error_to_vine_error((vine_data_path_error)ret); } } - ret = g_dp_plugin_fn.open(dp->plugin_handle, dp_addr_family, port, iface_name, max_conn, ssl); + ret = dp->plugin_fn->open(dp->plugin_handle, dp_addr_family, port, iface_name, max_conn, ssl); _destroy_security_info(&ssl); if (ret != VINE_DATA_PATH_ERROR_NONE) { @@ -723,15 +779,18 @@ int vine_data_path_open(vine_address_family_e addr_family, int port, const char return VINE_ERROR_NONE; } -int vine_data_path_connect(vine_address_family_e addr_family, +int vine_data_path_connect(vine_data_path_method_e method, + vine_address_family_e addr_family, const char *ip, int port, const char *iface_name, vine_security_h security, const char *host_name, const char *token, vine_data_path_connected_cb callback, void *user_data, vine_data_path_h *connected_datapath, vine_event_queue_h event_queue) { vine_data_path_s *dp = - _vine_data_path_create(addr_family, ip, port, security, NULL, event_queue); + _vine_data_path_create(method, addr_family, ip, port, security, NULL, event_queue); RET_VAL_IF(dp == NULL, VINE_ERROR_OUT_OF_MEMORY, "Out of memory"); + RET_VAL_IF(dp->plugin_fn == NULL, VINE_ERROR_INVALID_PARAMETER, "Invalid parameter"); + RET_VAL_IF(dp->plugin_fn->connect == NULL, VINE_ERROR_INVALID_PARAMETER, "Invalid parameter"); RET_VAL_IF(connected_datapath == NULL, VINE_ERROR_INVALID_PARAMETER, "connected_datapath is NULL"); @@ -744,16 +803,16 @@ int vine_data_path_connect(vine_address_family_e addr_family, int ret; // optional - if (host_name) { - ret = g_dp_plugin_fn.set_host_name(dp->plugin_handle, host_name); + if (host_name && dp->plugin_fn->set_host_name) { + ret = dp->plugin_fn->set_host_name(dp->plugin_handle, host_name); if (ret != VINE_DATA_PATH_ERROR_NONE) { vine_data_path_destroy(dp); return __convert_data_path_error_to_vine_error((vine_data_path_error)ret); } } - if (token) { - ret = g_dp_plugin_fn.set_token(dp->plugin_handle, token); + if (token && dp->plugin_fn->set_token) { + ret = dp->plugin_fn->set_token(dp->plugin_handle, token); if (ret != VINE_DATA_PATH_ERROR_NONE) { vine_data_path_destroy(dp); _destroy_security_info(&ssl); @@ -761,7 +820,7 @@ int vine_data_path_connect(vine_address_family_e addr_family, } } - ret = g_dp_plugin_fn.connect(dp->plugin_handle, + ret = dp->plugin_fn->connect(dp->plugin_handle, addr_family == VINE_ADDRESS_FAMILY_IPV4 ? VINE_DP_IPV4 : VINE_DP_IPV6, ip, port, iface_name, ssl); _destroy_security_info(&ssl); @@ -795,7 +854,10 @@ int vine_data_path_get_token(vine_data_path_h datapath, char **token) RET_VAL_IF(token == NULL, VINE_ERROR_INVALID_PARAMETER, "token is NULL"); vine_data_path_s *dp = (vine_data_path_s *)datapath; - int ret = g_dp_plugin_fn.get_token(dp->plugin_handle, token); + if (!dp->plugin_fn || !dp->plugin_fn->get_token) + return VINE_ERROR_INVALID_PARAMETER; + + int ret = dp->plugin_fn->get_token(dp->plugin_handle, token); return __convert_data_path_error_to_vine_error((vine_data_path_error)ret); } @@ -816,10 +878,13 @@ int vine_data_path_get_local_address_info(vine_data_path_h datapath, int *addres RET_VAL_IF(port == NULL, VINE_ERROR_INVALID_PARAMETER, "port is NULL"); vine_data_path_s *dp = (vine_data_path_s *)datapath; + RET_VAL_IF(!dp->plugin_fn || !dp->plugin_fn->get_local_address_info, + VINE_ERROR_INVALID_PARAMETER, "plugin_fn is NULL"); + vine_error_e error; int addr_family, local_port; char local_ip[VINE_MAX_IP_LEN + 1] = {0, }; - int ret = g_dp_plugin_fn.get_local_address_info(dp->plugin_handle, + int ret = dp->plugin_fn->get_local_address_info(dp->plugin_handle, &addr_family, local_ip, &local_port); error = __convert_data_path_error_to_vine_error((vine_data_path_error)ret); diff --git a/src/vine-dp.cpp b/src/vine-dp.cpp index 29a640f..5fb28ca 100644 --- a/src/vine-dp.cpp +++ b/src/vine-dp.cpp @@ -449,6 +449,7 @@ void DataPath::invoke_terminated_cb() DPServer::DPServer(void *event_queue) { VINE_LOGD("DPServer[%p] is created.", this); + mMethod = VINE_DATA_PATH_METHOD_LWS; mEventQueue = event_queue; mSecurity = NULL; mAddrFamily = VINE_ADDRESS_FAMILY_DEFAULT; @@ -474,6 +475,24 @@ DPServer::~DPServer() vine_data_path_destroy(mDataPath); } +int DPServer::set_method(vine_dp_method_e method) +{ + if (method < VINE_DP_METHOD_DEFAULT || method >= VINE_DP_METHOD_UNKNOWN) + return VINE_ERROR_INVALID_PARAMETER; + + switch (method) { + case VINE_DP_METHOD_DEFAULT: + mMethod = VINE_DATA_PATH_METHOD_LWS; + break; + case VINE_DP_METHOD_BLE_GATT: + mMethod = VINE_DATA_PATH_METHOD_BLE_GATT; + break; + default: + mMethod = VINE_DATA_PATH_METHOD_LWS; + } + return VINE_ERROR_NONE; +} + int DPServer::get_id(char **id) { return VINE_ERROR_INVALID_OPERATION; @@ -599,7 +618,8 @@ int DPServer::open(vine_dp_opened_cb callback, void *user_data) mOpenedCbData = user_data; mOpenState = VINE_DP_OPEN_STATE_WAIT; - int ret = vine_data_path_open(mAddrFamily, mListenPort, + int ret = vine_data_path_open(mMethod, + mAddrFamily, mListenPort, mIfaceName.size() > 0 ? mIfaceName.c_str() : NULL, mMaxConnNum, mSecurity, NULL, _opened_cb, static_cast(this), @@ -630,6 +650,7 @@ int DPServer::recv(unsigned char *buf, size_t buf_len, size_t *read_len) DPClient::DPClient(void *event_queue) { VINE_LOGD("DPClient[%p] is created.", this); + mMethod = VINE_DATA_PATH_METHOD_LWS; mEventQueue = event_queue; mSecurity = NULL; mAddrFamily = VINE_ADDRESS_FAMILY_DEFAULT; @@ -676,6 +697,24 @@ DPClient::~DPClient() vine_data_path_destroy(mDataPath); } +int DPClient::set_method(vine_dp_method_e method) +{ + if (method < VINE_DP_METHOD_DEFAULT || method >= VINE_DP_METHOD_UNKNOWN) + return VINE_ERROR_INVALID_PARAMETER; + + switch (method) { + case VINE_DP_METHOD_DEFAULT: + mMethod = VINE_DATA_PATH_METHOD_LWS; + break; + case VINE_DP_METHOD_BLE_GATT: + mMethod = VINE_DATA_PATH_METHOD_BLE_GATT; + break; + default: + mMethod = VINE_DATA_PATH_METHOD_LWS; + } + return VINE_ERROR_NONE; +} + int DPClient::get_id(char **id) { return VINE_ERROR_INVALID_OPERATION; @@ -789,7 +828,8 @@ int DPClient::open(vine_dp_opened_cb callback, void *user_data) mOpenedCbData = user_data; mOpenState = VINE_DP_OPEN_STATE_WAIT; - int ret = vine_data_path_connect(mAddrFamily, mPeerIp.c_str(), mPeerPort, + int ret = vine_data_path_connect(mMethod, + mAddrFamily, mPeerIp.c_str(), mPeerPort, mIfaceName.size() > 0 ? mIfaceName.c_str() : NULL, mSecurity, NULL, NULL, _connected_cb, static_cast(this), &mDataPath, mEventQueue); @@ -817,6 +857,7 @@ int DPClient::recv(unsigned char *buf, size_t buf_len, size_t *read_len) DPPubSub::DPPubSub(void *event_queue) { VINE_LOGD("DPPubSub[%p] is created.", this); + mMethod = VINE_DATA_PATH_METHOD_LWS; mEventQueue = event_queue; mSecurity = NULL; mIfaceName = ""; @@ -853,6 +894,18 @@ DPPubSub::~DPPubSub() close(); } +int DPPubSub::set_method(vine_dp_method_e method) +{ + if (method < VINE_DP_METHOD_DEFAULT || method >= VINE_DP_METHOD_UNKNOWN) + return VINE_ERROR_INVALID_PARAMETER; + + if (method == VINE_DP_METHOD_BLE_GATT) + return VINE_ERROR_INVALID_OPERATION; + + mMethod = VINE_DATA_PATH_METHOD_LWS; + return VINE_ERROR_NONE; +} + void DPPubSub::set_id(const char *id) { if (id == NULL || mId.compare(id) == 0) @@ -1003,7 +1056,8 @@ int DPPubSub::connect(const char *service_name, const char *ip, int port) conn_data->dp = static_cast(this); conn_data->service_name = STRDUP(service_name); - int ret = vine_data_path_connect(mAddrFamily, ip, port, iface_name, + int ret = vine_data_path_connect(mMethod, + mAddrFamily, ip, port, iface_name, mSecurity, service_name, mId.c_str(), _pubsub_connected_cb, (void *)conn_data, &datapath, mEventQueue); @@ -1113,7 +1167,8 @@ int DPPubSub::open(vine_dp_opened_cb callback, void *user_data) create_id(service_name); set_id(service_name); - int ret = vine_data_path_open(mAddrFamily, mListenPort, + int ret = vine_data_path_open(mMethod, + mAddrFamily, mListenPort, mIfaceName.size() > 0 ? mIfaceName.c_str() : NULL, mMaxConnNum, mSecurity, mId.c_str(), _pubsub_opened_cb, static_cast(this), @@ -1361,7 +1416,8 @@ int _vine_dp_create(vine_session_h session, vine_dp_type_e type, vine_dp_h *dp) vine_event_queue_h eq = NULL; int ret = _vine_session_get_event_queue(session, &eq); - if (ret != VINE_ERROR_NONE || !eq) + // eq can be NULL if glib is used as the eventloop. + if (ret != VINE_ERROR_NONE) return VINE_ERROR_INVALID_PARAMETER; if (type == VINE_DP_TYPE_SERVER) { @@ -1385,6 +1441,14 @@ int _vine_dp_destroy(vine_dp_h dp) return VINE_ERROR_NONE; } +int _vine_dp_set_method(vine_dp_h dp, vine_dp_method_e method) +{ + RET_VAL_IF(dp == NULL, VINE_ERROR_INVALID_PARAMETER, "dp is null."); + + DataPath *_dp = static_cast(dp); + return _dp->set_method(method);; +} + int _vine_dp_get_id(vine_dp_h dp, char **id) { RET_VAL_IF(dp == NULL, VINE_ERROR_INVALID_PARAMETER, "dp is null."); diff --git a/src/vine.cpp b/src/vine.cpp index 6c2d95e..2931ecb 100755 --- a/src/vine.cpp +++ b/src/vine.cpp @@ -350,6 +350,14 @@ API int vine_dp_destroy(vine_dp_h dp) return _vine_dp_destroy(dp); } +API int vine_dp_set_method(vine_dp_h dp, vine_dp_method_e method) +{ + __VINE_FUNC_ENTER__; + CHECK_FEATURE_SUPPORTED; + + return _vine_dp_set_method(dp, method); +} + API int vine_dp_get_id(vine_dp_h dp, char **id) { __VINE_FUNC_ENTER__; diff --git a/tests/unittest/vine-unittest-dp.cpp b/tests/unittest/vine-unittest-dp.cpp index 26fcfac..a6daf59 100755 --- a/tests/unittest/vine-unittest-dp.cpp +++ b/tests/unittest/vine-unittest-dp.cpp @@ -110,6 +110,20 @@ TEST_F(VineDpTest, DestroyP) vine_dp_destroy(dp)); } +TEST_F(VineDpTest, SetMethodN) +{ + EXPECT_EQ(VINE_ERROR_INVALID_PARAMETER, vine_dp_set_method(server_dp, VINE_DP_METHOD_UNKNOWN)); + EXPECT_EQ(VINE_ERROR_INVALID_PARAMETER, vine_dp_set_method(client_dp, VINE_DP_METHOD_UNKNOWN)); + EXPECT_EQ(VINE_ERROR_INVALID_OPERATION, vine_dp_set_method(pubsub_dp, VINE_DP_METHOD_BLE_GATT)); +} + +TEST_F(VineDpTest, SetMethodP) +{ + EXPECT_EQ(VINE_ERROR_NONE, vine_dp_set_method(server_dp, VINE_DP_METHOD_BLE_GATT)); + EXPECT_EQ(VINE_ERROR_NONE, vine_dp_set_method(client_dp, VINE_DP_METHOD_BLE_GATT)); + EXPECT_EQ(VINE_ERROR_NONE, vine_dp_set_method(pubsub_dp, VINE_DP_METHOD_DEFAULT)); +} + TEST_F(VineDpTest, GetIdN) { char *id = NULL; diff --git a/tool/tool_run.cpp b/tool/tool_run.cpp index 6c8445a..9bfd677 100755 --- a/tool/tool_run.cpp +++ b/tool/tool_run.cpp @@ -554,6 +554,9 @@ static void _set_dp_info(vine_dp_type_e type) vine_dp_set_address_family(vine_configs.dp, addr_family); vine_dp_set_max_connections(vine_configs.dp, tool_config_get_max_conn()); + if (vine_configs.with_ble) + vine_dp_set_method(vine_configs.dp, VINE_DP_METHOD_BLE_GATT); + if (type == VINE_DP_TYPE_CLIENT) { vine_dp_set_remote_port(vine_configs.dp, tool_config_get_port()); vine_dp_set_remote_ip(vine_configs.dp, addr_family, -- 2.7.4