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