#include #include #include #include #include #include #include #include #include #include #include #include #define VERSION 0x01 enum bluetooth_attr { ATTR_UNSPEC, INDEX, TYPE, FLAGS, CHANGED, /* Add attributes here */ __ATTR_MAX, ATTR_MAX = __ATTR_MAX - 1 }; enum bluetooth_cmds { CMD_UNSPEC, NEWHOST, DELHOST, /* Add command here */ __CMD_MAX, CMD_MAX = __CMD_MAX - 1 }; static inline void set_bit(int nr, void *addr) { *((uint32_t *) addr + (nr >> 5)) |= (1 << (nr & 31)); } static inline int test_bit(int nr, void *addr) { return *((uint32_t *) addr + (nr >> 5)) & (1 << (nr & 31)); } static struct nla_policy bluetooth_policy[ATTR_MAX + 1] = { [INDEX] = { .type = NLA_U16 }, [TYPE] = { .type = NLA_U16 }, [FLAGS] = { .type = NLA_U32 }, [CHANGED] = { .type = NLA_U32 }, }; static int parse_cb(struct nl_msg *msg, void *arg) { int err,i=0; struct nlmsghdr *nlh = nlmsg_hdr(msg); struct nlattr *attrs[ATTR_MAX+1]; struct genlmsghdr *ghdr = nlmsg_data(nlh); err = genlmsg_parse(nlh, 0, attrs, ATTR_MAX, NULL); if (err < 0) return -EINVAL; printf("\nhello\n"); switch(ghdr->cmd) { case NEWHOST: if(!attrs[INDEX]) { printf("\nNo index\n"); return -EINVAL; } int index = nla_get_u16(attrs[INDEX]); uint32_t changed = nla_get_u16(attrs[CHANGED]); if (test_bit(HCI_UP, &changed)) printf("Hci%d UP changed %d", index, changed); break; default: printf("\nUnknown command"); break; } return 0; } int main() { struct nl_handle *sock; struct nl_msg *msg; int family; int err; uint32_t flags = 0; sock = nl_handle_alloc(); genl_connect(sock); family = genl_ctrl_resolve(sock, "bluetooth"); msg = nlmsg_alloc(); genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, NLM_F_REQUEST, NEWHOST, VERSION); nla_put_u16(msg, INDEX, 0); set_bit(HCI_UP, &flags); set_bit(HCI_PSCAN, &flags); nla_put_u32(msg, FLAGS, flags); nl_send_auto_complete(sock, msg); nlmsg_free(msg); nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, parse_cb, NULL); err = nl_recvmsgs_default(sock); printf("\nerr = %d\n", err); return 0; }