static int open_sensors(const struct hw_module_t* module, const char* name,
struct hw_device_t** device)
{
int status = -EINVAL;
LOGD("%s: name: %s!\r\n", __func__, name);
struct sensors_data_context_t *dev;
dev = malloc(sizeof(*dev));
memset(dev, 0, sizeof(*dev));
dev->events_fd = open_input(O_RDONLY);
if (dev->events_fd < 0)
{
LOGE("%s: Open input device error!",__func__);
return -1;
}
dev->device_io_fd = -1;
if(dev->device_io_fd <= 0)
{
dev->device_io_fd = open(HWM_SENSOR_DEV, O_RDONLY);
LOGD("%s: device handle %d",__func__, dev->device_io_fd);
}
dev->activate = hwm__activate;
dev->set_delay = hwm__set_delay;
dev->poll = hwm__poll;
dev->device.common.tag = HARDWARE_DEVICE_TAG;
dev->device.common.version = 0;
dev->device.common.module = (struct hw_module_t*)module;
dev->device.common.close = data__close;
dev->device.activate = control__activate;
dev->device.setDelay = control__setDelay;
dev->device.poll = data__poll;
*device = &dev->device.common;
status = 0;
return status;
}
ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice)
SensorDevice::SensorDevice()
: mSensorDevice(0),
mSensorModule(0)
{
status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID,
(hw_module_t const**)&mSensorModule);
LOGE_IF(err, "couldn't load %s module (%s)",
SENSORS_HARDWARE_MODULE_ID, strerror(-err));
if (mSensorModule) {
err = sensors_open(&mSensorModule->common, &mSensorDevice);
LOGE_IF(err, "couldn't open device for module %s (%s)",
SENSORS_HARDWARE_MODULE_ID, strerror(-err));
if (mSensorDevice) {
sensor_t const* list;
ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
mActivationCount.setCapacity(count);
Info model;
for (size_t i=0 ; i<size_t(count) ; i++) {
mActivationCount.add(list[i].handle, model);
mSensorDevice->activate(mSensorDevice, list[i].handle, 0);
}
}
}
}
mSensorDevice->activate()调用的就是hwm_activate().