Configure resource security
[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         NS_LOG(DEBUG, "Check secured build option..");
93 #ifdef SECURED
94         NS_LOG(DEBUG, "Built with SECURED");
95
96         if(!config.resourceSecurity)
97         {
98             NS_LOG(DEBUG, "Resource Security Off");
99         }
100         NSSetResourceSecurity(config.resourceSecurity);
101 #endif
102
103         NSInitialize();
104         NSInitScheduler();
105         NSStartScheduler();
106
107         NSPushQueue(DISCOVERY_SCHEDULER, TASK_START_PRESENCE, NULL);
108         NSPushQueue(DISCOVERY_SCHEDULER, TASK_REGISTER_RESOURCE, NULL);
109     }
110     else
111     {
112         NS_LOG(DEBUG, "Already started Notification Provider");
113     }
114     pthread_mutex_unlock(&nsInitMutex);
115
116     NS_LOG(DEBUG, "NSStartProvider - OUT");
117     return NS_OK;
118 }
119
120 NSResult NSStopProvider()
121 {
122     NS_LOG(DEBUG, "NSStopProvider - IN");
123     pthread_mutex_lock(&nsInitMutex);
124
125     if(initProvider)
126     {
127         NSPushQueue(DISCOVERY_SCHEDULER, TASK_STOP_PRESENCE, NULL);
128         NSDeinitProviderInfo();
129         NSUnRegisterResource();
130         NSRegisterSubscribeRequestCb((NSSubscribeRequestCallback)NULL);
131         NSRegisterSyncCb((NSProviderSyncInfoCallback)NULL);
132         NSStopScheduler();
133         NSDeinitailize();
134
135         initProvider = false;
136     }
137
138     pthread_mutex_unlock(&nsInitMutex);
139     NS_LOG(DEBUG, "NSStopProvider - OUT");
140     return NS_OK;
141 }
142
143 NSResult NSProviderEnableRemoteService(char *serverAddress)
144 {
145 #if(defined WITH_CLOUD && defined RD_CLIENT)
146     NS_LOG(DEBUG, "NSProviderEnableRemoteService - IN");
147     pthread_mutex_lock(&nsInitMutex);
148
149     if(!initProvider || !serverAddress)
150     {
151         NS_LOG(DEBUG, "Provider service has not been started yet");
152         pthread_mutex_unlock(&nsInitMutex);
153         return NS_FAIL;
154     }
155     NS_LOG_V(DEBUG, "Remote server address: %s", serverAddress);
156     NS_LOG(DEBUG, "Request to publish resource");
157     NSPushQueue(DISCOVERY_SCHEDULER, TASK_PUBLISH_RESOURCE, serverAddress);
158
159     pthread_mutex_unlock(&nsInitMutex);
160     NS_LOG(DEBUG, "NSProviderEnableRemoteService - OUT");
161     return NS_OK;
162 #else
163     (void) serverAddress;
164 #endif
165     NS_LOG_V(DEBUG, "Not logged in remote server: %s", serverAddress);
166     return NS_FAIL;
167 }
168
169 NSResult NSProviderDisableRemoteService(char *serverAddress)
170 {
171 #if(defined WITH_CLOUD && defined RD_CLIENT)
172     NS_LOG(DEBUG, "NSProviderDisableRemoteService - IN");
173     pthread_mutex_lock(&nsInitMutex);
174
175     if(!initProvider || !serverAddress)
176     {
177         NS_LOG(DEBUG, "Provider service has not been started yet");
178         return NS_FAIL;
179     }
180     NS_LOG_V(DEBUG, "Remote server address: %s", serverAddress);
181
182     NS_LOG(DEBUG, "Delete remote server info");
183     NSDeleteRemoteServerAddress(serverAddress);
184
185     pthread_mutex_unlock(&nsInitMutex);
186     NS_LOG(DEBUG, "NSProviderDisableRemoteService - OUT");
187     return NS_OK;
188 #else
189     (void) serverAddress;
190 #endif
191     NS_LOG_V(DEBUG, "Not logged in remote server : %s", serverAddress);
192     return NS_FAIL;
193 }
194
195 NSResult NSSendMessage(NSMessage * msg)
196 {
197     NS_LOG(DEBUG, "NSSendNotification - IN");
198
199     pthread_mutex_lock(&nsInitMutex);
200
201     if(msg == NULL)
202     {
203         NS_LOG(ERROR, "Msg is NULL");
204         pthread_mutex_unlock(&nsInitMutex);
205         return NS_FAIL;
206     }
207
208     NSMessage * newMsg = NSDuplicateMessage(msg);
209     NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_NOTIFICATION, newMsg);
210
211     pthread_mutex_unlock(&nsInitMutex);
212
213     NS_LOG(DEBUG, "NSSendNotification - OUT");
214     return NS_OK;
215 }
216
217 NSResult NSProviderSendSyncInfo(uint64_t messageId, NSSyncType type)
218 {
219     NS_LOG(DEBUG, "NSProviderReadCheck - IN");
220     pthread_mutex_lock(&nsInitMutex);
221
222     NSSyncInfo * syncInfo = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
223     OICStrcpy(syncInfo->providerId, UUID_STRING_SIZE, NSGetProviderInfo()->providerId);
224     syncInfo->messageId = messageId;
225     syncInfo->state = type;
226     NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_READ, syncInfo);
227
228     pthread_mutex_unlock(&nsInitMutex);
229     NS_LOG(DEBUG, "NSProviderReadCheck - OUT");
230     return NS_OK;
231 }
232
233 NSResult NSAcceptSubscription(const char * consumerId, bool accepted)
234 {
235     NS_LOG(DEBUG, "NSAccept - IN");
236     pthread_mutex_lock(&nsInitMutex);
237
238     if(!consumerId)
239     {
240         NS_LOG(ERROR, "consumerId is NULL");
241         pthread_mutex_unlock(&nsInitMutex);
242         return NS_FAIL;
243     }
244
245     char * newConsumerId = OICStrdup(consumerId);
246     if(accepted)
247     {
248         NS_LOG(DEBUG, "accepted is true - ALLOW");
249         NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_ALLOW, newConsumerId);
250     }
251     else
252     {
253         NS_LOG(DEBUG, "accepted is false - DENY");
254         NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_DENY, newConsumerId);
255     }
256
257     pthread_mutex_unlock(&nsInitMutex);
258     NS_LOG(DEBUG, "NSAccept - OUT");
259     return NS_OK;
260 }
261
262 NSMessage * NSCreateMessage()
263 {
264     NS_LOG(DEBUG, "NSCreateMessage - IN");
265     pthread_mutex_lock(&nsInitMutex);
266
267     NSMessage * msg = NSInitializeMessage();
268
269     OICStrcpy(msg->providerId, UUID_STRING_SIZE, NSGetProviderInfo()->providerId);
270
271     pthread_mutex_unlock(&nsInitMutex);
272     NS_LOG(DEBUG, "NSCreateMessage - OUT");
273     return msg;
274 }
275
276 NSTopicLL * NSProviderGetConsumerTopics(const char * consumerId)
277 {
278     NS_LOG(DEBUG, "NSProviderGetConsumerTopics - IN");
279     pthread_mutex_lock(&nsInitMutex);
280
281     if(!consumerId || consumerId[0] == '\0')
282     {
283         NS_LOG(DEBUG, "consumer id should be set");
284         pthread_mutex_unlock(&nsInitMutex);
285         return NULL;
286     }
287
288     NSTopicSynchronization topics;
289     topics.consumerId = OICStrdup(consumerId);
290     topics.topics = NULL;
291     topics.condition = nstopicCond;
292
293     NSPushQueue(TOPIC_SCHEDULER, TAST_GET_CONSUMER_TOPICS, &topics);
294     pthread_cond_wait(&topics.condition, &nsInitMutex);
295     OICFree(topics.consumerId);
296
297     pthread_mutex_unlock(&nsInitMutex);
298     NS_LOG(DEBUG, "NSProviderGetConsumerTopics - OUT");
299     return topics.topics;
300 }
301
302 NSTopicLL * NSProviderGetTopics()
303 {
304     NS_LOG(DEBUG, "NSProviderGetTopics - IN");
305     pthread_mutex_lock(&nsInitMutex);
306
307     NSTopicSynchronization topics;
308     topics.consumerId = NULL;
309     topics.topics = NULL;
310     topics.condition = nstopicCond;
311
312     NSPushQueue(TOPIC_SCHEDULER, TASK_GET_TOPICS, &topics);
313     pthread_cond_wait(&topics.condition, &nsInitMutex);
314
315     pthread_mutex_unlock(&nsInitMutex);
316     NS_LOG(DEBUG, "NSProviderGetTopics - OUT");
317     return topics.topics;
318 }
319
320 NSResult NSProviderRegisterTopic(const char * topicName)
321 {
322     NS_LOG(DEBUG, "NSProviderAddTopics - IN");
323     pthread_mutex_lock(&nsInitMutex);
324
325     if(!topicName || topicName == '\0')
326     {
327         pthread_mutex_unlock(&nsInitMutex);
328         NS_LOG(DEBUG, "topic Name should be set");
329         return NS_FAIL;
330     }
331
332     NSPushQueue(TOPIC_SCHEDULER, TASK_ADD_TOPIC, OICStrdup(topicName));
333
334     pthread_mutex_unlock(&nsInitMutex);
335     NS_LOG(DEBUG, "NSProviderAddTopics - OUT");
336     return NS_OK;
337 }
338
339 NSResult NSProviderUnregisterTopic(const char * topicName)
340 {
341     NS_LOG(DEBUG, "NSProviderDeleteTopics - IN");
342     pthread_mutex_lock(&nsInitMutex);
343
344     if(!topicName || topicName[0] == '\0')
345     {
346         pthread_mutex_unlock(&nsInitMutex);
347         NS_LOG(DEBUG, "topic Name should be set");
348         return NS_FAIL;
349     }
350
351     NSPushQueue(TOPIC_SCHEDULER, TASK_DELETE_TOPIC, (void *) topicName);
352
353     pthread_mutex_unlock(&nsInitMutex);
354     NS_LOG(DEBUG, "NSProviderDeleteTopics - OUT");
355     return NS_OK;
356 }
357
358 NSResult NSProviderSetConsumerTopic(const char * consumerId, const char * topicName)
359 {
360     NS_LOG(DEBUG, "NSProviderSelectTopics - IN");
361     pthread_mutex_lock(&nsInitMutex);
362
363     if(!consumerId || consumerId[0] == '\0' || !topicName || topicName[0] == '\0' || !NSGetPolicy())
364     {
365         NS_LOG(DEBUG, "consumer id should be set for topic subscription or "
366                 "Configuration must set to true.");
367         pthread_mutex_unlock(&nsInitMutex);
368         return NS_FAIL;
369     }
370
371     NSCacheTopicSubData * topicSubData =
372             (NSCacheTopicSubData *) OICMalloc(sizeof(NSCacheTopicSubData));
373
374     OICStrcpy(topicSubData->id, NS_UUID_STRING_SIZE, consumerId);
375     topicSubData->topicName = OICStrdup(topicName);
376
377     NSPushQueue(TOPIC_SCHEDULER, TASK_SUBSCRIBE_TOPIC, (void *)topicSubData);
378
379     pthread_mutex_unlock(&nsInitMutex);
380     NS_LOG(DEBUG, "NSProviderSelectTopics - OUT");
381     return NS_OK;
382 }
383
384 NSResult NSProviderUnsetConsumerTopic(const char * consumerId, const char * topicName)
385 {
386     NS_LOG(DEBUG, "NSProviderUnselectTopics - IN");
387     pthread_mutex_lock(&nsInitMutex);
388
389     if(!consumerId || consumerId[0] == '\0' || !topicName || topicName[0] == '\0' || !NSGetPolicy())
390     {
391         NS_LOG(DEBUG, "consumer id should be set for topic subscription or "
392                 "Configuration must set to true.");
393         pthread_mutex_unlock(&nsInitMutex);
394         return NS_FAIL;
395     }
396
397     NSCacheTopicSubData * topicSubData =
398             (NSCacheTopicSubData *) OICMalloc(sizeof(NSCacheTopicSubData));
399
400     OICStrcpy(topicSubData->id, NS_UUID_STRING_SIZE, consumerId);
401     topicSubData->topicName = OICStrdup(topicName);
402
403     NSPushQueue(TOPIC_SCHEDULER, TASK_UNSUBSCRIBE_TOPIC, (void *)topicSubData);
404
405     pthread_mutex_unlock(&nsInitMutex);
406     NS_LOG(DEBUG, "NSProviderUnselectTopics - OUT");
407     return NS_OK;
408 }