Merge remote-tracking branch 'origin/notification-service'
[platform/upstream/iotivity.git] / service / notification / src / provider / NSProviderInterface.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 "NSProviderInterface.h"
22 #include "NSProviderScheduler.h"
23 #include "NSProviderListener.h"
24 #include "NSProviderSubscription.h"
25 #include "NSProviderNotification.h"
26 #include "NSProviderCallbackResponse.h"
27 #include "NSStorageAdapter.h"
28 #include "NSProviderMemoryCache.h"
29 #include "NSProviderTopic.h"
30 #include "oic_malloc.h"
31 #include "oic_string.h"
32 #include "cautilinterface.h"
33 #include "NSProviderSystem.h"
34 #include "oic_time.h"
35 #include <pthread.h>
36
37 bool initProvider = false;
38
39 pthread_mutex_t nsInitMutex;
40 pthread_cond_t nstopicCond;
41
42 void initializeMutex()
43 {
44     static pthread_mutex_t initMutex = PTHREAD_MUTEX_INITIALIZER;
45     nsInitMutex = initMutex;
46 }
47
48 void NSInitialize()
49 {
50     NS_LOG(DEBUG, "NSSetList - IN");
51
52     pthread_mutexattr_init(&NSCacheMutexAttr);
53     pthread_mutexattr_settype(&NSCacheMutexAttr, PTHREAD_MUTEX_RECURSIVE);
54     pthread_mutex_init(&NSCacheMutex, &NSCacheMutexAttr);
55     pthread_cond_init(&nstopicCond, NULL);
56
57     NSInitSubscriptionList();
58     NSInitTopicList();
59     NS_LOG(DEBUG, "NSSetList - OUT");
60 }
61
62 void NSDeinitailize()
63 {
64     NSStorageDestroy(consumerSubList);
65     NSStorageDestroy(consumerTopicList);
66     NSStorageDestroy(registeredTopicList);
67
68     pthread_mutex_destroy(&NSCacheMutex);
69     pthread_mutexattr_destroy(&NSCacheMutexAttr);
70     pthread_cond_destroy(&nstopicCond);
71 }
72
73 NSResult NSStartProvider(NSProviderConfig config)
74 {
75     NS_LOG(DEBUG, "NSStartProvider - IN");
76
77     initializeMutex();
78
79     pthread_mutex_lock(&nsInitMutex);
80
81     if (!initProvider)
82     {
83         NS_LOG(DEBUG, "Init Provider");
84         initProvider = true;
85         NSInitProviderInfo(config.userInfo);
86         NSSetSubscriptionAccessPolicy(config.subControllability);
87         NSRegisterSubscribeRequestCb(config.subRequestCallback);
88         NSRegisterSyncCb(config.syncInfoCallback);
89         CARegisterNetworkMonitorHandler((CAAdapterStateChangedCB)NSProviderAdapterStateListener,
90                 (CAConnectionStateChangedCB)NSProviderConnectionStateListener);
91
92         NSInitialize();
93         NSInitScheduler();
94         NSStartScheduler();
95
96         NSPushQueue(DISCOVERY_SCHEDULER, TASK_START_PRESENCE, NULL);
97         NSPushQueue(DISCOVERY_SCHEDULER, TASK_REGISTER_RESOURCE, NULL);
98     }
99     else
100     {
101         NS_LOG(DEBUG, "Already started Notification Provider");
102     }
103     pthread_mutex_unlock(&nsInitMutex);
104
105     NS_LOG(DEBUG, "NSStartProvider - OUT");
106     return NS_OK;
107 }
108
109 NSResult NSStopProvider()
110 {
111     NS_LOG(DEBUG, "NSStopProvider - IN");
112     pthread_mutex_lock(&nsInitMutex);
113
114     if(initProvider)
115     {
116         NSPushQueue(DISCOVERY_SCHEDULER, TASK_STOP_PRESENCE, NULL);
117         NSDeinitProviderInfo();
118         NSUnRegisterResource();
119         NSRegisterSubscribeRequestCb((NSSubscribeRequestCallback)NULL);
120         NSRegisterSyncCb((NSProviderSyncInfoCallback)NULL);
121         NSStopScheduler();
122         NSDeinitailize();
123
124         initProvider = false;
125     }
126
127     pthread_mutex_unlock(&nsInitMutex);
128     NS_LOG(DEBUG, "NSStopProvider - OUT");
129     return NS_OK;
130 }
131
132 NSResult NSProviderEnableRemoteService(char *serverAddress)
133 {
134 #if(defined WITH_CLOUD && defined RD_CLIENT)
135     NS_LOG(DEBUG, "NSProviderEnableRemoteService - IN");
136     pthread_mutex_lock(&nsInitMutex);
137
138     if(!initProvider || !serverAddress)
139     {
140         NS_LOG(DEBUG, "Provider service has not been started yet");
141         pthread_mutex_unlock(&nsInitMutex);
142         return NS_FAIL;
143     }
144     NS_LOG_V(DEBUG, "Remote server address: %s", serverAddress);
145     NS_LOG(DEBUG, "Request to publish resource");
146     NSPushQueue(DISCOVERY_SCHEDULER, TASK_PUBLISH_RESOURCE, serverAddress);
147
148     pthread_mutex_unlock(&nsInitMutex);
149     NS_LOG(DEBUG, "NSProviderEnableRemoteService - OUT");
150     return NS_OK;
151 #else
152     (void) serverAddress;
153 #endif
154     NS_LOG_V(DEBUG, "Not logged in remote server: %s", serverAddress);
155     return NS_FAIL;
156 }
157
158 NSResult NSProviderDisableRemoteService(char *serverAddress)
159 {
160 #if(defined WITH_CLOUD && defined RD_CLIENT)
161     NS_LOG(DEBUG, "NSProviderDisableRemoteService - IN");
162     pthread_mutex_lock(&nsInitMutex);
163
164     if(!initProvider || !serverAddress)
165     {
166         NS_LOG(DEBUG, "Provider service has not been started yet");
167         return NS_FAIL;
168     }
169     NS_LOG_V(DEBUG, "Remote server address: %s", serverAddress);
170
171     NS_LOG(DEBUG, "Delete remote server info");
172     NSDeleteRemoteServerAddress(serverAddress);
173
174     pthread_mutex_unlock(&nsInitMutex);
175     NS_LOG(DEBUG, "NSProviderDisableRemoteService - OUT");
176     return NS_OK;
177 #else
178     (void) serverAddress;
179 #endif
180     NS_LOG_V(DEBUG, "Not logged in remote server : %s", serverAddress);
181     return NS_FAIL;
182 }
183
184 NSResult NSSendMessage(NSMessage * msg)
185 {
186     NS_LOG(DEBUG, "NSSendNotification - IN");
187
188     pthread_mutex_lock(&nsInitMutex);
189
190     if(msg == NULL)
191     {
192         NS_LOG(ERROR, "Msg is NULL");
193         pthread_mutex_unlock(&nsInitMutex);
194         return NS_FAIL;
195     }
196
197     NSMessage * newMsg = NSDuplicateMessage(msg);
198     NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_NOTIFICATION, newMsg);
199
200     pthread_mutex_unlock(&nsInitMutex);
201
202     NS_LOG(DEBUG, "NSSendNotification - OUT");
203     return NS_OK;
204 }
205
206 NSResult NSProviderSendSyncInfo(uint64_t messageId, NSSyncType type)
207 {
208     NS_LOG(DEBUG, "NSProviderReadCheck - IN");
209     pthread_mutex_lock(&nsInitMutex);
210
211     NSSyncInfo * syncInfo = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
212     OICStrcpy(syncInfo->providerId, UUID_STRING_SIZE, NSGetProviderInfo()->providerId);
213     syncInfo->messageId = messageId;
214     syncInfo->state = type;
215     NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_READ, syncInfo);
216
217     pthread_mutex_unlock(&nsInitMutex);
218     NS_LOG(DEBUG, "NSProviderReadCheck - OUT");
219     return NS_OK;
220 }
221
222 NSResult NSAcceptSubscription(const char * consumerId, bool accepted)
223 {
224     NS_LOG(DEBUG, "NSAccept - IN");
225     pthread_mutex_lock(&nsInitMutex);
226
227     if(!consumerId)
228     {
229         NS_LOG(ERROR, "consumerId is NULL");
230         pthread_mutex_unlock(&nsInitMutex);
231         return NS_FAIL;
232     }
233
234     char * newConsumerId = OICStrdup(consumerId);
235     if(accepted)
236     {
237         NS_LOG(DEBUG, "accepted is true - ALLOW");
238         NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_ALLOW, newConsumerId);
239     }
240     else
241     {
242         NS_LOG(DEBUG, "accepted is false - DENY");
243         NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_DENY, newConsumerId);
244     }
245
246     pthread_mutex_unlock(&nsInitMutex);
247     NS_LOG(DEBUG, "NSAccept - OUT");
248     return NS_OK;
249 }
250
251 NSMessage * NSCreateMessage()
252 {
253     NS_LOG(DEBUG, "NSCreateMessage - IN");
254     pthread_mutex_lock(&nsInitMutex);
255
256     NSMessage * msg = NSInitializeMessage();
257
258     OICStrcpy(msg->providerId, UUID_STRING_SIZE, NSGetProviderInfo()->providerId);
259
260     pthread_mutex_unlock(&nsInitMutex);
261     NS_LOG(DEBUG, "NSCreateMessage - OUT");
262     return msg;
263 }
264
265 NSTopicLL * NSProviderGetConsumerTopics(const char * consumerId)
266 {
267     NS_LOG(DEBUG, "NSProviderGetConsumerTopics - IN");
268     pthread_mutex_lock(&nsInitMutex);
269
270     if(!consumerId || consumerId[0] == '\0')
271     {
272         NS_LOG(DEBUG, "consumer id should be set");
273         pthread_mutex_unlock(&nsInitMutex);
274         return NULL;
275     }
276
277     NSTopicSynchronization topics;
278     topics.consumerId = OICStrdup(consumerId);
279     topics.topics = NULL;
280     topics.condition = nstopicCond;
281
282     NSPushQueue(TOPIC_SCHEDULER, TAST_GET_CONSUMER_TOPICS, &topics);
283     pthread_cond_wait(&topics.condition, &nsInitMutex);
284     OICFree(topics.consumerId);
285
286     pthread_mutex_unlock(&nsInitMutex);
287     NS_LOG(DEBUG, "NSProviderGetConsumerTopics - OUT");
288     return topics.topics;
289 }
290
291 NSTopicLL * NSProviderGetTopics()
292 {
293     NS_LOG(DEBUG, "NSProviderGetTopics - IN");
294     pthread_mutex_lock(&nsInitMutex);
295
296     NSTopicSynchronization topics;
297     topics.consumerId = NULL;
298     topics.topics = NULL;
299     topics.condition = nstopicCond;
300
301     NSPushQueue(TOPIC_SCHEDULER, TASK_GET_TOPICS, &topics);
302     pthread_cond_wait(&topics.condition, &nsInitMutex);
303
304     pthread_mutex_unlock(&nsInitMutex);
305     NS_LOG(DEBUG, "NSProviderGetTopics - OUT");
306     return topics.topics;
307 }
308
309 NSResult NSProviderRegisterTopic(const char * topicName)
310 {
311     NS_LOG(DEBUG, "NSProviderAddTopics - IN");
312     pthread_mutex_lock(&nsInitMutex);
313
314     if(!topicName || topicName == '\0')
315     {
316         pthread_mutex_unlock(&nsInitMutex);
317         NS_LOG(DEBUG, "topic Name should be set");
318         return NS_FAIL;
319     }
320
321     NSPushQueue(TOPIC_SCHEDULER, TASK_ADD_TOPIC, OICStrdup(topicName));
322
323     pthread_mutex_unlock(&nsInitMutex);
324     NS_LOG(DEBUG, "NSProviderAddTopics - OUT");
325     return NS_OK;
326 }
327
328 NSResult NSProviderUnregisterTopic(const char * topicName)
329 {
330     NS_LOG(DEBUG, "NSProviderDeleteTopics - IN");
331     pthread_mutex_lock(&nsInitMutex);
332
333     if(!topicName || topicName[0] == '\0')
334     {
335         pthread_mutex_unlock(&nsInitMutex);
336         NS_LOG(DEBUG, "topic Name should be set");
337         return NS_FAIL;
338     }
339
340     NSPushQueue(TOPIC_SCHEDULER, TASK_DELETE_TOPIC, (void *) topicName);
341
342     pthread_mutex_unlock(&nsInitMutex);
343     NS_LOG(DEBUG, "NSProviderDeleteTopics - OUT");
344     return NS_OK;
345 }
346
347 NSResult NSProviderSetConsumerTopic(const char * consumerId, const char * topicName)
348 {
349     NS_LOG(DEBUG, "NSProviderSelectTopics - IN");
350     pthread_mutex_lock(&nsInitMutex);
351
352     if(!consumerId || consumerId[0] == '\0' || !topicName || topicName[0] == '\0' || !NSGetPolicy())
353     {
354         NS_LOG(DEBUG, "consumer id should be set for topic subscription or "
355                 "Configuration must set to true.");
356         pthread_mutex_unlock(&nsInitMutex);
357         return NS_FAIL;
358     }
359
360     NSCacheTopicSubData * topicSubData =
361             (NSCacheTopicSubData *) OICMalloc(sizeof(NSCacheTopicSubData));
362
363     OICStrcpy(topicSubData->id, NS_UUID_STRING_SIZE, consumerId);
364     topicSubData->topicName = OICStrdup(topicName);
365
366     NSPushQueue(TOPIC_SCHEDULER, TASK_SUBSCRIBE_TOPIC, (void *)topicSubData);
367
368     pthread_mutex_unlock(&nsInitMutex);
369     NS_LOG(DEBUG, "NSProviderSelectTopics - OUT");
370     return NS_OK;
371 }
372
373 NSResult NSProviderUnsetConsumerTopic(const char * consumerId, const char * topicName)
374 {
375     NS_LOG(DEBUG, "NSProviderUnselectTopics - IN");
376     pthread_mutex_lock(&nsInitMutex);
377
378     if(!consumerId || consumerId[0] == '\0' || !topicName || topicName[0] == '\0' || !NSGetPolicy())
379     {
380         NS_LOG(DEBUG, "consumer id should be set for topic subscription or "
381                 "Configuration must set to true.");
382         pthread_mutex_unlock(&nsInitMutex);
383         return NS_FAIL;
384     }
385
386     NSCacheTopicSubData * topicSubData =
387             (NSCacheTopicSubData *) OICMalloc(sizeof(NSCacheTopicSubData));
388
389     OICStrcpy(topicSubData->id, NS_UUID_STRING_SIZE, consumerId);
390     topicSubData->topicName = OICStrdup(topicName);
391
392     NSPushQueue(TOPIC_SCHEDULER, TASK_UNSUBSCRIBE_TOPIC, (void *)topicSubData);
393
394     pthread_mutex_unlock(&nsInitMutex);
395     NS_LOG(DEBUG, "NSProviderUnselectTopics - OUT");
396     return NS_OK;
397 }