1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers/at: add option to keep CR char

This commit is contained in:
Vincent Dupont 2018-05-23 10:37:42 +02:00
parent 9657274d0d
commit 3f94bd20f4
2 changed files with 22 additions and 12 deletions

View File

@ -101,10 +101,10 @@ ssize_t at_send_cmd_get_resp(at_dev_t *dev, const char *command,
goto out;
}
res = at_readline(dev, resp_buf, len, timeout);
res = at_readline(dev, resp_buf, len, false, timeout);
if (res == 0) {
/* skip possible empty line */
res = at_readline(dev, resp_buf, len, timeout);
res = at_readline(dev, resp_buf, len, false, timeout);
}
out:
@ -112,7 +112,7 @@ out:
}
ssize_t at_send_cmd_get_lines(at_dev_t *dev, const char *command,
char *resp_buf, size_t len, uint32_t timeout)
char *resp_buf, size_t len, bool keep_eol, uint32_t timeout)
{
ssize_t res;
size_t bytes_left = len - 1;
@ -128,17 +128,21 @@ ssize_t at_send_cmd_get_lines(at_dev_t *dev, const char *command,
}
while (1) {
res = at_readline(dev, pos, bytes_left, timeout);
res = at_readline(dev, pos, bytes_left, keep_eol, timeout);
if (res == 0) {
if (bytes_left) {
*pos++ = '\n';
bytes_left--;
}
continue;
}
else if (res > 0) {
bytes_left -= res;
if ((res == 2) && (strncmp(pos, "OK", 2) == 0)) {
if ((res == (2 + keep_eol)) && (strncmp(pos, "OK", 2) == 0)) {
res = len - bytes_left;
break;
}
else if ((res == 5) && (strncmp(pos, "ERROR", 5) == 0)) {
else if ((res == (5 + keep_eol)) && (strncmp(pos, "ERROR", 5) == 0)) {
return -1;
}
else if (strncmp(pos, "+CME ERROR:", 11) == 0) {
@ -209,7 +213,7 @@ int at_send_cmd_wait_ok(at_dev_t *dev, const char *command, uint32_t timeout)
return res;
}
ssize_t at_readline(at_dev_t *dev, char *resp_buf, size_t len, uint32_t timeout)
ssize_t at_readline(at_dev_t *dev, char *resp_buf, size_t len, bool keep_eol, uint32_t timeout)
{
ssize_t res = -1;
char *resp_pos = resp_buf;
@ -223,7 +227,9 @@ ssize_t at_readline(at_dev_t *dev, char *resp_buf, size_t len, uint32_t timeout)
print(resp_pos, read_res);
}
if (*resp_pos == '\r') {
continue;
if (!keep_eol) {
continue;
}
}
if (*resp_pos == '\n') {
*resp_pos = '\0';

View File

@ -35,6 +35,7 @@
#include <stdint.h>
#include <unistd.h>
#include <stdbool.h>
#include "isrpipe.h"
#include "periph/uart.h"
@ -138,13 +139,15 @@ ssize_t at_send_cmd_get_resp(at_dev_t *dev, const char *command, char *resp_buf,
* @param[in] dev device to operate on
* @param[in] command command to send
* @param[out] resp_buf buffer for storing response
* @param[in] len len of @p buffer
* @param[in] len len of @p resp_buf
* @param[in] keep_eol true to keep the CR character in the response
* @param[in] timeout timeout (in usec)
*
* @returns length of response on success
* @returns <0 on error
*/
ssize_t at_send_cmd_get_lines(at_dev_t *dev, const char *command, char *resp_buf, size_t len, uint32_t timeout);
ssize_t at_send_cmd_get_lines(at_dev_t *dev, const char *command, char *resp_buf,
size_t len, bool keep_eol, uint32_t timeout);
/**
* @brief Expect bytes from device
@ -184,13 +187,14 @@ int at_send_cmd(at_dev_t *dev, const char *command, uint32_t timeout);
*
* @param[in] dev device to operate on
* @param[in] resp_buf buffer to store line
* @param[in] len size of @p buffer
* @param[in] len size of @p resp_buf
* @param[in] keep_eol true to keep the CR character in the response
* @param[in] timeout timeout (in usec)
*
* @returns line length on success
* @returns <0 on error
*/
ssize_t at_readline(at_dev_t *dev, char *resp_buf, size_t len, uint32_t timeout);
ssize_t at_readline(at_dev_t *dev, char *resp_buf, size_t len, bool keep_eol, uint32_t timeout);
/**
* @brief Drain device input buffer