Merge branch 'notification-service'
[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 = NSStorageCreate();
28     messageList->cacheType = NS_PROVIDER_CACHE_MESSAGE;
29
30     NS_LOG(DEBUG, "NSInitMessageList - OUT");
31     return NS_OK;
32 }
33
34 NSResult NSSetMessagePayload(NSMessage *msg, OCRepPayload** msgPayload)
35 {
36     NS_LOG(DEBUG, "NSSetMessagePayload - IN");
37
38     *msgPayload = OCRepPayloadCreate();
39
40     if (!*msgPayload)
41     {
42         NS_LOG(ERROR, "Failed to allocate payload");
43         return NS_ERROR;
44     }
45
46     OCRepPayloadSetUri(*msgPayload, NS_COLLECTION_MESSAGE_URI);
47     OCRepPayloadSetPropInt(*msgPayload, NS_ATTRIBUTE_MESSAGE_ID, msg->messageId);
48     OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_PROVIDER_ID, msg->providerId);
49
50     NSDuplicateSetPropertyInt(msgPayload, NS_ATTRIBUTE_TYPE, msg->type);
51     NSDuplicateSetPropertyInt(msgPayload, NS_ATTRIBUTE_MESSAGE_ID, msg->ttl);
52     NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_DATETIME, msg->dateTime);
53     NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TITLE, msg->title);
54     NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TEXT, msg->contentText);
55     NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_SOURCE, msg->sourceName);
56     NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TOPIC_NAME, msg->topic);
57
58     NS_LOG(DEBUG, "NSSetMessagePayload - OUT");
59     return NS_OK;
60 }
61
62 NSResult NSSetSyncPayload(NSSyncInfo *sync, OCRepPayload** syncPayload)
63 {
64     NS_LOG(DEBUG, "NSSetSyncPayload - IN");
65
66     *syncPayload = OCRepPayloadCreate();
67
68     if (!*syncPayload)
69     {
70         NS_LOG(ERROR, "Failed to allocate payload");
71         return NS_ERROR;
72     }
73
74     OCRepPayloadSetUri(*syncPayload, NS_COLLECTION_SYNC_URI);
75
76     OCRepPayloadSetPropString(*syncPayload, NS_ATTRIBUTE_PROVIDER_ID, sync->providerId);
77     OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_MESSAGE_ID, sync->messageId);
78     OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->state);
79
80     NS_LOG(DEBUG, "NSSetSyncPayload - OUT");
81     return NS_OK;
82 }
83
84 NSResult NSSendNotification(NSMessage *msg)
85 {
86     NS_LOG(DEBUG, "NSSendMessage - IN");
87
88     OCResourceHandle rHandle;
89     OCObservationId obArray[255] = { 0, };
90     int obCount = 0, i;
91
92     if (NSPutMessageResource(msg, &rHandle) != NS_OK)
93     {
94         NS_LOG(ERROR, "fail to Put notification resource");
95         return NS_ERROR;
96     }
97
98     if (consumerSubList->head == NULL)
99     {
100         NS_LOG(ERROR, "SubList->head is NULL, empty SubList");
101         return NS_ERROR;
102     }
103
104     OCRepPayload* payload = NULL;
105
106     if (NSSetMessagePayload(msg, &payload) != NS_OK)
107     {
108         NS_LOG(ERROR, "fail to Get message payload");
109         return NS_ERROR;
110     }
111
112     NSCacheElement * it = consumerSubList->head;
113
114     while (it)
115     {
116         NSCacheSubData * subData = (NSCacheSubData *) it->data;
117         NS_LOG_V(DEBUG, "message subData->id = %s", subData->id);
118         NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId);
119         NS_LOG_V(DEBUG, "subData->cloud_messageId = %d", subData->remote_messageObId);
120         NS_LOG_V(DEBUG, "subData->syncId = %d", subData->syncObId);
121         NS_LOG_V(DEBUG, "subData->cloud_syncId = %d", subData->remote_syncObId);
122         NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite);
123
124         if (subData->isWhite)
125         {
126             if(subData->messageObId != 0)
127             {
128                 if(msg->topic)
129                 {
130                     NS_LOG_V(DEBUG, "this is topic message: %s", msg->topic);
131
132                     if(NSProviderIsTopicSubScribed(consumerTopicList->head, subData->id, msg->topic))
133                     {
134                         obArray[obCount++] = subData->messageObId;
135                     }
136                 }
137                 else
138                 {
139                     obArray[obCount++] = subData->messageObId;
140                 }
141             }
142
143 #ifdef RD_CLIENT
144             if(subData->remote_messageObId != 0)
145             {
146                 if(NSProviderIsTopicSubScribed(consumerTopicList->head, subData->id, msg->topic))
147                 {
148                     obArray[obCount++] = subData->remote_messageObId;
149                 }
150                 else
151                 {
152                     obArray[obCount++] = subData->remote_messageObId;
153                 }
154             }
155 #endif
156
157         }
158         it = it->next;
159     }
160
161     for (i = 0; i < obCount; ++i)
162     {
163         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
164         NS_LOG_V(DEBUG, "SubScription WhiteList[%d] = %d", i, obArray[i]);
165         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
166     }
167
168     if(!obCount)
169     {
170         NS_LOG(ERROR, "observer count is zero");
171         return NS_ERROR;
172     }
173
174     OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload,
175             OC_LOW_QOS);
176
177     NS_LOG_V(DEBUG, "Message ocstackResult = %d", ocstackResult);
178
179     if (ocstackResult != OC_STACK_OK)
180     {
181         NS_LOG(ERROR, "fail to send message");
182         OCRepPayloadDestroy(payload);
183         return NS_ERROR;
184     }
185
186     OCRepPayloadDestroy(payload);
187
188     NS_LOG(DEBUG, "NSSendMessage - OUT");
189
190     return NS_OK;
191 }
192
193 NSResult NSSendSync(NSSyncInfo *sync)
194 {
195     NS_LOG(DEBUG, "NSSendSync - IN");
196
197     OCObservationId obArray[255] = { 0, };
198     int obCount = 0;
199     int i;
200
201     OCResourceHandle rHandle;
202     if (NSPutSyncResource(sync, &rHandle) != NS_OK)
203     {
204         NS_LOG(ERROR, PCF("Fail to put sync resource"));
205         return NS_ERROR;
206     }
207
208     NSCacheElement * it = consumerSubList->head;
209
210     while (it)
211     {
212         NSCacheSubData * subData = (NSCacheSubData *) it->data;
213         NS_LOG_V(DEBUG, "sync subData->id = %s", subData->id);
214         NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId);
215         NS_LOG_V(DEBUG, "subData->cloud_messageId = %d", subData->remote_messageObId);
216         NS_LOG_V(DEBUG, "subData->syncId = %d", subData->syncObId);
217         NS_LOG_V(DEBUG, "subData->cloud_syncId = %d", subData->remote_syncObId);
218         NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite);
219
220         if (subData->isWhite)
221         {
222             if(subData->syncObId != 0)
223             {
224                 obArray[obCount++] = subData->syncObId;
225             }
226
227 #ifdef RD_CLIENT
228             if(subData->remote_syncObId != 0)
229             {
230                 obArray[obCount++] = subData->remote_syncObId;
231             }
232 #endif
233         }
234         it = it->next;
235     }
236
237     OCRepPayload* payload;
238     if (NSSetSyncPayload(sync, &payload) != NS_OK)
239     {
240         NS_LOG(ERROR, "Failed to allocate payload");
241         return NS_ERROR;
242     }
243
244     for (i = 0; i < obCount; ++i)
245     {
246         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
247         NS_LOG_V(DEBUG, "Sync WhiteList[%d] = %d", i, obArray[i]);
248         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
249     }
250
251     OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray,
252             obCount, payload, OC_LOW_QOS);
253
254     NS_LOG_V(DEBUG, "Sync ocstackResult = %d", ocstackResult);
255
256     if (ocstackResult != OC_STACK_OK)
257     {
258         NS_LOG(ERROR, "fail to send Sync");
259         OCRepPayloadDestroy(payload);
260         return NS_ERROR;
261     }
262
263     OCRepPayloadDestroy(payload);
264
265     NS_LOG(DEBUG, "NSSendSync - OUT");
266     return NS_OK;
267 }
268
269 void * NSNotificationSchedule(void *ptr)
270 {
271     if (ptr == NULL)
272     {
273         NS_LOG(DEBUG, "Create NSNotifiactionSchedule");
274     }
275
276     while (NSIsRunning[NOTIFICATION_SCHEDULER])
277     {
278         sem_wait(&NSSemaphore[NOTIFICATION_SCHEDULER]);
279         pthread_mutex_lock(&NSMutex[NOTIFICATION_SCHEDULER]);
280
281         if (NSHeadMsg[NOTIFICATION_SCHEDULER] != NULL)
282         {
283             NSTask *node = NSHeadMsg[NOTIFICATION_SCHEDULER];
284             NSHeadMsg[NOTIFICATION_SCHEDULER] = node->nextTask;
285
286             switch (node->taskType)
287             {
288                 case TASK_SEND_NOTIFICATION:
289                 {
290                     NS_LOG(DEBUG, "CASE TASK_SEND_NOTIFICATION : ");
291                     NSSendNotification((NSMessage *)node->taskData);
292                     NSFreeMessage((NSMessage *)node->taskData);
293                 }
294                     break;
295                 case TASK_SEND_READ:
296                     NS_LOG(DEBUG, "CASE TASK_SEND_READ : ");
297                     NSSendSync((NSSyncInfo*) node->taskData);
298                     NSFreeSync((NSSyncInfo*) node->taskData);
299                     break;
300                 case TASK_RECV_READ:
301                     NS_LOG(DEBUG, "CASE TASK_RECV_READ : ");
302                     NSSendSync((NSSyncInfo*) node->taskData);
303                     NSPushQueue(CALLBACK_RESPONSE_SCHEDULER, TASK_CB_SYNC, node->taskData);
304                     break;
305                 default:
306                     NS_LOG(ERROR, "Unknown type message");
307                     break;
308
309             }
310             OICFree(node);
311         }
312
313         pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);
314
315     }
316
317     NS_LOG(INFO, "Destroy NSNotificationSchedule");
318     return NULL;
319 }