Add device discovery
authorSangyoon Jang <jeremy.jang@samsung.com>
Tue, 20 Mar 2018 08:25:35 +0000 (17:25 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Fri, 23 Mar 2018 08:24:48 +0000 (17:24 +0900)
Change-Id: I3fc422576073f4f6b7fa08df96eba9082a0bbf85
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
CMakeLists.txt
data/oic_svr_db_capmgr.dat [new file with mode: 0644]
data/oic_svr_db_capmgr.json [new file with mode: 0644]
packaging/capmgr.spec
src/iotivity.cc
src/iotivity.h

index 38be32025b909b9ad5dd11482be7aef6c4a91349..aaf921a03cd304695ac7745f2227e849b44a35e8 100644 (file)
@@ -19,6 +19,9 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie -ldl")
 INCLUDE(FindPkgConfig)
 
 ADD_DEFINITIONS("-DPROJECT_TAG=\"CAPMGR\"")
+ADD_DEFINITIONS("-DSVR_DB_DIR=\"${SVR_DB_DIR}\"")
+ADD_DEFINITIONS("-DSVR_DB_PATH=\"${SVR_DB_PATH}\"")
+ADD_DEFINITIONS("-DPM_DB_PATH=\"${SVR_DB_DIR}/pdm.db\"")
 
 SET(CAPMGR "capmgr")
 #SET(SRCS src/main.cc)
@@ -38,3 +41,4 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
 TARGET_LINK_LIBRARIES(${CAPMGR} ${DEPS_LDFLAGS})
 
 INSTALL(TARGETS ${CAPMGR} DESTINATION bin)
+INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/data/oic_svr_db_capmgr.dat DESTINATION ${SVR_DB_DIR})
diff --git a/data/oic_svr_db_capmgr.dat b/data/oic_svr_db_capmgr.dat
new file mode 100644 (file)
index 0000000..196c430
Binary files /dev/null and b/data/oic_svr_db_capmgr.dat differ
diff --git a/data/oic_svr_db_capmgr.json b/data/oic_svr_db_capmgr.json
new file mode 100644 (file)
index 0000000..652caad
--- /dev/null
@@ -0,0 +1,73 @@
+{
+    "acl": {
+        "aclist": {
+            "aces": [
+                {
+                    "subjectuuid": "*",
+                    "resources": [
+                        {
+                            "href": "/oic/res",
+                            "rel": "",
+                            "rt": ["oic.wk.res"],
+                            "if": ["oic.if.ll"]
+                        },{
+                            "href": "/oic/d",
+                            "rel": "",
+                            "rt": ["oic.wk.d"],
+                            "if": ["oic.if.baseline", "oic.if.r"]
+                        },{
+                            "href": "/oic/p",
+                            "rel": "",
+                            "rt": ["oic.wk.p"],
+                            "if": ["oic.if.baseline", "oic.if.r"]
+                        }
+                    ],
+                    "permission": 2
+                },
+                {
+                    "subjectuuid": "*",
+                    "resources": [
+                        {
+                            "href": "/oic/sec/doxm",
+                            "rel": "",
+                            "rt": ["oic.r.doxm"],
+                            "if": ["oic.if.baseline"]
+                        },
+                        {
+                            "href": "/oic/sec/pstat",
+                            "rel": "",
+                            "rt": ["oic.r.pstat"],
+                            "if": ["oic.if.baseline"]
+                        },
+                        {
+                            "href": "/oic/sec/cred",
+                            "rel": "",
+                            "rt": ["oic.r.cred"],
+                            "if": ["oic.if.baseline"]
+                        }
+                    ],
+                    "permission": 6
+                }
+            ]
+        },
+        "rowneruuid": "00000000-0000-0000-0000-000000000000"
+    },
+    "pstat": {
+        "isop": false,
+        "deviceuuid": "00000000-0000-0000-0000-000000000000",
+        "rowneruuid": "00000000-0000-0000-0000-000000000000",
+        "cm": 2,
+        "tm": 0,
+        "om": 4,
+        "sm": 4
+     },
+    "doxm": {
+        "oxms": [0],
+        "oxmsel": 0,
+        "sct": 1,
+        "owned": false,
+        "deviceuuid": "00000000-0000-0000-0000-000000000000",
+        "devowneruuid": "",
+        "rowneruuid": "00000000-0000-0000-0000-000000000000"
+    }
+}
index b7fe073112261da236b3cf5fdb60523a5d7ea85f..7b2c387eaf0b42324197960aeb432c9968a648b5 100644 (file)
@@ -1,3 +1,6 @@
+%define svr_db_dir %{TZ_SYS_GLOBALUSER_DATA}/capmgr
+%define svr_db_path %{svr_db_dir}/oic_svr_db_capmgr.dat
+
 Name:       capmgr
 Summary:    Capability Manager
 Version:    0.0.1
@@ -13,6 +16,7 @@ BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(glib-2.0)
 BuildRequires:  pkgconfig(iotivity)
 BuildRequires:  pkgconfig(jsoncpp)
+BuildRequires:  pkgconfig(libtzplatform-config)
 BuildRequires:  pkgconfig(pkgmgr)
 BuildRequires:  pkgconfig(pkgmgr-info)
 
@@ -24,13 +28,16 @@ Tizen Capability Manager
 cp %{SOURCE1001} .
 
 %build
-%cmake .
+%cmake . -DSVR_DB_DIR=%{svr_db_dir} \
+         -DSVR_DB_PATH=%{svr_db_path}
 
 %__make %{?_smp_mflags}
 
 %install
 %make_install
 
+mkdir -p %{buildroot}%{svr_db_dir}
+
 %post
 /sbin/ldconfig
 
@@ -39,3 +46,4 @@ cp %{SOURCE1001} .
 %license LICENSE
 %defattr(-,root,root,-)
 %{_bindir}/*
+%{svr_db_path}
index ce3a54fe78c61c2aa0682247935846192a141096..2e856a4ea5d8c73b2f013c22c363e79689e56996 100644 (file)
@@ -4,31 +4,78 @@
 
 #include "src/iotivity.h"
 
+#include <octypes.h>
+#include <pmtypes.h>
+#include <OCPlatform.h>
+
+#include <OCProvisioningManager.hpp>
+
 #include <functional>
 #include <iostream>
 #include <string>
 
-#include <OCPlatform.h>
-
 #include "src/utils/logging.h"
 
 namespace {
 
+const int kDiscoverTimeoutSec = 5;
+const int kUUIDLength = UUID_LENGTH;
 const char kResourceTypeName[] = "capmgr.capabilities";
 const std::string kResourceInterface = OC::DEFAULT_INTERFACE;
 
+FILE* ServerFopen(const char* /* unused */, const char* mode) {
+  return fopen(SVR_DB_PATH, mode);
+}
+
+std::string ConvertUUIDtoString(const OicUuid_t& uuid) {
+  std::ostringstream oss;
+  for (int i = 0; i < kUUIDLength; i++)
+    oss << uuid.id[i];
+  return oss.str();
+}
+
+void OtmEventCb(void* /* ctx */, const char* addr, uint16_t port,
+    const char* owner_id, OCOtmEvent_t event) {
+  LOG(INFO) << "Get OTM Event";
+  LOG(INFO) << "Addr: " << addr;
+  LOG(INFO) << "owner_id: " << owner_id;
+  switch (event) {
+    case OC_OTM_READY:
+      LOG(INFO) << "OC_OTM_READY";
+      break;
+    case OC_OTM_STARTED:
+      LOG(INFO) << "OC_OTM_STARTED";
+      break;
+    case OC_OTM_DONE:
+      LOG(INFO) << "OC_OTM_DONE";
+      break;
+    case OC_OTM_ERROR:
+      LOG(INFO) << "OC_OTM_ERROR";
+      break;
+    default:
+      LOG(ERROR) << "Unknown OTM event";
+      break;
+  }
+}
+
 }  // namespace
 
 namespace capmgr {
 
 Iotivity::Iotivity() {
-  // legacy constructor
+  OCSetOtmEventHandler(nullptr, OtmEventCb);
+
+  ps_ = {
+    ServerFopen, fread, fwrite, fclose, unlink, nullptr, nullptr
+  };
+
   OC::PlatformConfig config = OC::PlatformConfig {
     OC::ServiceType::InProc,
     OC::ModeType::Both,
     "0.0.0.0",
     0,
     OC::QualityOfService::LowQos,
+    &ps_
   };
 
   OC::OCPlatform::Configure(config);
@@ -46,7 +93,7 @@ Iotivity::~Iotivity() {
 
 void Iotivity::RegisterResource() {
   std::string uri = "/capmgr/capabilities";
-  uint8_t property = OC_DISCOVERABLE | OC_OBSERVABLE;  // OC_SECURE
+  uint8_t property = OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE;
 
   OCStackResult result = OC::OCPlatform::registerResource(resource_,
       uri, kResourceTypeName, kResourceInterface,
@@ -81,6 +128,34 @@ void Iotivity::GetResource(std::shared_ptr<OC::OCResource> resource) {
       });
 }
 
+void Iotivity::DiscoverUnownedDevices() {
+  if (OC::OCSecure::provisionInit(PM_DB_PATH) != OC_STACK_OK)
+    LOG(ERROR) << "provisionInit() failed!";
+
+  unowned_dev_list_.clear();
+  OCStackResult result =
+      OC::OCSecure::discoverUnownedDevices(kDiscoverTimeoutSec,
+          unowned_dev_list_);
+
+  if (result != OC_STACK_OK)
+    LOG(ERROR) << "discoverUnownedDevices() failed: " << result;
+  else
+    LOG(INFO) << "Discovered " << unowned_dev_list_.size()
+              << " unowned devices";
+
+  for (const auto& sec_rsc : unowned_dev_list_) {
+    LOG(INFO) << "Do ownership transfer: " << sec_rsc->getDevAddr();
+    sec_rsc->doOwnershipTransfer([this](OC::PMResultList_t* result, int hasError) {
+          if (hasError) {
+            LOG(ERROR) << "Failed to ownership transfer";
+            return;
+          }
+          LOG(INFO) << "Ownership transferred: "
+                    << ConvertUUIDtoString(result->at(0).deviceId);
+        });
+  }
+}
+
 OCEntityHandlerResult Iotivity::EntityCb(
     std::shared_ptr<OC::OCResourceRequest> request) {
   LOG(DEBUG) << "entity cb";
index a016738b9b4e023fab50f12518ce02cd21c38a0f..ad407ae15532f95298e4325ad26cdd6f7e3eeba1 100644 (file)
@@ -5,9 +5,13 @@
 #ifndef IOTIVITY_H_
 #define IOTIVITY_H_
 
+#include <octypes.h>
+#include <pmtypes.h>
 #include <OCApi.h>
 #include <OCPlatform.h>
 
+#include <OCProvisioningManager.hpp>
+
 #include <map>
 #include <memory>
 
@@ -20,15 +24,18 @@ class Iotivity {
   void RegisterResource();
   void FindResource();
   void GetResource(std::shared_ptr<OC::OCResource> resource);
+  void DiscoverUnownedDevices();
 
  private:
   OCEntityHandlerResult EntityCb(
       std::shared_ptr<OC::OCResourceRequest> request);
   void FindCb(std::shared_ptr<OC::OCResource> resource);
 
+  OCPersistentStorage ps_;
   OCResourceHandle resource_;
   std::map<OC::OCResourceIdentifier, std::shared_ptr<OC::OCResource>>
       resource_list_;
+  OC::DeviceList_t unowned_dev_list_;
 };
 
 }  // namespace capmgr