1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "vehicle-hal-tool"
18
19 #include <inttypes.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <hardware/hardware.h>
23 #include <hardware/vehicle.h>
24
25 #include <cutils/log.h>
26
usage()27 void usage() {
28 printf("Usage: "
29 "./vehicle-hal-tool [-l] [-m -p -t [-v]]\n"
30 "-l - List properties\n"
31 "-m - Mode (cannot be used with -l). Accepted strings: get, set or sub.\n"
32 "-p - Property (only used with -m)\n"
33 "-t - Type (only used with -m)\n"
34 "-w - Wait time in seconds (only used with -m set to sub)\n"
35 "-v - Value to which vehicle_prop_value is set\n"
36 "Depending on the type pass the value:\n"
37 "Int: pass a quoted integer\n"
38 "Float: pass a quoted float\n"
39 "Int array: pass a quoted space delimited int array, eg: \"1 2 3 4\" for\n:"
40 "setting int32_array's all 4 elements (see VEHICLE_VALUE_TYPE_INT32_VEC4\n"
41 "String: pass a normal string\n\n"
42 "The configurations to use the tool are as follows:\n"
43 "List Properties\n"
44 "---------------\n"
45 "./vehicle-hal-tool -l \n"
46 "Lists the various properties defined in HAL implementation. Use this to check if "
47 "the HAL implementation is correctly set up and exposing the capabilities correctly.\n"
48
49 "Get Properties\n"
50 "---------------\n"
51 "./vehicle-hal-tool -m get -p <prop> -t <type> [-v <vehicle_prop_value>]\n"
52 "Example: ./vehicle-hal-tool -m get -p 1028 -t 3 # VEHICLE_PROPERTY_DRIVING_STATUS\n"
53 "./vehicle-hal-tool -m get -p 257 -t 1 # VEHICLE_PROPERTY_INFO_MAKE\n"
54 "./vehicle-hal-tool -m get -p 2049 -t 19 -v \"3 0 0 0\"\n"
55 " # VEHICLE_PROPERTY_RADIO_PRESET\n"
56 "with preset value set to 3.\n\n"
57 "Set properties\n"
58 "--------------\n"
59 "./vehicle-hal-tool -m set -p 10 -t 1 -v random_property\n"
60 "Set properties may not be applicable to most properties\n\n"
61 "Subscribe properties\n"
62 "--------------------\n"
63 "Subscribes to be notified about a property change (depending on whether\n"
64 "it is a on change property or a continuous property) for seconds provided\n"
65 "as -w paramter.\n"
66 "./vehicle-hal-tool -m sub -p 1028 -w 10\n"
67 );
68 }
69
list_all_properties(vehicle_hw_device_t * device)70 void list_all_properties(vehicle_hw_device_t *device) {
71 int num_configs = -1;
72 const vehicle_prop_config_t *configs = device->list_properties(device, &num_configs);
73 if (num_configs < 0) {
74 printf("List configs error. %d", num_configs);
75 exit(1);
76 }
77
78 printf("Listing configs\n--------------------\n");
79 int i = 0;
80 for (i = 0; i < num_configs; i++) {
81 const vehicle_prop_config_t *config_temp = configs + i;
82 printf("Property ID: %d\n"
83 "Property config_flags: %d\n"
84 "Property change mode: %d\n"
85 "Property min sample rate: %f\n"
86 "Property max sample rate: %f\n",
87 config_temp->prop, config_temp->config_flags, config_temp->change_mode,
88 config_temp->min_sample_rate, config_temp->max_sample_rate);
89 }
90 }
91
print_property(const vehicle_prop_value_t * data)92 static void print_property(const vehicle_prop_value_t *data) {
93 switch (data->value_type) {
94 case VEHICLE_VALUE_TYPE_STRING:
95 printf("Value type: STRING\n Size: %d\n", data->value.str_value.len);
96 // This implementation only supports ASCII.
97 char *ascii_out = (char *) malloc((data->value.str_value.len + 1) * sizeof(char));
98 memcpy(ascii_out, data->value.str_value.data, data->value.str_value.len);
99 ascii_out[data->value.str_value.len] = '\0';
100 printf("Value Type: STRING %s\n", ascii_out);
101 break;
102 case VEHICLE_VALUE_TYPE_BYTES:
103 printf("Value type: BYTES\n Size: %d", data->value.bytes_value.len);
104 for (int i = 0; i < data->value.bytes_value.len; i++) {
105 if ((i % 16) == 0) {
106 printf("\n %04X: ", i);
107 }
108 printf("%02X ", data->value.bytes_value.data[i]);
109 }
110 printf("\n");
111 break;
112 case VEHICLE_VALUE_TYPE_BOOLEAN:
113 printf("Value type: BOOLEAN\nValue: %d\n", data->value.boolean_value);
114 break;
115 case VEHICLE_VALUE_TYPE_ZONED_BOOLEAN:
116 printf("Value type: ZONED_BOOLEAN\nZone: %d\n", data->zone);
117 printf("Value: %d\n", data->value.boolean_value);
118 break;
119 case VEHICLE_VALUE_TYPE_INT64:
120 printf("Value type: INT64\nValue: %" PRId64 "\n", data->value.int64_value);
121 break;
122 case VEHICLE_VALUE_TYPE_FLOAT:
123 printf("Value type: FLOAT\nValue: %f\n", data->value.float_value);
124 break;
125 case VEHICLE_VALUE_TYPE_FLOAT_VEC2:
126 printf("Value type: FLOAT_VEC2\nValue[0]: %f ", data->value.float_array[0]);
127 printf("Value[1]: %f\n", data->value.float_array[1]);
128 break;
129 case VEHICLE_VALUE_TYPE_FLOAT_VEC3:
130 printf("Value type: FLOAT_VEC3\nValue[0]: %f ", data->value.float_array[0]);
131 printf("Value[1]: %f ", data->value.float_array[1]);
132 printf("Value[2]: %f\n", data->value.float_array[2]);
133 break;
134 case VEHICLE_VALUE_TYPE_FLOAT_VEC4:
135 printf("Value type: FLOAT_VEC4\nValue[0]: %f ", data->value.float_array[0]);
136 printf("Value[1]: %f ", data->value.float_array[1]);
137 printf("Value[2]: %f ", data->value.float_array[2]);
138 printf("Value[3]: %f\n", data->value.float_array[3]);
139 break;
140 case VEHICLE_VALUE_TYPE_INT32:
141 printf("Value type: INT32\nValue: %d\n", data->value.int32_value);
142 break;
143 case VEHICLE_VALUE_TYPE_INT32_VEC2:
144 printf("Value type: INT32_VEC2\nValue[0]: %d ", data->value.int32_array[0]);
145 printf("Value[1]: %d\n", data->value.int32_array[1]);
146 break;
147 case VEHICLE_VALUE_TYPE_INT32_VEC3:
148 printf("Value type: INT32_VEC3\nValue[0]: %d ", data->value.int32_array[0]);
149 printf("Value[1]: %d ", data->value.int32_array[1]);
150 printf("Value[2]: %d\n", data->value.int32_array[2]);
151 break;
152 case VEHICLE_VALUE_TYPE_INT32_VEC4:
153 printf("Value type: INT32_VEC4\nValue[0]: %d ", data->value.int32_array[0]);
154 printf("Value[1]: %d ", data->value.int32_array[1]);
155 printf("Value[2]: %d ", data->value.int32_array[2]);
156 printf("Value[3]: %d\n", data->value.int32_array[3]);
157 break;
158 case VEHICLE_VALUE_TYPE_ZONED_FLOAT:
159 printf("Value type: ZONED_FLOAT\nZone: %d ", data->zone);
160 printf("Value: %f\n", data->value.float_value);
161 break;
162 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC2:
163 printf("Value type: ZONED_FLOAT_VEC2\nZone: %d ", data->zone);
164 printf("Value[0]: %f", data->value.float_array[0]);
165 printf("Value[1]: %f\n", data->value.float_array[1]);
166 break;
167 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC3:
168 printf("Value type: ZONED_FLOAT_VEC3\nZone: %d ", data->zone);
169 printf("Value[0]: %f ", data->value.float_array[0]);
170 printf("Value[1]: %f ", data->value.float_array[1]);
171 printf("Value[2]: %f\n", data->value.float_array[2]);
172 break;
173 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC4:
174 printf("Value type: ZONED_FLOAT_VEC4\nZone: %d ", data->zone);
175 printf("Value[0]: %f ", data->value.float_array[0]);
176 printf("Value[1]: %f ", data->value.float_array[1]);
177 printf("Value[2]: %f ", data->value.float_array[2]);
178 printf("Value[3]: %f\n", data->value.float_array[3]);
179 break;
180 case VEHICLE_VALUE_TYPE_ZONED_INT32:
181 printf("Value type: ZONED_INT32\nZone: %d ", data->zone);
182 printf("Value: %d\n", data->value.int32_value);
183 break;
184 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC2:
185 printf("Value type: ZONED_INT32_VEC2\nZone: %d ", data->zone);
186 printf("Value[0]: %d ", data->value.int32_array[0]);
187 printf("Value[1]: %d\n", data->value.int32_array[1]);
188 break;
189 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC3:
190 printf("Value type: ZONED_INT32_VEC3\nZone: %d ", data->zone);
191 printf("Value[0]: %d ", data->value.int32_array[0]);
192 printf("Value[1]: %d ", data->value.int32_array[1]);
193 printf("Value[2]: %d\n", data->value.int32_array[2]);
194 break;
195 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC4:
196 printf("Value type: ZONED_INT32_VEC4\nZone: %d ", data->zone);
197 printf("Value[0]: %d ", data->value.int32_array[0]);
198 printf("Value[1]: %d ", data->value.int32_array[1]);
199 printf("Value[2]: %d ", data->value.int32_array[2]);
200 printf("Value[3]: %d\n", data->value.int32_array[3]);
201 break;
202 default:
203 printf("Value type not yet handled: %d.\n", data->value_type);
204 }
205 }
206
get_property(vehicle_hw_device_t * device,int32_t property,int32_t type,char * value_string)207 void get_property(
208 vehicle_hw_device_t *device, int32_t property, int32_t type, char *value_string) {
209 vehicle_prop_value_t *data = (vehicle_prop_value_t *) malloc (sizeof(vehicle_prop_value_t));
210
211 // Parse the string according to type.
212 if (value_string != NULL && strlen(value_string) > 0) {
213 switch (type) {
214 case VEHICLE_VALUE_TYPE_INT32:
215 sscanf(value_string, "%d", &(data->value.int32_value));
216 break;
217 case VEHICLE_VALUE_TYPE_INT32_VEC4:
218 {
219 int32_t vec[4];
220 sscanf(value_string, "%d %d %d %d", &vec[0], &vec[1], &vec[2], &vec[3]);
221 memcpy(data->value.int32_array, vec, sizeof(vec));
222 break;
223 }
224 default:
225 printf("%s Setting value type not supported: %d\n", __func__, type);
226 exit(1);
227 }
228 }
229
230 data->prop = property;
231 int ret_code = device->get(device, data);
232 if (ret_code != 0) {
233 printf("Cannot get property: %d\n", ret_code);
234 exit(1);
235 }
236
237 // We simply convert the data into the type mentioned by the result of the
238 // get call.
239 printf("Get output\n------------\n");
240 print_property(data);
241 free(data);
242 }
243
set_property(vehicle_hw_device_t * device,int32_t property,int32_t type,char * data)244 void set_property(vehicle_hw_device_t *device,
245 int32_t property,
246 int32_t type,
247 char *data) {
248 vehicle_prop_value_t vehicle_data;
249 vehicle_data.prop = property;
250 vehicle_data.value_type = type;
251 int32_t zone = 0;
252 float value = 0.0;
253 switch (type) {
254 case VEHICLE_VALUE_TYPE_STRING:
255 // TODO: Make the code generic to UTF8 characters.
256 vehicle_data.value.str_value.len = strlen(data);
257 vehicle_data.value.str_value.data =
258 (uint8_t *) malloc (strlen(data) * sizeof(uint8_t));
259 memcpy(vehicle_data.value.str_value.data, data, strlen(data) + 1);
260 break;
261 case VEHICLE_VALUE_TYPE_BYTES: {
262 int len = strlen(data);
263 int numBytes = (len + 1) / 3;
264 uint8_t *buf = calloc(numBytes, sizeof(uint8_t));
265 char *byte = strtok(data, " ");
266 for (int i = 0; byte != NULL && i < numBytes; i++) {
267 buf[i] = strtol(data, NULL, 16);
268 byte = strtok(NULL, " ");
269 }
270 vehicle_data.value.bytes_value.len = numBytes;
271 vehicle_data.value.bytes_value.data = buf;
272 }
273 break;
274 case VEHICLE_VALUE_TYPE_BOOLEAN:
275 vehicle_data.value.boolean_value = atoi(data);
276 break;
277 case VEHICLE_VALUE_TYPE_ZONED_BOOLEAN:
278 sscanf(data, "%d %d", &vehicle_data.zone,
279 &vehicle_data.value.boolean_value);
280 break;
281 case VEHICLE_VALUE_TYPE_INT64:
282 vehicle_data.value.int64_value = atoi(data);
283 break;
284 case VEHICLE_VALUE_TYPE_FLOAT:
285 vehicle_data.value.float_value = atof(data);
286 break;
287 case VEHICLE_VALUE_TYPE_FLOAT_VEC2:
288 sscanf(data, "%f %f", &vehicle_data.value.float_array[0],
289 &vehicle_data.value.float_array[1]);
290 break;
291 case VEHICLE_VALUE_TYPE_FLOAT_VEC3:
292 sscanf(data, "%f %f %f", &vehicle_data.value.float_array[0],
293 &vehicle_data.value.float_array[1],
294 &vehicle_data.value.float_array[2]);
295 break;
296 case VEHICLE_VALUE_TYPE_FLOAT_VEC4:
297 sscanf(data, "%f %f %f %f", &vehicle_data.value.float_array[0],
298 &vehicle_data.value.float_array[1],
299 &vehicle_data.value.float_array[2],
300 &vehicle_data.value.float_array[3]);
301 break;
302 case VEHICLE_VALUE_TYPE_INT32:
303 vehicle_data.value.int32_value = atoi(data);
304 break;
305 case VEHICLE_VALUE_TYPE_INT32_VEC2:
306 sscanf(data, "%d %d", &vehicle_data.value.int32_array[0],
307 &vehicle_data.value.int32_array[1]);
308 break;
309 case VEHICLE_VALUE_TYPE_INT32_VEC3:
310 sscanf(data, "%d %d %d", &vehicle_data.value.int32_array[0],
311 &vehicle_data.value.int32_array[1],
312 &vehicle_data.value.int32_array[2]);
313 break;
314 case VEHICLE_VALUE_TYPE_INT32_VEC4:
315 sscanf(data, "%d %d %d %d", &vehicle_data.value.int32_array[0],
316 &vehicle_data.value.int32_array[1],
317 &vehicle_data.value.int32_array[2],
318 &vehicle_data.value.int32_array[3]);
319 break;
320 case VEHICLE_VALUE_TYPE_ZONED_FLOAT:
321 sscanf(data, "%d %f", &zone, &value);
322 vehicle_data.zone = zone;
323 vehicle_data.value.float_value = value;
324 break;
325 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC2:
326 sscanf(data, "%d %f %f", &vehicle_data.zone,
327 &vehicle_data.value.float_array[0],
328 &vehicle_data.value.float_array[1]);
329 break;
330 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC3:
331 sscanf(data, "%d %f %f %f", &vehicle_data.zone,
332 &vehicle_data.value.float_array[0],
333 &vehicle_data.value.float_array[1],
334 &vehicle_data.value.float_array[2]);
335 break;
336 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC4:
337 sscanf(data, "%d %f %f %f %f", &vehicle_data.zone,
338 &vehicle_data.value.float_array[0],
339 &vehicle_data.value.float_array[1],
340 &vehicle_data.value.float_array[2],
341 &vehicle_data.value.float_array[3]);
342 break;
343 case VEHICLE_VALUE_TYPE_ZONED_INT32:
344 sscanf(data, "%d %d", &vehicle_data.zone,
345 &vehicle_data.value.int32_value);
346 break;
347 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC2:
348 sscanf(data, "%d %d %d", &vehicle_data.zone,
349 &vehicle_data.value.int32_array[0],
350 &vehicle_data.value.int32_array[1]);
351 break;
352 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC3:
353 sscanf(data, "%d %d %d %d", &vehicle_data.zone,
354 &vehicle_data.value.int32_array[0],
355 &vehicle_data.value.int32_array[1],
356 &vehicle_data.value.int32_array[2]);
357 break;
358 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC4:
359 sscanf(data, "%d %d %d %d %d", &vehicle_data.zone,
360 &vehicle_data.value.int32_array[0],
361 &vehicle_data.value.int32_array[1],
362 &vehicle_data.value.int32_array[2],
363 &vehicle_data.value.int32_array[3]);
364 break;
365 default:
366 printf("set_property: Value type not yet handled: %d\n", type);
367 exit(1);
368 }
369 printf("Setting Property id: %d\n", vehicle_data.prop);
370 print_property(&vehicle_data);
371
372 int ret_code = device->set(device, &vehicle_data);
373 if (ret_code != 0) {
374 printf("Cannot set property: %d\n", ret_code);
375 exit(1);
376 }
377 }
378
vehicle_event_callback(const vehicle_prop_value_t * event_data)379 int vehicle_event_callback(const vehicle_prop_value_t *event_data) {
380 // Print what we got.
381 printf("Got some value from callback property: %d\n", event_data->prop);
382 printf("Timestamp: %" PRId64 "\n", event_data->timestamp);
383 print_property(event_data);
384 return 0;
385 }
386
vehicle_error_callback(int32_t error_code,int32_t property,int32_t operation)387 int vehicle_error_callback(int32_t error_code, int32_t property, int32_t operation) {
388 // Print what we got.
389 printf("Error code obtained: %d\n", error_code);
390 return 0;
391 }
392
subscribe_to_property(vehicle_hw_device_t * device,int32_t prop,float sample_rate,uint32_t wait_in_seconds)393 void subscribe_to_property(
394 vehicle_hw_device_t *device,
395 int32_t prop,
396 float sample_rate,
397 uint32_t wait_in_seconds) {
398 // Init the device with a callback.
399 int ret_code = device->subscribe(device, prop, 0, 0);
400 if (ret_code != 0) {
401 printf("Could not subscribe: %d\n", ret_code);
402 exit(1);
403 }
404
405 // Callbacks will happen on one of the threads created by the HAL hence we
406 // can simply sleep here and see the output.
407 sleep(wait_in_seconds);
408
409 // Unsubscribe and uninit.
410 ret_code = device->unsubscribe(device, prop);
411 if (ret_code != 0) {
412 printf("Error unsubscribing the HAL, still continuining to uninit HAL ...");
413 }
414 }
415
main(int argc,char * argv[])416 int main(int argc, char* argv[]) {
417 // Open the vehicle module and just ask for the list of properties.
418 const hw_module_t *hw_module = NULL;
419 int ret_code = hw_get_module(VEHICLE_HARDWARE_MODULE_ID, &hw_module);
420 if (ret_code != 0) {
421 printf("Cannot open the hw module. Does the HAL exist? %d\n", ret_code);
422 return -1;
423 }
424
425 vehicle_module_t *vehicle_module = (vehicle_module_t *)(hw_module);
426 hw_device_t *device = NULL;
427 ret_code = vehicle_module->common.methods->open(hw_module, NULL, &device);
428 if (!device) {
429 printf("Cannot open the hw device: %d\n", ret_code);
430 return -1;
431 }
432 vehicle_hw_device_t *vehicle_device = (vehicle_hw_device_t *) (device);
433 printf("HAL Loaded!\n");
434
435 vehicle_device->init(vehicle_device, vehicle_event_callback, vehicle_error_callback);
436
437 // If this is a list properties command - we check for -l command.
438 int list_properties = 0;
439 // Type of the property (see #defines in vehicle.h).
440 int property = -1;
441 // Type of the value of the property (see enum vehicle_value_type).
442 int type = -1;
443 // Whether the mode is "get" or "set".
444 char mode[100] = "";
445 // Actual value as a string representation (supports only PODs for now).
446 // TODO: Support structures and complex types in the tool.
447 char value[100] = "";
448 // Wait time for the subscribe type of calls.
449 // We keep a default in case the user does not specify one.
450 int wait_time_in_sec = 10;
451 // Sample rate for subscribe type of calls.
452 // Default value is 0 for onchange type of properties.
453 int sample_rate = 0;
454 // Int array string which represents the vehicle_value_t in array of
455 // numbers. See vehicle_prop_value_t.value.int32_array.
456 char int_array_string[1000]; int_array_string[0] = '\0';
457
458 int opt;
459 while ((opt = getopt(argc, argv, "lm:p:t:v:w:s:")) != -1) {
460 switch (opt) {
461 case 'l':
462 list_properties = 1;
463 break;
464 case 'm':
465 strcpy(mode, optarg);
466 break;
467 case 'p':
468 property = atoi(optarg);
469 break;
470 case 't':
471 type = atoi(optarg);
472 break;
473 case 'v':
474 strcpy(value, optarg);
475 break;
476 case 'w':
477 wait_time_in_sec = atoi(optarg);
478 break;
479 case 's':
480 sample_rate = atoi(optarg);
481 break;
482 }
483 }
484
485 // We should have atleast one of list properties or mode (for get or set).
486 if (!list_properties &&
487 !(!strcmp(mode, "get") || !strcmp(mode, "set") || !strcmp(mode, "sub"))) {
488 usage();
489 exit(1);
490 }
491
492 if (list_properties) {
493 printf("Listing properties...\n");
494 list_all_properties(vehicle_device);
495 } else if (!strcmp(mode, "get")) {
496 printf("Getting property ...\n");
497 if (property == -1) {
498 printf("Use -p to pass a valid Property.\n");
499 usage();
500 exit(1);
501 }
502
503 int32_t int_array_list[4];
504 int count = -1;
505 if (strlen(int_array_string) > 0) {
506 count = sscanf(int_array_string, "%d%d%d%d",
507 &int_array_list[0], &int_array_list[1], &int_array_list[2], &int_array_list[3]);
508 }
509
510 get_property(vehicle_device, property, type, value);
511 } else if (!strcmp(mode, "set")) {
512 printf("Setting property ...\n");
513 if (property == -1 || type == -1) {
514 printf("Use -p to pass a valid Property and -t to pass a valid Type.\n");
515 usage();
516 exit(1);
517 }
518 set_property(vehicle_device, property, type, value);
519 } else if (!strcmp(mode, "sub")) {
520 printf("Subscribing property ...\n");
521 if (property == -1 || wait_time_in_sec <= 0) {
522 printf("Use -p to pass a valid property and -w to pass a valid wait time(s)\n");
523 usage();
524 exit(1);
525 }
526 subscribe_to_property(vehicle_device, property, sample_rate, wait_time_in_sec);
527 }
528
529 ret_code = vehicle_device->release(vehicle_device);
530 if (ret_code != 0) {
531 printf("Error uniniting HAL, exiting anyways.");
532 }
533 return 0;
534 }
535