Skip to content

Commit e51ec54

Browse files
anchaoxiaoxiang781216
authored andcommitted
stream/hexdump: add hexdump stream to dump binary to syslog
Signed-off-by: chao an <anchao@xiaomi.com>
1 parent 0203839 commit e51ec54

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed

include/nuttx/streams.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ struct lib_bufferedoutstream_s
225225
char buffer[CONFIG_STREAM_OUT_BUFFER_SIZE];
226226
};
227227

228+
struct lib_hexdumpstream_s
229+
{
230+
struct lib_outstream_s public;
231+
FAR struct lib_outstream_s *backend;
232+
int pending;
233+
char buffer[CONFIG_STREAM_HEXDUMP_BUFFER_SIZE + 1];
234+
};
235+
228236
/* This is a special stream that does buffered character I/O. NOTE that is
229237
* CONFIG_SYSLOG_BUFFER is not defined, it is the same as struct
230238
* lib_outstream_s
@@ -413,6 +421,25 @@ void lib_rawsostream(FAR struct lib_rawsostream_s *outstream, int fd);
413421
void lib_bufferedoutstream(FAR struct lib_bufferedoutstream_s *outstream,
414422
FAR struct lib_outstream_s *backend);
415423

424+
/****************************************************************************
425+
* Name: lib_hexdumpstream
426+
*
427+
* Description:
428+
* Convert binary stream to hex and redirect to syslog
429+
*
430+
* Input Parameters:
431+
* stream - User allocated, uninitialized instance of struct
432+
* lib_bufferedoutstream_s to be initialized.
433+
* backend - Stream backend port.
434+
*
435+
* Returned Value:
436+
* None (User allocated instance initialized).
437+
*
438+
****************************************************************************/
439+
440+
void lib_hexdumpstream(FAR struct lib_hexdumpstream_s *stream,
441+
FAR struct lib_outstream_s *backend);
442+
416443
/****************************************************************************
417444
* Name: lib_lowoutstream
418445
*

libs/libc/stream/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ config STREAM_OUT_BUFFER_SIZE
2525
int "Output stream buffer size"
2626
default 64
2727

28+
config STREAM_HEXDUMP_BUFFER_SIZE
29+
int "Output hexdump stream buffer size"
30+
default 128
31+
2832
endmenu # Locale Support

libs/libc/stream/Make.defs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ CSRCS += lib_rawoutstream.c lib_rawsistream.c lib_rawsostream.c
2727
CSRCS += lib_zeroinstream.c lib_nullinstream.c lib_nulloutstream.c
2828
CSRCS += lib_mtdoutstream.c lib_libnoflush.c lib_libsnoflush.c
2929
CSRCS += lib_syslogstream.c lib_syslograwstream.c lib_bufferedoutstream.c
30+
CSRCS += lib_hexdumpstream.c
3031

3132
# The remaining sources files depend upon C streams
3233

libs/libc/stream/lib_hexdumpstream.c

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/****************************************************************************
2+
* libs/libc/stream/lib_hexdumpstream.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/config.h>
26+
#include <nuttx/compiler.h>
27+
#include <nuttx/streams.h>
28+
29+
/****************************************************************************
30+
* Private Functions
31+
****************************************************************************/
32+
33+
/****************************************************************************
34+
* Name: nibble2hex
35+
*
36+
* Description:
37+
* Convert a binary nibble to a hexadecimal character.
38+
*
39+
****************************************************************************/
40+
41+
static char nibble2hex(unsigned char nibble)
42+
{
43+
if (nibble < 10)
44+
{
45+
return '0' + nibble;
46+
}
47+
else
48+
{
49+
return 'A' + nibble - 10;
50+
}
51+
}
52+
53+
/****************************************************************************
54+
* Name: bin2hex
55+
****************************************************************************/
56+
57+
static size_t bin2hex(FAR const uint8_t *buf, size_t buflen,
58+
FAR char *hex, size_t hexlen)
59+
{
60+
size_t i;
61+
62+
if (buflen > hexlen)
63+
{
64+
buflen = hexlen;
65+
}
66+
67+
for (i = 0; i < buflen; i++)
68+
{
69+
hex[2 * i] = nibble2hex(buf[i] >> 4);
70+
hex[2 * i + 1] = nibble2hex(buf[i] & 0xf);
71+
}
72+
73+
return buflen;
74+
}
75+
76+
/****************************************************************************
77+
* Name: hexdumpstream_flush
78+
****************************************************************************/
79+
80+
static int hexdumpstream_flush(FAR struct lib_outstream_s *this)
81+
{
82+
FAR struct lib_hexdumpstream_s *rthis = (FAR void *)this;
83+
84+
if (rthis->pending > 0)
85+
{
86+
rthis->buffer[rthis->pending] = '\n';
87+
lib_stream_puts(rthis->backend, rthis->buffer, rthis->pending + 1);
88+
rthis->pending = 0;
89+
}
90+
91+
return OK;
92+
}
93+
94+
/****************************************************************************
95+
* Name: hexdumpstream_putc
96+
****************************************************************************/
97+
98+
static void hexdumpstream_putc(FAR struct lib_outstream_s *this, int ch)
99+
{
100+
FAR struct lib_hexdumpstream_s *rthis = (FAR void *)this;
101+
int outlen = CONFIG_STREAM_HEXDUMP_BUFFER_SIZE;
102+
const uint8_t byte = ch;
103+
104+
bin2hex(&byte, 1, rthis->buffer + rthis->pending,
105+
(outlen - rthis->pending) / 2);
106+
107+
rthis->pending += 2;
108+
109+
if (rthis->pending == outlen)
110+
{
111+
hexdumpstream_flush(this);
112+
}
113+
}
114+
115+
/****************************************************************************
116+
* Name: hexdumpstream_puts
117+
****************************************************************************/
118+
119+
static int hexdumpstream_puts(FAR struct lib_outstream_s *this,
120+
FAR const void *buf, int len)
121+
{
122+
FAR struct lib_hexdumpstream_s *rthis = (FAR void *)this;
123+
const unsigned char *p = buf;
124+
int outlen = CONFIG_STREAM_HEXDUMP_BUFFER_SIZE;
125+
int line = outlen / 2;
126+
int remain = len;
127+
int ret;
128+
129+
while (remain > 0)
130+
{
131+
ret = remain > line ? line : remain;
132+
ret = bin2hex(p, ret, rthis->buffer + rthis->pending,
133+
(outlen - rthis->pending) / 2);
134+
135+
p += ret;
136+
remain -= ret;
137+
rthis->pending += ret * 2;
138+
139+
if (rthis->pending == outlen)
140+
{
141+
hexdumpstream_flush(this);
142+
}
143+
}
144+
145+
this->nput += len;
146+
147+
return len;
148+
}
149+
150+
/****************************************************************************
151+
* Public Functions
152+
****************************************************************************/
153+
154+
/****************************************************************************
155+
* Name: lib_hexdumpstream
156+
*
157+
* Description:
158+
* Convert binary stream to hex and redirect to syslog
159+
*
160+
* Input Parameters:
161+
* stream - User allocated, uninitialized instance of struct
162+
* lib_bufferedoutstream_s to be initialized.
163+
* backend - Stream backend port.
164+
*
165+
* Returned Value:
166+
* None (User allocated instance initialized).
167+
*
168+
****************************************************************************/
169+
170+
void lib_hexdumpstream(FAR struct lib_hexdumpstream_s *stream,
171+
FAR struct lib_outstream_s *backend)
172+
{
173+
struct lib_outstream_s *public = &stream->public;
174+
175+
public->putc = hexdumpstream_putc;
176+
public->puts = hexdumpstream_puts;
177+
public->flush = hexdumpstream_flush;
178+
public->nput = 0;
179+
180+
stream->pending = 0;
181+
stream->backend = backend;
182+
}

0 commit comments

Comments
 (0)