Resolve Klocwork c++ issues
[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 #ifdef CA_INT
161     OCConnectivityType connectivityType = OC_WIFI;
162
163     if(argc == 2)
164     {
165         try
166         {
167             std::size_t inputValLen;
168             int optionSelected = stoi(argv[1], &inputValLen);
169
170             if(inputValLen == strlen(argv[1]))
171             {
172                 if(optionSelected == 0)
173                 {
174                     connectivityType = OC_ETHERNET;
175                 }
176                 else if(optionSelected == 1)
177                 {
178                     connectivityType = OC_WIFI;
179                 }
180                 else
181                 {
182                     std::cout << "Invalid connectivity type selected. Using default WIFI"
183                         << std::endl;
184                 }
185             }
186             else
187             {
188                 std::cout << "Invalid connectivity type selected. Using default WIFI" << std::endl;
189             }
190         }
191         catch(exception& e)
192         {
193             std::cout << "Invalid input argument. Using WIFI as connectivity type" << std::endl;
194         }
195     }
196     else
197     {
198         std::cout<<"Usage: groupclient <ConnectivityType(0|1)>\n";
199         std::cout<<"ConnectivityType: Default WIFI\n";
200         std::cout<<"ConnectivityType 0: ETHERNET\n";
201         std::cout<<"ConnectivityType 1: WIFI\n";
202     }
203 #endif
204
205     PlatformConfig config
206     { OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos };
207
208     bool isRun = true;
209
210     try
211     {
212         OCPlatform::Configure(config);
213
214         string resourceTypeName = "a.collection";
215 #ifdef CA_INT
216         OCPlatform::findResource("", requestURI.str(),
217                                  connectivityType, &foundResource);
218 #else
219         OCPlatform::findResource("", requestURI.str(), &foundResource);
220 #endif
221
222         //Non-intensive block until foundResource callback is called by OCPlatform
223         //and onGet gets resource.
224         //isResourceReady takes care of spurious wake-up
225
226         std::unique_lock<std::mutex> lock(blocker);
227         cv.wait(lock, isResourceReady);
228
229         isReady = false;
230         while (isRun)
231         {
232             int selectedMenu;
233
234             cout << endl <<  "0 :: Quit 1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET \n";
235             cout << "3 :: GET ACTIONSET 4 :: DELETE ACTIONSET \n" << endl;
236
237             cin >> selectedMenu;
238             OCRepresentation rep;
239             string actionsetDesc;
240
241             switch(selectedMenu)
242             {
243                 case 0:
244                     isRun = false;
245                     break;
246                 case 1:
247                     actionsetDesc = buildActionSetDesc();
248                     if(!actionsetDesc.empty())
249                     {
250                         cout << "ActionSet :: " << actionsetDesc << endl;
251                         rep.setValue("ActionSet", actionsetDesc);
252                     }
253                     if (g_resource)
254                     {
255                         g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
256                         &onPut);
257                     }
258                     break;
259                 case 2:
260                     rep.setValue("DoAction", std::string("allbulboff"));
261                     if (g_resource)
262                     {
263                         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
264                                          &onPost);
265                      }
266                      break;
267                 case 3:
268                     rep.setValue("GetActionSet", std::string("allbulboff"));
269                     if (g_resource)
270                     {
271                         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
272                                 &onPost);
273                     }
274                     break;
275                 case 4:
276                     rep.setValue("DelActionSet", std::string("allbulboff"));
277                     if (g_resource)
278                     {
279                         g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
280                                 &onPut);
281                     }
282                     break;
283                 default:
284                     cout << "Invalid option" << endl;
285                     break;
286             }
287             fflush(stdin);
288         }
289     }
290     catch (OCException& e)
291     {
292         cout << e.what() << endl;
293     }
294     return 0;
295 }