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