[Iotcon] Added mechanism for storing handles
authorPiotr Kosko <p.kosko@samsung.com>
Fri, 12 Feb 2016 10:40:14 +0000 (11:40 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Thu, 18 Feb 2016 07:01:48 +0000 (16:01 +0900)
[Verification] Code compiles.

Change-Id: I41ddc1d2d81e546f65cd8c05d7692b74b19e94d3
Signed-off-by: Piotr Kosko <p.kosko@samsung.com>
src/iotcon/iotcon_api.js
src/iotcon/iotcon_client_manager.cc
src/iotcon/iotcon_client_manager.h
src/iotcon/iotcon_instance.cc
src/iotcon/iotcon_utils.cc
src/iotcon/iotcon_utils.h

index 2e20b6c8eef44b3d8341172f92ba88e9f216c577..a97da630178bc3b48f04a257cda78957054e53e7 100644 (file)
@@ -557,6 +557,39 @@ function State(key, state) {
   });
 }
 
+function prepareResourceInfo(that){
+  var callArgs = {};
+  callArgs.id = that[kIdKey];
+  if (!callArgs.id) {
+    console.log("RemoteResource is not already stored in C++ layer, adding all members");
+    callArgs.hostAddress = that.hostAddress;
+    callArgs.connectivityType = that.connectivityType;
+    callArgs.uriPath = that.uriPath;
+    //properties flags
+    callArgs.isObservable = that.isObservable;
+    callArgs.isDiscoverable = that.isDiscoverable;
+    callArgs.isActive = that.isActive;
+    callArgs.isSlow = that.isSlow;
+    callArgs.isSecure = that.isSecure;
+    callArgs.isExplicitDiscoverable = that.isExplicitDiscoverable;
+    callArgs.resourceTypes = that.resourceTypes;
+    callArgs.resourceInterfaces = that.resourceInterfaces;
+  } else {
+    console.log("Already stored in C++, all needed info is id");
+  }
+  return callArgs;
+}
+
+function manageId(that, result) {
+  if (result.keepId) {
+    that[kIdKey] = result.id;
+    console.log("Keep id of resource: " + that[kIdKey]);
+  } else {
+    console.log("Clear id of resource");
+    delete that[kIdKey];
+  }
+}
+
 function RemoteResource(data) {
   Object.defineProperties(this, {
     cachedRepresentation: {
@@ -598,8 +631,7 @@ RemoteResource.prototype.methodGet = function() {
     nullable: true
   }]);
 
-  var callArgs = {};
-  callArgs.id = this[kIdKey];
+  var callArgs = prepareResourceInfo(this);
   callArgs.query = args.query;
 
   var callback = function(result) {
@@ -636,8 +668,7 @@ RemoteResource.prototype.methodPut = function() {
     nullable: true
   }]);
 
-  var callArgs = {};
-  callArgs.id = this[kIdKey];
+  var callArgs = prepareResourceInfo(this);
   callArgs.representation = args.representation;
   callArgs.query = args.query;
 
@@ -675,8 +706,7 @@ RemoteResource.prototype.methodPost = function() {
     nullable: true
   }]);
 
-  var callArgs = {};
-  callArgs.id = this[kIdKey];
+  var callArgs = prepareResourceInfo(this);
   callArgs.representation = args.representation;
   callArgs.query = args.query;
 
@@ -707,8 +737,7 @@ RemoteResource.prototype.methodDelete = function() {
     nullable: true
   }]);
 
-  var callArgs = {};
-  callArgs.id = this[kIdKey];
+  var callArgs = prepareResourceInfo(this);
 
   var callback = function(result) {
     if (native.isFailure(result)) {
@@ -746,8 +775,7 @@ RemoteResource.prototype.setStateChangeListener = function() {
     nullable: true
   }]);
 
-  var callArgs = {};
-  callArgs.id = this[kIdKey];
+  var callArgs = prepareResourceInfo(this);
   callArgs.query = args.query;
   callArgs.observePolicy = args.observePolicy;
   var that = this;
@@ -771,8 +799,7 @@ RemoteResource.prototype.setStateChangeListener = function() {
 };
 
 RemoteResource.prototype.unsetStateChangeListener = function() {
-  var callArgs = {};
-  callArgs.id = this[kIdKey];
+  var callArgs = prepareResourceInfo(this);
 
   var result = native.callSync('IotconRemoteResource_unsetStateChangeListener', callArgs);
 
@@ -784,8 +811,7 @@ RemoteResource.prototype.unsetStateChangeListener = function() {
 };
 
 RemoteResource.prototype.startCaching = function() {
-  var callArgs = {};
-  callArgs.id = this[kIdKey];
+  var callArgs = prepareResourceInfo(this);
 
   var result = native.callSync('IotconRemoteResource_startCaching', callArgs);
 
@@ -795,8 +821,7 @@ RemoteResource.prototype.startCaching = function() {
 };
 
 RemoteResource.prototype.stopCaching = function() {
-  var callArgs = {};
-  callArgs.id = this[kIdKey];
+  var callArgs = prepareResourceInfo(this);
 
   var result = native.callSync('IotconRemoteResource_stopCaching', callArgs);
 
@@ -818,8 +843,7 @@ RemoteResource.prototype.setConnectionChangeListener = function() {
     nullable: true
   }]);
 
-  var callArgs = {};
-  callArgs.id = this[kIdKey];
+  var callArgs = prepareResourceInfo(this);
 
   var listener = function(result) {
     if (native.isFailure(result)) {
@@ -839,8 +863,7 @@ RemoteResource.prototype.setConnectionChangeListener = function() {
 };
 
 RemoteResource.prototype.unsetConnectionChangeListener = function() {
-  var callArgs = {};
-  callArgs.id = this[kIdKey];
+  var callArgs = prepareResourceInfo(this);
 
   var result = native.callSync('IotconRemoteResource_unsetConnectionChangeListener', callArgs);
 
index fb840c3d31ba1ced7bcdcb12e74030024111207e..879ce19d4f589790e02c687be6bd2546d309f781 100644 (file)
@@ -31,6 +31,11 @@ long long GetPresenceNextId() {
   return ++id;
 }
 
+long long GetRemoteNextId() {
+  static long long id = 0;
+  return ++id;
+}
+
 }  // namespace
 
 IotconClientManager& IotconClientManager::GetInstance() {
@@ -134,5 +139,53 @@ common::TizenResult IotconClientManager::RemovePresenceEventListener(long long i
   return TizenSuccess();
 }
 
+picojson::value IotconClientManager::StoreRemoteResource(FoundRemoteInfoPtr ptr) {
+  ScopeLogger();
+  if (0 == ptr->id) {
+    LoggerD("New remote, needed to be added to map");
+    ptr->id = GetRemoteNextId();
+    remotes_map_.insert(std::make_pair(ptr->id, ptr));
+  } else {
+    LoggerD("Remote is already stored, just increase ref_count");
+    ptr->ref_count++;
+  }
+  return PrepareManageIdAnswer(true, ptr->id);
+}
+
+picojson::value IotconClientManager::RemoveRemoteResource(FoundRemoteInfoPtr ptr) {
+  ScopeLogger();
+  ptr->ref_count--;
+  if (ptr->ref_count <= 0) {
+    LoggerD("Handle not needed anymore, removing from map");
+    remotes_map_.erase(ptr->id);
+    return PrepareManageIdAnswer(false);
+  }
+  return PrepareManageIdAnswer(true, ptr->id);
+}
+
+picojson::value IotconClientManager::PrepareManageIdAnswer(bool keep_id, long long id) {
+  picojson::value answer{picojson::object{}};
+  auto& obj = answer.get<picojson::object>();
+  obj.insert(std::make_pair(kKeepId, picojson::value{keep_id}));
+  if (keep_id) {
+    obj.insert(std::make_pair(kId, picojson::value{static_cast<double>(id)}));
+  }
+  return answer;
+}
+
+TizenResult IotconClientManager::GetRemoteById(long long id,
+                                               FoundRemoteInfoPtr* res_pointer) const {
+  ScopeLogger();
+
+  auto it = remotes_map_.find(id);
+  if (it == remotes_map_.end()) {
+    return LogAndCreateTizenError(NotFoundError, "Resource with specified ID does not exist");
+  }
+  *res_pointer = it->second;
+
+  return TizenSuccess();
+}
+
+
 }  // namespace iotcon
 }  // namespace extension
index 7473a43fd6a2c9793fd449c6f9ae646fc4afeb67..e57308d76405a8218ee52f0045899d25d67cc343 100644 (file)
@@ -39,6 +39,9 @@ class IotconClientManager {
                                                const char* resource_type,
                                                PresenceEventPtr presence);
   common::TizenResult RemovePresenceEventListener(long long id);
+  picojson::value StoreRemoteResource(FoundRemoteInfoPtr ptr);
+  picojson::value RemoveRemoteResource(FoundRemoteInfoPtr ptr);
+  common::TizenResult GetRemoteById(long long id, FoundRemoteInfoPtr* res_pointer) const;
 
  private:
   IotconClientManager() = default;
@@ -47,12 +50,14 @@ class IotconClientManager {
   IotconClientManager& operator=(const IotconClientManager&) = delete;
   IotconClientManager& operator=(IotconClientManager&&) = delete;
 
+  picojson::value PrepareManageIdAnswer(bool keep_id, long long id = 0);
   static void PresenceHandler(iotcon_presence_h  resource,
                              iotcon_error_e err,
                              iotcon_presence_response_h response,
                              void *user_data);
 
   PresenceMap presence_map_;
+  FoundRemotesMap remotes_map_;
 };
 
 } // namespace iotcon
index 3f024f64128596a21ba9ec84a77f6463c636cbc7..5f18e9ba0603ceb2e02ae18fca934bf010583df2 100644 (file)
@@ -36,26 +36,10 @@ typedef struct {
   common::PostCallback fun;
 } CallbackData;
 
-#define CHECK_EXIST(args, name) \
-  if (args.end() == args.find(name)) { \
-    return common::TypeMismatchError(std::string(name) + " is required argument"); \
-  }
-
 long long GetId(const picojson::object& args) {
   return static_cast<long long>(args.find(kId)->second.get<double>());
 }
 
-const picojson::value& GetArg(const picojson::object& args, const std::string& name) {
-  static const picojson::value kNull;
-
-  auto it = args.find(name);
-  if (args.end() == it) {
-    return kNull;
-  } else {
-    return it->second;
-  }
-}
-
 const common::ListenerToken kResourceRequestListenerToken{"ResourceRequestListener"};
 const common::ListenerToken kFindResourceListenerToken{"FindResourceListener"};
 const common::ListenerToken kPresenceEventListenerToken{"PresenceEventListener"};
@@ -211,13 +195,13 @@ common::TizenResult IotconInstance::ResourceNotify(const picojson::object& args)
     LogAndReturnTizenError(result, ("GetResourceById() failed"));
   }
 
-  auto& qos = GetArg(args, kQos);
+  auto& qos = IotconUtils::GetArg(args, kQos);
   if (!qos.is<std::string>()) {
     return common::TypeMismatchError("QOS needs to be a string");
   }
 
   // create observers to notify
-  auto& observer_ids = GetArg(args, kObserverIds);
+  auto& observer_ids = IotconUtils::GetArg(args, kObserverIds);
 
   std::vector<int> observers;
 
@@ -257,7 +241,7 @@ common::TizenResult IotconInstance::ResourceNotify(const picojson::object& args)
   // create representation from resource and states
   iotcon_representation_h representation = nullptr;
 
-  result = IotconUtils::RepresentationFromResource(resource, GetArg(args, kStates), &representation);
+  result = IotconUtils::RepresentationFromResource(resource, IotconUtils::GetArg(args, kStates), &representation);
   if (!result) {
     LogAndReturnTizenError(result, ("RepresentationFromResource() failed"));
   }
@@ -286,7 +270,7 @@ common::TizenResult IotconInstance::ResourceAddResourceType(const picojson::obje
     LogAndReturnTizenError(result, ("GetResourceById() failed"));
   }
 
-  result = IotconUtils::ConvertIotconError(iotcon_resource_bind_type(resource->handle, GetArg(args, kType).get<std::string>().c_str()));
+  result = IotconUtils::ConvertIotconError(iotcon_resource_bind_type(resource->handle, IotconUtils::GetArg(args, kType).get<std::string>().c_str()));
   if (!result) {
     LogAndReturnTizenError(result, ("iotcon_resource_bind_type() failed"));
   }
@@ -306,7 +290,7 @@ common::TizenResult IotconInstance::ResourceAddResourceInterface(const picojson:
     LogAndReturnTizenError(result, ("GetResourceById() failed"));
   }
 
-  result = IotconUtils::ConvertIotconError(iotcon_resource_bind_interface(resource->handle, IotconUtils::ToInterface(GetArg(args, kInterface).get<std::string>())));
+  result = IotconUtils::ConvertIotconError(iotcon_resource_bind_interface(resource->handle, IotconUtils::ToInterface(IotconUtils::GetArg(args, kInterface).get<std::string>())));
   if (!result) {
     LogAndReturnTizenError(result, ("iotcon_resource_bind_interface() failed"));
   }
@@ -326,7 +310,7 @@ common::TizenResult IotconInstance::ResourceAddChildResource(const picojson::obj
     LogAndReturnTizenError(result, ("GetResourceById() parent failed"));
   }
 
-  long long child_id = static_cast<long long>(GetArg(args, kChildId).get<double>());
+  long long child_id = static_cast<long long>(IotconUtils::GetArg(args, kChildId).get<double>());
   ResourceInfoPtr child;
 
   result = IotconServerManager::GetInstance().GetResourceById(child_id, &child);
@@ -357,7 +341,7 @@ common::TizenResult IotconInstance::ResourceRemoveChildResource(const picojson::
     LogAndReturnTizenError(result, ("GetResourceById() parent failed"));
   }
 
-  long long child_id = static_cast<long long>(GetArg(args, kChildId).get<double>());
+  long long child_id = static_cast<long long>(IotconUtils::GetArg(args, kChildId).get<double>());
   ResourceInfoPtr child;
 
   result = IotconServerManager::GetInstance().GetResourceById(child_id, &child);
@@ -437,7 +421,7 @@ common::TizenResult IotconInstance::ResponseSend(const picojson::object& args) {
   }
 
   {
-    const auto& js_response_result = GetArg(args, kResult);
+    const auto& js_response_result = IotconUtils::GetArg(args, kResult);
     if (!js_response_result.is<std::string>()) {
       return LogAndCreateTizenError(TypeMismatchError, "ResponseResult should be a string");
     }
@@ -450,7 +434,7 @@ common::TizenResult IotconInstance::ResponseSend(const picojson::object& args) {
   }
 
   {
-    const auto& js_representation = GetArg(args, kRepresentation);
+    const auto& js_representation = IotconUtils::GetArg(args, kRepresentation);
     if (!js_representation.is<picojson::object>()) {
       return LogAndCreateTizenError(TypeMismatchError, "Representation should be an object");
     }
@@ -463,14 +447,16 @@ common::TizenResult IotconInstance::ResponseSend(const picojson::object& args) {
       iotcon_representation_destroy(representation);
     };
 
-    result = IotconUtils::ConvertIotconError(iotcon_response_set_representation(response.get(), IotconUtils::ToInterface(GetArg(args, kInterface).get<std::string>()), representation));
+    result = IotconUtils::ConvertIotconError(
+        iotcon_response_set_representation(response.get(), IotconUtils::ToInterface(
+            IotconUtils::GetArg(args, kInterface).get<std::string>()), representation));
     if (!result) {
       LogAndReturnTizenError(result, ("iotcon_response_set_representation() failed"));
     }
   }
 
   {
-    const auto& js_options = GetArg(args, kOptions);
+    const auto& js_options = IotconUtils::GetArg(args, kOptions);
 
     if (js_options.is<picojson::array>()) {
       iotcon_options_h options = nullptr;
@@ -658,7 +644,7 @@ common::TizenResult IotconInstance::ClientAddPresenceEventListener(const picojso
     resource_type = const_cast<char*>(args.find(kResourceType)->second.get<std::string>().c_str());
   }
 
-  auto& con_type = GetArg(args, kConnectivityType);
+  auto& con_type = IotconUtils::GetArg(args, kConnectivityType);
   if (!con_type.is<std::string>()) {
     return common::TypeMismatchError("connectivityType needs to be a string");
   }
@@ -794,31 +780,13 @@ common::TizenResult IotconInstance::ServerCreateResource(const picojson::object&
 
   const std::string& uri_path = args.find(kUriPath)->second.get<std::string>();
 
-  const auto& interfaces = GetArg(args, kResourceInterfaces);
+  const auto& interfaces = IotconUtils::GetArg(args, kResourceInterfaces);
   const auto& resource_interfaces = interfaces.is<picojson::array>() ? interfaces.get<picojson::array>() : picojson::array();
 
-  const auto& types = GetArg(args, kResourceTypes);
+  const auto& types = IotconUtils::GetArg(args, kResourceTypes);
   const auto& resource_types = types.is<picojson::array>() ? types.get<picojson::array>() : picojson::array();
 
-  int properties = IOTCON_RESOURCE_NO_PROPERTY;
-
-  const auto& observable = GetArg(args, kIsObservable);
-  properties |= (observable.is<bool>() ? observable.get<bool>() : false) ? IOTCON_RESOURCE_OBSERVABLE : IOTCON_RESOURCE_NO_PROPERTY;
-
-  const auto& discoverable = GetArg(args, kIsDiscoverable);
-  properties |= (discoverable.is<bool>() ? discoverable.get<bool>() : false) ? IOTCON_RESOURCE_DISCOVERABLE : IOTCON_RESOURCE_NO_PROPERTY;
-
-  const auto& active = GetArg(args, kIsActive);
-  properties |= (active.is<bool>() ? active.get<bool>() : false) ? IOTCON_RESOURCE_ACTIVE : IOTCON_RESOURCE_NO_PROPERTY;
-
-  const auto& slow = GetArg(args, kIsSlow);
-  properties |= (slow.is<bool>() ? slow.get<bool>() : false) ? IOTCON_RESOURCE_SLOW : IOTCON_RESOURCE_NO_PROPERTY;
-
-  const auto& secure = GetArg(args, kIsSecure);
-  properties |= (secure.is<bool>() ? secure.get<bool>() : false) ? IOTCON_RESOURCE_SECURE : IOTCON_RESOURCE_NO_PROPERTY;
-
-  const auto& explicit_discoverable = GetArg(args, kIsExplicitDiscoverable);
-  properties |= (explicit_discoverable.is<bool>() ? explicit_discoverable.get<bool>() : false) ? IOTCON_RESOURCE_EXPLICIT_DISCOVERABLE : IOTCON_RESOURCE_NO_PROPERTY;
+  int properties = IotconUtils::GetProperties(args);
 
   ResourceInfoPtr resource{new ResourceInfo()};
   auto ret = IotconServerManager::GetInstance().CreateResource(uri_path, resource_interfaces, resource_types,
index 762f2cb3f601db92ec63b9552840c880bd7a4bc1..60e05fcdff3ef49dac99d4674b698ab185a0f226 100644 (file)
@@ -24,6 +24,7 @@
 #include "common/tools.h"
 
 #include "iotcon/iotcon_server_manager.h"
+#include "iotcon/iotcon_client_manager.h"
 
 namespace extension {
 namespace iotcon {
@@ -99,6 +100,7 @@ const std::string kResourceChildren = "resources";
 const std::string kUriPath = "uriPath";
 const std::string kStates = "states";
 const std::string kId = "id";
+const std::string kKeepId = "keepId";
 const std::string kDeviceId = "deviceId";
 const std::string kHostAddress = "hostAddress";
 const std::string kConnectivityType = "connectivityType";
@@ -143,6 +145,40 @@ const std::string kTriggerType = "triggerType";
 using common::TizenResult;
 using common::TizenSuccess;
 
+const picojson::value& IotconUtils::GetArg(const picojson::object& args, const std::string& name) {
+  static const picojson::value kNull;
+
+  auto it = args.find(name);
+  if (args.end() == it) {
+    return kNull;
+  } else {
+    return it->second;
+  }
+}
+
+int IotconUtils::GetProperties(const picojson::object& args) {
+  int properties = IOTCON_RESOURCE_NO_PROPERTY;
+
+  const auto& observable = IotconUtils::GetArg(args, kIsObservable);
+  properties |= (observable.is<bool>() ? observable.get<bool>() : false) ? IOTCON_RESOURCE_OBSERVABLE : IOTCON_RESOURCE_NO_PROPERTY;
+
+  const auto& discoverable = IotconUtils::GetArg(args, kIsDiscoverable);
+  properties |= (discoverable.is<bool>() ? discoverable.get<bool>() : false) ? IOTCON_RESOURCE_DISCOVERABLE : IOTCON_RESOURCE_NO_PROPERTY;
+
+  const auto& active = IotconUtils::GetArg(args, kIsActive);
+  properties |= (active.is<bool>() ? active.get<bool>() : false) ? IOTCON_RESOURCE_ACTIVE : IOTCON_RESOURCE_NO_PROPERTY;
+
+  const auto& slow = IotconUtils::GetArg(args, kIsSlow);
+  properties |= (slow.is<bool>() ? slow.get<bool>() : false) ? IOTCON_RESOURCE_SLOW : IOTCON_RESOURCE_NO_PROPERTY;
+
+  const auto& secure = IotconUtils::GetArg(args, kIsSecure);
+  properties |= (secure.is<bool>() ? secure.get<bool>() : false) ? IOTCON_RESOURCE_SECURE : IOTCON_RESOURCE_NO_PROPERTY;
+
+  const auto& explicit_discoverable = IotconUtils::GetArg(args, kIsExplicitDiscoverable);
+  properties |= (explicit_discoverable.is<bool>() ? explicit_discoverable.get<bool>() : false) ? IOTCON_RESOURCE_EXPLICIT_DISCOVERABLE : IOTCON_RESOURCE_NO_PROPERTY;
+
+  return properties;
+}
 
 void IotconUtils::PropertiesToJson(int properties, picojson::object* res) {
   bool value = properties & IOTCON_RESOURCE_OBSERVABLE;
@@ -412,6 +448,66 @@ TizenResult IotconUtils::RemoteResourceToJson(iotcon_remote_resource_h handle,
   return TizenSuccess();
 }
 
+common::TizenResult IotconUtils::RemoteResourceFromJson(const picojson::object& source,
+                                                        FoundRemoteInfoPtr* ptr) {
+  ScopeLogger();
+  //checking if resource has id
+  long long id = 0;
+  if (source.find(kId)->second.is<double>()) {
+    id = static_cast<long long>(source.find(kId)->second.get<double>());
+  }
+  if (id > 0) {
+    LoggerD("Resource stored, getting it from IotconClientManager");
+    return IotconClientManager::GetInstance().GetRemoteById(id, ptr);
+  } else {
+    LoggerD("Id is not defined, creating handle using resource info");
+  }
+
+  (*ptr).reset(new FoundRemoteInfo{});
+  CHECK_EXIST(source, kHostAddress);
+  char* host_address = nullptr;
+  if (source.find(kHostAddress)->second.is<std::string>()) {
+    host_address = const_cast<char*>(source.find(kHostAddress)->second.get<std::string>().c_str());
+  }
+
+  CHECK_EXIST(source, kConnectivityType);
+  iotcon_connectivity_type_e connectivity_type = IotconUtils::ToConnectivityType(
+      source.find(kConnectivityType)->second.get<std::string>());
+
+  CHECK_EXIST(source, kUriPath);
+  char* uri_path = nullptr;
+  if (source.find(kUriPath)->second.is<std::string>()) {
+    uri_path = const_cast<char*>(source.find(kUriPath)->second.get<std::string>().c_str());
+  }
+
+  int properties = IotconUtils::GetProperties(source);
+
+  CHECK_EXIST(source, kResourceTypes);
+  const auto& types_array = source.find(kResourceTypes)->second.get<picojson::array>();
+  iotcon_resource_types_h resource_types = nullptr;
+  auto res = IotconUtils::ArrayToTypes(types_array, &resource_types);
+  if (!res) {
+    return res;
+  }
+
+  CHECK_EXIST(source, kResourceInterfaces);
+  const auto& interfaces_array = source.find(kResourceInterfaces)->second.get<picojson::array>();
+  int interfaces = IOTCON_INTERFACE_NONE;
+  res = IotconUtils::ArrayToInterfaces(interfaces_array, &interfaces);
+  if (!res) {
+    return res;
+  }
+
+  res = IotconUtils::ConvertIotconError(
+      iotcon_remote_resource_create(host_address, connectivity_type, uri_path,
+                                    properties, resource_types, interfaces, &((*ptr)->handle)));
+  if (!res) {
+    LogAndReturnTizenError(res, ("creating handle failed"));
+  }
+
+  return TizenSuccess();
+}
+
 common::TizenResult IotconUtils::RequestToJson(iotcon_request_h request,
                                                picojson::object* out) {
   ScopeLogger();
index e171dcfeb8eecd2eefcbbcde8544b1c643e121af..4decbc5a9686525a8db381f237a92e07e54f1160 100644 (file)
 namespace extension {
 namespace iotcon {
 
+#define CHECK_EXIST(args, name) \
+  if (args.end() == args.find(name)) { \
+    return common::TypeMismatchError(std::string(name) + " is required argument"); \
+  }
+
 extern const std::string kIsDiscoverable;
 extern const std::string kIsObservable;
 extern const std::string kIsActive;
@@ -46,6 +51,7 @@ extern const std::string kResourceChildren;
 extern const std::string kUriPath;
 extern const std::string kStates;
 extern const std::string kId;
+extern const std::string kKeepId;
 extern const std::string kDeviceId;
 extern const std::string kHostAddress;
 extern const std::string kConnectivityType;
@@ -55,11 +61,14 @@ extern const std::string kOptions;
 
 class ResourceInfo;
 class PresenceEvent;
+class FoundRemoteInfo;
 
 typedef std::shared_ptr<ResourceInfo> ResourceInfoPtr;
 typedef std::map<long long, ResourceInfoPtr> ResourceInfoMap;
 typedef std::shared_ptr<PresenceEvent> PresenceEventPtr;
 typedef std::map<long long, PresenceEventPtr> PresenceMap;
+typedef std::shared_ptr<FoundRemoteInfo> FoundRemoteInfoPtr;
+typedef std::map<long long, FoundRemoteInfoPtr> FoundRemotesMap;
 
 using ResponsePtr = std::shared_ptr<std::remove_pointer<iotcon_response_h>::type>;
 
@@ -100,6 +109,19 @@ struct RemoteResourceInfo {
   }
 };
 
+struct FoundRemoteInfo {
+  long long id;
+  iotcon_remote_resource_h handle;
+  short ref_count; // counter for registered listeners for this handle
+  //TODO add listeners for each type
+  FoundRemoteInfo() :
+    id(0), handle(nullptr), ref_count(1) {} //initialize with 1 (struct is created, so it
+                                            //mean that some listener would be created)
+  ~FoundRemoteInfo() {
+    iotcon_remote_resource_destroy(handle);
+  }
+};
+
 struct PresenceEvent {
   long long id;
   iotcon_presence_h handle;
@@ -108,6 +130,8 @@ struct PresenceEvent {
 
 class IotconUtils {
  public:
+  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);
@@ -122,6 +146,8 @@ class IotconUtils {
   static common::TizenResult ExtractFromRemoteResource(RemoteResourceInfo* resource);
   static common::TizenResult RemoteResourceToJson(iotcon_remote_resource_h handle,
                                                   picojson::object* res);
+  static common::TizenResult RemoteResourceFromJson(const picojson::object& source,
+                                                    FoundRemoteInfoPtr* ptr);
 
   static common::TizenResult RequestToJson(iotcon_request_h request,
                                            picojson::object* out);