9ccc9c79f990b79f86b60887ab09925e6c8b59b5
[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 const int SUCCESS_RESPONSE = 0;
32 std::shared_ptr<OCResource> curResource;
33 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
34
35 OCPlatform* platformPtr;
36
37 // Callback to presence
38 void presenceHandler(OCStackResult result, const unsigned int nonce)
39 {
40     switch(result)
41     {
42         case OC_STACK_OK:
43             std::cout << "Nonce# " << nonce << std::endl;
44             break;
45         case OC_STACK_PRESENCE_STOPPED:
46             std::cout << "Presence Stopped\n";
47             break;
48         case OC_STACK_PRESENCE_DO_NOT_HANDLE:
49             std::cout << "Presence do not handle\n";
50             break;
51         default:
52             std::cout << "Error\n";
53             break;
54     }
55 }
56
57 // Callback to found resources
58 void foundResource(std::shared_ptr<OCResource> resource)
59 {
60
61     if(curResource)
62     {
63         std::cout << "Found another resource, ignoring"<<std::endl;
64     }
65
66     std::string resourceURI;
67     std::string hostAddress;
68     try
69     {
70         // Do some operations with resource object.
71         if(resource)
72         {
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;
77
78             // Get the resource host address
79             hostAddress = resource->host();
80             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
81
82             // Get the resource types 
83             std::cout << "\tList of resource types: " << std::endl;
84             for(auto &resourceTypes : resource->getResourceTypes())
85             {
86                 std::cout << "\t\t" << resourceTypes << std::endl;
87             }
88             
89             // Get the resource interfaces
90             std::cout << "\tList of resource interfaces: " << std::endl;
91             for(auto &resourceInterfaces : resource->getResourceInterfaces())
92             {
93                 std::cout << "\t\t" << resourceInterfaces << std::endl;
94             } 
95
96             if(resourceURI == "/a/light")
97             {
98                 curResource = resource;
99                 OCPlatform::OCPresenceHandle presenceHandle;
100                 platformPtr->subscribePresence(presenceHandle, hostAddress, &presenceHandler);
101             }
102         }
103         else
104         {
105             // Resource is invalid
106             std::cout << "Resource is invalid" << std::endl;
107         }
108
109     }
110     catch(std::exception& e)
111     {
112         //log(e.what());
113     }
114 }
115
116 void PrintUsage()
117 {
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;
122 }
123
124
125 int main(int argc, char* argv[]) {
126     if (argc == 1)
127     {
128         OBSERVE_TYPE_TO_USE = ObserveType::Observe;
129     }
130     else if (argc == 2)
131     {
132         int value = atoi(argv[1]);
133         if (value == 1)
134             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
135         else if (value == 2)
136             OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
137         else
138             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
139     }
140     else
141     {
142         PrintUsage();
143         return -1;
144     }
145
146     // Create PlatformConfig object
147     PlatformConfig cfg {
148         OC::ServiceType::InProc,
149         OC::ModeType::Client,
150         "192.168.1.10",
151         5683,
152         OC::QualityOfService::NonConfirmable
153     };
154
155     // Create a OCPlatform instance.
156     // Note: Platform creation is synchronous call.
157
158     try
159     {
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;
167         while(true)
168         {
169             // some operations
170         }
171
172     }catch(OCException& e)
173     {
174         //log(e.what());
175     }
176
177     return 0;
178 }
179