diff --git a/drivers/at/at.c b/drivers/at/at.c index b521b7f3d4..d02a095cba 100644 --- a/drivers/at/at.c +++ b/drivers/at/at.c @@ -80,6 +80,34 @@ ssize_t at_recv_bytes(at_dev_t *dev, char *bytes, size_t len, uint32_t timeout) return (resp_pos - bytes); } +int at_recv_bytes_until_string(at_dev_t *dev, const char *string, + char *bytes, size_t *bytes_len, uint32_t timeout) +{ + size_t len = 0; + char *_string = (char *)string; + + while (*_string && len < *bytes_len) { + int res; + char c; + if ((res = isrpipe_read_timeout(&dev->isrpipe, &c, 1, timeout)) == 1) { + if (AT_PRINT_INCOMING) { + print(&c, 1); + } + if (c == *_string) { + _string++; + } + bytes[len] = c; + len++; + } + else { + *bytes_len = len; + return res; + } + } + *bytes_len = len; + return 0; +} + 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 42132ed5ff..5349dfd359 100644 --- a/drivers/include/at.h +++ b/drivers/include/at.h @@ -211,6 +211,25 @@ 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, uint32_t timeout); +/** + * @brief Receives bytes into @p bytes buffer until the string pattern + * @p string is received or the buffer is full. + * + * @param[in] dev device to operate on + * @param[in] string string pattern to expect + * @param[out] bytes buffer to store received bytes + * @param[in, out] bytes_len pointer to the maximum number of bytes to + * receive. On return stores the amount of received + * bytes. + * @param[in] timeout timeout (in usec) of inactivity to finish read + * + * @returns 0 on success + * @returns <0 on error + */ +int at_recv_bytes_until_string(at_dev_t *dev, const char *string, + char *bytes, size_t *bytes_len, + uint32_t timeout); + /** * @brief Send raw bytes to a device *