- 论坛徽章:
- 1
|
本帖最后由 moxies 于 2015-08-31 23:21 编辑
晚上好,
有两个问题请教大家:
1、在看<endian.h>文件时,发现他引入了另一个头文件<bits/endian.h>,后一个文件是在/usr/include/i386-linux-gnu下,请问是在哪里指定的i386-linux-gnu这个路径的? (grep -R -n i386-linux-gnu /usr/include 无果)。
2、uint16_t bt_get_unaligned 相关,先上测试代码:- #include <bluetooth/bluetooth.h>
- #include "hci.h"
- #include "mgmt.h"
- #include <inttypes.h>
- int main(){
- printf("result: %d\n",1&1<<1);
- char buf[] = {0x04,0x04,0x0a,0x2e,0xe7,0xde,0x3d,0x1e,0x00,0x00,0x1f,0x00,0x01};
- struct mgmt_hdr *hdr = (void *)buf;
- printf("%s\n",(char*)&hdr->opcode); //这里实在不知道改怎么写了。想先把原始值打出来,和转换后的做对比。
- uint16_t code = btohs(bt_get_unaligned(&hdr->opcode));
- if(code == 0x000B){
- printf("ok\n");
- }
- printf("result is : %" PRIu16 "\n",code);
- return 0;
- }
复制代码 输出是:result is : 1028
这是bluez的代码:- opcode = btohs(bt_get_unaligned(&hdr->opcode));
- len = btohs(bt_get_unaligned(&hdr->len));
- index = btohs(bt_get_unaligned(&hdr->index));
- if (ret != MGMT_HDR_SIZE + len) {
- error("Packet length mismatch. ret %zd len %u", ret, len);
- return TRUE;
- }
- switch (opcode) {
- case MGMT_EV_CMD_COMPLETE:
- mgmt_cmd_complete(sk, index, buf + MGMT_HDR_SIZE, len);
- break;
- 。。。。
复制代码 补充下bt_get_unaligned宏:- #define bt_get_unaligned(ptr) \
- ({ \
- struct __attribute__((packed)) { \
- __typeof__(*(ptr)) __v; \
- } *__p = (__typeof__(__p)) (ptr); \
- __p->__v; \
- })
复制代码 跟踪bluez日志发现bluez 走的是MGMT_EV_DEVICE_CONNECTED(0x000B)分支。
这里就抓狂了。测试中的code 和 bluez的opcode 对不上了。很不解。也许printf的问题。还请大神帮忙讲讲bt_get_unaligned这个宏。为什么要这么做。谢谢。
|
|