Issue fixed about OCEntityHandlerRequest same pointer address.
[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     NSFreeMessage(msg);
148
149     NS_LOG(DEBUG, "NSSendMessage - OUT");
150
151     return NS_OK;
152 }
153
154 NSResult NSSendSync(NSSync *sync)
155 {
156     OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Sync to consumer");
157     NS_LOG(DEBUG, "NSSendSync - IN");
158
159     OCObservationId obArray[255] = { 0, };
160     int obCount = 0;
161     int i;
162
163     OCResourceHandle rHandle;
164     if (NSPutSyncResource(sync, &rHandle) != NS_OK)
165     {
166         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put sync resource"));
167         return NS_ERROR;
168     }
169
170     NSCacheElement * it = consumerSubList->head;
171
172     while (it)
173     {
174         NSCacheSubData * subData = (NSCacheSubData *) it->data;
175         if (subData->isWhite)
176         {
177             obArray[obCount++] = subData->syncObId;
178         }
179
180         it = it->next;
181
182     }
183
184     OCRepPayload* payload;
185     if (NSGetSyncPayload(sync, &payload) != NS_OK)
186     {
187         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
188         return NS_ERROR;
189     }
190
191     for (i = 0; i < obCount; ++i)
192     {
193         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
194         NS_LOG_V(DEBUG, "Sync WhiteList[%d] = %d", i, obArray[i]);
195         NS_LOG(DEBUG, "-------------------------------------------------------message\n");
196     }
197
198     OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray,
199             obCount, payload, OC_LOW_QOS);
200
201     NS_LOG_V(DEBUG, "Sync ocstackResult = %d", ocstackResult);
202
203     if (ocstackResult != OC_STACK_OK)
204     {
205         OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send Sync");
206         OCRepPayloadDestroy(payload);
207         return NS_ERROR;
208
209     }
210
211     OCRepPayloadDestroy(payload);
212
213     NS_LOG(DEBUG, "NSSendSync - OUT");
214     return NS_OK;
215 }
216
217 void * NSNotificationSchedule(void *ptr)
218 {
219     if (ptr == NULL)
220     {
221         OIC_LOG(DEBUG, NOTIFICATION_TAG, "Create NSNotifiactionSchedule");
222         NS_LOG(DEBUG, "Create NSNotifiactionSchedule");
223     }
224
225     while (NSIsRunning[NOTIFICATION_SCHEDULER])
226     {
227         sem_wait(&NSSemaphore[NOTIFICATION_SCHEDULER]);
228         pthread_mutex_lock(&NSMutex[NOTIFICATION_SCHEDULER]);
229
230         if (NSHeadMsg[NOTIFICATION_SCHEDULER] != NULL)
231         {
232             NSTask *node = NSHeadMsg[NOTIFICATION_SCHEDULER];
233             NSHeadMsg[NOTIFICATION_SCHEDULER] = node->nextTask;
234
235             switch ((int)node->taskType)
236             {
237                 case TASK_SEND_NOTIFICATION:
238                 {
239                     NS_LOG(DEBUG, "CASE TASK_SEND_NOTIFICATION : ");
240                     NSMessage * nsMsg = node->taskData;
241                     NSSendMessage(nsMsg);
242                     break;
243                 }
244                 case TASK_SEND_READ:
245                     NS_LOG(DEBUG, "CASE TASK_SEND_READ : ");
246                     NSSendSync((NSSync*) node->taskData);
247                     break;
248                 case TASK_RECV_READ:
249                     NS_LOG(DEBUG, "CASE TASK_RECV_READ : ");
250                     NSSendSync((NSSync*) node->taskData);
251                     NSPushQueue(RESPONSE_SCHEDULER, TASK_CB_SYNC, node->taskData);
252                     break;
253
254                 default:
255                     OIC_LOG(ERROR, NOTIFICATION_TAG, "Unknown type message");
256                     NS_LOG(ERROR, "Unknow type message");
257                     break;
258
259             }
260             OICFree(node);
261         }
262
263         pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);
264
265     }
266     return NULL;
267 }