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 "NSConsumerDiscovery.h"
25 #include "NSConsumerCommon.h"
26 #include "NSConstants.h"
27 #include "ocpayload.h"
28 #include "oic_malloc.h"
30 #define NS_PAYLOAD_KEY_ACCEPTER "ACCEPTER"
31 #define NS_DISCOVER_QUERY "/oic/res?rt=oic.r.notification"
32 #define NS_PRESENCE_SUBSCRIBE_QUERY "coap://224.0.1.187:5683/oic/ad?rt=oic.r.notification"
33 #define NS_GET_INFORMATION_QUERY "/notification?if=oic.if.notification"
35 OCStackApplicationResult NSConsumerPresenceListener(
36 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
41 NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
42 NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
44 NS_LOG_V(DEBUG, "Presence income : %s:%d",
45 clientResponse->devAddr.addr, clientResponse->devAddr.port);
46 NS_LOG_V(DEBUG, "Presence result : %d",
47 clientResponse->result);
48 NS_LOG_V(DEBUG, "Presence sequenceNum : %d",
49 clientResponse->sequenceNumber);
51 if (!NSIsStartedConsumer())
53 return OC_STACK_DELETE_TRANSACTION;
56 OCPresencePayload * payload = (OCPresencePayload *)clientResponse->payload;
57 if (payload->trigger == OC_PRESENCE_TRIGGER_DELETE ||
58 clientResponse->result == OC_STACK_PRESENCE_STOPPED)
60 // TODO find request and cancel
61 NS_LOG(DEBUG, "stopped presence or resource is deleted.");
62 //OCCancel(handle, NS_QOS, NULL, 0);
65 else if (payload->trigger == OC_PRESENCE_TRIGGER_CREATE)
67 NSInvokeRequest(NULL, OC_REST_DISCOVER, clientResponse->addr,
68 NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL);
71 return OC_STACK_KEEP_TRANSACTION;
74 OCStackApplicationResult NSProviderDiscoverListener(
75 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
80 NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
81 NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
83 NS_LOG_V(DEBUG, "Discover income : %s:%d",
84 clientResponse->devAddr.addr, clientResponse->devAddr.port);
85 NS_LOG_V(DEBUG, "Discover result : %d",
86 clientResponse->result);
87 NS_LOG_V(DEBUG, "Discover sequenceNum : %d",
88 clientResponse->sequenceNumber);
90 if (!NSIsStartedConsumer())
92 return OC_STACK_DELETE_TRANSACTION;
95 if (!clientResponse->payload)
97 return OC_STACK_KEEP_TRANSACTION;
100 OCResourcePayload * resource = ((OCDiscoveryPayload *)clientResponse->payload)->resources;
103 if (!strcmp(resource->uri, NS_RESOURCE_URI))
105 NSInvokeRequest(NULL, OC_REST_GET, clientResponse->addr,
106 NS_RESOURCE_URI, NULL, NSIntrospectProvider, NULL);
108 resource = resource->next;
111 return OC_STACK_KEEP_TRANSACTION;
114 OCStackApplicationResult NSIntrospectProvider(
115 void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
120 NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
121 NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
123 int64_t accepter = 0;
124 char * messageUri = NULL;
125 char * syncUri = NULL;
127 NS_LOG_V(DEBUG, "GET response income : %s:%d",
128 clientResponse->devAddr.addr, clientResponse->devAddr.port);
129 NS_LOG_V(DEBUG, "GET response result : %d",
130 clientResponse->result);
131 NS_LOG_V(DEBUG, "GET response sequenceNum : %d",
132 clientResponse->sequenceNumber);
133 NS_LOG_V(DEBUG, "GET response resource uri : %s",
134 clientResponse->resourceUri);
136 if (!NSIsStartedConsumer())
138 return OC_STACK_DELETE_TRANSACTION;
141 NS_VERTIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
143 OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
146 NS_LOG_V(DEBUG, "Payload Key : %s", payload->values->name);
147 payload = payload->next;
150 NS_LOG(DEBUG, "getting information of accepter");
151 bool getResult = OCRepPayloadGetPropInt((OCRepPayload *)clientResponse->payload,
152 NS_PAYLOAD_KEY_ACCEPTER, & accepter);
153 NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, OC_STACK_KEEP_TRANSACTION);
155 NS_LOG(DEBUG, "getting message URI");
156 getResult = OCRepPayloadGetPropString(
157 (OCRepPayload *)clientResponse->payload,
158 "MESSAGE_URI", & messageUri);
159 NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, OC_STACK_KEEP_TRANSACTION);
161 NS_LOG(DEBUG, "getting sync URI");
162 getResult = OCRepPayloadGetPropString((OCRepPayload *)clientResponse->payload,
163 NS_ATTRIBUTE_SYNC, & syncUri);
164 NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL,
165 OC_STACK_KEEP_TRANSACTION, OICFree(messageUri));
167 NSProvider * newProvider = (NSProvider *)OICMalloc(sizeof(NSProvider));
168 NS_VERTIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
171 newProvider->mId = NULL;
172 newProvider->messageUri = messageUri;
173 newProvider->syncUri = syncUri;
175 newProvider->mUserData = (void *)OICMalloc(sizeof(OCDevAddr));
176 NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(newProvider->mUserData,
177 OC_STACK_KEEP_TRANSACTION, OICFree(newProvider));
178 memcpy(newProvider->mUserData, clientResponse->addr, sizeof(OCDevAddr));
180 if (accepter == NS_ACCEPTER_CONSUMER)
182 NS_LOG(DEBUG, "accepter is NS_ACCEPTER_CONSUMER, Callback to user");
184 NSDiscoveredProvider(newProvider);
188 NS_LOG(DEBUG, "accepter is NS_ACCEPTER_PROVIDER, request subscribe");
190 NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) newProvider);
191 NS_VERTIFY_NOT_NULL(task, OC_STACK_KEEP_TRANSACTION);
193 NSConsumerPushEvent(task);
196 return OC_STACK_KEEP_TRANSACTION;
199 void NSConsumerDiscoveryTaskProcessing(NSTask * task)
201 NS_VERTIFY_NOT_NULL_V(task);
203 NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
204 if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_CONSUMER_REQ_DISCOVER)
206 NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL, NS_DISCOVER_QUERY,
207 NULL, NSProviderDiscoverListener, NULL);
211 NS_LOG(ERROR, "Unknown type message");