Loading...
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 | /** @file
* @brief Custom logging over UART
*/
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdint.h>
#include <stdbool.h>
#include <zephyr.h>
#include <device.h>
#include <init.h>
#include <drivers/console/uart_pipe.h>
#include <misc/byteorder.h>
#include <misc/printk.h>
#include <uart.h>
#include <bluetooth/buf.h>
#include "monitor.h"
/* This is the same default priority as for other console handlers,
* except that we're not exporting it as a Kconfig variable until a
* clear need arises.
*/
#define MONITOR_INIT_PRIORITY 60
static struct device *monitor_dev;
extern int _prf(int (*func)(), void *dest,
const char *format, va_list vargs);
static void monitor_send(const void *data, size_t len)
{
const uint8_t *buf = data;
while (len--) {
uart_poll_out(monitor_dev, *buf++);
}
}
static inline void encode_hdr(struct bt_monitor_hdr *hdr, uint16_t opcode,
uint16_t len)
{
uint32_t ts32;
hdr->hdr_len = sizeof(hdr->type) + sizeof(hdr->ts32);
hdr->data_len = sys_cpu_to_le16(4 + hdr->hdr_len + len);
hdr->opcode = sys_cpu_to_le16(opcode);
hdr->flags = 0;
/* Extended header */
hdr->type = BT_MONITOR_TS32;
ts32 = k_uptime_get() * 10;
hdr->ts32 = sys_cpu_to_le32(ts32);
}
static int log_out(int c, void *unused)
{
uart_poll_out(monitor_dev, c);
return 0;
}
void bt_log(int prio, const char *fmt, ...)
{
struct bt_monitor_user_logging log;
struct bt_monitor_hdr hdr;
const char id[] = "bt";
va_list ap;
int len, key;
va_start(ap, fmt);
len = vsnprintk(NULL, 0, fmt, ap);
va_end(ap);
if (len < 0) {
return;
}
log.priority = prio;
log.ident_len = sizeof(id);
encode_hdr(&hdr, BT_MONITOR_USER_LOGGING,
sizeof(log) + sizeof(id) + len + 1);
key = irq_lock();
monitor_send(&hdr, sizeof(hdr));
monitor_send(&log, sizeof(log));
monitor_send(id, sizeof(id));
va_start(ap, fmt);
_vprintk(log_out, NULL, fmt, ap);
va_end(ap);
/* Terminate the string with null */
uart_poll_out(monitor_dev, '\0');
irq_unlock(key);
}
void bt_monitor_send(uint16_t opcode, const void *data, size_t len)
{
struct bt_monitor_hdr hdr;
int key;
encode_hdr(&hdr, opcode, len);
key = irq_lock();
monitor_send(&hdr, sizeof(hdr));
monitor_send(data, len);
irq_unlock(key);
}
void bt_monitor_new_index(uint8_t type, uint8_t bus, bt_addr_t *addr,
const char *name)
{
struct bt_monitor_new_index pkt;
pkt.type = type;
pkt.bus = bus;
memcpy(pkt.bdaddr, addr, 6);
strncpy(pkt.name, name, sizeof(pkt.name) - 1);
pkt.name[sizeof(pkt.name) - 1] = '\0';
bt_monitor_send(BT_MONITOR_NEW_INDEX, &pkt, sizeof(pkt));
}
#if !defined(CONFIG_UART_CONSOLE)
static int monitor_console_out(int c)
{
static char buf[128];
static size_t len;
int key;
key = irq_lock();
if (c != '\n' && len < sizeof(buf) - 1) {
buf[len++] = c;
irq_unlock(key);
return c;
}
buf[len++] = '\0';
bt_monitor_send(BT_MONITOR_SYSTEM_NOTE, buf, len);
len = 0;
irq_unlock(key);
return c;
}
extern void __printk_hook_install(int (*fn)(int));
extern void __stdout_hook_install(int (*fn)(int));
#endif /* !CONFIG_UART_CONSOLE */
static int bt_monitor_init(struct device *d)
{
ARG_UNUSED(d);
monitor_dev = device_get_binding(CONFIG_BLUETOOTH_MONITOR_ON_DEV_NAME);
#if defined(CONFIG_UART_INTERRUPT_DRIVEN)
uart_irq_rx_disable(monitor_dev);
uart_irq_tx_disable(monitor_dev);
#endif
#if !defined(CONFIG_UART_CONSOLE)
__printk_hook_install(monitor_console_out);
__stdout_hook_install(monitor_console_out);
#endif
return 0;
}
SYS_INIT(bt_monitor_init, PRE_KERNEL_1, MONITOR_INIT_PRIORITY);
|