Clean the warning message in notification service .
[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     messageList = NSCacheCreate();
26     messageList->cacheType = NS_PROVIDER_CACHE_MESSAGE;
27     return NS_OK;
28 }
29
30 NSResult NSGetMessagePayload(NSMessage *msg, OCRepPayload** msgPayload)
31 {
32     *msgPayload = OCRepPayloadCreate();
33
34     if (!*msgPayload)
35     {
36         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
37         return NS_ERROR;
38     }
39
40     OCRepPayloadSetUri(*msgPayload, NSGetNotificationMessageUri());
41     OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_ID, msg->mId);
42     OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TITLE, msg->mTitle);
43     OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TEXT, msg->mContentText);
44
45     return NS_OK;
46 }
47
48 NSResult NSGetSyncPayload(NSSync *sync, OCRepPayload** syncPayload)
49 {
50     *syncPayload = OCRepPayloadCreate();
51
52     if (!*syncPayload)
53     {
54         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
55         return NS_ERROR;
56     }
57
58     OCRepPayloadSetUri(*syncPayload, NSGetNotificationSyncUri());
59     OCRepPayloadSetPropString(*syncPayload, NS_ATTRIBUTE_ID, sync->mMessageId);
60     OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->mState);
61
62     return NS_OK;
63 }
64
65 NSResult NSSendMessage(NSMessage *msg)
66 {
67     OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Message to consumer");
68     int i;
69     // Set Resource and get resource handle
70     OCResourceHandle rHandle;
71     OCObservationId obArray[255] =
72     { 0, };
73     int obCount = 0;
74     if (NSPutMessageResource(msg, &rHandle) != NS_OK)
75     {
76         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put notification resource"));
77         return NS_ERROR;
78     }
79
80     if (consumerSubList->head == NULL)
81     {
82         printf("printf - no observers (consumerSubList->head == NULL)\n");
83         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("no observers"));
84         return NS_ERROR;
85     }
86
87     OCRepPayload* payload;
88     printf("printf - no observers - 1\n");
89     if (NSGetMessagePayload(msg, &payload) != NS_OK)
90     {
91         printf("printf - no observers - 2\n");
92         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
93         return NS_ERROR;
94     }
95
96     printf("printf - no observers - 3\n");
97     NSCacheElement * it = consumerSubList->head;
98     printf("printf - no observers - 4\n");
99     while (it)
100     {
101         printf("printf - no observers - 5\n");
102         NSCacheSubData * subData = (NSCacheSubData *) it->data;
103         printf("NS_ subData->id = %s\n", subData->id);
104         printf("NS_ subData->messageId = %d\n", subData->messageObId);
105         printf("NS_ subData->obID = %d\n", subData->syncObId);
106         printf("NS_ subData->isWhite = %d\n", subData->isWhite);
107
108         printf("printf - no observers - 6\n");
109         if (subData->isWhite)
110         {
111             printf("printf - no observers - 7\n");
112             obArray[obCount++] = subData->messageObId;
113             printf("printf - no observers - 8\n");
114         }
115
116         it = it->next;
117     }
118     printf("printf - no observers - 9\n");
119     for (i = 0; i < obCount; ++i)
120     {
121         printf("NS_ -------------------------------------------------------message\n");
122         printf("NS_ whiteList->idList[%d] = %d\n", i, obArray[i]);
123         printf("NS_ -------------------------------------------------------message\n");
124     }
125     printf("printf - no observers - 10\n");
126
127     OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload,
128             OC_LOW_QOS);
129     printf("NS_ message ocstackResult = %d\n", ocstackResult);
130
131     if (ocstackResult != OC_STACK_OK)
132     {
133         printf("printf - no observers - 11\n");
134         OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send message");
135         OCRepPayloadDestroy(payload);
136         return NS_ERROR;
137
138     }
139     printf("printf - no observers - 12\n");
140     OCRepPayloadDestroy(payload);
141
142     return NS_OK;
143 }
144
145 NSResult NSSendSync(NSSync *sync)
146 {
147     OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Sync to consumer");
148
149     OCObservationId obArray[255] = { 0, };
150     int obCount = 0;
151     int i;
152
153     OCResourceHandle rHandle;
154     if (NSPutSyncResource(sync, &rHandle) != NS_OK)
155     {
156         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put sync resource"));
157         return NS_ERROR;
158     }
159
160     NSCacheElement * it = consumerSubList->head;
161
162     while (it)
163     {
164         NSCacheSubData * subData = (NSCacheSubData *) it->data;
165         if (subData->isWhite)
166         {
167             obArray[obCount++] = subData->syncObId;
168         }
169
170         it = it->next;
171
172     }
173
174     // Send sync to subscribers
175
176     OCRepPayload* payload;
177     if (NSGetSyncPayload(sync, &payload) != NS_OK)
178     {
179         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
180         return NS_ERROR;
181     }
182
183     // Notify sync to subscribers
184
185     for (i = 0; i < obCount; ++i)
186     {
187         printf("NS_ -------------------------------------------------------message\n");
188         printf("NS_ whiteList->idList[%d] = %d\n", i, obArray[i]);
189         printf("NS_ -------------------------------------------------------message\n");
190     }
191
192     OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray,
193             obCount, payload, OC_LOW_QOS);
194
195     printf("NS_ sync ocstackResult = %d\n", ocstackResult);
196     if (ocstackResult != OC_STACK_OK)
197     {
198         OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send Sync");
199         OCRepPayloadDestroy(payload);
200         return NS_ERROR;
201
202     }
203     OCRepPayloadDestroy(payload);
204
205     return NS_OK;
206 }
207
208 void * NSNotificationSchedule(void *ptr)
209 {
210     if (ptr == NULL)
211     {
212         OIC_LOG(DEBUG, NOTIFICATION_TAG, "Create NSNotifiactionSchedule");
213     }
214
215     while (NSIsRunning[NOTIFICATION_SCHEDULER])
216     {
217         sem_wait(&NSSemaphore[NOTIFICATION_SCHEDULER]);
218         pthread_mutex_lock(&NSMutex[NOTIFICATION_SCHEDULER]);
219
220         if (NSHeadMsg[NOTIFICATION_SCHEDULER] != NULL)
221         {
222             NSTask *node = NSHeadMsg[NOTIFICATION_SCHEDULER];
223             NSHeadMsg[NOTIFICATION_SCHEDULER] = node->nextTask;
224
225             switch ((int)node->taskType)
226             {
227                 case TASK_SEND_NOTIFICATION:
228                 {
229                     NSMessage * nsMsg = node->taskData;
230                     NSSendMessage(nsMsg);
231                     break;
232                 }
233                 case TASK_SEND_READ:
234                     NSSendSync((NSSync*) node->taskData);
235                     break;
236                 case TASK_RECV_READ:
237                     NSSendSync((NSSync*) node->taskData);
238                     NSPushQueue(RESPONSE_SCHEDULER, TASK_CB_SYNC, node->taskData);
239                     break;
240
241                 default:
242                     OIC_LOG(ERROR, NOTIFICATION_TAG, "Unknown type message");
243                     break;
244
245             }
246             OICFree(node);
247         }
248
249         pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);
250
251     }
252     return NULL;
253 }