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 NS_LOG(DEBUG, "NSInitMessageList - IN");
27 messageList = NSStorageCreate();
28 messageList->cacheType = NS_PROVIDER_CACHE_MESSAGE;
30 NS_LOG(DEBUG, "NSInitMessageList - OUT");
34 NSResult NSSetMessagePayload(NSMessage *msg, OCRepPayload** msgPayload)
36 NS_LOG(DEBUG, "NSSetMessagePayload - IN");
38 *msgPayload = OCRepPayloadCreate();
42 NS_LOG(ERROR, "Failed to allocate payload");
46 OCRepPayloadSetUri(*msgPayload, NS_COLLECTION_MESSAGE_URI);
47 OCRepPayloadSetPropInt(*msgPayload, NS_ATTRIBUTE_MESSAGE_ID, msg->messageId);
48 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_PROVIDER_ID, msg->providerId);
50 NSDuplicateSetPropertyInt(msgPayload, NS_ATTRIBUTE_TYPE, msg->type);
51 NSDuplicateSetPropertyInt(msgPayload, NS_ATTRIBUTE_MESSAGE_ID, msg->ttl);
52 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_DATETIME, msg->dateTime);
53 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TITLE, msg->title);
54 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TEXT, msg->contentText);
55 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_SOURCE, msg->sourceName);
56 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TOPIC_NAME, msg->topic);
58 NS_LOG(DEBUG, "NSSetMessagePayload - OUT");
62 NSResult NSSetSyncPayload(NSSyncInfo *sync, OCRepPayload** syncPayload)
64 NS_LOG(DEBUG, "NSSetSyncPayload - IN");
66 *syncPayload = OCRepPayloadCreate();
70 NS_LOG(ERROR, "Failed to allocate payload");
74 OCRepPayloadSetUri(*syncPayload, NS_COLLECTION_SYNC_URI);
76 OCRepPayloadSetPropString(*syncPayload, NS_ATTRIBUTE_PROVIDER_ID, sync->providerId);
77 OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_MESSAGE_ID, sync->messageId);
78 OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->state);
80 NS_LOG(DEBUG, "NSSetSyncPayload - OUT");
84 NSResult NSSendNotification(NSMessage *msg)
86 NS_LOG(DEBUG, "NSSendMessage - IN");
88 OCResourceHandle rHandle;
89 OCObservationId obArray[255] = { 0, };
92 if (NSPutMessageResource(msg, &rHandle) != NS_OK)
94 NS_LOG(ERROR, "fail to Put notification resource");
98 if (consumerSubList->head == NULL)
100 NS_LOG(ERROR, "SubList->head is NULL, empty SubList");
104 OCRepPayload* payload = NULL;
106 if (NSSetMessagePayload(msg, &payload) != NS_OK)
108 NS_LOG(ERROR, "fail to Get message payload");
112 NSCacheElement * it = consumerSubList->head;
116 NSCacheSubData * subData = (NSCacheSubData *) it->data;
117 NS_LOG_V(DEBUG, "message subData->id = %s", subData->id);
118 NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId);
119 NS_LOG_V(DEBUG, "subData->cloud_messageId = %d", subData->remote_messageObId);
120 NS_LOG_V(DEBUG, "subData->syncId = %d", subData->syncObId);
121 NS_LOG_V(DEBUG, "subData->cloud_syncId = %d", subData->remote_syncObId);
122 NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite);
124 if (subData->isWhite)
126 if(subData->messageObId != 0)
130 NS_LOG_V(DEBUG, "this is topic message: %s", msg->topic);
132 if(NSIsTopicSubscribed(subData->id, msg->topic))
134 obArray[obCount++] = subData->messageObId;
139 obArray[obCount++] = subData->messageObId;
144 if(subData->remote_messageObId != 0)
146 if(NSIsTopicSubscribed(subData->id, msg->topic))
148 obArray[obCount++] = subData->remote_messageObId;
152 obArray[obCount++] = subData->remote_messageObId;
161 for (i = 0; i < obCount; ++i)
163 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
164 NS_LOG_V(DEBUG, "SubScription WhiteList[%d] = %d", i, obArray[i]);
165 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
170 NS_LOG(ERROR, "observer count is zero");
174 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload,
177 NS_LOG_V(DEBUG, "Message ocstackResult = %d", ocstackResult);
179 if (ocstackResult != OC_STACK_OK)
181 NS_LOG(ERROR, "fail to send message");
182 OCRepPayloadDestroy(payload);
186 OCRepPayloadDestroy(payload);
188 NS_LOG(DEBUG, "NSSendMessage - OUT");
193 NSResult NSSendSync(NSSyncInfo *sync)
195 NS_LOG(DEBUG, "NSSendSync - IN");
197 OCObservationId obArray[255] = { 0, };
201 OCResourceHandle rHandle;
202 if (NSPutSyncResource(sync, &rHandle) != NS_OK)
204 NS_LOG(ERROR, PCF("Fail to put sync resource"));
208 NSCacheElement * it = consumerSubList->head;
212 NSCacheSubData * subData = (NSCacheSubData *) it->data;
213 NS_LOG_V(DEBUG, "sync subData->id = %s", subData->id);
214 NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId);
215 NS_LOG_V(DEBUG, "subData->cloud_messageId = %d", subData->remote_messageObId);
216 NS_LOG_V(DEBUG, "subData->syncId = %d", subData->syncObId);
217 NS_LOG_V(DEBUG, "subData->cloud_syncId = %d", subData->remote_syncObId);
218 NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite);
220 if (subData->isWhite)
222 if(subData->syncObId != 0)
224 obArray[obCount++] = subData->syncObId;
228 if(subData->remote_syncObId != 0)
230 obArray[obCount++] = subData->remote_syncObId;
237 OCRepPayload* payload;
238 if (NSSetSyncPayload(sync, &payload) != NS_OK)
240 NS_LOG(ERROR, "Failed to allocate payload");
244 for (i = 0; i < obCount; ++i)
246 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
247 NS_LOG_V(DEBUG, "Sync WhiteList[%d] = %d", i, obArray[i]);
248 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
251 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray,
252 obCount, payload, OC_HIGH_QOS);
254 NS_LOG_V(DEBUG, "Sync ocstackResult = %d", ocstackResult);
256 if (ocstackResult != OC_STACK_OK)
258 NS_LOG(ERROR, "fail to send Sync");
259 OCRepPayloadDestroy(payload);
263 OCRepPayloadDestroy(payload);
265 NS_LOG(DEBUG, "NSSendSync - OUT");
269 void * NSNotificationSchedule(void *ptr)
273 NS_LOG(DEBUG, "Create NSNotifiactionSchedule");
276 while (NSIsRunning[NOTIFICATION_SCHEDULER])
278 sem_wait(&NSSemaphore[NOTIFICATION_SCHEDULER]);
279 pthread_mutex_lock(&NSMutex[NOTIFICATION_SCHEDULER]);
281 if (NSHeadMsg[NOTIFICATION_SCHEDULER] != NULL)
283 NSTask *node = NSHeadMsg[NOTIFICATION_SCHEDULER];
284 NSHeadMsg[NOTIFICATION_SCHEDULER] = node->nextTask;
286 switch (node->taskType)
288 case TASK_SEND_NOTIFICATION:
290 NS_LOG(DEBUG, "CASE TASK_SEND_NOTIFICATION : ");
291 NSSendNotification((NSMessage *)node->taskData);
292 NSFreeMessage((NSMessage *)node->taskData);
296 NS_LOG(DEBUG, "CASE TASK_SEND_READ : ");
297 NSSendSync((NSSyncInfo*) node->taskData);
298 NSFreeSync((NSSyncInfo*) node->taskData);
301 NS_LOG(DEBUG, "CASE TASK_RECV_READ : ");
302 NSSendSync((NSSyncInfo*) node->taskData);
303 NSPushQueue(CALLBACK_RESPONSE_SCHEDULER, TASK_CB_SYNC, node->taskData);
306 NS_LOG(ERROR, "Unknown type message");
313 pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);
317 NS_LOG(INFO, "Destroy NSNotificationSchedule");