Openharmony软总线之连接模块分析

系统
openharmony 软总线需要将不同设备整合到一起,由于不同的设备通信存在差异,如wifi与蓝牙之间通信存在差异,软总线(bus)需要具备有处理不同类型设备之间通信功能。

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

软总线介绍

总线(Bus)是计算机各种功能部件之间传送信息的公共通信干线,软总线与总线协议类似存在相似的功能但又又差异。总线协议中多个设备通过公共通信干线来进行通信,设备需要具备收发数据功能。openharmony 软总线需要将不同设备整合到一起,由于不同的设备通信存在差异,如wifi与蓝牙之间通信存在差异,软总线(bus)需要具备有处理不同类型设备之间通信功能。connect 模块用于这种不同类型设备管理,当然也包含于不同模块的连接管理,如认证模块,

connection模块

connection 包含的文件类型如下:

  1. connection 
  2.  │ 
  3.  ├── ble        #低功耗蓝牙 
  4.  ├── br         #蓝牙 
  5.  ├── common       
  6.  ├── interface  #外部调用接口 
  7.  ├── manager    #设备模块接入、启停管理 
  8.  └── tcp        #tcp  

 当前设备需要支持的蓝牙/wifi/网口通信(目前蓝牙应该没有完善),其他网口和wifi 都基于tcp协议。蓝牙BLE/BR 基于蓝牙协议,因此当设备A与设备B(wifi)tcp通信时,设备A与设备c(蓝牙)同时需要具备有蓝牙通信能力。

openharmony软总线之--连接模块分析-鸿蒙HarmonyOS技术社区

g_connManager

  1. ConnectFuncInterface *g_connManager[CONNECT_TYPE_MAX] = {0}; 

 g_connManager 用于管理不同类型设备通信功能。其中支持的类似有CONNECT_TCP、CONNECT_BR及CONNECT_BLE。

  1. typedef enum { 
  2.     CONNECT_TCP = 1, 
  3.     CONNECT_BR, 
  4.     CONNECT_BLE, 
  5.     CONNECT_TYPE_MAX 
  6. } ConnectType; 

 由于不同设备通信存在差异,需要通过回调来实现通信方式的配置,通信接口如下:

  1. typedef struct { 
  2.     int32_t (*ConnectDevice)(const ConnectOption *option, uint32_t requestId, const ConnectResult *result); 
  3.     int32_t (*PostBytes)(uint32_t connectionId, const char *data, int32_t len, int32_t pid, int32_t flag); 
  4.     int32_t (*DisconnectDevice)(uint32_t connectionId); 
  5.     int32_t (*DisconnectDeviceNow)(const ConnectOption *option); 
  6.     int32_t (*GetConnectionInfo)(uint32_t connectionId, ConnectionInfo *info); 
  7.     int32_t (*StartLocalListening)(const LocalListenerInfo *info); 
  8.     int32_t (*StopLocalListening)(const LocalListenerInfo *info); 
  9. } ConnectFuncInterface; 

 设备启动时通ConnServerInit 函数进行初始化。ConnInitTcp、ConnInitBr、ConnInitBle 分别对应3种通信设备类型的初始化

  1. ConnectFuncInterface *ConnInitTcp(const ConnectCallback *callback) 
  2.     if (callback == NULL) { 
  3.         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "ConnectCallback is NULL."); 
  4.         return NULL
  5.     } 
  6.     if (InitProperty() != SOFTBUS_OK) { 
  7.         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "Can not InitProperty"); 
  8.         return NULL
  9.     } 
  10.     ConnectFuncInterface *interface = SoftBusCalloc(sizeof(ConnectFuncInterface)); 
  11.     if (interface == NULL) { 
  12.         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "InitTcp failed."); 
  13.         return NULL
  14.     } 
  15.     interface->ConnectDevice = TcpConnectDevice; 
  16.     interface->DisconnectDevice = TcpDisconnectDevice; 
  17.     interface->DisconnectDeviceNow = TcpDisconnectDeviceNow; 
  18.     interface->PostBytes = TcpPostBytes; 
  19.     interface->GetConnectionInfo = TcpGetConnectionInfo; 
  20.     interface->StartLocalListening = TcpStartListening; 
  21.     interface->StopLocalListening = TcpStopListening; 
  22.     g_tcpConnCallback = callback; 
  23.  
  24.     if (g_tcpConnInfoList == NULL) { 
  25.         g_tcpConnInfoList = CreateSoftBusList(); 
  26.         if (g_tcpConnInfoList == NULL) { 
  27.             SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "Create tcpConnInfoList failed."); 
  28.             SoftBusFree(interface); 
  29.             return NULL
  30.         } 
  31.         g_tcpConnInfoList->cnt = 0; 
  32.     } 
  33.     if (g_tcpListener == NULL) { 
  34.         g_tcpListener = (SoftbusBaseListener *)SoftBusCalloc(sizeof(SoftbusBaseListener)); 
  35.         if (g_tcpListener == NULL) { 
  36.             SoftBusFree(interface); 
  37.             DestroySoftBusList(g_tcpConnInfoList); 
  38.             g_tcpConnInfoList = NULL
  39.             return NULL
  40.         } 
  41.     } 
  42.     g_tcpListener->onConnectEvent = TcpOnConnectEvent; 
  43.     g_tcpListener->onDataEvent = TcpOnDataEvent; 
  44.     return interface; 

interface 接口为tcp 通信方式的配置,ConnectDevice 连接设备,DisconnectDevice 断开连接,PostBytes 发送数据,GetConnectionInfo 获取设备端信息,StartLocalListening 启动监听,StopLocalListening关闭监听,注意,设备通过g_tcpListener 将设备挂载到软总线(bus)上面。每个设备需要通过不同端口建立起服务端和客服端模式,服务端用于监听数据请求。当有数据到来时,总线上面触发,经过一系列处理,最终通过g_connManagerCb变量获取对应数据。

openharmony软总线之--连接模块分析-鸿蒙HarmonyOS技术社区
  1. typedef struct { 
  2.     void (*OnConnected)(uint32_t connectionId, const ConnectionInfo *info); 
  3.     void (*OnDisconnected)(uint32_t connectionId, const ConnectionInfo *info); 
  4.     void (*OnDataReceived)(uint32_t connectionId, ConnModule moduleId, int64_t seq, char *data, int32_t len); 
  5. } ConnectCallback; 

 数据接收函数

ConnManagerRecvData

  1. void ConnManagerRecvData(uint32_t connectionId, ConnModule moduleId, int64_t seq, char *data, int32_t len) 
  2.     ConnListenerNode listener; 
  3.     int32_t ret; 
  4.     char* pkt = NULL
  5.     int32_t pktLen; 
  6.  
  7.     if (data == NULL) { 
  8.         return
  9.     } 
  10.  
  11.     if (len <= (int32_t)sizeof(ConnPktHead)) { 
  12.         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "len %d \r\n", len); 
  13.         return
  14.     } 
  15.  
  16.     ret = GetListenerByModuleId(moduleId, &listener); 
  17.     if (ret == SOFTBUS_ERR) { 
  18.         SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "GetListenerByModuleId fail moduleId %d \r\n", moduleId); 
  19.         return
  20.     } 
  21.  
  22.     pktLen = len - sizeof(ConnPktHead); 
  23.     pkt = data + sizeof(ConnPktHead); 
  24.     listener.callback.OnDataReceived(connectionId, moduleId, seq, pkt, pktLen); 
  25.     return

软总线通信模块由不同模块组合而成,如发现,认证等,当认证模块通过connect 接入总线时,数据获取流程为:softbus-> ConnManagerRecvData-> listener.callback.OnDataReceived,可以参考总线认证一块测试用例。

当然由于当前软总线功能不完善,部分数据流程存在不完善的情况。

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2022-03-18 15:29:02

Harmony鸿蒙架构

2021-07-02 09:16:21

鸿蒙HarmonyOS应用

2022-03-21 15:02:05

Harmonyhiperf鸿蒙

2022-05-26 15:28:03

网络管理Socket 模块

2022-04-24 15:07:09

GPS模块on函数鸿蒙

2021-07-01 14:21:58

鸿蒙HarmonyOS应用

2022-07-07 14:32:30

蓝牙技术鸿蒙

2022-08-12 19:13:07

etswifi连接操作

2022-11-28 15:42:39

分布式软总线鸿蒙

2023-04-17 16:10:14

鸿蒙蓝牙

2022-07-19 20:04:31

NAPI模块鸿蒙

2023-04-26 15:36:51

WPA鸿蒙

2022-06-13 14:31:02

资源调度鸿蒙

2015-09-11 09:15:32

RyuSDN

2023-04-26 15:29:35

NAPI模块鸿蒙

2022-02-09 19:25:49

Hi3861WiFi操作鸿蒙

2021-02-16 10:55:02

Nodejs模块

2022-03-03 18:39:01

Harmonyioremap鸿蒙

2022-09-06 15:25:22

Wifi设备开发

2022-01-26 15:07:04

bytrace工具OpenHarmon
点赞
收藏

51CTO技术栈公众号