From e34d46e0966a488ce8cb6cb801d97e59d4325fc7 Mon Sep 17 00:00:00 2001
From: Federico Pellegrin <fede@evolware.org>
Date: Thu, 18 Oct 2018 11:25:50 +0200
Subject: [PATCH] drivers/at: add function to read raw data bytes from modem

---
 drivers/at/at.c        | 18 ++++++++++++++++++
 drivers/include/at.h   | 12 ++++++++++++
 tests/driver_at/main.c | 20 ++++++++++++++++++++
 3 files changed, 50 insertions(+)

diff --git a/drivers/at/at.c b/drivers/at/at.c
index 84ad661afc..f7c743d972 100644
--- a/drivers/at/at.c
+++ b/drivers/at/at.c
@@ -58,6 +58,24 @@ void at_send_bytes(at_dev_t *dev, const char *bytes, size_t len)
     uart_write(dev->uart, (const uint8_t *)bytes, len);
 }
 
+ssize_t at_recv_bytes(at_dev_t *dev, char *bytes, size_t len, uint32_t timeout)
+{
+    char *resp_pos = bytes;
+
+    while (len) {
+        int read_res;
+        if ((read_res = isrpipe_read_timeout(&dev->isrpipe, resp_pos, 1, timeout)) == 1) {
+            resp_pos += read_res;
+            len -= read_res;
+        }
+        else if (read_res == -ETIMEDOUT) {
+            break;
+        }
+    }
+
+    return (resp_pos - bytes);
+}
+
 int at_send_cmd(at_dev_t *dev, const char *command, uint32_t timeout)
 {
     size_t cmdlen = strlen(command);
diff --git a/drivers/include/at.h b/drivers/include/at.h
index efb0021348..a882b7cf73 100644
--- a/drivers/include/at.h
+++ b/drivers/include/at.h
@@ -220,6 +220,18 @@ int at_expect_bytes(at_dev_t *dev, const char *bytes, uint32_t timeout);
  */
 void at_send_bytes(at_dev_t *dev, const char *bytes, size_t len);
 
+/**
+ * @brief   Receive raw bytes from a device
+ *
+ * @param[in]   dev     device to operate on
+ * @param[in]   bytes   buffer where store received bytes
+ * @param[in]   len     maximum number of bytes to receive
+ * @param[in]   timeout timeout (in usec) of inactivity to finish read
+ *
+ * @returns     Number of bytes read, eventually zero if no bytes available
+ */
+ssize_t at_recv_bytes(at_dev_t *dev, char *bytes, size_t len, uint32_t timeout);
+
 /**
  * @brief   Send command to device
  *
diff --git a/tests/driver_at/main.c b/tests/driver_at/main.c
index 3593db60cf..1aeb776765 100644
--- a/tests/driver_at/main.c
+++ b/tests/driver_at/main.c
@@ -105,6 +105,25 @@ static int send_lines(int argc, char **argv)
     return 0;
 }
 
+static int send_recv_bytes(int argc, char **argv)
+{
+    char buffer[64];
+
+    if (argc < 3) {
+        printf("Usage: %s <command> <number of bytes>\n", argv[0]);
+        return 1;
+    }
+
+    sprintf(buffer, "%s%s", argv[1], AT_SEND_EOL);
+    at_send_bytes(&at_dev, buffer, strlen(buffer));
+
+    ssize_t len = at_recv_bytes(&at_dev, buffer, atoi(argv[2]), 10 * US_PER_SEC);
+
+    printf("Response (len=%d): %s\n", (int)len, buffer);
+
+    return 0;
+}
+
 static int drain(int argc, char **argv)
 {
     (void)argc;
@@ -228,6 +247,7 @@ static const shell_command_t shell_commands[] = {
     { "send", "Send a command and wait response", send },
     { "send_ok", "Send a command and wait OK", send_ok },
     { "send_lines", "Send a command and wait lines", send_lines },
+    { "send_recv_bytes", "Send a command and wait response as raw bytes", send_recv_bytes },
     { "drain", "Drain AT device", drain },
     { "power_on", "Power on AT device", power_on },
     { "power_off", "Power off AT device", power_off },
-- 
GitLab