Link object

Link object — Basic methods to create and use RATP links.

Functions

Types and Values

Description

This section defines the RATP link type as well as methods to create and use them.

Example 2. Opening and closing the link

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
static const char *message = "hello world!";
ratp_link_t       *ratp;
ratp_status_t      st;

ratp = ratp_link_new_tty ("/dev/ttyUSB3", B115200, 255);
if (!ratp) {
  fprintf (stderr, "error: couldn't create link\n");
  return EXIT_FAILURE;
}

if ((st = ratp_link_initialize (ratp)) != RATP_STATUS_OK) {
  fprintf (stderr, "error: couldn't initialize link: %s\n", ratp_status_str (st));
  return EXIT_FAILURE;
}

if ((st = ratp_link_active_open (ratp)) != RATP_STATUS_OK) {
  fprintf (stderr, "error: couldn't request active open: %s\n", ratp_status_str (st));
  return -1;
}

if ((st = ratp_link_send (ratp,
                          10000,
                          (const uint8_t *) message, strlen (message) + 1,
                          NULL, NULL)) != RATP_STATUS_OK) {
    fprintf (stderr, "error: couldn't schedule message to send: %s\n", ratp_status_str (st));
    return -1;
}

// Do more things here if needed

ratp_link_close (ratp);
ratp_link_shutdown (ratp);
ratp_link_free (ratp);

Functions

ratp_link_new_tty ()

ratp_link_t *
ratp_link_new_tty (const char *path,
                   speed_t speed,
                   uint8_t mdl);

Create a new RATP link associated to a TTY.

Parameters

path

Serial port device path.

[in]

speed

Baudrate to use in the serial port.

[in]

mdl

Maximum data length allowed in this end.

[in]

Returns

a newly created ratp_link_t which should be disposed with ratp_link_free() or NULL if an error happened.

[transfer full]


ratp_link_new_fifo ()

ratp_link_t *
ratp_link_new_fifo (const char *path_in,
                    const char *path_out,
                    uint8_t mdl);

Create a new RATP link associated to a pair of input/output FIFOs.

Parameters

path_in

Input FIFO path.

[in]

path_out

Output FIFO path.

[in]

mdl

Maximum data length allowed in this end.

[in]

Returns

a newly created ratp_link_t which should be disposed with ratp_link_free() or NULL if an error happened.

[transfer full]


ratp_link_free ()

void
ratp_link_free (ratp_link_t *self);

Dispose a ratp_link_t.

Parameters

self

a ratp_link_t.

[in][transfer full]

ratp_link_initialize ()

ratp_status_t
ratp_link_initialize (ratp_link_t *self);

Initialize a RATP link.

This operation just opens and sets up the physical channel, it doesn't send or receive any RATP packet.

Parameters

self

a ratp_link_t.

[in][not nullable]

Returns

a ratp_status_t.


ratp_link_get_initialized ()

bool
ratp_link_get_initialized (ratp_link_t *self);

Checks whether the RATP link is initialized.

Parameters

self

a ratp_link_t.

[in][not nullable]

Returns

a true if the link is already initialized, false otherwise..


ratp_link_shutdown ()

void
ratp_link_shutdown (ratp_link_t *self);

Shutdown a RATP link.

This operation closes the physical channel.

Parameters

self

a ratp_link_t.

[in][not nullable]

ratp_link_initialized_func ()

void
(*ratp_link_initialized_func) (ratp_link_t *self,
                               void *user_data);

Handler used to report initialization to the user.

Parameters

self

a ratp_link_t.

[in][not nullable]

user_data

user data given by the user when registering the callback.

[in]

ratp_link_set_initialized_callback ()

void
ratp_link_set_initialized_callback (ratp_link_t *self,
                                    ratp_link_initialized_func callback,
                                    void *user_data);

Set the callback to be called when the RATP link is initialized.

The callback is guaranteed to be called from within the link management thread.

Parameters

self

a ratp_link_t.

[in][not nullable]

callback

a ratp_link_initialized_func.

[in][nullable]

user_data

user data to be passed to callback .

[in][nullable]

ratp_link_state_str ()

const char *
ratp_link_state_str (ratp_link_state_t state);

Gets a description for the given ratp_link_state_t.

Parameters

state

a ratp_link_state_t.

[in]

Returns

a constant string.


ratp_link_get_state ()

ratp_link_state_t
ratp_link_get_state (ratp_link_t *self);

Gets the current state of the RATP link.

Parameters

self

a ratp_link_t.

[in][not nullable]

Returns

a ratp_link_state_t.


ratp_link_state_update_func ()

void
(*ratp_link_state_update_func) (ratp_link_t *self,
                                ratp_link_state_t old_state,
                                ratp_link_state_t new_state,
                                ratp_status_t status_reason,
                                void *user_data);

Handler used to report state updates to the user.

Parameters

self

a ratp_link_t.

[in][not nullable]

old_state

the previous RATP link state.

[in]

new_state

the new RATP link state.

[in]

status_reason

the reason for the state change.

[in]

user_data

user data given by the user when registering the callback.

[in][nullable]

ratp_link_set_state_update_callback ()

void
ratp_link_set_state_update_callback (ratp_link_t *self,
                                     ratp_link_state_update_func callback,
                                     void *user_data);

Set the callback to be called when RATP link state updates happen.

Parameters

self

a ratp_link_t.

[in][not nullable]

callback

a ratp_link_state_update_func.

[in][nullable]

user_data

user data to be passed to callback .

[in][nullable]

ratp_link_active_open ()

ratp_status_t
ratp_link_active_open (ratp_link_t *self);

Requests to actively open the RATP link.

This method does not wait for the RATP link to be established, it just schedules the request. Link state updates may be monitored with the callback configured via ratp_link_set_state_update_callback().

Parameters

self

a ratp_link_t.

[in][not nullable]

Returns

a ratp_status_t.


ratp_link_active_open_sync ()

ratp_status_t
ratp_link_active_open_sync (ratp_link_t *self,
                            unsigned long  timeout_ms);

Requests to actively open the RATP link.

This method blocks until the RATP link is established or until timeout_ms are elapsed.

Monitoring of the link state with ratp_link_set_state_update_callback() cannot be performed while this operation is ongoing; any callback previously configured will be ignored.

Parameters

self

a ratp_link_t.

[in][not nullable]

timeout_ms

maximum time, in milliseconds, to wait for the link to be established; or 0 to wait forever.

[in]

Returns

a ratp_status_t.


ratp_link_passive_open ()

ratp_status_t
ratp_link_passive_open (ratp_link_t *self);

Requests to passively open the RATP link.

This method does not wait for the RATP link to be established, it just schedules the request. Link state updates may be monitored with the callback configured via ratp_link_set_state_update_callback().

Parameters

self

a ratp_link_t.

[in][not nullable]

Returns

a ratp_status_t.


ratp_link_close ()

void
ratp_link_close (ratp_link_t *self);

Requests to close the RATP link.

This method does not wait for the RATP link to be closed, it just schedules the request. Link state updates may be monitored with the callback configured via ratp_link_set_state_update_callback().

Parameters

self

a ratp_link_t.

[in][not nullable]

Returns

a ratp_status_t.


ratp_link_close_sync ()

ratp_status_t
ratp_link_close_sync (ratp_link_t *self,
                      unsigned long  timeout_ms);

Requests to close the RATP link.

This method blocks until the RATP link is established or until timeout_ms are elapsed.

Monitoring of the link state with ratp_link_set_state_update_callback() cannot be performed while this operation is ongoing; any callback previously configured will be ignored.

Parameters

self

a ratp_link_t.

[in][not nullable]

timeout_ms

maximum time, in milliseconds, to wait for the link to be closed; or 0 to wait forever.

[in]

Returns

a ratp_status_t.


ratp_link_send_ready_func ()

void
(*ratp_link_send_ready_func) (ratp_link_t *self,
                              ratp_status_t status_reason,
                              void *user_data);

Handler used to report when a sent buffer has been completely sent.

Parameters

self

a (in) (not nullable): ratp_link_t.

 

status_reason

the status of the send operation.

[in]

user_data

user data given by the user when registering the callback.

[in][nullable]

ratp_link_send ()

ratp_status_t
ratp_link_send (ratp_link_t *self,
                unsigned long  timeout_ms,
                const uint8_t *buffer,
                size_t buffer_size,
                ratp_link_send_ready_func callback,
                void *user_data);

Schedules the data in buffer to be sent. If the link is not yet established, it will be sent as soon as the establishment is finished.

When all the data is sent and acknowledged by the peer, or if any error happens (timeout included), callback will be called to inform the user of the operation result.

Parameters

self

a ratp_link_t.

[in][not nullable]

timeout_ms

(in) maximum time, in milliseconds, to send the whole buffer ; or 0 to wait forever.

 

buffer

data to send through the link.

[in][array length=buffer_size][not nullable]

buffer_size

size of buffer .

[in]

callback

a ratp_link_send_ready_func.

[in][nullable]

user_data

user data to be passed to callback .

[in][nullable]

ratp_link_receive_ready_func ()

void
(*ratp_link_receive_ready_func) (ratp_link_t *self,
                                 const uint8_t *buffer,
                                 size_t buffer_size,
                                 void *user_data);

Handler used to report data received to the user.

Parameters

self

a ratp_link_t.

[in][not nullable]

buffer

data received through the link.

[in][array length=buffer_size][not nullable]

buffer_size

size of buffer .

[in]

user_data

user data given by the user when registering the callback.

[in][nullable]

ratp_link_set_receive_callback ()

void
ratp_link_set_receive_callback (ratp_link_t *self,
                                ratp_link_receive_ready_func callback,
                                void *user_data);

Set the callback to be called when data is received in the RATP link.

Parameters

self

a ratp_link_t.

[in][not nullable]

callback

a ratp_link_receive_ready_func.

[in][nullable]

user_data

user data to be passed to callback .

[in][nullable]

Types and Values

ratp_link_t

typedef struct ratp_link_s ratp_link_t;

This is an opaque type representing a RATP link.


enum ratp_link_state_t

State of the RATP link.

Members

RATP_LINK_STATE_LISTEN

Listen.

 

RATP_LINK_STATE_SYN_SENT

SYN sent.

 

RATP_LINK_STATE_SYN_RECEIVED

SYN received.

 

RATP_LINK_STATE_ESTABLISHED

Established.

 

RATP_LINK_STATE_FIN_WAIT

FIN wait.

 

RATP_LINK_STATE_LAST_ACK

Last ACK.

 

RATP_LINK_STATE_CLOSING

Closing.

 

RATP_LINK_STATE_TIME_WAIT

Time wait.

 

RATP_LINK_STATE_CLOSED

Closed.