在移植sensor的hal层代码时, SensorBase.cpp作为所有sensor类的基类, 定义了一些很有用的方法.
下面jwisp结合实际实验, 来跟大家分析下SensorBase的作用和流程
SensorBase::SensorBase( const char* dev_name, const char* data_name) : dev_name(dev_name), data_name(data_name), dev_fd(-1), data_fd(-1) { data_fd = openInput(data_name); }
SensorBase的构造, 子类会先调用父类的这个方法,
在SensorBase构造中, 将子类传来的设备名称dev_name和数据设备名data_name赋值本类成员,
然后通过openInput函数,打开data_name然后将open后的句柄, 传递给data_fd
SensorBase::~SensorBase() { if (data_fd >= 0) { close(data_fd); } if (dev_fd >= 0) { close(dev_fd); } }
析构函数, 关闭data_fd和dev_fd
int SensorBase::open_device() { if (dev_fd<0 && dev_name) { dev_fd = open(dev_name, O_RDONLY); LOGE_IF(dev_fd<0, "Couldn't open %s (%s)", dev_name, strerror(errno)); } return 0; } int SensorBase::close_device() { if (dev