New APIs : hasAttribute, numberOfAttributes, erase in OCRepresentation.
[platform/upstream/iotivity.git] / examples / presenceclient.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 // PresenceClient.cpp : A client example for presence notification
22 //
23 #include <string>
24 #include <cstdlib>
25 #include <pthread.h>
26 #include "OCPlatform.h"
27 #include "OCApi.h"
28
29 using namespace OC;
30
31 std::shared_ptr<OCResource> curResource;
32
33 OCPlatform* platformPtr;
34
35 // Callback to presence
36 void presenceHandler(OCStackResult result, const unsigned int nonce)
37 {
38     switch(result)
39     {
40         case OC_STACK_OK:
41             std::cout << "Nonce# " << nonce << std::endl;
42             break;
43         case OC_STACK_PRESENCE_STOPPED:
44             std::cout << "Presence Stopped\n";
45             break;
46         case OC_STACK_PRESENCE_DO_NOT_HANDLE:
47             std::cout << "Presence do not handle\n";
48             break;
49         default:
50             std::cout << "Error\n";
51             break;
52     }
53 }
54
55 // Callback to found resources
56 void foundResource(std::shared_ptr<OCResource> resource)
57 {
58     if(curResource)
59     {
60         std::cout << "Found another resource, ignoring"<<std::endl;
61     }
62
63     std::string resourceURI;
64     std::string hostAddress;
65     try
66     {
67         // Do some operations with resource object.
68         if(resource)
69         {
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;
74
75             // Get the resource host address
76             hostAddress = resource->host();
77             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
78
79             // Get the resource types
80             std::cout << "\tList of resource types: " << std::endl;
81             for(auto &resourceTypes : resource->getResourceTypes())
82             {
83                 std::cout << "\t\t" << resourceTypes << std::endl;
84             }
85
86             // Get the resource interfaces
87             std::cout << "\tList of resource interfaces: " << std::endl;
88             for(auto &resourceInterfaces : resource->getResourceInterfaces())
89             {
90                 std::cout << "\t\t" << resourceInterfaces << std::endl;
91             }
92
93             if(resourceURI == "/a/light")
94             {
95                 curResource = resource;
96                 OCPlatform::OCPresenceHandle presenceHandle;
97                 platformPtr->subscribePresence(presenceHandle, hostAddress, &presenceHandler);
98             }
99         }
100         else
101         {
102             // Resource is invalid
103             std::cout << "Resource is invalid" << std::endl;
104         }
105
106     }
107     catch(std::exception& e)
108     {
109         //log(e.what());
110     }
111 }
112
113 int main(int argc, char* argv[]) {
114
115     // Create PlatformConfig object
116     PlatformConfig cfg {
117         OC::ServiceType::InProc,
118         OC::ModeType::Client,
119         "0.0.0.0",
120         0,
121         OC::QualityOfService::NonConfirmable
122     };
123
124     // Create a OCPlatform instance.
125     // Note: Platform creation is synchronous call.
126
127     try
128     {
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;
136         while(true)
137         {
138             // some operations
139         }
140
141     }catch(OCException& e)
142     {
143         //log(e.what());
144     }
145
146     return 0;
147 }
148