NSDuplicateMessage copy logic not working(bug fixed).
[platform/upstream/iotivity.git] / service / notification / src / provider / NSProviderNotification.c
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "NSProviderNotification.h"
22
23 NSResult NSInitMessageList()
24 {
25     NS_LOG(DEBUG, "NSInitMessageList - IN");
26
27     messageList = NSCacheCreate();
28     messageList->cacheType = NS_PROVIDER_CACHE_MESSAGE;
29
30     NS_LOG(DEBUG, "NSInitMessageList - OUT");
31     return NS_OK;
32 }
33
34 NSResult NSGetMessagePayload(NSMessage *msg, OCRepPayload** msgPayload)
35 {
36     NS_LOG(DEBUG, "NSGetMessagePayload - IN");
37
38     *msgPayload = OCRepPayloadCreate();
39
40     if (!*msgPayload)
41     {
42         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
43         return NS_ERROR;
44     }
45
46     OCRepPayloadSetUri(*msgPayload, NSGetNotificationMessageUri());
47     OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_ID, msg->mId);
48     OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TITLE, msg->mTitle);
49     OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TEXT, msg->mContentText);
50     OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_SOURCE, msg->mSource);
51
52     NS_LOG(DEBUG, "NSGetMessagePayload - OUT");
53     return NS_OK;
54 }
55
56 NSResult NSGetSyncPayload(NSSync *sync, OCRepPayload** syncPayload)
57 {
58     NS_LOG(DEBUG, "NSGetSyncPayload - IN");
59
60     *syncPayload = OCRepPayloadCreate();
61
62     if (!*syncPayload)
63     {
64         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
65         return NS_ERROR;
66     }
67
68     OCRepPayloadSetUri(*syncPayload, NSGetNotificationSyncUri());
69     OCRepPayloadSetPropString(*syncPayload, NS_ATTRIBUTE_ID, sync->mMessageId);
70     OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->mState);
71
72     NS_LOG(DEBUG, "NSGetSyncPayload - OUT");
73     return NS_OK;
74 }
75
76 NSResult NSSendMessage(NSMessage *msg)
77 {
78     OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Message to consumer");
79     NS_LOG(DEBUG, "NSSendMessage - IN");
80
81     OCResourceHandle rHandle;
82     OCObservationId obArray[255] = { 0, };
83     int obCount = 0, i;
84
85     if (NSPutMessageResource(msg, &rHandle) != NS_OK)
86     {
87         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put notification resource"));
88         NS_LOG(DEBUG, "fail to Put notification resource");
89         return NS_ERROR;
90     }
91
92     if (consumerSubList->head == NULL)
93     {
94         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("no observers"));
95         NS_LOG(ERROR, "SubList->head is NULL, empty SubList");
96         return NS_ERROR;
97     }
98
99     OCRepPayload* payload;
100
101     if (NSGetMessagePayload(msg, &payload) != NS_OK)
102     {
103         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
104         NS_LOG(ERROR, "fail to Get message payload");
105         return NS_ERROR;
106     }
107
108     NSCacheElement * it = consumerSubList->head;
109
110     while (it)
111     {
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);
117
118         if (subData->isWhite)
119         {
120             obArray[obCount++] = subData->messageObId;
121         }
122
123         it = it->next;
124     }
125
126     NS_LOG_V(DEBUG, "observer Count = %d", obCount);
127
128     for (i = 0; i < obCount; ++i)
129     {
130         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
131         NS_LOG_V(DEBUG, "SubScription WhiteList[%d] = %d", i, obArray[i]);
132         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
133     }
134
135     OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload,
136             OC_LOW_QOS);
137
138     NS_LOG_V(DEBUG, "Message ocstackResult = %d", ocstackResult);
139
140     if (ocstackResult != OC_STACK_OK)
141     {
142         OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send message");
143         NS_LOG(ERROR, "fail to send message");
144         OCRepPayloadDestroy(payload);
145         return NS_ERROR;
146     }
147     OCRepPayloadDestroy(payload);
148     NSFreeMessage(msg);
149
150     NS_LOG(DEBUG, "NSSendMessage - OUT");
151
152     return NS_OK;
153 }
154
155 NSResult NSSendSync(NSSync *sync)
156 {
157     OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Sync to consumer");
158     NS_LOG(DEBUG, "NSSendSync - IN");
159
160     OCObservationId obArray[255] = { 0, };
161     int obCount = 0;
162     int i;
163
164     OCResourceHandle rHandle;
165     if (NSPutSyncResource(sync, &rHandle) != NS_OK)
166     {
167         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put sync resource"));
168         return NS_ERROR;
169     }
170
171     NSCacheElement * it = consumerSubList->head;
172
173     while (it)
174     {
175         NSCacheSubData * subData = (NSCacheSubData *) it->data;
176         if (subData->isWhite)
177         {
178             obArray[obCount++] = subData->syncObId;
179         }
180
181         it = it->next;
182
183     }
184
185     OCRepPayload* payload;
186     if (NSGetSyncPayload(sync, &payload) != NS_OK)
187     {
188         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
189         return NS_ERROR;
190     }
191
192     for (i = 0; i < obCount; ++i)
193     {
194         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
195         NS_LOG_V(DEBUG, "Sync WhiteList[%d] = %d", i, obArray[i]);
196         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
197     }
198
199     OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray,
200             obCount, payload, OC_LOW_QOS);
201
202     NS_LOG_V(DEBUG, "Sync ocstackResult = %d", ocstackResult);
203
204     if (ocstackResult != OC_STACK_OK)
205     {
206         OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send Sync");
207         OCRepPayloadDestroy(payload);
208         return NS_ERROR;
209
210     }
211
212     OCRepPayloadDestroy(payload);
213
214     NS_LOG(DEBUG, "NSSendSync - OUT");
215     return NS_OK;
216 }
217
218 void * NSNotificationSchedule(void *ptr)
219 {
220     if (ptr == NULL)
221     {
222         OIC_LOG(DEBUG, NOTIFICATION_TAG, "Create NSNotifiactionSchedule");
223         NS_LOG(DEBUG, "Create NSNotifiactionSchedule");
224     }
225
226     while (NSIsRunning[NOTIFICATION_SCHEDULER])
227     {
228         sem_wait(&NSSemaphore[NOTIFICATION_SCHEDULER]);
229         pthread_mutex_lock(&NSMutex[NOTIFICATION_SCHEDULER]);
230
231         if (NSHeadMsg[NOTIFICATION_SCHEDULER] != NULL)
232         {
233             NSTask *node = NSHeadMsg[NOTIFICATION_SCHEDULER];
234             NSHeadMsg[NOTIFICATION_SCHEDULER] = node->nextTask;
235
236             switch ((int)node->taskType)
237             {
238                 case TASK_SEND_NOTIFICATION:
239                 {
240                     NS_LOG(DEBUG, "CASE TASK_SEND_NOTIFICATION : ");
241                     NSMessage * nsMsg = (NSMessage *)node->taskData;
242                     NSSendMessage(nsMsg);
243                     break;
244                 }
245                 case TASK_SEND_READ:
246                     NS_LOG(DEBUG, "CASE TASK_SEND_READ : ");
247                     NSSendSync((NSSync*) node->taskData);
248                     break;
249                 case TASK_RECV_READ:
250                     NS_LOG(DEBUG, "CASE TASK_RECV_READ : ");
251                     NSSendSync((NSSync*) node->taskData);
252                     NSPushQueue(RESPONSE_SCHEDULER, TASK_CB_SYNC, node->taskData);
253                     break;
254
255                 default:
256                     OIC_LOG(ERROR, NOTIFICATION_TAG, "Unknown type message");
257                     NS_LOG(ERROR, "Unknow type message");
258                     break;
259
260             }
261             OICFree(node);
262         }
263
264         pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);
265
266     }
267     return NULL;
268 }