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 NSSetMessagePayload(NSMessage *msg, OCRepPayload** msgPayload)
25 NS_LOG(DEBUG, "NSSetMessagePayload - IN");
27 *msgPayload = OCRepPayloadCreate();
31 NS_LOG(ERROR, "Failed to allocate payload");
35 OCRepPayloadSetUri(*msgPayload, NS_COLLECTION_MESSAGE_URI);
36 OCRepPayloadSetPropInt(*msgPayload, NS_ATTRIBUTE_MESSAGE_ID, msg->messageId);
37 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_PROVIDER_ID, msg->providerId);
39 NSDuplicateSetPropertyInt(msgPayload, NS_ATTRIBUTE_TYPE, msg->type);
40 NSDuplicateSetPropertyInt(msgPayload, NS_ATTRIBUTE_MESSAGE_ID, msg->ttl);
41 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_DATETIME, msg->dateTime);
42 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TITLE, msg->title);
43 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TEXT, msg->contentText);
44 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_SOURCE, msg->sourceName);
45 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TOPIC_NAME, msg->topic);
47 NS_LOG(DEBUG, "NSSetMessagePayload - OUT");
51 NSResult NSSetSyncPayload(NSSyncInfo *sync, OCRepPayload** syncPayload)
53 NS_LOG(DEBUG, "NSSetSyncPayload - IN");
55 *syncPayload = OCRepPayloadCreate();
59 NS_LOG(ERROR, "Failed to allocate payload");
63 OCRepPayloadSetUri(*syncPayload, NS_COLLECTION_SYNC_URI);
65 OCRepPayloadSetPropString(*syncPayload, NS_ATTRIBUTE_PROVIDER_ID, sync->providerId);
66 OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_MESSAGE_ID, sync->messageId);
67 OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->state);
69 NS_LOG(DEBUG, "NSSetSyncPayload - OUT");
73 NSResult NSSendNotification(NSMessage *msg)
75 NS_LOG(DEBUG, "NSSendMessage - IN");
77 OCResourceHandle rHandle;
78 OCObservationId obArray[255] = { 0, };
81 if (NSPutMessageResource(msg, &rHandle) != NS_OK)
83 NS_LOG(ERROR, "fail to Put notification resource");
87 if (consumerSubList->head == NULL)
89 NS_LOG(ERROR, "SubList->head is NULL, empty SubList");
93 OCRepPayload* payload = NULL;
95 if (NSSetMessagePayload(msg, &payload) != NS_OK)
97 NS_LOG(ERROR, "fail to Get message payload");
101 NSCacheElement * it = consumerSubList->head;
105 NSCacheSubData * subData = (NSCacheSubData *) it->data;
106 NS_LOG_V(DEBUG, "message subData->id = %s", subData->id);
107 NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId);
108 NS_LOG_V(DEBUG, "subData->cloud_messageId = %d", subData->remote_messageObId);
109 NS_LOG_V(DEBUG, "subData->syncId = %d", subData->syncObId);
110 NS_LOG_V(DEBUG, "subData->cloud_syncId = %d", subData->remote_syncObId);
111 NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite);
113 if (subData->isWhite)
115 if(subData->messageObId != 0)
119 NS_LOG_V(DEBUG, "this is topic message: %s", msg->topic);
121 if(NSProviderIsTopicSubScribed(consumerTopicList->head, subData->id, msg->topic))
123 obArray[obCount++] = subData->messageObId;
128 obArray[obCount++] = subData->messageObId;
133 if(subData->remote_messageObId != 0)
135 if(NSProviderIsTopicSubScribed(consumerTopicList->head, subData->id, msg->topic))
137 obArray[obCount++] = subData->remote_messageObId;
141 obArray[obCount++] = subData->remote_messageObId;
150 for (i = 0; i < obCount; ++i)
152 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
153 NS_LOG_V(DEBUG, "SubScription WhiteList[%d] = %d", i, obArray[i]);
154 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
159 NS_LOG(ERROR, "observer count is zero");
163 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload,
166 NS_LOG_V(DEBUG, "Message ocstackResult = %d", ocstackResult);
168 if (ocstackResult != OC_STACK_OK)
170 NS_LOG(ERROR, "fail to send message");
171 OCRepPayloadDestroy(payload);
175 OCRepPayloadDestroy(payload);
177 NS_LOG(DEBUG, "NSSendMessage - OUT");
182 NSResult NSSendSync(NSSyncInfo *sync)
184 NS_LOG(DEBUG, "NSSendSync - IN");
186 OCObservationId obArray[255] = { 0, };
190 OCResourceHandle rHandle;
191 if (NSPutSyncResource(sync, &rHandle) != NS_OK)
193 NS_LOG(ERROR, PCF("Fail to put sync resource"));
197 NSCacheElement * it = consumerSubList->head;
201 NSCacheSubData * subData = (NSCacheSubData *) it->data;
202 NS_LOG_V(DEBUG, "sync subData->id = %s", subData->id);
203 NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId);
204 NS_LOG_V(DEBUG, "subData->cloud_messageId = %d", subData->remote_messageObId);
205 NS_LOG_V(DEBUG, "subData->syncId = %d", subData->syncObId);
206 NS_LOG_V(DEBUG, "subData->cloud_syncId = %d", subData->remote_syncObId);
207 NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite);
209 if (subData->isWhite)
211 if(subData->syncObId != 0)
213 obArray[obCount++] = subData->syncObId;
217 if(subData->remote_syncObId != 0)
219 obArray[obCount++] = subData->remote_syncObId;
226 OCRepPayload* payload;
227 if (NSSetSyncPayload(sync, &payload) != NS_OK)
229 NS_LOG(ERROR, "Failed to allocate payload");
233 for (i = 0; i < obCount; ++i)
235 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
236 NS_LOG_V(DEBUG, "Sync WhiteList[%d] = %d", i, obArray[i]);
237 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
240 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray,
241 obCount, payload, OC_LOW_QOS);
243 NS_LOG_V(DEBUG, "Sync ocstackResult = %d", ocstackResult);
245 if (ocstackResult != OC_STACK_OK)
247 NS_LOG(ERROR, "fail to send Sync");
248 OCRepPayloadDestroy(payload);
252 OCRepPayloadDestroy(payload);
254 NS_LOG(DEBUG, "NSSendSync - OUT");
258 void * NSNotificationSchedule(void *ptr)
262 NS_LOG(DEBUG, "Create NSNotifiactionSchedule");
265 while (NSIsRunning[NOTIFICATION_SCHEDULER])
267 sem_wait(&NSSemaphore[NOTIFICATION_SCHEDULER]);
268 pthread_mutex_lock(&NSMutex[NOTIFICATION_SCHEDULER]);
270 if (NSHeadMsg[NOTIFICATION_SCHEDULER] != NULL)
272 NSTask *node = NSHeadMsg[NOTIFICATION_SCHEDULER];
273 NSHeadMsg[NOTIFICATION_SCHEDULER] = node->nextTask;
275 switch (node->taskType)
277 case TASK_SEND_NOTIFICATION:
279 NS_LOG(DEBUG, "CASE TASK_SEND_NOTIFICATION : ");
280 NSSendNotification((NSMessage *)node->taskData);
281 NSFreeMessage((NSMessage *)node->taskData);
285 NS_LOG(DEBUG, "CASE TASK_SEND_READ : ");
286 NSSendSync((NSSyncInfo*) node->taskData);
287 NSFreeSync((NSSyncInfo*) node->taskData);
290 NS_LOG(DEBUG, "CASE TASK_RECV_READ : ");
291 NSSendSync((NSSyncInfo*) node->taskData);
292 NSPushQueue(CALLBACK_RESPONSE_SCHEDULER, TASK_CB_SYNC, node->taskData);
295 NS_LOG(ERROR, "Unknown type message");
302 pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);
306 NS_LOG(INFO, "Destroy NSNotificationSchedule");