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