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