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 *targetSsid;
51 * @brief Password of the target access point to which the device has to connect
53 static char *targetPass;
56 * @var gEnrolleeStatusCb
57 * @brief Fucntion pointer holding the callback for intimation of EasySetup Enrollee status callback
59 static EventCallback gEnrolleeStatusCb = NULL;
61 //-----------------------------------------------------------------------------
62 // Private internal function prototypes
63 //-----------------------------------------------------------------------------
64 void OnboardingCallback(ESResult esResult);
65 void ProvisioningCallback(ESResult esResult);
66 void OnboardingCallbackTargetNet(ESResult esResult);
67 bool validateParam(OCConnectivityType networkType, const char *ssid, const char *passwd,
71 void OnboardingCallback(ESResult esResult)
73 OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "OnboardingCallback with result = %d", esResult);
76 gEnrolleeStatusCb(esResult, ES_ON_BOARDED_STATE);
80 OC_LOG_V(DEBUG, ES_ENROLLEE_TAG,
81 "Onboarding is failed callback result is = %d", esResult);
82 gEnrolleeStatusCb(esResult, ES_INIT_STATE);
86 void ProvisioningCallback(ESResult esResult)
88 OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ProvisioningCallback with result = %d", esResult);
90 if (esResult == ES_RECVTRIGGEROFPROVRES)
92 targetSsid = (char *) malloc(MAXSSIDLEN);
93 targetPass = (char *) malloc(MAXNETCREDLEN);
95 GetTargetNetworkInfoFromProvResource(targetSsid, targetPass);
96 gEnrolleeStatusCb(ES_OK, ES_PROVISIONED_STATE);
97 OC_LOG(DEBUG, ES_ENROLLEE_TAG, "Connecting with target network");
99 // Connecting/onboarding to target network
100 ConnectToWiFiNetwork(targetSsid, targetPass, OnboardingCallbackTargetNet);
104 OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Provisioning is failed callback result is = %d", esResult);
105 // Resetting Enrollee to ONBOARDED_STATE as Enrollee is alreday onboarded in previous step
106 gEnrolleeStatusCb(ES_OK, ES_ON_BOARDED_STATE);
110 void OnboardingCallbackTargetNet(ESResult esResult)
112 OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "OnboardingCallback on target network with result = %d",
114 if(esResult == ES_OK)
116 gEnrolleeStatusCb(esResult, ES_ON_BOARDED_TARGET_NETWORK_STATE);
120 OC_LOG_V(DEBUG, ES_ENROLLEE_TAG,
121 "Onboarding is failed on target network and callback result is = %d", esResult);
122 // Resetting Enrollee state to the ES_PROVISIONED_STATE
123 // as device is already being provisioned with target network creds.
124 gEnrolleeStatusCb(esResult, ES_PROVISIONED_STATE);
128 static FILE* server_fopen(const char* /*path*/, const char *mode)
130 OC_LOG_V(INFO,ES_ENROLLEE_TAG,"oic_svr_db_server open %s",mode);
131 FILE *file= fopen("/opt/usr/media/Images/oic_svr_db_server.json", mode);
134 OC_LOG(ERROR,ES_ENROLLEE_TAG,"oic_svr_db_server failed");
139 ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid,
140 const char *passwd, EventCallback cb)
142 OC_LOG(INFO, ES_ENROLLEE_TAG, "InitEasySetup IN");
143 if(!validateParam(networkType,ssid,passwd,cb))
145 OC_LOG(ERROR, ES_ENROLLEE_TAG,
146 "InitEasySetup::Stopping Easy setup due to invalid parameters");
151 gEnrolleeStatusCb = cb;
153 // TODO : This onboarding state has to be set by lower layer, as they better
154 // knows when actually on-boarding started.
155 cb(ES_ERROR,ES_ON_BOARDING_STATE);
157 OC_LOG(INFO, ES_ENROLLEE_TAG, "received callback");
158 OC_LOG(INFO, ES_ENROLLEE_TAG, "onboarding now..");
160 if(!ESOnboard(ssid, passwd, OnboardingCallback))
162 OC_LOG(ERROR, ES_ENROLLEE_TAG, "InitEasySetup::On-boarding failed");
163 cb(ES_ERROR, ES_INIT_STATE);
167 OC_LOG(INFO, ES_ENROLLEE_TAG, "InitEasySetup OUT");
171 ESResult TerminateEasySetup()
173 if(OCStop() != OC_STACK_OK)
175 OC_LOG(ERROR, ES_ENROLLEE_TAG, "OCStack stop failed");
180 OC_LOG(ERROR, ES_ENROLLEE_TAG, "OCStack stop success");
185 ESResult InitProvisioning()
187 OC_LOG(INFO, ES_ENROLLEE_TAG, "InitProvisioning <<IN>>");
189 // Initialize the OC Stack in Server mode
190 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
192 OC_LOG(ERROR, ES_ENROLLEE_TAG, "OCStack init error");
196 if (CreateProvisioningResource() != OC_STACK_OK)
198 OC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateProvisioningResource error");
202 if (CreateNetworkResource() != OC_STACK_OK)
204 OC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateNetworkResource error");
208 RegisterResourceEventCallBack(ProvisioningCallback);
210 OC_LOG(INFO, ES_ENROLLEE_TAG, "InitProvisioning OUT");
211 return ES_RESOURCECREATED;
214 bool validateParam(OCConnectivityType networkType, const char *ssid, const char *passwd,
217 if (!ssid || !passwd || !cb)
219 OC_LOG(ERROR, ES_ENROLLEE_TAG, "validateParam - Invalid parameters");