1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 // PresenceClient.cpp : A client example for presence notification
27 #include <condition_variable>
29 #include "OCPlatform.h"
34 std::shared_ptr<OCResource> curResource;
35 std::mutex resourceLock;
36 static int TEST_CASE = 0;
38 static OCConnectivityType connectivityType = CT_ADAPTER_IP;
41 * List of methods that can be inititated from the client
44 TEST_UNICAST_PRESENCE_NORMAL = 1,
45 TEST_UNICAST_PRESENCE_WITH_FILTER,
46 TEST_UNICAST_PRESENCE_WITH_FILTERS,
47 TEST_MULTICAST_PRESENCE_NORMAL,
48 TEST_MULTICAST_PRESENCE_WITH_FILTER,
49 TEST_MULTICAST_PRESENCE_WITH_FILTERS,
55 std::cout << "Usage : presenceclient -t <1|2|3|4|5|6> -c <0|1>" << std::endl;
56 std::cout << "-t 1 : Discover Resources and Initiate Unicast Presence" << std::endl;
57 std::cout << "-t 2 : Discover Resources and Initiate Unicast Presence with Filter"
59 std::cout << "-t 3 : Discover Resources and Initiate Unicast Presence with Two Filters"
61 std::cout << "-t 4 : Discover Resources and Initiate Multicast Presence" << std::endl;
62 std::cout << "-t 5 : Discover Resources and Initiate Multicast Presence with Filter"
64 std::cout << "-t 6 : Discover Resources and Initiate Multicast Presence with two Filters"
66 std::cout<<"ConnectivityType: Default IPv4" << std::endl;
67 std::cout << "-c 0 : Send message with IPv4" << std::endl;
68 std::cout << "-c 1 : Send message with IPv6" << std::endl;
71 // Callback to presence
72 void presenceHandler(OCStackResult result, const unsigned int nonce, const std::string& hostAddress)
74 std::cout << "Received presence notification from : " << hostAddress << std::endl;
75 std::cout << "In presenceHandler: ";
80 std::cout << "Nonce# " << nonce << std::endl;
82 case OC_STACK_PRESENCE_STOPPED:
83 std::cout << "Presence Stopped\n";
85 case OC_STACK_PRESENCE_TIMEOUT:
86 std::cout << "Presence Timeout\n";
89 std::cout << "Error\n";
94 // Callback to found resources
95 void foundResource(std::shared_ptr<OCResource> resource)
97 std::lock_guard<std::mutex> lock(resourceLock);
100 std::cout << "Found another resource, ignoring"<<std::endl;
104 std::string resourceURI;
105 std::string hostAddress;
108 // Do some operations with resource object.
111 std::cout<<"DISCOVERED Resource:"<<std::endl;
112 // Get the resource URI
113 resourceURI = resource->uri();
114 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
116 // Get the resource host address
117 hostAddress = resource->host();
118 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
120 // Get the resource types
121 std::cout << "\tList of resource types: " << std::endl;
122 for(auto &resourceTypes : resource->getResourceTypes())
124 std::cout << "\t\t" << resourceTypes << std::endl;
127 // Get the resource interfaces
128 std::cout << "\tList of resource interfaces: " << std::endl;
129 for(auto &resourceInterfaces : resource->getResourceInterfaces())
131 std::cout << "\t\t" << resourceInterfaces << std::endl;
134 if(resourceURI == "/a/light")
136 OCStackResult result = OC_STACK_OK;
137 curResource = resource;
138 OCPlatform::OCPresenceHandle presenceHandle = nullptr;
140 if(TEST_CASE == TEST_UNICAST_PRESENCE_NORMAL)
142 result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
143 connectivityType, &presenceHandler);
144 if(result == OC_STACK_OK)
146 std::cout<< "Subscribed to unicast address: " << hostAddress << std::endl;
150 std::cout<< "Failed to subscribe to unicast address:" << hostAddress
154 if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTER ||
155 TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS)
157 result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
158 "core.light", connectivityType, &presenceHandler);
159 if(result == OC_STACK_OK)
161 std::cout<< "Subscribed to unicast address: " << hostAddress;
165 std::cout<< "Failed to subscribe to unicast address: " << hostAddress;
167 std::cout << " with resource type \"core.light\"." << std::endl;
169 if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS)
171 result = OCPlatform::subscribePresence(presenceHandle, hostAddress, "core.fan",
172 connectivityType, &presenceHandler);
173 if(result == OC_STACK_OK)
175 std::cout<< "Subscribed to unicast address: " << hostAddress;
179 std::cout<< "Failed to subscribe to unicast address: " << hostAddress;
181 std::cout << " with resource type \"core.fan\"." << std::endl;
187 // Resource is invalid
188 std::cout << "Resource is invalid" << std::endl;
192 catch(std::exception& e)
194 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
199 int main(int argc, char* argv[]) {
201 std::ostringstream requestURI;
209 while ((opt = getopt(argc, argv, "t:c:")) != -1)
214 TEST_CASE = std::stoi(optarg);
217 std::size_t inputValLen;
218 optionSelected = std::stoi(optarg, &inputValLen);
220 if(inputValLen == strlen(optarg))
222 if(optionSelected == 0)
224 connectivityType = CT_ADAPTER_IP;
228 std::cout << "Invalid connectivity type selected. Using default IP"
234 std::cout << "Invalid connectivity type selected. Using default IP"
244 catch(std::exception& )
246 std::cout << "Invalid input argument. Using IP as connectivity type"
250 if(TEST_CASE >= MAX_TESTS || TEST_CASE <= 0)
256 // Create PlatformConfig object
258 OC::ServiceType::InProc,
259 OC::ModeType::Client,
262 OC::QualityOfService::LowQos
265 OCPlatform::Configure(cfg);
269 std::cout << "Created Platform..."<<std::endl;
271 OCPlatform::OCPresenceHandle presenceHandle = nullptr;
272 OCStackResult result = OC_STACK_OK;
274 std::ostringstream multicastPresenceURI;
275 multicastPresenceURI << "coap://" << OC_MULTICAST_PREFIX;
277 if(TEST_CASE == TEST_MULTICAST_PRESENCE_NORMAL)
279 result = OCPlatform::subscribePresence(presenceHandle,
280 multicastPresenceURI.str(), connectivityType, presenceHandler);
282 if(result == OC_STACK_OK)
284 std::cout << "Subscribed to multicast presence." << std::endl;
288 std::cout << "Failed to subscribe to multicast presence." << std::endl;
291 else if(TEST_CASE == TEST_MULTICAST_PRESENCE_WITH_FILTER)
293 result = OCPlatform::subscribePresence(presenceHandle, multicastPresenceURI.str(), "core.light",
294 connectivityType, &presenceHandler);
295 if(result == OC_STACK_OK)
297 std::cout << "Subscribed to multicast presence with resource type";
301 std::cout << "Failed to subscribe to multicast presence with resource type";
303 std::cout << "\"core.light\"." << std::endl;
305 else if(TEST_CASE == TEST_MULTICAST_PRESENCE_WITH_FILTERS)
307 result = OCPlatform::subscribePresence(presenceHandle, multicastPresenceURI.str(), "core.light",
308 connectivityType, &presenceHandler);
309 if(result == OC_STACK_OK)
311 std::cout << "Subscribed to multicast presence with resource type";
315 std::cout << "Failed to subscribe to multicast presence with resource type";
317 std::cout << "\"core.light\"." << std::endl;
319 result = OCPlatform::subscribePresence(presenceHandle, multicastPresenceURI.str(), "core.fan",
320 connectivityType, &presenceHandler);
321 if(result == OC_STACK_OK)
323 std::cout<< "Subscribed to multicast presence with resource type";
327 std::cout << "Failed to subscribe to multicast presence with resource type.";
329 std::cout << "\"core.fan\"." << std::endl;
333 // Find all resources
334 requestURI << OC_MULTICAST_DISCOVERY_URI;
336 result = OCPlatform::findResource("", requestURI.str(),
337 CT_DEFAULT, &foundResource);
338 if(result == OC_STACK_OK)
340 std::cout << "Finding Resource... " << std::endl;
344 std::cout << "Failed to request to find resource(s)." << std::endl;
348 // A condition variable will free the mutex it is given, then do a non-
349 // intensive block until 'notify' is called on it. In this case, since we
350 // don't ever call cv.notify, this should be a non-processor intensive version
353 std::condition_variable cv;
354 std::unique_lock<std::mutex> lock(blocker);
358 catch(OCException& e)
360 oclog() << "Exception in main: "<< e.what();