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 std::vector< OCResourceHandle > gResourceHandleList;
35 static std::string collectionResourceType = "core.group";
37 void onFindResource(std::shared_ptr< OCResource > resource)
39 cout << "onFindResource" << endl;
45 OCResourceHandle resourceHandle;
46 OCStackResult result = OCPlatform::registerResource(resourceHandle, resource);
47 if (OC_STACK_OK == result)
49 cout << "onFindResource : Resource creation was successful\n";
53 cout << "onFindResource : Resource creation was unsuccessful\n";
57 result = gThingManager->joinGroup(collectionResourceType, resourceHandle);
58 if (OC_STACK_OK == result)
60 cout << "onFindResource : Joining group was successful\n";
64 cout << "onFindResource : Joining group was unsuccessful\n";
66 OCPlatform::unregisterResource(resourceHandle);
70 gResourceHandleList.push_back(resourceHandle);
74 cout << "onFindResource : There is no found resource." << endl;
76 }catch (std::exception& e)
78 std::cout << "Exception: " << e.what() << std::endl;
82 int main(int argc, char* argv[])
84 // Create PlatformConfig object
86 { OC::ServiceType::InProc, OC::ModeType::Both/*OC::ModeType::Server*/, "0.0.0.0", 0,
87 OC::QualityOfService::LowQos };
89 OCPlatform::Configure(cfg);
90 gThingManager = new ThingsManager();
100 cout << "(1) CREATE GROUP " << endl;
101 cout << "(11) FIND MUSIC PLAYER & JOIN GROUP | (12) FIND SPEAKER & JOIN GROUP" << endl;
102 cout << "(21) LEAVE GROUP - MUSIC PLAYER | (22) LEAVE GROUP - SPEAKER" << endl;
103 cout << "(31) DELETE GROUP " << endl;
105 std::cin >> selectedMenu;
107 if (selectedMenu == 1)
109 result = gThingManager->createGroup(collectionResourceType);
110 if (OC_STACK_OK == result)
112 cout << "Group creation was successful\n";
116 cout << "Group creation was unsuccessful\n";
119 else if (selectedMenu == 11)
122 query << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.musicplayer";
124 cout << query.str() << endl;
125 result = OCPlatform::findResource("",
130 if (OC_STACK_OK == result)
132 cout << "Finding music player was successful\n";
136 cout << "Finding music player was unsuccessful\n";
139 else if (selectedMenu == 12)
142 query << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.speaker";
143 result = OCPlatform::findResource("",
148 if (OC_STACK_OK == result)
150 cout << "Finding speaker was successful\n";
154 cout << "Finding speaker was unsuccessful\n";
157 else if (selectedMenu == 21)
159 std::vector< OCResourceHandle >::iterator It;
160 OCResourceHandle resourceHandle;
161 for (It = gResourceHandleList.begin(); It != gResourceHandleList.end(); ++It)
163 resourceHandle = (*It);
164 std::string mpType = "core.musicplayer";
165 std::string type = OCGetResourceTypeName(resourceHandle, 0);
166 if (0 == mpType.compare(type))
168 result = gThingManager->leaveGroup(collectionResourceType, resourceHandle);
169 if (OC_STACK_OK == result)
171 cout << "Leaving group of music player was successful\n";
175 cout << "Leaving group of music player was unsuccessful\n";
181 if(It == gResourceHandleList.end())
184 gResourceHandleList.erase(It);
185 result = OCPlatform::unregisterResource(resourceHandle);
186 if (OC_STACK_OK == result)
188 cout << "Unregistering music player was successful\n";
192 cout << "Unregistering music player was unsuccessful\n";
195 else if (selectedMenu == 22)
197 std::vector< OCResourceHandle >::iterator It;
198 OCResourceHandle resourceHandle;
199 for (It = gResourceHandleList.begin(); It != gResourceHandleList.end(); ++It)
201 resourceHandle = (*It);
202 std::string mpType = "core.speaker";
203 std::string type = OCGetResourceTypeName(resourceHandle, 0);
204 if (0 == mpType.compare(type))
206 result = gThingManager->leaveGroup(collectionResourceType, resourceHandle);
207 if (OC_STACK_OK == result)
209 cout << "Leaving group of speaker was successful\n";
213 cout << "Leaving group of speaker was unsuccessful\n";
219 if(It == gResourceHandleList.end())
222 gResourceHandleList.erase(It);
223 result = OCPlatform::unregisterResource(resourceHandle);
224 if (OC_STACK_OK == result)
226 cout << "Unregistering speaker was successful\n";
230 cout << "Unregistering speaker was unsuccessful\n";
233 else if (selectedMenu == 31)
235 gThingManager->deleteGroup(collectionResourceType);
239 catch (OCException& e)
241 std::cout << "Exception: " << e.what() << std::endl;