b87f0c8b88afb68ee1ccea3f7b9993bd268dca03
[platform/upstream/iotivity.git] / service / easy-setup / sdk / 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     if(OCStop() != OC_STACK_OK)
174     {
175         OC_LOG(ERROR, ES_ENROLLEE_TAG, "OCStack stop failed");
176         return ES_ERROR;
177     }
178     else
179     {
180         OC_LOG(ERROR, ES_ENROLLEE_TAG, "OCStack stop success");
181         return ES_OK;
182     }
183 }
184
185 ESResult InitProvisioning()
186 {
187     OC_LOG(INFO, ES_ENROLLEE_TAG, "InitProvisioning <<IN>>");
188
189    // Initialize the OC Stack in Server mode
190     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
191     {
192         OC_LOG(ERROR, ES_ENROLLEE_TAG, "OCStack init error");
193         return ES_ERROR;
194     }
195
196     if (CreateProvisioningResource() != OC_STACK_OK)
197     {
198         OC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateProvisioningResource error");
199         return ES_ERROR;
200     }
201
202     if (CreateNetworkResource() != OC_STACK_OK)
203     {
204         OC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateNetworkResource error");
205         return ES_ERROR;
206     }
207
208     RegisterResourceEventCallBack(ProvisioningCallback);
209
210     OC_LOG(INFO, ES_ENROLLEE_TAG, "InitProvisioning OUT");
211     return ES_RESOURCECREATED;
212 }
213
214 bool validateParam(OCConnectivityType networkType, const char *ssid, const char *passwd,
215               EventCallback cb)
216 {
217     if (!ssid || !passwd || !cb)
218     {
219         OC_LOG(ERROR, ES_ENROLLEE_TAG, "validateParam - Invalid parameters");
220         return false;
221     }
222     return true;
223 }
224