64f3d6cb3fdbe14731bb943266e710911089bd24
[platform/upstream/iotivity.git] / service / easy-setup / enrollee / src / easysetup.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 /**
22  * @file
23  *
24  * This file contains the implementation for EasySetup Enrollee device
25  */
26
27 #include "easysetup.h"
28 #include "softap.h"
29 #include "onboarding.h"
30 #include "logger.h"
31 #include "resourcehandler.h"
32
33 /**
34  * @var ES_ENROLLEE_TAG
35  * @brief Logging tag for module name.
36  */
37 #define ES_ENROLLEE_TAG "ES"
38
39 //-----------------------------------------------------------------------------
40 // Private variables
41 //-----------------------------------------------------------------------------
42
43 /**
44  * @var targetSsid
45  * @brief Target SSID of the Soft Access point to which the device has to connect
46  */
47 static char *targetSsid;
48
49 /**
50  * @var targetPass
51  * @brief Password of the target access point to which the device has to connect
52  */
53 static char *targetPass;
54
55 /**
56  * @var gEnrolleeStatusCb
57  * @brief Fucntion pointer holding the callback for intimation of EasySetup Enrollee status callback
58  */
59 static EventCallback gEnrolleeStatusCb = NULL;
60
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,
68               EventCallback cb);
69
70
71 void OnboardingCallback(ESResult esResult)
72 {
73         OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "OnboardingCallback with  result = %d", esResult);
74         if(esResult == ES_OK)
75         {
76             gEnrolleeStatusCb(esResult, ES_ON_BOARDED_STATE);
77         }
78         else
79         {
80             OC_LOG_V(DEBUG, ES_ENROLLEE_TAG,
81                         "Onboarding is failed callback result is = %d", esResult);
82             gEnrolleeStatusCb(esResult, ES_INIT_STATE);
83         }
84 }
85
86 void ProvisioningCallback(ESResult esResult)
87 {
88     OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ProvisioningCallback with  result = %d", esResult);
89     ESResult res = ES_OK;
90     if (esResult == ES_RECVTRIGGEROFPROVRES)
91     {
92         targetSsid = (char *) malloc(MAXSSIDLEN);
93         targetPass = (char *) malloc(MAXNETCREDLEN);
94
95         GetTargetNetworkInfoFromProvResource(targetSsid, targetPass);
96         gEnrolleeStatusCb(ES_OK, ES_PROVISIONED_STATE);
97         OC_LOG(DEBUG, ES_ENROLLEE_TAG, "Connecting with target network");
98
99         // Connecting/onboarding to target network
100         ConnectToWiFiNetwork(targetSsid, targetPass, OnboardingCallbackTargetNet);
101     }
102     else
103     {
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);
107     }
108 }
109
110 void OnboardingCallbackTargetNet(ESResult esResult)
111 {
112     OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "OnboardingCallback on target network with result = %d",
113                                                                                         esResult);
114     if(esResult == ES_OK)
115     {
116         gEnrolleeStatusCb(esResult, ES_ON_BOARDED_TARGET_NETWORK_STATE);
117     }
118     else
119     {
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);
125     }
126 }
127
128 static FILE* server_fopen(const char* /*path*/, const char *mode)
129 {
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);
132     if(file==NULL)
133     {
134         OC_LOG(ERROR,ES_ENROLLEE_TAG,"oic_svr_db_server failed");
135     }
136     return file;
137 }
138
139 ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid,
140                             const char *passwd, EventCallback cb)
141 {
142     OC_LOG(INFO, ES_ENROLLEE_TAG, "InitEasySetup IN");
143     if(!validateParam(networkType,ssid,passwd,cb))
144     {
145         OC_LOG(ERROR, ES_ENROLLEE_TAG,
146                             "InitEasySetup::Stopping Easy setup due to invalid parameters");
147         return ES_ERROR;
148     }
149
150     //Init callback
151     gEnrolleeStatusCb = cb;
152
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);
156
157     OC_LOG(INFO, ES_ENROLLEE_TAG, "received callback");
158     OC_LOG(INFO, ES_ENROLLEE_TAG, "onboarding now..");
159
160     if(!ESOnboard(ssid, passwd, OnboardingCallback))
161     {
162         OC_LOG(ERROR, ES_ENROLLEE_TAG, "InitEasySetup::On-boarding failed");
163         cb(ES_ERROR, ES_INIT_STATE);
164         return ES_ERROR;
165     }
166
167     OC_LOG(INFO, ES_ENROLLEE_TAG, "InitEasySetup OUT");
168     return ES_OK;
169 }
170
171 ESResult TerminateEasySetup()
172 {
173     UnRegisterResourceEventCallBack();
174
175     //Delete Prov resource
176     if (DeleteProvisioningResource() != OC_STACK_OK)
177     {
178         OC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
179         return ES_ERROR;
180     }
181
182     //Delete Prov resource
183 #ifdef ESWIFI
184     if (DeleteNetworkResource() != OC_STACK_OK)
185     {
186         OC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
187         return ES_ERROR;
188     }
189 #endif
190
191     OC_LOG(ERROR, ES_ENROLLEE_TAG, "TerminateEasySetup success");
192     return ES_OK;
193 }
194
195 ESResult InitProvisioning()
196 {
197     OC_LOG(INFO, ES_ENROLLEE_TAG, "InitProvisioning <<IN>>");
198
199     if (CreateProvisioningResource() != OC_STACK_OK)
200     {
201         OC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateProvisioningResource error");
202         return ES_ERROR;
203     }
204
205 #ifdef ESWIFI
206     if (CreateNetworkResource() != OC_STACK_OK)
207     {
208         OC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateNetworkResource error");
209         return ES_ERROR;
210     }
211 #endif
212
213     RegisterResourceEventCallBack(ProvisioningCallback);
214
215     OC_LOG(INFO, ES_ENROLLEE_TAG, "InitProvisioning OUT");
216     return ES_RESOURCECREATED;
217 }
218
219 bool validateParam(OCConnectivityType networkType, const char *ssid, const char *passwd,
220               EventCallback cb)
221 {
222     if (!ssid || !passwd || !cb)
223     {
224         OC_LOG(ERROR, ES_ENROLLEE_TAG, "validateParam - Invalid parameters");
225         return false;
226     }
227     return true;
228 }
229