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