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;
43 OCResourceHandle resourceHandle;
44 OCStackResult result = OCPlatform::registerResource(resourceHandle, resource);
45 if (OC_STACK_OK == result)
47 cout << "onFindResource : Resource creation was successful\n";
51 cout << "onFindResource : Resource creation was unsuccessful\n";
55 result = gThingManager->joinGroup(collectionResourceType, resourceHandle);
56 if (OC_STACK_OK == result)
58 cout << "onFindResource : Joining group was successful\n";
62 cout << "onFindResource : Joining group was unsuccessful\n";
64 OCPlatform::unregisterResource(resourceHandle);
68 gResourceHandleList.push_back(resourceHandle);
72 cout << "onFindResource : There is no found resource." << endl;
76 int main(int argc, char* argv[])
78 // Create PlatformConfig object
80 { OC::ServiceType::InProc, OC::ModeType::Both/*OC::ModeType::Server*/, "0.0.0.0", 0,
81 OC::QualityOfService::LowQos };
83 OCPlatform::Configure(cfg);
84 gThingManager = new ThingsManager();
94 cout << "(1) CREATE GROUP " << endl;
95 cout << "(11) FIND MUSIC PLAYER & JOIN GROUP | (12) FIND SPEAKER & JOIN GROUP" << endl;
96 cout << "(21) LEAVE GROUP - MUSIC PLAYER | (22) LEAVE GROUP - SPEAKER" << endl;
97 cout << "(31) DELETE GROUP " << endl;
99 std::cin >> selectedMenu;
101 if (selectedMenu == 1)
103 result = gThingManager->createGroup(collectionResourceType);
104 if (OC_STACK_OK == result)
106 cout << "Group creation was successful\n";
110 cout << "Group creation was unsuccessful\n";
113 else if (selectedMenu == 11)
116 query << OC_MULTICAST_DISCOVERY_URI << "?rt=core.musicplayer";
118 cout << query.str() << endl;
119 result = OCPlatform::findResource("",
124 result = OCPlatform::findResource("",
125 "coap://224.0.1.187/oc/core?rt=core.musicplayer",
129 if (OC_STACK_OK == result)
131 cout << "Finding music player was successful\n";
135 cout << "Finding music player was unsuccessful\n";
138 else if (selectedMenu == 12)
141 query << OC_MULTICAST_DISCOVERY_URI << "?rt=core.speaker";
142 result = OCPlatform::findResource("",
147 if (OC_STACK_OK == result)
149 cout << "Finding speaker was successful\n";
153 cout << "Finding speaker was unsuccessful\n";
156 else if (selectedMenu == 21)
158 std::vector< OCResourceHandle >::iterator It;
159 OCResourceHandle resourceHandle;
160 for (It = gResourceHandleList.begin(); It != gResourceHandleList.end(); ++It)
162 resourceHandle = (*It);
163 std::string mpType = "core.musicplayer";
164 std::string type = OCGetResourceTypeName(resourceHandle, 0);
165 if (0 == mpType.compare(type))
167 result = gThingManager->leaveGroup(collectionResourceType, resourceHandle);
168 if (OC_STACK_OK == result)
170 cout << "Leaving group of music player was successful\n";
174 cout << "Leaving group of music player was unsuccessful\n";
180 if(It == gResourceHandleList.end())
183 gResourceHandleList.erase(It);
184 result = OCPlatform::unregisterResource(resourceHandle);
185 if (OC_STACK_OK == result)
187 cout << "Unregistering music player was successful\n";
191 cout << "Unregistering music player was unsuccessful\n";
194 else if (selectedMenu == 22)
196 std::vector< OCResourceHandle >::iterator It;
197 OCResourceHandle resourceHandle;
198 for (It = gResourceHandleList.begin(); It != gResourceHandleList.end(); ++It)
200 resourceHandle = (*It);
201 std::string mpType = "core.speaker";
202 std::string type = OCGetResourceTypeName(resourceHandle, 0);
203 if (0 == mpType.compare(type))
205 result = gThingManager->leaveGroup(collectionResourceType, resourceHandle);
206 if (OC_STACK_OK == result)
208 cout << "Leaving group of speaker was successful\n";
212 cout << "Leaving group of speaker was unsuccessful\n";
218 if(It == gResourceHandleList.end())
221 gResourceHandleList.erase(It);
222 result = OCPlatform::unregisterResource(resourceHandle);
223 if (OC_STACK_OK == result)
225 cout << "Unregistering speaker was successful\n";
229 cout << "Unregistering speaker was unsuccessful\n";
232 else if (selectedMenu == 31)
234 gThingManager->deleteGroup(collectionResourceType);
238 catch (OCException& e)
240 std::cout << "Exception: " << e.what() << std::endl;