code cleanup.
[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             if(subData->remote_messageObId != 0)
131             {
132                 obArray[obCount++] = subData->remote_messageObId;
133             }
134         }
135         it = it->next;
136     }
137
138     for (i = 0; i < obCount; ++i)
139     {
140         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
141         NS_LOG_V(DEBUG, "SubScription WhiteList[%d] = %d", i, obArray[i]);
142         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
143     }
144
145     if(!obCount)
146     {
147         NS_LOG(ERROR, "observer count is zero");
148         return NS_ERROR;
149     }
150
151     OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload,
152             OC_HIGH_QOS);
153
154     NS_LOG_V(DEBUG, "Message ocstackResult = %d", ocstackResult);
155
156     if (ocstackResult != OC_STACK_OK)
157     {
158         NS_LOG(ERROR, "fail to send message");
159         OCRepPayloadDestroy(payload);
160         return NS_ERROR;
161     }
162     OCRepPayloadDestroy(payload);
163     NSFreeMessage(msg);
164
165     NS_LOG(DEBUG, "NSSendMessage - OUT");
166
167     return NS_OK;
168 }
169
170 NSResult NSSendSync(NSSyncInfo *sync)
171 {
172     NS_LOG(DEBUG, "NSSendSync - IN");
173
174     OCObservationId obArray[255] = { 0, };
175     int obCount = 0;
176     int i;
177
178     OCResourceHandle rHandle;
179     if (NSPutSyncResource(sync, &rHandle) != NS_OK)
180     {
181         NS_LOG(ERROR, PCF("Fail to put sync resource"));
182         return NS_ERROR;
183     }
184
185     NSCacheElement * it = consumerSubList->head;
186
187     while (it)
188     {
189         NSCacheSubData * subData = (NSCacheSubData *) it->data;
190         NS_LOG_V(DEBUG, "sync subData->id = %s", subData->id);
191         NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId);
192         NS_LOG_V(DEBUG, "subData->cloud_messageId = %d", subData->remote_messageObId);
193         NS_LOG_V(DEBUG, "subData->syncId = %d", subData->syncObId);
194         NS_LOG_V(DEBUG, "subData->cloud_syncId = %d", subData->remote_syncObId);
195         NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite);
196
197         if (subData->isWhite)
198         {
199             if(subData->syncObId != 0)
200             {
201                 obArray[obCount++] = subData->syncObId;
202             }
203
204             if(subData->remote_syncObId != 0)
205             {
206                 obArray[obCount++] = subData->remote_syncObId;
207             }
208         }
209         it = it->next;
210     }
211
212     OCRepPayload* payload;
213     if (NSSetSyncPayload(sync, &payload) != NS_OK)
214     {
215         NS_LOG(ERROR, "Failed to allocate payload");
216         return NS_ERROR;
217     }
218
219     for (i = 0; i < obCount; ++i)
220     {
221         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
222         NS_LOG_V(DEBUG, "Sync WhiteList[%d] = %d", i, obArray[i]);
223         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
224     }
225
226     OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray,
227             obCount, payload, OC_HIGH_QOS);
228
229     NS_LOG_V(DEBUG, "Sync ocstackResult = %d", ocstackResult);
230
231     if (ocstackResult != OC_STACK_OK)
232     {
233         NS_LOG(ERROR, "fail to send Sync");
234         OCRepPayloadDestroy(payload);
235         return NS_ERROR;
236     }
237
238     OCRepPayloadDestroy(payload);
239
240     NS_LOG(DEBUG, "NSSendSync - OUT");
241     return NS_OK;
242 }
243
244 void * NSNotificationSchedule(void *ptr)
245 {
246     if (ptr == NULL)
247     {
248         NS_LOG(DEBUG, "Create NSNotifiactionSchedule");
249     }
250
251     while (NSIsRunning[NOTIFICATION_SCHEDULER])
252     {
253         sem_wait(&NSSemaphore[NOTIFICATION_SCHEDULER]);
254         pthread_mutex_lock(&NSMutex[NOTIFICATION_SCHEDULER]);
255
256         if (NSHeadMsg[NOTIFICATION_SCHEDULER] != NULL)
257         {
258             NSTask *node = NSHeadMsg[NOTIFICATION_SCHEDULER];
259             NSHeadMsg[NOTIFICATION_SCHEDULER] = node->nextTask;
260
261             switch (node->taskType)
262             {
263                 case TASK_SEND_NOTIFICATION:
264                 {
265                     NS_LOG(DEBUG, "CASE TASK_SEND_NOTIFICATION : ");
266                     NSSendNotification((NSMessage *)node->taskData);
267                     break;
268                 }
269                 case TASK_SEND_READ:
270                     NS_LOG(DEBUG, "CASE TASK_SEND_READ : ");
271                     NSSendSync((NSSyncInfo*) node->taskData);
272                     NSFreeSync((NSSyncInfo*) node->taskData);
273                     break;
274                 case TASK_RECV_READ:
275                     NS_LOG(DEBUG, "CASE TASK_RECV_READ : ");
276                     NSSendSync((NSSyncInfo*) node->taskData);
277                     NSPushQueue(CALLBACK_RESPONSE_SCHEDULER, TASK_CB_SYNC, node->taskData);
278                     break;
279                 default:
280                     NS_LOG(ERROR, "Unknown type message");
281                     break;
282
283             }
284             OICFree(node);
285         }
286
287         pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);
288
289     }
290
291     NS_LOG(INFO, "Destroy NSNotificationSchedule");
292     return NULL;
293 }