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"
22 #include "NSConstants.h"
24 #include "oic_malloc.h"
25 #include "oic_string.h"
27 bool * NSGetBoneIsStartedConsumer()
29 static bool g_isStartedConsumer = false;
31 return & g_isStartedConsumer;
34 void NSSetIsStartedConsumer(bool setValue)
36 * NSGetBoneIsStartedConsumer() = setValue;
39 bool NSIsStartedConsumer()
41 return * NSGetBoneIsStartedConsumer();
44 NSProviderDiscoveredCallback * NSGetBoneDiscoverCb()
46 static NSProviderDiscoveredCallback g_discoverCb = NULL;
48 return & g_discoverCb;
51 void NSSetDiscoverProviderCb(NSProviderDiscoveredCallback cb)
53 * NSGetBoneDiscoverCb() = cb;
56 NSProviderDiscoveredCallback NSGetDiscoverCb()
58 return * NSGetBoneDiscoverCb();
61 void * NSDiscoverdProviderFunc(void * provider)
63 NSGetDiscoverCb()((NSProvider *) provider);
68 void NSDiscoveredProvider(NSProvider * provider)
70 if (!NSThreadInit(NSDiscoverdProviderFunc, (void *) provider))
72 NS_LOG(ERROR, "execute discovered provider callback fail");
77 NSSyncCallback * NSGetBoneNotificationSyncCb()
79 static NSSyncCallback g_syncCb = NULL;
84 void NSSetNotificationSyncCb(NSSyncCallback cb)
86 * NSGetBoneNotificationSyncCb() = cb;
91 NSProvider * provider;
95 void * NSNotificationSyncFunc(void * obj)
97 NSProvider * provider = ((NSSyncData *) obj)->provider;
98 NSSync * syncData = ((NSSyncData *) obj)->sync;
99 (* NSGetBoneNotificationSyncCb())(provider, syncData);
103 void NSNotificationSync(NSProvider * provider, NSSync * sync)
105 NSSyncData * obj = (NSSyncData *)OICMalloc(sizeof(NSSyncData));
106 obj->provider = provider;
109 if (!NSThreadInit(NSNotificationSyncFunc, (void *) obj))
111 NS_LOG(ERROR, "execute noti post callback fail");
116 NSNotificationReceivedCallback * NSGetBoneNotificationPostedCb()
118 static NSNotificationReceivedCallback g_postCb = NULL;
123 void NSSetNotificationPostedCb(NSNotificationReceivedCallback cb)
125 * NSGetBoneNotificationPostedCb() = cb;
128 NSNotificationReceivedCallback NSGetNotificationPostedCb()
130 return * NSGetBoneNotificationPostedCb();
135 NSProvider * provider;
139 void * NSNotificationPostFunc(void * obj)
141 NSMessageData * msgData = (NSMessageData *) obj;
143 NSGetNotificationPostedCb()((NSProvider *) msgData->provider,
144 (NSMessage *) msgData->msg);
148 void NSNotificationPost(NSProvider * provider, NSMessage * msg)
150 NSMessageData * obj = (NSMessageData *)OICMalloc(sizeof(NSMessageData));
153 NS_LOG(ERROR, "NSMessageData allocation is failed");
156 obj->provider = provider;
159 if (!NSThreadInit(NSNotificationPostFunc, (void *) obj))
161 NS_LOG(ERROR, "execute noti post callback fail");
166 onRIResponseListener * NSGetBoneResponseListener()
168 static onRIResponseListener g_responseCb = NULL;
170 return & g_responseCb;
173 void NSSetResponseListener(onRIResponseListener cb)
175 * NSGetBoneResponseListener() = cb;
178 onRIResponseListener NSGetResponseListener()
180 return * NSGetBoneResponseListener();
183 NSTask * NSMakeTask(NSTaskType type, void * data)
185 NSTask * retTask = OICMalloc(sizeof(NSTask));
191 retTask->taskType = type;
192 retTask->taskData = data;
193 retTask->nextTask = NULL;
198 NSMessage_consumer * NSCopyMessage(NSMessage_consumer * msg)
205 NSMessage_consumer * newMsg = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
211 newMsg->mId = OICStrdup(msg->mId);
212 newMsg->mTitle = OICStrdup(msg->mTitle);
213 newMsg->mContentText = OICStrdup(msg->mContentText);
214 newMsg->mSource = OICStrdup(msg->mSource);
215 newMsg->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
218 NS_LOG(ERROR, "OCDevAddr allocation is failed");
220 memcpy(newMsg->addr, msg->addr, sizeof(OCDevAddr));
224 void NSRemoveMessage(NSMessage_consumer * msg)
233 OICFree(msg->mTitle);
236 if (msg->mContentText)
238 OICFree(msg->mContentText);
239 msg->mContentText = NULL;
243 OICFree(msg->mSource);
256 OCStackResult NSRequestToResourceIntrospection(OCDoHandle * handle,
257 OCMethod method, const OCDevAddr * addr,
258 const char * queryUrl, OCPayload * payload, void * callback)
260 OCCallbackData cbdata;
262 cbdata.cb = NSGetResponseListener();
263 cbdata.context = callback;
266 return OCDoResource(handle, method, queryUrl, addr,
267 payload, CT_DEFAULT, NS_QOS, &cbdata, NULL, 0);