raiden00pl commented on code in PR #1460: URL: https://github.com/apache/nuttx-apps/pull/1460#discussion_r1051650350
########## logging/nxscope/nxscope.c: ########## @@ -0,0 +1,1142 @@ +/**************************************************************************** + * apps/logging/nxscope/nxscope.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 <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <logging/nxscope/nxscope.h> + +#include "nxscope_internals.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nxscope_frame_send + * + * NOTE: This function assumes that we have exclusive access to the nxscope + * instance + * + ****************************************************************************/ + +static int nxscope_frame_send(FAR struct nxscope_s *s, uint8_t id, + FAR uint8_t *data, size_t dlen) +{ + size_t tx_i = 0; + int ret = OK; + + DEBUGASSERT(s); + DEBUGASSERT(data); + + /* Offset for hdr */ + + tx_i = s->proto_cmd->hdrlen; + + /* Copy data */ + + memcpy(&s->txbuf[tx_i], data, dlen); + tx_i += dlen; + + /* Finalize a new frame */ + + ret = PROTO_FRAME_FINAL(s, s->proto_cmd, id, s->txbuf, &tx_i); + if (ret < 0) + { + _err("ERROR: PROTO_FRAME_FINAL failed %d\n", ret); + goto errout; + } + + /* Send frame */ + + ret = INTF_SEND(s, s->intf_cmd, s->txbuf, tx_i); + if (ret < 0) + { + _err("ERROR: INTF_SEND failed %d\n", ret); + } + +errout: + return ret; +} + +/**************************************************************************** + * Name: nxscope_cmninfo_send + * + * NOTE: This function assumes that we have exclusive access to the nxscope + * instance + * + ****************************************************************************/ + +static int nxscope_cmninfo_send(FAR struct nxscope_s *s) +{ + DEBUGASSERT(s); + + return nxscope_frame_send(s, + NXSCOPE_HDRID_CMNINFO, + (FAR uint8_t *)&s->cmninfo, + sizeof(struct nxscope_info_cmn_s)); +} + +/**************************************************************************** + * Name: nxscope_chinfo_send + * + * NOTE: This function assumes that we have exclusive access to the nxscope + * instance + * + ****************************************************************************/ + +static int nxscope_chinfo_send(FAR struct nxscope_s *s, uint8_t ch) +{ + FAR uint8_t *data = NULL; + size_t namelen = 0; + size_t txlen = 0; + size_t tmp = 0; + int ret = OK; + + DEBUGASSERT(s); + + /* Allocate data for channel info */ + + namelen = strnlen(s->chinfo[ch].name, CHAN_NAMELEN_MAX) + 1; + tmp = sizeof(struct nxscope_chinfo_s) - sizeof(char *); + txlen = tmp + namelen; + + data = zalloc(txlen); + if (data == NULL) + { + ret = -errno; + _err("ERROR: zalloc failed %d\n", ret); + goto errout; + } + + /* Copy channel info */ + + memcpy(data, &s->chinfo[ch], tmp); + memcpy(&data[tmp], s->chinfo[ch].name, namelen); + + /* Send frame */ + + ret = nxscope_frame_send(s, NXSCOPE_HDRID_CHINFO, data, txlen); + + /* Free allocated memory */ + + free(data); + +errout: + return ret; +} + +/**************************************************************************** + * Name: nxscope_enable_req + * + * NOTE: This function assumes that we have exclusive access to the nxscope + * instance + * + ****************************************************************************/ + +static int nxscope_enable_req(FAR struct nxscope_s *s, + FAR struct nxscope_set_frame_s *set, + uint16_t dlen) +{ + FAR struct nxscope_enable_data_s *data = NULL; + int ret = OK; + int i = 0; + + DEBUGASSERT(s); + DEBUGASSERT(set); + + /* Get data */ + + data = (FAR struct nxscope_enable_data_s *) set->data; + + /* Handle set request */ + + switch (set->req) + { + case NXSCOPE_SET_REQ_SINGLE: + { + /* Verify request length */ + + if (dlen != (sizeof(struct nxscope_set_frame_s) + + NXSCOPE_ENABLE_LEN - 1)) + { + _err("ERROR: invalid enable single dlen = %d\n", dlen); + ret = -EINVAL; + goto errout; + } + + /* Validate data */ + + if (data->ch[0].en != 0 && data->ch[0].en != 1) + { + ret = -EINVAL; + goto errout; + } + + if (set->chan > s->cmninfo.chmax) + { + ret = -EINVAL; + goto errout; + } + + /* Write configuration */ + + s->chinfo[set->chan].enable = data->ch[0].en; + + break; + } + + case NXSCOPE_SET_REQ_BULK: + { + /* Verify request length */ + + if (dlen != (sizeof(struct nxscope_set_frame_s) + + NXSCOPE_ENABLE_LEN * s->cmninfo.chmax - 1)) + { + _err("ERROR: invalid enable bulk dlen = %d\n", dlen); + ret = -EINVAL; + goto errout; + } + + /* Validate data */ + + for (i = 0; i < s->cmninfo.chmax; i += 1) + { + if (data->ch[i].en != 0 && data->ch[i].en != 1) + { + ret = -EINVAL; + goto errout; + } + } + + /* Write configuration */ + + for (i = 0; i < s->cmninfo.chmax; i += 1) + { + s->chinfo[i].enable = data->ch[i].en; + } + + break; + } + + case NXSCOPE_SET_REQ_ALL: + { + /* Verify request length */ + + if (dlen != (sizeof(struct nxscope_set_frame_s) + + NXSCOPE_ENABLE_LEN - 1)) + { + _err("ERROR: invalid enable all dlen = %d\n", dlen); + ret = -EINVAL; + goto errout; + } + + if (data->ch[0].en != 0 && data->ch[0].en != 1) + { + ret = -EINVAL; + goto errout; + } + + /* Write configuration */ + + for (i = 0; i < s->cmninfo.chmax; i += 1) + { + s->chinfo[i].enable = data->ch[0].en; + } + + break; + } + + default: + { + _err("ERROR: invalid set->req=%d\n", set->req); + ret = -EINVAL; + goto errout; + } + } + +errout: + return ret; +} + +#ifdef CONFIG_LOGGING_NXSCOPE_DIVIDER +/**************************************************************************** + * Name: nxscope_div_req + * + * NOTE: This function assumes that we have exclusive access to the nxscope + * instance + * + ****************************************************************************/ + +static int nxscope_div_req(FAR struct nxscope_s *s, + FAR struct nxscope_set_frame_s *set, + uint16_t dlen) +{ + FAR struct nxscope_div_data_s *data = NULL; + int ret = OK; + int i = 0; + + DEBUGASSERT(s); + DEBUGASSERT(set); + + /* Get data */ + + data = (FAR struct nxscope_div_data_s *) set->data; + + /* Handle set request */ + + switch (set->req) + { + case NXSCOPE_SET_REQ_SINGLE: + { + /* Verify request length */ + + if (dlen != (sizeof(struct nxscope_set_frame_s) + + NXSCOPE_DIV_LEN - 1)) + { + _err("ERROR: invalid div single dlen = %d\n", dlen); + ret = -EINVAL; + goto errout; + } + + if (set->chan > s->cmninfo.chmax) + { + ret = -EINVAL; + goto errout; + } + + /* Write configuration */ + + s->chinfo[set->chan].div = data->ch[0].div; + + break; + } + + case NXSCOPE_SET_REQ_BULK: + { + /* Verify request length */ + + if (dlen != (sizeof(struct nxscope_set_frame_s) + + NXSCOPE_DIV_LEN * s->cmninfo.chmax - 1)) + { + _err("ERROR: invalid div bulk dlen = %d\n", dlen); + ret = -EINVAL; + goto errout; + } + + /* Write configuration */ + + for (i = 0; i < s->cmninfo.chmax; i += 1) + { + s->chinfo[i].div = data->ch[i].div; + } + + break; + } + + case NXSCOPE_SET_REQ_ALL: + { + /* Verify request length */ + + if (dlen != (sizeof(struct nxscope_set_frame_s) + + NXSCOPE_DIV_LEN - 1)) + { + _err("ERROR: invalid div all dlen = %d\n", dlen); + ret = -EINVAL; + goto errout; + } + + /* Write configuration */ + + for (i = 0; i < s->cmninfo.chmax; i += 1) + { + s->chinfo[i].div = data->ch[0].div; + } + + break; + } + + default: + { + _err("ERROR: invalid set->req=%d\n", set->req); + ret = -EINVAL; + goto errout; + } + } + +errout: + return ret; +} +#endif + +/**************************************************************************** + * Name: nxscope_start_set + * + * NOTE: This function assumes that we have exclusive access to the nxscope + * instance + * + ****************************************************************************/ + +static int nxscope_start_set(FAR struct nxscope_s *s, bool start) +{ + int ret = OK; + + s->start = start; + + /* User specific callback */ + + if (s->callbacks != NULL && s->callbacks->start != NULL) + { + ret = s->callbacks->start(s->callbacks->start_priv, start); + if (ret < 0) + { + _err("ERROR: s->callbacks->start failed %d\n", ret); + } + } + + return ret; +} + +/**************************************************************************** + * Name: nxscope_start_req + * + * NOTE: This function assumes that we have exclusive access to the nxscope + * instance + * + ****************************************************************************/ + +static int nxscope_start_req(FAR struct nxscope_s *s, + FAR struct nxscope_start_data_s *data) +{ + int ret = OK; Review Comment: That looks much better -- 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