void *ChangeLightRepresentation (void *param) { (void)param; OCStackResult result = OC_STACK_ERROR; uint8_t j = 0; uint8_t numNotifies = (SAMPLE_MAX_NUM_OBSERVATIONS)/2; OCObservationId obsNotify[numNotifies]; while (!gQuitFlag) { sleep(3); Light.power += 5; if (gLightUnderObservation) { OIC_LOG_V(INFO, TAG, " =====> Notifying stack of new power level %d\n", Light.power); if (gObserveNotifyType == 1) { // Notify list of observers. Alternate observers on the list will be notified. j = 0; for (uint8_t i = 0; i < SAMPLE_MAX_NUM_OBSERVATIONS; (i=i+2)) { if (interestedObservers[i].valid == true) { obsNotify[j] = interestedObservers[i].observationId; j++; } } OCRepPayload* payload = getPayload(gResourceUri, Light.power, Light.state); result = OCNotifyListOfObservers (Light.handle, obsNotify, j, payload, OC_NA_QOS); OCRepPayloadDestroy(payload); } else if (gObserveNotifyType == 0) { // Notifying all observers result = OCNotifyAllObservers (Light.handle, OC_NA_QOS); if (OC_STACK_NO_OBSERVERS == result) { OIC_LOG (INFO, TAG, "=======> No more observers exist, stop sending observations"); gLightUnderObservation = 0; } } else { OIC_LOG (ERROR, TAG, "Incorrect notification type selected"); } } } return NULL; }
// This method is used to display 'Observe' functionality of OC Stack. void *ChangeFanRepresentation () { uint8_t j = 0; OCObservationId obsNotify[MAX_NUM_OBSERVATIONS]; OCStackResult result = OC_STACK_ERROR; if (gFanUnderObservation) { OC_LOG_V(INFO, TAG, " ===> Notifying stack of new Fan state %d\n", Fan.state); // Notify list of observers. Alternate observers on the list will be notified. j = 0; for (uint8_t i = 0; i < MAX_NUM_OBSERVATIONS; i++) { if (interestedObservers[i].valid == true) { obsNotify[j] = interestedObservers[i].observationId; j++; } } // MAX_RESPONSE_LENGTH for arduino is 256, for demo 64 is enough char obsResp[64] = {0}; char *getResp = constructJsonResponse(NULL); size_t responsePayloadGetLength = strlen(getResp); strncpy(obsResp, getResp, responsePayloadGetLength); free(getResp); result = OCNotifyListOfObservers(Fan.handle, obsNotify, j, (unsigned char *)obsResp, OC_NA_QOS); if ((OC_STACK_NO_OBSERVERS == result) || (OC_STACK_CONTINUE == result)) { gFanUnderObservation = 0; } } return NULL; }
NSResult NSSendSync(NSSyncInfo *sync) { NS_LOG(DEBUG, "NSSendSync - IN"); OCObservationId obArray[255] = { 0, }; int obCount = 0; int i; OCResourceHandle rHandle; if (NSPutSyncResource(sync, &rHandle) != NS_OK) { NS_LOG(ERROR, PCF("Fail to put sync resource")); return NS_ERROR; } NSCacheElement * it = consumerSubList->head; while (it) { NSCacheSubData * subData = (NSCacheSubData *) it->data; NS_LOG_V(INFO_PRIVATE, "sync subData->id = %s", subData->id); NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId); NS_LOG_V(DEBUG, "subData->cloud_messageId = %d", subData->remote_messageObId); NS_LOG_V(DEBUG, "subData->syncId = %d", subData->syncObId); NS_LOG_V(DEBUG, "subData->cloud_syncId = %d", subData->remote_syncObId); NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite); if (subData->isWhite) { if (subData->syncObId != 0) { obArray[obCount++] = subData->syncObId; } #if (defined WITH_CLOUD) if (subData->remote_syncObId != 0) { obArray[obCount++] = subData->remote_syncObId; } #endif } it = it->next; } OCRepPayload* payload = NULL; if (NSSetSyncPayload(sync, &payload) != NS_OK) { NS_LOG(ERROR, "Failed to allocate payload"); return NS_ERROR; } #ifdef WITH_MQ if (NSGetMQServerInfo()) { OCRepPayload* MQPayload = OCRepPayloadClone(payload); NSMessageType MQType = 0; if (sync->state == NS_SYNC_READ) { MQType = NS_MESSAGE_READ; } else if (sync->state == NS_SYNC_DELETED) { MQType = NS_MESSAGE_DELETED; } OCRepPayloadSetPropInt(MQPayload, NS_ATTRIBUTE_TYPE, (int64_t) MQType); NSProviderPublishTopic(MQPayload, NSProviderPublishMQResponseCB); } #endif for (i = 0; i < obCount; ++i) { NS_LOG(DEBUG, "-------------------------------------------------------message\n"); NS_LOG_V(DEBUG, "Sync WhiteList[%d] = %d", i, obArray[i]); NS_LOG(DEBUG, "-------------------------------------------------------message\n"); } OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload, OC_LOW_QOS); NS_LOG_V(DEBUG, "Sync ocstackResult = %d", ocstackResult); if (ocstackResult != OC_STACK_OK) { NS_LOG(ERROR, "fail to send Sync"); OCRepPayloadDestroy(payload); return NS_ERROR; } OCRepPayloadDestroy(payload); NS_LOG(DEBUG, "NSSendSync - OUT"); return NS_OK; }
NSResult NSSendNotification(NSMessage *msg) { NS_LOG(DEBUG, "NSSendMessage - IN"); OCResourceHandle rHandle; OCObservationId obArray[255] = { 0, }; int obCount = 0, i; if (NSPutMessageResource(msg, &rHandle) != NS_OK) { NS_LOG(ERROR, "fail to Put notification resource"); return NS_ERROR; } OCRepPayload* payload = NULL; if (NSSetMessagePayload(msg, &payload) != NS_OK) { NS_LOG(ERROR, "fail to Get message payload"); return NS_ERROR; } #ifdef WITH_MQ if (NSGetMQServerInfo()) { NSProviderPublishTopic(OCRepPayloadClone(payload), NSProviderPublishMQResponseCB); } #endif if (consumerSubList->head == NULL) { NS_LOG(ERROR, "SubList->head is NULL, empty SubList"); OCRepPayloadDestroy(payload); msg->extraInfo = NULL; return NS_ERROR; } NSCacheElement * it = consumerSubList->head; while (it) { NSCacheSubData * subData = (NSCacheSubData *) it->data; NS_LOG_V(INFO_PRIVATE, "message subData->id = %s", subData->id); NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId); NS_LOG_V(DEBUG, "subData->cloud_messageId = %d", subData->remote_messageObId); NS_LOG_V(DEBUG, "subData->syncId = %d", subData->syncObId); NS_LOG_V(DEBUG, "subData->cloud_syncId = %d", subData->remote_syncObId); NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite); if (subData->isWhite) { if(subData->messageObId != 0) { if (msg->topic && (msg->topic)[0] != '\0') { NS_LOG_V(DEBUG, "this is topic message: %s", msg->topic); if (NSProviderIsTopicSubScribed(consumerTopicList->head, subData->id, msg->topic)) { obArray[obCount++] = subData->messageObId; } } else { obArray[obCount++] = subData->messageObId; } } #if (defined WITH_CLOUD) if (subData->remote_messageObId != 0) { if (msg->topic && (msg->topic)[0] != '\0') { NS_LOG_V(DEBUG, "this is topic message via remote server: %s", msg->topic); if (NSProviderIsTopicSubScribed(consumerTopicList->head, subData->id, msg->topic)) { obArray[obCount++] = subData->remote_messageObId; } } else { obArray[obCount++] = subData->remote_messageObId; } } #endif } it = it->next; } for (i = 0; i < obCount; ++i) { NS_LOG(DEBUG, "-------------------------------------------------------message\n"); NS_LOG_V(DEBUG, "SubScription WhiteList[%d] = %d", i, obArray[i]); NS_LOG(DEBUG, "-------------------------------------------------------message\n"); } if (!obCount) { NS_LOG(ERROR, "observer count is zero"); OCRepPayloadDestroy(payload); msg->extraInfo = NULL; return NS_ERROR; } OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload, OC_LOW_QOS); NS_LOG_V(DEBUG, "Message ocstackResult = %d", ocstackResult); if (ocstackResult != OC_STACK_OK) { NS_LOG(ERROR, "fail to send message"); OCRepPayloadDestroy(payload); msg->extraInfo = NULL; return NS_ERROR; } OCRepPayloadDestroy(payload); msg->extraInfo = NULL; NS_LOG(DEBUG, "NSSendMessage - OUT"); return NS_OK; }
void *ChangeLightRepresentation (void *param) { (void)param; OCStackResult result = OC_STACK_ERROR; uint8_t j = 0; uint8_t numNotifies = (SAMPLE_MAX_NUM_OBSERVATIONS)/2; OCObservationId obsNotify[numNotifies]; while (!gQuitFlag) { sleep(10); Light.power += 5; if (gLightUnderObservation) { OC_LOG_V(INFO, TAG, " =====> Notifying stack of new power level %d\n", Light.power); if (gObserveNotifyType == 1) { // Notify list of observers. Alternate observers on the list will be notified. j = 0; for (uint8_t i = 0; i < SAMPLE_MAX_NUM_OBSERVATIONS; (i=i+2)) { if (interestedObservers[i].valid == true) { obsNotify[j] = interestedObservers[i].observationId; j++; } } cJSON *json = cJSON_CreateObject(); cJSON *format; cJSON_AddStringToObject(json,"href",gResourceUri); cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject()); cJSON_AddStringToObject(format, "state", (char *) (Light.state ? "on":"off")); cJSON_AddNumberToObject(format, "power", Light.power); char * obsResp = cJSON_Print(json); cJSON_Delete(json); result = OCNotifyListOfObservers (Light.handle, obsNotify, j, (unsigned char *)obsResp, OC_NA_QOS); free(obsResp); } else if (gObserveNotifyType == 0) { // Notifying all observers result = OCNotifyAllObservers (Light.handle, OC_NA_QOS); if (OC_STACK_NO_OBSERVERS == result) { OC_LOG (INFO, TAG, "=======> No more observers exist, stop sending observations"); gLightUnderObservation = 0; } } else { OC_LOG (ERROR, TAG, "Incorrect notification type selected"); } } #ifdef WITH_PRESENCE if(stopPresenceCount > 0) { OC_LOG_V(INFO, TAG, "================ presence count %d", stopPresenceCount); } if(!stopPresenceCount--) { OC_LOG(INFO, TAG, "================ stopping presence"); OCStopPresence(); } #endif } return NULL; }