1 //******************************************************************
3 // Copyright 2014 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "easysetup.h"
31 EventCallback g_cbForProvisioning = NULL;
32 EventCallback g_cbForOnboarding = NULL;
34 void EventCallbackInOnboarding(ESResult event);
35 void EventCallbackInProvisioning(ESResult event);
36 void EventCallbackAfterProvisioning(ESResult event);
38 void EventCallbackInOnboarding(ESResult event)
40 if (event == ES_NETWORKFOUND || event == ES_NETWORKCONNECTED)
42 if (g_cbForOnboarding != NULL)
44 g_cbForOnboarding(event);
49 void EventCallbackInProvisioning(ESResult event)
53 if (event == ES_RECVTRIGGEROFPROVRES)
55 targetSsid = (char *) malloc(MAXSSIDLEN);
56 targetPass = (char *) malloc(MAXNETCREDLEN);
58 if(TerminateEasySetup() != OC_STACK_OK)
60 OC_LOG(ERROR, TAG, PCF("Terminating stack failed"));
64 GetTargetNetworkInfoFromProvResource(targetSsid, targetPass);
66 res = ConnectToWiFiNetwork(targetSsid, targetPass, EventCallbackAfterProvisioning);
68 if (g_cbForProvisioning != NULL)
70 g_cbForProvisioning(res);
75 void EventCallbackAfterProvisioning(ESResult event)
77 if (event == ES_NETWORKFOUND || event == ES_NETWORKCONNECTED)
79 if (g_cbForProvisioning != NULL)
81 g_cbForProvisioning(event);
86 ESResult FindNetworkForOnboarding(NetworkType networkType,
96 if (networkType == ES_WIFI)
98 if (g_cbForOnboarding == NULL)
100 g_cbForOnboarding = cb;
103 if(ConnectToWiFiNetwork(ssid, passwd, EventCallbackInOnboarding) != ES_NETWORKCONNECTED)
105 OC_LOG(ERROR, TAG, PCF("ConnectToWiFiNetwork Failed"));
109 OC_LOG(INFO, TAG, PCF("ConnectToWiFiNetwork Success"));
116 ESResult InitEasySetup(NetworkType networkType, const char *ssid, const char *passwd,
119 if(FindNetworkForOnboarding(networkType, ssid, passwd, cb) != ES_OK)
121 OC_LOG(ERROR, TAG, PCF("OnBoarding Failed"));
125 // Initialize the OC Stack in Server mode
126 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
128 OC_LOG(ERROR, TAG, PCF("OCStack init error"));
133 OC_LOG(DEBUG, TAG, PCF("OCStack init success"));
138 ESResult TerminateEasySetup()
140 if(OCStop() != OC_STACK_OK)
142 OC_LOG(ERROR, TAG, PCF("OCStack stop failed"));
147 OC_LOG(ERROR, TAG, PCF("OCStack stop success"));
152 ESResult InitProvisioning(EventCallback cb)
160 g_cbForProvisioning = cb;
163 if (CreateProvisioningResource() != OC_STACK_OK)
168 if (CreateNetworkResource() != OC_STACK_OK)
173 RegisterResourceEventCallBack(EventCallbackInProvisioning);
175 return ES_RESOURCECREATED;