Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / linux / groupsyncaction / phone.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <string>
22 #include <cstdlib>
23 #include <pthread.h>
24 #include "OCPlatform.h"
25 #include "OCApi.h"
26 #include "ThingsManager.h"
27
28 using namespace OC;
29 using namespace OIC;
30
31 static ThingsManager* gThingManager = NULL;
32
33 static OCResourceHandle gPhoneResourceHandle = NULL;
34
35 static std::shared_ptr< OCResource > gFindGroup = NULL;
36
37 static std::string collectionResourceType = "core.group";
38
39 static ActionSet* gPlayStart;
40
41 static ActionSet* gPlayStop;
42
43 void onFindGroup(std::shared_ptr< OCResource > resource)
44 {
45     if (resource)
46     {
47         if (NULL == gPhoneResourceHandle)
48         {
49             cout
50                     << "onFindGroup : Error! No resource to join group. Register resource first!"
51                     << endl;
52             return;
53         }
54
55         if (gFindGroup)
56         {
57             cout << "onFindGroup : Found group is already saved." << endl;
58         }
59         else
60         {
61             cout << "onFindGroup : Found group is saved now." << endl;
62             gFindGroup = resource;
63         }
64
65         gThingManager->joinGroup(gFindGroup, gPhoneResourceHandle);
66     }
67     else
68     {
69         cout << "onFindGroup : Resource is invalid. So a new Group Resource has to be created."
70                 << endl;
71     }
72 }
73
74 void onAction(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
75 {
76     if (eCode == OC_STACK_OK)
77     {
78         cout << "onAction" << endl;
79     }
80     else
81     {
82         cout << "onAction : error - " << eCode << endl;
83     }
84 }
85
86 void onGetChild(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
87 {
88     if (eCode == OC_STACK_OK)
89     {
90         gPlayStart = new ActionSet();
91         gPlayStop = new ActionSet();
92
93         gPlayStart->actionsetName = "playstart";
94         gPlayStop->actionsetName = "playstop";
95
96         std::vector< OCRepresentation > childList = rep.getChildren();
97         OCRepresentation child;
98         std::string resourceURI;
99
100         QueryParamsMap query;
101         std::string playStart;
102         std::string playStop;
103
104         std::vector< std::string > types;
105         std::string resType;
106
107         OCStackResult result;
108
109         for (unsigned int i = 0; i < childList.size(); ++i)
110         {
111 //            cout << "\n\tchild resource - " << i + 1 << endl;
112
113             child = childList.at(i);
114             resourceURI = child.getUri();
115
116 //            cout << "\t\tURI of the resource: " << resourceURI << endl;
117
118 //            cout << "\t\tList of resource types: " << endl;
119
120             types = child.getResourceTypes();
121
122             for (unsigned int j = 0; j < types.size(); ++j)
123             {
124                 resType = types.at(j);
125 //                cout << "\t\t\t" << resType << endl;
126
127                 if (std::string::npos != resType.find("musicplayer"))
128                 {
129                     Capability *pStartCapa = new Capability;
130                     pStartCapa->capability = "play";
131                     pStartCapa->status = "on";
132
133                     Action* pPlayStart = new Action();
134                     pPlayStart->target = resourceURI;
135                     pPlayStart->listOfCapability.push_back(pStartCapa);
136
137                     gPlayStart->listOfAction.push_back(pPlayStart);
138
139                     Capability *pStopCapa = new Capability;
140                     pStopCapa->capability = "play";
141                     pStopCapa->status = "off";
142
143                     Action* pPlayStop = new Action();
144                     pPlayStop->target = resourceURI;
145                     pPlayStop->listOfCapability.push_back(pStopCapa);
146
147                     gPlayStop->listOfAction.push_back(pPlayStop);
148                 }
149                 else if (std::string::npos != resType.find("speaker"))
150                 {
151                     Capability *pStartCapa = new Capability;
152                     pStartCapa->capability = "volume";
153                     pStartCapa->status = "50";
154
155                     Action* pPlayStart = new Action();
156                     pPlayStart->target = resourceURI;
157                     pPlayStart->listOfCapability.push_back(pStartCapa);
158
159                     gPlayStart->listOfAction.push_back(pPlayStart);
160
161                     Capability *pStopCapa = new Capability;
162                     pStopCapa->capability = "volume";
163                     pStopCapa->status = "0";
164
165                     Action* pPlayStop = new Action();
166                     pPlayStop->target = resourceURI;
167                     pPlayStop->listOfCapability.push_back(pStopCapa);
168
169                     gPlayStop->listOfAction.push_back(pPlayStop);
170                 }
171             }
172         }
173
174 //        std::string temp = gThingManager->getStringFromActionSet (gPlayStart);
175 //        cout << "play start - " << temp << endl;
176
177 //        temp  = gThingManager->getStringFromActionSet (gPlayStop);
178 //        cout << "play stop - " << temp << endl;
179
180         if (0 == gPlayStart->listOfAction.empty())
181         {
182             result = gThingManager->addActionSet(gFindGroup, gPlayStart, onAction);
183             if (OC_STACK_OK == result)
184             {
185                 cout << "addActionSet(gPlayStart) was successful\n";
186             }
187             else
188             {
189                 cout << "addActionSet(gPlayStart) was unsuccessful. result = " << result << endl;
190             }
191         }
192
193         if (0 == gPlayStop->listOfAction.empty())
194         {
195             result = gThingManager->addActionSet(gFindGroup, gPlayStop, onAction);
196             if (OC_STACK_OK == result)
197             {
198                 cout << "addActionSet(gPlayStop) was successful\n";
199             }
200             else
201             {
202                 cout << "addActionSet(gPlayStop) was unsuccessful. result = " << result << endl;
203             }
204         }
205     }
206     else
207     {
208         cout << "onGetChild : error - " << eCode << endl;
209     }
210 }
211
212 int main(int argc, char* argv[])
213 {
214     // Create PlatformConfig object
215     PlatformConfig cfg
216     { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
217
218     OCPlatform::Configure(cfg);
219     gThingManager = new ThingsManager();
220
221     int selectedMenu;
222     OCStackResult result;
223
224     try
225     {
226         while (true)
227         {
228             // some operations
229             cout << "(1) CREATE PHONE" << endl;
230             cout
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;
235
236             std::cin >> selectedMenu;
237
238             if (selectedMenu == 1)
239             {
240                 if (gPhoneResourceHandle)
241                 {
242                     cout << "Phone resource is registered already." << endl;
243                     continue;
244                 }
245
246                 std::string resourceURi = "/core/phone";
247                 std::string resourceTypeName = "core.phone";
248                 std::string resourceInterface = DEFAULT_INTERFACE;
249
250                 result = OCPlatform::registerResource(gPhoneResourceHandle, resourceURi,
251                         resourceTypeName, resourceInterface,
252                         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
253
254                 if (OC_STACK_OK == result)
255                 {
256                     cout << "To register phone resource was successful\n";
257                 }
258                 else
259                 {
260                     cout << "To register phone resource was unsuccessful. result = " << result
261                             << endl;
262                 }
263             }
264             else if (selectedMenu == 11)
265             {
266                 std::vector< std::string > types;
267                 types.clear();
268                 types.push_back(collectionResourceType);
269
270                 result = gThingManager->findGroup(types, &onFindGroup);
271                 if (OC_STACK_OK == result)
272                 {
273                     cout << "Finding group was successful\n";
274                 }
275                 else
276                 {
277                     cout << "Finding group was unsuccessful. result = " << result << endl;
278                 }
279             }
280             else if (selectedMenu == 12)
281             {
282                 if (!gFindGroup)
283                 {
284                     cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
285                     continue;
286                 }
287
288                 QueryParamsMap queryParamsMap;
289                 result = gFindGroup->get("", DEFAULT_INTERFACE, queryParamsMap, onGetChild);
290                 if (OC_STACK_OK == result)
291                 {
292                     cout << "gFindGroup->get was successful\n";
293                 }
294                 else
295                 {
296                     cout << "gFindGroup->get was unsuccessful. result = " << result << endl;
297                 }
298             }
299             else if (selectedMenu == 13)
300             {
301                 if (!gFindGroup)
302                 {
303                     cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
304                     continue;
305                 }
306
307                 if (!gPlayStart)
308                 {
309                     cout << "gPlayStart is NULL. Please process step 12 first." << endl;
310                     continue;
311                 }
312
313                 result = gThingManager->executeActionSet(gFindGroup, "playstart", onAction);
314                 if (OC_STACK_OK == result)
315                 {
316                     cout << "DoAction(playstart) was successful\n";
317                 }
318                 else
319                 {
320                     cout << "DoAction(playstart) was unsuccessful. result = " << result << endl;
321                 }
322             }
323             else if (selectedMenu == 14)
324             {
325                 if (!gFindGroup)
326                 {
327                     cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
328                     continue;
329                 }
330
331                 if (!gPlayStop)
332                 {
333                     cout << "gPlayStop is NULL. Please process step 12 first." << endl;
334                     continue;
335                 }
336
337                 result = gThingManager->executeActionSet(gFindGroup, "playstop", onAction);
338                 if (OC_STACK_OK == result)
339                 {
340                     cout << "DoAction(playstop) was successful\n";
341                 }
342                 else
343                 {
344                     cout << "DoAction(playstop) was unsuccessful. result = " << result << endl;
345                 }
346             }
347             else if (selectedMenu == 15)
348             {
349                 if (!gFindGroup)
350                 {
351                     cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
352                     continue;
353                 }
354
355                 if (!gPlayStart)
356                 {
357                     cout << "gPlayStart is NULL. Please process step 12 first." << endl;
358                     continue;
359                 }
360
361                 if (!gPlayStop)
362                 {
363                     cout << "gPlayStop is NULL. Please process step 12 first." << endl;
364                     continue;
365                 }
366
367                 result = gThingManager->deleteActionSet(gFindGroup, "playstart", onAction);
368                 if (OC_STACK_OK == result)
369                 {
370                     cout << "Delete Action(playstart) was successful\n";
371                 }
372                 else
373                 {
374                     cout << "Delete Action(playstart) was unsuccessful. result = " << result
375                             << endl;
376                 }
377
378                 result = gThingManager->deleteActionSet(gFindGroup, "playstop", onAction);
379                 if (OC_STACK_OK == result)
380                 {
381                     cout << "Delete Action(playstop) was successful\n";
382                 }
383                 else
384                 {
385                     cout << "Delete Action(playstop) was unsuccessful. result = " << result << endl;
386                 }
387
388                 Action* a;
389                 Capability* c;
390
391                 for (auto actionIter = gPlayStart->listOfAction.begin();
392                         actionIter != gPlayStart->listOfAction.end(); ++actionIter)
393                 {
394                     a = (*actionIter);
395
396                     for (auto capaIter = a->listOfCapability.begin();
397                             capaIter != a->listOfCapability.end(); ++capaIter)
398                     {
399                         c = (*capaIter);
400                         delete c;
401                     }
402
403                     delete a;
404                 }
405
406                 delete gPlayStart;
407                 gPlayStart = NULL;
408
409                 for (auto actionIter = gPlayStop->listOfAction.begin();
410                         actionIter != gPlayStop->listOfAction.end(); ++actionIter)
411                 {
412                     a = (*actionIter);
413
414                     for (auto capaIter = a->listOfCapability.begin();
415                             capaIter != a->listOfCapability.end(); ++capaIter)
416                     {
417                         c = (*capaIter);
418                         delete c;
419                     }
420
421                     delete a;
422                 }
423
424                 delete gPlayStop;
425                 gPlayStop = NULL;
426             }
427             else if (selectedMenu == 16)
428             {
429                 if (NULL == gPhoneResourceHandle)
430                 {
431                     cout
432                             << "Error! No resource to leave group. Register resource first!"
433                             << endl;
434                     continue;
435                 }
436
437                 result = gThingManager->leaveGroup(gFindGroup, collectionResourceType,  gPhoneResourceHandle);
438                 if (OC_STACK_OK == result)
439                 {
440                     cout << "Leaving group was successful\n";
441                 }
442                 else
443                 {
444                     cout << "Leaving group was unsuccessful. result = " << result << endl;
445                 }
446             }
447             else if (selectedMenu == 21)
448             {
449                 if (NULL == gPhoneResourceHandle)
450                 {
451                     cout
452                             << "Error! No resource to unregister. Register resource first!"
453                             << endl;
454                     continue;
455                 }
456
457                 result = OCPlatform::unregisterResource(gPhoneResourceHandle);
458                 if (OC_STACK_OK == result)
459                 {
460                     cout << "To unregister phone resource was successful\n";
461                 }
462                 else
463                 {
464                     cout << "To unregister phone resource was unsuccessful. result = " << result
465                             << endl;
466                 }
467                 gPhoneResourceHandle = NULL;
468             }
469         }
470
471     }
472     catch (OCException& e)
473     {
474         std::cout << "Exception: " << e.what() << std::endl;
475     }
476
477     return 0;
478 }
479