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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
35 //string length of "/a/light/" + std::numeric_limits<int>::digits10 + '\0'"
37 const int URI_MAXSIZE = 19;
39 static int gObserveNotifyType = 3;
42 int gLightUnderObservation = 0;
44 static LightResource Light;
45 // This variable determines instance number of the Light resource.
46 // Used by POST method to create a new instance of Light resource.
47 static int gCurrLightInstance = 0;
49 static LightResource gLightInstance[SAMPLE_MAX_NUM_POST_INSTANCE];
51 Observers interestedObservers[SAMPLE_MAX_NUM_OBSERVATIONS];
54 static int stopPresenceCount = 10;
55 #define numPresenceResources (2)
58 //TODO: Follow the pattern used in constructJsonResponse() when the payload is decided.
59 const char responsePayloadDeleteOk[] =
60 "{App determines payload: Delete Resource operation succeeded.}";
61 const char responsePayloadDeleteNotOK[] =
62 "{App determines payload: Delete Resource operation failed.}";
63 const char responsePayloadResourceDoesNotExist[] =
64 "{App determines payload: The resource does not exist.}";
65 const char responsePayloadDeleteResourceNotSupported[] =
66 "{App determines payload: The request is received for a non-support resource.}";
69 char *gResourceUri= (char *)"/a/light";
70 const char *contentType = "myContentType";
71 const char *dateOfManufacture = "myDateOfManufacture";
72 const char *deviceName = "myDeviceName";
73 const char *deviceUUID = "myDeviceUUID";
74 const char *firmwareVersion = "myFirmwareVersion";
75 const char *hostName = "myHostName";
76 const char *manufacturerName = "myManufacturerNa";
77 const char *manufacturerUrl = "myManufacturerUrl";
78 const char *modelNumber = "myModelNumber";
79 const char *platformVersion = "myPlatformVersion";
80 const char *supportUrl = "mySupportUrl";
81 const char *version = "myVersion";
83 OCDeviceInfo deviceInfo;
85 //This function takes the request as an input and returns the response
87 char* constructJsonResponse (OCEntityHandlerRequest *ehRequest)
89 cJSON *json = cJSON_CreateObject();
92 LightResource *currLightResource = &Light;
94 if (ehRequest->resource == gLightInstance[0].handle)
96 currLightResource = &gLightInstance[0];
97 gResourceUri = (char *) "a/light/0";
99 else if (ehRequest->resource == gLightInstance[1].handle)
101 currLightResource = &gLightInstance[1];
102 gResourceUri = (char *) "a/light/1";
105 if(OC_REST_PUT == ehRequest->method)
107 // Get cJSON pointer to query
108 cJSON *putJson = cJSON_Parse((char *)ehRequest->reqJSONPayload);
110 // Get root of JSON payload, then the 1st resource.
111 cJSON* carrier = cJSON_GetObjectItem(putJson, "oc");
112 carrier = cJSON_GetArrayItem(carrier, 0);
113 carrier = cJSON_GetObjectItem(carrier, "rep");
115 currLightResource->power = cJSON_GetObjectItem(carrier,"power")->valueint;
116 currLightResource->state = cJSON_GetObjectItem(carrier,"state")->valueint;
118 cJSON_Delete(putJson);
121 cJSON_AddStringToObject(json,"href",gResourceUri);
122 cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject());
123 cJSON_AddBoolToObject(format, "state", currLightResource->state);
124 cJSON_AddNumberToObject(format, "power", currLightResource->power);
126 jsonResponse = cJSON_Print(json);
132 OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest,
133 char *payload, uint16_t maxPayloadSize)
135 OCEntityHandlerResult ehResult;
136 char *getResp = constructJsonResponse(ehRequest);
138 if (maxPayloadSize > strlen ((char *)getResp))
140 strncpy(payload, getResp, strlen((char *)getResp));
145 OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
147 ehResult = OC_EH_ERROR;
155 OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
156 char *payload, uint16_t maxPayloadSize)
158 OCEntityHandlerResult ehResult;
159 char *putResp = constructJsonResponse(ehRequest);
161 if (maxPayloadSize > strlen ((char *)putResp))
163 strncpy(payload, putResp, strlen((char *)putResp));
168 OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
170 ehResult = OC_EH_ERROR;
178 OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest,
179 OCEntityHandlerResponse *response, char *payload, uint16_t maxPayloadSize)
181 OCEntityHandlerResult ehResult = OC_EH_OK;
182 char *respPLPost_light = NULL;
187 * The entity handler determines how to process a POST request.
188 * Per the REST paradigm, POST can also be used to update representation of existing
189 * resource or create a new resource.
190 * In the sample below, if the POST is for /a/light then a new instance of the Light
191 * resource is created with default representation (if representation is included in
192 * POST payload it can be used as initial values) as long as the instance is
193 * lesser than max new instance count. Once max instance count is reached, POST on
194 * /a/light updated the representation of /a/light (just like PUT)
197 if (ehRequest->resource == Light.handle)
199 if (gCurrLightInstance < SAMPLE_MAX_NUM_POST_INSTANCE)
201 // Create new Light instance
202 char newLightUri[URI_MAXSIZE];
203 snprintf(newLightUri, URI_MAXSIZE, "/a/light/%d", gCurrLightInstance);
205 json = cJSON_CreateObject();
206 cJSON_AddStringToObject(json,"href",gResourceUri);
207 cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject());
208 cJSON_AddStringToObject(format, "createduri", (char *) newLightUri);
210 if (0 == createLightResource (newLightUri, &gLightInstance[gCurrLightInstance]))
212 OC_LOG (INFO, TAG, "Created new Light instance\n");
213 gLightInstance[gCurrLightInstance].state = 0;
214 gLightInstance[gCurrLightInstance].power = 0;
215 gCurrLightInstance++;
216 respPLPost_light = cJSON_Print(json);
217 strncpy ((char *)response->resourceUri, newLightUri, MAX_URI_LENGTH);
218 ehResult = OC_EH_RESOURCE_CREATED;
225 // Update repesentation of /a/light
228 respPLPost_light = constructJsonResponse(ehRequest);
233 for (int i = 0; i < SAMPLE_MAX_NUM_POST_INSTANCE; i++)
235 if (ehRequest->resource == gLightInstance[i].handle)
237 gLightInstance[i].state = true;
238 gLightInstance[i].power = 22;
241 respPLPost_light = constructJsonResponse(ehRequest);
246 respPLPost_light = constructJsonResponse(ehRequest);
252 if ((respPLPost_light != NULL) && (maxPayloadSize > strlen ((char *)respPLPost_light)))
254 strncpy(payload, respPLPost_light, strlen((char *)respPLPost_light));
258 OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
260 ehResult = OC_EH_ERROR;
263 free(respPLPost_light);
267 OCEntityHandlerResult ProcessDeleteRequest (OCEntityHandlerRequest *ehRequest,
268 char *payload, uint16_t maxPayloadSize)
270 if(ehRequest == NULL)
272 OC_LOG(INFO, TAG, "The ehRequest is NULL");
275 OCEntityHandlerResult ehResult = OC_EH_OK;
277 OC_LOG_V(INFO, TAG, "\n\nExecuting %s for resource %d ", __func__, ehRequest->resource);
280 * In the sample below, the application will:
281 * 1a. pass the delete request to the c stack
282 * 1b. internally, the c stack figures out what needs to be done and does it accordingly
283 * (e.g. send observers notification, remove observers...)
284 * 1c. the c stack returns with the result whether the request is fullfilled.
285 * 2. optionally, app removes observers out of its array 'interestedObservers'
288 const char* deleteResponse = NULL;
290 if ((ehRequest != NULL) && (ehRequest->resource == Light.handle))
292 //Step 1: Ask stack to do the work.
293 OCStackResult result = OCDeleteResource(ehRequest->resource);
295 if (result == OC_STACK_OK)
297 OC_LOG (INFO, TAG, "\n\nDelete Resource operation succeeded.");
299 deleteResponse = responsePayloadDeleteOk;
301 //Step 2: clear observers who wanted to observe this resource at the app level.
302 for (uint8_t i = 0; i < SAMPLE_MAX_NUM_OBSERVATIONS; i++)
304 if (interestedObservers[i].resourceHandle == ehRequest->resource)
306 interestedObservers[i].valid = false;
307 interestedObservers[i].observationId = 0;
308 interestedObservers[i].resourceHandle = NULL;
312 else if (result == OC_STACK_NO_RESOURCE)
314 OC_LOG(INFO, TAG, "\n\nThe resource doesn't exist or it might have been deleted.");
315 deleteResponse = responsePayloadResourceDoesNotExist;
316 ehResult = OC_EH_RESOURCE_DELETED;
320 OC_LOG(INFO, TAG, "\n\nEncountered error from OCDeleteResource().");
321 deleteResponse = responsePayloadDeleteNotOK;
322 ehResult = OC_EH_ERROR;
325 else if (ehRequest->resource != Light.handle)
327 //Let's this app not supporting DELETE on some resources so
328 //consider the DELETE request is received for a non-support resource.
329 OC_LOG_V(INFO, TAG, "\n\nThe request is received for a non-support resource.");
330 deleteResponse = responsePayloadDeleteResourceNotSupported;
331 ehResult = OC_EH_FORBIDDEN;
334 if (maxPayloadSize > strlen ((char *)deleteResponse))
336 strncpy(payload, deleteResponse, strlen((char *)deleteResponse));
340 OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
342 ehResult = OC_EH_ERROR;
348 OCEntityHandlerResult ProcessNonExistingResourceRequest(OCEntityHandlerRequest *ehRequest,
349 char *payload, uint16_t maxPayloadSize)
351 OC_LOG_V(INFO, TAG, "\n\nExecuting %s ", __func__);
353 const char* response = NULL;
354 response = responsePayloadResourceDoesNotExist;
356 if ( (ehRequest != NULL) &&
357 (maxPayloadSize > strlen ((char *)response)) )
359 strncpy((char *)payload, response, strlen((char *)response));
363 OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
367 return OC_EH_RESOURCE_DELETED;
370 void ProcessObserveRegister (OCEntityHandlerRequest *ehRequest)
372 OC_LOG_V (INFO, TAG, "Received observation registration request with observation Id %d",
373 ehRequest->obsInfo.obsId);
374 for (uint8_t i = 0; i < SAMPLE_MAX_NUM_OBSERVATIONS; i++)
376 if (interestedObservers[i].valid == false)
378 interestedObservers[i].observationId = ehRequest->obsInfo.obsId;
379 interestedObservers[i].valid = true;
380 gLightUnderObservation = 1;
386 void ProcessObserveDeregister (OCEntityHandlerRequest *ehRequest)
388 bool clientStillObserving = false;
390 OC_LOG_V (INFO, TAG, "Received observation deregistration request for observation Id %d",
391 ehRequest->obsInfo.obsId);
392 for (uint8_t i = 0; i < SAMPLE_MAX_NUM_OBSERVATIONS; i++)
394 if (interestedObservers[i].observationId == ehRequest->obsInfo.obsId)
396 interestedObservers[i].valid = false;
398 if (interestedObservers[i].valid == true)
400 // Even if there is one single client observing we continue notifying entity handler
401 clientStillObserving = true;
404 if (clientStillObserving == false)
405 gLightUnderObservation = 0;
408 OCEntityHandlerResult
409 OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag,
410 OCEntityHandlerRequest *entityHandlerRequest, char* uri)
412 OC_LOG_V (INFO, TAG, "Inside device default entity handler - flags: 0x%x, uri: %s", flag, uri);
414 OCEntityHandlerResult ehResult = OC_EH_OK;
415 OCEntityHandlerResponse response;
416 char payload[MAX_RESPONSE_LENGTH] = {0};
419 if (!entityHandlerRequest)
421 OC_LOG (ERROR, TAG, "Invalid request pointer");
425 // Initialize certain response fields
426 response.numSendVendorSpecificHeaderOptions = 0;
427 memset(response.sendVendorSpecificHeaderOptions, 0,
428 sizeof response.sendVendorSpecificHeaderOptions);
429 memset(response.resourceUri, 0, sizeof response.resourceUri);
431 if (flag & OC_INIT_FLAG)
433 OC_LOG (INFO, TAG, "Flag includes OC_INIT_FLAG");
435 if (flag & OC_REQUEST_FLAG)
437 OC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
438 if (entityHandlerRequest->resource == NULL)
440 OC_LOG (INFO, TAG, "Received request from client to a non-existing resource");
441 ehResult = ProcessNonExistingResourceRequest(entityHandlerRequest,
442 payload, sizeof(payload) - 1);
444 else if (OC_REST_GET == entityHandlerRequest->method)
446 OC_LOG (INFO, TAG, "Received OC_REST_GET from client");
447 ehResult = ProcessGetRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
449 else if (OC_REST_PUT == entityHandlerRequest->method)
451 OC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
452 ehResult = ProcessPutRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
454 else if (OC_REST_DELETE == entityHandlerRequest->method)
456 OC_LOG (INFO, TAG, "Received OC_REST_DELETE from client");
457 ehResult = ProcessDeleteRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
461 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
462 entityHandlerRequest->method);
463 ehResult = OC_EH_ERROR;
466 // If the result isn't an error or forbidden, send response
467 if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN)))
469 // Format the response. Note this requires some info about the request
470 response.requestHandle = entityHandlerRequest->requestHandle;
471 response.resourceHandle = entityHandlerRequest->resource;
472 response.ehResult = ehResult;
473 response.payload = payload;
474 response.payloadSize = strlen(payload);
475 // Indicate that response is NOT in a persistent buffer
476 response.persistentBufferFlag = 0;
479 if (OCDoResponse(&response) != OC_STACK_OK)
481 OC_LOG(ERROR, TAG, "Error sending response");
482 ehResult = OC_EH_ERROR;
486 if (flag & OC_OBSERVE_FLAG)
488 OC_LOG(INFO, TAG, "Flag includes OC_OBSERVE_FLAG");
489 if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action)
491 OC_LOG (INFO, TAG, "Received OC_OBSERVE_REGISTER from client");
493 else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action)
495 OC_LOG (INFO, TAG, "Received OC_OBSERVE_DEREGISTER from client");
502 OCEntityHandlerResult
503 OCNOPEntityHandlerCb (OCEntityHandlerFlag flag,
504 OCEntityHandlerRequest *entityHandlerRequest)
506 // This is callback is associated with the 2 presence notification
507 // resources. They are non-operational.
511 OCEntityHandlerResult
512 OCEntityHandlerCb (OCEntityHandlerFlag flag,
513 OCEntityHandlerRequest *entityHandlerRequest)
515 OC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag);
517 OCEntityHandlerResult ehResult = OC_EH_OK;
518 OCEntityHandlerResponse response;
519 char payload[MAX_RESPONSE_LENGTH] = {0};
522 if (!entityHandlerRequest)
524 OC_LOG (ERROR, TAG, "Invalid request pointer");
528 // Initialize certain response fields
529 response.numSendVendorSpecificHeaderOptions = 0;
530 memset(response.sendVendorSpecificHeaderOptions,
531 0, sizeof response.sendVendorSpecificHeaderOptions);
532 memset(response.resourceUri, 0, sizeof response.resourceUri);
534 if (flag & OC_INIT_FLAG)
536 OC_LOG (INFO, TAG, "Flag includes OC_INIT_FLAG");
538 if (flag & OC_REQUEST_FLAG)
540 OC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
541 if (OC_REST_GET == entityHandlerRequest->method)
543 OC_LOG (INFO, TAG, "Received OC_REST_GET from client");
544 ehResult = ProcessGetRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
546 else if (OC_REST_PUT == entityHandlerRequest->method)
548 OC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
549 ehResult = ProcessPutRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
551 else if (OC_REST_POST == entityHandlerRequest->method)
553 OC_LOG (INFO, TAG, "Received OC_REST_POST from client");
554 ehResult = ProcessPostRequest (entityHandlerRequest,
555 &response, payload, sizeof(payload) - 1);
557 else if (OC_REST_DELETE == entityHandlerRequest->method)
559 OC_LOG (INFO, TAG, "Received OC_REST_DELETE from client");
560 ehResult = ProcessDeleteRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
564 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
565 entityHandlerRequest->method);
568 // If the result isn't an error or forbidden, send response
569 if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN)))
571 // Format the response. Note this requires some info about the request
572 response.requestHandle = entityHandlerRequest->requestHandle;
573 response.resourceHandle = entityHandlerRequest->resource;
574 response.ehResult = ehResult;
575 response.payload = payload;
576 response.payloadSize = strlen(payload);
577 // Indicate that response is NOT in a persistent buffer
578 response.persistentBufferFlag = 0;
580 // Handle vendor specific options
581 if(entityHandlerRequest->rcvdVendorSpecificHeaderOptions &&
582 entityHandlerRequest->numRcvdVendorSpecificHeaderOptions)
584 OC_LOG (INFO, TAG, "Received vendor specific options");
586 OCHeaderOption * rcvdOptions =
587 entityHandlerRequest->rcvdVendorSpecificHeaderOptions;
588 for( i = 0; i < entityHandlerRequest->numRcvdVendorSpecificHeaderOptions; i++)
590 if(((OCHeaderOption)rcvdOptions[i]).protocolID == OC_COAP_ID)
592 OC_LOG_V(INFO, TAG, "Received option with OC_COAP_ID and ID %u with",
593 ((OCHeaderOption)rcvdOptions[i]).optionID );
595 OC_LOG_BUFFER(INFO, TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
596 MAX_HEADER_OPTION_DATA_LENGTH);
599 OCHeaderOption * sendOptions = response.sendVendorSpecificHeaderOptions;
600 uint8_t option2[] = {21,22,23,24,25,26,27,28,29,30};
601 uint8_t option3[] = {31,32,33,34,35,36,37,38,39,40};
602 sendOptions[0].protocolID = OC_COAP_ID;
603 sendOptions[0].optionID = 2248;
604 memcpy(sendOptions[0].optionData, option2, sizeof(option2));
605 sendOptions[0].optionLength = 10;
606 sendOptions[1].protocolID = OC_COAP_ID;
607 sendOptions[1].optionID = 2600;
608 memcpy(sendOptions[1].optionData, option3, sizeof(option3));
609 sendOptions[1].optionLength = 10;
610 response.numSendVendorSpecificHeaderOptions = 2;
614 if (OCDoResponse(&response) != OC_STACK_OK)
616 OC_LOG(ERROR, TAG, "Error sending response");
617 ehResult = OC_EH_ERROR;
621 if (flag & OC_OBSERVE_FLAG)
623 OC_LOG(INFO, TAG, "Flag includes OC_OBSERVE_FLAG");
625 if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action)
627 OC_LOG (INFO, TAG, "Received OC_OBSERVE_REGISTER from client");
628 ProcessObserveRegister (entityHandlerRequest);
630 else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action)
632 OC_LOG (INFO, TAG, "Received OC_OBSERVE_DEREGISTER from client");
633 ProcessObserveDeregister (entityHandlerRequest);
640 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
641 void handleSigInt(int signum)
643 if (signum == SIGINT)
649 void *ChangeLightRepresentation (void *param)
652 OCStackResult result = OC_STACK_ERROR;
655 uint8_t numNotifies = (SAMPLE_MAX_NUM_OBSERVATIONS)/2;
656 OCObservationId obsNotify[numNotifies];
662 if (gLightUnderObservation)
664 OC_LOG_V(INFO, TAG, " =====> Notifying stack of new power level %d\n", Light.power);
665 if (gObserveNotifyType == 1)
667 // Notify list of observers. Alternate observers on the list will be notified.
669 for (uint8_t i = 0; i < SAMPLE_MAX_NUM_OBSERVATIONS; (i=i+2))
671 if (interestedObservers[i].valid == true)
673 obsNotify[j] = interestedObservers[i].observationId;
678 cJSON *json = cJSON_CreateObject();
680 cJSON_AddStringToObject(json,"href",gResourceUri);
681 cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject());
682 cJSON_AddBoolToObject(format, "state", Light.state);
683 cJSON_AddNumberToObject(format, "power", Light.power);
684 char * obsResp = cJSON_Print(json);
686 result = OCNotifyListOfObservers (Light.handle, obsNotify, j,
690 else if (gObserveNotifyType == 0)
692 // Notifying all observers
693 result = OCNotifyAllObservers (Light.handle, OC_NA_QOS);
694 if (OC_STACK_NO_OBSERVERS == result)
697 "=======> No more observers exist, stop sending observations");
698 gLightUnderObservation = 0;
703 OC_LOG (ERROR, TAG, "Incorrect notification type selected");
707 if(stopPresenceCount > 0)
709 OC_LOG_V(INFO, TAG, "================ presence count %d", stopPresenceCount);
711 if(!stopPresenceCount--)
713 OC_LOG(INFO, TAG, "================ stopping presence");
721 void *presenceNotificationGenerator(void *param)
725 OCDoHandle presenceNotificationHandles[numPresenceResources];
726 OCStackResult res = OC_STACK_OK;
728 std::array<std::string, numPresenceResources> presenceNotificationResources { {
729 std::string("core.fan"),
730 std::string("core.led") } };
731 std::array<std::string, numPresenceResources> presenceNotificationUris { {
732 std::string("/a/fan"),
733 std::string("/a/led") } };
735 for(int i=0; i<numPresenceResources; i++)
737 if(res == OC_STACK_OK)
740 res = OCCreateResource(&presenceNotificationHandles[i],
741 presenceNotificationResources.at(i).c_str(),
743 presenceNotificationUris.at(i).c_str(),
744 OCNOPEntityHandlerCb,
745 OC_DISCOVERABLE|OC_OBSERVABLE);
747 if(res != OC_STACK_OK)
749 OC_LOG_V(ERROR, TAG, "\"Presence Notification Generator\" failed to create resource "
750 "%s with result %s.", presenceNotificationResources.at(i).c_str(),
756 for(int i=0; i<numPresenceResources; i++)
758 if(res == OC_STACK_OK)
760 res = OCDeleteResource(presenceNotificationHandles[i]);
762 if(res != OC_STACK_OK)
764 OC_LOG_V(ERROR, TAG, "\"Presence Notification Generator\" failed to delete "\
765 "resource %s.", presenceNotificationResources.at(i).c_str());
772 static void PrintUsage()
774 OC_LOG(INFO, TAG, "Usage : ocserver -o <0|1>");
775 OC_LOG(INFO, TAG, "-o 0 : Notify all observers");
776 OC_LOG(INFO, TAG, "-o 1 : Notify list of observers");
779 int main(int argc, char* argv[])
782 pthread_t threadId_presence;
785 while ((opt = getopt(argc, argv, "o:")) != -1)
790 gObserveNotifyType = atoi(optarg);
798 if ((gObserveNotifyType != 0) && (gObserveNotifyType != 1))
804 OC_LOG(DEBUG, TAG, "OCServer is starting...");
806 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
808 OC_LOG(ERROR, TAG, "OCStack init error");
812 if (OCStartPresence(0) != OC_STACK_OK)
814 OC_LOG(ERROR, TAG, "OCStack presence/discovery error");
819 OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandlerCb);
821 OCStackResult deviceResult = SetDeviceInfo(contentType, dateOfManufacture, deviceName,
822 deviceUUID, firmwareVersion, hostName, manufacturerName,
823 manufacturerUrl, modelNumber, platformVersion, supportUrl, version);
825 if (deviceResult != OC_STACK_OK)
827 OC_LOG(INFO, TAG, "Device Registration failed!");
831 deviceResult = OCSetDeviceInfo(deviceInfo);
833 if (deviceResult != OC_STACK_OK)
835 OC_LOG(INFO, TAG, "Device Registration failed!");
840 * Declare and create the example resource: Light
842 createLightResource(gResourceUri, &Light);
844 // Initialize observations data structure for the resource
845 for (uint8_t i = 0; i < SAMPLE_MAX_NUM_OBSERVATIONS; i++)
847 interestedObservers[i].valid = false;
851 * Create a thread for changing the representation of the Light
853 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);
856 * Create a thread for generating changes that cause presence notifications
857 * to be sent to clients
859 pthread_create(&threadId_presence, NULL, presenceNotificationGenerator, (void *)NULL);
861 // Break from loop with Ctrl-C
862 OC_LOG(INFO, TAG, "Entering ocserver main loop...");
864 signal(SIGINT, handleSigInt);
867 if (OCProcess() != OC_STACK_OK)
869 OC_LOG(ERROR, TAG, "OCStack process error");
877 * Cancel the Light thread and wait for it to terminate
879 pthread_cancel(threadId);
880 pthread_join(threadId, NULL);
881 pthread_cancel(threadId_presence);
882 pthread_join(threadId_presence, NULL);
884 OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
886 if (OCStop() != OC_STACK_OK)
888 OC_LOG(ERROR, TAG, "OCStack process error");
894 int createLightResource (char *uri, LightResource *lightResource)
898 OC_LOG(ERROR, TAG, "Resource URI cannot be NULL");
902 lightResource->state = false;
903 lightResource->power= 0;
904 OCStackResult res = OCCreateResource(&(lightResource->handle),
909 OC_DISCOVERABLE|OC_OBSERVABLE);
910 OC_LOG_V(INFO, TAG, "Created Light resource with result: %s", getResult(res));
915 void DeleteDeviceInfo()
917 free(deviceInfo.contentType);
918 free(deviceInfo.dateOfManufacture);
919 free(deviceInfo.deviceName);
920 free(deviceInfo.deviceUUID);
921 free(deviceInfo.firmwareVersion);
922 free(deviceInfo.hostName);
923 free(deviceInfo.manufacturerName);
924 free(deviceInfo.manufacturerUrl);
925 free(deviceInfo.modelNumber);
926 free(deviceInfo.platformVersion);
927 free(deviceInfo.supportUrl);
928 free(deviceInfo.version);
931 bool DuplicateString(char** targetString, const char* sourceString)
939 *targetString = (char *) malloc(strlen(sourceString) + 1);
942 strncpy(*targetString, sourceString, (strlen(sourceString) + 1));
949 OCStackResult SetDeviceInfo(const char *contentType, const char *dateOfManufacture,
950 const char *deviceName, const char *deviceUUID, const char *firmwareVersion,
951 const char *hostName, const char *manufacturerName, const char *manufacturerUrl,
952 const char *modelNumber, const char *platformVersion, const char *supportUrl,
958 if(manufacturerName != NULL && (strlen(manufacturerName) > MAX_MANUFACTURER_NAME_LENGTH))
960 return OC_STACK_INVALID_PARAM;
963 if(manufacturerUrl != NULL && (strlen(manufacturerUrl) > MAX_MANUFACTURER_URL_LENGTH))
965 return OC_STACK_INVALID_PARAM;
968 if(!DuplicateString(&deviceInfo.contentType, contentType))
973 if(!DuplicateString(&deviceInfo.dateOfManufacture, dateOfManufacture))
978 if(!DuplicateString(&deviceInfo.deviceName, deviceName))
983 if(!DuplicateString(&deviceInfo.deviceUUID, deviceUUID))
988 if(!DuplicateString(&deviceInfo.firmwareVersion, firmwareVersion))
993 if(!DuplicateString(&deviceInfo.hostName, hostName))
998 if(!DuplicateString(&deviceInfo.manufacturerName, manufacturerName))
1003 if(!DuplicateString(&deviceInfo.manufacturerUrl, manufacturerUrl))
1008 if(!DuplicateString(&deviceInfo.modelNumber, modelNumber))
1013 if(!DuplicateString(&deviceInfo.platformVersion, platformVersion))
1018 if(!DuplicateString(&deviceInfo.supportUrl, supportUrl))
1023 if(!DuplicateString(&deviceInfo.version, version))
1034 return OC_STACK_ERROR;