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