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 OIC_LOG_V (INFO, LISTENER_TAG, "Inside entity handler - flags: 0x%x", flag);
\r
28 OCEntityHandlerResult ehResult = OC_EH_OK;
\r
29 OCEntityHandlerResponse response =
\r
30 { 0, 0, OC_EH_ERROR, 0, 0,
\r
37 if (!entityHandlerRequest)
\r
39 OIC_LOG (ERROR, LISTENER_TAG, "Invalid request pointer");
\r
43 // Initialize certain response fields
\r
44 response.numSendVendorSpecificHeaderOptions = 0;
\r
45 memset(response.sendVendorSpecificHeaderOptions, 0,
\r
46 sizeof response.sendVendorSpecificHeaderOptions);
\r
47 memset(response.resourceUri, 0, sizeof response.resourceUri);
\r
48 OCRepPayload* payload = NULL;
\r
50 if (flag & OC_REQUEST_FLAG)
\r
52 OIC_LOG (INFO, LISTENER_TAG, "Flag includes OC_REQUEST_FLAG");
\r
54 if (OC_REST_GET == entityHandlerRequest->method)
\r
56 OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_GET from client");
\r
58 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_POLICY, (void *)entityHandlerRequest);
\r
59 ehResult = OC_EH_OK;
\r
62 else if (OC_REST_PUT == entityHandlerRequest->method)
\r
64 OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_PUT from client");
\r
65 ehResult = OC_EH_OK;
\r
67 else if (OC_REST_POST == entityHandlerRequest->method)
\r
69 OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_POST from client");
\r
70 ehResult = OC_EH_OK;
\r
72 else if (OC_REST_DELETE == entityHandlerRequest->method)
\r
74 OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_DELETE from client");
\r
75 ehResult = OC_EH_OK;
\r
79 OIC_LOG_V (INFO, LISTENER_TAG, "Received unsupported method %d from client",
\r
80 entityHandlerRequest->method);
\r
81 ehResult = OC_EH_OK;
\r
84 // If the result isn't an error or forbidden, send response
\r
85 if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN)))
\r
87 // Format the response. Note this requires some info about the request
\r
88 response.requestHandle = entityHandlerRequest->requestHandle;
\r
89 response.resourceHandle = entityHandlerRequest->resource;
\r
90 response.ehResult = ehResult;
\r
91 //response.payload = reinterpret_cast<OCPayload*>(payload);
\r
92 response.payload = (OCPayload*) payload;
\r
93 // Indicate that response is NOT in a persistent buffer
\r
94 response.persistentBufferFlag = 0;
\r
96 // Handle vendor specific options
\r
97 if (entityHandlerRequest->rcvdVendorSpecificHeaderOptions
\r
98 && entityHandlerRequest->numRcvdVendorSpecificHeaderOptions)
\r
100 OIC_LOG (INFO, LISTENER_TAG, "Received vendor specific options");
\r
102 OCHeaderOption * rcvdOptions = entityHandlerRequest->rcvdVendorSpecificHeaderOptions;
\r
103 for (i = 0; i < entityHandlerRequest->numRcvdVendorSpecificHeaderOptions; i++)
\r
105 if (((OCHeaderOption) rcvdOptions[i]).protocolID == OC_COAP_ID)
\r
107 OIC_LOG_V(INFO, LISTENER_TAG, "Received option with OC_COAP_ID and ID %u with",
\r
108 ((OCHeaderOption)rcvdOptions[i]).optionID );
\r
110 OIC_LOG_BUFFER(INFO, LISTENER_TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
\r
111 MAX_HEADER_OPTION_DATA_LENGTH);
\r
114 OCHeaderOption * sendOptions = response.sendVendorSpecificHeaderOptions;
\r
115 uint8_t option2[] =
\r
116 { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
\r
117 uint8_t option3[] =
\r
118 { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
\r
119 sendOptions[0].protocolID = OC_COAP_ID;
\r
120 sendOptions[0].optionID = 2248;
\r
121 memcpy(sendOptions[0].optionData, option2, sizeof(option2));
\r
122 sendOptions[0].optionLength = 10;
\r
123 sendOptions[1].protocolID = OC_COAP_ID;
\r
124 sendOptions[1].optionID = 2600;
\r
125 memcpy(sendOptions[1].optionData, option3, sizeof(option3));
\r
126 sendOptions[1].optionLength = 10;
\r
127 response.numSendVendorSpecificHeaderOptions = 2;
\r
131 // Send the response
\r
132 /*if (OCDoResponse(&response) != OC_STACK_OK)
\r
134 OIC_LOG(ERROR, LISTENER_TAG, "Error sending response");
\r
135 ehResult = OC_EH_ERROR;
\r
140 OCPayloadDestroy(response.payload);
\r
144 OCEntityHandlerResult NSEntityHandlerMessageCb(OCEntityHandlerFlag flag,
\r
145 OCEntityHandlerRequest *entityHandlerRequest, void* callback)
\r
147 OIC_LOG_V (INFO, LISTENER_TAG, "Inside entity handler - flags: 0x%x", flag);
\r
149 OCEntityHandlerResult ehResult = OC_EH_OK;
\r
150 OCEntityHandlerResponse response =
\r
151 { 0, 0, OC_EH_ERROR, 0, 0,
\r
157 // Validate pointer
\r
158 if (!entityHandlerRequest)
\r
160 OIC_LOG (ERROR, LISTENER_TAG, "Invalid request pointer");
\r
161 return OC_EH_ERROR;
\r
164 // Initialize certain response fields
\r
165 response.numSendVendorSpecificHeaderOptions = 0;
\r
166 memset(response.sendVendorSpecificHeaderOptions, 0,
\r
167 sizeof response.sendVendorSpecificHeaderOptions);
\r
168 memset(response.resourceUri, 0, sizeof response.resourceUri);
\r
169 OCRepPayload* payload = NULL;
\r
171 if (flag & OC_REQUEST_FLAG)
\r
173 OIC_LOG (INFO, LISTENER_TAG, "Flag includes OC_REQUEST_FLAG");
\r
175 if (OC_REST_GET == entityHandlerRequest->method)
\r
177 OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_GET from client");
\r
178 ehResult = OC_EH_OK;
\r
180 else if (OC_REST_PUT == entityHandlerRequest->method)
\r
182 OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_PUT from client");
\r
183 ehResult = OC_EH_OK;
\r
185 else if (OC_REST_POST == entityHandlerRequest->method)
\r
187 OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_POST from client");
\r
188 ehResult = OC_EH_OK;
\r
190 else if (OC_REST_DELETE == entityHandlerRequest->method)
\r
192 OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_DELETE from client");
\r
193 ehResult = OC_EH_OK;
\r
197 OIC_LOG_V (INFO, LISTENER_TAG, "Received unsupported method %d from client",
\r
198 entityHandlerRequest->method);
\r
199 ehResult = OC_EH_OK;
\r
202 // If the result isn't an error or forbidden, send response
\r
203 if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN)))
\r
205 // Format the response. Note this requires some info about the request
\r
206 response.requestHandle = entityHandlerRequest->requestHandle;
\r
207 response.resourceHandle = entityHandlerRequest->resource;
\r
208 response.ehResult = ehResult;
\r
209 //response.payload = reinterpret_cast<OCPayload*>(payload);
\r
210 response.payload = (OCPayload*) payload;
\r
211 // Indicate that response is NOT in a persistent buffer
\r
212 response.persistentBufferFlag = 0;
\r
214 // Handle vendor specific options
\r
215 if (entityHandlerRequest->rcvdVendorSpecificHeaderOptions
\r
216 && entityHandlerRequest->numRcvdVendorSpecificHeaderOptions)
\r
218 OIC_LOG (INFO, LISTENER_TAG, "Received vendor specific options");
\r
220 OCHeaderOption * rcvdOptions = entityHandlerRequest->rcvdVendorSpecificHeaderOptions;
\r
221 for (i = 0; i < entityHandlerRequest->numRcvdVendorSpecificHeaderOptions; i++)
\r
223 if (((OCHeaderOption) rcvdOptions[i]).protocolID == OC_COAP_ID)
\r
225 OIC_LOG_V(INFO, LISTENER_TAG, "Received option with OC_COAP_ID and ID %u with",
\r
226 ((OCHeaderOption)rcvdOptions[i]).optionID );
\r
228 OIC_LOG_BUFFER(INFO, LISTENER_TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
\r
229 MAX_HEADER_OPTION_DATA_LENGTH);
\r
232 OCHeaderOption * sendOptions = response.sendVendorSpecificHeaderOptions;
\r
233 uint8_t option2[] =
\r
234 { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
\r
235 uint8_t option3[] =
\r
236 { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
\r
237 sendOptions[0].protocolID = OC_COAP_ID;
\r
238 sendOptions[0].optionID = 2248;
\r
239 memcpy(sendOptions[0].optionData, option2, sizeof(option2));
\r
240 sendOptions[0].optionLength = 10;
\r
241 sendOptions[1].protocolID = OC_COAP_ID;
\r
242 sendOptions[1].optionID = 2600;
\r
243 memcpy(sendOptions[1].optionData, option3, sizeof(option3));
\r
244 sendOptions[1].optionLength = 10;
\r
245 response.numSendVendorSpecificHeaderOptions = 2;
\r
250 if (flag & OC_OBSERVE_FLAG)
\r
252 OIC_LOG(INFO, LISTENER_TAG, "Flag includes OC_OBSERVE_FLAG");
\r
254 if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action)
\r
256 OIC_LOG (INFO, LISTENER_TAG, "Received OC_OBSERVE_REGISTER from client");
\r
258 printf("NS_ register message observerID : %d\n", entityHandlerRequest->obsInfo.obsId);
\r
259 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_RECV_SUBSCRIPTION, entityHandlerRequest);
\r
264 OCPayloadDestroy(response.payload);
\r
268 OCEntityHandlerResult NSEntityHandlerSyncCb(OCEntityHandlerFlag flag,
\r
269 OCEntityHandlerRequest *entityHandlerRequest, void* callback)
\r
271 OIC_LOG_V (INFO, LISTENER_TAG, "Inside entity handler - flags: 0x%x", flag);
\r
273 OCEntityHandlerResult ehResult = OC_EH_OK;
\r
274 OCEntityHandlerResponse response =
\r
275 { 0, 0, OC_EH_ERROR, 0, 0,
\r
281 // Validate pointer
\r
282 if (!entityHandlerRequest)
\r
284 OIC_LOG (ERROR, LISTENER_TAG, "Invalid request pointer");
\r
285 return OC_EH_ERROR;
\r
288 // Initialize certain response fields
\r
289 response.numSendVendorSpecificHeaderOptions = 0;
\r
290 memset(response.sendVendorSpecificHeaderOptions, 0,
\r
291 sizeof response.sendVendorSpecificHeaderOptions);
\r
292 memset(response.resourceUri, 0, sizeof response.resourceUri);
\r
293 OCRepPayload* payload = NULL;
\r
295 if (flag & OC_REQUEST_FLAG)
\r
297 OIC_LOG (INFO, LISTENER_TAG, "Flag includes OC_REQUEST_FLAG");
\r
299 if (OC_REST_GET == entityHandlerRequest->method)
\r
301 OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_GET from client");
\r
302 ehResult = OC_EH_OK;
\r
304 else if (OC_REST_PUT == entityHandlerRequest->method)
\r
306 OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_PUT from client");
\r
307 ehResult = OC_EH_OK;
\r
309 else if (OC_REST_POST == entityHandlerRequest->method)
\r
311 OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_POST from client");
\r
313 NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_READ, NSBuildOICNotificationSync(entityHandlerRequest->payload));
\r
314 ehResult = OC_EH_OK;
\r
316 else if (OC_REST_DELETE == entityHandlerRequest->method)
\r
318 OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_DELETE from client");
\r
319 ehResult = OC_EH_OK;
\r
323 OIC_LOG_V (INFO, LISTENER_TAG, "Received unsupported method %d from client",
\r
324 entityHandlerRequest->method);
\r
325 ehResult = OC_EH_OK;
\r
328 // If the result isn't an error or forbidden, send response
\r
329 if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN)))
\r
331 // Format the response. Note this requires some info about the request
\r
332 response.requestHandle = entityHandlerRequest->requestHandle;
\r
333 response.resourceHandle = entityHandlerRequest->resource;
\r
334 response.ehResult = ehResult;
\r
335 //response.payload = reinterpret_cast<OCPayload*>(payload);
\r
336 response.payload = (OCPayload*) payload;
\r
337 // Indicate that response is NOT in a persistent buffer
\r
338 response.persistentBufferFlag = 0;
\r
340 // Handle vendor specific options
\r
341 if (entityHandlerRequest->rcvdVendorSpecificHeaderOptions
\r
342 && entityHandlerRequest->numRcvdVendorSpecificHeaderOptions)
\r
344 OIC_LOG (INFO, LISTENER_TAG, "Received vendor specific options");
\r
346 OCHeaderOption * rcvdOptions = entityHandlerRequest->rcvdVendorSpecificHeaderOptions;
\r
347 for (i = 0; i < entityHandlerRequest->numRcvdVendorSpecificHeaderOptions; i++)
\r
349 if (((OCHeaderOption) rcvdOptions[i]).protocolID == OC_COAP_ID)
\r
351 OIC_LOG_V(INFO, LISTENER_TAG, "Received option with OC_COAP_ID and ID %u with",
\r
352 ((OCHeaderOption)rcvdOptions[i]).optionID );
\r
354 OIC_LOG_BUFFER(INFO, LISTENER_TAG, ((OCHeaderOption)rcvdOptions[i]).optionData,
\r
355 MAX_HEADER_OPTION_DATA_LENGTH);
\r
358 OCHeaderOption * sendOptions = response.sendVendorSpecificHeaderOptions;
\r
359 uint8_t option2[] =
\r
360 { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
\r
361 uint8_t option3[] =
\r
362 { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
\r
363 sendOptions[0].protocolID = OC_COAP_ID;
\r
364 sendOptions[0].optionID = 2248;
\r
365 memcpy(sendOptions[0].optionData, option2, sizeof(option2));
\r
366 sendOptions[0].optionLength = 10;
\r
367 sendOptions[1].protocolID = OC_COAP_ID;
\r
368 sendOptions[1].optionID = 2600;
\r
369 memcpy(sendOptions[1].optionData, option3, sizeof(option3));
\r
370 sendOptions[1].optionLength = 10;
\r
371 response.numSendVendorSpecificHeaderOptions = 2;
\r
376 if (flag & OC_OBSERVE_FLAG)
\r
378 OIC_LOG(INFO, LISTENER_TAG, "Flag includes OC_OBSERVE_FLAG");
\r
380 if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action)
\r
382 OIC_LOG (INFO, LISTENER_TAG, "Received OC_OBSERVE_REGISTER from client");
\r
383 printf("NS_ register sync observerID : %d\n", entityHandlerRequest->obsInfo.obsId);
\r
384 NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SYNC_SUBSCRIPTION, entityHandlerRequest);
\r
389 OCPayloadDestroy(response.payload);
\r
393 void NSProviderConnectionStateListener(CATransportAdapter_t adapter, const char *remote_address,
\r
396 OIC_LOG (INFO, LISTENER_TAG, "Connection State Changed");
\r
400 OIC_LOG (INFO, LISTENER_TAG, "CONNECTED");
\r
402 // Set Connection State
\r
403 NSSetProviderConnectionState(CONNECTED);
\r
406 NSPushQueue(DISCOVERY_SCHEDULER, TASK_START_PRESENCE, NULL);
\r
410 void NSProviderAdapterStateListener(CATransportAdapter_t adapter, bool enabled)
\r
412 OIC_LOG (INFO, LISTENER_TAG, "Adapter State Changed");
\r
416 OIC_LOG (INFO, LISTENER_TAG, "CONNECTED");
\r
418 // Set Connection State
\r
419 NSSetProviderConnectionState(CONNECTED);
\r
422 NSPushQueue(DISCOVERY_SCHEDULER, TASK_START_PRESENCE, NULL);
\r
426 NSSync * NSBuildOICNotificationSync(OCPayload * payload)
\r
432 NSSync * retSync = (NSSync *)OICMalloc(sizeof(NSSync));
\r
438 retSync->mMessageId = NULL;
\r
439 retSync->mState = Notification_Read;
\r
441 OCRepPayload * repPayload = (OCRepPayload *)payload;
\r
442 if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_ID, &retSync->mMessageId))
\r
444 OIC_LOG(DEBUG, LISTENER_TAG, "id of received sync is null");
\r
449 if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_STATE, & state))
\r
452 OIC_LOG(DEBUG, LISTENER_TAG, "id of received sync is null");
\r
454 OICFree(retSync->mMessageId);
\r
459 retSync->mState = (NSSyncTypes) state;
\r
461 OIC_LOG_V(DEBUG, LISTENER_TAG, "Sync ID : %s", retSync->mMessageId);
\r
462 OIC_LOG_V(DEBUG, LISTENER_TAG, "Sync State : %d", (int) retSync->mState);
\r
467 NSResult NSMakeTask(NSTaskType type, OCEntityHandlerRequest *request, NSTask * task)
\r
469 task = (NSTask*) OICMalloc(sizeof(NSTask));
\r
472 OIC_LOG(ERROR, LISTENER_TAG, PCF("Fail to allocate memory"));
\r
476 task->taskType = type;
\r
477 task->taskData = request;
\r
478 task->nextTask = NULL;
\r