A demo application for HXC900 LoRaWAN module using Nucleo-L053R8.

Dependencies:   mbed

Committer:
fahadmirza
Date:
Mon Jul 16 20:12:42 2018 +0000
Revision:
5:53302861bfea
Parent:
hxc_client.c@0:a0c5877bd360
Child:
15:2860c960d2ff
Updated directories;

Who changed what in which revision?

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