Merge test folder of test branch
[iotivity.git] / test / src / iBtestapp / modules / ic / c_cpp / src / CloudTestAppMain.cpp
1 /******************************************************************
2  *
3  * Copyright 2018 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
22 #include <CloudHelper.h>
23
24 string m_hostAddress = "";
25 string g_authProvider = "";
26 string g_authCode = "";
27 string g_groupId = "";
28 int g_userInput = -1;
29 int g_apptype = NONE;
30 bool g_value = false;
31 bool g_controleeOn = true;
32 bool g_controllerOn = true;
33 string g_devIDcontrolee = "";
34 string g_cmd = "";
35 string g_AccTokcontrolee = "";
36 string g_authTokenControlee = "";
37 string g_refreshAuthTokenControlee = "";
38 string g_devIDcontroller = "";
39 string g_authTokenController = "";
40 string g_refreshAuthTokenController = "";
41 OCAccountManager::Ptr g_accountMgrControlee = nullptr;
42 OCAccountManager::Ptr g_accountMgrController = nullptr;
43 OCPlatform::OCPresenceHandle g_presenceHandle = nullptr;
44 OC::OCResource::Ptr g_mqBrokerResourcePublisher = nullptr;
45 OC::OCResource::Ptr g_mqBrokerResourceSubscriber = nullptr;
46 OC::OCResource::Ptr g_mqSelectedTopicResource = nullptr;
47 QualityOfService g_qos = QualityOfService::LowQos;
48 std::vector< std::string > g_deviceIdAddded;
49 std::vector< std::string > g_deviceIdRemoving;
50 QueryParamsMap g_Query;
51 string g_Key;
52 string g_Value;
53 string g_AcceptInvitaition;
54 OCRepresentation g_PropertyValue;
55 vector< string > g_Values;
56 string g_URI;
57 string g_TopicType;
58 bool g_IsControlleeSignUp = false;
59 bool g_IsControllerSignUp = false;
60
61 /*
62  * UI Related
63  */
64 typedef enum
65 {
66     EXIT,
67     START_CONTROLEE = 101,
68     SIGN_UP_CONTROLEE,
69     SIGN_IN_CONTROLEE,
70     SIGN_OUT_CONTROLEE,
71     CREATE_GROUP,
72     CREATE_GROUP_WITH_QUERY,
73
74     SEND_INVITATION,
75
76     PUBLISH_RESOURCE_TO_RD,
77     TOGGLE_RESOURCE_ATTRIBUTE,
78
79     START_CONTROLLER = 201,
80     SIGN_UP_CONTROLLER,
81     SIGN_IN_CONTROLLER,
82     SIGN_OUT_CONTROLLER,
83     SEARCH_USER_UUID,
84     SEARCH_USER_PHONE,
85     SEARCH_USER_EMAIL,
86     DELETE_DEVICE,
87     DELETE_GROUP,
88     GET_GROUP_INFO_ALL,
89     GET_GROUP_INFO,
90
91     OBSERVE_GROUP,
92     CANCEL_OBSERVE_GROUP,
93     OBSERVE_INVITATION,
94     CANCEL_OBSERVE_INVITATION,
95     CANCEL_INVITATION,
96     DELETE_INVITATION,
97     FIND_RESOURCE_WITH_QUERY,
98     FIND_RESOURCE,
99     TOGGLE_RESOURCE_CONTROLLER,
100     ADD_PROPERTY_VALUE,
101     DELETE_PROPERTY_VALUE,
102     UPDATE_PROPERTY_VALUE,
103     DELETE_RESOURCE,
104     OBSERVE_RESOURCE,
105     CANCEL_OBSERVE_RESOURCE,
106
107     CREATE_TOPIC = 301,
108     CREATE_TYPED_TOPIC,
109     DISCOVER_ALL_TOPIC_FOR_PUBLISH,
110     DISCOVER_TOPIC_TYPE_FOR_PUBLISH,
111     SELECT_TOPIC_INDEX,
112     PUBLISH_TOPIC_DATA,
113
114     DISCOVER_ALL_TOPIC = 401,
115     DISCOVER_TOPIC_TYPE,
116     SELECT_TOPIC,
117     SUBSCRIBE_TOPIC,
118     UNSUBSCRIBE_TOPIC
119
120 } menuCloud;
121
122 static void printMenu(void)
123 {
124     cout << "====================Controlee===========================" << endl;
125     cout << START_CONTROLEE << ". Start Controlee" << endl;
126     cout << SIGN_UP_CONTROLEE << ". Sign Up With Controlee" << endl;
127     cout << SIGN_IN_CONTROLEE << ". Sign In to Cloud" << endl;
128     cout << SIGN_OUT_CONTROLEE << ". Sign Out from Cloud" << endl;
129
130     cout << CREATE_GROUP << ". Create Group" << endl;
131     cout << CREATE_GROUP_WITH_QUERY << ". Create Group with query" << endl;
132
133     cout << SEND_INVITATION << ". Send Invitation" << endl;
134
135     cout << PUBLISH_RESOURCE_TO_RD << ". Publish Resource" << endl;
136     cout << TOGGLE_RESOURCE_ATTRIBUTE << ". Toggle Resource Attribute" << endl; //feature not implemented
137
138     cout << "====================Controller===========================" << endl;
139     cout << START_CONTROLLER << ". Start Controller" << endl;
140     cout << SIGN_UP_CONTROLLER << ". Sign Up With Controller" << endl;
141     cout << SIGN_IN_CONTROLLER << ". Sign In With Controller" << endl;
142     cout << SIGN_OUT_CONTROLLER << ". Sign Out from Cloud" << endl;
143     cout << SEARCH_USER_UUID << ". Search User Information With DEV UUID" << endl;
144     cout << SEARCH_USER_PHONE << ". Search User Information With Phone number" << endl;
145     cout << SEARCH_USER_EMAIL << ". Search User Information With Email Address" << endl;
146     cout << DELETE_DEVICE << ". Delete Device" << endl;
147     cout << DELETE_GROUP << ". Delete Group" << endl;
148     cout << GET_GROUP_INFO_ALL << ". Get Group Info ALL" << endl;
149     cout << GET_GROUP_INFO << ". Get Group Info" << endl;
150
151     cout << OBSERVE_GROUP << ". Observe Group" << endl;
152     cout << CANCEL_OBSERVE_GROUP << ". Cancel Observe Group" << endl;
153     cout << OBSERVE_INVITATION << ". Observe Invitation" << endl;
154     cout << CANCEL_OBSERVE_INVITATION << ". Cancel Observe Invitation" << endl;
155     cout << CANCEL_INVITATION << ". Cancel Invitation" << endl;
156     cout << DELETE_INVITATION << ". Delete Invitation" << endl;
157
158     cout << FIND_RESOURCE_WITH_QUERY << ". Find  Resource with query" << endl;
159     cout << FIND_RESOURCE << ". Find  Resource without query" << endl;
160     cout << TOGGLE_RESOURCE_CONTROLLER << ". Toggle Resource Attribute from controller" << endl;
161     cout << ADD_PROPERTY_VALUE << ". Add property value to group" << endl;
162     cout << DELETE_PROPERTY_VALUE << ". Delete property value to group " << endl;
163     cout << UPDATE_PROPERTY_VALUE << ". Update property value to group " << endl;
164     cout << DELETE_RESOURCE << ". Send DELETE Request to resource" << endl;
165     cout << OBSERVE_RESOURCE << ". Send OBSERVE Request to resource" << endl;
166     cout << CANCEL_OBSERVE_RESOURCE << ". OBSERVE Cancel Request to resource" << endl;
167
168     cout << "====================MQ publisher===========================" << endl;
169     cout << CREATE_TOPIC << ". create topic" << endl;
170     cout << CREATE_TYPED_TOPIC << ". create type based topic" << endl;
171     cout << DISCOVER_ALL_TOPIC_FOR_PUBLISH << ". Discover all topics" << endl;
172     cout << DISCOVER_TOPIC_TYPE_FOR_PUBLISH << ". Discover type based topics" << endl;
173     cout << SELECT_TOPIC_INDEX << ". Select topic index for publishing data" << endl;
174     cout << PUBLISH_TOPIC_DATA << ". Publish data to selected topic" << endl;
175
176     cout << "====================MQ Subscriber===========================" << endl;
177     cout << DISCOVER_ALL_TOPIC << ". Discover all topics" << endl;
178     cout << DISCOVER_TOPIC_TYPE << ". Discover type based topics" << endl;
179     cout << SELECT_TOPIC << ". Select topic for subscribing or unsubscribing" << endl;
180     cout << SUBSCRIBE_TOPIC << ". Subscribe to selected topic" << endl;
181     cout << UNSUBSCRIBE_TOPIC << ". Unsubscribe to selected topic" << endl;
182
183     cout << EXIT << ". Exit" << endl;
184     printf("Enter Input: ");
185 }
186
187 int checkAppStatus(int expectedType)
188 {
189     if (expectedType != g_apptype && expectedType == CONTROLEE)
190     {
191         cout << "App is not initialized as Controlee" << endl;
192         return 1;
193     }
194     if (expectedType != g_apptype && expectedType == CONTROLLER)
195     {
196         cout << "App is not initialized as Controller" << endl;
197         return 1;
198     }
199     return 0;
200 }
201
202 void doAction(int userInput)
203 {
204     OCRepresentation rep;
205     if (g_IsControlleeSignUp || g_IsControllerSignUp || userInput == START_CONTROLEE
206             || userInput == SIGN_UP_CONTROLEE || userInput == SIGN_IN_CONTROLEE
207             || userInput == START_CONTROLLER || userInput == SIGN_UP_CONTROLLER
208             || userInput == SIGN_IN_CONTROLLER)
209     {
210         switch (userInput)
211         {
212             case START_CONTROLEE:
213             {
214                 //initCloudClient();
215                 OCPersistentStorage psControlee
216                 { controleeOpen, fread, fwrite, fclose, unlink };
217                 PlatformConfig cfg
218                 { ServiceType::InProc, ModeType::Both, CT_ADAPTER_TCP, CT_ADAPTER_TCP,
219                         QualityOfService::LowQos, &psControlee };
220
221                 OCPlatform::Configure(cfg);
222                 g_accountMgrControlee = OCPlatform::constructAccountManagerObject(m_hostAddress,
223                         CT_ADAPTER_TCP);
224                 break;
225             }
226             case SIGN_UP_CONTROLEE:
227             {
228                 cout << "signUp start......" << endl;
229                 CloudCommonUtil::signUp(g_accountMgrControlee);
230                 g_IsControlleeSignUp = true;
231                 break;
232             }
233             case SIGN_IN_CONTROLEE:
234             {
235                 CloudCommonUtil::signIn(g_accountMgrControlee);
236                 g_IsControlleeSignUp = true;
237                 break;
238             }
239             case SIGN_OUT_CONTROLEE:
240             {
241                 CloudCommonUtil::signOut(g_accountMgrControlee);
242                 g_IsControlleeSignUp = false;
243                 break;
244             }
245
246             case CREATE_GROUP:
247             {
248                 //query.insert(pair< string, string >(NULL, NULL));
249                 createGroup(g_accountMgrControlee, g_Query, onCreateGroup);
250                 break;
251             }
252             case CREATE_GROUP_WITH_QUERY:
253             {
254                 cout << "query key(ex: gname/parent): ";
255                 cin >> g_Key;
256                 cout << "query value: ";
257                 cin >> g_Value;
258                 g_Query.insert(pair< string, string >(g_Key, g_Value));
259                 createGroup(g_accountMgrControlee, g_Query, onCreateGroup);
260                 break;
261             }
262
263             case SEND_INVITATION:
264             {
265                 cout << "Enter Group ID to invite : ";
266                 cin >> g_groupId;
267                 cout << "Enter UUID to invite : ";
268                 cin >> g_devIDcontrolee;
269                 sendInvitation(g_accountMgrControlee, g_groupId, g_devIDcontrolee,
270                         onSendInvitation);
271                 break;
272             }
273
274             case PUBLISH_RESOURCE_TO_RD:
275             {
276                 BinarySwitchResource binarySwitch(SWITCH_RES_URI,
277                 { SWITCH_RES_TYPE },
278                 { DEFAULT_INTERFACE });
279                 g_URI = binarySwitch.getResourceUri();
280                 string rt = binarySwitch.getResourceType()[0];
281                 string itf = binarySwitch.getInterfaces()[0];
282                 int result = OCPlatform::registerResource(binarySwitch.m_handle, g_URI, rt, itf,
283                         std::bind(&BinarySwitchResource::entityHandler, &binarySwitch,
284                                 std::placeholders::_1), OC_DISCOVERABLE);
285                 if (result != OC_STACK_OK)
286                 {
287                     cout << "Resource registration was unsuccessful" << endl;
288                 }
289
290                 ResourceHandles resourceHandles;
291                 OCDeviceInfo devInfoAirConditioner;
292                 OCStringLL deviceType;
293                 deviceType.value = DEVICE_TYPE_VALUE;
294                 deviceType.next = NULL;
295                 devInfoAirConditioner.deviceName = RES_DEVICE_NAME;
296                 devInfoAirConditioner.types = &deviceType;
297                 devInfoAirConditioner.specVersion = NULL;
298                 devInfoAirConditioner.dataModelVersions = NULL;
299                 OCPlatform::registerDeviceInfo(devInfoAirConditioner);
300                 resourceHandles.push_back(binarySwitch.m_handle);
301                 publishResourceToRD(m_hostAddress, OCConnectivityType::CT_ADAPTER_TCP,
302                         resourceHandles, &onPublish);
303                 break;
304             }
305             case TOGGLE_RESOURCE_ATTRIBUTE:
306             {
307                 OCRepresentation rep;
308                 if (g_controleeOn)
309                 {
310                     rep.setValue(string("value"), false);
311                     BinarySwitchResource binarySwitch("/power/0",
312                     { "oic.r.switch.binary" },
313                     { DEFAULT_INTERFACE });
314                     binarySwitch.setBinarySwitchRepresentation(rep);
315                 }
316                 else
317                 {
318                     rep.setValue(string("value"), true);
319                     BinarySwitchResource binarySwitch("/power/0",
320                     { "oic.r.switch.binary" },
321                     { DEFAULT_INTERFACE });
322                     binarySwitch.setBinarySwitchRepresentation(rep);
323                 }
324                 break;
325             }
326             case START_CONTROLLER:
327             {
328
329                 OCPersistentStorage psController
330                 { controllerOpen, fread, fwrite, fclose, unlink };
331
332                 PlatformConfig cfgController
333                 { ServiceType::InProc, ModeType::Both, CT_ADAPTER_TCP, CT_ADAPTER_TCP,
334                         QualityOfService::LowQos, &psController };
335
336                 OCPlatform::Configure(cfgController);
337                 g_accountMgrController = OCPlatform::constructAccountManagerObject(m_hostAddress,
338                         CT_ADAPTER_TCP);
339                 break;
340             }
341             case SIGN_UP_CONTROLLER:
342             {
343                 CloudCommonUtil::signUp(g_accountMgrController);
344                 g_IsControllerSignUp = true;
345                 break;
346             }
347             case SIGN_IN_CONTROLLER:
348             {
349                 CloudCommonUtil::signIn(g_accountMgrController);
350                 g_IsControllerSignUp = true;
351                 break;
352             }
353             case SIGN_OUT_CONTROLLER:
354             {
355                 CloudCommonUtil::signOut(g_accountMgrController);
356                 g_IsControllerSignUp = false;
357                 break;
358             }
359             case SEARCH_USER_UUID:
360             {
361                 cout << "Enter UserUUID to search : ";
362                 cin >> g_cmd;
363                 g_Query.clear();
364                 g_Query["uid"] = g_cmd;
365                 searchUser(g_accountMgrController, g_Query, onSearchUser);
366                 break;
367             }
368             case SEARCH_USER_PHONE:
369             {
370                 cout << "Enter phone number to search : ";
371                 cin >> g_cmd;
372                 g_Query.clear();
373                 g_Query["phone"] = g_cmd;
374                 searchUser(g_accountMgrController, g_Query, onSearchUser);
375                 break;
376             }
377             case SEARCH_USER_EMAIL:
378             {
379                 cout << "Enter Email ID to search : ";
380                 cin >> g_cmd;
381                 g_Query.clear();
382                 g_Query["email"] = g_cmd;
383                 searchUser(g_accountMgrController, g_Query, onSearchUser);
384                 break;
385             }
386             case DELETE_DEVICE:
387             {
388                 cout << "Enter Device ID : ";
389                 cin >> g_devIDcontrolee;
390                 cout << "Enter AccessToken : ";
391                 cin >> g_AccTokcontrolee;
392                 deleteDevice(g_accountMgrController, g_devIDcontrolee, g_AccTokcontrolee,
393                         onDeleteDevice);
394                 break;
395             }
396             case GET_GROUP_INFO_ALL:
397             {
398                 g_groupId = "";
399                 getGroupInfo(g_accountMgrController, g_groupId, onGetGroupInfo);
400                 break;
401             }
402             case GET_GROUP_INFO:
403             {
404                 cout << "Enter Group ID : ";
405                 cin >> g_groupId;
406                 getGroupInfo(g_accountMgrController, g_groupId, onGetGroupInfo);
407                 break;
408             }
409             case DELETE_GROUP:
410             {
411                 cout << "Enter Group ID : ";
412                 cin >> g_groupId;
413                 deleteGroup(g_accountMgrController, g_groupId, onDeleteGroup);
414                 break;
415             }
416             case OBSERVE_GROUP:
417             {
418                 cout << "Enter Group ID : ";
419                 cin >> g_groupId;
420                 observeGroup(g_accountMgrController, onObserveGroup);
421                 break;
422             }
423             case CANCEL_OBSERVE_GROUP:
424             {
425                 cout << "Enter Group ID : ";
426                 cin >> g_groupId;
427                 cancelObserveGroup(g_accountMgrController);
428                 break;
429             }
430             case OBSERVE_INVITATION:
431             {
432                 observeInvitation(g_accountMgrController, onObserveInvitation);
433                 break;
434             }
435             case CANCEL_OBSERVE_INVITATION:
436             {
437                 cancelObserveInvitation(g_accountMgrController);
438                 break;
439             }
440             case CANCEL_INVITATION:
441             {
442                 cout << "Enter Group ID to cancel invitation : ";
443                 cin >> g_groupId;
444                 cout << "Enter Device ID to cancel invitation : ";
445                 cin >> g_devIDcontrolee;
446                 cancelInvitation(g_accountMgrController, g_groupId, g_devIDcontrolee,
447                         onCancelInvitation);
448                 break;
449             }
450             case DELETE_INVITATION:
451             {
452                 cout << "Enter Group ID to reply and then delete invitation: ";
453                 cin >> g_groupId;
454                 cout << "accept to invitation? (Enter 1 for accept invitation)";
455                 cin >> g_AcceptInvitaition;
456                 replyToInvitation(g_accountMgrController, g_groupId, g_AcceptInvitaition,
457                         onDeleteInvitation);
458                 break;
459             }
460             case FIND_RESOURCE_WITH_QUERY:
461             {
462                 g_URI = QUERY_FOR_SWITCH;
463                 cout << "insert Query : (For example: oic/ps/)";
464                 cin >> g_cmd;
465                 g_URI += "?";
466                 g_URI += g_cmd;
467                 findResource(m_hostAddress, g_URI,
468                         static_cast< OCConnectivityType >(CT_ADAPTER_TCP | CT_IP_USE_V4),
469                         foundDevice);
470                 break;
471             }
472             case FIND_RESOURCE:
473             {
474                 findResource(m_hostAddress, QUERY_FOR_SWITCH,
475                         static_cast< OCConnectivityType >(CT_ADAPTER_TCP | CT_IP_USE_V4),
476                         foundDevice);
477                 break;
478             }
479             case TOGGLE_RESOURCE_CONTROLLER:
480             {
481                 if (g_controllerOn)
482                 {
483                     turnOnOffSwitch(false);
484                 }
485                 else
486                 {
487                     turnOnOffSwitch(true);
488                 }
489                 break;
490             }
491             case UPDATE_PROPERTY_VALUE:
492             {
493                 OCRepresentation propertyValue;
494                 cout << "Enter group ID: ";
495                 cin >> g_groupId;
496                 cout << "property(ex: gname/owner): ";
497                 cin >> g_Key;
498                 cout << "\tvalue: ";
499                 cin >> g_Value;
500                 propertyValue.setValue< string >(g_Key, g_Value);
501                 updatePropertyValueOnGroup(g_accountMgrController, g_groupId, propertyValue,
502                         &onPost);
503                 break;
504             }
505             case ADD_PROPERTY_VALUE:
506             {
507                 cout << "Enter group ID: ";
508                 cin >> g_groupId;
509                 cout << "property(ex: members/devices): ";
510                 cin >> g_Key;
511                 cout << "\tvalue: ";
512                 cin >> g_Value;
513                 g_Values.push_back(g_Value);
514                 g_PropertyValue.setValue< vector< string > >(g_Key, g_Values);
515                 addPropertyValueToGroup(g_accountMgrController, g_groupId, g_PropertyValue,
516                         &onPost);
517                 break;
518             }
519             case DELETE_PROPERTY_VALUE:
520             {
521                 cout << "Enter group ID: ";
522                 cin >> g_groupId;
523                 cout << "property(ex: members/devices): ";
524                 cin >> g_Key;
525                 cout << "\tvalue: ";
526                 cin >> g_Value;
527                 g_Values.push_back(g_Value);
528                 g_PropertyValue.setValue< vector< string > >(g_Key, g_Values);
529                 deletePropertyValueFromGroup(g_accountMgrController, g_groupId, g_PropertyValue,
530                         &onPost);
531                 break;
532             }
533             case DELETE_RESOURCE:
534             {
535                 if (getFoundResource())
536                 {
537                     getFoundResource()->deleteResource(onDelete, g_qos);
538                 }
539                 else
540                 {
541                     cout << "No Resource found yet" << endl;
542                 }
543                 break;
544             }
545             case OBSERVE_RESOURCE:
546             {
547                 if (getFoundResource())
548                 {
549                     getFoundResource()->observe(ObserveType::Observe, QueryParamsMap(), &onObserve,
550                             g_qos);
551                 }
552                 else
553                 {
554                     cout << "No Resource found yet" << endl;
555                 }
556                 break;
557             }
558             case CANCEL_OBSERVE_RESOURCE:
559             {
560                 if (getFoundResource())
561                 {
562                     getFoundResource()->cancelObserve(g_qos);
563                 }
564                 else
565                 {
566                     cout << "No Resource found yet" << endl;
567                 }
568                 break;
569             }
570             case CREATE_TOPIC:
571             {
572                 cout << "Put topic uri to create topic: (For exp: /oic/ps/light) : ";
573                 cin >> g_cmd;
574                 createMQTopic(g_mqBrokerResourcePublisher, rep, g_cmd, g_Query, &createTopicCB,
575                         g_qos);
576                 break;
577             }
578             case CREATE_TYPED_TOPIC:
579             {
580                 cout << "Put topic uri to create topic: (For exp: /oic/ps/light) : ";
581                 cin >> g_cmd;
582                 cout << "Put topic type: (For exp: light) : ";
583                 cin >> g_TopicType;
584                 g_Query["rt"] = g_TopicType;
585                 createMQTopic(g_mqBrokerResourcePublisher, rep, g_cmd, g_Query, &createTopicCB,
586                         g_qos);
587                 break;
588             }case DISCOVER_ALL_TOPIC_FOR_PUBLISH:
589                 g_topicList.clear();
590                 g_Query.clear();
591                 cout << "Discovering topics " << endl;
592                 discoveryMQTopics(g_mqBrokerResourcePublisher, g_Query, &discoverTopicCB, g_qos);
593                 break;
594             case DISCOVER_TOPIC_TYPE_FOR_PUBLISH:
595             {
596                 g_topicList.clear();
597                 cout << "Put topic type to discover: ";
598                 cin >> g_cmd;
599                 g_Query["rt"] = g_cmd;
600                 discoveryMQTopics(g_mqBrokerResourcePublisher, g_Query, &discoverTopicCB, g_qos);
601                 break;
602             }
603             case SELECT_TOPIC_INDEX:
604             {
605                 cout << "Put discovered topic index to select: ";
606                 cin >> g_cmd;
607                 {
608                     int index = atoi(g_cmd.c_str());
609                     if (index < 0 || (unsigned int) index >= g_topicList.size())
610                     {
611                         cout << "invalid topic index selected" << endl;
612                     }
613                     g_mqSelectedTopicResource = g_topicList[index];
614                     cout << g_mqSelectedTopicResource->uri() << " selected" << endl;
615                 }
616                 break;
617             }
618             case PUBLISH_TOPIC_DATA:
619             {
620                 if (g_mqSelectedTopicResource == nullptr)
621                 {
622                     cout << "Topic is not selected." << endl;
623                 }
624                 cout << "Put message to selected topic: (For exp: Hello) : ";
625                 cin >> g_cmd;
626                 rep["samplemessage"] = g_cmd;
627                 publishMQTopic(g_mqSelectedTopicResource, rep, g_Query, &publishMessageCB, g_qos);
628                 break;
629             }
630             case DISCOVER_ALL_TOPIC:
631                 g_topicList.clear();
632                 g_Query.clear();
633                 cout << "Discovering topics" << endl;
634                 discoveryMQTopics(g_mqBrokerResourceSubscriber, g_Query, &discoverTopicCB, g_qos);
635                 break;
636             case DISCOVER_TOPIC_TYPE:
637             {
638                 g_topicList.clear();
639                 cout << "Put topic type to discover: ";
640                 cin >> g_cmd;
641                 g_Query["rt"] = g_cmd;
642                 discoveryMQTopics(g_mqBrokerResourceSubscriber, g_Query, &discoverTopicCB, g_qos);
643                 break;
644             }
645             case SELECT_TOPIC:
646             {
647                 cout << "Put discovered topic index to select: ";
648                 cin >> g_cmd;
649                 {
650                     int index = atoi(g_cmd.c_str());
651                     if (index < 0 || (unsigned int) index >= g_topicList.size())
652                     {
653                         cout << "invalid topic index selected" << endl;
654                     }
655
656                     g_mqSelectedTopicResource = g_topicList[index];
657                     cout << g_mqSelectedTopicResource->uri() << " selected" << endl;
658                 }
659                 break;
660             }
661             case SUBSCRIBE_TOPIC:
662             {
663                 if (g_mqSelectedTopicResource == nullptr)
664                 {
665                     cout << "Topic is not selected." << endl;
666                 }
667                 cout << "Subscribe to selected topic" << endl;
668                 subscribeMQTopic(g_mqSelectedTopicResource, ObserveType::Observe, g_Query,
669                         &subscribeCB, g_qos);
670                 break;
671             }
672             case UNSUBSCRIBE_TOPIC:
673             {
674                 if (g_mqSelectedTopicResource == nullptr)
675                 {
676                     cout << "Topic is not selected." << endl;
677                 }
678                 cout << "Unsubscribe to selected topic" << endl;
679                 unsubscribeMQTopic(g_mqSelectedTopicResource, g_qos);
680                 break;
681             }
682
683             default:
684                 cout << "Wrong Input, Please provide Input Again" << endl;
685                 return;
686         }
687     }
688     else
689     {
690         cout
691                 << "\033[1;31m======================Error=========================================\033[0m\n";
692         cout
693                 << "\033[1;31mYour expected feature not work without SignUp/SignIn \nPlease signUp/signIn first.\033[0m\n";
694         cout
695                 << "\033[1;31m==================================================================\033[0m\n";
696     }
697
698 }
699
700 int main()
701 {
702     m_hostAddress = CloudCommonUtil::getDefaultHostAddess();
703     cout << "Host Address : " << m_hostAddress << endl;
704     g_mqBrokerResourcePublisher = OCPlatform::constructResourceObject(m_hostAddress, DEFAULT_MQ_BROKER_URI,
705             static_cast< OCConnectivityType >(CT_ADAPTER_TCP | CT_IP_USE_V4), false,
706             { string("oic.wk.ps") },
707             { string(DEFAULT_INTERFACE) });
708     g_mqBrokerResourceSubscriber = OCPlatform::constructResourceObject(m_hostAddress,
709             DEFAULT_MQ_BROKER_URI, static_cast< OCConnectivityType >(CT_ADAPTER_TCP | CT_IP_USE_V4),
710             false,
711             { string("oic.wk.ps") },
712             { string(DEFAULT_INTERFACE) });
713     for (;;)
714     {
715         printMenu();
716         cin >> g_userInput;
717         if (!g_userInput)
718         {
719             break;
720         }
721         doAction(g_userInput);
722     }
723     return 0;
724 }