📄 btctl.c
字号:
size = atoi(argv[argind++]); fd = socket(PF_AFFIX, SOCK_SEQPACKET, BTPROTO_L2CAP); if (fd < 0) { printf("Unable to create RFCOMM socket: %d\n", PF_AFFIX); return 1; } saddr.bda = bda; saddr.port = 0; saddr.devnum = HCIDEV_ANY; err = connect(fd, (struct sockaddr*)&saddr, sizeof(saddr)); if (err < 0) { printf("Unable to connect to remote side\n"); close(fd); return 1; } data = (__u8*)malloc(size); if( data == NULL ) return 0; for(i = 0; i < size; i++) data[i] = i; printf("Sending %d bytes...\n", size); print_data(data, size); if (cmd->cmd == 0) { err = l2cap_ping(fd, data, size); if (err < 0) { printf("ping error\n"); exit(1); } printf("Received %d bytes back\n", err); print_data(data, err); } else if (cmd->cmd == 1) { struct timeval tv_start, tv_end; long int sec, rsec; long int usec, rusec; double speed; gettimeofday(&tv_start, NULL); err = l2cap_singleping(fd, data, size); if (err < 0) { printf("ping error\n"); exit(1); } printf("flushing...\n"); err = l2cap_flush(fd); if (err < 0) { printf("flush error\n"); exit(1); } gettimeofday(&tv_end, NULL); sec = tv_end.tv_sec - tv_start.tv_sec; usec = (1000000 * sec) + tv_end.tv_usec - tv_start.tv_usec; rsec = usec/1000000; rusec = (usec - (rsec * 1000000))/10000; speed = (double)(size)/((double)(rsec) + (double)(rusec)/100); printf("%d bytes sent in %ld.%ld secs (%.2f B/s)\n", size, rsec, rusec, speed); } close(fd); return 0;}void sig_handler(int sig){}int cmd_periodic_inquiry(struct btctl_command *cmd){ int fd; unsigned char buf[HCI_MAX_EVENT_SIZE]; unsigned int max_period_len, min_period_len, length; int err; __u8 num; struct Inquiry_Result_Event *ir = (void*)buf; struct Inquiry_Complete_Event *ic = (void*)buf; /* Default values */ length = 3; min_period_len = 4; max_period_len = 5; if (cmd->cmd == 0) { if (argv[argind]) { sscanf(argv[argind++], "%d", &max_period_len); if(argv[argind]) { sscanf(argv[argind++], "%d", &min_period_len); if(argv[argind]) { sscanf(argv[argind++], "%d", &length); } } } } num=0; /* Unlimited */ fd = hci_open(btdev); if (fd < 0) { printf("Unable to open device %s: %s\n", btdev, strerror(errno)); return -1; } switch (cmd->cmd) { case 1: { err = HCI_ExitPeriodicInquiryMode(fd); if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } return 0; } } err = HCI_PeriodicInquiryMode(fd, max_period_len, min_period_len, length, num); if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } hci_event_mask(fd, COMMAND_STATUS_MASK|INQUIRY_RESULT_MASK|INQUIRY_COMPLETE_MASK); signal(SIGINT, sig_handler); signal(SIGTERM, sig_handler); signal(SIGABRT, sig_handler); signal(SIGQUIT, sig_handler); do { err = hci_recv_event(fd, buf, sizeof(buf), 20); if (err < 0) { fprintf(stderr, "%s\n", hci_error(err)); fprintf(stderr, "Exit from Periodic Inquire Mode ...\n"); HCI_ExitPeriodicInquiryMode(fd); exit(1); } if (ir->hdr.EventCode == HCI_E_INQUIRY_RESULT) { int i; for (i = 0; i < ir->Num_Responses; i++) { printf("Found bda: %s\n", bda2str(&ir->Results[i].bda)); } } } while (1);//ic->hdr.EventCode != HCI_E_INQUIRY_COMPLETE); return ic->Status;}int cmd_link_info(struct btctl_command *cmd){ int err, fd, interval; BD_ADDR bda; __u8 lq; __s8 RSSI; __s8 cpl; int handle; if (argv[argind] == NULL) { printf("parameters missing\n"); print_usage(cmd); return 1; } err = get_bda(&bda, argv[argind++]); if (err) { printf("Incorrect address given\n"); return 1; } interval = 0; /* No loop */ if (cmd->cmd == 3) { if (argv[argind] != NULL) { sscanf(argv[argind], "%d", &interval); if (interval <= 0 || interval > 100) { printf("Interval value 1 - 100\n"); exit(1); } } } fd = hci_open(btdev); if (fd < 0) { printf("Unable to open device %s: %s\n", btdev, strerror(errno)); return -1; } handle = hci_get_conn(fd, &bda); if (handle < 0) { printf("Connection not found\n"); return 1; } switch (cmd->cmd) { case 0: /* lq */ { err = HCI_GetLinkQuality(fd, handle, &lq); if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } printf("Link quality: %d\n", lq); break; } case 1: /* RSSI dBm = 10*log(P/Pref) Pref = 1 milliwatt */ { err = HCI_ReadRSSI(fd, handle, &RSSI); if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } printf("RSSI: %d\n", RSSI); break; } case 2: /* Transmit Power Level */ { err = HCI_ReadTransmitPowerLevel(fd, handle, 0, &cpl); /* 0 means current transmit power level */ if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } printf("Current Transmit Power Level: %d\n", cpl); break; } case 3: /* All HCI info parameters */ { do { err = HCI_ReadRSSI(fd, handle, &RSSI); if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } err = HCI_GetLinkQuality(fd, handle, &lq); if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } err = HCI_ReadTransmitPowerLevel(fd, handle, 0, &cpl); /* 0 means current transmit power level */ if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } printf("RSSI: %d, LQ: %d, CTPL: %d\n", RSSI, lq, cpl); sleep(interval); } while (interval); break; } } //pause(); hci_close(fd); return 0;}int get_port(char *svc, struct sockaddr_affix *saddr){#if defined(CONFIG_AFFIX_SDP) int sch, err; uint16_t ServiceID; uint16_t count; slist_t *searchList = NULL; slist_t *attrList = NULL; slist_t *svcList = NULL; sdpsvc_t *svcRec; if (strncasecmp(svc, "SERial", 3) == 0) ServiceID = SDP_UUID_SERIAL_PORT; else if (strncasecmp(svc, "DUN", 3) == 0) ServiceID = SDP_UUID_DUN; else if (strncasecmp(svc, "FAX", 3) == 0) ServiceID = SDP_UUID_FAX; else if (strncasecmp(svc, "LAN", 3) == 0) ServiceID = SDP_UUID_LAN; else if (strncasecmp(svc, "HEAdset", 3) == 0) ServiceID = SDP_UUID_HEADSET; else if (strncasecmp(svc, "AUDio", 3) == 0) ServiceID = SDP_UUID_HANDSFREE_AG; else { fprintf(stderr, "unknown service: %s\n", svc); return 1; } /* search for service ServiceID */ s_list_append_uuid16(&searchList, ServiceID); /* set attributes to find */ s_list_append_uint(&attrList, SDP_ATTR_SERVICE_RECORD_HANDLE); s_list_append_uint(&attrList, SDP_ATTR_PROTO_DESC_LIST); err = __sdp_search_attr_req(saddr, searchList, IndividualAttributes, attrList, 0xffff, &svcList, &count); s_list_destroy(&searchList); s_list_free(&attrList); if (err) { fprintf(stderr, "%s\n", sdp_error(err)); return -1; } if (count == 0) { printf("no services found\n"); return -1; } svcRec = s_list_dequeue(&svcList); // get first sdp_free_svclist(&svcList); sch = sdp_get_rfcomm_port(svcRec); sdp_free_svc(svcRec); if (sch > 0) { printf("Service found on channel: %d\n", sch); return sch; } else if (sch == 0) { printf("Service is not available\n"); return -1; } else { printf("Unable to get service channel\n"); return -2; }#endif return -1;}/* * RFCOMM stuff */int cmd_connect(struct btctl_command *cmd){ int err, fd; BD_ADDR bda; int sch; int line = RFCOMM_BTY_ANY; struct sockaddr_affix saddr; if (argc - argind < 2) { printf("Parameters missing\n"); print_usage(cmd); return 1; } err = get_bda(&bda, argv[argind++]); if (err) { printf("Incorrect address given\n"); return 1; } if (cmd->cmd != 2) printf("Connecting to host %s ...\n", bda2str(&bda)); saddr.family = PF_AFFIX; saddr.bda = bda; saddr.devnum = hci_devnum(btdev);//HCIDEV_ANY; err = sscanf(argv[argind++], "%d", &sch); if (argv[argind]) { // bty line number line = atoi(argv[argind]); } if (err > 0) goto found; if (!sdpmode) { fprintf(stderr, "SDP disabled\n"); return 1; } else { sch = get_port(argv[argind - 1], &saddr); if (sch < 0) return sch; }found: if (cmd->cmd == 0) // just sdp request for port return 0; saddr.port = sch; if (cmd->cmd != 2) printf("Connecting to channel %d ...\n", sch); fd = socket(PF_AFFIX, SOCK_STREAM, BTPROTO_RFCOMM); if (fd < 0) { printf("Unable to create RFCOMM socket: %d\n", PF_AFFIX); return 1; } if (cmd->cmd == 1) { // connect err = rfcomm_set_type(fd, RFCOMM_TYPE_BTY); if (err < 0) { BTERROR("Unable to set RFCOMM interface type to tty"); close(fd); return 1; } err = connect(fd, (struct sockaddr*)&saddr, sizeof(saddr)); if (err < 0) { fprintf(stderr, "Unable to connect to remote side: %s\n", strerror(errno)); close(fd); return 1; } line = rfcomm_open_tty(fd, line); if (line < 0) { printf("Unable to bind a port to the socket\n"); close(fd); return 1; } printf("Connected. Bound to line %d [/dev/bty%d].\n", line, line); } else if (cmd->cmd == 2) { // bind line = rfcomm_bind_tty(fd, &saddr, line); if (line < 0) { fprintf(stderr, "Unable to bind a port to the socket: %s\n", strerror(errno)); close(fd); return 1; } printf("Bound to bda: %s, channel: %d, line %d [/dev/bty%d].\n", bda2str(&bda), sch, line, line); } close(fd); return 0;}int cmd_disconnect(struct btctl_command *cmd){ int line = -1, err; if (argv[argind] == NULL) { printf("disconnect all\n"); } else { line = atoi(argv[argind]); printf("Disconnecting line %d [/dev/bty%d]\n", line, line); } // Disconnect here err = rfcomm_close_tty(line); if (err < 0) { printf("Unable to disconnect line: %d\n", line); return 1; } return 0;}int cmd_switch(struct btctl_command *cmd){ int err, fd; int role; BD_ADDR bda; fd = hci_open(btdev); if (fd < 0) { printf("Unable to open device %s: %s\n", btdev, strerror(errno)); return -1; } err = get_bda(&bda, argv[argind++]); if (err) { printf("Incorrect address given\n"); return 1; } if (cmd->cmd == 1) { /* policy */ int handle; int policy; handle = hci_get_conn(fd, &bda); if (handle < 0) { printf("Connection not found\n"); return 1; } if (argv[argind]) { err = sscanf(argv[argind++], "%x", &policy); if (err <= 0) policy = 0x0001; } else policy = 0x0001; err = HCI_WriteLinkPolicySettings(fd, handle, policy); } else { role = atoi(argv[argind++]); err = HCI_SwitchRole(fd, &bda, role); } if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } hci_close(fd); return 0;}int cmd_status(struct btctl_command *cmd){ struct rfcomm_port info[10]; int count; count = rfcomm_get_ports(info, 10); if (count < 0) { printf("Unable to get portinfo\n"); return 1; } if (count == 0) printf("No connected lines\n"); else { int i; struct rfcomm_port *port = info; printf("Connected lines:\n"); for (i=0; i < count; i++, port++) { printf("line: %d [/dev/bty%d], bda: %s, channel: %d, flags: %s %s\n", port->line, port->line, bda2str(&port->addr.bda), port->addr.port, (port->flags & RFCOMM_SOCK_BOUND) ? "bound" : "\b", (port->flags & RFCOMM_SOCK_CONNECTED) ? "connected" : "\b" ); } } return 0;}int cmd_pkttype(struct btctl_command *cmd){ int err, value, fd; char buf[128]; fd = hci_open(btdev); if (fd < 0) { printf("Unable to open device %s: %s\n", btdev, strerror(errno)); return -1; } if (argv[argind] == NULL) value = 0; //set default else { if (argv[argind][0] == '0') err = sscanf(argv[argind], "%x", &value); else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -