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 "ocpayload.h"
27 #include "oic_malloc.h"
29 #define NS_PAYLOAD_KEY_ACCEPTER "ACCEPTER"
30 #define NS_DISCOVER_QUERY "/oic/res?rt=oic.r.notification"
31 #define NS_PRESENCE_SUBSCRIBE_QUERY "coap://224.0.1.187:5683/oic/ad?rt=oic.r.notification"
32 #define NS_GET_INFORMATION_QUERY "/notification?if=oic.if.notification"
34 OCDoHandle * getPresenceHandle()
36 static OCDoHandle * g_PresenceHandle = NULL;
38 return g_PresenceHandle;
41 OCStackApplicationResult NSConsumerPresenceListener(
42 OCDoHandle handle, OCClientResponse * clientResponse)
45 NS_CONSUMER_LOG_V(DEBUG, "Presence income : %s:%d",
46 clientResponse->devAddr.addr, clientResponse->devAddr.port);
47 NS_CONSUMER_LOG_V(DEBUG, "Presence result : %d",
48 clientResponse->result);
49 NS_CONSUMER_LOG_V(DEBUG, "Presence sequenceNum : %d",
50 clientResponse->sequenceNumber);
52 if (!NSIsStartedConsumer())
54 return OC_STACK_DELETE_TRANSACTION;
57 OCPresencePayload * payload = (OCPresencePayload *)clientResponse->payload;
58 if (payload->trigger == OC_PRESENCE_TRIGGER_DELETE ||
59 clientResponse->result == OC_STACK_PRESENCE_STOPPED)
61 // TODO find request and cancel
62 NS_CONSUMER_LOG(DEBUG, "stopped presence or resource is deleted.");
63 //OCCancel(handle, NS_QOS, NULL, 0);
66 else if (payload->trigger == OC_PRESENCE_TRIGGER_CREATE)
68 NSRequestToResourceIntrospection(NULL, OC_REST_DISCOVER, clientResponse->addr,
69 NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener);
72 return OC_STACK_KEEP_TRANSACTION;
75 OCStackApplicationResult NSProviderDiscoverListener(
76 OCDoHandle handle, OCClientResponse * clientResponse)
79 NS_CONSUMER_LOG_V(DEBUG, "Discover income : %s:%d",
80 clientResponse->devAddr.addr, clientResponse->devAddr.port);
81 NS_CONSUMER_LOG_V(DEBUG, "Discover result : %d",
82 clientResponse->result);
83 NS_CONSUMER_LOG_V(DEBUG, "Discover sequenceNum : %d",
84 clientResponse->sequenceNumber);
86 if (!NSIsStartedConsumer())
88 return OC_STACK_DELETE_TRANSACTION;
91 if (!clientResponse->payload)
93 return OC_STACK_KEEP_TRANSACTION;
96 OCResourcePayload * resource = ((OCDiscoveryPayload *)clientResponse->payload)->resources;
99 if (!strcmp(resource->uri, NS_RESOURCE_URI))
101 NSRequestToResourceIntrospection(
102 NULL, OC_REST_GET, clientResponse->addr,
103 NS_RESOURCE_URI, NULL, NSGetProviderInformation);
105 resource = resource->next;
108 return OC_STACK_KEEP_TRANSACTION;
111 OCStackApplicationResult NSGetProviderInformation(
112 OCDoHandle handle, OCClientResponse * clientResponse)
115 int64_t accepter = 0;
117 NS_CONSUMER_LOG_V(DEBUG, "GET response income : %s:%d",
118 clientResponse->devAddr.addr, clientResponse->devAddr.port);
119 NS_CONSUMER_LOG_V(DEBUG, "GET response result : %d",
120 clientResponse->result);
121 NS_CONSUMER_LOG_V(DEBUG, "GET response sequenceNum : %d",
122 clientResponse->sequenceNumber);
123 NS_CONSUMER_LOG_V(DEBUG, "GET response resource uri : %s",
124 clientResponse->resourceUri);
126 if (!NSIsStartedConsumer())
128 return OC_STACK_DELETE_TRANSACTION;
131 if (!clientResponse->payload)
133 NS_CONSUMER_LOG(ERROR, "payload is null");
134 return OC_STACK_KEEP_TRANSACTION;
137 if (!OCRepPayloadGetPropInt((OCRepPayload *)clientResponse->payload,
138 NS_PAYLOAD_KEY_ACCEPTER, & accepter))
140 NS_CONSUMER_LOG(ERROR, "can not seach for accepter");
141 return OC_STACK_KEEP_TRANSACTION;
144 NSProvider * newProvider = (NSProvider *)OICMalloc(sizeof(NSProvider));
147 NS_CONSUMER_LOG(DEBUG, "NSProvider allocation fail");
148 return OC_STACK_KEEP_TRANSACTION;
152 newProvider->mId = NULL;
153 newProvider->mUserData = (void *)OICMalloc(sizeof(OCDevAddr));
156 NS_CONSUMER_LOG(DEBUG, "OCDevAddr allocation fail");
157 OICFree(newProvider);
158 return OC_STACK_KEEP_TRANSACTION;
160 memcpy(newProvider->mUserData, clientResponse->addr, sizeof(OCDevAddr));
163 OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
166 NS_CONSUMER_LOG_V(DEBUG, "Payload Key : %s", payload->values->name);
167 payload = payload->next;
171 if (!OCRepPayloadGetPropString((OCRepPayload *)clientResponse->payload,
172 "MESSAGE_URI", & newProvider->messageUri))
174 OICFree(newProvider->mUserData);
175 OICFree(newProvider);
176 NS_CONSUMER_LOG(ERROR, "can not seach for message uri");
177 return OC_STACK_KEEP_TRANSACTION;
180 if (!OCRepPayloadGetPropString((OCRepPayload *)clientResponse->payload,
181 NS_ATTRIBUTE_SYNC, & newProvider->syncUri))
183 OICFree(newProvider->messageUri);
184 OICFree(newProvider->mUserData);
185 OICFree(newProvider);
186 NS_CONSUMER_LOG(ERROR, "can not seach for sync uri");
187 return OC_STACK_KEEP_TRANSACTION;
190 if (accepter == NS_ACCEPTER_CONSUMER)
192 NS_CONSUMER_LOG(DEBUG, "accepter is NS_ACCEPTER_CONSUMER, Callback to user");
194 NSDiscoveredProvider(newProvider);
198 NS_CONSUMER_LOG(DEBUG, "accepter is NS_ACCEPTER_PROVIDER, request subscribe");
200 NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) newProvider);
203 NS_CONSUMER_LOG(DEBUG, "NSTask allocation fail");
204 return OC_STACK_KEEP_TRANSACTION;
207 NSConsumerPushEvent(task);
210 return OC_STACK_KEEP_TRANSACTION;
213 void NSConsumerDiscoveryHandleMsg(NSTask * task)
217 NS_CONSUMER_LOG(ERROR, "task is null");
221 NS_CONSUMER_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
222 if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_CONSUMER_REQ_DISCOVER)
224 NSRequestToResourceIntrospection(NULL, OC_REST_DISCOVER, NULL, NS_DISCOVER_QUERY,
225 NULL, NSProviderDiscoverListener);
229 NS_CONSUMER_LOG(ERROR, "Unknown type message");