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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #include "OCPlatform.h"
26 #include "ThingsManager.h"
31 static ThingsManager* gThingManager = NULL;
33 static OCResourceHandle gPhoneResourceHandle = NULL;
35 static std::shared_ptr< OCResource > gFindGroup = NULL;
37 static std::string collectionResourceType = "core.group";
39 static ActionSet* gPlayStart;
41 static ActionSet* gPlayStop;
43 void onFindGroup(std::shared_ptr< OCResource > resource)
47 if (NULL == gPhoneResourceHandle)
50 << "onFindGroup : Error! No resource to join group. Register resource first!"
57 cout << "onFindGroup : Found group is already saved." << endl;
61 cout << "onFindGroup : Found group is saved now." << endl;
62 gFindGroup = resource;
65 gThingManager->joinGroup(gFindGroup, gPhoneResourceHandle);
69 cout << "onFindGroup : Resource is invalid. So a new Group Resource has to be created."
74 void onAction(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
76 if (eCode == OC_STACK_OK)
78 cout << "onAction" << endl;
82 cout << "onAction : error - " << eCode << endl;
86 void onGetChild(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
88 if (eCode == OC_STACK_OK)
90 gPlayStart = new ActionSet();
91 gPlayStop = new ActionSet();
93 gPlayStart->actionsetName = "playstart";
94 gPlayStop->actionsetName = "playstop";
96 std::vector< OCRepresentation > childList = rep.getChildren();
97 OCRepresentation child;
98 std::string resourceURI;
100 QueryParamsMap query;
101 OCRepresentation rep;
102 std::string playStart;
103 std::string playStop;
105 std::vector< std::string > types;
108 OCStackResult result;
110 for (unsigned int i = 0; i < childList.size(); ++i)
112 // cout << "\n\tchild resource - " << i + 1 << endl;
114 child = childList.at(i);
115 resourceURI = child.getUri();
117 // cout << "\t\tURI of the resource: " << resourceURI << endl;
119 // cout << "\t\tList of resource types: " << endl;
121 types = child.getResourceTypes();
123 for (unsigned int j = 0; j < types.size(); ++j)
125 resType = types.at(j);
126 // cout << "\t\t\t" << resType << endl;
128 if (std::string::npos != resType.find("musicplayer"))
130 Capability *pStartCapa = new Capability;
131 pStartCapa->capability = "play";
132 pStartCapa->status = "on";
134 Action* pPlayStart = new Action();
135 pPlayStart->target = resourceURI;
136 pPlayStart->listOfCapability.push_back(pStartCapa);
138 gPlayStart->listOfAction.push_back(pPlayStart);
140 Capability *pStopCapa = new Capability;
141 pStopCapa->capability = "play";
142 pStopCapa->status = "off";
144 Action* pPlayStop = new Action();
145 pPlayStop->target = resourceURI;
146 pPlayStop->listOfCapability.push_back(pStopCapa);
148 gPlayStop->listOfAction.push_back(pPlayStop);
150 else if (std::string::npos != resType.find("speaker"))
152 Capability *pStartCapa = new Capability;
153 pStartCapa->capability = "volume";
154 pStartCapa->status = "50";
156 Action* pPlayStart = new Action();
157 pPlayStart->target = resourceURI;
158 pPlayStart->listOfCapability.push_back(pStartCapa);
160 gPlayStart->listOfAction.push_back(pPlayStart);
162 Capability *pStopCapa = new Capability;
163 pStopCapa->capability = "volume";
164 pStopCapa->status = "0";
166 Action* pPlayStop = new Action();
167 pPlayStop->target = resourceURI;
168 pPlayStop->listOfCapability.push_back(pStopCapa);
170 gPlayStop->listOfAction.push_back(pPlayStop);
175 // std::string temp = gThingManager->getStringFromActionSet (gPlayStart);
176 // cout << "play start - " << temp << endl;
178 // temp = gThingManager->getStringFromActionSet (gPlayStop);
179 // cout << "play stop - " << temp << endl;
181 if (0 == gPlayStart->listOfAction.empty())
183 result = gThingManager->addActionSet(gFindGroup, gPlayStart, onAction);
184 if (OC_STACK_OK == result)
186 cout << "addActionSet(gPlayStart) was successful\n";
190 cout << "addActionSet(gPlayStart) was unsuccessful. result = " << result << endl;
194 if (0 == gPlayStop->listOfAction.empty())
196 result = gThingManager->addActionSet(gFindGroup, gPlayStop, onAction);
197 if (OC_STACK_OK == result)
199 cout << "addActionSet(gPlayStop) was successful\n";
203 cout << "addActionSet(gPlayStop) was unsuccessful. result = " << result << endl;
209 cout << "onGetChild : error - " << eCode << endl;
213 int main(int argc, char* argv[])
215 // Create PlatformConfig object
217 { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
219 OCPlatform::Configure(cfg);
220 gThingManager = new ThingsManager();
223 OCStackResult result;
230 cout << "(1) CREATE PHONE" << endl;
232 << "(11) FIND & JOIN GROUP | (12) ADD GROUP ACTION | (13) PLAY START"
233 << " | (14) PLAY STOP"<< endl;
234 cout << "(15) DELETE GROUP ACTION | (16) LEAVE GROUP" << endl;
235 cout << "(21) DELETE PHONE" << endl;
237 std::cin >> selectedMenu;
239 if (selectedMenu == 1)
241 if (gPhoneResourceHandle)
243 cout << "Phone resource is registered already." << endl;
247 std::string resourceURi = "/core/phone";
248 std::string resourceTypeName = "core.phone";
249 std::string resourceInterface = DEFAULT_INTERFACE;
251 result = OCPlatform::registerResource(gPhoneResourceHandle, resourceURi,
252 resourceTypeName, resourceInterface, NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
253 if (OC_STACK_OK == result)
255 cout << "To register phone resource was successful\n";
259 cout << "To register phone resource was unsuccessful. result = " << result
263 else if (selectedMenu == 11)
265 std::vector< std::string > types;
267 types.push_back(collectionResourceType);
269 result = gThingManager->findGroup(types, &onFindGroup);
270 if (OC_STACK_OK == result)
272 cout << "Finding group was successful\n";
276 cout << "Finding group was unsuccessful. result = " << result << endl;
279 else if (selectedMenu == 12)
283 cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
287 QueryParamsMap queryParamsMap;
288 result = gFindGroup->get("", DEFAULT_INTERFACE, queryParamsMap, onGetChild);
289 if (OC_STACK_OK == result)
291 cout << "gFindGroup->get was successful\n";
295 cout << "gFindGroup->get was unsuccessful. result = " << result << endl;
298 else if (selectedMenu == 13)
302 cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
308 cout << "gPlayStart is NULL. Please process step 12 first." << endl;
312 result = gThingManager->executeActionSet(gFindGroup, "playstart", onAction);
313 if (OC_STACK_OK == result)
315 cout << "DoAction(playstart) was successful\n";
319 cout << "DoAction(playstart) was unsuccessful. result = " << result << endl;
322 else if (selectedMenu == 14)
326 cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
332 cout << "gPlayStop is NULL. Please process step 12 first." << endl;
336 result = gThingManager->executeActionSet(gFindGroup, "playstop", onAction);
337 if (OC_STACK_OK == result)
339 cout << "DoAction(playstop) was successful\n";
343 cout << "DoAction(playstop) was unsuccessful. result = " << result << endl;
346 else if (selectedMenu == 15)
350 cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
356 cout << "gPlayStart is NULL. Please process step 12 first." << endl;
362 cout << "gPlayStop is NULL. Please process step 12 first." << endl;
366 result = gThingManager->deleteActionSet(gFindGroup, "playstart", onAction);
367 if (OC_STACK_OK == result)
369 cout << "Delete Action(playstart) was successful\n";
373 cout << "Delete Action(playstart) was unsuccessful. result = " << result
377 result = gThingManager->deleteActionSet(gFindGroup, "playstop", onAction);
378 if (OC_STACK_OK == result)
380 cout << "Delete Action(playstop) was successful\n";
384 cout << "Delete Action(playstop) was unsuccessful. result = " << result << endl;
390 for (auto actionIter = gPlayStart->listOfAction.begin();
391 actionIter != gPlayStart->listOfAction.end(); ++actionIter)
395 for (auto capaIter = a->listOfCapability.begin();
396 capaIter != a->listOfCapability.end(); ++capaIter)
408 for (auto actionIter = gPlayStop->listOfAction.begin();
409 actionIter != gPlayStop->listOfAction.end(); ++actionIter)
413 for (auto capaIter = a->listOfCapability.begin();
414 capaIter != a->listOfCapability.end(); ++capaIter)
426 else if (selectedMenu == 16)
428 if (NULL == gPhoneResourceHandle)
431 << "Error! No resource to leave group. Register resource first!"
436 result = gThingManager->leaveGroup(gFindGroup, collectionResourceType, gPhoneResourceHandle);
437 if (OC_STACK_OK == result)
439 cout << "Leaving group was successful\n";
443 cout << "Leaving group was unsuccessful. result = " << result << endl;
446 else if (selectedMenu == 21)
448 if (NULL == gPhoneResourceHandle)
451 << "Error! No resource to unregister. Register resource first!"
456 result = OCPlatform::unregisterResource(gPhoneResourceHandle);
457 if (OC_STACK_OK == result)
459 cout << "To unregister phone resource was successful\n";
463 cout << "To unregister phone resource was unsuccessful. result = " << result
466 gPhoneResourceHandle = NULL;
471 catch (OCException& e)