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