HXC Client Shield Repository.

Dependencies:   mbed

Committer:
kashish_mbed
Date:
Tue Apr 13 22:04:32 2021 +0000
Revision:
2:52bb5ee0a72d
Parent:
0:bacc6e701fb4
Provisioning Cmd process(Not Working)

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