From: Sangyoon Jang Date: Mon, 26 Feb 2018 11:38:46 +0000 (+0900) Subject: Implement basic functionalities for Iotivity X-Git-Tag: submit/tizen/20190208.015210~91 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cd35ec5adc49ecaa20a1da2fc0f916c94d5004ad;p=platform%2Fcore%2Fappfw%2Fcapmgr.git Implement basic functionalities for Iotivity Change-Id: I38e6165a36732091c68cd6e379306ccfcb0aff3b Signed-off-by: Sangyoon Jang --- diff --git a/CMakeLists.txt b/CMakeLists.txt index c1c99e9..6faf62b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/packaging/capmgr.spec b/packaging/capmgr.spec index 5eed1b0..8300fd6 100644 --- a/packaging/capmgr.spec +++ b/packaging/capmgr.spec @@ -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 index 0000000..5f94123 --- /dev/null +++ b/src/iotivity.cc @@ -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 +#include +#include + +#include + +#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 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 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(); + 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 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 index 0000000..a016738 --- /dev/null +++ b/src/iotivity.h @@ -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 +#include + +#include +#include + +namespace capmgr { + +class Iotivity { + public: + Iotivity(); + ~Iotivity(); + void RegisterResource(); + void FindResource(); + void GetResource(std::shared_ptr resource); + + private: + OCEntityHandlerResult EntityCb( + std::shared_ptr request); + void FindCb(std::shared_ptr resource); + + OCResourceHandle resource_; + std::map> + resource_list_; +}; + +} // namespace capmgr + +#endif // IOTIVITY_H_ diff --git a/src/main.cc b/src/main.cc index 80cb9ee..499a0cb 100644 --- a/src/main.cc +++ b/src/main.cc @@ -6,6 +6,9 @@ #include #include +// 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";