RDK Documentation (Open Sourced RDK Components)
usbtest.cpp
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
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 #include <iostream>
20 #include "usbctrl.h"
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <list>
24 
25 static std::list<int> connected_device_ids;
26 void callback(int id, int connected, void *data)
27 {
28  std::cout<<"Enter callback. Payload is "<<(char *)data<<std::endl;;
29  if(connected)
30  {
31  const char * prop = rusbCtrl_getProperty(id, "product");
32  std::cout<<"device "<<id<<" is connected\n";
33  std::cout<<"Product: "<<prop<<std::endl;
34  free((void *)prop);
35  connected_device_ids.push_back(id);
36  }
37  else
38  {
39  connected_device_ids.remove(id);
40  std::cout<<"device "<<id<<" was removed\n";
41  }
42 
43 }
44 
45 
46 
47 
48 
49 void print_menu(void)
50 {
51  std::cout<<"\n--- libusbctrl test application menu ---\n";
52  std::cout<<"1. rusbbCtrl_init()\n";
53  std::cout<<"2. rusbbCtrl_term()\n";
54  std::cout<<"3. rusbCtrl_registerCallback()\n";
55  std::cout<<"4. rusbCtrl_getProperty()\n";
56  std::cout<<"5. List hot-plugged dev_ids (not thread-safe).\n";
57  std::cout<<"9. Quit.\n";
58 }
59 
60 static std::string callback_payload = "Uninitialized";
61 void dump_connected_devices()
62 {
63  //Note: This function is not thread-safe.
64  std::cout<<"Hot-plugged device ids:\n";
65  if(0 == connected_device_ids.size())
66  {
67  std::cout<<"No devices detected so far.\n";
68  }
69  else
70  {
71  std::list<int>::const_iterator iter;
72  for(iter = connected_device_ids.begin(); iter != connected_device_ids.end(); iter++)
73  {
74  std::cout<<"["<<*iter<<"] ";
75  }
76  std::cout<<std::endl;
77  }
78 }
79 void launcher()
80 {
81  bool keep_running = true;
82  while(keep_running)
83  {
84  int choice = 0;
85  print_menu();
86  std::cout<<"Enter command:\n";
87  if(!(std::cin >> choice))
88  {
89  std::cout<<"Oops!\n";
90  std::cin.clear();
91  std::cin.ignore(10000, '\n');
92  continue;
93  }
94  switch(choice)
95  {
96  case 1:
97  rusbCtrl_init();
98  break;
99  case 2:
100  rusbCtrl_term();
101  connected_device_ids.clear();
102  break;
103  case 3:
104  std::cout<<"Enter a string for callback_payload. This will be used to identify the callback you register.\n";
105  if(!(std::cin >> callback_payload))
106  {
107  std::cout<<"Whoops! Bad input.\n";
108  std::cin.clear();
109  std::cin.ignore(10000, '\n');
110  }
111  else
112  {
113  int * device_list_ptr;
114  int device_list_size;
115  if(0 != rusbCtrl_registerCallback(callback, (void *)callback_payload.c_str(), &device_list_ptr, &device_list_size))
116  {
117  std::cout<<"Failed to register callback.\n";
118  }
119  else
120  {
121  std::cout<<"Registered callback with payload "<<callback_payload<<std::endl;
122  for(int i = 0; i < device_list_size; i++)
123  {
124  connected_device_ids.push_back(device_list_ptr[i]);
125  }
126  free(device_list_ptr);
127  dump_connected_devices();
128  }
129  }
130  break;
131  case 4:
132  {
133  std::cout<<"Enter devId(integer) and property(string) separated by a space.\n";
134  std::cout<<"Some examples of properties: product serial manufacturer idProduct idVendor\n";
135  int dev_id;
136  std::string property;
137  if(!(std::cin>>dev_id>>property))
138  {
139  std::cout<<"Whoops! Bad input.\n";
140  std::cin.clear();
141  std::cin.ignore(10000, '\n');
142  }
143  else
144  {
145  std::cout<<"Querying property "<<property<<" for dev_id "<<dev_id<<std::endl;
146  const char * result = rusbCtrl_getProperty(dev_id, property.c_str());
147  if(NULL == result)
148  {
149  std::cout<<"Query returned NULL!\n";
150  }
151  else
152  {
153  std::cout<<"Query returned "<<result<<std::endl;
154  free((void *)result);
155  }
156  }
157  break;
158  }
159  case 5:
160  {
161  dump_connected_devices();
162  break;
163  }
164  case 9:
165  keep_running = false;
166  std::cout<<"Quitting.\n";
167  break;
168 
169  default:
170  std::cout<<"Unknown input!\n";
171  }
172  }
173 }
174 
175 int main(int argc, char *argv[])
176 {
177  launcher();
178  return 0;
179 }
rusbCtrl_init
int rusbCtrl_init(void)
This API Initiate the library to a state that it is ready to detect device events and invoke callback...
Definition: usbctrl.cpp:540
print_menu
void print_menu()
This API prints the LED use-cases.
Definition: ledmgrmain.cpp:383
rusbCtrl_getProperty
char * rusbCtrl_getProperty(int devId, const char *propertyName)
This API compares unique identifier against its internal data structures and validates to provides pr...
Definition: usbctrl.cpp:555
rusbCtrl_term
int rusbCtrl_term()
This API Release all allocated resources.
Definition: usbctrl.cpp:546
rusbCtrl_registerCallback
int rusbCtrl_registerCallback(rusbCtrl_devCallback_t cb, void *cbData, int **devList, int *devListNumEntries)
This callback Allow application to listen for USB insert/remove events. An appliction can only regist...
Definition: usbctrl.cpp:551