<commit_before>/*
 *
 *  Bluetooth packet analyzer - CSR parser
 *
 *  Copyright (C) 2004-2005  <NAME> <<EMAIL>>
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *  $Id$
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include <sys/types.h>
#include <netinet/in.h>

#include "parser.h"

#define CSR_U8(frm)  (get_u8(frm))
#define CSR_U16(frm) (btohs(htons(get_u16(frm))))
#define CSR_U32(frm) ((CSR_U16(frm) << 16) + CSR_U16(frm))
#define CSR_S16(frm) (btohs(htons(get_u16(frm))))

static char *type2str(uint16_t type)
{
	switch (type) {
	case 0x0000:
		return "Get req";
	case 0x0001:
		return "Get rsp";
	case 0x0002:
		return "Set req";
	default:
		return "Reserved";
	}
}

static inline void valueless_dump(int level, char *str, struct frame *frm)
{
	p_indent(level, frm);
	printf("%s\n", str);
}

static inline void complex_dump(int level, char *str, struct frame *frm)
{
	p_indent(level, frm);
	printf("%s\n", str);

	raw_dump(level, frm);
}

static inline void int8_dump(int level, char *str, struct frame *frm)
{
	int16_t value;

	value = CSR_S16(frm);

	p_indent(level, frm);
	printf("%s: value %d (0x%2.2x)\n", str, value, value);
}

static inline void uint16_dump(int level, char *str, struct frame *frm)
{
	uint16_t value;

	value = CSR_U16(frm);

	p_indent(level, frm);
	printf("%s: value %d (0x%4.4x)\n", str, value, value);
}

static inline void uint32_dump(int level, char *str, struct frame *frm)
{
	uint32_t value;

	value = CSR_U32(frm);

	p_indent(level, frm);
	printf("%s: value %d (0x%4.4x)\n", str, value, value);
}

static inline void bdaddr_dump(int level, char *str, struct frame *frm)
{
	char addr[18];

	ba2str(frm->ptr, addr);

	p_indent(level, frm);
	printf("%s: bdaddr %s\n", str, addr);
}

static inline void features_dump(int level, char *str, struct frame *frm)
{
	unsigned char features[8];
	int i;

	memcpy(features, frm->ptr, 8);

	p_indent(level, frm);
	printf("%s: features", str);
	for (i = 0; i < 8; i++)
		printf(" 0x%02x", features[i]);
	printf("\n");
}

static inline void handle_length_dump(int level, char *str, struct frame *frm)
{
	uint16_t handle, length;

	handle = CSR_U16(frm);
	length = CSR_U16(frm);

	p_indent(level, frm);
	printf("%s: handle %d length %d\n", str, handle, length);
}

static inline void handle_clock_dump(int level, char *str, struct frame *frm)
{
	uint16_t handle;
	uint32_t clock;

	handle = CSR_U16(frm);
	clock  = CSR_U32(frm);

	p_indent(level, frm);
	printf("%s: handle %d clock 0x%4.4x\n", str, handle, clock);
}

static inline void pskey_dump(int level, struct frame *frm)
{
	uint16_t key, length, stores;

	key    = CSR_U16(frm);
	length = CSR_U16(frm);
	stores = CSR_U16(frm);

	p_indent(level, frm);
	printf("PSKEY: key 0x%4.4x len %d stores %d\n", key, length, stores);

	switch (key) {
	case 0x0001:
		bdaddr_dump(level + 1, "BDADDR", frm);
		break;
	case 0x00da:
		uint16_dump(level + 1, "ENC_KEY_LMIN", frm);
		break;
	case 0x00db:
		uint16_dump(level + 1, "ENC_KEY_LMAX", frm);
		break;
	case 0x00ef:
		features_dump(level + 1, "LOCAL_SUPPORTED_FEATURES", frm);
		break;
	case 0x010d:
		uint16_dump(level + 1, "HCI_LMP_LOCAL_VERSION", frm);
		break;
	case 0x010e:
		uint16_dump(level + 1, "LMP_REMOTE_VERSION", frm);
		break;
	case 0x01ab:
		uint16_dump(level + 1, "HOSTIO_MAP_SCO_PCM", frm);
		break;
	case 0x01be:
		uint16_dump(level + 1, "UART_BAUDRATE", frm);
		break;
	case 0x01f6
		uint16_dump(level + 1, "ANA_FTRIM", frm);
		break;
	case 0x01f9:<commit_msg>hcidump: Typo<commit_after>/*
 *
 *  Bluetooth packet analyzer - CSR parser
 *
 *  Copyright (C) 2004-2005  <NAME> <<EMAIL>>
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *  $Id$
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include <sys/types.h>
#include <netinet/in.h>

#include "parser.h"

#define CSR_U8(frm)  (get_u8(frm))
#define CSR_U16(frm) (btohs(htons(get_u16(frm))))
#define CSR_U32(frm) ((CSR_U16(frm) << 16) + CSR_U16(frm))
#define CSR_S16(frm) (btohs(htons(get_u16(frm))))

static char *type2str(uint16_t type)
{
	switch (type) {
	case 0x0000:
		return "Get req";
	case 0x0001:
		return "Get rsp";
	case 0x0002:
		return "Set req";
	default:
		return "Reserved";
	}
}

static inline void valueless_dump(int level, char *str, struct frame *frm)
{
	p_indent(level, frm);
	printf("%s\n", str);
}

static inline void complex_dump(int level, char *str, struct frame *frm)
{
	p_indent(level, frm);
	printf("%s\n", str);

	raw_dump(level, frm);
}

static inline void int8_dump(int level, char *str, struct frame *frm)
{
	int16_t value;

	value = CSR_S16(frm);

	p_indent(level, frm);
	printf("%s: value %d (0x%2.2x)\n", str, value, value);
}

static inline void uint16_dump(int level, char *str, struct frame *frm)
{
	uint16_t value;

	value = CSR_U16(frm);

	p_indent(level, frm);
	printf("%s: value %d (0x%4.4x)\n", str, value, value);
}

static inline void uint32_dump(int level, char *str, struct frame *frm)
{
	uint32_t value;

	value = CSR_U32(frm);

	p_indent(level, frm);
	printf("%s: value %d (0x%4.4x)\n", str, value, value);
}

static inline void bdaddr_dump(int level, char *str, struct frame *frm)
{
	char addr[18];

	ba2str(frm->ptr, addr);

	p_indent(level, frm);
	printf("%s: bdaddr %s\n", str, addr);
}

static inline void features_dump(int level, char *str, struct frame *frm)
{
	unsigned char features[8];
	int i;

	memcpy(features, frm->ptr, 8);

	p_indent(level, frm);
	printf("%s: features", str);
	for (i = 0; i < 8; i++)
		printf(" 0x%02x", features[i]);
	printf("\n");
}

static inline void handle_length_dump(int level, char *str, struct frame *frm)
{
	uint16_t handle, length;

	handle = CSR_U16(frm);
	length = CSR_U16(frm);

	p_indent(level, frm);
	printf("%s: handle %d length %d\n", str, handle, length);
}

static inline void handle_clock_dump(int level, char *str, struct frame *frm)
{
	uint16_t handle;
	uint32_t clock;

	handle = CSR_U16(frm);
	clock  = CSR_U32(frm);

	p_indent(level, frm);
	printf("%s: handle %d clock 0x%4.4x\n", str, handle, clock);
}

static inline void pskey_dump(int level, struct frame *frm)
{
	uint16_t key, length, stores;

	key    = CSR_U16(frm);
	length = CSR_U16(frm);
	stores = CSR_U16(frm);

	p_indent(level, frm);
	printf("PSKEY: key 0x%4.4x len %d stores %d\n", key, length, stores);

	switch (key) {
	case 0x0001:
		bdaddr_dump(level + 1, "BDADDR", frm);
		break;
	case 0x00da:
		uint16_dump(level + 1, "ENC_KEY_LMIN", frm);
		break;
	case 0x00db:
		uint16_dump(level + 1, "ENC_KEY_LMAX", frm);
		break;
	case 0x00ef:
		features_dump(level + 1, "LOCAL_SUPPORTED_FEATURES", frm);
		break;
	case 0x010d:
		uint16_dump(level + 1, "HCI_LMP_LOCAL_VERSION", frm);
		break;
	case 0x010e:
		uint16_dump(level + 1, "LMP_REMOTE_VERSION", frm);
		break;
	case 0x01ab:
		uint16_dump(level + 1, "HOSTIO_MAP_SCO_PCM", frm);
		break;
	case 0x01be:
		uint16_dump(level + 1, "UART_BAUDRATE", frm);
		break;
	case 0x01f6:
		uint16_dump(level + 1, "ANA_FTRIM", frm);
		break;
	case 0x01f9:
{'fraction_non_alphanumeric': 0.14178482068390325, 'fraction_numerical': 0.043369474562135114, 'mean_word_length': 4.387676508344031, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5587607', 'n_tokens_mistral': 3703, 'n_tokens_neox': 3396, 'n_words': 968}