tinyproto
Classes | Typedefs | Functions
Tiny HDLC protocol API functions

high level HDLC protocol implementation More...

Classes

struct  _hdlc_handle_t
 Structure describes configuration of lowest HDLC level Initialize this structure by 0 before passing to hdlc_init() function. More...
 
class  tinyproto::Hdlc
 Hdlc class incapsulates hdlc Protocol functionality. More...
 

Typedefs

typedef struct _hdlc_handle_t hdlc_struct_t
 Structure describes configuration of lowest HDLC level Initialize this structure by 0 before passing to hdlc_init() function.
 
typedef struct _hdlc_handle_thdlc_handle_t
 hdlc handle
 

Functions

hdlc_handle_t hdlc_init (hdlc_struct_t *hdlc_info)
 Initializes hdlc level and returns hdlc handle or NULL in case of error. More...
 
int hdlc_close (hdlc_handle_t handle)
 Shutdowns all hdlc activity. More...
 
void hdlc_reset (hdlc_handle_t handle)
 Resets hdlc state. More...
 
int hdlc_run_rx (hdlc_handle_t handle, const void *data, int len, int *error)
 Processes incoming data. More...
 
int hdlc_run_tx (hdlc_handle_t handle)
 Runs transmission at hdlc level. More...
 
int hdlc_get_tx_data (hdlc_handle_t handle, void *data, int len)
 If hdlc protocol has some data to send it will full data with This function returns either if no more data to send, or specified buffer is filled completely. More...
 
int hdlc_send (hdlc_handle_t handle, const void *data, int len, uint32_t timeout)
 Puts next frame for sending. More...
 

Detailed Description

high level HDLC protocol implementation

this group implements high level HDLC functions, which implement framing only according to RFC 1662: 0x7E, 0x7D, 0x20 (ISO Standard 3309-1979).

Function Documentation

◆ hdlc_close()

int hdlc_close ( hdlc_handle_t  handle)

Shutdowns all hdlc activity.

Parameters
handlehandle to hdlc instance

◆ hdlc_get_tx_data()

int hdlc_get_tx_data ( hdlc_handle_t  handle,
void *  data,
int  len 
)

If hdlc protocol has some data to send it will full data with This function returns either if no more data to send, or specified buffer is filled completely.

Parameters
handlehandle to hdlc instance
datapointer to buffer to fill with data
lenlength of specified buffer
Returns
number of bytes written to specified buffer

◆ hdlc_init()

hdlc_handle_t hdlc_init ( hdlc_struct_t hdlc_info)

Initializes hdlc level and returns hdlc handle or NULL in case of error.

Parameters
hdlc_infopointer to hdlc_struct_t structure, which defines user-specific configuration
Returns
handle to hdlc instance or NULL.
Warning
hdlc_info structure passed to the function must be allocated all the time.

◆ hdlc_reset()

void hdlc_reset ( hdlc_handle_t  handle)

Resets hdlc state.

Use this function, if hw error happened on tx or rx line, and this requires hardware change, and cancelling current operation.

Parameters
handlehandle to hdlc instance

◆ hdlc_run_rx()

int hdlc_run_rx ( hdlc_handle_t  handle,
const void *  data,
int  len,
int *  error 
)

Processes incoming data.

Implementation of reading data from hw is user responsibility. If hdlc_run_rx() returns value less than size of data passed to the function, then hdlc_run_rx() must be called later second time with the pointer to and size of not processed bytes.

If you don't care about errors on RX line, it is allowed to ignore all error codes except TINY_ERR_FAILED, which means general failure.

if hdlc_run_rx() returns 0 bytes processed, just call it once again. It is guaranteed, that at least second call will process bytes.

This function will return the following codes in error field:

  • TINY_ERR_DATA_TOO_LARGE if receiving data fails to fit incoming buffer
  • TINY_ERR_FAILED if generic failure happened
  • TINY_ERR_WRONG_CRC if crc field of incoming frame is incorrect
  • TINY_SUCCESS if operation completed successfully
Parameters
handlehandle to hdlc instance
datapointer to incoming data to process
lensize of received data in bytes
errorpointer to store error code. If no error, 0 is returned. this argument can be NULL.
Returns
number of processed bytes from specified data buffer. Never returns negative value

◆ hdlc_run_tx()

int hdlc_run_tx ( hdlc_handle_t  handle)

Runs transmission at hdlc level.

If there is frame to send, or send is in progress, this function will call send_tx() callback multiple times. If send_tx() callback reports 0, that means that hw device is busy and in this case hdlc_run_tx() will return immediately.

Warning
this function must be called from one thread only.
Parameters
handlehandle to hdlc instance
Returns
negative value in case of error 0 if hw is busy, or no data is waiting for sending positive number of bytes passed to hw channel.

◆ hdlc_send()

int hdlc_send ( hdlc_handle_t  handle,
const void *  data,
int  len,
uint32_t  timeout 
)

Puts next frame for sending.

This function is thread-safe. You may call it from parallel threads.

If multithread_mode is specificed for hdlc protocol, then hdlc_send() function will wait for specified timeout until some tx thread, implemented by application, completes sending of the frame. If timeout happens, but the frame is not sent still, hdlc level rejects sending of the frame. In this case the frame will be sent partially, causing RX errors on other side. Please use reasonable timeout.

If multithread_mode is disabled for hdlc protocol, then hdlc_send() function will start frame sending immediately by itself if TX line is not busy. hdlc_send() will block until frame is sent or timeout. If timeout happens, but the frame is not sent still, hdlc level rejects sending of the frame. In this case the frame will be sent partially, causing RX errors on other side. Please use reasonable timeout.

If timeout is specified as 0, hdlc_send() function will not wait or perform send operation, but only pass data pointer to hdlc state machine. In this case, some other thread needs to or in the same thread you need to send data using hdlc_run_tx().

Parameters
handlehandle to hdlc instance
datapointer to new data to send (can be NULL is you need to retry sending)
lensize of data to send in bytes
timeouttime in milliseconds to wait for data to be sent
Returns
TINY_ERR_FAILED if generic error happens TINY_ERR_BUSY if TX queue is busy with another frame. TINY_ERR_TIMEOUT if send operation cannot be completed in specified time. TINY_ERR_INVALID_DATA if len is zero. TINY_SUCCESS if data is successfully sent
Warning
buffer with data must be available all the time until data are actually sent to tx hw channel. That is if you use zero timeout, you need to use callback to understand, when buffer is not longer needed at hdlc level.
Note
TINY_ERR_BUSY and TINY_ERR_INVALID_DATA refer to putting new frame to TX hdlc queue.