MicroModemGP/protocol/LLP.h

72 lines
2 KiB
C
Raw Permalink Normal View History

2015-04-26 22:06:55 +02:00
#ifndef PROTOCOL_LLP_H
#define PROTOCOL_LLP_H
#include <stdio.h>
#include <stdbool.h>
#include "device.h"
2015-05-21 00:00:15 +02:00
#define LLP_ADDR_BROADCAST 0xFFFF
2015-04-26 22:06:55 +02:00
2015-05-21 00:00:15 +02:00
#define LLP_INTERLEAVE_SIZE 12
#define LLP_MIN_FRAME_LENGTH LLP_INTERLEAVE_SIZE
#define LLP_MAX_FRAME_LENGTH 48 * LLP_INTERLEAVE_SIZE
2015-05-21 00:00:15 +02:00
#define LLP_HEADER_SIZE 10
#define LLP_CHECKSUM_SIZE 2
2015-05-21 10:42:03 +02:00
#define LLP_MAX_DATA_SIZE LLP_MAX_FRAME_LENGTH - LLP_HEADER_SIZE - LLP_CHECKSUM_SIZE
2015-05-21 00:00:15 +02:00
#define LLP_DATA_BLOCK_SIZE ((LLP_INTERLEAVE_SIZE/3)*2)
#define LLP_CRC_SIZE 2
2015-04-26 22:06:55 +02:00
#define LLP_CRC_CORRECT 0xF0B8
struct LLPCtx; // Forward declarations
typedef void (*llp_callback_t)(struct LLPCtx *ctx);
typedef struct LLPAddress {
uint16_t network;
uint16_t host;
} LLPAddress;
2015-05-21 00:00:15 +02:00
typedef struct LLPHeader {
2015-04-26 22:06:55 +02:00
LLPAddress src;
LLPAddress dst;
2015-05-21 00:00:15 +02:00
uint8_t flags;
uint8_t padding;
} LLPHeader;
typedef struct LLPMsg {
LLPHeader header;
2015-04-26 22:06:55 +02:00
const uint8_t *data;
size_t len;
} LLPMsg;
typedef struct LLPCtx {
2015-05-21 00:00:15 +02:00
uint8_t buf[LLP_MAX_FRAME_LENGTH];
2015-04-26 22:06:55 +02:00
FILE *ch;
LLPAddress *address;
size_t frame_len;
2015-05-21 00:00:15 +02:00
size_t readLength;
2015-04-26 22:06:55 +02:00
uint16_t crc_in;
uint16_t crc_out;
2015-05-21 00:00:15 +02:00
uint8_t calculatedParity;
long correctionsMade;
2015-04-26 22:06:55 +02:00
llp_callback_t hook;
bool sync;
bool escape;
2018-11-28 23:56:48 +01:00
bool ready_for_data;
2015-05-21 00:00:15 +02:00
uint8_t interleaveCounter; // Keeps track of when we have received an entire interleaved block
uint8_t interleaveOut[LLP_INTERLEAVE_SIZE]; // A buffer for interleaving bytes before they are sent
uint8_t interleaveIn[LLP_INTERLEAVE_SIZE]; // A buffer for storing interleaved bytes before they are deinterleaved
2015-04-26 22:06:55 +02:00
} LLPCtx;
2015-05-21 00:00:15 +02:00
void llp_broadcast(LLPCtx *ctx, const void *_buf, size_t len);
2015-04-26 22:06:55 +02:00
void llp_send(LLPCtx *ctx, LLPAddress *dst, const void *_buf, size_t len);
2015-04-26 22:51:15 +02:00
void llp_sendRaw(LLPCtx *ctx, const void *_buf, size_t len);
2015-04-26 22:06:55 +02:00
void llp_poll(LLPCtx *ctx);
void llp_init(LLPCtx *ctx, LLPAddress *address, FILE *channel, llp_callback_t hook);
2015-05-21 00:00:15 +02:00
void llpInterleave(LLPCtx *ctx, uint8_t byte);
void llpDeinterleave(LLPCtx *ctx);
uint8_t llpParityBlock(uint8_t first, uint8_t other);
2015-04-26 22:06:55 +02:00
#endif