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;
37 static int TEST_CASE = 0;
39 static OCConnectivityType connectivityType = OC_WIFI;
42 * List of methods that can be inititated from the client
45 TEST_UNICAST_PRESENCE_NORMAL = 1,
46 TEST_UNICAST_PRESENCE_WITH_FILTER,
47 TEST_UNICAST_PRESENCE_WITH_FILTERS,
48 TEST_MULTICAST_PRESENCE_NORMAL,
49 TEST_MULTICAST_PRESENCE_WITH_FILTER,
50 TEST_MULTICAST_PRESENCE_WITH_FILTERS,
56 std::cout << "Usage : presenceclient -t <1|2|3|4|5|6> -c <0|1>" << std::endl;
57 std::cout << "-t 1 : Discover Resources and Initiate Unicast Presence" << std::endl;
58 std::cout << "-t 2 : Discover Resources and Initiate Unicast Presence with Filter"
60 std::cout << "-t 3 : Discover Resources and Initiate Unicast Presence with Two Filters"
62 std::cout << "-t 4 : Discover Resources and Initiate Multicast Presence" << std::endl;
63 std::cout << "-t 5 : Discover Resources and Initiate Multicast Presence with Filter"
65 std::cout << "-t 6 : Discover Resources and Initiate Multicast Presence with two Filters"
67 std::cout<<"ConnectivityType: Default WIFI" << std::endl;
68 std::cout << "-c 0 : Send message over ETHERNET interface" << std::endl;
69 std::cout << "-c 1 : Send message over WIFI interface" << std::endl;
72 // Callback to presence
73 void presenceHandler(OCStackResult result, const unsigned int nonce, const std::string& hostAddress)
75 std::cout << "Received presence notification from : " << hostAddress << std::endl;
76 std::cout << "In presenceHandler: ";
81 std::cout << "Nonce# " << nonce << std::endl;
83 case OC_STACK_PRESENCE_STOPPED:
84 std::cout << "Presence Stopped\n";
86 case OC_STACK_PRESENCE_TIMEOUT:
87 std::cout << "Presence Timeout\n";
90 std::cout << "Error\n";
95 // Callback to found resources
96 void foundResource(std::shared_ptr<OCResource> resource)
98 std::lock_guard<std::mutex> lock(resourceLock);
101 std::cout << "Found another resource, ignoring"<<std::endl;
105 std::string resourceURI;
106 std::string hostAddress;
109 // Do some operations with resource object.
112 std::cout<<"DISCOVERED Resource:"<<std::endl;
113 // Get the resource URI
114 resourceURI = resource->uri();
115 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
117 // Get the resource host address
118 hostAddress = resource->host();
119 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
121 // Get the resource types
122 std::cout << "\tList of resource types: " << std::endl;
123 for(auto &resourceTypes : resource->getResourceTypes())
125 std::cout << "\t\t" << resourceTypes << std::endl;
128 // Get the resource interfaces
129 std::cout << "\tList of resource interfaces: " << std::endl;
130 for(auto &resourceInterfaces : resource->getResourceInterfaces())
132 std::cout << "\t\t" << resourceInterfaces << std::endl;
135 if(resourceURI == "/a/light")
137 OCStackResult result = OC_STACK_OK;
138 curResource = resource;
139 OCPlatform::OCPresenceHandle presenceHandle = nullptr;
141 if(TEST_CASE == TEST_UNICAST_PRESENCE_NORMAL)
143 result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
144 connectivityType, &presenceHandler);
145 if(result == OC_STACK_OK)
147 std::cout<< "Subscribed to unicast address: " << hostAddress << std::endl;
151 std::cout<< "Failed to subscribe to unicast address:" << hostAddress
155 if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTER ||
156 TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS)
158 result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
159 "core.light", connectivityType, &presenceHandler);
160 if(result == OC_STACK_OK)
162 std::cout<< "Subscribed to unicast address: " << hostAddress;
166 std::cout<< "Failed to subscribe to unicast address: " << hostAddress;
168 std::cout << " with resource type \"core.light\"." << std::endl;
170 if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS)
172 result = OCPlatform::subscribePresence(presenceHandle, hostAddress, "core.fan",
173 connectivityType, &presenceHandler);
174 if(result == OC_STACK_OK)
176 std::cout<< "Subscribed to unicast address: " << hostAddress;
180 std::cout<< "Failed to subscribe to unicast address: " << hostAddress;
182 std::cout << " with resource type \"core.fan\"." << std::endl;
188 // Resource is invalid
189 std::cout << "Resource is invalid" << std::endl;
193 catch(std::exception& e)
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 = OC_ETHERNET;
226 else if(optionSelected == 1)
228 connectivityType = OC_WIFI;
232 std::cout << "Invalid connectivity type selected. Using default WIFI"
238 std::cout << "Invalid connectivity type selected. Using default WIFI"
248 catch(std::exception& e)
250 std::cout << "Invalid input argument. Using WIFI as connectivity type"
254 if(TEST_CASE >= MAX_TESTS || TEST_CASE <= 0)
260 // Create PlatformConfig object
262 OC::ServiceType::InProc,
263 OC::ModeType::Client,
266 OC::QualityOfService::LowQos
269 OCPlatform::Configure(cfg);
273 std::cout << "Created Platform..."<<std::endl;
275 OCPlatform::OCPresenceHandle presenceHandle = nullptr;
276 OCStackResult result = OC_STACK_OK;
278 if(TEST_CASE == TEST_MULTICAST_PRESENCE_NORMAL)
280 result = OCPlatform::subscribePresence(presenceHandle,
281 OC_MULTICAST_IP, connectivityType, presenceHandler);
283 if(result == OC_STACK_OK)
285 std::cout << "Subscribed to multicast presence." << std::endl;
289 std::cout << "Failed to subscribe to multicast presence." << std::endl;
292 else if(TEST_CASE == TEST_MULTICAST_PRESENCE_WITH_FILTER)
294 result = OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, "core.light",
295 connectivityType, &presenceHandler);
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, OC_MULTICAST_IP, "core.light",
309 connectivityType, &presenceHandler);
310 if(result == OC_STACK_OK)
312 std::cout << "Subscribed to multicast presence with resource type";
316 std::cout << "Failed to subscribe to multicast presence with resource type";
318 std::cout << "\"core.light\"." << std::endl;
320 result = OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, "core.fan",
321 connectivityType, &presenceHandler);
322 if(result == OC_STACK_OK)
324 std::cout<< "Subscribed to multicast presence with resource type";
328 std::cout << "Failed to subscribe to multicast presence with resource type.";
330 std::cout << "\"core.fan\"." << std::endl;
334 // Find all resources
335 requestURI << OC_WELL_KNOWN_QUERY;
337 result = OCPlatform::findResource("", requestURI.str(),
338 connectivityType, &foundResource);
339 if(result == OC_STACK_OK)
341 std::cout << "Finding Resource... " << std::endl;
345 std::cout << "Failed to request to find resource(s)." << std::endl;
349 // A condition variable will free the mutex it is given, then do a non-
350 // intensive block until 'notify' is called on it. In this case, since we
351 // don't ever call cv.notify, this should be a non-processor intensive version
354 std::condition_variable cv;
355 std::unique_lock<std::mutex> lock(blocker);
359 catch(OCException& e)
361 oclog() << "Exception in main: "<< e.what();