Kashish Haresh Shah
/
HXCClientShield
HXC Client Shield Repository.
LoRa/lora_driver.c@2:52bb5ee0a72d, 2021-04-13 (annotated)
- Committer:
- kashish_mbed
- Date:
- Tue Apr 13 22:04:32 2021 +0000
- Revision:
- 2:52bb5ee0a72d
- Parent:
- 1:d4df183e5c12
Provisioning Cmd process(Not Working)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kashish_mbed | 0:bacc6e701fb4 | 1 | /* |
kashish_mbed | 0:bacc6e701fb4 | 2 | _ _ _____ _______ |
kashish_mbed | 0:bacc6e701fb4 | 3 | | | | | |_ _| |__ __| |
kashish_mbed | 0:bacc6e701fb4 | 4 | | |__| | __ ___ __ | | ___ | | |
kashish_mbed | 0:bacc6e701fb4 | 5 | | __ |/ _` \ \/ / | | / _ \| | |
kashish_mbed | 0:bacc6e701fb4 | 6 | | | | | (_| |> < _| || (_) | | |
kashish_mbed | 0:bacc6e701fb4 | 7 | |_| |_|\__,_/_/\_\_____\___/|_| |
kashish_mbed | 0:bacc6e701fb4 | 8 | (C)2017 HaxIoT |
kashish_mbed | 0:bacc6e701fb4 | 9 | */ |
kashish_mbed | 0:bacc6e701fb4 | 10 | /******************************************************************************* |
kashish_mbed | 0:bacc6e701fb4 | 11 | * @File : lora_driver.c |
kashish_mbed | 0:bacc6e701fb4 | 12 | * @Author : Fahad Mirza (Haxiot) |
kashish_mbed | 0:bacc6e701fb4 | 13 | * @Version : V1.0.0 |
kashish_mbed | 0:bacc6e701fb4 | 14 | * @Modified: 18 October, 2018 |
kashish_mbed | 0:bacc6e701fb4 | 15 | * @Brief : LoRa Driver |
kashish_mbed | 0:bacc6e701fb4 | 16 | ****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 17 | * @attention |
kashish_mbed | 0:bacc6e701fb4 | 18 | * |
kashish_mbed | 0:bacc6e701fb4 | 19 | * <h2><center>© COPYRIGHT(c) 2017 Haxiot</center></h2> |
kashish_mbed | 0:bacc6e701fb4 | 20 | * |
kashish_mbed | 0:bacc6e701fb4 | 21 | * Redistribution and use in source and binary forms, with or without modification, |
kashish_mbed | 0:bacc6e701fb4 | 22 | * are permitted provided that the following conditions are met: |
kashish_mbed | 0:bacc6e701fb4 | 23 | * 1. Redistributions of source code must retain the above copyright notice, |
kashish_mbed | 0:bacc6e701fb4 | 24 | * this list of conditions and the following disclaimer. |
kashish_mbed | 0:bacc6e701fb4 | 25 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
kashish_mbed | 0:bacc6e701fb4 | 26 | * this list of conditions and the following disclaimer in the documentation |
kashish_mbed | 0:bacc6e701fb4 | 27 | * and/or other materials provided with the distribution. |
kashish_mbed | 0:bacc6e701fb4 | 28 | * 3. Neither the name of Haxiot nor the names of its contributors |
kashish_mbed | 0:bacc6e701fb4 | 29 | * may be used to endorse or promote products derived from this software |
kashish_mbed | 0:bacc6e701fb4 | 30 | * without specific prior written permission. |
kashish_mbed | 0:bacc6e701fb4 | 31 | * |
kashish_mbed | 0:bacc6e701fb4 | 32 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
kashish_mbed | 0:bacc6e701fb4 | 33 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
kashish_mbed | 0:bacc6e701fb4 | 34 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
kashish_mbed | 0:bacc6e701fb4 | 35 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
kashish_mbed | 0:bacc6e701fb4 | 36 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
kashish_mbed | 0:bacc6e701fb4 | 37 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
kashish_mbed | 0:bacc6e701fb4 | 38 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
kashish_mbed | 0:bacc6e701fb4 | 39 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
kashish_mbed | 0:bacc6e701fb4 | 40 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
kashish_mbed | 0:bacc6e701fb4 | 41 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
kashish_mbed | 0:bacc6e701fb4 | 42 | * |
kashish_mbed | 0:bacc6e701fb4 | 43 | ****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 44 | */ |
kashish_mbed | 0:bacc6e701fb4 | 45 | /* Includes ------------------------------------------------------------------*/ |
kashish_mbed | 0:bacc6e701fb4 | 46 | #include "lora_driver.h" |
kashish_mbed | 0:bacc6e701fb4 | 47 | |
kashish_mbed | 0:bacc6e701fb4 | 48 | /* Private Macros ------------------------------------------------------------*/ |
kashish_mbed | 0:bacc6e701fb4 | 49 | #define JOIN_SEND_DELAY_MAX (10000U) // Randomization range - 10s |
kashish_mbed | 0:bacc6e701fb4 | 50 | #define JOIN_STATUS_REQ_DELAY (7000U) // milliseconds. Join req takes 6s. |
kashish_mbed | 0:bacc6e701fb4 | 51 | |
kashish_mbed | 0:bacc6e701fb4 | 52 | // Re Authenticate every REJOIN_TIME |
kashish_mbed | 0:bacc6e701fb4 | 53 | #define REJOIN_TIME (1 * 60 * 60 * 1000U) // 1 hour |
kashish_mbed | 0:bacc6e701fb4 | 54 | |
kashish_mbed | 0:bacc6e701fb4 | 55 | /* Private global variables --------------------------------------------------*/ |
kashish_mbed | 2:52bb5ee0a72d | 56 | sLoraConfig_t *LoraConfigParam; |
kashish_mbed | 2:52bb5ee0a72d | 57 | sLoraDriverParam_t *LoraDriverParam; |
kashish_mbed | 0:bacc6e701fb4 | 58 | static volatile eDeviceState_t DeviceState = CLIENT_INIT; |
kashish_mbed | 0:bacc6e701fb4 | 59 | |
kashish_mbed | 0:bacc6e701fb4 | 60 | static TimerEvent_t JoinRequestTimer; |
kashish_mbed | 0:bacc6e701fb4 | 61 | static TimerEvent_t RejoinTimer; |
kashish_mbed | 0:bacc6e701fb4 | 62 | static TimerEvent_t SensorMeasureTimer; |
kashish_mbed | 0:bacc6e701fb4 | 63 | static TimerEvent_t JoinStatusReqTimer; |
kashish_mbed | 0:bacc6e701fb4 | 64 | |
kashish_mbed | 0:bacc6e701fb4 | 65 | // Object definition for data to be sent to loRa application server |
kashish_mbed | 0:bacc6e701fb4 | 66 | static uint8_t DataBinaryBuff[MAX_PAYLOAD_SIZE]; |
kashish_mbed | 0:bacc6e701fb4 | 67 | static sSendDataBinary_t SendDataBinary = {DataBinaryBuff, 0 , 0, 0}; |
kashish_mbed | 0:bacc6e701fb4 | 68 | |
kashish_mbed | 0:bacc6e701fb4 | 69 | // RNG handler declaration |
kashish_mbed | 0:bacc6e701fb4 | 70 | RNG_HandleTypeDef RngHandle = {.Instance = RNG}; |
kashish_mbed | 0:bacc6e701fb4 | 71 | |
kashish_mbed | 0:bacc6e701fb4 | 72 | /* Private functions ---------------------------------------------------------*/ |
kashish_mbed | 0:bacc6e701fb4 | 73 | static void OnRejoinTimerEvent(void); |
kashish_mbed | 0:bacc6e701fb4 | 74 | static void OnJoinRequestTimerEvt(void); |
kashish_mbed | 0:bacc6e701fb4 | 75 | static void OnJoinStatusReqTimerEvt(void); |
kashish_mbed | 0:bacc6e701fb4 | 76 | static void OnSensorMeasureTimerEvt(void); |
kashish_mbed | 0:bacc6e701fb4 | 77 | static void setJoinRequestTimer(void); |
kashish_mbed | 0:bacc6e701fb4 | 78 | |
kashish_mbed | 0:bacc6e701fb4 | 79 | |
kashish_mbed | 0:bacc6e701fb4 | 80 | /* Function definitions ------------------------------------------------------*/ |
kashish_mbed | 0:bacc6e701fb4 | 81 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 82 | * @Brief : Initialize LoRa Modem |
kashish_mbed | 0:bacc6e701fb4 | 83 | * @Param : sLoraConfig_t |
kashish_mbed | 0:bacc6e701fb4 | 84 | * @Return : None |
kashish_mbed | 0:bacc6e701fb4 | 85 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 86 | void Lora_init(sLoraConfig_t *loraConfig, sLoraDriverParam_t *loraDriverParam) |
kashish_mbed | 0:bacc6e701fb4 | 87 | { |
kashish_mbed | 0:bacc6e701fb4 | 88 | |
kashish_mbed | 0:bacc6e701fb4 | 89 | LoraConfigParam = loraConfig; |
kashish_mbed | 0:bacc6e701fb4 | 90 | LoraDriverParam = loraDriverParam; |
kashish_mbed | 0:bacc6e701fb4 | 91 | |
kashish_mbed | 0:bacc6e701fb4 | 92 | if(Modem_Init() != AT_OK) |
kashish_mbed | 0:bacc6e701fb4 | 93 | { |
kashish_mbed | 0:bacc6e701fb4 | 94 | DBG_PRINTF("Modem_Init failed\r\n"); |
kashish_mbed | 0:bacc6e701fb4 | 95 | } |
kashish_mbed | 0:bacc6e701fb4 | 96 | |
kashish_mbed | 0:bacc6e701fb4 | 97 | // Initialize RNG for join request send randomization |
kashish_mbed | 0:bacc6e701fb4 | 98 | HAL_RNG_Init(&RngHandle); |
kashish_mbed | 0:bacc6e701fb4 | 99 | |
kashish_mbed | 0:bacc6e701fb4 | 100 | |
kashish_mbed | 0:bacc6e701fb4 | 101 | // Timer for join request send |
kashish_mbed | 0:bacc6e701fb4 | 102 | TimerInit(&JoinRequestTimer, OnJoinRequestTimerEvt); |
kashish_mbed | 0:bacc6e701fb4 | 103 | // Timer for join status check |
kashish_mbed | 0:bacc6e701fb4 | 104 | TimerInit(&JoinStatusReqTimer, OnJoinStatusReqTimerEvt); |
kashish_mbed | 0:bacc6e701fb4 | 105 | TimerSetValue(&JoinStatusReqTimer, JOIN_STATUS_REQ_DELAY); |
kashish_mbed | 0:bacc6e701fb4 | 106 | // Timer for sensor occurrence measure |
kashish_mbed | 0:bacc6e701fb4 | 107 | TimerInit(&SensorMeasureTimer, OnSensorMeasureTimerEvt); |
kashish_mbed | 0:bacc6e701fb4 | 108 | TimerSetValue(&SensorMeasureTimer, LoraDriverParam->UplinkCycle); |
kashish_mbed | 0:bacc6e701fb4 | 109 | // Timer for Re-Authenticate / Re-Join |
kashish_mbed | 0:bacc6e701fb4 | 110 | TimerInit(&RejoinTimer, OnRejoinTimerEvent); |
kashish_mbed | 0:bacc6e701fb4 | 111 | TimerSetValue(&RejoinTimer, REJOIN_TIME); |
kashish_mbed | 0:bacc6e701fb4 | 112 | } |
kashish_mbed | 0:bacc6e701fb4 | 113 | |
kashish_mbed | 0:bacc6e701fb4 | 114 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 115 | * @Brief : Check if the modem responds |
kashish_mbed | 0:bacc6e701fb4 | 116 | * @Param : void |
kashish_mbed | 0:bacc6e701fb4 | 117 | * @Return : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 118 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 119 | static eAtStatus_t is_modem_working(void) |
kashish_mbed | 0:bacc6e701fb4 | 120 | { |
kashish_mbed | 0:bacc6e701fb4 | 121 | return Modem_AT_Cmd(AT_CTRL, AT, NULL); |
kashish_mbed | 0:bacc6e701fb4 | 122 | } |
kashish_mbed | 0:bacc6e701fb4 | 123 | |
kashish_mbed | 0:bacc6e701fb4 | 124 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 125 | * @Brief : Set Device EUI |
kashish_mbed | 0:bacc6e701fb4 | 126 | * @Param : Pointer to Device EUI |
kashish_mbed | 0:bacc6e701fb4 | 127 | * @Return : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 128 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 129 | static eAtStatus_t Lora_setDevEui(char *devEui) |
kashish_mbed | 0:bacc6e701fb4 | 130 | { |
kashish_mbed | 0:bacc6e701fb4 | 131 | return Modem_AT_Cmd(AT_SET, AT_DEVEUI, devEui); |
kashish_mbed | 0:bacc6e701fb4 | 132 | } |
kashish_mbed | 0:bacc6e701fb4 | 133 | |
kashish_mbed | 0:bacc6e701fb4 | 134 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 135 | * @Brief : Print Device EUI |
kashish_mbed | 0:bacc6e701fb4 | 136 | * @Param : None |
kashish_mbed | 0:bacc6e701fb4 | 137 | * @Retval : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 138 | ******************************************************************************/ |
kashish_mbed | 1:d4df183e5c12 | 139 | void Lora_printDevEui(void) |
kashish_mbed | 0:bacc6e701fb4 | 140 | { |
kashish_mbed | 0:bacc6e701fb4 | 141 | char deveui_str[25]; |
kashish_mbed | 0:bacc6e701fb4 | 142 | |
kashish_mbed | 0:bacc6e701fb4 | 143 | Modem_AT_Cmd(AT_GET, AT_DEVEUI, deveui_str); |
kashish_mbed | 0:bacc6e701fb4 | 144 | |
kashish_mbed | 0:bacc6e701fb4 | 145 | DBG_PRINTF("DevEui: %s\n", deveui_str); |
kashish_mbed | 0:bacc6e701fb4 | 146 | } |
kashish_mbed | 0:bacc6e701fb4 | 147 | |
kashish_mbed | 0:bacc6e701fb4 | 148 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 149 | * @Brief : Print Device Address |
kashish_mbed | 0:bacc6e701fb4 | 150 | * @Param : None |
kashish_mbed | 0:bacc6e701fb4 | 151 | * @Retval : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 152 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 153 | static void Lora_printDevAdr(void) |
kashish_mbed | 0:bacc6e701fb4 | 154 | { |
kashish_mbed | 0:bacc6e701fb4 | 155 | char devadr_str[15]; |
kashish_mbed | 0:bacc6e701fb4 | 156 | |
kashish_mbed | 0:bacc6e701fb4 | 157 | Modem_AT_Cmd(AT_GET, AT_DEVADR, devadr_str); |
kashish_mbed | 0:bacc6e701fb4 | 158 | |
kashish_mbed | 0:bacc6e701fb4 | 159 | DBG_PRINTF("DevAdr: %s\n", devadr_str); |
kashish_mbed | 0:bacc6e701fb4 | 160 | } |
kashish_mbed | 0:bacc6e701fb4 | 161 | |
kashish_mbed | 0:bacc6e701fb4 | 162 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 163 | * @Brief : Turn on or off ADR |
kashish_mbed | 0:bacc6e701fb4 | 164 | * @Param : ADR_ON or ADR_OFF |
kashish_mbed | 0:bacc6e701fb4 | 165 | * @Return : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 166 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 167 | static eAtStatus_t Lora_setAdr(eAdrStatus_t adrStatus) |
kashish_mbed | 0:bacc6e701fb4 | 168 | { |
kashish_mbed | 0:bacc6e701fb4 | 169 | //char adr = (adrStatus == ADR_ON ? '1' : '0'); |
kashish_mbed | 0:bacc6e701fb4 | 170 | return Modem_AT_Cmd(AT_SET, AT_ADR, (uint8_t *)(&adrStatus)); |
kashish_mbed | 0:bacc6e701fb4 | 171 | } |
kashish_mbed | 0:bacc6e701fb4 | 172 | |
kashish_mbed | 0:bacc6e701fb4 | 173 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 174 | * @Brief : Set Application EUI |
kashish_mbed | 0:bacc6e701fb4 | 175 | * @Param : Pointer to Application EUI |
kashish_mbed | 0:bacc6e701fb4 | 176 | * @Return : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 177 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 178 | static eAtStatus_t Lora_setAppEui(char *appEui) |
kashish_mbed | 0:bacc6e701fb4 | 179 | { |
kashish_mbed | 0:bacc6e701fb4 | 180 | return Modem_AT_Cmd(AT_SET, AT_APPEUI, appEui); |
kashish_mbed | 0:bacc6e701fb4 | 181 | } |
kashish_mbed | 0:bacc6e701fb4 | 182 | |
kashish_mbed | 0:bacc6e701fb4 | 183 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 184 | * @Brief : Set Application Key |
kashish_mbed | 0:bacc6e701fb4 | 185 | * @Param : Pointer to Application Key |
kashish_mbed | 0:bacc6e701fb4 | 186 | * @Return : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 187 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 188 | static eAtStatus_t Lora_setAppKey(char *appKey) |
kashish_mbed | 0:bacc6e701fb4 | 189 | { |
kashish_mbed | 0:bacc6e701fb4 | 190 | return Modem_AT_Cmd(AT_SET, AT_APPKEY, appKey); |
kashish_mbed | 0:bacc6e701fb4 | 191 | } |
kashish_mbed | 0:bacc6e701fb4 | 192 | |
kashish_mbed | 0:bacc6e701fb4 | 193 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 194 | * @Brief : Set join mode |
kashish_mbed | 0:bacc6e701fb4 | 195 | * @Param : OTAA or ABP |
kashish_mbed | 0:bacc6e701fb4 | 196 | * @Retval : AT_OK if successful, otherwise other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 197 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 198 | static eAtStatus_t Lora_setJoinMode(eJoinMode_t joinMode) |
kashish_mbed | 0:bacc6e701fb4 | 199 | { |
kashish_mbed | 0:bacc6e701fb4 | 200 | if(joinMode == OTAA) |
kashish_mbed | 0:bacc6e701fb4 | 201 | { |
kashish_mbed | 0:bacc6e701fb4 | 202 | return Modem_AT_Cmd(AT_SET, AT_NJM, "OTAA"); |
kashish_mbed | 0:bacc6e701fb4 | 203 | } |
kashish_mbed | 0:bacc6e701fb4 | 204 | |
kashish_mbed | 0:bacc6e701fb4 | 205 | return Modem_AT_Cmd(AT_SET, AT_NJM, "ABP"); |
kashish_mbed | 0:bacc6e701fb4 | 206 | } |
kashish_mbed | 0:bacc6e701fb4 | 207 | |
kashish_mbed | 0:bacc6e701fb4 | 208 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 209 | * @Brief : Set Class |
kashish_mbed | 0:bacc6e701fb4 | 210 | * @Param : CLASS_A or CLASS_C |
kashish_mbed | 0:bacc6e701fb4 | 211 | * @Retval : AT_OK if successful, otherwise other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 212 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 213 | static eAtStatus_t Lora_setClass(char class) |
kashish_mbed | 0:bacc6e701fb4 | 214 | { |
kashish_mbed | 0:bacc6e701fb4 | 215 | return Modem_AT_Cmd(AT_SET, AT_CLASS, &class); |
kashish_mbed | 0:bacc6e701fb4 | 216 | } |
kashish_mbed | 0:bacc6e701fb4 | 217 | |
kashish_mbed | 0:bacc6e701fb4 | 218 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 219 | * @Brief : Join network and initiate join sleep transition timer |
kashish_mbed | 0:bacc6e701fb4 | 220 | * @Param : None |
kashish_mbed | 0:bacc6e701fb4 | 221 | * @Retval : AT_OK or other eAtStatus_t |
kashish_mbed | 0:bacc6e701fb4 | 222 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 223 | static eAtStatus_t Lora_Join(void) |
kashish_mbed | 0:bacc6e701fb4 | 224 | { |
kashish_mbed | 0:bacc6e701fb4 | 225 | return Modem_AT_Cmd(AT_CTRL, AT_JOIN, NULL); |
kashish_mbed | 0:bacc6e701fb4 | 226 | } |
kashish_mbed | 0:bacc6e701fb4 | 227 | |
kashish_mbed | 0:bacc6e701fb4 | 228 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 229 | * @Brief : Check JOIN status |
kashish_mbed | 0:bacc6e701fb4 | 230 | * @Param : None |
kashish_mbed | 0:bacc6e701fb4 | 231 | * @Retval : JOINED or NOT_JOINED |
kashish_mbed | 0:bacc6e701fb4 | 232 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 233 | static eJoinStatus_t Lora_getJoinStatus(void) |
kashish_mbed | 0:bacc6e701fb4 | 234 | { |
kashish_mbed | 0:bacc6e701fb4 | 235 | char joinStatus[12]; // "NOT JOINED" is 10 characters + 1 Null char |
kashish_mbed | 0:bacc6e701fb4 | 236 | |
kashish_mbed | 0:bacc6e701fb4 | 237 | Modem_AT_Cmd(AT_GET, AT_NJS, joinStatus); |
kashish_mbed | 0:bacc6e701fb4 | 238 | |
kashish_mbed | 0:bacc6e701fb4 | 239 | if(strncmp("JOINED", joinStatus, 6) == 0) |
kashish_mbed | 0:bacc6e701fb4 | 240 | { |
kashish_mbed | 0:bacc6e701fb4 | 241 | return JOINED; |
kashish_mbed | 0:bacc6e701fb4 | 242 | } |
kashish_mbed | 0:bacc6e701fb4 | 243 | |
kashish_mbed | 0:bacc6e701fb4 | 244 | return NOT_JOINED; |
kashish_mbed | 0:bacc6e701fb4 | 245 | } |
kashish_mbed | 0:bacc6e701fb4 | 246 | |
kashish_mbed | 0:bacc6e701fb4 | 247 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 248 | * @Brief : Get firmware version |
kashish_mbed | 0:bacc6e701fb4 | 249 | * @Param : None |
kashish_mbed | 0:bacc6e701fb4 | 250 | * @Retval : Integer firmware version |
kashish_mbed | 0:bacc6e701fb4 | 251 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 252 | uint16_t Lora_getFwVersion(void) |
kashish_mbed | 0:bacc6e701fb4 | 253 | { |
kashish_mbed | 0:bacc6e701fb4 | 254 | char fwVersion_str[5]; |
kashish_mbed | 0:bacc6e701fb4 | 255 | uint16_t fwVer_int; |
kashish_mbed | 0:bacc6e701fb4 | 256 | |
kashish_mbed | 0:bacc6e701fb4 | 257 | eAtStatus_t status = Modem_AT_Cmd(AT_GET, AT_VER, fwVersion_str); |
kashish_mbed | 0:bacc6e701fb4 | 258 | |
kashish_mbed | 0:bacc6e701fb4 | 259 | if(status != AT_OK) |
kashish_mbed | 0:bacc6e701fb4 | 260 | { |
kashish_mbed | 0:bacc6e701fb4 | 261 | return 0; |
kashish_mbed | 0:bacc6e701fb4 | 262 | } |
kashish_mbed | 0:bacc6e701fb4 | 263 | |
kashish_mbed | 0:bacc6e701fb4 | 264 | // FW version is a decimal number e.g. 1.18 |
kashish_mbed | 0:bacc6e701fb4 | 265 | // Convert the float number into integer. |
kashish_mbed | 0:bacc6e701fb4 | 266 | uint8_t i = 0; |
kashish_mbed | 0:bacc6e701fb4 | 267 | while(fwVersion_str[++i] != '.'); |
kashish_mbed | 0:bacc6e701fb4 | 268 | |
kashish_mbed | 0:bacc6e701fb4 | 269 | while(fwVersion_str[i] != '\0') |
kashish_mbed | 0:bacc6e701fb4 | 270 | { |
kashish_mbed | 0:bacc6e701fb4 | 271 | fwVersion_str[i] = fwVersion_str[i + 1]; |
kashish_mbed | 0:bacc6e701fb4 | 272 | i++; |
kashish_mbed | 0:bacc6e701fb4 | 273 | } |
kashish_mbed | 0:bacc6e701fb4 | 274 | |
kashish_mbed | 0:bacc6e701fb4 | 275 | tiny_sscanf(fwVersion_str, "%hu", &fwVer_int); |
kashish_mbed | 0:bacc6e701fb4 | 276 | |
kashish_mbed | 0:bacc6e701fb4 | 277 | return fwVer_int; |
kashish_mbed | 0:bacc6e701fb4 | 278 | } |
kashish_mbed | 0:bacc6e701fb4 | 279 | |
kashish_mbed | 0:bacc6e701fb4 | 280 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 281 | * @Brief : Get battery level |
kashish_mbed | 0:bacc6e701fb4 | 282 | * @Param : None |
kashish_mbed | 0:bacc6e701fb4 | 283 | * @Retval : Battery level |
kashish_mbed | 0:bacc6e701fb4 | 284 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 285 | uint8_t Lora_getBatteryLevel(void) |
kashish_mbed | 0:bacc6e701fb4 | 286 | { |
kashish_mbed | 0:bacc6e701fb4 | 287 | char batteryLevel_str[5]; |
kashish_mbed | 0:bacc6e701fb4 | 288 | uint8_t batteryLevel = 0; |
kashish_mbed | 0:bacc6e701fb4 | 289 | |
kashish_mbed | 0:bacc6e701fb4 | 290 | eAtStatus_t status = Modem_AT_Cmd(AT_GET, AT_BAT, batteryLevel_str); |
kashish_mbed | 0:bacc6e701fb4 | 291 | |
kashish_mbed | 0:bacc6e701fb4 | 292 | if(status == AT_OK) |
kashish_mbed | 0:bacc6e701fb4 | 293 | { |
kashish_mbed | 0:bacc6e701fb4 | 294 | tiny_sscanf(batteryLevel_str, "%hhu", &batteryLevel); |
kashish_mbed | 0:bacc6e701fb4 | 295 | } |
kashish_mbed | 0:bacc6e701fb4 | 296 | |
kashish_mbed | 0:bacc6e701fb4 | 297 | return batteryLevel; |
kashish_mbed | 0:bacc6e701fb4 | 298 | } |
kashish_mbed | 0:bacc6e701fb4 | 299 | |
kashish_mbed | 0:bacc6e701fb4 | 300 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 301 | * @Brief : Update the uplink rate |
kashish_mbed | 0:bacc6e701fb4 | 302 | * @Param : time_s seconds |
kashish_mbed | 0:bacc6e701fb4 | 303 | * @Retval : None |
kashish_mbed | 0:bacc6e701fb4 | 304 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 305 | void Lora_updateUplinkRate(uint32_t time_s) |
kashish_mbed | 0:bacc6e701fb4 | 306 | { |
kashish_mbed | 0:bacc6e701fb4 | 307 | TimerSetValue(&SensorMeasureTimer, (time_s * 1000)); |
kashish_mbed | 0:bacc6e701fb4 | 308 | TimerReset(&SensorMeasureTimer); |
kashish_mbed | 0:bacc6e701fb4 | 309 | } |
kashish_mbed | 0:bacc6e701fb4 | 310 | |
kashish_mbed | 0:bacc6e701fb4 | 311 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 312 | * @Brief : Send uplink packet using binary payload |
kashish_mbed | 0:bacc6e701fb4 | 313 | * @Param : Pointer of sSendDataBinary_t variable |
kashish_mbed | 0:bacc6e701fb4 | 314 | * @Retval : AT_OK or other eAtStatus_t statuses |
kashish_mbed | 0:bacc6e701fb4 | 315 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 316 | static eAtStatus_t Lora_SendDataBinary(sSendDataBinary_t *binaryData) |
kashish_mbed | 0:bacc6e701fb4 | 317 | { |
kashish_mbed | 0:bacc6e701fb4 | 318 | return Modem_AT_Cmd(AT_SET, AT_SENDB, binaryData); |
kashish_mbed | 0:bacc6e701fb4 | 319 | } |
kashish_mbed | 0:bacc6e701fb4 | 320 | |
kashish_mbed | 0:bacc6e701fb4 | 321 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 322 | * @Brief : Read the received downlink packet |
kashish_mbed | 0:bacc6e701fb4 | 323 | * @Param : Pointer to sRecvDataBinary_t variable |
kashish_mbed | 0:bacc6e701fb4 | 324 | * @Return : AT_OK or other eAtStatus_t statuses |
kashish_mbed | 0:bacc6e701fb4 | 325 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 326 | static eAtStatus_t Lora_ReadData(sRecvDataBinary_t *rxData) |
kashish_mbed | 0:bacc6e701fb4 | 327 | { |
kashish_mbed | 0:bacc6e701fb4 | 328 | char *rxString = Modem_GetResponseBuffer(); |
kashish_mbed | 0:bacc6e701fb4 | 329 | |
kashish_mbed | 0:bacc6e701fb4 | 330 | // Find the position after : |
kashish_mbed | 0:bacc6e701fb4 | 331 | // e.g. rxdata:0,1,86GF25 |
kashish_mbed | 0:bacc6e701fb4 | 332 | rxString = (strchr(rxString,':') + 1); |
kashish_mbed | 0:bacc6e701fb4 | 333 | |
kashish_mbed | 0:bacc6e701fb4 | 334 | if(rxString == NULL) |
kashish_mbed | 0:bacc6e701fb4 | 335 | { |
kashish_mbed | 0:bacc6e701fb4 | 336 | return AT_ERROR; |
kashish_mbed | 0:bacc6e701fb4 | 337 | } |
kashish_mbed | 0:bacc6e701fb4 | 338 | |
kashish_mbed | 0:bacc6e701fb4 | 339 | if(tiny_sscanf(rxString, "%hhu,%hhu", &(rxData->Ack), &(rxData->Port)) != 2) |
kashish_mbed | 0:bacc6e701fb4 | 340 | { |
kashish_mbed | 0:bacc6e701fb4 | 341 | return AT_ERROR; |
kashish_mbed | 0:bacc6e701fb4 | 342 | } |
kashish_mbed | 0:bacc6e701fb4 | 343 | |
kashish_mbed | 0:bacc6e701fb4 | 344 | // Find the position after the second comma |
kashish_mbed | 0:bacc6e701fb4 | 345 | rxString = strchr((strchr(rxString,',') + 1),',') + 1; |
kashish_mbed | 0:bacc6e701fb4 | 346 | if(rxString == NULL) |
kashish_mbed | 0:bacc6e701fb4 | 347 | { |
kashish_mbed | 0:bacc6e701fb4 | 348 | return AT_ERROR; |
kashish_mbed | 0:bacc6e701fb4 | 349 | } |
kashish_mbed | 0:bacc6e701fb4 | 350 | |
kashish_mbed | 0:bacc6e701fb4 | 351 | rxData->DataSize = stringHexToByteArray(rxString, rxData->Buffer, MAX_PAYLOAD_SIZE); |
kashish_mbed | 0:bacc6e701fb4 | 352 | |
kashish_mbed | 0:bacc6e701fb4 | 353 | return AT_OK; |
kashish_mbed | 0:bacc6e701fb4 | 354 | } |
kashish_mbed | 0:bacc6e701fb4 | 355 | |
kashish_mbed | 0:bacc6e701fb4 | 356 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 357 | * @Brief : LoRa Modem state machine |
kashish_mbed | 0:bacc6e701fb4 | 358 | * @Param : Void |
kashish_mbed | 0:bacc6e701fb4 | 359 | * @Return : None |
kashish_mbed | 0:bacc6e701fb4 | 360 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 361 | void Lora_fsm(void) |
kashish_mbed | 0:bacc6e701fb4 | 362 | { |
kashish_mbed | 0:bacc6e701fb4 | 363 | switch(DeviceState) |
kashish_mbed | 0:bacc6e701fb4 | 364 | { |
kashish_mbed | 0:bacc6e701fb4 | 365 | case CLIENT_INIT: |
kashish_mbed | 0:bacc6e701fb4 | 366 | { |
kashish_mbed | 0:bacc6e701fb4 | 367 | if(is_modem_working() != AT_OK) |
kashish_mbed | 0:bacc6e701fb4 | 368 | { |
kashish_mbed | 0:bacc6e701fb4 | 369 | DBG_PRINTF("AT failed. Resetting HW...\r\n"); |
kashish_mbed | 0:bacc6e701fb4 | 370 | // Modem isn't responding. Execute hard reset. |
kashish_mbed | 0:bacc6e701fb4 | 371 | Modem_HardReset(); |
kashish_mbed | 0:bacc6e701fb4 | 372 | // We stay in CLIENT_INIT state and try again. |
kashish_mbed | 0:bacc6e701fb4 | 373 | } |
kashish_mbed | 0:bacc6e701fb4 | 374 | else |
kashish_mbed | 0:bacc6e701fb4 | 375 | { |
kashish_mbed | 0:bacc6e701fb4 | 376 | DeviceState = CLIENT_CONFIG; |
kashish_mbed | 0:bacc6e701fb4 | 377 | } |
kashish_mbed | 0:bacc6e701fb4 | 378 | |
kashish_mbed | 0:bacc6e701fb4 | 379 | break; |
kashish_mbed | 0:bacc6e701fb4 | 380 | } |
kashish_mbed | 0:bacc6e701fb4 | 381 | case CLIENT_CONFIG: |
kashish_mbed | 0:bacc6e701fb4 | 382 | { |
kashish_mbed | 0:bacc6e701fb4 | 383 | eAtStatus_t loraModemRetCode = Lora_setDevEui(LoraConfigParam->DevEui); |
kashish_mbed | 0:bacc6e701fb4 | 384 | loraModemRetCode |= Lora_setAppEui(LoraConfigParam->AppEui); |
kashish_mbed | 0:bacc6e701fb4 | 385 | loraModemRetCode |= Lora_setAppKey(LoraConfigParam->AppKey); |
kashish_mbed | 0:bacc6e701fb4 | 386 | loraModemRetCode |= Lora_setJoinMode(LoraConfigParam->JoinMode); |
kashish_mbed | 0:bacc6e701fb4 | 387 | loraModemRetCode |= Lora_setClass(LoraConfigParam->Class); |
kashish_mbed | 0:bacc6e701fb4 | 388 | loraModemRetCode |= Lora_setAdr(LoraConfigParam->AdrStatus); |
kashish_mbed | 0:bacc6e701fb4 | 389 | |
kashish_mbed | 0:bacc6e701fb4 | 390 | if(loraModemRetCode == AT_OK) |
kashish_mbed | 0:bacc6e701fb4 | 391 | { |
kashish_mbed | 0:bacc6e701fb4 | 392 | // If users use AUTO as Device EUI, print the DevEUI |
kashish_mbed | 0:bacc6e701fb4 | 393 | // so that they can add that on X-ON |
kashish_mbed | 0:bacc6e701fb4 | 394 | Lora_printDevEui(); |
kashish_mbed | 0:bacc6e701fb4 | 395 | |
kashish_mbed | 0:bacc6e701fb4 | 396 | setJoinRequestTimer(); |
kashish_mbed | 0:bacc6e701fb4 | 397 | DeviceState = CLIENT_SLEEP; |
kashish_mbed | 0:bacc6e701fb4 | 398 | } |
kashish_mbed | 0:bacc6e701fb4 | 399 | else if(loraModemRetCode == AT_TIMEOUT) |
kashish_mbed | 0:bacc6e701fb4 | 400 | { |
kashish_mbed | 0:bacc6e701fb4 | 401 | DeviceState = CLIENT_INIT; |
kashish_mbed | 0:bacc6e701fb4 | 402 | } |
kashish_mbed | 0:bacc6e701fb4 | 403 | else |
kashish_mbed | 0:bacc6e701fb4 | 404 | { |
kashish_mbed | 0:bacc6e701fb4 | 405 | DBG_PRINTF("Check your keys\r\n"); |
kashish_mbed | 0:bacc6e701fb4 | 406 | while(1); |
kashish_mbed | 0:bacc6e701fb4 | 407 | } |
kashish_mbed | 0:bacc6e701fb4 | 408 | |
kashish_mbed | 0:bacc6e701fb4 | 409 | break; |
kashish_mbed | 0:bacc6e701fb4 | 410 | } |
kashish_mbed | 0:bacc6e701fb4 | 411 | case CLIENT_JOIN: |
kashish_mbed | 0:bacc6e701fb4 | 412 | { |
kashish_mbed | 0:bacc6e701fb4 | 413 | DBG_PRINTF("Joining...\r\n"); |
kashish_mbed | 0:bacc6e701fb4 | 414 | HXC_BSP_RGB_Off(); |
kashish_mbed | 0:bacc6e701fb4 | 415 | |
kashish_mbed | 0:bacc6e701fb4 | 416 | switch(Lora_Join()) |
kashish_mbed | 0:bacc6e701fb4 | 417 | { |
kashish_mbed | 0:bacc6e701fb4 | 418 | case AT_OK: |
kashish_mbed | 0:bacc6e701fb4 | 419 | { |
kashish_mbed | 0:bacc6e701fb4 | 420 | // Start the Join status request timer and go to sleep |
kashish_mbed | 0:bacc6e701fb4 | 421 | TimerStart(&JoinStatusReqTimer); |
kashish_mbed | 0:bacc6e701fb4 | 422 | DeviceState = CLIENT_SLEEP; |
kashish_mbed | 0:bacc6e701fb4 | 423 | break; |
kashish_mbed | 0:bacc6e701fb4 | 424 | } |
kashish_mbed | 0:bacc6e701fb4 | 425 | case AT_TIMEOUT: |
kashish_mbed | 0:bacc6e701fb4 | 426 | { |
kashish_mbed | 0:bacc6e701fb4 | 427 | // The modem isn't responding. Execute hard reset |
kashish_mbed | 0:bacc6e701fb4 | 428 | DeviceState = CLIENT_INIT; |
kashish_mbed | 0:bacc6e701fb4 | 429 | break; |
kashish_mbed | 0:bacc6e701fb4 | 430 | } |
kashish_mbed | 0:bacc6e701fb4 | 431 | default: |
kashish_mbed | 0:bacc6e701fb4 | 432 | { |
kashish_mbed | 0:bacc6e701fb4 | 433 | DBG_PRINTF("Join cmd failed\n"); |
kashish_mbed | 0:bacc6e701fb4 | 434 | // We stay in CLIENT_JOIN state and redo Lora_Join() |
kashish_mbed | 0:bacc6e701fb4 | 435 | break; |
kashish_mbed | 0:bacc6e701fb4 | 436 | } |
kashish_mbed | 0:bacc6e701fb4 | 437 | } |
kashish_mbed | 0:bacc6e701fb4 | 438 | |
kashish_mbed | 0:bacc6e701fb4 | 439 | break; |
kashish_mbed | 0:bacc6e701fb4 | 440 | } |
kashish_mbed | 0:bacc6e701fb4 | 441 | case CLIENT_JOIN_STATUS_CHECK: |
kashish_mbed | 0:bacc6e701fb4 | 442 | { |
kashish_mbed | 0:bacc6e701fb4 | 443 | if(Lora_getJoinStatus() == JOINED) |
kashish_mbed | 0:bacc6e701fb4 | 444 | { |
kashish_mbed | 0:bacc6e701fb4 | 445 | // Indicate Join status using LED |
kashish_mbed | 0:bacc6e701fb4 | 446 | HXC_BSP_RGB_On(PINK); |
kashish_mbed | 0:bacc6e701fb4 | 447 | DBG_PRINTF("Nwk Joined\n"); |
kashish_mbed | 0:bacc6e701fb4 | 448 | Lora_printDevAdr(); |
kashish_mbed | 0:bacc6e701fb4 | 449 | |
kashish_mbed | 0:bacc6e701fb4 | 450 | // Start timer for Re-Authentication |
kashish_mbed | 0:bacc6e701fb4 | 451 | TimerStart(&RejoinTimer); |
kashish_mbed | 0:bacc6e701fb4 | 452 | |
kashish_mbed | 0:bacc6e701fb4 | 453 | // Start timer for uplink transmission for sensor-data |
kashish_mbed | 0:bacc6e701fb4 | 454 | TimerStart(&SensorMeasureTimer); |
kashish_mbed | 0:bacc6e701fb4 | 455 | DeviceState = CLIENT_SLEEP; |
kashish_mbed | 0:bacc6e701fb4 | 456 | } |
kashish_mbed | 0:bacc6e701fb4 | 457 | else |
kashish_mbed | 0:bacc6e701fb4 | 458 | { |
kashish_mbed | 0:bacc6e701fb4 | 459 | // Try joining again |
kashish_mbed | 0:bacc6e701fb4 | 460 | setJoinRequestTimer(); |
kashish_mbed | 0:bacc6e701fb4 | 461 | DeviceState = CLIENT_SLEEP; |
kashish_mbed | 0:bacc6e701fb4 | 462 | } |
kashish_mbed | 0:bacc6e701fb4 | 463 | break; |
kashish_mbed | 0:bacc6e701fb4 | 464 | } |
kashish_mbed | 0:bacc6e701fb4 | 465 | case CLIENT_SLEEP: |
kashish_mbed | 0:bacc6e701fb4 | 466 | { |
kashish_mbed | 0:bacc6e701fb4 | 467 | /* Wake up through RTC events or asynchronous event coming from HXC modem*/ |
kashish_mbed | 0:bacc6e701fb4 | 468 | if(Modem_IsNewDataReceived() == true) |
kashish_mbed | 0:bacc6e701fb4 | 469 | { |
kashish_mbed | 0:bacc6e701fb4 | 470 | DeviceState = CLIENT_DATA_RECEIVED; |
kashish_mbed | 0:bacc6e701fb4 | 471 | } |
kashish_mbed | 0:bacc6e701fb4 | 472 | break; |
kashish_mbed | 0:bacc6e701fb4 | 473 | } |
kashish_mbed | 0:bacc6e701fb4 | 474 | case CLIENT_SEND: |
kashish_mbed | 0:bacc6e701fb4 | 475 | { |
kashish_mbed | 0:bacc6e701fb4 | 476 | // Read sensor data and populate payload |
kashish_mbed | 0:bacc6e701fb4 | 477 | SendDataBinary.DataSize = LoraDriverParam->UplinkHandler(\ |
kashish_mbed | 0:bacc6e701fb4 | 478 | SendDataBinary.Buffer ,\ |
kashish_mbed | 0:bacc6e701fb4 | 479 | MAX_PAYLOAD_SIZE ,\ |
kashish_mbed | 0:bacc6e701fb4 | 480 | &SendDataBinary.Ack ,\ |
kashish_mbed | 0:bacc6e701fb4 | 481 | &SendDataBinary.Port ); |
kashish_mbed | 0:bacc6e701fb4 | 482 | |
kashish_mbed | 0:bacc6e701fb4 | 483 | // Initiate uplink transmission |
kashish_mbed | 0:bacc6e701fb4 | 484 | eAtStatus_t status = Lora_SendDataBinary(&SendDataBinary); |
kashish_mbed | 0:bacc6e701fb4 | 485 | |
kashish_mbed | 0:bacc6e701fb4 | 486 | if (status == AT_OK) |
kashish_mbed | 0:bacc6e701fb4 | 487 | { |
kashish_mbed | 0:bacc6e701fb4 | 488 | DBG_PRINTF("Uplink sent\n"); |
kashish_mbed | 0:bacc6e701fb4 | 489 | // Schedule the next packet |
kashish_mbed | 0:bacc6e701fb4 | 490 | TimerStart(&SensorMeasureTimer); |
kashish_mbed | 0:bacc6e701fb4 | 491 | DeviceState = CLIENT_SLEEP; |
kashish_mbed | 0:bacc6e701fb4 | 492 | } |
kashish_mbed | 0:bacc6e701fb4 | 493 | else if(status == AT_TIMEOUT) |
kashish_mbed | 0:bacc6e701fb4 | 494 | { |
kashish_mbed | 0:bacc6e701fb4 | 495 | // Device isn't responding. Go to init. |
kashish_mbed | 0:bacc6e701fb4 | 496 | DeviceState = CLIENT_INIT; |
kashish_mbed | 0:bacc6e701fb4 | 497 | } |
kashish_mbed | 0:bacc6e701fb4 | 498 | else if(status == AT_NO_NET_JOINED) |
kashish_mbed | 0:bacc6e701fb4 | 499 | { |
kashish_mbed | 0:bacc6e701fb4 | 500 | DeviceState = CLIENT_JOIN; |
kashish_mbed | 0:bacc6e701fb4 | 501 | } |
kashish_mbed | 0:bacc6e701fb4 | 502 | else |
kashish_mbed | 0:bacc6e701fb4 | 503 | { |
kashish_mbed | 0:bacc6e701fb4 | 504 | DBG_PRINTF("Uplink Failed (Error: %d)\n", (uint8_t)status); |
kashish_mbed | 0:bacc6e701fb4 | 505 | } |
kashish_mbed | 0:bacc6e701fb4 | 506 | break; |
kashish_mbed | 0:bacc6e701fb4 | 507 | } |
kashish_mbed | 0:bacc6e701fb4 | 508 | case CLIENT_DATA_RECEIVED: |
kashish_mbed | 0:bacc6e701fb4 | 509 | { |
kashish_mbed | 0:bacc6e701fb4 | 510 | uint8_t rBuffer[64]; |
kashish_mbed | 0:bacc6e701fb4 | 511 | sRecvDataBinary_t rxPacket = {.Buffer = rBuffer}; |
kashish_mbed | 0:bacc6e701fb4 | 512 | |
kashish_mbed | 0:bacc6e701fb4 | 513 | if(Lora_ReadData(&rxPacket) == AT_OK) |
kashish_mbed | 0:bacc6e701fb4 | 514 | { |
kashish_mbed | 0:bacc6e701fb4 | 515 | // Execute users ReceivedPacketHandler function |
kashish_mbed | 0:bacc6e701fb4 | 516 | LoraDriverParam->DownlinkHandler(rxPacket.Buffer, rxPacket.DataSize, rxPacket.Ack, rxPacket.Port); |
kashish_mbed | 0:bacc6e701fb4 | 517 | } |
kashish_mbed | 0:bacc6e701fb4 | 518 | else |
kashish_mbed | 0:bacc6e701fb4 | 519 | { |
kashish_mbed | 0:bacc6e701fb4 | 520 | DBG_PRINTF("Dwlink read failed\n"); |
kashish_mbed | 0:bacc6e701fb4 | 521 | } |
kashish_mbed | 0:bacc6e701fb4 | 522 | |
kashish_mbed | 0:bacc6e701fb4 | 523 | DeviceState = CLIENT_SLEEP; |
kashish_mbed | 0:bacc6e701fb4 | 524 | break; |
kashish_mbed | 0:bacc6e701fb4 | 525 | } |
kashish_mbed | 0:bacc6e701fb4 | 526 | default: |
kashish_mbed | 0:bacc6e701fb4 | 527 | { |
kashish_mbed | 0:bacc6e701fb4 | 528 | DeviceState = CLIENT_INIT; |
kashish_mbed | 0:bacc6e701fb4 | 529 | break; |
kashish_mbed | 0:bacc6e701fb4 | 530 | } |
kashish_mbed | 0:bacc6e701fb4 | 531 | } |
kashish_mbed | 0:bacc6e701fb4 | 532 | } |
kashish_mbed | 0:bacc6e701fb4 | 533 | |
kashish_mbed | 0:bacc6e701fb4 | 534 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 535 | * @Brief : Set Join request timer |
kashish_mbed | 0:bacc6e701fb4 | 536 | * @Param : none |
kashish_mbed | 0:bacc6e701fb4 | 537 | * @Return : none |
kashish_mbed | 0:bacc6e701fb4 | 538 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 539 | static void setJoinRequestTimer(void) |
kashish_mbed | 0:bacc6e701fb4 | 540 | { |
kashish_mbed | 0:bacc6e701fb4 | 541 | // Use a random delay to avoid synchronized join request from all LoRa node after power up |
kashish_mbed | 0:bacc6e701fb4 | 542 | uint32_t joinDelay = (HAL_RNG_GetRandomNumber(&RngHandle) % JOIN_SEND_DELAY_MAX) + 1; |
kashish_mbed | 0:bacc6e701fb4 | 543 | |
kashish_mbed | 0:bacc6e701fb4 | 544 | TimerSetValue(&JoinRequestTimer, joinDelay); |
kashish_mbed | 0:bacc6e701fb4 | 545 | TimerStart(&JoinRequestTimer); |
kashish_mbed | 0:bacc6e701fb4 | 546 | } |
kashish_mbed | 0:bacc6e701fb4 | 547 | |
kashish_mbed | 0:bacc6e701fb4 | 548 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 549 | * @Brief : Function executed on JoinStatusDelayTimer Timeout event |
kashish_mbed | 0:bacc6e701fb4 | 550 | * @Param : none |
kashish_mbed | 0:bacc6e701fb4 | 551 | * @Return : none |
kashish_mbed | 0:bacc6e701fb4 | 552 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 553 | static void OnJoinRequestTimerEvt(void) |
kashish_mbed | 0:bacc6e701fb4 | 554 | { |
kashish_mbed | 0:bacc6e701fb4 | 555 | TimerStop(&JoinRequestTimer); |
kashish_mbed | 0:bacc6e701fb4 | 556 | // If this is a re-join SensorMeasureTimer is active |
kashish_mbed | 0:bacc6e701fb4 | 557 | TimerStop(&SensorMeasureTimer); |
kashish_mbed | 0:bacc6e701fb4 | 558 | |
kashish_mbed | 0:bacc6e701fb4 | 559 | DeviceState = CLIENT_JOIN; |
kashish_mbed | 0:bacc6e701fb4 | 560 | } |
kashish_mbed | 0:bacc6e701fb4 | 561 | |
kashish_mbed | 0:bacc6e701fb4 | 562 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 563 | * @Brief : Function executed on JoinStatusDelayTimer Timeout event |
kashish_mbed | 0:bacc6e701fb4 | 564 | * @Param : none |
kashish_mbed | 0:bacc6e701fb4 | 565 | * @Return : none |
kashish_mbed | 0:bacc6e701fb4 | 566 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 567 | static void OnJoinStatusReqTimerEvt(void) |
kashish_mbed | 0:bacc6e701fb4 | 568 | { |
kashish_mbed | 0:bacc6e701fb4 | 569 | TimerStop(&JoinStatusReqTimer); |
kashish_mbed | 0:bacc6e701fb4 | 570 | DeviceState = CLIENT_JOIN_STATUS_CHECK; |
kashish_mbed | 0:bacc6e701fb4 | 571 | } |
kashish_mbed | 0:bacc6e701fb4 | 572 | |
kashish_mbed | 0:bacc6e701fb4 | 573 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 574 | * @Brief : Function executed on SensorMeasureTimer Timeout event |
kashish_mbed | 0:bacc6e701fb4 | 575 | * @Param : none |
kashish_mbed | 0:bacc6e701fb4 | 576 | * @Return : none |
kashish_mbed | 0:bacc6e701fb4 | 577 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 578 | static void OnSensorMeasureTimerEvt(void) |
kashish_mbed | 0:bacc6e701fb4 | 579 | { |
kashish_mbed | 0:bacc6e701fb4 | 580 | TimerStop(&SensorMeasureTimer); |
kashish_mbed | 0:bacc6e701fb4 | 581 | DeviceState = CLIENT_SEND; |
kashish_mbed | 0:bacc6e701fb4 | 582 | } |
kashish_mbed | 0:bacc6e701fb4 | 583 | |
kashish_mbed | 0:bacc6e701fb4 | 584 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 585 | * @Brief : Function executed on RejoinTimer Timeout event |
kashish_mbed | 0:bacc6e701fb4 | 586 | * @Param : none |
kashish_mbed | 0:bacc6e701fb4 | 587 | * @Return : none |
kashish_mbed | 0:bacc6e701fb4 | 588 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 589 | static void OnRejoinTimerEvent(void) |
kashish_mbed | 0:bacc6e701fb4 | 590 | { |
kashish_mbed | 0:bacc6e701fb4 | 591 | TimerStop(&RejoinTimer); |
kashish_mbed | 0:bacc6e701fb4 | 592 | setJoinRequestTimer(); |
kashish_mbed | 0:bacc6e701fb4 | 593 | } |
kashish_mbed | 0:bacc6e701fb4 | 594 | |
kashish_mbed | 0:bacc6e701fb4 | 595 | /****************************************************************************** |
kashish_mbed | 0:bacc6e701fb4 | 596 | * @brief RNG MSP Initialization |
kashish_mbed | 0:bacc6e701fb4 | 597 | * This function configures the hardware resources used in this example: |
kashish_mbed | 0:bacc6e701fb4 | 598 | * - Peripheral's clock enable |
kashish_mbed | 0:bacc6e701fb4 | 599 | * @param hrng: RNG handle pointer |
kashish_mbed | 0:bacc6e701fb4 | 600 | * @retval None |
kashish_mbed | 0:bacc6e701fb4 | 601 | ******************************************************************************/ |
kashish_mbed | 0:bacc6e701fb4 | 602 | void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng) |
kashish_mbed | 0:bacc6e701fb4 | 603 | { |
kashish_mbed | 0:bacc6e701fb4 | 604 | /* RNG Peripheral clock enable */ |
kashish_mbed | 0:bacc6e701fb4 | 605 | __HAL_RCC_RNG_CLK_ENABLE(); |
kashish_mbed | 0:bacc6e701fb4 | 606 | } |
kashish_mbed | 0:bacc6e701fb4 | 607 | |
kashish_mbed | 0:bacc6e701fb4 | 608 |