Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 92 | void get_property( |
| 93 | vehicle_hw_device_t *device, int32_t property, int32_t type, char *value_string) { |
| 94 | vehicle_prop_value_t *data = (vehicle_prop_value_t *) malloc (sizeof(vehicle_prop_value_t)); |
| 95 | |
| 96 | // Parse the string according to type. |
| 97 | if (value_string != NULL && strlen(value_string) > 0) { |
| 98 | switch (type) { |
| 99 | case VEHICLE_VALUE_TYPE_INT32: |
| 100 | sscanf(value_string, "%d", &(data->value.int32_value)); |
| 101 | break; |
| 102 | case VEHICLE_VALUE_TYPE_INT32_VEC4: |
| 103 | { |
| 104 | int32_t vec[4]; |
| 105 | sscanf(value_string, "%d %d %d %d", &vec[0], &vec[1], &vec[2], &vec[3]); |
| 106 | memcpy(data->value.int32_array, vec, sizeof(vec)); |
| 107 | break; |
| 108 | } |
| 109 | default: |
| 110 | printf("%s Setting value type not supported: %d\n", __func__, type); |
| 111 | exit(1); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | data->prop = property; |
| 116 | int ret_code = device->get(device, data); |
| 117 | if (ret_code != 0) { |
| 118 | printf("Cannot get property: %d\n", ret_code); |
| 119 | exit(1); |
| 120 | } |
| 121 | |
| 122 | // We simply convert the data into the type mentioned by the result of the |
| 123 | // get call. |
| 124 | printf("Get output\n------------\n"); |
| 125 | switch (data->value_type) { |
| 126 | case VEHICLE_VALUE_TYPE_FLOAT: |
| 127 | printf("Value type: FLOAT\nValue: %f\n", data->value.float_value); |
| 128 | break; |
| 129 | case VEHICLE_VALUE_TYPE_INT32: |
| 130 | printf("Value type: INT32\nValue: %d\n", data->value.int32_value); |
| 131 | break; |
| 132 | case VEHICLE_VALUE_TYPE_INT64: |
| 133 | printf("Value type: INT64\nValue: %" PRId64 "\n", data->value.int64_value); |
| 134 | break; |
| 135 | case VEHICLE_VALUE_TYPE_BOOLEAN: |
| 136 | printf("Value type: BOOLEAN\nValue: %d\n", data->value.boolean_value); |
| 137 | break; |
| 138 | case VEHICLE_VALUE_TYPE_STRING: |
| 139 | printf("Value type: STRING\n Size: %d\n", data->value.str_value.len); |
| 140 | // This implementation only supports ASCII. |
| 141 | char *ascii_out = (char *) malloc((data->value.str_value.len + 1) * sizeof(char)); |
| 142 | memcpy(ascii_out, data->value.str_value.data, data->value.str_value.len); |
| 143 | ascii_out[data->value.str_value.len] = '\0'; |
| 144 | printf("Value: %s\n", ascii_out); |
| 145 | break; |
| 146 | case VEHICLE_VALUE_TYPE_ZONED_FLOAT: |
| 147 | printf("Value type: ZONED_FLOAT\nZone: %d\n", data->value.zoned_float_value.zone); |
| 148 | printf("Value type: ZONED_FLOAT\nValue: %f\n", data->value.zoned_float_value.value); |
| 149 | break; |
| 150 | case VEHICLE_VALUE_TYPE_INT32_VEC3: |
| 151 | printf("Value type: INT32_VEC3\nValue[0]: %d\n", data->value.int32_array[0]); |
| 152 | printf("Value type: INT32_VEC3\nValue[1]: %d\n", data->value.int32_array[1]); |
| 153 | printf("Value type: INT32_VEC3\nValue[2]: %d\n", data->value.int32_array[2]); |
| 154 | break; |
| 155 | case VEHICLE_VALUE_TYPE_INT32_VEC4: |
| 156 | printf("Value type: INT32_VEC4\nValue[0]: %d\n", data->value.int32_array[0]); |
| 157 | printf("Value type: INT32_VEC4\nValue[1]: %d\n", data->value.int32_array[1]); |
| 158 | printf("Value type: INT32_VEC4\nValue[2]: %d\n", data->value.int32_array[2]); |
| 159 | printf("Value type: INT32_VEC4\nValue[3]: %d\n", data->value.int32_array[3]); |
| 160 | break; |
| 161 | default: |
| 162 | printf("Value type not yet handled: %d.\n", data->value_type); |
| 163 | } |
| 164 | free(data); |
| 165 | } |
| 166 | |
| 167 | void set_property(vehicle_hw_device_t *device, |
| 168 | int32_t property, |
| 169 | int32_t type, |
| 170 | char *data) { |
| 171 | vehicle_prop_value_t vehicle_data; |
| 172 | vehicle_data.prop = property; |
| 173 | vehicle_data.value_type = type; |
| 174 | int32_t zone = 0; |
| 175 | float value = 0.0; |
| 176 | switch (type) { |
| 177 | case VEHICLE_VALUE_TYPE_FLOAT: |
| 178 | vehicle_data.value.float_value = atof(data); |
| 179 | break; |
| 180 | case VEHICLE_VALUE_TYPE_INT32: |
| 181 | vehicle_data.value.int32_value = atoi(data); |
| 182 | break; |
| 183 | case VEHICLE_VALUE_TYPE_INT64: |
| 184 | vehicle_data.value.int64_value = atoi(data); |
| 185 | break; |
| 186 | case VEHICLE_VALUE_TYPE_BOOLEAN: |
| 187 | vehicle_data.value.boolean_value = atoi(data); |
| 188 | break; |
| 189 | case VEHICLE_VALUE_TYPE_STRING: |
| 190 | // TODO: Make the code generic to UTF8 characters. |
| 191 | vehicle_data.value.str_value.len = strlen(data); |
| 192 | vehicle_data.value.str_value.data = |
| 193 | (uint8_t *) malloc (strlen(data) * sizeof(uint8_t)); |
| 194 | memcpy(vehicle_data.value.str_value.data, data, strlen(data) + 1); |
| 195 | break; |
| 196 | case VEHICLE_VALUE_TYPE_ZONED_FLOAT: |
| 197 | sscanf(data, "%d %f", &zone, &value); |
| 198 | vehicle_data.value.zoned_float_value.zone = zone; |
| 199 | vehicle_data.value.zoned_float_value.value = value; |
| 200 | printf("Value type: ZONED_FLOAT\nZone: %d\n", vehicle_data.value.zoned_float_value.zone); |
| 201 | printf("Value type: ZONED_FLOAT\nValue: %f\n", vehicle_data.value.zoned_float_value.value); |
| 202 | break; |
| 203 | case VEHICLE_VALUE_TYPE_INT32_VEC2: |
| 204 | sscanf(data, "%d %d", &vehicle_data.value.int32_array[0], |
| 205 | &vehicle_data.value.int32_array[1]); |
| 206 | printf("Setting: Value type INT32_VEC2: %d %d\n", |
| 207 | vehicle_data.value.int32_array[0], |
| 208 | vehicle_data.value.int32_array[1]); |
| 209 | break; |
| 210 | case VEHICLE_VALUE_TYPE_INT32_VEC3: |
| 211 | sscanf(data, "%d %d %d", &vehicle_data.value.int32_array[0], |
| 212 | &vehicle_data.value.int32_array[1], |
| 213 | &vehicle_data.value.int32_array[2]); |
| 214 | printf("Setting: Value type INT32_VEC3: %d %d %d\n", |
| 215 | vehicle_data.value.int32_array[0], |
| 216 | vehicle_data.value.int32_array[1], |
| 217 | vehicle_data.value.int32_array[2]); |
| 218 | break; |
| 219 | case VEHICLE_VALUE_TYPE_INT32_VEC4: |
| 220 | sscanf(data, "%d %d %d %d", &vehicle_data.value.int32_array[0], |
| 221 | &vehicle_data.value.int32_array[1], |
| 222 | &vehicle_data.value.int32_array[2], |
| 223 | &vehicle_data.value.int32_array[3]); |
| 224 | printf("Setting: Value type INT32_VEC4: %d %d %d %d\n", |
| 225 | vehicle_data.value.int32_array[0], |
| 226 | vehicle_data.value.int32_array[1], |
| 227 | vehicle_data.value.int32_array[2], |
| 228 | vehicle_data.value.int32_array[3]); |
| 229 | break; |
| 230 | default: |
| 231 | printf("set_property: Value type not yet handled: %d\n", type); |
| 232 | exit(1); |
| 233 | } |
| 234 | printf("Property id: %d\n", vehicle_data.prop); |
| 235 | int ret_code = device->set(device, &vehicle_data); |
| 236 | if (ret_code != 0) { |
| 237 | printf("Cannot set property: %d\n", ret_code); |
| 238 | exit(1); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | int vehicle_event_callback(const vehicle_prop_value_t *event_data) { |
| 243 | // Print what we got. |
| 244 | printf("Got some value from callback property: %d\n", event_data->prop); |
| 245 | printf("Timestamp: %" PRId64 "\n", event_data->timestamp); |
| 246 | char *ascii_out; |
| 247 | switch (event_data->value_type) { |
| 248 | case VEHICLE_VALUE_TYPE_FLOAT: |
| 249 | printf("Float value: %f\n", event_data->value.float_value); |
| 250 | break; |
| 251 | case VEHICLE_VALUE_TYPE_INT32: |
| 252 | printf("int32 value: %d\n", event_data->value.int32_value); |
| 253 | break; |
| 254 | case VEHICLE_VALUE_TYPE_INT64: |
| 255 | printf("int64 value: %" PRId64 "\n", event_data->value.int64_value); |
| 256 | break; |
| 257 | case VEHICLE_VALUE_TYPE_BOOLEAN: |
| 258 | printf("bool value: %d\n", event_data->value.boolean_value); |
| 259 | break; |
| 260 | case VEHICLE_VALUE_TYPE_STRING: |
| 261 | // TODO: Make the code generic to UTF8 characters. |
| 262 | ascii_out = (char *) malloc ((event_data->value.str_value.len + 1) * sizeof(char)); |
| 263 | memcpy(ascii_out, event_data->value.str_value.data, event_data->value.str_value.len); |
| 264 | ascii_out[event_data->value.str_value.len] = '\0'; |
| 265 | printf("Ascii value: %s\n", ascii_out); |
| 266 | break; |
| 267 | case VEHICLE_VALUE_TYPE_ZONED_FLOAT: |
| 268 | printf("Value type: ZONED_FLOAT\nZone: %d\n", event_data->value.zoned_float_value.zone); |
| 269 | printf("Value type: ZONED_FLOAT\nValue: %f\n", event_data->value.zoned_float_value.value); |
| 270 | break; |
| 271 | case VEHICLE_VALUE_TYPE_INT32_VEC4: |
| 272 | printf("Value type: INT32_VEC4\nValue[0]: %d Value[1] %d Value[2] %d Value[3] %d\n", |
| 273 | event_data->value.int32_array[0], event_data->value.int32_array[1], |
| 274 | event_data->value.int32_array[2], event_data->value.int32_array[3]); |
| 275 | break; |
| 276 | default: |
| 277 | printf("vehicle_event_callback: Value type not yet handled: %d\n", |
| 278 | event_data->value_type); |
| 279 | exit(1); |
| 280 | } |
| 281 | return 0; |
| 282 | } |
| 283 | int vehicle_error_callback(int32_t error_code, int32_t property, int32_t operation) { |
| 284 | // Print what we got. |
| 285 | printf("Error code obtained: %d\n", error_code); |
| 286 | return 0; |
| 287 | } |
| 288 | void subscribe_to_property( |
| 289 | vehicle_hw_device_t *device, |
| 290 | int32_t prop, |
| 291 | float sample_rate, |
| 292 | uint32_t wait_in_seconds) { |
| 293 | // Init the device with a callback. |
| 294 | device->init(device, vehicle_event_callback, vehicle_error_callback); |
| 295 | int ret_code = device->subscribe(device, prop, 0); |
| 296 | if (ret_code != 0) { |
| 297 | printf("Could not subscribe: %d\n", ret_code); |
| 298 | exit(1); |
| 299 | } |
| 300 | |
| 301 | // Callbacks will happen on one of the threads created by the HAL hence we |
| 302 | // can simply sleep here and see the output. |
| 303 | sleep(wait_in_seconds); |
| 304 | |
| 305 | // Unsubscribe and uninit. |
| 306 | ret_code = device->unsubscribe(device, prop); |
| 307 | if (ret_code != 0) { |
| 308 | printf("Error unsubscribing the HAL, still continuining to uninit HAL ..."); |
| 309 | } |
| 310 | |
| 311 | ret_code = device->release(device); |
| 312 | if (ret_code != 0) { |
| 313 | printf("Error uniniting HAL, exiting anyways."); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | int main(int argc, char* argv[]) { |
| 318 | // Open the vehicle module and just ask for the list of properties. |
| 319 | const hw_module_t *hw_module = NULL; |
| 320 | int ret_code = hw_get_module(VEHICLE_HARDWARE_MODULE_ID, &hw_module); |
| 321 | if (ret_code != 0) { |
| 322 | printf("Cannot open the hw module. Does the HAL exist? %d\n", ret_code); |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | vehicle_module_t *vehicle_module = (vehicle_module_t *)(hw_module); |
| 327 | hw_device_t *device = NULL; |
| 328 | ret_code = vehicle_module->common.methods->open(hw_module, NULL, &device); |
| 329 | if (!device) { |
| 330 | printf("Cannot open the hw device: %d\n", ret_code); |
| 331 | return -1; |
| 332 | } |
| 333 | vehicle_hw_device_t *vehicle_device = (vehicle_hw_device_t *) (device); |
| 334 | printf("HAL Loaded!\n"); |
| 335 | |
| 336 | // If this is a list properties command - we check for -l command. |
| 337 | int list_properties = 0; |
| 338 | // Type of the property (see #defines in vehicle.h). |
| 339 | int property = -1; |
| 340 | // Type of the value of the property (see enum vehicle_value_type). |
| 341 | int type = -1; |
| 342 | // Whether the mode is "get" or "set". |
| 343 | char mode[100] = ""; |
| 344 | // Actual value as a string representation (supports only PODs for now). |
| 345 | // TODO: Support structures and complex types in the tool. |
| 346 | char value[100] = ""; |
| 347 | // Wait time for the subscribe type of calls. |
| 348 | // We keep a default in case the user does not specify one. |
| 349 | int wait_time_in_sec = 10; |
| 350 | // Sample rate for subscribe type of calls. |
| 351 | // Default value is 0 for onchange type of properties. |
| 352 | int sample_rate = 0; |
| 353 | // Int array string which represents the vehicle_value_t in array of |
| 354 | // numbers. See vehicle_prop_value_t.value.int32_array. |
| 355 | char int_array_string[1000]; int_array_string[0] = '\0'; |
| 356 | |
| 357 | int opt; |
| 358 | while ((opt = getopt(argc, argv, "lm:p:t:v:w:s:")) != -1) { |
| 359 | switch (opt) { |
| 360 | case 'l': |
| 361 | list_properties = 1; |
| 362 | break; |
| 363 | case 'm': |
| 364 | strcpy(mode, optarg); |
| 365 | break; |
| 366 | case 'p': |
| 367 | property = atoi(optarg); |
| 368 | break; |
| 369 | case 't': |
| 370 | type = atoi(optarg); |
| 371 | break; |
| 372 | case 'v': |
| 373 | strcpy(value, optarg); |
| 374 | break; |
| 375 | case 'w': |
| 376 | wait_time_in_sec = atoi(optarg); |
| 377 | break; |
| 378 | case 's': |
| 379 | sample_rate = atoi(optarg); |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // We should have atleast one of list properties or mode (for get or set). |
| 385 | if (!list_properties && |
| 386 | !(!strcmp(mode, "get") || !strcmp(mode, "set") || !strcmp(mode, "sub"))) { |
| 387 | usage(); |
| 388 | exit(1); |
| 389 | } |
| 390 | |
| 391 | if (list_properties) { |
| 392 | printf("Listing properties...\n"); |
| 393 | list_all_properties(vehicle_device); |
| 394 | } else if (!strcmp(mode, "get")) { |
| 395 | printf("Getting property ...\n"); |
| 396 | if (property == -1) { |
| 397 | printf("Use -p to pass a valid Property.\n"); |
| 398 | usage(); |
| 399 | exit(1); |
| 400 | } |
| 401 | |
| 402 | int32_t int_array_list[4]; |
| 403 | int count = -1; |
| 404 | if (strlen(int_array_string) > 0) { |
| 405 | count = sscanf(int_array_string, "%d%d%d%d", |
| 406 | &int_array_list[0], &int_array_list[1], &int_array_list[2], &int_array_list[3]); |
| 407 | } |
| 408 | |
| 409 | get_property(vehicle_device, property, type, value); |
| 410 | } else if (!strcmp(mode, "set")) { |
| 411 | printf("Setting property ...\n"); |
| 412 | if (property == -1 || type == -1) { |
| 413 | printf("Use -p to pass a valid Property and -t to pass a valid Type.\n"); |
| 414 | usage(); |
| 415 | exit(1); |
| 416 | } |
| 417 | set_property(vehicle_device, property, type, value); |
| 418 | } else if (!strcmp(mode, "sub")) { |
| 419 | printf("Subscribing property ...\n"); |
| 420 | if (property == -1 || wait_time_in_sec <= 0) { |
| 421 | printf("Use -p to pass a valid property and -w to pass a valid wait time(s)\n"); |
| 422 | usage(); |
| 423 | exit(1); |
| 424 | } |
| 425 | subscribe_to_property(vehicle_device, property, sample_rate, wait_time_in_sec); |
| 426 | } |
| 427 | return 0; |
| 428 | } |