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 NSGetObsIdList(OCObservationId *list)
25 // // TODO: return white list
32 uint32_t NSGetNumberOfObsList()
34 // TODO: return # of list
39 NSResult NSGetMessagePayload(NSMessage *msg, OCRepPayload** msgPayload)
42 *msgPayload = OCRepPayloadCreate();
46 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
50 OCRepPayloadSetUri(*msgPayload, NSGetNotificationMessageUri());
51 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_ID, msg->mId);
52 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TITLE, msg->mTitle);
53 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TEXT, msg->mContentText);
58 NSResult NSGetSyncPayload(NSSync *sync, OCRepPayload** syncPayload)
61 *syncPayload = OCRepPayloadCreate();
65 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
69 OCRepPayloadSetUri(*syncPayload, NSGetNotificationSyncUri());
70 OCRepPayloadSetPropString(*syncPayload, NS_ATTRIBUTE_ID, sync->mMessageId);
71 OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->mState);
76 NSResult NSSendMessage(NSMessage *msg)
78 OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Message to consumer");
80 // Set Resource and get resource handle
81 OCResourceHandle rHandle;
82 if (NSPutMessageResource(msg, &rHandle) != NS_OK)
84 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put notification resource"));
88 // Send Notification to subscribers
90 NSWhiteList * whiteList = NSProviderReadCache(NS_CONSUMER_WHITELIST, 0);
92 if(whiteList->size == 0)
94 printf("printf - no observers\n");
95 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("no observers"));
99 OCRepPayload* payload;
100 printf("printf - no observers - 1\n");
101 if (NSGetMessagePayload(msg, &payload) != NS_OK)
103 printf("printf - no observers - 2\n");
104 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
108 for(i = 0; i < whiteList->size; ++i)
110 printf("NS_ -------------------------------------------------------message\n");
111 printf("NS_ whiteList->idList[%d] = %d\n", i, whiteList->idList[i]);
112 printf("NS_ -------------------------------------------------------message\n");
115 // Notify message to subscribers
117 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, whiteList->idList, whiteList->size, payload, OC_LOW_QOS);
118 printf("NS_ message ocstackResult = %d\n", ocstackResult);
121 if (ocstackResult != OC_STACK_OK)
123 printf("printf - no observers - 3\n");
124 OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send message");
125 OCRepPayloadDestroy(payload);
129 printf("printf - no observers - 4\n");
130 OCRepPayloadDestroy(payload);
135 NSResult NSSendSync(NSSync *sync)
137 OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Sync to consumer");
139 // Set Resource and get resource handle
141 OCResourceHandle rHandle;
142 if (NSPutSyncResource(sync, &rHandle) != NS_OK)
144 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put sync resource"));
148 NSWhiteList * whiteList = NSProviderReadCache(NS_CONSUMER_WHITELIST, 1);
150 if(whiteList->size == 0)
152 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("no observers"));
156 // Send sync to subscribers
158 OCRepPayload* payload;
159 if (NSGetSyncPayload(sync, &payload) != NS_OK)
161 OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
165 // Notify sync to subscribers
167 for(i = 0; i < whiteList->size; ++i)
169 printf("NS_ -------------------------------------------------------sync\n");
170 printf("NS_ whiteList->idList[%d] = %d\n", i, whiteList->idList[i]);
171 printf("NS_ -------------------------------------------------------sync\n");
174 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, whiteList->idList, whiteList->size, payload, OC_LOW_QOS);
176 printf("NS_ sync ocstackResult = %d\n", ocstackResult);
177 if (ocstackResult != OC_STACK_OK)
179 OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send Sync");
180 OCRepPayloadDestroy(payload);
184 OCRepPayloadDestroy(payload);
189 void * NSNotificationSchedule(void *ptr)
193 OIC_LOG(DEBUG, NOTIFICATION_TAG, "Create NSNotifiactionSchedule");
196 while (NSIsRunning[NOTIFICATION_SCHEDULER])
198 sem_wait(&NSSemaphore[NOTIFICATION_SCHEDULER]);
199 pthread_mutex_lock(&NSMutex[NOTIFICATION_SCHEDULER]);
201 if (NSHeadMsg[NOTIFICATION_SCHEDULER] != NULL)
203 NSTask *node = NSHeadMsg[NOTIFICATION_SCHEDULER];
204 NSHeadMsg[NOTIFICATION_SCHEDULER] = node->nextTask;
206 switch (node->taskType)
208 case TASK_SEND_NOTIFICATION:
210 NSMessage * nsMsg = node->taskData;
211 NSSendMessage(nsMsg);
215 NSSendSync((NSSync*) node->taskData);
218 NSSendSync((NSSync*) node->taskData);
219 NSPushQueue(RESPONSE_SCHEDULER, TASK_CB_SYNC, node->taskData);
223 OIC_LOG(ERROR, NOTIFICATION_TAG, "Unknown type message");
230 pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);