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;
107 // Do some operations with resource object.
110 std::cout<<"DISCOVERED Resource:"<<std::endl;
111 // Get the resource URI
112 resourceURI = resource->uri();
113 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
115 // Get the resource host address
116 hostAddress = resource->host();
117 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
119 // Get the resource types
120 std::cout << "\tList of resource types: " << std::endl;
121 for(auto &resourceTypes : resource->getResourceTypes())
123 std::cout << "\t\t" << resourceTypes << std::endl;
126 // Get the resource interfaces
127 std::cout << "\tList of resource interfaces: " << std::endl;
128 for(auto &resourceInterfaces : resource->getResourceInterfaces())
130 std::cout << "\t\t" << resourceInterfaces << std::endl;
133 if(resourceURI == "/a/light")
135 OCStackResult result = OC_STACK_OK;
136 curResource = resource;
137 OCPlatform::OCPresenceHandle presenceHandle = nullptr;
139 if(TEST_CASE == TEST_UNICAST_PRESENCE_NORMAL)
141 result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
142 connectivityType, &presenceHandler);
143 if(result == OC_STACK_OK)
145 std::cout<< "Subscribed to unicast address: " << hostAddress << std::endl;
149 std::cout<< "Failed to subscribe to unicast address:" << hostAddress
153 if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTER ||
154 TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS)
156 result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
157 "core.light", connectivityType, &presenceHandler);
158 if(result == OC_STACK_OK)
160 std::cout<< "Subscribed to unicast address: " << hostAddress;
164 std::cout<< "Failed to subscribe to unicast address: " << hostAddress;
166 std::cout << " with resource type \"core.light\"." << std::endl;
168 if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS)
170 result = OCPlatform::subscribePresence(presenceHandle, hostAddress, "core.fan",
171 connectivityType, &presenceHandler);
172 if(result == OC_STACK_OK)
174 std::cout<< "Subscribed to unicast address: " << hostAddress;
178 std::cout<< "Failed to subscribe to unicast address: " << hostAddress;
180 std::cout << " with resource type \"core.fan\"." << std::endl;
186 // Resource is invalid
187 std::cout << "Resource is invalid" << std::endl;
191 catch(std::exception& e)
193 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
198 int main(int argc, char* argv[]) {
200 std::ostringstream requestURI;
208 while ((opt = getopt(argc, argv, "t:c:")) != -1)
213 TEST_CASE = std::stoi(optarg);
216 std::size_t inputValLen;
217 optionSelected = std::stoi(optarg, &inputValLen);
219 if(inputValLen == strlen(optarg))
221 if(optionSelected == 0)
223 std::cout << "Using IP."<< std::endl;
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 if(TEST_CASE == TEST_MULTICAST_PRESENCE_NORMAL)
276 result = OCPlatform::subscribePresence(presenceHandle,
281 if(result == OC_STACK_OK)
283 std::cout << "Subscribed to multicast presence." << std::endl;
287 std::cout << "Failed to subscribe to multicast presence." << std::endl;
290 else if(TEST_CASE == TEST_MULTICAST_PRESENCE_WITH_FILTER)
292 result = OCPlatform::subscribePresence(presenceHandle,
296 if(result == OC_STACK_OK)
298 std::cout << "Subscribed to multicast presence with resource type";
302 std::cout << "Failed to subscribe to multicast presence with resource type";
304 std::cout << "\"core.light\"." << std::endl;
306 else if(TEST_CASE == TEST_MULTICAST_PRESENCE_WITH_FILTERS)
308 result = OCPlatform::subscribePresence(presenceHandle,
312 if(result == OC_STACK_OK)
314 std::cout << "Subscribed to multicast presence with resource type";
318 std::cout << "Failed to subscribe to multicast presence with resource type";
320 std::cout << "\"core.light\"." << std::endl;
322 result = OCPlatform::subscribePresence(presenceHandle,
326 if(result == OC_STACK_OK)
328 std::cout<< "Subscribed to multicast presence with resource type";
332 std::cout << "Failed to subscribe to multicast presence with resource type.";
334 std::cout << "\"core.fan\"." << std::endl;
338 // Find all resources
339 requestURI << OC_RSRVD_WELL_KNOWN_URI;
341 result = OCPlatform::findResource("", requestURI.str(),
342 CT_DEFAULT, &foundResource);
343 if(result == OC_STACK_OK)
345 std::cout << "Finding Resource... " << std::endl;
349 std::cout << "Failed to request to find resource(s)." << std::endl;
353 // A condition variable will free the mutex it is given, then do a non-
354 // intensive block until 'notify' is called on it. In this case, since we
355 // don't ever call cv.notify, this should be a non-processor intensive version
358 std::condition_variable cv;
359 std::unique_lock<std::mutex> lock(blocker);
363 catch(OCException& e)
365 oclog() << "Exception in main: "<< e.what();