Firmware
Classes | Macros | Functions
hx_stream.c File Reference

A simple serial line framing protocol based on HDLC with 32-bit CRC protection. More...

#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <crc32.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <perf/perf_counter.h>
#include "hx_stream.h"

Classes

struct  hx_stream
 

Macros

#define FBO   0x7e
 Frame Boundary Octet.
 
#define CEO   0x7c
 Control Escape Octet.
 

Functions

hx_stream_t hx_stream_init (int fd, hx_stream_rx_callback callback, void *arg)
 Allocate a new hx_stream object. More...
 
void hx_stream_free (hx_stream_t stream)
 Free a hx_stream object. More...
 
void hx_stream_set_counters (hx_stream_t stream, perf_counter_t tx_frames, perf_counter_t rx_frames, perf_counter_t rx_errors)
 Set performance counters for the stream. More...
 
void hx_stream_reset (hx_stream_t stream)
 Reset a stream. More...
 
int hx_stream_start (hx_stream_t stream, const void *data, size_t count)
 Prepare to send a frame. More...
 
int hx_stream_send_next (hx_stream_t stream)
 Get the next byte to send for a stream. More...
 
int hx_stream_send (hx_stream_t stream, const void *data, size_t count)
 Send a frame. More...
 
void hx_stream_rx (hx_stream_t stream, uint8_t c)
 Handle a byte from the stream. More...
 

Detailed Description

A simple serial line framing protocol based on HDLC with 32-bit CRC protection.

Function Documentation

§ hx_stream_free()

void hx_stream_free ( hx_stream_t  stream)

Free a hx_stream object.

Parameters
streamA handle returned from hx_stream_init.

§ hx_stream_init()

hx_stream_t hx_stream_init ( int  fd,
hx_stream_rx_callback  callback,
void *  arg 
)

Allocate a new hx_stream object.

Parameters
fdThe file handle over which the protocol will communicate, or -1 if the protocol will use hx_stream_start/hx_stream_send_next.
callbackCalled when a frame is received.
callback_argPassed to the callback.
Returns
A handle to the stream, or NULL if memory could not be allocated.

§ hx_stream_reset()

void hx_stream_reset ( hx_stream_t  stream)

Reset a stream.

Forces the local stream state to idle.

Parameters
streamA handle returned from hx_stream_init.

§ hx_stream_rx()

void hx_stream_rx ( hx_stream_t  stream,
uint8_t  c 
)

Handle a byte from the stream.

Parameters
streamA handle returned from hx_stream_init.
cThe character to process.

§ hx_stream_send()

int hx_stream_send ( hx_stream_t  stream,
const void *  data,
size_t  count 
)

Send a frame.

This function will block until all frame bytes are sent if the descriptor passed to hx_stream_init is marked blocking, otherwise it will return -1 (but may transmit a runt frame at the same time).

Todo:
Handling of non-blocking streams needs to be better.
Parameters
streamA handle returned from hx_stream_init.
dataPointer to the data to send.
countThe number of bytes to send.
Returns
Zero on success, -errno on error.

§ hx_stream_send_next()

int hx_stream_send_next ( hx_stream_t  stream)

Get the next byte to send for a stream.

This requires that the stream be prepared for sending by calling hx_stream_start first.

Parameters
streamA handle returned from hx_stream_init.
Returns
The byte to send, or -1 if there is nothing left to send.

§ hx_stream_set_counters()

void hx_stream_set_counters ( hx_stream_t  stream,
perf_counter_t  tx_frames,
perf_counter_t  rx_frames,
perf_counter_t  rx_errors 
)

Set performance counters for the stream.

Any counter may be set to NULL to disable counting that datum.

Parameters
streamA handle returned from hx_stream_init.
tx_framesCounter for transmitted frames.
rx_framesCounter for received frames.
rx_errorsCounter for short and corrupt received frames.

§ hx_stream_start()

int hx_stream_start ( hx_stream_t  stream,
const void *  data,
size_t  count 
)

Prepare to send a frame.

Use this in conjunction with hx_stream_send_next to set the frame to be transmitted.

Use hx_stream_send() to write to the stream fd directly.

Parameters
streamA handle returned from hx_stream_init.
dataPointer to the data to send.
countThe number of bytes to send.
Returns
Zero on success, -errno on error.