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"
24 #define TAG PCF("ES_RH")
29 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
30 OCEntityHandlerRequest *ehRequest,
32 const char *getResult(OCStackResult result);
34 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
35 OCRepPayload** payload);
36 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
37 OCRepPayload** payload);
38 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
39 OCRepPayload** payload);
40 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest);
44 ResourceEventCallback g_cbForResEvent = NULL;
46 void RegisterResourceEventCallBack(ResourceEventCallback cb)
51 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
53 if (name != NULL && pass != NULL)
55 sprintf(name, "%s", g_prov.tnn);
56 sprintf(pass, "%s", g_prov.cd);
60 OCStackResult CreateProvisioningResource()
62 g_prov.ps = 1; // need to provisioning
64 sprintf(g_prov.tnn, "Unknown");
65 sprintf(g_prov.cd, "Unknown");
67 OCStackResult res = OCCreateResource(&g_prov.handle, "oic.prov", OC_RSRVD_INTERFACE_DEFAULT,
68 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
70 OC_LOG_V(INFO, TAG, PCF("Created Prov resource with result: %s"), getResult(res));
75 OCStackResult CreateNetworkResource()
79 if (getCurrentNetworkInfo(ES_WIFI, &netInfo) != 0)
81 return OC_STACK_ERROR;
84 if (netInfo.type != ES_WIFI)
86 return OC_STACK_ERROR;
89 g_net.cnt = (int) netInfo.type;
90 g_net.ant[0] = (int) ES_WIFI;
91 sprintf(g_net.ipaddr, "%d.%d.%d.%d", netInfo.ipaddr[0], netInfo.ipaddr[1], netInfo.ipaddr[2],
93 sprintf(g_net.cnn, "%s", netInfo.ssid);
95 OC_LOG_V(INFO, TAG, PCF("SSID: %s"), g_net.cnn);
96 OC_LOG_V(INFO, TAG, PCF("IP Address: %s"), g_net.ipaddr);
98 OCStackResult res = OCCreateResource(&g_net.handle, "oic.net", OC_RSRVD_INTERFACE_DEFAULT,
99 OC_RSRVD_ES_URI_NET, OCEntityHandlerCb,NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
100 OC_LOG_V(INFO, TAG, "Created Net resource with result: %s", getResult(res));
105 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
106 OCRepPayload **payload)
108 OCEntityHandlerResult ehResult = OC_EH_ERROR;
111 OC_LOG(ERROR, TAG, PCF("Request is Null"));
114 if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
116 OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
120 OCRepPayload *getResp = constructResponse(ehRequest);
123 OC_LOG(ERROR, TAG, PCF("constructResponse failed"));
133 OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
134 OCRepPayload** payload)
137 OCEntityHandlerResult ehResult=OC_EH_ERROR;
138 if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
140 OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
144 OCRepPayload* input = (OCRepPayload*)(ehRequest->payload);
147 OC_LOG_V(ERROR, TAG, PCF("Failed to parse"));
152 if(OCRepPayloadGetPropString(input,OC_RSRVD_ES_TNN, &tnn))
154 sprintf(g_prov.tnn, "%s", tnn);
158 if(OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
160 sprintf(g_prov.cd, "%s", cd);
165 OCRepPayload *getResp = constructResponse(ehRequest);
168 OC_LOG(ERROR, TAG, PCF("constructResponse failed"));
181 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
182 OCRepPayload** payload)
184 OCEntityHandlerResult ehResult = OC_EH_ERROR;
187 OC_LOG(ERROR, TAG, PCF("Request is Null"));
190 if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
192 OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
196 OCRepPayload* input = (OCRepPayload*)(ehRequest->payload);
199 OC_LOG_V(ERROR, TAG, PCF("Failed to parse") );
203 if(OCRepPayloadGetPropString(input, OC_RSRVD_ES_TR, &tr))
215 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest)
217 OCRepPayload* payload = OCRepPayloadCreate();
220 OC_LOG(ERROR, TAG, PCF("Failed to allocate Payload"));
224 if (ehRequest->resource == g_prov.handle)
226 OCRepPayloadSetUri(payload,OC_RSRVD_ES_URI_PROV);
227 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PS,g_prov.ps);
228 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_TNT, g_prov.tnt);
229 OCRepPayloadSetPropString(payload,OC_RSRVD_ES_TNN, g_prov.tnn);
230 OCRepPayloadSetPropString(payload,OC_RSRVD_ES_CD, g_prov.cd);
232 else if (ehRequest->requestHandle == g_net.handle)
235 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_NET);
236 OCRepPayloadSetPropInt(payload, "ant", g_net.ant[0]);
241 // This is the entity handler for the registered resource.
242 // This is invoked by OCStack whenever it recevies a request for this resource.
243 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
244 OCEntityHandlerRequest* entityHandlerRequest,void *callback)
247 OCEntityHandlerResult ehRet = OC_EH_OK;
248 OCEntityHandlerResponse response =
250 OCRepPayload* payload = NULL;
251 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
253 if (OC_REST_GET == entityHandlerRequest->method)
255 OC_LOG_V(INFO, TAG, PCF("Received GET request"));
256 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
258 else if (OC_REST_PUT == entityHandlerRequest->method)
260 OC_LOG_V(INFO, TAG, PCF("Received PUT request"));
262 if (g_prov.handle != NULL && entityHandlerRequest->resource == g_prov.handle)
264 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
271 else if (OC_REST_POST == entityHandlerRequest->method)
273 // TODO: As of now, POST request will be not received.
274 OC_LOG(INFO, TAG, "Received OC_REST_POST from client");
275 //ehRet = ProcessPostRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
278 if (ehRet == OC_EH_OK)
280 // Format the response. Note this requires some info about the request
281 response.requestHandle = entityHandlerRequest->requestHandle;
282 response.resourceHandle = entityHandlerRequest->resource;
283 response.ehResult = ehRet;
284 //response uses OCPaylod while all get,put methodes use OCRepPayload
285 response.payload = (OCPayload*)(payload);
286 response.numSendVendorSpecificHeaderOptions = 0;
287 memset(response.sendVendorSpecificHeaderOptions, 0,
288 sizeof response.sendVendorSpecificHeaderOptions);
289 memset(response.resourceUri, 0, sizeof response.resourceUri);
290 // Indicate that response is NOT in a persistent buffer
291 response.persistentBufferFlag = 0;
294 if (OCDoResponse(&response) != OC_STACK_OK)
296 OC_LOG(ERROR, TAG, PCF("Error sending response"));
304 g_cbForResEvent(ES_RECVTRIGGEROFPROVRES);
311 const char *getResult(OCStackResult result)
316 return "OC_STACK_OK";
317 case OC_STACK_INVALID_URI:
318 return "OC_STACK_INVALID_URI";
319 case OC_STACK_INVALID_QUERY:
320 return "OC_STACK_INVALID_QUERY";
321 case OC_STACK_INVALID_IP:
322 return "OC_STACK_INVALID_IP";
323 case OC_STACK_INVALID_PORT:
324 return "OC_STACK_INVALID_PORT";
325 case OC_STACK_INVALID_CALLBACK:
326 return "OC_STACK_INVALID_CALLBACK";
327 case OC_STACK_INVALID_METHOD:
328 return "OC_STACK_INVALID_METHOD";
329 case OC_STACK_NO_MEMORY:
330 return "OC_STACK_NO_MEMORY";
331 case OC_STACK_COMM_ERROR:
332 return "OC_STACK_COMM_ERROR";
333 case OC_STACK_INVALID_PARAM:
334 return "OC_STACK_INVALID_PARAM";
335 case OC_STACK_NOTIMPL:
336 return "OC_STACK_NOTIMPL";
337 case OC_STACK_NO_RESOURCE:
338 return "OC_STACK_NO_RESOURCE";
339 case OC_STACK_RESOURCE_ERROR:
340 return "OC_STACK_RESOURCE_ERROR";
341 case OC_STACK_SLOW_RESOURCE:
342 return "OC_STACK_SLOW_RESOURCE";
343 case OC_STACK_NO_OBSERVERS:
344 return "OC_STACK_NO_OBSERVERS";
346 return "OC_STACK_ERROR";