Changeset for reviewing RI-CA integration changes.
[platform/upstream/iotivity.git] / resource / examples / groupclient.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Samsung Electronics 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 #include "OCPlatform.h"
22 #include "OCApi.h"
23 #include "logger.h"
24
25 #include <functional>
26 #include <pthread.h>
27 #include <mutex>
28 #include <condition_variable>
29 #include <iostream>
30
31 using namespace std;
32 using namespace OC;
33 namespace PH = std::placeholders;
34
35 OCResourceHandle resourceHandle;
36
37 shared_ptr< OCResource > g_resource;
38 vector< string > lights;
39
40 std::mutex blocker;
41 std::condition_variable cv;
42
43 bool isReady = false;
44
45 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode);
46
47 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
48
49 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
50
51 void foundResource(std::shared_ptr< OCResource > resource)
52 {
53     std::string resourceURI;
54     std::string hostAddress;
55
56     try
57     {
58         cout << "FOUND Resource" << endl;
59
60         if (resource)
61         {
62             string resourceURI = resource->uri();
63             cout << resourceURI << endl;
64             if (resourceURI == "/core/a/collection")
65             {
66                 g_resource = resource;
67             }
68
69             g_resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
70             printf("HOST :: %s\n", resource->host().c_str());
71         }
72     }
73     catch (std::exception& e)
74     {
75         std::cout << "" << std::endl;
76     }
77 }
78
79 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode)
80 {
81     // printf("onGet\n");
82
83     std::vector< OCRepresentation > children = rep.getChildren();
84
85     cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
86     for (auto iter = children.begin(); iter != children.end(); ++iter)
87     {
88         lights.push_back((*iter).getUri());
89         cout << "\tURI :: " << (*iter).getUri() << endl;
90     }
91
92     isReady = true;
93     cv.notify_one();
94 }
95
96 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
97 {
98     printf("\nonPut\n");
99 }
100
101 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
102 {
103     printf("\nonPost\n");
104
105     std::vector< OCRepresentation > children = rep.getChildren();
106
107     cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
108     for (auto iter = children.begin(); iter != children.end(); ++iter)
109     {
110         std::string power;
111         (*iter).getValue("power", power);
112
113         cout << "\tURI :: " << (*iter).getUri() << endl;
114         cout << "\t\tpower :: " << power << endl;
115     }
116
117     if (rep.hasAttribute("ActionSet"))
118     {
119         std::string plainText;
120
121         rep.getValue("ActionSet", plainText);
122
123         printf("\tPlain Text :: %s\n", plainText.c_str());
124     }
125     else
126     {
127         printf("Not found ActionSet\n");
128     }
129 }
130
131 string buildActionSetDesc()
132 {
133     string actionsetDesc = "";
134     actionsetDesc = "allbulboff";
135     actionsetDesc.append("*");
136     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
137     {
138         actionsetDesc.append("uri=").append((*iter));
139         actionsetDesc.append("|");
140         actionsetDesc.append("power=");
141         actionsetDesc.append("off");
142         if ((iter + 1) != lights.end())
143         {
144             actionsetDesc.append("*");
145         }
146     }
147     return actionsetDesc;
148 }
149
150 bool isResourceReady()
151 {
152     return isReady;
153 }
154
155 int main(int argc, char* argv[])
156 {
157     ostringstream requestURI;
158     requestURI << OC_WELL_KNOWN_QUERY << "?rt=a.collection";
159
160     OCConnectivityType connectivityType = OC_WIFI;
161
162     if(argc == 2)
163     {
164         try
165         {
166             std::size_t inputValLen;
167             int optionSelected = stoi(argv[1], &inputValLen);
168
169             if(inputValLen == strlen(argv[1]))
170             {
171                 if(optionSelected == 0)
172                 {
173                     connectivityType = OC_ETHERNET;
174                 }
175                 else if(optionSelected == 1)
176                 {
177                     connectivityType = OC_WIFI;
178                 }
179                 else
180                 {
181                     std::cout << "Invalid connectivity type selected. Using default WIFI"
182                         << std::endl;
183                 }
184             }
185             else
186             {
187                 std::cout << "Invalid connectivity type selected. Using default WIFI" << std::endl;
188             }
189         }
190         catch(exception& e)
191         {
192             std::cout << "Invalid input argument. Using WIFI as connectivity type" << std::endl;
193         }
194     }
195     else
196     {
197         std::cout<<"Usage: groupclient <ConnectivityType(0|1)>\n";
198         std::cout<<"ConnectivityType: Default WIFI\n";
199         std::cout<<"ConnectivityType 0: ETHERNET\n";
200         std::cout<<"ConnectivityType 1: WIFI\n";
201     }
202
203     PlatformConfig config
204     { OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos };
205
206     bool isRun = true;
207
208     try
209     {
210         OCPlatform::Configure(config);
211
212         string resourceTypeName = "a.collection";
213
214         OCPlatform::findResource("", requestURI.str(),
215                                  connectivityType, &foundResource);
216
217         //Non-intensive block until foundResource callback is called by OCPlatform
218         //and onGet gets resource.
219         //isResourceReady takes care of spurious wake-up
220
221         std::unique_lock<std::mutex> lock(blocker);
222         cv.wait(lock, isResourceReady);
223
224         isReady = false;
225         while (isRun)
226         {
227             int selectedMenu;
228
229             cout << endl <<  "0 :: Quit 1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET \n";
230             cout << "3 :: GET ACTIONSET 4 :: DELETE ACTIONSET \n" << endl;
231
232             cin >> selectedMenu;
233             OCRepresentation rep;
234             string actionsetDesc;
235
236             switch(selectedMenu)
237             {
238                 case 0:
239                     isRun = false;
240                     break;
241                 case 1:
242                     actionsetDesc = buildActionSetDesc();
243                     if(!actionsetDesc.empty())
244                     {
245                         cout << "ActionSet :: " << actionsetDesc << endl;
246                         rep.setValue("ActionSet", actionsetDesc);
247                     }
248                     if (g_resource)
249                     {
250                         g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
251                         &onPut);
252                     }
253                     break;
254                 case 2:
255                     rep.setValue("DoAction", std::string("allbulboff"));
256                     if (g_resource)
257                     {
258                         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
259                                          &onPost);
260                      }
261                      break;
262                 case 3:
263                     rep.setValue("GetActionSet", std::string("allbulboff"));
264                     if (g_resource)
265                     {
266                         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
267                                 &onPost);
268                     }
269                     break;
270                 case 4:
271                     rep.setValue("DelActionSet", std::string("allbulboff"));
272                     if (g_resource)
273                     {
274                         g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
275                                 &onPut);
276                     }
277                     break;
278                 default:
279                     cout << "Invalid option" << endl;
280                     break;
281             }
282             fflush(stdin);
283         }
284     }
285     catch (OCException& e)
286     {
287         cout << e.what() << endl;
288     }
289     return 0;
290 }
291