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)
32 *msgPayload = OCRepPayloadCreate();
36 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
40 OCRepPayloadSetUri(*msgPayload, NSGetNotificationMessageUri());
41 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_ID, msg->mId);
42 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TITLE, msg->mTitle);
43 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TEXT, msg->mContentText);
48 NSResult NSGetSyncPayload(NSSync *sync, OCRepPayload** syncPayload)
50 *syncPayload = OCRepPayloadCreate();
54 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
58 OCRepPayloadSetUri(*syncPayload, NSGetNotificationSyncUri());
59 OCRepPayloadSetPropString(*syncPayload, NS_ATTRIBUTE_ID, sync->mMessageId);
60 OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->mState);
65 NSResult NSSendMessage(NSMessage *msg)
67 OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Message to consumer");
69 // Set Resource and get resource handle
70 OCResourceHandle rHandle;
71 OCObservationId obArray[255] =
74 if (NSPutMessageResource(msg, &rHandle) != NS_OK)
76 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put notification resource"));
80 if (consumerSubList->head == NULL)
82 printf("printf - no observers (consumerSubList->head == NULL)\n");
83 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("no observers"));
87 OCRepPayload* payload;
88 printf("printf - no observers - 1\n");
89 if (NSGetMessagePayload(msg, &payload) != NS_OK)
91 printf("printf - no observers - 2\n");
92 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
96 printf("printf - no observers - 3\n");
97 NSCacheElement * it = consumerSubList->head;
98 printf("printf - no observers - 4\n");
101 printf("printf - no observers - 5\n");
102 NSCacheSubData * subData = (NSCacheSubData *) it->data;
103 printf("NS_ subData->id = %s\n", subData->id);
104 printf("NS_ subData->messageId = %d\n", subData->messageObId);
105 printf("NS_ subData->obID = %d\n", subData->syncObId);
106 printf("NS_ subData->isWhite = %d\n", subData->isWhite);
108 printf("printf - no observers - 6\n");
109 if (subData->isWhite)
111 printf("printf - no observers - 7\n");
112 obArray[obCount++] = subData->messageObId;
113 printf("printf - no observers - 8\n");
118 printf("printf - no observers - 9\n");
119 for (i = 0; i < obCount; ++i)
121 printf("NS_ -------------------------------------------------------message\n");
122 printf("NS_ whiteList->idList[%d] = %d\n", i, obArray[i]);
123 printf("NS_ -------------------------------------------------------message\n");
125 printf("printf - no observers - 10\n");
127 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload,
129 printf("NS_ message ocstackResult = %d\n", ocstackResult);
131 if (ocstackResult != OC_STACK_OK)
133 printf("printf - no observers - 11\n");
134 OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send message");
135 OCRepPayloadDestroy(payload);
139 printf("printf - no observers - 12\n");
140 OCRepPayloadDestroy(payload);
145 NSResult NSSendSync(NSSync *sync)
147 OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Sync to consumer");
149 OCObservationId obArray[255] = { 0, };
153 OCResourceHandle rHandle;
154 if (NSPutSyncResource(sync, &rHandle) != NS_OK)
156 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put sync resource"));
160 NSCacheElement * it = consumerSubList->head;
164 NSCacheSubData * subData = (NSCacheSubData *) it->data;
165 if (subData->isWhite)
167 obArray[obCount++] = subData->syncObId;
174 // Send sync to subscribers
176 OCRepPayload* payload;
177 if (NSGetSyncPayload(sync, &payload) != NS_OK)
179 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
183 // Notify sync to subscribers
185 for (i = 0; i < obCount; ++i)
187 printf("NS_ -------------------------------------------------------message\n");
188 printf("NS_ whiteList->idList[%d] = %d\n", i, obArray[i]);
189 printf("NS_ -------------------------------------------------------message\n");
192 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray,
193 obCount, payload, OC_LOW_QOS);
195 printf("NS_ sync ocstackResult = %d\n", ocstackResult);
196 if (ocstackResult != OC_STACK_OK)
198 OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send Sync");
199 OCRepPayloadDestroy(payload);
203 OCRepPayloadDestroy(payload);
208 void * NSNotificationSchedule(void *ptr)
212 OIC_LOG(DEBUG, NOTIFICATION_TAG, "Create NSNotifiactionSchedule");
215 while (NSIsRunning[NOTIFICATION_SCHEDULER])
217 sem_wait(&NSSemaphore[NOTIFICATION_SCHEDULER]);
218 pthread_mutex_lock(&NSMutex[NOTIFICATION_SCHEDULER]);
220 if (NSHeadMsg[NOTIFICATION_SCHEDULER] != NULL)
222 NSTask *node = NSHeadMsg[NOTIFICATION_SCHEDULER];
223 NSHeadMsg[NOTIFICATION_SCHEDULER] = node->nextTask;
225 switch ((int)node->taskType)
227 case TASK_SEND_NOTIFICATION:
229 NSMessage * nsMsg = node->taskData;
230 NSSendMessage(nsMsg);
234 NSSendSync((NSSync*) node->taskData);
237 NSSendSync((NSSync*) node->taskData);
238 NSPushQueue(RESPONSE_SCHEDULER, TASK_CB_SYNC, node->taskData);
242 OIC_LOG(ERROR, NOTIFICATION_TAG, "Unknown type message");
249 pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);