1 //******************************************************************
3 // Copyright 2015 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"
27 * @brief Logging tag for module name.
29 #define ES_RH_TAG "ES_RH"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
36 * @brief Structure for holding the Provisioning status and target information required to connect to the target network
38 static ProvResource gProvResource;
42 * @brief Structure forr holding the Provisioning status of network information
44 static NetResource gNetResource;
46 //-----------------------------------------------------------------------------
47 // Private internal function prototypes
48 //-----------------------------------------------------------------------------
49 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
50 OCEntityHandlerRequest *ehRequest, void *callback);
51 const char *getResult(OCStackResult result);
52 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
53 OCRepPayload** payload);
54 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
55 OCRepPayload** payload);
56 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
57 OCRepPayload** payload);
58 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest);
60 static int g_flag = 0;
62 ResourceEventCallback gNetworkInfoProvEventCb = NULL;
64 void RegisterResourceEventCallBack(ResourceEventCallback cb)
66 gNetworkInfoProvEventCb = cb;
69 void UnRegisterResourceEventCallBack()
71 if (gNetworkInfoProvEventCb)
73 gNetworkInfoProvEventCb = NULL;
77 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
79 if (name != NULL && pass != NULL)
81 sprintf(name, "%s", gProvResource.tnn);
82 sprintf(pass, "%s", gProvResource.cd);
86 OCStackResult CreateProvisioningResource(bool isSecured)
88 gProvResource.ps = ES_PS_NEED_PROVISIONING;
90 gProvResource.tnt = CT_ADAPTER_IP;
91 sprintf(gProvResource.tnn, "Unknown");
92 sprintf(gProvResource.cd, "Unknown");
94 OCStackResult res = OC_STACK_ERROR;
97 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_PROV_RES_TYPE,
98 OC_RSRVD_INTERFACE_DEFAULT,
102 OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
106 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_PROV_RES_TYPE,
107 OC_RSRVD_INTERFACE_DEFAULT,
108 OC_RSRVD_ES_URI_PROV,
111 OC_DISCOVERABLE | OC_OBSERVABLE);
114 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
118 OCStackResult DeleteProvisioningResource()
120 OCStackResult res = OCDeleteResource(gProvResource.handle);
121 if (res != OC_STACK_OK)
123 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
130 OCStackResult CreateNetworkResource()
134 if (getCurrentNetworkInfo(CT_ADAPTER_IP, &netInfo) != ES_OK)
136 return OC_STACK_ERROR;
139 if (netInfo.type != CT_ADAPTER_IP)
141 return OC_STACK_ERROR;
144 gNetResource.cnt = (int) netInfo.type;
145 gNetResource.ant[0] = (int) CT_ADAPTER_IP;
147 if(netInfo.ipaddr != NULL)
149 sprintf(gNetResource.ipaddr, "%d.%d.%d.%d", netInfo.ipaddr[0], netInfo.ipaddr[1],
150 netInfo.ipaddr[2], netInfo.ipaddr[3]);
152 sprintf(gNetResource.cnn, "%s", netInfo.ssid);
154 OIC_LOG_V(INFO, ES_RH_TAG, "SSID: %s", gNetResource.cnn);
155 OIC_LOG_V(INFO, ES_RH_TAG, "IP Address: %s", gNetResource.ipaddr);
157 OCStackResult res = OCCreateResource(&gNetResource.handle, "oic.r.net", OC_RSRVD_INTERFACE_DEFAULT,
158 OC_RSRVD_ES_URI_NET, OCEntityHandlerCb,NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
160 OIC_LOG_V(INFO, ES_RH_TAG, "Created Net resource with result: %s", getResult(res));
165 OCStackResult DeleteNetworkResource()
167 OCStackResult res = OCDeleteResource(gNetResource.handle);
168 if (res != OC_STACK_OK)
170 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Network resource error with result: %s",
178 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
179 OCRepPayload **payload)
181 OCEntityHandlerResult ehResult = OC_EH_ERROR;
184 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
187 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
189 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
193 OCRepPayload *getResp = constructResponse(ehRequest);
196 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
206 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
207 OCRepPayload** payload)
209 OIC_LOG(INFO, ES_RH_TAG, "ProcessPutRequest enter");
210 OCEntityHandlerResult ehResult = OC_EH_ERROR;
211 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
213 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
217 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
220 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
224 //TODO : ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
225 // A new request for provisioning means overriding existing network provisioning information.
226 // Metadata to indicate that it is override is needed. The metadata can be a new attribute
227 // should be added to the /oic/prov resource indicating to override the existing network
229 if (gProvResource.ps == ES_PS_PROVISIONING_COMPLETED)
231 OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed. "
232 "This a request to override the existing the network provisioning information");
235 // PUT request is appropriate for provisioning information to the enrollee.
236 // When an enrollee receives the put request, the entire resource information should
238 sprintf(gProvResource.tnn, "%s", "");
239 sprintf(gProvResource.tnn, "%s", "");
242 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn))
244 sprintf(gProvResource.tnn, "%s", tnn);
245 OIC_LOG(INFO, ES_RH_TAG, "got ssid");
248 OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.tnn %s", gProvResource.tnn);
250 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
252 sprintf(gProvResource.cd, "%s", cd);
253 OIC_LOG(INFO, ES_RH_TAG, "got password");
255 OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.cd %s", gProvResource.cd);
256 gProvResource.ps = 2;
257 OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.ps %d", gProvResource.ps);
260 OCRepPayload *getResp = constructResponse(ehRequest);
263 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
273 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
274 OCRepPayload** /*payload*/)
276 OCEntityHandlerResult ehResult = OC_EH_ERROR;
279 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
282 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
284 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
288 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
291 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
295 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TR, &tr))
305 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest)
307 OCRepPayload* payload = OCRepPayloadCreate();
310 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
314 if (ehRequest->resource == gProvResource.handle)
316 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
317 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
318 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PS, gProvResource.ps);
319 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_TNT, gProvResource.tnt);
320 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_TNN, gProvResource.tnn);
321 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CD, gProvResource.cd);
323 else if (ehRequest->requestHandle == gNetResource.handle)
326 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_NET);
327 OCRepPayloadSetPropInt(payload, "ant", gNetResource.ant[0]);
333 * This is the entity handler for the registered resource.
334 * This is invoked by OCStack whenever it recevies a request for this resource.
336 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
337 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
340 OCEntityHandlerResult ehRet = OC_EH_OK;
341 OCEntityHandlerResponse response = { 0, 0, OC_EH_ERROR, 0, 0, { },{ 0 }, false };
342 OCRepPayload* payload = NULL;
343 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
345 if (OC_REST_GET == entityHandlerRequest->method)
347 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
348 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
350 else if (OC_REST_PUT == entityHandlerRequest->method)
352 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
354 if (gProvResource.handle != NULL && entityHandlerRequest->resource == gProvResource.handle)
356 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
360 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
364 else if (OC_REST_POST == entityHandlerRequest->method)
366 // TODO: As of now, POST request will be not received.
367 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
368 //ehRet = ProcessPostRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
371 if (ehRet == OC_EH_OK)
373 // Format the response. Note this requires some info about the request
374 response.requestHandle = entityHandlerRequest->requestHandle;
375 response.resourceHandle = entityHandlerRequest->resource;
376 response.ehResult = ehRet;
377 //response uses OCPaylod while all get,put methodes use OCRepPayload
378 response.payload = (OCPayload*) (payload);
379 response.numSendVendorSpecificHeaderOptions = 0;
380 memset(response.sendVendorSpecificHeaderOptions, 0,
381 sizeof response.sendVendorSpecificHeaderOptions);
382 memset(response.resourceUri, 0, sizeof response.resourceUri);
383 // Indicate that response is NOT in a persistent buffer
384 response.persistentBufferFlag = 0;
387 if (OCDoResponse(&response) != OC_STACK_OK)
389 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
397 gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES);
404 const char *getResult(OCStackResult result)
409 return "OC_STACK_OK";
410 case OC_STACK_INVALID_URI:
411 return "OC_STACK_INVALID_URI";
412 case OC_STACK_INVALID_QUERY:
413 return "OC_STACK_INVALID_QUERY";
414 case OC_STACK_INVALID_IP:
415 return "OC_STACK_INVALID_IP";
416 case OC_STACK_INVALID_PORT:
417 return "OC_STACK_INVALID_PORT";
418 case OC_STACK_INVALID_CALLBACK:
419 return "OC_STACK_INVALID_CALLBACK";
420 case OC_STACK_INVALID_METHOD:
421 return "OC_STACK_INVALID_METHOD";
422 case OC_STACK_NO_MEMORY:
423 return "OC_STACK_NO_MEMORY";
424 case OC_STACK_COMM_ERROR:
425 return "OC_STACK_COMM_ERROR";
426 case OC_STACK_INVALID_PARAM:
427 return "OC_STACK_INVALID_PARAM";
428 case OC_STACK_NOTIMPL:
429 return "OC_STACK_NOTIMPL";
430 case OC_STACK_NO_RESOURCE:
431 return "OC_STACK_NO_RESOURCE";
432 case OC_STACK_RESOURCE_ERROR:
433 return "OC_STACK_RESOURCE_ERROR";
434 case OC_STACK_SLOW_RESOURCE:
435 return "OC_STACK_SLOW_RESOURCE";
436 case OC_STACK_NO_OBSERVERS:
437 return "OC_STACK_NO_OBSERVERS";
439 return "OC_STACK_ERROR";