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"
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
37 * @brief Structure for holding the Provisioning status and target information required to connect to the target network
39 static ProvResource gProvResource;
43 * @brief Structure forr holding the Provisioning status of network information
45 static NetResource gNetResource;
47 //-----------------------------------------------------------------------------
48 // Private internal function prototypes
49 //-----------------------------------------------------------------------------
50 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
51 OCEntityHandlerRequest *ehRequest, void *callback);
52 const char *getResult(OCStackResult result);
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);
61 static int g_flag = 0;
63 ResourceEventCallback gNetworkInfoProvEventCb = NULL;
65 void RegisterResourceEventCallBack(ResourceEventCallback cb)
67 gNetworkInfoProvEventCb = cb;
70 void UnRegisterResourceEventCallBack()
72 if (gNetworkInfoProvEventCb)
74 gNetworkInfoProvEventCb = NULL;
78 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
80 if (name != NULL && pass != NULL)
82 sprintf(name, "%s", gProvResource.tnn);
83 sprintf(pass, "%s", gProvResource.cd);
87 OCStackResult CreateProvisioningResource()
89 gProvResource.ps = 1; // need to do provisioning
90 gProvResource.tnt = CT_ADAPTER_IP;
91 sprintf(gProvResource.tnn, "Unknown");
92 sprintf(gProvResource.cd, "Unknown");
94 OCStackResult res = OCCreateResource(&gProvResource.handle, "oic.r.prov", OC_RSRVD_INTERFACE_DEFAULT,
95 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb, NULL,
96 OC_DISCOVERABLE | OC_OBSERVABLE);
97 OC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
101 OCStackResult DeleteProvisioningResource()
103 OCStackResult res = OCDeleteResource(gProvResource.handle);
104 if (res != OC_STACK_OK)
106 OC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
113 OCStackResult CreateNetworkResource()
117 if (getCurrentNetworkInfo(CT_ADAPTER_IP, &netInfo) != ES_OK)
119 return OC_STACK_ERROR;
122 if (netInfo.type != CT_ADAPTER_IP)
124 return OC_STACK_ERROR;
127 gNetResource.cnt = (int) netInfo.type;
128 gNetResource.ant[0] = (int) CT_ADAPTER_IP;
130 if(netInfo.ipaddr != NULL)
132 sprintf(gNetResource.ipaddr, "%d.%d.%d.%d", netInfo.ipaddr[0], netInfo.ipaddr[1],
133 netInfo.ipaddr[2], netInfo.ipaddr[3]);
135 sprintf(gNetResource.cnn, "%s", netInfo.ssid);
137 OC_LOG_V(INFO, ES_RH_TAG, "SSID: %s", gNetResource.cnn);
138 OC_LOG_V(INFO, ES_RH_TAG, "IP Address: %s", gNetResource.ipaddr);
140 OCStackResult res = OCCreateResource(&gNetResource.handle, "oic.r.net", OC_RSRVD_INTERFACE_DEFAULT,
141 OC_RSRVD_ES_URI_NET, OCEntityHandlerCb,NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
143 OC_LOG_V(INFO, ES_RH_TAG, "Created Net resource with result: %s", getResult(res));
148 OCStackResult DeleteNetworkResource()
150 OCStackResult res = OCDeleteResource(gNetResource.handle);
151 if (res != OC_STACK_OK)
153 OC_LOG_V(INFO, ES_RH_TAG, "Deleting Network resource error with result: %s",
161 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
162 OCRepPayload **payload)
164 OCEntityHandlerResult ehResult = OC_EH_ERROR;
167 OC_LOG(ERROR, ES_RH_TAG, "Request is Null");
170 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
172 OC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
176 OCRepPayload *getResp = constructResponse(ehRequest);
179 OC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
189 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
190 OCRepPayload** payload)
192 OC_LOG(INFO, ES_RH_TAG, "ProcessPutRequest enter");
193 OCEntityHandlerResult ehResult = OC_EH_ERROR;
194 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
196 OC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
200 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
203 OC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
207 //TODO : ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
208 // A new request for provisioning means overriding existing network provisioning information.
209 // Metadata to indicate that it is override is needed. The metadata can be a new attribute
210 // should be added to the /oic/prov resource indicating to override the existing network
212 if (gProvResource.ps == ES_PS_PROVISIONING_COMPLETED)
214 OC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed. "
215 "This a request to override the existing the network provisioning information");
218 // PUT request is appropriate for provisioning information to the enrollee.
219 // When an enrollee receives the put request, the entire resource information should
221 sprintf(gProvResource.tnn, "%s", "");
222 sprintf(gProvResource.tnn, "%s", "");
225 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn))
227 sprintf(gProvResource.tnn, "%s", tnn);
228 OC_LOG(INFO, ES_RH_TAG, "got ssid");
231 OC_LOG_V(INFO, ES_RH_TAG, "gProvResource.tnn %s", gProvResource.tnn);
233 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
235 sprintf(gProvResource.cd, "%s", cd);
236 OC_LOG(INFO, ES_RH_TAG, "got password");
238 OC_LOG_V(INFO, ES_RH_TAG, "gProvResource.cd %s", gProvResource.cd);
239 gProvResource.ps = 2;
240 OC_LOG_V(INFO, ES_RH_TAG, "gProvResource.ps %d", gProvResource.ps);
243 OCRepPayload *getResp = constructResponse(ehRequest);
246 OC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
256 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
257 OCRepPayload** payload)
259 OCEntityHandlerResult ehResult = OC_EH_ERROR;
262 OC_LOG(ERROR, ES_RH_TAG, "Request is Null");
265 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
267 OC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
271 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
274 OC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
278 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TR, &tr))
288 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest)
290 OCRepPayload* payload = OCRepPayloadCreate();
293 OC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
297 if (ehRequest->resource == gProvResource.handle)
299 OC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
300 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
301 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PS, gProvResource.ps);
302 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_TNT, gProvResource.tnt);
303 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_TNN, gProvResource.tnn);
304 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CD, gProvResource.cd);
306 else if (ehRequest->requestHandle == gNetResource.handle)
309 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_NET);
310 OCRepPayloadSetPropInt(payload, "ant", gNetResource.ant[0]);
316 * This is the entity handler for the registered resource.
317 * This is invoked by OCStack whenever it recevies a request for this resource.
319 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
320 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
323 OCEntityHandlerResult ehRet = OC_EH_OK;
324 OCEntityHandlerResponse response = { 0 };
325 OCRepPayload* payload = NULL;
326 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
328 if (OC_REST_GET == entityHandlerRequest->method)
330 OC_LOG(INFO, ES_RH_TAG, "Received GET request");
331 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
333 else if (OC_REST_PUT == entityHandlerRequest->method)
335 OC_LOG(INFO, ES_RH_TAG, "Received PUT request");
337 if (gProvResource.handle != NULL && entityHandlerRequest->resource == gProvResource.handle)
339 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
343 OC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
347 else if (OC_REST_POST == entityHandlerRequest->method)
349 // TODO: As of now, POST request will be not received.
350 OC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
351 //ehRet = ProcessPostRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
354 if (ehRet == OC_EH_OK)
356 // Format the response. Note this requires some info about the request
357 response.requestHandle = entityHandlerRequest->requestHandle;
358 response.resourceHandle = entityHandlerRequest->resource;
359 response.ehResult = ehRet;
360 //response uses OCPaylod while all get,put methodes use OCRepPayload
361 response.payload = (OCPayload*) (payload);
362 response.numSendVendorSpecificHeaderOptions = 0;
363 memset(response.sendVendorSpecificHeaderOptions, 0,
364 sizeof response.sendVendorSpecificHeaderOptions);
365 memset(response.resourceUri, 0, sizeof response.resourceUri);
366 // Indicate that response is NOT in a persistent buffer
367 response.persistentBufferFlag = 0;
370 if (OCDoResponse(&response) != OC_STACK_OK)
372 OC_LOG(ERROR, ES_RH_TAG, "Error sending response");
380 gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES);
387 const char *getResult(OCStackResult result)
392 return "OC_STACK_OK";
393 case OC_STACK_INVALID_URI:
394 return "OC_STACK_INVALID_URI";
395 case OC_STACK_INVALID_QUERY:
396 return "OC_STACK_INVALID_QUERY";
397 case OC_STACK_INVALID_IP:
398 return "OC_STACK_INVALID_IP";
399 case OC_STACK_INVALID_PORT:
400 return "OC_STACK_INVALID_PORT";
401 case OC_STACK_INVALID_CALLBACK:
402 return "OC_STACK_INVALID_CALLBACK";
403 case OC_STACK_INVALID_METHOD:
404 return "OC_STACK_INVALID_METHOD";
405 case OC_STACK_NO_MEMORY:
406 return "OC_STACK_NO_MEMORY";
407 case OC_STACK_COMM_ERROR:
408 return "OC_STACK_COMM_ERROR";
409 case OC_STACK_INVALID_PARAM:
410 return "OC_STACK_INVALID_PARAM";
411 case OC_STACK_NOTIMPL:
412 return "OC_STACK_NOTIMPL";
413 case OC_STACK_NO_RESOURCE:
414 return "OC_STACK_NO_RESOURCE";
415 case OC_STACK_RESOURCE_ERROR:
416 return "OC_STACK_RESOURCE_ERROR";
417 case OC_STACK_SLOW_RESOURCE:
418 return "OC_STACK_SLOW_RESOURCE";
419 case OC_STACK_NO_OBSERVERS:
420 return "OC_STACK_NO_OBSERVERS";
422 return "OC_STACK_ERROR";