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 = NSCacheCreate();
28 messageList->cacheType = NS_PROVIDER_CACHE_MESSAGE;
30 NS_LOG(DEBUG, "NSInitMessageList - OUT");
34 NSResult NSGetMessagePayload(NSMessage *msg, OCRepPayload** msgPayload)
36 NS_LOG(DEBUG, "NSGetMessagePayload - IN");
38 *msgPayload = OCRepPayloadCreate();
42 NS_LOG(ERROR, "Failed to allocate payload");
46 OCRepPayloadSetUri(*msgPayload, NS_COLLECTION_MESSAGE_URI);
48 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_ID, msg->mId);
50 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TITLE, msg->mTitle);
52 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TEXT, msg->mContentText);
54 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_SOURCE, msg->mSource);
56 NS_LOG(DEBUG, "NSGetMessagePayload - OUT");
60 NSResult NSGetSyncPayload(NSSync *sync, OCRepPayload** syncPayload)
62 NS_LOG(DEBUG, "NSGetSyncPayload - IN");
64 *syncPayload = OCRepPayloadCreate();
68 NS_LOG(ERROR, "Failed to allocate payload");
72 OCRepPayloadSetUri(*syncPayload, NS_COLLECTION_SYNC_URI);
75 OCRepPayloadSetPropString(*syncPayload, NS_ATTRIBUTE_ID, sync->mMessageId);
76 OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->mState);
79 NS_LOG(DEBUG, "NSGetSyncPayload - OUT");
83 NSResult NSSendMessage(NSMessage *msg)
85 NS_LOG(DEBUG, "NSSendMessage - IN");
87 OCResourceHandle rHandle;
88 OCObservationId obArray[255] = { 0, };
91 if (NSPutMessageResource(msg, &rHandle) != NS_OK)
93 NS_LOG(ERROR, "fail to Put notification resource");
97 if (consumerSubList->head == NULL)
99 NS_LOG(ERROR, "SubList->head is NULL, empty SubList");
103 OCRepPayload* payload;
105 if (NSGetMessagePayload(msg, &payload) != NS_OK)
107 NS_LOG(ERROR, "fail to Get message payload");
111 NSCacheElement * it = consumerSubList->head;
115 NSCacheSubData * subData = (NSCacheSubData *) it->data;
116 NS_LOG_V(DEBUG, "subData->id = %s", subData->id);
117 NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId);
118 NS_LOG_V(DEBUG, "subData->obID = %d", subData->syncObId);
119 NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite);
121 if (subData->isWhite)
123 obArray[obCount++] = subData->messageObId;
128 NS_LOG_V(DEBUG, "observer Count = %d", obCount);
130 for (i = 0; i < obCount; ++i)
132 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
133 NS_LOG_V(DEBUG, "SubScription WhiteList[%d] = %d", i, obArray[i]);
134 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
137 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload,
140 NS_LOG_V(DEBUG, "Message ocstackResult = %d", ocstackResult);
142 if (ocstackResult != OC_STACK_OK)
144 NS_LOG(ERROR, "fail to send message");
145 OCRepPayloadDestroy(payload);
148 OCRepPayloadDestroy(payload);
151 NS_LOG(DEBUG, "NSSendMessage - OUT");
156 NSResult NSSendSync(NSSync *sync)
158 NS_LOG(DEBUG, "NSSendSync - IN");
160 OCObservationId obArray[255] = { 0, };
164 OCResourceHandle rHandle;
165 if (NSPutSyncResource(sync, &rHandle) != NS_OK)
167 NS_LOG(ERROR, PCF("Fail to put sync resource"));
171 NSCacheElement * it = consumerSubList->head;
175 NSCacheSubData * subData = (NSCacheSubData *) it->data;
176 if (subData->isWhite)
178 obArray[obCount++] = subData->syncObId;
183 OCRepPayload* payload;
184 if (NSGetSyncPayload(sync, &payload) != NS_OK)
186 NS_LOG(ERROR, "Failed to allocate payload");
190 for (i = 0; i < obCount; ++i)
192 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
193 NS_LOG_V(DEBUG, "Sync WhiteList[%d] = %d", i, obArray[i]);
194 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
197 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray,
198 obCount, payload, OC_LOW_QOS);
200 NS_LOG_V(DEBUG, "Sync ocstackResult = %d", ocstackResult);
202 if (ocstackResult != OC_STACK_OK)
204 NS_LOG(ERROR, "fail to send Sync");
205 OCRepPayloadDestroy(payload);
210 OCRepPayloadDestroy(payload);
212 NS_LOG(DEBUG, "NSSendSync - OUT");
216 void * NSNotificationSchedule(void *ptr)
220 NS_LOG(DEBUG, "Create NSNotifiactionSchedule");
223 while (NSIsRunning[NOTIFICATION_SCHEDULER])
225 sem_wait(&NSSemaphore[NOTIFICATION_SCHEDULER]);
226 pthread_mutex_lock(&NSMutex[NOTIFICATION_SCHEDULER]);
228 if (NSHeadMsg[NOTIFICATION_SCHEDULER] != NULL)
230 NSTask *node = NSHeadMsg[NOTIFICATION_SCHEDULER];
231 NSHeadMsg[NOTIFICATION_SCHEDULER] = node->nextTask;
233 switch (node->taskType)
235 case TASK_SEND_NOTIFICATION:
237 NS_LOG(DEBUG, "CASE TASK_SEND_NOTIFICATION : ");
238 NSSendMessage((NSMessage *)node->taskData);
242 NS_LOG(DEBUG, "CASE TASK_SEND_READ : ");
243 NSSendSync((NSSync*) node->taskData);
246 NS_LOG(DEBUG, "CASE TASK_RECV_READ : ");
247 NSSendSync((NSSync*) node->taskData);
248 NSPushQueue(CALLBACK_SCHEDULER, TASK_CB_SYNC, node->taskData);
252 NS_LOG(ERROR, "Unknown type message");
259 pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);
263 NS_LOG(INFO, "Destroy NSNotificationSchedule");