You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.5 KiB

import Foundation
#if canImport(JL_BLEKit)
import JL_BLEKit
#endif
/// Custom RCSP command pass-through. Mirrors `feature/CustomCmdFeature.kt`.
///
/// Android's plugin takes (opCode: Int, payload: ByteArray) and the SDK
/// wraps them into a private RCSP frame. The iOS SDK exposes only
/// `cmdCustomData:isNeedResponse:Result:` which takes a single `data`
/// argument, so we prepend the opCode byte to the payload and call that.
/// The reply comes back through the result block. This matches the shape
/// the Dart caller expects: a `[Int]` response.
class CustomCmdFeature {
weak var server: JieliHomeServer?
init(server: JieliHomeServer) { self.server = server }
func send(
address: String,
opCode: UInt8,
payload: Data,
completion: @escaping (Data?, Error?) -> Void
) {
#if canImport(JL_BLEKit)
guard let entity = server?.connectFeature.currentEntity() as? JL_EntityM,
entity.mUUID == address else {
completion(nil, PluginError("CUSTOM_CMD_ERR", "no active session"))
return
}
var frame = Data(capacity: 1 + payload.count)
frame.append(opCode)
frame.append(payload)
entity.mCmdManager.mCustomManager.cmdCustomData(
frame,
isNeedResponse: true
) { status, _, data in
if status == .success {
completion(data ?? Data(), nil)
} else {
completion(nil, PluginError("CUSTOM_CMD_ERR", "JL_CMDStatus=\(status.rawValue)"))
}
}
#else
completion(nil, PluginError("CUSTOM_CMD_ERR", "JL_BLEKit not linked"))
#endif
}
}