changed log in provider side
[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         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
43         return NS_ERROR;
44     }
45
46     OCRepPayloadSetUri(*msgPayload, NSGetNotificationMessageUri());
47     OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_ID, msg->mId);
48     OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TITLE, msg->mTitle);
49     OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TEXT, msg->mContentText);
50
51     NS_LOG(DEBUG, "NSGetMessagePayload - OUT");
52     return NS_OK;
53 }
54
55 NSResult NSGetSyncPayload(NSSync *sync, OCRepPayload** syncPayload)
56 {
57     NS_LOG(DEBUG, "NSGetSyncPayload - IN");
58
59     *syncPayload = OCRepPayloadCreate();
60
61     if (!*syncPayload)
62     {
63         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
64         return NS_ERROR;
65     }
66
67     OCRepPayloadSetUri(*syncPayload, NSGetNotificationSyncUri());
68     OCRepPayloadSetPropString(*syncPayload, NS_ATTRIBUTE_ID, sync->mMessageId);
69     OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->mState);
70
71     NS_LOG(DEBUG, "NSGetSyncPayload - OUT");
72     return NS_OK;
73 }
74
75 NSResult NSSendMessage(NSMessage *msg)
76 {
77     OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Message to consumer");
78     NS_LOG(DEBUG, "NSSendMessage - IN");
79
80     OCResourceHandle rHandle;
81     OCObservationId obArray[255] = { 0, };
82     int obCount = 0, i;
83
84     if (NSPutMessageResource(msg, &rHandle) != NS_OK)
85     {
86         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put notification resource"));
87         NS_LOG(DEBUG, "fail to Put notification resource");
88         return NS_ERROR;
89     }
90
91     if (consumerSubList->head == NULL)
92     {
93         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("no observers"));
94         NS_LOG(ERROR, "SubList->head is NULL, empty SubList");
95         return NS_ERROR;
96     }
97
98     OCRepPayload* payload;
99
100     if (NSGetMessagePayload(msg, &payload) != NS_OK)
101     {
102         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
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
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         OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send message");
142         NS_LOG(ERROR, "fail to send message");
143         OCRepPayloadDestroy(payload);
144         return NS_ERROR;
145     }
146     OCRepPayloadDestroy(payload);
147
148     NS_LOG(DEBUG, "NSSendMessage - OUT");
149
150     return NS_OK;
151 }
152
153 NSResult NSSendSync(NSSync *sync)
154 {
155     OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Sync to consumer");
156     NS_LOG(DEBUG, "NSSendSync - IN");
157
158     OCObservationId obArray[255] = { 0, };
159     int obCount = 0;
160     int i;
161
162     OCResourceHandle rHandle;
163     if (NSPutSyncResource(sync, &rHandle) != NS_OK)
164     {
165         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put sync resource"));
166         return NS_ERROR;
167     }
168
169     NSCacheElement * it = consumerSubList->head;
170
171     while (it)
172     {
173         NSCacheSubData * subData = (NSCacheSubData *) it->data;
174         if (subData->isWhite)
175         {
176             obArray[obCount++] = subData->syncObId;
177         }
178
179         it = it->next;
180
181     }
182
183     OCRepPayload* payload;
184     if (NSGetSyncPayload(sync, &payload) != NS_OK)
185     {
186         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
187         return NS_ERROR;
188     }
189
190     for (i = 0; i < obCount; ++i)
191     {
192         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
193         NS_LOG_V(DEBUG, "Sync WhiteList[%d] = %d", i, obArray[i]);
194         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
195     }
196
197     OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray,
198             obCount, payload, OC_LOW_QOS);
199
200     NS_LOG_V(DEBUG, "Sync ocstackResult = %d", ocstackResult);
201
202     if (ocstackResult != OC_STACK_OK)
203     {
204         OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send Sync");
205         OCRepPayloadDestroy(payload);
206         return NS_ERROR;
207
208     }
209
210     OCRepPayloadDestroy(payload);
211
212     NS_LOG(DEBUG, "NSSendSync - OUT");
213     return NS_OK;
214 }
215
216 void * NSNotificationSchedule(void *ptr)
217 {
218     if (ptr == NULL)
219     {
220         OIC_LOG(DEBUG, NOTIFICATION_TAG, "Create NSNotifiactionSchedule");
221         NS_LOG(DEBUG, "Create NSNotifiactionSchedule");
222     }
223
224     while (NSIsRunning[NOTIFICATION_SCHEDULER])
225     {
226         sem_wait(&NSSemaphore[NOTIFICATION_SCHEDULER]);
227         pthread_mutex_lock(&NSMutex[NOTIFICATION_SCHEDULER]);
228
229         if (NSHeadMsg[NOTIFICATION_SCHEDULER] != NULL)
230         {
231             NSTask *node = NSHeadMsg[NOTIFICATION_SCHEDULER];
232             NSHeadMsg[NOTIFICATION_SCHEDULER] = node->nextTask;
233
234             switch ((int)node->taskType)
235             {
236                 case TASK_SEND_NOTIFICATION:
237                 {
238                     NS_LOG(DEBUG, "CASE TASK_SEND_NOTIFICATION : ");
239                     NSMessage * nsMsg = node->taskData;
240                     NSSendMessage(nsMsg);
241                     break;
242                 }
243                 case TASK_SEND_READ:
244                     NS_LOG(DEBUG, "CASE TASK_SEND_READ : ");
245                     NSSendSync((NSSync*) node->taskData);
246                     break;
247                 case TASK_RECV_READ:
248                     NS_LOG(DEBUG, "CASE TASK_RECV_READ : ");
249                     NSSendSync((NSSync*) node->taskData);
250                     NSPushQueue(RESPONSE_SCHEDULER, TASK_CB_SYNC, node->taskData);
251                     break;
252
253                 default:
254                     OIC_LOG(ERROR, NOTIFICATION_TAG, "Unknown type message");
255                     NS_LOG(ERROR, "Unknow type message");
256                     break;
257
258             }
259             OICFree(node);
260         }
261
262         pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);
263
264     }
265     return NULL;
266 }