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