Merge Things Manager codes CA and Master branch.
[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         OCRepresentation rep;
102         std::string playStart;
103         std::string playStop;
104
105         std::vector< std::string > types;
106         std::string resType;
107
108         OCStackResult result;
109
110         for (unsigned int i = 0; i < childList.size(); ++i)
111         {
112 //            cout << "\n\tchild resource - " << i + 1 << endl;
113
114             child = childList.at(i);
115             resourceURI = child.getUri();
116
117 //            cout << "\t\tURI of the resource: " << resourceURI << endl;
118
119 //            cout << "\t\tList of resource types: " << endl;
120
121             types = child.getResourceTypes();
122
123             for (unsigned int j = 0; j < types.size(); ++j)
124             {
125                 resType = types.at(j);
126 //                cout << "\t\t\t" << resType << endl;
127
128                 if (std::string::npos != resType.find("musicplayer"))
129                 {
130                     Capability *pStartCapa = new Capability;
131                     pStartCapa->capability = "play";
132                     pStartCapa->status = "on";
133
134                     Action* pPlayStart = new Action();
135                     pPlayStart->target = resourceURI;
136                     pPlayStart->listOfCapability.push_back(pStartCapa);
137
138                     gPlayStart->listOfAction.push_back(pPlayStart);
139
140                     Capability *pStopCapa = new Capability;
141                     pStopCapa->capability = "play";
142                     pStopCapa->status = "off";
143
144                     Action* pPlayStop = new Action();
145                     pPlayStop->target = resourceURI;
146                     pPlayStop->listOfCapability.push_back(pStopCapa);
147
148                     gPlayStop->listOfAction.push_back(pPlayStop);
149                 }
150                 else if (std::string::npos != resType.find("speaker"))
151                 {
152                     Capability *pStartCapa = new Capability;
153                     pStartCapa->capability = "volume";
154                     pStartCapa->status = "50";
155
156                     Action* pPlayStart = new Action();
157                     pPlayStart->target = resourceURI;
158                     pPlayStart->listOfCapability.push_back(pStartCapa);
159
160                     gPlayStart->listOfAction.push_back(pPlayStart);
161
162                     Capability *pStopCapa = new Capability;
163                     pStopCapa->capability = "volume";
164                     pStopCapa->status = "0";
165
166                     Action* pPlayStop = new Action();
167                     pPlayStop->target = resourceURI;
168                     pPlayStop->listOfCapability.push_back(pStopCapa);
169
170                     gPlayStop->listOfAction.push_back(pPlayStop);
171                 }
172             }
173         }
174
175 //        std::string temp = gThingManager->getStringFromActionSet (gPlayStart);
176 //        cout << "play start - " << temp << endl;
177
178 //        temp  = gThingManager->getStringFromActionSet (gPlayStop);
179 //        cout << "play stop - " << temp << endl;
180
181         if (0 == gPlayStart->listOfAction.empty())
182         {
183             result = gThingManager->addActionSet(gFindGroup, gPlayStart, onAction);
184             if (OC_STACK_OK == result)
185             {
186                 cout << "addActionSet(gPlayStart) was successful\n";
187             }
188             else
189             {
190                 cout << "addActionSet(gPlayStart) was unsuccessful. result = " << result << endl;
191             }
192         }
193
194         if (0 == gPlayStop->listOfAction.empty())
195         {
196             result = gThingManager->addActionSet(gFindGroup, gPlayStop, onAction);
197             if (OC_STACK_OK == result)
198             {
199                 cout << "addActionSet(gPlayStop) was successful\n";
200             }
201             else
202             {
203                 cout << "addActionSet(gPlayStop) was unsuccessful. result = " << result << endl;
204             }
205         }
206     }
207     else
208     {
209         cout << "onGetChild : error - " << eCode << endl;
210     }
211 }
212
213 int main(int argc, char* argv[])
214 {
215     // Create PlatformConfig object
216     PlatformConfig cfg
217     { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
218
219     OCPlatform::Configure(cfg);
220     gThingManager = new ThingsManager();
221
222     int selectedMenu;
223     OCStackResult result;
224
225     try
226     {
227         while (true)
228         {
229             // some operations
230             cout << "(1) CREATE PHONE" << endl;
231             cout
232                     << "(11) FIND & JOIN GROUP | (12) ADD GROUP ACTION | (13) PLAY START"
233                     << " | (14) PLAY STOP"<< endl;
234             cout << "(15) DELETE GROUP ACTION | (16) LEAVE GROUP" << endl;
235             cout << "(21) DELETE PHONE" << endl;
236
237             std::cin >> selectedMenu;
238
239             if (selectedMenu == 1)
240             {
241                 if (gPhoneResourceHandle)
242                 {
243                     cout << "Phone resource is registered already." << endl;
244                     continue;
245                 }
246
247                 std::string resourceURi = "/core/phone";
248                 std::string resourceTypeName = "core.phone";
249                 std::string resourceInterface = DEFAULT_INTERFACE;
250
251                 result = OCPlatform::registerResource(gPhoneResourceHandle, resourceURi,
252                         resourceTypeName, resourceInterface, NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
253                 if (OC_STACK_OK == result)
254                 {
255                     cout << "To register phone resource was successful\n";
256                 }
257                 else
258                 {
259                     cout << "To register phone resource was unsuccessful. result = " << result
260                             << endl;
261                 }
262             }
263             else if (selectedMenu == 11)
264             {
265                 std::vector< std::string > types;
266                 types.clear();
267                 types.push_back(collectionResourceType);
268
269                 result = gThingManager->findGroup(types, &onFindGroup);
270                 if (OC_STACK_OK == result)
271                 {
272                     cout << "Finding group was successful\n";
273                 }
274                 else
275                 {
276                     cout << "Finding group was unsuccessful. result = " << result << endl;
277                 }
278             }
279             else if (selectedMenu == 12)
280             {
281                 if (!gFindGroup)
282                 {
283                     cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
284                     continue;
285                 }
286
287                 QueryParamsMap queryParamsMap;
288                 result = gFindGroup->get("", DEFAULT_INTERFACE, queryParamsMap, onGetChild);
289                 if (OC_STACK_OK == result)
290                 {
291                     cout << "gFindGroup->get was successful\n";
292                 }
293                 else
294                 {
295                     cout << "gFindGroup->get was unsuccessful. result = " << result << endl;
296                 }
297             }
298             else if (selectedMenu == 13)
299             {
300                 if (!gFindGroup)
301                 {
302                     cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
303                     continue;
304                 }
305
306                 if (!gPlayStart)
307                 {
308                     cout << "gPlayStart is NULL. Please process step 12 first." << endl;
309                     continue;
310                 }
311
312                 result = gThingManager->executeActionSet(gFindGroup, "playstart", onAction);
313                 if (OC_STACK_OK == result)
314                 {
315                     cout << "DoAction(playstart) was successful\n";
316                 }
317                 else
318                 {
319                     cout << "DoAction(playstart) was unsuccessful. result = " << result << endl;
320                 }
321             }
322             else if (selectedMenu == 14)
323             {
324                 if (!gFindGroup)
325                 {
326                     cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
327                     continue;
328                 }
329
330                 if (!gPlayStop)
331                 {
332                     cout << "gPlayStop is NULL. Please process step 12 first." << endl;
333                     continue;
334                 }
335
336                 result = gThingManager->executeActionSet(gFindGroup, "playstop", onAction);
337                 if (OC_STACK_OK == result)
338                 {
339                     cout << "DoAction(playstop) was successful\n";
340                 }
341                 else
342                 {
343                     cout << "DoAction(playstop) was unsuccessful. result = " << result << endl;
344                 }
345             }
346             else if (selectedMenu == 15)
347             {
348                 if (!gFindGroup)
349                 {
350                     cout << "gFindGroup is NULL. Please process step 1 and step 11 first." << endl;
351                     continue;
352                 }
353
354                 if (!gPlayStart)
355                 {
356                     cout << "gPlayStart is NULL. Please process step 12 first." << endl;
357                     continue;
358                 }
359
360                 if (!gPlayStop)
361                 {
362                     cout << "gPlayStop is NULL. Please process step 12 first." << endl;
363                     continue;
364                 }
365
366                 result = gThingManager->deleteActionSet(gFindGroup, "playstart", onAction);
367                 if (OC_STACK_OK == result)
368                 {
369                     cout << "Delete Action(playstart) was successful\n";
370                 }
371                 else
372                 {
373                     cout << "Delete Action(playstart) was unsuccessful. result = " << result
374                             << endl;
375                 }
376
377                 result = gThingManager->deleteActionSet(gFindGroup, "playstop", onAction);
378                 if (OC_STACK_OK == result)
379                 {
380                     cout << "Delete Action(playstop) was successful\n";
381                 }
382                 else
383                 {
384                     cout << "Delete Action(playstop) was unsuccessful. result = " << result << endl;
385                 }
386
387                 Action* a;
388                 Capability* c;
389
390                 for (auto actionIter = gPlayStart->listOfAction.begin();
391                         actionIter != gPlayStart->listOfAction.end(); ++actionIter)
392                 {
393                     a = (*actionIter);
394
395                     for (auto capaIter = a->listOfCapability.begin();
396                             capaIter != a->listOfCapability.end(); ++capaIter)
397                     {
398                         c = (*capaIter);
399                         delete c;
400                     }
401
402                     delete a;
403                 }
404
405                 delete gPlayStart;
406                 gPlayStart = NULL;
407
408                 for (auto actionIter = gPlayStop->listOfAction.begin();
409                         actionIter != gPlayStop->listOfAction.end(); ++actionIter)
410                 {
411                     a = (*actionIter);
412
413                     for (auto capaIter = a->listOfCapability.begin();
414                             capaIter != a->listOfCapability.end(); ++capaIter)
415                     {
416                         c = (*capaIter);
417                         delete c;
418                     }
419
420                     delete a;
421                 }
422
423                 delete gPlayStop;
424                 gPlayStop = NULL;
425             }
426             else if (selectedMenu == 16)
427             {
428                 if (NULL == gPhoneResourceHandle)
429                 {
430                     cout
431                             << "Error! No resource to leave group. Register resource first!"
432                             << endl;
433                     continue;
434                 }
435
436                 result = gThingManager->leaveGroup(gFindGroup, collectionResourceType,  gPhoneResourceHandle);
437                 if (OC_STACK_OK == result)
438                 {
439                     cout << "Leaving group was successful\n";
440                 }
441                 else
442                 {
443                     cout << "Leaving group was unsuccessful. result = " << result << endl;
444                 }
445             }
446             else if (selectedMenu == 21)
447             {
448                 if (NULL == gPhoneResourceHandle)
449                 {
450                     cout
451                             << "Error! No resource to unregister. Register resource first!"
452                             << endl;
453                     continue;
454                 }
455
456                 result = OCPlatform::unregisterResource(gPhoneResourceHandle);
457                 if (OC_STACK_OK == result)
458                 {
459                     cout << "To unregister phone resource was successful\n";
460                 }
461                 else
462                 {
463                     cout << "To unregister phone resource was unsuccessful. result = " << result
464                             << endl;
465                 }
466                 gPhoneResourceHandle = NULL;
467             }
468         }
469
470     }
471     catch (OCException& e)
472     {
473         //log(e.what());
474     }
475
476     return 0;
477 }
478