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 const int SUCCESS_RESPONSE = 0;
32 std::shared_ptr<OCResource> curResource;
33 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
35 OCPlatform* platformPtr;
37 // Callback to presence
38 void presenceHandler(OCStackResult result, const unsigned int nonce)
43 std::cout << "Nonce# " << nonce << std::endl;
45 case OC_STACK_PRESENCE_STOPPED:
46 std::cout << "Presence Stopped\n";
48 case OC_STACK_PRESENCE_DO_NOT_HANDLE:
49 std::cout << "Presence do not handle\n";
52 std::cout << "Error\n";
57 // Callback to found resources
58 void foundResource(std::shared_ptr<OCResource> resource)
63 std::cout << "Found another resource, ignoring"<<std::endl;
66 std::string resourceURI;
67 std::string hostAddress;
70 // Do some operations with resource object.
73 std::cout<<"DISCOVERED Resource:"<<std::endl;
74 // Get the resource URI
75 resourceURI = resource->uri();
76 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
78 // Get the resource host address
79 hostAddress = resource->host();
80 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
82 // Get the resource types
83 std::cout << "\tList of resource types: " << std::endl;
84 for(auto &resourceTypes : resource->getResourceTypes())
86 std::cout << "\t\t" << resourceTypes << std::endl;
89 // Get the resource interfaces
90 std::cout << "\tList of resource interfaces: " << std::endl;
91 for(auto &resourceInterfaces : resource->getResourceInterfaces())
93 std::cout << "\t\t" << resourceInterfaces << std::endl;
96 if(resourceURI == "/a/light")
98 curResource = resource;
99 OCPlatform::OCPresenceHandle presenceHandle;
100 platformPtr->subscribePresence(presenceHandle, hostAddress, &presenceHandler);
105 // Resource is invalid
106 std::cout << "Resource is invalid" << std::endl;
110 catch(std::exception& e)
118 std::cout << std::endl;
119 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
120 std::cout << " ObserveType : 1 - Observe" << std::endl;
121 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
125 int main(int argc, char* argv[]) {
128 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
132 int value = atoi(argv[1]);
134 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
136 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
138 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
146 // Create PlatformConfig object
148 OC::ServiceType::InProc,
149 OC::ModeType::Client,
152 OC::QualityOfService::NonConfirmable
155 // Create a OCPlatform instance.
156 // Note: Platform creation is synchronous call.
160 OCPlatform platform(cfg);
161 // PlatformPtr is used in another function
162 platformPtr = &platform;
163 std::cout << "Created Platform..."<<std::endl;
164 // Find all resources
165 platform.findResource("", "coap://224.0.1.187/oc/core", &foundResource);
166 std::cout<< "Finding Resource... " <<std::endl;
172 }catch(OCException& e)