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;
32 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
34 OCPlatform* platformPtr;
36 // Callback to presence
37 void presenceHandler(OCStackResult result, const unsigned int nonce)
42 std::cout << "Nonce# " << nonce << std::endl;
44 case OC_STACK_PRESENCE_STOPPED:
45 std::cout << "Presence Stopped\n";
47 case OC_STACK_PRESENCE_DO_NOT_HANDLE:
48 std::cout << "Presence do not handle\n";
51 std::cout << "Error\n";
56 // Callback to found resources
57 void foundResource(std::shared_ptr<OCResource> resource)
62 std::cout << "Found another resource, ignoring"<<std::endl;
65 std::string resourceURI;
66 std::string hostAddress;
69 // Do some operations with resource object.
72 std::cout<<"DISCOVERED Resource:"<<std::endl;
73 // Get the resource URI
74 resourceURI = resource->uri();
75 std::cout << "\tURI of the resource: " << resourceURI << std::endl;
77 // Get the resource host address
78 hostAddress = resource->host();
79 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
81 // Get the resource types
82 std::cout << "\tList of resource types: " << std::endl;
83 for(auto &resourceTypes : resource->getResourceTypes())
85 std::cout << "\t\t" << resourceTypes << std::endl;
88 // Get the resource interfaces
89 std::cout << "\tList of resource interfaces: " << std::endl;
90 for(auto &resourceInterfaces : resource->getResourceInterfaces())
92 std::cout << "\t\t" << resourceInterfaces << std::endl;
95 if(resourceURI == "/a/light")
97 curResource = resource;
98 OCPlatform::OCPresenceHandle presenceHandle;
99 platformPtr->subscribePresence(presenceHandle, hostAddress, &presenceHandler);
104 // Resource is invalid
105 std::cout << "Resource is invalid" << std::endl;
109 catch(std::exception& e)
117 std::cout << std::endl;
118 std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
119 std::cout << " ObserveType : 1 - Observe" << std::endl;
120 std::cout << " ObserveType : 2 - ObserveAll" << std::endl;
124 int main(int argc, char* argv[]) {
127 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
131 int value = atoi(argv[1]);
133 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
135 OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
137 OBSERVE_TYPE_TO_USE = ObserveType::Observe;
145 // Create PlatformConfig object
147 OC::ServiceType::InProc,
148 OC::ModeType::Client,
151 OC::QualityOfService::NonConfirmable
154 // Create a OCPlatform instance.
155 // Note: Platform creation is synchronous call.
159 OCPlatform platform(cfg);
160 // PlatformPtr is used in another function
161 platformPtr = &platform;
162 std::cout << "Created Platform..."<<std::endl;
163 // Find all resources
164 platform.findResource("", "coap://224.0.1.187/oc/core", &foundResource);
165 std::cout<< "Finding Resource... " <<std::endl;
171 }catch(OCException& e)