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
26 #include "OCPlatform.h"
31 std::shared_ptr<OCResource> curResource;
33 // Callback to presence
34 void presenceHandler(OCStackResult result, const unsigned int nonce)
39 std::cout << "Nonce# " << nonce << std::endl;
41 case OC_STACK_PRESENCE_STOPPED:
42 std::cout << "Presence Stopped\n";
44 case OC_STACK_PRESENCE_DO_NOT_HANDLE:
45 std::cout << "Presence do not handle\n";
48 std::cout << "Error\n";
53 // Callback to found resources
54 void foundResource(std::shared_ptr<OCResource> resource)
58 std::cout << "Found another resource, ignoring"<<std::endl;
61 std::string resourceURI;
62 std::string hostAddress;
65 // Do some operations with resource object.
68 std::cout<<"DISCOVERED Resource:"<<std::endl;
69 // Get the resource URI
70 resourceURI = resource->uri();
71 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
73 // Get the resource host address
74 hostAddress = resource->host();
75 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
77 // Get the resource types
78 std::cout << "\tList of resource types: " << std::endl;
79 for(auto &resourceTypes : resource->getResourceTypes())
81 std::cout << "\t\t" << resourceTypes << std::endl;
84 // Get the resource interfaces
85 std::cout << "\tList of resource interfaces: " << std::endl;
86 for(auto &resourceInterfaces : resource->getResourceInterfaces())
88 std::cout << "\t\t" << resourceInterfaces << std::endl;
91 if(resourceURI == "/a/light")
93 curResource = resource;
94 OCPlatform::OCPresenceHandle presenceHandle;
95 OCPlatform::subscribePresence(presenceHandle, hostAddress, &presenceHandler);
96 std::cout<< "subscribing for unicast presence... " <<std::endl;
101 // Resource is invalid
102 std::cout << "Resource is invalid" << std::endl;
106 catch(std::exception& e)
114 std::cout << std::endl;
115 std::cout << "Usage : presenceclient <isMulticastAddress>\n";
116 std::cout << " 0 - unicast subcribe presence\n";
117 std::cout << " 1 - multicast subscribe presence\n\n";
120 // 0 - unicast subcribe presence
121 // 1 - multicast subscribe presence
122 int isMulticastAddress = 0;
124 int main(int argc, char* argv[1])
130 isMulticastAddress = 0;
134 int value = atoi(argv[1]);
136 isMulticastAddress = 1;
138 isMulticastAddress = 0;
145 // Create PlatformConfig object
147 OC::ServiceType::InProc,
148 OC::ModeType::Client,
151 OC::QualityOfService::LowQos
156 OCPlatform::Configure(cfg);
158 if(isMulticastAddress)
160 OCPlatform::OCPresenceHandle presenceHandle;
161 OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, presenceHandler);
162 std::cout<< "subscribing for multicast presence... " <<std::endl;
166 // Find all resources
167 OCPlatform::findResource("", "coap://224.0.1.187/oc/core", &foundResource);
168 std::cout<< "Finding Resource... " <<std::endl;
176 }catch(OCException& e)