bool isOnline() override;
- void ownDevice(const std::string& provider, const std::string& auth_code) override;
+ void ownDevice() override;
void unOwnDevice() override;
private:
#include "iotdevice_impl.h"
#include "iotivity.h"
#include <chrono>
+#include <stdexcept>
//#include <OCProvisioningManager.hpp>
#include "EasySetup.h"
return online;
}
-void IoTDevice_impl::ownDevice(const std::string& provider, const std::string& auth_code)
+void IoTDevice_impl::ownDevice()
{
- const auto& cred = IoTivity::getInstance()->getOAuthCredentials();
+ auto iotinst = IoTivity::getInstance();
+ const auto& cred = iotinst->getOAuthCredentials();
OIC::Service::CloudProp cp;
- cp.setCloudProp(auth_code, provider, cred.host);
+ cp.setCloudProp(iotinst->getAuthCode(), cred.tokenProvider, cred.host);
cp.setCloudID(cred.cloudId);
cp.setCredID(1);
auto enrolee = OIC::Service::EasySetup::getInstance()->createRemoteEnrollee(dev);
+ std::unique_lock<std::mutex> lock(mtx);
+ std::string error{"Unknown error"};
+
enrolee->provisionCloudProperties(cp,
- [](std::shared_ptr<OIC::Service::CloudPropProvisioningStatus> status)
+ [this, &error](std::shared_ptr<OIC::Service::CloudPropProvisioningStatus> status)
{
switch(status->getESResult())
{
case ES_OK:
+ error = "";
std::cout << "!!! Cloud Provisioning is success. !!!" << std::endl;
break;
case ES_SECURE_RESOURCE_DISCOVERY_FAILURE:
- std::cout << "!!! Enrollee is not found in a given network. !!!" << std::endl;
+ error = "!!! Enrollee is not found in a given network. !!!";
+ std::cout << error << std::endl;
break;
case ES_ACL_PROVISIONING_FAILURE:
- std::cout << "!!! ACL provisioning is failed. !!!" << std::endl;
+ error = "!!! ACL provisioning is failed. !!!";
+ std::cout << error << std::endl;
break;
case ES_CERT_PROVISIONING_FAILURE:
- std::cout << "!!! CERT provisioning is failed. !!!" << std::endl;
+ error = "!!! CERT provisioning is failed. !!!";
+ std::cout << error << std::endl;
break;
default:
- std::cout << "!!! Cloud Provisioning is failed. !!!" << std::endl;
+ error = "!!! Cloud Provisioning is failed. !!!";
+ std::cout << error << std::endl;
break;
}
+
+ cond_var.notify_one();
}
);
+
+ cond_var.wait_for(lock, std::chrono::seconds(CALLBACK_WAIT_TIMEOUT_S));
+
+ if (error != "") throw std::runtime_error(error);
}
void IoTDevice_impl::unOwnDevice()
IoTivity* IoTivity::instance = nullptr;
+std::string IoTivity::getAuthCode()
+{
+ // TODO add normal implementation
+ std::string authcode = "AuthCode_A";
+ return authcode;
+}
+
IoTivity::IoTivity(): signedIn(false)
{
params = new Params{OC::PlatformConfig{
std::unique_lock<std::mutex> lock(mtx);
std::condition_variable condVar;
- // TODO add authcode provider
- std::string authcode = "AuthCode_A";
+ std::string authcode = getAuthCode();
signedIn = false;
int resultCodeCb;
bool signUpTimeout = true;
struct NM_DeviceList {
IoTDevicesMap map;
+ IoTDevicesMap::iterator it;
};
NM_ErrorCode NM_init(NM_hContext* ctx)
{
if (dev_list == nullptr) return EC_NULL_POINTER;
*dev_list = new NM_DeviceList{ctx->instance->getOwnedDevices()};
+ (*dev_list)->it = (*dev_list)->map.begin();
return EC_OK;
}
NM_ErrorCode NM_getUnownedDevices(NM_hContext ctx, NM_hDeviceList* dev_list)
{
if (dev_list == nullptr) return EC_NULL_POINTER;
- *dev_list = new NM_DeviceList{ctx->instance->getUnOwnedDevices()};
+ *dev_list = new NM_DeviceList{ctx->instance->getUnOwnedDevices()};\
+ (*dev_list)->it = (*dev_list)->map.begin();
return EC_OK;
}
void NM_resetDeviceList(NM_hDeviceList dev_list)
{
- if (dev_list != nullptr) delete dev_list;
+ if (dev_list != nullptr)
+ {
+ dev_list->it = dev_list->map.begin();
+ }
}
const char* NM_deviceListEnum(NM_hDeviceList dev_list)
{
- return nullptr;
+ const char* uid = nullptr;
+ if (dev_list->it != dev_list->map.end())
+ {
+ uid = dev_list->it->first.c_str();
+ ++(dev_list->it);
+ }
+ return uid;
}
NM_ErrorCode NM_deviceListForEach(NM_hDeviceList dev_list, NM_deviceEnumerationCb callback, void* user_defined)
NM_ErrorCode NM_ownDevice(NM_hDeviceList dev_list, const char* dev_id)
{
- return EC_NOT_IMPLEMENTED_YET;
+ if (dev_list == nullptr || dev_id == nullptr) return EC_NULL_POINTER;
+
+ auto it = dev_list->map.find(dev_id);
+ if (it == dev_list->map.end()) return EC_ITEM_NOT_FOUND;
+
+ try
+ {
+ it->second->ownDevice();
+ }
+ catch (std::exception& e)
+ {
+ // TODO: add error logging
+ return EC_IOTIVITY_ERROR;
+ }
+
+ return EC_OK;
}
NM_ErrorCode NM_unOwnDevice(NM_hDeviceList dev_list, const char* dev_id)
virtual bool isOnline() = 0;
- virtual void ownDevice(const std::string& provider, const std::string& auth_code) = 0;
+ virtual void ownDevice() = 0;
virtual void unOwnDevice() = 0;
};
return oAuthCred;
}
+ std::string getAuthCode();
+
static IoTivity* getInstance();
/**
ASSERT_EQ(EC_NULL_POINTER, NM_getUnownedDevices(ctx, nullptr));
+ ASSERT_EQ(EC_OK, NM_freeDeviceList(nullptr));
+
+ ASSERT_EQ(EC_OK, NM_getOwnedDevices(ctx, &dev_list));
+ std::cout << "Owned devices found: " << NM_getListSize(dev_list) << std::endl;
+ ASSERT_EQ(EC_OK, NM_deviceListForEach(dev_list, each_callback, nullptr));
+
+ ASSERT_EQ(EC_OK, NM_freeDeviceList(&dev_list));
+
ASSERT_EQ(EC_OK, NM_getUnownedDevices(ctx, &dev_list));
std::cout << "Unowned devices found: " << NM_getListSize(dev_list) << std::endl;
ASSERT_EQ(EC_NULL_POINTER, NM_deviceListForEach(nullptr, each_callback, nullptr));
ASSERT_EQ(EC_OK, NM_deviceListForEach(dev_list, each_callback, nullptr));
- NM_freeDeviceList(nullptr);
- NM_freeDeviceList(&dev_list);
- ASSERT_EQ(EC_OK, NM_getOwnedDevices(ctx, &dev_list));
- std::cout << "Owned devices found: " << NM_getListSize(dev_list) << std::endl;
- ASSERT_EQ(EC_OK, NM_deviceListForEach(dev_list, each_callback, nullptr));
+ auto list_size = NM_getListSize(dev_list);
+ if (list_size > 0)
+ {
+ ASSERT_NO_THROW(NM_resetDeviceList(dev_list));
+ const char* uid = nullptr;
+ ASSERT_NE(nullptr, uid = NM_deviceListEnum(dev_list));
+ ASSERT_EQ(EC_OK, NM_ownDevice(dev_list, uid));
+ }
+
+ ASSERT_EQ(EC_OK, NM_freeDeviceList(&dev_list));
}