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";
160 OCConnectivityType connectivityType = OC_WIFI;
166 std::size_t inputValLen;
167 int optionSelected = stoi(argv[1], &inputValLen);
169 if(inputValLen == strlen(argv[1]))
171 if(optionSelected == 0)
173 connectivityType = OC_ETHERNET;
175 else if(optionSelected == 1)
177 connectivityType = OC_WIFI;
181 std::cout << "Invalid connectivity type selected. Using default WIFI"
187 std::cout << "Invalid connectivity type selected. Using default WIFI" << std::endl;
192 std::cout << "Invalid input argument. Using WIFI as connectivity type" << std::endl;
197 std::cout<<"Usage: groupclient <ConnectivityType(0|1)>\n";
198 std::cout<<"ConnectivityType: Default WIFI\n";
199 std::cout<<"ConnectivityType 0: ETHERNET\n";
200 std::cout<<"ConnectivityType 1: WIFI\n";
203 PlatformConfig config
204 { OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos };
210 OCPlatform::Configure(config);
212 string resourceTypeName = "a.collection";
214 OCPlatform::findResource("", requestURI.str(),
215 connectivityType, &foundResource);
217 //Non-intensive block until foundResource callback is called by OCPlatform
218 //and onGet gets resource.
219 //isResourceReady takes care of spurious wake-up
221 std::unique_lock<std::mutex> lock(blocker);
222 cv.wait(lock, isResourceReady);
229 cout << endl << "0 :: Quit 1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET \n";
230 cout << "3 :: GET ACTIONSET 4 :: DELETE ACTIONSET \n" << endl;
233 OCRepresentation rep;
234 string actionsetDesc;
242 actionsetDesc = buildActionSetDesc();
243 if(!actionsetDesc.empty())
245 cout << "ActionSet :: " << actionsetDesc << endl;
246 rep.setValue("ActionSet", actionsetDesc);
250 g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
255 rep.setValue("DoAction", std::string("allbulboff"));
258 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
263 rep.setValue("GetActionSet", std::string("allbulboff"));
266 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
271 rep.setValue("DelActionSet", std::string("allbulboff"));
274 g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
279 cout << "Invalid option" << endl;
285 catch (OCException& e)
287 cout << e.what() << endl;