* This function Initializes the EasySetup. This API must be called prior to invoking any other API
*
* @param networkType NetworkType on which OnBoarding has to be performed.
- * @param ssid SSID of the target SoftAP network to which the Enrollee is connecting.
- * @param passwd Password of the target SoftAP network to which the Enrollee is connecting
- * @param eventCallback EventCallback for for updating the Enrollee OnBoarding and Provisioning status
- * result to the application
+ * @param ssid SSID of the target SoftAP network to which the Enrollee is connecting.
+ * @param passwd Password of the target SoftAP network to which the Enrollee is connecting
+ * @param isSecured True if the Enrollee is operating in secured mode.
+ * @param eventCallback EventCallback for for updating the Enrollee OnBoarding status result to
+ * the application
* @return ::ES_OK on success, some other value upon failure.
*/
-ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid,
- const char *passwd,
- EventCallback eventCallback);
+ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid, const char *passwd,
+ bool isSecured,
+ EventCallback eventCallback);
/**
- * This function performs initialization of Provisioning and Network resources needed for EasySetup process.
- *
+ * This function performs initialization of Provisioning and Network resources needed for EasySetup
+ * process.
* @return ::ES_OK on success, some other value upon failure.
*/
ESResult InitProvisioning();
typedef struct PROVRESOURCE
{
OCResourceHandle handle;
- int ps; // provisiong status, 1 : need to provisioning, 2 : Connected to Internet
+ int ps; // provisiong status, 1 : need to provisioning, 2 : Connected to Enroller
int tnt; // target network type, 1: WLAN, 2: BT, 3: BLE, 4: Zigbee, ...
char tnn[MAXSSIDLEN]; // target network name, i.e. SSID for WLAN, MAC address for BT
char cd[MAXNETCREDLEN]; // credential information
char cnn[MAXSSIDLEN]; // current network name
} NetResource;
-OCStackResult CreateProvisioningResource();
+OCStackResult CreateProvisioningResource(bool isSecured);
OCStackResult DeleteProvisioningResource();
OCStackResult DeleteNetworkResource();
//-----------------------------------------------------------------------------
/**
- * @var targetSsid
+ * @var gTargetSsid
* @brief Target SSID of the Soft Access point to which the device has to connect
*/
-static char *targetSsid;
+static char gTargetSsid[MAXSSIDLEN];
/**
- * @var targetPass
+ * @var gTargetPass
* @brief Password of the target access point to which the device has to connect
*/
-static char *targetPass;
+static char gTargetPass[MAXNETCREDLEN];
/**
* @var gEnrolleeStatusCb
*/
static EventCallback gEnrolleeStatusCb = NULL;
+/**
+ * @var gIsSecured
+ * @brief Variable to check if secure mode is enabled or not.
+ */
+static bool gIsSecured = false;
+
//-----------------------------------------------------------------------------
// Private internal function prototypes
//-----------------------------------------------------------------------------
void OnboardingCallback(ESResult esResult);
void ProvisioningCallback(ESResult esResult);
void OnboardingCallbackTargetNet(ESResult esResult);
-bool validateParam(OCConnectivityType networkType, const char *ssid, const char *passwd,
+static bool ValidateParam(OCConnectivityType networkType, const char *ssid, const char *passwd,
EventCallback cb);
void ProvisioningCallback(ESResult esResult)
{
OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ProvisioningCallback with result = %d", esResult);
- ESResult res = ES_OK;
+
if (esResult == ES_RECVTRIGGEROFPROVRES)
{
- targetSsid = (char *) malloc(MAXSSIDLEN);
- targetPass = (char *) malloc(MAXNETCREDLEN);
-
- GetTargetNetworkInfoFromProvResource(targetSsid, targetPass);
+ GetTargetNetworkInfoFromProvResource(gTargetSsid, gTargetPass);
gEnrolleeStatusCb(ES_OK, ES_PROVISIONED_STATE);
OC_LOG(DEBUG, ES_ENROLLEE_TAG, "Connecting with target network");
// Connecting/onboarding to target network
- ConnectToWiFiNetwork(targetSsid, targetPass, OnboardingCallbackTargetNet);
+ ConnectToWiFiNetwork(gTargetSsid, gTargetPass, OnboardingCallbackTargetNet);
}
else
{
}
}
-static FILE* server_fopen(const char* /*path*/, const char *mode)
-{
- OC_LOG_V(INFO,ES_ENROLLEE_TAG,"oic_svr_db_server open %s",mode);
- FILE *file= fopen("/opt/usr/media/Images/oic_svr_db_server.json", mode);
- if(file==NULL)
- {
- OC_LOG(ERROR,ES_ENROLLEE_TAG,"oic_svr_db_server failed");
- }
- return file;
-}
-
-ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid,
- const char *passwd, EventCallback cb)
+ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid, const char *passwd,
+ bool isSecured,
+ EventCallback cb)
{
OC_LOG(INFO, ES_ENROLLEE_TAG, "InitEasySetup IN");
- if(!validateParam(networkType,ssid,passwd,cb))
+ if(!ValidateParam(networkType,ssid,passwd,cb))
{
OC_LOG(ERROR, ES_ENROLLEE_TAG,
"InitEasySetup::Stopping Easy setup due to invalid parameters");
//Init callback
gEnrolleeStatusCb = cb;
+ gIsSecured = isSecured;
+
// TODO : This onboarding state has to be set by lower layer, as they better
// knows when actually on-boarding started.
cb(ES_ERROR,ES_ON_BOARDING_STATE);
{
OC_LOG(INFO, ES_ENROLLEE_TAG, "InitProvisioning <<IN>>");
- if (CreateProvisioningResource() != OC_STACK_OK)
+ if (CreateProvisioningResource(gIsSecured) != OC_STACK_OK)
{
OC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateProvisioningResource error");
return ES_ERROR;
return ES_RESOURCECREATED;
}
-bool validateParam(OCConnectivityType networkType, const char *ssid, const char *passwd,
+static bool ValidateParam(OCConnectivityType networkType, const char *ssid, const char *passwd,
EventCallback cb)
{
if (!ssid || !passwd || !cb)
{
- OC_LOG(ERROR, ES_ENROLLEE_TAG, "validateParam - Invalid parameters");
+ OC_LOG(ERROR, ES_ENROLLEE_TAG, "ValidateParam - Invalid parameters");
return false;
}
return true;
* @brief Logging tag for module name.
*/
#define ES_RH_TAG "ES_RH"
-
//-----------------------------------------------------------------------------
// Private variables
//-----------------------------------------------------------------------------
}
}
-OCStackResult CreateProvisioningResource()
+OCStackResult CreateProvisioningResource(bool isSecured)
{
- gProvResource.ps = 1; // need to do provisioning
+ gProvResource.ps = ES_PS_NEED_PROVISIONING;
+
gProvResource.tnt = CT_ADAPTER_IP;
sprintf(gProvResource.tnn, "Unknown");
sprintf(gProvResource.cd, "Unknown");
- OCStackResult res = OCCreateResource(&gProvResource.handle, "oic.r.prov", OC_RSRVD_INTERFACE_DEFAULT,
- OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb, NULL,
- OC_DISCOVERABLE | OC_OBSERVABLE);
+ OCStackResult res = OC_STACK_ERROR;
+ if (isSecured)
+ {
+ res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_PROV_RES_TYPE,
+ OC_RSRVD_INTERFACE_DEFAULT,
+ OC_RSRVD_ES_URI_PROV,
+ OCEntityHandlerCb,
+ NULL,
+ OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
+ }
+ else
+ {
+ res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_PROV_RES_TYPE,
+ OC_RSRVD_INTERFACE_DEFAULT,
+ OC_RSRVD_ES_URI_PROV,
+ OCEntityHandlerCb,
+ NULL,
+ OC_DISCOVERABLE | OC_OBSERVABLE);
+ }
+
OC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
return res;
}
#define OC_RSRVD_ES_TR "tr"
#define OC_RSRVD_ES_TNT "tnt"
#define OC_RSRVD_ES_ANT "ant"
+
+/**
+ * Easysetup defined resoruce types and uris.
+ */
+#define OC_RSRVD_ES_PROV_RES_TYPE "oic.r.prov"
#define OC_RSRVD_ES_URI_PROV "/oic/prov"
#define OC_RSRVD_ES_URI_NET "/oic/net"
enrollee_env.PrependUnique(LIBS = ['oc', 'octbstack', 'oc_logger', 'ocsrm', 'pthread', 'connectivity_abstraction','coap', 'ESEnrolleeSDK'])
-if env.get('SECURED') == '1':
- enrollee = enrollee_env.Program('enrollee', 'enrolleewifisecured.cpp')
-else :
- enrollee = enrollee_env.Program('enrollee', 'enrolleewifi.cpp')
+enrollee = enrollee_env.Program('enrollee', 'enrolleewifi.cpp')
i_enrollee = enrollee_env.Install(env.get('BUILD_DIR'), enrollee)
#include "easysetup.h"
+#include <unistd.h>
#include <string.h>
#include <iostream>
#include <pthread.h>
*/
static char passwd[] = "EasySetup123";
+/**
+ * Secure Virtual Resource database for Iotivity Server
+ * It contains Server's Identity and the PSK credentials
+ * of other devices which the server trusts
+ */
+static char CRED_FILE[] = "oic_svr_db_server.json";
+
+OCPersistentStorage ps ;
+
+
+/**
+ * @var gIsSecured
+ * @brief Variable to check if secure mode is enabled or not.
+ */
+static bool gIsSecured = false;
+
void PrintMenu()
{
cout<<"============"<<endl;
- cout<<"S: start easy setup"<<endl;
+ cout<<"S: Enabled Security"<<endl;
+ cout<<"I: Init easy setup"<<endl;
cout<<"P: start provisioning resources"<<endl;
cout<<"T: terminate"<<endl;
cout<<"Q: quit"<<endl;
PrintMenu();
}
+FILE* server_fopen(const char *path, const char *mode)
+{
+ (void) path;
+ return fopen(CRED_FILE, mode);
+}
+
+void EnableSecurity()
+{
+ cout << "Inside EnableSecurity API.." << endl;
+
+ gIsSecured = true;
+
+ // Initialize Persistent Storage for SVR database
+ ps = { server_fopen, fread, fwrite, fclose, unlink };
+ OCRegisterPersistentStorageHandler(&ps);
+}
void StartEasySetup()
{
cout<<"StartEasySetup and onboarding started.."<<endl;
- if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, EventCallbackInApp) == ES_ERROR)
+ if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, gIsSecured, EventCallbackInApp) == ES_ERROR)
{
cout<<"StartEasySetup and onboarding Fail!!"<<endl;
return;
}
+}
+
+void StartOICStackAndStartResources()
+{
+ cout<<"Starting Enrollee Provisioning"<<endl;
+
+ // Initialize the OC Stack in Server mode
+ if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
+ {
+ cout<<"OCStack init error!!"<<endl;
+ return;
+ }
+
+ if (InitProvisioning() == ES_ERROR)
+ {
+ cout<<"Init Provisioning Failed!!"<<endl;
+ return;
+ }
pthread_t thread_handle;
if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
{
cout<<"Thread creation failed"<<endl;
}
+
+ cout<<"InitProvisioning Success"<<endl;
}
-void StartProvisioning()
+void StopEasySetup()
{
- cout<<"Starting Enrollee Provisioning"<<endl;
+ cout<<"StopEasySetup IN"<<endl;
- if(InitProvisioning()== ES_ERROR)
+ if (TerminateEasySetup() == ES_ERROR)
{
- cout<<"Init Provisioning Failed"<<endl;
+ cout<<"TerminateEasySetup Failed!!"<<endl;
return;
}
- cout<<"InitProvisioning:Success"<<endl;
-}
-void StopEasySetup()
-{
- cout<<"StopEasySetup IN"<<endl;
- if(TerminateEasySetup()== ES_ERROR)
+ //stop OC Stack
+ if (OCStop() != OC_STACK_OK)
{
- cout<<"return value is: ES_ERROR"<<endl;
+ cout<<"OCStack stop failed!!"<<endl;
return;
}
+
cout<<"StopEasySetup OUT"<<endl;
}
{
cout<<"#########################"<<endl;
cout<<"EasySetup Enrollee SAMPLE"<<endl;
- cout<<"This is modified sample:1"<<endl;
cout<<"#########################"<<endl;
PrintMenu();
char option;
- // Initialize the OC Stack in Server mode
- if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
- {
- return -1;
- }
-
while(true)
{
cin>>option;
cout<<"quit";
break;
- case 'S': // start easy setup
+ case 'S': // Enable Security
case 's':
+ EnableSecurity();
+ break;
+
+ case 'I': // Init EasySetup
+ case 'i':
StartEasySetup();
break;
case 'P': // start provisioning
case 'p':
- StartProvisioning();
+ StartOICStackAndStartResources();
break;
case 'T': // stop easy setup
cout<<"wrong option"<<endl;
break;
}
- if(option=='Q') break;
+ if (option == 'Q' || option == 'q') break;
}
return 0;
}
*/
static char passwd[] = "EasySetup123";
+/**
+ * Secure Virtual Resource database for Iotivity Server
+ * It contains Server's Identity and the PSK credentials
+ * of other devices which the server trusts
+ */
+static char CRED_FILE[] = "oic_svr_db_server.json";
+
+/**
+ * @var gIsSecured
+ * @brief Variable to check if secure mode is enabled or not.
+ */
+static bool gIsSecured = false;
+
void PrintMenu()
{
cout<<"============"<<endl;
- cout<<"S: start easy setup"<<endl;
+ cout<<"I: Init easy setup"<<endl;
+ cout<<"S: Enabled Security"<<endl;
cout<<"P: start provisioning resources"<<endl;
cout<<"T: terminate"<<endl;
cout<<"Q: quit"<<endl;
PrintMenu();
}
+FILE* server_fopen(const char *path, const char *mode)
+{
+ (void) path;
+ return fopen(CRED_FILE, mode);
+}
+
+void EnableSecurity()
+{
+ cout << "Inside EnableSecurity API.." << endl;
+
+ gIsSecured = true;
+
+ // Initialize Persistent Storage for SVR database
+ OCPersistentStorage ps = { server_fopen, fread, fwrite, fclose, unlink };
+ OCRegisterPersistentStorageHandler(&ps);
+}
void StartEasySetup()
{
cout<<"StartEasySetup and onboarding started.."<<endl;
- if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, EventCallbackInApp) == ES_ERROR)
+ if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, gIsSecured, EventCallbackInApp) == ES_ERROR)
{
cout<<"StartEasySetup and onboarding Fail!!"<<endl;
return;
}
}
-void StartProvisioning()
+void StartOICStackAndStartResources()
{
cout<<"Starting Enrollee Provisioning"<<endl;
cout<<"quit";
break;
- case 'S': // start easy setup
- case 's':
+ case 'I': // Init EasySetup
+ case 'i':
StartEasySetup();
break;
+ case 'S': // Enable Security
+ case 's':
+ EnableSecurity();
+ break;
+
case 'P': // start provisioning
case 'p':
- StartProvisioning();
+ StartOICStackAndStartResources();
break;
case 'T': // stop easy setup