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>
34 namespace PH = std::placeholders;
35 std::mutex resourceLock;
37 OCResourceHandle resourceHandle;
38 shared_ptr< OCResource > g_resource;
39 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::lock_guard<std::mutex> lock(resourceLock);
56 std::cout << "Found another resource, ignoring"<<std::endl;
60 std::string resourceURI;
61 std::string hostAddress;
65 cout << "FOUND Resource" << endl;
69 string resourceURI = resource->uri();
70 cout << resourceURI << endl;
71 cout << "HOST :: " << resource->host() << endl;
72 if (resourceURI == "/core/a/collection")
74 g_resource = resource;
75 resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
79 catch (std::exception& e)
81 std::cout << "" << std::endl;
85 void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCode)
89 std::vector< OCRepresentation > children = rep.getChildren();
91 cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
92 for (auto iter = children.begin(); iter != children.end(); ++iter)
94 lights.push_back((*iter).getUri());
95 cout << "\tURI :: " << (*iter).getUri() << endl;
102 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
107 void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
109 printf("\nonPost\n");
111 std::vector< OCRepresentation > children = rep.getChildren();
113 cout << "\n\n\nCHILD RESOURCE OF GROUP" << endl;
114 for (auto iter = children.begin(); iter != children.end(); ++iter)
117 (*iter).getValue("power", power);
119 cout << "\tURI :: " << (*iter).getUri() << endl;
120 cout << "\t\tpower :: " << power << endl;
123 if (rep.hasAttribute("ActionSet"))
125 std::string plainText;
127 rep.getValue("ActionSet", plainText);
129 printf("\tPlain Text :: %s\n", plainText.c_str());
133 printf("Not found ActionSet\n");
137 string buildActionSetDesc()
139 string actionsetDesc = "";
140 //"movieTime*uri=coap://10.251.44.228:49858/a/light|power=10*uri=coap:
141 //10.251.44.228:49858/a/light|power=10|";
142 actionsetDesc = "allbulboff";
143 actionsetDesc.append("*");
144 for (auto iter = lights.begin(); iter != lights.end(); ++iter)
146 actionsetDesc.append("uri=").append((*iter));
147 actionsetDesc.append("|");
148 actionsetDesc.append("power=");
149 actionsetDesc.append("off");
150 if ((iter + 1) != lights.end())
152 actionsetDesc.append("*");
155 return actionsetDesc;
158 bool isResourceReady()
164 PlatformConfig config
165 { OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos };
171 OCPlatform::Configure(config);
173 string resourceTypeName = "a.collection";
174 OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=a.collection", &foundResource);
176 //Non-intensive block util foundResource callback is called by OCPlatform
177 //and onGet gets resource.
178 //isResourceReady takes care of spurious wake-up
180 std::unique_lock<std::mutex> lock(blocker);
181 cv.wait(lock, isResourceReady);
189 << "0 :: Quit 1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET \n";
190 cout << "3 :: GET ACTIONSET 4 :: DELETE ACTIONSET \n" << endl;
194 OCRepresentation rep;
195 string actionsetDesc;
202 actionsetDesc = buildActionSetDesc();
204 if(!actionsetDesc.empty())
206 cout << "ActionSet :: " << actionsetDesc << endl;
207 rep.setValue("ActionSet", actionsetDesc);
212 g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
217 rep.setValue("DoAction", std::string("allbulboff"));
220 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
225 rep.setValue("GetActionSet", std::string("allbulboff"));
228 g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
233 rep.setValue("DelActionSet", std::string("allbulboff"));
236 g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
241 cout << "Incorrect option" << endl;
248 catch (OCException& e)
250 cout << e.what() << endl;