如果您要大量使用 AV/C 帧,而不是创建一个结构(这对部分字节打包没有帮助),您应该创建一个AVCFrame
类,以便于设置这些帧,健全性检查你给它的值,有一个调试描述,并会为你处理所有蹩脚的细节。
您的代码可能如下所示:
AVCFrame *frame = [AVCFrame frameWithCommandType:AVCCommandTypePlay
subunitType:mySubunitType
subunitID:mySubunitID];
// You likely won't actually be writing to the L2CAPChannel. See below.
[l2capChannel writeAsync:[frame mutableBytes] length:[frame length] refcon:nil];
这不是最好的界面。您需要通读AV/C 数字接口命令集通用规范。
就字节打包而言(最终将不得不发生),您将需要使用以下内容:
// Returns |subunitType| shifted and masked appropriately for bit_oring
// with subunit ID to create an address octet.
inline UInt8
AVRCAddressSubunitType(UInt8 subunitType) {
const UInt8 kLeastThreeBytes = 0x07;
UInt8 shiftedType = (subunitType << 3) & ~kLeastThreeBytes;
return shiftedType;
}
// Returns |subunitID| masked appropriately for bit_oring with subunit type
// to create an address octet.
inline UInt8
AVRCAddressSubunitID(UInt8 subunitID) {
const UInt8 kLeastThreeBytes = 0x07;
UInt8 maskedID = subunitID & kLeastThreeBytes;
if (subunitID & ~kLeastThreeBytes) {
NSLog(@"*** %s: subunit ID %#hhx > 0x07 cannot be represented "
"in the 3 bits allotted. Truncating to %#hhx.",
__PRETTY_FUNCTION__, subunitID, maskedID);
}
return maskedID;
}
- (void)l2capChannelOpenComplete:(IOBluetoothL2CAPChannel *)l2capChannel
status:(IOReturn)error {
/* might be worth looking at the error... */
NSLog(@"%s: open complete - "
"error: (system: %#x; subsystem: %#x; code: %#x)",
__PRETTY_FUNCTION__,
err_get_system(error), err_get_sub(error), err_get_code(error));
/* to send, first pack your data into byte-sized variables */
// some variables...
// address byte layout is [3:7] = 9 = PANEL; [0:2] = 0 = subunit ID
UInt8 address = (AVRCAddressSubunitType(0x09) | AVRCAddressSubunitID(0x00));
// some more variables...
/* create a mutable data and append the bytes in sequence */
// some appending...
[playData appendBytes:&address length:sizeof(address)];
// more appending...
/* finally, send all the bytes */
[l2capChannel writeAsync:[playData mutableBytes]
length:[playData length]
refcon:NULL];
}
有关 的更多详细信息IOWhatever
,请查看广泛的 IOKit 文档。至少在 10.5 中,文档集中的参考文档(而不是编程指南)有点古怪,因此您最好查看标题本身。
您需要查阅比目前更多的文档。您包含的图的 AV/C 命令帧实际上是AVCTP 帧的有效负载(承载在命令/响应消息信息字段中) ,这是您实际上必须通过 L2CAP 传输发送的内容。AVCTP规范在“附录 A,AVCTP 上接口”中勾画了一个基本的 API。
您需要找到或自己编写一个 AVCTP 库才能发送 AV/C 命令帧。您需要让 AVCTP 库包装 L2CAP 通道,以便您实际通过它发送命令帧并从中接收命令帧。祝你好运!与硬件交互会很有趣,而且您会学到很多东西。