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 OCPlatform* platformPtr;
35 // Callback to presence
36 void presenceHandler(OCStackResult result, const unsigned int nonce)
41 std::cout << "Nonce# " << nonce << std::endl;
43 case OC_STACK_PRESENCE_STOPPED:
44 std::cout << "Presence Stopped\n";
46 case OC_STACK_PRESENCE_DO_NOT_HANDLE:
47 std::cout << "Presence do not handle\n";
50 std::cout << "Error\n";
55 // Callback to found resources
56 void foundResource(std::shared_ptr<OCResource> resource)
60 std::cout << "Found another resource, ignoring"<<std::endl;
63 std::string resourceURI;
64 std::string hostAddress;
67 // Do some operations with resource object.
70 std::cout<<"DISCOVERED Resource:"<<std::endl;
71 // Get the resource URI
72 resourceURI = resource->uri();
73 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
75 // Get the resource host address
76 hostAddress = resource->host();
77 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
79 // Get the resource types
80 std::cout << "\tList of resource types: " << std::endl;
81 for(auto &resourceTypes : resource->getResourceTypes())
83 std::cout << "\t\t" << resourceTypes << std::endl;
86 // Get the resource interfaces
87 std::cout << "\tList of resource interfaces: " << std::endl;
88 for(auto &resourceInterfaces : resource->getResourceInterfaces())
90 std::cout << "\t\t" << resourceInterfaces << std::endl;
93 if(resourceURI == "/a/light")
95 curResource = resource;
96 OCPlatform::OCPresenceHandle presenceHandle;
97 platformPtr->subscribePresence(presenceHandle, hostAddress, &presenceHandler);
102 // Resource is invalid
103 std::cout << "Resource is invalid" << std::endl;
107 catch(std::exception& e)
113 int main(int argc, char* argv[]) {
115 // Create PlatformConfig object
117 OC::ServiceType::InProc,
118 OC::ModeType::Client,
121 OC::QualityOfService::NonConfirmable
124 // Create a OCPlatform instance.
125 // Note: Platform creation is synchronous call.
129 OCPlatform platform(cfg);
130 // PlatformPtr is used in another function
131 platformPtr = &platform;
132 std::cout << "Created Platform..."<<std::endl;
133 // Find all resources
134 platform.findResource("", "coap://224.0.1.187/oc/core", &foundResource);
135 std::cout<< "Finding Resource... " <<std::endl;
141 }catch(OCException& e)