📄 btutils.c
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <ctype.h>#include <dirent.h>#include <fcntl.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <netdb.h>#include <bluetooth/bluetooth.h>#include <bluetooth/hci.h>#include <bluetooth/hci_lib.h>#include <bluetooth/rfcomm.h>#include <errno.h>#include "btdefines.h"#include "nonbluez.h"#include "btutils.h"#ifdef ENABLE_DEBUG_LOGchar* state2str(int state){ switch (state) { case BT_STATE_UNINITIALIZED: return "BT_STATE_UNINITIALIZED"; case BT_STATE_INITIALIZED: return "BT_STATE_INITIALIZED"; case BT_STATE_START_LISTENING: return "BT_STATE_START_LISTENING"; case BT_STATE_LISTENING: return "BT_STATE_LISTENING"; case BT_STATE_START_CONNECTING: return "BT_STATE_START_CONNECTING"; case BT_STATE_REMOVE_CONNECTION: return "BT_STATE_REMOVE_CONNCETION"; case BT_STATE_CONNECTING: return "BT_STATE_CONNECTING"; case BT_STATE_CONNECTED: return "BT_STATE_CONNECTED"; case BT_STATE_APP_START: return "BT_STATE_APP_START"; case BT_STATE_APP_REMOVE_CONNECTION: return "BT_STATE_APP_REMOVE_CONNECTION"; case BT_STATE_APP_READY: return "BT_STATE_APP_READY"; case BT_STATE_APP_CLOSING: return "BT_STATE_APP_CLOSING"; case BT_STATE_CLOSING: return "BT_STATE_CLOSING"; case BT_PPP_STATE_UNINITIALIZED: return "BT_PPP_STATE_UNINITIALIZED"; case BT_PPP_STATE_INIT: return "BT_PPP_STATE_INIT"; case BT_PPP_STATE_REMOVE: return "BT_PPP_STATE_REMOVE"; case BT_PPP_STATE_CREATETTY: return "BT_PPP_STATE_CREATETTY"; case BT_PPP_STATE_CHECKTTY: return "BT_PPP_STATE_CHECKTTY"; case BT_PPP_STATE_START: return "BT_PPP_STATE_START"; case BT_PPP_STATE_CHECKREADY: return "BT_PPP_STATE_CHECKREADY"; case BT_PPP_STATE_CLOSING: return "BT_PPP_STATE_CLOSING"; default: return "!UNKNOWN!"; }}char* error2str(int error){ switch (error) { case BT_ERR_NONE: return "BT_ERR_NONE"; case BT_ERR_FAILED: return "BT_ERR_FAILED"; case BT_ERR_TIMEOUT: return "BT_ERR_TIMEOUT"; case BT_ERR_ARGUMENT: return "BT_ERR_ARGUMENT"; case BT_ERR_NOMEMORY: return "BT_ERR_NOMEMORY"; case BT_ERR_STATE: return "BT_ERR_STATE"; case BT_ERR_INUSE: return "BT_ERR_INUSE"; case BT_ERR_CONNECTION: return "BT_ERR_CONNECTION"; default: return "!UNKNOWN!"; }}#endif //ENABLE_DEBUG_LOGint BTSendSig(char* procname, int sig){ struct dirent *de; char path[PATH_MAX + 1]; char name[PATH_MAX + 1]; DIR *dir; int pid = -1; dir = opendir(BTU_PROCBASE); if (!dir) { DEBUG_LOG1("sendsig(): cannot locate %s\n", BTU_PROCBASE); return -1; } while ((de = readdir(dir)) != NULL) { if (isdigit(de->d_name[0])) { sprintf(path, "%s/%s/exe", BTU_PROCBASE, de->d_name); memset(name, 0, PATH_MAX + 1); readlink(path, name, PATH_MAX); if ((strlen(name) == strlen(procname) && !strcmp(name, procname)) || (strlen(name) > strlen(procname) && name[strlen(name) - strlen(procname) - 1] == '/' && !strcmp(&name[strlen(name) - strlen(procname)], procname)) ) pid = atoi(de->d_name); } } closedir(dir); if (sig > 0) { if (pid > 0) { DEBUG_LOG3("sending %d to %s(%d)", sig, procname, pid); kill(pid, sig); } else { DEBUG_LOG("cannot locate pid"); } } else { DEBUG_LOG2("located %s at pid %d", procname, pid); } return pid;}void BTSetTimer(BTTimer timer, unsigned int timeout){ struct timeval tvadd; gettimeofday(&timer->start, NULL); if (timeout < 1000000) { tvadd.tv_sec = 0; tvadd.tv_usec = timeout; } else { tvadd.tv_sec = timeout / 1000000; tvadd.tv_usec = timeout % 1000000; } timeradd(&timer->start, &tvadd, &timer->stop); timer->timeout = timeout;}int BTTimerExpired(BTTimer timer){ struct timeval now; gettimeofday(&now, NULL); if (timercmp(&now, &timer->start, <= )) { DEBUG_LOG("adjusting a timer"); BTSetTimer(timer, now.tv_sec*1000000 + now.tv_usec + timer->timeout / 2); } return timercmp(&timer->stop, &now, <= );}void BTEnablePageScan(){ int s; struct THCIDeviceRequirements dr; if ((s = socket(AF_BLUETOOTH, SOCK_RAW, BluetoothProtocolHCI)) != -1) { dr.iDeviceID = 0; dr.iDeviceOptions = HciDeviceScanPage | HciDeviceScanInquiry; if (fcntl(s, F_SETFL, O_NONBLOCK) != -1) { if (ioctl(s, HCISETSCAN, (unsigned long)&dr) != -1) { DEBUG_LOG("page scan enabled"); } else { DEBUG_LOG("enabling page scan failed"); } } else { DEBUG_LOG("enabling page scan fcntl failed"); } close(s); } else { DEBUG_LOG("enabling page scan socket failed"); }}void BTDisablePageScan(){ int s; struct THCIDeviceRequirements dr; if ((s = socket(AF_BLUETOOTH, SOCK_RAW, BluetoothProtocolHCI)) != -1) { if (fcntl(s, F_SETFL, O_NONBLOCK) != -1) { dr.iDeviceID = 0; dr.iDeviceOptions = HciDeviceDisableScan; if ((ioctl(s, HCISETSCAN, (unsigned long)&dr)) != -1) { DEBUG_LOG("page scan disabled"); } else { DEBUG_LOG("disabling page scan ioctl failed"); } } else { DEBUG_LOG("disabling page scan fcntl failed"); } close(s); } else { DEBUG_LOG("disabling page scan socket failed"); }}int BTGetActiveAclDev(char *bdaddrstr){ struct hci_dev_list_req *dl = NULL; struct hci_dev_req *dr; struct hci_conn_list_req *cl = NULL; struct hci_conn_info *ci; bdaddr_t bdaddr; int ii, i, sk; int result = -1; if ((sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) return -1; if (!(dl = (struct hci_dev_list_req*)malloc(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl)))) goto done; if (!(cl = (struct hci_conn_list_req*)malloc(10 * sizeof(*ci) + sizeof(*cl)))) goto done; dl->dev_num = HCI_MAX_DEV; dr = dl->dev_req; if (ioctl(sk, HCIGETDEVLIST, (void *) dl)) { DEBUG_LOG("can't get device list"); goto done; } for (i = 0; i < dl->dev_num; i++, dr++) { if (hci_test_bit(HCI_UP, &dr->dev_opt)) { cl->dev_id = dr->dev_id; cl->conn_num = 10; ci = cl->conn_info; if (ioctl(sk, HCIGETCONNLIST, (void *) cl)) { DEBUG_LOG("can't get connection list"); goto done; } for (ii = 0; ii < cl->conn_num; ii++, ci++) { str2ba(bdaddrstr, &bdaddr); if ( !bacmp(&ci->bdaddr, &bdaddr) && ci->type == ACL_LINK) { result = dr->dev_id; goto done; } } } }done: close(sk); if (cl) free(cl); if (dl) free(dl); return result;}int BTIsAclLinkActive(char *bdaddrstr){ if (BTGetActiveAclDev(bdaddrstr) < 0) return BT_FALSE; else return BT_TRUE;}void BTDestroyAclLink(char *bdaddrstr){ struct hci_conn_info_req *cr; bdaddr_t bdaddr; int dd, dev_id = BTGetActiveAclDev(bdaddrstr); if (dev_id < 0) return; dd = hci_open_dev(dev_id); if (dd < 0) { DEBUG_LOG("HCI device open failed"); return; } if (!(cr = (struct hci_conn_info_req*)malloc(sizeof(*cr) + sizeof(struct hci_conn_info)))) { DEBUG_LOG("cannot allocate memory"); return; } str2ba(bdaddrstr, &bdaddr); bacpy(&cr->bdaddr, &bdaddr); cr->type = ACL_LINK; if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) { DEBUG_LOG("get connection info failed"); return; } if (hci_disconnect(dd, htobs(cr->conn_info->handle), HCI_OE_USER_ENDED_CONNECTION, 10000) < 0) DEBUG_LOG("disconnect failed"); close(dd); free(cr); return;}int BTGetFileInt(const char* name){ int ret; char fname[128]; snprintf(fname, sizeof fname, "%s", name); FILE *fp = fopen(fname, "r"); if (fp != NULL) { if (fscanf(fp, "%d", &ret) != 1) { DEBUG_LOG2("%s: Unable to read value from %s", __func__, fname); ret = -1; } fclose(fp); } else { DEBUG_LOG3("%s: Unable to open %s: %s", __func__, fname, strerror(errno)); ret = -1; } return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -