From 0781db95d7113446d7a92d65d4a1c7f77b91e8c4 Mon Sep 17 00:00:00 2001 From: Pawel Andruszkiewicz Date: Tue, 23 Feb 2016 13:58:58 +0100 Subject: [PATCH] [iotcon] Implementation updated to latest changes in native API. Change-Id: Idd808a8f1f6e748f5980744aa23fb6eceea21a71 Signed-off-by: Pawel Andruszkiewicz --- src/iotcon/iotcon_api.js | 13 +-- src/iotcon/iotcon_instance.cc | 8 +- src/iotcon/iotcon_server_manager.cc | 11 ++- src/iotcon/iotcon_utils.cc | 152 +++++++++++++++++++----------------- src/iotcon/iotcon_utils.h | 10 +-- 5 files changed, 102 insertions(+), 92 deletions(-) diff --git a/src/iotcon/iotcon_api.js b/src/iotcon/iotcon_api.js index 450b6d5..29e6482 100644 --- a/src/iotcon/iotcon_api.js +++ b/src/iotcon/iotcon_api.js @@ -155,13 +155,6 @@ var ConnectivityType = { ALL: 'ALL' }; -var ResourceInterface = { - DEFAULT: 'DEFAULT', - LINK: 'LINK', - BATCH: 'BATCH', - GROUP: 'GROUP' -}; - var ObservePolicy = { IGNORE_OUT_OF_ORDER: 'IGNORE_OUT_OF_ORDER', ACCEPT_OUT_OF_ORDER: 'ACCEPT_OUT_OF_ORDER' @@ -365,8 +358,7 @@ Resource.prototype.addResourceTypes = function() { Resource.prototype.addResourceInterface = function() { var args = validator.validateMethod(arguments, [{ name: 'iface', - type: types.ENUM, - values: T.getValues(ResourceInterface) + type: types.STRING }]); var callArgs = {}; @@ -501,8 +493,7 @@ function Response(request) { Response.prototype.send = function() { var args = validator.validateMethod(arguments, [{ name: 'iface', - type: types.ENUM, - values: T.getValues(ResourceInterface) + type: types.STRING }]); var callArgs = {}; diff --git a/src/iotcon/iotcon_instance.cc b/src/iotcon/iotcon_instance.cc index 6b8276c..3344a02 100644 --- a/src/iotcon/iotcon_instance.cc +++ b/src/iotcon/iotcon_instance.cc @@ -327,7 +327,7 @@ common::TizenResult IotconInstance::ResourceAddResourceInterface(const picojson: LogAndReturnTizenError(result, ("GetResourceById() failed")); } - result = IotconUtils::ConvertIotconError(iotcon_resource_bind_interface(resource->handle, IotconUtils::ToInterface(IotconUtils::GetArg(args, kInterface).get()))); + result = IotconUtils::ConvertIotconError(iotcon_resource_bind_interface(resource->handle, IotconUtils::GetArg(args, kInterface).get().c_str())); if (!result) { LogAndReturnTizenError(result, ("iotcon_resource_bind_interface() failed")); } @@ -485,8 +485,10 @@ common::TizenResult IotconInstance::ResponseSend(const picojson::object& args) { }; result = IotconUtils::ConvertIotconError( - iotcon_response_set_representation(response.get(), IotconUtils::ToInterface( - IotconUtils::GetArg(args, kInterface).get()), representation)); + iotcon_response_set_representation( + response.get(), + IotconUtils::GetArg(args, kInterface).get().c_str(), + representation)); if (!result) { LogAndReturnTizenError(result, ("iotcon_response_set_representation() failed")); } diff --git a/src/iotcon/iotcon_server_manager.cc b/src/iotcon/iotcon_server_manager.cc index 079e783..a873cb4 100644 --- a/src/iotcon/iotcon_server_manager.cc +++ b/src/iotcon/iotcon_server_manager.cc @@ -17,6 +17,7 @@ #include "iotcon/iotcon_server_manager.h" #include "common/logger.h" +#include "common/scope_exit.h" #include "iotcon/iotcon_instance.h" @@ -49,7 +50,7 @@ TizenResult IotconServerManager::RestoreHandles() { ResourceInfoPtr resource = it.second; char* uri_path = nullptr; iotcon_resource_types_h res_types = nullptr; - int ifaces = 0; + iotcon_resource_interfaces_h ifaces = 0; int properties = 0; auto res = IotconUtils::ExtractFromResource(resource, &uri_path, @@ -176,17 +177,23 @@ TizenResult IotconServerManager::CreateResource(const std::string& uri_path, ScopeLogger(); int ret; - int interfaces = IOTCON_INTERFACE_NONE; + iotcon_resource_interfaces_h interfaces = nullptr; auto res = IotconUtils::ArrayToInterfaces(interfaces_array, &interfaces); if (!res) { return res; } + SCOPE_EXIT { + iotcon_resource_interfaces_destroy(interfaces); + }; iotcon_resource_types_h resource_types = nullptr; res = IotconUtils::ArrayToTypes(types_array, &resource_types); if (!res) { return res; } + SCOPE_EXIT { + iotcon_resource_types_destroy(resource_types); + }; // Create resource ret = iotcon_resource_create(uri_path.c_str(), diff --git a/src/iotcon/iotcon_utils.cc b/src/iotcon/iotcon_utils.cc index d44e8a1..6647814 100644 --- a/src/iotcon/iotcon_utils.cc +++ b/src/iotcon/iotcon_utils.cc @@ -53,15 +53,6 @@ namespace { X(IOTCON_OBSERVE_DEREGISTER, "DEREGISTER") \ XD(IOTCON_OBSERVE_NO_TYPE, "unknown") -#define IOTCON_INTERFACE_E \ - X(IOTCON_INTERFACE_NONE, "NONE") \ - X(IOTCON_INTERFACE_DEFAULT, "DEFAULT") \ - X(IOTCON_INTERFACE_LINK, "LINK") \ - X(IOTCON_INTERFACE_BATCH, "BATCH") \ - X(IOTCON_INTERFACE_GROUP, "GROUP") \ - X(IOTCON_INTERFACE_READONLY, "READONLY") \ - XD(IOTCON_INTERFACE_NONE, "unknown") - #define IOTCON_QOS_E \ X(IOTCON_QOS_LOW, "LOW") \ X(IOTCON_QOS_HIGH, "HIGH") \ @@ -201,43 +192,52 @@ void IotconUtils::PropertiesToJson(int properties, picojson::object* res) { res->insert(std::make_pair(kIsExplicitDiscoverable, picojson::value(value))); } -TizenResult IotconUtils::ArrayToInterfaces(const picojson::array& interfaces, int* res) { +TizenResult IotconUtils::ArrayToInterfaces(const picojson::array& i, iotcon_resource_interfaces_h* out) { ScopeLogger(); - int result_value = IOTCON_INTERFACE_NONE; + iotcon_resource_interfaces_h interfaces = nullptr; + + auto result = ConvertIotconError(iotcon_resource_interfaces_create(&interfaces)); + if (!result) { + LogAndReturnTizenError(result, ("iotcon_resource_interfaces_create() failed")); + } + + std::unique_ptr::type, void(*)(iotcon_resource_interfaces_h)> ptr{interfaces, &iotcon_resource_interfaces_destroy}; - for (auto iter = interfaces.begin(); iter != interfaces.end(); ++iter) { - if (!iter->is()) { - return LogAndCreateTizenError(InvalidValuesError, "Array holds incorrect interface names"); + for (const auto& iter : i) { + if (!iter.is()) { + return LogAndCreateTizenError(InvalidValuesError, "Interface name should be a string"); } else { - iotcon_interface_e interface = ToInterface(iter->get()); - result_value |= interface; + result = ConvertIotconError(iotcon_resource_interfaces_add(interfaces, iter.get().c_str())); + if (!result) { + LogAndReturnTizenError(result, ("iotcon_resource_interfaces_add() failed")); + } } } - *res = result_value; + + *out = ptr.release(); return TizenSuccess(); } -picojson::array IotconUtils::InterfacesToArray(int interfaces) { +TizenResult IotconUtils::InterfacesToArray(iotcon_resource_interfaces_h interfaces, picojson::array* arr) { ScopeLogger(); - picojson::array res; - if (interfaces & IOTCON_INTERFACE_DEFAULT) { - res.push_back(picojson::value(FromInterface(IOTCON_INTERFACE_DEFAULT))); - } - if (interfaces & IOTCON_INTERFACE_LINK) { - res.push_back(picojson::value(FromInterface(IOTCON_INTERFACE_LINK))); - } - if (interfaces & IOTCON_INTERFACE_BATCH) { - res.push_back(picojson::value(FromInterface(IOTCON_INTERFACE_BATCH))); - } - if (interfaces & IOTCON_INTERFACE_GROUP) { - res.push_back(picojson::value(FromInterface(IOTCON_INTERFACE_GROUP))); - } - if (interfaces & IOTCON_INTERFACE_READONLY) { - res.push_back(picojson::value(FromInterface(IOTCON_INTERFACE_READONLY))); + auto result = ConvertIotconError(iotcon_resource_interfaces_foreach(interfaces, [](const char* iface, void* user_data) -> bool { + ScopeLogger("iotcon_resource_interfaces_foreach"); + + if (iface) { + auto arr = static_cast(user_data); + arr->push_back(picojson::value(iface)); + } + + // always continue with iteration + return true; + }, arr)); + if (!result) { + LogAndReturnTizenError(result, ("iotcon_resource_interfaces_foreach() failed")); } - return res; + + return TizenSuccess(); } TizenResult IotconUtils::ArrayToTypes(const picojson::array& types, iotcon_resource_types_h* res) { @@ -249,18 +249,20 @@ TizenResult IotconUtils::ArrayToTypes(const picojson::array& types, iotcon_resou LogAndReturnTizenError(result, ("iotcon_resource_types_create() failed")); } - for (auto iter = types.begin(); iter != types.end(); ++iter) { - if (!iter->is()) { - return LogAndCreateTizenError(InvalidValuesError, "Array holds incorrect types"); + std::unique_ptr::type, void(*)(iotcon_resource_types_h)> ptr{resource_types, &iotcon_resource_types_destroy}; + + for (const auto& iter : types) { + if (!iter.is()) { + return LogAndCreateTizenError(InvalidValuesError, "Resource type should be a string"); } else { - result = ConvertIotconError(iotcon_resource_types_add(resource_types, iter->get().c_str())); + result = ConvertIotconError(iotcon_resource_types_add(resource_types, iter.get().c_str())); if (!result) { - iotcon_resource_types_destroy(resource_types); LogAndReturnTizenError(result, ("iotcon_resource_types_add() failed")); } } } - *res = resource_types; + + *res = ptr.release(); return TizenSuccess(); } @@ -280,7 +282,7 @@ static bool ResourceTypeIterator(const char *type, void *user_data) { TizenResult IotconUtils::ExtractFromResource(const ResourceInfoPtr& pointer, char** uri_path, iotcon_resource_types_h* res_types, - int* ifaces, + iotcon_resource_interfaces_h* ifaces, int* properties) { ScopeLogger(); @@ -312,7 +314,7 @@ TizenResult IotconUtils::ResourceToJson(ResourceInfoPtr pointer, char* uri_path = nullptr; iotcon_resource_types_h res_types = nullptr; - int ifaces = 0; + iotcon_resource_interfaces_h ifaces = nullptr; int properties = 0; auto ret = ExtractFromResource(pointer, &uri_path, &res_types, &ifaces, &properties); if (!ret){ @@ -325,8 +327,12 @@ TizenResult IotconUtils::ResourceToJson(ResourceInfoPtr pointer, iotcon_resource_types_foreach(res_types, ResourceTypeIterator, &types); res->insert(std::make_pair(kResourceTypes, picojson::value(types))); - res->insert(std::make_pair(kResourceInterfaces, - picojson::value(InterfacesToArray(ifaces)))); + picojson::array interfaces; + ret = InterfacesToArray(ifaces, &interfaces); + if (!ret) { + LogAndReturnTizenError(ret, ("InterfacesToArray() failed")); + } + res->insert(std::make_pair(kResourceInterfaces, picojson::value(interfaces))); IotconUtils::PropertiesToJson(properties, res); picojson::array children; @@ -428,8 +434,14 @@ TizenResult IotconUtils::RemoteResourceToJson(iotcon_remote_resource_h handle, res->insert(std::make_pair(kResourceTypes, picojson::value(types))); } - res->insert(std::make_pair(kResourceInterfaces, - picojson::value(InterfacesToArray(remote_res.ifaces)))); + if (remote_res.ifaces) { + picojson::array interfaces; + result = InterfacesToArray(remote_res.ifaces, &interfaces); + if (!result) { + LogAndReturnTizenError(result, ("InterfacesToArray() failed")); + } + res->insert(std::make_pair(kResourceInterfaces, picojson::value(interfaces))); + } IotconUtils::PropertiesToJson(remote_res.properties, res); @@ -495,14 +507,20 @@ common::TizenResult IotconUtils::RemoteResourceFromJson(const picojson::object& if (!res) { return res; } + SCOPE_EXIT { + iotcon_resource_types_destroy(resource_types); + }; CHECK_EXIST(source, kResourceInterfaces); const auto& interfaces_array = source.find(kResourceInterfaces)->second.get(); - int interfaces = IOTCON_INTERFACE_NONE; + iotcon_resource_interfaces_h interfaces = nullptr; res = IotconUtils::ArrayToInterfaces(interfaces_array, &interfaces); if (!res) { return res; } + SCOPE_EXIT { + iotcon_resource_interfaces_destroy(interfaces); + }; res = IotconUtils::ConvertIotconError( iotcon_remote_resource_create(host_address, connectivity_type, uri_path, @@ -648,12 +666,17 @@ common::TizenResult IotconUtils::RepresentationToJson(iotcon_representation_h re { // resourceInterfaces - int interfaces = 0; + iotcon_resource_interfaces_h interfaces = nullptr; auto result = ConvertIotconError(iotcon_representation_get_resource_interfaces(representation, &interfaces)); if (!result) { LogAndReturnTizenError(result, ("iotcon_representation_get_resource_interfaces() failed")); } - out->insert(std::make_pair(kResourceInterfaces, picojson::value{InterfacesToArray(interfaces)})); + picojson::array js_interfaces; + result = InterfacesToArray(interfaces, &js_interfaces); + if (!result) { + LogAndReturnTizenError(result, ("InterfacesToArray() failed")); + } + out->insert(std::make_pair(kResourceInterfaces, picojson::value{js_interfaces})); } { @@ -999,12 +1022,12 @@ common::TizenResult IotconUtils::QueryToJson(iotcon_query_h query, { // resourceInterface - iotcon_interface_e interface = IOTCON_INTERFACE_NONE; + char* interface = nullptr; auto result = ConvertIotconError(iotcon_query_get_interface(query, &interface)); - if (!result) { + if (!result || !interface) { LogAndReturnTizenError(result, ("iotcon_query_get_interface() failed")); } - out->insert(std::make_pair(kResourceInterface, picojson::value{FromInterface(interface)})); + out->insert(std::make_pair(kResourceInterface, picojson::value{interface})); } { @@ -1052,8 +1075,8 @@ common::TizenResult IotconUtils::QueryFromJson(const picojson::object& source, i // resourceInterface auto it = source.find(kResourceInterface); if (source.end() != it && it->second.is()) { - iotcon_interface_e interface = ToInterface(it->second.get()); - auto result = ConvertIotconError(iotcon_query_set_interface(query, interface)); + auto& interface = it->second.get(); + auto result = ConvertIotconError(iotcon_query_set_interface(query, interface.c_str())); if (!result) { LogAndReturnTizenError(result, ("iotcon_query_set_interface() failed")); } @@ -1178,7 +1201,7 @@ common::TizenResult IotconUtils::RepresentationFromResource(const ResourceInfoPt } { - int intrefaces = IOTCON_INTERFACE_NONE; + iotcon_resource_interfaces_h intrefaces = nullptr; result = IotconUtils::ConvertIotconError(iotcon_resource_get_interfaces(resource->handle, &intrefaces)); if (!result) { LogAndReturnTizenError(result, ("iotcon_resource_get_interfaces() failed")); @@ -1648,12 +1671,15 @@ common::TizenResult IotconUtils::RepresentationFromJson(const picojson::object& { const auto& resource_interfaces = r.find(kResourceInterfaces); if (r.end() != resource_interfaces && resource_interfaces->second.is()) { - int interfaces = IOTCON_INTERFACE_NONE; + iotcon_resource_interfaces_h interfaces = nullptr; result = ArrayToInterfaces(resource_interfaces->second.get(), &interfaces); if (!result) { LogAndReturnTizenError(result, ("ArrayToInterfaces() failed")); } + SCOPE_EXIT { + iotcon_resource_interfaces_destroy(interfaces); + }; result = ConvertIotconError(iotcon_representation_set_resource_interfaces(representation, interfaces)); if (!result) { @@ -1967,14 +1993,6 @@ std::string IotconUtils::FromObserveType(iotcon_observe_type_e e) { } } -std::string IotconUtils::FromInterface(iotcon_interface_e e) { - ScopeLogger(); - - switch (e) { - IOTCON_INTERFACE_E - } -} - std::string IotconUtils::FromPresenceResponseResultType(iotcon_presence_result_e e) { ScopeLogger(); @@ -2007,12 +2025,6 @@ std::string IotconUtils::FromResponseResultType(iotcon_response_result_e e) { LoggerE("Unknown value: %s, returning default: %d", e.c_str(), v); \ return v; -iotcon_interface_e IotconUtils::ToInterface(const std::string& e) { - ScopeLogger(); - - IOTCON_INTERFACE_E -} - iotcon_qos_e IotconUtils::ToQos(const std::string& e) { ScopeLogger(); diff --git a/src/iotcon/iotcon_utils.h b/src/iotcon/iotcon_utils.h index 21dbc5b..1a783a0 100644 --- a/src/iotcon/iotcon_utils.h +++ b/src/iotcon/iotcon_utils.h @@ -97,7 +97,7 @@ struct RemoteResourceInfo { char* host_address; char* device_id; iotcon_resource_types_h types; - int ifaces; + iotcon_resource_interfaces_h ifaces; int properties; // to check if observable iotcon_options_h options; iotcon_representation_h representation; @@ -137,13 +137,13 @@ class IotconUtils { static const picojson::value& GetArg(const picojson::object& args, const std::string& name); static int GetProperties(const picojson::object& args); static void PropertiesToJson(int properties, picojson::object* res); - static common::TizenResult ArrayToInterfaces(const picojson::array& interfaces, int* res); - static picojson::array InterfacesToArray(int interfaces); + static common::TizenResult ArrayToInterfaces(const picojson::array& interfaces, iotcon_resource_interfaces_h* res); + static common::TizenResult InterfacesToArray(iotcon_resource_interfaces_h interfaces, picojson::array* arr); static common::TizenResult ArrayToTypes(const picojson::array& types, iotcon_resource_types_h* res); static common::TizenResult ExtractFromResource(const ResourceInfoPtr& pointer, char** uri_path, iotcon_resource_types_h* res_types, - int* ifaces, + iotcon_resource_interfaces_h* ifaces, int* properties); static common::TizenResult ResourceToJson(ResourceInfoPtr pointer, picojson::object* res); @@ -204,12 +204,10 @@ class IotconUtils { static std::string FromConnectivityType(iotcon_connectivity_type_e e); static std::string FromRequestType(iotcon_request_type_e e); static std::string FromObserveType(iotcon_observe_type_e e); - static std::string FromInterface(iotcon_interface_e e); static std::string FromPresenceResponseResultType(iotcon_presence_result_e e); static std::string FromPresenceTriggerType(iotcon_presence_trigger_e e); static std::string FromResponseResultType(iotcon_response_result_e e); - static iotcon_interface_e ToInterface(const std::string& e); static iotcon_connectivity_type_e ToConnectivityType(const std::string& e); static iotcon_observe_policy_e ToObservePolicy(const std::string& e); static iotcon_qos_e ToQos(const std::string& e); -- 2.7.4