--- /dev/null
+// 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