added cacheadapter interface and memorycache of notification.
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSConsumerCommon.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 "NSConsumerCommon.h"
22 #include "NSConstants.h"
23 #include "NSThread.h"
24 #include "oic_malloc.h"
25 #include "oic_string.h"
26
27 bool * NSGetBoneIsStartedConsumer()
28 {
29     static bool g_isStartedConsumer = false;
30
31     return & g_isStartedConsumer;
32 }
33
34 void NSSetIsStartedConsumer(bool setValue)
35 {
36     * NSGetBoneIsStartedConsumer() = setValue;
37 }
38
39 bool NSIsStartedConsumer()
40 {
41     return * NSGetBoneIsStartedConsumer();
42 }
43
44 NSProviderDiscoveredCallback * NSGetBoneDiscoverCb()
45 {
46     static NSProviderDiscoveredCallback g_discoverCb = NULL;
47
48     return & g_discoverCb;
49 }
50
51 void NSSetDiscoverProviderCb(NSProviderDiscoveredCallback cb)
52 {
53     * NSGetBoneDiscoverCb() = cb;
54 }
55
56 NSProviderDiscoveredCallback NSGetDiscoverCb()
57 {
58     return * NSGetBoneDiscoverCb();
59 }
60
61 void * NSDiscoverdProviderFunc(void * provider)
62 {
63     NSGetDiscoverCb()((NSProvider *) provider);
64
65     return NULL;
66 }
67
68 void NSDiscoveredProvider(NSProvider * provider)
69 {
70     if (!NSThreadInit(NSDiscoverdProviderFunc, (void *) provider))
71     {
72         NS_LOG(ERROR, "execute discovered provider callback fail");
73         return;
74     }
75 }
76
77 NSSyncCallback * NSGetBoneNotificationSyncCb()
78 {
79     static NSSyncCallback g_syncCb = NULL;
80
81     return & g_syncCb;
82 }
83
84 void NSSetNotificationSyncCb(NSSyncCallback cb)
85 {
86     * NSGetBoneNotificationSyncCb() = cb;
87 }
88
89 typedef struct
90 {
91     NSProvider * provider;
92     NSSync * sync;
93 } NSSyncData;
94
95 void * NSNotificationSyncFunc(void * obj)
96 {
97     NSProvider * provider = ((NSSyncData *) obj)->provider;
98     NSSync * syncData = ((NSSyncData *) obj)->sync;
99     (* NSGetBoneNotificationSyncCb())(provider, syncData);
100     return NULL;
101 }
102
103 void NSNotificationSync(NSProvider * provider, NSSync * sync)
104 {
105     NSSyncData * obj = (NSSyncData *)OICMalloc(sizeof(NSSyncData));
106     obj->provider = provider;
107     obj->sync = sync;
108
109     if (!NSThreadInit(NSNotificationSyncFunc, (void *) obj))
110     {
111         NS_LOG(ERROR, "execute noti post callback fail");
112         return;
113     }
114 }
115
116 NSNotificationReceivedCallback  * NSGetBoneNotificationPostedCb()
117 {
118     static NSNotificationReceivedCallback  g_postCb = NULL;
119
120     return & g_postCb;
121 }
122
123 void NSSetNotificationPostedCb(NSNotificationReceivedCallback  cb)
124 {
125     * NSGetBoneNotificationPostedCb() = cb;
126 }
127
128 NSNotificationReceivedCallback  NSGetNotificationPostedCb()
129 {
130     return * NSGetBoneNotificationPostedCb();
131 }
132
133 typedef struct
134 {
135     NSProvider * provider;
136     NSMessage * msg;
137 } NSMessageData;
138
139 void * NSNotificationPostFunc(void * obj)
140 {
141     NSMessageData * msgData = (NSMessageData *) obj;
142
143     NSGetNotificationPostedCb()((NSProvider *) msgData->provider,
144             (NSMessage *) msgData->msg);
145     return NULL;
146 }
147
148 void NSNotificationPost(NSProvider * provider, NSMessage * msg)
149 {
150     NSMessageData * obj = (NSMessageData *)OICMalloc(sizeof(NSMessageData));
151     if (!obj)
152     {
153         NS_LOG(ERROR, "NSMessageData allocation is failed");
154         return;
155     }
156     obj->provider = provider;
157     obj->msg = msg;
158
159     if (!NSThreadInit(NSNotificationPostFunc, (void *) obj))
160     {
161         NS_LOG(ERROR, "execute noti post callback fail");
162         return;
163     }
164 }
165
166 onRIResponseListener * NSGetBoneResponseListener()
167 {
168     static onRIResponseListener g_responseCb = NULL;
169
170     return & g_responseCb;
171 }
172
173 void NSSetResponseListener(onRIResponseListener cb)
174 {
175     * NSGetBoneResponseListener() = cb;
176 }
177
178 onRIResponseListener NSGetResponseListener()
179 {
180     return * NSGetBoneResponseListener();
181 }
182
183 NSTask * NSMakeTask(NSTaskType type, void * data)
184 {
185     NSTask * retTask = OICMalloc(sizeof(NSTask));
186     if (!retTask)
187     {
188         return NULL;
189     }
190
191     retTask->taskType = type;
192     retTask->taskData = data;
193     retTask->nextTask = NULL;
194
195     return retTask;
196 }
197
198 NSMessage_consumer * NSCopyMessage(NSMessage_consumer * msg)
199 {
200     if (!msg)
201     {
202         return NULL;
203     }
204
205     NSMessage_consumer * newMsg = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
206     if (!newMsg)
207     {
208         return NULL;
209     }
210
211     newMsg->mId = OICStrdup(msg->mId);
212     newMsg->mTitle = OICStrdup(msg->mTitle);
213     newMsg->mContentText = OICStrdup(msg->mContentText);
214     newMsg->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
215     if (!newMsg->addr)
216     {
217         NS_LOG(ERROR, "OCDevAddr allocation is failed");
218     }
219     memcpy(newMsg->addr, msg->addr, sizeof(OCDevAddr));
220
221     return newMsg;
222 }
223 void NSRemoveMessage(NSMessage_consumer * msg)
224 {
225     if (msg->mId)
226     {
227         OICFree(msg->mId);
228         msg->mId = NULL;
229     }
230     if (msg->mTitle)
231     {
232         OICFree(msg->mTitle);
233         msg->mTitle = NULL;
234     }
235     if (msg->mContentText)
236     {
237         OICFree(msg->mContentText);
238         msg->mContentText = NULL;
239     }
240     if (msg->addr)
241     {
242         OICFree(msg->addr);
243         msg->addr = NULL;
244     }
245
246     OICFree(msg);
247     msg = NULL;
248 }
249
250 OCStackResult NSRequestToResourceIntrospection(OCDoHandle * handle,
251         OCMethod method, const OCDevAddr * addr,
252         const char * queryUrl, OCPayload * payload, void * callback)
253 {
254     OCCallbackData cbdata;
255
256     cbdata.cb = NSGetResponseListener();
257     cbdata.context = callback;
258     cbdata.cd = NULL;
259
260     return OCDoResource(handle, method, queryUrl, addr,
261             payload, CT_DEFAULT, NS_QOS, &cbdata, NULL, 0);
262 }
263