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;
36 static int TEST_CASE = 0;
39 * List of methods that can be inititated from the client
42 TEST_UNICAST_PRESENCE_NORMAL = 1,
43 TEST_UNICAST_PRESENCE_WITH_FILTER,
44 TEST_UNICAST_PRESENCE_WITH_FILTERS,
45 TEST_MULTICAST_PRESENCE_NORMAL,
46 TEST_MULTICAST_PRESENCE_WITH_FILTER,
47 TEST_MULTICAST_PRESENCE_WITH_FILTERS,
53 std::cout << "Usage : presenceclient -t <1|2>" << std::endl;
54 std::cout << "-t 1 : Discover Resources and Initiate Unicast Presence" << std::endl;
55 std::cout << "-t 2 : Discover Resources and Initiate Unicast Presence with Filter"
57 std::cout << "-t 3 : Discover Resources and Initiate Unicast Presence with Two Filters"
59 std::cout << "-t 4 : Discover Resources and Initiate Multicast Presence" << std::endl;
60 std::cout << "-t 5 : Discover Resources and Initiate Multicast Presence with Filter"
62 std::cout << "-t 6 : Discover Resources and Initiate Multicast Presence with two Filters"
66 // Callback to presence
67 void presenceHandler(OCStackResult result, const unsigned int nonce, const std::string& hostAddress)
69 std::cout << "Received presence notification from : " << hostAddress << std::endl;
70 std::cout << "In presenceHandler: ";
75 std::cout << "Nonce# " << nonce << std::endl;
77 case OC_STACK_PRESENCE_STOPPED:
78 std::cout << "Presence Stopped\n";
80 case OC_STACK_PRESENCE_TIMEOUT:
81 std::cout << "Presence Timeout\n";
83 case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
84 std::cout << "Virtual do not handle\n";
87 std::cout << "Error\n";
92 // Callback to found resources
93 void foundResource(std::shared_ptr<OCResource> resource)
97 std::cout << "Found another resource, ignoring"<<std::endl;
100 std::string resourceURI;
101 std::string hostAddress;
104 // Do some operations with resource object.
107 std::cout<<"DISCOVERED Resource:"<<std::endl;
108 // Get the resource URI
109 resourceURI = resource->uri();
110 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
112 // Get the resource host address
113 hostAddress = resource->host();
114 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
116 // Get the resource types
117 std::cout << "\tList of resource types: " << std::endl;
118 for(auto &resourceTypes : resource->getResourceTypes())
120 std::cout << "\t\t" << resourceTypes << std::endl;
123 // Get the resource interfaces
124 std::cout << "\tList of resource interfaces: " << std::endl;
125 for(auto &resourceInterfaces : resource->getResourceInterfaces())
127 std::cout << "\t\t" << resourceInterfaces << std::endl;
130 if(resourceURI == "/a/light")
132 OCStackResult result = OC_STACK_OK;
133 curResource = resource;
134 OCPlatform::OCPresenceHandle presenceHandle = nullptr;
136 if(TEST_CASE == TEST_UNICAST_PRESENCE_NORMAL)
138 result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
140 if(result == OC_STACK_OK)
142 std::cout<< "Subscribed to unicast address: " << hostAddress << std::endl;
146 std::cout<< "Failed to subscribe to unicast address:" << hostAddress
150 if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTER ||
151 TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS)
153 result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
154 "core.light", &presenceHandler);
155 if(result == OC_STACK_OK)
157 std::cout<< "Subscribed to unicast address: " << hostAddress;
161 std::cout<< "Failed to subscribe to unicast address: " << hostAddress;
163 std::cout << " with resource type \"core.light\"." << std::endl;
165 if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS)
167 result = OCPlatform::subscribePresence(presenceHandle, hostAddress, "core.fan",
169 if(result == OC_STACK_OK)
171 std::cout<< "Subscribed to unicast address: " << hostAddress;
175 std::cout<< "Failed to subscribe to unicast address: " << hostAddress;
177 std::cout << " with resource type \"core.fan\"." << std::endl;
183 // Resource is invalid
184 std::cout << "Resource is invalid" << std::endl;
188 catch(std::exception& e)
194 int main(int argc, char* argv[]) {
197 while ((opt = getopt(argc, argv, "t:")) != -1)
202 TEST_CASE = atoi(optarg);
209 if(TEST_CASE >= MAX_TESTS || TEST_CASE <= 0)
215 // Create PlatformConfig object
217 OC::ServiceType::InProc,
218 OC::ModeType::Client,
221 OC::QualityOfService::LowQos
224 OCPlatform::Configure(cfg);
228 std::cout << "Created Platform..."<<std::endl;
230 OCPlatform::OCPresenceHandle presenceHandle = nullptr;
231 OCStackResult result = OC_STACK_OK;
233 if(TEST_CASE == TEST_MULTICAST_PRESENCE_NORMAL)
235 result = OCPlatform::subscribePresence(presenceHandle,
236 OC_MULTICAST_IP, presenceHandler);
237 if(result == OC_STACK_OK)
239 std::cout << "Subscribed to multicast presence." << std::endl;
243 std::cout << "Failed to subscribe to multicast presence." << std::endl;
246 else if(TEST_CASE == TEST_MULTICAST_PRESENCE_WITH_FILTER)
248 result = OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, "core.light",
250 if(result == OC_STACK_OK)
252 std::cout << "Subscribed to multicast presence with resource type";
256 std::cout << "Failed to subscribe to multicast presence with resource type";
258 std::cout << "\"core.light\"." << std::endl;
260 else if(TEST_CASE == TEST_MULTICAST_PRESENCE_WITH_FILTERS)
262 result = OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, "core.light",
264 if(result == OC_STACK_OK)
266 std::cout << "Subscribed to multicast presence with resource type";
270 std::cout << "Failed to subscribe to multicast presence with resource type";
272 std::cout << "\"core.light\"." << std::endl;
274 result = OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, "core.fan",
276 if(result == OC_STACK_OK)
278 std::cout<< "Subscribed to multicast presence with resource type";
282 std::cout << "Failed to subscribe to multicast presence with resource type.";
284 std::cout << "\"core.fan\"." << std::endl;
288 // Find all resources
289 result = OCPlatform::findResource("", "coap://224.0.1.187/oc/core", &foundResource);
290 if(result == OC_STACK_OK)
292 std::cout << "Finding Resource... " << std::endl;
296 std::cout << "Failed to request to find resource(s)." << std::endl;
300 // A condition variable will free the mutex it is given, then do a non-
301 // intensive block until 'notify' is called on it. In this case, since we
302 // don't ever call cv.notify, this should be a non-processor intensive version
305 std::condition_variable cv;
306 std::unique_lock<std::mutex> lock(blocker);
309 }catch(OCException& e)