/* * Copyright (c) 2022 Avnet * Author: Guo Wenxue * * This library is free software; you can redistribute it and/or modify it * under the terms of the GPL license. */ #ifndef _ATCMD_H_ #define _ATCMD_H_ #include "comport.h" #include "logger.h" /* AT command common reply message max length */ #define ATCMD_REPLY_LEN 512 /* Max AT command reply message length */ #define ATCMD_LEN 128 /* Max AT command length */ /* AT command reply message got expect or error string */ #define AT_OKSTR "\r\nOK\r\n" /* expect string always be OK */ #define AT_ERRSTR "\r\nERROR\r\n" /* error string always be ERROR */ /* AT command should be end by $AT_SUFFIX */ #define AT_SUFFIX "\r\n" /* send_atcmd() return value status */ enum { ATRES_EXPECT, /* AT command reply got expect string, such as "OK\r\n" */ ATRES_ERROR, /* AT command reply got error string, such as "ERROR\r\n" */ ATRES_TIMEOUT, /* AT command not get error/expect string, receive until timeout */ }; /* Description: this function used to send an AT command from serial port and wait for reply message from * the communication module, and it will return once get expect/error string or timeout. * * Arugments: * $comport: the serial port which connected to GPRS/3G/4G/NB-IoT/WiFi/BLE/Zigbee/LoRa... * $at: the AT command need to be sent, without "\r\n" * $timeout: wait for module reply for AT command timeout value, unit micro seconds(ms) * $exepct: the expect string reply from communication module * $error: the error string reply from communication module * $reply: the module reply message output buffer * $size: the output buffer ($reply) size * * Return value: <0: Function error 0: Got expect string 1: Got error string 2: Receive until timeout * */ int send_atcmd(comport_t *comport, char *at, unsigned long timeout, char *expect, char *error, char *reply, int size); /* * Description: Send AT command which will only reply by "OK" or "ERROR", such as AT: * Reply: \r\nOK\r\n * * Return Value: 0: OK -X: ERROR */ int send_atcmd_check_ok(comport_t *comport, char *at, unsigned long timeout); /* * Description: Send AT command which will reply by a value directly in a single line, such as AT+CGMM: * Reply: \r\nEC20F\r\nOK\r\n * * Return Value: 0: OK -X: ERROR */ int send_atcmd_check_value(comport_t *comport, char *at, unsigned long timeout, char *reply, int size); /* * Description: Parser the $value from $key like "xxx: " line, such as AT+CSQ: * Reply: \r\n+CSQ: 26,99\r\nOK\r\n * * Return Value: 0: OK -X: Failure */ int parser_request_value(char *buf, char *key, char *value, int size); /* * Description: Send AT command which will reply by the value with a prefix "+CMD: " line, such as AT+CSQ: * Reply: \r\n+CSQ: 26,99\r\nOK\r\n * * Return Value: 0: OK -X: ERROR */ int send_atcmd_check_request(comport_t *comport, char *at, unsigned long timeout, char *repy, int size); #endif /* ----- #ifndef _ATCMD_H_ ----- */