raiden00pl commented on code in PR #15828:
URL: https://github.com/apache/nuttx/pull/15828#discussion_r1957262072


##########
drivers/wireless/lpwan/rn2xx3/rn2xx3.c:
##########
@@ -0,0 +1,1612 @@
+/****************************************************************************
+ * drivers/wireless/lpwan/rn2xx3/rn2xx3.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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 <debug.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <unistd.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mutex.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/signal.h>
+#include <nuttx/wqueue.h>
+
+#include <nuttx/wireless/lpwan/rn2xx3.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Duration of maximum MAC layer pause in milliseconds */
+
+#define MAC_PAUSE_DUR "4294967245"
+
+/* TODO: implement the following commands
+ * coding rate
+ * iqi
+ * crc
+ * sync word
+ * bitrate
+ */
+
+/****************************************************************************
+ * Private Data Types
+ ****************************************************************************/
+
+/* Radio transceiver modules */
+
+enum rn2xx3_type_e
+{
+  RN2903 = 0, /* RN2903 transceiver */
+  RN2483 = 1, /* RN2483 transceiver */
+};
+
+struct rn2xx3_dev_s
+{
+  FAR struct file uart;     /* UART interface */
+  bool receiving;           /* Currently receiving */
+  mutex_t devlock;          /* Exclusive access */
+  enum rn2xx3_type_e model; /* Transceiver model */
+  enum rn2xx3_mod_e mod;    /* Modulation mode */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  int16_t crefs; /* Number of open references */
+#endif
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+static int rn2xx3_open(FAR struct file *filep);
+static int rn2xx3_close(FAR struct file *filep);
+#endif
+static ssize_t rn2xx3_write(FAR struct file *filep, FAR const char *buffer,
+                            size_t buflen);
+static ssize_t rn2xx3_read(FAR struct file *filep, FAR char *buffer,
+                           size_t buflen);
+static int rn2xx3_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct file_operations g_rn2xx3fops =
+{
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  .open = rn2xx3_open,
+  .close = rn2xx3_close,
+#else
+  .open = NULL,
+  .close = NULL,
+#endif
+  .read = rn2xx3_read,
+  .write = rn2xx3_write,
+  .ioctl = rn2xx3_ioctl,
+};
+
+/* Modulation types */
+
+static const char *MODULATIONS[] =
+{
+  "lora", /* RN2XX3_MOD_LORA */
+  "fsk",  /* RN2XX3_MOD_FSK */
+};
+
+/* Max packet sizes for each modulation type. */
+
+static const uint8_t MAX_PKTSIZE[] =
+{
+  255, /* RN2XX3_MOD_LORA */
+  64,  /* RN2XX3_MOD_FSK */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: radio_flush
+ *
+ * Description:
+ *   Flushes all characters in the receive buffer from the RN2xx3 radio.
+ *   Returns the number of bytes read.
+ *
+ ****************************************************************************/
+
+static int radio_flush(FAR struct rn2xx3_dev_s *priv)
+{
+  int err;
+  int to_read;
+  char buf[10];
+
+  err = file_ioctl(&priv->uart, FIONREAD, &to_read);
+  if (err < 0)
+    {
+      return err;
+    }
+
+  while (to_read > 0)
+    {
+      err = file_read(&priv->uart, buf, sizeof(buf));
+      if (err < 0)
+        {
+          return err;
+        }
+
+      to_read -= err;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: read_line
+ *
+ * Description:
+ *   Reads a whole line of response from the RN2xx3 radio. Returns the number
+ *   of bytes read, excluding the terminating \r\n and null terminator.
+ *
+ ****************************************************************************/
+
+static ssize_t read_line(FAR struct rn2xx3_dev_s *priv, FAR char *buf,
+                         size_t nbytes)
+{
+  ssize_t length = 0;
+  ssize_t i = 0;
+  bool line_complete = false;
+
+  for (; i <= nbytes; )
+    {
+      length = file_read(&priv->uart, &buf[i], nbytes - i);
+
+      if (length < 0)
+        {
+          return length;
+        }
+
+      i += length;
+
+      /* Check if the character we just read was '\n'. This only occurs when
+       * the transceiver's response is complete. Length check ensures that
+       * we're not checking uninitialized memory in the case that `length` is
+       * 0.
+       */
+
+      if (length > 0 && buf[i - 1] == '\n')
+        {
+          line_complete = true;
+          break;
+        }
+    }
+
+  /* Insufficient buffer space to handle response */
+
+  if (!line_complete)
+    {
+      return -ENOBUFS;
+    }
+
+  /* Overwrite preceding \r with null terminator */
+
+  buf[i - 2] = '\0';
+  return i - 2; /* Number of bytes read excluding \r\n */
+}
+
+/****************************************************************************
+ * Name: mac_pause
+ *
+ * Description:
+ *   Pauses the MAC layer. Required before every transmission/receive.
+ *
+ ****************************************************************************/
+
+static int mac_pause(FAR struct rn2xx3_dev_s *priv)
+{
+  ssize_t length = 0;
+  char response[30];
+
+  /* Issue pause command */
+
+  length =
+      file_write(&priv->uart, "mac pause\r\n", sizeof("mac pause\r\n") - 1);
+  if (length < 0)
+    {
+      return length;
+    }
+
+  /* Wait for response of watchdog timer timeout */
+
+  length = read_line(priv, response, sizeof(response));
+  if (length < 0)
+    {
+      return length;
+    }
+
+  /* Check for pause duration */
+
+  if (strstr(response, MAC_PAUSE_DUR) == NULL)
+    {
+      return -EIO;
+    }
+
+  return 0;
+};
+
+/****************************************************************************
+ * Name: get_command_err
+ *
+ * Description:
+ *   Parses the command error response from the radio module and converts it
+ *   into an error code. 0 for 'ok', -EINVAL for 'invalid_param' and -EBUSY
+ *   for 'busy'. If an unknown error occurs, -EIO is returned.
+ *
+ ****************************************************************************/
+
+static int get_command_err(FAR struct rn2xx3_dev_s *priv)
+{
+  char response[18];
+  ssize_t length;
+
+  length = read_line(priv, response, sizeof(response));
+  if (length < 0)
+    {
+      return length;
+    }
+
+  if (strstr(response, "ok"))
+    {
+      /* Do nothing, this is good */
+
+      return 0;
+    }
+  else if (strstr(response, "invalid_param"))
+    {
+      wlerr("RN2xx3 invalid_param\n");
+      return -EINVAL;
+    }
+  else if (strstr(response, "busy"))
+    {
+      wlerr("RN2xx3 busy");
+      return -EBUSY;
+    }
+
+  wlerr("Unknown error");
+  return -EIO;
+}
+
+/****************************************************************************
+ * Name: rn2903_txpwrlevel
+ *
+ * Description:
+ *    Get the transmission power level that is greater than or equal to the
+ *    requested transmission power in dBm. These levels are for the RN2903.
+ *    The true dBm level that was set is returned in the `dbm` pointer.
+ *
+ ****************************************************************************/
+
+static int8_t rn2903_txpwrlevel(FAR float *dbm)

Review Comment:
   are floats really required here? it's not friendly for MCU without FPU. You 
should avoid floats in shared code and use something more universal if 
possible, like `fixedmath.h`



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

Reply via email to