sbmp cleaning, sbmp now works ok

This commit is contained in:
2016-03-17 12:01:46 +01:00
parent 5f14fdf1e9
commit e3b96fd2f9
19 changed files with 132 additions and 106 deletions
+4 -4
View File
@@ -115,22 +115,22 @@ static uint32_t ICACHE_RODATA_ATTR STORE_ATTR crc_32_tab[] = { /* CRC polynomial
#define UPDC32(octet, crc) (crc_32_tab[((crc) ^ ((uint8_t)octet)) & 0xff] ^ ((crc) >> 8))
uint32_t crc32_begin(void)
uint32_t FLASH_FN crc32_begin(void)
{
return 0xFFFFFFFF;
}
uint32_t crc32_update(uint32_t crc_scratch, uint8_t ch)
uint32_t FLASH_FN crc32_update(uint32_t crc_scratch, uint8_t ch)
{
return UPDC32(ch, crc_scratch);
}
uint32_t crc32_end(uint32_t crc_scratch)
uint32_t FLASH_FN crc32_end(uint32_t crc_scratch)
{
return ~crc_scratch;
}
uint32_t crc32buf(uint8_t *buf, size_t len)
uint32_t FLASH_FN crc32buf(uint8_t *buf, size_t len)
{
uint32_t scratch = crc32_begin();
+2
View File
@@ -8,6 +8,8 @@
* #include in your application code.
*/
#define SBMP_VER "1.3"
#include "sbmp_config.h"
// Common utils & the frame parser
+5 -5
View File
@@ -11,7 +11,7 @@
/** Get nr of bytes in a checksum */
uint8_t chksum_length(SBMP_CksumType cksum_type)
uint8_t FLASH_FN chksum_length(SBMP_CksumType cksum_type)
{
switch (cksum_type) {
case SBMP_CKSUM_CRC32: return 4;
@@ -23,7 +23,7 @@ uint8_t chksum_length(SBMP_CksumType cksum_type)
}
/** Start calculating a checksum */
void cksum_begin(SBMP_CksumType type, uint32_t *scratch)
void FLASH_FN cksum_begin(SBMP_CksumType type, uint32_t *scratch)
{
switch (type) {
@@ -43,7 +43,7 @@ void cksum_begin(SBMP_CksumType type, uint32_t *scratch)
}
/** Update the checksum calculation with an incoming byte */
void cksum_update(SBMP_CksumType type, uint32_t *scratch, uint8_t byte)
void FLASH_FN cksum_update(SBMP_CksumType type, uint32_t *scratch, uint8_t byte)
{
switch (type) {
@@ -63,7 +63,7 @@ void cksum_update(SBMP_CksumType type, uint32_t *scratch, uint8_t byte)
}
/** Stop the checksum calculation, get the result */
void cksum_end(SBMP_CksumType type, uint32_t *scratch)
void FLASH_FN cksum_end(SBMP_CksumType type, uint32_t *scratch)
{
switch (type) {
@@ -83,7 +83,7 @@ void cksum_end(SBMP_CksumType type, uint32_t *scratch)
}
/** Check if the calculated checksum matches the received one */
bool cksum_verify(SBMP_CksumType type, uint32_t *scratch, uint32_t received_cksum)
bool FLASH_FN cksum_verify(SBMP_CksumType type, uint32_t *scratch, uint32_t received_cksum)
{
cksum_end(type, scratch);
+1 -1
View File
@@ -44,7 +44,7 @@
* If handshake is used, the peer will detect that CRC32 is not
* supported here, and should start using XOR.
*/
#define SBMP_HAS_CRC32 0
#define SBMP_HAS_CRC32 1
#endif
/* ------------------------------------- */
+3 -3
View File
@@ -4,7 +4,7 @@
#include "sbmp_logging.h"
#include "sbmp_datagram.h"
SBMP_Datagram *sbmp_dg_parse(SBMP_Datagram *dg, const uint8_t *payload, uint16_t length)
SBMP_Datagram FLASH_FN *sbmp_dg_parse(SBMP_Datagram *dg, const uint8_t *payload, uint16_t length)
{
if (length < 3) {
sbmp_error("Can't parse datagram, payload too short.");
@@ -34,7 +34,7 @@ SBMP_Datagram *sbmp_dg_parse(SBMP_Datagram *dg, const uint8_t *payload, uint16_t
/** Start a datagram transmission */
bool sbmp_dg_start(SBMP_FrmInst *frm, SBMP_CksumType cksum_type, uint16_t session, SBMP_DgType type, uint16_t length)
bool FLASH_FN sbmp_dg_start(SBMP_FrmInst *frm, SBMP_CksumType cksum_type, uint16_t session, SBMP_DgType type, uint16_t length)
{
if (length > (0xFFFF - 3)) {
sbmp_error("Can't send a datagram, payload too long.");
@@ -57,7 +57,7 @@ bool sbmp_dg_start(SBMP_FrmInst *frm, SBMP_CksumType cksum_type, uint16_t sessio
/** Send a whole datagram in one go */
bool sbmp_dg_send(SBMP_FrmInst *frm, SBMP_CksumType cksum_type, SBMP_Datagram *dg)
bool FLASH_FN sbmp_dg_send(SBMP_FrmInst *frm, SBMP_CksumType cksum_type, SBMP_Datagram *dg)
{
if (! sbmp_dg_start(frm, cksum_type, dg->session, dg->type, dg->length)) {
sbmp_error("Failed to start datagram.");
+18 -18
View File
@@ -12,7 +12,7 @@ static void call_frame_rx_callback(SBMP_FrmInst *frm);
/** Allocate the state struct & init all fields */
SBMP_FrmInst *sbmp_frm_init(
SBMP_FrmInst FLASH_FN *sbmp_frm_init(
SBMP_FrmInst *frm,
uint8_t *buffer,
uint16_t buffer_size,
@@ -58,39 +58,39 @@ SBMP_FrmInst *sbmp_frm_init(
}
/** Reset the internal state */
void sbmp_frm_reset(SBMP_FrmInst *frm)
void FLASH_FN sbmp_frm_reset(SBMP_FrmInst *frm)
{
sbmp_frm_reset_rx(frm);
sbmp_frm_reset_tx(frm);
}
/** Enable or disable Rx */
void sbmp_frm_enable_rx(SBMP_FrmInst *frm, bool enable)
void FLASH_FN sbmp_frm_enable_rx(SBMP_FrmInst *frm, bool enable)
{
frm->rx_enabled = enable;
}
/** Enable or disable Tx */
void sbmp_frm_enable_tx(SBMP_FrmInst *frm, bool enable)
void FLASH_FN sbmp_frm_enable_tx(SBMP_FrmInst *frm, bool enable)
{
frm->tx_enabled = enable;
}
/** Enable or disable both Rx and Tx */
void sbmp_frm_enable(SBMP_FrmInst *frm, bool enable)
void FLASH_FN sbmp_frm_enable(SBMP_FrmInst *frm, bool enable)
{
sbmp_frm_enable_rx(frm, enable);
sbmp_frm_enable_tx(frm, enable);
}
/** Set user token */
void sbmp_frm_set_user_token(SBMP_FrmInst *frm, void *token)
void FLASH_FN sbmp_frm_set_user_token(SBMP_FrmInst *frm, void *token)
{
frm->user_token = token;
}
/** Reset the receiver state */
void sbmp_frm_reset_rx(SBMP_FrmInst *frm)
void FLASH_FN sbmp_frm_reset_rx(SBMP_FrmInst *frm)
{
frm->rx_buffer_i = 0;
frm->rx_length = 0;
@@ -104,7 +104,7 @@ void sbmp_frm_reset_rx(SBMP_FrmInst *frm)
}
/** Reset the transmitter state */
void sbmp_frm_reset_tx(SBMP_FrmInst *frm)
void FLASH_FN sbmp_frm_reset_tx(SBMP_FrmInst *frm)
{
frm->tx_status = FRM_STATE_IDLE;
frm->tx_remain = 0;
@@ -114,21 +114,21 @@ void sbmp_frm_reset_tx(SBMP_FrmInst *frm)
}
/** Update a header XOR */
static inline
static inline FLASH_FN
void hdrxor_update(SBMP_FrmInst *frm, uint8_t rxbyte)
{
frm->rx_hdr_xor ^= rxbyte;
}
/** Check header xor against received value */
static inline
static inline FLASH_FN
bool hdrxor_verify(SBMP_FrmInst *frm, uint8_t rx_xor)
{
return frm->rx_hdr_xor == rx_xor;
}
/** Append a byte to the rx buffer */
static inline
static inline FLASH_FN
void append_rx_byte(SBMP_FrmInst *frm, uint8_t b)
{
frm->rx_buffer[frm->rx_buffer_i++] = b;
@@ -136,7 +136,7 @@ void append_rx_byte(SBMP_FrmInst *frm, uint8_t b)
/** Set n-th byte (0 = LSM) to a value */
static inline
void set_byte(uint32_t *acc, uint8_t pos, uint8_t byte)
void FLASH_FN set_byte(uint32_t *acc, uint8_t pos, uint8_t byte)
{
*acc |= (uint32_t)(byte << (pos * 8));
}
@@ -145,7 +145,7 @@ void set_byte(uint32_t *acc, uint8_t pos, uint8_t byte)
* Call the message handler with the payload.
*
*/
static void call_frame_rx_callback(SBMP_FrmInst *frm)
static void FLASH_FN call_frame_rx_callback(SBMP_FrmInst *frm)
{
if (frm->rx_handler == NULL) {
sbmp_error("frame_handler is null!");
@@ -165,7 +165,7 @@ static void call_frame_rx_callback(SBMP_FrmInst *frm)
* @param rxbyte
* @return status
*/
SBMP_RxStatus sbmp_frm_receive(SBMP_FrmInst *frm, uint8_t rxbyte)
SBMP_RxStatus FLASH_FN sbmp_frm_receive(SBMP_FrmInst *frm, uint8_t rxbyte)
{
if (! frm->rx_enabled) {
return SBMP_RX_DISABLED;
@@ -303,7 +303,7 @@ SBMP_RxStatus sbmp_frm_receive(SBMP_FrmInst *frm, uint8_t rxbyte)
}
/** Send a frame header */
bool sbmp_frm_start(SBMP_FrmInst *frm, SBMP_CksumType cksum_type, uint16_t length)
bool FLASH_FN sbmp_frm_start(SBMP_FrmInst *frm, SBMP_CksumType cksum_type, uint16_t length)
{
if (! frm->tx_enabled) {
sbmp_error("Can't tx, not enabled.");
@@ -356,7 +356,7 @@ bool sbmp_frm_start(SBMP_FrmInst *frm, SBMP_CksumType cksum_type, uint16_t lengt
}
/** End frame and enter idle mode */
static void end_frame(SBMP_FrmInst *frm)
static void FLASH_FN end_frame(SBMP_FrmInst *frm)
{
if (!frm->tx_enabled) {
sbmp_error("Can't tx, not enabled.");
@@ -387,7 +387,7 @@ static void end_frame(SBMP_FrmInst *frm)
}
/** Send a byte in the currently open frame */
bool sbmp_frm_send_byte(SBMP_FrmInst *frm, uint8_t byte)
bool FLASH_FN sbmp_frm_send_byte(SBMP_FrmInst *frm, uint8_t byte)
{
if (!frm->tx_enabled) {
sbmp_error("Can't tx, not enabled.");
@@ -410,7 +410,7 @@ bool sbmp_frm_send_byte(SBMP_FrmInst *frm, uint8_t byte)
}
/** Send a buffer in the currently open frame */
uint16_t sbmp_frm_send_buffer(SBMP_FrmInst *frm, const uint8_t *buffer, uint16_t length)
uint16_t FLASH_FN sbmp_frm_send_buffer(SBMP_FrmInst *frm, const uint8_t *buffer, uint16_t length)
{
if (! frm->tx_enabled) {
sbmp_error("Can't tx, not enabled.");
-6
View File
@@ -1,6 +0,0 @@
#include <stdarg.h>
#include "esp8266.h"
#include "sbmp_config.h"
#include "sbmp_logging.h"
+2 -2
View File
@@ -9,7 +9,7 @@
*/
// do-nothing definitions
#define sbmp_error(fmt, ...) os_printf("\x1b[31;1mSBMP][E] " #fmt "\x1b[0m\r\n", ##__VA_ARGS__)
#define sbmp_info(fmt, ...) os_printf("\x1b[32;1m[SBMP][i] " #fmt "\x1b[0m\r\n", ##__VA_ARGS__)
#define sbmp_error(fmt, ...) os_printf("\x1b[31;1mSBMP][E] "fmt"\x1b[0m\r\n", ##__VA_ARGS__)
#define sbmp_info(fmt, ...) os_printf("\x1b[32;1m[SBMP][i] "fmt"\x1b[0m\r\n", ##__VA_ARGS__)
#endif /* SBMP_COMMON_H */
+22 -22
View File
@@ -18,7 +18,7 @@ static void handle_hsk_datagram(SBMP_Endpoint *ep, SBMP_Datagram *dg);
/** Rx handler that is assigned to the framing layer */
static void ep_rx_handler(uint8_t *buf, uint16_t len, void *token)
static void FLASH_FN ep_rx_handler(uint8_t *buf, uint16_t len, void *token)
{
// endpoint pointer is stored in the user token
SBMP_Endpoint *ep = (SBMP_Endpoint *)token;
@@ -39,7 +39,7 @@ static void ep_rx_handler(uint8_t *buf, uint16_t len, void *token)
* @param buffer_size : Rx buffer length
* @return the endpoint struct pointer (allocated if ep was NULL)
*/
SBMP_Endpoint *sbmp_ep_init(
SBMP_Endpoint FLASH_FN *sbmp_ep_init(
SBMP_Endpoint *ep,
uint8_t *buffer,
uint16_t buffer_size,
@@ -85,7 +85,7 @@ SBMP_Endpoint *sbmp_ep_init(
*
* @param ep : Endpoint
*/
void sbmp_ep_reset(SBMP_Endpoint *ep)
void FLASH_FN sbmp_ep_reset(SBMP_Endpoint *ep)
{
ep->next_session = 0;
ep->origin = 0;
@@ -103,19 +103,19 @@ void sbmp_ep_reset(SBMP_Endpoint *ep)
// --- Customizing settings ---
/** Set session number (good to randomize before first message) */
void sbmp_ep_seed_session(SBMP_Endpoint *ep, uint16_t sesn)
void FLASH_FN sbmp_ep_seed_session(SBMP_Endpoint *ep, uint16_t sesn)
{
ep->next_session = sesn & 0x7FFF;
}
/** Set the origin bit (bypass handshake) */
void sbmp_ep_set_origin(SBMP_Endpoint *endp, bool bit)
void FLASH_FN sbmp_ep_set_origin(SBMP_Endpoint *endp, bool bit)
{
endp->origin = bit;
}
/** Set the preferred checksum. */
void sbmp_ep_set_preferred_cksum(SBMP_Endpoint *endp, SBMP_CksumType cksum_type)
void FLASH_FN sbmp_ep_set_preferred_cksum(SBMP_Endpoint *endp, SBMP_CksumType cksum_type)
{
if (cksum_type == SBMP_CKSUM_CRC32 && !SBMP_HAS_CRC32) {
sbmp_error("CRC32 not avail, using XOR instead.");
@@ -127,19 +127,19 @@ void sbmp_ep_set_preferred_cksum(SBMP_Endpoint *endp, SBMP_CksumType cksum_type)
/** Enable or disable RX in the FrmInst backing this Endpoint */
void sbmp_ep_enable_rx(SBMP_Endpoint *ep, bool enable_rx)
void FLASH_FN sbmp_ep_enable_rx(SBMP_Endpoint *ep, bool enable_rx)
{
sbmp_frm_enable_rx(&ep->frm, enable_rx);
}
/** Enable or disable TX in the FrmInst backing this Endpoint */
void sbmp_ep_enable_tx(SBMP_Endpoint *ep, bool enable_tx)
void FLASH_FN sbmp_ep_enable_tx(SBMP_Endpoint *ep, bool enable_tx)
{
sbmp_frm_enable_tx(&ep->frm, enable_tx);
}
/** Enable or disable Rx & TX in the FrmInst backing this Endpoint */
void sbmp_ep_enable(SBMP_Endpoint *ep, bool enable)
void FLASH_FN sbmp_ep_enable(SBMP_Endpoint *ep, bool enable)
{
sbmp_frm_enable(&ep->frm, enable);
}
@@ -147,7 +147,7 @@ void sbmp_ep_enable(SBMP_Endpoint *ep, bool enable)
// ---
/** Get a new session number */
static uint16_t next_session(SBMP_Endpoint *ep)
static uint16_t FLASH_FN next_session(SBMP_Endpoint *ep)
{
uint16_t sesn = ep->next_session;
@@ -163,7 +163,7 @@ static uint16_t next_session(SBMP_Endpoint *ep)
// --- Header/body send funcs ---
/** Start a message as a reply */
bool sbmp_ep_start_response(SBMP_Endpoint *ep, SBMP_DgType type, uint16_t length, uint16_t sesn)
bool FLASH_FN sbmp_ep_start_response(SBMP_Endpoint *ep, SBMP_DgType type, uint16_t length, uint16_t sesn)
{
uint16_t peer_accepts = ep->peer_buffer_size - DATAGRA_HEADER_LEN;
@@ -176,7 +176,7 @@ bool sbmp_ep_start_response(SBMP_Endpoint *ep, SBMP_DgType type, uint16_t length
}
/** Start a message in a new session */
bool sbmp_ep_start_session(SBMP_Endpoint *ep, SBMP_DgType type, uint16_t length, uint16_t *sesn_ptr)
bool FLASH_FN sbmp_ep_start_session(SBMP_Endpoint *ep, SBMP_DgType type, uint16_t length, uint16_t *sesn_ptr)
{
uint16_t sn = next_session(ep);
@@ -189,19 +189,19 @@ bool sbmp_ep_start_session(SBMP_Endpoint *ep, SBMP_DgType type, uint16_t length,
}
/** Send one byte in the current message */
bool sbmp_ep_send_byte(SBMP_Endpoint *ep, uint8_t byte)
bool FLASH_FN sbmp_ep_send_byte(SBMP_Endpoint *ep, uint8_t byte)
{
return sbmp_frm_send_byte(&ep->frm, byte);
}
/** Send a data buffer (or a part) in the current message */
uint16_t sbmp_ep_send_buffer(SBMP_Endpoint *ep, const uint8_t *buffer, uint16_t length)
uint16_t FLASH_FN sbmp_ep_send_buffer(SBMP_Endpoint *ep, const uint8_t *buffer, uint16_t length)
{
return sbmp_frm_send_buffer(&ep->frm, buffer, length);
}
/** Rx, pass to framing layer */
SBMP_RxStatus sbmp_ep_receive(SBMP_Endpoint *ep, uint8_t byte)
SBMP_RxStatus FLASH_FN sbmp_ep_receive(SBMP_Endpoint *ep, uint8_t byte)
{
return sbmp_frm_receive(&ep->frm, byte);
}
@@ -210,7 +210,7 @@ SBMP_RxStatus sbmp_ep_receive(SBMP_Endpoint *ep, uint8_t byte)
// --- All-in-one send funcs ---
/** Send a message in a session. */
bool sbmp_ep_send_response(
bool FLASH_FN sbmp_ep_send_response(
SBMP_Endpoint *ep,
SBMP_DgType type,
const uint8_t *buffer,
@@ -230,7 +230,7 @@ bool sbmp_ep_send_response(
}
/** Send message in a new session */
bool sbmp_ep_send_message(
bool FLASH_FN sbmp_ep_send_message(
SBMP_Endpoint *ep,
SBMP_DgType type,
const uint8_t *buffer,
@@ -267,7 +267,7 @@ bool sbmp_ep_send_message(
*
* The buffer is long HSK_PAYLOAD_LEN bytes
*/
static void populate_hsk_buf(SBMP_Endpoint *ep, uint8_t* buf)
static void FLASH_FN populate_hsk_buf(SBMP_Endpoint *ep, uint8_t* buf)
{
// [ pref_crc 1B | buf_size 2B ]
@@ -277,7 +277,7 @@ static void populate_hsk_buf(SBMP_Endpoint *ep, uint8_t* buf)
}
/** Parse peer info from received handhsake dg payload */
static void parse_peer_hsk_buf(SBMP_Endpoint *ep, const uint8_t* buf)
static void FLASH_FN parse_peer_hsk_buf(SBMP_Endpoint *ep, const uint8_t* buf)
{
ep->peer_pref_cksum = buf[0];
ep->peer_buffer_size = (uint16_t)(buf[1] | (buf[2] << 8));
@@ -297,7 +297,7 @@ static void parse_peer_hsk_buf(SBMP_Endpoint *ep, const uint8_t* buf)
* @brief Start a handshake (origin bit arbitration)
* @param ep : Endpoint state
*/
bool sbmp_ep_start_handshake(SBMP_Endpoint *ep)
bool FLASH_FN sbmp_ep_start_handshake(SBMP_Endpoint *ep)
{
if (ep->hsk_state == SBMP_HSK_AWAIT_REPLY) {
// busy now
@@ -322,7 +322,7 @@ bool sbmp_ep_start_handshake(SBMP_Endpoint *ep)
}
/** Get hsk state */
SBMP_HandshakeStatus sbmp_ep_handshake_status(SBMP_Endpoint *ep)
SBMP_HandshakeStatus FLASH_FN sbmp_ep_handshake_status(SBMP_Endpoint *ep)
{
return ep->hsk_state;
}
@@ -335,7 +335,7 @@ SBMP_HandshakeStatus sbmp_ep_handshake_status(SBMP_Endpoint *ep)
* @param ep : endpoint
* @param dg : datagram
*/
static void handle_hsk_datagram(SBMP_Endpoint *ep, SBMP_Datagram *dg)
static void FLASH_FN handle_hsk_datagram(SBMP_Endpoint *ep, SBMP_Datagram *dg)
{
bool hsk_start = (dg->type == SBMP_DG_HSK_START);
bool hsk_accept = (dg->type == SBMP_DG_HSK_ACCEPT);