18ac9403ff666c6456c77af3c4f7e6aa4bd2f2a9
[platform/upstream/iotivity.git] / service / easy-setup / sdk / enrollee / src / resourcehandler.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 #include "resourcehandler.h"
22 #include "ocpayload.h"
23
24
25 /**
26  * @var ES_RH_TAG
27  * @brief Logging tag for module name.
28  */
29 #define ES_RH_TAG "ES_RH"
30
31 //-----------------------------------------------------------------------------
32 // Private variables
33 //-----------------------------------------------------------------------------
34
35 /**
36  * @var gProvResource
37  * @brief Structure for holding the Provisioning status and target information required to connect to the target network
38  */
39 static ProvResource gProvResource;
40
41 /**
42  * @var gNetResource
43  * @brief Structure forr holding the Provisioning status of network information
44  */
45 static NetResource gNetResource;
46
47 //-----------------------------------------------------------------------------
48 // Private internal function prototypes
49 //-----------------------------------------------------------------------------
50 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
51                                                 OCEntityHandlerRequest *ehRequest, void *callback);
52 const char *getResult(OCStackResult result);
53 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
54                                                OCRepPayload** payload);
55 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
56                                                OCRepPayload** payload);
57 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
58                                                 OCRepPayload** payload);
59 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest);
60
61 static int g_flag = 0;
62
63 ResourceEventCallback gNetworkInfoProvEventCb = NULL;
64
65 void RegisterResourceEventCallBack(ResourceEventCallback cb)
66 {
67     gNetworkInfoProvEventCb = cb;
68 }
69
70 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
71 {
72     if (name != NULL && pass != NULL)
73     {
74         sprintf(name, "%s", gProvResource.tnn);
75         sprintf(pass, "%s", gProvResource.cd);
76     }
77 }
78
79 OCStackResult CreateProvisioningResource()
80 {
81     gProvResource.ps = 1; // need to do provisioning
82     gProvResource.tnt = CT_ADAPTER_IP;
83     sprintf(gProvResource.tnn, "Unknown");
84     sprintf(gProvResource.cd, "Unknown");
85
86     OCStackResult res = OCCreateResource(&gProvResource.handle, "oic.r.prov", OC_RSRVD_INTERFACE_DEFAULT,
87                                                 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb, NULL,
88                                                 OC_DISCOVERABLE | OC_OBSERVABLE);
89     OC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
90     return res;
91 }
92
93 #ifdef ESWIFI
94 OCStackResult CreateNetworkResource()
95 {
96     NetworkInfo netInfo;
97
98     if (getCurrentNetworkInfo(CT_ADAPTER_IP, &netInfo) != ES_OK)
99     {
100         return OC_STACK_ERROR;
101     }
102
103     if (netInfo.type != CT_ADAPTER_IP)
104     {
105         return OC_STACK_ERROR;
106     }
107
108     gNetResource.cnt = (int) netInfo.type;
109     gNetResource.ant[0] = (int) CT_ADAPTER_IP;
110
111     if(netInfo.ipaddr != NULL)
112     {
113         sprintf(gNetResource.ipaddr, "%d.%d.%d.%d", netInfo.ipaddr[0], netInfo.ipaddr[1],
114                                             netInfo.ipaddr[2], netInfo.ipaddr[3]);
115     }
116     sprintf(gNetResource.cnn, "%s", netInfo.ssid);
117
118     OC_LOG_V(INFO, ES_RH_TAG, "SSID: %s", gNetResource.cnn);
119     OC_LOG_V(INFO, ES_RH_TAG, "IP Address: %s", gNetResource.ipaddr);
120
121     OCStackResult res = OCCreateResource(&gNetResource.handle, "oic.r.net", OC_RSRVD_INTERFACE_DEFAULT,
122             OC_RSRVD_ES_URI_NET, OCEntityHandlerCb,NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
123
124     OC_LOG_V(INFO, ES_RH_TAG, "Created Net resource with result: %s", getResult(res));
125
126     return res;
127 }
128 #endif
129 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
130                                                 OCRepPayload **payload)
131 {
132     OCEntityHandlerResult ehResult = OC_EH_ERROR;
133     if (!ehRequest)
134     {
135         OC_LOG(ERROR, ES_RH_TAG, "Request is Null");
136         return ehResult;
137     }
138     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
139     {
140         OC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
141         return ehResult;
142     }
143
144     OCRepPayload *getResp = constructResponse(ehRequest);
145     if (!getResp)
146     {
147         OC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
148         return OC_EH_ERROR;
149     }
150
151     *payload = getResp;
152     ehResult = OC_EH_OK;
153
154     return ehResult;
155 }
156
157 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
158                                                OCRepPayload** payload)
159 {
160     OC_LOG(INFO, ES_RH_TAG, "ProcessPutRequest enter");
161     OCEntityHandlerResult ehResult = OC_EH_ERROR;
162     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
163     {
164         OC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
165         return ehResult;
166     }
167
168     OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
169     if (!input)
170     {
171         OC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
172         return ehResult;
173     }
174
175     char* tnn;
176     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn))
177     {
178         sprintf(gProvResource.tnn, "%s", tnn);
179         OC_LOG(INFO, ES_RH_TAG, "got ssid");
180     }
181
182     char* cd;
183     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
184     {
185         sprintf(gProvResource.cd, "%s", cd);
186         OC_LOG(INFO, ES_RH_TAG, "got password");
187     }
188     gProvResource.ps = 2;
189     OC_LOG_V(INFO, ES_RH_TAG, "gProvResource.ps %d", gProvResource.ps);
190     g_flag = 1;
191
192     OCRepPayload *getResp = constructResponse(ehRequest);
193     if (!getResp)
194     {
195         OC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
196         return OC_EH_ERROR;
197     }
198
199     *payload = getResp;
200     ehResult = OC_EH_OK;
201
202     return ehResult;
203 }
204
205 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
206                                                 OCRepPayload** payload)
207 {
208     OCEntityHandlerResult ehResult = OC_EH_ERROR;
209     if (!ehRequest)
210     {
211         OC_LOG(ERROR, ES_RH_TAG, "Request is Null");
212         return ehResult;
213     }
214     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
215     {
216         OC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
217         return ehResult;
218     }
219
220     OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
221     if (!input)
222     {
223         OC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
224         return ehResult;
225     }
226     char* tr;
227     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TR, &tr))
228     {
229         // Triggering
230         ehResult = OC_EH_OK;
231     }
232     g_flag = 1;
233
234     return ehResult;
235 }
236
237 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest)
238 {
239     OCRepPayload* payload = OCRepPayloadCreate();
240     if (!payload)
241     {
242         OC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
243         return NULL;
244     }
245
246     if (ehRequest->resource == gProvResource.handle)
247     {
248         OC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
249         OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
250         OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PS, gProvResource.ps);
251         OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_TNT, gProvResource.tnt);
252         OCRepPayloadSetPropString(payload, OC_RSRVD_ES_TNN, gProvResource.tnn);
253         OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CD, gProvResource.cd);
254     }
255     else if (ehRequest->requestHandle == gNetResource.handle)
256     {
257
258         OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_NET);
259         OCRepPayloadSetPropInt(payload, "ant", gNetResource.ant[0]);
260     }
261     return payload;
262 }
263
264 /**
265  * This is the entity handler for the registered resource.
266  * This is invoked by OCStack whenever it recevies a request for this resource.
267  */
268 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
269         OCEntityHandlerRequest* entityHandlerRequest, void *callback)
270 {
271     (void) callback;
272     OCEntityHandlerResult ehRet = OC_EH_OK;
273     OCEntityHandlerResponse response = { 0 };
274     OCRepPayload* payload = NULL;
275     if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
276     {
277         if (OC_REST_GET == entityHandlerRequest->method)
278         {
279             OC_LOG(INFO, ES_RH_TAG, "Received GET request");
280             ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
281         }
282         else if (OC_REST_PUT == entityHandlerRequest->method)
283         {
284             OC_LOG(INFO, ES_RH_TAG, "Received PUT request");
285
286             if (gProvResource.handle != NULL && entityHandlerRequest->resource == gProvResource.handle)
287             {
288                 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
289             }
290             else
291             {
292                 OC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
293                 ehRet = OC_EH_ERROR;
294             }
295         }
296         else if (OC_REST_POST == entityHandlerRequest->method)
297         {
298             // TODO: As of now, POST request will be not received.
299             OC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
300             //ehRet = ProcessPostRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
301         }
302
303         if (ehRet == OC_EH_OK)
304         {
305             // Format the response.  Note this requires some info about the request
306             response.requestHandle = entityHandlerRequest->requestHandle;
307             response.resourceHandle = entityHandlerRequest->resource;
308             response.ehResult = ehRet;
309             //response uses OCPaylod while all get,put methodes use OCRepPayload
310             response.payload = (OCPayload*) (payload);
311             response.numSendVendorSpecificHeaderOptions = 0;
312             memset(response.sendVendorSpecificHeaderOptions, 0,
313                     sizeof response.sendVendorSpecificHeaderOptions);
314             memset(response.resourceUri, 0, sizeof response.resourceUri);
315             // Indicate that response is NOT in a persistent buffer
316             response.persistentBufferFlag = 0;
317
318             // Send the response
319             if (OCDoResponse(&response) != OC_STACK_OK)
320             {
321                 OC_LOG(ERROR, ES_RH_TAG, "Error sending response");
322                 ehRet = OC_EH_ERROR;
323             }
324         }
325     }
326
327     if (g_flag == 1)
328     {
329         gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES);
330         g_flag = 0;
331     }
332
333     return ehRet;
334 }
335
336 const char *getResult(OCStackResult result)
337 {
338     switch (result)
339     {
340         case OC_STACK_OK:
341             return "OC_STACK_OK";
342         case OC_STACK_INVALID_URI:
343             return "OC_STACK_INVALID_URI";
344         case OC_STACK_INVALID_QUERY:
345             return "OC_STACK_INVALID_QUERY";
346         case OC_STACK_INVALID_IP:
347             return "OC_STACK_INVALID_IP";
348         case OC_STACK_INVALID_PORT:
349             return "OC_STACK_INVALID_PORT";
350         case OC_STACK_INVALID_CALLBACK:
351             return "OC_STACK_INVALID_CALLBACK";
352         case OC_STACK_INVALID_METHOD:
353             return "OC_STACK_INVALID_METHOD";
354         case OC_STACK_NO_MEMORY:
355             return "OC_STACK_NO_MEMORY";
356         case OC_STACK_COMM_ERROR:
357             return "OC_STACK_COMM_ERROR";
358         case OC_STACK_INVALID_PARAM:
359             return "OC_STACK_INVALID_PARAM";
360         case OC_STACK_NOTIMPL:
361             return "OC_STACK_NOTIMPL";
362         case OC_STACK_NO_RESOURCE:
363             return "OC_STACK_NO_RESOURCE";
364         case OC_STACK_RESOURCE_ERROR:
365             return "OC_STACK_RESOURCE_ERROR";
366         case OC_STACK_SLOW_RESOURCE:
367             return "OC_STACK_SLOW_RESOURCE";
368         case OC_STACK_NO_OBSERVERS:
369             return "OC_STACK_NO_OBSERVERS";
370         case OC_STACK_ERROR:
371             return "OC_STACK_ERROR";
372         default:
373             return "UNKNOWN";
374     }
375 }
376