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