implementation NSStopProvider and 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         NS_LOG(ERROR, "Failed to allocate payload");
43         return NS_ERROR;
44     }
45
46     OCRepPayloadSetUri(*msgPayload, NSGetNotificationMessageUri());
47     if(msg->mId)
48         OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_ID, msg->mId);
49     if(msg->mTitle)
50         OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TITLE, msg->mTitle);
51     if(msg->mContentText)
52         OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TEXT, msg->mContentText);
53     if(msg->mSource)
54         OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_SOURCE, msg->mSource);
55
56     NS_LOG(DEBUG, "NSGetMessagePayload - OUT");
57     return NS_OK;
58 }
59
60 NSResult NSGetSyncPayload(NSSync *sync, OCRepPayload** syncPayload)
61 {
62     NS_LOG(DEBUG, "NSGetSyncPayload - IN");
63
64     *syncPayload = OCRepPayloadCreate();
65
66     if (!*syncPayload)
67     {
68         NS_LOG(ERROR, "Failed to allocate payload");
69         return NS_ERROR;
70     }
71
72     OCRepPayloadSetUri(*syncPayload, NSGetNotificationSyncUri());
73
74     if(sync->mMessageId)
75     {
76         OCRepPayloadSetPropString(*syncPayload, NS_ATTRIBUTE_ID, sync->mMessageId);
77         OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->mState);
78     }
79
80     NS_LOG(DEBUG, "NSGetSyncPayload - OUT");
81     return NS_OK;
82 }
83
84 NSResult NSSendMessage(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;
105
106     if (NSGetMessagePayload(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, "subData->id = %s", subData->id);
118         NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId);
119         NS_LOG_V(DEBUG, "subData->obID = %d", subData->syncObId);
120         NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite);
121
122         if (subData->isWhite)
123         {
124             obArray[obCount++] = subData->messageObId;
125         }
126
127         it = it->next;
128     }
129
130     NS_LOG_V(DEBUG, "observer Count = %d", obCount);
131
132     for (i = 0; i < obCount; ++i)
133     {
134         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
135         NS_LOG_V(DEBUG, "SubScription WhiteList[%d] = %d", i, obArray[i]);
136         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
137     }
138
139     OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload,
140             OC_LOW_QOS);
141
142     NS_LOG_V(DEBUG, "Message ocstackResult = %d", ocstackResult);
143
144     if (ocstackResult != OC_STACK_OK)
145     {
146         NS_LOG(ERROR, "fail to send message");
147         OCRepPayloadDestroy(payload);
148         return NS_ERROR;
149     }
150     OCRepPayloadDestroy(payload);
151     NSFreeMessage(msg);
152
153     NS_LOG(DEBUG, "NSSendMessage - OUT");
154
155     return NS_OK;
156 }
157
158 NSResult NSSendSync(NSSync *sync)
159 {
160     NS_LOG(DEBUG, "NSSendSync - IN");
161
162     OCObservationId obArray[255] = { 0, };
163     int obCount = 0;
164     int i;
165
166     OCResourceHandle rHandle;
167     if (NSPutSyncResource(sync, &rHandle) != NS_OK)
168     {
169         NS_LOG(ERROR, PCF("Fail to put sync resource"));
170         return NS_ERROR;
171     }
172
173     NSCacheElement * it = consumerSubList->head;
174
175     while (it)
176     {
177         NSCacheSubData * subData = (NSCacheSubData *) it->data;
178         if (subData->isWhite)
179         {
180             obArray[obCount++] = subData->syncObId;
181         }
182
183         it = it->next;
184
185     }
186
187     OCRepPayload* payload;
188     if (NSGetSyncPayload(sync, &payload) != NS_OK)
189     {
190         NS_LOG(ERROR, "Failed to allocate payload");
191         return NS_ERROR;
192     }
193
194     for (i = 0; i < obCount; ++i)
195     {
196         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
197         NS_LOG_V(DEBUG, "Sync WhiteList[%d] = %d", i, obArray[i]);
198         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
199     }
200
201     OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray,
202             obCount, payload, OC_LOW_QOS);
203
204     NS_LOG_V(DEBUG, "Sync ocstackResult = %d", ocstackResult);
205
206     if (ocstackResult != OC_STACK_OK)
207     {
208         NS_LOG(ERROR, "fail to send Sync");
209         OCRepPayloadDestroy(payload);
210         return NS_ERROR;
211
212     }
213
214     OCRepPayloadDestroy(payload);
215
216     NS_LOG(DEBUG, "NSSendSync - OUT");
217     return NS_OK;
218 }
219
220 void * NSNotificationSchedule(void *ptr)
221 {
222     if (ptr == NULL)
223     {
224         NS_LOG(DEBUG, "Create NSNotifiactionSchedule");
225     }
226
227     while (NSIsRunning[NOTIFICATION_SCHEDULER])
228     {
229         sem_wait(&NSSemaphore[NOTIFICATION_SCHEDULER]);
230         pthread_mutex_lock(&NSMutex[NOTIFICATION_SCHEDULER]);
231
232         if (NSHeadMsg[NOTIFICATION_SCHEDULER] != NULL)
233         {
234             NSTask *node = NSHeadMsg[NOTIFICATION_SCHEDULER];
235             NSHeadMsg[NOTIFICATION_SCHEDULER] = node->nextTask;
236
237             switch (node->taskType)
238             {
239                 case TASK_SEND_NOTIFICATION:
240                 {
241                     NS_LOG(DEBUG, "CASE TASK_SEND_NOTIFICATION : ");
242                     NSSendMessage((NSMessage *)node->taskData);
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                     NS_LOG(ERROR, "Unknown type message");
257                     break;
258
259             }
260             OICFree(node);
261         }
262
263         pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);
264
265     }
266
267     NS_LOG(INFO, "Destroy NSNotificationSchedule");
268     return NULL;
269 }