1 //******************************************************************
3 // Copyright 2016 Samsung Electronics 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "NSProviderNotification.h"
23 NSResult NSInitMessageList()
25 messageList = NSCacheCreate();
26 messageList->cacheType = NS_PROVIDER_CACHE_MESSAGE;
30 NSResult NSGetMessagePayload(NSMessage *msg, OCRepPayload** msgPayload)
33 *msgPayload = OCRepPayloadCreate();
37 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
41 OCRepPayloadSetUri(*msgPayload, NSGetNotificationMessageUri());
42 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_ID, msg->mId);
43 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TITLE, msg->mTitle);
44 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TEXT, msg->mContentText);
49 NSResult NSGetSyncPayload(NSSync *sync, OCRepPayload** syncPayload)
52 *syncPayload = OCRepPayloadCreate();
56 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
60 OCRepPayloadSetUri(*syncPayload, NSGetNotificationSyncUri());
61 OCRepPayloadSetPropString(*syncPayload, NS_ATTRIBUTE_ID, sync->mMessageId);
62 OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->mState);
67 NSResult NSSendMessage(NSMessage *msg)
69 OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Message to consumer");
71 // Set Resource and get resource handle
72 OCResourceHandle rHandle;
73 OCObservationId obArray[255] =
76 if (NSPutMessageResource(msg, &rHandle) != NS_OK)
78 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put notification resource"));
82 // Send Notification to subscribers
84 if (consumerSubList->head == NULL)
86 printf("printf - no observers (consumerSubList->head == NULL)\n");
87 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("no observers"));
91 OCRepPayload* payload;
92 printf("printf - no observers - 1\n");
93 if (NSGetMessagePayload(msg, &payload) != NS_OK)
95 printf("printf - no observers - 2\n");
96 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
100 printf("printf - no observers - 3\n");
101 NSCacheElement * it = consumerSubList->head;
102 printf("printf - no observers - 4\n");
105 printf("printf - no observers - 5\n");
106 NSCacheSubData * subData = (NSCacheSubData *) it->data;
107 printf("NS_ subData->id = %s\n", subData->id);
108 printf("NS_ subData->messageId = %d\n", subData->messageObId);
109 printf("NS_ subData->obID = %d\n", subData->syncObId);
110 printf("NS_ subData->isWhite = %d\n", subData->isWhite);
112 printf("printf - no observers - 6\n");
113 if (subData->isWhite)
115 printf("printf - no observers - 7\n");
116 obArray[obCount++] = subData->messageObId;
117 printf("printf - no observers - 8\n");
122 printf("printf - no observers - 9\n");
123 for (i = 0; i < obCount; ++i)
125 printf("NS_ -------------------------------------------------------message\n");
126 printf("NS_ whiteList->idList[%d] = %d\n", i, obArray[i]);
127 printf("NS_ -------------------------------------------------------message\n");
129 printf("printf - no observers - 10\n");
130 // Notify message to subscribers
132 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload,
134 printf("NS_ message ocstackResult = %d\n", ocstackResult);
136 if (ocstackResult != OC_STACK_OK)
138 printf("printf - no observers - 11\n");
139 OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send message");
140 OCRepPayloadDestroy(payload);
144 printf("printf - no observers - 12\n");
145 OCRepPayloadDestroy(payload);
150 NSResult NSSendSync(NSSync *sync)
152 OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Sync to consumer");
154 OCObservationId obArray[255] = { 0, };
158 OCResourceHandle rHandle;
159 if (NSPutSyncResource(sync, &rHandle) != NS_OK)
161 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put sync resource"));
165 NSCacheElement * it = consumerSubList->head;
169 NSCacheSubData * subData = (NSCacheSubData *) it->data;
170 if (subData->isWhite)
172 obArray[obCount++] = subData->syncObId;
179 // Send sync to subscribers
181 OCRepPayload* payload;
182 if (NSGetSyncPayload(sync, &payload) != NS_OK)
184 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
188 // Notify sync to subscribers
190 for (i = 0; i < obCount; ++i)
192 printf("NS_ -------------------------------------------------------message\n");
193 printf("NS_ whiteList->idList[%d] = %d\n", i, obArray[i]);
194 printf("NS_ -------------------------------------------------------message\n");
197 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray,
198 obCount, payload, OC_LOW_QOS);
200 printf("NS_ sync ocstackResult = %d\n", ocstackResult);
201 if (ocstackResult != OC_STACK_OK)
203 OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send Sync");
204 OCRepPayloadDestroy(payload);
208 OCRepPayloadDestroy(payload);
213 void * NSNotificationSchedule(void *ptr)
217 OIC_LOG(DEBUG, NOTIFICATION_TAG, "Create NSNotifiactionSchedule");
220 while (NSIsRunning[NOTIFICATION_SCHEDULER])
222 sem_wait(&NSSemaphore[NOTIFICATION_SCHEDULER]);
223 pthread_mutex_lock(&NSMutex[NOTIFICATION_SCHEDULER]);
225 if (NSHeadMsg[NOTIFICATION_SCHEDULER] != NULL)
227 NSTask *node = NSHeadMsg[NOTIFICATION_SCHEDULER];
228 NSHeadMsg[NOTIFICATION_SCHEDULER] = node->nextTask;
230 switch (node->taskType)
232 case TASK_SEND_NOTIFICATION:
234 NSMessage * nsMsg = node->taskData;
235 NSSendMessage(nsMsg);
239 NSSendSync((NSSync*) node->taskData);
242 NSSendSync((NSSync*) node->taskData);
243 NSPushQueue(RESPONSE_SCHEDULER, TASK_CB_SYNC, node->taskData);
247 OIC_LOG(ERROR, NOTIFICATION_TAG, "Unknown type message");
254 pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);