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"
30 #include "resourceHandler.h"
33 * @var ES_ENROLLEE_TAG
34 * @brief Logging tag for module name.
36 #define ES_ENROLLEE_TAG "ES"
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
44 * @brief Target SSID of the Soft Access point to which the device has to connect
46 static char *targetSsid;
50 * @brief Password of the target access point to which the device has to connect
52 static char *targetPass;
55 * @var g_cbForEnrolleeStatus
56 * @brief Fucntion pointer holding the callback for intimation of EasySetup Enrollee status callback
58 static EventCallback g_cbForEnrolleeStatus = NULL;
60 //-----------------------------------------------------------------------------
61 // Private internal function prototypes
62 //-----------------------------------------------------------------------------
64 void EventCallbackInOnboarding(ESResult esResult);
65 void EventCallbackInProvisioning(ESResult esResult);
66 void EventCallbackAfterProvisioning(ESResult esResult);
68 void EventCallbackInOnboarding(ESResult esResult)
70 if (g_cbForEnrolleeStatus != NULL){
71 OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Calling the application with esResult = %d", esResult);
72 if(esResult == ES_OK){
73 OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Calling the application with enrolleestate = %d",
75 g_cbForEnrolleeStatus(esResult, ES_ON_BOARDED_STATE);
78 OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Calling the application with enrolleestate = %d",
80 g_cbForEnrolleeStatus(esResult, ES_INIT_STATE);
84 OC_LOG(ERROR, ES_ENROLLEE_TAG, "g_cbForEnrolleeStatus is NULL");
88 void EventCallbackInProvisioning(ESResult esResult)
92 if (esResult == ES_RECVTRIGGEROFPROVRES)
94 targetSsid = (char *) malloc(MAXSSIDLEN);
95 targetPass = (char *) malloc(MAXNETCREDLEN);
97 if(TerminateEasySetup() != OC_STACK_OK)
99 OC_LOG(ERROR, ES_ENROLLEE_TAG, "Terminating stack failed");
103 GetTargetNetworkInfoFromProvResource(targetSsid, targetPass);
105 res = ConnectToWiFiNetwork(targetSsid, targetPass, EventCallbackAfterProvisioning);
107 if (g_cbForEnrolleeStatus != NULL)
109 if(res == ES_NETWORKCONNECTED){
110 OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Calling the application with enrolleestate = %d",
111 ES_PROVISIONED_STATE);
112 g_cbForEnrolleeStatus(ES_OK, ES_PROVISIONED_STATE);
115 OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Calling the application with enrolleestate = %d",
116 ES_PROVISIONING_STATE);
117 g_cbForEnrolleeStatus(ES_OK, ES_PROVISIONING_STATE);
124 void EventCallbackAfterProvisioning(ESResult esResult)
126 if (g_cbForEnrolleeStatus != NULL){
127 if(esResult == ES_OK){
128 OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Calling the application with enrolleestate = %d",
129 ES_PROVISIONED_STATE);
130 g_cbForEnrolleeStatus(esResult, ES_PROVISIONED_STATE);
133 OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Calling the application with enrolleestate = %d",
134 ES_PROVISIONING_STATE);
135 g_cbForEnrolleeStatus(esResult, ES_PROVISIONING_STATE);
139 OC_LOG(ERROR, ES_ENROLLEE_TAG, "g_cbForEnrolleeStatus is NULL");
143 ESResult FindNetworkForOnboarding(OCConnectivityType networkType,
148 if (!ssid || !passwd)
153 if (networkType == CT_ADAPTER_IP)
155 if (g_cbForEnrolleeStatus == NULL)
157 g_cbForEnrolleeStatus = cb;
160 if(ConnectToWiFiNetwork(ssid, passwd, EventCallbackInOnboarding) != ES_NETWORKCONNECTED)
162 OC_LOG(ERROR, ES_ENROLLEE_TAG, "ConnectToWiFiNetwork Failed");
163 cb(ES_ERROR, ES_ON_BOARDING_STATE);
167 OC_LOG(INFO, ES_ENROLLEE_TAG, "ConnectToWiFiNetwork Success");
168 cb(ES_OK, ES_ON_BOARDED_STATE);
176 ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid, const char *passwd,
179 if(FindNetworkForOnboarding(networkType, ssid, passwd, cb) != ES_OK)
181 OC_LOG(ERROR, ES_ENROLLEE_TAG, "OnBoarding Failed");
185 // Initialize the OC Stack in Server mode
186 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
188 OC_LOG(ERROR, ES_ENROLLEE_TAG, "OCStack init error");
193 OC_LOG(DEBUG, ES_ENROLLEE_TAG, "OCStack init success");
198 ESResult TerminateEasySetup()
200 if(OCStop() != OC_STACK_OK)
202 OC_LOG(ERROR, ES_ENROLLEE_TAG, "OCStack stop failed");
207 OC_LOG(ERROR, ES_ENROLLEE_TAG, "OCStack stop success");
212 ESResult InitProvisioning()
214 if (CreateProvisioningResource() != OC_STACK_OK)
219 if (CreateNetworkResource() != OC_STACK_OK)
224 RegisterResourceEventCallBack(EventCallbackInProvisioning);
226 return ES_RESOURCECREATED;