blob: b5ea44424557e9902c5f4f58cc8bfd7461d2b2f1 [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) {
293 char buf[100];
294 int i;
295 for (i = 0; i < sizeof(CONFIGS) / sizeof(vehicle_prop_config_t); i++) {
296 subscription_t* sub = (subscription_t*)CONFIGS[i].hal_data;
297 if (sub != NULL) {
298 sprintf(buf, "prop: %d rate: %f", sub->prop, sub->sample_rate);
299 ALOGD(buf);
300 }
301 }
302}
303
304// This should be run in a separate thread always.
305void fake_event_thread(struct subscription *sub) {
306 if (!sub) {
307 ALOGE("oops! subscription object cannot be NULL.");
308 exit(-1);
309 }
310 prctl(PR_SET_NAME, (unsigned long)sub->name, 0, 0, 0);
311 // Emit values in a loop, every 2 seconds.
312 while (1) {
313 // Create a random value depending on the property type.
314 vehicle_prop_value_t event;
315 event.prop = sub->prop;
316 event.timestamp = elapsedRealtimeNano();
317 switch (sub->prop) {
318 case VEHICLE_PROPERTY_DRIVING_STATUS:
319 event.value_type = VEHICLE_VALUE_TYPE_INT32;
320 switch ((event.timestamp & 0x30000000)>>28) {
321 case 0:
322 event.value.driving_status = VEHICLE_DRIVING_STATUS_UNRESTRICTED;
323 break;
324 case 1:
325 event.value.driving_status = VEHICLE_DRIVING_STATUS_NO_VIDEO;
326 break;
327 case 2:
328 event.value.driving_status = VEHICLE_DRIVING_STATUS_NO_KEYBOARD_INPUT;
329 break;
330 default:
331 event.value.driving_status = VEHICLE_DRIVING_STATUS_NO_CONFIG;
332 }
333 break;
334 case VEHICLE_PROPERTY_GEAR_SELECTION:
335 event.value_type = VEHICLE_VALUE_TYPE_INT32;
336 switch ((event.timestamp & 0x30000000)>>28) {
337 case 0:
338 event.value.gear_selection = VEHICLE_GEAR_PARKING;
339 break;
340 case 1:
341 event.value.gear_selection = VEHICLE_GEAR_NEUTRAL;
342 break;
343 case 2:
344 event.value.gear_selection = VEHICLE_GEAR_DRIVE;
345 break;
346 case 3:
347 event.value.gear_selection = VEHICLE_GEAR_REVERSE;
348 break;
349 }
350 break;
351 case VEHICLE_PROPERTY_PARKING_BRAKE_ON:
352 event.value_type = VEHICLE_VALUE_TYPE_BOOLEAN;
353 if (event.timestamp & 0x20000000) {
354 event.value.parking_brake = VEHICLE_FALSE;
355 } else {
356 event.value.parking_brake = VEHICLE_TRUE;
357 }
358 break;
359 case VEHICLE_PROPERTY_PERF_VEHICLE_SPEED:
360 event.value_type = VEHICLE_VALUE_TYPE_FLOAT;
361 event.value.vehicle_speed = (float) ((event.timestamp & 0xff000000)>>24);
362 break;
363 case VEHICLE_PROPERTY_RADIO_PRESET:
364 event.value_type = VEHICLE_VALUE_TYPE_INT32_VEC4;
365 int presetInfo1[4] = {1 /* preset number */, 0 /* AM Band */, 1000, 0};
366 int presetInfo2[4] = {2 /* preset number */, 1 /* FM Band */, 1000, 0};
367 if (event.timestamp & 0x20000000) {
368 memcpy(event.value.int32_array, presetInfo1, sizeof(presetInfo1));
369 } else {
370 memcpy(event.value.int32_array, presetInfo2, sizeof(presetInfo2));
371 }
372 break;
373 default: // unsupported
374 if (sub->impl->error_fn_ != NULL) {
375 sub->impl->error_fn_(VEHICLE_ERROR_UNKNOWN, VEHICLE_PROPERTY_INVALID,
376 VEHICLE_OPERATION_GENERIC);
377 } else {
378 ALOGE("Error function is null");
379 }
380 ALOGE("Unsupported prop 0x%x, quit", sub->prop);
381 return;
382 }
383 if (sub->impl->event_fn_ != NULL) {
384 sub->impl->event_fn_(&event);
385 } else {
386 ALOGE("Event function is null");
387 return;
388 }
389 pthread_mutex_lock(&sub->lock);
390 if (sub->stop_thread) {
391 ALOGD("exiting subscription request here.");
392 // Do any cleanup here.
393 pthread_mutex_unlock(&sub->lock);
394 return 0;
395 }
396 struct timespec now;
397 clock_gettime(CLOCK_REALTIME, &now);
398 now.tv_sec += 1; // sleep for one sec
399 pthread_cond_timedwait(&sub->cond, &sub->lock, &now);
400 pthread_mutex_unlock(&sub->lock);
401 }
402}
403
404static int vdev_subscribe(vehicle_hw_device_t* device, int32_t prop, float sample_rate) {
405 ALOGD("vdev_subscribe 0x%x, %f", prop, sample_rate);
406 vehicle_device_impl_t* impl = (vehicle_device_impl_t*)device;
407 // Check that the device is initialized.
408 pthread_mutex_lock(&lock_);
409 if (!impl->initialized_) {
410 pthread_mutex_unlock(&lock_);
411 ALOGE("vdev_subscribe: have you called init()?");
412 return -EINVAL;
413 }
414 vehicle_prop_config_t* config = find_config(prop);
415 if (config == NULL) {
416 pthread_mutex_unlock(&lock_);
417 ALOGE("vdev_subscribe not supported property 0x%x", prop);
418 return -EINVAL;
419 }
420 if ((config->access != VEHICLE_PROP_ACCESS_READ) &&
421 (config->access != VEHICLE_PROP_ACCESS_READ_WRITE)) {
422 pthread_mutex_unlock(&lock_);
423 ALOGE("vdev_subscribe read not supported on the property 0x%x", prop);
424 return -EINVAL;
425 }
426 if (config->change_mode == VEHICLE_PROP_CHANGE_MODE_STATIC) {
427 pthread_mutex_unlock(&lock_);
428 ALOGE("vdev_subscribe cannot subscribe static property 0x%x", prop);
429 return -EINVAL;
430 }
431 if ((config->change_mode == VEHICLE_PROP_CHANGE_MODE_ON_CHANGE) && (sample_rate != 0)) {
432 pthread_mutex_unlock(&lock_);
433 ALOGE("vdev_subscribe on change type should have 0 sample rate, property 0x%x, sample rate %f",
434 prop, sample_rate);
435 return -EINVAL;
436 }
437 if ((config->max_sample_rate < sample_rate) || (config->min_sample_rate > sample_rate)) {
438
439 ALOGE("vdev_subscribe property 0x%x, invalid sample rate %f, min:%f, max:%f",
440 prop, sample_rate, config->min_sample_rate, config->max_sample_rate);
441 pthread_mutex_unlock(&lock_);
442 return -EINVAL;
443 }
444 subscription_t* sub = (subscription_t*)config->hal_data;
445 if (sub == NULL) {
446 sub = calloc(1, sizeof(subscription_t));
447 sub->prop = prop;
448 sub->sample_rate = sample_rate;
449 sub->stop_thread = 0;
450 sub->impl = impl;
451 pthread_mutex_init(&sub->lock, NULL);
452 pthread_cond_init(&sub->cond, NULL);
453 config->hal_data = sub;
454 sprintf(sub->name, "vhal0x%x", prop);
455 } else if (sub->sample_rate != sample_rate){ // sample rate changed
456 //TODO notify this to fake sensor thread
457 sub->sample_rate = sample_rate;
458 pthread_mutex_unlock(&lock_);
459 return 0;
460 }
461 int ret_code = pthread_create(
462 &sub->thread, NULL, fake_event_thread, sub);
463 print_subscribe_info(impl);
464 pthread_mutex_unlock(&lock_);
465 return 0;
466}
467
468static int vdev_unsubscribe(vehicle_hw_device_t* device, int32_t prop) {
469 ALOGD("vdev_unsubscribe 0x%x", prop);
470 vehicle_device_impl_t* impl = (vehicle_device_impl_t*)device;
471 pthread_mutex_lock(&lock_);
472 vehicle_prop_config_t* config = find_config(prop);
473 if (config == NULL) {
474 pthread_mutex_unlock(&lock_);
475 return -EINVAL;
476 }
477 subscription_t* sub = (subscription_t*)config->hal_data;
478 if (sub == NULL) {
479 pthread_mutex_unlock(&lock_);
480 return -EINVAL;
481 }
482 config->hal_data = NULL;
483 pthread_mutex_unlock(&lock_);
484 pthread_mutex_lock(&sub->lock);
485 sub->stop_thread = 1;
486 pthread_cond_signal(&sub->cond);
487 pthread_mutex_unlock(&sub->lock);
488 pthread_join(sub->thread, NULL);
489 pthread_cond_destroy(&sub->cond);
490 pthread_mutex_destroy(&sub->lock);
491 free(sub);
492 pthread_mutex_lock(&lock_);
493 print_subscribe_info(impl);
494 pthread_mutex_unlock(&lock_);
495 return 0;
496}
497
498static int vdev_close(hw_device_t* device) {
499 vehicle_device_impl_t* impl = (vehicle_device_impl_t*)device;
500 if (impl) {
501 free(impl);
502 return 0;
503 } else {
504 return -1;
505 }
506}
507
508/*
509 * The open function is provided as an interface in harwdare.h which fills in
510 * all the information about specific implementations and version specific
511 * informations in hw_device_t structure. After calling open() the client should
512 * use the hw_device_t to execute any Vehicle HAL device specific functions.
513 */
514static int vdev_open(const hw_module_t* module, const char* __unused name,
515 hw_device_t** device) {
516 ALOGD("vdev_open");
517
518 // Oops, out of memory!
519 vehicle_device_impl_t* vdev = calloc(1, sizeof(vehicle_device_impl_t));
520 if (vdev == NULL) {
521 return -ENOMEM;
522 }
523
524 // Common functions provided by harware.h to access module and device(s).
525 vdev->vehicle_device.common.tag = HARDWARE_DEVICE_TAG;
526 vdev->vehicle_device.common.version = VEHICLE_DEVICE_API_VERSION_1_0;
527 vdev->vehicle_device.common.module = (hw_module_t *) module;
528 vdev->vehicle_device.common.close = vdev_close;
529
530 // Define the Vehicle HAL device specific functions.
531 vdev->vehicle_device.list_properties = vdev_list_properties;
532 vdev->vehicle_device.init = vdev_init;
533 vdev->vehicle_device.release = vdev_release;
534 vdev->vehicle_device.get = vdev_get;
535 vdev->vehicle_device.set = vdev_set;
536 vdev->vehicle_device.subscribe = vdev_subscribe;
537 vdev->vehicle_device.unsubscribe = vdev_unsubscribe;
538
539 *device = (hw_device_t *) vdev;
540 return 0;
541}
542
543static struct hw_module_methods_t hal_module_methods = {
544 .open = vdev_open,
545};
546
547/*
548 * This structure is mandatory to be implemented by each HAL implementation. It
549 * exposes the open method (see hw_module_methods_t above) which opens a device.
550 * The vehicle HAL is supposed to be used as a single device HAL hence all the
551 * functions should be implemented inside of the vehicle_hw_device_t struct (see
552 * the vehicle.h in include/ folder.
553 */
554vehicle_module_t HAL_MODULE_INFO_SYM = {
555 .common = {
556 .tag = HARDWARE_MODULE_TAG,
557 .module_api_version = VEHICLE_MODULE_API_VERSION_1_0,
558 .hal_api_version = HARDWARE_HAL_API_VERSION,
559 .id = VEHICLE_HARDWARE_MODULE_ID,
560 .name = "Default vehicle HW HAL",
561 .author = "",
562 .methods = &hal_module_methods,
563 },
564};