Remove redundant files in Enrollee folder
[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     /*
144     OIC_LOG(INFO, ES_ENROLLEE_TAG, "received callback");
145     OIC_LOG(INFO, ES_ENROLLEE_TAG, "onboarding now..");
146
147     if(!ESOnboard(ssid, passwd, ESOnboardingCallback))
148     {
149         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESInitEnrollee::On-boarding failed");
150         cb(ES_ERROR, ES_INIT_STATE);
151         return ES_ERROR;
152     }
153     */
154
155     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee OUT");
156     return ES_OK;
157 }
158
159 ESResult ESTerminateEnrollee()
160 {
161     UnRegisterResourceEventCallBack();
162
163     //Delete Prov resource
164     if (DeleteEasySetupResources() != OC_STACK_OK)
165     {
166         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
167         return ES_ERROR;
168     }
169
170     OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESTerminateEnrollee success");
171     return ES_OK;
172 }
173
174 ESResult ESInitProvisioning()
175 {
176     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitProvisioning <<IN>>");
177
178     if (CreateEasySetupResources(gIsSecured) != OC_STACK_OK)
179     {
180         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateProvisioningResource error");
181         return ES_ERROR;
182     }
183
184     RegisterResourceEventCallBack(ESProvisioningCallback);
185
186     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitProvisioning <<OUT>>");
187     return ES_RESOURCECREATED;
188 }
189
190 static bool ESEnrolleeValidateParam(OCConnectivityType networkType, const char *ssid,
191                                                 const char *passwd, ESEnrolleeEventCallback cb)
192 {
193     if (!ssid || !passwd || !cb)
194     {
195         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESEnrolleeValidateParam - Invalid parameters");
196         return false;
197     }
198     return true;
199 }
200