Update resource models of easy setup in Enrollee side
[platform/upstream/iotivity.git] / service / easy-setup / enrollee / src / easysetup.c
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 #include "easysetupcallbacks.h"
33
34 /**
35  * @var ES_ENROLLEE_TAG
36  * @brief Logging tag for module name.
37  */
38 #define ES_ENROLLEE_TAG "ES"
39
40 //-----------------------------------------------------------------------------
41 // Private variables
42 //-----------------------------------------------------------------------------
43
44 /**
45  * @var gTargetSsid
46  * @brief Target SSID of the Soft Access point to which the device has to connect
47  */
48 static char gTargetSsid[MAXSSIDLEN];
49
50 /**
51  * @var gTargetPass
52  * @brief Password of the target access point to which the device has to connect
53  */
54 static char gTargetPass[MAXNETCREDLEN];
55
56 /**
57  * @var gEnrolleeStatusCb
58  * @brief Fucntion pointer holding the callback for intimation of EasySetup Enrollee status callback
59  */
60 static ESEnrolleeEventCallback gEnrolleeStatusCb = NULL;
61
62 /**
63  * @var gIsSecured
64  * @brief Variable to check if secure mode is enabled or not.
65  */
66 static bool gIsSecured = false;
67
68 void ESOnboardingCallback(ESResult esResult)
69 {
70         OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESOnboardingCallback with  result = %d", esResult);
71         if(esResult == ES_OK)
72         {
73             gEnrolleeStatusCb(esResult, ES_ON_BOARDED_STATE);
74         }
75         else
76         {
77             OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG,
78                         "Onboarding is failed callback result is = %d", esResult);
79             gEnrolleeStatusCb(esResult, ES_INIT_STATE);
80         }
81 }
82
83 void ESProvisioningCallback(ESResult esResult)
84 {
85     OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESProvisioningCallback with  result = %d", esResult);
86
87     if (esResult == ES_RECVTRIGGEROFPROVRES)
88     {
89         GetTargetNetworkInfoFromProvResource(gTargetSsid, gTargetPass);
90         gEnrolleeStatusCb(ES_OK, ES_PROVISIONED_STATE);
91         OIC_LOG(DEBUG, ES_ENROLLEE_TAG, "Connecting with target network");
92
93         // Connecting/onboarding to target network
94         ConnectToWiFiNetwork(gTargetSsid, gTargetPass, ESOnboardingCallbackTargetNet);
95     }
96     else
97     {
98        OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Provisioning is failed callback result is = %d", esResult);
99        // Resetting Enrollee to ONBOARDED_STATE as Enrollee is alreday onboarded in previous step
100        gEnrolleeStatusCb(ES_OK, ES_ON_BOARDED_STATE);
101     }
102 }
103
104 void ESOnboardingCallbackTargetNet(ESResult esResult)
105 {
106     OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESOnboardingCallback on target network with result = %d",
107                                                                                         esResult);
108     if(esResult == ES_OK)
109     {
110         gEnrolleeStatusCb(esResult, ES_ON_BOARDED_TARGET_NETWORK_STATE);
111     }
112     else
113     {
114         OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG,
115                     "Onboarding is failed on target network and callback result is = %d", esResult);
116         // Resetting Enrollee state to the ES_PROVISIONED_STATE
117         // as device is already being provisioned with target network creds.
118         gEnrolleeStatusCb(esResult, ES_PROVISIONED_STATE);
119     }
120 }
121
122 ESResult ESInitEnrollee(OCConnectivityType networkType, const char *ssid, const char *passwd,
123         bool isSecured,
124         ESEnrolleeEventCallback cb)
125 {
126     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee IN");
127     if(!ESEnrolleeValidateParam(networkType,ssid,passwd,cb))
128     {
129         OIC_LOG(ERROR, ES_ENROLLEE_TAG,
130                             "ESInitEnrollee::Stopping Easy setup due to invalid parameters");
131         return ES_ERROR;
132     }
133
134     //Init callback
135     gEnrolleeStatusCb = cb;
136
137     gIsSecured = isSecured;
138
139     // TODO : This onboarding state has to be set by lower layer, as they better
140     // knows when actually on-boarding started.
141     cb(ES_ERROR,ES_ON_BOARDING_STATE);
142
143     OIC_LOG(INFO, ES_ENROLLEE_TAG, "received callback");
144     OIC_LOG(INFO, ES_ENROLLEE_TAG, "onboarding now..");
145
146     if(!ESOnboard(ssid, passwd, ESOnboardingCallback))
147     {
148         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESInitEnrollee::On-boarding failed");
149         cb(ES_ERROR, ES_INIT_STATE);
150         return ES_ERROR;
151     }
152
153     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee OUT");
154     return ES_OK;
155 }
156
157 ESResult ESTerminateEnrollee()
158 {
159     UnRegisterResourceEventCallBack();
160
161     //Delete Prov resource
162     if (DeleteEasySetupResources() != OC_STACK_OK)
163     {
164         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
165         return ES_ERROR;
166     }
167
168     OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESTerminateEnrollee success");
169     return ES_OK;
170 }
171
172 ESResult ESInitProvisioning()
173 {
174     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitProvisioning <<IN>>");
175
176     if (CreateEasySetupResources(gIsSecured) != OC_STACK_OK)
177     {
178         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateProvisioningResource error");
179         return ES_ERROR;
180     }
181
182     RegisterResourceEventCallBack(ESProvisioningCallback);
183
184     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitProvisioning <<OUT>>");
185     return ES_RESOURCECREATED;
186 }
187
188 static bool ESEnrolleeValidateParam(OCConnectivityType networkType, const char *ssid,
189                                                 const char *passwd, ESEnrolleeEventCallback cb)
190 {
191     if (!ssid || !passwd || !cb)
192     {
193         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESEnrolleeValidateParam - Invalid parameters");
194         return false;
195     }
196     return true;
197 }
198