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_hw_default" |
| 18 | #define LOG_NDEBUG 1 |
| 19 | #define RADIO_PRESET_NUM 6 |
| 20 | |
Keun-young Park | 08c255e | 2015-12-09 13:47:30 -0800 | [diff] [blame] | 21 | #define UNUSED __attribute__((__unused__)) |
| 22 | |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 23 | #include <errno.h> |
Keun-young Park | 08c255e | 2015-12-09 13:47:30 -0800 | [diff] [blame] | 24 | #include <inttypes.h> |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 25 | #include <malloc.h> |
| 26 | #include <pthread.h> |
| 27 | #include <stdint.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/prctl.h> |
| 31 | #include <sys/time.h> |
| 32 | #include <time.h> |
| 33 | |
| 34 | #include <cutils/log.h> |
| 35 | #include <system/radio.h> |
| 36 | #include <hardware/hardware.h> |
| 37 | #include <hardware/vehicle.h> |
| 38 | |
| 39 | extern int64_t elapsedRealtimeNano(); |
| 40 | |
| 41 | static char VEHICLE_MAKE[] = "android_car"; |
| 42 | |
| 43 | typedef struct vehicle_device_impl { |
| 44 | vehicle_hw_device_t vehicle_device; |
| 45 | uint32_t initialized_; |
| 46 | vehicle_event_callback_fn event_fn_; |
| 47 | vehicle_error_callback_fn error_fn_; |
| 48 | } vehicle_device_impl_t ; |
| 49 | |
| 50 | static pthread_mutex_t lock_; |
| 51 | |
| 52 | typedef struct subscription { |
| 53 | // Each subscription has it's own thread. |
| 54 | pthread_t thread_id; |
| 55 | int32_t prop; |
| 56 | float sample_rate; |
| 57 | pthread_mutex_t lock; |
| 58 | // This field should be protected by the above mutex. |
| 59 | // TODO change this to something better as flag alone takes long time to finish. |
| 60 | uint32_t stop_thread; |
| 61 | vehicle_device_impl_t* impl; |
| 62 | pthread_t thread; |
| 63 | pthread_cond_t cond; |
| 64 | char name[100]; |
| 65 | } subscription_t; |
| 66 | |
| 67 | static vehicle_prop_config_t CONFIGS[] = { |
| 68 | { |
| 69 | .prop = VEHICLE_PROPERTY_INFO_MAKE, |
| 70 | .access = VEHICLE_PROP_ACCESS_READ, |
| 71 | .change_mode = VEHICLE_PROP_CHANGE_MODE_STATIC, |
| 72 | .value_type = VEHICLE_VALUE_TYPE_STRING, |
| 73 | .min_sample_rate = 0, |
| 74 | .max_sample_rate = 0, |
| 75 | .hal_data = NULL, |
| 76 | }, |
| 77 | { |
| 78 | .prop = VEHICLE_PROPERTY_GEAR_SELECTION, |
| 79 | .access = VEHICLE_PROP_ACCESS_READ, |
| 80 | .change_mode = VEHICLE_PROP_CHANGE_MODE_ON_CHANGE, |
| 81 | .value_type = VEHICLE_VALUE_TYPE_INT32, |
| 82 | .min_sample_rate = 0, |
| 83 | .max_sample_rate = 0, |
| 84 | .hal_data = NULL, |
| 85 | }, |
| 86 | { |
| 87 | .prop = VEHICLE_PROPERTY_DRIVING_STATUS, |
| 88 | .access = VEHICLE_PROP_ACCESS_READ, |
| 89 | .change_mode = VEHICLE_PROP_CHANGE_MODE_ON_CHANGE, |
| 90 | .value_type = VEHICLE_VALUE_TYPE_INT32, |
| 91 | .min_sample_rate = 0, |
| 92 | .max_sample_rate = 0, |
| 93 | .hal_data = NULL, |
| 94 | }, |
| 95 | { |
| 96 | .prop = VEHICLE_PROPERTY_PARKING_BRAKE_ON, |
| 97 | .access = VEHICLE_PROP_ACCESS_READ, |
| 98 | .change_mode = VEHICLE_PROP_CHANGE_MODE_ON_CHANGE, |
| 99 | .value_type = VEHICLE_VALUE_TYPE_BOOLEAN, |
| 100 | .min_sample_rate = 0, |
| 101 | .max_sample_rate = 0, |
| 102 | .hal_data = NULL, |
| 103 | }, |
| 104 | { |
| 105 | .prop = VEHICLE_PROPERTY_PERF_VEHICLE_SPEED, |
| 106 | .access = VEHICLE_PROP_ACCESS_READ, |
| 107 | .change_mode = VEHICLE_PROP_CHANGE_MODE_CONTINUOUS, |
| 108 | .value_type = VEHICLE_VALUE_TYPE_FLOAT, |
| 109 | .min_sample_rate = 0.1, |
| 110 | .max_sample_rate = 10.0, |
| 111 | .hal_data = NULL, |
| 112 | }, |
| 113 | { |
| 114 | .prop = VEHICLE_PROPERTY_RADIO_PRESET, |
| 115 | .access = VEHICLE_PROP_ACCESS_READ_WRITE, |
| 116 | .change_mode = VEHICLE_PROP_CHANGE_MODE_ON_CHANGE, |
| 117 | .value_type = VEHICLE_VALUE_TYPE_INT32_VEC4, |
| 118 | .vehicle_radio_num_presets = RADIO_PRESET_NUM, |
| 119 | .min_sample_rate = 0, |
| 120 | .max_sample_rate = 0, |
| 121 | .hal_data = NULL, |
| 122 | }, |
| 123 | }; |
| 124 | |
| 125 | vehicle_prop_config_t* find_config(int prop) { |
Keun-young Park | 08c255e | 2015-12-09 13:47:30 -0800 | [diff] [blame] | 126 | unsigned int i; |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 127 | for (i = 0; i < sizeof(CONFIGS) / sizeof(vehicle_prop_config_t); i++) { |
| 128 | if (CONFIGS[i].prop == prop) { |
| 129 | return &CONFIGS[i]; |
| 130 | } |
| 131 | } |
| 132 | return NULL; |
| 133 | } |
| 134 | |
| 135 | static int alloc_vehicle_str_from_cstr(const char* string, vehicle_str_t* vehicle_str) { |
| 136 | int len = strlen(string); |
| 137 | vehicle_str->data = (uint8_t*) malloc(len); |
| 138 | if (vehicle_str->data == NULL) { |
| 139 | return -ENOMEM; |
| 140 | } |
| 141 | memcpy(vehicle_str->data, string, len); |
| 142 | vehicle_str->len = len; |
| 143 | return 0; |
| 144 | } |
| 145 | |
Keun-young Park | 08c255e | 2015-12-09 13:47:30 -0800 | [diff] [blame] | 146 | static vehicle_prop_config_t const * vdev_list_properties(vehicle_hw_device_t* device UNUSED, |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 147 | int* num_properties) { |
| 148 | ALOGD("vdev_list_properties."); |
| 149 | |
| 150 | *num_properties = sizeof(CONFIGS) / sizeof(vehicle_prop_config_t); |
| 151 | return CONFIGS; |
| 152 | } |
| 153 | |
| 154 | static int vdev_init(vehicle_hw_device_t* device, |
| 155 | vehicle_event_callback_fn event_callback_fn, |
| 156 | vehicle_error_callback_fn error_callback_fn) { |
| 157 | ALOGD("vdev_init."); |
| 158 | vehicle_device_impl_t* impl = (vehicle_device_impl_t*)device; |
| 159 | pthread_mutex_lock(&lock_); |
| 160 | if (impl->initialized_) { |
| 161 | ALOGE("vdev_init: Callback and Error functions are already existing."); |
| 162 | pthread_mutex_unlock(&lock_); |
| 163 | return -EEXIST; |
| 164 | } |
| 165 | |
| 166 | impl->initialized_ = 1; |
| 167 | impl->event_fn_ = event_callback_fn; |
| 168 | impl->error_fn_ = error_callback_fn; |
| 169 | pthread_mutex_unlock(&lock_); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int vdev_release(vehicle_hw_device_t* device) { |
| 174 | vehicle_device_impl_t* impl = (vehicle_device_impl_t*)device; |
| 175 | pthread_mutex_lock(&lock_); |
| 176 | if (!impl->initialized_) { |
| 177 | ALOGD("vdev_release: Already released before, returning early."); |
| 178 | } else { |
| 179 | // unsubscribe_all() |
| 180 | impl->initialized_ = 0; |
| 181 | } |
| 182 | pthread_mutex_unlock(&lock_); |
| 183 | return 0; |
| 184 | } |
| 185 | |
Keun-young Park | 08c255e | 2015-12-09 13:47:30 -0800 | [diff] [blame] | 186 | static int vdev_get(vehicle_hw_device_t* device UNUSED, vehicle_prop_value_t* data) { |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 187 | ALOGD("vdev_get."); |
| 188 | //TODO all data supporting read should support get |
| 189 | if (!data) { |
| 190 | ALOGE("vdev_get: Data cannot be null."); |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | vehicle_prop_config_t* config = find_config(data->prop); |
| 194 | if (config == NULL) { |
| 195 | ALOGE("vdev_get: cannot find config 0x%x", data->prop); |
| 196 | return -EINVAL; |
| 197 | } |
| 198 | data->value_type = config->value_type; |
| 199 | // for STATIC type, time can be just 0 instead |
| 200 | data->timestamp = elapsedRealtimeNano(); |
| 201 | int r; |
| 202 | switch (data->prop) { |
| 203 | case VEHICLE_PROPERTY_INFO_MAKE: |
| 204 | r = alloc_vehicle_str_from_cstr(VEHICLE_MAKE, &(data->value.str_value)); |
| 205 | if (r != 0) { |
| 206 | ALOGE("vdev_get: alloc failed"); |
| 207 | return r; |
| 208 | } |
| 209 | break; |
| 210 | |
| 211 | case VEHICLE_PROPERTY_RADIO_PRESET: { |
| 212 | int radio_preset = data->value.int32_array[0]; |
| 213 | if (radio_preset < VEHICLE_RADIO_PRESET_MIN_VALUE || |
| 214 | radio_preset >= RADIO_PRESET_NUM) { |
| 215 | ALOGE("%s Invalid radio preset: %d\n", __func__, radio_preset); |
| 216 | return -1; |
| 217 | } |
| 218 | ALOGD("%s Radio Preset number: %d", __func__, radio_preset); |
| 219 | int32_t selector = radio_preset % 2 == 0; |
| 220 | // Populate the channel and subchannel to be some variation of the |
| 221 | // preset number for mocking. |
| 222 | |
| 223 | // Restore the preset number. |
| 224 | data->value.int32_array[0] = radio_preset; |
| 225 | // Channel type values taken from |
| 226 | // system/core/include/system/radio.h |
| 227 | data->value.int32_array[1] = selector ? RADIO_BAND_FM : RADIO_BAND_AM; |
| 228 | // For FM set a value in Mhz and for AM set a value in Khz range |
| 229 | // (channel). |
| 230 | data->value.int32_array[2] = selector ? 99000000 : 100000; |
| 231 | // For FM we have a sub-channel and we care about it, for AM pass |
| 232 | // a dummy value. |
| 233 | data->value.int32_array[3] = selector ? radio_preset : -1; |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | default: |
| 238 | // actual implementation will be much complex than this. It should track proper last |
| 239 | // state. Here just fill with zero. |
| 240 | memset(&(data->value), 0, sizeof(data->value)); |
| 241 | break; |
| 242 | } |
Keun-young Park | 08c255e | 2015-12-09 13:47:30 -0800 | [diff] [blame] | 243 | ALOGI("vdev_get, type 0x%x, time %" PRId64 ", value_type %d", data->prop, data->timestamp, |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 244 | data->value_type); |
| 245 | return 0; |
| 246 | } |
| 247 | |
Keun-young Park | ab68e37 | 2016-03-10 16:28:42 -0800 | [diff] [blame] | 248 | static void vdev_release_memory_from_get(struct vehicle_hw_device* device UNUSED, |
| 249 | vehicle_prop_value_t *data) { |
| 250 | switch (data->value_type) { |
| 251 | case VEHICLE_VALUE_TYPE_STRING: |
| 252 | case VEHICLE_VALUE_TYPE_BYTES: |
| 253 | free(data->value.str_value.data); |
| 254 | data->value.str_value.data = NULL; |
| 255 | break; |
| 256 | default: |
| 257 | ALOGW("release_memory_from_get for property 0x%x which is not string or bytes type 0x%x" |
| 258 | , data->prop, data->value_type); |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | |
Keun-young Park | 08c255e | 2015-12-09 13:47:30 -0800 | [diff] [blame] | 263 | static int vdev_set(vehicle_hw_device_t* device UNUSED, const vehicle_prop_value_t* data) { |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 264 | ALOGD("vdev_set."); |
| 265 | // Just print what data will be setting here. |
| 266 | ALOGD("Setting property %d with value type %d\n", data->prop, data->value_type); |
| 267 | vehicle_prop_config_t* config = find_config(data->prop); |
| 268 | if (config == NULL) { |
| 269 | ALOGE("vdev_set: cannot find config 0x%x", data->prop); |
| 270 | return -EINVAL; |
| 271 | } |
| 272 | if (config->value_type != data->value_type) { |
| 273 | ALOGE("vdev_set: type mismatch, passed 0x%x expecting 0x%x", data->value_type, |
| 274 | config->value_type); |
| 275 | return -EINVAL; |
| 276 | } |
| 277 | switch (data->value_type) { |
| 278 | case VEHICLE_VALUE_TYPE_FLOAT: |
| 279 | ALOGD("Value type: FLOAT\nValue: %f\n", data->value.float_value); |
| 280 | break; |
| 281 | case VEHICLE_VALUE_TYPE_INT32: |
| 282 | ALOGD("Value type: INT32\nValue: %d\n", data->value.int32_value); |
| 283 | break; |
| 284 | case VEHICLE_VALUE_TYPE_INT64: |
| 285 | ALOGD("Value type: INT64\nValue: %lld\n", data->value.int64_value); |
| 286 | break; |
| 287 | case VEHICLE_VALUE_TYPE_BOOLEAN: |
| 288 | ALOGD("Value type: BOOLEAN\nValue: %d\n", data->value.boolean_value); |
| 289 | break; |
| 290 | case VEHICLE_VALUE_TYPE_STRING: |
| 291 | ALOGD("Value type: STRING\n Size: %d\n", data->value.str_value.len); |
| 292 | // NOTE: We only handle ASCII strings here. |
| 293 | // Print the UTF-8 string. |
| 294 | char *ascii_out = (char *) malloc ((data->value.str_value.len + 1) * sizeof (char)); |
| 295 | memcpy(ascii_out, data->value.str_value.data, data->value.str_value.len); |
| 296 | ascii_out[data->value.str_value.len] = '\0'; |
| 297 | ALOGD("Value: %s\n", ascii_out); |
| 298 | break; |
| 299 | case VEHICLE_VALUE_TYPE_INT32_VEC4: |
| 300 | ALOGD("Value type: INT32_VEC4\nValue[0]: %d Value[1] %d Value[2] %d Value[3] %d", |
| 301 | data->value.int32_array[0], data->value.int32_array[1], |
| 302 | data->value.int32_array[2], data->value.int32_array[3]); |
| 303 | break; |
| 304 | default: |
| 305 | ALOGD("Value type not yet handled: %d.\n", data->value_type); |
| 306 | } |
| 307 | return 0; |
| 308 | } |
| 309 | |
Keun-young Park | 08c255e | 2015-12-09 13:47:30 -0800 | [diff] [blame] | 310 | void print_subscribe_info(vehicle_device_impl_t* impl UNUSED) { |
| 311 | unsigned int i; |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 312 | for (i = 0; i < sizeof(CONFIGS) / sizeof(vehicle_prop_config_t); i++) { |
| 313 | subscription_t* sub = (subscription_t*)CONFIGS[i].hal_data; |
| 314 | if (sub != NULL) { |
keunyoung | 4529d7e | 2015-11-13 14:58:01 -0800 | [diff] [blame] | 315 | ALOGD("prop: %d rate: %f", sub->prop, sub->sample_rate); |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // This should be run in a separate thread always. |
| 321 | void fake_event_thread(struct subscription *sub) { |
| 322 | if (!sub) { |
| 323 | ALOGE("oops! subscription object cannot be NULL."); |
| 324 | exit(-1); |
| 325 | } |
| 326 | prctl(PR_SET_NAME, (unsigned long)sub->name, 0, 0, 0); |
| 327 | // Emit values in a loop, every 2 seconds. |
| 328 | while (1) { |
| 329 | // Create a random value depending on the property type. |
| 330 | vehicle_prop_value_t event; |
| 331 | event.prop = sub->prop; |
| 332 | event.timestamp = elapsedRealtimeNano(); |
| 333 | switch (sub->prop) { |
| 334 | case VEHICLE_PROPERTY_DRIVING_STATUS: |
| 335 | event.value_type = VEHICLE_VALUE_TYPE_INT32; |
| 336 | switch ((event.timestamp & 0x30000000)>>28) { |
| 337 | case 0: |
| 338 | event.value.driving_status = VEHICLE_DRIVING_STATUS_UNRESTRICTED; |
| 339 | break; |
| 340 | case 1: |
| 341 | event.value.driving_status = VEHICLE_DRIVING_STATUS_NO_VIDEO; |
| 342 | break; |
| 343 | case 2: |
| 344 | event.value.driving_status = VEHICLE_DRIVING_STATUS_NO_KEYBOARD_INPUT; |
| 345 | break; |
| 346 | default: |
| 347 | event.value.driving_status = VEHICLE_DRIVING_STATUS_NO_CONFIG; |
| 348 | } |
| 349 | break; |
| 350 | case VEHICLE_PROPERTY_GEAR_SELECTION: |
| 351 | event.value_type = VEHICLE_VALUE_TYPE_INT32; |
| 352 | switch ((event.timestamp & 0x30000000)>>28) { |
| 353 | case 0: |
Keun-young Park | 77761e2 | 2016-03-08 15:02:44 -0800 | [diff] [blame] | 354 | event.value.gear_selection = VEHICLE_GEAR_PARK; |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 355 | break; |
| 356 | case 1: |
| 357 | event.value.gear_selection = VEHICLE_GEAR_NEUTRAL; |
| 358 | break; |
| 359 | case 2: |
| 360 | event.value.gear_selection = VEHICLE_GEAR_DRIVE; |
| 361 | break; |
| 362 | case 3: |
| 363 | event.value.gear_selection = VEHICLE_GEAR_REVERSE; |
| 364 | break; |
| 365 | } |
| 366 | break; |
| 367 | case VEHICLE_PROPERTY_PARKING_BRAKE_ON: |
| 368 | event.value_type = VEHICLE_VALUE_TYPE_BOOLEAN; |
| 369 | if (event.timestamp & 0x20000000) { |
| 370 | event.value.parking_brake = VEHICLE_FALSE; |
| 371 | } else { |
| 372 | event.value.parking_brake = VEHICLE_TRUE; |
| 373 | } |
| 374 | break; |
| 375 | case VEHICLE_PROPERTY_PERF_VEHICLE_SPEED: |
| 376 | event.value_type = VEHICLE_VALUE_TYPE_FLOAT; |
| 377 | event.value.vehicle_speed = (float) ((event.timestamp & 0xff000000)>>24); |
| 378 | break; |
| 379 | case VEHICLE_PROPERTY_RADIO_PRESET: |
| 380 | event.value_type = VEHICLE_VALUE_TYPE_INT32_VEC4; |
| 381 | int presetInfo1[4] = {1 /* preset number */, 0 /* AM Band */, 1000, 0}; |
| 382 | int presetInfo2[4] = {2 /* preset number */, 1 /* FM Band */, 1000, 0}; |
| 383 | if (event.timestamp & 0x20000000) { |
| 384 | memcpy(event.value.int32_array, presetInfo1, sizeof(presetInfo1)); |
| 385 | } else { |
| 386 | memcpy(event.value.int32_array, presetInfo2, sizeof(presetInfo2)); |
| 387 | } |
| 388 | break; |
| 389 | default: // unsupported |
Keun-young Park | bf877a7 | 2015-12-21 14:16:05 -0800 | [diff] [blame] | 390 | if (sub->impl == NULL) { |
| 391 | ALOGE("subscription impl NULL"); |
| 392 | return; |
| 393 | } |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 394 | if (sub->impl->error_fn_ != NULL) { |
Keun-young Park | ab68e37 | 2016-03-10 16:28:42 -0800 | [diff] [blame] | 395 | sub->impl->error_fn_(-EINVAL, VEHICLE_PROPERTY_INVALID, |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 396 | VEHICLE_OPERATION_GENERIC); |
| 397 | } else { |
| 398 | ALOGE("Error function is null"); |
| 399 | } |
| 400 | ALOGE("Unsupported prop 0x%x, quit", sub->prop); |
| 401 | return; |
| 402 | } |
| 403 | if (sub->impl->event_fn_ != NULL) { |
| 404 | sub->impl->event_fn_(&event); |
| 405 | } else { |
| 406 | ALOGE("Event function is null"); |
| 407 | return; |
| 408 | } |
| 409 | pthread_mutex_lock(&sub->lock); |
| 410 | if (sub->stop_thread) { |
| 411 | ALOGD("exiting subscription request here."); |
| 412 | // Do any cleanup here. |
| 413 | pthread_mutex_unlock(&sub->lock); |
keunyoung | 4529d7e | 2015-11-13 14:58:01 -0800 | [diff] [blame] | 414 | return; |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 415 | } |
| 416 | struct timespec now; |
| 417 | clock_gettime(CLOCK_REALTIME, &now); |
| 418 | now.tv_sec += 1; // sleep for one sec |
| 419 | pthread_cond_timedwait(&sub->cond, &sub->lock, &now); |
| 420 | pthread_mutex_unlock(&sub->lock); |
| 421 | } |
| 422 | } |
| 423 | |
Keun-young Park | bf877a7 | 2015-12-21 14:16:05 -0800 | [diff] [blame] | 424 | static int vdev_subscribe(vehicle_hw_device_t* device, int32_t prop, float sample_rate, |
Keun-young Park | 77761e2 | 2016-03-08 15:02:44 -0800 | [diff] [blame] | 425 | int32_t zones UNUSED) { |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 426 | ALOGD("vdev_subscribe 0x%x, %f", prop, sample_rate); |
| 427 | vehicle_device_impl_t* impl = (vehicle_device_impl_t*)device; |
| 428 | // Check that the device is initialized. |
| 429 | pthread_mutex_lock(&lock_); |
| 430 | if (!impl->initialized_) { |
| 431 | pthread_mutex_unlock(&lock_); |
| 432 | ALOGE("vdev_subscribe: have you called init()?"); |
| 433 | return -EINVAL; |
| 434 | } |
| 435 | vehicle_prop_config_t* config = find_config(prop); |
| 436 | if (config == NULL) { |
| 437 | pthread_mutex_unlock(&lock_); |
| 438 | ALOGE("vdev_subscribe not supported property 0x%x", prop); |
| 439 | return -EINVAL; |
| 440 | } |
| 441 | if ((config->access != VEHICLE_PROP_ACCESS_READ) && |
| 442 | (config->access != VEHICLE_PROP_ACCESS_READ_WRITE)) { |
| 443 | pthread_mutex_unlock(&lock_); |
| 444 | ALOGE("vdev_subscribe read not supported on the property 0x%x", prop); |
| 445 | return -EINVAL; |
| 446 | } |
| 447 | if (config->change_mode == VEHICLE_PROP_CHANGE_MODE_STATIC) { |
| 448 | pthread_mutex_unlock(&lock_); |
| 449 | ALOGE("vdev_subscribe cannot subscribe static property 0x%x", prop); |
| 450 | return -EINVAL; |
| 451 | } |
| 452 | if ((config->change_mode == VEHICLE_PROP_CHANGE_MODE_ON_CHANGE) && (sample_rate != 0)) { |
| 453 | pthread_mutex_unlock(&lock_); |
| 454 | ALOGE("vdev_subscribe on change type should have 0 sample rate, property 0x%x, sample rate %f", |
| 455 | prop, sample_rate); |
| 456 | return -EINVAL; |
| 457 | } |
| 458 | if ((config->max_sample_rate < sample_rate) || (config->min_sample_rate > sample_rate)) { |
| 459 | |
| 460 | ALOGE("vdev_subscribe property 0x%x, invalid sample rate %f, min:%f, max:%f", |
| 461 | prop, sample_rate, config->min_sample_rate, config->max_sample_rate); |
| 462 | pthread_mutex_unlock(&lock_); |
| 463 | return -EINVAL; |
| 464 | } |
| 465 | subscription_t* sub = (subscription_t*)config->hal_data; |
| 466 | if (sub == NULL) { |
| 467 | sub = calloc(1, sizeof(subscription_t)); |
| 468 | sub->prop = prop; |
| 469 | sub->sample_rate = sample_rate; |
| 470 | sub->stop_thread = 0; |
| 471 | sub->impl = impl; |
| 472 | pthread_mutex_init(&sub->lock, NULL); |
| 473 | pthread_cond_init(&sub->cond, NULL); |
| 474 | config->hal_data = sub; |
| 475 | sprintf(sub->name, "vhal0x%x", prop); |
| 476 | } else if (sub->sample_rate != sample_rate){ // sample rate changed |
| 477 | //TODO notify this to fake sensor thread |
| 478 | sub->sample_rate = sample_rate; |
| 479 | pthread_mutex_unlock(&lock_); |
| 480 | return 0; |
| 481 | } |
| 482 | int ret_code = pthread_create( |
Keun-young Park | 08c255e | 2015-12-09 13:47:30 -0800 | [diff] [blame] | 483 | &sub->thread, NULL, (void *(*)(void*))fake_event_thread, sub); |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 484 | print_subscribe_info(impl); |
| 485 | pthread_mutex_unlock(&lock_); |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static int vdev_unsubscribe(vehicle_hw_device_t* device, int32_t prop) { |
| 490 | ALOGD("vdev_unsubscribe 0x%x", prop); |
| 491 | vehicle_device_impl_t* impl = (vehicle_device_impl_t*)device; |
| 492 | pthread_mutex_lock(&lock_); |
| 493 | vehicle_prop_config_t* config = find_config(prop); |
| 494 | if (config == NULL) { |
| 495 | pthread_mutex_unlock(&lock_); |
| 496 | return -EINVAL; |
| 497 | } |
| 498 | subscription_t* sub = (subscription_t*)config->hal_data; |
| 499 | if (sub == NULL) { |
| 500 | pthread_mutex_unlock(&lock_); |
| 501 | return -EINVAL; |
| 502 | } |
| 503 | config->hal_data = NULL; |
| 504 | pthread_mutex_unlock(&lock_); |
| 505 | pthread_mutex_lock(&sub->lock); |
| 506 | sub->stop_thread = 1; |
| 507 | pthread_cond_signal(&sub->cond); |
| 508 | pthread_mutex_unlock(&sub->lock); |
| 509 | pthread_join(sub->thread, NULL); |
| 510 | pthread_cond_destroy(&sub->cond); |
| 511 | pthread_mutex_destroy(&sub->lock); |
| 512 | free(sub); |
| 513 | pthread_mutex_lock(&lock_); |
| 514 | print_subscribe_info(impl); |
| 515 | pthread_mutex_unlock(&lock_); |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | static int vdev_close(hw_device_t* device) { |
| 520 | vehicle_device_impl_t* impl = (vehicle_device_impl_t*)device; |
| 521 | if (impl) { |
| 522 | free(impl); |
| 523 | return 0; |
| 524 | } else { |
| 525 | return -1; |
| 526 | } |
| 527 | } |
| 528 | |
Keun-young Park | 418c7e8 | 2016-04-06 10:44:20 -0700 | [diff] [blame^] | 529 | static int vdev_dump(struct vehicle_hw_device* device UNUSED, int fd UNUSED) { |
| 530 | //TODO |
| 531 | return 0; |
| 532 | } |
| 533 | |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 534 | /* |
| 535 | * The open function is provided as an interface in harwdare.h which fills in |
| 536 | * all the information about specific implementations and version specific |
| 537 | * informations in hw_device_t structure. After calling open() the client should |
| 538 | * use the hw_device_t to execute any Vehicle HAL device specific functions. |
| 539 | */ |
Keun-young Park | 08c255e | 2015-12-09 13:47:30 -0800 | [diff] [blame] | 540 | static int vdev_open(const hw_module_t* module, const char* name UNUSED, |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 541 | hw_device_t** device) { |
| 542 | ALOGD("vdev_open"); |
| 543 | |
| 544 | // Oops, out of memory! |
| 545 | vehicle_device_impl_t* vdev = calloc(1, sizeof(vehicle_device_impl_t)); |
| 546 | if (vdev == NULL) { |
| 547 | return -ENOMEM; |
| 548 | } |
| 549 | |
| 550 | // Common functions provided by harware.h to access module and device(s). |
| 551 | vdev->vehicle_device.common.tag = HARDWARE_DEVICE_TAG; |
| 552 | vdev->vehicle_device.common.version = VEHICLE_DEVICE_API_VERSION_1_0; |
| 553 | vdev->vehicle_device.common.module = (hw_module_t *) module; |
| 554 | vdev->vehicle_device.common.close = vdev_close; |
| 555 | |
| 556 | // Define the Vehicle HAL device specific functions. |
| 557 | vdev->vehicle_device.list_properties = vdev_list_properties; |
| 558 | vdev->vehicle_device.init = vdev_init; |
| 559 | vdev->vehicle_device.release = vdev_release; |
| 560 | vdev->vehicle_device.get = vdev_get; |
Keun-young Park | ab68e37 | 2016-03-10 16:28:42 -0800 | [diff] [blame] | 561 | vdev->vehicle_device.release_memory_from_get = vdev_release_memory_from_get; |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 562 | vdev->vehicle_device.set = vdev_set; |
| 563 | vdev->vehicle_device.subscribe = vdev_subscribe; |
| 564 | vdev->vehicle_device.unsubscribe = vdev_unsubscribe; |
Keun-young Park | 418c7e8 | 2016-04-06 10:44:20 -0700 | [diff] [blame^] | 565 | vdev->vehicle_device.dump = vdev_dump; |
Sanket Agarwal | fb63668 | 2015-08-11 14:34:29 -0700 | [diff] [blame] | 566 | |
| 567 | *device = (hw_device_t *) vdev; |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static struct hw_module_methods_t hal_module_methods = { |
| 572 | .open = vdev_open, |
| 573 | }; |
| 574 | |
| 575 | /* |
| 576 | * This structure is mandatory to be implemented by each HAL implementation. It |
| 577 | * exposes the open method (see hw_module_methods_t above) which opens a device. |
| 578 | * The vehicle HAL is supposed to be used as a single device HAL hence all the |
| 579 | * functions should be implemented inside of the vehicle_hw_device_t struct (see |
| 580 | * the vehicle.h in include/ folder. |
| 581 | */ |
| 582 | vehicle_module_t HAL_MODULE_INFO_SYM = { |
| 583 | .common = { |
| 584 | .tag = HARDWARE_MODULE_TAG, |
| 585 | .module_api_version = VEHICLE_MODULE_API_VERSION_1_0, |
| 586 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 587 | .id = VEHICLE_HARDWARE_MODULE_ID, |
| 588 | .name = "Default vehicle HW HAL", |
| 589 | .author = "", |
| 590 | .methods = &hal_module_methods, |
| 591 | }, |
| 592 | }; |