当前位置: 首页 > 科技观察

Openharmony软总线之连接模块分析

时间:2023-03-20 16:26:58 科技观察

Openharmony软总线的连接模块分析软总线是组件间传递信息的公共通信干线,软总线类似于总线协议,功能相似但又有所区别。在总线协议中,多个设备通过公用通信干线进行通信,设备需要具备发送和接收数据的功能。openharmony软总线需要将不同的设备集成在一起。由于不同设备之间通信的差异,比如wifi和蓝牙之间的通信差异,软总线(bus)需要具备处理不同类型设备之间通信的能力。connect模块用于不同类型设备的管理,当然也包含在不同模块的连接管理中,比如认证模块,connection模块connection包含的文件类型如下:connection│├──ble#BluetoothLowPower├──br#Bluetooth├──common├──interface#外部调用接口├──manager#设备模块接入、启停管理└──tcp#tcp当前设备需要支持蓝牙/wifi/网口通信(目前蓝牙应该还不完善),其他网口和wifi都是基于tcp协议的。蓝牙BLE/BR是基于蓝牙协议的,所以当设备A与设备B(wifi)tcp通信时,设备A和设备c(蓝牙)需要同时具备蓝牙通信能力。g_connManagerConnectFuncInterface*g_connManager[CONNECT_TYPE_MAX]={0};g_connManager用于管理不同类型设备的通信功能。支持的有CONNECT_TCP、CONNECT_BR和CONNECT_BLE。typedefenum{CONNECT_TCP=1,CONNECT_BR,CONNECT_BLE,CONNECT_TYPE_MAX}连接类型;由于不同设备之间的通讯方式存在差异,通讯方式的配置需要通过回调的方式来实现。通信接口如下:typedefstruct{int32_t(*ConnectDevice)(constConnectOption*option,uint32_trequestId,constConnectResult*result);int32_t(*PostBytes)(uint32_tconnectionId,constchar*data,int32_tlen,int32_tpid,int32_tflag);int32_t(*DisconnectDevice)(uint32_tconnectionId);int32_t(*DisconnectDeviceNow)(constConnectOptionId*option);int32_t(,ConnectionInfo*info);int32_t(*StartLocalListening)(constLocalListenerInfo*info);int32_t(*StopLocalListening)(constLocalListenerInfo*info);}ConnectFuncInterface;设备启动时,通过ConnServerInit函数进行初始化。ConnInitTcp、ConnInitBr、ConnInitBle分别对应3种通信设备类型的初始化ConnectFuncInterface*ConnInitTcp(constConnectCallback*callback){if(callback==NULL){SoftBusLog(SOFTBUS_LOG_CONN,SOFTBUS_LOG_ERROR};CallbackisNULL("CallbackisNULL")!=SOFTBUS_OK){SoftBusLog(SOFTBUS_LOG_CONN,SOFTBUS_LOG_ERROR,"CannotInitProperty");returnNULL;}ConnectFuncInterface*interface=SoftBusCalloc(sizeof(ConnectFuncInterface));if(interface==NULL){SoftBusLog(SOFTBUS_LOG_CONN,SOFTBUS_LOG_ERROR,"InitTcpfailed.");returnNULL;}interface->ConnectDevice=TcpConnectDevice;interface->DisconnectDevice=TcpDisconnectDevice;interface->DisconnectDeviceNow=TcpDisconnectDeviceNow;interface->PostBytes=TcpPostBytes;interface->GetConnectionInfo=TcpGetConnectionInfo;interface->StartLocalListening=TcpStartListening;interface->StopLocalListening=TcpStopListening;g_tcpConnCallback=回调;if(g_tcpConnInfoList==NULL){g_tcpConnInfoList=CreateSoftBusList();if(g_tcpConnInfoList==NULL){SoftBusLog(SOFTBUS_LOG_CONN,SOFTBUS_LOG_ERROR,"CreatetcpConnInfoListfailed.");SoftBusFree(interface);returnNULL;}g_tcpConnInfoList->cnt=0;}if(g_tcpListener==NULLListen){g_tcp(SoftbusBaseListener*)SoftBusCalloc(sizeof(SoftbusBaseListener));if(g_tcpListener==NULL){SoftBusFree(接口);DestroySoftBusList(g_tcpConnInfoList);g_tcpConnInfoList=NULL;returnNULL;}}g_tcpListener->onConnectEvent-ListonData=TcpOnConnectEventer;TcpOnDataEvent;returninterface;}interface接口是tcp通信方式的配置,ConnectDevice连接设备,DisconnectDevice断开连接,PostBytes发送数据,GetConnectionInfo获取设备信息,StartLocalListening开始监听,StopLocalListening停止监听,注意通过g_tcpListener挂载每个设备在软总线(bus)上需要通过不同端口建立服务器和客服模式,服务器用于监听数据请求。当数据到达时,在总线上触发,经过一系列处理,最终通过g_connManagerCb变量获取到相应的数据。typedefstruct{void(*OnConnected)(uint32_tconnectionId,constConnectionInfo*info);void(*OnDisconnected)(uint32_tconnectionId,constConnectionInfo*info);void(*OnDataReceived)(uint32_tconnectionId,ConnModulemoduleId,int64_tseq,char*data,int}2_tallback);数据接收函数ConnManagerRecvDatavoidConnManagerRecvData(uint32_tconnectionId,ConnModulemoduleId,int64_tseq,char*data,int32_tlen){ConnListenerNodelistener;int32_tret;char*pkt=NULL;int32_tpktLen;if(data==NULL){return;}if(len<=)(int32_(ConnPktHead)){SoftBusLog(SOFTBUS_LOG_CONN,SOFTBUS_LOG_ERROR,"len%d\r\n",len);return;}ret=GetListenerByModuleId(moduleId,&listener);if(ret==SOFTBUS_ERR){SoftBusLog(SOFTBUS_LOG_CONN,SOFTBUS_LOG_ERROR,"GetListenerByModuleIdfailmoduleId%d\r\n",moduleId);return;}pktLen=len-sizeof(ConnPktHead);pkt=data+sizeof(ConnPktHead);listener.callback.OnDataReceived(connectionId,moduleId,seq,pkt,pktLen);return;}软总线通信模块由发现、认证等不同模块组成,认证模块通过connect连接时使用总线时,数据获取过程为:softbus->ConnManagerRecvData->listener.callback.OnDataReceived,总线认证可以参考一个测试用例。当然,由于目前的软总线功能还不完善,一些数据处理也不是很完善。更多信息请访问:Harmonyos.51cto.com,与华为官方合作打造的鸿蒙技术社区