Implement basic functionalities for Iotivity
authorSangyoon Jang <jeremy.jang@samsung.com>
Mon, 26 Feb 2018 11:38:46 +0000 (20:38 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Mon, 26 Feb 2018 11:45:26 +0000 (20:45 +0900)
Change-Id: I38e6165a36732091c68cd6e379306ccfcb0aff3b
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
CMakeLists.txt
packaging/capmgr.spec
src/iotivity.cc [new file with mode: 0644]
src/iotivity.h [new file with mode: 0644]
src/main.cc

index c1c99e97aa17c9da3a9438adf618ac33568ea7d1..6faf62b53fe7d840d8304bf6eee95ad33abea520 100644 (file)
@@ -25,7 +25,7 @@ SET(CAPMGR "capmgr")
 AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src SRC)
 AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src/utils SRC_UTILS)
 
-SET(SERVER_CHECK_MODULES dlog glib-2.0 pkgmgr pkgmgr-info)
+SET(SERVER_CHECK_MODULES dlog glib-2.0 iotivity pkgmgr pkgmgr-info)
 
 PKG_CHECK_MODULES(DEPS REQUIRED ${SERVER_CHECK_MODULES})
 
index 5eed1b0e984e062e188aa32ff2112cb5d2d9659b..8300fd63b2cb2da6d4891df65ddefe4f3e6cfe39 100644 (file)
@@ -6,13 +6,14 @@ Group:      Application Framework/Package Management
 License:    Apache-2.0
 Source0:    %{name}-%{version}.tar.gz
 Source1001: %{name}.manifest
+BuildRequires:  boost-devel
 BuildRequires:  cmake
-BuildRequires:  pkgconfig(glib-2.0)
-#BuildRequires:  pkgconfig(gio-2.0)
 BuildRequires:  pkgconfig(dlog)
+#BuildRequires:  pkgconfig(gio-2.0)
+BuildRequires:  pkgconfig(glib-2.0)
+BuildRequires:  pkgconfig(iotivity)
 BuildRequires:  pkgconfig(pkgmgr)
 BuildRequires:  pkgconfig(pkgmgr-info)
-BuildRequires:  boost-devel
 
 %description
 Tizen Capability Manager
diff --git a/src/iotivity.cc b/src/iotivity.cc
new file mode 100644 (file)
index 0000000..5f94123
--- /dev/null
@@ -0,0 +1,139 @@
+// Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache 2.0 license that can be
+// found in the LICENSE file.
+
+#include "src/iotivity.h"
+
+#include <functional>
+#include <iostream>
+#include <string>
+
+#include <OCPlatform.h>
+
+#include "src/utils/logging.h"
+
+namespace {
+
+const char kResourceTypeName[] = "capmgr.capabilities";
+const std::string kResourceInterface = OC::DEFAULT_INTERFACE;
+
+}  // namespace
+
+namespace capmgr {
+
+Iotivity::Iotivity() {
+  // legacy constructor
+  OC::PlatformConfig config = OC::PlatformConfig {
+    OC::ServiceType::InProc,
+    OC::ModeType::Both,
+    "0.0.0.0",
+    0,
+    OC::QualityOfService::LowQos,
+  };
+
+  OC::OCPlatform::Configure(config);
+/*
+  OCStackResult result = SetPlatformInfo(kPlatformId, kManufactureName,
+      kManufacturerLink, kModelNumber, kDateOfManufacture, kPlatformVersion,
+      kOperatingSystemVersion, kHardwareVersion, kFirmwareVersion, kSupportLink,
+      kSystemTime);
+  result = OCPlatform::registerPlatformInfo(platformInfo);
+*/
+}
+
+Iotivity::~Iotivity() {
+}
+
+void Iotivity::RegisterResource() {
+  std::string uri = "/capmgr/capabilities";
+  uint8_t property = OC_DISCOVERABLE | OC_OBSERVABLE;  // OC_SECURE
+
+  OCStackResult result = OC::OCPlatform::registerResource(resource_,
+      uri, kResourceTypeName, kResourceInterface,
+      std::bind(&Iotivity::EntityCb, this, std::placeholders::_1),
+      property);
+  if (result != OC_STACK_OK)
+    LOG(ERROR) << "OCPlatform::registerResource() failed: " << result;
+
+  LOG(INFO) << "Registered as: " << uri;
+}
+
+void Iotivity::FindResource() {
+  OCStackResult result = OC::OCPlatform::findResource(nullptr, nullptr,
+      CT_DEFAULT, std::bind(&Iotivity::FindCb, this, std::placeholders::_1));
+  if (result != OC_STACK_OK)
+    LOG(ERROR) << "OCPlatform::findResource() failed: " << result;
+}
+
+void Iotivity::GetResource(std::shared_ptr<OC::OCResource> resource) {
+  // empty QueryParams
+  OC::QueryParamsMap qmap;
+  resource->get(qmap,
+      [](const OC::HeaderOptions& opts, const OC::OCRepresentation& rep,
+          const int ec) {
+        if (ec != OC_STACK_OK) {
+          LOG(ERROR) << "GET error";
+          return;
+        }
+
+        std::string data;
+        rep.getValue("data", data);
+      });
+}
+
+OCEntityHandlerResult Iotivity::EntityCb(
+    std::shared_ptr<OC::OCResourceRequest> request) {
+  LOG(DEBUG) << "entity cb";
+  if (!request) {
+    LOG(ERROR) << "Invalid OC request";
+    return OC_EH_ERROR;
+  }
+
+  std::string req_type = request->getRequestType();
+  if (req_type != "GET") {
+    LOG(WARNING) << "The request is not GET";
+    return OC_EH_OK;
+  }
+
+  // OC::QueryParamsMap qmap = request->getQueryParameters();
+
+  auto response = std::make_shared<OC::OCResourceResponse>();
+  response->setErrorCode(200);  // ??
+  response->setResponseResult(OC_EH_OK);
+
+  OC::OCRepresentation rep;
+
+  response->setResourceRepresentation(rep);
+  // TODO(jeremy.jang): return capabilities
+  rep.setValue("data", "TESTDATA");
+  OCStackResult result = OC::OCPlatform::sendResponse(response);
+  if (result != OC_STACK_OK) {
+    LOG(ERROR) << "OCPlatform::sendResponse() error: " << result;
+  }
+
+  return OC_EH_OK;
+}
+
+void Iotivity::FindCb(std::shared_ptr<OC::OCResource> resource) {
+  if (!resource) {
+    LOG(ERROR) << "Invalid resource discovered";
+    return;
+  }
+
+  if (resource_list_.find(resource->uniqueIdentifier()) !=
+      resource_list_.end()) {
+    LOG(DEBUG) << "Already discovered. Skip this resource";
+    return;
+  }
+
+  LOG(DEBUG) << "Resource discovered: " << resource->uri();
+  for (std::string& type : resource->getResourceTypes())
+    LOG(DEBUG) << "  type: " << type;
+
+  for (std::string& interface : resource->getResourceInterfaces())
+    LOG(DEBUG) << "  interface: " << interface;
+
+  resource_list_[resource->uniqueIdentifier()] = resource;
+}
+
+}  // namespace capmgr
diff --git a/src/iotivity.h b/src/iotivity.h
new file mode 100644 (file)
index 0000000..a016738
--- /dev/null
@@ -0,0 +1,36 @@
+// Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache 2.0 license that can be
+// found in the LICENSE file.
+
+#ifndef IOTIVITY_H_
+#define IOTIVITY_H_
+
+#include <OCApi.h>
+#include <OCPlatform.h>
+
+#include <map>
+#include <memory>
+
+namespace capmgr {
+
+class Iotivity {
+ public:
+  Iotivity();
+  ~Iotivity();
+  void RegisterResource();
+  void FindResource();
+  void GetResource(std::shared_ptr<OC::OCResource> resource);
+
+ private:
+  OCEntityHandlerResult EntityCb(
+      std::shared_ptr<OC::OCResourceRequest> request);
+  void FindCb(std::shared_ptr<OC::OCResource> resource);
+
+  OCResourceHandle resource_;
+  std::map<OC::OCResourceIdentifier, std::shared_ptr<OC::OCResource>>
+      resource_list_;
+};
+
+}  // namespace capmgr
+
+#endif  // IOTIVITY_H_
index 80cb9eed0fc7d554acaa7f15c2abe0ffb4a0a316..499a0cb14165ed02194baf87e421b9f65b20df9c 100644 (file)
@@ -6,6 +6,9 @@
 #include <signal.h>
 #include <sys/signalfd.h>
 
+// iotivity headers should be included before boost headers...
+// this makes compilation warning. (redefinition)
+#include "src/iotivity.h"
 #include "src/capability_manager.h"
 #include "src/package_event_listener.h"
 #include "src/utils/logging.h"
@@ -90,6 +93,9 @@ int main(int argc, char* argv[]) {
   capmgr::PackageEventListener pkgmgr;
   pkgmgr.SubscribePackageEvent();
 
+  capmgr::Iotivity iotivity;
+  iotivity.RegisterResource();
+
   source = RegisterSignalHandler();
   if (source == 0)
     LOG(ERROR) << "Failed to register signal handler";