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);
48 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_ID, msg->mId);
49 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TITLE, msg->mTitle);
50 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TEXT, msg->mContentText);
51 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_SOURCE, msg->mSource);
53 NS_LOG(DEBUG, "NSSetMessagePayload - OUT");
57 NSResult NSSetSyncPayload(NSSync *sync, OCRepPayload** syncPayload)
59 NS_LOG(DEBUG, "NSSetSyncPayload - IN");
61 *syncPayload = OCRepPayloadCreate();
65 NS_LOG(ERROR, "Failed to allocate payload");
69 OCRepPayloadSetUri(*syncPayload, NS_COLLECTION_SYNC_URI);
71 NSDuplicateSetPropertyString(syncPayload, NS_ATTRIBUTE_ID, sync->mMessageId);
72 NSDuplicateSetPropertyString(syncPayload, NS_ATTRIBUTE_SOURCE, sync->mSourceId);
74 OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->mState);
76 NS_LOG(DEBUG, "NSSetSyncPayload - OUT");
80 NSResult NSSendNotification(NSMessage *msg)
82 NS_LOG(DEBUG, "NSSendMessage - IN");
84 OCResourceHandle rHandle;
85 OCObservationId obArray[255] = { 0, };
88 if (NSPutMessageResource(msg, &rHandle) != NS_OK)
90 NS_LOG(ERROR, "fail to Put notification resource");
94 if (consumerSubList->head == NULL)
96 NS_LOG(ERROR, "SubList->head is NULL, empty SubList");
100 OCRepPayload* payload;
102 if (NSSetMessagePayload(msg, &payload) != NS_OK)
104 NS_LOG(ERROR, "fail to Get message payload");
108 NSCacheElement * it = consumerSubList->head;
112 NSCacheSubData * subData = (NSCacheSubData *) it->data;
113 NS_LOG_V(DEBUG, "subData->id = %s", subData->id);
114 NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId);
115 NS_LOG_V(DEBUG, "subData->obID = %d", subData->syncObId);
116 NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite);
118 if (subData->isWhite)
120 obArray[obCount++] = subData->messageObId;
125 NS_LOG_V(DEBUG, "observer Count = %d", obCount);
127 for (i = 0; i < obCount; ++i)
129 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
130 NS_LOG_V(DEBUG, "SubScription WhiteList[%d] = %d", i, obArray[i]);
131 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
134 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload,
137 NS_LOG_V(DEBUG, "Message ocstackResult = %d", ocstackResult);
139 if (ocstackResult != OC_STACK_OK)
141 NS_LOG(ERROR, "fail to send message");
142 OCRepPayloadDestroy(payload);
145 OCRepPayloadDestroy(payload);
148 NS_LOG(DEBUG, "NSSendMessage - OUT");
153 NSResult NSSendSync(NSSync *sync)
155 NS_LOG(DEBUG, "NSSendSync - IN");
157 OCObservationId obArray[255] = { 0, };
161 OCResourceHandle rHandle;
162 if (NSPutSyncResource(sync, &rHandle) != NS_OK)
164 NS_LOG(ERROR, PCF("Fail to put sync resource"));
168 NSCacheElement * it = consumerSubList->head;
172 NSCacheSubData * subData = (NSCacheSubData *) it->data;
173 if (subData->isWhite)
175 obArray[obCount++] = subData->syncObId;
180 OCRepPayload* payload;
181 if (NSSetSyncPayload(sync, &payload) != NS_OK)
183 NS_LOG(ERROR, "Failed to allocate payload");
187 for (i = 0; i < obCount; ++i)
189 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
190 NS_LOG_V(DEBUG, "Sync WhiteList[%d] = %d", i, obArray[i]);
191 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
194 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray,
195 obCount, payload, OC_LOW_QOS);
197 NS_LOG_V(DEBUG, "Sync ocstackResult = %d", ocstackResult);
199 if (ocstackResult != OC_STACK_OK)
201 NS_LOG(ERROR, "fail to send Sync");
202 OCRepPayloadDestroy(payload);
207 OCRepPayloadDestroy(payload);
209 NS_LOG(DEBUG, "NSSendSync - OUT");
213 void * NSNotificationSchedule(void *ptr)
217 NS_LOG(DEBUG, "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 NS_LOG(DEBUG, "CASE TASK_SEND_NOTIFICATION : ");
235 NSSendNotification((NSMessage *)node->taskData);
239 NS_LOG(DEBUG, "CASE TASK_SEND_READ : ");
240 NSSendSync((NSSync*) node->taskData);
241 NSFreeSync((NSSync*) node->taskData);
244 NS_LOG(DEBUG, "CASE TASK_RECV_READ : ");
245 NSSendSync((NSSync*) node->taskData);
246 NSPushQueue(INTERFACE_SCHEDULER, TASK_CB_SYNC, node->taskData);
250 NS_LOG(ERROR, "Unknown type message");
257 pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);
261 NS_LOG(INFO, "Destroy NSNotificationSchedule");