From 7340e773286acc2067f749abe51c5bea801696d3 Mon Sep 17 00:00:00 2001
From: Vincent Dupont <vincent@otakeys.com>
Date: Thu, 13 Jul 2017 18:44:28 +0200
Subject: [PATCH] drivers/at: add at_send_cmd_wait_prompt and at_send_bytes

---
 drivers/at/at.c      | 29 +++++++++++++++++++++++++++++
 drivers/include/at.h | 24 ++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/drivers/at/at.c b/drivers/at/at.c
index ff0e9e2583..aff5757dca 100644
--- a/drivers/at/at.c
+++ b/drivers/at/at.c
@@ -54,6 +54,11 @@ int at_expect_bytes(at_dev_t *dev, const char *bytes, size_t len, uint32_t timeo
     return 0;
 }
 
+void at_send_bytes(at_dev_t *dev, const char *bytes, size_t len)
+{
+    uart_write(dev->uart, (const uint8_t *)bytes, len);
+}
+
 int at_send_cmd(at_dev_t *dev, const char *command, uint32_t timeout)
 {
     unsigned cmdlen = strlen(command);
@@ -155,6 +160,30 @@ out:
     return res;
 }
 
+int at_send_cmd_wait_prompt(at_dev_t *dev, const char *command, uint32_t timeout)
+{
+    unsigned cmdlen = strlen(command);
+
+    at_drain(dev);
+
+    uart_write(dev->uart, (const uint8_t *)command, cmdlen);
+    uart_write(dev->uart, (const uint8_t *)AT_END_OF_LINE, sizeof(AT_END_OF_LINE) - 1);
+
+    if (at_expect_bytes(dev, command, cmdlen, timeout)) {
+        return -1;
+    }
+
+    if (at_expect_bytes(dev, AT_END_OF_LINE "\n", sizeof(AT_END_OF_LINE), timeout)) {
+        return -2;
+    }
+
+    if (at_expect_bytes(dev, ">", 1, timeout)) {
+        return -3;
+    }
+
+    return 0;
+}
+
 int at_send_cmd_wait_ok(at_dev_t *dev, const char *command, uint32_t timeout)
 {
     int res;
diff --git a/drivers/include/at.h b/drivers/include/at.h
index a073ef3dc6..37608bee9d 100644
--- a/drivers/include/at.h
+++ b/drivers/include/at.h
@@ -84,6 +84,21 @@ int at_dev_init(at_dev_t *dev, uart_t uart, uint32_t baudrate, char *buf, size_t
  */
 int at_send_cmd_wait_ok(at_dev_t *dev, const char *command, uint32_t timeout);
 
+/**
+ * @brief   send AT command, wait for a prompt
+ *
+ * This function will send the supplied @p command, then wait for the prompt (>)
+ * character and return
+ *
+ * @param[in]   dev     device to operate on
+ * @param[in]   command command string to send
+ * @param[in]   timeout timeout (in usec)
+ *
+ * @return      0 when prompt is received
+ * @return      <0 otherwise
+ */
+int at_send_cmd_wait_prompt(at_dev_t *dev, const char *command, uint32_t timeout);
+
 /**
  * @brief   send AT command, wait for response
  *
@@ -136,6 +151,15 @@ ssize_t at_send_cmd_get_lines(at_dev_t *dev, const char *command, char *resp_buf
  */
 int at_expect_bytes(at_dev_t *dev, const char *bytes, size_t len, uint32_t timeout);
 
+/**
+ * @brief  Send raw bytes to a device
+ *
+ * @param[in]   dev     device to operate on
+ * @param[in]   bytes   buffer containing bytes to send
+ * @param[in]   len     number of bytes to send
+ */
+void at_send_bytes(at_dev_t *dev, const char *bytes, size_t len);
+
 /**
  * @brief   send command to device
  *
-- 
GitLab