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"
32 #include "easysetupcallbacks.h"
35 * @var ES_ENROLLEE_TAG
36 * @brief Logging tag for module name.
38 #define ES_ENROLLEE_TAG "ES"
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
46 * @brief Target SSID of the Soft Access point to which the device has to connect
48 static char gTargetSsid[MAXSSIDLEN];
52 * @brief Password of the target access point to which the device has to connect
54 static char gTargetPass[MAXNETCREDLEN];
57 * @var gEnrolleeStatusCb
58 * @brief Fucntion pointer holding the callback for intimation of EasySetup Enrollee status callback
60 static EventCallback gEnrolleeStatusCb = NULL;
64 * @brief Variable to check if secure mode is enabled or not.
66 static bool gIsSecured = false;
68 void OnboardingCallback(ESResult esResult)
70 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "OnboardingCallback with result = %d", esResult);
73 gEnrolleeStatusCb(esResult, ES_ON_BOARDED_STATE);
77 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG,
78 "Onboarding is failed callback result is = %d", esResult);
79 gEnrolleeStatusCb(esResult, ES_INIT_STATE);
83 void ProvisioningCallback(ESResult esResult)
85 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ProvisioningCallback with result = %d", esResult);
87 if (esResult == ES_RECVTRIGGEROFPROVRES)
89 GetTargetNetworkInfoFromProvResource(gTargetSsid, gTargetPass);
90 gEnrolleeStatusCb(ES_OK, ES_PROVISIONED_STATE);
91 OIC_LOG(DEBUG, ES_ENROLLEE_TAG, "Connecting with target network");
93 // Connecting/onboarding to target network
94 ConnectToWiFiNetwork(gTargetSsid, gTargetPass, OnboardingCallbackTargetNet);
98 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Provisioning is failed callback result is = %d", esResult);
99 // Resetting Enrollee to ONBOARDED_STATE as Enrollee is alreday onboarded in previous step
100 gEnrolleeStatusCb(ES_OK, ES_ON_BOARDED_STATE);
104 void OnboardingCallbackTargetNet(ESResult esResult)
106 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "OnboardingCallback on target network with result = %d",
108 if(esResult == ES_OK)
110 gEnrolleeStatusCb(esResult, ES_ON_BOARDED_TARGET_NETWORK_STATE);
114 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG,
115 "Onboarding is failed on target network and callback result is = %d", esResult);
116 // Resetting Enrollee state to the ES_PROVISIONED_STATE
117 // as device is already being provisioned with target network creds.
118 gEnrolleeStatusCb(esResult, ES_PROVISIONED_STATE);
122 ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid, const char *passwd,
126 OIC_LOG(INFO, ES_ENROLLEE_TAG, "InitEasySetup IN");
127 if(!ValidateParam(networkType,ssid,passwd,cb))
129 OIC_LOG(ERROR, ES_ENROLLEE_TAG,
130 "InitEasySetup::Stopping Easy setup due to invalid parameters");
135 gEnrolleeStatusCb = cb;
137 gIsSecured = isSecured;
139 // TODO : This onboarding state has to be set by lower layer, as they better
140 // knows when actually on-boarding started.
141 cb(ES_ERROR,ES_ON_BOARDING_STATE);
143 OIC_LOG(INFO, ES_ENROLLEE_TAG, "received callback");
144 OIC_LOG(INFO, ES_ENROLLEE_TAG, "onboarding now..");
146 if(!ESOnboard(ssid, passwd, OnboardingCallback))
148 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "InitEasySetup::On-boarding failed");
149 cb(ES_ERROR, ES_INIT_STATE);
153 OIC_LOG(INFO, ES_ENROLLEE_TAG, "InitEasySetup OUT");
157 ESResult TerminateEasySetup()
159 UnRegisterResourceEventCallBack();
161 //Delete Prov resource
162 if (DeleteProvisioningResource() != OC_STACK_OK)
164 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
168 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "TerminateEasySetup success");
172 ESResult InitProvisioning()
174 OIC_LOG(INFO, ES_ENROLLEE_TAG, "InitProvisioning <<IN>>");
176 if (CreateProvisioningResource(gIsSecured) != OC_STACK_OK)
178 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateProvisioningResource error");
182 RegisterResourceEventCallBack(ProvisioningCallback);
184 OIC_LOG(INFO, ES_ENROLLEE_TAG, "InitProvisioning OUT");
185 return ES_RESOURCECREATED;
188 static bool ValidateParam(OCConnectivityType networkType, const char *ssid, const char *passwd,
191 if (!ssid || !passwd || !cb)
193 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ValidateParam - Invalid parameters");