| 1 |
/* |
| 2 |
* BlueALSA - hcitop.c |
| 3 |
* Copyright (c) 2016-2023 Arkadiusz Bokowy |
| 4 |
* |
| 5 |
* This file is a part of bluez-alsa. |
| 6 |
* |
| 7 |
* This project is licensed under the terms of the MIT license. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
#if HAVE_CONFIG_H |
| 12 |
# include <config.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#include <getopt.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#include <curses.h> |
| 21 |
#include <bsd/stdlib.h> |
| 22 |
|
| 23 |
#include <bluetooth/bluetooth.h> |
| 24 |
#include <bluetooth/hci.h> |
| 25 |
#include <bluetooth/hci_lib.h> |
| 26 |
|
| 27 |
static const struct { |
| 28 |
unsigned int bit; |
| 29 |
char flag; |
| 30 |
} hci_flags_map[] = { |
| 31 |
{ HCI_UP, 'U' }, |
| 32 |
{ HCI_INIT, 'N' }, |
| 33 |
{ HCI_RUNNING, 'R' }, |
| 34 |
{ HCI_PSCAN, 'P' }, |
| 35 |
{ HCI_ISCAN, 'I' }, |
| 36 |
{ HCI_AUTH, 'A' }, |
| 37 |
{ HCI_ENCRYPT, 'E' }, |
| 38 |
{ HCI_INQUIRY, 'Q' }, |
| 39 |
{ HCI_RAW, 'X' }, |
| 40 |
}; |
| 41 |
|
| 42 |
static size_t get_devinfo(struct hci_dev_info di[HCI_MAX_DEV]) { |
| 43 |
size_t num = 0; |
| 44 |
for (size_t i = 0; i < HCI_MAX_DEV; i++) |
| 45 |
if (hci_devinfo(i, &di[num]) == 0) |
| 46 |
num++; |
| 47 |
return num; |
| 48 |
} |
| 49 |
|
| 50 |
static unsigned int get_average_rate(unsigned int *array, size_t size) { |
| 51 |
|
| 52 |
/* at least two points are required */ |
| 53 |
if (size < 2) |
| 54 |
return 0; |
| 55 |
|
| 56 |
unsigned int x = 0; |
| 57 |
unsigned int y = 0; |
| 58 |
size_t i; |
| 59 |
|
| 60 |
size--; |
| 61 |
i = size; |
| 62 |
|
| 63 |
while (i--) { |
| 64 |
int b = (array[i] - array[i + 1]) % size; |
| 65 |
x += (array[i] - array[i + 1]) / size; |
| 66 |
if (y >= size - b) { |
| 67 |
y -= size - b; |
| 68 |
x++; |
| 69 |
} |
| 70 |
else { |
| 71 |
y += b; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return x + y / size; |
| 76 |
} |
| 77 |
|
| 78 |
static void sprint_hci_flags(char *str, unsigned int flags) { |
| 79 |
|
| 80 |
size_t i; |
| 81 |
|
| 82 |
for (i = 0; i < sizeof(hci_flags_map) / sizeof(*hci_flags_map); i++) |
| 83 |
str[i] = hci_test_bit(hci_flags_map[i].bit, &flags) ? hci_flags_map[i].flag : ' '; |
| 84 |
|
| 85 |
str[i] = '\0'; |
| 86 |
} |
| 87 |
|
| 88 |
int main(int argc, char *argv[]) { |
| 89 |
|
| 90 |
int opt; |
| 91 |
const char *opts = "hVd:"; |
| 92 |
const struct option longopts[] = { |
| 93 |
{ "help", no_argument, NULL, 'h' }, |
| 94 |
{ "version", no_argument, NULL, 'V' }, |
| 95 |
{ "delay", required_argument, NULL, 'd' }, |
| 96 |
{ 0, 0, 0, 0 }, |
| 97 |
}; |
| 98 |
|
| 99 |
int delay_sec = 1; |
| 100 |
int delay_msec = 0; |
| 101 |
|
| 102 |
while ((opt = getopt_long(argc, argv, opts, longopts, NULL)) != -1) |
| 103 |
switch (opt) { |
| 104 |
case 'h' /* --help */ : |
| 105 |
printf("usage: %s [ -d sec ]\n" |
| 106 |
" -h, --help\t\tprint this help and exit\n" |
| 107 |
" -V, --version\t\tprint version and exit\n" |
| 108 |
" -d, --delay=SEC\tdelay time interval\n", |
| 109 |
argv[0]); |
| 110 |
return EXIT_SUCCESS; |
| 111 |
|
| 112 |
case 'V' /* --version */ : |
| 113 |
printf("%s\n", PACKAGE_VERSION); |
| 114 |
return EXIT_SUCCESS; |
| 115 |
|
| 116 |
case 'd' /* --delay=SEC */ : |
| 117 |
delay_sec = atoi(optarg); |
| 118 |
delay_msec = (int)((atof(optarg) - delay_sec) * 10) * 100; |
| 119 |
if (delay_sec < 0 || delay_msec < 0 || (delay_sec == 0 && delay_msec == 0)) { |
| 120 |
fprintf(stderr, "%s: -d requires positive argument (max precision: 0.1)\n", argv[0]); |
| 121 |
return EXIT_FAILURE; |
| 122 |
} |
| 123 |
break; |
| 124 |
|
| 125 |
default: |
| 126 |
fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]); |
| 127 |
return EXIT_FAILURE; |
| 128 |
} |
| 129 |
|
| 130 |
struct hci_dev_info devices[HCI_MAX_DEV]; |
| 131 |
unsigned int byte_rx[HCI_MAX_DEV][3]; |
| 132 |
unsigned int byte_tx[HCI_MAX_DEV][3]; |
| 133 |
|
| 134 |
memset(byte_rx, 0, sizeof(byte_rx)); |
| 135 |
memset(byte_tx, 0, sizeof(byte_tx)); |
| 136 |
|
| 137 |
initscr(); |
| 138 |
cbreak(); |
| 139 |
noecho(); |
| 140 |
curs_set(0); |
| 141 |
|
| 142 |
for (size_t ii = 1;; ii++) { |
| 143 |
|
| 144 |
const char *template_top = "%-5s %4s %-17s %-9s %8s %8s %8s %8s"; |
| 145 |
const char *template_row = "%-5s %4s %17s %9s %8s %8s %8s %8s"; |
| 146 |
|
| 147 |
attron(A_REVERSE); |
| 148 |
mvprintw(0, 0, template_top, "HCI", "BUS", "ADDR", "FLAGS", "RX", "TX", "RX/s", "TX/s"); |
| 149 |
attroff(A_REVERSE); |
| 150 |
|
| 151 |
size_t count = get_devinfo(devices); |
| 152 |
for (size_t i = 0; i < HCI_MAX_DEV; i++) { |
| 153 |
|
| 154 |
/* shift historic data to the right by one sample */ |
| 155 |
memmove(&byte_rx[i][1], &byte_rx[i][0], sizeof(*byte_rx) - sizeof(**byte_rx)); |
| 156 |
memmove(&byte_tx[i][1], &byte_tx[i][0], sizeof(*byte_tx) - sizeof(**byte_tx)); |
| 157 |
|
| 158 |
if (i >= count) |
| 159 |
continue; |
| 160 |
|
| 161 |
char addr[18]; |
| 162 |
ba2str(&devices[i].bdaddr, addr); |
| 163 |
|
| 164 |
char *bus = hci_dtypetostr(devices[i].type); |
| 165 |
|
| 166 |
char flags[sizeof(hci_flags_map) / sizeof(*hci_flags_map) + 1]; |
| 167 |
sprint_hci_flags(flags, devices[i].flags); |
| 168 |
|
| 169 |
byte_rx[i][0] = devices[i].stat.byte_rx; |
| 170 |
byte_tx[i][0] = devices[i].stat.byte_tx; |
| 171 |
|
| 172 |
const size_t ii_max = sizeof(*byte_rx) / sizeof(**byte_rx); |
| 173 |
const size_t samples = ii < ii_max ? ii : ii_max; |
| 174 |
unsigned int rate_rx = get_average_rate(byte_rx[i], samples); |
| 175 |
unsigned int rate_tx = get_average_rate(byte_tx[i], samples); |
| 176 |
|
| 177 |
rate_rx = rate_rx * 10 / (delay_sec * 10 + delay_msec / 100); |
| 178 |
rate_tx = rate_tx * 10 / (delay_sec * 10 + delay_msec / 100); |
| 179 |
|
| 180 |
char rx[7], rx_rate[9]; |
| 181 |
char tx[7], tx_rate[9]; |
| 182 |
|
| 183 |
humanize_number(rx, sizeof(rx), byte_rx[i][0], "B", HN_AUTOSCALE, 0); |
| 184 |
humanize_number(tx, sizeof(tx), byte_tx[i][0], "B", HN_AUTOSCALE, 0); |
| 185 |
humanize_number(rx_rate, sizeof(rx_rate), rate_rx, "B", HN_AUTOSCALE, 0); |
| 186 |
humanize_number(tx_rate, sizeof(tx_rate), rate_tx, "B", HN_AUTOSCALE, 0); |
| 187 |
|
| 188 |
mvprintw(i + 1, 0, template_row, |
| 189 |
devices[i].name, bus, addr, flags, rx, tx, rx_rate, tx_rate); |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
timeout(delay_sec * 1000 + delay_msec); |
| 194 |
if (getch() == 'q') |
| 195 |
break; |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
endwin(); |
| 200 |
return EXIT_SUCCESS; |
| 201 |
} |