Modifying version number for building on tizen 3.0
[platform/upstream/iotivity.git] / resource / 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 <mutex>
27 #include <condition_variable>
28
29 #include "OCPlatform.h"
30 #include "OCApi.h"
31
32 using namespace OC;
33
34 std::shared_ptr<OCResource> curResource;
35
36 static int TEST_CASE = 0;
37
38 /**
39  * List of methods that can be inititated from the client
40  */
41 typedef enum {
42     TEST_UNICAST_PRESENCE_NORMAL = 1,
43     TEST_UNICAST_PRESENCE_WITH_FILTER,
44     TEST_UNICAST_PRESENCE_WITH_FILTERS,
45     TEST_MULTICAST_PRESENCE_NORMAL,
46     TEST_MULTICAST_PRESENCE_WITH_FILTER,
47     TEST_MULTICAST_PRESENCE_WITH_FILTERS,
48     MAX_TESTS
49 } CLIENT_TEST;
50
51 void printUsage()
52 {
53     std::cout << "Usage : presenceclient -t <1|2>" << std::endl;
54     std::cout << "-t 1 : Discover Resources and Initiate Unicast Presence" << std::endl;
55     std::cout << "-t 2 : Discover Resources and Initiate Unicast Presence with Filter"
56               << std::endl;
57     std::cout << "-t 3 : Discover Resources and Initiate Unicast Presence with Two Filters"
58             << std::endl;
59     std::cout << "-t 4 : Discover Resources and Initiate Multicast Presence" << std::endl;
60     std::cout << "-t 5 : Discover Resources and Initiate Multicast Presence with Filter"
61               << std::endl;
62     std::cout << "-t 6 : Discover Resources and Initiate Multicast Presence with two Filters"
63                   << std::endl;
64 }
65
66 // Callback to presence
67 void presenceHandler(OCStackResult result, const unsigned int nonce, const std::string& hostAddress)
68 {
69     std::cout << "Received presence notification from : " << hostAddress << std::endl;
70     std::cout << "In presenceHandler: ";
71
72     switch(result)
73     {
74         case OC_STACK_OK:
75             std::cout << "Nonce# " << nonce << std::endl;
76             break;
77         case OC_STACK_PRESENCE_STOPPED:
78             std::cout << "Presence Stopped\n";
79             break;
80         case OC_STACK_PRESENCE_TIMEOUT:
81             std::cout << "Presence Timeout\n";
82             break;
83         case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
84             std::cout << "Virtual do not handle\n";
85             break;
86         default:
87             std::cout << "Error\n";
88             break;
89     }
90 }
91
92 // Callback to found resources
93 void foundResource(std::shared_ptr<OCResource> resource)
94 {
95     if(curResource)
96     {
97         std::cout << "Found another resource, ignoring"<<std::endl;
98     }
99
100     std::string resourceURI;
101     std::string hostAddress;
102     try
103     {
104         // Do some operations with resource object.
105         if(resource)
106         {
107             std::cout<<"DISCOVERED Resource:"<<std::endl;
108             // Get the resource URI
109             resourceURI = resource->uri();
110             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
111
112             // Get the resource host address
113             hostAddress = resource->host();
114             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
115
116             // Get the resource types
117             std::cout << "\tList of resource types: " << std::endl;
118             for(auto &resourceTypes : resource->getResourceTypes())
119             {
120                 std::cout << "\t\t" << resourceTypes << std::endl;
121             }
122
123             // Get the resource interfaces
124             std::cout << "\tList of resource interfaces: " << std::endl;
125             for(auto &resourceInterfaces : resource->getResourceInterfaces())
126             {
127                 std::cout << "\t\t" << resourceInterfaces << std::endl;
128             }
129
130             if(resourceURI == "/a/light")
131             {
132                 OCStackResult result = OC_STACK_OK;
133                 curResource = resource;
134                 OCPlatform::OCPresenceHandle presenceHandle = nullptr;
135
136                 if(TEST_CASE == TEST_UNICAST_PRESENCE_NORMAL)
137                 {
138                     result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
139                             &presenceHandler);
140                     if(result == OC_STACK_OK)
141                     {
142                         std::cout<< "Subscribed to unicast address: " << hostAddress << std::endl;
143                     }
144                     else
145                     {
146                         std::cout<< "Failed to subscribe to unicast address:" << hostAddress
147                                 << std::endl;
148                     }
149                 }
150                 if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTER ||
151                         TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS)
152                 {
153                     result = OCPlatform::subscribePresence(presenceHandle, hostAddress,
154                             "core.light", &presenceHandler);
155                     if(result == OC_STACK_OK)
156                     {
157                         std::cout<< "Subscribed to unicast address: " << hostAddress;
158                     }
159                     else
160                     {
161                         std::cout<< "Failed to subscribe to unicast address: " << hostAddress;
162                     }
163                     std::cout << " with resource type \"core.light\"." << std::endl;
164                 }
165                 if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS)
166                 {
167                     result = OCPlatform::subscribePresence(presenceHandle, hostAddress, "core.fan",
168                             &presenceHandler);
169                     if(result == OC_STACK_OK)
170                     {
171                         std::cout<< "Subscribed to unicast address: " << hostAddress;
172                     }
173                     else
174                     {
175                         std::cout<< "Failed to subscribe to unicast address: " << hostAddress;
176                     }
177                     std::cout << " with resource type \"core.fan\"." << std::endl;
178                 }
179             }
180         }
181         else
182         {
183             // Resource is invalid
184             std::cout << "Resource is invalid" << std::endl;
185         }
186
187     }
188     catch(std::exception& e)
189     {
190         //log(e.what());
191     }
192 }
193
194 int main(int argc, char* argv[]) {
195     int opt;
196
197     while ((opt = getopt(argc, argv, "t:")) != -1)
198     {
199         switch(opt)
200         {
201             case 't':
202                 TEST_CASE = atoi(optarg);
203                 break;
204             default:
205                 printUsage();
206                 return -1;
207         }
208     }
209     if(TEST_CASE >= MAX_TESTS || TEST_CASE <= 0)
210     {
211         printUsage();
212         return -1;
213     }
214
215     // Create PlatformConfig object
216     PlatformConfig cfg {
217         OC::ServiceType::InProc,
218         OC::ModeType::Client,
219         "0.0.0.0",
220         0,
221         OC::QualityOfService::LowQos
222     };
223
224     OCPlatform::Configure(cfg);
225
226     try
227     {
228         std::cout << "Created Platform..."<<std::endl;
229
230         OCPlatform::OCPresenceHandle presenceHandle = nullptr;
231         OCStackResult result = OC_STACK_OK;
232
233         if(TEST_CASE == TEST_MULTICAST_PRESENCE_NORMAL)
234         {
235             result = OCPlatform::subscribePresence(presenceHandle,
236                     OC_MULTICAST_IP, presenceHandler);
237             if(result == OC_STACK_OK)
238             {
239                 std::cout << "Subscribed to multicast presence." << std::endl;
240             }
241             else
242             {
243                 std::cout << "Failed to subscribe to multicast presence." << std::endl;
244             }
245         }
246         else if(TEST_CASE == TEST_MULTICAST_PRESENCE_WITH_FILTER)
247         {
248             result = OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, "core.light",
249                     &presenceHandler);
250             if(result == OC_STACK_OK)
251             {
252                 std::cout << "Subscribed to multicast presence with resource type";
253             }
254             else
255             {
256                 std::cout << "Failed to subscribe to multicast presence with resource type";
257             }
258             std::cout << "\"core.light\"." << std::endl;
259         }
260         else if(TEST_CASE == TEST_MULTICAST_PRESENCE_WITH_FILTERS)
261         {
262             result = OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, "core.light",
263                     &presenceHandler);
264             if(result == OC_STACK_OK)
265             {
266                 std::cout << "Subscribed to multicast presence with resource type";
267             }
268             else
269             {
270                 std::cout << "Failed to subscribe to multicast presence with resource type";
271             }
272             std::cout << "\"core.light\"." << std::endl;
273
274             result = OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, "core.fan",
275                     &presenceHandler);
276             if(result == OC_STACK_OK)
277             {
278                 std::cout<< "Subscribed to multicast presence with resource type";
279             }
280             else
281             {
282                 std::cout << "Failed to subscribe to multicast presence with resource type.";
283             }
284             std::cout << "\"core.fan\"." << std::endl;
285         }
286         else
287         {
288             // Find all resources
289             result = OCPlatform::findResource("", "coap://224.0.1.187/oc/core", &foundResource);
290             if(result == OC_STACK_OK)
291             {
292                 std::cout << "Finding Resource... " << std::endl;
293             }
294             else
295             {
296                 std::cout << "Failed to request to find resource(s)." << std::endl;
297             }
298         }
299         //
300         // A condition variable will free the mutex it is given, then do a non-
301         // intensive block until 'notify' is called on it.  In this case, since we
302         // don't ever call cv.notify, this should be a non-processor intensive version
303         // of while(true);
304         std::mutex blocker;
305         std::condition_variable cv;
306         std::unique_lock<std::mutex> lock(blocker);
307         cv.wait(lock);
308
309     }catch(OCException& e)
310     {
311         //log(e.what());
312     }
313
314     return 0;
315 }
316