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 * NSDiscoveredProviderFunc(void * provider)
63 NSGetDiscoverCb()((NSProvider *) provider);
68 void NSDiscoveredProvider(NSProvider * provider)
70 if (!NSThreadInit(NSDiscoveredProviderFunc, (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 NSTask * NSMakeTask(NSTaskType type, void * data)
168 NSTask * retTask = OICMalloc(sizeof(NSTask));
174 retTask->taskType = type;
175 retTask->taskData = data;
176 retTask->nextTask = NULL;
181 NSMessage_consumer * NSCopyMessage(NSMessage_consumer * msg)
188 NSMessage_consumer * newMsg = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
194 newMsg->mId = OICStrdup(msg->mId);
195 newMsg->mTitle = OICStrdup(msg->mTitle);
196 newMsg->mContentText = OICStrdup(msg->mContentText);
197 newMsg->mSource = OICStrdup(msg->mSource);
198 newMsg->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
201 NS_LOG(ERROR, "OCDevAddr allocation is failed");
203 memcpy(newMsg->addr, msg->addr, sizeof(OCDevAddr));
207 void NSRemoveMessage(NSMessage_consumer * msg)
216 OICFree(msg->mTitle);
219 if (msg->mContentText)
221 OICFree(msg->mContentText);
222 msg->mContentText = NULL;
226 OICFree(msg->mSource);
239 OCStackResult NSInvokeRequest(OCDoHandle * handle,
240 OCMethod method, const OCDevAddr * addr,
241 const char * queryUrl, OCPayload * payload,
242 void * callbackFunc, void * callbackData)
244 OCCallbackData cbdata;
246 cbdata.cb = callbackFunc;
247 cbdata.context = callbackData;
250 return OCDoResource(handle, method, queryUrl, addr,
251 payload, CT_DEFAULT, NS_QOS, &cbdata, NULL, 0);