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 IP" << std::endl;
67 std::cout << "-c 0 : Send message with IP" << std::endl;
70 // Callback to presence
71 void presenceHandler(OCStackResult result, const unsigned int nonce, const std::string& hostAddress)
73 std::cout << "Received presence notification from : " << hostAddress << std::endl;
74 std::cout << "In presenceHandler: ";
79 std::cout << "Nonce# " << nonce << std::endl;
81 case OC_STACK_PRESENCE_STOPPED:
82 std::cout << "Presence Stopped\n";
84 case OC_STACK_PRESENCE_TIMEOUT:
85 std::cout << "Presence Timeout\n";
88 std::cout << "Error\n";
93 // Callback to found resources
94 void foundResource(std::shared_ptr<OCResource> resource)
96 std::lock_guard<std::mutex> lock(resourceLock);
99 std::cout << "Found another resource, ignoring"<<std::endl;
103 std::string resourceURI;
104 std::string hostAddress;
105 std::string platformDiscoveryURI = "/oic/p";
106 std::string deviceDiscoveryURI = "/oic/d";
110 // Do some operations with resource object.
113 std::cout<<"DISCOVERED Resource:"<<std::endl;
114 // Get the resource URI
115 resourceURI = resource->uri();
116 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
118 // Get the resource host address
119 hostAddress = resource->host();
120 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
122 // Get the resource types
123 std::cout << "\tList of resource types: " << std::endl;
124 for(auto &resourceTypes : resource->getResourceTypes())
126 std::cout << "\t\t" << resourceTypes << std::endl;
129 // Get the resource interfaces
130 std::cout << "\tList of resource interfaces: " << std::endl;
131 for(auto &resourceInterfaces : resource->getResourceInterfaces())
133 std::cout << "\t\t" << resourceInterfaces << std::endl;
138 std::cout << "Querying for platform information... " << std::endl;
140 ret = OCPlatform::getPlatformInfo("", platformDiscoveryURI, CT_ADAPTER_IP, NULL);
142 if (ret == OC_STACK_OK)
144 std::cout << "Get platform information is done." << std::endl;
148 std::cout << "Get platform information failed." << std::endl;
151 std::cout << "Querying for device information... " << std::endl;
153 ret = OCPlatform::getDeviceInfo(resource->host(), deviceDiscoveryURI, CT_ADAPTER_IP,
156 if (ret == OC_STACK_OK)
158 std::cout << "Getting device information is done." << std::endl;
162 std::cout << "Getting device information failed." << std::endl;
165 if(resourceURI == "/a/light")
167 OCStackResult result = OC_STACK_OK;
168 curResource = resource;
169 OCPlatform::OCPresenceHandle presenceHandle = nullptr;
171 if(TEST_CASE == TEST_UNICAST_PRESENCE_NORMAL)
173 result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
174 connectivityType, &presenceHandler);
175 if(result == OC_STACK_OK)
177 std::cout<< "Subscribed to unicast address: " << hostAddress << std::endl;
181 std::cout<< "Failed to subscribe to unicast address:" << hostAddress
185 if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTER ||
186 TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS)
188 result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
189 "core.light", connectivityType, &presenceHandler);
190 if(result == OC_STACK_OK)
192 std::cout<< "Subscribed to unicast address: " << hostAddress;
196 std::cout<< "Failed to subscribe to unicast address: " << hostAddress;
198 std::cout << " with resource type \"core.light\"." << std::endl;
200 if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS)
202 result = OCPlatform::subscribePresence(presenceHandle, hostAddress, "core.fan",
203 connectivityType, &presenceHandler);
204 if(result == OC_STACK_OK)
206 std::cout<< "Subscribed to unicast address: " << hostAddress;
210 std::cout<< "Failed to subscribe to unicast address: " << hostAddress;
212 std::cout << " with resource type \"core.fan\"." << std::endl;
218 // Resource is invalid
219 std::cout << "Resource is invalid" << std::endl;
223 catch(std::exception& e)
225 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
230 int main(int argc, char* argv[]) {
232 std::ostringstream requestURI;
240 while ((opt = getopt(argc, argv, "t:c:")) != -1)
245 TEST_CASE = std::stoi(optarg);
248 std::size_t inputValLen;
249 optionSelected = std::stoi(optarg, &inputValLen);
251 if(inputValLen == strlen(optarg))
253 if(optionSelected == 0)
255 std::cout << "Using IP."<< std::endl;
256 connectivityType = CT_ADAPTER_IP;
260 std::cout << "Invalid connectivity type selected. Using default IP"
266 std::cout << "Invalid connectivity type selected. Using default IP"
276 catch(std::exception& )
278 std::cout << "Invalid input argument. Using IP as connectivity type"
282 if(TEST_CASE >= MAX_TESTS || TEST_CASE <= 0)
288 // Create PlatformConfig object
290 OC::ServiceType::InProc,
291 OC::ModeType::Client,
294 OC::QualityOfService::LowQos
297 OCPlatform::Configure(cfg);
301 std::cout << "Created Platform..."<<std::endl;
303 OCPlatform::OCPresenceHandle presenceHandle = nullptr;
304 OCStackResult result = OC_STACK_OK;
306 if(TEST_CASE == TEST_MULTICAST_PRESENCE_NORMAL)
308 result = OCPlatform::subscribePresence(presenceHandle,
313 if(result == OC_STACK_OK)
315 std::cout << "Subscribed to multicast presence." << std::endl;
319 std::cout << "Failed to subscribe to multicast presence." << std::endl;
322 else if(TEST_CASE == TEST_MULTICAST_PRESENCE_WITH_FILTER)
324 result = OCPlatform::subscribePresence(presenceHandle,
328 if(result == OC_STACK_OK)
330 std::cout << "Subscribed to multicast presence with resource type";
334 std::cout << "Failed to subscribe to multicast presence with resource type";
336 std::cout << "\"core.light\"." << std::endl;
338 else if(TEST_CASE == TEST_MULTICAST_PRESENCE_WITH_FILTERS)
340 result = OCPlatform::subscribePresence(presenceHandle,
344 if(result == OC_STACK_OK)
346 std::cout << "Subscribed to multicast presence with resource type";
350 std::cout << "Failed to subscribe to multicast presence with resource type";
352 std::cout << "\"core.light\"." << std::endl;
354 result = OCPlatform::subscribePresence(presenceHandle,
358 if(result == OC_STACK_OK)
360 std::cout<< "Subscribed to multicast presence with resource type";
364 std::cout << "Failed to subscribe to multicast presence with resource type.";
366 std::cout << "\"core.fan\"." << std::endl;
370 // Find all resources
371 requestURI << OC_RSRVD_WELL_KNOWN_URI;
373 result = OCPlatform::findResource("", requestURI.str(),
374 CT_DEFAULT, &foundResource);
375 if(result == OC_STACK_OK)
377 std::cout << "Finding Resource... " << std::endl;
381 std::cout << "Failed to request to find resource(s)." << std::endl;
385 // A condition variable will free the mutex it is given, then do a non-
386 // intensive block until 'notify' is called on it. In this case, since we
387 // don't ever call cv.notify, this should be a non-processor intensive version
390 std::condition_variable cv;
391 std::unique_lock<std::mutex> lock(blocker);
395 catch(OCException& e)
397 oclog() << "Exception in main: "<< e.what();