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