1 //******************************************************************
\r
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
7 // Licensed under the Apache License, Version 2.0 (the "License");
\r
8 // you may not use this file except in compliance with the License.
\r
9 // You may obtain a copy of the License at
\r
11 // http://www.apache.org/licenses/LICENSE-2.0
\r
13 // Unless required by applicable law or agreed to in writing, software
\r
14 // distributed under the License is distributed on an "AS IS" BASIS,
\r
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 // See the License for the specific language governing permissions and
\r
17 // limitations under the License.
\r
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
21 #include "NSProviderListener.h"
\r
23 OCEntityHandlerResult NSEntityHandlerNotificationCb(OCEntityHandlerFlag flag,
\r
24 OCEntityHandlerRequest *entityHandlerRequest, void* callback)
\r
26 NS_LOG(DEBUG, "NSEntityHandlerNotificationCb - IN");
\r
28 OCEntityHandlerResult ehResult = OC_EH_OK;
\r
32 if (!entityHandlerRequest)
\r
34 NS_LOG(ERROR, "Invalid request pointer");
\r
38 if (flag & OC_REQUEST_FLAG)
\r
40 NS_LOG(DEBUG, "Flag includes OC_REQUEST_FLAG");
\r
42 if (OC_REST_GET == entityHandlerRequest->method)
\r
44 NS_LOG(DEBUG, "NSEntityHandlerNotificationCb - OC_REST_GET");
\r
46 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_POLICY,
\r
47 NSCopyOCEntityHandlerRequest(entityHandlerRequest));
\r
52 NS_LOG_V (DEBUG, "Received method %d from client", entityHandlerRequest->method);
\r
56 NS_LOG(DEBUG, "NSEntityHandlerNotificationCb - OUT");
\r
60 OCEntityHandlerResult NSEntityHandlerMessageCb(OCEntityHandlerFlag flag,
\r
61 OCEntityHandlerRequest *entityHandlerRequest, void* callback)
\r
63 NS_LOG(DEBUG, "NSEntityHandlerMessageCb - IN");
\r
65 OCEntityHandlerResult ehResult = OC_EH_OK;
\r
69 if (!entityHandlerRequest)
\r
71 NS_LOG (ERROR,"Invalid request pointer");
\r
75 OCEntityHandlerResponse response;
\r
76 response.numSendVendorSpecificHeaderOptions = 0;
\r
77 memset(response.sendVendorSpecificHeaderOptions, 0,
\r
78 sizeof response.sendVendorSpecificHeaderOptions);
\r
79 memset(response.resourceUri, 0, sizeof response.resourceUri);
\r
81 if (flag & OC_REQUEST_FLAG)
\r
83 NS_LOG(DEBUG, "Flag includes OC_REQUEST_FLAG");
\r
84 NS_LOG_V(DEBUG, "Received method %d from client", entityHandlerRequest->method);
\r
87 if (flag & OC_OBSERVE_FLAG)
\r
89 NS_LOG(DEBUG, "NSEntityHandlerMessageCb - OC_OBSERVE_FLAG");
\r
91 OCObserveAction ocObAction = entityHandlerRequest->obsInfo.action;
\r
93 if (ocObAction == OC_OBSERVE_REGISTER)
\r
95 NS_LOG(DEBUG, "NSEntityHandlerMessageCb - OC_OBSERVE_REGISTER");
\r
96 NS_LOG_V(DEBUG, "NSEntityHandlerMessageCb\n"
\r
97 "Register message observerID : %d\n", entityHandlerRequest->obsInfo.obsId);
\r
98 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_RECV_SUBSCRIPTION,
\r
99 NSCopyOCEntityHandlerRequest(entityHandlerRequest));
\r
101 else if(ocObAction == OC_OBSERVE_DEREGISTER)
\r
103 NS_LOG(DEBUG, "NSEntityHandlerMessageCb - OC_OBSERVE_DEREGISTER");
\r
104 NS_LOG_V(DEBUG, "NSEntityHandlerMessageCb\n - "
\r
105 "Deregister Message observerID : %d\n", entityHandlerRequest->obsInfo.obsId);
\r
106 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_RECV_UNSUBSCRIPTION,
\r
107 NSCopyOCEntityHandlerRequest(entityHandlerRequest));
\r
111 response.requestHandle = entityHandlerRequest->requestHandle;
\r
112 response.resourceHandle = entityHandlerRequest->resource;
\r
113 response.persistentBufferFlag = 0;
\r
114 response.ehResult = OC_EH_OK;
\r
115 response.payload = (OCPayload *) NULL;
\r
117 if (OCDoResponse(&response) != OC_STACK_OK)
\r
119 NS_LOG(ERROR, "Fail to AccessPolicy send response");
\r
123 NS_LOG(DEBUG, "NSEntityHandlerMessageCb - OUT");
\r
127 OCEntityHandlerResult NSEntityHandlerSyncCb(OCEntityHandlerFlag flag,
\r
128 OCEntityHandlerRequest *entityHandlerRequest, void* callback)
\r
130 NS_LOG(DEBUG, "NSEntityHandlerSyncCb - IN");
\r
131 OCEntityHandlerResult ehResult = OC_EH_OK;
\r
135 if (!entityHandlerRequest)
\r
137 NS_LOG(ERROR, "Invalid request pointer");
\r
138 return OC_EH_ERROR;
\r
141 OCEntityHandlerResponse response;
\r
142 response.numSendVendorSpecificHeaderOptions = 0;
\r
143 memset(response.sendVendorSpecificHeaderOptions, 0,
\r
144 sizeof response.sendVendorSpecificHeaderOptions);
\r
145 memset(response.resourceUri, 0, sizeof response.resourceUri);
\r
147 if (flag & OC_REQUEST_FLAG)
\r
149 NS_LOG(DEBUG, "Flag includes OC_REQUEST_FLAG");
\r
151 if (OC_REST_POST == entityHandlerRequest->method)
\r
153 /** Receive sync data from consumer which read or dismiss notification message.
\r
154 And broadcast the sync data to all subscribers including provider app
\r
155 to synchronize the notification message status. */
\r
157 NS_LOG(DEBUG, "NSEntityHandlerSyncCb - OC_REST_POST");
\r
159 NSPushQueue(NOTIFICATION_SCHEDULER, TASK_RECV_READ,
\r
160 NSGetSyncInfo(entityHandlerRequest->payload));
\r
164 NS_LOG_V(DEBUG, "Received method %d from client", entityHandlerRequest->method);
\r
167 if (flag & OC_OBSERVE_FLAG)
\r
169 /** Requested by consumers to synchronize notification message status.
\r
170 Store the observer IDs to storage or cache */
\r
172 NS_LOG(DEBUG, "NSEntityHandlerSyncCb - OC_OBSERVE_FLAG");
\r
174 OCObserveAction ocObAction = entityHandlerRequest->obsInfo.action;
\r
176 if (ocObAction == OC_OBSERVE_REGISTER)
\r
178 NS_LOG(DEBUG, "NSEntityHandlerSyncCb - OC_OBSERVE_REGISTER");
\r
179 NS_LOG_V(DEBUG, "NSEntityHandlerSyncCb\n - "
\r
180 "Register Sync observerID : %d\n", entityHandlerRequest->obsInfo.obsId);
\r
181 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SYNC_SUBSCRIPTION,
\r
182 NSCopyOCEntityHandlerRequest(entityHandlerRequest));
\r
184 else if(ocObAction == OC_OBSERVE_DEREGISTER)
\r
186 NS_LOG(DEBUG, "NSEntityHandlerSyncCb - OC_OBSERVE_DEREGISTER");
\r
187 NS_LOG_V(DEBUG, "NSEntityHandlerSyncCb\n - "
\r
188 "Deregister Sync observerID : %d\n", entityHandlerRequest->obsInfo.obsId);
\r
189 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_RECV_UNSUBSCRIPTION,
\r
190 NSCopyOCEntityHandlerRequest(entityHandlerRequest));
\r
194 response.requestHandle = entityHandlerRequest->requestHandle;
\r
195 response.resourceHandle = entityHandlerRequest->resource;
\r
196 response.persistentBufferFlag = 0;
\r
197 response.ehResult = OC_EH_OK;
\r
198 response.payload = (OCPayload *) NULL;
\r
200 if (OCDoResponse(&response) != OC_STACK_OK)
\r
202 NS_LOG(ERROR, "Fail to AccessPolicy send response");
\r
206 NS_LOG(DEBUG, "NSEntityHandlerSyncCb - OUT");
\r
210 OCEntityHandlerResult NSEntityHandlerTopicCb(OCEntityHandlerFlag flag,
\r
211 OCEntityHandlerRequest *entityHandlerRequest, void* callback)
\r
213 NS_LOG(DEBUG, "NSEntityHandlerTopicCb - IN");
\r
214 OCEntityHandlerResult ehResult = OC_EH_OK;
\r
218 if (!entityHandlerRequest)
\r
220 NS_LOG(ERROR, "Invalid request pointer");
\r
221 return OC_EH_ERROR;
\r
224 if (flag & OC_REQUEST_FLAG)
\r
226 NS_LOG(DEBUG, "Flag includes OC_REQUEST_FLAG");
\r
228 if (OC_REST_GET == entityHandlerRequest->method)
\r
230 NS_LOG(DEBUG, "NSEntityHandlerTopicCb - OC_REST_GET");
\r
232 // send consumer's interesting topic list if consumer id exists
\r
233 // otherwise send registered topic list
\r
234 NSPushQueue(TOPIC_SCHEDULER, TASK_SEND_TOPICS,
\r
235 NSCopyOCEntityHandlerRequest(entityHandlerRequest));
\r
237 ehResult = OC_EH_OK;
\r
239 else if (OC_REST_POST == entityHandlerRequest->method)
\r
241 // Receive interesting topic list from consumers
\r
242 // Send topic notice message(id = TOPIC) to the consumer
\r
243 // which requests to post.
\r
244 NS_LOG(DEBUG, "NSEntityHandlerTopicCb - OC_REST_POST");
\r
246 // Accepter is provider. our service is not support sendtopiclist from OC_REST_POST
\r
247 ehResult = OC_EH_ERROR;
\r
249 // Accepter is consumer. our service is support sendtopiclist from OC_REST_POST
\r
252 NSPushQueue(TOPIC_SCHEDULER, TASK_POST_TOPIC,
\r
253 NSCopyOCEntityHandlerRequest(entityHandlerRequest));
\r
254 ehResult = OC_EH_OK;
\r
259 NS_LOG_V(DEBUG, "Received unsupported method %d from client",
\r
260 entityHandlerRequest->method);
\r
261 ehResult = OC_EH_ERROR;
\r
265 NS_LOG(DEBUG, "NSEntityHandlerTopicCb - OUT");
\r
269 void NSProviderConnectionStateListener(const CAEndpoint_t * info, bool connected)
\r
272 NS_LOG(DEBUG, "NSProviderConnectionStateListener - IN");
\r
276 NS_LOG(DEBUG, "CONNECTED");
\r
278 // Set Connection State
\r
279 NSSetProviderConnectionState(CONNECTED);
\r
282 NSPushQueue(DISCOVERY_SCHEDULER, TASK_START_PRESENCE, NULL);
\r
284 if(info->adapter == CA_ADAPTER_TCP)
\r
286 NS_LOG_V(DEBUG, "TCP Connected remote address: %s:%d", info->addr, info->port);
\r
292 NS_LOG(DEBUG, "DISCONNECTED");
\r
294 // Set Connection State
\r
295 NSSetProviderConnectionState(DISCONNECTED);
\r
297 if(info->adapter == CA_ADAPTER_TCP)
\r
299 NS_LOG_V(DEBUG, "TCP Disconnected remote address: %s:%d", info->addr, info->port);
\r
303 NS_LOG(DEBUG, "NSProviderConnectionStateListener - OUT");
\r
306 void NSProviderAdapterStateListener(CATransportAdapter_t adapter, bool enabled)
\r
310 NS_LOG(DEBUG, "NSProviderAdapterStateListener - IN");
\r
314 NS_LOG(DEBUG, "CONNECTED");
\r
316 // Set Connection State
\r
317 NSSetProviderConnectionState(CONNECTED);
\r
320 NSPushQueue(DISCOVERY_SCHEDULER, TASK_START_PRESENCE, NULL);
\r
325 NS_LOG(DEBUG, "DISCONNECTED");
\r
327 // Set Connection State
\r
328 NSSetProviderConnectionState(DISCONNECTED);
\r
331 NS_LOG(DEBUG, "NSProviderAdapterStateListener - OUT");
\r