Imported Upstream version 0.9.2
[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, const OCRepresentation& rep, const int eCode)
109 {
110     printf("\nonPut\n");
111 }
112
113 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
114 {
115     printf("\nonPost\n");
116
117     std::vector< OCRepresentation > children = rep.getChildren();
118
119     cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
120     for (auto iter = children.begin(); iter != children.end(); ++iter)
121     {
122         std::string power;
123         (*iter).getValue("power", power);
124
125         cout << "\tURI :: " << (*iter).getUri() << endl;
126         cout << "\t\tpower :: " << power << endl;
127     }
128
129     if (rep.hasAttribute("ActionSet"))
130     {
131         std::string plainText;
132
133         rep.getValue("ActionSet", plainText);
134
135         printf("\tPlain Text :: %s\n", plainText.c_str());
136     }
137     else
138     {
139         printf("Not found ActionSet\n");
140     }
141 }
142
143 string buildActionSetDesc(unsigned int delay = 0, unsigned int type = 0)
144 {
145     string actionsetDesc = "";
146     actionsetDesc = "allbulboff";
147     actionsetDesc.append("*");
148     actionsetDesc.append(std::to_string(delay));        // Set delay time.
149     actionsetDesc.append(" ");
150     actionsetDesc.append(std::to_string(type));         // Set action type.
151     actionsetDesc.append("*");
152     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
153     {
154         actionsetDesc.append("uri=").append((*iter));
155         actionsetDesc.append("|");
156         actionsetDesc.append("power=");
157         actionsetDesc.append("off");
158         if ((iter + 1) != lights.end())
159         {
160             actionsetDesc.append("*");
161         }
162     }
163     return actionsetDesc;
164 }
165
166 bool isResourceReady()
167 {
168     return isReady;
169 }
170
171 int main(int argc, char* argv[])
172 {
173     ostringstream requestURI;
174     requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=a.collection";
175
176     PlatformConfig config
177     { OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos };
178
179     bool isRun = true;
180
181     try
182     {
183         OCPlatform::Configure(config);
184
185         string resourceTypeName = "a.collection";
186
187         OCPlatform::findResource("", requestURI.str(),
188                                  CT_DEFAULT, &foundResource);
189
190         //Non-intensive block until foundResource callback is called by OCPlatform
191         //and onGet gets resource.
192         //isResourceReady takes care of spurious wake-up
193
194         std::unique_lock<std::mutex> lock(blocker);
195         cv.wait(lock, isResourceReady);
196
197         isReady = false;
198         while (isRun)
199         {
200             int selectedMenu;
201
202             cout << endl <<  "0 :: Quit 1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET \n";
203             cout << "3 :: GET ACTIONSET 4 :: DELETE ACTIONSET \n" << endl;
204
205             cin >> selectedMenu;
206             OCRepresentation rep;
207             string actionsetDesc;
208
209             switch(selectedMenu)
210             {
211                 case 0:
212                     isRun = false;
213                     break;
214                 case 1:
215                     actionsetDesc = buildActionSetDesc();
216                     if(!actionsetDesc.empty())
217                     {
218                         cout << "ActionSet :: " << actionsetDesc << endl;
219                         rep.setValue("ActionSet", actionsetDesc);
220                     }
221                     if (g_resource)
222                     {
223                         g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
224                         &onPut);
225                     }
226                     break;
227                 case 2:
228                     rep.setValue(DO_ACTION, std::string("allbulboff"));
229                     if (g_resource)
230                     {
231                         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
232                                          &onPost);
233                      }
234                      break;
235                 case 3:
236                     rep.setValue(GET_ACTIONSET, std::string("allbulboff"));
237                     if (g_resource)
238                     {
239                         g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
240                                 &onPost);
241                     }
242                     break;
243                 case 4:
244                     rep.setValue("DelActionSet", std::string("allbulboff"));
245                     if (g_resource)
246                     {
247                         g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
248                                 &onPut);
249                     }
250                     break;
251                 default:
252                     cout << "Invalid option" << endl;
253                     break;
254             }
255             fflush(stdin);
256         }
257     }
258     catch (OCException& e)
259     {
260         cout << e.what() << endl;
261     }
262     return 0;
263 }
264