Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/next/external/cache/sources/hcitools/monitor/btsnoop.c
Views: 3959
/*1*2* BlueZ - Bluetooth protocol stack for Linux3*4* Copyright (C) 2011-2012 Intel Corporation5* Copyright (C) 2004-2010 Marcel Holtmann <[email protected]>6*7*8* This program is free software; you can redistribute it and/or modify9* it under the terms of the GNU General Public License as published by10* the Free Software Foundation; either version 2 of the License, or11* (at your option) any later version.12*13* This program is distributed in the hope that it will be useful,14* but WITHOUT ANY WARRANTY; without even the implied warranty of15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16* GNU General Public License for more details.17*18* You should have received a copy of the GNU General Public License19* along with this program; if not, write to the Free Software20* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA21*22*/2324#ifdef HAVE_CONFIG_H25#include <config.h>26#endif2728#include <stdio.h>29#include <errno.h>30#include <fcntl.h>31#include <unistd.h>32#include <stdint.h>33#include <string.h>34#include <sys/stat.h>35#include <arpa/inet.h>3637#include <bluetooth/bluetooth.h>38#include <bluetooth/hci.h>3940#include "btsnoop.h"4142struct btsnoop_hdr {43uint8_t id[8]; /* Identification Pattern */44uint32_t version; /* Version Number = 1 */45uint32_t type; /* Datalink Type */46} __attribute__ ((packed));47#define BTSNOOP_HDR_SIZE (sizeof(struct btsnoop_hdr))4849struct btsnoop_pkt {50uint32_t size; /* Original Length */51uint32_t len; /* Included Length */52uint32_t flags; /* Packet Flags */53uint32_t drops; /* Cumulative Drops */54uint64_t ts; /* Timestamp microseconds */55uint8_t data[0]; /* Packet Data */56} __attribute__ ((packed));57#define BTSNOOP_PKT_SIZE (sizeof(struct btsnoop_pkt))5859static const uint8_t btsnoop_id[] = { 0x62, 0x74, 0x73, 0x6e,600x6f, 0x6f, 0x70, 0x00 };6162static const uint32_t btsnoop_version = 1;63static uint32_t btsnoop_type = 0;6465static int btsnoop_fd = -1;66static uint16_t btsnoop_index = 0xffff;6768void btsnoop_create(const char *path, uint32_t type)69{70struct btsnoop_hdr hdr;71ssize_t written;7273if (btsnoop_fd >= 0)74return;7576btsnoop_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,77S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);78if (btsnoop_fd < 0)79return;8081btsnoop_type = type;8283memcpy(hdr.id, btsnoop_id, sizeof(btsnoop_id));84hdr.version = htonl(btsnoop_version);85hdr.type = htonl(btsnoop_type);8687written = write(btsnoop_fd, &hdr, BTSNOOP_HDR_SIZE);88if (written < 0) {89close(btsnoop_fd);90btsnoop_fd = -1;91return;92}93}9495void btsnoop_write(struct timeval *tv, uint32_t flags,96const void *data, uint16_t size)97{98struct btsnoop_pkt pkt;99uint64_t ts;100ssize_t written;101102ts = (tv->tv_sec - 946684800ll) * 1000000ll + tv->tv_usec;103104pkt.size = htonl(size);105pkt.len = htonl(size);106pkt.flags = htonl(flags);107pkt.drops = htonl(0);108pkt.ts = hton64(ts + 0x00E03AB44A676000ll);109110written = write(btsnoop_fd, &pkt, BTSNOOP_PKT_SIZE);111if (written < 0)112return;113114if (data && size > 0) {115written = write(btsnoop_fd, data, size);116if (written < 0)117return;118}119}120121static uint32_t get_flags_from_opcode(uint16_t opcode)122{123switch (opcode) {124case BTSNOOP_OPCODE_NEW_INDEX:125case BTSNOOP_OPCODE_DEL_INDEX:126break;127case BTSNOOP_OPCODE_COMMAND_PKT:128return 0x02;129case BTSNOOP_OPCODE_EVENT_PKT:130return 0x03;131case BTSNOOP_OPCODE_ACL_TX_PKT:132return 0x00;133case BTSNOOP_OPCODE_ACL_RX_PKT:134return 0x01;135case BTSNOOP_OPCODE_SCO_TX_PKT:136case BTSNOOP_OPCODE_SCO_RX_PKT:137break;138}139140return 0xff;141}142143void btsnoop_write_hci(struct timeval *tv, uint16_t index, uint16_t opcode,144const void *data, uint16_t size)145{146uint32_t flags;147148if (!tv)149return;150151if (btsnoop_fd < 0)152return;153154switch (btsnoop_type) {155case BTSNOOP_TYPE_HCI:156if (btsnoop_index == 0xffff)157btsnoop_index = index;158159if (index != btsnoop_index)160return;161162flags = get_flags_from_opcode(opcode);163if (flags == 0xff)164return;165break;166167case BTSNOOP_TYPE_EXTENDED_HCI:168flags = (index << 16) | opcode;169break;170171default:172return;173}174175btsnoop_write(tv, flags, data, size);176}177178void btsnoop_write_phy(struct timeval *tv, uint16_t frequency,179const void *data, uint16_t size)180{181uint32_t flags;182183if (!tv)184return;185186if (btsnoop_fd < 0)187return;188189switch (btsnoop_type) {190case BTSNOOP_TYPE_EXTENDED_PHY:191flags = (1 << 16) | frequency;192break;193194default:195return;196}197198btsnoop_write(tv, flags, data, size);199}200201int btsnoop_open(const char *path, uint32_t *type)202{203struct btsnoop_hdr hdr;204ssize_t len;205206if (btsnoop_fd >= 0) {207fprintf(stderr, "Too many open files\n");208return -1;209}210211btsnoop_fd = open(path, O_RDONLY | O_CLOEXEC);212if (btsnoop_fd < 0) {213perror("Failed to open file");214return -1;215}216217len = read(btsnoop_fd, &hdr, BTSNOOP_HDR_SIZE);218if (len < 0 || len != BTSNOOP_HDR_SIZE) {219perror("Failed to read header");220close(btsnoop_fd);221btsnoop_fd = -1;222return -1;223}224225if (memcmp(hdr.id, btsnoop_id, sizeof(btsnoop_id))) {226fprintf(stderr, "Invalid btsnoop header\n");227close(btsnoop_fd);228btsnoop_fd = -1;229return -1;230}231232if (ntohl(hdr.version) != btsnoop_version) {233fprintf(stderr, "Invalid btsnoop version\n");234close(btsnoop_fd);235btsnoop_fd = -1;236return -1;237}238239btsnoop_type = ntohl(hdr.type);240241if (type)242*type = btsnoop_type;243244return 0;245}246247static uint16_t get_opcode_from_flags(uint8_t type, uint32_t flags)248{249switch (type) {250case HCI_COMMAND_PKT:251return BTSNOOP_OPCODE_COMMAND_PKT;252case HCI_ACLDATA_PKT:253if (flags & 0x01)254return BTSNOOP_OPCODE_ACL_RX_PKT;255else256return BTSNOOP_OPCODE_ACL_TX_PKT;257case HCI_SCODATA_PKT:258if (flags & 0x01)259return BTSNOOP_OPCODE_SCO_RX_PKT;260else261return BTSNOOP_OPCODE_SCO_TX_PKT;262case HCI_EVENT_PKT:263return BTSNOOP_OPCODE_EVENT_PKT;264case 0xff:265if (flags & 0x02) {266if (flags & 0x01)267return BTSNOOP_OPCODE_EVENT_PKT;268else269return BTSNOOP_OPCODE_COMMAND_PKT;270} else {271if (flags & 0x01)272return BTSNOOP_OPCODE_ACL_RX_PKT;273else274return BTSNOOP_OPCODE_ACL_TX_PKT;275}276break;277}278279return 0xff;280}281282int btsnoop_read_hci(struct timeval *tv, uint16_t *index, uint16_t *opcode,283void *data, uint16_t *size)284{285struct btsnoop_pkt pkt;286uint32_t toread, flags;287uint64_t ts;288uint8_t pkt_type;289ssize_t len;290291if (btsnoop_fd < 0)292return -1;293294len = read(btsnoop_fd, &pkt, BTSNOOP_PKT_SIZE);295if (len == 0)296return -1;297298if (len < 0 || len != BTSNOOP_PKT_SIZE) {299perror("Failed to read packet");300close(btsnoop_fd);301btsnoop_fd = -1;302return -1;303}304305toread = ntohl(pkt.size);306flags = ntohl(pkt.flags);307308ts = ntoh64(pkt.ts) - 0x00E03AB44A676000ll;309tv->tv_sec = (ts / 1000000ll) + 946684800ll;310tv->tv_usec = ts % 1000000ll;311312switch (btsnoop_type) {313case BTSNOOP_TYPE_HCI:314*index = 0;315*opcode = get_opcode_from_flags(0xff, flags);316break;317318case BTSNOOP_TYPE_UART:319len = read(btsnoop_fd, &pkt_type, 1);320if (len < 0) {321perror("Failed to read packet type");322close(btsnoop_fd);323btsnoop_fd = -1;324return -1;325}326toread--;327328*index = 0;329*opcode = get_opcode_from_flags(pkt_type, flags);330break;331332case BTSNOOP_TYPE_EXTENDED_HCI:333*index = flags >> 16;334*opcode = flags & 0xffff;335break;336337default:338fprintf(stderr, "Unknown packet type\n");339close(btsnoop_fd);340btsnoop_fd = -1;341return -1;342}343344len = read(btsnoop_fd, data, toread);345if (len < 0) {346perror("Failed to read data");347close(btsnoop_fd);348btsnoop_fd = -1;349return -1;350}351352*size = toread;353354return 0;355}356357int btsnoop_read_phy(struct timeval *tv, uint16_t *frequency,358void *data, uint16_t *size)359{360struct btsnoop_pkt pkt;361uint32_t toread, flags;362uint64_t ts;363ssize_t len;364365if (btsnoop_fd < 0)366return -1;367368len = read(btsnoop_fd, &pkt, BTSNOOP_PKT_SIZE);369if (len == 0)370return -1;371372if (len < 0 || len != BTSNOOP_PKT_SIZE) {373perror("Failed to read packet");374close(btsnoop_fd);375btsnoop_fd = -1;376return -1;377}378379toread = ntohl(pkt.size);380flags = ntohl(pkt.flags);381382ts = ntoh64(pkt.ts) - 0x00E03AB44A676000ll;383tv->tv_sec = (ts / 1000000ll) + 946684800ll;384tv->tv_usec = ts % 1000000ll;385386switch (btsnoop_type) {387case BTSNOOP_TYPE_EXTENDED_PHY:388if ((flags >> 16) != 1)389break;390*frequency = flags & 0xffff;391break;392393default:394fprintf(stderr, "Unknown packet type\n");395close(btsnoop_fd);396btsnoop_fd = -1;397return -1;398}399400len = read(btsnoop_fd, data, toread);401if (len < 0) {402perror("Failed to read data");403close(btsnoop_fd);404btsnoop_fd = -1;405return -1;406}407408*size = toread;409410return 0;411}412413void btsnoop_close(void)414{415if (btsnoop_fd < 0)416return;417418close(btsnoop_fd);419btsnoop_fd = -1;420421btsnoop_index = 0xffff;422}423424425