1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "NSConsumerCommon.h"
23 #include "oic_malloc.h"
24 #include "oic_string.h"
26 bool * NSGetBoneIsStartedConsumer()
28 static bool g_isStartedConsumer = false;
30 return & g_isStartedConsumer;
33 void NSSetIsStartedConsumer(bool setValue)
35 * NSGetBoneIsStartedConsumer() = setValue;
38 bool NSIsStartedConsumer()
40 return * NSGetBoneIsStartedConsumer();
43 NSProviderDiscoveredCallback * NSGetBoneDiscoverCb()
45 static NSProviderDiscoveredCallback g_discoverCb = NULL;
47 return & g_discoverCb;
50 void NSSetDiscoverProviderCb(NSProviderDiscoveredCallback cb)
52 * NSGetBoneDiscoverCb() = cb;
55 NSProviderDiscoveredCallback NSGetDiscoverCb()
57 return * NSGetBoneDiscoverCb();
60 void * NSDiscoverdProviderFunc(void * provider)
62 NSGetDiscoverCb()((NSProvider *) provider);
67 void NSDiscoveredProvider(NSProvider * provider)
69 if (!NSThreadInit(NSDiscoverdProviderFunc, (void *) provider))
71 NS_CONSUMER_LOG(ERROR, "execute discovered provider callback fail");
76 NSSyncCallback * NSGetBoneNotificationSyncCb()
78 static NSSyncCallback g_syncCb = NULL;
83 void NSSetNotificationSyncCb(NSSyncCallback cb)
85 * NSGetBoneNotificationSyncCb() = cb;
90 NSProvider * provider;
94 void * NSNotificationSyncFunc(void * obj)
96 NSProvider * provider = ((NSSyncData *) obj)->provider;
97 NSSync * syncData = ((NSSyncData *) obj)->sync;
98 (* NSGetBoneNotificationSyncCb())(provider, syncData);
102 void NSNotificationSync(NSProvider * provider, NSSync * sync)
104 NSSyncData * obj = (NSSyncData *)OICMalloc(sizeof(NSSyncData));
105 obj->provider = provider;
108 if (!NSThreadInit(NSNotificationSyncFunc, (void *) obj))
110 NS_CONSUMER_LOG(ERROR, "execute noti post callback fail");
115 NSNotificationReceivedCallback * NSGetBoneNotificationPostedCb()
117 static NSNotificationReceivedCallback g_postCb = NULL;
122 void NSSetNotificationPostedCb(NSNotificationReceivedCallback cb)
124 * NSGetBoneNotificationPostedCb() = cb;
127 NSNotificationReceivedCallback NSGetNotificationPostedCb()
129 return * NSGetBoneNotificationPostedCb();
134 NSProvider * provider;
138 void * NSNotificationPostFunc(void * obj)
140 NSMessageData * msgData = (NSMessageData *) obj;
142 NSGetNotificationPostedCb()((NSProvider *) msgData->provider,
143 (NSMessage *) msgData->msg);
147 void NSNotificationPost(NSProvider * provider, NSMessage * msg)
149 NSMessageData * obj = (NSMessageData *)OICMalloc(sizeof(NSMessageData));
152 NS_CONSUMER_LOG(ERROR, "NSMessageData allocation is failed");
155 obj->provider = provider;
158 if (!NSThreadInit(NSNotificationPostFunc, (void *) obj))
160 NS_CONSUMER_LOG(ERROR, "execute noti post callback fail");
165 onRIResponseListener * NSGetBoneResponseListener()
167 static onRIResponseListener g_responseCb = NULL;
169 return & g_responseCb;
172 void NSSetResponseListener(onRIResponseListener cb)
174 * NSGetBoneResponseListener() = cb;
177 onRIResponseListener NSGetResponseListener()
179 return * NSGetBoneResponseListener();
182 NSTask * NSMakeTask(NSTaskType type, void * data)
184 NSTask * retTask = OICMalloc(sizeof(NSTask));
190 retTask->taskType = type;
191 retTask->taskData = data;
192 retTask->nextTask = NULL;
197 NSMessage_consumer * NSCopyMessage(NSMessage_consumer * msg)
204 NSMessage_consumer * newMsg = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
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));
216 NS_CONSUMER_LOG(ERROR, "OCDevAddr allocation is failed");
218 memcpy(newMsg->addr, msg->addr, sizeof(OCDevAddr));
222 void NSRemoveMessage(NSMessage_consumer * msg)
231 OICFree(msg->mTitle);
234 if (msg->mContentText)
236 OICFree(msg->mContentText);
237 msg->mContentText = NULL;
249 OCStackResult NSRequestToResourceIntrospection(OCDoHandle * handle,
250 OCMethod method, const OCDevAddr * addr,
251 const char * queryUrl, OCPayload * payload, void * callback)
253 OCCallbackData cbdata;
255 cbdata.cb = NSGetResponseListener();
256 cbdata.context = callback;
259 return OCDoResource(handle, method, queryUrl, addr,
260 payload, CT_DEFAULT, NS_QOS, &cbdata, NULL, 0);
263 const char * NS_CONVERT_LEVEL(int level)
265 return ((level) == 0) ? "DEBUG" :
266 ((level) == 1) ? "INFO" :
267 ((level) == 2) ? "WARNING" :
268 ((level) == 3) ? "ERROR" : "FATAL";