|
| 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