Merge branch 'master' into notification-service
[platform/upstream/iotivity.git] / service / notification / src / provider / NSProviderInterface.c
1 //******************************************************************\r
2 //\r
3 // Copyright 2016 Samsung Electronics All Rights Reserved.\r
4 //\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
6 //\r
7 // Licensed under the Apache License, Version 2.0 (the "License");\r
8 // you may not use this file except in compliance with the License.\r
9 // You may obtain a copy of the License at\r
10 //\r
11 //      http://www.apache.org/licenses/LICENSE-2.0\r
12 //\r
13 // Unless required by applicable law or agreed to in writing, software\r
14 // distributed under the License is distributed on an "AS IS" BASIS,\r
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16 // See the License for the specific language governing permissions and\r
17 // limitations under the License.\r
18 //\r
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
20 \r
21 #include "NSProviderInterface.h"\r
22 #include "NSProviderScheduler.h"\r
23 #include "NSProviderListener.h"\r
24 #include "NSProviderSubscription.h"\r
25 #include "NSProviderNotification.h"\r
26 #include "NSStorageAdapter.h"\r
27 #include "NSProviderMemoryCache.h"\r
28 #include "oic_malloc.h"\r
29 #include "oic_string.h"\r
30 #include "cautilinterface.h"\r
31 #include "NSProviderSystem.h"\r
32 #include "oic_time.h"\r
33 \r
34 bool initProvider = false;\r
35 static NSSubscribeRequestCallback g_subscribeRequestCb = NULL;\r
36 static NSProviderSyncInfoCallback g_syncCb = NULL;\r
37 \r
38 pthread_mutex_t nsInitMutex;\r
39 \r
40 void initializeMutex()\r
41 {\r
42     static pthread_mutex_t initMutex = PTHREAD_MUTEX_INITIALIZER;\r
43     nsInitMutex = initMutex;\r
44 }\r
45 \r
46 void NSRegisterSubscribeRequestCb(NSSubscribeRequestCallback subscribeRequestCb)\r
47 {\r
48     NS_LOG(DEBUG, "NSRegisterSubscribeRequestCb - IN");\r
49     g_subscribeRequestCb = subscribeRequestCb;\r
50     NS_LOG(DEBUG, "NSRegisterSubscribeRequestCb - OUT");\r
51 }\r
52 \r
53 void  NSRegisterSyncCb(NSProviderSyncInfoCallback syncCb)\r
54 {\r
55     NS_LOG(DEBUG, "NSRegisterSyncCb - IN");\r
56     g_syncCb = syncCb;\r
57     NS_LOG(DEBUG, "NSRegisterSyncCb - OUT");\r
58 }\r
59 \r
60 void NSSubscribeRequestCb(NSConsumer *consumer)\r
61 {\r
62     NS_LOG(DEBUG, "NSSubscribeRequestCb - IN");\r
63     g_subscribeRequestCb(consumer);\r
64     NS_LOG(DEBUG, "NSSubscribeRequestCb - OUT");\r
65 }\r
66 \r
67 void NSSyncCb(NSSyncInfo *sync)\r
68 {\r
69     NS_LOG(DEBUG, "NSSyncCb - IN");\r
70     g_syncCb(sync);\r
71     NS_LOG(DEBUG, "NSSyncCb - OUT");\r
72 }\r
73 \r
74 NSResult NSStartProvider(NSAccessPolicy policy, NSSubscribeRequestCallback subscribeRequestCb,\r
75         NSProviderSyncInfoCallback syncCb)\r
76 {\r
77     NS_LOG(DEBUG, "NSStartProvider - IN");\r
78 \r
79     initializeMutex();\r
80 \r
81     pthread_mutex_lock(&nsInitMutex);\r
82 \r
83     if (!initProvider)\r
84     {\r
85         NS_LOG(DEBUG, "Init Provider");\r
86         initProvider = true;\r
87         NSInitProviderInfo();\r
88         NSSetSubscriptionAccessPolicy(policy);\r
89         NSRegisterSubscribeRequestCb(subscribeRequestCb);\r
90         NSRegisterSyncCb(syncCb);\r
91         CARegisterNetworkMonitorHandler(NSProviderAdapterStateListener,\r
92                 NSProviderConnectionStateListener);\r
93 \r
94         NSSetList();\r
95         NSInitScheduler();\r
96         NSStartScheduler();\r
97 \r
98         NSPushQueue(DISCOVERY_SCHEDULER, TASK_START_PRESENCE, NULL);\r
99         NSPushQueue(DISCOVERY_SCHEDULER, TASK_REGISTER_RESOURCE, NULL);\r
100     }\r
101     else\r
102     {\r
103         NS_LOG(DEBUG, "Already started Notification Provider");\r
104     }\r
105     pthread_mutex_unlock(&nsInitMutex);\r
106 \r
107     NS_LOG(DEBUG, "NSStartProvider - OUT");\r
108     return NS_OK;\r
109 }\r
110 \r
111 void NSSetList()\r
112 {\r
113     NS_LOG(DEBUG, "NSSetList - IN");\r
114     pthread_mutex_init(&NSCacheMutex, NULL);\r
115     NSInitSubscriptionList();\r
116     NSInitMessageList();\r
117     NS_LOG(DEBUG, "NSSetList - OUT");\r
118 }\r
119 \r
120 NSResult NSStopProvider()\r
121 {\r
122     NS_LOG(DEBUG, "NSStopProvider - IN");\r
123     pthread_mutex_lock(&nsInitMutex);\r
124 \r
125     if(initProvider)\r
126     {\r
127         NSDeinitProviderInfo();\r
128         NSUnRegisterResource();\r
129         NSRegisterSubscribeRequestCb((NSSubscribeRequestCallback)NULL);\r
130         NSRegisterSyncCb((NSProviderSyncInfoCallback)NULL);\r
131         NSStopScheduler();\r
132         NSStorageDestroy(consumerSubList);\r
133         NSStorageDestroy(messageList);\r
134 \r
135         initProvider = false;\r
136     }\r
137 \r
138     pthread_mutex_unlock(&nsInitMutex);\r
139     NS_LOG(DEBUG, "NSStopProvider - OUT");\r
140     return NS_OK;\r
141 }\r
142 \r
143 NSResult NSSendMessage(NSMessage *msg)\r
144 {\r
145     NS_LOG(DEBUG, "NSSendNotification - IN");\r
146 \r
147     pthread_mutex_lock(&nsInitMutex);\r
148 \r
149     if(msg == NULL)\r
150     {\r
151         NS_LOG(ERROR, "Msg is NULL");\r
152         pthread_mutex_unlock(&nsInitMutex);\r
153         return NS_ERROR;\r
154     }\r
155 \r
156     NSMessage * newMsg = NSDuplicateMessage(msg);\r
157     NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_NOTIFICATION, newMsg);\r
158 \r
159     pthread_mutex_unlock(&nsInitMutex);\r
160 \r
161     NS_LOG(DEBUG, "NSSendNotification - OUT");\r
162     return NS_OK;\r
163 }\r
164 \r
165 NSResult NSProviderSendSyncInfo(uint64_t messageId, NSSyncType type)\r
166 {\r
167     NS_LOG(DEBUG, "NSProviderReadCheck - IN");\r
168     pthread_mutex_lock(&nsInitMutex);\r
169 \r
170     NSSyncInfo * syncInfo = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));\r
171     OICStrcpy(syncInfo->providerId, UUID_STRING_SIZE, NSGetProviderInfo()->providerId);\r
172     syncInfo->messageId = messageId;\r
173     syncInfo->state = type;\r
174     NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_READ, syncInfo);\r
175 \r
176     pthread_mutex_unlock(&nsInitMutex);\r
177     NS_LOG(DEBUG, "NSProviderReadCheck - OUT");\r
178     return NS_OK;\r
179 }\r
180 \r
181 NSResult NSAccept(NSConsumer *consumer, bool accepted)\r
182 {\r
183     NS_LOG(DEBUG, "NSAccept - IN");\r
184 \r
185     pthread_mutex_lock(&nsInitMutex);\r
186 \r
187     NSConsumer * newConsumer = NSDuplicateConsumer(consumer);\r
188 \r
189     if(accepted)\r
190     {\r
191         NS_LOG(DEBUG, "accepted is true - ALLOW");\r
192         NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_ALLOW, newConsumer);\r
193     }\r
194     else\r
195     {\r
196         NS_LOG(DEBUG, "accepted is false - DENY");\r
197         NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_DENY, newConsumer);\r
198     }\r
199 \r
200     pthread_mutex_unlock(&nsInitMutex);\r
201     NS_LOG(DEBUG, "NSAccept - OUT");\r
202     return NS_OK;\r
203 }\r
204 \r
205 NSMessage * NSCreateMessage()\r
206 {\r
207     NS_LOG(DEBUG, "NSCreateMessage - IN");\r
208     pthread_mutex_lock(&nsInitMutex);\r
209 \r
210     NSMessage * msg = NSInitializeMessage();\r
211     OICStrcpy(msg->providerId, UUID_STRING_SIZE, NSGetProviderInfo()->providerId);\r
212 \r
213     pthread_mutex_unlock(&nsInitMutex);\r
214     NS_LOG(DEBUG, "NSCreateMessage - OUT");\r
215     return msg;\r
216 }\r
217 \r
218 void * NSInterfaceSchedule(void * ptr)\r
219 {\r
220     if (ptr == NULL)\r
221     {\r
222         NS_LOG(DEBUG, "Create NSReponseSchedule");\r
223     }\r
224 \r
225     while (NSIsRunning[INTERFACE_SCHEDULER])\r
226     {\r
227         sem_wait(&NSSemaphore[INTERFACE_SCHEDULER]);\r
228         pthread_mutex_lock(&NSMutex[INTERFACE_SCHEDULER]);\r
229 \r
230         if (NSHeadMsg[INTERFACE_SCHEDULER] != NULL)\r
231         {\r
232             NSTask *node = NSHeadMsg[INTERFACE_SCHEDULER];\r
233             NSHeadMsg[INTERFACE_SCHEDULER] = node->nextTask;\r
234 \r
235             switch (node->taskType)\r
236             {\r
237                 case TASK_CB_SUBSCRIPTION:\r
238                 {\r
239                     NS_LOG(DEBUG, "CASE TASK_CB_SUBSCRIPTION : ");\r
240 \r
241                     OCEntityHandlerRequest * request = (OCEntityHandlerRequest*)node->taskData;\r
242                     NSConsumer * consumer = (NSConsumer *)OICMalloc(sizeof(NSConsumer));\r
243 \r
244                     char * consumerId = NSGetValueFromQuery(OICStrdup(request->query),\r
245                             NS_QUERY_CONSUMER_ID);\r
246 \r
247                     if(consumerId)\r
248                     {\r
249                         OICStrcpy(consumer->consumerId, UUID_STRING_SIZE, consumerId);\r
250                         NSSubscribeRequestCb(consumer);\r
251                     }\r
252 \r
253                     NSFreeConsumer(consumer);\r
254                     NSFreeOCEntityHandlerRequest(request);\r
255 \r
256                     break;\r
257                 }\r
258                 case TASK_CB_SYNC:\r
259                 {\r
260                     NS_LOG(DEBUG, "CASE TASK_CB_SYNC : ");\r
261                     NSSyncInfo * sync = (NSSyncInfo*)node->taskData;\r
262                     NSSyncCb(NSDuplicateSync(sync));\r
263                     NSFreeSync(sync);\r
264                     break;\r
265                 }\r
266                 default:\r
267                     NS_LOG(DEBUG, "No Task Type");\r
268                     break;\r
269             }\r
270             OICFree(node);\r
271         }\r
272 \r
273         pthread_mutex_unlock(&NSMutex[INTERFACE_SCHEDULER]);\r
274     }\r
275 \r
276     NS_LOG(DEBUG, "Destroy NSResponseSchedule");\r
277     return NULL;\r
278 }\r
279 \r