monitor: Extract TTY data processing to separate function
Subsystems (2)
PATCH v3 1/3 monitor: Extract TTY data processing to separate function
From: Andrzej Kaczmarek <[email protected]>
--- monitor/control.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-)
monitor/control.c +21 -16
@@ -1300,23 +1300,8 @@ static bool tty_parse_header(uint8_t *hdr, uint8_t len, struct timeval **tv,return true; } -static void tty_callback (int fd,uint32_t events,void *user_data) +static void process_data (struct control_data *data) { -struct control_data *data = user_data; -ssize_t len; - -if (events & (EPOLLERR |EPOLLHUP )) { -mainloop_remove_fd (data->fd); -return ; - } - - len =read (data->fd, data->buf + data->offset, -sizeof (data->buf) - data->offset); -if (len <0 ) -return ; - - data->offset += len; -while (data->offset >=sizeof (struct tty_hdr )) {struct tty_hdr *hdr = (struct tty_hdr *) data->buf;uint16_t pktlen, opcode, data_len;
@@ -1358,6 +1343,26 @@ static void tty_callback(int fd, uint32_t events, void *user_data) } } +static void tty_callback (int fd,uint32_t events,void *user_data) +{ +struct control_data *data = user_data; +ssize_t len; + +if (events & (EPOLLERR |EPOLLHUP )) { +mainloop_remove_fd (data->fd); +return ; + } + + len =read (data->fd, data->buf + data->offset, +sizeof (data->buf) - data->offset); +if (len <0 ) +return ; + + data->offset += len; + +process_data (data); +} +int control_tty (const char *path,unsigned int speed) {struct control_data *data;
Signature · 1 line
2.21.0
PATCH v3 2/3 monitor: Add interface for J-Link library
From: Andrzej Kaczmarek <[email protected]>
This adds simple interface to libjlinkarm.so which will be used to read data from RTT buffer. It was mostly made by trial and error since there is no public documentation for this library so it may lack something, but seems to work fine with few Cortex-M devices I tried.
--- monitor/jlink.c | 283 ++++++++++++++++++++++++++++++++++++++++++++++++ monitor/jlink.h | 27 +++++ 2 files changed, 310 insertions(+) create mode 100644 monitor/jlink.c create mode 100644 monitor/jlink.h
monitor/jlink.c new file +283 -0
@@ -0,0 +1,283 @@ +/* +* +* BlueZ - Bluetooth protocol stack for Linux +* +* Copyright (C) 2018 Codecoup +* +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library 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 +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +* +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <dlfcn.h> +#include <errno.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "jlink.h" + +#define RTT_CONTROL_START 0 +#define RTT_CONTROL_STOP 1 +#define RTT_CONTROL_GET_DESC 2 +#define RTT_CONTROL_GET_NUM_BUF 3 +#define RTT_CONTROL_GET_STAT 4 + +#define RTT_DIRECTION_UP 0 +#define RTT_DIRECTION_DOWN 1 + +static const char *const jlink_so_name[] = { +"/usr/lib/libjlinkarm.so" , +"/usr/lib/libjlinkarm.so.6" , +"/opt/SEGGER/JLink/libjlinkarm.so" , +"/opt/SEGGER/JLink/libjlinkarm.so.6" , +}; + +struct rtt_desc { +uint32_t index; +uint32_t direction; +char name[32 ]; +uint32_t size; +uint32_t flags; +}; + +static struct rtt_desc rtt_desc; + +typedef int (*jlink_emu_selectbyusbsn_func ) (unsigned int sn); +typedef int (*jlink_open_func ) (void ); +typedef int (*jlink_execcommand_func ) (char *in,char *out,int size); +typedef int (*jlink_tif_select_func ) (int ); +typedef void (*jlink_setspeed_func ) (long int speed); +typedef int (*jlink_connect_func ) (void ); +typedef unsigned int (*jlink_getsn_func ) (void ); +typedef void (*jlink_emu_getproductname_func ) (char *out,int size); +typedef int (*jlink_rtterminal_control_func ) (int cmd,void *data); +typedef int (*jlink_rtterminal_read_func ) (int cmd,char *buf,int size); + +struct jlink { +jlink_emu_selectbyusbsn_func emu_selectbyusbsn; +jlink_open_func open; +jlink_execcommand_func execcommand; +jlink_tif_select_func tif_select; +jlink_setspeed_func setspeed; +jlink_connect_func connect; +jlink_getsn_func getsn; +jlink_emu_getproductname_func emu_getproductname; +jlink_rtterminal_control_func rtterminal_control; +jlink_rtterminal_read_func rtterminal_read; +}; + +static struct jlink jlink; + +#ifndef NELEM +#define NELEM (x) (sizeof(x) / sizeof((x)[0])) +#endif + +int jlink_init (void ) +{ +void *so; +unsigned int i; + +for (i =0 ; i <NELEM (jlink_so_name); i++) { + so =dlopen (jlink_so_name[i],RTLD_LAZY ); +if (so) +break ; + } + +if (!so) +return -EIO ; + + jlink.emu_selectbyusbsn =dlsym (so,"JLINK_EMU_SelectByUSBSN" ); + jlink.open =dlsym (so,"JLINK_Open" ); + jlink.execcommand =dlsym (so,"JLINK_ExecCommand" ); + jlink.tif_select =dlsym (so,"JLINK_TIF_Select" ); + jlink.setspeed =dlsym (so,"JLINK_SetSpeed" ); + jlink.connect =dlsym (so,"JLINK_Connect" ); + jlink.getsn =dlsym (so,"JLINK_GetSN" ); + jlink.emu_getproductname =dlsym (so,"JLINK_EMU_GetProductName" ); + jlink.rtterminal_control =dlsym (so,"JLINK_RTTERMINAL_Control" ); + jlink.rtterminal_read =dlsym (so,"JLINK_RTTERMINAL_Read" ); + +if (!jlink.emu_selectbyusbsn || !jlink.open || !jlink.execcommand || + !jlink.tif_select || !jlink.setspeed || + !jlink.connect || !jlink.getsn || + !jlink.emu_getproductname || + !jlink.rtterminal_control || !jlink.rtterminal_read) +return -EIO ; + +return 0 ; +} + +int jlink_connect (char *cfg) +{ +const char *device =NULL ; +int tif =1 ; +unsigned int speed =1000 ; +unsigned int serial_no =0 ; +char *tok; +char buf[64 ]; + + tok =strtok (cfg,"," ); + device = tok; + + tok =strtok (NULL ,"," ); +if (!tok) + goto connect; +if (strlen (tok)) + serial_no =atoi (tok); + + tok =strtok (NULL ,"," ); +if (!tok) + goto connect; +if (strlen (tok)) { +if (!strcasecmp ("swd" , tok)) + tif =1 ; +else +return -EINVAL ; + } + + tok =strtok (NULL ,"," ); +if (!tok) + goto connect; +if (strlen (tok)) + speed =atoi (tok); + +connect: +if (serial_no) +if (jlink.emu_selectbyusbsn (serial_no) <0 ) { +fprintf (stderr,"Failed to select emu by SN\n" ); +return -ENODEV ; + } + +if (jlink.open () <0 ) { +fprintf (stderr,"Failed to open J-Link\n" ); +return -ENODEV ; + } + +snprintf (buf,sizeof (buf),"device=%s" , device); +if (jlink.execcommand (buf,NULL ,0 ) <0 ) { +fprintf (stderr,"Failed to select target device\n" ); +return -ENODEV ; + } + +if (jlink.tif_select (tif) <0 ) { +fprintf (stderr,"Failed to select target interface\n" ); +return -ENODEV ; + } + + jlink.setspeed (speed); + +if (jlink.connect () <0 ) { +fprintf (stderr,"Failed to open target\n" ); +return -EIO ; + } + + serial_no = jlink.getsn (); + jlink.emu_getproductname (buf,sizeof (buf)); + +printf ("Connected to %s (S/N: %u)\n" , buf, serial_no); + +return 0 ; +} + +int jlink_start_rtt (char *cfg) +{ +unsigned int address =0 ; +unsigned int area_size =0 ; +const char *buffer ="btmonitor" ; +char *tok; +char cmd[64 ]; +int rtt_dir; +int count; +int i; + +if (!cfg) + goto find_rttcb; + + tok =strtok (cfg,"," ); +if (strlen (tok)) { + address =strtol (tok,NULL ,0 ); + area_size =0x1000 ; + } + + tok =strtok (NULL ,"," ); +if (!tok) + goto find_rttcb; +if (strlen (tok)) + area_size =strtol (tok,NULL ,0 ); + + tok =strtok (NULL ,"," ); +if (!tok) + goto find_rttcb; +if (strlen (tok)) + buffer = tok; + +find_rttcb: +if (address || area_size) { +if (!area_size) +snprintf (cmd,sizeof (cmd),"SetRTTAddr 0x%x" , address); +else +snprintf (cmd,sizeof (cmd), +"SetRTTSearchRanges 0x%x 0x%x" , + address, area_size); + +if (jlink.execcommand (cmd,NULL ,0 ) <0 ) +return -EIO ; + } + +if (jlink.rtterminal_control (RTT_CONTROL_START ,NULL ) <0 ) { +fprintf (stderr,"Failed to initialize RTT\n" ); +return -1 ; + } + +/* RTT may need some time to find control block so we need to wait */ +do { +usleep (100 ); + rtt_dir =RTT_DIRECTION_UP ; + count = jlink.rtterminal_control (RTT_CONTROL_GET_NUM_BUF , + &rtt_dir); + }while (count <0 ); + +for (i =0 ; i < count; i++) { +memset (&rtt_desc,0 ,sizeof (rtt_desc)); + rtt_desc.index = i; + rtt_desc.direction =RTT_DIRECTION_UP ; + +if (jlink.rtterminal_control (RTT_CONTROL_GET_DESC , + &rtt_desc) <0 ) +continue ; + +if (rtt_desc.size >0 && !strcmp (buffer, rtt_desc.name)) +break ; + } + +if (i == count) +return -ENODEV ; + +printf ("Using RTT up buffer #%d (size: %d)\n" , i, rtt_desc.size); + +return 0 ; +} + +int jlink_rtt_read (void *buf,size_t size) +{ +return jlink.rtterminal_read (rtt_desc.index, buf, size); +}
monitor/jlink.h new file +27 -0
@@ -0,0 +1,27 @@ +/* +* +* BlueZ - Bluetooth protocol stack for Linux +* +* Copyright (C) 2018 Codecoup +* +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library 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 +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +* +*/ + +int jlink_init (void ); +int jlink_connect (char *cfg); +int jlink_start_rtt (char *cfg); +int jlink_rtt_read (void *buf,size_t size);
Signature · 1 line
2.21.0
PATCH v3 3/3 monitor: Add support for reading over J-Link RTT
In reply to Szymon JancFrom: Andrzej Kaczmarek <[email protected]>
This patch adds support for reading data over J-Link RTT. It can be used as replacement for TTY when reading from embedded devices since it's much faster and does block a UART. Data format is the same as for TTY. At the moment monitor over RTT is only supported by Apache Mynewt project.
Reading data is done by polling RTT every 1 msec since there is no blocking API to read something from RTT buffer.
To enable reading from RTT, J-Link configuration needs to be passed via command line (all parameters except <device> can be skipped to use default value):
-J <device>,<serialno=0>,<interface=swd>,<speed=1000>
-J nrf52,683649029
In some cases J-Link cannot locate RTT buffer in RAM. In such case RAM area and buffer name should be provided via command line:
-R <address=0x0>,<area=0x1000>,<buffer=monitor>
-R 0x20000000,0x10000
--- Makefile.tools | 3 ++- monitor/control.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++ monitor/control.h | 1 + monitor/main.c | 21 ++++++++++++++++++-- 4 files changed, 72 insertions(+), 3 deletions(-)
Makefile.tools +2 -1
@@ -42,9 +42,10 @@ monitor_btmon_SOURCES = monitor/main.c monitor/bt.h \ monitor/analyze.h monitor/analyze.c \ monitor/intel.h monitor/intel.c \ monitor/broadcom.h monitor/broadcom.c \ + monitor/jlink.h monitor/jlink.c \ monitor/tty.h monitor_btmon_LDADD = lib/libbluetooth-internal.la \ - src/libshared-mainloop.la $(UDEV_LIBS) + src/libshared-mainloop.la $(UDEV_LIBS) -ldl endif if LOGGER
monitor/control.c +50 -0
62 lines not on this page · raw message
monitor/control.h +1 -0
@@ -28,6 +28,7 @@ bool control_writer(const char *path);void control_reader (const char *path,bool pager);void control_server (const char *path);int control_tty (const char *path,unsigned int speed); +int control_rtt (char *jlink,char *rtt);int control_tracing (void );void control_disable_decoding (void );void control_filter_index (uint16_t index);
monitor/main.c +19 -2
@@ -75,6 +75,10 @@ static void usage(void)"\t-A, --a2dp Dump A2DP stream traffic\n" "\t-E, --ellisys [ip] Send Ellisys HCI Injection\n" "\t-P, --no-pager Disable pager usage\n" +"\t-J --jlink <device>,[<serialno>],[<interface>],[<speed>]\n" +"\t Read data from RTT\n" +"\t-R --rtt [<address>],[<area>],[<name>]\n" +"\t RTT control block parameters\n" "\t-h, --help Show help options\n"); }
@@ -94,6 +98,8 @@ static const struct option main_options[] = { { "a2dp", no_argument, NULL, 'A' }, { "ellisys", required_argument, NULL, 'E' }, { "no-pager", no_argument, NULL, 'P' }, + { "jlink", required_argument, NULL, 'J' }, + { "rtt", required_argument, NULL, 'R' }, { "todo", no_argument, NULL, '#' }, { "version", no_argument, NULL, 'v' }, { "help", no_argument, NULL, 'h' },
@@ -112,6 +118,8 @@ int main(int argc, char *argv[])unsigned int tty_speed =B115200 ;unsigned short ellisys_port =0 ;const char *str; +char *jlink =NULL ; +char *rtt =NULL ;int exit_status;mainloop_init ();
@@ -122,7 +130,7 @@ int main(int argc, char *argv[])int opt;struct sockaddr_un addr; - opt =getopt_long (argc, argv,"r:w:a:s:p:i:d:B:V:tTSAEPvh" , + opt =getopt_long (argc, argv,"r:w:a:s:p:i:d:B:V:tTSAE:PJ:R:vh" , main_options,NULL );if (opt <0 )break ;
@@ -194,6 +202,12 @@ int main(int argc, char *argv[])case 'P' : use_pager = false;break ; +case 'J' : + jlink = optarg; +break ; +case 'R' : + rtt = optarg; +break ;case '#' :packet_todo ();lmp_todo ();
@@ -246,12 +260,15 @@ int main(int argc, char *argv[])if (ellisys_server)ellisys_enable (ellisys_server, ellisys_port); -if (!tty &&control_tracing () <0 ) +if (!tty && !jlink &&control_tracing () <0 )return EXIT_FAILURE ;if (tty &&control_tty (tty, tty_speed) <0 )return EXIT_FAILURE ; +if (jlink &&control_rtt (jlink, rtt) <0 ) +return EXIT_FAILURE ; + exit_status =mainloop_run_with_signal (signal_callback,NULL );keys_cleanup ();
Signature · 1 line
2.21.0
Szymon Janc
AppliedQuoted 63 lines · On Friday, 4 October 2019 07:43:32 CEST Szymon Janc wrote:
From: Andrzej Kaczmarek <[email protected]>
--- monitor/control.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-)monitor/control.c +1
-16@@ -1300,23 +1300,8 @@ static bool tty_parse_header(uint8_t *hdr, uint8_t len, struct timeval **tv, return true; } -static void tty_callback (int fd,uint32_t events,void *user_data) +static void process_data (struct control_data *data) { -struct control_data *data = user_data; -ssize_t len; - -if (events & (EPOLLERR |EPOLLHUP )) { -mainloop_remove_fd (data->fd); -return ; - } - - len =read (data->fd, data->buf + data->offset, -sizeof (data->buf) - data- offset); -if (len <0 ) -return ; - - data->offset += len; -while (data->offset >=sizeof (struct tty_hdr )) {struct tty_hdr *hdr = (struct tty_hdr *) data->buf;monitor/control.c +20
-0uint16_t pktlen, opcode, data_len;@@ -1358,6 +1343,26 @@ static void tty_callback(int fd, uint32_t events, void *user_data) } } +static void tty_callback (int fd,uint32_t events,void *user_data) +{ +struct control_data *data = user_data; +ssize_t len; + +if (events & (EPOLLERR |EPOLLHUP )) { +mainloop_remove_fd (data->fd); +return ; + } + + len =read (data->fd, data->buf + data->offset, +sizeof (data->buf) - data- offset); +if (len <0 ) +return ; + + data->offset += len; + +process_data (data); +} +int control_tty (const char *path,unsigned int speed) {
Quoted 1 line
struct
control_data*data;
Applied.
Signature · 2 lines
pozdrawiam Szymon Janc