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[])
79 // Create PlatformConfig object
81 { OC::ServiceType::InProc, OC::ModeType::Both/*OC::ModeType::Server*/, "0.0.0.0", 0,
82 OC::QualityOfService::LowQos };
84 OCPlatform::Configure(cfg);
85 gThingManager = new ThingsManager();
95 cout << "(1) CREATE GROUP " << endl;
96 cout << "(11) FIND MUSIC PLAYER & JOIN GROUP | (12) FIND SPEAKER & JOIN GROUP" << endl;
97 cout << "(21) LEAVE GROUP - MUSIC PLAYER | (22) LEAVE GROUP - SPEAKER" << endl;
98 cout << "(31) DELETE GROUP " << endl;
100 std::cin >> selectedMenu;
102 if (selectedMenu == 1)
104 result = gThingManager->createGroup(collectionResourceType);
105 if (OC_STACK_OK == result)
107 cout << "Group creation was successful\n";
111 cout << "Group creation was unsuccessful\n";
114 else if (selectedMenu == 11)
116 result = OCPlatform::findResource("",
117 "coap://224.0.1.187/oc/core?rt=core.musicplayer", onFindResource);
118 if (OC_STACK_OK == result)
120 cout << "Finding music player was successful\n";
124 cout << "Finding music player was unsuccessful\n";
127 else if (selectedMenu == 12)
129 result = OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.speaker",
131 if (OC_STACK_OK == result)
133 cout << "Finding speaker was successful\n";
137 cout << "Finding speaker was unsuccessful\n";
140 else if (selectedMenu == 21)
142 std::vector< OCResourceHandle >::iterator It;
143 OCResourceHandle resourceHandle;
144 for (It = gResourceHandleList.begin(); It != gResourceHandleList.end(); ++It)
146 resourceHandle = (*It);
147 std::string mpType = "core.musicplayer";
148 std::string type = OCGetResourceTypeName(resourceHandle, 0);
149 if (0 == mpType.compare(type))
151 result = gThingManager->leaveGroup(collectionResourceType, resourceHandle);
152 if (OC_STACK_OK == result)
154 cout << "Leaving group of music player was successful\n";
158 cout << "Leaving group of music player was unsuccessful\n";
164 gResourceHandleList.erase(It);
165 result = OCPlatform::unregisterResource(resourceHandle);
166 if (OC_STACK_OK == result)
168 cout << "Unregistering music player was successful\n";
172 cout << "Unregistering music player was unsuccessful\n";
175 else if (selectedMenu == 22)
177 std::vector< OCResourceHandle >::iterator It;
178 OCResourceHandle resourceHandle;
179 for (It = gResourceHandleList.begin(); It != gResourceHandleList.end(); ++It)
181 resourceHandle = (*It);
182 std::string mpType = "core.speaker";
183 std::string type = OCGetResourceTypeName(resourceHandle, 0);
184 if (0 == mpType.compare(type))
186 result = gThingManager->leaveGroup(collectionResourceType, resourceHandle);
187 if (OC_STACK_OK == result)
189 cout << "Leaving group of speaker was successful\n";
193 cout << "Leaving group of speaker was unsuccessful\n";
199 gResourceHandleList.erase(It);
200 result = OCPlatform::unregisterResource(resourceHandle);
201 if (OC_STACK_OK == result)
203 cout << "Unregistering speaker was successful\n";
207 cout << "Unregistering speaker was unsuccessful\n";
210 else if (selectedMenu == 31)
212 gThingManager->deleteGroup(collectionResourceType);
216 catch (OCException& e)