使用 IOKit 通过 USB 进行 OBEX
我对整个 IOKit 东西都很陌生,所以可能有一些简单的解决方案可以解决我的问题。我正在使用通过 USB 执行 OBEX 的 Smartpen。到目前为止,我得到了 OBEXSession 的子类来成功连接到设备。
OBEXAddTargetHeader("LivescribeService",
strlen("LivescribeService"),
header);
CFMutableDataRef headerData = OBEXHeadersToBytes(header);
OBEXError error = [session OBEXConnect:kOBEXConnectFlagNone
maxPacketLength:maxPacketLength
optionalHeaders:(void *)CFDataGetBytePtr(headerData)
optionalHeadersLength:CFDataGetLength(headerData)
eventSelector:@selector(openedConnection)
selectorTarget:target
refCon:NULL];
之后error
为0并且openedConnection消息被发送到目标。 USB 管道写入和读取的数据看起来没问题。现在我想发送 GET,但不知何故失败了。
UInt32 connectionIDInt = 0x1;
const char *connectionID[4] = {0x0,0x0,0x0,0x0};
memcpy(connectionID, &connectionIDInt, 4);
OBEXAddConnectionIDHeader(connectionID, 4, header);
OBEXAddNameHeader(CFSTR("ppdata?key=pp0000"), header);
headerData = OBEXHeadersToBytes(header);
error = [session OBEXGet:YES
headers:(void *)CFDataGetBytePtr(headerData)
headersLength:CFDataGetLength(headerData)
eventSelector:@selector(OBEXGetHandler:)
selectorTarget:target
refCon:nil];
但这总是会导致 kOBEXBadArgumentError
并且我完全不知道我做错了什么。我尝试使用不同的标题,它总是相同的,据我所知,这应该是正确的标题。或者还有什么其他论点可能是错误的?
这可能与我用于连接的 maxPacketLength
有关吗?我使用 1024 因为我不知道该用什么。我尝试调用 -getMaxPacketLength 但只返回 0。我是否必须重写该方法?或者我该如何处理该数据包长度?
I'm new to the entire IOKit stuff, so there might be some trivial solutions for my problems. I'm playing around with a Smartpen that does OBEX over USB. So far I got a subclass of OBEXSession
to Successfully connect to the Device.
OBEXAddTargetHeader("LivescribeService",
strlen("LivescribeService"),
header);
CFMutableDataRef headerData = OBEXHeadersToBytes(header);
OBEXError error = [session OBEXConnect:kOBEXConnectFlagNone
maxPacketLength:maxPacketLength
optionalHeaders:(void *)CFDataGetBytePtr(headerData)
optionalHeadersLength:CFDataGetLength(headerData)
eventSelector:@selector(openedConnection)
selectorTarget:target
refCon:NULL];
After that error
is 0 and the openedConnection message is sent to the target. The data that gets written and read to/from my USB pipe looks ok. Now I'd like to send a GET, but that somehow fails.
UInt32 connectionIDInt = 0x1;
const char *connectionID[4] = {0x0,0x0,0x0,0x0};
memcpy(connectionID, &connectionIDInt, 4);
OBEXAddConnectionIDHeader(connectionID, 4, header);
OBEXAddNameHeader(CFSTR("ppdata?key=pp0000"), header);
headerData = OBEXHeadersToBytes(header);
error = [session OBEXGet:YES
headers:(void *)CFDataGetBytePtr(headerData)
headersLength:CFDataGetLength(headerData)
eventSelector:@selector(OBEXGetHandler:)
selectorTarget:target
refCon:nil];
But that always results in a kOBEXBadArgumentError
and I have absolutely no idea what I'm doing wrong. I tried to play around with different headers, it's always the same, and as far as I know, this should be the correct header. Or what other argument could probably be wrong?
Might this have something to do with the maxPacketLength
I used for connecting? I used 1024 because I had no idea what to use. I tried to call -getMaxPacketLength but that returns just 0. Do I have to override that method? Or how do I have to deal with that packet length?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后我想通了。问题由两部分组成。第一个问题是 OBEXAddTargetHeader("LivescribeService", strlen("LivescribeService"), header);。因为 strlen 是字符串的长度,而不是使用了多少字节(0x00 字符串终止符为 +1),所以设备响应错误,因为它期望字符串被终止。遗憾的是 OBEXSession 只是忽略了从设备返回的错误。
第二个问题是我使用
kOBEXTransportEventTypeStatus
而不是kOBEXTransportEventTypeDataReceived
将接收到的数据发送到clientHandleIncomingData:
方法。现在与设备的连接按预期工作。
Finally I figured it out. The problem consisted of two parts. First problem was
OBEXAddTargetHeader("LivescribeService", strlen("LivescribeService"), header);
. Because strlen is the length of the string and not how many bytes are used (+1 for the 0x00 string terminator) the device responded with an error because it expects the string to be terminated. Sadly the OBEXSession just ignored the error that came back from the device.Second problem was that I sent the received data with
kOBEXTransportEventTypeStatus
instead ofkOBEXTransportEventTypeDataReceived
to theclientHandleIncomingData:
method.Now the connection to the device works as expected.