1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
32 const char *getResult(OCStackResult result);
34 #define TAG PCF("ocservercontainer")
36 volatile sig_atomic_t gQuitFlag = 0;
37 int gLightUnderObservation = 0;
38 void createResources();
39 typedef struct LIGHTRESOURCE{
40 OCResourceHandle handle;
45 static LightResource light;
47 // TODO : hard coded for now, change after Sprint10
48 const char rspGetRoomDefault[] = "{\"href\":\"/a/room\",\"rep\":{\"name\":\"John's Room\"}}";
49 const char rspGetRoomCollection[] = "{\"href\":\"/a/room\"}";
50 // TODO : Needs to be changed to retrieve current status of room and return that in response
51 const char rspPutRoomDefault[] = "{\"href\":\"/a/room\",\"rep\":{\"name\":\"John's Room\"}}";
52 const char rspPutRoomCollection[] = "{\"href\":\"/a/room\"}";
53 const char rspFailureRoom[] = "{\"href\":\"/a/room\",\"rep\":{\"error\":\"ROOM_OP_FAIL\"}}";
55 // TODO : hard coded for now, change after Sprint4
56 const char rspGetLightDefault[] =
57 "{\"href\":\"/a/light\",\"rep\":{\"state\":\"false\",\"color\":\"0\"}}";
58 const char rspGetLightCollection[] = "{\"href\":\"/a/light\"}";
59 // TODO : Needs to be changed to retrieve current status of light and return that in response
60 const char rspPutLightDefault[] =
61 "{\"href\":\"/a/light\",\"rep\":{\"state\":\"true\",\"color\":\"0\"}}";
62 const char rspPutLightCollection[] = "{\"href\":\"/a/light\"}";
63 const char rspFailureLight[] = "{\"href\":\"/a/light\",\"rep\":{\"error\":\"LIGHT_OP_FAIL\"}}";
66 // TODO : hard coded for now, change after Sprint4
67 const char rspGetFanDefault[] = "{\"href\":\"/a/fan\",\"rep\":{\"state\":\"true\",\"speed\":10}}";
68 const char rspGetFanCollection[] = "{\"href\":\"/a/fan\"}";
69 // TODO : Needs to be changed to retrieve current status of fan and return that in response
70 const char rspPutFanDefault[] = "{\"href\":\"/a/fan\",\"rep\":{\"state\":\"false\",\"speed\":0}}";
71 const char rspPutFanCollection[] = "{\"href\":\"/a/fan\"}";
72 const char rspFailureFan[] = "{\"href\":\"/a/fan\",\"rep\":{\"error\":\"FAN_OP_FAIL\"}}";
84 OC_LOG(INFO, TAG, "Usage : ocservercoll -t <Test Case>");
86 "Test Case 1 : Create room resource with default collection entity handler.");
88 "Test Case 2 : Create room resource with application collection entity handler.");
91 unsigned static int TEST = TEST_INVALID;
93 static OCEntityHandlerResult
94 HandleCallback(OCEntityHandlerRequest * ehRequest,
98 uint16_t maxPayloadSize)
100 OCEntityHandlerResult ret = OC_EH_OK;
102 // Append opStr or errStr, after making sure there is
103 // enough room in the payload
104 if (strlen(opStr) < (maxPayloadSize - strlen(payload)))
106 strncat((char*)payload, opStr, strlen(opStr));
108 else if (strlen(errStr) < (maxPayloadSize - strlen(payload)))
110 strncat((char*)payload, errStr, strlen(errStr));
122 PrintReceivedMsgInfo(OCEntityHandlerFlag flag, OCEntityHandlerRequest * ehRequest)
124 const char* typeOfMessage;
128 case OC_REQUEST_FLAG:
129 typeOfMessage = "OC_REQUEST_FLAG";
131 case OC_OBSERVE_FLAG:
132 typeOfMessage = "OC_OBSERVE_FLAG";
135 typeOfMessage = "UNKNOWN";
138 OC_LOG_V(INFO, TAG, "Receiving message type: %s, method %s",
140 (ehRequest->method == OC_REST_GET) ? "OC_REST_GET" : "OC_REST_PUT" );
143 OCEntityHandlerResult OCEntityHandlerRoomCb(OCEntityHandlerFlag flag,
144 OCEntityHandlerRequest * ehRequest,
147 OCEntityHandlerResult ret = OC_EH_OK;
148 OCEntityHandlerResponse response;
149 char payload[MAX_RESPONSE_LENGTH] = {0};
151 OC_LOG_V(INFO, TAG, "Callback for Room");
152 PrintReceivedMsgInfo(flag, ehRequest );
154 if(ehRequest && flag == OC_REQUEST_FLAG )
156 std::string query = (const char*)ehRequest->query;
158 if(OC_REST_GET == ehRequest->method)
160 if(query.find(OC_RSRVD_INTERFACE_DEFAULT) != std::string::npos)
162 ret = HandleCallback(ehRequest,
163 rspGetRoomDefault, rspFailureRoom, payload, sizeof(payload));
164 if(ret != OC_EH_ERROR)
166 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
167 ret = HandleCallback(ehRequest,
168 rspGetLightCollection, rspFailureLight, payload, sizeof(payload));
170 if(ret != OC_EH_ERROR)
172 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
173 ret = HandleCallback(ehRequest,
174 rspGetFanCollection, rspFailureFan, payload, sizeof(payload));
177 else if(query.find(OC_RSRVD_INTERFACE_LL) != std::string::npos)
179 ret = HandleCallback(ehRequest,
180 rspGetRoomCollection, rspFailureRoom, payload, sizeof(payload));
181 if(ret != OC_EH_ERROR)
183 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
184 ret = HandleCallback(ehRequest,
185 rspGetLightCollection, rspFailureLight, payload, sizeof(payload));
187 if(ret != OC_EH_ERROR)
189 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
190 ret = HandleCallback(ehRequest,
191 rspGetFanCollection, rspFailureFan, payload, sizeof(payload));
194 else if(query.find(OC_RSRVD_INTERFACE_BATCH) != std::string::npos)
196 ret = HandleCallback(ehRequest,
197 rspGetRoomCollection, rspFailureRoom, payload, sizeof(payload));
198 if(ret != OC_EH_ERROR)
200 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
201 ret = HandleCallback(ehRequest,
202 rspGetLightDefault, rspFailureLight, payload, sizeof(payload));
204 if(ret != OC_EH_ERROR)
206 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
207 ret = HandleCallback(ehRequest,
208 rspGetFanDefault, rspFailureFan, payload, sizeof(payload));
213 // Format the response. Note this requires some info about the request
214 response.requestHandle = ehRequest->requestHandle;
215 response.resourceHandle = ehRequest->resource;
216 response.ehResult = ret;
217 response.payload = payload;
218 response.payloadSize = strlen(payload);
219 response.numSendVendorSpecificHeaderOptions = 0;
220 memset(response.sendVendorSpecificHeaderOptions,
221 0, sizeof response.sendVendorSpecificHeaderOptions);
222 memset(response.resourceUri, 0, sizeof response.resourceUri);
223 // Indicate that response is NOT in a persistent buffer
224 response.persistentBufferFlag = 0;
226 if (OCDoResponse(&response) != OC_STACK_OK)
228 OC_LOG(ERROR, TAG, "Error sending response");
233 else if(OC_REST_PUT == ehRequest->method)
235 if(query.find(OC_RSRVD_INTERFACE_DEFAULT) != std::string::npos)
237 if(ret != OC_EH_ERROR)
239 ret = HandleCallback(ehRequest,
240 rspPutRoomDefault, rspFailureRoom, payload, sizeof(payload));
243 if(query.find(OC_RSRVD_INTERFACE_LL) != std::string::npos)
245 if(ret != OC_EH_ERROR)
247 ret = HandleCallback(ehRequest,
248 rspPutRoomCollection, rspFailureRoom, payload, sizeof(payload));
250 if(ret != OC_EH_ERROR)
252 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
253 ret = HandleCallback(ehRequest,
254 rspPutLightCollection, rspFailureLight, payload, sizeof(payload));
256 if(ret != OC_EH_ERROR)
258 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
259 ret = HandleCallback(ehRequest,
260 rspPutFanCollection, rspFailureFan, payload, sizeof(payload));
263 if(query.find(OC_RSRVD_INTERFACE_BATCH ) != std::string::npos)
265 if(ret != OC_EH_ERROR)
267 ret = HandleCallback(ehRequest,
268 rspPutRoomCollection, rspFailureRoom, payload, sizeof(payload));
270 if(ret != OC_EH_ERROR)
272 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
273 ret = HandleCallback(ehRequest,
274 rspPutLightDefault, rspFailureLight, payload, sizeof(payload));
276 if(ret != OC_EH_ERROR)
278 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
279 ret = HandleCallback(ehRequest,
280 rspPutFanDefault, rspFailureFan, payload, sizeof(payload));
285 // Format the response. Note this requires some info about the request
286 response.requestHandle = ehRequest->requestHandle;
287 response.resourceHandle = ehRequest->resource;
288 response.ehResult = ret;
289 response.payload = payload;
290 response.payloadSize = strlen(payload);
291 response.numSendVendorSpecificHeaderOptions = 0;
292 memset(response.sendVendorSpecificHeaderOptions,
293 0, sizeof response.sendVendorSpecificHeaderOptions);
294 memset(response.resourceUri, 0, sizeof response.resourceUri);
295 // Indicate that response is NOT in a persistent buffer
296 response.persistentBufferFlag = 0;
298 if (OCDoResponse(&response) != OC_STACK_OK)
300 OC_LOG(ERROR, TAG, "Error sending response");
307 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
312 else if (ehRequest && flag == OC_OBSERVE_FLAG)
314 gLightUnderObservation = 1;
319 OCEntityHandlerResult OCEntityHandlerLightCb(OCEntityHandlerFlag flag,
320 OCEntityHandlerRequest * ehRequest,void* callbackParam)
322 OCEntityHandlerResult ret = OC_EH_OK;
323 OCEntityHandlerResponse response;
324 char payload[MAX_RESPONSE_LENGTH] = {0};
326 OC_LOG_V(INFO, TAG, "Callback for Light");
327 PrintReceivedMsgInfo(flag, ehRequest );
329 if(ehRequest && flag == OC_REQUEST_FLAG)
331 if(OC_REST_GET == ehRequest->method)
333 ret = HandleCallback(ehRequest,
334 rspGetLightDefault, rspFailureLight, payload, sizeof(payload));
336 else if(OC_REST_PUT == ehRequest->method)
338 ret = HandleCallback(ehRequest,
339 rspPutLightDefault, rspFailureLight, payload, sizeof(payload));
343 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
350 // Format the response. Note this requires some info about the request
351 response.requestHandle = ehRequest->requestHandle;
352 response.resourceHandle = ehRequest->resource;
353 response.ehResult = ret;
354 response.payload = payload;
355 response.payloadSize = strlen(payload);
356 response.numSendVendorSpecificHeaderOptions = 0;
357 memset(response.sendVendorSpecificHeaderOptions,
358 0, sizeof response.sendVendorSpecificHeaderOptions);
359 memset(response.resourceUri, 0, sizeof response.resourceUri);
360 // Indicate that response is NOT in a persistent buffer
361 response.persistentBufferFlag = 0;
364 if (OCDoResponse(&response) != OC_STACK_OK)
366 OC_LOG(ERROR, TAG, "Error sending response");
371 else if (ehRequest && flag == OC_OBSERVE_FLAG)
373 gLightUnderObservation = 1;
379 OCEntityHandlerResult OCEntityHandlerFanCb(OCEntityHandlerFlag flag,
380 OCEntityHandlerRequest * ehRequest, void* callback)
382 OCEntityHandlerResult ret = OC_EH_OK;
383 OCEntityHandlerResponse response;
384 char payload[MAX_RESPONSE_LENGTH] = {0};
386 OC_LOG_V(INFO, TAG, "Callback for Fan");
387 PrintReceivedMsgInfo(flag, ehRequest );
389 if(ehRequest && flag == OC_REQUEST_FLAG)
391 if(OC_REST_GET == ehRequest->method)
393 ret = HandleCallback(ehRequest, rspGetFanDefault,
394 rspFailureFan, payload, sizeof(payload));
396 else if(OC_REST_PUT == ehRequest->method)
398 ret = HandleCallback(ehRequest, rspPutFanDefault,
399 rspFailureFan, payload, sizeof(payload));
403 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
410 // Format the response. Note this requires some info about the request
411 response.requestHandle = ehRequest->requestHandle;
412 response.resourceHandle = ehRequest->resource;
413 response.ehResult = ret;
414 response.payload = payload;
415 response.payloadSize = strlen(payload);
416 response.numSendVendorSpecificHeaderOptions = 0;
417 memset(response.sendVendorSpecificHeaderOptions,
418 0, sizeof response.sendVendorSpecificHeaderOptions);
419 memset(response.resourceUri, 0, sizeof response.resourceUri);
420 // Indicate that response is NOT in a persistent buffer
421 response.persistentBufferFlag = 0;
424 if (OCDoResponse(&response) != OC_STACK_OK)
426 OC_LOG(ERROR, TAG, "Error sending response");
432 else if (ehRequest && flag == OC_OBSERVE_FLAG)
434 gLightUnderObservation = 1;
440 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
441 void handleSigInt(int signum)
443 if (signum == SIGINT)
449 void *ChangeLightRepresentation (void *param)
452 OCStackResult result = OC_STACK_ERROR;
458 if (gLightUnderObservation)
461 " =====> Notifying stack of new power level %d\n", light.power);
462 result = OCNotifyAllObservers (light.handle, OC_NA_QOS);
463 if (OC_STACK_NO_OBSERVERS == result)
465 gLightUnderObservation = 0;
472 int main(int argc, char* argv[])
477 while ((opt = getopt(argc, argv, "t:")) != -1)
489 if(TEST <= TEST_INVALID || TEST >= MAX_TESTS)
495 OC_LOG(DEBUG, TAG, "OCServer is starting...");
497 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
499 OC_LOG(ERROR, TAG, "OCStack init error");
504 * Declare and create the example resource: light
509 * Create a thread for changing the representation of the light
511 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);
513 // Break from loop with Ctrl-C
514 OC_LOG(INFO, TAG, "Entering ocserver main loop...");
515 signal(SIGINT, handleSigInt);
518 if (OCProcess() != OC_STACK_OK)
520 OC_LOG(ERROR, TAG, "OCStack process error");
527 * Cancel the light thread and wait for it to terminate
529 pthread_cancel(threadId);
530 pthread_join(threadId, NULL);
532 OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
534 if (OCStop() != OC_STACK_OK)
536 OC_LOG(ERROR, TAG, "OCStack process error");
541 void createResources()
545 OCResourceHandle fan;
546 OCStackResult res = OCCreateResource(&fan,
548 OC_RSRVD_INTERFACE_DEFAULT,
550 OCEntityHandlerFanCb,
552 OC_DISCOVERABLE|OC_OBSERVABLE);
553 OC_LOG_V(INFO, TAG, "Created fan resource with result: %s", getResult(res));
555 OCResourceHandle light;
556 res = OCCreateResource(&light,
558 OC_RSRVD_INTERFACE_DEFAULT,
560 OCEntityHandlerLightCb,
562 OC_DISCOVERABLE|OC_OBSERVABLE);
563 OC_LOG_V(INFO, TAG, "Created light resource with result: %s", getResult(res));
565 OCResourceHandle room;
567 if(TEST == TEST_APP_COLL_EH)
569 res = OCCreateResource(&room,
571 OC_RSRVD_INTERFACE_BATCH,
573 OCEntityHandlerRoomCb,
579 res = OCCreateResource(&room,
581 OC_RSRVD_INTERFACE_BATCH,
588 OC_LOG_V(INFO, TAG, "Created room resource with result: %s", getResult(res));
589 OCBindResourceInterfaceToResource(room, OC_RSRVD_INTERFACE_LL);
590 OCBindResourceInterfaceToResource(room, OC_RSRVD_INTERFACE_DEFAULT);
592 res = OCBindResource(room, light);
593 OC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));
595 res = OCBindResource(room, fan);
596 OC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));