1 /*
2 * Copyright 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "bt_btif_sock"
18
19 #include "btif/include/btif_sock_logging.h"
20
21 #include <frameworks/proto_logging/stats/enums/bluetooth/enums.pb.h>
22 #include <time.h>
23
24 #include <atomic>
25
26 #include "btif/include/btif_metrics_logging.h"
27 #include "btif/include/btif_sock.h"
28 #include "os/log.h"
29 #include "types/raw_address.h"
30
31 #define SOCK_LOGGER_SIZE_MAX 16
32
33 using namespace bluetooth;
34
35 struct SockConnectionEvent {
36 bool used;
37 RawAddress addr;
38 int state;
39 int role;
40 int channel;
41 int type;
42 char server_name[64];
43 struct timespec timestamp;
44
45 void dump(const int fd);
46 };
47
48 static std::atomic<uint8_t> logger_index;
49
50 static SockConnectionEvent connection_logger[SOCK_LOGGER_SIZE_MAX];
51
52 static android::bluetooth::SocketConnectionstateEnum toConnectionStateEnum(
53 int state);
54 static android::bluetooth::SocketRoleEnum toSocketRoleEnum(int role);
55
btif_sock_connection_logger(const RawAddress & address,int port,int type,int state,int role,int uid,int server_port,int64_t tx_bytes,int64_t rx_bytes,const char * server_name)56 void btif_sock_connection_logger(const RawAddress& address, int port, int type,
57 int state, int role, int uid, int server_port,
58 int64_t tx_bytes, int64_t rx_bytes,
59 const char* server_name) {
60 uint8_t index = logger_index++ % SOCK_LOGGER_SIZE_MAX;
61
62 connection_logger[index] = {
63 .used = true,
64 .addr = address,
65 .state = state,
66 .role = role,
67 .channel = server_port,
68 .type = type,
69 .server_name = {'\0'},
70 };
71
72 if (server_name != nullptr) {
73 strncpy(connection_logger[index].server_name, server_name,
74 sizeof(connection_logger[index].server_name) - 1);
75 }
76
77 clock_gettime(CLOCK_REALTIME, &connection_logger[index].timestamp);
78 log_socket_connection_state(address, port, type, toConnectionStateEnum(state),
79 tx_bytes, rx_bytes, uid, server_port,
80 toSocketRoleEnum(role));
81 }
82
btif_sock_dump(int fd)83 void btif_sock_dump(int fd) {
84 dprintf(fd, "\nSocket Events: \n");
85 dprintf(fd,
86 " Time \tAddress \tState \tRole"
87 " \tChannel \tType \tServerName\n");
88
89 const uint8_t head = logger_index.load() % SOCK_LOGGER_SIZE_MAX;
90
91 uint8_t index = head;
92 do {
93 connection_logger[index].dump(fd);
94
95 index++;
96 index %= SOCK_LOGGER_SIZE_MAX;
97 } while (index != head);
98 dprintf(fd, "\n");
99 }
100
dump(const int fd)101 void SockConnectionEvent::dump(const int fd) {
102 if (!used) {
103 return;
104 }
105
106 char eventtime[20];
107 char temptime[20];
108 struct tm* tstamp = localtime(×tamp.tv_sec);
109 strftime(temptime, sizeof(temptime), "%H:%M:%S", tstamp);
110 snprintf(eventtime, sizeof(eventtime), "%s.%03ld", temptime,
111 timestamp.tv_nsec / 1000000);
112
113 const char* str_state;
114 switch (state) {
115 case SOCKET_CONNECTION_STATE_LISTENING:
116 str_state = "STATE_LISTENING";
117 break;
118 case SOCKET_CONNECTION_STATE_CONNECTING:
119 str_state = "STATE_CONNECTING";
120 break;
121 case SOCKET_CONNECTION_STATE_CONNECTED:
122 str_state = "STATE_CONNECTED";
123 break;
124 case SOCKET_CONNECTION_STATE_DISCONNECTING:
125 str_state = "STATE_DISCONNECTING";
126 break;
127 case SOCKET_CONNECTION_STATE_DISCONNECTED:
128 str_state = "STATE_DISCONNECTED";
129 break;
130 default:
131 str_state = "STATE_UNKNOWN";
132 break;
133 }
134
135 const char* str_role;
136 switch (role) {
137 case SOCKET_ROLE_LISTEN:
138 str_role = "ROLE_LISTEN";
139 break;
140 case SOCKET_ROLE_CONNECTION:
141 str_role = "ROLE_CONNECTION";
142 break;
143 default:
144 str_role = "ROLE_UNKNOWN";
145 break;
146 }
147
148 const char* str_type;
149 switch (type) {
150 case BTSOCK_RFCOMM:
151 str_type = "RFCOMM";
152 break;
153 case BTSOCK_L2CAP:
154 str_type = "L2CAP";
155 break;
156 case BTSOCK_L2CAP_LE:
157 str_type = "L2CAP_LE";
158 break;
159 case BTSOCK_SCO:
160 str_type = "SCO";
161 break;
162 default:
163 str_type = "UNKNOWN";
164 break;
165 }
166
167 dprintf(fd, " %s\t%s\t%s \t%s \t%d \t%s\t%s\n", eventtime,
168 ADDRESS_TO_LOGGABLE_CSTR(addr), str_state, str_role, channel,
169 str_type, server_name);
170 }
171
toConnectionStateEnum(int state)172 static android::bluetooth::SocketConnectionstateEnum toConnectionStateEnum(
173 int state) {
174 switch (state) {
175 case SOCKET_CONNECTION_STATE_LISTENING:
176 return android::bluetooth::SocketConnectionstateEnum::
177 SOCKET_CONNECTION_STATE_LISTENING;
178 break;
179 case SOCKET_CONNECTION_STATE_CONNECTING:
180 return android::bluetooth::SocketConnectionstateEnum::
181 SOCKET_CONNECTION_STATE_CONNECTING;
182 case SOCKET_CONNECTION_STATE_CONNECTED:
183 return android::bluetooth::SocketConnectionstateEnum::
184 SOCKET_CONNECTION_STATE_CONNECTED;
185 case SOCKET_CONNECTION_STATE_DISCONNECTING:
186 return android::bluetooth::SocketConnectionstateEnum::
187 SOCKET_CONNECTION_STATE_DISCONNECTING;
188 case SOCKET_CONNECTION_STATE_DISCONNECTED:
189 return android::bluetooth::SocketConnectionstateEnum::
190 SOCKET_CONNECTION_STATE_DISCONNECTED;
191 }
192 return android::bluetooth::SocketConnectionstateEnum::
193 SOCKET_CONNECTION_STATE_UNKNOWN;
194 }
195
toSocketRoleEnum(int role)196 static android::bluetooth::SocketRoleEnum toSocketRoleEnum(int role) {
197 switch (role) {
198 case SOCKET_ROLE_LISTEN:
199 return android::bluetooth::SOCKET_ROLE_LISTEN;
200 case SOCKET_ROLE_CONNECTION:
201 return android::bluetooth::SOCKET_ROLE_CONNECTION;
202 }
203 return android::bluetooth::SOCKET_ROLE_UNKNOWN;
204 }