/*! * \file LoRaMac.h * * \brief LoRa MAC layer implementation * * \copyright Revised BSD License, see section \ref LICENSE. * * \code * ______ _ * / _____) _ | | * ( (____ _____ ____ _| |_ _____ ____| |__ * \____ \| ___ | (_ _) ___ |/ ___) _ \ * _____) ) ____| | | || |_| ____( (___| | | | * (______/|_____)_|_|_| \__)_____)\____)_| |_| * (C)2013 Semtech * * ___ _____ _ ___ _ _____ ___ ___ ___ ___ * / __|_ _/_\ / __| |/ / __/ _ \| _ \/ __| __| * \__ \ | |/ _ \ (__| ' <| _| (_) | / (__| _| * |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___| * embedded.connectivity.solutions=============== * * \endcode * * \author Miguel Luis ( Semtech ) * * \author Gregory Cristian ( Semtech ) * * \author Daniel Jäckle ( STACKFORCE ) * * \defgroup LORAMAC LoRa MAC layer implementation * This module specifies the API implementation of the LoRaMAC layer. * This is a placeholder for a detailed description of the LoRaMac * layer and the supported features. * \{ * * \example classA/LoRaMote/main.c * LoRaWAN class A application example for the LoRaMote. * * \example classB/LoRaMote/main.c * LoRaWAN class B application example for the LoRaMote. * * \example classC/LoRaMote/main.c * LoRaWAN class C application example for the LoRaMote. */ #ifndef __LORAMAC_H__ #define __LORAMAC_H__ // Includes board dependent definitions such as channels frequencies //#include "LoRaMac-board.h" /******************************** Lora物理层帧结构定义 ********************************/ #include #include //#include #include #include /*! * LoRaMAC Status */ typedef enum eLoRaMacStatus { /*! * Service started successfully */ LORAMAC_STATUS_OK, /*! * Service not started - LoRaMAC is busy */ LORAMAC_STATUS_BUSY, /*! * Service unknown */ LORAMAC_STATUS_SERVICE_UNKNOWN, /*! * Service not started - invalid parameter */ LORAMAC_STATUS_PARAMETER_INVALID, /*! * Service not started - invalid frequency */ LORAMAC_STATUS_FREQUENCY_INVALID, /*! * Service not started - invalid datarate */ LORAMAC_STATUS_DATARATE_INVALID, /*! * Service not started - invalid frequency and datarate */ LORAMAC_STATUS_FREQ_AND_DR_INVALID, /*! * Service not started - the device is not in a LoRaWAN */ LORAMAC_STATUS_NO_NETWORK_JOINED, /*! * Service not started - playload lenght error */ LORAMAC_STATUS_LENGTH_ERROR, /*! * Service not started - playload lenght error */ LORAMAC_STATUS_MAC_CMD_LENGTH_ERROR, /*! * Service not started - the device is switched off */ LORAMAC_STATUS_DEVICE_OFF, }LoRaMacStatus_t; /*! * LoRaMAC frame types * * LoRaWAN Specification V1.0, chapter 4.2.1, table 1 */ typedef enum eLoRaMacFrameType { /*! * LoRaMAC join request frame */ FRAME_TYPE_JOIN_REQ = 0x00, /*! * LoRaMAC join accept frame */ FRAME_TYPE_JOIN_ACCEPT = 0x01, /*! * LoRaMAC unconfirmed up-link frame */ FRAME_TYPE_DATA_UNCONFIRMED_UP = 0x02, /*! * LoRaMAC unconfirmed down-link frame */ FRAME_TYPE_DATA_UNCONFIRMED_DOWN = 0x03, /*! * LoRaMAC confirmed up-link frame */ FRAME_TYPE_DATA_CONFIRMED_UP = 0x04, /*! * LoRaMAC confirmed down-link frame */ FRAME_TYPE_DATA_CONFIRMED_DOWN = 0x05, /*! * LoRaMAC RFU frame */ FRAME_TYPE_RFU = 0x06, /*! * LoRaMAC proprietary frame */ FRAME_TYPE_PROPRIETARY = 0x07, }LoRaMacFrameType_t; #define UP_LINK 0 /*! * Frame direction definition for down-link communications */ #define DOWN_LINK 1 /*! * Sets the length of the LoRaMAC footer field. * Mainly indicates the MIC field length */ #define LORAMAC_MFR_LEN 4 /*! * LoRaMAC header field definition (MHDR field) * * LoRaWAN Specification V1.0, chapter 4.2 */ typedef union uLoRaMacHeader { /*! * Byte-access to the bits */ uint8_t Value; /*! * Structure containing single access to header bits */ struct sHdrBits { /*! * Major version */ uint8_t Major : 2; /*! * RFU */ uint8_t RFU : 3; /*! * Message type */ uint8_t MType : 3; }Bits; }LoRaMacHeader_t; /*! * LoRaMAC frame control field definition (FCtrl) * * LoRaWAN Specification V1.0, chapter 4.3.1 */ typedef union uLoRaMacFrameCtrl { /*! * Byte-access to the bits */ uint8_t Value; struct sCtrlBits { /*! * Frame options length */ uint8_t FOptsLen : 4; /*! * Frame pending bit */ uint8_t FPending : 1; /*! * Message acknowledge bit */ uint8_t Ack : 1; /*! * ADR acknowledgment request bit */ uint8_t AdrAckReq : 1; /*! * ADR control in frame header */ uint8_t Adr : 1; }Bits; }LoRaMacFrameCtrl_t; /*! * Device IEEE EUI */ //static uint8_t *LoRaMacDevEui; typedef struct { uint8_t ADR; //实际只用1bit uint8_t ADRACKReq; //实际只用1bit uint8_t ACK; //实际只用1bit uint8_t FOptsLen; //实际只用4bit }FCtrl_t; typedef struct { uint32_t DevAddr; LoRaMacFrameCtrl_t FCtrl; uint16_t FCnt; uint8_t Fopts; //变长,约定1字节 }FHDR_t; typedef struct { uint8_t AppEUI[8]; //uint64_t AppEUI; uint8_t DevEUI[8]; //uint64_t DevEUI; uint16_t DevNonce; uint32_t AppNonce; //实际只用3字节 uint32_t NetID; //实际只用3字节 uint32_t DevAddr; uint8_t DLSettings; uint8_t RxDelay; uint8_t CFList[16]; FHDR_t FHDR; uint8_t Fport; //变长,约定1字节 uint8_t FRMPayload[128]; }MACPayload_t; typedef struct { LoRaMacHeader_t MHDR; MACPayload_t MACPayload; uint32_t MIC; }PHYPayload_t; typedef struct sPHYPayload { PHYPayload_t *PHYPayload; struct sPHYPayload *Next; }sPHYPayload_t; void sendJoinRequest(void); void sendUnconfirmUp(void); #endif // __LORAMAC_H__