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