db7fca28fd79ef8d90460f36809087ae08aa5fdd
[platform/upstream/iotivity.git] / service / simulator / examples / client / simulator_client.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 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 "simulator_manager.h"
22 #include <map>
23 #include <mutex>
24
25 std::string getOperationStateString(OperationState state)
26 {
27     switch (state)
28     {
29         case OP_START: return "OP_START";
30         case OP_COMPLETE: return "OP_COMPLETE";
31         case OP_ABORT: return "OP_ABORT";
32     }
33
34     return "OP_UNKNOWN";
35 }
36
37 std::string getPropertyTypeString(SimulatorResourceModel::AttributeProperty::Type type)
38 {
39     switch(type)
40     {
41         case SimulatorResourceModel::AttributeProperty::Type::RANGE:
42             return "RANGE";
43         case SimulatorResourceModel::AttributeProperty::Type::VALUE_SET:
44             return "VALUE_SET";
45         default:
46             break;
47     }
48
49     return "UNKNOWN";
50 }
51
52 class AppLogger : public ILogger
53 {
54     public:
55         void write(std::string time, ILogger::Level level, std::string message)
56         {
57             std::cout << "[APPLogger] " << time << " " << ILogger::getString(level) << " " << message;
58         }
59 };
60 std::shared_ptr<AppLogger> gAppLogger(new AppLogger());
61
62 class ClientController
63 {
64     public:
65         void startTest()
66         {
67             printMenu();
68             bool cont = true;
69             while (cont)
70             {
71                 int choice = -1;
72                 std::cout << "Enter your choice: ";
73                 std::cin >> choice;
74                 if (choice < 0 || choice > 12)
75                 {
76                     std::cout << "Invaild choice !" << std::endl; continue;
77                 }
78
79                 switch (choice)
80                 {
81                     case 1: findResource(); break;
82                     case 2: displayResource(); break;
83                     case 3: observeResource(); break;
84                     case 4: cancelObserving(); break;
85                     case 5: sendGet(); break;
86                     case 6: sendPut(); break;
87                     case 7: sendPost(); break;
88                     case 8: sendAllGETRequests(); break;
89                     case 9: sendAllPUTRequests(); break;
90                     case 10: sendAllPOSTRequests(); break;
91                     case 11: configure(); break;
92                     case 12: printMenu(); break;
93                     case 0: cont = false;
94                 }
95             }
96         }
97
98     private:
99         void printMenu()
100         {
101             std::cout << "########### SIMULATOR CLIENT CONTROLLER ###########" << std::endl;
102             std::cout << "1. Find resource" << std::endl;
103             std::cout << "2. Display resource information" << std::endl;
104             std::cout << "3. Observe for resource change" << std::endl;
105             std::cout << "4. Cancel observation" << std::endl;
106             std::cout << "5. Send GET message" << std::endl;
107             std::cout << "6. Send PUT message" << std::endl;
108             std::cout << "7. Send POST message" << std::endl;
109             std::cout << "8. Send All GET requests" << std::endl;
110             std::cout << "9. Send All PUT requests" << std::endl;
111             std::cout << "10. Send All POST requests" << std::endl;
112             std::cout << "11. Configure (using RAML file)" << std::endl;
113             std::cout << "12: Help" << std::endl;
114             std::cout << "0. Exit" << std::endl;
115             std::cout << "###################################################" << std::endl;
116         }
117
118         SimulatorRemoteResourceSP selectResource()
119         {
120             std::lock_guard<std::recursive_mutex> lock(m_mutex);
121             if (0 == m_resList.size())
122             {
123                 std::cout << "No resources!" << std::endl;
124                 return nullptr;
125             }
126
127             int index = 1;
128             std::vector<std::string> ids;
129             for (auto & resourceEntry : m_resList)
130             {
131                 std::cout << index++ << ": " << (resourceEntry.second)->getURI() << "[" <<
132                           (resourceEntry.second)->getHost()  << "]" << std::endl;
133                 ids.push_back((resourceEntry.second)->getID());
134             }
135
136             int choice = -1;
137             std::cout << "Choose the resource: ";
138             std::cin >> choice;
139
140             if (choice < 1 || choice > index - 1)
141             {
142                 std::cout << "Invalid choice !" << std::endl;
143                 return nullptr;
144             }
145
146             return m_resList[ids[choice - 1]];
147         }
148
149         void findResource()
150         {
151             std::string resourceType;
152             std::cout << "Enter resource type : ";
153             std::cin >> resourceType;
154
155             ResourceFindCallback callback = [this](std::shared_ptr<SimulatorRemoteResource> resource)
156             {
157                 std::cout << "Resource found ######" << std::endl;
158                 displayResource(resource);
159
160                 // Add to local list
161                 std::lock_guard<std::recursive_mutex> lock(m_mutex);
162                 if (m_resList.end() == m_resList.find(resource->getID()))
163                     m_resList[resource->getID()] = resource;
164                 else
165                     std::cout << "Resource with UID: " << resource->getID() << "already exist in the list!" <<
166                               std::endl;
167             };
168
169             try
170             {
171                 SimulatorManager::getInstance()->findResource(resourceType, callback);
172                 std::cout << "SimulatorManager::findResource is successful" << std::endl;
173                 m_resList.clear();
174             }
175             catch (InvalidArgsException &e)
176             {
177                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: " << e.what() << "]"
178                           << std::endl;
179             }
180             catch (SimulatorException &e)
181             {
182                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " << e.what() << "]" <<
183                           std::endl;
184             }
185         }
186
187         void displayResource()
188         {
189             displayResource(selectResource());
190         }
191
192         void displayResource(SimulatorRemoteResourceSP resource)
193         {
194             if (!resource) return;
195
196             std::cout << "#############################" << std::endl;
197             std::cout << "URI: " << resource->getURI().c_str() << std::endl;
198             std::cout << "Host: " << resource->getHost().c_str() << std::endl;
199             std::cout << "ID: " << resource->getID().c_str() << std::endl;
200             std::cout << "Resource Types: ";
201             for (auto & type : resource->getResourceTypes())
202                 std::cout << type << " ";
203             std::cout << "\nInterface Types: ";
204             for (auto & type : resource->getResourceInterfaces())
205                 std::cout << type << " ";
206             std::cout << std::boolalpha << "\nisObservable : " << resource->isObservable()
207                     << std::noboolalpha << std::endl;
208             std::cout << "#############################" << std::endl;
209         }
210
211         void observeResource()
212         {
213             SimulatorRemoteResourceSP resource = selectResource();
214             if (!resource) return;
215
216             // callback implementaion
217             SimulatorRemoteResource::ObserveNotificationCallback callback =
218                 [](std::string uid, SimulatorResult errorCode, SimulatorResourceModelSP rep, int seq)
219             {
220                 std::cout << "\nObserve notification received ###[errorcode:  " << errorCode <<
221                     " seq:  " << seq << "UID: " << uid << "]" << std::endl;
222
223                 std::cout << "Representation is: " << std::endl;
224                 std::cout << rep->toString() << std::endl;
225             };
226
227             try
228             {
229                 resource->observe(ObserveType::OBSERVE, callback);
230                 std::cout << "Observe is successful!" << std::endl;
231             }
232             catch (InvalidArgsException &e)
233             {
234                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
235                         << e.what() << "]" << std::endl;
236             }
237             catch (SimulatorException &e)
238             {
239                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
240                         e.what() << "]" << std::endl;
241             }
242         }
243
244         void cancelObserving()
245         {
246             SimulatorRemoteResourceSP resource = selectResource();
247             if (!resource) return;
248
249             try
250             {
251                 resource->cancelObserve();
252                 std::cout << "Cancelling observe is successful!" << std::endl;
253             }
254             catch (SimulatorException &e)
255             {
256                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
257                         e.what() << "]" << std::endl;
258             }
259         }
260
261         void sendGet()
262         {
263             SimulatorRemoteResourceSP resource = selectResource();
264             if (!resource) return;
265
266             // callback implementaion
267             SimulatorRemoteResource::ResponseCallback callback =
268                 [](std::string uId, SimulatorResult errorCode, SimulatorResourceModelSP rep)
269             {
270                 std::cout << "\nGET Response received ### [errorcode:  " << errorCode << "]"
271                         << std::endl;
272                 std::cout << "UID is: " << uId << std::endl;
273                 std::cout << "Representation is: " << std::endl;
274                 std::cout << rep->toString() << std::endl;
275             };
276
277             try
278             {
279                 resource->get(std::map <std::string, std::string>(), callback);
280                 std::cout << "GET is successful!" << std::endl;
281             }
282             catch (InvalidArgsException &e)
283             {
284                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
285                         << e.what() << "]" << std::endl;
286             }
287             catch (NoSupportException &e)
288             {
289                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
290                         e.what() << "]" << std::endl;
291             }
292             catch (SimulatorException &e)
293             {
294                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
295                         e.what() << "]" << std::endl;
296             }
297         }
298
299         void sendPut()
300         {
301             SimulatorRemoteResourceSP resource = selectResource();
302             if (!resource) return;
303
304             // callback implementaion
305             SimulatorRemoteResource::ResponseCallback callback =
306                 [](std::string uId, SimulatorResult errorCode, SimulatorResourceModelSP rep)
307             {
308                 std::cout << "\nPUT Response received ![errorcode:  " << errorCode << "]"
309                         << std::endl;
310                 std::cout << "UID is: " << uId << std::endl;
311                 std::cout << "Representation is: " << std::endl;
312                 std::cout << rep->toString() << std::endl;
313             };
314
315             try
316             {
317                 SimulatorResourceModelSP rep = std::make_shared<SimulatorResourceModel>();
318                 bool value = false;
319                 rep->add("power", value);
320                 rep->add("intensity", 15);
321
322                 resource->put(std::map <std::string, std::string>(), rep, callback);
323                 std::cout << "PUT is successful!" << std::endl;
324             }
325             catch (InvalidArgsException &e)
326             {
327                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
328                         << e.what() << "]" << std::endl;
329             }
330             catch (NoSupportException &e)
331             {
332                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
333                         e.what() << "]" << std::endl;
334             }
335             catch (SimulatorException &e)
336             {
337                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
338                         e.what() << "]" << std::endl;
339             }
340         }
341
342         void sendPost()
343         {
344             SimulatorRemoteResourceSP resource = selectResource();
345             if (!resource) return;
346
347             // callback implementaion
348             SimulatorRemoteResource::ResponseCallback callback =
349                 [](std::string uId, SimulatorResult errorCode, SimulatorResourceModelSP rep)
350             {
351                 std::cout << "\nPOST Response received ![errorcode:  " << errorCode << "]"
352                         << std::endl;
353                 std::cout << "UID is: " << uId << std::endl;
354                 std::cout << "Representation is: " << std::endl;
355                 std::cout << rep->toString() << std::endl;
356             };
357
358             try
359             {
360                 SimulatorResourceModelSP rep = std::make_shared<SimulatorResourceModel>();
361                 bool value = true;
362                 rep->add("power", value);
363                 rep->add("intensity", 17);
364
365                 resource->post(std::map <std::string, std::string>(), rep, callback);
366                 std::cout << "POST is successful!" << std::endl;
367             }
368             catch (InvalidArgsException &e)
369             {
370                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
371                         << e.what() << "]" << std::endl;
372             }
373             catch (NoSupportException &e)
374             {
375                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
376                         e.what() << "]" << std::endl;
377             }
378             catch (SimulatorException &e)
379             {
380                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
381                         e.what() << "]" << std::endl;
382             }
383         }
384
385         void sendAllGETRequests()
386         {
387             SimulatorRemoteResourceSP resource = selectResource();
388             if (!resource) return;
389
390             SimulatorRemoteResource::StateCallback callback = [] (std::string uid, int sessionId,
391                     OperationState state)
392             {
393                 std::cout << "\nResource verification status received ![id:  " << sessionId <<
394                         "  State: " << getOperationStateString(state) << " UID: " << uid << "]" <<
395                         std::endl;
396             };
397
398             try
399             {
400                 int id = resource->startVerification(RequestType::RQ_TYPE_GET, callback);
401                 std::cout << "startVerification for GET is successful!id: " << id << std::endl;
402             }
403             catch (InvalidArgsException &e)
404             {
405                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
406                         << e.what() << "]" << std::endl;
407             }
408             catch (NoSupportException &e)
409             {
410                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
411                         e.what() << "]" << std::endl;
412             }
413             catch (SimulatorException &e)
414             {
415                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
416                         e.what() << "]" << std::endl;
417             }
418         }
419
420         void sendAllPUTRequests()
421         {
422             SimulatorRemoteResourceSP resource = selectResource();
423             if (!resource) return;
424
425             SimulatorRemoteResource::StateCallback callback = [] (std::string uid, int sessionId,
426                     OperationState state)
427             {
428                 std::cout << "\nResource verification status received ![id:  " << sessionId <<
429                         "  State: " << getOperationStateString(state) << " UID: " << uid << "]" <<
430                         std::endl;
431             };
432
433             try
434             {
435                 int id = resource->startVerification(RequestType::RQ_TYPE_PUT, callback);
436                 std::cout << "startVerification for PUT is successful!id: " << id << std::endl;
437             }
438             catch (InvalidArgsException &e)
439             {
440                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
441                         << e.what() << "]" << std::endl;
442             }
443             catch (NoSupportException &e)
444             {
445                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
446                         e.what() << "]" << std::endl;
447             }
448             catch (SimulatorException &e)
449             {
450                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
451                         e.what() << "]" << std::endl;
452             }
453         }
454
455         void sendAllPOSTRequests()
456         {
457             SimulatorRemoteResourceSP resource = selectResource();
458             if (!resource) return;
459
460             SimulatorRemoteResource::StateCallback callback = [] (std::string uid, int sessionId,
461                     OperationState state)
462             {
463                 std::cout << "\nResource verification status received ![id:  " << sessionId <<
464                         "  State: " << getOperationStateString(state) << " UID: " << uid << "]"
465                         << std::endl;
466             };
467
468             try
469             {
470                 int id = resource->startVerification(RequestType::RQ_TYPE_POST, callback);
471                 std::cout << "startVerification for POST is successful!id: " << id << std::endl;
472             }
473             catch (InvalidArgsException &e)
474             {
475                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
476                         << e.what() << "]" << std::endl;
477             }
478             catch (NoSupportException &e)
479             {
480                 std::cout << "NoSupportException occured [code : " << e.code() << " Detail: " <<
481                         e.what() << "]" << std::endl;
482             }
483             catch (SimulatorException &e)
484             {
485                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
486                         e.what() << "]" << std::endl;
487             }
488         }
489
490         void configure()
491         {
492             SimulatorRemoteResourceSP resource = selectResource();
493             if (!resource)
494                 return;
495
496             try
497             {
498                 std::string configPath;
499                 std::cout << "Enter the config path: ";
500                 std::cin >> configPath;
501
502                 SimulatorResourceModelSP representation = resource->configure(configPath);
503                 if (representation)
504                 {
505                     std::cout << "configuration is successful!" << std::endl;
506                     std::map<std::string, SimulatorResourceModel::Attribute> attributes =
507                     representation->getAttributes();
508                     std::cout << "##### Attributes [" << attributes.size() << "]" << std::endl;
509                     for (auto & attribute : attributes)
510                     {
511                         std::cout << (attribute.second).getName() << " :  {" << std::endl;
512                         std::cout << "value: " << (attribute.second).toString() << std::endl;
513                         SimulatorResourceModel::AttributeProperty prop = (attribute.second).getProperty();
514                         std::cout << "Supported values given by : " << getPropertyTypeString(prop.type()) << std::endl;
515                         if (SimulatorResourceModel::AttributeProperty::Type::RANGE == prop.type())
516                         {
517                             std::cout << "Min: " << prop.min() << std::endl;
518                             std::cout << "Max: " << prop.max() << std::endl;
519                         }
520                         else if (SimulatorResourceModel::AttributeProperty::Type::VALUE_SET == prop.type())
521                         {
522                             std::cout << "Value set: " << prop.valueSetToString() << std::endl;
523                         }
524
525                         std::cout << "}" << std::endl << std::endl;
526                     }
527                     std::cout << "#############################" << std::endl;
528                 }
529             }
530             catch (InvalidArgsException &e)
531             {
532                 std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: "
533                         << e.what() << "]" << std::endl;
534             }
535             catch (SimulatorException &e)
536             {
537                 std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " <<
538                         e.what() << "]" << std::endl;
539             }
540         }
541
542     private:
543         std::recursive_mutex m_mutex;
544         std::map<std::string, SimulatorRemoteResourceSP> m_resList;
545 };
546
547 void printMainMenu()
548 {
549     std::cout << "############### MAIN MENU###############" << std::endl;
550     std::cout << "1. Client Controller Test" << std::endl;
551     std::cout << "2. Get device information" << std::endl;
552     std::cout << "3. Get platform information" << std::endl;
553     std::cout << "4. Set Logger" << std::endl;
554     std::cout << "5. Help" << std::endl;
555     std::cout << "0. Exit" << std::endl;
556     std::cout << "######################################" << std::endl;
557 }
558
559 void setLogger()
560 {
561     std::cout << "1. Default console logger" << std::endl;
562     std::cout << "2. Default file logger" << std::endl;
563     std::cout << "3. custom logger" << std::endl;
564
565     int choice = -1;
566     std::cin >> choice;
567     if (choice <= 0 || choice > 3)
568     {
569         std::cout << "Invalid selection !" << std::endl;
570         return;
571     }
572
573     switch (choice)
574     {
575         case 1:
576             {
577                 if (false == SimulatorManager::getInstance()->setConsoleLogger())
578                     std::cout << "Failed to set the default console logger" << std::endl;
579             }
580             break;
581
582         case 2:
583             {
584                 std::string filePath;
585                 std::cout << "Enter the file path (without file name) : ";
586                 std::cin >> filePath;
587                 if (false == SimulatorManager::getInstance()->setFileLogger(filePath))
588                     std::cout << "Failed to set default file logger" << std::endl;
589             }
590             break;
591
592         case 3:
593             SimulatorManager::getInstance()->setLogger(gAppLogger);
594     }
595 }
596
597 int main(void)
598 {
599     ClientController clientController;
600     printMainMenu();
601     bool cont = true;
602     while (cont == true)
603     {
604         int choice = -1;
605         std::cout << "Enter your choice: ";
606         std::cin >> choice;
607         if (choice < 0 || choice > 5)
608         {
609             std::cout << "Invaild choice !" << std::endl; continue;
610         }
611
612         switch (choice)
613         {
614             case 1: clientController.startTest();
615                 std::cout << "Welcome back to main menu !" << std::endl;
616                 break;
617
618             case 2:
619                 {
620                     try
621                     {
622                         SimulatorManager::getInstance()->getDeviceInfo(std::bind([](DeviceInfo & deviceInfo)
623                         {
624                             std::cout << "###Device Information received...." << std::endl;
625                             std::ostringstream out;
626                             out << "Device name: " << deviceInfo.getName() << std::endl;
627                             out << "Device ID: " << deviceInfo.getID() << std::endl;
628                             out << "Device Spec version: " << deviceInfo.getSpecVersion() << std::endl;
629                             out << "Device dat model version: " << deviceInfo.getDataModelVersion() << std::endl;
630
631                             std::cout << out.str() << std::endl;
632                         }, std::placeholders::_1));
633                     }
634                     catch (InvalidArgsException &e)
635                     {
636                         std::cout << "InvalidArgsException occured [code : " << e.code() << " Detail: " << e.what() << "]"
637                                   << std::endl;
638                     }
639                     catch (SimulatorException &e)
640                     {
641                         std::cout << "SimulatorException occured [code : " << e.code() << " Detail: " << e.what() << "]" <<
642                                   std::endl;
643                     }
644                 }
645                 break;
646
647             case 3:
648                 {
649                     try
650                     {
651                         SimulatorManager::getInstance()->getPlatformInfo(std::bind([](PlatformInfo & platformInfo)
652                         {
653                             std::cout << "###Platform Information received...." << std::endl;
654                             std::ostringstream out;
655                             out << "Platform ID: " << platformInfo.getPlatformID() << std::endl;
656                             out << "Platform version: " << platformInfo.getPlatformVersion() << std::endl;
657                             out << "Manufacturer name: " << platformInfo.getManufacturerName() << std::endl;
658                             out << "Manufacturer url: " << platformInfo.getManufacturerUrl() << std::endl;
659                             out << "Modle number: " << platformInfo.getModelNumber() << std::endl;
660                             out << "Date of manufacture: " << platformInfo.getDateOfManfacture() << std::endl;
661                             out << "Operatio system version: " << platformInfo.getOSVersion() << std::endl;
662                             out << "Hardware version: " << platformInfo.getHardwareVersion() << std::endl;
663                             out << "Firmware version: " << platformInfo.getFirmwareVersion() << std::endl;
664                             out << "Support url: " << platformInfo.getSupportUrl() << std::endl;
665                             out << "System time: " << platformInfo.getSystemTime() << std::endl;
666
667                             std::cout << out.str() << std::endl;
668                         }, std::placeholders::_1));
669                     }
670                     catch (InvalidArgsException &e)
671                     {
672                         std::cout << "InvalidArgsException occured [code : " << e.code()
673                                 << " Detail: " << e.what() << "]" << std::endl;
674                     }
675                     catch (SimulatorException &e)
676                     {
677                         std::cout << "SimulatorException occured [code : " << e.code()
678                                 << " Detail: " << e.what() << "]" << std::endl;
679                     }
680                 }
681                 break;
682
683             case 4: setLogger(); break;
684
685             case 5: printMainMenu(); break;
686
687             case 0: cont = false;
688         }
689     }
690
691     std::cout << "Terminating test !!!" << std::endl;
692 }