Aloha implementation of LoRa technology

Dependencies:   SX1276Lib mbed

Fork of SX1276PingPong by Semtech

AlohaPacket.h

Committer:
rba90
Date:
2016-05-31
Revision:
15:f790f35839db
Child:
16:c3c6b13c3c42

File content as of revision 15:f790f35839db:

#ifndef ALOHAPACKET_H_
#define ALOHAPACKET_H_

#include "stdint.h"
#include "crc.h"

typedef struct 
{
    uint8_t fid;
    uint8_t no;
} HeaderStruct;

typedef struct
{
    uint8_t pd0;
    uint8_t pd1;
} DataStruct;

inline void createAlohaPacket(uint8_t *output, HeaderStruct *header, DataStruct *data)
{
    // assume user has already allocated memory for output
    
    // create header
    if (header)
    {
        output[0] = header->no;
        output[0] |= header->fid << 4;
    }
    
    // fit data
    if (data)
    {
        output[1] = data->pd0;
        output[2] = data->pd1;
    }
    
    // calculate CRC
    if (header && data)
    {
        output[3] = crc8(output, 3);
    }
}

inline bool dissectAlohaPacket(uint8_t *input, HeaderStruct *header, DataStruct *data)
{
    // assume user has already prepare memory in *in
    
    // get header
    if (header)
    {
        header->fid = input[0] >> 4; // higher four bits
        header->no = input[0] & 0x0f; // lower four bits
    }
    
    // get data
    if (data)
    {
        data->pd0 = input[1];
        data->pd1 = input[2];
    }
    
    // check crc
    if (header && data)
    {
        return input[3] == crc8(input, 3);
    }
    else
    {
        return false;
    }
}

#endif