分布式通信子系统

简介

设备通信方式多种多样(USB/WIFI/BT等),不同通信方式使用差异很大且繁琐,同时通信链路的融合共享和冲突无法处理,通信安全问题也不好保证。本项目致力于实现近场设备间统一的分布式通信能力管理,提供不区分链路的设备发现和传输接口。目前实现能力包含:

  • 服务发布:服务发布后周边的设备可以发现并使用服务。
  • 数据传输:根据服务的名称和设备ID建立一个会话,就可以实现服务间的传输功能。
  • 安全:提供通信数据的加密能力。

开发者通过使用项目的API实现设备间高速安全的通信,不用关心通信细节管理,进而实现业务开发的跨平台能力。

目录

softbus_lite源代码目录结构如下图所示:

表1 softbus_lite源代码目录结构

名称 描述
authmanager 提供设备认证机制和设备知识库管理。
discovery 提供基于coap协议的设备发现机制。
trans_service 提供认证和传输通道。

约束

  • 语言限制:C语言。
  • 组网环境:必须确保设备在同一个局域网中。

使用

  1. 设备发现

用户使用发现功能时,需要保证发现端设备与被发现端设备在同一个局域网内,并且互相能收到对方以下流程的报文。

(1) 发现端设备,发起discover请求后,使用coap协议在局域网内发送广播。报文如下:

分布式通信子系统 - 图1

(2)被发现端设备使用PublishService接口发布服务,接收端收到广播后,发送coap协议单播给发现端。报文格式如下:

分布式通信子系统 - 图2

(3)发现端设备收到报文会更新设备信息。

被发现端接口使用示例如下:


1. // 回调函数声明:
2. void onSuccess(int publishId)
3. {
4. printf("public success,publishId = %d\r\n", publishId);
5. }
6. void onFail(int publishId, PublishFailReason reason)
7. {
8. printf("publish failed, publishId = %d, reason = %d\r\n", publishId, reason);
9. }
10. // 服务发布接口使用
11. PublishInfo info = {0};
12. IPublishCallback cb = {0};
13. cb.onPublishSuccess = onSuccess;
14. cb.onPublishFail = onFail;
15. char a[] = "456";
16. info.capabilityData = a;
17. info.capability = "ddmpCapability";
18. info.dataLen = strlen("456");
19. info.medium = 2;
20. info.publishId = 1;
21. PublishService("cxx", &info, &cb);

  1. 传输

软总线提供统一的基于Session的传输功能,业务可以通过sessionId收发数据或获取其相关基本属性。当前本项目只实现被动接收Session连接的功能,业务可根据自身需要及Session自身属性判断是否接受此Session,如不接受,可以主动拒绝此连接。本项目暂未提供打开Session的相关能力。

传输主要代码示例如下:


1. // 定义业务自身的业务名称,会话名称及相关回调
2. const char *g_pkgName = "BUSINESS_NAME";
3. const char *g_sessionName = "SESSION_NAME";
4. struct ISessionListener * g_sessionCallback= NULL;

6. // 回调实现:接收对方通过SendBytes发送的数据,此示例实现是接收到对端发送的数据后回复固定消息
7. void OnBytesReceivedTest(int sessionId, const void* data, unsigned int dataLen)
8. {
9. printf("OnBytesReceivedTest\n");
10. printf("Recv Data: %s\n", (char *)data);
11. printf("Recv Data dataLen: %d\n", dataLen);
12. char *testSendData = "Hello World, Hello!";
13. SendBytes(sessionId, testSendData, strlen(testSendData));
14. return;
15. }

17. // 回调实现:用于处理会话关闭后的相关业务操作,如释放当前会话相关的业务资源,会话无需业务主动释放
18. void OnSessionClosedEventTest(int sessionId)
19. {
20. printf("Close session successfully, sessionId=%d\n", sessionId);
21. }

23. // 回调实现:用于处理会话打开后的相关业务操作。返回值为0,表示接收;反之,非0表示拒绝。此示例表示只接受其他设备的同名会话连接
24. int OnSessionOpenedEventTest(int sessionId)
25. {
26. if (strcmp(GetPeerSessionName(sessionId), SESSION_NAME) != 0) {
27. printf("Reject the session which name is different from mine, sessionId=%d\n", sessionId);
28. return -1;
29. }
30. printf("Open session successfully, sessionId=%d\n", sessionId);
31. return 0;
32. }

34. // 向SoftBus注册业务会话服务及其回调
35. int StartSessionServer()
36. {
37. if (g_sessionCallback != NULL) {
38. g_sessionCallback = (struct ISessionListener*)malloc(sizeof(struct ISessionListener));
39. }
40. if (g_sessionCallback == NULL) {
41. printf("Failed to malloc g_sessionCallback!\n");
42. return -1;
43. }
44. g_sessionCallback->onBytesReceived = OnBytesReceivedTest;
45. g_sessionCallback->onSessionOpened = OnSessionOpenedEventTest;
46. g_sessionCallback->onSessionClosed = OnSessionClosedEventTest;
47. int ret = CreateSessionServer(g_pkgName, g_sessionName, g_sessionCallback);
48. if (ret < 0) {
49. printf("Failed to create session server!\n");
50. free(g_sessionCallback);
51. g_sessionCallback = NULL;
52. }
53. return ret;
54. }

56. // 从SoftBus中删除业务会话服务及其回调
57. void StopSessionServer()
58. {
59. int ret = RemoveSessionServer(g_pkgName, g_sessionName);
60. if (ret < 0) {
61. printf("Failed to remove session server!\n");
62. return;
63. }
64. if (g_sessionCallback != NULL) {
65. free(g_sessionCallback);
66. g_sessionCallback = NULL;
67. }
68. }

涉及仓

communication_frameworks_wifi_lite

communication_frameworks_ipc_lite

communication_hals_wifi_lite

communication_interfaces_kits_ipc_lite

communication_interfaces_kits_softbuskit_lite

communication_interfaces_kits_wifi_lite

communication_services_softbus_lite