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