The NSL01 library contains the software stack to control the NSL01 LoRaWAN shield from mCloud System GmbH. The NSL01 is a professional plug & play LoRaWAN shield for a wide range of STM32 Nucleo-64 boards with Arduino Uno Rev 3 connectivity. For more information about the NSL01 LoRaWAN shield have a look at: http://www.mcloud-systems.com/nsl01-lorawan-nucleo-arduino-shield

Fork of NSL01 by mCloud Systems

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SLIP.cpp Source File

SLIP.cpp

00001 /*******************************************************************************
00002  *  Functions for NSL01 class for SLIP encoder/decoder.
00003  *
00004  *  For more information about the NSL01 LoRaWAN shield:
00005  *      http://www.mcloud-systems.com/nsl01-lorawan-nucleo-arduino-shield
00006  *
00007  *  @note The SLIP files are included in the dependencies directory of the
00008  *        project and these files are necessary for the NSL01 class!
00009  *
00010  *  @author  -
00011  *  @version 1.0
00012  *  @date    20-June-2018
00013 *******************************************************************************/
00014 
00015 //------------------------------------------------------------------------------
00016 //
00017 //  Include Files
00018 //
00019 //------------------------------------------------------------------------------
00020 
00021 #include "./dependencies/SLIP.h"
00022 
00023 //------------------------------------------------------------------------------
00024 //
00025 //  Protocol definitions
00026 //
00027 //------------------------------------------------------------------------------
00028 
00029 //--SLIP receiver/decoder states
00030 #define SLIPDEC_IDLE_STATE          0
00031 #define SLIPDEC_START_STATE         1
00032 #define SLIPDEC_IN_FRAME_STATE      2
00033 #define SLIPDEC_ESC_STATE           3
00034 
00035 //------------------------------------------------------------------------------
00036 //
00037 // Declaration of SLIP variables
00038 //
00039 //------------------------------------------------------------------------------
00040 
00041 typedef struct
00042 {
00043     //--Decoder
00044     int                 RxState;
00045     int                 RxIndex;
00046     int                 RxBufferSize;
00047     UINT8*              RxBuffer;
00048     TSLIP_CbRxMessage   CbRxMessage;
00049 
00050     //--Encoder
00051     int                 TxIndex;
00052     int                 TxBufferSize;
00053     UINT8*              TxBuffer;
00054 }TSLIP;
00055 
00056 //------------------------------------------------------------------------------
00057 //
00058 // Section RAM
00059 //
00060 //------------------------------------------------------------------------------
00061 
00062 //--SLIP Instance
00063 static TSLIP   SLIP;
00064 
00065 //------------------------------------------------------------------------------
00066 //
00067 //  Function: SLIP_Init
00068 //
00069 //  @brief Function to init SLIP decoder.
00070 //
00071 //  @param cbRxMessage : Message receiver callback
00072 //
00073 //------------------------------------------------------------------------------
00074 
00075 void
00076 SLIP_Init(TSLIP_CbRxMessage cbRxMessage)
00077 {
00078     //--Init decoder to idle state, no Rx-buffer avaliable
00079     SLIP.RxState         =   SLIPDEC_IDLE_STATE;
00080     SLIP.RxIndex         =   0;
00081     SLIP.RxBuffer        =   0;
00082     SLIP.RxBufferSize    =   0;
00083 
00084     //--Save message receiver callback
00085     SLIP.CbRxMessage     =   cbRxMessage;
00086 
00087     //--Init encoder
00088     SLIP.TxIndex         =   0;
00089     SLIP.TxBuffer        =   0;
00090     SLIP.TxBufferSize    =   0;
00091 }
00092 
00093 //------------------------------------------------------------------------------
00094 //
00095 //  Function: SLIP_StoreTxByte
00096 //
00097 //  @brief Internal function to store a byte into TxBuffer.
00098 //
00099 //  @param txByte : Tx Byte
00100 //
00101 //------------------------------------------------------------------------------
00102 
00103 static void
00104 SLIP_StoreTxByte(UINT8 txByte)
00105 {
00106     if (SLIP.TxIndex < SLIP.TxBufferSize)
00107         SLIP.TxBuffer[SLIP.TxIndex++] = txByte;
00108 }
00109 
00110 //------------------------------------------------------------------------------
00111 //
00112 //  Function: SLIP_EncodeData
00113 //
00114 //  @brief Function to encode outgoing data.
00115 //
00116 //  @param dstBuffer    : Pointer to destination buffer
00117 //  @param dstBufferSize: Tx message buffer size
00118 //  @param srcData      : Pointer to source data
00119 //  @param srcLength    : Length of source data
00120 //
00121 //  @returns >=0 on success, -1 on error
00122 //
00123 //------------------------------------------------------------------------------
00124 
00125 int
00126 SLIP_EncodeData(UINT8* dstBuffer, int dstBufferSize, UINT8* srcData, int srcLength)
00127 {
00128     //--Init TxBuffer
00129     SLIP.TxBuffer = dstBuffer;
00130 
00131     //--Init TxIndex
00132     SLIP.TxIndex  = 0;
00133 
00134     //--Init size
00135     SLIP.TxBufferSize = dstBufferSize;
00136 
00137     //--Send start of SLIP message
00138     SLIP_StoreTxByte(SLIP_END);
00139 
00140     //--Iterate over all message bytes
00141     while(srcLength--)
00142     {
00143         switch (*srcData)
00144         {
00145                 case SLIP_END:
00146                     SLIP_StoreTxByte(SLIP_ESC);
00147                     SLIP_StoreTxByte(SLIP_ESC_END);
00148                     break;
00149 
00150                 case SLIP_ESC:
00151                     SLIP_StoreTxByte(SLIP_ESC);
00152                     SLIP_StoreTxByte(SLIP_ESC_ESC);
00153                     break;
00154 
00155                 default:
00156                     SLIP_StoreTxByte(*srcData);
00157                     break;
00158         }
00159         
00160         //--Update pointer: next byte
00161         srcData++;
00162     }
00163 
00164     //--Send end of SLIP message
00165     SLIP_StoreTxByte(SLIP_END);
00166 
00167     //--Check if length is okay
00168     if (SLIP.TxIndex <= SLIP.TxBufferSize)
00169         return SLIP.TxIndex;
00170 
00171     //--Return Tx length error
00172     return -1;
00173 }
00174 
00175 //------------------------------------------------------------------------------
00176 //
00177 //  Function: SLIP_SetRxBuffer
00178 //
00179 //  @brief Function to init Rx buffer and enable receiver/decoder.
00180 //
00181 //  @param rxBuffer     : Pointer to Rx message buffer
00182 //  @param rxBufferSize : Rx message buffer size
00183 //
00184 //  @returns true on success, false on error
00185 //
00186 //------------------------------------------------------------------------------
00187 
00188 bool
00189 SLIP_SetRxBuffer(UINT8* rxBuffer, int rxBufferSize)
00190 {
00191     //--Receiver in IDLE state ==> Check if client already registered
00192     if ((SLIP.RxState == SLIPDEC_IDLE_STATE) && SLIP.CbRxMessage)
00193     {
00194         //--Same buffer parameters
00195         SLIP.RxBuffer        = rxBuffer;
00196         SLIP.RxBufferSize    = rxBufferSize;
00197 
00198         //--Enable decoder
00199         SLIP.RxState = SLIPDEC_START_STATE;
00200 
00201         return true;
00202     }
00203 
00204     return false;
00205 }
00206 
00207 //------------------------------------------------------------------------------
00208 //
00209 //  Function: SLIP_StoreRxByte
00210 //
00211 //  @brief Internal function to store SLIP decoded Rx byte.
00212 //
00213 //  @param rxBuffer     : Rx Byte
00214 //
00215 //------------------------------------------------------------------------------
00216 
00217 static void
00218 SLIP_StoreRxByte(UINT8 rxByte)
00219 {
00220     if (SLIP.RxIndex < SLIP.RxBufferSize)
00221         SLIP.RxBuffer[SLIP.RxIndex++] = rxByte;
00222 }
00223 
00224 //------------------------------------------------------------------------------
00225 //
00226 //  Function: SLIP_DecodeData
00227 //
00228 //  @brief Function to process/decode received byte stream.
00229 //
00230 //  @param srcData      : Pointer to source data
00231 //  @param srcLength    : Length of source data
00232 //
00233 //------------------------------------------------------------------------------
00234 
00235 void
00236 SLIP_DecodeData(UINT8* srcData, int srcLength)
00237 {
00238     //--Iterate over all received bytes
00239     while(srcLength--)
00240     {
00241         //--Get Rx byte
00242         UINT8 rxByte = *srcData++;
00243 
00244         //--Decode according to current state
00245         switch(SLIP.RxState)
00246         {
00247             case    SLIPDEC_START_STATE:                                       
00248                     //--Start of SLIP frame?
00249                     if(rxByte == SLIP_END)
00250                     {                        
00251                         //--Init read index
00252                         SLIP.RxIndex = 0;
00253 
00254                         //--Next state
00255                         SLIP.RxState = SLIPDEC_IN_FRAME_STATE;
00256                     }
00257                     break;
00258 
00259             case    SLIPDEC_IN_FRAME_STATE:
00260                     switch(rxByte)
00261                     {
00262                         case    SLIP_END:
00263                                 //--Data received?
00264                                 if(SLIP.RxIndex > 0)
00265                                 {
00266                                     //--Yes, receiver registered?
00267                                     if (SLIP.CbRxMessage)
00268                                     {
00269                                         //--Yes, call message receive
00270                                         SLIP.RxBuffer = (*SLIP.CbRxMessage)(SLIP.RxBuffer, SLIP.RxIndex);
00271 
00272                                         //--New buffer available?
00273                                         if (!SLIP.RxBuffer)
00274                                         {
00275                                             SLIP.RxState = SLIPDEC_IDLE_STATE;
00276                                         }
00277                                         else
00278                                         {
00279                                             SLIP.RxState = SLIPDEC_START_STATE;
00280                                         }
00281                                     }
00282                                     else
00283                                     {
00284                                         //--Disable decoder, temp. no buffer avaliable
00285                                         SLIP.RxState = SLIPDEC_IDLE_STATE;
00286                                     }
00287                                 }
00288                                 //--Init read index
00289                                 SLIP.RxIndex = 0;
00290                                 break;
00291 
00292                         case  SLIP_ESC:
00293                                 //--Enter escape sequence state
00294                                 SLIP.RxState = SLIPDEC_ESC_STATE;
00295                                 break;
00296 
00297                         default:
00298                                 //--Store byte
00299                                 SLIP_StoreRxByte(rxByte);
00300                                 break;
00301                     }
00302                     break;
00303 
00304             case    SLIPDEC_ESC_STATE:
00305                     switch(rxByte)
00306                     {
00307                         case    SLIP_ESC_END:
00308                                 SLIP_StoreRxByte(SLIP_END);
00309                                 //--Quit escape sequence state
00310                                 SLIP.RxState = SLIPDEC_IN_FRAME_STATE;
00311                                 break;
00312 
00313                         case    SLIP_ESC_ESC:
00314                                 SLIP_StoreRxByte(SLIP_ESC);
00315                                 //--Quit escape sequence state
00316                                 SLIP.RxState = SLIPDEC_IN_FRAME_STATE;
00317                                 break;
00318 
00319                         default:
00320                                 //--Abort frame receiption
00321                                 SLIP.RxState = SLIPDEC_START_STATE;
00322                                 break;
00323                     }
00324                     break;
00325 
00326             default:
00327                     break;
00328         }
00329     }
00330 }
00331 
00332 //------------------------------------------------------------------------------
00333 // end of file
00334 //------------------------------------------------------------------------------