From c34d3333061c75f55247b3cd382c311a3e265d39 Mon Sep 17 00:00:00 2001 From: "Hongkuk, Son" Date: Thu, 2 Jun 2016 13:47:11 +0900 Subject: [PATCH] d2d-conv-manager: sync with SPIN removed dbus activation service flie added validation check for providing service bug fix. request object for callback is sometimes overwritten added internal APIs for web device API modified code for creating local service modified code for publishing message bug fix for server application removed SVACE warning removed build warnings integrated dev branch. applied code review feedbacks and code clean-ups applied code-review feedbacks Signed-off-by: Hongkuk, Son Change-Id: I7f540efcee5f919e88c0feac69594ceca987f9ef --- CMakeLists.txt | 2 +- daemon/CMakeLists.txt | 6 +- daemon/access_control/peer_creds.cpp | 1 + daemon/client.cpp | 9 +- daemon/client.h | 4 +- daemon/client_mgr_impl.cpp | 2 +- daemon/client_mgr_impl.h | 3 - daemon/connection_mgr_impl.cpp | 3 +- daemon/conv_json.cpp | 127 ++++++++---------- daemon/dbus_server_iface.h | 2 - daemon/dbus_server_impl.cpp | 17 +-- daemon/discovery_mgr_impl.cpp | 144 ++++++++++++--------- daemon/discovery_mgr_impl.h | 3 +- .../discovery_provider/ble_discovery_provider.cpp | 40 ++---- daemon/discovery_provider/ble_discovery_provider.h | 6 +- .../discovery_provider/iotcon/device_adapter.cpp | 25 +++- .../iotcon_discovery_provider.cpp | 68 ++++------ .../discovery_provider/iotcon_discovery_provider.h | 1 - .../{ => discovery_provider/smartview}/device.cpp | 43 +++++- daemon/{ => discovery_provider/smartview}/device.h | 8 +- .../{ => discovery_provider/smartview}/service.cpp | 2 +- .../{ => discovery_provider/smartview}/service.h | 6 +- .../smartview_discovery_provider.cpp | 52 +++----- .../smartview_discovery_provider.h | 7 +- daemon/discovery_provider_base.cpp | 25 ++++ daemon/discovery_provider_base.h | 5 +- daemon/manager_iface.h | 1 - daemon/org.tizen.d2dconv.service.in | 5 - daemon/request.cpp | 3 +- daemon/request_handler.cpp | 80 +++++------- daemon/service_mgr_impl.cpp | 15 +-- daemon/service_provider/app_comm_service_info.h | 23 ++-- .../service_provider/app_comm_service_provider.cpp | 131 ++++++++++--------- .../remote_app_control_service_provider.cpp | 69 +++++----- daemon/util.cpp | 26 ++-- lib/conv_lib.cpp | 37 ++---- lib/conv_lib_channel.cpp | 13 ++ lib/conv_lib_json.cpp | 132 +++++++++---------- lib/conv_lib_payload.cpp | 14 ++ lib/conv_lib_service.cpp | 60 ++++++--- lib/dbus_client.cpp | 7 +- lib/include/d2d_conv_internal.h | 78 +++++++++++ packaging/d2d-conv-manager.service | 2 - packaging/d2d-conv-manager.spec | 1 - test/CMakeLists.txt | 1 - test/test.c | 11 +- 46 files changed, 703 insertions(+), 617 deletions(-) mode change 100644 => 100755 CMakeLists.txt mode change 100644 => 100755 daemon/CMakeLists.txt mode change 100644 => 100755 daemon/access_control/peer_creds.cpp mode change 100644 => 100755 daemon/client.cpp mode change 100644 => 100755 daemon/client.h mode change 100644 => 100755 daemon/client_mgr_impl.cpp mode change 100644 => 100755 daemon/client_mgr_impl.h mode change 100644 => 100755 daemon/connection_mgr_impl.cpp mode change 100644 => 100755 daemon/conv_json.cpp mode change 100644 => 100755 daemon/dbus_server_iface.h mode change 100644 => 100755 daemon/dbus_server_impl.cpp mode change 100644 => 100755 daemon/discovery_mgr_impl.cpp mode change 100644 => 100755 daemon/discovery_mgr_impl.h mode change 100644 => 100755 daemon/discovery_provider/ble_discovery_provider.cpp mode change 100644 => 100755 daemon/discovery_provider/ble_discovery_provider.h mode change 100644 => 100755 daemon/discovery_provider/iotcon/device_adapter.cpp mode change 100644 => 100755 daemon/discovery_provider/iotcon_discovery_provider.cpp mode change 100644 => 100755 daemon/discovery_provider/iotcon_discovery_provider.h rename daemon/{ => discovery_provider/smartview}/device.cpp (59%) mode change 100644 => 100755 rename daemon/{ => discovery_provider/smartview}/device.h (90%) mode change 100644 => 100755 rename daemon/{ => discovery_provider/smartview}/service.cpp (97%) mode change 100644 => 100755 rename daemon/{ => discovery_provider/smartview}/service.h (95%) mode change 100644 => 100755 mode change 100644 => 100755 daemon/discovery_provider/smartview_discovery_provider.cpp mode change 100644 => 100755 daemon/discovery_provider/smartview_discovery_provider.h create mode 100755 daemon/discovery_provider_base.cpp mode change 100644 => 100755 daemon/discovery_provider_base.h mode change 100644 => 100755 daemon/manager_iface.h delete mode 100644 daemon/org.tizen.d2dconv.service.in mode change 100644 => 100755 daemon/request.cpp mode change 100644 => 100755 daemon/request_handler.cpp mode change 100644 => 100755 daemon/service_mgr_impl.cpp mode change 100644 => 100755 daemon/service_provider/app_comm_service_info.h mode change 100644 => 100755 daemon/service_provider/app_comm_service_provider.cpp mode change 100644 => 100755 daemon/service_provider/remote_app_control_service_provider.cpp mode change 100644 => 100755 daemon/util.cpp mode change 100644 => 100755 lib/conv_lib.cpp mode change 100644 => 100755 lib/conv_lib_channel.cpp mode change 100644 => 100755 lib/conv_lib_json.cpp mode change 100644 => 100755 lib/conv_lib_payload.cpp mode change 100644 => 100755 lib/conv_lib_service.cpp mode change 100644 => 100755 lib/dbus_client.cpp create mode 100755 lib/include/d2d_conv_internal.h mode change 100644 => 100755 packaging/d2d-conv-manager.service mode change 100644 => 100755 packaging/d2d-conv-manager.spec mode change 100644 => 100755 test/CMakeLists.txt mode change 100644 => 100755 test/test.c diff --git a/CMakeLists.txt b/CMakeLists.txt old mode 100644 new mode 100755 index 1f0bba2..bf24b16 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ SET(CMAKE_EXE_LINKER_FLAGS " -Wl,--hash-style=both") SET(CLIENT "d2d-conv-manager") SET(DAEMON "d2d-conv-managerd") -SET(DBUS_INTERFACE "org.tizen.d2d-conv-manager") +SET(DBUS_INTERFACE "org.tizen.d2dconv") ADD_SUBDIRECTORY(lib) ADD_SUBDIRECTORY(daemon) diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt old mode 100644 new mode 100755 index ae9c8b7..48bab71 --- a/daemon/CMakeLists.txt +++ b/daemon/CMakeLists.txt @@ -9,6 +9,7 @@ FILE(GLOB DAEMON_SRCS ${DAEMON_SRCS} discovery_provider/*.cpp) FILE(GLOB DAEMON_SRCS ${DAEMON_SRCS} service_provider/*.cpp) FILE(GLOB DAEMON_SRCS ${DAEMON_SRCS} access_control/*.cpp) FILE(GLOB DAEMON_SRCS ${DAEMON_SRCS} discovery_provider/iotcon/*.cpp) +FILE(GLOB DAEMON_SRCS ${DAEMON_SRCS} discovery_provider/smartview/*.cpp) SET(provider_deps "glib-2.0 dlog json-glib-1.0 iotcon capi-appfw-app-manager msf-api iotcon vconf capi-network-bluetooth capi-network-wifi-direct capi-appfw-application bundle capi-network-connection cynara-creds-gdbus cynara-client cynara-session capi-appfw-package-manager") # SET(provider_deps "${provider_deps} security-server") @@ -34,9 +35,4 @@ ADD_EXECUTABLE(${DAEMON} ${DAEMON_SRCS}) TARGET_LINK_LIBRARIES(${DAEMON} ${daemon_pkgs_LIBRARIES} -lpthread) - INSTALL(TARGETS ${DAEMON} DESTINATION ${BIN_INSTALL_DIR}) - -SET(DBUS_SERVICE_FILE "org.tizen.d2dconv") -CONFIGURE_FILE(${DBUS_SERVICE_FILE}.service.in ${DBUS_SERVICE_FILE}.service @ONLY) -INSTALL(FILES ${DBUS_SERVICE_FILE}.service DESTINATION ${SHARE_INSTALL_PREFIX}/dbus-1/services) diff --git a/daemon/access_control/peer_creds.cpp b/daemon/access_control/peer_creds.cpp old mode 100644 new mode 100755 index d2b2e27..e8ed9cf --- a/daemon/access_control/peer_creds.cpp +++ b/daemon/access_control/peer_creds.cpp @@ -67,6 +67,7 @@ bool conv::peer_creds::get(GDBusConnection *connection, const char *unique_name, *creds = new(std::nothrow) credentials(package_id, client, session, user); IF_FAIL_CATCH_TAG(*creds, _E, "Memory allocation failed"); + g_free(app_id); return true; CATCH: diff --git a/daemon/client.cpp b/daemon/client.cpp old mode 100644 new mode 100755 index bbd4055..d4ab3ac --- a/daemon/client.cpp +++ b/daemon/client.cpp @@ -29,7 +29,7 @@ conv::client::client(string client_id, GDBusMethodInvocation *inv) conv::client::~client() { for (service_info_map_t::iterator it = service_info_map.begin(); it != service_info_map.end(); ++it) { -// delete (it->second); + delete (it->second); } service_info_map.clear(); } @@ -54,13 +54,10 @@ conv::service_info_base* conv::client::get_service_info(string type, string id) service_info_map_t::iterator it; it = service_info_map.find(std::pair(type, id)); - if ( it != service_info_map.end() ) - { + if ( it != service_info_map.end() ) { _D("service info found : %s, %s", type.c_str(), id.c_str()); return (service_info_base*)(it->second); - } - else - { + } else { _D("service info not found : %s, %s", type.c_str(), id.c_str()); return NULL; } diff --git a/daemon/client.h b/daemon/client.h old mode 100644 new mode 100755 index e1e901e..6a284a7 --- a/daemon/client.h +++ b/daemon/client.h @@ -23,12 +23,12 @@ #include #include "client.h" #include "conv_json.h" -#include "device.h" +#include "device_iface.h" #include "service_info_base.h" namespace conv { class client { - typedef std::vector device_list_t; + typedef std::vector device_list_t; typedef std::pair service_key_t; typedef std::map service_info_map_t; diff --git a/daemon/client_mgr_impl.cpp b/daemon/client_mgr_impl.cpp old mode 100644 new mode 100755 index 00972a3..f72e6a2 --- a/daemon/client_mgr_impl.cpp +++ b/daemon/client_mgr_impl.cpp @@ -76,7 +76,7 @@ conv::client* conv::client_manager_impl::get_client(std::string client_id) } _D("new client obj created"); - client* client_obj = new conv::client(client_id, NULL); + client* client_obj = new(std::nothrow) conv::client(client_id, NULL); client_list.push_back(client_obj); return client_obj; diff --git a/daemon/client_mgr_impl.h b/daemon/client_mgr_impl.h old mode 100644 new mode 100755 index eed05d3..afce8da --- a/daemon/client_mgr_impl.h +++ b/daemon/client_mgr_impl.h @@ -31,9 +31,6 @@ namespace conv { client_manager_impl(); ~client_manager_impl(); - // int start(int time, request_info* request, json option); - // int stop(); - int init(); int release(); int handle_request(request* request_obj); diff --git a/daemon/connection_mgr_impl.cpp b/daemon/connection_mgr_impl.cpp old mode 100644 new mode 100755 index fb71644..ffb14b6 --- a/daemon/connection_mgr_impl.cpp +++ b/daemon/connection_mgr_impl.cpp @@ -51,8 +51,7 @@ int conv::connection_manager_impl::handle_request(request* request_obj) int error = CONV_ERROR_INVALID_OPERATION; if ( !conv::privilege_manager::is_allowed(request_obj->get_creds(), CONV_PRIVILEGE_INTERNET) || - !conv::privilege_manager::is_allowed(request_obj->get_creds(), CONV_PRIVILEGE_BLUETOOTH) ) - { + !conv::privilege_manager::is_allowed(request_obj->get_creds(), CONV_PRIVILEGE_BLUETOOTH) ) { _E("permission denied"); request_obj->reply(CONV_ERROR_PERMISSION_DENIED); delete request_obj; diff --git a/daemon/conv_json.cpp b/daemon/conv_json.cpp old mode 100644 new mode 100755 index 5de858f..64d5355 --- a/daemon/conv_json.cpp +++ b/daemon/conv_json.cpp @@ -69,20 +69,18 @@ json::json(const json& j) json::json(const char* s) { - if (s) { + if (s) parse(s); - } else { + else parse(EMPTY_JSON_OBJECT); - } } json::json(const std::string& s) { - if (s.empty()) { + if (s.empty()) parse(EMPTY_JSON_OBJECT); - } else { + else parse(s.c_str()); - } } json::~json() @@ -125,31 +123,31 @@ json& json::operator=(const json& j) { release(); json_node = json_node_copy(j.json_node); - if (!json_node) { + if (!json_node) _E("Json object copy failed"); - } + return *this; } json& json::operator=(const char* s) { release(); - if (s) { + if (s) parse(s); - } else { + else parse(EMPTY_JSON_OBJECT); - } + return *this; } json& json::operator=(const std::string& s) { release(); - if (s.empty()) { + if (s.empty()) parse(EMPTY_JSON_OBJECT); - } else { + else parse(s.c_str()); - } + return *this; } @@ -163,13 +161,6 @@ bool json::operator!=(const json& rhs) return !operator==(rhs); } -/* TODO -bool json::contains(const json& subset) const -{ - return false; -} -*/ - char* json::dup_cstr() { IF_FAIL_RETURN_TAG(json_node, NULL, _E, "Json object not initialized"); @@ -188,9 +179,8 @@ char* json::dup_cstr() return output; CATCH: - if (jgen) { + if (jgen) g_object_unref(jgen); - } _E("Memory allocation failed"); return NULL; @@ -225,11 +215,9 @@ static char** tokenize_path(const char* path, int* length) *length = 1; - for (pch = path; *pch != '\0'; pch++) { - if (*pch == PATH_DELIM) { + for (pch = path; *pch != '\0'; pch++) + if (*pch == PATH_DELIM) *length = *length + 1; - } - } tokens = static_cast(g_malloc((*length) * sizeof(char*))); IF_FAIL_RETURN_TAG(tokens, NULL, _E, "Memory allocation failed"); @@ -248,17 +236,16 @@ static char** tokenize_path(const char* path, int* length) begin = pch + 1; } - if (*pch == '\0') { + if (*pch == '\0') break; - } } return tokens; CATCH: - for (j = 0; j < i; j++) { + for (j = 0; j < i; j++) g_free(tokens[j]); - } + g_free(tokens); return NULL; } @@ -267,9 +254,9 @@ static void free_tokenized_path(int length, char** tokens) { int i; if (tokens) { - for (i = 0; i < length; i++) { + for (i = 0; i < length; i++) g_free(tokens[i]); - } + g_free(tokens); } } @@ -303,6 +290,7 @@ static JsonObject* traverse(JsonNode* jnode, const char* path, bool force) goto CATCH; } } + child_node = json_object_get_member(jobj, path_token[depth]); IF_FAIL_CATCH(child_node && json_node_get_node_type(child_node) == JSON_NODE_OBJECT); @@ -364,9 +352,8 @@ bool json::set(const char* path, const char* key, double val, int prec) JsonObject *jobj = traverse(json_node, path, true); IF_FAIL_RETURN(jobj, false); - if (json_object_has_member(jobj, key)) { + if (json_object_has_member(jobj, key)) json_object_remove_member(jobj, key); - } //NOTE: json-glib causes a precision issue while handling double values json_object_set_string_member(jobj, key, double_to_string(val, prec).c_str()); @@ -381,9 +368,8 @@ bool json::set(const char* path, const char* key, std::string val) JsonObject *jobj = traverse(json_node, path, true); IF_FAIL_RETURN(jobj, false); - if (json_object_has_member(jobj, key)) { + if (json_object_has_member(jobj, key)) json_object_remove_member(jobj, key); - } json_object_set_string_member(jobj, key, val.c_str()); return true; @@ -421,9 +407,9 @@ bool json::get(const char* path, const char* key, json* val) node = json_object_dup_member(jobj, key); IF_FAIL_RETURN_TAG(node, false, _E, "Memory allocation failed"); - if (val->json_node) { + if (val->json_node) json_node_free(val->json_node); - } + val->json_node = node; return true; @@ -469,14 +455,13 @@ bool json::get(const char* path, const char* key, int64_t* val) IF_FAIL_RETURN(node, false); GType vtype = json_node_get_value_type(node); - if (vtype == G_TYPE_INT64) { + if (vtype == G_TYPE_INT64) *val = json_node_get_int(node); - } else if (vtype == G_TYPE_STRING) { + else if (vtype == G_TYPE_STRING) //TODO: if the string is not a number? *val = static_cast(string_to_double(json_node_get_string(node))); - } else { + else return false; - } return true; } @@ -490,16 +475,15 @@ bool json::get(const char* path, const char* key, double* val) IF_FAIL_RETURN(node, false); GType vtype = json_node_get_value_type(node); - if (vtype == G_TYPE_DOUBLE) { + if (vtype == G_TYPE_DOUBLE) *val = json_node_get_double(node); - } else if (vtype == G_TYPE_INT64) { + else if (vtype == G_TYPE_INT64) *val = json_node_get_int(node); - } else if (vtype == G_TYPE_STRING) { + else if (vtype == G_TYPE_STRING) //NOTE: json-glib causes a precision issue while handling double values *val = string_to_double(json_node_get_string(node)); - } else { + else return false; - } return true; } @@ -566,6 +550,7 @@ static JsonArray* search_array(JsonNode* jnode, const char* path, const char* ke return NULL; } } + node = json_object_get_member(jobj, key); IF_FAIL_RETURN_TAG(node && json_node_get_node_type(node) == JSON_NODE_ARRAY, NULL, _W, "Type mismatched: %s", key); @@ -733,9 +718,9 @@ bool json::get_array_elem(const char* path, const char* key, int index, json* va JsonNode *node_copy = json_node_copy(node); IF_FAIL_RETURN_TAG(node_copy, false, _E, "Memory allocation failed"); - if (val->json_node) { + if (val->json_node) json_node_free(val->json_node); - } + val->json_node = node_copy; return true; @@ -869,18 +854,18 @@ bool json::node_equals(json_node_t* lhs, json_node_t* rhs) IF_FAIL_RETURN(ltype == rtype, false); switch (ltype) { - case JSON_NODE_VALUE: - IF_FAIL_RETURN(value_equals(lhs, rhs), false); - break; - case JSON_NODE_OBJECT: - IF_FAIL_RETURN(object_equals(lhs, rhs), false); - break; - case JSON_NODE_ARRAY: - IF_FAIL_RETURN(array_equals(lhs, rhs), false); - break; - default: - _W("Unsupported type"); - return false; + case JSON_NODE_VALUE: + IF_FAIL_RETURN(value_equals(lhs, rhs), false); + break; + case JSON_NODE_OBJECT: + IF_FAIL_RETURN(object_equals(lhs, rhs), false); + break; + case JSON_NODE_ARRAY: + IF_FAIL_RETURN(array_equals(lhs, rhs), false); + break; + default: + _W("Unsupported type"); + return false; } return true; @@ -893,15 +878,15 @@ bool json::value_equals(json_node_t* lhs, json_node_t* rhs) IF_FAIL_RETURN(ltype == rtype, false); switch (ltype) { - case G_TYPE_INT64: - return json_node_get_int(lhs) == json_node_get_int(rhs); - case G_TYPE_DOUBLE: - return json_node_get_double(lhs) == json_node_get_double(rhs); - case G_TYPE_STRING: - return STR_EQ(json_node_get_string(lhs), json_node_get_string(rhs)); - default: - _W("Unsupported type"); - return false; + case G_TYPE_INT64: + return json_node_get_int(lhs) == json_node_get_int(rhs); + case G_TYPE_DOUBLE: + return json_node_get_double(lhs) == json_node_get_double(rhs); + case G_TYPE_STRING: + return STR_EQ(json_node_get_string(lhs), json_node_get_string(rhs)); + default: + _W("Unsupported type"); + return false; } } diff --git a/daemon/dbus_server_iface.h b/daemon/dbus_server_iface.h old mode 100644 new mode 100755 index 26c7a22..61397ad --- a/daemon/dbus_server_iface.h +++ b/daemon/dbus_server_iface.h @@ -23,8 +23,6 @@ namespace conv { class dbus_server_iface { public: virtual ~dbus_server_iface() {} -// virtual int64_t signal_subscribe(const char* sender, const char* path, const char* iface, const char* name, dbus_listener_iface* listener) = 0; -// virtual void signal_unsubscribe(int64_t subscription_id) = 0; }; /* class conv::dbus_server */ } /* namespace ctx */ diff --git a/daemon/dbus_server_impl.cpp b/daemon/dbus_server_impl.cpp old mode 100644 new mode 100755 index f5c5a63..e111410 --- a/daemon/dbus_server_impl.cpp +++ b/daemon/dbus_server_impl.cpp @@ -93,7 +93,6 @@ static void handle_request(GDBusConnection* conn, const char *sender, GVariant * conv::request *recv_request = NULL; try{ recv_request = new conv::request(req_type, DEFAULT_APP_ID, req_id, subject, input, sender, creds, invocation); -// recv_request = new conv::request(req_type, app_id, req_id, subject, input, sender, NULL, invocation); } catch (std::bad_alloc& ba) { _E("Memory Allocation Failed.."); g_dbus_method_invocation_return_value(invocation, g_variant_new("(iss)", CONV_ERROR_INVALID_OPERATION, EMPTY_JSON_OBJECT, EMPTY_JSON_OBJECT)); @@ -122,11 +121,10 @@ static void handle_method_call(GDBusConnection *conn, const gchar *sender, IF_FAIL_VOID_TAG(STR_EQ(obj_path, DBUS_PATH), _W, "Invalid path: %s", obj_path); IF_FAIL_VOID_TAG(STR_EQ(iface, DBUS_IFACE), _W, "Invalid interface: %s", obj_path); - if (STR_EQ(method_name, METHOD_REQUEST)) { + if (STR_EQ(method_name, METHOD_REQUEST)) handle_request(conn, sender, param, invocation); - } else { + else _W("Invalid method: %s", method_name); - } _D("end of handle_method_call"); } @@ -203,9 +201,8 @@ bool conv::dbus_server_impl::init() void conv::dbus_server_impl::release() { - if (dbus_connection) { + if (dbus_connection) g_dbus_connection_flush_sync(dbus_connection, NULL, NULL); - } if (dbus_owner_id > 0) { g_bus_unown_name(dbus_owner_id); @@ -238,16 +235,10 @@ void conv::dbus_server_impl::publish(const char* dest, int req_id, const char* s g_dbus_connection_call(dbus_connection, dest, DBUS_PATH, DBUS_IFACE, METHOD_RESPOND, param, NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_TIMEOUT, NULL, NULL, &err); - if (err != NULL) - { + if (err != NULL) { _D("dbus_connection_call Error msg : %s", err->message); - HANDLE_GERROR(err); } - else - { - _D("err is NULL"); - } } static void handle_call_result(GObject *source, GAsyncResult *res, gpointer user_data) diff --git a/daemon/discovery_mgr_impl.cpp b/daemon/discovery_mgr_impl.cpp old mode 100644 new mode 100755 index b8eab9e..9dea0ad --- a/daemon/discovery_mgr_impl.cpp +++ b/daemon/discovery_mgr_impl.cpp @@ -22,13 +22,15 @@ #include "common.h" #include "util.h" +#include +#include + +using namespace std; static conv::discovery_manager_impl *_instance = NULL; typedef std::map discovered_ones_map_t; static discovered_ones_map_t discovered_results; -using namespace std; - conv::discovery_manager_impl::discovery_manager_impl() { count_discovery_request = 0; @@ -41,26 +43,23 @@ conv::discovery_manager_impl::~discovery_manager_impl() int conv::discovery_manager_impl::init() { _D("Discovery_Manager Init!!.."); - register_provider(new conv::smartview_discovery_provider()); - register_provider(new conv::ble_discovery_provider()); - register_provider(new conv::iotcon_discovery_provider()); + IF_FAIL_RETURN_TAG(register_provider(new(std::nothrow) conv::smartview_discovery_provider()) == CONV_ERROR_NONE, CONV_ERROR_INVALID_OPERATION, _E, "smartview_discover_provider register failed"); + IF_FAIL_RETURN_TAG(register_provider(new(std::nothrow) conv::ble_discovery_provider()) == CONV_ERROR_NONE, CONV_ERROR_INVALID_OPERATION, _E, "ble_discovery_provider register failed"); + IF_FAIL_RETURN_TAG(register_provider(new(std::nothrow) conv::iotcon_discovery_provider()) == CONV_ERROR_NONE, CONV_ERROR_INVALID_OPERATION, _E, "iotcon_discovery_provider register failed"); request_map.clear(); request_timer_map.clear(); - return CONV_ERROR_NONE; } int conv::discovery_manager_impl::release() { - for (discovery_provider_list_t::iterator it = provider_list.begin(); it != provider_list.end(); ++it) { + for (discovery_provider_list_t::iterator it = provider_list.begin(); it != provider_list.end(); ++it) (*it)->stop(); - } - for (discovery_provider_list_t::iterator it = provider_list.begin(); it != provider_list.end(); ++it) { + for (discovery_provider_list_t::iterator it = provider_list.begin(); it != provider_list.end(); ++it) delete *it; - } provider_list.clear(); @@ -76,15 +75,13 @@ int conv::discovery_manager_impl::notify_time_up(std::string client) { // 1. When no client is using discovery, it should be stopped _D("notify_time_up.. with current discovery count :%d", count_discovery_request); - if (--count_discovery_request == 0) - { + if (--count_discovery_request == 0) { stop_discovery(); } // 2. Reqeust to stop timer related to client in the timer_map timer_map_t::iterator timer_itr = request_timer_map.find(client); - if (timer_itr != request_timer_map.end()) - { + if (timer_itr != request_timer_map.end()) { int timer_id = timer_itr->second; _D("timer_id[%d]", timer_id); conv::util::misc_stop_timer(reinterpret_cast (timer_id)); @@ -92,8 +89,7 @@ int conv::discovery_manager_impl::notify_time_up(std::string client) // 3. Notify the client that the requested discovery has been finished request_map_t::iterator request_itr = request_map.find(client); - if (request_itr != request_map.end()) - { + if (request_itr != request_map.end()) { json no_data; request* cur_Req = request_itr->second; cur_Req->publish(CONV_DISCOVERY_FINISHED, no_data); @@ -126,13 +122,10 @@ int conv::discovery_manager_impl::checkBoundaryForTimeout(int givenTimeout) int conv::discovery_manager_impl::handle_request(request* request_obj) { _D("handle_request called .. request:%x _instance:%x", request_obj, _instance); - if ( _instance ) - { - if ( !strcmp(request_obj->get_subject(), CONV_SUBJECT_DISCOVERY_START) ) - { + if ( _instance ) { + if ( !strcmp(request_obj->get_subject(), CONV_SUBJECT_DISCOVERY_START) ) { if ( !conv::privilege_manager::is_allowed(request_obj->get_creds(), CONV_PRIVILEGE_INTERNET) || - !conv::privilege_manager::is_allowed(request_obj->get_creds(), CONV_PRIVILEGE_BLUETOOTH) ) - { + !conv::privilege_manager::is_allowed(request_obj->get_creds(), CONV_PRIVILEGE_BLUETOOTH) ) { _E("permission denied"); request_obj->reply(CONV_ERROR_PERMISSION_DENIED); delete request_obj; @@ -143,14 +136,11 @@ int conv::discovery_manager_impl::handle_request(request* request_obj) count_discovery_request++; for (discovery_provider_list_t::iterator it = provider_list.begin(); it != provider_list.end(); ++it) - { // Discovery Provider Starts!!!! (*it)->start(); - } const char* client = request_obj->get_sender(); - if (client == NULL) - { + if (client == NULL) { _D("client is empty.."); return CONV_ERROR_INVALID_OPERATION; } @@ -163,34 +153,29 @@ int conv::discovery_manager_impl::handle_request(request* request_obj) request_map_t::iterator map_itr = request_map.find(string(client)); - if ( map_itr == request_map.end()) - { + if ( map_itr == request_map.end()) { // current request inserted into request_map.. request_map.insert(request_map_t::value_type(string(client), request_obj)); _D("client[%s] inserted into request_map", client); - } - else - { + } else { _D("client[%s] already in request_map.. Replace!!!", client); map_itr->second = request_obj; } // request timer gpointer *param = g_new0(gpointer, 2); - param[0] = reinterpret_cast(new string(client)); + param[0] = reinterpret_cast(new(std::nothrow) string(client)); param[1] = reinterpret_cast(this); int timer_id = reinterpret_cast(conv::util::misc_start_timer(timer_worker, timeout, param)); request_timer_map[ string(client) ] = timer_id; request_obj->reply(CONV_ERROR_NONE); - - //delete request_obj; } else if ( !strcmp(request_obj->get_subject(), CONV_SUBJECT_DISCOVERY_STOP) ){ stop_discovery(); request_obj->reply(CONV_ERROR_NONE); - //delete request_obj; + delete request_obj; } return CONV_ERROR_NONE; @@ -217,20 +202,21 @@ int conv::discovery_manager_impl::register_provider(discovery_provider_base *pro { if (!provider) { _E("Provider NULL"); - throw static_cast(CONV_ERROR_INVALID_PARAMETER); + return CONV_ERROR_INVALID_PARAMETER; } provider_list.push_back(provider); + if (provider->set_manager(this) != CONV_ERROR_NONE) { + _E("Provider set_manager failed"); + return CONV_ERROR_INVALID_OPERATION; + } + if (provider->init() != CONV_ERROR_NONE) { _E("Provider initialization failed"); - throw CONV_ERROR_INVALID_OPERATION; + return CONV_ERROR_INVALID_OPERATION; } - if (provider->set_manager(this) != CONV_ERROR_NONE) { - _E("Provider set_manager failed"); - throw CONV_ERROR_INVALID_OPERATION; - } return CONV_ERROR_NONE; } int conv::discovery_manager_impl::convert_device_into_json(conv::device_iface* device_info, json* json_data) @@ -253,7 +239,46 @@ int conv::discovery_manager_impl::convert_service_into_json(conv::service_iface* return CONV_ERROR_NONE; } -int conv::discovery_manager_impl::append_discovered_result(conv::device_iface* disc_device, conv::service_iface* disc_service) +static bool serviceComparision(conv::service_iface* obj, int serviceType) +{ + if (obj->getServiceType() == serviceType) + return true; + else + return false; +} + +// return value : the number of new services +int conv::discovery_manager_impl::merge_exclude_services(conv::device_iface* org_device, conv::device_iface* new_device) +{ + int new_serv_count = 0; + std::list org_serv_list; + std::list new_serv_list; + + org_device->get_services_list(&org_serv_list); + _D("[%d] Services in the origin device info[%s]", org_serv_list.size(), org_device->getName().c_str() ); + new_device->get_services_list(&new_serv_list); + _D("[%d] Services in the new device info[%s]", new_serv_list.size(), new_device->getName().c_str() ); + + std::list::iterator new_iter = new_serv_list.begin(); + for (; new_iter != new_serv_list.end(); ++new_iter) { + service_iface* cur_serv = *new_iter; + std::list::iterator org_iter = + std::find_if(org_serv_list.begin(), org_serv_list.end(), std::bind(serviceComparision, std::placeholders::_1, cur_serv->getServiceType())); + if (org_iter != org_serv_list.end()) { + // already exists in org_device.. means it's not new!.. so remove the service from new!! + new_device->remove_service(cur_serv); + // add the service into the original device + org_device->add_service(cur_serv); + _D("Service[%d] has been already found in Device[%s]", cur_serv->getServiceType(), org_device->getName().c_str() ); + } else { + _D("New Service[%d] found in Device[%s]", cur_serv->getServiceType(), org_device->getName().c_str() ); + new_serv_count++; + } + } + return new_serv_count; +} + +int conv::discovery_manager_impl::append_discovered_result(conv::device_iface* disc_device) { _D("Append Discovered Result.. Device:%x, Service:%x"); IF_FAIL_RETURN_TAG((disc_device != NULL), CONV_ERROR_INVALID_PARAMETER, _E, "device_iface not initialized.."); @@ -261,14 +286,14 @@ int conv::discovery_manager_impl::append_discovered_result(conv::device_iface* d _D("Check if key[%s] exists in discovered_results", disc_device->getId().c_str()); discovered_ones_map_t::iterator itor_disc; itor_disc = discovered_results.find(disc_device->getId()); - if (itor_disc != discovered_results.end()) - { + if (itor_disc != discovered_results.end()) { _D("update discovered device's info [%s]", disc_device->getId().c_str()); device_iface* cur_device = itor_disc->second; - cur_device->add_service(disc_service); - } - else - { + + int count_new_services = merge_exclude_services(cur_device, disc_device); + if (count_new_services == 0) + return CONV_ERROR_NONE; + } else { _D("newbie!! discovered device's info [%s]", disc_device->getId().c_str()); discovered_results.insert(discovered_ones_map_t::value_type(disc_device->getId(), disc_device)); } @@ -278,19 +303,13 @@ int conv::discovery_manager_impl::append_discovered_result(conv::device_iface* d convert_device_into_json(disc_device, &device_json); _D("Convert service info into json type.."); - if (disc_service !=NULL) - convert_service_into_json(disc_service, &device_json); - else - { - typedef std::list serv_list_t; - serv_list_t serv_list; - disc_device->get_services_list(&serv_list); - - for (serv_list_t::iterator iterPos = serv_list.begin(); iterPos != serv_list.end(); ++iterPos) - { - service_iface* cur_serv = *iterPos; - convert_service_into_json(cur_serv, &device_json); - } + typedef std::list serv_list_t; + serv_list_t serv_list; + disc_device->get_services_list(&serv_list); + + for (serv_list_t::iterator iterPos = serv_list.begin(); iterPos != serv_list.end(); ++iterPos) { + service_iface* cur_serv = *iterPos; + convert_service_into_json(cur_serv, &device_json); } // 1.set data info about device @@ -300,8 +319,7 @@ int conv::discovery_manager_impl::append_discovered_result(conv::device_iface* d _D("Iterate through request_map to publish.."); int index = 0; request_map_t::iterator IterPos; - for (IterPos = request_map.begin(); IterPos != request_map.end(); ++IterPos) - { + for (IterPos = request_map.begin(); IterPos != request_map.end(); ++IterPos) { request* cur_Req = IterPos->second; cur_Req->publish(CONV_ERROR_NONE, device_json); diff --git a/daemon/discovery_mgr_impl.h b/daemon/discovery_mgr_impl.h old mode 100644 new mode 100755 index e06929a..c7ea6a5 --- a/daemon/discovery_mgr_impl.h +++ b/daemon/discovery_mgr_impl.h @@ -43,7 +43,7 @@ namespace conv { int handle_request(request* request_obj); // discovery_manager aggregates devices with services which have been discovered through the registered discover providers - int append_discovered_result(device_iface* disc_device, service_iface* disc_service); + int append_discovered_result(device_iface* disc_device); int notify_time_up(std::string client); int stop_discovery(); int start_discovery(); @@ -71,6 +71,7 @@ namespace conv { int set_discovery_filter(request* req_obj); int isvalid_discovery_on_filter(json& filter_json); int checkBoundaryForTimeout(int givenTimeout); + int merge_exclude_services(conv::device_iface* org_device, conv::device_iface* new_device); }; namespace discovery_manager { diff --git a/daemon/discovery_provider/ble_discovery_provider.cpp b/daemon/discovery_provider/ble_discovery_provider.cpp old mode 100644 new mode 100755 index 9e3825c..8266d29 --- a/daemon/discovery_provider/ble_discovery_provider.cpp +++ b/daemon/discovery_provider/ble_discovery_provider.cpp @@ -53,10 +53,6 @@ void _cb_activation(int error_code, wifi_direct_device_state_e device_state, voi bool _cb_discovered_peers_impl(wifi_direct_discovered_peer_info_s* peer, void* user_data) { -// conv::ble_discovery_provider* disc_provider = (conv::ble_discovery_provider*)user_data; - -// conv::service *conv_service = new conv::service; - #if 0//def TEMP_TEST static int first = 1; if (!strcmp(peer->device_name, "D2d note" ) ) { @@ -67,6 +63,9 @@ bool _cb_discovered_peers_impl(wifi_direct_discovered_peer_info_s* peer, void* u #endif #if 0 // TODO: make and notice if it's device support d2d + conv::ble_discovery_provider* disc_provider = (conv::ble_discovery_provider*)user_data; + conv::service *conv_service = new(std::nothrow) conv::service; + conv_service->setName(peer->device_name); conv_service->setVersion("0.0"); conv_service->setType(""); @@ -159,24 +158,19 @@ void _cb_connection(int error_code, wifi_direct_connection_state_e connection_st if (owner) { wifi_direct_get_ip_address(&ip_addr); if (NULL != ip_addr) { - sprintf(status, "GO - IP : %s", ip_addr); + snprintf(status, sizeof(status), "GO - IP : %s", ip_addr); free(ip_addr); } } else { wifi_direct_get_ip_address(&ip_addr); if (NULL != ip_addr) { - sprintf(status, "STA - IP : %s", ip_addr); + snprintf(status, sizeof(status), "STA - IP : %s", ip_addr); free(ip_addr); } } } else { - if (error_code == WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT) - _D("Error Code - WIFI_DIRECT_ERROR_CONNECTION_TIME_OUT"); - else if (error_code == WIFI_DIRECT_ERROR_AUTH_FAILED) - _D("Error Code - WIFI_DIRECT_ERROR_AUTH_FAILED"); - else if (error_code == WIFI_DIRECT_ERROR_CONNECTION_FAILED) - _D("Error Code - WIFI_DIRECT_ERROR_CONNECTION_FAILED"); + _E("Error : wifi direct (Error Code:%d)", error_code); } } break; @@ -251,6 +245,7 @@ void _cb_connection(int error_code, wifi_direct_connection_state_e connection_st break; default: + _D("event - %d", connection_state); break; } } @@ -322,17 +317,12 @@ int conv::ble_discovery_provider::release() int conv::ble_discovery_provider::start() { - return start_wfd_discovery(); -} - -int conv::ble_discovery_provider::stop() -{ + //return start_wfd_discovery(); return CONV_ERROR_NONE; } -int conv::ble_discovery_provider::set_manager(discovery_manager_impl* disc_manager) +int conv::ble_discovery_provider::stop() { - discovery_manager = disc_manager; return CONV_ERROR_NONE; } @@ -344,16 +334,13 @@ int conv::ble_discovery_provider::checkExistence(conv::service* conv_service) // insert into cache string cache_key = conv_service->getId(); // Serivce URI as Map Key.. - if (cache.find(cache_key) == cache.end()) - { + if (cache.find(cache_key) == cache.end()) { _D("conv_service with key[%s] does not exist..so go into the cache", cache_key.c_str()); cache.insert(map::value_type(cache_key, conv_service)); return CONV_ERROR_NONE; - } - else - { + } else { _D("conv_service with key[%s] already exists..", cache_key.c_str()); - return 1; // temp.. + return 1; } } @@ -367,7 +354,8 @@ int conv::ble_discovery_provider::notice_discovered(conv::service* conv_service) if (!alreadyExisted) { //the discovered one is NEW!! - discovery_manager->append_discovered_result(NULL, conv_service); + // TO-DO : need to re-write this code + //_discovery_manager->append_discovered_result(NULL, conv_service); } return CONV_ERROR_NONE; diff --git a/daemon/discovery_provider/ble_discovery_provider.h b/daemon/discovery_provider/ble_discovery_provider.h old mode 100644 new mode 100755 index 1f7c71e..c1b751a --- a/daemon/discovery_provider/ble_discovery_provider.h +++ b/daemon/discovery_provider/ble_discovery_provider.h @@ -24,7 +24,7 @@ #include "../discovery_provider_base.h" #include "../request.h" -#include "../service.h" +#include "smartview/service.h" namespace conv { class ble_discovery_provider : public discovery_provider_base { @@ -38,13 +38,9 @@ namespace conv { int start(); int stop(); - int set_manager(discovery_manager_impl* disc_manager); int notice_discovered(conv::service* conv_service); private: - discovery_manager_impl* discovery_manager; - - // for double check.. map cache; int checkExistence(conv::service* conv_service); diff --git a/daemon/discovery_provider/iotcon/device_adapter.cpp b/daemon/discovery_provider/iotcon/device_adapter.cpp old mode 100644 new mode 100755 index 0129d2f..81f9e47 --- a/daemon/discovery_provider/iotcon/device_adapter.cpp +++ b/daemon/discovery_provider/iotcon/device_adapter.cpp @@ -15,6 +15,8 @@ */ #include "device_adapter.h" +#include +#include using namespace std; @@ -27,9 +29,27 @@ conv::device_adapter::~device_adapter() { } +static bool serviceComparision(conv::service_iface* obj, int serviceType) +{ + if (obj->getServiceType() == serviceType) + return true; + else + return false; +} + int conv::device_adapter::add_service(service_iface* service_obj) { - service_list.push_back(service_obj); + service_list_t::iterator itr; + itr = std::find_if(service_list.begin(), service_list.end(), std::bind(serviceComparision, std::placeholders::_1, service_obj->getServiceType())); + + if (itr == service_list.end()) { + _D("New Service Type[%d] added to the device[%s]", + service_obj->getServiceType(), getId().c_str()); + service_list.push_back(service_obj); + } else { + _D("Service Type[%d] is already included in device[%s] so skipped!", + service_obj->getServiceType(), getId().c_str()); + } return CONV_ERROR_NONE; } @@ -56,8 +76,7 @@ string conv::device_adapter::getAddress() int conv::device_adapter::get_services_list(std::list *list) { - for (service_list_t::iterator iterPos = service_list.begin(); iterPos != service_list.end(); ++iterPos) - { + for (service_list_t::iterator iterPos = service_list.begin(); iterPos != service_list.end(); ++iterPos) { list->push_back(*iterPos); } return CONV_ERROR_NONE; diff --git a/daemon/discovery_provider/iotcon_discovery_provider.cpp b/daemon/discovery_provider/iotcon_discovery_provider.cpp old mode 100644 new mode 100755 index d426120..f8a2664 --- a/daemon/discovery_provider/iotcon_discovery_provider.cpp +++ b/daemon/discovery_provider/iotcon_discovery_provider.cpp @@ -38,8 +38,6 @@ typedef std::map discovery_process_map_t; static discovery_complete_list_t discovery_complete_list; static discovery_process_map_t discovery_process_map; -static conv::discovery_manager_impl* disc_manager; - conv::iotcon_discovery_provider::iotcon_discovery_provider() { } @@ -55,8 +53,9 @@ int conv::iotcon_discovery_provider::init() _E("Failed iotcon_connect... Error:%d", ret); } - if (resource_h_map != NULL) delete resource_h_map; - resource_h_map = new (std::nothrow) resource_h_map_t; + if (resource_h_map != NULL) + delete resource_h_map; + resource_h_map = new(std::nothrow) resource_h_map_t; return CONV_ERROR_NONE; } @@ -65,7 +64,8 @@ int conv::iotcon_discovery_provider::release() { iotcon_deinitialize(); - if (resource_h_map != NULL) delete resource_h_map; + if (resource_h_map != NULL) + delete resource_h_map; return CONV_ERROR_NONE; } @@ -113,17 +113,17 @@ void conv::iotcon_discovery_provider::_on_response_get(iotcon_remote_resource_h discovery_complete_list_t::iterator itor; _D("Check if key[%s] already exists in complete_list", discoveryKey.c_str()); itor = find(discovery_complete_list.begin(), discovery_complete_list.end(), discoveryKey); - if (itor != discovery_complete_list.end() ) return; // Already done pushing upwards.. + if (itor != discovery_complete_list.end() ) + return; // Already done pushing upwards.. // Retreive info from process_map.. resource_handle cur_resource_h; discovery_process_map_t::iterator itor_process; itor_process = discovery_process_map.find(discoveryKey); _D("Check if key[%s] exists in process_map", discoveryKey.c_str()); - if (itor_process != discovery_process_map.end()) + if (itor_process != discovery_process_map.end()) { cur_resource_h = itor_process->second; - else - { + } else { _D("Not supposed to happen"); return; } @@ -176,8 +176,7 @@ void conv::iotcon_discovery_provider::_on_response_get(iotcon_remote_resource_h char* service_json_char = NULL; ret = iotcon_state_get_str(recv_state, "service_json", &service_json_char); - if (service_json_char == NULL) - { + if (service_json_char == NULL) { _D("service_json does not exist..."); service_json_char = const_cast(""); } @@ -188,19 +187,16 @@ void conv::iotcon_discovery_provider::_on_response_get(iotcon_remote_resource_h _D("on_response : device_id[%s] device_name[%s] device_type[%s] version[%s] service_list[%s]" , device_id, device_name, device_type, version, service_list); - if (disc_manager != NULL) - { + if (_discovery_manager != NULL) { device_adapter* device = new(std::nothrow) device_adapter (cur_resource_h); int num_service = service_list_json.array_get_size(NULL, "service_list"); - for (int index = 0; index < num_service; index++) - { + for (int index = 0; index < num_service; index++) { int serv_type_int; json cur_service_json; bool ret = service_list_json.get_array_elem(NULL, "service_list", index, &cur_service_json); - if (ret != true) - { + if (ret != true) { _D("json get array elem with service_list error.."); continue; } @@ -215,7 +211,7 @@ void conv::iotcon_discovery_provider::_on_response_get(iotcon_remote_resource_h device->add_service(serv); } - disc_manager->append_discovered_result(device, NULL); + _discovery_manager->append_discovered_result(device); } discovery_complete_list.push_back(discoveryKey); @@ -235,17 +231,17 @@ void conv::iotcon_discovery_provider::on_received_detail_info(iotcon_remote_reso _D("On Received Detail Info.. with type[%d]", request_type); switch (request_type) { - case IOTCON_REQUEST_GET : - _on_response_get(resource, response, NULL); - break; - case IOTCON_REQUEST_PUT: - case IOTCON_REQUEST_POST: - case IOTCON_REQUEST_DELETE: - _D("Not supported request_type"); - break; - default: - _E("Invalid Request Type"); - return; + case IOTCON_REQUEST_GET : + _on_response_get(resource, response, NULL); + break; + case IOTCON_REQUEST_PUT: + case IOTCON_REQUEST_POST: + case IOTCON_REQUEST_DELETE: + _D("Not supported request_type"); + break; + default: + _E("Invalid Request Type"); + return; } } @@ -309,11 +305,11 @@ int conv::iotcon_discovery_provider::add_iot_resource(iotcon_remote_resource_h r iotcon_remote_resource_get_interfaces(resource, &resource_interfaces); resource_handle res_handle; - // uri_path + // uri_path res_handle.set_uri_path(string(resource_uri_path)); - // host_address + // host_address res_handle.set_host_address(string(resource_host)); - // resource types + // resource types iotcon_resource_types_foreach(resource_types, _get_resource_foreach_types, (void*)(&res_handle.get_types()) ); // Add resource handle into Temp Cache @@ -359,11 +355,3 @@ int conv::iotcon_discovery_provider::stop() return CONV_ERROR_NONE; } -int conv::iotcon_discovery_provider::set_manager(discovery_manager_impl* disco_manager) -{ - _D("set discovery manager.."); - disc_manager = disco_manager; - - return CONV_ERROR_NONE; -} - diff --git a/daemon/discovery_provider/iotcon_discovery_provider.h b/daemon/discovery_provider/iotcon_discovery_provider.h old mode 100644 new mode 100755 index 47d8917..ebd717a --- a/daemon/discovery_provider/iotcon_discovery_provider.h +++ b/daemon/discovery_provider/iotcon_discovery_provider.h @@ -58,7 +58,6 @@ namespace conv { int start(); int stop(); - int set_manager(discovery_manager_impl* disc_manager); static int notice_discovered(service_iface* service); }; } diff --git a/daemon/device.cpp b/daemon/discovery_provider/smartview/device.cpp old mode 100644 new mode 100755 similarity index 59% rename from daemon/device.cpp rename to daemon/discovery_provider/smartview/device.cpp index 3f048d9..9d4f2f4 --- a/daemon/device.cpp +++ b/daemon/discovery_provider/smartview/device.cpp @@ -18,6 +18,8 @@ #include "device.h" #include "log.h" #include "d2d_conv_manager.h" +#include +#include using namespace std; @@ -29,14 +31,53 @@ conv::device::~device() { } +template +struct GenericComparator +{ + typedef int (T::*GETTER)() const; + GETTER m_getterFunc; + int m_data; + GenericComparator(GETTER getterFunc, int data) + { + m_getterFunc = getterFunc; + m_data = data; + } + bool operator()(const T & obj) + { + if ((obj.*m_getterFunc)() == m_data) + return true; + else + return false; + } +}; + +static bool serviceComparision(conv::service_iface* obj, int serviceType) +{ + if (obj->getServiceType() == serviceType) + return true; + else + return false; +} + int conv::device::add_service(service_iface* service_obj) { - service_list.push_back(service_obj); + service_list_t::iterator itr; + itr = std::find_if(service_list.begin(), service_list.end(), std::bind(serviceComparision, std::placeholders::_1, service_obj->getServiceType())); + + if (itr == service_list.end()) { + _D("New Service Type[%d] added to the device[%s]", + service_obj->getServiceType(), getId().c_str()); + service_list.push_back(service_obj); + } else { + _D("Service Type[%d] is already included in device[%s] so skipped!", + service_obj->getServiceType(), getId().c_str()); + } return CONV_ERROR_NONE; } int conv::device::remove_service(service_iface* service_obj) { + service_list.remove(service_obj); return CONV_ERROR_NONE; } diff --git a/daemon/device.h b/daemon/discovery_provider/smartview/device.h old mode 100644 new mode 100755 similarity index 90% rename from daemon/device.h rename to daemon/discovery_provider/smartview/device.h index 5e7c380..f0aea3d --- a/daemon/device.h +++ b/daemon/discovery_provider/smartview/device.h @@ -19,15 +19,15 @@ #include #include -#include "service_iface.h" -#include "conv_json.h" +#include "../../service_iface.h" +#include "../../conv_json.h" -#include "device_iface.h" +#include "../../device_iface.h" namespace conv { class device : public device_iface { public: - typedef std::vector service_list_t; + typedef std::list service_list_t; device(); ~device(); diff --git a/daemon/service.cpp b/daemon/discovery_provider/smartview/service.cpp old mode 100644 new mode 100755 similarity index 97% rename from daemon/service.cpp rename to daemon/discovery_provider/smartview/service.cpp index 17efcbe..0c6e8b6 --- a/daemon/service.cpp +++ b/daemon/discovery_provider/smartview/service.cpp @@ -54,7 +54,7 @@ string conv::service::getUri() int conv::service::getServiceType() { - return 0; //string("oic.r.tizen.app-communication"); + return 0; } string conv::service::getServiceInfo() diff --git a/daemon/service.h b/daemon/discovery_provider/smartview/service.h old mode 100644 new mode 100755 similarity index 95% rename from daemon/service.h rename to daemon/discovery_provider/smartview/service.h index 5a74313..eb8d7d5 --- a/daemon/service.h +++ b/daemon/discovery_provider/smartview/service.h @@ -18,8 +18,8 @@ #define __CONV_SERVICE_H__ #include -#include "conv_json.h" -#include "service_iface.h" +#include "../../conv_json.h" +#include "../../service_iface.h" using namespace std; @@ -39,7 +39,6 @@ namespace conv int getServiceType(); string getServiceInfo(); - // void setName(string name); void setVersion(string version); void setType(string type); @@ -56,7 +55,6 @@ namespace conv int service_type; string service_info; - //.. string service_version; string service_id; string service_uri; diff --git a/daemon/discovery_provider/smartview_discovery_provider.cpp b/daemon/discovery_provider/smartview_discovery_provider.cpp old mode 100644 new mode 100755 index a52797e..a19d575 --- a/daemon/discovery_provider/smartview_discovery_provider.cpp +++ b/daemon/discovery_provider/smartview_discovery_provider.cpp @@ -17,8 +17,6 @@ #include "smartview_discovery_provider.h" #include "../discovery_mgr_impl.h" -#include "../service.h" -#include "../device.h" #include "../conv_json.h" #include "../util.h" @@ -31,14 +29,14 @@ class SearchListenerImpl : public SearchListener { conv::smartview_discovery_provider* disc_provider; public: - void set_discovery_manager(conv::discovery_manager_impl* disc_manager) + void set_discovery_manager(conv::discovery_manager_impl* discovery_manager) { - this->disc_manager = disc_manager; + this->disc_manager = discovery_manager; } - void set_discovery_provider(conv::smartview_discovery_provider* disc_provider) + void set_discovery_provider(conv::smartview_discovery_provider* discovery_provider) { - this->disc_provider = disc_provider; + this->disc_provider = discovery_provider; } void onStart() { @@ -71,11 +69,17 @@ conv::smartview_discovery_provider::~smartview_discovery_provider() int conv::smartview_discovery_provider::init() { _D("smartview_discovery init"); - search = new Search(); - if (listener_impl == NULL) - { - listener_impl = new SearchListenerImpl; + search = new(std::nothrow) Search(); + ASSERT_ALLOC(search); + if (listener_impl == NULL) { + listener_impl = new(std::nothrow) SearchListenerImpl; + if ( listener_impl == NULL) { + _E("listener_impl allocation failed"); + delete search; + return CONV_ERROR_OUT_OF_MEMORY; + } listener_impl->set_discovery_provider(this); + listener_impl->set_discovery_manager(_discovery_manager); } search->setSearchListener(listener_impl); @@ -86,8 +90,7 @@ int conv::smartview_discovery_provider::init() int conv::smartview_discovery_provider::release() { - if ( search != NULL ) - { + if ( search != NULL ) { delete search; } @@ -113,14 +116,6 @@ int conv::smartview_discovery_provider::stop() return CONV_ERROR_NONE; } -int conv::smartview_discovery_provider::set_manager(discovery_manager_impl* disc_manager) -{ - discovery_manager = disc_manager; - - listener_impl->set_discovery_manager(disc_manager); - return CONV_ERROR_NONE; -} - conv::device* conv::smartview_discovery_provider::convert_into_conv_device(Service* smartview_service) { string serv_name, serv_version, serv_type, serv_id, serv_uri; @@ -139,15 +134,6 @@ conv::device* conv::smartview_discovery_provider::convert_into_conv_device(Servi device_info->setId(serv_id); device_info->setName(serv_name); - /* - conv::service* service_info = new(std::nothrow) conv::service; - service_info->setVersion (serv_version); - service_info->setType (serv_type); - service_info->setUri (serv_uri); - - device_info->add_service (service_info); - */ - return device_info; } @@ -172,8 +158,12 @@ conv::service* conv::smartview_discovery_provider::convert_into_conv_service(Ser json_serv_info.set(CONV_JSON_SERVICE_DATA_PATH, CONV_JSON_SERVICE_DATA_VERSION, serv_version); json_serv_info.set(CONV_JSON_SERVICE_DATA_PATH, CONV_JSON_SERVICE_DATA_TYPE, serv_type); - conv::service *conv_service = new conv::service; + conv::service *conv_service = new(std::nothrow) conv::service; + if ( conv_service == NULL ) { + _E("conv_service allocation failed"); + return NULL; + } conv_service->setName(serv_name); conv_service->setVersion(serv_version); conv_service->setType(serv_type); @@ -242,7 +232,7 @@ int conv::smartview_discovery_provider::notice_discovered(Service* service) if (!alreadyExisted) { //the discovered one is NEW!! - discovery_manager->append_discovered_result(conv_device, conv_service); + _discovery_manager->append_discovered_result(conv_device); } return CONV_ERROR_NONE; diff --git a/daemon/discovery_provider/smartview_discovery_provider.h b/daemon/discovery_provider/smartview_discovery_provider.h old mode 100644 new mode 100755 index 2fe450c..a45572f --- a/daemon/discovery_provider/smartview_discovery_provider.h +++ b/daemon/discovery_provider/smartview_discovery_provider.h @@ -24,8 +24,8 @@ #include "../discovery_provider_base.h" #include "../request.h" -#include "../service.h" -#include "../device.h" +#include "smartview/service.h" +#include "smartview/device.h" #include #include @@ -43,13 +43,10 @@ namespace conv { int start(); int stop(); - int set_manager(discovery_manager_impl* disc_manager); int notice_discovered(Service *service); - private: Service service; Search* search; - discovery_manager_impl* discovery_manager; map cache; // for double check diff --git a/daemon/discovery_provider_base.cpp b/daemon/discovery_provider_base.cpp new file mode 100755 index 0000000..6ff79dd --- /dev/null +++ b/daemon/discovery_provider_base.cpp @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "discovery_mgr_impl.h" + +conv::discovery_manager_impl* conv::discovery_provider_base::_discovery_manager = NULL; + +int conv::discovery_provider_base::set_manager(discovery_manager_impl* discovery_manager) +{ + _discovery_manager = discovery_manager; + return CONV_ERROR_NONE; +} diff --git a/daemon/discovery_provider_base.h b/daemon/discovery_provider_base.h old mode 100644 new mode 100755 index a041829..99ec6de --- a/daemon/discovery_provider_base.h +++ b/daemon/discovery_provider_base.h @@ -33,7 +33,10 @@ namespace conv { virtual int start() = 0; virtual int stop() = 0; - virtual int set_manager(discovery_manager_impl* discovery_manager) = 0; + int set_manager(discovery_manager_impl* discovery_manager); + + protected: + static discovery_manager_impl* _discovery_manager; }; /* class discovery_provider_base */ } diff --git a/daemon/manager_iface.h b/daemon/manager_iface.h old mode 100644 new mode 100755 index e867be5..eace095 --- a/daemon/manager_iface.h +++ b/daemon/manager_iface.h @@ -29,7 +29,6 @@ namespace conv { virtual int init() = 0; virtual int release() = 0; virtual int handle_request(request* request_obj) = 0; -// virtual int publish(const char* subject, json data_updated, json option) = 0; }; /* class manager_iface */ } diff --git a/daemon/org.tizen.d2dconv.service.in b/daemon/org.tizen.d2dconv.service.in deleted file mode 100644 index 15bd540..0000000 --- a/daemon/org.tizen.d2dconv.service.in +++ /dev/null @@ -1,5 +0,0 @@ -[D-BUS Service] -Name=@DBUS_INTERFACE@ -Exec=@BIN_INSTALL_DIR@/d2d-conv-managerd - -Libs: -L${libdir} -pthread diff --git a/daemon/request.cpp b/daemon/request.cpp old mode 100644 new mode 100755 index 5f96fe3..1e09e8d --- a/daemon/request.cpp +++ b/daemon/request.cpp @@ -44,8 +44,7 @@ conv::request::request(int type, conv::request::~request() { - if ( __creds != NULL ) - { + if ( __creds != NULL ) { delete __creds; } } diff --git a/daemon/request_handler.cpp b/daemon/request_handler.cpp old mode 100644 new mode 100755 index 6a900c9..4cf394e --- a/daemon/request_handler.cpp +++ b/daemon/request_handler.cpp @@ -45,53 +45,39 @@ int conv::request_handler::handle_request(request* request_obj) int result = CONV_ERROR_INVALID_OPERATION; switch (request_obj->get_type()) { - case REQ_SUBSCRIBE: - _D("Requested type : REQ_SUBSCRIBE.."); - if ( !strcmp(request_obj->get_subject(), CONV_SUBJECT_COMMUNICATION_RECV) ) - { - result = service_manager::handle_request(request_obj); - } - else if ( !strcmp(request_obj->get_subject(), CONV_SUBJECT_CONNECTION_START) ) - { - result = connection_manager::handle_request(request_obj); - } - break; - case REQ_UNSUBSCRIBE: - if (!strcmp(request_obj->get_subject(), CONV_SUBJECT_COMMUNICATION_RECV) ) - { - result = service_manager::handle_request(request_obj); - } - else if ( !strcmp(request_obj->get_subject(), CONV_SUBJECT_CONNECTION_START) ) - { - result = connection_manager::handle_request(request_obj); - } - break; - case REQ_READ: - break; - case REQ_READ_SYNC: - break; - case REQ_WRITE: - _D("Requested type : REQ_WRITE.."); - if (!strcmp (request_obj->get_subject(), CONV_SUBJECT_DISCOVERY_START) || !strcmp (request_obj->get_subject(), CONV_SUBJECT_DISCOVERY_STOP)) - { - return discovery_manager::handle_request (request_obj); - } - else if ( !strcmp(request_obj->get_subject(), CONV_SUBJECT_COMMUNICATION_START) || !strcmp(request_obj->get_subject(), CONV_SUBJECT_COMMUNICATION_STOP) - || !strcmp(request_obj->get_subject(), CONV_SUBJECT_COMMUNICATION_SET) || !strcmp(request_obj->get_subject(), CONV_SUBJECT_COMMUNICATION_GET)) - { - result = service_manager::handle_request(request_obj); - } - else if ( !strcmp(request_obj->get_subject(), CONV_SUBJECT_CONNECTION_START) || !strcmp(request_obj->get_subject(), CONV_SUBJECT_CONNECTION_STOP) ) - { - result = connection_manager::handle_request(request_obj); - } - break; - case REQ_SUPPORT: - break; - default: - _E("Invalid type of request"); - request_obj->reply(result); - delete request_obj; + case REQ_SUBSCRIBE: + _D("Requested type : REQ_SUBSCRIBE.."); + if ( !strcmp(request_obj->get_subject(), CONV_SUBJECT_COMMUNICATION_RECV) ) + result = service_manager::handle_request(request_obj); + else if ( !strcmp(request_obj->get_subject(), CONV_SUBJECT_CONNECTION_START) ) + result = connection_manager::handle_request(request_obj); + break; + case REQ_UNSUBSCRIBE: + if (!strcmp(request_obj->get_subject(), CONV_SUBJECT_COMMUNICATION_RECV) ) + result = service_manager::handle_request(request_obj); + else if ( !strcmp(request_obj->get_subject(), CONV_SUBJECT_CONNECTION_START) ) + result = connection_manager::handle_request(request_obj); + break; + case REQ_READ: + break; + case REQ_READ_SYNC: + break; + case REQ_WRITE: + _D("Requested type : REQ_WRITE.."); + if (!strcmp(request_obj->get_subject(), CONV_SUBJECT_DISCOVERY_START) || !strcmp(request_obj->get_subject(), CONV_SUBJECT_DISCOVERY_STOP)) + return discovery_manager::handle_request (request_obj); + else if ( !strcmp(request_obj->get_subject(), CONV_SUBJECT_COMMUNICATION_START) || !strcmp(request_obj->get_subject(), CONV_SUBJECT_COMMUNICATION_STOP) + || !strcmp(request_obj->get_subject(), CONV_SUBJECT_COMMUNICATION_SET) || !strcmp(request_obj->get_subject(), CONV_SUBJECT_COMMUNICATION_GET)) + result = service_manager::handle_request(request_obj); + else if ( !strcmp(request_obj->get_subject(), CONV_SUBJECT_CONNECTION_START) || !strcmp(request_obj->get_subject(), CONV_SUBJECT_CONNECTION_STOP) ) + result = connection_manager::handle_request(request_obj); + break; + case REQ_SUPPORT: + break; + default: + _E("Invalid type of request"); + request_obj->reply(result); + delete request_obj; } return result; diff --git a/daemon/service_mgr_impl.cpp b/daemon/service_mgr_impl.cpp old mode 100644 new mode 100755 index 1d0bc4f..c3b2567 --- a/daemon/service_mgr_impl.cpp +++ b/daemon/service_mgr_impl.cpp @@ -37,10 +37,9 @@ conv::service_manager_impl::~service_manager_impl() int conv::service_manager_impl::init() { - register_provider(new conv::app_comm_service_provider()); - register_provider(new conv::remote_app_control_service_provider()); + IF_FAIL_RETURN_TAG(register_provider(new(std::nothrow) conv::app_comm_service_provider()) == CONV_ERROR_NONE, CONV_ERROR_INVALID_OPERATION, _E, "app_comm_service_provider register failed"); + IF_FAIL_RETURN_TAG(register_provider(new(std::nothrow) conv::remote_app_control_service_provider()) == CONV_ERROR_NONE, CONV_ERROR_INVALID_OPERATION, _E, "remote_app_control_service_provider register failed"); - //TBD register_discovery_info(); return CONV_ERROR_NONE; } @@ -162,14 +161,14 @@ int conv::service_manager_impl::register_provider(conv::service_provider_base *p { if (!provider) { _E("Provider NULL"); - throw static_cast(CONV_ERROR_INVALID_PARAMETER); + return CONV_ERROR_INVALID_PARAMETER; } provider_list.push_back(provider); if (provider->init() != CONV_ERROR_NONE) { _E("Provider initialization failed"); - throw CONV_ERROR_INVALID_OPERATION; + return CONV_ERROR_INVALID_OPERATION; } return CONV_ERROR_NONE; @@ -278,8 +277,7 @@ int conv::service_manager_impl::get_service_info_for_discovery(json* service_jso for (service_provider_list_t::iterator it = provider_list.begin(); it != provider_list.end(); ++it) { json service_info; - if ( (*it)->get_service_info_for_discovery(&service_info) == CONV_ERROR_NONE ) - { + if ((*it)->get_service_info_for_discovery(&service_info) == CONV_ERROR_NONE) { service_json->array_append(NULL, "service_list", service_info); } } @@ -316,8 +314,7 @@ static void iotcon_request_cb(iotcon_resource_h resource, iotcon_request_h reque return; } - if (IOTCON_REQUEST_GET == type) - { + if (IOTCON_REQUEST_GET == type) { iotcon_representation_h resp_repr; _I("GET request"); diff --git a/daemon/service_provider/app_comm_service_info.h b/daemon/service_provider/app_comm_service_info.h old mode 100644 new mode 100755 index c8518ad..1029c40 --- a/daemon/service_provider/app_comm_service_info.h +++ b/daemon/service_provider/app_comm_service_info.h @@ -31,7 +31,7 @@ namespace conv { class app_comm_service_listener : public OnConnectListener, public OnDisconnectListener, public OnClientConnectListener, public OnClientDisconnectListener, public OnMessageListener, public OnErrorListener { public: - conv::request* request_obj; + conv::request** request_obj; string uri; string channel_id; @@ -46,7 +46,7 @@ namespace conv { } void onClientConnect(Client client){ - _D("onClientDisconnect Called"); + _D("onClientConnect Called"); publish_response(CONV_ERROR_NONE, "onClientConnect", &client); } @@ -58,11 +58,10 @@ namespace conv { void onMessage(Message message){ _D("onMessage Called"); - if ( request_obj != NULL ) + if ((*request_obj) != NULL) { _D(RED("publishing_response")); json result; - json payload; json message_json; json description; @@ -74,21 +73,20 @@ namespace conv { message_json.set(NULL, CONV_JSON_FROM, message.m_from); string payload_str(reinterpret_cast(message.m_payload), message.m_payload_size); - json payload_json = payload_str; - message_json.set(NULL, CONV_JSON_PAYLOAD, payload_json); - message_json.set(NULL, CONV_JSON_PAYLOAD_SIZE, message.m_payload_size); + json payload = payload_str; + payload.set(NULL, CONV_JSON_PAYLOAD_SIZE, message.m_payload_size); payload.set(NULL, CONV_JSON_RESULT_TYPE, "onMessage"); payload.set(NULL, CONV_JSON_MESSAGE, message_json); - description = request_obj->get_description(); + description = (*request_obj)->get_description(); description.set(CONV_JSON_CHANNEL, CONV_JSON_URI, uri); description.set(CONV_JSON_CHANNEL, CONV_JSON_CHANNEL_ID, channel_id); result.set(NULL, CONV_JSON_DESCRIPTION, description); result.set(NULL, CONV_JSON_PAYLOAD, payload); - request_obj->publish(CONV_ERROR_NONE, result); + (*request_obj)->publish(CONV_ERROR_NONE, result); } } @@ -101,7 +99,7 @@ namespace conv { bool isHost = client->isHost(); int connecttime = client->getConnectTime(); - if ( request_obj != NULL ) + if ((*request_obj) != NULL) { _D(RED("publishing_response")); json result; @@ -116,14 +114,14 @@ namespace conv { payload.set(NULL, CONV_JSON_RESULT_TYPE, result_type); payload.set(NULL, CONV_JSON_CLIENT, client_json); - description = request_obj->get_description(); + description = (*request_obj)->get_description(); description.set(CONV_JSON_CHANNEL, CONV_JSON_URI, uri); description.set(CONV_JSON_CHANNEL, CONV_JSON_CHANNEL_ID, channel_id); result.set(NULL, CONV_JSON_DESCRIPTION, description); result.set(NULL, CONV_JSON_PAYLOAD, payload); - request_obj->publish(error, result); + (*request_obj)->publish(error, result); } } }; @@ -133,6 +131,7 @@ namespace conv { public: Channel* application; app_comm_service_listener service_listener; + Service local_service; }; typedef vector application_info_list_t; diff --git a/daemon/service_provider/app_comm_service_provider.cpp b/daemon/service_provider/app_comm_service_provider.cpp old mode 100644 new mode 100755 index 344e335..8c1ac53 --- a/daemon/service_provider/app_comm_service_provider.cpp +++ b/daemon/service_provider/app_comm_service_provider.cpp @@ -106,24 +106,21 @@ int conv::app_comm_service_provider::load_service_info(request* request_obj) app_comm_service_info *svc_info = NULL; - if (is_local == 1) - { + if (is_local == 1) { service_info_base* svc_info_base = client_obj->get_service_info(_type, "LOCAL_HOST"); if ( svc_info_base != NULL ) { _D("local service instance already exists"); svc_info = reinterpret_cast(svc_info_base); - if (svc_info == NULL) - { + if (svc_info == NULL) { _D("casting failed"); return CONV_ERROR_INVALID_OPERATION; } - } - else - { + } else { _D("allocating new service instance for local service"); - svc_info = new app_comm_service_info(); + svc_info = new(std::nothrow) app_comm_service_info(); + ASSERT_ALLOC(svc_info); svc_info->is_local = true; client_obj->add_service_info(_type, "LOCAL_HOST", (service_info_base*)svc_info); @@ -137,19 +134,18 @@ int conv::app_comm_service_provider::load_service_info(request* request_obj) _D("service instance already exists"); svc_info = reinterpret_cast(svc_info_base); - if (svc_info == NULL) - { + if (svc_info == NULL) { _D("casting failed"); return CONV_ERROR_INVALID_OPERATION; } - } - else - { + } else { _D("allocating new service instance"); - svc_info = new app_comm_service_info(); + svc_info = new(std::nothrow) app_comm_service_info(); + ASSERT_ALLOC(svc_info); _D("uri : %s", uri.c_str()); - svc_info->service_obj = new Service(id, version, name, type, uri); + svc_info->service_obj = new(std::nothrow) Service(id, version, name, type, uri); + ASSERT_ALLOC(svc_info->service_obj); svc_info->is_local = false; client_obj->add_service_info(_type, id, (service_info_base*)svc_info); @@ -177,10 +173,8 @@ int conv::app_comm_service_provider::start_request(request* request_obj) for (application_info_list_t::iterator iter = svc_info->application_info_list.begin(); iter != svc_info->application_info_list.end(); ++iter) { _D("iteration"); - if ( (*iter) != NULL && !(*iter)->service_listener.uri.compare(uri) && !(*iter)->service_listener.channel_id.compare(channel_id) ) - { - if ( (*iter)->application != NULL ) - { + if ( (*iter) != NULL && !(*iter)->service_listener.uri.compare(uri) && !(*iter)->service_listener.channel_id.compare(channel_id) ) { + if ( (*iter)->application != NULL ) { _D("already started"); // check if it's connected and re-try if it's not return CONV_ERROR_INVALID_OPERATION; @@ -192,22 +186,24 @@ int conv::app_comm_service_provider::start_request(request* request_obj) } } - if ( app_info == NULL ) - { - app_info = new application_info(); + if ( app_info == NULL ) { + app_info = new(std::nothrow) application_info(); + ASSERT_ALLOC(app_info); app_info->service_listener.uri = uri; app_info->service_listener.channel_id = channel_id; } // if it's local -> server application - if ( svc_info->is_local ) - { + if ( svc_info->is_local ) { _D("COMMUNCATION_START : local channel. channel_id : %s", channel_id.c_str()); - Channel* application = Service::getLocal().createChannel(channel_id); + + app_info->local_service = Service::getLocal(); + + Channel* application = app_info->local_service.createChannel(channel_id); // add listeners - app_info->service_listener.request_obj = svc_info->registered_request; + app_info->service_listener.request_obj = &(svc_info->registered_request); application->setonConnectListener(&app_info->service_listener); application->setonClientConnectListener(&app_info->service_listener); application->setonClientDisconnectListener(&app_info->service_listener); @@ -222,10 +218,11 @@ int conv::app_comm_service_provider::start_request(request* request_obj) svc_info->application_info_list.push_back(app_info); } else { _D("COMMUNCATION_START : uri : %s, channel_id : %s", uri.c_str(), channel_id.c_str()); - Application* application = new Application(svc_info->service_obj, uri, channel_id); + Application* application = new(std::nothrow) Application(svc_info->service_obj, uri, channel_id); + ASSERT_ALLOC(application); // add listeners - app_info->service_listener.request_obj = svc_info->registered_request; + app_info->service_listener.request_obj = &(svc_info->registered_request); application->setonConnectListener(&app_info->service_listener); application->setonClientConnectListener(&app_info->service_listener); application->setonClientDisconnectListener(&app_info->service_listener); @@ -260,19 +257,16 @@ int conv::app_comm_service_provider::stop_request(request* request_obj) for (application_info_list_t::iterator iter = svc_info->application_info_list.begin(); iter != svc_info->application_info_list.end(); ++iter) { _D("%s, %s", (*iter)->service_listener.uri.c_str(), (*iter)->service_listener.channel_id.c_str()); - if ( (*iter) != NULL && !(*iter)->service_listener.uri.compare(uri) && !(*iter)->service_listener.channel_id.compare(channel_id) ) - { + if ( (*iter) != NULL && !(*iter)->service_listener.uri.compare(uri) && !(*iter)->service_listener.channel_id.compare(channel_id) ) { application_info *app_info = *iter; _D("COMMUNCATION_STOP : uri : %s, channel_id : %s", uri.c_str(), channel_id.c_str()); - if ( *iter == NULL ) - { + if ( *iter == NULL ) { _D("iter is NULL"); } - if ( svc_info->is_local ) - { + if ( svc_info->is_local ) { result_disconnect.app_info = app_info; app_info->application->set_disconnect_result(NULL); app_info->application->disconnect(); @@ -316,17 +310,14 @@ int conv::app_comm_service_provider::get_request(request* request_obj) for (application_info_list_t::iterator iter = svc_info->application_info_list.begin(); iter != svc_info->application_info_list.end(); ++iter) { _D("iteration"); - if ( (*iter) != NULL && !(*iter)->service_listener.uri.compare(uri) && !(*iter)->service_listener.channel_id.compare(channel_id) ) - { - if ( (*iter)->application != NULL ) - { + if ( (*iter) != NULL && !(*iter)->service_listener.uri.compare(uri) && !(*iter)->service_listener.channel_id.compare(channel_id) ) { + if ( (*iter)->application != NULL ) { _D("app_info exists and application instance exists"); app_info = (*iter); Clients* client_list = app_info->application->getclients(); - if ( svc_info->registered_request == NULL) - { + if ( svc_info->registered_request == NULL) { _D("No callback is registered"); request_obj->reply(CONV_ERROR_INVALID_OPERATION); delete request_obj; @@ -382,11 +373,10 @@ int conv::app_comm_service_provider::send_response(json payload, request* reques { _D(RED("publishing_response")); json result; - json description; - payload.set(NULL, CONV_JSON_RESULT_TYPE, "getClient"); + json description = request_obj->get_description(); - description = request_obj->get_description(); + payload.set(NULL, CONV_JSON_RESULT_TYPE, "getClient"); result.set(NULL, CONV_JSON_DESCRIPTION, description); result.set(NULL, CONV_JSON_PAYLOAD, payload); @@ -410,19 +400,18 @@ int conv::app_comm_service_provider::set_request(request* request_obj) for (application_info_list_t::iterator iter = svc_info->application_info_list.begin(); iter != svc_info->application_info_list.end(); ++iter) { _D("iteration"); - if ( (*iter) != NULL && !(*iter)->service_listener.uri.compare(uri) && !(*iter)->service_listener.channel_id.compare(channel_id) ) - { - if ( (*iter)->application != NULL ) - { + if ( (*iter) != NULL && !(*iter)->service_listener.uri.compare(uri) && !(*iter)->service_listener.channel_id.compare(channel_id) ) { + if ( (*iter)->application != NULL ) { _D("publishing payload"); json payload; request_obj->get_payload_from_description(&payload); - char* message = new char[strlen(payload.str().c_str())+1]; + char* message = new(std::nothrow) char[strlen(payload.str().c_str())+1]; + ASSERT_ALLOC(message); - strcpy(message, payload.str().c_str()); + strncpy(message, payload.str().c_str(), strlen(payload.str().c_str())); string payload_str = payload.str(); @@ -430,7 +419,7 @@ int conv::app_comm_service_provider::set_request(request* request_obj) _D("publishing done"); - delete message; + delete[] message; return CONV_ERROR_NONE; } } @@ -444,11 +433,27 @@ int conv::app_comm_service_provider::register_request(request* request_obj) { _D("communcation/recv requested"); app_comm_service_info *svc_info = reinterpret_cast(request_obj->service_info); - svc_info->registered_request = request_obj; - - request_obj->reply(CONV_ERROR_NONE); - _D("subscribe requested"); + switch (request_obj->get_type()) { + case REQ_SUBSCRIBE: + if ( svc_info->registered_request != NULL ) { + delete svc_info->registered_request; + } + svc_info->registered_request = request_obj; + request_obj->reply(CONV_ERROR_NONE); + _D("subscribe requested"); + break; + case REQ_UNSUBSCRIBE: + svc_info->registered_request = NULL; + request_obj->reply(CONV_ERROR_NONE); + delete request_obj; + break; + default: + request_obj->reply(CONV_ERROR_INVALID_OPERATION); + delete request_obj; + return CONV_ERROR_INVALID_OPERATION; + break; + } return CONV_ERROR_NONE; } @@ -463,27 +468,29 @@ int conv::app_comm_service_provider::get_service_info_for_discovery(json* json_o char* address = NULL; int ret = connection_get_ip_address(connection, CONNECTION_ADDRESS_FAMILY_IPV4, &address); - if(ret != CONNECTION_ERROR_NONE) - { + if(ret != CONNECTION_ERROR_NONE) { _E("connection error"); - if (connection_get_ip_address(connection, CONNECTION_ADDRESS_FAMILY_IPV6, &address) != CONNECTION_ERROR_NONE) - { + if (connection_get_ip_address(connection, CONNECTION_ADDRESS_FAMILY_IPV6, &address) != CONNECTION_ERROR_NONE) { _E("connection error"); connection_destroy(connection); return CONV_ERROR_NOT_SUPPORTED; } } - if ( address == NULL || strlen(address) < 1 ) - { + if ( address == NULL || strlen(address) < 1 ) { _E("connection error"); connection_destroy(connection); return CONV_ERROR_NOT_SUPPORTED; } else { - char uri[1000]; + char uri[200]; Service local_service = Service::getLocal(); - sprintf(uri, "http://%s:8001/api/v2/", address); + if (local_service.getUri().empty()) { + g_free(address); + return CONV_ERROR_NOT_SUPPORTED; + } + + snprintf(uri, sizeof(uri), "http://%s:8001/api/v2/", address); json info; diff --git a/daemon/service_provider/remote_app_control_service_provider.cpp b/daemon/service_provider/remote_app_control_service_provider.cpp old mode 100644 new mode 100755 index 5718f54..5bb1400 --- a/daemon/service_provider/remote_app_control_service_provider.cpp +++ b/daemon/service_provider/remote_app_control_service_provider.cpp @@ -163,8 +163,7 @@ static void handle_request(iotcon_representation_h rep, iotcon_request_h request iotcon_state_get_int(state, CONV_JSON_REPLY, &reply); - if ( reply == 1 ) - { + if (reply == 1) { bool waiting_reply = false; // check if it's already launched and waiting for app_control response @@ -177,8 +176,7 @@ static void handle_request(iotcon_representation_h rep, iotcon_request_h request } } - if ( !waiting_reply ) - { + if (!waiting_reply) { int req_id; int reply_id = get_req_id(); @@ -198,8 +196,7 @@ static void handle_request(iotcon_representation_h rep, iotcon_request_h request iotcon_response_result_e result; ret = app_control_send_launch_request(app_control, NULL, NULL); - if (ret != APP_CONTROL_ERROR_NONE) - { + if (ret != APP_CONTROL_ERROR_NONE) { _E("Launch request failed(%d)", ret); result = IOTCON_RESPONSE_ERROR; } else { @@ -240,8 +237,7 @@ void conv::remote_app_control_service_provider::iotcon_request_cb(iotcon_resourc _D("request type : %d", type); - if (IOTCON_REQUEST_PUT == type) - { + if (IOTCON_REQUEST_PUT == type) { iotcon_representation_h req_repr; _I("GET request"); @@ -314,8 +310,7 @@ int conv::remote_app_control_service_provider::start_request(request* request_ob remote_app_control_service_info *svc_info = reinterpret_cast(request_obj->service_info); - if ( svc_info->iotcon_info_obj.iotcon_resource_handle != NULL ) - { + if (svc_info->iotcon_info_obj.iotcon_resource_handle != NULL) { _D("already started"); return CONV_ERROR_INVALID_OPERATION; } @@ -352,8 +347,7 @@ int conv::remote_app_control_service_provider::stop_request(request* request_obj remote_app_control_service_info *svc_info = reinterpret_cast(request_obj->service_info); - if ( svc_info->iotcon_info_obj.iotcon_resource_handle == NULL ) - { + if (svc_info->iotcon_info_obj.iotcon_resource_handle == NULL) { _D("not even started"); return CONV_ERROR_INVALID_OPERATION; } @@ -415,17 +409,15 @@ static void on_response(iotcon_remote_resource_h resource, iotcon_error_e err, } _D(RED("publishing_response")); - json result; - json payload; - json description; + if (cb_info.request_obj) { + json result; + json payload; + json description; - payload.set(NULL, CONV_JSON_APP_CONTROL, appctl_char); + payload.set(NULL, CONV_JSON_APP_CONTROL, appctl_char); + result.set(NULL, CONV_JSON_DESCRIPTION, cb_info.request_obj->get_description()); + result.set(NULL, CONV_JSON_PAYLOAD, payload); - result.set(NULL, CONV_JSON_DESCRIPTION, cb_info.request_obj->get_description()); - result.set(NULL, CONV_JSON_PAYLOAD, payload); - - if ( cb_info.request_obj ) - { cb_info.request_obj->publish(CONV_ERROR_NONE, result); _D("response published"); } @@ -457,8 +449,7 @@ int conv::remote_app_control_service_provider::set_request(request* request_obj) iotcon_state_add_str(state, CONV_JSON_APP_CONTROL, (char*)app_control.c_str()); iotcon_state_add_int(state, CONV_JSON_REPLY, reply); - if ( reply == 1 ) - { + if (reply == 1) { int req_id = get_req_id(); iotcon_state_add_int(state, CONV_JSON_REQ_ID, req_id); @@ -484,10 +475,27 @@ int conv::remote_app_control_service_provider::register_request(request* request { _D("communcation/recv requested"); remote_app_control_service_info *svc_info = reinterpret_cast(request_obj->service_info); - svc_info->registered_request = request_obj; - request_obj->reply(CONV_ERROR_NONE); - _D("subscribe requested"); + switch (request_obj->get_type()) { + case REQ_SUBSCRIBE: + if (svc_info->registered_request != NULL) { + delete svc_info->registered_request; + } + svc_info->registered_request = request_obj; + request_obj->reply(CONV_ERROR_NONE); + _D("subscribe requested"); + break; + case REQ_UNSUBSCRIBE: + svc_info->registered_request = NULL; + request_obj->reply(CONV_ERROR_NONE); + delete request_obj; + break; + default: + request_obj->reply(CONV_ERROR_INVALID_OPERATION); + delete request_obj; + return CONV_ERROR_INVALID_OPERATION; + break; + } return CONV_ERROR_NONE; } @@ -524,7 +532,7 @@ int conv::remote_app_control_service_provider::load_service_info(request* reques remote_app_control_service_info *svc_info = NULL; service_info_base* svc_info_base = client_obj->get_service_info(_type, device_id); - if ( svc_info_base != NULL ) { + if (svc_info_base != NULL) { _D("service instance already exists"); svc_info = reinterpret_cast(svc_info_base); @@ -533,11 +541,10 @@ int conv::remote_app_control_service_provider::load_service_info(request* reques _D("casting failed"); return CONV_ERROR_INVALID_OPERATION; } - } - else - { + } else { _D("allocating new service instance"); - svc_info = new remote_app_control_service_info(); + svc_info = new(std::nothrow) remote_app_control_service_info(); + IF_FAIL_RETURN_TAG(svc_info, CONV_ERROR_OUT_OF_MEMORY, _E, "svc_info alloc failed"); svc_info->device_id = device_id; svc_info->device_name = device_name; diff --git a/daemon/util.cpp b/daemon/util.cpp old mode 100644 new mode 100755 index 8dc4510..46e5452 --- a/daemon/util.cpp +++ b/daemon/util.cpp @@ -28,7 +28,7 @@ using namespace std; std::string conv::util::get_bt_mac_address() { static std::string g_mac_address; - if(g_mac_address.empty()){ + if(g_mac_address.empty()) { bt_initialize(); char* mac_address; bt_adapter_enable(); @@ -49,9 +49,7 @@ std::string conv::util::get_device_name() static std::string g_device_name; if(g_device_name.empty()) { char* device_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR); - //jhp27.park - if (device_name == NULL) - { + if (device_name == NULL) { g_device_name = "Tizen"; } else { g_device_name = device_name; @@ -82,17 +80,14 @@ static char make_p2p_mac(char c) std::string conv::util::get_p2p_mac_address() { static std::string g_p2p_mac_address; - if(g_p2p_mac_address.empty()){ + if(g_p2p_mac_address.empty()) { char p2p_mac[MAC_ADDR_STR_LEN]; memset(p2p_mac, 0x0, MAC_ADDR_STR_LEN); char* temp_addr = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS); - if (temp_addr == NULL) - { + if (temp_addr == NULL) { _E("vconf_get_str Failed for %s", VCONFKEY_WIFI_BSSID_ADDRESS); - } - else - { + } else { memcpy(p2p_mac, temp_addr, MAC_ADDR_STR_LEN-1); p2p_mac[1] = make_p2p_mac(p2p_mac[1]); _I("P2P mac is %s", p2p_mac); @@ -109,8 +104,7 @@ static gboolean misc_timer_worker(gpointer ud) { _D("timer_work.."); gpointer *tdata = (gpointer*)ud; - if (tdata[0]) - { + if (tdata[0]) { ((conv::util::timer_function)tdata[0])(tdata[1]); } return TRUE; @@ -124,10 +118,8 @@ void* conv::util::misc_start_timer(timer_function function, unsigned int interva _D("misc_start_timer with interval[%d]", interval); src = g_timeout_source_new(interval*1000); - //ASSERT(src != NULL); tdata = g_new0(gpointer, 2); - //ASSERT(tdata != NULL); tdata[0] = (void*)function; tdata[1] = data; @@ -144,10 +136,8 @@ void conv::util::misc_stop_timer(void *timer) { guint id = (guint) timer; _D("Requested Stop Timer[%d]", id); - if (id) - { - if (!g_source_remove(id)) - { + if (id) { + if (!g_source_remove(id)) { _E("g_source_remove is fail (timer)"); } } diff --git a/lib/conv_lib.cpp b/lib/conv_lib.cpp old mode 100644 new mode 100755 index 3faa678..4d2917b --- a/lib/conv_lib.cpp +++ b/lib/conv_lib.cpp @@ -38,13 +38,12 @@ struct device_callback_info_s { typedef std::map callback_map_t; static callback_map_t callback_map; -//static device_callback_info_s* device_callback; static void conv_subject_cb(const char* subject, int req_id, int error, json data){ _D("Callback response %d : subject[%s] json_data[%s]", req_id, subject, data.str().c_str()); - conv_device_h device = new (std::nothrow) _conv_device_handle_s(); + conv_device_h device = new(std::nothrow) _conv_device_handle_s(); IF_FAIL_VOID_TAG(device, _E, "Memory Allocation Failed"); device->jbody = data; @@ -89,8 +88,7 @@ EXTAPI int conv_destroy(conv_h handle) conv::dbus_client::release(); std::list::iterator itr_end = handle->request_ids.end(); callback_map_t::iterator map_iter_end = callback_map.end(); - for (std::list::iterator iter_pos = handle->request_ids.begin(); iter_pos != itr_end; iter_pos++) - { + for (std::list::iterator iter_pos = handle->request_ids.begin(); iter_pos != itr_end; iter_pos++) { callback_map_t::iterator map_iter = callback_map.find(*iter_pos); if (map_iter != map_iter_end) callback_map.erase(map_iter); @@ -105,9 +103,6 @@ EXTAPI int conv_discovery_start(conv_h handle, const int timeout_seconds, conv_d ASSERT_NOT_NULL(handle); ASSERT_NOT_NULL(callback); - //temp - //conv_set_discovery_cb(handle, callback, user_data); - register_subject_callbacks(); device_callback_info_s *cb_info = new(std::nothrow)device_callback_info_s(); @@ -143,11 +138,9 @@ EXTAPI int conv_discovery_stop(conv_h handle) // unset callback.. std::list::iterator req_itr = handle->request_ids.begin(); - for (; req_itr != handle->request_ids.end(); req_itr++) - { + for (; req_itr != handle->request_ids.end(); req_itr++) { int cur_req_id = *req_itr; - if (callback_map[cur_req_id] != NULL) - { + if (callback_map[cur_req_id] != NULL) { _D("free memory for callback[id:%d]", cur_req_id); delete callback_map[cur_req_id]; callback_map.erase(callback_map.find(cur_req_id)); @@ -163,7 +156,7 @@ EXTAPI int conv_device_clone(conv_device_h original_handle, conv_device_h* targe IF_FAIL_RETURN_TAG(conv::util::is_feature_supported(), CONV_ERROR_NOT_SUPPORTED, _E, "Not supported"); ASSERT_NOT_NULL(original_handle); ASSERT_NOT_NULL(target_handle); - _conv_device_handle* device = new (std::nothrow) _conv_device_handle(); + _conv_device_handle* device = new(std::nothrow) _conv_device_handle(); device->jbody = original_handle->jbody; *target_handle = device; @@ -187,7 +180,8 @@ EXTAPI int conv_device_get_property_string(conv_device_h handle, const char* key std::string strval; bool ret = handle->jbody.get(NULL, key, &strval); // path is NULL.. - if (ret == false || strval.empty()) return CONV_ERROR_NO_DATA; + if (ret == false || strval.empty()) + return CONV_ERROR_NO_DATA; *value = strdup(strval.c_str()); return CONV_ERROR_NONE; @@ -196,27 +190,23 @@ EXTAPI int conv_device_get_property_string(conv_device_h handle, const char* key EXTAPI int conv_device_foreach_service(conv_device_h handle, conv_service_foreach_cb cb, void* user_data) { IF_FAIL_RETURN_TAG(conv::util::is_feature_supported(), CONV_ERROR_NOT_SUPPORTED, _E, "Not supported"); + ASSERT_NOT_NULL(handle); + ASSERT_NOT_NULL(cb); + json json_data = handle->jbody; std::string strval; int service_count = 0; service_count = json_data.array_get_size(CONV_JSON_SERVICE_PATH, CONV_JSON_SERVICE_DATA); // json data.. - for (int index =0; index < service_count; index++) - { - _conv_service_handle* service = new (std::nothrow) _conv_service_handle(); + for (int index =0; index < service_count; index++) { + _conv_service_handle* service = new(std::nothrow) _conv_service_handle(); ASSERT_ALLOC(service); // service json data json json_service; std::string version, type, uri; conv_service_e service_type; - /* - json_data.get_array_elem (CONV_JSON_SERVICE_PATH, CONV_JSON_SERVICE_DATA_VERSION, index, &version); - json_data.get_array_elem (CONV_JSON_SERVICE_PATH, CONV_JSON_SERVICE_DATA_TYPE, index, &type); - json_data.get_array_elem (CONV_JSON_SERVICE_PATH, CONV_JSON_SERVICE_DATA_URI, index, &uri); - json_data.get_array_elem (CONV_JSON_SERVICE_PATH, CONV_JSON_SERVICE_TYPE, index, (int*)&service_type); - */ json_data.get_array_elem(CONV_JSON_SERVICE_PATH, CONV_JSON_SERVICE_DATA, index, &json_service); json_data.get_array_elem(CONV_JSON_SERVICE_PATH, CONV_JSON_SERVICE_TYPE, index, (int*)&service_type); @@ -227,11 +217,8 @@ EXTAPI int conv_device_foreach_service(conv_device_h handle, conv_service_foreac _D("index[%d] service json : %s", index, json_service.str().c_str()); - //typedef void(* conv_service_foreach_cb)(conv_service_h service_handle, void* user_data); cb(service, user_data); delete service; - - // service json data includes features like name, version, type, uri and id.. } return CONV_ERROR_NONE; diff --git a/lib/conv_lib_channel.cpp b/lib/conv_lib_channel.cpp old mode 100644 new mode 100755 index 28a72f5..73535b7 --- a/lib/conv_lib_channel.cpp +++ b/lib/conv_lib_channel.cpp @@ -25,6 +25,7 @@ #include "common.h" #include "d2d_conv_manager.h" +#include "d2d_conv_internal.h" #include "conv_lib_json.h" #include "internal_types.h" #include "dbus_client.h" @@ -84,3 +85,15 @@ EXTAPI int conv_channel_get_string(conv_channel_h handle, const char* key, char* return CONV_ERROR_NONE; } +// internal API +EXTAPI int conv_channel_internal_export_to_string(conv_channel_h handle, char** value) +{ + IF_FAIL_RETURN_TAG(conv::util::is_feature_supported(), CONV_ERROR_NOT_SUPPORTED, _E, "Not supported"); + ASSERT_NOT_NULL(handle); + ASSERT_NOT_NULL(value); + + *value = g_strdup(handle->jchannel.str().c_str()); + ASSERT_ALLOC(*value); + + return CONV_ERROR_NONE; +} diff --git a/lib/conv_lib_json.cpp b/lib/conv_lib_json.cpp old mode 100644 new mode 100755 index 08d3141..24644bf --- a/lib/conv_lib_json.cpp +++ b/lib/conv_lib_json.cpp @@ -69,20 +69,18 @@ json::json(const json& j) json::json(const char* s) { - if (s) { + if (s) parse(s); - } else { + else parse(EMPTY_JSON_OBJECT); - } } json::json(const std::string& s) { - if (s.empty()) { + if (s.empty()) parse(EMPTY_JSON_OBJECT); - } else { + else parse(s.c_str()); - } } json::~json() @@ -125,31 +123,31 @@ json& json::operator=(const json& j) { release(); json_node = json_node_copy(j.json_node); - if (!json_node) { + if (!json_node) _E("Json object copy failed"); - } + return *this; } json& json::operator=(const char* s) { release(); - if (s) { + if (s) parse(s); - } else { + else parse(EMPTY_JSON_OBJECT); - } + return *this; } json& json::operator=(const std::string& s) { release(); - if (s.empty()) { + if (s.empty()) parse(EMPTY_JSON_OBJECT); - } else { + else parse(s.c_str()); - } + return *this; } @@ -163,13 +161,6 @@ bool json::operator!=(const json& rhs) return !operator==(rhs); } -/* TODO -bool json::contains(const json& subset) const -{ - return false; -} -*/ - char* json::dup_cstr() { IF_FAIL_RETURN_TAG(json_node, NULL, _E, "Json object not initialized"); @@ -188,9 +179,8 @@ char* json::dup_cstr() return output; CATCH: - if (jgen) { + if (jgen) g_object_unref(jgen); - } _E("Memory allocation failed"); return NULL; @@ -225,11 +215,9 @@ static char** tokenize_path(const char* path, int* length) *length = 1; - for (pch = path; *pch != '\0'; pch++) { - if (*pch == PATH_DELIM) { + for (pch = path; *pch != '\0'; pch++) + if (*pch == PATH_DELIM) *length = *length + 1; - } - } tokens = static_cast(g_malloc((*length) * sizeof(char*))); IF_FAIL_RETURN_TAG(tokens, NULL, _E, "Memory allocation failed"); @@ -248,17 +236,16 @@ static char** tokenize_path(const char* path, int* length) begin = pch + 1; } - if (*pch == '\0') { + if (*pch == '\0') break; - } } return tokens; CATCH: - for (j = 0; j < i; j++) { + for (j = 0; j < i; j++) g_free(tokens[j]); - } + g_free(tokens); return NULL; } @@ -267,9 +254,9 @@ static void free_tokenized_path(int length, char** tokens) { int i; if (tokens) { - for (i = 0; i < length; i++) { + for (i = 0; i < length; i++) g_free(tokens[i]); - } + g_free(tokens); } } @@ -303,6 +290,7 @@ static JsonObject* traverse(JsonNode* jnode, const char* path, bool force) goto CATCH; } } + child_node = json_object_get_member(jobj, path_token[depth]); IF_FAIL_CATCH(child_node && json_node_get_node_type(child_node) == JSON_NODE_OBJECT); @@ -364,9 +352,8 @@ bool json::set(const char* path, const char* key, double val, int prec) JsonObject *jobj = traverse(json_node, path, true); IF_FAIL_RETURN(jobj, false); - if (json_object_has_member(jobj, key)) { + if (json_object_has_member(jobj, key)) json_object_remove_member(jobj, key); - } //NOTE: json-glib causes a precision issue while handling double values json_object_set_string_member(jobj, key, double_to_string(val, prec).c_str()); @@ -381,9 +368,8 @@ bool json::set(const char* path, const char* key, std::string val) JsonObject *jobj = traverse(json_node, path, true); IF_FAIL_RETURN(jobj, false); - if (json_object_has_member(jobj, key)) { + if (json_object_has_member(jobj, key)) json_object_remove_member(jobj, key); - } json_object_set_string_member(jobj, key, val.c_str()); return true; @@ -415,9 +401,8 @@ bool json::remove(const char* path, const char* key) JsonObject *jobj = traverse(json_node, path, true); IF_FAIL_RETURN(jobj, false); - if (json_object_has_member(jobj, key)) { - json_object_remove_member(jobj, key); - } + if (json_object_has_member(jobj, key)) + json_object_remove_member(jobj, key); return true; } @@ -436,9 +421,9 @@ bool json::get(const char* path, const char* key, json* val) node = json_object_dup_member(jobj, key); IF_FAIL_RETURN_TAG(node, false, _E, "Memory allocation failed"); - if (val->json_node) { + if (val->json_node) json_node_free(val->json_node); - } + val->json_node = node; return true; @@ -484,14 +469,13 @@ bool json::get(const char* path, const char* key, int64_t* val) IF_FAIL_RETURN(node, false); GType vtype = json_node_get_value_type(node); - if (vtype == G_TYPE_INT64) { + if (vtype == G_TYPE_INT64) *val = json_node_get_int(node); - } else if (vtype == G_TYPE_STRING) { + else if (vtype == G_TYPE_STRING) //TODO: if the string is not a number? *val = static_cast(string_to_double(json_node_get_string(node))); - } else { + else return false; - } return true; } @@ -505,16 +489,15 @@ bool json::get(const char* path, const char* key, double* val) IF_FAIL_RETURN(node, false); GType vtype = json_node_get_value_type(node); - if (vtype == G_TYPE_DOUBLE) { + if (vtype == G_TYPE_DOUBLE) *val = json_node_get_double(node); - } else if (vtype == G_TYPE_INT64) { + else if (vtype == G_TYPE_INT64) *val = json_node_get_int(node); - } else if (vtype == G_TYPE_STRING) { + else if (vtype == G_TYPE_STRING) //NOTE: json-glib causes a precision issue while handling double values *val = string_to_double(json_node_get_string(node)); - } else { + else return false; - } return true; } @@ -581,6 +564,7 @@ static JsonArray* search_array(JsonNode* jnode, const char* path, const char* ke return NULL; } } + node = json_object_get_member(jobj, key); IF_FAIL_RETURN_TAG(node && json_node_get_node_type(node) == JSON_NODE_ARRAY, NULL, _W, "Type mismatched: %s", key); @@ -748,9 +732,9 @@ bool json::get_array_elem(const char* path, const char* key, int index, json* va JsonNode *node_copy = json_node_copy(node); IF_FAIL_RETURN_TAG(node_copy, false, _E, "Memory allocation failed"); - if (val->json_node) { + if (val->json_node) json_node_free(val->json_node); - } + val->json_node = node_copy; return true; @@ -884,18 +868,18 @@ bool json::node_equals(json_node_t* lhs, json_node_t* rhs) IF_FAIL_RETURN(ltype == rtype, false); switch (ltype) { - case JSON_NODE_VALUE: - IF_FAIL_RETURN(value_equals(lhs, rhs), false); - break; - case JSON_NODE_OBJECT: - IF_FAIL_RETURN(object_equals(lhs, rhs), false); - break; - case JSON_NODE_ARRAY: - IF_FAIL_RETURN(array_equals(lhs, rhs), false); - break; - default: - _W("Unsupported type"); - return false; + case JSON_NODE_VALUE: + IF_FAIL_RETURN(value_equals(lhs, rhs), false); + break; + case JSON_NODE_OBJECT: + IF_FAIL_RETURN(object_equals(lhs, rhs), false); + break; + case JSON_NODE_ARRAY: + IF_FAIL_RETURN(array_equals(lhs, rhs), false); + break; + default: + _W("Unsupported type"); + return false; } return true; @@ -908,15 +892,15 @@ bool json::value_equals(json_node_t* lhs, json_node_t* rhs) IF_FAIL_RETURN(ltype == rtype, false); switch (ltype) { - case G_TYPE_INT64: - return json_node_get_int(lhs) == json_node_get_int(rhs); - case G_TYPE_DOUBLE: - return json_node_get_double(lhs) == json_node_get_double(rhs); - case G_TYPE_STRING: - return STR_EQ(json_node_get_string(lhs), json_node_get_string(rhs)); - default: - _W("Unsupported type"); - return false; + case G_TYPE_INT64: + return json_node_get_int(lhs) == json_node_get_int(rhs); + case G_TYPE_DOUBLE: + return json_node_get_double(lhs) == json_node_get_double(rhs); + case G_TYPE_STRING: + return STR_EQ(json_node_get_string(lhs), json_node_get_string(rhs)); + default: + _W("Unsupported type"); + return false; } } diff --git a/lib/conv_lib_payload.cpp b/lib/conv_lib_payload.cpp old mode 100644 new mode 100755 index fa9fa55..2ba113e --- a/lib/conv_lib_payload.cpp +++ b/lib/conv_lib_payload.cpp @@ -28,6 +28,7 @@ #include "common.h" #include "d2d_conv_manager.h" +#include "d2d_conv_internal.h" #include "conv_lib_json.h" #include "internal_types.h" #include "dbus_client.h" @@ -177,3 +178,16 @@ EXTAPI int conv_payload_get_byte(conv_payload_h handle, const char* key, int* le return CONV_ERROR_NONE; } + +// internal API +EXTAPI int conv_payload_internal_export_to_string(conv_payload_h handle, char** value) +{ + IF_FAIL_RETURN_TAG(conv::util::is_feature_supported(), CONV_ERROR_NOT_SUPPORTED, _E, "Not supported"); + ASSERT_NOT_NULL(handle); + ASSERT_NOT_NULL(value); + + *value = g_strdup(handle->jpayload.str().c_str()); + ASSERT_ALLOC(*value); + + return CONV_ERROR_NONE; +} diff --git a/lib/conv_lib_service.cpp b/lib/conv_lib_service.cpp old mode 100644 new mode 100755 index 112e1c5..cc6b496 --- a/lib/conv_lib_service.cpp +++ b/lib/conv_lib_service.cpp @@ -60,6 +60,8 @@ static void conv_subject_cb(const char* subject, int req_id, int error, json dat key.set(NULL, CONV_JSON_SERVICE, service); key.set(NULL, CONV_JSON_TYPE, service_type); + key.set(NULL, CONV_JSON_IS_LOCAL, is_local); + _D("key:%s", key.str().c_str()); std::map::iterator it = callback_map.find(key.str()); if (it == callback_map.end()) { @@ -162,6 +164,7 @@ EXTAPI int conv_service_create(conv_service_h* handle) ASSERT_ALLOC(*handle); (*handle)->is_local = 1; + (*handle)->service_type = CONV_SERVICE_NONE; (*handle)->connection_state = CONV_SERVICE_CONNECTION_STATE_CONNECTED; return CONV_ERROR_NONE; @@ -191,9 +194,7 @@ EXTAPI int conv_service_destroy(conv_service_h handle) ASSERT_NOT_NULL(handle); if ( handle->callback ) - { delete handle->callback; - } delete handle; @@ -213,9 +214,12 @@ EXTAPI int conv_service_set_listener_cb(conv_service_h handle, conv_service_list json service = handle->jservice; std::string type = convert_type_to_string(handle->service_type); + if (type.empty()) + return CONV_ERROR_INVALID_PARAMETER; description.set(NULL, CONV_JSON_SERVICE, service); description.set(NULL, CONV_JSON_TYPE, type); + description.set(NULL, CONV_JSON_IS_LOCAL, handle->is_local); _conv_service_callback_info *cb_info = new(std::nothrow)_conv_service_callback_info(); cb_info->cb = callback; @@ -245,12 +249,15 @@ EXTAPI int conv_service_unset_listener_cb(conv_service_h handle) json service = handle->jservice; std::string type = convert_type_to_string(handle->service_type); + if (type.empty()) + return CONV_ERROR_INVALID_PARAMETER; description.set(NULL, CONV_JSON_SERVICE, service); description.set(NULL, CONV_JSON_TYPE, type); + description.set(NULL, CONV_JSON_IS_LOCAL, handle->is_local); int req_id; - int err = conv::dbus_client::request(REQ_UNSUBSCRIBE, &req_id, CONV_SUBJECT_COMMUNICATION_RECV, NULL, NULL, NULL); + int err = conv::dbus_client::request(REQ_UNSUBSCRIBE, &req_id, CONV_SUBJECT_COMMUNICATION_RECV, description.str().c_str(), NULL, NULL); IF_FAIL_RETURN_TAG(err == CONV_ERROR_NONE, err, _E, "Unset observe failed"); std::map::iterator it = callback_map.find(description.str()); @@ -278,13 +285,13 @@ EXTAPI int conv_service_start(conv_service_h handle, conv_channel_h channel_hand json channel; if ( channel_handle != NULL ) - { channel = channel_handle->jchannel; - } json service = handle->jservice; json device = handle->jdevice; std::string type = convert_type_to_string(handle->service_type); + if (type.empty()) + return CONV_ERROR_INVALID_PARAMETER; description.set(NULL, CONV_JSON_SERVICE, service); description.set(NULL, CONV_JSON_CHANNEL, channel); @@ -298,7 +305,7 @@ EXTAPI int conv_service_start(conv_service_h handle, conv_channel_h channel_hand return CONV_ERROR_NONE; } -EXTAPI int conv_service_get(conv_service_h handle, conv_channel_h channel_handle, conv_payload_h payload_handle) +EXTAPI int conv_service_read(conv_service_h handle, conv_channel_h channel_handle, conv_payload_h payload_handle) { IF_FAIL_RETURN_TAG(conv::util::is_feature_supported(), CONV_ERROR_NOT_SUPPORTED, _E, "Not supported"); ASSERT_NOT_NULL(handle); @@ -309,18 +316,17 @@ EXTAPI int conv_service_get(conv_service_h handle, conv_channel_h channel_handle json channel; if ( channel_handle != NULL ) - { channel = channel_handle->jchannel; - } json payload; - if ( payload_handle != NULL) { + if ( payload_handle != NULL) payload = payload_handle->jpayload; - } json service = handle->jservice; std::string type = convert_type_to_string(handle->service_type); + if (type.empty()) + return CONV_ERROR_INVALID_PARAMETER; description.set(NULL, CONV_JSON_SERVICE, service); description.set(NULL, CONV_JSON_CHANNEL, channel); @@ -345,13 +351,13 @@ EXTAPI int conv_service_stop(conv_service_h handle, conv_channel_h channel_handl json channel; if ( channel_handle != NULL ) - { channel = channel_handle->jchannel; - } json service = handle->jservice; json device = handle->jdevice; std::string type = convert_type_to_string(handle->service_type); + if (type.empty()) + return CONV_ERROR_INVALID_PARAMETER; description.set(NULL, CONV_JSON_SERVICE, service); description.set(NULL, CONV_JSON_CHANNEL, channel); @@ -374,16 +380,20 @@ EXTAPI int conv_service_publish(conv_service_h handle, conv_channel_h channel_ha json description; json channel; + json payload; - if ( channel_handle != NULL ) - { + if (channel_handle != NULL) channel = channel_handle->jchannel; - } - json payload = payload_handle->jpayload; + if (payload_handle != NULL) + payload = payload_handle->jpayload; + json service = handle->jservice; json device = handle->jdevice; + std::string type = convert_type_to_string(handle->service_type); + if (type.empty()) + return CONV_ERROR_INVALID_PARAMETER; description.set(NULL, CONV_JSON_SERVICE, service); description.set(NULL, CONV_JSON_CHANNEL, channel); @@ -431,6 +441,8 @@ static int conv_service_unset_connected_cb(conv_service_h handle) json service = handle->jservice; std::string service_type = convert_type_to_string(handle->service_type); + if (service_type.empty()) + return CONV_ERROR_INVALID_PARAMETER; description.set(NULL, CONV_JSON_SERVICE, service); description.set(NULL, CONV_JSON_TYPE, service_type); @@ -478,13 +490,14 @@ EXTAPI int conv_service_connect(conv_service_h handle, conv_service_connected_cb json device = handle->jdevice; std::string service_type = convert_type_to_string(handle->service_type); + if (service_type.empty()) + return CONV_ERROR_INVALID_PARAMETER; description.set(NULL, CONV_JSON_SERVICE, service); description.set(NULL, CONV_JSON_TYPE, service_type); description.set(NULL, CONV_JSON_DEVICE, device); description.set(NULL, CONV_JSON_IS_LOCAL, handle->is_local); - //temp conv_service_set_connected_cb(handle, description, callback, user_data); int err = conv::dbus_client::request(REQ_WRITE, &req_id, CONV_SUBJECT_CONNECTION_START, description.str().c_str(), NULL, NULL); @@ -498,7 +511,6 @@ EXTAPI int conv_service_disconnect(conv_service_h handle) IF_FAIL_RETURN_TAG(conv::util::is_feature_supported(), CONV_ERROR_NOT_SUPPORTED, _E, "Not supported"); ASSERT_NOT_NULL(handle); - //temp conv_service_unset_connected_cb(handle); int req_id; @@ -507,6 +519,8 @@ EXTAPI int conv_service_disconnect(conv_service_h handle) json service = handle->jservice; json device = handle->jdevice; std::string service_type = convert_type_to_string(handle->service_type); + if (service_type.empty()) + return CONV_ERROR_INVALID_PARAMETER; description.set(NULL, CONV_JSON_SERVICE, service); description.set(NULL, CONV_JSON_TYPE, service_type); @@ -538,7 +552,8 @@ EXTAPI int conv_service_get_property_string(conv_service_h handle, const char* k std::string strval; bool ret = handle->jservice.get(NULL, key, &strval); // path is NULL.. - if (ret == false || strval.empty()) return CONV_ERROR_NO_DATA; + if (ret == false || strval.empty()) + return CONV_ERROR_NO_DATA; *value = strdup(strval.c_str()); return CONV_ERROR_NONE; @@ -549,6 +564,10 @@ EXTAPI int conv_service_set_type(conv_service_h handle, conv_service_e value) IF_FAIL_RETURN_TAG(conv::util::is_feature_supported(), CONV_ERROR_NOT_SUPPORTED, _E, "Not supported"); ASSERT_NOT_NULL(handle); + std::string value_str = convert_type_to_string(value); + if (value_str.empty()) + return CONV_ERROR_INVALID_PARAMETER; + handle->service_type = value; return CONV_ERROR_NONE; @@ -586,8 +605,7 @@ conv_service_e convert_string_to_type(std::string type_name) { conv_service_e service_type = CONV_SERVICE_NONE; - if ( !type_name.compare(CONV_SERVICE_TYPE_SMARTVIEW_APP_COMMUNICATION)) - { + if ( !type_name.compare(CONV_SERVICE_TYPE_SMARTVIEW_APP_COMMUNICATION)) { service_type = CONV_SERVICE_APP_TO_APP_COMMUNICATION; } else if ( !type_name.compare(CONV_SERVICE_TYPE_REMOTE_APP_CONTROL)) { service_type = CONV_SERVICE_REMOTE_APP_CONTROL; diff --git a/lib/dbus_client.cpp b/lib/dbus_client.cpp old mode 100644 new mode 100755 index 0e77a7b..b741c1e --- a/lib/dbus_client.cpp +++ b/lib/dbus_client.cpp @@ -60,7 +60,6 @@ static void handle_response(const gchar *sender, GVariant *param, GDBusMethodInv g_variant_get(param, "(i&si&s)", &req_id, &subject, &error, &data); _D("[Response] ReqId: %d, Subject: %s, Error: %d", req_id, subject, error); - // conv::response_handler::deliver(subject, req_id, error, data); response_cb_map_t::iterator it = response_cb_map->find(subject); IF_FAIL_VOID_TAG(it!= response_cb_map->end(), _E, "Unknown subject'%s'", subject); it->second(subject, req_id, error, data); @@ -88,16 +87,12 @@ static void handle_method_call(GDBusConnection *conn, const gchar *sender, bool conv::dbus_client::init() { -// static GMutex connection_mutex; -// conv::scope_mutex sm(&connection_mutex); - _D("dbus_client init with dbus_connection %d response_cb_map:%x", dbus_connection, response_cb_map); if (dbus_connection) { return true; } - //response_cb_map = g_new0 (response_cb_map_t,1); - response_cb_map = new (std::nothrow) response_cb_map_t; + response_cb_map = new(std::nothrow) response_cb_map_t; _D("dbus_client init with response_cb_map:%x", response_cb_map); GError *gerr = NULL; diff --git a/lib/include/d2d_conv_internal.h b/lib/include/d2d_conv_internal.h new file mode 100755 index 0000000..af36059 --- /dev/null +++ b/lib/include/d2d_conv_internal.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __TIZEN_D2D_CONV_INTERNAL_H__ +#define __TIZEN_D2D_CONV_INTERNAL_H__ + +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** + * @file d2d_conv_internal.h + */ + +/** + * @brief Exports json string from channel. + * @since_tizen 3.0 + * @remarks The @a value must be released using free(). + * + * @param[in] handle The channel handle + * @param[out] value The result value + * + * @return 0 on success, otherwise a negative error value + * @retval #CONV_ERROR_NONE Successful + * @retval #CONV_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #CONV_ERROR_NOT_SUPPORTED Not supported + * @retval #CONV_ERROR_NO_DATA No Data + * @retval #CONV_ERROR_OUT_OF_MEMORY Out of memory + * + */ +int conv_channel_internal_export_to_string(conv_channel_h handle, char** value); + +/** + * @brief Exports json string from payload. + * @since_tizen 3.0 + * @remarks The @a value must be released using free(). + * + * @param[in] handle The payload handle + * @param[out] value The result value + * + * @return 0 on success, otherwise a negative error value + * @retval #CONV_ERROR_NONE Successful + * @retval #CONV_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #CONV_ERROR_NOT_SUPPORTED Not supported + * @retval #CONV_ERROR_NO_DATA No Data + * @retval #CONV_ERROR_OUT_OF_MEMORY Out of memory + * + */ +int conv_payload_internal_export_to_string(conv_payload_h handle, char** value); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +/** +* @} +*/ + +#endif /* __TIZEN_D2D_CONV_INTERNAL_H__ */ diff --git a/packaging/d2d-conv-manager.service b/packaging/d2d-conv-manager.service old mode 100644 new mode 100755 index b842679..fc364c8 --- a/packaging/d2d-conv-manager.service +++ b/packaging/d2d-conv-manager.service @@ -1,7 +1,5 @@ [Unit] Description=D2D Convergence Manager Daemon -#Wants=net-config.service,bt-service -#After=net-config.service,bt-service [Service] Type=dbus diff --git a/packaging/d2d-conv-manager.spec b/packaging/d2d-conv-manager.spec old mode 100644 new mode 100755 index b3065df..c7b7a6e --- a/packaging/d2d-conv-manager.spec +++ b/packaging/d2d-conv-manager.spec @@ -122,7 +122,6 @@ rm -f %{_unitdir_user}/default.target.wants/%{name}.service %config %{_sysconfdir}/dbus-1/session.d/d2d-conv-manager.conf %{_unitdir_user}/%{name}.service %{_bindir}/%{name}d -%{_datadir}/dbus-1/services/org.tizen.d2dconv.service %{_bindir}/%{name}-test %{_datadir}/license/%{name} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt old mode 100644 new mode 100755 index 572e9c1..d8d0b49 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,5 @@ LINK_DIRECTORIES(${CMAKE_BINARY_DIR}) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../lib/include) -INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../lib/disc_mgr/include) SET(TEST "d2d-conv-manager-test") diff --git a/test/test.c b/test/test.c old mode 100644 new mode 100755 index c4b31b4..8c49058 --- a/test/test.c +++ b/test/test.c @@ -124,8 +124,6 @@ void service_remote_app_control_foreach_cb(conv_service_h handle, void* user_dat } printf("next\n"); - - } } @@ -188,10 +186,10 @@ void service_app_communication_foreach_cb(conv_service_h handle, void* user_data printf("conv_service_stop called..\n"); conv_service_stop(handle, channel_handle, NULL); - /* +/* printf("conv_service_get called..\n"); conv_service_get(handle, channel_handle, NULL, NULL); - */ +*/ printf("conv_payload_destroy called..\n"); conv_payload_destroy(payload_handle); @@ -358,7 +356,6 @@ void simple_test_conv_app_communication_start_stop_discovery() conv_discovery_start(handle, 10, device_conv_app_communication_discovery_cb, NULL); } - // Simple Test.. Start/Stop Discovery void simple_test_conv_start_stop_discovery() { @@ -369,8 +366,7 @@ void simple_test_conv_start_stop_discovery() /* printf("set discovery cb..\n"); - //conv_set_discovery_cb (handle, simple_test_conv_discovery_cb, NULL); - conv_set_discovery_cb (handle, device_conv_discovery_cb, NULL); + conv_set_discovery_cb(handle, device_conv_discovery_cb, NULL); */ printf("conv_discovery_start called..\n"); @@ -443,7 +439,6 @@ int main(int argc, char** argv) loop = g_main_loop_new(NULL, FALSE); if (argc > 1) { - printf("1"); int type = atoi(argv[1]); struct arg_data* args = g_slice_alloc(sizeof(*args)); args->type = type; -- 2.7.4