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