|
| 1 | +/* ------------------------------------------ |
| 2 | + * Copyright (c) 2018, Synopsys, Inc. All rights reserved. |
| 3 | +
|
| 4 | + * Redistribution and use in source and binary forms, with or without modification, |
| 5 | + * are permitted provided that the following conditions are met: |
| 6 | +
|
| 7 | + * 1) Redistributions of source code must retain the above copyright notice, this |
| 8 | + * list of conditions and the following disclaimer. |
| 9 | +
|
| 10 | + * 2) Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation and/or |
| 12 | + * other materials provided with the distribution. |
| 13 | +
|
| 14 | + * 3) Neither the name of the Synopsys, Inc., nor the names of its contributors may |
| 15 | + * be used to endorse or promote products derived from this software without |
| 16 | + * specific prior written permission. |
| 17 | +
|
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 22 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 25 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * |
| 29 | +--------------------------------------------- */ |
| 30 | + |
| 31 | +#include "embARC_error.h" |
| 32 | +#include "string.h" |
| 33 | +#include "board.h" |
| 34 | +#include "at_parser.h" |
| 35 | +//#include "embARC_error.h" |
| 36 | + |
| 37 | +#define DBG_MORE |
| 38 | +#include "embARC_debug.h" |
| 39 | + |
| 40 | +/* if \r\n is needed to be attached at the end of AT comand, define AT_ADD_POSTFIX |
| 41 | +* otherwise comment out this line |
| 42 | +*/ |
| 43 | +#define AT_ADD_POSTFIX |
| 44 | + |
| 45 | +#define AT_PREFIX "AT" |
| 46 | +#define AT_POSTFIX "\r\n" |
| 47 | + |
| 48 | +#define AT_TIMENOW() OSP_GET_CUR_MS() |
| 49 | + |
| 50 | +#define AT_MAX_LEN 128 |
| 51 | +#define AT_MAX_PARAMETER 8 |
| 52 | + |
| 53 | + |
| 54 | +int32_t at_parser_init(AT_PARSER_DEF_PTR obj, uint32_t baudrate){ |
| 55 | + obj->psio = ez_sio_open(obj->uart_id, baudrate, AT_TX_BUFSIZE, AT_RX_BUFSIZE); |
| 56 | + dbg_printf(DBG_MORE_INFO, "[%s]%d: obj->psio 0x%x -> 0x%x\r\n", __FUNCTION__, __LINE__, obj->psio, *obj->psio); |
| 57 | + return (obj->psio != NULL) ? AT_OK : AT_ERROR; |
| 58 | +} |
| 59 | + |
| 60 | +void at_parser_deinit(AT_PARSER_DEF_PTR obj){ |
| 61 | + ez_sio_close(obj->psio); |
| 62 | + return; |
| 63 | +} |
| 64 | + |
| 65 | +int32_t at_read(AT_PARSER_DEF_PTR obj, char *buf, uint32_t cnt) |
| 66 | +{ |
| 67 | + return ez_sio_read(obj->psio, buf, cnt); |
| 68 | +} |
| 69 | + |
| 70 | +int32_t at_write(AT_PARSER_DEF_PTR obj, char *buf, uint32_t cnt) |
| 71 | +{ |
| 72 | + return ez_sio_write(obj->psio, buf, cnt); |
| 73 | +} |
| 74 | + |
| 75 | +/* |
| 76 | +* please use NULL as last parameter |
| 77 | +*/ |
| 78 | +int32_t at_send_cmd(AT_PARSER_DEF_PTR obj, AT_MODE mode, AT_STRING command, ...){ |
| 79 | + va_list vl; |
| 80 | + char at_out[AT_MAX_LEN] = AT_PREFIX; |
| 81 | + char * str = command; |
| 82 | + if(str == NULL){ |
| 83 | + dbg_printf(DBG_MORE_INFO, "[%s]%d: command is NULL, send AT test command\r\n", __FUNCTION__, __LINE__); |
| 84 | + } else { |
| 85 | + strcat(at_out,"+"); |
| 86 | + strcat(at_out, command); |
| 87 | + switch(mode){ |
| 88 | + case AT_LIST: |
| 89 | + strcat(at_out, "=?"); |
| 90 | + break; |
| 91 | + case AT_READ: |
| 92 | + strcat(at_out, "?"); |
| 93 | + break; |
| 94 | + case AT_WRITE: |
| 95 | + strcat(at_out, "="); |
| 96 | + va_start(vl, command); |
| 97 | + for(int i = 0; i < AT_MAX_PARAMETER; i++){ |
| 98 | + str = va_arg(vl, AT_STRING); |
| 99 | + if(str == NULL){ |
| 100 | + break; |
| 101 | + } |
| 102 | + if(i != 0){ |
| 103 | + strcat(at_out, ","); |
| 104 | + } |
| 105 | + strcat(at_out, str); |
| 106 | + } |
| 107 | + va_end(vl); |
| 108 | + break; |
| 109 | + case AT_EXECUTE: |
| 110 | + default: |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | +#ifdef AT_ADD_POSTFIX |
| 115 | + strcat(at_out, AT_POSTFIX); |
| 116 | +#endif /*AT_ADD_POSTFIX*/ |
| 117 | + dbg_printf(DBG_LESS_INFO, "[%s]%d: at_out: \"%s\" (%d)\r\n", __FUNCTION__, __LINE__, at_out, strlen(at_out)); |
| 118 | + return at_write(obj, at_out, strlen(at_out)); |
| 119 | +} |
| 120 | + |
| 121 | +/* make sure the buf is large enough*/ |
| 122 | +int32_t at_get_reply(AT_PARSER_DEF_PTR obj, char *buf, uint32_t timeout) |
| 123 | +{ |
| 124 | + uint32_t cur_ofs = 0; |
| 125 | + uint32_t read_cnt; |
| 126 | + uint32_t cur_time; |
| 127 | + cur_time = AT_TIMENOW(); |
| 128 | + do { |
| 129 | + read_cnt = at_read(obj, &buf[cur_ofs], 1); |
| 130 | + cur_ofs += read_cnt; |
| 131 | + buf[cur_ofs] = '\0'; |
| 132 | + if ((strstr(buf, AT_OK_STR)!= NULL) || (strstr(buf, AT_ERROR_STR)!= NULL)){ |
| 133 | + break; |
| 134 | + } |
| 135 | + } while((AT_TIMENOW()-cur_time) < timeout); |
| 136 | + buf[cur_ofs] = '\0'; |
| 137 | + dbg_printf(DBG_LESS_INFO, "[%s]%d: \"%s\" (%d)\r\n", __FUNCTION__, __LINE__, buf, strlen(buf)); |
| 138 | + if (strstr(buf, AT_OK_STR)!= NULL){ |
| 139 | + return AT_OK; |
| 140 | + } |
| 141 | + return AT_ERROR; |
| 142 | +} |
| 143 | + |
| 144 | +int32_t at_test(AT_PARSER_DEF_PTR obj){ |
| 145 | + char rcv_buf[64]; |
| 146 | + at_send_cmd(obj, AT_LIST, NULL); |
| 147 | + return at_get_reply(obj, rcv_buf, AT_NORMAL_TIMEOUT); |
| 148 | +} |
| 149 | + |
0 commit comments