使用 IOKit 通过 USB 进行 OBEX

发布于 2024-12-26 09:57:51 字数 1647 浏览 2 评论 0原文

我对整个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯了 2025-01-02 09:57:51

最后我想通了。问题由两部分组成。第一个问题是 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 of kOBEXTransportEventTypeDataReceived to the clientHandleIncomingData: method.

Now the connection to the device works as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文