demo app + SDK updates:\n- Add direct ANI transfer (prepare -> transfer)\n- Add firmware update flow (fetch latest w/ x-api-key, OTA transfer)\n- Extract x-api-key to NetworkConstants\n- Rename example app to demo; move folder and entry to DemoApp\n- Switch from SPM package dependency to local framework target\n- Enable automatic Info.plist generation for framework target\n- Add placeholder test to satisfy SwiftPM test target\n- Regenerate XcodeGen project (demo.xcodeproj)
This commit is contained in:
158
Sources/DuooomiBleSDK/Services/BleProtocolService.swift
Normal file
158
Sources/DuooomiBleSDK/Services/BleProtocolService.swift
Normal file
@@ -0,0 +1,158 @@
|
||||
import Foundation
|
||||
|
||||
/// 协议通信服务:帧收发、分包重组
|
||||
final class BleProtocolService: @unchecked Sendable {
|
||||
private let client: BleClient
|
||||
|
||||
/// 分包重组会话
|
||||
private struct FragmentSession {
|
||||
let total: Int
|
||||
var frames: [Int: Data] // curPage -> data
|
||||
}
|
||||
|
||||
private var fragments: [String: FragmentSession] = [:]
|
||||
private var listenerTask: Task<Void, Never>?
|
||||
|
||||
private var messageContinuation: AsyncStream<(commandType: UInt8, data: Data)>.Continuation?
|
||||
private(set) var incomingMessages: AsyncStream<(commandType: UInt8, data: Data)>!
|
||||
|
||||
init(client: BleClient) {
|
||||
self.client = client
|
||||
incomingMessages = AsyncStream { continuation in
|
||||
self.messageContinuation = continuation
|
||||
}
|
||||
BleLog.i("Protocol service created", "Protocol")
|
||||
}
|
||||
|
||||
// MARK: - Start/Stop Listening
|
||||
|
||||
/// 开始监听底层通知并进行帧解析与重组。
|
||||
/// - Note: 重新创建消息流并清空历史分片缓存。
|
||||
func startListening() {
|
||||
// Recreate the stream in case it was previously finished
|
||||
incomingMessages = AsyncStream { continuation in
|
||||
self.messageContinuation = continuation
|
||||
}
|
||||
|
||||
fragments.removeAll()
|
||||
|
||||
listenerTask = Task { [weak self] in
|
||||
guard let self else { return }
|
||||
BleLog.i("Start listening for notifications", "Protocol")
|
||||
for await data in self.client.notifications() {
|
||||
self.handleRawData(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 停止监听,结束消息流并清理缓存。
|
||||
func stopListening() {
|
||||
listenerTask?.cancel()
|
||||
listenerTask = nil
|
||||
messageContinuation?.finish()
|
||||
messageContinuation = nil
|
||||
fragments.removeAll()
|
||||
BleLog.i("Stop listening; fragments cleared", "Protocol")
|
||||
}
|
||||
|
||||
// MARK: - Send
|
||||
|
||||
/// 发送二进制数据(自动拆分为协议帧)。
|
||||
/// - Parameters:
|
||||
/// - type: 命令类型值。
|
||||
/// - data: 需要发送的原始负载。
|
||||
/// - onProgress: 发送进度回调(0.0~1.0)。
|
||||
/// - Throws: 写入失败或负载过大等错误。
|
||||
func send(
|
||||
type: UInt8,
|
||||
data: Data,
|
||||
onProgress: ((Double) -> Void)? = nil
|
||||
) async throws {
|
||||
let maxWriteLen = client.maximumWriteLength
|
||||
let maxDataSize = min(
|
||||
maxWriteLen - FrameConstants.headerSize - FrameConstants.footerSize,
|
||||
FrameConstants.maxDataSize
|
||||
)
|
||||
let safeMaxDataSize = max(1, maxDataSize)
|
||||
|
||||
let frames = ProtocolManager.createFrames(
|
||||
type: type,
|
||||
data: data,
|
||||
head: .appToDevice,
|
||||
maxDataSize: safeMaxDataSize
|
||||
)
|
||||
|
||||
guard !frames.isEmpty else {
|
||||
BleLog.e("Payload too large; frames empty", "Protocol")
|
||||
throw DuooomiBleError.transferFailed("Payload too large: exceeds 65535-page limit")
|
||||
}
|
||||
|
||||
let total = frames.count
|
||||
BleLog.d("Sending frames: total=\(total), maxChunk=\(safeMaxDataSize)", "Protocol")
|
||||
for (index, frame) in frames.enumerated() {
|
||||
try client.writeWithoutResponse(frame)
|
||||
try await Task.sleep(nanoseconds: FrameConstants.frameIntervalNanos)
|
||||
onProgress?(Double(index + 1) / Double(total))
|
||||
}
|
||||
BleLog.i("Frames sent: total=\(total)", "Protocol")
|
||||
}
|
||||
|
||||
/// 发送 JSON 命令,内部完成编码与帧拆分。
|
||||
/// - Parameters:
|
||||
/// - type: 命令类型。
|
||||
/// - payload: 可编码的命令体。
|
||||
/// - Throws: 编码/发送失败时抛出。
|
||||
func sendJSON<T: Encodable>(type: CommandType, payload: T) async throws {
|
||||
let jsonData = try JSONEncoder().encode(payload)
|
||||
try await send(type: type.rawValue, data: jsonData)
|
||||
}
|
||||
|
||||
// MARK: - Receive & Reassemble
|
||||
|
||||
private func handleRawData(_ data: Data) {
|
||||
guard let frame = ProtocolManager.parseFrame(data) else {
|
||||
BleLog.w("Drop invalid frame (len=\(data.count))", "Protocol")
|
||||
return
|
||||
}
|
||||
|
||||
if frame.subpageTotal > 0 {
|
||||
handleFragment(frame)
|
||||
} else {
|
||||
BleLog.d("Single frame received: type=\(frame.type), len=\(frame.data.count)", "Protocol")
|
||||
messageContinuation?.yield((commandType: frame.type, data: frame.data))
|
||||
}
|
||||
}
|
||||
|
||||
private func handleFragment(_ frame: ProtocolFrame) {
|
||||
// Match TS reference behavior: scope reassembly per peripheral so concurrent transfers
|
||||
// (or stale fragments from a previous device after reconnect) cannot pollute each other.
|
||||
let deviceKey = client.connectedPeripheral?.identifier.uuidString ?? "unknown"
|
||||
let key = "\(deviceKey)_\(frame.type)"
|
||||
|
||||
if fragments[key] == nil {
|
||||
fragments[key] = FragmentSession(total: Int(frame.subpageTotal), frames: [:])
|
||||
BleLog.d("New fragment session: key=\(key), total=\(frame.subpageTotal)", "Protocol")
|
||||
}
|
||||
|
||||
guard var session = fragments[key] else { return }
|
||||
guard frame.curPage < session.total else { return }
|
||||
|
||||
session.frames[Int(frame.curPage)] = frame.data
|
||||
fragments[key] = session
|
||||
|
||||
// Check if all pages received
|
||||
guard session.frames.count == session.total else { return }
|
||||
|
||||
// Reassemble: from high page to low page
|
||||
var combined = Data()
|
||||
for i in stride(from: session.total - 1, through: 0, by: -1) {
|
||||
if let part = session.frames[i] {
|
||||
combined.append(part)
|
||||
}
|
||||
}
|
||||
|
||||
fragments.removeValue(forKey: key)
|
||||
BleLog.i("Fragment reassembled: key=\(key), size=\(combined.count)", "Protocol")
|
||||
messageContinuation?.yield((commandType: frame.type, data: combined))
|
||||
}
|
||||
}
|
||||
98
Sources/DuooomiBleSDK/Services/DeviceInfoService.swift
Normal file
98
Sources/DuooomiBleSDK/Services/DeviceInfoService.swift
Normal file
@@ -0,0 +1,98 @@
|
||||
import Foundation
|
||||
|
||||
/// 设备命令服务:发送 JSON 命令,等待协议响应
|
||||
final class DeviceInfoService {
|
||||
private let protocolService: BleProtocolService
|
||||
|
||||
init(protocolService: BleProtocolService) {
|
||||
self.protocolService = protocolService
|
||||
}
|
||||
|
||||
// MARK: - Commands
|
||||
|
||||
/// 发送获取设备信息命令。
|
||||
/// - Throws: 发送失败时抛出。
|
||||
func getDeviceInfo() async throws {
|
||||
try await protocolService.sendJSON(
|
||||
type: .getDeviceInfo,
|
||||
payload: CommandPayload(type: CommandType.getDeviceInfo.rawValue)
|
||||
)
|
||||
}
|
||||
|
||||
/// 发送获取设备版本命令。
|
||||
/// - Throws: 发送失败时抛出。
|
||||
func getDeviceVersion() async throws {
|
||||
try await protocolService.sendJSON(
|
||||
type: .getDeviceVersion,
|
||||
payload: CommandPayload(type: CommandType.getDeviceVersion.rawValue)
|
||||
)
|
||||
}
|
||||
|
||||
/// 发送绑定命令。
|
||||
/// - Parameter userId: 用户唯一标识。
|
||||
/// - Throws: 发送失败时抛出。
|
||||
func bindDevice(userId: String) async throws {
|
||||
try await protocolService.sendJSON(
|
||||
type: .bindDevice,
|
||||
payload: BindPayload(type: CommandType.bindDevice.rawValue, userId: userId)
|
||||
)
|
||||
}
|
||||
|
||||
/// 发送解绑命令。
|
||||
/// - Parameter userId: 用户唯一标识。
|
||||
/// - Throws: 发送失败时抛出。
|
||||
func unbindDevice(userId: String) async throws {
|
||||
try await protocolService.sendJSON(
|
||||
type: .unbindDevice,
|
||||
payload: BindPayload(type: CommandType.unbindDevice.rawValue, userId: userId)
|
||||
)
|
||||
}
|
||||
|
||||
/// 发送删除文件命令。
|
||||
/// - Parameter key: 文件 key 或完整 URL。
|
||||
/// - Throws: 发送失败时抛出。
|
||||
func deleteFile(key: String) async throws {
|
||||
try await protocolService.sendJSON(
|
||||
type: .deleteFile,
|
||||
payload: FileKeyPayload(type: CommandType.deleteFile.rawValue, key: key)
|
||||
)
|
||||
}
|
||||
|
||||
/// 发送传输准备命令。
|
||||
/// - Parameters:
|
||||
/// - key: 文件标识 key。
|
||||
/// - size: 文件大小(字节)。
|
||||
/// - Throws: 发送失败时抛出。
|
||||
func prepareTransfer(key: String, size: Int) async throws {
|
||||
try await protocolService.sendJSON(
|
||||
type: .prepareTransfer,
|
||||
payload: PrepareTransferPayload(
|
||||
type: CommandType.prepareTransfer.rawValue,
|
||||
key: key,
|
||||
size: size
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Command Payloads
|
||||
|
||||
private struct CommandPayload: Encodable {
|
||||
let type: UInt8
|
||||
}
|
||||
|
||||
private struct BindPayload: Encodable {
|
||||
let type: UInt8
|
||||
let userId: String
|
||||
}
|
||||
|
||||
private struct FileKeyPayload: Encodable {
|
||||
let type: UInt8
|
||||
let key: String
|
||||
}
|
||||
|
||||
private struct PrepareTransferPayload: Encodable {
|
||||
let type: UInt8
|
||||
let key: String
|
||||
let size: Int
|
||||
}
|
||||
48
Sources/DuooomiBleSDK/Services/FileTransferService.swift
Normal file
48
Sources/DuooomiBleSDK/Services/FileTransferService.swift
Normal file
@@ -0,0 +1,48 @@
|
||||
import Foundation
|
||||
|
||||
/// 文件传输服务:下载文件并通过协议发送二进制数据
|
||||
final class FileTransferService {
|
||||
private let protocolService: BleProtocolService
|
||||
|
||||
init(protocolService: BleProtocolService) {
|
||||
self.protocolService = protocolService
|
||||
}
|
||||
|
||||
/// 传输文件到设备
|
||||
/// - Parameters:
|
||||
/// - fileUri: 本地 file:// URL 或远程 https:// URL
|
||||
/// - commandType: 传输命令类型
|
||||
/// - onProgress: 进度回调 0.0 ~ 1.0
|
||||
func transferFile(
|
||||
fileUri: String,
|
||||
commandType: CommandType,
|
||||
onProgress: ((Double) -> Void)? = nil
|
||||
) async throws {
|
||||
BleLog.i("Load file: \(fileUri)", "Transfer")
|
||||
let data = try await loadFileData(from: fileUri)
|
||||
BleLog.d("File loaded: size=\(data.count) bytes", "Transfer")
|
||||
|
||||
try await protocolService.send(
|
||||
type: commandType.rawValue,
|
||||
data: data,
|
||||
onProgress: onProgress
|
||||
)
|
||||
}
|
||||
|
||||
private func loadFileData(from uri: String) async throws -> Data {
|
||||
guard let url = URL(string: uri) else {
|
||||
throw DuooomiBleError.transferFailed("Invalid URL: \(uri)")
|
||||
}
|
||||
|
||||
if url.isFileURL {
|
||||
return try Data(contentsOf: url)
|
||||
} else {
|
||||
let (data, response) = try await URLSession.shared.data(from: url)
|
||||
guard let httpResponse = response as? HTTPURLResponse,
|
||||
(200...299).contains(httpResponse.statusCode) else {
|
||||
throw DuooomiBleError.transferFailed("Failed to download file: \(uri)")
|
||||
}
|
||||
return data
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user