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 std::string playStart;
102 std::string playStop;
104 std::vector< std::string > types;
107 OCStackResult result;
109 for (unsigned int i = 0; i < childList.size(); ++i)
111 // cout << "\n\tchild resource - " << i + 1 << endl;
113 child = childList.at(i);
114 resourceURI = child.getUri();
116 // cout << "\t\tURI of the resource: " << resourceURI << endl;
118 // cout << "\t\tList of resource types: " << endl;
120 types = child.getResourceTypes();
122 for (unsigned int j = 0; j < types.size(); ++j)
124 resType = types.at(j);
125 // cout << "\t\t\t" << resType << endl;
127 if (std::string::npos != resType.find("musicplayer"))
129 Capability *pStartCapa = new Capability;
130 pStartCapa->capability = "play";
131 pStartCapa->status = "on";
133 Action* pPlayStart = new Action();
134 pPlayStart->target = resourceURI;
135 pPlayStart->listOfCapability.push_back(pStartCapa);
137 gPlayStart->listOfAction.push_back(pPlayStart);
139 Capability *pStopCapa = new Capability;
140 pStopCapa->capability = "play";
141 pStopCapa->status = "off";
143 Action* pPlayStop = new Action();
144 pPlayStop->target = resourceURI;
145 pPlayStop->listOfCapability.push_back(pStopCapa);
147 gPlayStop->listOfAction.push_back(pPlayStop);
149 else if (std::string::npos != resType.find("speaker"))
151 Capability *pStartCapa = new Capability;
152 pStartCapa->capability = "volume";
153 pStartCapa->status = "50";
155 Action* pPlayStart = new Action();
156 pPlayStart->target = resourceURI;
157 pPlayStart->listOfCapability.push_back(pStartCapa);
159 gPlayStart->listOfAction.push_back(pPlayStart);
161 Capability *pStopCapa = new Capability;
162 pStopCapa->capability = "volume";
163 pStopCapa->status = "0";
165 Action* pPlayStop = new Action();
166 pPlayStop->target = resourceURI;
167 pPlayStop->listOfCapability.push_back(pStopCapa);
169 gPlayStop->listOfAction.push_back(pPlayStop);
174 // std::string temp = gThingManager->getStringFromActionSet (gPlayStart);
175 // cout << "play start - " << temp << endl;
177 // temp = gThingManager->getStringFromActionSet (gPlayStop);
178 // cout << "play stop - " << temp << endl;
180 if (0 == gPlayStart->listOfAction.empty())
182 result = gThingManager->addActionSet(gFindGroup, gPlayStart, onAction);
183 if (OC_STACK_OK == result)
185 cout << "addActionSet(gPlayStart) was successful\n";
189 cout << "addActionSet(gPlayStart) was unsuccessful. result = " << result << endl;
193 if (0 == gPlayStop->listOfAction.empty())
195 result = gThingManager->addActionSet(gFindGroup, gPlayStop, onAction);
196 if (OC_STACK_OK == result)
198 cout << "addActionSet(gPlayStop) was successful\n";
202 cout << "addActionSet(gPlayStop) was unsuccessful. result = " << result << endl;
208 cout << "onGetChild : error - " << eCode << endl;
212 int main(int argc, char* argv[])
214 // Create PlatformConfig object
216 { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
218 OCPlatform::Configure(cfg);
219 gThingManager = new ThingsManager();
222 OCStackResult result;
229 cout << "(1) CREATE PHONE" << endl;
231 << "(11) FIND & JOIN GROUP | (12) ADD GROUP ACTION | (13) PLAY START"
232 << " | (14) PLAY STOP"<< endl;
233 cout << "(15) DELETE GROUP ACTION | (16) LEAVE GROUP" << endl;
234 cout << "(21) DELETE PHONE" << endl;
236 std::cin >> selectedMenu;
238 if (selectedMenu == 1)
240 if (gPhoneResourceHandle)
242 cout << "Phone resource is registered already." << endl;
246 std::string resourceURi = "/core/phone";
247 std::string resourceTypeName = "core.phone";
248 std::string resourceInterface = DEFAULT_INTERFACE;
250 result = OCPlatform::registerResource(gPhoneResourceHandle, resourceURi,
251 resourceTypeName, resourceInterface,
252 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
254 if (OC_STACK_OK == result)
256 cout << "To register phone resource was successful\n";
260 cout << "To register phone resource was unsuccessful. result = " << result
264 else if (selectedMenu == 11)
266 std::vector< std::string > types;
268 types.push_back(collectionResourceType);
270 result = gThingManager->findGroup(types, &onFindGroup);
271 if (OC_STACK_OK == result)
273 cout << "Finding group was successful\n";
277 cout << "Finding group was unsuccessful. result = " << result << endl;
280 else if (selectedMenu == 12)
284 cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
288 QueryParamsMap queryParamsMap;
289 result = gFindGroup->get("", DEFAULT_INTERFACE, queryParamsMap, onGetChild);
290 if (OC_STACK_OK == result)
292 cout << "gFindGroup->get was successful\n";
296 cout << "gFindGroup->get was unsuccessful. result = " << result << endl;
299 else if (selectedMenu == 13)
303 cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
309 cout << "gPlayStart is NULL. Please process step 12 first." << endl;
313 result = gThingManager->executeActionSet(gFindGroup, "playstart", onAction);
314 if (OC_STACK_OK == result)
316 cout << "DoAction(playstart) was successful\n";
320 cout << "DoAction(playstart) was unsuccessful. result = " << result << endl;
323 else if (selectedMenu == 14)
327 cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
333 cout << "gPlayStop is NULL. Please process step 12 first." << endl;
337 result = gThingManager->executeActionSet(gFindGroup, "playstop", onAction);
338 if (OC_STACK_OK == result)
340 cout << "DoAction(playstop) was successful\n";
344 cout << "DoAction(playstop) was unsuccessful. result = " << result << endl;
347 else if (selectedMenu == 15)
351 cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
357 cout << "gPlayStart is NULL. Please process step 12 first." << endl;
363 cout << "gPlayStop is NULL. Please process step 12 first." << endl;
367 result = gThingManager->deleteActionSet(gFindGroup, "playstart", onAction);
368 if (OC_STACK_OK == result)
370 cout << "Delete Action(playstart) was successful\n";
374 cout << "Delete Action(playstart) was unsuccessful. result = " << result
378 result = gThingManager->deleteActionSet(gFindGroup, "playstop", onAction);
379 if (OC_STACK_OK == result)
381 cout << "Delete Action(playstop) was successful\n";
385 cout << "Delete Action(playstop) was unsuccessful. result = " << result << endl;
391 for (auto actionIter = gPlayStart->listOfAction.begin();
392 actionIter != gPlayStart->listOfAction.end(); ++actionIter)
396 for (auto capaIter = a->listOfCapability.begin();
397 capaIter != a->listOfCapability.end(); ++capaIter)
409 for (auto actionIter = gPlayStop->listOfAction.begin();
410 actionIter != gPlayStop->listOfAction.end(); ++actionIter)
414 for (auto capaIter = a->listOfCapability.begin();
415 capaIter != a->listOfCapability.end(); ++capaIter)
427 else if (selectedMenu == 16)
429 if (NULL == gPhoneResourceHandle)
432 << "Error! No resource to leave group. Register resource first!"
437 result = gThingManager->leaveGroup(gFindGroup, collectionResourceType, gPhoneResourceHandle);
438 if (OC_STACK_OK == result)
440 cout << "Leaving group was successful\n";
444 cout << "Leaving group was unsuccessful. result = " << result << endl;
447 else if (selectedMenu == 21)
449 if (NULL == gPhoneResourceHandle)
452 << "Error! No resource to unregister. Register resource first!"
457 result = OCPlatform::unregisterResource(gPhoneResourceHandle);
458 if (OC_STACK_OK == result)
460 cout << "To unregister phone resource was successful\n";
464 cout << "To unregister phone resource was unsuccessful. result = " << result
467 gPhoneResourceHandle = NULL;
472 catch (OCException& e)
474 std::cout << "Exception: " << e.what() << std::endl;