You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "acassis (via GitHub)" <gi...@apache.org> on 2023/05/24 15:33:46 UTC
[GitHub] [nuttx] acassis commented on a diff in pull request #9369: sched/misc: add coredump support on assert
acassis commented on code in PR #9369:
URL: https://github.com/apache/nuttx/pull/9369#discussion_r1204402387
##########
libs/libc/stream/lib_hexdumpstream.c:
##########
@@ -0,0 +1,195 @@
+/****************************************************************************
+ * libs/libc/stream/lib_hexdumpstream.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <syslog.h>
+
+#include "libc.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nibble2hex
+ *
+ * Description:
+ * Convert a binary nibble to a hexadecimal character.
+ *
+ ****************************************************************************/
+
+static char nibble2hex(unsigned char nibble)
+{
+ if (nibble < 10)
+ {
+ return '0' + nibble;
+ }
+ else
+ {
+ return 'A' + nibble - 10;
+ }
+}
+
+/****************************************************************************
+ * Name: bin2hex
+ ****************************************************************************/
+
+static size_t bin2hex(FAR const uint8_t *buf, size_t buflen,
+ FAR char *hex, size_t hexlen)
+{
+ size_t i;
+
+ if (buflen > hexlen)
+ {
+ buflen = hexlen;
+ }
+
+ for (i = 0; i < buflen; i++)
+ {
+ hex[2 * i] = nibble2hex(buf[i] >> 4);
+ hex[2 * i + 1] = nibble2hex(buf[i] & 0xf);
+ }
+
+ return buflen;
+}
+
+/****************************************************************************
+ * Name: hexdumpstream_flush
+ ****************************************************************************/
+
+static int hexdumpstream_flush(FAR struct lib_outstream_s *this)
+{
+ FAR struct lib_hexdumpstream_s *rthis = (FAR void *)this;
+
+ if (rthis->pending > 0)
+ {
+ rthis->buffer[rthis->pending] = '\0';
+
+ if (rthis->backend == NULL)
+ {
+ syslog(rthis->priority, "%s\n", rthis->buffer);
+ }
+ else
+ {
+ lib_stream_puts(rthis->backend, rthis->buffer, rthis->pending);
+ }
+
+ rthis->pending = 0;
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Name: hexdumpstream_putc
+ ****************************************************************************/
+
+static void hexdumpstream_putc(FAR struct lib_outstream_s *this, int ch)
+{
+ FAR struct lib_hexdumpstream_s *rthis = (FAR void *)this;
+ int outlen = sizeof(rthis->buffer) - 1;
+ const uint8_t byte = ch;
+
+ bin2hex(&byte, 1, rthis->buffer + rthis->pending,
+ (outlen - rthis->pending) / 2);
+
+ rthis->pending += 2;
+
+ if (rthis->pending == outlen)
+ {
+ hexdumpstream_flush(this);
+ }
+}
+
+/****************************************************************************
+ * Name: hexdumpstream_puts
+ ****************************************************************************/
+
+static int hexdumpstream_puts(FAR struct lib_outstream_s *this,
+ FAR const void *buf, int len)
+{
+ FAR struct lib_hexdumpstream_s *rthis = (FAR void *)this;
+ const unsigned char *p = buf;
+ int outlen = sizeof(rthis->buffer) - 1;
+ int line = outlen / 2;
+ int remain = len;
+ int ret;
+
+ while (remain > 0)
+ {
+ ret = remain > line ? line : remain;
+ ret = bin2hex(p, ret, rthis->buffer + rthis->pending,
+ (outlen - rthis->pending) / 2);
+
+ p += ret;
+ remain -= ret;
+ rthis->pending += ret * 2;
+
+ if (rthis->pending == outlen)
+ {
+ hexdumpstream_flush(this);
+ }
+ }
+
+ this->nput += len;
+
+ return len;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: lib_hexdumpstream
+ *
+ * Description:
+ * Convert binary stream to hex and redirect to syslog
+ *
+ * Input Parameters:
+ * outstream - User allocated, uninitialized instance of struct
Review Comment:
```suggestion
* stream - User allocated, uninitialized instance of struct
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org