1 //******************************************************************
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "resourceHandler.h"
22 #include "ocpayload.h"
29 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
30 OCEntityHandlerRequest *ehRequest, void *callback);
31 const char *getResult(OCStackResult result);
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);
40 ResourceEventCallback g_cbForResEvent = NULL;
42 void RegisterResourceEventCallBack(ResourceEventCallback cb)
47 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
49 if (name != NULL && pass != NULL)
51 sprintf(name, "%s", g_prov.tnn);
52 sprintf(pass, "%s", g_prov.cd);
56 OCStackResult CreateProvisioningResource()
58 g_prov.ps = 1; // need to provisioning
60 sprintf(g_prov.tnn, "Unknown");
61 sprintf(g_prov.cd, "Unknown");
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);
66 OC_LOG_V(INFO, TAG, "Created Prov resource with result: %s", getResult(res));
71 OCStackResult CreateNetworkResource()
75 if (getCurrentNetworkInfo(ES_WIFI, &netInfo) != 0)
77 return OC_STACK_ERROR;
80 if (netInfo.type != ES_WIFI)
82 return OC_STACK_ERROR;
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],
89 sprintf(g_net.cnn, "%s", netInfo.ssid);
91 OC_LOG_V(INFO, TAG, "SSID: %s", g_net.cnn);
92 OC_LOG_V(INFO, TAG, "IP Address: %s", g_net.ipaddr);
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));
101 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
103 OCEntityHandlerResult ehResult = OC_EH_ERROR;
106 OC_LOG(ERROR, TAG, "Request is Null");
109 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
111 OC_LOG(ERROR, TAG, "Incoming payload not a representation");
115 OCRepPayload *getResp = constructResponse(ehRequest);
118 OC_LOG(ERROR, TAG, "constructResponse failed");
128 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
131 OCEntityHandlerResult ehResult = OC_EH_ERROR;
132 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
134 OC_LOG(ERROR, TAG, "Incoming payload not a representation");
138 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
141 OC_LOG(ERROR, TAG, "Failed to parse");
146 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn))
148 sprintf(g_prov.tnn, "%s", tnn);
152 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
154 sprintf(g_prov.cd, "%s", cd);
159 OCRepPayload *getResp = constructResponse(ehRequest);
162 OC_LOG(ERROR, TAG, "constructResponse failed");
172 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
174 OCEntityHandlerResult ehResult = OC_EH_ERROR;
177 OC_LOG(ERROR, TAG, "Request is Null");
180 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
182 OC_LOG(ERROR, TAG, "Incoming payload not a representation");
186 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
189 OC_LOG(ERROR, TAG, "Failed to parse");
193 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TR, &tr))
205 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest)
207 OCRepPayload* payload = OCRepPayloadCreate();
210 OC_LOG(ERROR, TAG, "Failed to allocate Payload");
214 if (ehRequest->resource == g_prov.handle)
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);
222 else if (ehRequest->requestHandle == g_net.handle)
225 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_NET);
226 OCRepPayloadSetPropInt(payload, "ant", g_net.ant[0]);
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)
237 OCEntityHandlerResult ehRet = OC_EH_OK;
238 OCEntityHandlerResponse response =
240 OCRepPayload* payload = NULL;
241 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
243 if (OC_REST_GET == entityHandlerRequest->method)
245 OC_LOG(INFO, TAG, "Received GET request");
246 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
248 else if (OC_REST_PUT == entityHandlerRequest->method)
250 OC_LOG(INFO, TAG, "Received PUT request");
252 if (g_prov.handle != NULL && entityHandlerRequest->resource == g_prov.handle)
254 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
261 else if (OC_REST_POST == entityHandlerRequest->method)
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);
268 if (ehRet == OC_EH_OK)
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;
284 if (OCDoResponse(&response) != OC_STACK_OK)
286 OC_LOG(ERROR, TAG, "Error sending response");
294 g_cbForResEvent(ES_RECVTRIGGEROFPROVRES);
301 const char *getResult(OCStackResult result)
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";
336 return "OC_STACK_ERROR";