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)
146 OCEntityHandlerResult ret = OC_EH_OK;
147 OCEntityHandlerResponse response;
148 char payload[MAX_RESPONSE_LENGTH] = {0};
150 OC_LOG_V(INFO, TAG, "Callback for Room");
151 PrintReceivedMsgInfo(flag, ehRequest );
153 if(ehRequest && flag == OC_REQUEST_FLAG )
155 std::string query = (const char*)ehRequest->query;
157 if(OC_REST_GET == ehRequest->method)
159 if(query.find(OC_RSRVD_INTERFACE_DEFAULT) != std::string::npos)
161 ret = HandleCallback(ehRequest,
162 rspGetRoomDefault, rspFailureRoom, payload, sizeof(payload));
163 if(ret != OC_EH_ERROR)
165 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
166 ret = HandleCallback(ehRequest,
167 rspGetLightCollection, rspFailureLight, payload, sizeof(payload));
169 if(ret != OC_EH_ERROR)
171 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
172 ret = HandleCallback(ehRequest,
173 rspGetFanCollection, rspFailureFan, payload, sizeof(payload));
176 else if(query.find(OC_RSRVD_INTERFACE_LL) != std::string::npos)
178 ret = HandleCallback(ehRequest,
179 rspGetRoomCollection, rspFailureRoom, payload, sizeof(payload));
180 if(ret != OC_EH_ERROR)
182 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
183 ret = HandleCallback(ehRequest,
184 rspGetLightCollection, rspFailureLight, payload, sizeof(payload));
186 if(ret != OC_EH_ERROR)
188 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
189 ret = HandleCallback(ehRequest,
190 rspGetFanCollection, rspFailureFan, payload, sizeof(payload));
193 else if(query.find(OC_RSRVD_INTERFACE_BATCH) != std::string::npos)
195 ret = HandleCallback(ehRequest,
196 rspGetRoomCollection, rspFailureRoom, payload, sizeof(payload));
197 if(ret != OC_EH_ERROR)
199 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
200 ret = HandleCallback(ehRequest,
201 rspGetLightDefault, rspFailureLight, payload, sizeof(payload));
203 if(ret != OC_EH_ERROR)
205 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
206 ret = HandleCallback(ehRequest,
207 rspGetFanDefault, rspFailureFan, payload, sizeof(payload));
212 // Format the response. Note this requires some info about the request
213 response.requestHandle = ehRequest->requestHandle;
214 response.resourceHandle = ehRequest->resource;
215 response.ehResult = ret;
216 response.payload = payload;
217 response.payloadSize = strlen(payload);
218 response.numSendVendorSpecificHeaderOptions = 0;
219 memset(response.sendVendorSpecificHeaderOptions,
220 0, sizeof response.sendVendorSpecificHeaderOptions);
221 memset(response.resourceUri, 0, sizeof response.resourceUri);
222 // Indicate that response is NOT in a persistent buffer
223 response.persistentBufferFlag = 0;
225 if (OCDoResponse(&response) != OC_STACK_OK)
227 OC_LOG(ERROR, TAG, "Error sending response");
232 else if(OC_REST_PUT == ehRequest->method)
234 if(query.find(OC_RSRVD_INTERFACE_DEFAULT) != std::string::npos)
236 if(ret != OC_EH_ERROR)
238 ret = HandleCallback(ehRequest,
239 rspPutRoomDefault, rspFailureRoom, payload, sizeof(payload));
242 if(query.find(OC_RSRVD_INTERFACE_LL) != std::string::npos)
244 if(ret != OC_EH_ERROR)
246 ret = HandleCallback(ehRequest,
247 rspPutRoomCollection, rspFailureRoom, payload, sizeof(payload));
249 if(ret != OC_EH_ERROR)
251 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
252 ret = HandleCallback(ehRequest,
253 rspPutLightCollection, rspFailureLight, payload, sizeof(payload));
255 if(ret != OC_EH_ERROR)
257 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
258 ret = HandleCallback(ehRequest,
259 rspPutFanCollection, rspFailureFan, payload, sizeof(payload));
262 if(query.find(OC_RSRVD_INTERFACE_BATCH ) != std::string::npos)
264 if(ret != OC_EH_ERROR)
266 ret = HandleCallback(ehRequest,
267 rspPutRoomCollection, rspFailureRoom, payload, sizeof(payload));
269 if(ret != OC_EH_ERROR)
271 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
272 ret = HandleCallback(ehRequest,
273 rspPutLightDefault, rspFailureLight, payload, sizeof(payload));
275 if(ret != OC_EH_ERROR)
277 ret = HandleCallback(ehRequest, ",", ",", payload, sizeof(payload));
278 ret = HandleCallback(ehRequest,
279 rspPutFanDefault, rspFailureFan, payload, sizeof(payload));
284 // Format the response. Note this requires some info about the request
285 response.requestHandle = ehRequest->requestHandle;
286 response.resourceHandle = ehRequest->resource;
287 response.ehResult = ret;
288 response.payload = payload;
289 response.payloadSize = strlen(payload);
290 response.numSendVendorSpecificHeaderOptions = 0;
291 memset(response.sendVendorSpecificHeaderOptions,
292 0, sizeof response.sendVendorSpecificHeaderOptions);
293 memset(response.resourceUri, 0, sizeof response.resourceUri);
294 // Indicate that response is NOT in a persistent buffer
295 response.persistentBufferFlag = 0;
297 if (OCDoResponse(&response) != OC_STACK_OK)
299 OC_LOG(ERROR, TAG, "Error sending response");
306 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
311 else if (ehRequest && flag == OC_OBSERVE_FLAG)
313 gLightUnderObservation = 1;
318 OCEntityHandlerResult OCEntityHandlerLightCb(OCEntityHandlerFlag flag,
319 OCEntityHandlerRequest * ehRequest)
321 OCEntityHandlerResult ret = OC_EH_OK;
322 OCEntityHandlerResponse response;
323 char payload[MAX_RESPONSE_LENGTH] = {0};
325 OC_LOG_V(INFO, TAG, "Callback for Light");
326 PrintReceivedMsgInfo(flag, ehRequest );
328 if(ehRequest && flag == OC_REQUEST_FLAG)
330 if(OC_REST_GET == ehRequest->method)
332 ret = HandleCallback(ehRequest,
333 rspGetLightDefault, rspFailureLight, payload, sizeof(payload));
335 else if(OC_REST_PUT == ehRequest->method)
337 ret = HandleCallback(ehRequest,
338 rspPutLightDefault, rspFailureLight, payload, sizeof(payload));
342 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
349 // Format the response. Note this requires some info about the request
350 response.requestHandle = ehRequest->requestHandle;
351 response.resourceHandle = ehRequest->resource;
352 response.ehResult = ret;
353 response.payload = payload;
354 response.payloadSize = strlen(payload);
355 response.numSendVendorSpecificHeaderOptions = 0;
356 memset(response.sendVendorSpecificHeaderOptions,
357 0, sizeof response.sendVendorSpecificHeaderOptions);
358 memset(response.resourceUri, 0, sizeof response.resourceUri);
359 // Indicate that response is NOT in a persistent buffer
360 response.persistentBufferFlag = 0;
363 if (OCDoResponse(&response) != OC_STACK_OK)
365 OC_LOG(ERROR, TAG, "Error sending response");
370 else if (ehRequest && flag == OC_OBSERVE_FLAG)
372 gLightUnderObservation = 1;
378 OCEntityHandlerResult OCEntityHandlerFanCb(OCEntityHandlerFlag flag,
379 OCEntityHandlerRequest * ehRequest)
381 OCEntityHandlerResult ret = OC_EH_OK;
382 OCEntityHandlerResponse response;
383 char payload[MAX_RESPONSE_LENGTH] = {0};
385 OC_LOG_V(INFO, TAG, "Callback for Fan");
386 PrintReceivedMsgInfo(flag, ehRequest );
388 if(ehRequest && flag == OC_REQUEST_FLAG)
390 if(OC_REST_GET == ehRequest->method)
392 ret = HandleCallback(ehRequest, rspGetFanDefault,
393 rspFailureFan, payload, sizeof(payload));
395 else if(OC_REST_PUT == ehRequest->method)
397 ret = HandleCallback(ehRequest, rspPutFanDefault,
398 rspFailureFan, payload, sizeof(payload));
402 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
409 // Format the response. Note this requires some info about the request
410 response.requestHandle = ehRequest->requestHandle;
411 response.resourceHandle = ehRequest->resource;
412 response.ehResult = ret;
413 response.payload = payload;
414 response.payloadSize = strlen(payload);
415 response.numSendVendorSpecificHeaderOptions = 0;
416 memset(response.sendVendorSpecificHeaderOptions,
417 0, sizeof response.sendVendorSpecificHeaderOptions);
418 memset(response.resourceUri, 0, sizeof response.resourceUri);
419 // Indicate that response is NOT in a persistent buffer
420 response.persistentBufferFlag = 0;
423 if (OCDoResponse(&response) != OC_STACK_OK)
425 OC_LOG(ERROR, TAG, "Error sending response");
431 else if (ehRequest && flag == OC_OBSERVE_FLAG)
433 gLightUnderObservation = 1;
439 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
440 void handleSigInt(int signum)
442 if (signum == SIGINT)
448 void *ChangeLightRepresentation (void *param)
451 OCStackResult result = OC_STACK_ERROR;
457 if (gLightUnderObservation)
460 " =====> Notifying stack of new power level %d\n", light.power);
461 result = OCNotifyAllObservers (light.handle, OC_NA_QOS);
462 if (OC_STACK_NO_OBSERVERS == result)
464 gLightUnderObservation = 0;
471 int main(int argc, char* argv[])
476 while ((opt = getopt(argc, argv, "t:")) != -1)
488 if(TEST <= TEST_INVALID || TEST >= MAX_TESTS)
494 OC_LOG(DEBUG, TAG, "OCServer is starting...");
496 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
498 OC_LOG(ERROR, TAG, "OCStack init error");
503 * Declare and create the example resource: light
508 * Create a thread for changing the representation of the light
510 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);
512 // Break from loop with Ctrl-C
513 OC_LOG(INFO, TAG, "Entering ocserver main loop...");
514 signal(SIGINT, handleSigInt);
517 if (OCProcess() != OC_STACK_OK)
519 OC_LOG(ERROR, TAG, "OCStack process error");
526 * Cancel the light thread and wait for it to terminate
528 pthread_cancel(threadId);
529 pthread_join(threadId, NULL);
531 OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
533 if (OCStop() != OC_STACK_OK)
535 OC_LOG(ERROR, TAG, "OCStack process error");
540 void createResources()
544 OCResourceHandle fan;
545 OCStackResult res = OCCreateResource(&fan,
547 OC_RSRVD_INTERFACE_DEFAULT,
549 OCEntityHandlerFanCb,
550 OC_DISCOVERABLE|OC_OBSERVABLE);
551 OC_LOG_V(INFO, TAG, "Created fan resource with result: %s", getResult(res));
553 OCResourceHandle light;
554 res = OCCreateResource(&light,
556 OC_RSRVD_INTERFACE_DEFAULT,
558 OCEntityHandlerLightCb,
559 OC_DISCOVERABLE|OC_OBSERVABLE);
560 OC_LOG_V(INFO, TAG, "Created light resource with result: %s", getResult(res));
562 OCResourceHandle room;
564 if(TEST == TEST_APP_COLL_EH)
566 res = OCCreateResource(&room,
568 OC_RSRVD_INTERFACE_BATCH,
570 OCEntityHandlerRoomCb,
575 res = OCCreateResource(&room,
577 OC_RSRVD_INTERFACE_BATCH,
583 OC_LOG_V(INFO, TAG, "Created room resource with result: %s", getResult(res));
584 OCBindResourceInterfaceToResource(room, OC_RSRVD_INTERFACE_LL);
585 OCBindResourceInterfaceToResource(room, OC_RSRVD_INTERFACE_DEFAULT);
587 res = OCBindResource(room, light);
588 OC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));
590 res = OCBindResource(room, fan);
591 OC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));