L3STIC / Mbed 2 deprecated LoRaWAN-MULTI_RTC_heure-V04

Dependencies:   mbed LoRaWAN-lib SX1272Lib

Committer:
MGstic
Date:
Wed Feb 06 19:16:44 2019 +0000
Revision:
11:8407ff54c40e
Parent:
10:1a85ff06be1a
V04;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mluis 0:45496a70a8a5 1 #include "mbed.h"
mluis 0:45496a70a8a5 2 #include "board.h"
mluis 0:45496a70a8a5 3 #include "radio.h"
mluis 0:45496a70a8a5 4 #include "LoRaMac.h"
mluis 7:5077515c163b 5 #include "Commissioning.h"
mluis 0:45496a70a8a5 6 #include "SerialDisplay.h"
MGstic 10:1a85ff06be1a 7 #include "unixtimestamp.h"
MGstic 10:1a85ff06be1a 8 #include <stdio.h>
MGstic 10:1a85ff06be1a 9 #include <time.h>
mluis 0:45496a70a8a5 10
mluis 0:45496a70a8a5 11
mluis 0:45496a70a8a5 12 /*!
MGstic 10:1a85ff06be1a 13 * Defines the application data transmission duty cycle. 1s, value in [ms].
mluis 0:45496a70a8a5 14 */
MGstic 11:8407ff54c40e 15 #define APP_TX_DUTYCYCLE 500
MGstic 11:8407ff54c40e 16 #define TAB_MAX 20
MGstic 11:8407ff54c40e 17 #define APP_TX_DUTYCYCLE_RND 5
mluis 0:45496a70a8a5 18
MGstic 8:cfe39840b9fb 19 #define LORAWAN_DEFAULT_DATARATE DR_5
mluis 0:45496a70a8a5 20 #define LORAWAN_CONFIRMED_MSG_ON true
mluis 0:45496a70a8a5 21
mluis 0:45496a70a8a5 22 /*!
mluis 0:45496a70a8a5 23 * LoRaWAN Adaptive Data Rate
mluis 0:45496a70a8a5 24 *
mluis 0:45496a70a8a5 25 * \remark Please note that when ADR is enabled the end-device should be static
mluis 0:45496a70a8a5 26 */
mluis 0:45496a70a8a5 27 #define LORAWAN_ADR_ON 1
mluis 0:45496a70a8a5 28
mluis 0:45496a70a8a5 29 #include "LoRaMacTest.h"
mluis 0:45496a70a8a5 30
mluis 0:45496a70a8a5 31 /*!
MGstic 10:1a85ff06be1a 32 * LoRaWAN ETSI European Telecommunications Standards Institute
MGstic 10:1a85ff06be1a 33 * duty cycle control enable/disable
MGstic 10:1a85ff06be1a 34 * remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
mluis 0:45496a70a8a5 35 */
mluis 7:5077515c163b 36 #define LORAWAN_DUTYCYCLE_ON false
mluis 0:45496a70a8a5 37
mluis 3:3152aa75c58d 38 #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 1
mluis 3:3152aa75c58d 39
MGstic 10:1a85ff06be1a 40 //############INITIALISATION LIAISON SERIE####################
MGstic 10:1a85ff06be1a 41
MGstic 10:1a85ff06be1a 42 void saisie_serie(int tab[6]);
MGstic 11:8407ff54c40e 43 //int tabd[6]= {0,0,0,1,1,2018};
MGstic 11:8407ff54c40e 44 int tabd[6];
MGstic 10:1a85ff06be1a 45
MGstic 10:1a85ff06be1a 46 InterruptIn bouton(USER_BUTTON); // Déclaration de l'interruption extern activée par le bouton utilisateur bleu
MGstic 10:1a85ff06be1a 47 //Serial pc(USBTX, USBRX); // tx, rx//Serial pc(SERIAL_TX, SERIAL_RX); // Déclaration de entrées pour la liaison série
MGstic 10:1a85ff06be1a 48 int etat=0;
MGstic 10:1a85ff06be1a 49 void interruption_extern_bouton(void)
MGstic 10:1a85ff06be1a 50 {
MGstic 10:1a85ff06be1a 51 etat = not(etat); // changement d'etat
MGstic 10:1a85ff06be1a 52 }
MGstic 10:1a85ff06be1a 53 //############################################################
mluis 3:3152aa75c58d 54
mluis 3:3152aa75c58d 55 #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 3:3152aa75c58d 56 #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 3:3152aa75c58d 57 #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 3:3152aa75c58d 58 #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 3:3152aa75c58d 59 #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
mluis 3:3152aa75c58d 60 #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 }
mluis 3:3152aa75c58d 61 #define LC10 { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 }
mluis 3:3152aa75c58d 62
mluis 0:45496a70a8a5 63 /*!
mluis 0:45496a70a8a5 64 * LoRaWAN application port
mluis 0:45496a70a8a5 65 */
mluis 0:45496a70a8a5 66 #define LORAWAN_APP_PORT 15
mluis 0:45496a70a8a5 67
mluis 0:45496a70a8a5 68 /*!
mluis 0:45496a70a8a5 69 * User application data buffer size
mluis 0:45496a70a8a5 70 */
MGstic 10:1a85ff06be1a 71 #define LORAWAN_APP_DATA_SIZE 24
mluis 0:45496a70a8a5 72
mluis 0:45496a70a8a5 73 static uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
mluis 0:45496a70a8a5 74 static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
mluis 0:45496a70a8a5 75 static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
mluis 0:45496a70a8a5 76 static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
mluis 0:45496a70a8a5 77 static uint8_t AppSKey[] = LORAWAN_APPSKEY;
mluis 0:45496a70a8a5 78
mluis 0:45496a70a8a5 79 /*!
mluis 0:45496a70a8a5 80 * Device address
mluis 0:45496a70a8a5 81 */
mluis 0:45496a70a8a5 82 static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS;
mluis 0:45496a70a8a5 83
mluis 0:45496a70a8a5 84 /*!
mluis 0:45496a70a8a5 85 * Application port
mluis 0:45496a70a8a5 86 */
mluis 0:45496a70a8a5 87 static uint8_t AppPort = LORAWAN_APP_PORT;
mluis 0:45496a70a8a5 88
mluis 0:45496a70a8a5 89 /*!
mluis 0:45496a70a8a5 90 * User application data size
mluis 0:45496a70a8a5 91 */
mluis 0:45496a70a8a5 92 static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 0:45496a70a8a5 93
mluis 0:45496a70a8a5 94 /*!
mluis 0:45496a70a8a5 95 * User application data buffer size
mluis 0:45496a70a8a5 96 */
mluis 0:45496a70a8a5 97 #define LORAWAN_APP_DATA_MAX_SIZE 64
mluis 0:45496a70a8a5 98
mluis 0:45496a70a8a5 99 /*!
mluis 0:45496a70a8a5 100 * User application data
mluis 0:45496a70a8a5 101 */
mluis 0:45496a70a8a5 102 static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
mluis 0:45496a70a8a5 103
mluis 0:45496a70a8a5 104 /*!
mluis 0:45496a70a8a5 105 * Indicates if the node is sending confirmed or unconfirmed messages
mluis 0:45496a70a8a5 106 */
mluis 0:45496a70a8a5 107 static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 0:45496a70a8a5 108
mluis 0:45496a70a8a5 109 /*!
mluis 0:45496a70a8a5 110 * Defines the application data transmission duty cycle
mluis 0:45496a70a8a5 111 */
mluis 0:45496a70a8a5 112 static uint32_t TxDutyCycleTime;
mluis 0:45496a70a8a5 113
mluis 0:45496a70a8a5 114 /*!
mluis 0:45496a70a8a5 115 * Timer to handle the application data transmission duty cycle
mluis 0:45496a70a8a5 116 */
mluis 0:45496a70a8a5 117 static TimerEvent_t TxNextPacketTimer;
mluis 0:45496a70a8a5 118
MGstic 10:1a85ff06be1a 119 /*
mluis 0:45496a70a8a5 120 * Indicates if a new packet can be sent
mluis 0:45496a70a8a5 121 */
mluis 0:45496a70a8a5 122 static bool NextTx = true;
mluis 0:45496a70a8a5 123
mluis 0:45496a70a8a5 124 /*!
mluis 0:45496a70a8a5 125 * Device states
mluis 0:45496a70a8a5 126 */
mluis 7:5077515c163b 127 static enum eDeviceState
mluis 0:45496a70a8a5 128 {
mluis 0:45496a70a8a5 129 DEVICE_STATE_INIT,
mluis 0:45496a70a8a5 130 DEVICE_STATE_JOIN,
mluis 0:45496a70a8a5 131 DEVICE_STATE_SEND,
mluis 0:45496a70a8a5 132 DEVICE_STATE_CYCLE,
mluis 0:45496a70a8a5 133 DEVICE_STATE_SLEEP
mluis 0:45496a70a8a5 134 }DeviceState;
mluis 0:45496a70a8a5 135
mluis 0:45496a70a8a5 136 /*!
mluis 0:45496a70a8a5 137 * LoRaWAN compliance tests support data
mluis 0:45496a70a8a5 138 */
mluis 0:45496a70a8a5 139 struct ComplianceTest_s
mluis 0:45496a70a8a5 140 {
mluis 0:45496a70a8a5 141 bool Running;
mluis 0:45496a70a8a5 142 uint8_t State;
mluis 0:45496a70a8a5 143 bool IsTxConfirmed;
mluis 0:45496a70a8a5 144 uint8_t AppPort;
mluis 0:45496a70a8a5 145 uint8_t AppDataSize;
mluis 0:45496a70a8a5 146 uint8_t *AppDataBuffer;
mluis 0:45496a70a8a5 147 uint16_t DownLinkCounter;
mluis 0:45496a70a8a5 148 bool LinkCheck;
mluis 0:45496a70a8a5 149 uint8_t DemodMargin;
mluis 0:45496a70a8a5 150 uint8_t NbGateways;
mluis 0:45496a70a8a5 151 }ComplianceTest;
mluis 0:45496a70a8a5 152
mluis 0:45496a70a8a5 153 /*!
mluis 0:45496a70a8a5 154 * Indicates if the MAC layer network join status has changed.
mluis 0:45496a70a8a5 155 */
mluis 0:45496a70a8a5 156 static bool IsNetworkJoinedStatusUpdate = false;
mluis 0:45496a70a8a5 157
mluis 0:45496a70a8a5 158 /*!
mluis 0:45496a70a8a5 159 * Strucure containing the Uplink status
mluis 0:45496a70a8a5 160 */
mluis 0:45496a70a8a5 161 struct sLoRaMacUplinkStatus
mluis 0:45496a70a8a5 162 {
mluis 0:45496a70a8a5 163 uint8_t Acked;
mluis 0:45496a70a8a5 164 int8_t Datarate;
mluis 0:45496a70a8a5 165 uint16_t UplinkCounter;
mluis 0:45496a70a8a5 166 uint8_t Port;
mluis 0:45496a70a8a5 167 uint8_t *Buffer;
mluis 0:45496a70a8a5 168 uint8_t BufferSize;
mluis 0:45496a70a8a5 169 }LoRaMacUplinkStatus;
mluis 0:45496a70a8a5 170 volatile bool UplinkStatusUpdated = false;
mluis 0:45496a70a8a5 171
mluis 0:45496a70a8a5 172 /*!
mluis 0:45496a70a8a5 173 * Strucure containing the Downlink status
mluis 0:45496a70a8a5 174 */
mluis 0:45496a70a8a5 175 struct sLoRaMacDownlinkStatus
mluis 0:45496a70a8a5 176 {
mluis 0:45496a70a8a5 177 int16_t Rssi;
mluis 0:45496a70a8a5 178 int8_t Snr;
mluis 0:45496a70a8a5 179 uint16_t DownlinkCounter;
mluis 0:45496a70a8a5 180 bool RxData;
mluis 0:45496a70a8a5 181 uint8_t Port;
mluis 0:45496a70a8a5 182 uint8_t *Buffer;
mluis 0:45496a70a8a5 183 uint8_t BufferSize;
mluis 0:45496a70a8a5 184 }LoRaMacDownlinkStatus;
mluis 0:45496a70a8a5 185 volatile bool DownlinkStatusUpdated = false;
mluis 0:45496a70a8a5 186
MGstic 11:8407ff54c40e 187 void SerialDisplayRefresh( int tabd[6] )
mluis 0:45496a70a8a5 188 {
mluis 0:45496a70a8a5 189 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 190
mluis 0:45496a70a8a5 191 SerialDisplayInit( );
mluis 0:45496a70a8a5 192 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
mluis 0:45496a70a8a5 193
mluis 0:45496a70a8a5 194 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 0:45496a70a8a5 195 SerialDisplayUpdateDevAddr( DevAddr );
mluis 0:45496a70a8a5 196 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 0:45496a70a8a5 197 SerialDisplayUpdateKey( 13, AppSKey );
mluis 0:45496a70a8a5 198 SerialDisplayUpdateEui( 5, DevEui );
mluis 0:45496a70a8a5 199 SerialDisplayUpdateEui( 6, AppEui );
mluis 0:45496a70a8a5 200 SerialDisplayUpdateKey( 7, AppKey );
mluis 0:45496a70a8a5 201
mluis 0:45496a70a8a5 202 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 203 LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 204 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
mluis 0:45496a70a8a5 205
mluis 0:45496a70a8a5 206 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
mluis 0:45496a70a8a5 207 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
MGstic 10:1a85ff06be1a 208 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
MGstic 10:1a85ff06be1a 209 SerialDisplayRTCactive(etat);
MGstic 10:1a85ff06be1a 210 SerialDisplayTIME(tabd);
MGstic 10:1a85ff06be1a 211
MGstic 10:1a85ff06be1a 212 //SerialDisplayUpdateLedState( 3, AppLedStateOn );
mluis 0:45496a70a8a5 213 }
mluis 0:45496a70a8a5 214
mluis 0:45496a70a8a5 215 void SerialRxProcess( void )
mluis 0:45496a70a8a5 216 {
mluis 0:45496a70a8a5 217 if( SerialDisplayReadable( ) == true )
mluis 0:45496a70a8a5 218 {
mluis 0:45496a70a8a5 219 switch( SerialDisplayGetChar( ) )
mluis 0:45496a70a8a5 220 {
mluis 0:45496a70a8a5 221 case 'R':
mluis 0:45496a70a8a5 222 case 'r':
mluis 0:45496a70a8a5 223 // Refresh Serial screen
MGstic 11:8407ff54c40e 224 SerialDisplayRefresh(tabd);
mluis 0:45496a70a8a5 225 break;
mluis 0:45496a70a8a5 226 default:
mluis 0:45496a70a8a5 227 break;
mluis 0:45496a70a8a5 228 }
mluis 0:45496a70a8a5 229 }
mluis 0:45496a70a8a5 230 }
mluis 0:45496a70a8a5 231
mluis 0:45496a70a8a5 232 /*!
mluis 0:45496a70a8a5 233 * \brief Prepares the payload of the frame
mluis 0:45496a70a8a5 234 */
MGstic 10:1a85ff06be1a 235
MGstic 11:8407ff54c40e 236 int poidstest=34;
MGstic 11:8407ff54c40e 237
MGstic 10:1a85ff06be1a 238
MGstic 10:1a85ff06be1a 239 typedef struct {
MGstic 11:8407ff54c40e 240 int heure[8]; // Augmentation taille prog. Solution : heure = pointeur vers malloc (26 ou sizeof ctime)=1bit
MGstic 11:8407ff54c40e 241 int masse[8];
MGstic 10:1a85ff06be1a 242 } data;
MGstic 10:1a85ff06be1a 243 data data_packet[TAB_MAX];
MGstic 10:1a85ff06be1a 244
MGstic 10:1a85ff06be1a 245 //###################################
MGstic 10:1a85ff06be1a 246
MGstic 10:1a85ff06be1a 247 void copy_time(int data_packet[8],time_t seconds)
mluis 0:45496a70a8a5 248 {
MGstic 10:1a85ff06be1a 249 char temp[32];
MGstic 10:1a85ff06be1a 250 int tempp;
MGstic 10:1a85ff06be1a 251 //printf("=>%s\n",ctime(&seconds));
MGstic 10:1a85ff06be1a 252 //printf("%d\r\n",seconds);
MGstic 10:1a85ff06be1a 253 sprintf(temp,"%d",(int)seconds);// copie de la valeur de la RTC en cours dans le tableau de structure data_packet
MGstic 10:1a85ff06be1a 254 tempp=atoi(temp);
MGstic 10:1a85ff06be1a 255 Whex_buffer(tempp,data_packet);
MGstic 10:1a85ff06be1a 256 }
MGstic 10:1a85ff06be1a 257 //##################################
MGstic 10:1a85ff06be1a 258
MGstic 11:8407ff54c40e 259 static void PrepareTxFrame( uint8_t port,data datat[TAB_MAX],int compt )//, 2EME PARAMETRE TABLEAU DONNEE INIT=>APPDATA DANS FONCION fournir 1 case tableau data_packet
MGstic 11:8407ff54c40e 260 {
mluis 0:45496a70a8a5 261 switch( port )
mluis 0:45496a70a8a5 262 {
mluis 0:45496a70a8a5 263 case 15:
mluis 0:45496a70a8a5 264 {
MGstic 11:8407ff54c40e 265
MGstic 10:1a85ff06be1a 266 for(int h=0;h<8;h++)
MGstic 10:1a85ff06be1a 267 {
MGstic 11:8407ff54c40e 268 AppData[h]=datat[compt].heure[h];
MGstic 10:1a85ff06be1a 269 }
MGstic 10:1a85ff06be1a 270
MGstic 11:8407ff54c40e 271 AppData[16]=datat[compt].masse[0];
mluis 0:45496a70a8a5 272 }
mluis 0:45496a70a8a5 273 break;
mluis 0:45496a70a8a5 274 case 224:
mluis 0:45496a70a8a5 275 if( ComplianceTest.LinkCheck == true )
mluis 0:45496a70a8a5 276 {
mluis 0:45496a70a8a5 277 ComplianceTest.LinkCheck = false;
mluis 0:45496a70a8a5 278 AppDataSize = 3;
mluis 0:45496a70a8a5 279 AppData[0] = 5;
mluis 0:45496a70a8a5 280 AppData[1] = ComplianceTest.DemodMargin;
mluis 0:45496a70a8a5 281 AppData[2] = ComplianceTest.NbGateways;
mluis 0:45496a70a8a5 282 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 283 }
mluis 0:45496a70a8a5 284 else
mluis 0:45496a70a8a5 285 {
mluis 0:45496a70a8a5 286 switch( ComplianceTest.State )
mluis 0:45496a70a8a5 287 {
mluis 0:45496a70a8a5 288 case 4:
mluis 0:45496a70a8a5 289 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 290 break;
mluis 0:45496a70a8a5 291 case 1:
mluis 0:45496a70a8a5 292 AppDataSize = 2;
mluis 0:45496a70a8a5 293 AppData[0] = ComplianceTest.DownLinkCounter >> 8;
mluis 0:45496a70a8a5 294 AppData[1] = ComplianceTest.DownLinkCounter;
mluis 0:45496a70a8a5 295 break;
mluis 0:45496a70a8a5 296 }
mluis 0:45496a70a8a5 297 }
mluis 0:45496a70a8a5 298 break;
mluis 0:45496a70a8a5 299 default:
mluis 0:45496a70a8a5 300 break;
mluis 0:45496a70a8a5 301 }
mluis 0:45496a70a8a5 302 }
mluis 0:45496a70a8a5 303
mluis 0:45496a70a8a5 304 /*!
mluis 0:45496a70a8a5 305 * \brief Prepares the payload of the frame
mluis 0:45496a70a8a5 306 *
mluis 0:45496a70a8a5 307 * \retval [0: frame could be send, 1: error]
mluis 0:45496a70a8a5 308 */
mluis 0:45496a70a8a5 309 static bool SendFrame( void )
mluis 0:45496a70a8a5 310 {
mluis 0:45496a70a8a5 311 McpsReq_t mcpsReq;
mluis 0:45496a70a8a5 312 LoRaMacTxInfo_t txInfo;
mluis 7:5077515c163b 313
mluis 0:45496a70a8a5 314 if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK )
mluis 0:45496a70a8a5 315 {
mluis 0:45496a70a8a5 316 // Send empty frame in order to flush MAC commands
mluis 0:45496a70a8a5 317 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 0:45496a70a8a5 318 mcpsReq.Req.Unconfirmed.fBuffer = NULL;
mluis 0:45496a70a8a5 319 mcpsReq.Req.Unconfirmed.fBufferSize = 0;
mluis 0:45496a70a8a5 320 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 3:3152aa75c58d 321
mluis 0:45496a70a8a5 322 LoRaMacUplinkStatus.Acked = false;
mluis 0:45496a70a8a5 323 LoRaMacUplinkStatus.Port = 0;
mluis 0:45496a70a8a5 324 LoRaMacUplinkStatus.Buffer = NULL;
mluis 0:45496a70a8a5 325 LoRaMacUplinkStatus.BufferSize = 0;
mluis 0:45496a70a8a5 326 SerialDisplayUpdateFrameType( false );
mluis 0:45496a70a8a5 327 }
mluis 0:45496a70a8a5 328 else
mluis 0:45496a70a8a5 329 {
mluis 0:45496a70a8a5 330 LoRaMacUplinkStatus.Acked = false;
mluis 0:45496a70a8a5 331 LoRaMacUplinkStatus.Port = AppPort;
mluis 0:45496a70a8a5 332 LoRaMacUplinkStatus.Buffer = AppData;
mluis 0:45496a70a8a5 333 LoRaMacUplinkStatus.BufferSize = AppDataSize;
mluis 0:45496a70a8a5 334 SerialDisplayUpdateFrameType( IsTxConfirmed );
mluis 0:45496a70a8a5 335
mluis 0:45496a70a8a5 336 if( IsTxConfirmed == false )
mluis 0:45496a70a8a5 337 {
mluis 0:45496a70a8a5 338 mcpsReq.Type = MCPS_UNCONFIRMED;
mluis 0:45496a70a8a5 339 mcpsReq.Req.Unconfirmed.fPort = AppPort;
mluis 0:45496a70a8a5 340 mcpsReq.Req.Unconfirmed.fBuffer = AppData;
mluis 0:45496a70a8a5 341 mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize;
mluis 0:45496a70a8a5 342 mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 0:45496a70a8a5 343 }
mluis 0:45496a70a8a5 344 else
mluis 0:45496a70a8a5 345 {
mluis 0:45496a70a8a5 346 mcpsReq.Type = MCPS_CONFIRMED;
mluis 0:45496a70a8a5 347 mcpsReq.Req.Confirmed.fPort = AppPort;
mluis 0:45496a70a8a5 348 mcpsReq.Req.Confirmed.fBuffer = AppData;
mluis 0:45496a70a8a5 349 mcpsReq.Req.Confirmed.fBufferSize = AppDataSize;
mluis 2:2eeaeae2c725 350 mcpsReq.Req.Confirmed.NbTrials = 8;
mluis 0:45496a70a8a5 351 mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE;
mluis 0:45496a70a8a5 352 }
mluis 0:45496a70a8a5 353 }
mluis 0:45496a70a8a5 354
mluis 0:45496a70a8a5 355 if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK )
mluis 0:45496a70a8a5 356 {
MGstic 11:8407ff54c40e 357
mluis 0:45496a70a8a5 358 return false;
mluis 0:45496a70a8a5 359 }
mluis 0:45496a70a8a5 360 return true;
MGstic 11:8407ff54c40e 361
mluis 0:45496a70a8a5 362 }
mluis 0:45496a70a8a5 363
mluis 0:45496a70a8a5 364 /*!
mluis 0:45496a70a8a5 365 * \brief Function executed on TxNextPacket Timeout event
mluis 0:45496a70a8a5 366 */
mluis 0:45496a70a8a5 367 static void OnTxNextPacketTimerEvent( void )
mluis 0:45496a70a8a5 368 {
mluis 0:45496a70a8a5 369 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 370 LoRaMacStatus_t status;
mluis 0:45496a70a8a5 371
mluis 0:45496a70a8a5 372 TimerStop( &TxNextPacketTimer );
mluis 0:45496a70a8a5 373
mluis 0:45496a70a8a5 374 mibReq.Type = MIB_NETWORK_JOINED;
mluis 0:45496a70a8a5 375 status = LoRaMacMibGetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 376
mluis 0:45496a70a8a5 377 if( status == LORAMAC_STATUS_OK )
mluis 0:45496a70a8a5 378 {
mluis 0:45496a70a8a5 379 if( mibReq.Param.IsNetworkJoined == true )
mluis 0:45496a70a8a5 380 {
mluis 0:45496a70a8a5 381 DeviceState = DEVICE_STATE_SEND;
mluis 0:45496a70a8a5 382 NextTx = true;
mluis 0:45496a70a8a5 383 }
mluis 0:45496a70a8a5 384 else
mluis 0:45496a70a8a5 385 {
mluis 0:45496a70a8a5 386 DeviceState = DEVICE_STATE_JOIN;
mluis 0:45496a70a8a5 387 }
mluis 0:45496a70a8a5 388 }
mluis 0:45496a70a8a5 389 }
mluis 0:45496a70a8a5 390
MGstic 10:1a85ff06be1a 391 /*
mluis 0:45496a70a8a5 392 * \brief MCPS-Confirm event function
mluis 0:45496a70a8a5 393 *
mluis 3:3152aa75c58d 394 * \param [IN] mcpsConfirm - Pointer to the confirm structure,
mluis 0:45496a70a8a5 395 * containing confirm attributes.
mluis 0:45496a70a8a5 396 */
MGstic 10:1a85ff06be1a 397
mluis 3:3152aa75c58d 398 static void McpsConfirm( McpsConfirm_t *mcpsConfirm )
mluis 0:45496a70a8a5 399 {
mluis 3:3152aa75c58d 400 if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 401 {
mluis 3:3152aa75c58d 402 switch( mcpsConfirm->McpsRequest )
mluis 0:45496a70a8a5 403 {
mluis 0:45496a70a8a5 404 case MCPS_UNCONFIRMED:
mluis 0:45496a70a8a5 405 {
mluis 0:45496a70a8a5 406 // Check Datarate
mluis 0:45496a70a8a5 407 // Check TxPower
mluis 0:45496a70a8a5 408 break;
mluis 0:45496a70a8a5 409 }
mluis 0:45496a70a8a5 410 case MCPS_CONFIRMED:
mluis 0:45496a70a8a5 411 {
mluis 0:45496a70a8a5 412 // Check Datarate
mluis 0:45496a70a8a5 413 // Check TxPower
mluis 0:45496a70a8a5 414 // Check AckReceived
mluis 3:3152aa75c58d 415 // Check NbTrials
mluis 3:3152aa75c58d 416 LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived;
mluis 0:45496a70a8a5 417 break;
mluis 0:45496a70a8a5 418 }
mluis 0:45496a70a8a5 419 case MCPS_PROPRIETARY:
mluis 0:45496a70a8a5 420 {
mluis 0:45496a70a8a5 421 break;
mluis 0:45496a70a8a5 422 }
mluis 0:45496a70a8a5 423 default:
mluis 0:45496a70a8a5 424 break;
mluis 0:45496a70a8a5 425 }
mluis 3:3152aa75c58d 426 LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate;
mluis 3:3152aa75c58d 427 LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter;
mluis 3:3152aa75c58d 428
MGstic 10:1a85ff06be1a 429 /* Switch LED 1 ON
mluis 5:62862ef9480b 430 Led1State = true;
mluis 5:62862ef9480b 431 Led1StateChanged = true;
MGstic 10:1a85ff06be1a 432 TimerStart( &Led1Timer );*/
mluis 5:62862ef9480b 433
mluis 0:45496a70a8a5 434 UplinkStatusUpdated = true;
mluis 0:45496a70a8a5 435 }
mluis 0:45496a70a8a5 436 NextTx = true;
mluis 0:45496a70a8a5 437 }
mluis 0:45496a70a8a5 438
mluis 0:45496a70a8a5 439 /*!
mluis 0:45496a70a8a5 440 * \brief MCPS-Indication event function
mluis 0:45496a70a8a5 441 *
mluis 3:3152aa75c58d 442 * \param [IN] mcpsIndication - Pointer to the indication structure,
mluis 0:45496a70a8a5 443 * containing indication attributes.
mluis 0:45496a70a8a5 444 */
MGstic 11:8407ff54c40e 445 uint8_t c_send=TAB_MAX;
mluis 3:3152aa75c58d 446 static void McpsIndication( McpsIndication_t *mcpsIndication )
mluis 0:45496a70a8a5 447 {
mluis 3:3152aa75c58d 448 if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 449 {
mluis 0:45496a70a8a5 450 return;
mluis 0:45496a70a8a5 451 }
mluis 0:45496a70a8a5 452
mluis 3:3152aa75c58d 453 switch( mcpsIndication->McpsIndication )
mluis 0:45496a70a8a5 454 {
mluis 0:45496a70a8a5 455 case MCPS_UNCONFIRMED:
mluis 0:45496a70a8a5 456 {
mluis 0:45496a70a8a5 457 break;
mluis 0:45496a70a8a5 458 }
mluis 0:45496a70a8a5 459 case MCPS_CONFIRMED:
mluis 0:45496a70a8a5 460 {
mluis 0:45496a70a8a5 461 break;
mluis 0:45496a70a8a5 462 }
mluis 0:45496a70a8a5 463 case MCPS_PROPRIETARY:
mluis 0:45496a70a8a5 464 {
mluis 0:45496a70a8a5 465 break;
mluis 0:45496a70a8a5 466 }
mluis 0:45496a70a8a5 467 case MCPS_MULTICAST:
mluis 0:45496a70a8a5 468 {
mluis 0:45496a70a8a5 469 break;
mluis 0:45496a70a8a5 470 }
mluis 0:45496a70a8a5 471 default:
mluis 0:45496a70a8a5 472 break;
mluis 0:45496a70a8a5 473 }
mluis 0:45496a70a8a5 474
mluis 0:45496a70a8a5 475 // Check Multicast
mluis 0:45496a70a8a5 476 // Check Port
mluis 0:45496a70a8a5 477 // Check Datarate
mluis 0:45496a70a8a5 478 // Check FramePending
mluis 0:45496a70a8a5 479 // Check Buffer
mluis 0:45496a70a8a5 480 // Check BufferSize
mluis 0:45496a70a8a5 481 // Check Rssi
mluis 0:45496a70a8a5 482 // Check Snr
mluis 0:45496a70a8a5 483 // Check RxSlot
mluis 3:3152aa75c58d 484 LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi;
mluis 3:3152aa75c58d 485 if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1
mluis 0:45496a70a8a5 486 {
mluis 0:45496a70a8a5 487 // Invert and divide by 4
mluis 3:3152aa75c58d 488 LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2;
mluis 0:45496a70a8a5 489 LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr;
mluis 0:45496a70a8a5 490 }
mluis 0:45496a70a8a5 491 else
mluis 0:45496a70a8a5 492 {
mluis 0:45496a70a8a5 493 // Divide by 4
mluis 3:3152aa75c58d 494 LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2;
mluis 0:45496a70a8a5 495 }
mluis 0:45496a70a8a5 496 LoRaMacDownlinkStatus.DownlinkCounter++;
MGstic 11:8407ff54c40e 497 c_send--;
mluis 3:3152aa75c58d 498 LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData;
mluis 3:3152aa75c58d 499 LoRaMacDownlinkStatus.Port = mcpsIndication->Port;
mluis 3:3152aa75c58d 500 LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer;
mluis 3:3152aa75c58d 501 LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize;
mluis 0:45496a70a8a5 502
mluis 0:45496a70a8a5 503 if( ComplianceTest.Running == true )
mluis 0:45496a70a8a5 504 {
mluis 0:45496a70a8a5 505 ComplianceTest.DownLinkCounter++;
mluis 0:45496a70a8a5 506 }
mluis 0:45496a70a8a5 507
mluis 3:3152aa75c58d 508 if( mcpsIndication->RxData == true )
mluis 0:45496a70a8a5 509 {
mluis 3:3152aa75c58d 510 switch( mcpsIndication->Port )
mluis 0:45496a70a8a5 511 {
mluis 0:45496a70a8a5 512 case 1: // The application LED can be controlled on port 1 or 2
mluis 0:45496a70a8a5 513 case 2:
MGstic 10:1a85ff06be1a 514 /*if( mcpsIndication->BufferSize == 1 )
mluis 0:45496a70a8a5 515 {
mluis 3:3152aa75c58d 516 AppLedStateOn = mcpsIndication->Buffer[0] & 0x01;
mluis 0:45496a70a8a5 517 Led3StateChanged = true;
MGstic 10:1a85ff06be1a 518 }*/
mluis 0:45496a70a8a5 519 break;
mluis 0:45496a70a8a5 520 case 224:
mluis 0:45496a70a8a5 521 if( ComplianceTest.Running == false )
mluis 0:45496a70a8a5 522 {
mluis 0:45496a70a8a5 523 // Check compliance test enable command (i)
mluis 3:3152aa75c58d 524 if( ( mcpsIndication->BufferSize == 4 ) &&
mluis 3:3152aa75c58d 525 ( mcpsIndication->Buffer[0] == 0x01 ) &&
mluis 3:3152aa75c58d 526 ( mcpsIndication->Buffer[1] == 0x01 ) &&
mluis 3:3152aa75c58d 527 ( mcpsIndication->Buffer[2] == 0x01 ) &&
mluis 3:3152aa75c58d 528 ( mcpsIndication->Buffer[3] == 0x01 ) )
mluis 0:45496a70a8a5 529 {
mluis 0:45496a70a8a5 530 IsTxConfirmed = false;
mluis 0:45496a70a8a5 531 AppPort = 224;
mluis 0:45496a70a8a5 532 AppDataSize = 2;
mluis 0:45496a70a8a5 533 ComplianceTest.DownLinkCounter = 0;
mluis 0:45496a70a8a5 534 ComplianceTest.LinkCheck = false;
mluis 0:45496a70a8a5 535 ComplianceTest.DemodMargin = 0;
mluis 0:45496a70a8a5 536 ComplianceTest.NbGateways = 0;
mluis 0:45496a70a8a5 537 ComplianceTest.Running = true;
mluis 0:45496a70a8a5 538 ComplianceTest.State = 1;
mluis 7:5077515c163b 539
mluis 0:45496a70a8a5 540 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 541 mibReq.Type = MIB_ADR;
mluis 0:45496a70a8a5 542 mibReq.Param.AdrEnable = true;
mluis 0:45496a70a8a5 543 LoRaMacMibSetRequestConfirm( &mibReq );
MGstic 10:1a85ff06be1a 544 LoRaMacTestSetDutyCycleOn( false );
mluis 0:45496a70a8a5 545
mluis 0:45496a70a8a5 546 }
mluis 0:45496a70a8a5 547 }
mluis 0:45496a70a8a5 548 else
mluis 0:45496a70a8a5 549 {
mluis 3:3152aa75c58d 550 ComplianceTest.State = mcpsIndication->Buffer[0];
mluis 0:45496a70a8a5 551 switch( ComplianceTest.State )
mluis 0:45496a70a8a5 552 {
mluis 0:45496a70a8a5 553 case 0: // Check compliance test disable command (ii)
mluis 0:45496a70a8a5 554 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 0:45496a70a8a5 555 AppPort = LORAWAN_APP_PORT;
mluis 0:45496a70a8a5 556 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 0:45496a70a8a5 557 ComplianceTest.DownLinkCounter = 0;
mluis 0:45496a70a8a5 558 ComplianceTest.Running = false;
mluis 7:5077515c163b 559
mluis 0:45496a70a8a5 560 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 561 mibReq.Type = MIB_ADR;
mluis 0:45496a70a8a5 562 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 0:45496a70a8a5 563 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 564 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
MGstic 10:1a85ff06be1a 565
mluis 0:45496a70a8a5 566 break;
mluis 0:45496a70a8a5 567 case 1: // (iii, iv)
mluis 0:45496a70a8a5 568 AppDataSize = 2;
mluis 0:45496a70a8a5 569 break;
mluis 0:45496a70a8a5 570 case 2: // Enable confirmed messages (v)
mluis 0:45496a70a8a5 571 IsTxConfirmed = true;
mluis 0:45496a70a8a5 572 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 573 break;
mluis 0:45496a70a8a5 574 case 3: // Disable confirmed messages (vi)
mluis 0:45496a70a8a5 575 IsTxConfirmed = false;
mluis 0:45496a70a8a5 576 ComplianceTest.State = 1;
mluis 0:45496a70a8a5 577 break;
mluis 0:45496a70a8a5 578 case 4: // (vii)
mluis 3:3152aa75c58d 579 AppDataSize = mcpsIndication->BufferSize;
mluis 0:45496a70a8a5 580
mluis 0:45496a70a8a5 581 AppData[0] = 4;
mluis 0:45496a70a8a5 582 for( uint8_t i = 1; i < AppDataSize; i++ )
mluis 0:45496a70a8a5 583 {
mluis 3:3152aa75c58d 584 AppData[i] = mcpsIndication->Buffer[i] + 1;
mluis 0:45496a70a8a5 585 }
mluis 0:45496a70a8a5 586 break;
mluis 0:45496a70a8a5 587 case 5: // (viii)
mluis 0:45496a70a8a5 588 {
mluis 0:45496a70a8a5 589 MlmeReq_t mlmeReq;
mluis 0:45496a70a8a5 590 mlmeReq.Type = MLME_LINK_CHECK;
mluis 0:45496a70a8a5 591 LoRaMacMlmeRequest( &mlmeReq );
mluis 0:45496a70a8a5 592 }
mluis 0:45496a70a8a5 593 break;
mluis 5:62862ef9480b 594 case 6: // (ix)
mluis 5:62862ef9480b 595 {
mluis 5:62862ef9480b 596 MlmeReq_t mlmeReq;
mluis 5:62862ef9480b 597
mluis 7:5077515c163b 598 // Disable TestMode and revert back to normal operation
mluis 7:5077515c163b 599 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mluis 7:5077515c163b 600 AppPort = LORAWAN_APP_PORT;
mluis 7:5077515c163b 601 AppDataSize = LORAWAN_APP_DATA_SIZE;
mluis 7:5077515c163b 602 ComplianceTest.DownLinkCounter = 0;
mluis 7:5077515c163b 603 ComplianceTest.Running = false;
mluis 7:5077515c163b 604
mluis 7:5077515c163b 605 MibRequestConfirm_t mibReq;
mluis 7:5077515c163b 606 mibReq.Type = MIB_ADR;
mluis 7:5077515c163b 607 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
mluis 7:5077515c163b 608 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 7:5077515c163b 609 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mluis 5:62862ef9480b 610 mlmeReq.Type = MLME_JOIN;
mluis 5:62862ef9480b 611 mlmeReq.Req.Join.DevEui = DevEui;
mluis 5:62862ef9480b 612 mlmeReq.Req.Join.AppEui = AppEui;
mluis 5:62862ef9480b 613 mlmeReq.Req.Join.AppKey = AppKey;
mluis 7:5077515c163b 614 mlmeReq.Req.Join.NbTrials = 3;
mluis 5:62862ef9480b 615 LoRaMacMlmeRequest( &mlmeReq );
mluis 5:62862ef9480b 616 DeviceState = DEVICE_STATE_SLEEP;
mluis 5:62862ef9480b 617 }
mluis 5:62862ef9480b 618 break;
mluis 7:5077515c163b 619 case 7: // (x)
mluis 7:5077515c163b 620 {
mluis 7:5077515c163b 621 if( mcpsIndication->BufferSize == 3 )
mluis 7:5077515c163b 622 {
mluis 7:5077515c163b 623 MlmeReq_t mlmeReq;
mluis 7:5077515c163b 624 mlmeReq.Type = MLME_TXCW;
mluis 7:5077515c163b 625 mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
mluis 7:5077515c163b 626 LoRaMacMlmeRequest( &mlmeReq );
mluis 7:5077515c163b 627 }
mluis 7:5077515c163b 628 else if( mcpsIndication->BufferSize == 7 )
mluis 7:5077515c163b 629 {
mluis 7:5077515c163b 630 MlmeReq_t mlmeReq;
mluis 7:5077515c163b 631 mlmeReq.Type = MLME_TXCW_1;
mluis 7:5077515c163b 632 mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] );
mluis 7:5077515c163b 633 mlmeReq.Req.TxCw.Frequency = ( uint32_t )( ( mcpsIndication->Buffer[3] << 16 ) | ( mcpsIndication->Buffer[4] << 8 ) | mcpsIndication->Buffer[5] ) * 100;
mluis 7:5077515c163b 634 mlmeReq.Req.TxCw.Power = mcpsIndication->Buffer[6];
mluis 7:5077515c163b 635 LoRaMacMlmeRequest( &mlmeReq );
mluis 7:5077515c163b 636 }
mluis 7:5077515c163b 637 ComplianceTest.State = 1;
mluis 7:5077515c163b 638 }
mluis 7:5077515c163b 639 break;
mluis 0:45496a70a8a5 640 default:
mluis 0:45496a70a8a5 641 break;
mluis 0:45496a70a8a5 642 }
mluis 0:45496a70a8a5 643 }
mluis 0:45496a70a8a5 644 break;
mluis 0:45496a70a8a5 645 default:
mluis 0:45496a70a8a5 646 break;
mluis 0:45496a70a8a5 647 }
mluis 0:45496a70a8a5 648 }
mluis 0:45496a70a8a5 649
mluis 0:45496a70a8a5 650 // Switch LED 2 ON for each received downlink
MGstic 10:1a85ff06be1a 651 //Led2State = true;
MGstic 10:1a85ff06be1a 652 //Led2StateChanged = true;
MGstic 10:1a85ff06be1a 653 //TimerStart( &Led2Timer );
mluis 0:45496a70a8a5 654 DownlinkStatusUpdated = true;
mluis 0:45496a70a8a5 655 }
mluis 0:45496a70a8a5 656
mluis 0:45496a70a8a5 657 /*!
mluis 0:45496a70a8a5 658 * \brief MLME-Confirm event function
mluis 0:45496a70a8a5 659 *
mluis 3:3152aa75c58d 660 * \param [IN] mlmeConfirm - Pointer to the confirm structure,
mluis 0:45496a70a8a5 661 * containing confirm attributes.
mluis 0:45496a70a8a5 662 */
mluis 3:3152aa75c58d 663 static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm )
mluis 0:45496a70a8a5 664 {
mluis 7:5077515c163b 665 switch( mlmeConfirm->MlmeRequest )
mluis 0:45496a70a8a5 666 {
mluis 7:5077515c163b 667 case MLME_JOIN:
mluis 0:45496a70a8a5 668 {
mluis 7:5077515c163b 669 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 670 {
mluis 0:45496a70a8a5 671 // Status is OK, node has joined the network
mluis 0:45496a70a8a5 672 IsNetworkJoinedStatusUpdate = true;
mluis 5:62862ef9480b 673 DeviceState = DEVICE_STATE_SEND;
mluis 7:5077515c163b 674 }
mluis 7:5077515c163b 675 else
mluis 7:5077515c163b 676 {
mluis 7:5077515c163b 677 // Join was not successful. Try to join again
mluis 7:5077515c163b 678 DeviceState = DEVICE_STATE_JOIN;
mluis 0:45496a70a8a5 679 }
mluis 7:5077515c163b 680 break;
mluis 7:5077515c163b 681 }
mluis 7:5077515c163b 682 case MLME_LINK_CHECK:
mluis 7:5077515c163b 683 {
mluis 7:5077515c163b 684 if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK )
mluis 0:45496a70a8a5 685 {
mluis 0:45496a70a8a5 686 // Check DemodMargin
mluis 0:45496a70a8a5 687 // Check NbGateways
mluis 0:45496a70a8a5 688 if( ComplianceTest.Running == true )
mluis 0:45496a70a8a5 689 {
mluis 0:45496a70a8a5 690 ComplianceTest.LinkCheck = true;
mluis 3:3152aa75c58d 691 ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin;
mluis 3:3152aa75c58d 692 ComplianceTest.NbGateways = mlmeConfirm->NbGateways;
mluis 0:45496a70a8a5 693 }
mluis 0:45496a70a8a5 694 }
mluis 7:5077515c163b 695 break;
mluis 0:45496a70a8a5 696 }
mluis 7:5077515c163b 697 default:
mluis 7:5077515c163b 698 break;
mluis 0:45496a70a8a5 699 }
mluis 0:45496a70a8a5 700 NextTx = true;
mluis 0:45496a70a8a5 701 UplinkStatusUpdated = true;
mluis 0:45496a70a8a5 702 }
MGstic 11:8407ff54c40e 703 void fil (int tabd[6],time_t seconde)
MGstic 11:8407ff54c40e 704 {
MGstic 11:8407ff54c40e 705 char buffer[8];
MGstic 11:8407ff54c40e 706 strftime(buffer, 8, "%Y\n", localtime(&seconde));
MGstic 11:8407ff54c40e 707 tabd[5]=atoi(buffer);
MGstic 11:8407ff54c40e 708 strftime(buffer, 8, "%m\n", localtime(&seconde));
MGstic 11:8407ff54c40e 709 tabd[4]=atoi(buffer);
MGstic 11:8407ff54c40e 710 strftime(buffer, 8, "%d\n", localtime(&seconde));
MGstic 11:8407ff54c40e 711 tabd[3]=atoi(buffer);
MGstic 11:8407ff54c40e 712 strftime(buffer, 8, "%S\n", localtime(&seconde));
MGstic 11:8407ff54c40e 713 tabd[2]=atoi(buffer);
MGstic 11:8407ff54c40e 714 strftime(buffer, 8, "%M\n", localtime(&seconde));
MGstic 11:8407ff54c40e 715 tabd[1]=atoi(buffer);
MGstic 11:8407ff54c40e 716 strftime(buffer, 8, "%H\n", localtime(&seconde));
MGstic 11:8407ff54c40e 717 tabd[0]=atoi(buffer);
MGstic 11:8407ff54c40e 718
MGstic 11:8407ff54c40e 719 }
mluis 0:45496a70a8a5 720
MGstic 10:1a85ff06be1a 721 /*######################################################################
mluis 0:45496a70a8a5 722 * Main application entry point.
mluis 0:45496a70a8a5 723 */
MGstic 10:1a85ff06be1a 724
MGstic 10:1a85ff06be1a 725
mluis 0:45496a70a8a5 726 int main( void )
mluis 0:45496a70a8a5 727 {
MGstic 10:1a85ff06be1a 728 //####### INITIALISATION MODULE LORA ############
mluis 0:45496a70a8a5 729 LoRaMacPrimitives_t LoRaMacPrimitives;
mluis 0:45496a70a8a5 730 LoRaMacCallback_t LoRaMacCallbacks;
mluis 0:45496a70a8a5 731 MibRequestConfirm_t mibReq;
mluis 0:45496a70a8a5 732 BoardInit( );
mluis 0:45496a70a8a5 733 SerialDisplayInit( );
mluis 5:62862ef9480b 734 SerialDisplayUpdateEui( 5, DevEui );
mluis 5:62862ef9480b 735 SerialDisplayUpdateEui( 6, AppEui );
mluis 5:62862ef9480b 736 SerialDisplayUpdateKey( 7, AppKey );
mluis 5:62862ef9480b 737 SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID );
mluis 5:62862ef9480b 738 SerialDisplayUpdateDevAddr( DevAddr );
mluis 5:62862ef9480b 739 SerialDisplayUpdateKey( 12, NwkSKey );
mluis 5:62862ef9480b 740 SerialDisplayUpdateKey( 13, AppSKey );
MGstic 10:1a85ff06be1a 741 DeviceState = DEVICE_STATE_INIT;
MGstic 10:1a85ff06be1a 742
MGstic 10:1a85ff06be1a 743 //####### LIAISON SERIE ########
MGstic 10:1a85ff06be1a 744
MGstic 10:1a85ff06be1a 745 /*pc.baud(9600);// vitesse liaison série
MGstic 10:1a85ff06be1a 746 pc.format(8, SerialBase::None, 1); // format de la liaison*/
MGstic 10:1a85ff06be1a 747 bouton.rise(&interruption_extern_bouton); //initialisation de l'interruption extern sur le bouton user
MGstic 10:1a85ff06be1a 748
MGstic 10:1a85ff06be1a 749 //####### RTC INIT ########
MGstic 11:8407ff54c40e 750 //set_time(1514764800);
MGstic 10:1a85ff06be1a 751 int config_s=0;
mluis 5:62862ef9480b 752
MGstic 10:1a85ff06be1a 753 //####### VARIABLES ########
MGstic 10:1a85ff06be1a 754 uint8_t compt=0;
MGstic 11:8407ff54c40e 755
mluis 0:45496a70a8a5 756
mluis 0:45496a70a8a5 757 while( 1 )
mluis 0:45496a70a8a5 758 {
MGstic 10:1a85ff06be1a 759 if(1)
mluis 0:45496a70a8a5 760 {
MGstic 10:1a85ff06be1a 761 SerialRxProcess( );
MGstic 10:1a85ff06be1a 762 if( IsNetworkJoinedStatusUpdate == true )
MGstic 10:1a85ff06be1a 763 {
MGstic 10:1a85ff06be1a 764 IsNetworkJoinedStatusUpdate = false;
MGstic 10:1a85ff06be1a 765 mibReq.Type = MIB_NETWORK_JOINED;
MGstic 10:1a85ff06be1a 766 LoRaMacMibGetRequestConfirm( &mibReq );
MGstic 10:1a85ff06be1a 767 SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined );
MGstic 10:1a85ff06be1a 768 }
MGstic 10:1a85ff06be1a 769
MGstic 10:1a85ff06be1a 770 if( UplinkStatusUpdated == true )
MGstic 10:1a85ff06be1a 771 {
MGstic 10:1a85ff06be1a 772 UplinkStatusUpdated = false;
MGstic 10:1a85ff06be1a 773 SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize );
MGstic 10:1a85ff06be1a 774 }
MGstic 10:1a85ff06be1a 775 if( DownlinkStatusUpdated == true )
MGstic 10:1a85ff06be1a 776 {
MGstic 10:1a85ff06be1a 777 DownlinkStatusUpdated = false;
MGstic 10:1a85ff06be1a 778 //SerialDisplayUpdateLedState( 2, Led2State );
MGstic 10:1a85ff06be1a 779 SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize );
MGstic 10:1a85ff06be1a 780 }
MGstic 11:8407ff54c40e 781
MGstic 11:8407ff54c40e 782
MGstic 10:1a85ff06be1a 783
mluis 0:45496a70a8a5 784 }
MGstic 11:8407ff54c40e 785 data_packet[compt].masse[0]=poidstest;
MGstic 10:1a85ff06be1a 786 poidstest++;
MGstic 10:1a85ff06be1a 787 switch (etat)
MGstic 10:1a85ff06be1a 788 {
MGstic 10:1a85ff06be1a 789
MGstic 10:1a85ff06be1a 790 case 0: //bouton non enclenché
MGstic 11:8407ff54c40e 791 {
MGstic 11:8407ff54c40e 792 time_t seconds=time(NULL);
MGstic 11:8407ff54c40e 793 fil (tabd,seconds);
MGstic 11:8407ff54c40e 794 SerialDisplaytest( compt,0 );
MGstic 11:8407ff54c40e 795 SerialDisplaytest( c_send,1 );
MGstic 10:1a85ff06be1a 796 SerialDisplayRTCactive(etat);
MGstic 10:1a85ff06be1a 797 SerialDisplayTIME(tabd);
MGstic 11:8407ff54c40e 798 //time_t seconds = time(NULL)
MGstic 11:8407ff54c40e 799 if (compt<=(TAB_MAX-1)||(c_send==0))
MGstic 10:1a85ff06be1a 800 {
MGstic 11:8407ff54c40e 801 if( c_send==0)
MGstic 11:8407ff54c40e 802 {
MGstic 11:8407ff54c40e 803 compt=0;
MGstic 11:8407ff54c40e 804 c_send=TAB_MAX;
MGstic 11:8407ff54c40e 805 }
MGstic 11:8407ff54c40e 806 wait(3);
MGstic 11:8407ff54c40e 807 //time_t seconds=time(NULL);
MGstic 11:8407ff54c40e 808 copy_time(data_packet[compt].heure,seconds);
MGstic 11:8407ff54c40e 809 compt++;
MGstic 11:8407ff54c40e 810 }
mluis 0:45496a70a8a5 811
MGstic 11:8407ff54c40e 812 else
MGstic 11:8407ff54c40e 813 { //SerialDisplaytest( compt+1,8 );
MGstic 11:8407ff54c40e 814 switch( DeviceState )
MGstic 10:1a85ff06be1a 815 {
MGstic 11:8407ff54c40e 816 case DEVICE_STATE_INIT:
MGstic 11:8407ff54c40e 817 {
MGstic 11:8407ff54c40e 818 LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
MGstic 11:8407ff54c40e 819 LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
MGstic 11:8407ff54c40e 820 LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm;
MGstic 11:8407ff54c40e 821 LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
MGstic 11:8407ff54c40e 822 LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks );
MGstic 11:8407ff54c40e 823 TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent );
mluis 3:3152aa75c58d 824
MGstic 11:8407ff54c40e 825 mibReq.Type = MIB_ADR;
MGstic 11:8407ff54c40e 826 mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
MGstic 11:8407ff54c40e 827 LoRaMacMibSetRequestConfirm( &mibReq );
MGstic 11:8407ff54c40e 828 mibReq.Type = MIB_PUBLIC_NETWORK;
MGstic 11:8407ff54c40e 829 mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
MGstic 11:8407ff54c40e 830 LoRaMacMibSetRequestConfirm( &mibReq );
MGstic 11:8407ff54c40e 831 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
MGstic 11:8407ff54c40e 832 SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
MGstic 10:1a85ff06be1a 833
mluis 5:62862ef9480b 834
MGstic 11:8407ff54c40e 835 LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 );
MGstic 11:8407ff54c40e 836 LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 );
MGstic 11:8407ff54c40e 837 LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 );
MGstic 11:8407ff54c40e 838 LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 );
MGstic 11:8407ff54c40e 839 LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 );
MGstic 11:8407ff54c40e 840 LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 );
MGstic 11:8407ff54c40e 841 LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 );
mluis 7:5077515c163b 842
MGstic 11:8407ff54c40e 843 mibReq.Type = MIB_RX2_DEFAULT_CHANNEL;
MGstic 11:8407ff54c40e 844 mibReq.Param.Rx2DefaultChannel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
MGstic 11:8407ff54c40e 845 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 3:3152aa75c58d 846
MGstic 11:8407ff54c40e 847 mibReq.Type = MIB_RX2_CHANNEL;
MGstic 11:8407ff54c40e 848 mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
MGstic 11:8407ff54c40e 849 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 850
MGstic 11:8407ff54c40e 851 SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
MGstic 11:8407ff54c40e 852 SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
MGstic 11:8407ff54c40e 853 SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
MGstic 10:1a85ff06be1a 854
MGstic 11:8407ff54c40e 855 LoRaMacDownlinkStatus.DownlinkCounter = 0;
mluis 0:45496a70a8a5 856
MGstic 11:8407ff54c40e 857 DeviceState = DEVICE_STATE_JOIN;
MGstic 11:8407ff54c40e 858
MGstic 11:8407ff54c40e 859 break;
MGstic 11:8407ff54c40e 860 }
MGstic 11:8407ff54c40e 861 case DEVICE_STATE_JOIN:
MGstic 11:8407ff54c40e 862 {
MGstic 11:8407ff54c40e 863 mibReq.Type = MIB_NET_ID;
MGstic 11:8407ff54c40e 864 mibReq.Param.NetID = LORAWAN_NETWORK_ID;
MGstic 11:8407ff54c40e 865 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 866
MGstic 11:8407ff54c40e 867 mibReq.Type = MIB_DEV_ADDR;
MGstic 11:8407ff54c40e 868 mibReq.Param.DevAddr = DevAddr;
MGstic 11:8407ff54c40e 869 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 870
MGstic 11:8407ff54c40e 871 mibReq.Type = MIB_NWK_SKEY;
MGstic 11:8407ff54c40e 872 mibReq.Param.NwkSKey = NwkSKey;
MGstic 11:8407ff54c40e 873 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 874
MGstic 11:8407ff54c40e 875 mibReq.Type = MIB_APP_SKEY;
MGstic 11:8407ff54c40e 876 mibReq.Param.AppSKey = AppSKey;
MGstic 11:8407ff54c40e 877 LoRaMacMibSetRequestConfirm( &mibReq );
mluis 0:45496a70a8a5 878
MGstic 11:8407ff54c40e 879 mibReq.Type = MIB_NETWORK_JOINED;
MGstic 11:8407ff54c40e 880 mibReq.Param.IsNetworkJoined = true;
MGstic 11:8407ff54c40e 881 LoRaMacMibSetRequestConfirm( &mibReq );
MGstic 11:8407ff54c40e 882 //c_send--;
MGstic 11:8407ff54c40e 883 // SerialDisplaytest( c_send,3 );
MGstic 11:8407ff54c40e 884 DeviceState = DEVICE_STATE_SEND;
MGstic 11:8407ff54c40e 885 IsNetworkJoinedStatusUpdate = true;
MGstic 11:8407ff54c40e 886 break;
MGstic 11:8407ff54c40e 887 }
MGstic 11:8407ff54c40e 888 case DEVICE_STATE_SEND:
MGstic 11:8407ff54c40e 889 {
MGstic 11:8407ff54c40e 890 if(NextTx == true )
MGstic 11:8407ff54c40e 891 {
MGstic 11:8407ff54c40e 892 SerialDisplayUpdateUplinkAcked( false );
MGstic 11:8407ff54c40e 893 SerialDisplayUpdateDonwlinkRxData( false );
MGstic 11:8407ff54c40e 894
MGstic 11:8407ff54c40e 895 ///prepare tx
MGstic 11:8407ff54c40e 896 PrepareTxFrame( AppPort,data_packet,compt-c_send);
MGstic 11:8407ff54c40e 897 NextTx = SendFrame( );
MGstic 11:8407ff54c40e 898 SerialDisplaytest( compt-c_send,6 );
MGstic 11:8407ff54c40e 899
MGstic 11:8407ff54c40e 900
MGstic 11:8407ff54c40e 901
MGstic 11:8407ff54c40e 902 }
MGstic 11:8407ff54c40e 903
MGstic 11:8407ff54c40e 904 if( ComplianceTest.Running == true )
MGstic 11:8407ff54c40e 905 {
MGstic 11:8407ff54c40e 906 // Schedule next packet transmission
MGstic 11:8407ff54c40e 907 TxDutyCycleTime = 500; // 5000 ms
MGstic 11:8407ff54c40e 908
MGstic 11:8407ff54c40e 909
MGstic 11:8407ff54c40e 910
MGstic 11:8407ff54c40e 911 }
MGstic 11:8407ff54c40e 912 else
MGstic 11:8407ff54c40e 913 {
MGstic 11:8407ff54c40e 914
MGstic 11:8407ff54c40e 915 // Schedule next packet transmission
MGstic 11:8407ff54c40e 916 TxDutyCycleTime = 75;//APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
MGstic 11:8407ff54c40e 917 }
MGstic 11:8407ff54c40e 918
MGstic 11:8407ff54c40e 919 DeviceState = DEVICE_STATE_CYCLE;
MGstic 11:8407ff54c40e 920
MGstic 11:8407ff54c40e 921
MGstic 11:8407ff54c40e 922
MGstic 11:8407ff54c40e 923 break;
MGstic 11:8407ff54c40e 924 }
MGstic 11:8407ff54c40e 925 case DEVICE_STATE_CYCLE:
MGstic 10:1a85ff06be1a 926 {
MGstic 11:8407ff54c40e 927 //
MGstic 11:8407ff54c40e 928
MGstic 11:8407ff54c40e 929
MGstic 10:1a85ff06be1a 930 // Schedule next packet transmission
MGstic 11:8407ff54c40e 931 TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
MGstic 11:8407ff54c40e 932 TimerStart( &TxNextPacketTimer );
MGstic 11:8407ff54c40e 933 DeviceState = DEVICE_STATE_SLEEP;
MGstic 11:8407ff54c40e 934 break;
MGstic 10:1a85ff06be1a 935 }
MGstic 11:8407ff54c40e 936 case DEVICE_STATE_SLEEP:
MGstic 11:8407ff54c40e 937 {
MGstic 11:8407ff54c40e 938 // Wake up through events
MGstic 11:8407ff54c40e 939 break;
MGstic 11:8407ff54c40e 940 }
MGstic 11:8407ff54c40e 941 default:
MGstic 11:8407ff54c40e 942 {
MGstic 11:8407ff54c40e 943 DeviceState = DEVICE_STATE_INIT;
MGstic 11:8407ff54c40e 944 break;
MGstic 11:8407ff54c40e 945 }
MGstic 11:8407ff54c40e 946
MGstic 10:1a85ff06be1a 947 }
MGstic 11:8407ff54c40e 948
MGstic 10:1a85ff06be1a 949 }
MGstic 11:8407ff54c40e 950 break;
MGstic 10:1a85ff06be1a 951 }
MGstic 10:1a85ff06be1a 952 case 1 : //bouton enclenché
MGstic 10:1a85ff06be1a 953 {
MGstic 10:1a85ff06be1a 954 SerialDisplayRTCactive(etat);
MGstic 10:1a85ff06be1a 955 saisie_serie(tabd);
MGstic 10:1a85ff06be1a 956 SerialDisplayTIME(tabd);
MGstic 11:8407ff54c40e 957 //chk_and_set_time(tabd);
MGstic 10:1a85ff06be1a 958 config_s=unix_timestamp(tabd);
MGstic 10:1a85ff06be1a 959 set_time(config_s);
MGstic 10:1a85ff06be1a 960 //printf( "%d\r\n",etat);
MGstic 10:1a85ff06be1a 961 etat=0;
MGstic 10:1a85ff06be1a 962 break;
MGstic 10:1a85ff06be1a 963 }
mluis 0:45496a70a8a5 964 }
MGstic 10:1a85ff06be1a 965
MGstic 10:1a85ff06be1a 966
MGstic 10:1a85ff06be1a 967
mluis 0:45496a70a8a5 968 }
mluis 0:45496a70a8a5 969 }