1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 * This file contains the implementation for EasySetup Enrollee device
27 #include "easysetup.h"
29 #include "onboarding.h"
31 #include "resourcehandler.h"
34 * @var ES_ENROLLEE_TAG
35 * @brief Logging tag for module name.
37 #define ES_ENROLLEE_TAG "ES"
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
45 * @brief Target SSID of the Soft Access point to which the device has to connect
47 static char gTargetSsid[MAXSSIDLEN];
51 * @brief Password of the target access point to which the device has to connect
53 static char gTargetPass[MAXNETCREDLEN];
56 * @var gEnrolleeStatusCb
57 * @brief Fucntion pointer holding the callback for intimation of EasySetup Enrollee status callback
59 static EventCallback gEnrolleeStatusCb = NULL;
63 * @brief Variable to check if secure mode is enabled or not.
65 static bool gIsSecured = false;
67 //-----------------------------------------------------------------------------
68 // Private internal function prototypes
69 //-----------------------------------------------------------------------------
70 void OnboardingCallback(ESResult esResult);
71 void ProvisioningCallback(ESResult esResult);
72 void OnboardingCallbackTargetNet(ESResult esResult);
73 static bool ValidateParam(OCConnectivityType networkType, const char *ssid, const char *passwd,
77 void OnboardingCallback(ESResult esResult)
79 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "OnboardingCallback with result = %d", esResult);
82 gEnrolleeStatusCb(esResult, ES_ON_BOARDED_STATE);
86 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG,
87 "Onboarding is failed callback result is = %d", esResult);
88 gEnrolleeStatusCb(esResult, ES_INIT_STATE);
92 void ProvisioningCallback(ESResult esResult)
94 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ProvisioningCallback with result = %d", esResult);
96 if (esResult == ES_RECVTRIGGEROFPROVRES)
98 GetTargetNetworkInfoFromProvResource(gTargetSsid, gTargetPass);
99 gEnrolleeStatusCb(ES_OK, ES_PROVISIONED_STATE);
100 OIC_LOG(DEBUG, ES_ENROLLEE_TAG, "Connecting with target network");
102 // Connecting/onboarding to target network
103 ConnectToWiFiNetwork(gTargetSsid, gTargetPass, OnboardingCallbackTargetNet);
107 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Provisioning is failed callback result is = %d", esResult);
108 // Resetting Enrollee to ONBOARDED_STATE as Enrollee is alreday onboarded in previous step
109 gEnrolleeStatusCb(ES_OK, ES_ON_BOARDED_STATE);
113 void OnboardingCallbackTargetNet(ESResult esResult)
115 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "OnboardingCallback on target network with result = %d",
117 if(esResult == ES_OK)
119 gEnrolleeStatusCb(esResult, ES_ON_BOARDED_TARGET_NETWORK_STATE);
123 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG,
124 "Onboarding is failed on target network and callback result is = %d", esResult);
125 // Resetting Enrollee state to the ES_PROVISIONED_STATE
126 // as device is already being provisioned with target network creds.
127 gEnrolleeStatusCb(esResult, ES_PROVISIONED_STATE);
131 ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid, const char *passwd,
135 OIC_LOG(INFO, ES_ENROLLEE_TAG, "InitEasySetup IN");
136 if(!ValidateParam(networkType,ssid,passwd,cb))
138 OIC_LOG(ERROR, ES_ENROLLEE_TAG,
139 "InitEasySetup::Stopping Easy setup due to invalid parameters");
144 gEnrolleeStatusCb = cb;
146 gIsSecured = isSecured;
148 // TODO : This onboarding state has to be set by lower layer, as they better
149 // knows when actually on-boarding started.
150 cb(ES_ERROR,ES_ON_BOARDING_STATE);
152 OIC_LOG(INFO, ES_ENROLLEE_TAG, "received callback");
153 OIC_LOG(INFO, ES_ENROLLEE_TAG, "onboarding now..");
155 if(!ESOnboard(ssid, passwd, OnboardingCallback))
157 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "InitEasySetup::On-boarding failed");
158 cb(ES_ERROR, ES_INIT_STATE);
162 OIC_LOG(INFO, ES_ENROLLEE_TAG, "InitEasySetup OUT");
166 ESResult TerminateEasySetup()
168 UnRegisterResourceEventCallBack();
170 //Delete Prov resource
171 if (DeleteProvisioningResource() != OC_STACK_OK)
173 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
177 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "TerminateEasySetup success");
181 ESResult InitProvisioning()
183 OIC_LOG(INFO, ES_ENROLLEE_TAG, "InitProvisioning <<IN>>");
185 if (CreateProvisioningResource(gIsSecured) != OC_STACK_OK)
187 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateProvisioningResource error");
191 RegisterResourceEventCallBack(ProvisioningCallback);
193 OIC_LOG(INFO, ES_ENROLLEE_TAG, "InitProvisioning OUT");
194 return ES_RESOURCECREATED;
197 static bool ValidateParam(OCConnectivityType /*networkType*/, const char *ssid, const char *passwd,
200 if (!ssid || !passwd || !cb)
202 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ValidateParam - Invalid parameters");