1 //******************************************************************
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "OCPlatform.h"
28 #include <condition_variable>
33 namespace PH = std::placeholders;
35 OCResourceHandle resourceHandle;
37 shared_ptr< OCResource > g_resource;
38 vector< string > lights;
41 std::condition_variable cv;
45 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode);
47 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
49 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
51 void foundResource(std::shared_ptr< OCResource > resource)
53 std::string resourceURI;
54 std::string hostAddress;
58 cout << "FOUND Resource" << endl;
62 string resourceURI = resource->uri();
63 cout << resourceURI << endl;
64 if (resourceURI == "/core/a/collection")
66 g_resource = resource;
69 g_resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
70 printf("HOST :: %s\n", resource->host().c_str());
73 catch (std::exception& e)
75 std::cout << "" << std::endl;
79 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode)
83 std::vector< OCRepresentation > children = rep.getChildren();
85 cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
86 for (auto iter = children.begin(); iter != children.end(); ++iter)
88 lights.push_back((*iter).getUri());
89 cout << "\tURI :: " << (*iter).getUri() << endl;
96 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
101 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
103 printf("\nonPost\n");
105 std::vector< OCRepresentation > children = rep.getChildren();
107 cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
108 for (auto iter = children.begin(); iter != children.end(); ++iter)
111 (*iter).getValue("power", power);
113 cout << "\tURI :: " << (*iter).getUri() << endl;
114 cout << "\t\tpower :: " << power << endl;
117 if (rep.hasAttribute("ActionSet"))
119 std::string plainText;
121 rep.getValue("ActionSet", plainText);
123 printf("\tPlain Text :: %s\n", plainText.c_str());
127 printf("Not found ActionSet\n");
131 string buildActionSetDesc()
133 string actionsetDesc = "";
134 actionsetDesc = "allbulboff";
135 actionsetDesc.append("*");
136 for (auto iter = lights.begin(); iter != lights.end(); ++iter)
138 actionsetDesc.append("uri=").append((*iter));
139 actionsetDesc.append("|");
140 actionsetDesc.append("power=");
141 actionsetDesc.append("off");
142 if ((iter + 1) != lights.end())
144 actionsetDesc.append("*");
147 return actionsetDesc;
150 bool isResourceReady()
155 int main(int argc, char* argv[])
157 ostringstream requestURI;
158 requestURI << OC_WELL_KNOWN_QUERY << "?rt=a.collection";
161 OCConnectivityType connectivityType = OC_WIFI;
167 std::size_t inputValLen;
168 int optionSelected = stoi(argv[1], &inputValLen);
170 if(inputValLen == strlen(argv[1]))
172 if(optionSelected == 0)
174 connectivityType = OC_ETHERNET;
176 else if(optionSelected == 1)
178 connectivityType = OC_WIFI;
182 std::cout << "Invalid connectivity type selected. Using default WIFI"
188 std::cout << "Invalid connectivity type selected. Using default WIFI" << std::endl;
193 std::cout << "Invalid input argument. Using WIFI as connectivity type" << std::endl;
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";
205 PlatformConfig config
206 { OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos };
212 OCPlatform::Configure(config);
214 string resourceTypeName = "a.collection";
216 OCPlatform::findResource("", requestURI.str(),
217 connectivityType, &foundResource);
219 OCPlatform::findResource("", requestURI.str(), &foundResource);
222 //Non-intensive block until foundResource callback is called by OCPlatform
223 //and onGet gets resource.
224 //isResourceReady takes care of spurious wake-up
226 std::unique_lock<std::mutex> lock(blocker);
227 cv.wait(lock, isResourceReady);
234 cout << endl << "0 :: Quit 1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET \n";
235 cout << "3 :: GET ACTIONSET 4 :: DELETE ACTIONSET \n" << endl;
238 OCRepresentation rep;
239 string actionsetDesc;
247 actionsetDesc = buildActionSetDesc();
248 if(!actionsetDesc.empty())
250 cout << "ActionSet :: " << actionsetDesc << endl;
251 rep.setValue("ActionSet", actionsetDesc);
255 g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
260 rep.setValue("DoAction", std::string("allbulboff"));
263 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
268 rep.setValue("GetActionSet", std::string("allbulboff"));
271 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
276 rep.setValue("DelActionSet", std::string("allbulboff"));
279 g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
284 cout << "Invalid option" << endl;
290 catch (OCException& e)
292 cout << e.what() << endl;