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