los-15.1 update

This commit is contained in:
leonardo meitz 2018-05-24 21:43:45 +02:00
parent be138c1f2e
commit c61f6f682c
19 changed files with 1321 additions and 254 deletions

3
Android.bp Normal file
View file

@ -0,0 +1,3 @@
subdirs = [
"sensors",
]

19
camera/Android.mk Normal file
View file

@ -0,0 +1,19 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := \
system/media/camera/include \
frameworks/native/include/media/openmax
LOCAL_SRC_FILES := \
CameraWrapper.cpp
LOCAL_SHARED_LIBRARIES := \
libhardware liblog libcamera_client libutils libcutils libgui libsensor
LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR_SHARED_LIBRARIES)/hw
LOCAL_MODULE := camera.msm8226
LOCAL_MODULE_TAGS := optional
LOCAL_PROPRIETARY_MODULE := true
include $(BUILD_SHARED_LIBRARY)

View file

@ -1,19 +0,0 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := \
system/media/camera/include
LOCAL_SRC_FILES := \
CameraWrapper.cpp
LOCAL_SHARED_LIBRARIES := \
libhardware liblog libcamera_client libutils libdl \
android.hidl.token@1.0-utils \
android.hardware.graphics.bufferqueue@1.0
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_MODULE := camera.msm8226
LOCAL_MODULE_TAGS := optional
include $(BUILD_SHARED_LIBRARY)

View file

@ -1,5 +1,6 @@
/*
* Copyright (C) 2014, The CyanogenMod Project
* Copyright (C) 2016 The CyanogenMod Project
* Copyright (C) 2018 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,28 +23,39 @@
*/
//#define LOG_NDEBUG 0
#define LOG_TAG "CameraWrapper"
#include <cutils/log.h>
#include <utils/threads.h>
#include <utils/String8.h>
#include <hardware/hardware.h>
#include <hardware/camera.h>
#include <camera/Camera.h>
#include <camera/CameraParameters2.h>
#include <camera/CameraParameters.h>
static android::Mutex gCameraWrapperLock;
#include "CameraWrapper.h"
static const char PIXEL_FORMAT_YUV420SP_NV21E[] = "yuv420sp-nv21e";
#define BACK_CAMERA 0
#define FRONT_CAMERA 1
using namespace android;
static Mutex gCameraWrapperLock;
static camera_module_t *gVendorModule = 0;
static camera_notify_callback gUserNotifyCb = NULL;
static camera_data_callback gUserDataCb = NULL;
static camera_data_timestamp_callback gUserDataCbTimestamp = NULL;
static camera_request_memory gUserGetMemory = NULL;
static void *gUserCameraDevice = NULL;
static char **fixed_set_params = NULL;
static int camera_device_open(const hw_module_t *module, const char *name,
hw_device_t **device);
static int camera_get_number_of_cameras(void);
static int camera_get_camera_info(int camera_id, struct camera_info *info);
static int camera_send_command(struct camera_device *device, int32_t cmd,
int32_t arg1, int32_t arg2);
static struct hw_module_methods_t camera_module_methods = {
.open = camera_device_open
@ -51,26 +63,30 @@ static struct hw_module_methods_t camera_module_methods = {
camera_module_t HAL_MODULE_INFO_SYM = {
.common = {
.tag = HARDWARE_MODULE_TAG,
.module_api_version = CAMERA_MODULE_API_VERSION_1_0,
.hal_api_version = HARDWARE_HAL_API_VERSION,
.id = CAMERA_HARDWARE_MODULE_ID,
.name = "Samsung MSM8226 Camera Wrapper",
.author = "The CyanogenMod Project",
.methods = &camera_module_methods,
.dso = NULL, /* remove compilation warnings */
.reserved = {0}, /* remove compilation warnings */
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor = 0,
.id = CAMERA_HARDWARE_MODULE_ID,
.name = "MSM8226 Camera Wrapper",
.author = "The LineageOS Project",
.methods = &camera_module_methods,
.dso = NULL, /* remove compilation warnings */
.reserved = {0}, /* remove compilation warnings */
},
.get_number_of_cameras = camera_get_number_of_cameras,
.get_camera_info = camera_get_camera_info,
.set_callbacks = NULL, /* remove compilation warnings */
.get_vendor_tag_ops = NULL, /* remove compilation warnings */
.open_legacy = NULL, /* remove compilation warnings */
.set_torch_mode = camera_set_torch_mode,
.init = NULL, /* remove compilation warnings */
.reserved = {0}, /* remove compilation warnings */
};
typedef struct wrapper_camera_device {
camera_device_t base;
int camera_released;
int id;
camera_device_t *vendor;
} wrapper_camera_device_t;
@ -91,21 +107,16 @@ static int check_vendor_module()
return 0;
rv = hw_get_module_by_class("camera", "vendor",
(const hw_module_t **)&gVendorModule);
(const hw_module_t**)&gVendorModule);
if (rv)
ALOGE("failed to open vendor camera module");
return rv;
}
#define KEY_VIDEO_HFR_VALUES "video-hfr-values"
const static char * iso_values[] = {"auto,ISO_HJR,ISO100,ISO200,ISO400,ISO800,ISO1600,auto"};
static char *camera_fixup_getparams(int id, const char *settings)
{
android::CameraParameters params;
params.unflatten(android::String8(settings));
CameraParameters params;
params.unflatten(String8(settings));
#if !LOG_NDEBUG
ALOGV("%s: original parameters:", __FUNCTION__);
@ -113,85 +124,50 @@ static char *camera_fixup_getparams(int id, const char *settings)
#endif
// fix params here
params.set(KEY_SUPPORTED_ISO_MODES, iso_values[id]);
params.set(KEY_EXPOSURE_COMPENSATION_STEP, "0.5");
params.set(KEY_MIN_EXPOSURE_COMPENSATION, "-4");
params.set(KEY_MAX_EXPOSURE_COMPENSATION, "4");
params.set(android::CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP, "0.5");
params.set(android::CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION, "-2");
params.set(android::CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION, "2");
/* If the vendor has HFR values but doesn't also expose that
* this can be turned off, fixup the params to tell the Camera
* that it really is okay to turn it off.
*/
const char *hfrValues = params.get(KEY_VIDEO_HFR_VALUES);
if (hfrValues && *hfrValues && ! strstr(hfrValues, "off")) {
char tmp[strlen(hfrValues) + 4 + 1];
sprintf(tmp, "%s,off", hfrValues);
params.set(KEY_VIDEO_HFR_VALUES, tmp);
}
params.set(android::CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES, "640x360,640x480,352x288,320x240,176x144");
/* Enforce video-snapshot-supported to true */
params.set(KEY_VIDEO_SNAPSHOT_SUPPORTED, "true");
android::String8 strParams = params.flatten();
char *ret = strdup(strParams.string());
params.set(android::CameraParameters::KEY_VIDEO_SNAPSHOT_SUPPORTED, "true");
#if !LOG_NDEBUG
ALOGV("%s: fixed parameters:", __FUNCTION__);
params.dump();
#endif
String8 strParams = params.flatten();
char *ret = strdup(strParams.string());
return ret;
}
static char *camera_fixup_setparams(struct camera_device *device, const char *settings)
static char *camera_fixup_setparams(int id, const char *settings)
{
int id = CAMERA_ID(device);
android::CameraParameters params;
params.unflatten(android::String8(settings));
CameraParameters params;
params.unflatten(String8(settings));
#if !LOG_NDEBUG
ALOGV("%s: original parameters:", __FUNCTION__);
params.dump();
#endif
const char *recordingHint = params.get(KEY_RECORDING_HINT);
bool isVideo = recordingHint && !strcmp(recordingHint, "true");
if (isVideo) {
params.set(KEY_DIS, DIS_DISABLE);
params.set(KEY_ZSL, ZSL_OFF);
} else {
params.set(KEY_ZSL, ZSL_ON);
}
// fix params here
// No need to fix-up ISO_HJR, it is the same for userspace and the camera lib
if (params.get("iso")) {
const char *isoMode = params.get(KEY_ISO_MODE);
if (strcmp(isoMode, "ISO100") == 0)
params.set(KEY_ISO_MODE, "100");
else if (strcmp(isoMode, "ISO200") == 0)
params.set(KEY_ISO_MODE, "200");
else if (strcmp(isoMode, "ISO400") == 0)
params.set(KEY_ISO_MODE, "400");
else if (strcmp(isoMode, "ISO800") == 0)
params.set(KEY_ISO_MODE, "800");
else if (strcmp(isoMode, "ISO1600") == 0)
params.set(KEY_ISO_MODE, "1600");
}
android::String8 strParams = params.flatten();
if (fixed_set_params[id])
free(fixed_set_params[id]);
fixed_set_params[id] = strdup(strParams.string());
char *ret = fixed_set_params[id];
params.set(android::CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES, "640x360,640x480,528x432,352x288,320x240,176x144");
params.set(android::CameraParameters::KEY_PREVIEW_FPS_RANGE, "7500,30000");
#if !LOG_NDEBUG
ALOGV("%s: fixed parameters:", __FUNCTION__);
params.dump();
#endif
String8 strParams = params.flatten();
if (fixed_set_params[id])
free(fixed_set_params[id]);
fixed_set_params[id] = strdup(strParams.string());
char *ret = fixed_set_params[id];
return ret;
}
@ -202,15 +178,34 @@ static char *camera_fixup_setparams(struct camera_device *device, const char *se
static int camera_set_preview_window(struct camera_device *device,
struct preview_stream_ops *window)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
return VENDOR_CALL(device, set_preview_window, window);
}
void camera_notify_cb(int32_t msg_type, int32_t ext1, int32_t ext2, void *user) {
gUserNotifyCb(msg_type, ext1, ext2, gUserCameraDevice);
}
void camera_data_cb(int32_t msg_type, const camera_memory_t *data, unsigned int index,
camera_frame_metadata_t *metadata, void *user) {
gUserDataCb(msg_type, data, index, metadata, gUserCameraDevice);
}
void camera_data_cb_timestamp(nsecs_t timestamp, int32_t msg_type,
const camera_memory_t *data, unsigned index, void *user) {
gUserDataCbTimestamp(timestamp, msg_type, data, index, gUserCameraDevice);
}
camera_memory_t* camera_get_memory(int fd, size_t buf_size,
uint_t num_bufs, void *user) {
return gUserGetMemory(fd, buf_size, num_bufs, gUserCameraDevice);
}
static void camera_set_callbacks(struct camera_device *device,
camera_notify_callback notify_cb,
camera_data_callback data_cb,
@ -218,195 +213,203 @@ static void camera_set_callbacks(struct camera_device *device,
camera_request_memory get_memory,
void *user)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return;
VENDOR_CALL(device, set_callbacks, notify_cb, data_cb, data_cb_timestamp, get_memory, user);
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
gUserNotifyCb = notify_cb;
gUserDataCb = data_cb;
gUserDataCbTimestamp = data_cb_timestamp;
gUserGetMemory = get_memory;
gUserCameraDevice = user;
VENDOR_CALL(device, set_callbacks, camera_notify_cb, camera_data_cb,
camera_data_cb_timestamp, camera_get_memory, user);
}
static void camera_enable_msg_type(struct camera_device *device,
int32_t msg_type)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
VENDOR_CALL(device, enable_msg_type, msg_type);
}
static void camera_disable_msg_type(struct camera_device *device,
int32_t msg_type)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
VENDOR_CALL(device, disable_msg_type, msg_type);
}
static int camera_msg_type_enabled(struct camera_device *device,
int32_t msg_type)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return 0;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
return VENDOR_CALL(device, msg_type_enabled, msg_type);
}
static int camera_start_preview(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
return VENDOR_CALL(device, start_preview);
}
static void camera_stop_preview(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
VENDOR_CALL(device, stop_preview);
}
static int camera_preview_enabled(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
return VENDOR_CALL(device, preview_enabled);
}
static int camera_store_meta_data_in_buffers(struct camera_device *device,
int enable)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
return VENDOR_CALL(device, store_meta_data_in_buffers, enable);
}
static int camera_start_recording(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
return VENDOR_CALL(device, start_recording);
}
static void camera_stop_recording(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
VENDOR_CALL(device, stop_recording);
}
static int camera_recording_enabled(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
return VENDOR_CALL(device, recording_enabled);
}
static void camera_release_recording_frame(struct camera_device *device,
const void *opaque)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
VENDOR_CALL(device, release_recording_frame, opaque);
}
static int camera_auto_focus(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
return VENDOR_CALL(device, auto_focus);
}
static int camera_cancel_auto_focus(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
return VENDOR_CALL(device, cancel_auto_focus);
}
static int camera_take_picture(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
return VENDOR_CALL(device, take_picture);
}
static int camera_cancel_picture(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
return VENDOR_CALL(device, cancel_picture);
}
static int camera_set_parameters(struct camera_device *device, const char *params)
static int camera_set_parameters(struct camera_device *device,
const char *params)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
char *tmp = NULL;
tmp = camera_fixup_setparams(device, params);
tmp = camera_fixup_setparams(CAMERA_ID(device), params);
int ret = VENDOR_CALL(device, set_parameters, tmp);
return ret;
@ -414,26 +417,22 @@ static int camera_set_parameters(struct camera_device *device, const char *param
static char *camera_get_parameters(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return NULL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
char *params = VENDOR_CALL(device, get_parameters);
char *tmp = camera_fixup_getparams(CAMERA_ID(device), params);
VENDOR_CALL(device, put_parameters, params);
params = tmp;
return params;
}
static void camera_put_parameters(struct camera_device *device, char *params)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (params)
free(params);
}
@ -441,34 +440,40 @@ static void camera_put_parameters(struct camera_device *device, char *params)
static int camera_send_command(struct camera_device *device,
int32_t cmd, int32_t arg1, int32_t arg2)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
return VENDOR_CALL(device, send_command, cmd, arg1, arg2);
}
static void camera_release(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
wrapper_camera_device_t* wrapper_dev = NULL;
if (!device)
return;
wrapper_dev = (wrapper_camera_device_t*) device;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
VENDOR_CALL(device, release);
wrapper_dev->camera_released = true;
}
static int camera_dump(struct camera_device *device, int fd)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
return VENDOR_CALL(device, dump, fd);
}
@ -481,7 +486,7 @@ static int camera_device_close(hw_device_t *device)
ALOGV("%s", __FUNCTION__);
android::Mutex::Autolock lock(gCameraWrapperLock);
Mutex::Autolock lock(gCameraWrapperLock);
if (!device) {
ret = -EINVAL;
@ -495,6 +500,15 @@ static int camera_device_close(hw_device_t *device)
wrapper_dev = (wrapper_camera_device_t*) device;
if (!wrapper_dev->camera_released) {
ALOGI("%s: releasing camera device with id %d", __FUNCTION__,
wrapper_dev->id);
VENDOR_CALL(wrapper_dev, release);
wrapper_dev->camera_released = true;
}
wrapper_dev->vendor->common.close((hw_device_t*)wrapper_dev->vendor);
if (wrapper_dev->base.ops)
free(wrapper_dev->base.ops);
@ -525,7 +539,7 @@ static int camera_device_open(const hw_module_t *module, const char *name,
wrapper_camera_device_t *camera_device = NULL;
camera_device_ops_t *camera_ops = NULL;
android::Mutex::Autolock lock(gCameraWrapperLock);
Mutex::Autolock lock(gCameraWrapperLock);
ALOGV("%s", __FUNCTION__);
@ -559,6 +573,7 @@ static int camera_device_open(const hw_module_t *module, const char *name,
goto fail;
}
memset(camera_device, 0, sizeof(*camera_device));
camera_device->camera_released = false;
camera_device->id = cameraid;
rv = gVendorModule->common.methods->open(
@ -581,7 +596,7 @@ static int camera_device_open(const hw_module_t *module, const char *name,
memset(camera_ops, 0, sizeof(*camera_ops));
camera_device->base.common.tag = HARDWARE_DEVICE_TAG;
camera_device->base.common.version = HARDWARE_DEVICE_API_VERSION(1, 0);
camera_device->base.common.version = CAMERA_MODULE_API_VERSION_1_0;
camera_device->base.common.module = (hw_module_t *)(module);
camera_device->base.common.close = camera_device_close;
camera_device->base.ops = camera_ops;
@ -643,3 +658,11 @@ static int camera_get_camera_info(int camera_id, struct camera_info *info)
return 0;
return gVendorModule->get_camera_info(camera_id, info);
}
static int camera_set_torch_mode(const char* camera_id, bool enabled)
{
ALOGV("%s", __FUNCTION__);
if (check_vendor_module())
return 0;
return gVendorModule->set_torch_mode(camera_id, enabled);
}

19
camera/CameraWrapper.h Normal file
View file

@ -0,0 +1,19 @@
/*
* Copyright (C) 2018, The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <hardware/camera.h>
static int camera_set_torch_mode(const char* camera_id, bool enabled);

1
configs/_hals.conf Normal file
View file

@ -0,0 +1 @@
sensors.vendor.msm8226.so

View file

@ -48,13 +48,13 @@ PRODUCT_PACKAGES += \
tinymix
PRODUCT_PACKAGES += \
libwvm_shim
libwvm_shim \
imx175_shim
# Camera
PRODUCT_PACKAGES += \
android.hardware.camera.provider@2.4-impl \
camera.device@3.2-impl \
camera.vendor.msm8226 \
camera.msm8226 \
libboringssl-compat \
libxml2 \
@ -157,8 +157,12 @@ PRODUCT_PACKAGES += \
# Sensor HAL
PRODUCT_PACKAGES += \
android.hardware.sensors@1.0-impl
android.hardware.sensors@1.0-impl \
sensors.msm8226
# Sensors
PRODUCT_COPY_FILES += \
$(LOCAL_PATH)/configs/_hals.conf:$(TARGET_COPY_OUT_VENDOR)/etc/sensors/_hals.conf
# Bluetooth
PRODUCT_PACKAGES += \

View file

@ -1,5 +1,5 @@
#
# Copyright 2015 The CyanogenMod Project
# Copyright 2016 The CyanogenMod Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -15,11 +15,12 @@
#
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_C_INCLUDES := $(TARGET_POWERHAL_HEADER_PATH)
LOCAL_SRC_FILES := power.c
LOCAL_SHARED_LIBRARIES := liblog libcutils
LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := power.$(TARGET_BOARD_PLATFORM)
LOCAL_MODULE := power.msm8226
LOCAL_PROPRIETARY_MODULE := true
include $(BUILD_SHARED_LIBRARY)

View file

@ -1,5 +1,5 @@
/*
* Copyright (C) 2015 The CyanogenMod Project
* Copyright (C) 2016 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,27 +18,23 @@
#include <hardware/hardware.h>
#include <hardware/power.h>
#include <stdbool.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <utils/Log.h>
#include "power.h"
#define CPUFREQ_PATH "/sys/devices/system/cpu/cpu0/cpufreq/"
#define INTERACTIVE_PATH "/sys/devices/system/cpu/cpufreq/interactive/"
/* touchkeys */
#define TK_POWER "/sys/class/input/input1/enabled"
/* touchscreen */
#define TS_POWER "/sys/class/input/input2/enabled"
#define CPUFREQ_PATH "/sys/devices/system/cpu/cpu0/cpufreq/"
#define INTERACTIVE_PATH "/sys/devices/system/cpu/cpufreq/interactive/"
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static int boostpulse_fd = -1;
@ -78,15 +74,6 @@ static int sysfs_write_int(char *path, int value)
return sysfs_write_str(path, buf);
}
static bool check_governor(void)
{
struct stat s;
int err = stat(INTERACTIVE_PATH, &s);
if (err != 0) return false;
if (S_ISDIR(s.st_mode)) return true;
return false;
}
static int is_profile_valid(int profile)
{
return profile >= 0 && profile < PROFILE_MAX;
@ -123,23 +110,24 @@ static void power_set_interactive(__attribute__((unused)) struct power_module *m
power_set_interactive_ext(on);
// break out early if governor is not interactive
if (!check_governor()) return;
if (on) {
sysfs_write_int(INTERACTIVE_PATH "hispeed_freq",
profiles[current_power_profile].hispeed_freq);
sysfs_write_int(INTERACTIVE_PATH "go_hispeed_load",
profiles[current_power_profile].go_hispeed_load);
sysfs_write_str(INTERACTIVE_PATH "target_loads",
sysfs_write_int(INTERACTIVE_PATH "target_loads",
profiles[current_power_profile].target_loads);
sysfs_write_int(CPUFREQ_PATH "scaling_min_freq",
profiles[current_power_profile].scaling_min_freq);
} else {
sysfs_write_int(INTERACTIVE_PATH "hispeed_freq",
profiles[current_power_profile].hispeed_freq_off);
sysfs_write_int(INTERACTIVE_PATH "go_hispeed_load",
profiles[current_power_profile].go_hispeed_load_off);
sysfs_write_str(INTERACTIVE_PATH "target_loads",
sysfs_write_int(INTERACTIVE_PATH "target_loads",
profiles[current_power_profile].target_loads_off);
sysfs_write_int(CPUFREQ_PATH "scaling_min_freq",
profiles[current_power_profile].scaling_min_freq_off);
}
}
@ -150,9 +138,6 @@ static void set_power_profile(int profile)
return;
}
// break out early if governor is not interactive
if (!check_governor()) return;
if (profile == current_power_profile)
return;
@ -166,16 +151,18 @@ static void set_power_profile(int profile)
profiles[profile].go_hispeed_load);
sysfs_write_int(INTERACTIVE_PATH "hispeed_freq",
profiles[profile].hispeed_freq);
sysfs_write_int(INTERACTIVE_PATH "io_is_busy",
profiles[profile].io_is_busy);
sysfs_write_int(INTERACTIVE_PATH "min_sample_time",
profiles[profile].min_sample_time);
sysfs_write_int(INTERACTIVE_PATH "sampling_down_factor",
profiles[profile].sampling_down_factor);
sysfs_write_str(INTERACTIVE_PATH "target_loads",
sysfs_write_int(INTERACTIVE_PATH "timer_rate",
profiles[profile].timer_rate);
sysfs_write_int(INTERACTIVE_PATH "above_hispeed_delay",
profiles[profile].above_hispeed_delay);
sysfs_write_int(INTERACTIVE_PATH "target_loads",
profiles[profile].target_loads);
sysfs_write_int(CPUFREQ_PATH "scaling_max_freq",
profiles[profile].scaling_max_freq);
sysfs_write_int(CPUFREQ_PATH "scaling_min_freq",
profiles[profile].scaling_min_freq);
current_power_profile = profile;
}
@ -196,9 +183,6 @@ static void power_hint(__attribute__((unused)) struct power_module *module,
if (!profiles[current_power_profile].boostpulse_duration)
return;
// break out early if governor is not interactive
if (!check_governor()) return;
if (boostpulse_open() >= 0) {
snprintf(buf, sizeof(buf), "%d", 1);
len = write(boostpulse_fd, &buf, sizeof(buf));
@ -226,10 +210,6 @@ static void power_hint(__attribute__((unused)) struct power_module *module,
}
}
static struct hw_module_methods_t power_module_methods = {
.open = NULL,
};
static int get_feature(__attribute__((unused)) struct power_module *module,
feature_t feature)
{
@ -239,6 +219,43 @@ static int get_feature(__attribute__((unused)) struct power_module *module,
return -1;
}
static int power_open(const hw_module_t* module, const char* name,
hw_device_t** device)
{
ALOGD("%s: enter; name=%s", __FUNCTION__, name);
if (strcmp(name, POWER_HARDWARE_MODULE_ID)) {
return -EINVAL;
}
power_module_t *dev = (power_module_t *)calloc(1,
sizeof(power_module_t));
if (!dev) {
ALOGD("%s: failed to allocate memory", __FUNCTION__);
return -ENOMEM;
}
dev->common.tag = HARDWARE_MODULE_TAG;
dev->common.module_api_version = POWER_MODULE_API_VERSION_0_2;
dev->common.hal_api_version = HARDWARE_HAL_API_VERSION;
dev->init = power_init;
dev->powerHint = power_hint; // This is handled by framework
dev->setInteractive = power_set_interactive;
dev->getFeature = get_feature;
*device = (hw_device_t*)dev;
ALOGD("%s: exit", __FUNCTION__);
return 0;
}
static struct hw_module_methods_t power_module_methods = {
.open = power_open,
};
struct power_module HAL_MODULE_INFO_SYM = {
.common = {
.tag = HARDWARE_MODULE_TAG,
@ -246,7 +263,7 @@ struct power_module HAL_MODULE_INFO_SYM = {
.hal_api_version = HARDWARE_HAL_API_VERSION,
.id = POWER_HARDWARE_MODULE_ID,
.name = "msm8226 Power HAL",
.author = "Gabriele M",
.author = "The LineageOS Project",
.methods = &power_module_methods,
},

View file

@ -1,5 +1,5 @@
/*
* Copyright (C) 2015 The CyanogenMod Project
* Copyright (C) 2016 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -30,12 +30,14 @@ typedef struct governor_settings {
int go_hispeed_load_off;
int hispeed_freq;
int hispeed_freq_off;
int io_is_busy;
int min_sample_time;
int sampling_down_factor;
char *target_loads;
char *target_loads_off;
int timer_rate;
int above_hispeed_delay;
int target_loads;
int target_loads_off;
int scaling_max_freq;
int scaling_min_freq;
int scaling_min_freq_off;
} power_profile;
static power_profile profiles[PROFILE_MAX] = {
@ -44,55 +46,65 @@ static power_profile profiles[PROFILE_MAX] = {
.boostpulse_duration = 0,
.go_hispeed_load = 90,
.go_hispeed_load_off = 90,
.hispeed_freq = 787200,
.hispeed_freq_off = 787200,
.io_is_busy = 0,
.hispeed_freq = 800000,
.hispeed_freq_off = 800000,
.min_sample_time = 60000,
.sampling_down_factor = 100000,
.target_loads = "95 1401600:99",
.target_loads_off = "95 1401600:99",
.scaling_max_freq = 787200,
.timer_rate = 20000,
.above_hispeed_delay = 20000,
.target_loads = 90,
.target_loads_off = 90,
.scaling_max_freq = 998400,
.scaling_min_freq = 400000,
.scaling_min_freq_off = 200000,
},
[PROFILE_BALANCED] = {
.boost = 0,
.boostpulse_duration = 60000,
.go_hispeed_load = 50,
.go_hispeed_load = 80,
.go_hispeed_load_off = 90,
.hispeed_freq = 998400,
.hispeed_freq_off = 787200,
.io_is_busy = 1,
.hispeed_freq_off = 800000,
.min_sample_time = 60000,
.sampling_down_factor = 100000,
.target_loads = "80 998400:90 1401600:99",
.target_loads_off = "95 1401600:99",
.scaling_max_freq = 1401600,
.timer_rate = 20000,
.above_hispeed_delay = 20000,
.target_loads = 80,
.target_loads_off = 90,
.scaling_max_freq = 1209600,
.scaling_min_freq = 800000,
.scaling_min_freq_off = 200000,
},
[PROFILE_HIGH_PERFORMANCE] = {
.boost = 1,
.boostpulse_duration = 0, /* prevent unnecessary write */
.go_hispeed_load = 50,
.go_hispeed_load_off = 50,
/* The CPU is already boosted, set duration to zero
* to avoid unneccessary writes to boostpulse */
.boostpulse_duration = 0,
.go_hispeed_load = 60,
.go_hispeed_load_off = 70,
.hispeed_freq = 998400,
.hispeed_freq_off = 998400,
.io_is_busy = 1,
.min_sample_time = 60000,
.sampling_down_factor = 100000,
.target_loads = "80",
.target_loads_off = "80",
.scaling_max_freq = 1401600,
.timer_rate = 20000,
.above_hispeed_delay = 20000,
.target_loads = 60,
.target_loads_off = 70,
.scaling_max_freq = 1209600,
.scaling_min_freq = 800000,
.scaling_min_freq_off = 200000,
},
[PROFILE_BIAS_POWER_SAVE] = {
.boost = 0,
.boostpulse_duration = 0,
.boostpulse_duration = 40000,
.go_hispeed_load = 90,
.go_hispeed_load_off = 90,
.hispeed_freq = 787200,
.hispeed_freq_off = 787200,
.io_is_busy = 0,
.hispeed_freq = 800000,
.hispeed_freq_off = 800000,
.min_sample_time = 60000,
.sampling_down_factor = 100000,
.target_loads = "95 1401600:99",
.target_loads_off = "95 1401600:99",
.scaling_max_freq = 1401600,
.timer_rate = 20000,
.above_hispeed_delay = 20000,
.target_loads = 90,
.target_loads_off = 90,
.scaling_max_freq = 1209600,
.scaling_min_freq = 400000,
.scaling_min_freq_off = 200000,
},
};

View file

@ -30,7 +30,7 @@ import init.qcom.usb.rc
import init.target.rc
on early-init
export LD_SHIM_LIBS "/system/lib/hw/camera.vendor.msm8226.so|libboringssl-compat.so:/system/vendor/lib/libqomx_jpegenc.so|libboringssl-compat.so:/system/lib/libcrypto.so|libboringssl-compat.so:/system/lib/libril.so|libril_shim.so:/system/vendor/lib/libwvm.so|libwvm_shim.so"
export LD_SHIM_LIBS "/system/lib/hw/camera.vendor.msm8226.so|libboringssl-compat.so:/system/vendor/lib/libqomx_jpegenc.so|libboringssl-compat.so:/system/lib/libcrypto.so|libboringssl-compat.so:/system/lib/libril.so|libril_shim.so:/system/vendor/lib/libwvm.so|libwvm_shim.so:/system/vendor/lib/libmmcamera_imx175.so|libimx175_shim.so"
mount debugfs debugfs /sys/kernel/debug

15
sensors/Android.bp Normal file
View file

@ -0,0 +1,15 @@
cc_library_static {
name: "multihal-samsung",
vendor: true,
srcs: [
"multihal.cpp",
"SensorEventQueue.cpp"
],
shared_libs: [
"liblog",
"libcutils",
"libutils",
"libdl"
],
export_include_dirs: ["."],
}

42
sensors/Android.mk Normal file
View file

@ -0,0 +1,42 @@
#
# Copyright (C) 2013 The Android Open-Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := sensors.$(TARGET_BOARD_PLATFORM)
LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_PROPRIETARY_MODULE := true
LOCAL_CFLAGS := -DLOG_TAG=\"MultiHal\"
LOCAL_SRC_FILES := \
multihal.cpp \
SensorEventQueue.cpp \
LOCAL_SHARED_LIBRARIES := \
libcutils \
libdl \
liblog \
libutils \
LOCAL_STRIP_MODULE := false
include $(BUILD_SHARED_LIBRARY)
include $(call all-makefiles-under, $(LOCAL_PATH))

View file

@ -0,0 +1,91 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <hardware/sensors.h>
#include <algorithm>
#include <pthread.h>
#include <cutils/log.h>
#include "SensorEventQueue.h"
SensorEventQueue::SensorEventQueue(int capacity) {
mCapacity = capacity;
mStart = 0;
mSize = 0;
mData = new sensors_event_t[mCapacity];
pthread_cond_init(&mSpaceAvailableCondition, NULL);
}
SensorEventQueue::~SensorEventQueue() {
delete[] mData;
mData = NULL;
pthread_cond_destroy(&mSpaceAvailableCondition);
}
int SensorEventQueue::getWritableRegion(int requestedLength, sensors_event_t** out) {
if (mSize == mCapacity || requestedLength <= 0) {
*out = NULL;
return 0;
}
// Start writing after the last readable record.
int firstWritable = (mStart + mSize) % mCapacity;
int lastWritable = firstWritable + requestedLength - 1;
// Don't go past the end of the data array.
if (lastWritable > mCapacity - 1) {
lastWritable = mCapacity - 1;
}
// Don't go into the readable region.
if (firstWritable < mStart && lastWritable >= mStart) {
lastWritable = mStart - 1;
}
*out = &mData[firstWritable];
return lastWritable - firstWritable + 1;
}
void SensorEventQueue::markAsWritten(int count) {
mSize += count;
}
int SensorEventQueue::getSize() {
return mSize;
}
sensors_event_t* SensorEventQueue::peek() {
if (mSize == 0) return NULL;
return &mData[mStart];
}
void SensorEventQueue::dequeue() {
if (mSize == 0) return;
if (mSize == mCapacity) {
pthread_cond_broadcast(&mSpaceAvailableCondition);
}
mSize--;
mStart = (mStart + 1) % mCapacity;
}
// returns true if it waited, or false if it was a no-op.
bool SensorEventQueue::waitForSpace(pthread_mutex_t* mutex) {
bool waited = false;
while (mSize == mCapacity) {
waited = true;
pthread_cond_wait(&mSpaceAvailableCondition, mutex);
}
return waited;
}

View file

@ -0,0 +1,76 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SENSOREVENTQUEUE_H_
#define SENSOREVENTQUEUE_H_
#include <hardware/sensors.h>
#include <pthread.h>
/*
* Fixed-size circular queue, with an API developed around the sensor HAL poll() method.
* Poll() takes a pointer to a buffer, which is written by poll() before it returns.
* This class can provide a pointer to a spot in its internal buffer for poll() to
* write to, instead of using an intermediate buffer and a memcpy.
*
* Thread safety:
* Reading can be done safely after grabbing the mutex lock, while poll() writing in a separate
* thread without a mutex lock. But there can only be one writer at a time.
*/
class SensorEventQueue {
int mCapacity;
int mStart; // start of readable region
int mSize; // number of readable items
sensors_event_t* mData;
pthread_cond_t mSpaceAvailableCondition;
public:
explicit SensorEventQueue(int capacity);
~SensorEventQueue();
// Returns length of region, between zero and min(capacity, requestedLength). If there is any
// writable space, it will return a region of at least one. Because it must return
// a pointer to a contiguous region, it may return smaller regions as we approach the end of
// the data array.
// Only call while holding the lock.
// The region is not marked internally in any way. Subsequent calls may return overlapping
// regions. This class expects there to be exactly one writer at a time.
int getWritableRegion(int requestedLength, sensors_event_t** out);
// After writing to the region returned by getWritableRegion(), call this to indicate how
// many records were actually written.
// This increases size() by count.
// Only call while holding the lock.
void markAsWritten(int count);
// Gets the number of readable records.
// Only call while holding the lock.
int getSize();
// Returns pointer to the first readable record, or NULL if size() is zero.
// Only call this while holding the lock.
sensors_event_t* peek();
// This will decrease the size by one, freeing up the oldest readable event's slot for writing.
// Only call while holding the lock.
void dequeue();
// Blocks until space is available. No-op if there is already space.
// Returns true if it had to wait.
bool waitForSpace(pthread_mutex_t* mutex);
};
#endif // SENSOREVENTQUEUE_H_

712
sensors/multihal.cpp Normal file
View file

@ -0,0 +1,712 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SensorEventQueue.h"
#include "multihal.h"
#define LOG_NDEBUG 1
#include <cutils/log.h>
#include <cutils/atomic.h>
#include <hardware/sensors.h>
#include <vector>
#include <string>
#include <fstream>
#include <map>
#include <dirent.h>
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <math.h>
#include <poll.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
static pthread_mutex_t init_modules_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t init_sensors_mutex = PTHREAD_MUTEX_INITIALIZER;
// This mutex is shared by all queues
static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
// Used to pause the multihal poll(). Broadcasted by sub-polling tasks if waiting_for_data.
static pthread_cond_t data_available_cond = PTHREAD_COND_INITIALIZER;
bool waiting_for_data = false;
/*
* Vector of sub modules, whose indexes are referred to in this file as module_index.
*/
static std::vector<hw_module_t *> *sub_hw_modules = NULL;
/*
* Comparable class that globally identifies a sensor, by module index and local handle.
* A module index is the module's index in sub_hw_modules.
* A local handle is the handle the sub-module assigns to a sensor.
*/
struct FullHandle {
int moduleIndex;
int localHandle;
bool operator<(const FullHandle &that) const {
if (moduleIndex < that.moduleIndex) {
return true;
}
if (moduleIndex > that.moduleIndex) {
return false;
}
return localHandle < that.localHandle;
}
bool operator==(const FullHandle &that) const {
return moduleIndex == that.moduleIndex && localHandle == that.localHandle;
}
};
std::map<int, FullHandle> global_to_full;
std::map<FullHandle, int> full_to_global;
int next_global_handle = 1;
static int assign_global_handle(int module_index, int local_handle) {
int global_handle = next_global_handle++;
FullHandle full_handle;
full_handle.moduleIndex = module_index;
full_handle.localHandle = local_handle;
full_to_global[full_handle] = global_handle;
global_to_full[global_handle] = full_handle;
return global_handle;
}
// Returns the local handle, or -1 if it does not exist.
static int get_local_handle(int global_handle) {
if (global_to_full.count(global_handle) == 0) {
ALOGW("Unknown global_handle %d", global_handle);
return -1;
}
return global_to_full[global_handle].localHandle;
}
// Returns the sub_hw_modules index of the module that contains the sensor associates with this
// global_handle, or -1 if that global_handle does not exist.
static int get_module_index(int global_handle) {
if (global_to_full.count(global_handle) == 0) {
ALOGW("Unknown global_handle %d", global_handle);
return -1;
}
FullHandle f = global_to_full[global_handle];
ALOGV("FullHandle for global_handle %d: moduleIndex %d, localHandle %d",
global_handle, f.moduleIndex, f.localHandle);
return f.moduleIndex;
}
// Returns the global handle for this full_handle, or -1 if the full_handle is unknown.
static int get_global_handle(FullHandle* full_handle) {
int global_handle = -1;
if (full_to_global.count(*full_handle)) {
global_handle = full_to_global[*full_handle];
} else {
ALOGW("Unknown FullHandle: moduleIndex %d, localHandle %d",
full_handle->moduleIndex, full_handle->localHandle);
}
return global_handle;
}
static const int SENSOR_EVENT_QUEUE_CAPACITY = 36;
struct TaskContext {
sensors_poll_device_t* device;
SensorEventQueue* queue;
};
void *writerTask(void* ptr) {
ALOGV("writerTask STARTS");
TaskContext* ctx = (TaskContext*)ptr;
sensors_poll_device_t* device = ctx->device;
SensorEventQueue* queue = ctx->queue;
sensors_event_t* buffer;
int eventsPolled;
while (1) {
pthread_mutex_lock(&queue_mutex);
if (queue->waitForSpace(&queue_mutex)) {
ALOGV("writerTask waited for space");
}
int bufferSize = queue->getWritableRegion(SENSOR_EVENT_QUEUE_CAPACITY, &buffer);
// Do blocking poll outside of lock
pthread_mutex_unlock(&queue_mutex);
ALOGV("writerTask before poll() - bufferSize = %d", bufferSize);
eventsPolled = device->poll(device, buffer, bufferSize);
ALOGV("writerTask poll() got %d events.", eventsPolled);
if (eventsPolled <= 0) {
if (eventsPolled < 0) {
ALOGV("writerTask ignored error %d from %s", eventsPolled, device->common.module->name);
ALOGE("ERROR: Fix %s so it does not return error from poll()", device->common.module->name);
}
continue;
}
pthread_mutex_lock(&queue_mutex);
queue->markAsWritten(eventsPolled);
ALOGV("writerTask wrote %d events", eventsPolled);
if (waiting_for_data) {
ALOGV("writerTask - broadcast data_available_cond");
pthread_cond_broadcast(&data_available_cond);
}
pthread_mutex_unlock(&queue_mutex);
}
// never actually returns
return NULL;
}
/*
* Cache of all sensors, with original handles replaced by global handles.
* This will be handled to get_sensors_list() callers.
*/
static struct sensor_t const* global_sensors_list = NULL;
static int global_sensors_count = -1;
/*
* Extends a sensors_poll_device_1 by including all the sub-module's devices.
*/
struct sensors_poll_context_t {
/*
* This is the device that SensorDevice.cpp uses to make API calls
* to the multihal, which fans them out to sub-HALs.
*/
sensors_poll_device_1 proxy_device; // must be first
void addSubHwDevice(struct hw_device_t*);
int activate(int handle, int enabled);
int setDelay(int handle, int64_t ns);
int poll(sensors_event_t* data, int count);
int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
int flush(int handle);
int inject_sensor_data(struct sensors_poll_device_1 *dev, const sensors_event_t *data);
int close();
std::vector<hw_device_t*> sub_hw_devices;
std::vector<SensorEventQueue*> queues;
std::vector<pthread_t> threads;
int nextReadIndex;
sensors_poll_device_t* get_v0_device_by_handle(int global_handle);
sensors_poll_device_1_t* get_v1_device_by_handle(int global_handle);
int get_device_version_by_handle(int global_handle);
void copy_event_remap_handle(sensors_event_t* src, sensors_event_t* dest, int sub_index);
};
void sensors_poll_context_t::addSubHwDevice(struct hw_device_t* sub_hw_device) {
ALOGV("addSubHwDevice");
this->sub_hw_devices.push_back(sub_hw_device);
SensorEventQueue *queue = new SensorEventQueue(SENSOR_EVENT_QUEUE_CAPACITY);
this->queues.push_back(queue);
TaskContext* taskContext = new TaskContext();
taskContext->device = (sensors_poll_device_t*) sub_hw_device;
taskContext->queue = queue;
pthread_t writerThread;
pthread_create(&writerThread, NULL, writerTask, taskContext);
this->threads.push_back(writerThread);
}
// Returns the device pointer, or NULL if the global handle is invalid.
sensors_poll_device_t* sensors_poll_context_t::get_v0_device_by_handle(int global_handle) {
int sub_index = get_module_index(global_handle);
if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) {
return NULL;
}
return (sensors_poll_device_t*) this->sub_hw_devices[sub_index];
}
// Returns the device pointer, or NULL if the global handle is invalid.
sensors_poll_device_1_t* sensors_poll_context_t::get_v1_device_by_handle(int global_handle) {
int sub_index = get_module_index(global_handle);
if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) {
return NULL;
}
return (sensors_poll_device_1_t*) this->sub_hw_devices[sub_index];
}
// Returns the device version, or -1 if the handle is invalid.
int sensors_poll_context_t::get_device_version_by_handle(int handle) {
sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
if (v0) {
return v0->common.version;
} else {
return -1;
}
}
const char *apiNumToStr(int version) {
switch(version) {
case SENSORS_DEVICE_API_VERSION_1_0:
return "SENSORS_DEVICE_API_VERSION_1_0";
case SENSORS_DEVICE_API_VERSION_1_1:
return "SENSORS_DEVICE_API_VERSION_1_1";
case SENSORS_DEVICE_API_VERSION_1_2:
return "SENSORS_DEVICE_API_VERSION_1_2";
case SENSORS_DEVICE_API_VERSION_1_3:
return "SENSORS_DEVICE_API_VERSION_1_3";
case SENSORS_DEVICE_API_VERSION_1_4:
return "SENSORS_DEVICE_API_VERSION_1_4";
default:
return "UNKNOWN";
}
}
int sensors_poll_context_t::activate(int handle, int enabled) {
int retval = -EINVAL;
ALOGV("activate");
int local_handle = get_local_handle(handle);
sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
retval = v0->activate(v0, local_handle, enabled);
ALOGV("retval %d", retval);
return retval;
}
int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
int retval = -EINVAL;
ALOGV("setDelay");
int local_handle = get_local_handle(handle);
sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
retval = v0->setDelay(v0, local_handle, ns);
ALOGV("retval %d", retval);
return retval;
}
void sensors_poll_context_t::copy_event_remap_handle(sensors_event_t* dest, sensors_event_t* src,
int sub_index) {
memcpy(dest, src, sizeof(struct sensors_event_t));
// A normal event's "sensor" field is a local handle. Convert it to a global handle.
// A meta-data event must have its sensor set to 0, but it has a nested event
// with a local handle that needs to be converted to a global handle.
FullHandle full_handle;
full_handle.moduleIndex = sub_index;
// If it's a metadata event, rewrite the inner payload, not the sensor field.
// If the event's sensor field is unregistered for any reason, rewrite the sensor field
// with a -1, instead of writing an incorrect but plausible sensor number, because
// get_global_handle() returns -1 for unknown FullHandles.
if (dest->type == SENSOR_TYPE_META_DATA) {
full_handle.localHandle = dest->meta_data.sensor;
dest->meta_data.sensor = get_global_handle(&full_handle);
} else {
full_handle.localHandle = dest->sensor;
dest->sensor = get_global_handle(&full_handle);
}
}
int sensors_poll_context_t::poll(sensors_event_t *data, int maxReads) {
ALOGV("poll");
int empties = 0;
int queueCount = 0;
int eventsRead = 0;
pthread_mutex_lock(&queue_mutex);
queueCount = (int)this->queues.size();
while (eventsRead == 0) {
while (empties < queueCount && eventsRead < maxReads) {
SensorEventQueue* queue = this->queues.at(this->nextReadIndex);
sensors_event_t* event = queue->peek();
if (event == NULL) {
empties++;
} else {
empties = 0;
this->copy_event_remap_handle(&data[eventsRead], event, nextReadIndex);
if (data[eventsRead].sensor == -1) {
// Bad handle, do not pass corrupted event upstream !
ALOGW("Dropping bad local handle event packet on the floor");
} else {
eventsRead++;
}
queue->dequeue();
}
this->nextReadIndex = (this->nextReadIndex + 1) % queueCount;
}
if (eventsRead == 0) {
// The queues have been scanned and none contain data, so wait.
ALOGV("poll stopping to wait for data");
waiting_for_data = true;
pthread_cond_wait(&data_available_cond, &queue_mutex);
waiting_for_data = false;
empties = 0;
}
}
pthread_mutex_unlock(&queue_mutex);
ALOGV("poll returning %d events.", eventsRead);
return eventsRead;
}
int sensors_poll_context_t::batch(int handle, int flags, int64_t period_ns, int64_t timeout) {
ALOGV("batch");
int retval = -EINVAL;
int local_handle = get_local_handle(handle);
sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
retval = v1->batch(v1, local_handle, flags, period_ns, timeout);
ALOGV("retval %d", retval);
return retval;
}
int sensors_poll_context_t::flush(int handle) {
ALOGV("flush");
int retval = -EINVAL;
int local_handle = get_local_handle(handle);
sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle);
retval = v1->flush(v1, local_handle);
ALOGV("retval %d", retval);
return retval;
}
int sensors_poll_context_t::inject_sensor_data(struct sensors_poll_device_1 *dev,
const sensors_event_t *data) {
int retval = -EINVAL;
ALOGV("inject_sensor_data");
// Get handle for the sensor owning the event being injected
int local_handle = get_local_handle(data->sensor);
sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(data->sensor);
retval = v1->inject_sensor_data(dev, data);
ALOGV("retval %d", retval);
return retval;
}
int sensors_poll_context_t::close() {
ALOGV("close");
for (std::vector<hw_device_t*>::iterator it = this->sub_hw_devices.begin();
it != this->sub_hw_devices.end(); it++) {
hw_device_t* dev = *it;
int retval = dev->close(dev);
ALOGV("retval %d", retval);
}
return 0;
}
static int device__close(struct hw_device_t *dev) {
sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
if (ctx != NULL) {
int retval = ctx->close();
delete ctx;
}
return 0;
}
static int device__activate(struct sensors_poll_device_t *dev, int handle,
int enabled) {
sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
return ctx->activate(handle, enabled);
}
static int device__setDelay(struct sensors_poll_device_t *dev, int handle,
int64_t ns) {
sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
return ctx->setDelay(handle, ns);
}
static int device__poll(struct sensors_poll_device_t *dev, sensors_event_t* data,
int count) {
sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
return ctx->poll(data, count);
}
static int device__batch(struct sensors_poll_device_1 *dev, int handle,
int flags, int64_t period_ns, int64_t timeout) {
sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
ctx->setDelay(handle, period_ns);
return 0;
}
static int device__flush(struct sensors_poll_device_1 *dev, int handle) {
return -EINVAL;
}
static int device__inject_sensor_data(struct sensors_poll_device_1 *dev,
const sensors_event_t *data) {
sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev;
return ctx->inject_sensor_data(dev, data);
}
static int open_sensors(const struct hw_module_t* module, const char* name,
struct hw_device_t** device);
static bool starts_with(const char* s, const char* prefix) {
if (s == NULL || prefix == NULL) {
return false;
}
size_t s_size = strlen(s);
size_t prefix_size = strlen(prefix);
return s_size >= prefix_size && strncmp(s, prefix, prefix_size) == 0;
}
/*
* Adds valid paths from the config file to the vector passed in.
* The vector must not be null.
*/
static void get_so_paths(std::vector<std::string> *so_paths) {
const std::vector<const char *> config_path_list(
{ MULTI_HAL_CONFIG_FILE_PATH, DEPRECATED_MULTI_HAL_CONFIG_FILE_PATH });
std::ifstream stream;
const char *path = nullptr;
for (auto i : config_path_list) {
std::ifstream f(i);
if (f) {
stream = std::move(f);
path = i;
break;
}
}
if(!stream) {
ALOGW("No multihal config file found");
return;
}
ALOGE_IF(strcmp(path, DEPRECATED_MULTI_HAL_CONFIG_FILE_PATH) == 0,
"Multihal configuration file path %s is not compatible with Treble "
"requirements. Please move it to %s.",
path, MULTI_HAL_CONFIG_FILE_PATH);
ALOGV("Multihal config file found at %s", path);
std::string line;
while (std::getline(stream, line)) {
ALOGV("config file line: '%s'", line.c_str());
so_paths->push_back(line);
}
}
/*
* Ensures that the sub-module array is initialized.
* This can be first called from get_sensors_list or from open_sensors.
*/
static void lazy_init_modules() {
pthread_mutex_lock(&init_modules_mutex);
if (sub_hw_modules != NULL) {
pthread_mutex_unlock(&init_modules_mutex);
return;
}
std::vector<std::string> *so_paths = new std::vector<std::string>();
get_so_paths(so_paths);
// dlopen the module files and cache their module symbols in sub_hw_modules
sub_hw_modules = new std::vector<hw_module_t *>();
dlerror(); // clear any old errors
const char* sym = HAL_MODULE_INFO_SYM_AS_STR;
for (std::vector<std::string>::iterator it = so_paths->begin(); it != so_paths->end(); it++) {
const char* path = it->c_str();
void* lib_handle = dlopen(path, RTLD_LAZY);
if (lib_handle == NULL) {
ALOGW("dlerror(): %s", dlerror());
} else {
ALOGI("Loaded library from %s", path);
ALOGV("Opening symbol \"%s\"", sym);
// clear old errors
dlerror();
struct hw_module_t* module = (hw_module_t*) dlsym(lib_handle, sym);
const char* error;
if ((error = dlerror()) != NULL) {
ALOGW("Error calling dlsym: %s", error);
} else if (module == NULL) {
ALOGW("module == NULL");
} else {
ALOGV("Loaded symbols from \"%s\"", sym);
sub_hw_modules->push_back(module);
}
}
}
pthread_mutex_unlock(&init_modules_mutex);
}
/*
* Fix the fields of the sensor to be compliant with the API version
* reported by the wrapper.
*/
static void fix_sensor_fields(sensor_t& sensor) {
/*
* Becasue batching and flushing don't work modify the
* sensor fields to not report any fifo counts.
*/
sensor.fifoReservedEventCount = 0;
sensor.fifoMaxEventCount = 0;
switch (sensor.type) {
/*
* Use the flags suggested by the sensors documentation.
*/
case SENSOR_TYPE_TILT_DETECTOR:
sensor.flags = SENSOR_FLAG_WAKE_UP | SENSOR_FLAG_ON_CHANGE_MODE;
break;
/*
* Report a proper range to fix doze proximity check.
*/
case SENSOR_TYPE_PROXIMITY:
sensor.flags = SENSOR_FLAG_WAKE_UP | SENSOR_FLAG_ON_CHANGE_MODE;
sensor.maxRange = 5.0;
break;
}
}
/*
* Lazy-initializes global_sensors_count, global_sensors_list, and module_sensor_handles.
*/
static void lazy_init_sensors_list() {
ALOGV("lazy_init_sensors_list");
pthread_mutex_lock(&init_sensors_mutex);
if (global_sensors_list != NULL) {
// already initialized
pthread_mutex_unlock(&init_sensors_mutex);
ALOGV("lazy_init_sensors_list - early return");
return;
}
ALOGV("lazy_init_sensors_list needs to do work");
lazy_init_modules();
// Count all the sensors, then allocate an array of blanks.
global_sensors_count = 0;
const struct sensor_t *subhal_sensors_list;
for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
it != sub_hw_modules->end(); it++) {
struct sensors_module_t *module = (struct sensors_module_t*) *it;
global_sensors_count += module->get_sensors_list(module, &subhal_sensors_list);
ALOGV("increased global_sensors_count to %d", global_sensors_count);
}
// The global_sensors_list is full of consts.
// Manipulate this non-const list, and point the const one to it when we're done.
sensor_t* mutable_sensor_list = new sensor_t[global_sensors_count];
// index of the next sensor to set in mutable_sensor_list
int mutable_sensor_index = 0;
int module_index = 0;
for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
it != sub_hw_modules->end(); it++) {
hw_module_t *hw_module = *it;
ALOGV("examine one module");
// Read the sub-module's sensor list.
struct sensors_module_t *module = (struct sensors_module_t*) hw_module;
int module_sensor_count = module->get_sensors_list(module, &subhal_sensors_list);
ALOGV("the module has %d sensors", module_sensor_count);
// Copy the HAL's sensor list into global_sensors_list,
// with the handle changed to be a global handle.
for (int i = 0; i < module_sensor_count; i++) {
ALOGV("examining one sensor");
const struct sensor_t *local_sensor = &subhal_sensors_list[i];
int local_handle = local_sensor->handle;
memcpy(&mutable_sensor_list[mutable_sensor_index], local_sensor,
sizeof(struct sensor_t));
// Overwrite the global version's handle with a global handle.
int global_handle = assign_global_handle(module_index, local_handle);
mutable_sensor_list[mutable_sensor_index].handle = global_handle;
ALOGV("module_index %d, local_handle %d, global_handle %d",
module_index, local_handle, global_handle);
fix_sensor_fields(mutable_sensor_list[mutable_sensor_index]);
mutable_sensor_index++;
}
module_index++;
}
// Set the const static global_sensors_list to the mutable one allocated by this function.
global_sensors_list = mutable_sensor_list;
pthread_mutex_unlock(&init_sensors_mutex);
ALOGV("end lazy_init_sensors_list");
}
static int module__get_sensors_list(__unused struct sensors_module_t* module,
struct sensor_t const** list) {
ALOGV("module__get_sensors_list start");
lazy_init_sensors_list();
*list = global_sensors_list;
ALOGV("global_sensors_count: %d", global_sensors_count);
for (int i = 0; i < global_sensors_count; i++) {
ALOGV("sensor type: %d", global_sensors_list[i].type);
}
return global_sensors_count;
}
static struct hw_module_methods_t sensors_module_methods = {
.open = open_sensors
};
struct sensors_module_t HAL_MODULE_INFO_SYM = {
.common = {
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor = 1,
.id = SENSORS_HARDWARE_MODULE_ID,
.name = "MultiHal Sensor Module",
.author = "Google, Inc",
.methods = &sensors_module_methods,
.dso = NULL,
.reserved = {0},
},
.get_sensors_list = module__get_sensors_list
};
struct sensors_module_t *get_multi_hal_module_info() {
return (&HAL_MODULE_INFO_SYM);
}
static int open_sensors(const struct hw_module_t* hw_module, const char* name,
struct hw_device_t** hw_device_out) {
ALOGV("open_sensors begin...");
lazy_init_modules();
// Create proxy device, to return later.
sensors_poll_context_t *dev = new sensors_poll_context_t();
memset(dev, 0, sizeof(sensors_poll_device_1_t));
dev->proxy_device.common.tag = HARDWARE_DEVICE_TAG;
dev->proxy_device.common.version = SENSORS_DEVICE_API_VERSION_1_3;
dev->proxy_device.common.module = const_cast<hw_module_t*>(hw_module);
dev->proxy_device.common.close = device__close;
dev->proxy_device.activate = device__activate;
dev->proxy_device.setDelay = device__setDelay;
dev->proxy_device.poll = device__poll;
dev->proxy_device.batch = device__batch;
dev->proxy_device.flush = device__flush;
dev->proxy_device.inject_sensor_data = device__inject_sensor_data;
dev->nextReadIndex = 0;
// Open() the subhal modules. Remember their devices in a vector parallel to sub_hw_modules.
for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin();
it != sub_hw_modules->end(); it++) {
sensors_module_t *sensors_module = (sensors_module_t*) *it;
struct hw_device_t* sub_hw_device;
int sub_open_result = sensors_module->common.methods->open(*it, name, &sub_hw_device);
if (!sub_open_result) {
dev->addSubHwDevice(sub_hw_device);
}
}
// Prepare the output param and return
*hw_device_out = &dev->proxy_device.common;
ALOGV("...open_sensors end");
return 0;
}

29
sensors/multihal.h Normal file
View file

@ -0,0 +1,29 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HARDWARE_LIBHARDWARE_MODULES_SENSORS_MULTIHAL_H_
#define HARDWARE_LIBHARDWARE_MODULES_SENSORS_MULTIHAL_H_
#include <hardware/sensors.h>
#include <hardware/hardware.h>
static const char* MULTI_HAL_CONFIG_FILE_PATH = "/vendor/etc/sensors/_hals.conf";
// Depracated because system partition HAL config file does not satisfy treble requirements.
static const char* DEPRECATED_MULTI_HAL_CONFIG_FILE_PATH = "/system/etc/sensors/_hals.conf";
struct sensors_module_t *get_multi_hal_module_info(void);
#endif // HARDWARE_LIBHARDWARE_MODULES_SENSORS_MULTIHAL_H_

View file

@ -7,4 +7,9 @@ LOCAL_SHARED_LIBRARIES := libstagefright_foundation liblog libmedia libcutils
LOCAL_MODULE := libwvm_shim
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := imx175_shim.cpp
LOCAL_MODULE := libimx175_shim
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_MODULE_TAGS := optional
include $(BUILD_SHARED_LIBRARY)

17
shims/imx175_shim.cpp Normal file
View file

@ -0,0 +1,17 @@
#include <cutils/log.h>
#include <sys/types.h>
#include <dlfcn.h>
#include <string.h>
extern "C" {
int property_get(const char * key, char * value, const char * default_value) {
ALOGE("%s: E", __FUNCTION__);
if (strcmp("ro.revision", key) == 0) {
ALOGE("%s: ro.revision was called!", __FUNCTION__);
strcpy(value, "4");
return 3;
}
ALOGE("%s: called other property: %s", __FUNCTION__, key);
return ((int( * )(const char * , char *, const char * ))(dlsym((void * ) - 1, "property_get")))(key, value, default_value);
}
}