Bluetooth UART support for the Adafruit BluefruitLE SPI, for the University of York Engineering Stage 1 project
Adafruit_ATParser.h@3:bdfd15be7b82, 2021-03-12 (annotated)
- Committer:
- ajp109
- Date:
- Fri Mar 12 14:35:25 2021 +0000
- Revision:
- 3:bdfd15be7b82
- Parent:
- 0:a80552d32b80
Remove readme
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ajp109 | 0:a80552d32b80 | 1 | /**************************************************************************/ |
ajp109 | 0:a80552d32b80 | 2 | /*! |
ajp109 | 0:a80552d32b80 | 3 | @file Adafruit_ATParser.h |
ajp109 | 0:a80552d32b80 | 4 | @author hathach |
ajp109 | 0:a80552d32b80 | 5 | |
ajp109 | 0:a80552d32b80 | 6 | @section LICENSE |
ajp109 | 0:a80552d32b80 | 7 | |
ajp109 | 0:a80552d32b80 | 8 | Software License Agreement (BSD License) |
ajp109 | 0:a80552d32b80 | 9 | |
ajp109 | 0:a80552d32b80 | 10 | Copyright (c) 2016, Adafruit Industries (adafruit.com) |
ajp109 | 0:a80552d32b80 | 11 | All rights reserved. |
ajp109 | 0:a80552d32b80 | 12 | |
ajp109 | 0:a80552d32b80 | 13 | Redistribution and use in source and binary forms, with or without |
ajp109 | 0:a80552d32b80 | 14 | modification, are permitted provided that the following conditions are met: |
ajp109 | 0:a80552d32b80 | 15 | 1. Redistributions of source code must retain the above copyright |
ajp109 | 0:a80552d32b80 | 16 | notice, this list of conditions and the following disclaimer. |
ajp109 | 0:a80552d32b80 | 17 | 2. Redistributions in binary form must reproduce the above copyright |
ajp109 | 0:a80552d32b80 | 18 | notice, this list of conditions and the following disclaimer in the |
ajp109 | 0:a80552d32b80 | 19 | documentation and/or other materials provided with the distribution. |
ajp109 | 0:a80552d32b80 | 20 | 3. Neither the name of the copyright holders nor the |
ajp109 | 0:a80552d32b80 | 21 | names of its contributors may be used to endorse or promote products |
ajp109 | 0:a80552d32b80 | 22 | derived from this software without specific prior written permission. |
ajp109 | 0:a80552d32b80 | 23 | |
ajp109 | 0:a80552d32b80 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
ajp109 | 0:a80552d32b80 | 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
ajp109 | 0:a80552d32b80 | 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
ajp109 | 0:a80552d32b80 | 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY |
ajp109 | 0:a80552d32b80 | 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
ajp109 | 0:a80552d32b80 | 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
ajp109 | 0:a80552d32b80 | 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
ajp109 | 0:a80552d32b80 | 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
ajp109 | 0:a80552d32b80 | 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
ajp109 | 0:a80552d32b80 | 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
ajp109 | 0:a80552d32b80 | 34 | */ |
ajp109 | 0:a80552d32b80 | 35 | /**************************************************************************/ |
ajp109 | 0:a80552d32b80 | 36 | |
ajp109 | 0:a80552d32b80 | 37 | #ifndef _ADAFRUIT_ATPARSER_H_ |
ajp109 | 0:a80552d32b80 | 38 | #define _ADAFRUIT_ATPARSER_H_ |
ajp109 | 0:a80552d32b80 | 39 | |
ajp109 | 0:a80552d32b80 | 40 | #include "mbed.h" |
ajp109 | 0:a80552d32b80 | 41 | #include "platform/Stream.h" |
ajp109 | 0:a80552d32b80 | 42 | #include "utility/sdep.h" |
ajp109 | 0:a80552d32b80 | 43 | |
ajp109 | 0:a80552d32b80 | 44 | // Class to facilitate sending AT Command and check response |
ajp109 | 0:a80552d32b80 | 45 | |
ajp109 | 0:a80552d32b80 | 46 | #define BLUEFRUIT_MODE_COMMAND true |
ajp109 | 0:a80552d32b80 | 47 | #define BLUEFRUIT_MODE_DATA false |
ajp109 | 0:a80552d32b80 | 48 | #define BLE_BUFSIZE 4*SDEP_MAX_PACKETSIZE |
ajp109 | 0:a80552d32b80 | 49 | |
ajp109 | 0:a80552d32b80 | 50 | #define SerialDebug Serial |
ajp109 | 0:a80552d32b80 | 51 | |
ajp109 | 0:a80552d32b80 | 52 | // High byte is type, Low byte is datacount |
ajp109 | 0:a80552d32b80 | 53 | // datacount is needed when passing ByteArray argument |
ajp109 | 0:a80552d32b80 | 54 | enum { |
ajp109 | 0:a80552d32b80 | 55 | AT_ARGTYPE_STRING = 0x0100, |
ajp109 | 0:a80552d32b80 | 56 | AT_ARGTYPE_BYTEARRAY = 0x0200, |
ajp109 | 0:a80552d32b80 | 57 | AT_ARGTYPE_INT32 = 0x0300, |
ajp109 | 0:a80552d32b80 | 58 | AT_ARGTYPE_UINT32 = 0x0400, |
ajp109 | 0:a80552d32b80 | 59 | AT_ARGTYPE_INT16 = 0x0500, |
ajp109 | 0:a80552d32b80 | 60 | AT_ARGTYPE_UINT16 = 0x0600, |
ajp109 | 0:a80552d32b80 | 61 | AT_ARGTYPE_INT8 = 0x0700, |
ajp109 | 0:a80552d32b80 | 62 | AT_ARGTYPE_UINT8 = 0x0800, |
ajp109 | 0:a80552d32b80 | 63 | }; |
ajp109 | 0:a80552d32b80 | 64 | |
ajp109 | 0:a80552d32b80 | 65 | class Adafruit_ATParser : public Stream |
ajp109 | 0:a80552d32b80 | 66 | { |
ajp109 | 0:a80552d32b80 | 67 | protected: |
ajp109 | 0:a80552d32b80 | 68 | uint8_t _mode; |
ajp109 | 0:a80552d32b80 | 69 | bool _verbose; |
ajp109 | 0:a80552d32b80 | 70 | uint16_t _timeout = 1000; |
ajp109 | 0:a80552d32b80 | 71 | |
ajp109 | 0:a80552d32b80 | 72 | // internal function |
ajp109 | 0:a80552d32b80 | 73 | bool send_arg_get_resp(int32_t* reply, uint8_t argcount, uint16_t argtype[], uint32_t args[]); |
ajp109 | 0:a80552d32b80 | 74 | |
ajp109 | 0:a80552d32b80 | 75 | // Required internally - was part of Arduino stream interface |
ajp109 | 0:a80552d32b80 | 76 | virtual int available(void) = 0; |
ajp109 | 0:a80552d32b80 | 77 | |
ajp109 | 0:a80552d32b80 | 78 | |
ajp109 | 0:a80552d32b80 | 79 | public: |
ajp109 | 0:a80552d32b80 | 80 | Adafruit_ATParser(void); |
ajp109 | 0:a80552d32b80 | 81 | |
ajp109 | 0:a80552d32b80 | 82 | char buffer[BLE_BUFSIZE+1]; |
ajp109 | 0:a80552d32b80 | 83 | |
ajp109 | 0:a80552d32b80 | 84 | uint8_t getMode(void) |
ajp109 | 0:a80552d32b80 | 85 | { |
ajp109 | 0:a80552d32b80 | 86 | return _mode; |
ajp109 | 0:a80552d32b80 | 87 | } |
ajp109 | 0:a80552d32b80 | 88 | virtual bool setMode(uint8_t mode) = 0; |
ajp109 | 0:a80552d32b80 | 89 | |
ajp109 | 0:a80552d32b80 | 90 | // Auto print out TX & RX data to normal Serial |
ajp109 | 0:a80552d32b80 | 91 | void verbose(bool enable) |
ajp109 | 0:a80552d32b80 | 92 | { |
ajp109 | 0:a80552d32b80 | 93 | _verbose = enable; |
ajp109 | 0:a80552d32b80 | 94 | } |
ajp109 | 0:a80552d32b80 | 95 | |
ajp109 | 0:a80552d32b80 | 96 | bool atcommand_full(const char cmd[], int32_t* reply, uint8_t argcount, uint16_t argtype[], uint32_t args[]); |
ajp109 | 0:a80552d32b80 | 97 | |
ajp109 | 0:a80552d32b80 | 98 | //--------------------------------------------------------------------+ |
ajp109 | 0:a80552d32b80 | 99 | // Without Reply |
ajp109 | 0:a80552d32b80 | 100 | //--------------------------------------------------------------------+ |
ajp109 | 0:a80552d32b80 | 101 | bool atcommand(const char cmd[] ) |
ajp109 | 0:a80552d32b80 | 102 | { |
ajp109 | 0:a80552d32b80 | 103 | return this->atcommand_full(cmd, NULL, 0, NULL, NULL); |
ajp109 | 0:a80552d32b80 | 104 | } |
ajp109 | 0:a80552d32b80 | 105 | |
ajp109 | 0:a80552d32b80 | 106 | //------------- One integer argument -------------// |
ajp109 | 0:a80552d32b80 | 107 | bool atcommand(const char cmd[], int32_t para1) |
ajp109 | 0:a80552d32b80 | 108 | { |
ajp109 | 0:a80552d32b80 | 109 | uint16_t type[] = { AT_ARGTYPE_INT32 }; |
ajp109 | 0:a80552d32b80 | 110 | uint32_t args[] = { (uint32_t) para1 }; |
ajp109 | 0:a80552d32b80 | 111 | return this->atcommand_full(cmd, NULL, 1, type, args); |
ajp109 | 0:a80552d32b80 | 112 | } |
ajp109 | 0:a80552d32b80 | 113 | |
ajp109 | 0:a80552d32b80 | 114 | //------------- Two integer arguments -------------// |
ajp109 | 0:a80552d32b80 | 115 | bool atcommand(const char cmd[], int32_t para1, int32_t para2) |
ajp109 | 0:a80552d32b80 | 116 | { |
ajp109 | 0:a80552d32b80 | 117 | uint16_t type[] = { AT_ARGTYPE_INT32, AT_ARGTYPE_INT32 }; |
ajp109 | 0:a80552d32b80 | 118 | uint32_t args[] = { (uint32_t) para1, (uint32_t) para2 }; |
ajp109 | 0:a80552d32b80 | 119 | return this->atcommand_full(cmd, NULL, 2, type, args); |
ajp109 | 0:a80552d32b80 | 120 | } |
ajp109 | 0:a80552d32b80 | 121 | |
ajp109 | 0:a80552d32b80 | 122 | //------------- One ByteArray arguments -------------// |
ajp109 | 0:a80552d32b80 | 123 | bool atcommand(const char cmd[], const uint8_t bytearray[], uint16_t count) |
ajp109 | 0:a80552d32b80 | 124 | { |
ajp109 | 0:a80552d32b80 | 125 | uint16_t type[] = { (uint16_t) (AT_ARGTYPE_BYTEARRAY+count) }; |
ajp109 | 0:a80552d32b80 | 126 | uint32_t args[] = { (uint32_t) bytearray }; |
ajp109 | 0:a80552d32b80 | 127 | return this->atcommand_full(cmd, NULL, 1, type, args); |
ajp109 | 0:a80552d32b80 | 128 | } |
ajp109 | 0:a80552d32b80 | 129 | |
ajp109 | 0:a80552d32b80 | 130 | //------------- One String argument -------------// |
ajp109 | 0:a80552d32b80 | 131 | bool atcommand(const char cmd[], const char* str) |
ajp109 | 0:a80552d32b80 | 132 | { |
ajp109 | 0:a80552d32b80 | 133 | uint16_t type[] = { AT_ARGTYPE_STRING }; |
ajp109 | 0:a80552d32b80 | 134 | uint32_t args[] = { (uint32_t) str }; |
ajp109 | 0:a80552d32b80 | 135 | return this->atcommand_full(cmd, NULL, 1, type, args); |
ajp109 | 0:a80552d32b80 | 136 | } |
ajp109 | 0:a80552d32b80 | 137 | |
ajp109 | 0:a80552d32b80 | 138 | //--------------------------------------------------------------------+ |
ajp109 | 0:a80552d32b80 | 139 | // With Reply |
ajp109 | 0:a80552d32b80 | 140 | //--------------------------------------------------------------------+ |
ajp109 | 0:a80552d32b80 | 141 | bool atcommandIntReply(const char cmd[], int32_t* reply) |
ajp109 | 0:a80552d32b80 | 142 | { |
ajp109 | 0:a80552d32b80 | 143 | return this->atcommand_full(cmd, reply, 0, NULL, NULL); |
ajp109 | 0:a80552d32b80 | 144 | } |
ajp109 | 0:a80552d32b80 | 145 | |
ajp109 | 0:a80552d32b80 | 146 | uint16_t atcommandStrReply(const char cmd[], char* buf, uint16_t bufsize, uint16_t timeout); |
ajp109 | 0:a80552d32b80 | 147 | |
ajp109 | 0:a80552d32b80 | 148 | uint16_t atcommandStrReplyPerLine(const char cmd[], char* linebuf, uint16_t bufsize, uint16_t timeout, void (*line_callback)(void*, char*, uint16_t), void* callback_data); |
ajp109 | 0:a80552d32b80 | 149 | |
ajp109 | 0:a80552d32b80 | 150 | |
ajp109 | 0:a80552d32b80 | 151 | |
ajp109 | 0:a80552d32b80 | 152 | //------------- One integer argument -------------// |
ajp109 | 0:a80552d32b80 | 153 | bool atcommandIntReply(const char cmd[], int32_t* reply, int32_t para1) |
ajp109 | 0:a80552d32b80 | 154 | { |
ajp109 | 0:a80552d32b80 | 155 | uint16_t type[] = { AT_ARGTYPE_INT32 }; |
ajp109 | 0:a80552d32b80 | 156 | uint32_t args[] = { (uint32_t) para1 }; |
ajp109 | 0:a80552d32b80 | 157 | return this->atcommand_full(cmd, reply, 1, type, args); |
ajp109 | 0:a80552d32b80 | 158 | } |
ajp109 | 0:a80552d32b80 | 159 | |
ajp109 | 0:a80552d32b80 | 160 | //------------- Two integer arguments -------------// |
ajp109 | 0:a80552d32b80 | 161 | bool atcommandIntReply(const char cmd[], int32_t* reply, int32_t para1, int32_t para2) |
ajp109 | 0:a80552d32b80 | 162 | { |
ajp109 | 0:a80552d32b80 | 163 | uint16_t type[] = { AT_ARGTYPE_INT32, AT_ARGTYPE_INT32 }; |
ajp109 | 0:a80552d32b80 | 164 | uint32_t args[] = { (uint32_t) para1, (uint32_t) para2 }; |
ajp109 | 0:a80552d32b80 | 165 | return this->atcommand_full(cmd, reply, 2, type, args); |
ajp109 | 0:a80552d32b80 | 166 | } |
ajp109 | 0:a80552d32b80 | 167 | |
ajp109 | 0:a80552d32b80 | 168 | //------------- One ByteArray arguments -------------// |
ajp109 | 0:a80552d32b80 | 169 | bool atcommandIntReply(const char cmd[], int32_t* reply, const uint8_t bytearray[], uint16_t count) |
ajp109 | 0:a80552d32b80 | 170 | { |
ajp109 | 0:a80552d32b80 | 171 | uint16_t type[] = { (uint16_t) (AT_ARGTYPE_BYTEARRAY+count) }; |
ajp109 | 0:a80552d32b80 | 172 | uint32_t args[] = { (uint32_t) bytearray }; |
ajp109 | 0:a80552d32b80 | 173 | return this->atcommand_full(cmd, reply, 1, type, args); |
ajp109 | 0:a80552d32b80 | 174 | } |
ajp109 | 0:a80552d32b80 | 175 | |
ajp109 | 0:a80552d32b80 | 176 | //------------- One String argument -------------// |
ajp109 | 0:a80552d32b80 | 177 | bool atcommandIntReply(const char cmd[], int32_t* reply, const char* str) |
ajp109 | 0:a80552d32b80 | 178 | { |
ajp109 | 0:a80552d32b80 | 179 | uint16_t type[] = { AT_ARGTYPE_STRING }; |
ajp109 | 0:a80552d32b80 | 180 | uint32_t args[] = { (uint32_t) str }; |
ajp109 | 0:a80552d32b80 | 181 | return this->atcommand_full(cmd, reply, 1, type, args); |
ajp109 | 0:a80552d32b80 | 182 | } |
ajp109 | 0:a80552d32b80 | 183 | |
ajp109 | 0:a80552d32b80 | 184 | //--------------------------------------------------------------------+ |
ajp109 | 0:a80552d32b80 | 185 | // RESPONSE PROCESSING |
ajp109 | 0:a80552d32b80 | 186 | //--------------------------------------------------------------------+ |
ajp109 | 0:a80552d32b80 | 187 | bool waitForOK(void); |
ajp109 | 0:a80552d32b80 | 188 | |
ajp109 | 0:a80552d32b80 | 189 | // Read one line of response into internal buffer TODO use below API |
ajp109 | 0:a80552d32b80 | 190 | |
ajp109 | 0:a80552d32b80 | 191 | // Read one line of response into provided buffer |
ajp109 | 0:a80552d32b80 | 192 | uint16_t readline(char * buf, uint16_t bufsize, uint16_t timeout, bool multiline = false); |
ajp109 | 0:a80552d32b80 | 193 | uint16_t readline(uint8_t * buf, uint16_t bufsize, uint16_t timeout, bool multiline = false) |
ajp109 | 0:a80552d32b80 | 194 | { |
ajp109 | 0:a80552d32b80 | 195 | return readline( (char*) buf, bufsize, timeout, multiline ); |
ajp109 | 0:a80552d32b80 | 196 | } |
ajp109 | 0:a80552d32b80 | 197 | |
ajp109 | 0:a80552d32b80 | 198 | uint16_t readline(char * buf, uint16_t bufsize) |
ajp109 | 0:a80552d32b80 | 199 | { |
ajp109 | 0:a80552d32b80 | 200 | return readline(buf, bufsize, _timeout, false); |
ajp109 | 0:a80552d32b80 | 201 | } |
ajp109 | 0:a80552d32b80 | 202 | uint16_t readline(uint8_t * buf, uint16_t bufsize) |
ajp109 | 0:a80552d32b80 | 203 | { |
ajp109 | 0:a80552d32b80 | 204 | return readline(buf, bufsize, _timeout, false); |
ajp109 | 0:a80552d32b80 | 205 | } |
ajp109 | 0:a80552d32b80 | 206 | |
ajp109 | 0:a80552d32b80 | 207 | uint16_t readline(uint16_t timeout, bool multiline = false) |
ajp109 | 0:a80552d32b80 | 208 | { |
ajp109 | 0:a80552d32b80 | 209 | return readline(this->buffer, BLE_BUFSIZE, timeout, multiline); |
ajp109 | 0:a80552d32b80 | 210 | } |
ajp109 | 0:a80552d32b80 | 211 | |
ajp109 | 0:a80552d32b80 | 212 | uint16_t readline(void) |
ajp109 | 0:a80552d32b80 | 213 | { |
ajp109 | 0:a80552d32b80 | 214 | return this->readline(this->buffer, BLE_BUFSIZE, _timeout, false); |
ajp109 | 0:a80552d32b80 | 215 | } |
ajp109 | 0:a80552d32b80 | 216 | |
ajp109 | 0:a80552d32b80 | 217 | |
ajp109 | 0:a80552d32b80 | 218 | // read one line and convert the string to integer number |
ajp109 | 0:a80552d32b80 | 219 | int32_t readline_parseInt(void); |
ajp109 | 0:a80552d32b80 | 220 | |
ajp109 | 0:a80552d32b80 | 221 | uint16_t readraw(uint16_t timeout); |
ajp109 | 0:a80552d32b80 | 222 | uint16_t readraw(void) |
ajp109 | 0:a80552d32b80 | 223 | { |
ajp109 | 0:a80552d32b80 | 224 | return readraw(_timeout); |
ajp109 | 0:a80552d32b80 | 225 | } |
ajp109 | 0:a80552d32b80 | 226 | |
ajp109 | 0:a80552d32b80 | 227 | //--------------------------------------------------------------------+ |
ajp109 | 0:a80552d32b80 | 228 | // HELPER |
ajp109 | 0:a80552d32b80 | 229 | //--------------------------------------------------------------------+ |
ajp109 | 0:a80552d32b80 | 230 | int printByteArray(uint8_t const bytearray[], int size); |
ajp109 | 0:a80552d32b80 | 231 | }; |
ajp109 | 0:a80552d32b80 | 232 | |
ajp109 | 0:a80552d32b80 | 233 | #endif /* _ADAFRUIT_ATPARSER_H_ */ |