--- /dev/null
+#ifndef __IOT_EASY_SETUP_H__
+#define __IOT_EASY_SETUP_H__
+
+#include <stdlib.h>
+
+#include "easysetup.h"
+
+/*******************************************************/
+/*******************************************************/
+typedef struct
+{
+ int unused;
+} IOT_EasySetup_WiFiInfo;
+
+typedef struct
+{
+ int unused;
+} IOT_EasySetup_ConfInfo;
+
+typedef struct
+{
+ char host[OIC_STRING_MAX_VALUE];
+ char auth_provider[OIC_STRING_MAX_VALUE];
+ char auth_code[OIC_STRING_MAX_VALUE];
+} IOT_EasySetup_CloudInfo;
+
+typedef struct
+{
+ IOT_EasySetup_WiFiInfo wifi_info;
+ IOT_EasySetup_ConfInfo conf_info;
+ IOT_EasySetup_CloudInfo cloud_info;
+} IOT_EasySetup_Info;
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*******************************************************/
+/*******************************************************/
+extern bool IOT_EasySetup(IOT_EasySetup_Info* _info);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __IOT_EASY_SETUP_H__ */
--- /dev/null
+#ifndef __IOT_ENROLLEE_H__
+#define __IOT_ENROLLEE_H__
+
+#include <memory>
+#include <string>
+
+#include "OCPlatform.h"
+#include "OCApi.h"
+#include "OCAccountManager.h"
+
+/*******************************************************/
+/*******************************************************/
+class IOT_Enrollee final
+{
+public:
+ static IOT_Enrollee Create(const OC::PlatformConfig& _platform_config, OCConnectivityType _conn_type);
+
+ IOT_Enrollee( const OC::PlatformConfig& _platform_config,
+ OCConnectivityType _conn_type,
+ const std::string& _host,
+ const std::string& _auth_provider,
+ const std::string& _auth_code );
+ IOT_Enrollee(const IOT_Enrollee& _obj);
+
+ ~IOT_Enrollee();
+
+ IOT_Enrollee& operator=(const IOT_Enrollee& _obj);
+
+private:
+ std::string m_host;
+ std::string m_auth_provider;
+ std::string m_auth_code;
+
+ std::shared_ptr<OC::OCAccountManager> m_account_manager;
+
+ std::string m_uid;
+ std::string m_access_token;
+};
+
+#endif /* __IOT_ENROLLEE_H__ */
--- /dev/null
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "easysetup.h"
+
+#include "IOT_EasySetup.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*******************************************************/
+/*******************************************************/
+bool IOT_EasySetup(IOT_EasySetup_Info* _info)
+{
+ volatile bool running = true;
+
+ if(OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
+ return false;
+
+ void cb_wifi_prov(ESWiFiProvData* _data)
+ {
+ (void)(_data);
+ }
+ void cb_conf_prov(ESDevConfProvData* _data)
+ {
+ (void)(_data);
+ }
+ void cb_cloud_prov(ESCloudProvData* _data)
+ {
+ strcpy(_info->cloud_info.host, _data->ciServer);
+ strcpy(_info->cloud_info.auth_provider, _data->authProvider);
+ strcpy(_info->cloud_info.auth_code, _data->authCode);
+ running = false;
+ }
+
+ ESResourceMask mask = ES_WIFI_RESOURCE|ES_CLOUD_RESOURCE|ES_DEVCONF_RESOURCE;
+ ESProvisioningCallbacks callbacks =
+ {
+ .WiFiProvCb = &cb_wifi_prov,
+ .DevConfProvCb = &cb_conf_prov,
+ .CloudDataProvCb = &cb_cloud_prov,
+ };
+ if(ESInitEnrollee(false, mask, callbacks) != ES_OK)
+ {
+ OCStop();
+ return false;
+ }
+
+ while(running && (OCProcess() == OC_STACK_OK))
+ ;
+
+ ESTerminateEnrollee();
+ OCStop();
+
+ return !running;
+}
+
+#ifdef __cplusplus
+}
+#endif
--- /dev/null
+#include <iostream>
+#include <memory>
+#include <string>
+#include <mutex>
+#include <condition_variable>
+#include <exception>
+#include <stdexcept>
+
+#include "OCPlatform.h"
+#include "OCApi.h"
+
+#include "IOT_EasySetup.h"
+
+#include "IOT_Enrollee.h"
+
+/*******************************************************/
+/*******************************************************/
+/*static*/ IOT_Enrollee IOT_Enrollee::Create(const OC::PlatformConfig& _platform_config, OCConnectivityType _conn_type)
+{
+ IOT_EasySetup_Info info;
+
+ if(!IOT_EasySetup(&info))
+ throw std::runtime_error("IOT_EasySetup failed");
+
+ return IOT_Enrollee(_platform_config, _conn_type, info.cloud_info.host, info.cloud_info.auth_provider, info.cloud_info.auth_code);
+}
+
+/*******************************************************/
+/*******************************************************/
+IOT_Enrollee::IOT_Enrollee( const OC::PlatformConfig& _platform_config,
+ OCConnectivityType _conn_type,
+ const std::string& _host,
+ const std::string& _auth_provider,
+ const std::string& _auth_code ) :
+ m_host(_host), m_auth_provider(_auth_provider), m_auth_code(_auth_code), m_account_manager(nullptr), m_uid(""), m_access_token("")
+{
+ OC::OCPlatform::Configure(_platform_config);
+ m_account_manager = OC::OCPlatform::constructAccountManagerObject(m_host, _conn_type);
+
+ std::mutex mtx;
+ std::unique_lock<std::mutex> lck(mtx);
+ std::condition_variable cvar;
+
+ m_account_manager->signUp( m_auth_provider,
+ m_auth_code,
+ [&](const OC::HeaderOptions& /*_hopt*/, const OC::OCRepresentation& _rep, const int _ecode)
+ {
+ if(_ecode == 4)
+ {
+ m_uid = _rep.getValueToString("uid");
+ m_access_token = _rep.getValueToString("accesstoken");
+ }
+ cvar.notify_all();
+ } );
+ cvar.wait(lck);
+
+ m_account_manager->signIn( m_uid,
+ m_access_token,
+ [&](const OC::HeaderOptions& /*_hopt*/, const OC::OCRepresentation& /*_rep*/, const int /*_ecode*/)
+ {
+ cvar.notify_all();
+ } );
+ cvar.wait(lck);
+}
+IOT_Enrollee::IOT_Enrollee(const IOT_Enrollee& _obj) :
+ m_host(_obj.m_host), m_auth_provider(_obj.m_auth_provider), m_auth_code(_obj.m_auth_code), m_account_manager(_obj.m_account_manager), m_uid(_obj.m_uid), m_access_token(_obj.m_access_token)
+{
+}
+
+/*******************************************************/
+/*******************************************************/
+IOT_Enrollee::~IOT_Enrollee()
+{
+}
+
+/*******************************************************/
+/*******************************************************/
+IOT_Enrollee& IOT_Enrollee::operator=(const IOT_Enrollee& _obj)
+{
+ if(this != &_obj)
+ {
+ m_host = _obj.m_host;
+ m_auth_provider = _obj.m_auth_provider;
+ m_auth_code = _obj.m_auth_code;
+ m_account_manager = _obj.m_account_manager;
+ m_uid = _obj.m_uid;
+ m_access_token = _obj.m_access_token;
+ }
+ return *this;
+}