From e11d7991ba89c05ae25739f2c494e3876659284d Mon Sep 17 00:00:00 2001 From: lankamadan Date: Thu, 18 Feb 2016 19:28:06 +0900 Subject: [PATCH] Easysetup - Enable Justworks security option in Linux Enrollee - Enabled Justworks security option in Linux Enrollee Change-Id: I8ed43278e27aad19b68dc9dc65bddf671df8fe9f Signed-off-by: lankamadan Reviewed-on: https://gerrit.iotivity.org/gerrit/5041 Tested-by: jenkins-iotivity --- service/easy-setup/enrollee/inc/easysetup.h | 19 +++-- service/easy-setup/enrollee/inc/resourcehandler.h | 4 +- service/easy-setup/enrollee/src/easysetup.cpp | 51 ++++++------ .../easy-setup/enrollee/src/resourcehandler.cpp | 29 +++++-- service/easy-setup/inc/escommon.h | 5 ++ .../easy-setup/sampleapp/enrollee/linux/SConscript | 5 +- .../sampleapp/enrollee/linux/enrolleewifi.cpp | 95 ++++++++++++++++------ .../tizen-sdb/EnrolleeSample/enrolleewifi.cpp | 47 +++++++++-- 8 files changed, 177 insertions(+), 78 deletions(-) mode change 100644 => 100755 service/easy-setup/enrollee/inc/easysetup.h mode change 100644 => 100755 service/easy-setup/enrollee/inc/resourcehandler.h mode change 100644 => 100755 service/easy-setup/enrollee/src/easysetup.cpp mode change 100644 => 100755 service/easy-setup/enrollee/src/resourcehandler.cpp mode change 100644 => 100755 service/easy-setup/inc/escommon.h mode change 100644 => 100755 service/easy-setup/sampleapp/enrollee/linux/enrolleewifi.cpp mode change 100644 => 100755 service/easy-setup/sampleapp/enrollee/tizen-sdb/EnrolleeSample/enrolleewifi.cpp diff --git a/service/easy-setup/enrollee/inc/easysetup.h b/service/easy-setup/enrollee/inc/easysetup.h old mode 100644 new mode 100755 index 12c6324..7de994f --- a/service/easy-setup/enrollee/inc/easysetup.h +++ b/service/easy-setup/enrollee/inc/easysetup.h @@ -44,19 +44,20 @@ typedef void (*EventCallback)(ESResult esResult, EnrolleeState enrolleeState); * 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(); diff --git a/service/easy-setup/enrollee/inc/resourcehandler.h b/service/easy-setup/enrollee/inc/resourcehandler.h old mode 100644 new mode 100755 index 8fafb7e..51938f1 --- a/service/easy-setup/enrollee/inc/resourcehandler.h +++ b/service/easy-setup/enrollee/inc/resourcehandler.h @@ -36,7 +36,7 @@ typedef void (*ResourceEventCallback)(ESResult); 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 @@ -52,7 +52,7 @@ typedef struct NETRESOURCE char cnn[MAXSSIDLEN]; // current network name } NetResource; -OCStackResult CreateProvisioningResource(); +OCStackResult CreateProvisioningResource(bool isSecured); OCStackResult DeleteProvisioningResource(); OCStackResult DeleteNetworkResource(); diff --git a/service/easy-setup/enrollee/src/easysetup.cpp b/service/easy-setup/enrollee/src/easysetup.cpp old mode 100644 new mode 100755 index 64f3d6c..4d55ff8 --- a/service/easy-setup/enrollee/src/easysetup.cpp +++ b/service/easy-setup/enrollee/src/easysetup.cpp @@ -41,16 +41,16 @@ //----------------------------------------------------------------------------- /** - * @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 @@ -58,13 +58,19 @@ static char *targetPass; */ 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); @@ -86,18 +92,15 @@ void OnboardingCallback(ESResult esResult) 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 { @@ -125,22 +128,12 @@ void OnboardingCallbackTargetNet(ESResult esResult) } } -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"); @@ -150,6 +143,8 @@ ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid, //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); @@ -196,7 +191,7 @@ ESResult InitProvisioning() { OC_LOG(INFO, ES_ENROLLEE_TAG, "InitProvisioning <>"); - if (CreateProvisioningResource() != OC_STACK_OK) + if (CreateProvisioningResource(gIsSecured) != OC_STACK_OK) { OC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateProvisioningResource error"); return ES_ERROR; @@ -216,12 +211,12 @@ ESResult InitProvisioning() 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; diff --git a/service/easy-setup/enrollee/src/resourcehandler.cpp b/service/easy-setup/enrollee/src/resourcehandler.cpp old mode 100644 new mode 100755 index 905b5ae..828e5cb --- a/service/easy-setup/enrollee/src/resourcehandler.cpp +++ b/service/easy-setup/enrollee/src/resourcehandler.cpp @@ -27,7 +27,6 @@ * @brief Logging tag for module name. */ #define ES_RH_TAG "ES_RH" - //----------------------------------------------------------------------------- // Private variables //----------------------------------------------------------------------------- @@ -84,16 +83,34 @@ void GetTargetNetworkInfoFromProvResource(char *name, char *pass) } } -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; } diff --git a/service/easy-setup/inc/escommon.h b/service/easy-setup/inc/escommon.h old mode 100644 new mode 100755 index 6ee898e..1798ade --- a/service/easy-setup/inc/escommon.h +++ b/service/easy-setup/inc/escommon.h @@ -53,6 +53,11 @@ using namespace std; #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" diff --git a/service/easy-setup/sampleapp/enrollee/linux/SConscript b/service/easy-setup/sampleapp/enrollee/linux/SConscript index 683854b..03d77fb 100644 --- a/service/easy-setup/sampleapp/enrollee/linux/SConscript +++ b/service/easy-setup/sampleapp/enrollee/linux/SConscript @@ -57,10 +57,7 @@ enrollee_env.PrependUnique(CPPPATH = [ 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) diff --git a/service/easy-setup/sampleapp/enrollee/linux/enrolleewifi.cpp b/service/easy-setup/sampleapp/enrollee/linux/enrolleewifi.cpp old mode 100644 new mode 100755 index 4c15e22..fac4c5d --- a/service/easy-setup/sampleapp/enrollee/linux/enrolleewifi.cpp +++ b/service/easy-setup/sampleapp/enrollee/linux/enrolleewifi.cpp @@ -21,6 +21,7 @@ #include "easysetup.h" +#include #include #include #include @@ -43,10 +44,27 @@ static char ssid[] = "EasySetup123"; */ 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<<"============"<>option; @@ -155,14 +199,19 @@ int main() 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 @@ -174,7 +223,7 @@ int main() cout<<"wrong option"<