raiden00pl commented on code in PR #1460: URL: https://github.com/apache/nuttx-apps/pull/1460#discussion_r1051649667
########## logging/nxscope/nxscope_pser.c: ########## @@ -0,0 +1,297 @@ +/**************************************************************************** + * apps/logging/nxscope/nxscope_pser.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 <assert.h> +#include <debug.h> +#include <byteswap.h> +#include <errno.h> +#include <string.h> + +#include <nuttx/crc16.h> + +#include <logging/nxscope/nxscope.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define NXSCOPE_HDR_LEN (sizeof(struct nxscope_hdr_s)) +#define NXSCOPE_DATA_START (NXSCOPE_HDR_LEN) +#define NXSCOPE_BASEFRAME_LEN (NXSCOPE_HDR_LEN + NXSCOPE_CRC_LEN) +#define NXSCOPE_HDR_SOF (0x55) +#define NXSCOPE_CRC_LEN (sizeof(uint16_t)) + +/**************************************************************************** + * Private Type Definition + ****************************************************************************/ + +/* Nxscope serial protocol frame: + * +--------------------+------------+-------------+ + * | HDR [4B] | frame data | footer [2B] | + * +-----+---------+----+------------+-------------+ + * | SOF | len [1] | id | frame data | crc16 [2] | + * +-----+----+----+----+------------+------+------+ + * | 0 | 1 | 2 | 3 | ... | n+1 | n+2 | + * +-----+----+----+----+------------+------+------+ + * + * [1] - always little-endian + * [2] - always big-endian + */ + +/* Nxscope header */ + +begin_packed_struct struct nxscope_hdr_s +{ + uint8_t sof; /* SOF */ + uint16_t len; /* Frame length */ + uint8_t id; /* Frame ID */ +} end_packed_struct; + +/* Nxscope footer */ + +begin_packed_struct struct nxscope_footer_s +{ + uint16_t crc16; /* check sum (see nxscope_frame_final()) */ +} end_packed_struct; + +/**************************************************************************** + * Private Function Protototypes + ****************************************************************************/ + +static int nxscope_frame_get(FAR struct nxscope_proto_s *p, + FAR uint8_t *buff, size_t len, + FAR struct nxscope_frame_s *frame); +static int nxscope_frame_final(FAR struct nxscope_proto_s *p, + uint8_t id, + FAR uint8_t *buff, FAR size_t *len); + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +struct nxscope_proto_ops_s g_nxscope_proto_ser_ops = +{ + .frame_get = nxscope_frame_get, + .frame_final = nxscope_frame_final, +}; + +struct nxscope_proto_s g_nxscope_proto_ser = +{ + .priv = NULL, + .ops = &g_nxscope_proto_ser_ops, + .footlen = NXSCOPE_CRC_LEN, + .hdrlen = NXSCOPE_HDR_LEN +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nxscope_hdr_fill + ****************************************************************************/ + +static void nxscope_hdr_fill(FAR uint8_t *buff, uint8_t id, uint16_t len) +{ + FAR struct nxscope_hdr_s *hdr = NULL; + + hdr = (FAR struct nxscope_hdr_s *) buff; + + hdr->sof = NXSCOPE_HDR_SOF; + + /* Length always as little endian */ + +#ifdef CONFIG_ENDIAN_BIG + hdr->len = bswap_16(len); +#else + hdr->len = len; +#endif + + hdr->id = id; +} + +/**************************************************************************** + * Name: nxscope_frame_get + ****************************************************************************/ + +static int nxscope_frame_get(FAR struct nxscope_proto_s *p, + FAR uint8_t *buff, size_t len, + FAR struct nxscope_frame_s *frame) +{ + FAR struct nxscope_hdr_s *hdr = NULL; + uint16_t crc = 0; + int ret = OK; + int i = 0; + + DEBUGASSERT(p); + DEBUGASSERT(buff); + DEBUGASSERT(frame); + + UNUSED(p); + + /* Find SOF */ + + for (i = 0 ; i < len - NXSCOPE_HDR_LEN; i += 1) + { + hdr = (FAR struct nxscope_hdr_s *) &buff[i]; + + /* Verify SOF */ + + if (hdr->sof == NXSCOPE_HDR_SOF) + { + break; + } + } + + if (hdr->sof != NXSCOPE_HDR_SOF) + { + ret = -EINVAL; + goto errout; + } + + /* Verify len - always little-endian */ + +#ifdef CONFIG_ENDIAN_BIG + hdr->len = bswap_16(hdr->len); +#endif + + if ((len - i) < hdr->len) + { + ret = -EINVAL; + goto errout; + } + + /* Verify crc16 for the whole frame */ + + crc = crc16(&buff[i], hdr->len); + if (crc != 0) + { + _err("ERROR: invalid crc16 %d\n", crc); + ret = -EINVAL; + goto errout; + } + + /* Return the frame data */ + + frame->id = hdr->id; + frame->data = &buff[i + NXSCOPE_DATA_START]; + frame->drop = hdr->len + i; + frame->dlen = hdr->len - NXSCOPE_BASEFRAME_LEN; + +errout: + return ret; +} + +/**************************************************************************** + * Name: nxscope_frame_final + ****************************************************************************/ + +static int nxscope_frame_final(FAR struct nxscope_proto_s *p, + uint8_t id, + FAR uint8_t *buff, FAR size_t *len) +{ + uint16_t crc = 0; + int ret = OK; + + DEBUGASSERT(p); + DEBUGASSERT(buff); + DEBUGASSERT(len); + + if (*len <= NXSCOPE_HDR_LEN) + { + /* No data */ + + ret = -ENODATA; + goto errout; Review Comment: I prefer one `return` statement when possible -- 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-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org