[Verification] Code compiles.
Change-Id: I41ddc1d2d81e546f65cd8c05d7692b74b19e94d3
Signed-off-by: Piotr Kosko <p.kosko@samsung.com>
});
}
+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: {
nullable: true
}]);
- var callArgs = {};
- callArgs.id = this[kIdKey];
+ var callArgs = prepareResourceInfo(this);
callArgs.query = args.query;
var callback = function(result) {
nullable: true
}]);
- var callArgs = {};
- callArgs.id = this[kIdKey];
+ var callArgs = prepareResourceInfo(this);
callArgs.representation = args.representation;
callArgs.query = args.query;
nullable: true
}]);
- var callArgs = {};
- callArgs.id = this[kIdKey];
+ var callArgs = prepareResourceInfo(this);
callArgs.representation = args.representation;
callArgs.query = args.query;
nullable: true
}]);
- var callArgs = {};
- callArgs.id = this[kIdKey];
+ var callArgs = prepareResourceInfo(this);
var callback = function(result) {
if (native.isFailure(result)) {
nullable: true
}]);
- var callArgs = {};
- callArgs.id = this[kIdKey];
+ var callArgs = prepareResourceInfo(this);
callArgs.query = args.query;
callArgs.observePolicy = args.observePolicy;
var that = this;
};
RemoteResource.prototype.unsetStateChangeListener = function() {
- var callArgs = {};
- callArgs.id = this[kIdKey];
+ var callArgs = prepareResourceInfo(this);
var result = native.callSync('IotconRemoteResource_unsetStateChangeListener', callArgs);
};
RemoteResource.prototype.startCaching = function() {
- var callArgs = {};
- callArgs.id = this[kIdKey];
+ var callArgs = prepareResourceInfo(this);
var result = native.callSync('IotconRemoteResource_startCaching', callArgs);
};
RemoteResource.prototype.stopCaching = function() {
- var callArgs = {};
- callArgs.id = this[kIdKey];
+ var callArgs = prepareResourceInfo(this);
var result = native.callSync('IotconRemoteResource_stopCaching', callArgs);
nullable: true
}]);
- var callArgs = {};
- callArgs.id = this[kIdKey];
+ var callArgs = prepareResourceInfo(this);
var listener = function(result) {
if (native.isFailure(result)) {
};
RemoteResource.prototype.unsetConnectionChangeListener = function() {
- var callArgs = {};
- callArgs.id = this[kIdKey];
+ var callArgs = prepareResourceInfo(this);
var result = native.callSync('IotconRemoteResource_unsetConnectionChangeListener', callArgs);
return ++id;
}
+long long GetRemoteNextId() {
+ static long long id = 0;
+ return ++id;
+}
+
} // namespace
IotconClientManager& IotconClientManager::GetInstance() {
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
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;
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
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"};
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;
// 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"));
}
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"));
}
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"));
}
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);
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);
}
{
- 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");
}
}
{
- 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");
}
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;
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");
}
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,
#include "common/tools.h"
#include "iotcon/iotcon_server_manager.h"
+#include "iotcon/iotcon_client_manager.h"
namespace extension {
namespace iotcon {
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";
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;
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();
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;
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;
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>;
}
};
+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;
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);
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);