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;
125 const char* typeOfMethod;
129 case OC_REQUEST_FLAG:
130 typeOfMessage = "OC_REQUEST_FLAG";
132 case OC_OBSERVE_FLAG:
133 typeOfMessage = "OC_OBSERVE_FLAG";
136 typeOfMessage = "UNKNOWN";
139 if (ehRequest == NULL)
141 typeOfMethod = "UNKNOWN";
143 else if (ehRequest->method == OC_REST_GET)
145 typeOfMethod = "OC_REST_GET";
149 typeOfMethod = "OC_REST_PUT";
152 OC_LOG_V(INFO, TAG, "Receiving message type: %s, method %s", typeOfMessage,
156 OCEntityHandlerResult OCEntityHandlerRoomCb(OCEntityHandlerFlag flag,
157 OCEntityHandlerRequest * ehRequest,
160 OCEntityHandlerResult ret = OC_EH_OK;
161 OCEntityHandlerResponse response;
162 char payload[MAX_RESPONSE_LENGTH] = {0};
164 OC_LOG_V(INFO, TAG, "Callback for Room");
165 PrintReceivedMsgInfo(flag, ehRequest );
167 if(ehRequest && flag == OC_REQUEST_FLAG )
169 std::string query = (const char*)ehRequest->query;
171 if(OC_REST_GET == ehRequest->method)
173 if(query.find(OC_RSRVD_INTERFACE_DEFAULT) != std::string::npos)
175 ret = HandleCallback(ehRequest,
176 rspGetRoomDefault, rspFailureRoom, payload, sizeof(payload));
177 if(ret != OC_EH_ERROR)
179 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
180 ret = HandleCallback(ehRequest,
181 rspGetLightCollection, rspFailureLight, payload, sizeof(payload));
183 if(ret != OC_EH_ERROR)
185 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
186 ret = HandleCallback(ehRequest,
187 rspGetFanCollection, rspFailureFan, payload, sizeof(payload));
190 else if(query.find(OC_RSRVD_INTERFACE_LL) != std::string::npos)
192 ret = HandleCallback(ehRequest,
193 rspGetRoomCollection, rspFailureRoom, payload, sizeof(payload));
194 if(ret != OC_EH_ERROR)
196 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
197 ret = HandleCallback(ehRequest,
198 rspGetLightCollection, rspFailureLight, payload, sizeof(payload));
200 if(ret != OC_EH_ERROR)
202 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
203 ret = HandleCallback(ehRequest,
204 rspGetFanCollection, rspFailureFan, payload, sizeof(payload));
207 else if(query.find(OC_RSRVD_INTERFACE_BATCH) != std::string::npos)
209 ret = HandleCallback(ehRequest,
210 rspGetRoomCollection, rspFailureRoom, payload, sizeof(payload));
211 if(ret != OC_EH_ERROR)
213 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
214 ret = HandleCallback(ehRequest,
215 rspGetLightDefault, rspFailureLight, payload, sizeof(payload));
217 if(ret != OC_EH_ERROR)
219 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
220 ret = HandleCallback(ehRequest,
221 rspGetFanDefault, rspFailureFan, payload, sizeof(payload));
226 // Format the response. Note this requires some info about the request
227 response.requestHandle = ehRequest->requestHandle;
228 response.resourceHandle = ehRequest->resource;
229 response.ehResult = ret;
230 response.payload = payload;
231 response.payloadSize = strlen(payload);
232 response.numSendVendorSpecificHeaderOptions = 0;
233 memset(response.sendVendorSpecificHeaderOptions,
234 0, sizeof response.sendVendorSpecificHeaderOptions);
235 memset(response.resourceUri, 0, sizeof response.resourceUri);
236 // Indicate that response is NOT in a persistent buffer
237 response.persistentBufferFlag = 0;
239 if (OCDoResponse(&response) != OC_STACK_OK)
241 OC_LOG(ERROR, TAG, "Error sending response");
246 else if(OC_REST_PUT == ehRequest->method)
248 if(query.find(OC_RSRVD_INTERFACE_DEFAULT) != std::string::npos)
250 if(ret != OC_EH_ERROR)
252 ret = HandleCallback(ehRequest,
253 rspPutRoomDefault, rspFailureRoom, payload, sizeof(payload));
256 if(query.find(OC_RSRVD_INTERFACE_LL) != std::string::npos)
258 if(ret != OC_EH_ERROR)
260 ret = HandleCallback(ehRequest,
261 rspPutRoomCollection, rspFailureRoom, payload, sizeof(payload));
263 if(ret != OC_EH_ERROR)
265 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
266 ret = HandleCallback(ehRequest,
267 rspPutLightCollection, rspFailureLight, payload, sizeof(payload));
269 if(ret != OC_EH_ERROR)
271 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
272 ret = HandleCallback(ehRequest,
273 rspPutFanCollection, rspFailureFan, payload, sizeof(payload));
276 if(query.find(OC_RSRVD_INTERFACE_BATCH ) != std::string::npos)
278 if(ret != OC_EH_ERROR)
280 ret = HandleCallback(ehRequest,
281 rspPutRoomCollection, rspFailureRoom, payload, sizeof(payload));
283 if(ret != OC_EH_ERROR)
285 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
286 ret = HandleCallback(ehRequest,
287 rspPutLightDefault, rspFailureLight, payload, sizeof(payload));
289 if(ret != OC_EH_ERROR)
291 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
292 ret = HandleCallback(ehRequest,
293 rspPutFanDefault, rspFailureFan, payload, sizeof(payload));
298 // Format the response. Note this requires some info about the request
299 response.requestHandle = ehRequest->requestHandle;
300 response.resourceHandle = ehRequest->resource;
301 response.ehResult = ret;
302 response.payload = payload;
303 response.payloadSize = strlen(payload);
304 response.numSendVendorSpecificHeaderOptions = 0;
305 memset(response.sendVendorSpecificHeaderOptions,
306 0, sizeof response.sendVendorSpecificHeaderOptions);
307 memset(response.resourceUri, 0, sizeof response.resourceUri);
308 // Indicate that response is NOT in a persistent buffer
309 response.persistentBufferFlag = 0;
311 if (OCDoResponse(&response) != OC_STACK_OK)
313 OC_LOG(ERROR, TAG, "Error sending response");
320 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
325 else if (ehRequest && flag == OC_OBSERVE_FLAG)
327 gLightUnderObservation = 1;
332 OCEntityHandlerResult OCEntityHandlerLightCb(OCEntityHandlerFlag flag,
333 OCEntityHandlerRequest * ehRequest,void* callbackParam)
335 OCEntityHandlerResult ret = OC_EH_OK;
336 OCEntityHandlerResponse response;
337 char payload[MAX_RESPONSE_LENGTH] = {0};
339 OC_LOG_V(INFO, TAG, "Callback for Light");
340 PrintReceivedMsgInfo(flag, ehRequest );
342 if(ehRequest && flag == OC_REQUEST_FLAG)
344 if(OC_REST_GET == ehRequest->method)
346 ret = HandleCallback(ehRequest,
347 rspGetLightDefault, rspFailureLight, payload, sizeof(payload));
349 else if(OC_REST_PUT == ehRequest->method)
351 ret = HandleCallback(ehRequest,
352 rspPutLightDefault, rspFailureLight, payload, sizeof(payload));
356 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
363 // Format the response. Note this requires some info about the request
364 response.requestHandle = ehRequest->requestHandle;
365 response.resourceHandle = ehRequest->resource;
366 response.ehResult = ret;
367 response.payload = payload;
368 response.payloadSize = strlen(payload);
369 response.numSendVendorSpecificHeaderOptions = 0;
370 memset(response.sendVendorSpecificHeaderOptions,
371 0, sizeof response.sendVendorSpecificHeaderOptions);
372 memset(response.resourceUri, 0, sizeof response.resourceUri);
373 // Indicate that response is NOT in a persistent buffer
374 response.persistentBufferFlag = 0;
377 if (OCDoResponse(&response) != OC_STACK_OK)
379 OC_LOG(ERROR, TAG, "Error sending response");
384 else if (ehRequest && flag == OC_OBSERVE_FLAG)
386 gLightUnderObservation = 1;
392 OCEntityHandlerResult OCEntityHandlerFanCb(OCEntityHandlerFlag flag,
393 OCEntityHandlerRequest * ehRequest, void* callback)
395 OCEntityHandlerResult ret = OC_EH_OK;
396 OCEntityHandlerResponse response;
397 char payload[MAX_RESPONSE_LENGTH] = {0};
399 OC_LOG_V(INFO, TAG, "Callback for Fan");
400 PrintReceivedMsgInfo(flag, ehRequest );
402 if(ehRequest && flag == OC_REQUEST_FLAG)
404 if(OC_REST_GET == ehRequest->method)
406 ret = HandleCallback(ehRequest, rspGetFanDefault,
407 rspFailureFan, payload, sizeof(payload));
409 else if(OC_REST_PUT == ehRequest->method)
411 ret = HandleCallback(ehRequest, rspPutFanDefault,
412 rspFailureFan, payload, sizeof(payload));
416 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
423 // Format the response. Note this requires some info about the request
424 response.requestHandle = ehRequest->requestHandle;
425 response.resourceHandle = ehRequest->resource;
426 response.ehResult = ret;
427 response.payload = payload;
428 response.payloadSize = strlen(payload);
429 response.numSendVendorSpecificHeaderOptions = 0;
430 memset(response.sendVendorSpecificHeaderOptions,
431 0, sizeof response.sendVendorSpecificHeaderOptions);
432 memset(response.resourceUri, 0, sizeof response.resourceUri);
433 // Indicate that response is NOT in a persistent buffer
434 response.persistentBufferFlag = 0;
437 if (OCDoResponse(&response) != OC_STACK_OK)
439 OC_LOG(ERROR, TAG, "Error sending response");
445 else if (ehRequest && flag == OC_OBSERVE_FLAG)
447 gLightUnderObservation = 1;
453 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
454 void handleSigInt(int signum)
456 if (signum == SIGINT)
462 void *ChangeLightRepresentation (void *param)
465 OCStackResult result = OC_STACK_ERROR;
471 if (gLightUnderObservation)
474 " =====> Notifying stack of new power level %d\n", light.power);
475 result = OCNotifyAllObservers (light.handle, OC_NA_QOS);
476 if (OC_STACK_NO_OBSERVERS == result)
478 gLightUnderObservation = 0;
485 int main(int argc, char* argv[])
490 while ((opt = getopt(argc, argv, "t:")) != -1)
502 if(TEST <= TEST_INVALID || TEST >= MAX_TESTS)
508 OC_LOG(DEBUG, TAG, "OCServer is starting...");
510 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
512 OC_LOG(ERROR, TAG, "OCStack init error");
517 * Declare and create the example resource: light
522 * Create a thread for changing the representation of the light
524 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);
526 // Break from loop with Ctrl-C
527 OC_LOG(INFO, TAG, "Entering ocserver main loop...");
528 signal(SIGINT, handleSigInt);
531 if (OCProcess() != OC_STACK_OK)
533 OC_LOG(ERROR, TAG, "OCStack process error");
540 * Cancel the light thread and wait for it to terminate
542 pthread_cancel(threadId);
543 pthread_join(threadId, NULL);
545 OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
547 if (OCStop() != OC_STACK_OK)
549 OC_LOG(ERROR, TAG, "OCStack process error");
554 void createResources()
558 OCResourceHandle fan;
559 OCStackResult res = OCCreateResource(&fan,
561 OC_RSRVD_INTERFACE_DEFAULT,
563 OCEntityHandlerFanCb,
565 OC_DISCOVERABLE|OC_OBSERVABLE);
566 OC_LOG_V(INFO, TAG, "Created fan resource with result: %s", getResult(res));
568 OCResourceHandle light;
569 res = OCCreateResource(&light,
571 OC_RSRVD_INTERFACE_DEFAULT,
573 OCEntityHandlerLightCb,
575 OC_DISCOVERABLE|OC_OBSERVABLE);
576 OC_LOG_V(INFO, TAG, "Created light resource with result: %s", getResult(res));
578 OCResourceHandle room;
580 if(TEST == TEST_APP_COLL_EH)
582 res = OCCreateResource(&room,
584 OC_RSRVD_INTERFACE_BATCH,
586 OCEntityHandlerRoomCb,
592 res = OCCreateResource(&room,
594 OC_RSRVD_INTERFACE_BATCH,
601 OC_LOG_V(INFO, TAG, "Created room resource with result: %s", getResult(res));
602 OCBindResourceInterfaceToResource(room, OC_RSRVD_INTERFACE_LL);
603 OCBindResourceInterfaceToResource(room, OC_RSRVD_INTERFACE_DEFAULT);
605 res = OCBindResource(room, light);
606 OC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));
608 res = OCBindResource(room, fan);
609 OC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));