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";
89 case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
90 std::cout << "Virtual do not handle\n";
93 std::cout << "Error\n";
98 // Callback to found resources
99 void foundResource(std::shared_ptr<OCResource> resource)
101 std::lock_guard<std::mutex> lock(resourceLock);
104 std::cout << "Found another resource, ignoring"<<std::endl;
108 std::string resourceURI;
109 std::string hostAddress;
112 // Do some operations with resource object.
115 std::cout<<"DISCOVERED Resource:"<<std::endl;
116 // Get the resource URI
117 resourceURI = resource->uri();
118 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
120 // Get the resource host address
121 hostAddress = resource->host();
122 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
124 // Get the resource types
125 std::cout << "\tList of resource types: " << std::endl;
126 for(auto &resourceTypes : resource->getResourceTypes())
128 std::cout << "\t\t" << resourceTypes << std::endl;
131 // Get the resource interfaces
132 std::cout << "\tList of resource interfaces: " << std::endl;
133 for(auto &resourceInterfaces : resource->getResourceInterfaces())
135 std::cout << "\t\t" << resourceInterfaces << std::endl;
138 if(resourceURI == "/a/light")
140 OCStackResult result = OC_STACK_OK;
141 curResource = resource;
142 OCPlatform::OCPresenceHandle presenceHandle = nullptr;
144 if(TEST_CASE == TEST_UNICAST_PRESENCE_NORMAL)
146 result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
147 connectivityType, &presenceHandler);
148 if(result == OC_STACK_OK)
150 std::cout<< "Subscribed to unicast address: " << hostAddress << std::endl;
154 std::cout<< "Failed to subscribe to unicast address:" << hostAddress
158 if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTER ||
159 TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS)
161 result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
162 "core.light", connectivityType, &presenceHandler);
163 if(result == OC_STACK_OK)
165 std::cout<< "Subscribed to unicast address: " << hostAddress;
169 std::cout<< "Failed to subscribe to unicast address: " << hostAddress;
171 std::cout << " with resource type \"core.light\"." << std::endl;
173 if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS)
175 result = OCPlatform::subscribePresence(presenceHandle, hostAddress, "core.fan",
176 connectivityType, &presenceHandler);
177 if(result == OC_STACK_OK)
179 std::cout<< "Subscribed to unicast address: " << hostAddress;
183 std::cout<< "Failed to subscribe to unicast address: " << hostAddress;
185 std::cout << " with resource type \"core.fan\"." << std::endl;
191 // Resource is invalid
192 std::cout << "Resource is invalid" << std::endl;
196 catch(std::exception& e)
202 int main(int argc, char* argv[]) {
204 std::ostringstream requestURI;
212 while ((opt = getopt(argc, argv, "t:c:")) != -1)
217 TEST_CASE = std::stoi(optarg);
220 std::size_t inputValLen;
221 optionSelected = std::stoi(optarg, &inputValLen);
223 if(inputValLen == strlen(optarg))
225 if(optionSelected == 0)
227 connectivityType = OC_ETHERNET;
229 else if(optionSelected == 1)
231 connectivityType = OC_WIFI;
235 std::cout << "Invalid connectivity type selected. Using default WIFI"
241 std::cout << "Invalid connectivity type selected. Using default WIFI"
251 catch(std::exception& e)
253 std::cout << "Invalid input argument. Using WIFI as connectivity type"
257 if(TEST_CASE >= MAX_TESTS || TEST_CASE <= 0)
263 // Create PlatformConfig object
265 OC::ServiceType::InProc,
266 OC::ModeType::Client,
269 OC::QualityOfService::LowQos
272 OCPlatform::Configure(cfg);
276 std::cout << "Created Platform..."<<std::endl;
278 OCPlatform::OCPresenceHandle presenceHandle = nullptr;
279 OCStackResult result = OC_STACK_OK;
281 if(TEST_CASE == TEST_MULTICAST_PRESENCE_NORMAL)
283 result = OCPlatform::subscribePresence(presenceHandle,
284 OC_MULTICAST_IP, connectivityType, presenceHandler);
286 if(result == OC_STACK_OK)
288 std::cout << "Subscribed to multicast presence." << std::endl;
292 std::cout << "Failed to subscribe to multicast presence." << std::endl;
295 else if(TEST_CASE == TEST_MULTICAST_PRESENCE_WITH_FILTER)
297 result = OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, "core.light",
298 connectivityType, &presenceHandler);
299 if(result == OC_STACK_OK)
301 std::cout << "Subscribed to multicast presence with resource type";
305 std::cout << "Failed to subscribe to multicast presence with resource type";
307 std::cout << "\"core.light\"." << std::endl;
309 else if(TEST_CASE == TEST_MULTICAST_PRESENCE_WITH_FILTERS)
311 result = OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, "core.light",
312 connectivityType, &presenceHandler);
313 if(result == OC_STACK_OK)
315 std::cout << "Subscribed to multicast presence with resource type";
319 std::cout << "Failed to subscribe to multicast presence with resource type";
321 std::cout << "\"core.light\"." << std::endl;
323 result = OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, "core.fan",
324 connectivityType, &presenceHandler);
325 if(result == OC_STACK_OK)
327 std::cout<< "Subscribed to multicast presence with resource type";
331 std::cout << "Failed to subscribe to multicast presence with resource type.";
333 std::cout << "\"core.fan\"." << std::endl;
337 // Find all resources
338 requestURI << OC_WELL_KNOWN_QUERY;
340 result = OCPlatform::findResource("", requestURI.str(),
341 connectivityType, &foundResource);
342 if(result == OC_STACK_OK)
344 std::cout << "Finding Resource... " << std::endl;
348 std::cout << "Failed to request to find resource(s)." << std::endl;
352 // A condition variable will free the mutex it is given, then do a non-
353 // intensive block until 'notify' is called on it. In this case, since we
354 // don't ever call cv.notify, this should be a non-processor intensive version
357 std::condition_variable cv;
358 std::unique_lock<std::mutex> lock(blocker);
362 catch(OCException& e)
364 oclog() << "Exception in main: "<< e.what();