BitTube来龙去脉
SensorManager.cpp文件展开客户端从JNI获取SensorServer服务,创建SensorEventConnection,SensorEventQueue,并通过SensorEventQueue与SensorEventConnection加交互
从底层获取数据,android_hardware_SensorManager.cpp作为客户端JNI层获取Sensor信息的"集散中心"而SystemSensorManager作为应用层Sensor信息的处理中心
(一)客户端创建EventQueue
sp<SensorEventQueue> SensorManager::createEventQueue()
{
sp<SensorEventQueue> queue;
Mutex::Autolock _l(mLock);
while (assertStateLocked() == NO_ERROR) {
sp<ISensorEventConnection> connection =
mSensorServer->
createSensorEventConnection(); // 二. 创建传感器事件connection
if (connection == NULL) {
// SensorService just died.
ALOGE("createEventQueue: connection is NULL. SensorService died.");
continue;
}
queue = new
SensorEventQueue(connection); // 三. 创建传感器事件
break;
}
return queue;
}
(二)创建createSensorEventConnection
BpSensorServer::createSensorEventConnection
{
Parcel data, reply;
data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
remote()->transact(
CREATE_SENSOR_EVENT_CONNECTION, data, &reply); // 2.1 BnSensorServer::onTransact
return interface_cast<ISensorEventConnection>(reply.readStrongBinder()); // 2.4 读取BnSensorServer端创建的BpBinder对象
}
2.1 BnSensorServer接受BpSensorServer请求创建EventConnection
status_t BnSensorServer::onTransact(
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
switch(code) {
case GET_SENSOR_LIST: {
......
} break;
case
CREATE_SENSOR_EVENT_CONNECTION: {
CHECK_INTERFACE(ISensorServer, data, reply);
sp<ISensorEventConnection> connection(
createSensorEventConnection()); // 2.2调用SensorServer服务
reply->writeStrongBinder(connection->asBinder()); // 2.3 利用Parcel对象传递构建BpBinder代理对象
return NO_ERROR;
} break;
}
return BBinder::onTransact(code, data, reply, flags);
}
2.2 调用SensorServer服务创建SensorEventConnection
sp<ISensorEventConnection> SensorService::createSensorEventConnection()
{
uid_t uid = IPCThreadState::self()->getCallingUid();
sp<SensorEventConnection> result(new SensorEventConnection(this, uid)); // 2.3调用子类创建
return result;
}
也即
SensorService::SensorEventConnection::SensorEventConnection(
const sp<SensorService>& service, uid_t uid)
:
mService(service),
mChannel(new BitTube()),
mUid(uid)
{
}
mChannel 为服务端的BitTube,mService值为SenSorServer服务
2.3 本地端(BnSensorServer)发送创建好的Binder对象,SensorEventConnection继承BnSensorEventConnection,
通过 asBinder()
转成BpSensorEventConnection对象
reply->writeStrongBinder(connection->asBinder());
2.4 代理端(BpSensorServer)获取SensorEventConnection的BpBinder代理对象
interface_cast<ISensorEventConnection>(reply.readStrongBinder());
(三)创建SensorEventQueue
SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection)
: mSensorEventConnection(connection) // connection为 第二步中创建的BpSensorEventConnection
{
}
3.1 获取传感器通道
void SensorEventQueue::onFirstRef()
{
mSensorChannel = mSensorEventConnection->getSensorChannel(); // 3.2 BpSensorEventConnection::getSensorChannel()
}
3.2 BpSensorEventConnection获取SensorChannel
virtual sp<BitTube> getSensorChannel() const
{
Parcel data, reply;
data.
writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor()); // writeInterfaceToken为Bp代理端向服务端发送数据时编写的RPC报头
remote()->transact(
GET_SENSOR_CHANNEL, data, &reply); // 3.3 BnSensorEventConnection 获取SensorChannel
return new BitTube(reply); // 3.5 利用收到的mReceiveFd,创建客户端BitTube
}
status_t BnSensorEventConnection::onTransact(
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
switch(code) {
case
GET_SENSOR_CHANNEL: {
CHECK_INTERFACE(ISensorEventConnection, data, reply);
sp<BitTube> channel(
getSensorChannel()); // 3.4 SensorEventConnection::getSensorChannel获取SensorChannel
channel->writeToParcel(reply);
return NO_ERROR;
} break;
case ENABLE_DISABLE: {
......
} break;
case SET_EVENT_RATE: {
......
} break;
}
return BBinder::onTransact(code, data, reply, flags);
}
3.4 调用SensorEventConnection::getSensorChannel()返回服务端创建的mChannel ( BitTube )
sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
{
return mChannel;
}
接着调用writeToParcel 传送传感器(状态接受描述符)mReceiveFd返回给BpSensorEventConnection创建客户端BitTube
status_t BitTube::writeToParcel(Parcel* reply) const
{
if (mReceiveFd < 0)
return -EINVAL;
status_t result =
reply->writeDupFileDescriptor(
mReceiveFd
);
close(mReceiveFd);
mReceiveFd = -1;
return result;
}
3.5 创建客户端BitTube
BitTube::BitTube(const Parcel& data)
: mSendFd(-1), mReceiveFd(-1)
{
mReceiveFd
=
dup(
data.readFileDescriptor());
if (mReceiveFd >= 0) {
int size = SOCKET_BUFFER_SIZE;
setsockopt(
mReceiveFd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
setsockopt(
mReceiveFd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
fcntl(
mReceiveFd, F_SETFL, O_NONBLOCK);
} else {
mReceiveFd = -errno;
ALOGE("BitTube(Parcel): can't dup filedescriptor (%s)",
strerror(-mReceiveFd));
}
}
备注:注意mReceiveFd描述符的传输过程,writeDupFileDescriptor,dup(data.readFileDescriptor()) ,SensorEventQueue::waitForEvent
通过getFd()获取mReceiveFd,SensorEventQueue::getLooper()通过Looper::addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL)
对mReceiveFd(读通道or管道)进行监听epoll_wait
相关链接:
BitTube 进程间数据传递 http://blog.csdn.net/new_abc/article/details/8971961
SensorThread线程 http://blog.csdn.net/new_abc/article/details/8971938