ble_server_main.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. --[[
  2. @module ble_server_main
  3. @summary ble server 主应用功能模块
  4. @version 1.0
  5. @date 2025.08.29
  6. @author 王世豪
  7. @usage
  8. 本文件为ble server 主应用功能模块,核心业务逻辑为:
  9. 1. 初始化BLE功能
  10. 2. 创建GATT数据库
  11. 3. 设置广播内容并开始广播
  12. 4. 处理各类BLE事件(连接、断开连接、写入请求等)
  13. 5. 接收并处理来自其他模块的请求
  14. 6. 依赖模块
  15. - ble_server_receiver: 用于处理接收到的BLE数据
  16. - ble_server_sender: 用于发送BLE数据
  17. ]]
  18. local ble_server_receiver = require "ble_server_receiver"
  19. local ble_server_sender = require "ble_server_sender"
  20. -- ble_server_main的任务名
  21. local TASK_NAME = ble_server_sender.TASK_NAME_PREFIX.."main"
  22. -- 配置参数
  23. config = {
  24. device_name = "LuatOS", -- 设备名称
  25. service_uuid = "FA00", -- 服务UUID
  26. char_uuid1 = "EA01", -- Characteristic 1
  27. char_uuid2 = "EA02", -- Characteristic 2
  28. char_uuid3 = "EA03", -- Characteristic 3
  29. }
  30. local bluetooth_device = nil
  31. local ble_device = nil
  32. local adv_create = nil
  33. local gatt_create = nil
  34. -- GATT数据库定义
  35. local att_db = {
  36. string.fromHex(config.service_uuid), -- Service UUID
  37. -- Characteristic
  38. { -- Characteristic 1 (Notify + Read + Write)
  39. string.fromHex(config.char_uuid1),
  40. ble.NOTIFY | ble.READ | ble.WRITE
  41. }, { -- Characteristic 2 (Write)
  42. string.fromHex(config.char_uuid2),
  43. ble.WRITE | ble.WRITE_CMD -- ble.WRITE_CMD表示支持Write Without Response写入
  44. }, { -- Characteristic 3 (Read)
  45. string.fromHex(config.char_uuid3),
  46. ble.READ
  47. }
  48. }
  49. -- 事件回调函数
  50. local function ble_event_cb(ble_device, ble_event, ble_param)
  51. -- 连接中心设备成功
  52. if ble_event == ble.EVENT_CONN then
  53. sys.sendMsg(TASK_NAME, "BLE_EVENT", "CONNECT", ble_param)
  54. -- 连接断开
  55. elseif ble_event == ble.EVENT_DISCONN then
  56. sys.sendMsg(TASK_NAME, "BLE_EVENT", "DISCONNECTED", ble_param.reason)
  57. -- 收到中心设备的写请求
  58. elseif ble_event == ble.EVENT_WRITE then
  59. sys.sendMsg(TASK_NAME, "BLE_EVENT", "WRITE_REQ", ble_param)
  60. end
  61. end
  62. -- 初始化BLE
  63. local function ble_init()
  64. -- 初始化蓝牙核心
  65. bluetooth_device = bluetooth_device or bluetooth.init()
  66. if not bluetooth_device then
  67. log.error("BLE", "蓝牙初始化失败")
  68. return false
  69. end
  70. -- 初始化BLE功能
  71. ble_device = ble_device or bluetooth_device:ble(ble_event_cb)
  72. if not ble_device then
  73. log.error("BLE", "当前固件不支持完整的BLE")
  74. return false
  75. end
  76. -- 创建GATT
  77. gatt_create = gatt_create or ble_device:gatt_create(att_db)
  78. if not gatt_create then
  79. log.error("BLE", "BLE创建GATT失败")
  80. return false
  81. end
  82. -- 设置广播内容
  83. adv_create = adv_create or ble_device:adv_create({
  84. addr_mode = ble.PUBLIC, -- 广播地址模式, 可选值: ble.PUBLIC, ble.RANDOM, ble.RPA, ble.NRPA
  85. channel_map = ble.CHNLS_ALL, -- 广播的通道, 可选值: ble.CHNL_37, ble.CHNL_38, ble.CHNL_39, ble.CHNLS_ALL
  86. intv_min = 120, -- 广播间隔最小值, 单位为0.625ms, 最小值为20, 最大值为10240
  87. intv_max = 120, -- 广播间隔最大值, 单位为0.625ms, 最小值为20, 最大值为10240
  88. adv_data = { -- 支持表格形式, 也支持字符串形式(255字节以内)
  89. {ble.FLAGS, string.char(0x06)}, -- 广播标志位
  90. {ble.COMPLETE_LOCAL_NAME, config.device_name}, -- 广播本地名称
  91. },
  92. adv_prop = ble.ADV_PROP_CONNECTABLE | ble.ADV_PROP_SCANNABLE -- 广播属性, ble.ADV_PROP_CONNECTABLE(可被连接), ble.ADV_PROP_SCANNABLE(可被扫描)
  93. })
  94. if not adv_create then
  95. log.error("BLE", "BLE创建广播失败")
  96. return false
  97. end
  98. -- 开始广播
  99. ble_device:adv_start()
  100. return true
  101. end
  102. -- 主任务处理函数
  103. local function ble_server_main_task_func()
  104. local result,msg
  105. while true do
  106. result = ble_init()
  107. if not result then
  108. log.error("ble_main_task_func", "ble_init error")
  109. goto EXCEPTION_PROC
  110. end
  111. while true do
  112. msg = sys.waitMsg(TASK_NAME, "BLE_EVENT")
  113. if not msg then
  114. log.error("ble_client_main_task_func", "waitMsg timeout")
  115. goto EXCEPTION_PROC
  116. end
  117. if msg[2] == "CONNECT" then
  118. local conn_param = msg[3]
  119. log.info("BLE", "设备连接成功: " .. conn_param.addr:toHex())
  120. sys.sendMsg(ble_server_sender.TASK_NAME, "BLE_EVENT", "CONNECT_OK", ble_device)
  121. elseif msg[2] == "DISCONNECTED" then
  122. log.info("BLE", "设备断开连接,原因: " .. msg[3])
  123. break
  124. -- 收到中心设备的写请求,将写的数据发给ble_server_receiver模块处理
  125. elseif msg[2] == "WRITE_REQ" then
  126. local ble_param = msg[3]
  127. log.info("BLE", "收到写请求: " .. ble_param.uuid_service:toHex() .. " " .. ble_param.uuid_characteristic:toHex() .. " " .. ble_param.data:toHex())
  128. ble_server_receiver.proc(ble_param.uuid_service:toHex(), ble_param.uuid_characteristic:toHex(), ble_param.data)
  129. end
  130. end
  131. ::EXCEPTION_PROC::
  132. log.error("ble_server_main_task_func", "异常退出, 5秒后重新开启广播")
  133. -- 停止广播
  134. ble_device:adv_stop()
  135. -- 清空此task绑定的消息队列中的未处理的消息
  136. sys.cleanMsg(TASK_NAME)
  137. -- 通知ble sender数据发送应用模块的task,ble连接已经断开
  138. sys.sendMsg(ble_server_sender.TASK_NAME, "BLE_EVENT", "DISCONNECTED")
  139. -- 5秒后跳转到循环体开始位置,自动发起重连
  140. sys.wait(5000)
  141. end
  142. end
  143. -- 启动主任务
  144. sys.taskInitEx(ble_server_main_task_func, TASK_NAME)