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"
24 PROGMEM const char TAG[] = "resourceHandler";
29 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag, OCEntityHandlerRequest *);
30 const char *getResult(OCStackResult result);
32 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, char *payload,
33 uint16_t maxPayloadSize);
34 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, char *payload,
35 uint16_t maxPayloadSize);
36 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, char *payload,
37 uint16_t maxPayloadSize);
38 char* constructJsonResponse(OCEntityHandlerRequest *ehRequest);
42 ResourceEventCallback g_cbForResEvent = NULL;
44 void RegisterResourceEventCallBack(ResourceEventCallback cb)
49 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
51 if (name != NULL && pass != NULL)
53 sprintf(name, "%s", g_prov.tnn);
54 sprintf(pass, "%s", g_prov.cd);
58 OCStackResult CreateProvisioningResource()
60 g_prov.ps = 1; // need to provisioning
62 sprintf(g_prov.tnn, "Unknown");
63 sprintf(g_prov.cd, "Unknown");
65 OCStackResult res = OCCreateResource(&g_prov.handle, "oic.prov", OC_RSRVD_INTERFACE_DEFAULT,
66 "/oic/prov", OCEntityHandlerCb, OC_DISCOVERABLE | OC_OBSERVABLE);
68 OC_LOG_V(INFO, TAG, "Created Prov resource with result: %s", getResult(res));
73 OCStackResult CreateNetworkResource()
77 if (getCurrentNetworkInfo(ES_WIFI, &netInfo) != 0)
79 return OC_STACK_ERROR;
82 if (netInfo.type != ES_WIFI)
84 return OC_STACK_ERROR;
87 g_net.cnt = (int) netInfo.type;
88 g_net.ant[0] = (int) ES_WIFI;
89 sprintf(g_net.ipaddr, "%d.%d.%d.%d", netInfo.ipaddr[0], netInfo.ipaddr[1], netInfo.ipaddr[2],
91 sprintf(g_net.cnn, "%s", netInfo.ssid);
93 OC_LOG_V(INFO, TAG, "SSID: %s", g_net.cnn);
94 OC_LOG_V(INFO, TAG, "IP Address: %s", g_net.ipaddr);
96 OCStackResult res = OCCreateResource(&g_net.handle, "oic.net", OC_RSRVD_INTERFACE_DEFAULT,
97 "/oic/net", OCEntityHandlerCb, OC_DISCOVERABLE | OC_OBSERVABLE);
98 OC_LOG_V(INFO, TAG, "Created Net resource with result: %s", getResult(res));
103 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, char *payload,
104 uint16_t maxPayloadSize)
106 OCEntityHandlerResult ehResult = OC_EH_ERROR;
108 char *getResp = constructJsonResponse(ehRequest);
112 OC_LOG(ERROR, TAG, "constructJsonResponse failed");
116 if (MAX_RESPONSE_LENGTH > strlen(getResp))
118 strncpy(payload, getResp, strlen(getResp));
123 OC_LOG_V(INFO, TAG, "Response buffer: %d bytes is too small", maxPayloadSize);
124 ehResult = OC_EH_ERROR;
132 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, char *payload,
133 uint16_t maxPayloadSize)
135 OCEntityHandlerResult ehResult = OC_EH_ERROR;
137 OC_LOG_V(INFO, TAG, "PUT Request Payload: %s", ehRequest->reqJSONPayload);
139 // Get cJSON pointer to query
140 cJSON *putJson = cJSON_Parse(ehRequest->reqJSONPayload);
143 OC_LOG_V(ERROR, TAG, "Failed to parse JSON: %s", ehRequest->reqJSONPayload);
147 // Get root of JSON payload, then the 1st resource.
148 cJSON* carrier = cJSON_GetObjectItem(putJson, "oic");
151 OC_LOG_V(ERROR, TAG, "Failed to parse JSON: %s", ehRequest->reqJSONPayload);
154 carrier = cJSON_GetArrayItem(carrier, 0);
155 carrier = cJSON_GetObjectItem(carrier, "rep");
158 OC_LOG_V(ERROR, TAG, "Failed to parse JSON: %s", ehRequest->reqJSONPayload);
162 cJSON* prop = cJSON_GetObjectItem(carrier, "tnn");
165 sprintf(g_prov.tnn, "%s", prop->valuestring);
168 prop = cJSON_GetObjectItem(carrier, "cd");
171 sprintf(g_prov.cd, "%s", prop->valuestring);
174 cJSON_Delete(putJson);
176 // TODO: Now once receiving PUT request, the enrollee start to connect to enroller.
179 char *getResp = constructJsonResponse(ehRequest);
183 OC_LOG(ERROR, TAG, "constructJsonResponse failed");
187 if (MAX_RESPONSE_LENGTH > strlen(getResp))
189 strncpy(payload, getResp, strlen(getResp));
194 OC_LOG_V(INFO, TAG, "Response buffer: %d bytes is too small", maxPayloadSize);
195 ehResult = OC_EH_ERROR;
202 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, char *payload,
203 uint16_t maxPayloadSize)
205 OCEntityHandlerResult ehResult = OC_EH_ERROR;
206 OC_LOG_V(INFO, TAG, "PUT Request Payload: %s", ehRequest->reqJSONPayload);
208 // Get cJSON pointer to query
209 cJSON *putJson = cJSON_Parse(ehRequest->reqJSONPayload);
213 OC_LOG_V(ERROR, TAG, "Failed to parse JSON: %s", ehRequest->reqJSONPayload);
217 // Get root of JSON payload, then the 1st resource.
218 cJSON* carrier = cJSON_GetObjectItem(putJson, "oic");
221 OC_LOG_V(ERROR, TAG, "Failed to parse JSON: %s", ehRequest->reqJSONPayload);
224 carrier = cJSON_GetArrayItem(carrier, 0);
225 carrier = cJSON_GetObjectItem(carrier, "rep");
228 OC_LOG_V(ERROR, TAG, "Failed to parse JSON: %s", ehRequest->reqJSONPayload);
232 cJSON* prop = cJSON_GetObjectItem(carrier, "tr");
239 cJSON_Delete(putJson);
246 char* constructJsonResponse(OCEntityHandlerRequest *ehRequest)
248 cJSON *json = cJSON_CreateObject();
254 if (g_prov.handle != NULL && ehRequest->resource == g_prov.handle)
256 char *uri = (char *) "/oic/prov";
258 cJSON_AddStringToObject(json, "href", uri);
259 cJSON_AddItemToObject(json, "rep", format = cJSON_CreateObject());
260 cJSON_AddNumberToObject(format, "ps", g_prov.ps);
261 cJSON_AddNumberToObject(format, "tnt", g_prov.tnt);
262 cJSON_AddStringToObject(format, "tnn", g_prov.tnn);
263 cJSON_AddStringToObject(format, "cd", g_prov.cd);
265 jsonResponse = cJSON_Print(json);
268 else if (g_net.handle != NULL && ehRequest->requestHandle == g_net.handle)
270 char *uri = (char *) "/oic/net";
272 cJSON_AddStringToObject(json, "href", uri);
273 cJSON_AddItemToObject(json, "rep", format = cJSON_CreateObject());
274 cJSON_AddNumberToObject(format, "ant", g_net.ant[0]);
276 jsonResponse = cJSON_Print(json);
284 OC_LOG_V(INFO, TAG, "Constructed Response: %s", jsonResponse);
289 // This is the entity handler for the registered resource.
290 // This is invoked by OCStack whenever it recevies a request for this resource.
292 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
293 OCEntityHandlerRequest* entityHandlerRequest)
295 OCEntityHandlerResult ehRet = OC_EH_OK;
296 OCEntityHandlerResponse response =
298 char payload[MAX_RESPONSE_LENGTH] =
301 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
303 if (OC_REST_GET == entityHandlerRequest->method)
305 OC_LOG_V(INFO, TAG, "Received GET request");
306 ehRet = ProcessGetRequest(entityHandlerRequest, payload, sizeof(payload) - 1);
308 else if (OC_REST_PUT == entityHandlerRequest->method)
310 OC_LOG_V(INFO, TAG, "Received PUT request");
312 if (g_prov.handle != NULL && entityHandlerRequest->resource == g_prov.handle)
314 ehRet = ProcessPutRequest(entityHandlerRequest, payload, sizeof(payload) - 1);
321 else if (OC_REST_POST == entityHandlerRequest->method)
323 // TODO: As of now, POST request will be not received.
324 OC_LOG(INFO, TAG, "Received OC_REST_POST from client");
325 //ehRet = ProcessPostRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
328 if (ehRet == OC_EH_OK)
330 // Format the response. Note this requires some info about the request
331 response.requestHandle = entityHandlerRequest->requestHandle;
332 response.resourceHandle = entityHandlerRequest->resource;
333 response.ehResult = ehRet;
334 response.payload = payload;
335 response.payloadSize = strlen(payload);
336 response.numSendVendorSpecificHeaderOptions = 0;
337 memset(response.sendVendorSpecificHeaderOptions, 0,
338 sizeof response.sendVendorSpecificHeaderOptions);
339 memset(response.resourceUri, 0, sizeof response.resourceUri);
340 // Indicate that response is NOT in a persistent buffer
341 response.persistentBufferFlag = 0;
344 if (OCDoResponse(&response) != OC_STACK_OK)
346 OC_LOG(ERROR, TAG, "Error sending response");
354 g_cbForResEvent(ES_RECVTRIGGEROFPROVRES);
361 const char *getResult(OCStackResult result)
366 return "OC_STACK_OK";
367 case OC_STACK_INVALID_URI:
368 return "OC_STACK_INVALID_URI";
369 case OC_STACK_INVALID_QUERY:
370 return "OC_STACK_INVALID_QUERY";
371 case OC_STACK_INVALID_IP:
372 return "OC_STACK_INVALID_IP";
373 case OC_STACK_INVALID_PORT:
374 return "OC_STACK_INVALID_PORT";
375 case OC_STACK_INVALID_CALLBACK:
376 return "OC_STACK_INVALID_CALLBACK";
377 case OC_STACK_INVALID_METHOD:
378 return "OC_STACK_INVALID_METHOD";
379 case OC_STACK_NO_MEMORY:
380 return "OC_STACK_NO_MEMORY";
381 case OC_STACK_COMM_ERROR:
382 return "OC_STACK_COMM_ERROR";
383 case OC_STACK_INVALID_PARAM:
384 return "OC_STACK_INVALID_PARAM";
385 case OC_STACK_NOTIMPL:
386 return "OC_STACK_NOTIMPL";
387 case OC_STACK_NO_RESOURCE:
388 return "OC_STACK_NO_RESOURCE";
389 case OC_STACK_RESOURCE_ERROR:
390 return "OC_STACK_RESOURCE_ERROR";
391 case OC_STACK_SLOW_RESOURCE:
392 return "OC_STACK_SLOW_RESOURCE";
393 case OC_STACK_NO_OBSERVERS:
394 return "OC_STACK_NO_OBSERVERS";
396 return "OC_STACK_ERROR";