HXC Client Shield Repository.

Dependencies:   mbed

Committer:
kashish_mbed
Date:
Mon Apr 19 17:43:09 2021 +0000
Revision:
3:5e1a54378107
Parent:
0:bacc6e701fb4
Child:
2:52bb5ee0a72d
Successful Build file with CmdProcess Functionality

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kashish_mbed 0:bacc6e701fb4 1 /**
kashish_mbed 0:bacc6e701fb4 2 ******************************************************************************
kashish_mbed 0:bacc6e701fb4 3 * @file hxc_client.c
kashish_mbed 0:bacc6e701fb4 4 * @author Fahad (Haxiot)
kashish_mbed 0:bacc6e701fb4 5 * @version V1.0.0
kashish_mbed 0:bacc6e701fb4 6 * @date 15-July-2018
kashish_mbed 0:bacc6e701fb4 7 * @brief This file provides set of firmware functions to communicate
kashish_mbed 0:bacc6e701fb4 8 * with HXC Client using AT commands:
kashish_mbed 0:bacc6e701fb4 9 * - AT_SET
kashish_mbed 0:bacc6e701fb4 10 * - AT_GET
kashish_mbed 0:bacc6e701fb4 11 * - AT_RUN
kashish_mbed 0:bacc6e701fb4 12 ******************************************************************************
kashish_mbed 0:bacc6e701fb4 13 * @attention
kashish_mbed 0:bacc6e701fb4 14 *
kashish_mbed 0:bacc6e701fb4 15 * _ _ _____ _______
kashish_mbed 0:bacc6e701fb4 16 * | | | | |_ _| |__ __|
kashish_mbed 0:bacc6e701fb4 17 * | |__| | __ ___ __ | | ___ | |
kashish_mbed 0:bacc6e701fb4 18 * | __ |/ _` \ \/ / | | / _ \| |
kashish_mbed 0:bacc6e701fb4 19 * | | | | (_| |> < _| || (_) | |
kashish_mbed 0:bacc6e701fb4 20 * |_| |_|\__,_/_/\_\_____\___/|_|
kashish_mbed 0:bacc6e701fb4 21 * (C)2017 HaxIoT
kashish_mbed 0:bacc6e701fb4 22 *
kashish_mbed 0:bacc6e701fb4 23 *
kashish_mbed 0:bacc6e701fb4 24 * <h2><center>&copy; COPYRIGHT(c) 2017 Haxiot</center></h2>
kashish_mbed 0:bacc6e701fb4 25 *
kashish_mbed 0:bacc6e701fb4 26 * Redistribution and use in source and binary forms, with or without modification,
kashish_mbed 0:bacc6e701fb4 27 * are permitted provided that the following conditions are met:
kashish_mbed 0:bacc6e701fb4 28 * 1. Redistributions of source code must retain the above copyright notice,
kashish_mbed 0:bacc6e701fb4 29 * this list of conditions and the following disclaimer.
kashish_mbed 0:bacc6e701fb4 30 * 2. Redistributions in binary form must reproduce the above copyright notice,
kashish_mbed 0:bacc6e701fb4 31 * this list of conditions and the following disclaimer in the documentation
kashish_mbed 0:bacc6e701fb4 32 * and/or other materials provided with the distribution.
kashish_mbed 0:bacc6e701fb4 33 * 3. Neither the name of Haxiot nor the names of its contributors
kashish_mbed 0:bacc6e701fb4 34 * may be used to endorse or promote products derived from this software
kashish_mbed 0:bacc6e701fb4 35 * without specific prior written permission.
kashish_mbed 0:bacc6e701fb4 36 *
kashish_mbed 0:bacc6e701fb4 37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
kashish_mbed 0:bacc6e701fb4 38 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
kashish_mbed 0:bacc6e701fb4 39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
kashish_mbed 0:bacc6e701fb4 40 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
kashish_mbed 0:bacc6e701fb4 41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
kashish_mbed 0:bacc6e701fb4 42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
kashish_mbed 0:bacc6e701fb4 43 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
kashish_mbed 0:bacc6e701fb4 44 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
kashish_mbed 0:bacc6e701fb4 45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
kashish_mbed 0:bacc6e701fb4 46 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
kashish_mbed 0:bacc6e701fb4 47 *
kashish_mbed 0:bacc6e701fb4 48 ******************************************************************************
kashish_mbed 0:bacc6e701fb4 49 */
kashish_mbed 0:bacc6e701fb4 50
kashish_mbed 0:bacc6e701fb4 51 /* Includes ------------------------------------------------------------------*/
kashish_mbed 0:bacc6e701fb4 52 #include <stdbool.h>
kashish_mbed 0:bacc6e701fb4 53 #include <stdint.h>
kashish_mbed 0:bacc6e701fb4 54 #include <stdarg.h>
kashish_mbed 0:bacc6e701fb4 55 #include <string.h>
kashish_mbed 0:bacc6e701fb4 56 #include "hw_usart.h"
kashish_mbed 0:bacc6e701fb4 57 #include "utilities.h"
kashish_mbed 0:bacc6e701fb4 58 #include "tiny_vsnprintf.h"
kashish_mbed 0:bacc6e701fb4 59 #include "hxc_client.h"
kashish_mbed 0:bacc6e701fb4 60 #include "hw_conf.h"
kashish_mbed 0:bacc6e701fb4 61 #include "delay.h"
kashish_mbed 0:bacc6e701fb4 62 #include "time_server.h"
kashish_mbed 0:bacc6e701fb4 63 #include "hw_gpio.h"
kashish_mbed 0:bacc6e701fb4 64
kashish_mbed 0:bacc6e701fb4 65
kashish_mbed 0:bacc6e701fb4 66 /* Private macro -------------------------------------------------------------*/
kashish_mbed 0:bacc6e701fb4 67 #define AT_VPRINTF(...) at_cmd_vprintf(__VA_ARGS__)
kashish_mbed 0:bacc6e701fb4 68 #define AT_VSSCANF(...) tiny_sscanf(__VA_ARGS__)
kashish_mbed 0:bacc6e701fb4 69
kashish_mbed 0:bacc6e701fb4 70 #define BAUD_RATE 9600U
kashish_mbed 0:bacc6e701fb4 71 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
kashish_mbed 0:bacc6e701fb4 72
kashish_mbed 0:bacc6e701fb4 73 #define AT_RESPONSE_BUFF_SIZE 64U // Max size of the received buffer.
kashish_mbed 0:bacc6e701fb4 74 #define DATA_TX_MAX_BUFF_SIZE 78U // Max size of the transmit buffer
kashish_mbed 0:bacc6e701fb4 75 #define HXC_TIMEOUT 2000U // 2 seconds
kashish_mbed 0:bacc6e701fb4 76
kashish_mbed 0:bacc6e701fb4 77
kashish_mbed 0:bacc6e701fb4 78 // These strings will be used to compare the responses return from HXC modem.
kashish_mbed 0:bacc6e701fb4 79 // In direct relation with sAtRetCode_t
kashish_mbed 0:bacc6e701fb4 80 #define OK "OK"
kashish_mbed 0:bacc6e701fb4 81 #define ERROR "AT_ERROR"
kashish_mbed 0:bacc6e701fb4 82 #define PARAM_ERROR "AT_PARAM_ERROR"
kashish_mbed 0:bacc6e701fb4 83 #define BUSY_ERROR "AT_BUSY_ERROR"
kashish_mbed 0:bacc6e701fb4 84 #define PARAM_OVERFLOW "AT_PARAM_OVERFLOW"
kashish_mbed 0:bacc6e701fb4 85 #define INVALID_MODE "AT_INVALID_MODE"
kashish_mbed 0:bacc6e701fb4 86 #define NO_NETWORK_JOINED "AT_NO_NETWORK_JOINED"
kashish_mbed 0:bacc6e701fb4 87 #define PAYLOAD_SIZE_ERROR "AT_PAYLOAD_SIZE_ERROR"
kashish_mbed 0:bacc6e701fb4 88
kashish_mbed 0:bacc6e701fb4 89
kashish_mbed 0:bacc6e701fb4 90 /* Private typedef -----------------------------------------------------------*/
kashish_mbed 0:bacc6e701fb4 91 // Type definition for return code analysis
kashish_mbed 0:bacc6e701fb4 92 typedef struct sAtRetCode
kashish_mbed 0:bacc6e701fb4 93 {
kashish_mbed 0:bacc6e701fb4 94 char* RetCodeStr;
kashish_mbed 0:bacc6e701fb4 95 uint8_t SizeRetCodeStr;
kashish_mbed 0:bacc6e701fb4 96 eAtStatus_t RetCode;
kashish_mbed 0:bacc6e701fb4 97 }sAtRetCode_t;
kashish_mbed 0:bacc6e701fb4 98
kashish_mbed 0:bacc6e701fb4 99
kashish_mbed 0:bacc6e701fb4 100 /* Private functions ---------------------------------------------------------*/
kashish_mbed 0:bacc6e701fb4 101 static uint16_t at_cmd_format(ATGroup_t at_group, ATCmd_t Cmd, const void *ptr);
kashish_mbed 0:bacc6e701fb4 102 static uint16_t at_set_cmd_format(ATCmd_t Cmd, const void *ptr);
kashish_mbed 0:bacc6e701fb4 103 static eAtStatus_t at_cmd_send(uint16_t len);
kashish_mbed 0:bacc6e701fb4 104 static eAtStatus_t at_cmd_receive(void *pdata);
kashish_mbed 0:bacc6e701fb4 105 static eAtStatus_t at_cmd_analyzeResponse(const char *ReturnResp);
kashish_mbed 0:bacc6e701fb4 106 static uint16_t at_cmd_vprintf(const char *format, ...);
kashish_mbed 0:bacc6e701fb4 107
kashish_mbed 0:bacc6e701fb4 108
kashish_mbed 0:bacc6e701fb4 109 /* Private Variables --------------------------------------------------------*/
kashish_mbed 0:bacc6e701fb4 110 // NOTE: sizeof of a string take account of the NULL character too, unlike
kashish_mbed 0:bacc6e701fb4 111 // strlen(). Hence subtract one from the sizeof.
kashish_mbed 0:bacc6e701fb4 112 static const sAtRetCode_t AT_RetCode[] = {
kashish_mbed 0:bacc6e701fb4 113 {OK, sizeof(OK) - 1, AT_OK},
kashish_mbed 0:bacc6e701fb4 114 {ERROR, sizeof(ERROR) - 1, AT_ERROR},
kashish_mbed 0:bacc6e701fb4 115 {PARAM_ERROR, sizeof(PARAM_ERROR) - 1, AT_PARAM_ERROR},
kashish_mbed 0:bacc6e701fb4 116 {NO_NETWORK_JOINED, sizeof(NO_NETWORK_JOINED) - 1, AT_NO_NET_JOINED},
kashish_mbed 0:bacc6e701fb4 117 {BUSY_ERROR, sizeof(BUSY_ERROR) - 1, AT_BUSY_ERROR},
kashish_mbed 0:bacc6e701fb4 118 {PARAM_OVERFLOW, sizeof(PARAM_OVERFLOW) - 1, AT_PARAM_OVERFLOW},
kashish_mbed 0:bacc6e701fb4 119 {INVALID_MODE, sizeof(INVALID_MODE) - 1, AT_INVALID_MODE},
kashish_mbed 0:bacc6e701fb4 120 {PAYLOAD_SIZE_ERROR, sizeof(PAYLOAD_SIZE_ERROR) - 1, AT_PAYLOAD_SIZE_ERROR}};
kashish_mbed 0:bacc6e701fb4 121
kashish_mbed 0:bacc6e701fb4 122
kashish_mbed 0:bacc6e701fb4 123 /*
kashish_mbed 0:bacc6e701fb4 124 * List of AT cmd supported by the HXC Client Module:
kashish_mbed 0:bacc6e701fb4 125 * HXC900 and HXC400
kashish_mbed 0:bacc6e701fb4 126 */
kashish_mbed 0:bacc6e701fb4 127 static const char *CmdTab[] =
kashish_mbed 0:bacc6e701fb4 128 {
kashish_mbed 0:bacc6e701fb4 129 "", // AT
kashish_mbed 0:bacc6e701fb4 130 "+RESET", // Reset modem
kashish_mbed 0:bacc6e701fb4 131 "+FD", // Factory default
kashish_mbed 0:bacc6e701fb4 132 "+DEVEUI", // Device identifier
kashish_mbed 0:bacc6e701fb4 133 "+DEVADR", // Device address
kashish_mbed 0:bacc6e701fb4 134 "+APPKEY", // Application key
kashish_mbed 0:bacc6e701fb4 135 "+NWKSKEY", // Network session key
kashish_mbed 0:bacc6e701fb4 136 "+APPSKEY", // Application session key
kashish_mbed 0:bacc6e701fb4 137 "+APPEUI", // Application identifier
kashish_mbed 0:bacc6e701fb4 138 "+ADR", // Adaptive data rate
kashish_mbed 0:bacc6e701fb4 139 "+TXP", // Transmit power
kashish_mbed 0:bacc6e701fb4 140 "+DR", // Data rate
kashish_mbed 0:bacc6e701fb4 141 "+DCS", // DCS duty cycle settings
kashish_mbed 0:bacc6e701fb4 142 "+PNM", // Public/Private network
kashish_mbed 0:bacc6e701fb4 143 "+RX2WND", // Rx2 window frequency and datarate
kashish_mbed 0:bacc6e701fb4 144 "+RX1DL", // Delay of the Rx1 window
kashish_mbed 0:bacc6e701fb4 145 "+RX2DL", // Delay of the Rx2 window
kashish_mbed 0:bacc6e701fb4 146 "+JN1DL", // Join delay on Rx1 window
kashish_mbed 0:bacc6e701fb4 147 "+JN2DL", // Join delay on Rx2 window
kashish_mbed 0:bacc6e701fb4 148 "+NJM", // Network join mode
kashish_mbed 0:bacc6e701fb4 149 "+NWKID", // Network ID
kashish_mbed 0:bacc6e701fb4 150 "+FCU", // Uplink frame counter
kashish_mbed 0:bacc6e701fb4 151 "+FCD", // Downlink frame counter
kashish_mbed 0:bacc6e701fb4 152 "+CLASS", // LoRa class
kashish_mbed 0:bacc6e701fb4 153 "+CH", // Channel configuration
kashish_mbed 0:bacc6e701fb4 154 "+JOIN", // Join network server
kashish_mbed 0:bacc6e701fb4 155 "+NJS", // Network join mode
kashish_mbed 0:bacc6e701fb4 156 "+SENDB", // Send binary formatted data
kashish_mbed 0:bacc6e701fb4 157 "+SEND", // Send data in ASCII format
kashish_mbed 0:bacc6e701fb4 158 "+RECVB", // Get the last received data
kashish_mbed 0:bacc6e701fb4 159 "+CFS", // Confirm status
kashish_mbed 0:bacc6e701fb4 160 "+SNR", // Signal to noise ratio
kashish_mbed 0:bacc6e701fb4 161 "+RSSI", // Signal strength indicator on received radio signal
kashish_mbed 0:bacc6e701fb4 162 "+MODE", // Modem mode
kashish_mbed 0:bacc6e701fb4 163 "+RFCFG", // LoRa only configuration
kashish_mbed 0:bacc6e701fb4 164 "+TXCW", // Continuous Tx
kashish_mbed 0:bacc6e701fb4 165 "+TX", // Send LoRa only data in raw format
kashish_mbed 0:bacc6e701fb4 166 "+RX", // Continuous Rx
kashish_mbed 0:bacc6e701fb4 167 "+BAT", // Battery level
kashish_mbed 0:bacc6e701fb4 168 "+VER", // Firmware version of the HXC Client
kashish_mbed 0:bacc6e701fb4 169 };
kashish_mbed 0:bacc6e701fb4 170
kashish_mbed 0:bacc6e701fb4 171
kashish_mbed 0:bacc6e701fb4 172 static char AtCmdBuff[DATA_TX_MAX_BUFF_SIZE];
kashish_mbed 0:bacc6e701fb4 173 // Write position needed for AtCmdBuff[] during AT_SET
kashish_mbed 0:bacc6e701fb4 174 static uint16_t Offset = 0;
kashish_mbed 0:bacc6e701fb4 175
kashish_mbed 0:bacc6e701fb4 176 // Has to be the largest of the response e.g. APPKEY.
kashish_mbed 0:bacc6e701fb4 177 static char AtResponseBuff[AT_RESPONSE_BUFF_SIZE];
kashish_mbed 0:bacc6e701fb4 178
kashish_mbed 0:bacc6e701fb4 179
kashish_mbed 0:bacc6e701fb4 180 /* Exported functions ------------------------------------------------------- */
kashish_mbed 0:bacc6e701fb4 181
kashish_mbed 0:bacc6e701fb4 182 /*******************************************************************************
kashish_mbed 0:bacc6e701fb4 183 * @Brief : Configures HXC UART interface, Reset Pin
kashish_mbed 0:bacc6e701fb4 184 * @Param : None
kashish_mbed 0:bacc6e701fb4 185 * @Return : AT_OK in case of success
kashish_mbed 0:bacc6e701fb4 186 * AT_UART_LINK_ERROR in case of UART init failure
kashish_mbed 0:bacc6e701fb4 187 *******************************************************************************/
kashish_mbed 0:bacc6e701fb4 188 eAtStatus_t Modem_Init( void )
kashish_mbed 0:bacc6e701fb4 189 {
kashish_mbed 0:bacc6e701fb4 190 if (HW_UART_Modem_Init(BAUD_RATE) == false)
kashish_mbed 0:bacc6e701fb4 191 {
kashish_mbed 0:bacc6e701fb4 192 return AT_UART_LINK_ERROR;
kashish_mbed 0:bacc6e701fb4 193 }
kashish_mbed 0:bacc6e701fb4 194
kashish_mbed 0:bacc6e701fb4 195 // Reset pin initialization
kashish_mbed 0:bacc6e701fb4 196 HXC_RESET_GPIO_CLK_ENABLE();
kashish_mbed 0:bacc6e701fb4 197 GPIO_InitTypeDef GPIO_InitStruct;
kashish_mbed 0:bacc6e701fb4 198 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
kashish_mbed 0:bacc6e701fb4 199 GPIO_InitStruct.Pull = GPIO_PULLUP;
kashish_mbed 0:bacc6e701fb4 200 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
kashish_mbed 0:bacc6e701fb4 201 HW_GPIO_Init(HXC_RESET_PORT, HXC_RESET_PIN, &GPIO_InitStruct);
kashish_mbed 0:bacc6e701fb4 202
kashish_mbed 0:bacc6e701fb4 203 return AT_OK;
kashish_mbed 0:bacc6e701fb4 204 }
kashish_mbed 0:bacc6e701fb4 205
kashish_mbed 0:bacc6e701fb4 206
kashish_mbed 0:bacc6e701fb4 207 /*******************************************************************************
kashish_mbed 0:bacc6e701fb4 208 * @Brief : Deinitialize modem UART interface.
kashish_mbed 0:bacc6e701fb4 209 * @Param : None
kashish_mbed 0:bacc6e701fb4 210 * @Return : None
kashish_mbed 0:bacc6e701fb4 211 *******************************************************************************/
kashish_mbed 0:bacc6e701fb4 212 void Modem_IO_DeInit( void )
kashish_mbed 0:bacc6e701fb4 213 {
kashish_mbed 0:bacc6e701fb4 214 HW_UART_Modem_DeInit();
kashish_mbed 0:bacc6e701fb4 215 }
kashish_mbed 0:bacc6e701fb4 216
kashish_mbed 0:bacc6e701fb4 217
kashish_mbed 0:bacc6e701fb4 218 /*******************************************************************************
kashish_mbed 0:bacc6e701fb4 219 * @Brief : Handle the AT cmd following their Group type
kashish_mbed 0:bacc6e701fb4 220 * @Param : at_group AT group (control, set , get)
kashish_mbed 0:bacc6e701fb4 221 * Cmd AT command
kashish_mbed 0:bacc6e701fb4 222 * pdata pointer to the IN/OUT buffer
kashish_mbed 0:bacc6e701fb4 223 * @Return : Module status
kashish_mbed 0:bacc6e701fb4 224 ******************************************************************************/
kashish_mbed 0:bacc6e701fb4 225 eAtStatus_t Modem_AT_Cmd(ATGroup_t at_group, ATCmd_t atCmd, void *pdata )
kashish_mbed 0:bacc6e701fb4 226 {
kashish_mbed 0:bacc6e701fb4 227 eAtStatus_t atStatus = AT_END_ERROR;
kashish_mbed 0:bacc6e701fb4 228 uint16_t atCmdLen;
kashish_mbed 0:bacc6e701fb4 229
kashish_mbed 0:bacc6e701fb4 230 // Reset At_cmd buffer for each transmission
kashish_mbed 0:bacc6e701fb4 231 memset1((uint8_t *)AtCmdBuff, 0x00, DATA_TX_MAX_BUFF_SIZE);
kashish_mbed 0:bacc6e701fb4 232
kashish_mbed 0:bacc6e701fb4 233 // Reset the UART circular buffer for each transmission to make sure
kashish_mbed 0:bacc6e701fb4 234 // the responses we will get are for the current AT cmd.
kashish_mbed 0:bacc6e701fb4 235 HW_UART_ResetBuffer();
kashish_mbed 0:bacc6e701fb4 236
kashish_mbed 0:bacc6e701fb4 237 // Format AT cmd
kashish_mbed 0:bacc6e701fb4 238 atCmdLen = at_cmd_format(at_group, atCmd, pdata);
kashish_mbed 0:bacc6e701fb4 239 if(atCmdLen == 0)
kashish_mbed 0:bacc6e701fb4 240 {
kashish_mbed 0:bacc6e701fb4 241 // You are trying to use a command behavior that HXC Client doesn't
kashish_mbed 0:bacc6e701fb4 242 // support. Check manual for possible AT command behaviors.
kashish_mbed 0:bacc6e701fb4 243 return AT_CMD_ERROR;
kashish_mbed 0:bacc6e701fb4 244 }
kashish_mbed 0:bacc6e701fb4 245
kashish_mbed 0:bacc6e701fb4 246 // Send AT cmd string
kashish_mbed 0:bacc6e701fb4 247 if(at_cmd_send(atCmdLen) != AT_OK)
kashish_mbed 0:bacc6e701fb4 248 {
kashish_mbed 0:bacc6e701fb4 249 return AT_UART_LINK_ERROR;
kashish_mbed 0:bacc6e701fb4 250 }
kashish_mbed 0:bacc6e701fb4 251
kashish_mbed 0:bacc6e701fb4 252 // Read response from HXC client for the AT cmd
kashish_mbed 0:bacc6e701fb4 253 if (at_group == AT_GET)
kashish_mbed 0:bacc6e701fb4 254 {
kashish_mbed 0:bacc6e701fb4 255 // Get the value
kashish_mbed 0:bacc6e701fb4 256 atStatus = at_cmd_receive(pdata);
kashish_mbed 0:bacc6e701fb4 257 }
kashish_mbed 0:bacc6e701fb4 258 else
kashish_mbed 0:bacc6e701fb4 259 {
kashish_mbed 0:bacc6e701fb4 260 // Check for the return status
kashish_mbed 0:bacc6e701fb4 261 atStatus = at_cmd_receive(NULL);
kashish_mbed 0:bacc6e701fb4 262 }
kashish_mbed 0:bacc6e701fb4 263
kashish_mbed 0:bacc6e701fb4 264 return atStatus;
kashish_mbed 0:bacc6e701fb4 265 }
kashish_mbed 0:bacc6e701fb4 266
kashish_mbed 0:bacc6e701fb4 267
kashish_mbed 0:bacc6e701fb4 268 /*******************************************************************************
kashish_mbed 0:bacc6e701fb4 269 * @Brief : Format the cmd in order to be send
kashish_mbed 0:bacc6e701fb4 270 * @Param : at_group - the behavior of AT cmd
kashish_mbed 0:bacc6e701fb4 271 * Cmd - AT command
kashish_mbed 0:bacc6e701fb4 272 * ptr - generic pointer to the IN/OUT buffer
kashish_mbed 0:bacc6e701fb4 273 * @Return: Length of the formated frame to be send
kashish_mbed 0:bacc6e701fb4 274 ******************************************************************************/
kashish_mbed 0:bacc6e701fb4 275 static uint16_t at_cmd_format(ATGroup_t at_group, ATCmd_t Cmd, const void *ptr)
kashish_mbed 0:bacc6e701fb4 276 {
kashish_mbed 0:bacc6e701fb4 277 uint16_t len = 0; /*length of the formated command*/
kashish_mbed 0:bacc6e701fb4 278
kashish_mbed 0:bacc6e701fb4 279 switch (at_group)
kashish_mbed 0:bacc6e701fb4 280 {
kashish_mbed 0:bacc6e701fb4 281 case AT_CTRL:
kashish_mbed 0:bacc6e701fb4 282 {
kashish_mbed 0:bacc6e701fb4 283 len = AT_VPRINTF("AT%s\r\n", CmdTab[Cmd]);
kashish_mbed 0:bacc6e701fb4 284 break;
kashish_mbed 0:bacc6e701fb4 285 }
kashish_mbed 0:bacc6e701fb4 286 case AT_GET:
kashish_mbed 0:bacc6e701fb4 287 {
kashish_mbed 0:bacc6e701fb4 288 len = AT_VPRINTF("AT%s=?\r\n", CmdTab[Cmd]);
kashish_mbed 0:bacc6e701fb4 289 break;
kashish_mbed 0:bacc6e701fb4 290 }
kashish_mbed 0:bacc6e701fb4 291 case AT_SET:
kashish_mbed 0:bacc6e701fb4 292 {
kashish_mbed 0:bacc6e701fb4 293 len = at_set_cmd_format(Cmd, ptr);
kashish_mbed 0:bacc6e701fb4 294 break;
kashish_mbed 0:bacc6e701fb4 295 }
kashish_mbed 0:bacc6e701fb4 296 default:
kashish_mbed 0:bacc6e701fb4 297 {
kashish_mbed 0:bacc6e701fb4 298 break;
kashish_mbed 0:bacc6e701fb4 299 }
kashish_mbed 0:bacc6e701fb4 300 }
kashish_mbed 0:bacc6e701fb4 301
kashish_mbed 0:bacc6e701fb4 302 return len;
kashish_mbed 0:bacc6e701fb4 303 }
kashish_mbed 0:bacc6e701fb4 304
kashish_mbed 0:bacc6e701fb4 305
kashish_mbed 0:bacc6e701fb4 306 /*******************************************************************************
kashish_mbed 0:bacc6e701fb4 307 * @Brief : Format the at set cmd
kashish_mbed 0:bacc6e701fb4 308 * @Param : Cmd - AT command
kashish_mbed 0:bacc6e701fb4 309 * ptr - generic pointer to the IN/OUT buffer
kashish_mbed 0:bacc6e701fb4 310 * @Return: Length of the formated frame to be send
kashish_mbed 0:bacc6e701fb4 311 ******************************************************************************/
kashish_mbed 0:bacc6e701fb4 312 static uint16_t at_set_cmd_format(ATCmd_t Cmd, const void *ptr)
kashish_mbed 0:bacc6e701fb4 313 {
kashish_mbed 0:bacc6e701fb4 314 uint32_t value; /*for 32_02X and 32_D*/
kashish_mbed 0:bacc6e701fb4 315
kashish_mbed 0:bacc6e701fb4 316
kashish_mbed 0:bacc6e701fb4 317 Offset = AT_VPRINTF("AT%s=", CmdTab[Cmd]);
kashish_mbed 0:bacc6e701fb4 318 switch (Cmd)
kashish_mbed 0:bacc6e701fb4 319 {
kashish_mbed 0:bacc6e701fb4 320 case AT_SEND:
kashish_mbed 0:bacc6e701fb4 321 {
kashish_mbed 0:bacc6e701fb4 322 sSendDataString_t *SendData = (sSendDataString_t *)ptr;
kashish_mbed 0:bacc6e701fb4 323 Offset += AT_VPRINTF("%d,%d:%s", SendData->Ack, SendData->Port, SendData->Buffer);
kashish_mbed 0:bacc6e701fb4 324 break;
kashish_mbed 0:bacc6e701fb4 325 }
kashish_mbed 0:bacc6e701fb4 326 case AT_SENDB:
kashish_mbed 0:bacc6e701fb4 327 {
kashish_mbed 0:bacc6e701fb4 328 sSendDataBinary_t *SendData = (sSendDataBinary_t *)ptr;
kashish_mbed 0:bacc6e701fb4 329
kashish_mbed 0:bacc6e701fb4 330 Offset += AT_VPRINTF("%d,%d:", SendData->Ack, SendData->Port);
kashish_mbed 0:bacc6e701fb4 331 unsigned i;
kashish_mbed 0:bacc6e701fb4 332 for (i = 0; i < SendData->DataSize; i++)
kashish_mbed 0:bacc6e701fb4 333 {
kashish_mbed 0:bacc6e701fb4 334 Offset+=AT_VPRINTF("%02x", SendData->Buffer[i]);
kashish_mbed 0:bacc6e701fb4 335 }
kashish_mbed 0:bacc6e701fb4 336
kashish_mbed 0:bacc6e701fb4 337 break;
kashish_mbed 0:bacc6e701fb4 338 }
kashish_mbed 0:bacc6e701fb4 339 case AT_APPKEY:
kashish_mbed 0:bacc6e701fb4 340 case AT_NWKSKEY:
kashish_mbed 0:bacc6e701fb4 341 case AT_APPSKEY:
kashish_mbed 0:bacc6e701fb4 342 case AT_DEVADR:
kashish_mbed 0:bacc6e701fb4 343 case AT_APPEUI:
kashish_mbed 0:bacc6e701fb4 344 case AT_DEVEUI:
kashish_mbed 0:bacc6e701fb4 345 {
kashish_mbed 0:bacc6e701fb4 346 char *key = (char*) ptr;
kashish_mbed 0:bacc6e701fb4 347 Offset += AT_VPRINTF("%s", key);
kashish_mbed 0:bacc6e701fb4 348
kashish_mbed 0:bacc6e701fb4 349 break;
kashish_mbed 0:bacc6e701fb4 350 }
kashish_mbed 0:bacc6e701fb4 351 case AT_RX1DL:
kashish_mbed 0:bacc6e701fb4 352 case AT_RX2DL:
kashish_mbed 0:bacc6e701fb4 353 case AT_JN1DL:
kashish_mbed 0:bacc6e701fb4 354 case AT_JN2DL:
kashish_mbed 0:bacc6e701fb4 355 case AT_FCU:
kashish_mbed 0:bacc6e701fb4 356 case AT_FCD:
kashish_mbed 0:bacc6e701fb4 357 {
kashish_mbed 0:bacc6e701fb4 358 value = *(uint32_t*)ptr;
kashish_mbed 0:bacc6e701fb4 359 Offset += AT_VPRINTF("%u", value);
kashish_mbed 0:bacc6e701fb4 360 break;
kashish_mbed 0:bacc6e701fb4 361 }
kashish_mbed 0:bacc6e701fb4 362 case AT_DR:
kashish_mbed 0:bacc6e701fb4 363 case AT_TXP:
kashish_mbed 0:bacc6e701fb4 364 case AT_PNM:
kashish_mbed 0:bacc6e701fb4 365 case AT_DCS:
kashish_mbed 0:bacc6e701fb4 366 case AT_ADR:
kashish_mbed 0:bacc6e701fb4 367 case AT_BAT:
kashish_mbed 0:bacc6e701fb4 368 {
kashish_mbed 0:bacc6e701fb4 369 uint8_t value_8 = *(uint8_t*)ptr;
kashish_mbed 0:bacc6e701fb4 370 Offset += AT_VPRINTF("%d", value_8);
kashish_mbed 0:bacc6e701fb4 371 break;
kashish_mbed 0:bacc6e701fb4 372 }
kashish_mbed 0:bacc6e701fb4 373 case AT_CLASS:
kashish_mbed 0:bacc6e701fb4 374 {
kashish_mbed 0:bacc6e701fb4 375 char value_c = *(char*)ptr;
kashish_mbed 0:bacc6e701fb4 376 Offset += AT_VPRINTF("%c", value_c);
kashish_mbed 0:bacc6e701fb4 377 break;
kashish_mbed 0:bacc6e701fb4 378 }
kashish_mbed 0:bacc6e701fb4 379 case AT_NJM:
kashish_mbed 0:bacc6e701fb4 380 {
kashish_mbed 0:bacc6e701fb4 381 Offset += AT_VPRINTF("%s", (char*)ptr);
kashish_mbed 0:bacc6e701fb4 382 break;
kashish_mbed 0:bacc6e701fb4 383 }
kashish_mbed 0:bacc6e701fb4 384 default:
kashish_mbed 0:bacc6e701fb4 385 {
kashish_mbed 0:bacc6e701fb4 386 //DBG_PRINTF ("Not supported\r\n");
kashish_mbed 0:bacc6e701fb4 387 break;
kashish_mbed 0:bacc6e701fb4 388 }
kashish_mbed 0:bacc6e701fb4 389 }
kashish_mbed 0:bacc6e701fb4 390
kashish_mbed 0:bacc6e701fb4 391 Offset += AT_VPRINTF("\r\n");
kashish_mbed 0:bacc6e701fb4 392 uint16_t len = Offset;
kashish_mbed 0:bacc6e701fb4 393 Offset = 0;
kashish_mbed 0:bacc6e701fb4 394
kashish_mbed 0:bacc6e701fb4 395 return len;
kashish_mbed 0:bacc6e701fb4 396 }
kashish_mbed 0:bacc6e701fb4 397
kashish_mbed 0:bacc6e701fb4 398
kashish_mbed 0:bacc6e701fb4 399 /*******************************************************************************
kashish_mbed 0:bacc6e701fb4 400 * @Brief : This function sends an AT cmd to the slave device
kashish_mbed 0:bacc6e701fb4 401 * @Param : len - length of the AT cmd to be sent
kashish_mbed 0:bacc6e701fb4 402 * @Return: eAtStatus_t return code
kashish_mbed 0:bacc6e701fb4 403 *******************************************************************************/
kashish_mbed 0:bacc6e701fb4 404 static eAtStatus_t at_cmd_send(uint16_t len)
kashish_mbed 0:bacc6e701fb4 405 {
kashish_mbed 0:bacc6e701fb4 406 /*transmit the command from master to slave*/
kashish_mbed 0:bacc6e701fb4 407 if( HW_UART_Modem_SendBytes(AtCmdBuff, len) == false)
kashish_mbed 0:bacc6e701fb4 408 {
kashish_mbed 0:bacc6e701fb4 409 return AT_UART_LINK_ERROR;
kashish_mbed 0:bacc6e701fb4 410 }
kashish_mbed 0:bacc6e701fb4 411
kashish_mbed 0:bacc6e701fb4 412 return AT_OK;
kashish_mbed 0:bacc6e701fb4 413 }
kashish_mbed 0:bacc6e701fb4 414
kashish_mbed 0:bacc6e701fb4 415
kashish_mbed 0:bacc6e701fb4 416 /*******************************************************************************
kashish_mbed 0:bacc6e701fb4 417 * @Brief : This function receives response from the slave device
kashish_mbed 0:bacc6e701fb4 418 * @Param : pdata - pointer to the value returned by the slave
kashish_mbed 0:bacc6e701fb4 419 * @Return: Return code coming from HXC slave
kashish_mbed 0:bacc6e701fb4 420 *******************************************************************************/
kashish_mbed 0:bacc6e701fb4 421 static eAtStatus_t at_cmd_receive(void *pdata)
kashish_mbed 0:bacc6e701fb4 422 {
kashish_mbed 0:bacc6e701fb4 423 bool ResponseComplete = false;
kashish_mbed 0:bacc6e701fb4 424 uint8_t i = 0;
kashish_mbed 0:bacc6e701fb4 425 eAtStatus_t RetCode = AT_END_ERROR;
kashish_mbed 0:bacc6e701fb4 426
kashish_mbed 0:bacc6e701fb4 427 // Cleanup the response buffer
kashish_mbed 0:bacc6e701fb4 428 memset1((uint8_t *)AtResponseBuff, 0x00, AT_RESPONSE_BUFF_SIZE);
kashish_mbed 0:bacc6e701fb4 429
kashish_mbed 0:bacc6e701fb4 430 uint32_t currentTime = TimerGetCurrentTime();
kashish_mbed 0:bacc6e701fb4 431 while(ResponseComplete != true)
kashish_mbed 0:bacc6e701fb4 432 {
kashish_mbed 0:bacc6e701fb4 433 if(HW_UART_Modem_IsNewCharReceived() == false)
kashish_mbed 0:bacc6e701fb4 434 {
kashish_mbed 0:bacc6e701fb4 435 if(TimerGetElapsedTime(currentTime) > HXC_TIMEOUT)
kashish_mbed 0:bacc6e701fb4 436 {
kashish_mbed 0:bacc6e701fb4 437 ResponseComplete = true;
kashish_mbed 0:bacc6e701fb4 438 RetCode = AT_TIMEOUT;
kashish_mbed 0:bacc6e701fb4 439 }
kashish_mbed 0:bacc6e701fb4 440 }
kashish_mbed 0:bacc6e701fb4 441 else
kashish_mbed 0:bacc6e701fb4 442 {
kashish_mbed 0:bacc6e701fb4 443 AtResponseBuff[i++] = HW_UART_Modem_GetNewChar();
kashish_mbed 0:bacc6e701fb4 444
kashish_mbed 0:bacc6e701fb4 445 // Wait up to line feed marker
kashish_mbed 0:bacc6e701fb4 446 if (AtResponseBuff[i - 1] == '\n')
kashish_mbed 0:bacc6e701fb4 447 {
kashish_mbed 0:bacc6e701fb4 448 // Last two bytes are <CR><LF>, set CR as NULL byte.
kashish_mbed 0:bacc6e701fb4 449 AtResponseBuff[i - 2] = '\0';
kashish_mbed 0:bacc6e701fb4 450 i = 0;
kashish_mbed 0:bacc6e701fb4 451
kashish_mbed 0:bacc6e701fb4 452 RetCode = at_cmd_analyzeResponse(AtResponseBuff);
kashish_mbed 0:bacc6e701fb4 453 if(RetCode != AT_END_ERROR)
kashish_mbed 0:bacc6e701fb4 454 {
kashish_mbed 0:bacc6e701fb4 455 ResponseComplete = true;
kashish_mbed 0:bacc6e701fb4 456 }
kashish_mbed 0:bacc6e701fb4 457 else if(pdata != NULL)
kashish_mbed 0:bacc6e701fb4 458 {
kashish_mbed 0:bacc6e701fb4 459 // If pdata isn't null that means we are using GET cmd to get
kashish_mbed 0:bacc6e701fb4 460 // return value. Copy the return value into pdata.
kashish_mbed 0:bacc6e701fb4 461
kashish_mbed 0:bacc6e701fb4 462 strcpy(pdata, AtResponseBuff);
kashish_mbed 0:bacc6e701fb4 463 memset1((uint8_t *)AtResponseBuff, 0x00, AT_RESPONSE_BUFF_SIZE);
kashish_mbed 0:bacc6e701fb4 464 // Now, let's get the status
kashish_mbed 0:bacc6e701fb4 465 }
kashish_mbed 0:bacc6e701fb4 466 }
kashish_mbed 0:bacc6e701fb4 467 else
kashish_mbed 0:bacc6e701fb4 468 {
kashish_mbed 0:bacc6e701fb4 469 if (i == (AT_RESPONSE_BUFF_SIZE - 1))
kashish_mbed 0:bacc6e701fb4 470 {
kashish_mbed 0:bacc6e701fb4 471 // Frame overflow. Reset index and stop reading.
kashish_mbed 0:bacc6e701fb4 472 i = 0;
kashish_mbed 0:bacc6e701fb4 473 RetCode = AT_PARAM_OVERFLOW;
kashish_mbed 0:bacc6e701fb4 474 ResponseComplete = true;
kashish_mbed 0:bacc6e701fb4 475 }
kashish_mbed 0:bacc6e701fb4 476 }
kashish_mbed 0:bacc6e701fb4 477 }
kashish_mbed 0:bacc6e701fb4 478 } // End while(ResponseComplete != true)
kashish_mbed 0:bacc6e701fb4 479
kashish_mbed 0:bacc6e701fb4 480 HW_UART_Modem_Ready();
kashish_mbed 0:bacc6e701fb4 481 return RetCode;
kashish_mbed 0:bacc6e701fb4 482 }
kashish_mbed 0:bacc6e701fb4 483
kashish_mbed 0:bacc6e701fb4 484
kashish_mbed 0:bacc6e701fb4 485 /*******************************************************************************
kashish_mbed 0:bacc6e701fb4 486 * @Brief : Analyze the response received by the device
kashish_mbed 0:bacc6e701fb4 487 * @Param : response: pointer to the received response
kashish_mbed 0:bacc6e701fb4 488 * @Return: eAtStatus_t error type
kashish_mbed 0:bacc6e701fb4 489 *******************************************************************************/
kashish_mbed 0:bacc6e701fb4 490 static eAtStatus_t at_cmd_analyzeResponse(const char *ReturnResp)
kashish_mbed 0:bacc6e701fb4 491 {
kashish_mbed 0:bacc6e701fb4 492 uint8_t i;
kashish_mbed 0:bacc6e701fb4 493
kashish_mbed 0:bacc6e701fb4 494 for (i = 0; i < ARRAY_SIZE(AT_RetCode); i++)
kashish_mbed 0:bacc6e701fb4 495 {
kashish_mbed 0:bacc6e701fb4 496 if (strncmp(ReturnResp, AT_RetCode[i].RetCodeStr, (AT_RetCode[i].SizeRetCodeStr)) == 0)
kashish_mbed 0:bacc6e701fb4 497 {
kashish_mbed 0:bacc6e701fb4 498 /* Command has been found */
kashish_mbed 0:bacc6e701fb4 499 return AT_RetCode[i].RetCode;
kashish_mbed 0:bacc6e701fb4 500 }
kashish_mbed 0:bacc6e701fb4 501 }
kashish_mbed 0:bacc6e701fb4 502 return AT_END_ERROR;
kashish_mbed 0:bacc6e701fb4 503 }
kashish_mbed 0:bacc6e701fb4 504
kashish_mbed 0:bacc6e701fb4 505
kashish_mbed 0:bacc6e701fb4 506 /*******************************************************************************
kashish_mbed 0:bacc6e701fb4 507 * @Brief : Format the AT frame to be sent to the modem (slave)
kashish_mbed 0:bacc6e701fb4 508 * @Param : Pointer to the format string
kashish_mbed 0:bacc6e701fb4 509 * @Return: Length of the string to be sent
kashish_mbed 0:bacc6e701fb4 510 *******************************************************************************/
kashish_mbed 0:bacc6e701fb4 511 static uint16_t at_cmd_vprintf(const char *format, ...)
kashish_mbed 0:bacc6e701fb4 512 {
kashish_mbed 0:bacc6e701fb4 513 va_list args;
kashish_mbed 0:bacc6e701fb4 514 uint16_t len;
kashish_mbed 0:bacc6e701fb4 515
kashish_mbed 0:bacc6e701fb4 516 va_start(args, format);
kashish_mbed 0:bacc6e701fb4 517
kashish_mbed 0:bacc6e701fb4 518 len = tiny_vsnprintf_like(AtCmdBuff + Offset, DATA_TX_MAX_BUFF_SIZE - Offset, format, args);
kashish_mbed 0:bacc6e701fb4 519
kashish_mbed 0:bacc6e701fb4 520 va_end(args);
kashish_mbed 0:bacc6e701fb4 521
kashish_mbed 0:bacc6e701fb4 522 return len;
kashish_mbed 0:bacc6e701fb4 523 }
kashish_mbed 0:bacc6e701fb4 524
kashish_mbed 0:bacc6e701fb4 525 /*******************************************************************************
kashish_mbed 0:bacc6e701fb4 526 * @Brief : Reset the HXC client modem using HXC RESET pin
kashish_mbed 0:bacc6e701fb4 527 * @Param : None
kashish_mbed 0:bacc6e701fb4 528 * @Return : Module status
kashish_mbed 0:bacc6e701fb4 529 ******************************************************************************/
kashish_mbed 0:bacc6e701fb4 530 eAtStatus_t Modem_HardReset(void)
kashish_mbed 0:bacc6e701fb4 531 {
kashish_mbed 0:bacc6e701fb4 532 HW_GPIO_Write(HXC_RESET_PORT, HXC_RESET_PIN, GPIO_PIN_RESET);
kashish_mbed 0:bacc6e701fb4 533 DelayMs(200);
kashish_mbed 0:bacc6e701fb4 534 HW_GPIO_Write(HXC_RESET_PORT, HXC_RESET_PIN, GPIO_PIN_SET);
kashish_mbed 0:bacc6e701fb4 535
kashish_mbed 0:bacc6e701fb4 536 // Check for the return status - OK
kashish_mbed 0:bacc6e701fb4 537 return at_cmd_receive(NULL);
kashish_mbed 0:bacc6e701fb4 538 }
kashish_mbed 0:bacc6e701fb4 539
kashish_mbed 0:bacc6e701fb4 540 /*******************************************************************************
kashish_mbed 0:bacc6e701fb4 541 * @Brief : Check if any downlink packet is received
kashish_mbed 0:bacc6e701fb4 542 * @Param : None
kashish_mbed 0:bacc6e701fb4 543 * @Return: TRUE or FALSE
kashish_mbed 0:bacc6e701fb4 544 ******************************************************************************/
kashish_mbed 0:bacc6e701fb4 545 bool Modem_IsNewDataReceived(void)
kashish_mbed 0:bacc6e701fb4 546 {
kashish_mbed 0:bacc6e701fb4 547 if(HW_UART_Modem_IsNewCharReceived() == false)
kashish_mbed 0:bacc6e701fb4 548 {
kashish_mbed 0:bacc6e701fb4 549 return false;
kashish_mbed 0:bacc6e701fb4 550 }
kashish_mbed 0:bacc6e701fb4 551 // Cleanup the response buffer
kashish_mbed 0:bacc6e701fb4 552 memset1((uint8_t *)AtResponseBuff, 0x00, AT_RESPONSE_BUFF_SIZE);
kashish_mbed 0:bacc6e701fb4 553
kashish_mbed 0:bacc6e701fb4 554 HW_UART_Modem_GetCharactersUntilNewLine(AtResponseBuff, AT_RESPONSE_BUFF_SIZE, HXC_TIMEOUT);
kashish_mbed 0:bacc6e701fb4 555
kashish_mbed 0:bacc6e701fb4 556 if(strncmp("rxdata", AtResponseBuff, 6) == 0)
kashish_mbed 0:bacc6e701fb4 557 {
kashish_mbed 0:bacc6e701fb4 558 return true;
kashish_mbed 0:bacc6e701fb4 559 }
kashish_mbed 0:bacc6e701fb4 560 return false;
kashish_mbed 0:bacc6e701fb4 561 }
kashish_mbed 0:bacc6e701fb4 562
kashish_mbed 0:bacc6e701fb4 563 char* Modem_GetResponseBuffer(void)
kashish_mbed 0:bacc6e701fb4 564 {
kashish_mbed 0:bacc6e701fb4 565 return AtResponseBuff;
kashish_mbed 0:bacc6e701fb4 566 }
kashish_mbed 0:bacc6e701fb4 567 /************************ (C) COPYRIGHT Haxiot *****END OF FILE*****/
kashish_mbed 0:bacc6e701fb4 568