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 presenceCallback(std::string msg, OCStackResult res)
45 std::cout << "Presence Callback: " << msg << "(" << res << ")" << std::endl;
48 void onFindGroup(std::shared_ptr< OCResource > resource)
52 if (NULL == gPhoneResourceHandle)
55 << "onFindGroup : Error! No resource to join group. Register resource first!"
62 cout << "onFindGroup : Found group is already saved." << endl;
66 cout << "onFindGroup : Found group is saved now." << endl;
67 gFindGroup = resource;
70 res = gThingManager->subscribeCollectionPresence( resource, &presenceCallback);
72 std::cout << "Return Value: " << res << std::endl;
76 gThingManager->joinGroup(gFindGroup, gPhoneResourceHandle);
80 cout << "onFindGroup : Resource is invalid. So a new Group Resource has to be created."
85 void onAction(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
87 if (eCode == OC_STACK_OK)
89 cout << "onAction" << endl;
93 cout << "onAction : error - " << eCode << endl;
97 void onGetChild(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
99 if (eCode == OC_STACK_OK)
101 gPlayStart = new ActionSet();
102 gPlayStop = new ActionSet();
104 gPlayStart->actionsetName = "playstart";
105 gPlayStop->actionsetName = "playstop";
107 std::vector< OCRepresentation > childList = rep.getChildren();
108 OCRepresentation child;
109 std::string resourceURI;
111 QueryParamsMap query;
112 std::string playStart;
113 std::string playStop;
115 std::vector< std::string > types;
118 OCStackResult result;
120 for (unsigned int i = 0; i < childList.size(); ++i)
122 // cout << "\n\tchild resource - " << i + 1 << endl;
124 child = childList.at(i);
125 resourceURI = child.getUri();
127 // cout << "\t\tURI of the resource: " << resourceURI << endl;
129 // cout << "\t\tList of resource types: " << endl;
131 types = child.getResourceTypes();
133 for (unsigned int j = 0; j < types.size(); ++j)
135 resType = types.at(j);
136 // cout << "\t\t\t" << resType << endl;
138 if (std::string::npos != resType.find("musicplayer"))
140 Capability *pStartCapa = new Capability;
141 pStartCapa->capability = "play";
142 pStartCapa->status = "on";
144 Action* pPlayStart = new Action();
145 pPlayStart->target = resourceURI;
146 pPlayStart->listOfCapability.push_back(pStartCapa);
148 gPlayStart->listOfAction.push_back(pPlayStart);
150 Capability *pStopCapa = new Capability;
151 pStopCapa->capability = "play";
152 pStopCapa->status = "off";
154 Action* pPlayStop = new Action();
155 pPlayStop->target = resourceURI;
156 pPlayStop->listOfCapability.push_back(pStopCapa);
158 gPlayStop->listOfAction.push_back(pPlayStop);
160 else if (std::string::npos != resType.find("speaker"))
162 Capability *pStartCapa = new Capability;
163 pStartCapa->capability = "volume";
164 pStartCapa->status = "50";
166 Action* pPlayStart = new Action();
167 pPlayStart->target = resourceURI;
168 pPlayStart->listOfCapability.push_back(pStartCapa);
170 gPlayStart->listOfAction.push_back(pPlayStart);
172 Capability *pStopCapa = new Capability;
173 pStopCapa->capability = "volume";
174 pStopCapa->status = "0";
176 Action* pPlayStop = new Action();
177 pPlayStop->target = resourceURI;
178 pPlayStop->listOfCapability.push_back(pStopCapa);
180 gPlayStop->listOfAction.push_back(pPlayStop);
185 // std::string temp = gThingManager->getStringFromActionSet (gPlayStart);
186 // cout << "play start - " << temp << endl;
188 // temp = gThingManager->getStringFromActionSet (gPlayStop);
189 // cout << "play stop - " << temp << endl;
191 if (0 == gPlayStart->listOfAction.empty())
193 result = gThingManager->addActionSet(gFindGroup, gPlayStart, onAction);
194 if (OC_STACK_OK == result)
196 cout << "addActionSet(gPlayStart) was successful\n";
200 cout << "addActionSet(gPlayStart) was unsuccessful. result = " << result << endl;
204 if (0 == gPlayStop->listOfAction.empty())
206 result = gThingManager->addActionSet(gFindGroup, gPlayStop, onAction);
207 if (OC_STACK_OK == result)
209 cout << "addActionSet(gPlayStop) was successful\n";
213 cout << "addActionSet(gPlayStop) was unsuccessful. result = " << result << endl;
219 cout << "onGetChild : error - " << eCode << endl;
223 int main(int argc, char* argv[])
225 // Create PlatformConfig object
227 { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
229 OCPlatform::Configure(cfg);
230 gThingManager = new ThingsManager();
233 OCStackResult result;
240 cout << "(1) CREATE PHONE" << endl;
242 << "(11) FIND & JOIN GROUP | (12) ADD GROUP ACTION | (13) PLAY START"
243 << " | (14) PLAY STOP"<< endl;
244 cout << "(15) DELETE GROUP ACTION | (16) LEAVE GROUP" << endl;
245 cout << "(21) DELETE PHONE" << endl;
247 std::cin >> selectedMenu;
249 if (selectedMenu == 1)
251 if (gPhoneResourceHandle)
253 cout << "Phone resource is registered already." << endl;
257 std::string resourceURi = "/core/phone";
258 std::string resourceTypeName = "core.phone";
259 std::string resourceInterface = DEFAULT_INTERFACE;
261 result = OCPlatform::registerResource(gPhoneResourceHandle, resourceURi,
262 resourceTypeName, resourceInterface,
263 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
265 if (OC_STACK_OK == result)
267 cout << "To register phone resource was successful\n";
271 cout << "To register phone resource was unsuccessful. result = " << result
275 else if (selectedMenu == 11)
277 std::vector< std::string > types;
279 types.push_back(collectionResourceType);
281 result = gThingManager->findGroup(types, &onFindGroup);
282 if (OC_STACK_OK == result)
284 cout << "Finding group was successful\n";
288 cout << "Finding group was unsuccessful. result = " << result << endl;
291 else if (selectedMenu == 12)
295 cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
299 QueryParamsMap queryParamsMap;
300 result = gFindGroup->get("", DEFAULT_INTERFACE, queryParamsMap, onGetChild);
301 if (OC_STACK_OK == result)
303 cout << "gFindGroup->get was successful\n";
307 cout << "gFindGroup->get was unsuccessful. result = " << result << endl;
310 else if (selectedMenu == 13)
314 cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
320 cout << "gPlayStart is NULL. Please process step 12 first." << endl;
324 result = gThingManager->executeActionSet(gFindGroup, "playstart", onAction);
325 if (OC_STACK_OK == result)
327 cout << "DoAction(playstart) was successful\n";
331 cout << "DoAction(playstart) was unsuccessful. result = " << result << endl;
334 else if (selectedMenu == 14)
338 cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
344 cout << "gPlayStop is NULL. Please process step 12 first." << endl;
348 result = gThingManager->executeActionSet(gFindGroup, "playstop", onAction);
349 if (OC_STACK_OK == result)
351 cout << "DoAction(playstop) was successful\n";
355 cout << "DoAction(playstop) was unsuccessful. result = " << result << endl;
358 else if (selectedMenu == 15)
362 cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
368 cout << "gPlayStart is NULL. Please process step 12 first." << endl;
374 cout << "gPlayStop is NULL. Please process step 12 first." << endl;
378 result = gThingManager->deleteActionSet(gFindGroup, "playstart", onAction);
379 if (OC_STACK_OK == result)
381 cout << "Delete Action(playstart) was successful\n";
385 cout << "Delete Action(playstart) was unsuccessful. result = " << result
389 result = gThingManager->deleteActionSet(gFindGroup, "playstop", onAction);
390 if (OC_STACK_OK == result)
392 cout << "Delete Action(playstop) was successful\n";
396 cout << "Delete Action(playstop) was unsuccessful. result = " << result << endl;
402 for (auto actionIter = gPlayStart->listOfAction.begin();
403 actionIter != gPlayStart->listOfAction.end(); ++actionIter)
407 for (auto capaIter = a->listOfCapability.begin();
408 capaIter != a->listOfCapability.end(); ++capaIter)
420 for (auto actionIter = gPlayStop->listOfAction.begin();
421 actionIter != gPlayStop->listOfAction.end(); ++actionIter)
425 for (auto capaIter = a->listOfCapability.begin();
426 capaIter != a->listOfCapability.end(); ++capaIter)
438 else if (selectedMenu == 16)
440 if (NULL == gPhoneResourceHandle)
443 << "Error! No resource to leave group. Register resource first!"
448 result = gThingManager->leaveGroup(gFindGroup, collectionResourceType, gPhoneResourceHandle);
449 if (OC_STACK_OK == result)
451 cout << "Leaving group was successful\n";
455 cout << "Leaving group was unsuccessful. result = " << result << endl;
458 else if (selectedMenu == 21)
460 if (NULL == gPhoneResourceHandle)
463 << "Error! No resource to unregister. Register resource first!"
468 result = OCPlatform::unregisterResource(gPhoneResourceHandle);
469 if (OC_STACK_OK == result)
471 cout << "To unregister phone resource was successful\n";
475 cout << "To unregister phone resource was unsuccessful. result = " << result
478 gPhoneResourceHandle = NULL;
483 catch (OCException& e)
485 std::cout << "Exception: " << e.what() << std::endl;