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