[Iotcon] Added server_manager and utils stubs
authorPiotr Kosko <p.kosko@samsung.com>
Thu, 21 Jan 2016 07:51:52 +0000 (08:51 +0100)
committerPiotr Kosko <p.kosko@samsung.com>
Mon, 25 Jan 2016 07:35:30 +0000 (08:35 +0100)
[Verification] Code compiles without errors

Change-Id: I0f606a402a0f0f6faf4b9939aab4330d133f0ef9
Signed-off-by: Piotr Kosko <p.kosko@samsung.com>
src/iotcon/iotcon.gyp
src/iotcon/iotcon_instance.cc
src/iotcon/iotcon_instance.h
src/iotcon/iotcon_server_manager.cc [new file with mode: 0644]
src/iotcon/iotcon_server_manager.h [new file with mode: 0644]
src/iotcon/iotcon_utils.cc [new file with mode: 0644]
src/iotcon/iotcon_utils.h [new file with mode: 0644]

index 5d23de52518f305e85483a07599310a561811d1d..b5fd720f46a62f75db5a2d58ad5d2159cd7c6513 100644 (file)
         'iotcon_extension.cc',
         'iotcon_extension.h',
         'iotcon_instance.cc',
-        'iotcon_instance.h'
+        'iotcon_instance.h',
+        'iotcon_server_manager.cc',
+        'iotcon_server_manager.h',
+        'iotcon_utils.cc',
+        'iotcon_utils.h',
       ],
       'includes': [
         '../common/pkg-config.gypi',
index 826671e79de04e0c2afd13e541e75b653283a828..0cd3e9398488827afed5e5db0dc4a101bb237acf 100644 (file)
 
 #include "iotcon/iotcon_instance.h"
 
-#include <iotcon.h>
-
 #include "common/logger.h"
+#include "common/task-queue.h"
+#include "common/tools.h"
+
+#include "iotcon/iotcon_utils.h"
+
+using common::PlatformResult;
+using common::ErrorCode;
+using common::TypeMismatchException;
+using common::tools::ReportError;
+using common::tools::ReportSuccess;
+using common::Instance;
+using common::TaskQueue;
 
 namespace extension {
 namespace iotcon {
 
-IotconInstance::IotconInstance() {
+namespace {
+const std::string kCallbackId = "callbackId";
+const std::string kIsDiscoverable = "isDiscoverable";
+const std::string kIsObservable = "isObservable";
+const std::string kResourceTypes = "resourceTypes";
+const std::string kUriPath = "uriPath";
+
+#define CHECK_EXIST(args, name, out) \
+  if (!args.contains(name)) {\
+    std::string message = std::string(name) + " is required argument";\
+    common::tools::ReportError(\
+        common::PlatformResult(common::ErrorCode::TYPE_MISMATCH_ERR, message), &out);\
+    return;\
+  }
+}
+
+IotconInstance::IotconInstance() : manager_(this) {
   LoggerD("Enter");
   using std::placeholders::_1;
   using std::placeholders::_2;
@@ -102,8 +128,10 @@ IotconInstance::IotconInstance() {
     LoggerE("Could not connnect to iotcon service: %s", get_error_message(ret));
   } else {
     ret = iotcon_add_connection_changed_cb(ConnectionChangedCallback, nullptr);
-    LoggerE("Could not add connection changed callback for iotcon service: %s",
-            get_error_message(ret));
+    if (IOTCON_ERROR_NONE != ret) {
+      LoggerE("Could not add connection changed callback for iotcon service: %s",
+              get_error_message(ret));
+    }
   }
 }
 
@@ -280,7 +308,46 @@ void IotconInstance::IotconClientGetPlatformInfo(const picojson::value& args,
 void IotconInstance::IotconServerCreateResource(const picojson::value& args,
                                                 picojson::object& out) {
   LoggerD("Enter");
+  LoggerD("args: %s", args.serialize().c_str());
+  //args: {"callbackId":2,"isDiscoverable":true,
+  //"isObservable":true,"resourceTypes":["t1","t2"],"uriPath":"uriPath"}
+
+  CHECK_EXIST(args, kCallbackId, out)
+  CHECK_EXIST(args, kIsDiscoverable, out)
+  CHECK_EXIST(args, kIsObservable, out)
+  CHECK_EXIST(args, kResourceTypes, out)
+  CHECK_EXIST(args, kUriPath, out)
+
+  const double callback_id = args.get(kCallbackId).get<double>();
+  const bool is_discoverable = args.get(kIsDiscoverable).get<bool>();
+  const bool is_observable = args.get(kIsObservable).get<bool>();
+  const auto& resource_type = args.get(kResourceTypes).get<picojson::array>();
+  const std::string& uri_path = args.get(kUriPath).get<std::string>();
+
+  auto create = [this, callback_id, is_discoverable, is_observable, resource_type, uri_path]
+                 (const std::shared_ptr<picojson::value>& response) -> void {
+    LoggerD("Create resource");
+    picojson::value result = picojson::value(picojson::object());
+    // TODO implement CreateResource
+    PlatformResult ret = manager_.CreateResource();
+    if (ret.IsError()) {
+      LogAndReportError(ret,&(response->get<picojson::object>()));
+      return;
+    }
+    ReportSuccess(result, response->get<picojson::object>());
+  };
+
+  auto create_response = [this, callback_id](const std::shared_ptr<picojson::value>& response) -> void {
+    LoggerD("Response");
+    picojson::object& obj = response->get<picojson::object>();
+    obj.insert(std::make_pair("callbackId", picojson::value{static_cast<double>(callback_id)}));
+    LoggerD("message: %s", response->serialize().c_str());
+    Instance::PostMessage(this, response->serialize().c_str());
+  };
+
+  auto data = std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
 
+  TaskQueue::GetInstance().Queue<picojson::value>(create, create_response, data);
 }
 
 void IotconInstance::IotconServerRemoveResource(const picojson::value& args,
index ea8b3cc9f5b79dbbeef8d48a31a5ca891c8eefbf..97bfb5287d6dcfbaf6dece6fdb2579a9dafd2fcc 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "common/picojson.h"
 #include "common/extension.h"
+#include "iotcon/iotcon_server_manager.h"
 
 namespace extension {
 namespace iotcon {
@@ -90,6 +91,8 @@ class IotconInstance : public common::ParsedInstance {
                         picojson::object& out);
   void IotconSetTimeout(const picojson::value& args,
                         picojson::object& out);
+
+  IotconServerManager manager_;
 };
 
 }  // namespace iotcon
diff --git a/src/iotcon/iotcon_server_manager.cc b/src/iotcon/iotcon_server_manager.cc
new file mode 100644 (file)
index 0000000..07663d4
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    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 "iotcon/iotcon_server_manager.h"
+
+#include "common/logger.h"
+
+#include "iotcon/iotcon_instance.h"
+
+namespace extension {
+namespace iotcon {
+
+using common::PlatformResult;
+using common::ErrorCode;
+
+IotconServerManager::IotconServerManager(IotconInstance* instance)
+      : instance_(instance) {
+  LoggerD("Entered");
+}
+
+IotconServerManager::~IotconServerManager() {
+  LoggerD("Enter");
+}
+
+PlatformResult IotconServerManager::RestoreHandles() {
+  LoggerD("Entered");
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+PlatformResult IotconServerManager::CreateResource() {
+  LoggerD("Entered");
+  // TODO implement
+  return PlatformResult(ErrorCode::NOT_SUPPORTED_ERR, "Not implemented yet");
+}
+
+}  // namespace iotcon
+}  // namespace extension
diff --git a/src/iotcon/iotcon_server_manager.h b/src/iotcon/iotcon_server_manager.h
new file mode 100644 (file)
index 0000000..8e3e45a
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    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 WEBAPI_PLUGINS_IOTCON_SERVER_MANAGER_H__
+#define WEBAPI_PLUGINS_IOTCON_SERVER_MANAGER_H__
+
+#include <memory>
+#include <map>
+#include <string>
+#include <iotcon.h>
+
+#include "common/picojson.h"
+#include "common/platform_result.h"
+
+namespace extension {
+namespace iotcon {
+
+struct ResourceInfo {
+  const char *uri_path;
+  iotcon_resource_types_h res_types;
+  int ifaces;
+  int properties;
+  iotcon_resource_h handle;
+  ResourceInfo() :
+    uri_path(nullptr), res_types(nullptr), ifaces(0),
+    properties(0), handle(nullptr) {}
+  ~ResourceInfo() {
+    delete uri_path;
+    iotcon_resource_types_destroy(res_types);
+    iotcon_resource_destroy (handle);
+  }
+};
+
+typedef std::shared_ptr<ResourceInfo> ResourceInfoPtr;
+typedef std::map<long long, ResourceInfoPtr> ResourceInfoMap;
+
+class IotconInstance;
+
+class IotconServerManager {
+ public:
+  IotconServerManager(IotconInstance* instance);
+  ~IotconServerManager();
+
+  common::PlatformResult RestoreHandles();
+
+  common::PlatformResult CreateResource(/*std::string uri_path, bool is_discoverable,
+                                        bool is_observable, picojson::array array,
+                                        iotcon_resource_h* res_handle*/);
+
+ private:
+  IotconInstance* instance_;
+  ResourceInfoMap resource_map_;
+};
+} // namespace iotcon
+} // namespace extension
+
+#endif // WEBAPI_PLUGINS_IOTCON_SERVER_MANAGER_H__
diff --git a/src/iotcon/iotcon_utils.cc b/src/iotcon/iotcon_utils.cc
new file mode 100644 (file)
index 0000000..2abd7ea
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    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 "iotcon_utils.h"
+
+#include <memory>
+
+#include "common/logger.h"
+#include "common/platform_exception.h"
+#include "common/scope_exit.h"
+
+namespace extension {
+namespace iotcon {
+
+using common::PlatformResult;
+using common::ErrorCode;
+
+PlatformResult IotconUtils::ResourceToJson(iotcon_resource_h handle, picojson::value* res) {
+  LoggerD("Entered");
+
+  // TODO implement conversion
+  return LogAndCreateResult(ErrorCode::NOT_SUPPORTED_ERR, "Not implemented yet");
+}
+
+} // namespace iotcon
+} // namespace extension
diff --git a/src/iotcon/iotcon_utils.h b/src/iotcon/iotcon_utils.h
new file mode 100644 (file)
index 0000000..e9c6328
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    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 WEBAPI_PLUGINS_IOTCON_IOTCON_UTILS_H__
+#define WEBAPI_PLUGINS_IOTCON_IOTCON_UTILS_H__
+
+#include <string>
+
+#include <iotcon.h>
+
+#include "common/picojson.h"
+#include "common/platform_result.h"
+
+namespace extension {
+namespace iotcon {
+
+class IotconUtils {
+ public:
+  static common::PlatformResult ResourceToJson(iotcon_resource_h handle, picojson::value* res);
+};
+
+} // namespace iotcon
+} // namespace extension
+
+#endif // WEBAPI_PLUGINS_IOTCON_IOTCON_UTILS_H__