API Reference¶
Task Scheduler¶
-
void
schedule_task(void (*cb)(void *arg), void *arg, )¶ Schedule task
Scheduling tasks is safe from an interrupt handler and from an other task.
- Parameters
cb: task function to be scheduled
-
void
scheduler_run_task(void)¶ Run first task in queue This function should be called from the main loop of a user application
-
static void
scheduler_run_tasks(void)¶ Run all tasks in loop
This function should be used as a main loop in user application
Timers¶
-
TIMER_INIT(__tim)¶ Initialize a timer at compile time
-
void
timer_subsystem_init(void)¶ Initialize timer subsystem
This function should be called from architecture dependant timer interrupt handler.
-
static void
timer_subsystem_stop(void)¶ Stop architecture dependant timer interrupt
-
void
timer_init(tim_t *timer)¶ Initialize timer
- Parameters
timer: timer
-
void
timer_add(tim_t *timer, uint32_t expiry, void (*cb)(void *), void *arg, )¶ Schedule timer
- Parameters
timer: timerexpiry: expiry in microsecondscb: callback function
-
void
timer_reschedule(tim_t *timer, uint32_t expiry)¶ Reschedule timer
This function is not re-entrant, a timer cannot be added multiple times.
- Parameters
timer: timerexpiry: expiry in microseconds
-
void
timer_del(tim_t *timer)¶ Remove timer
This function is re-entrant, it is safe to remove a timer that is not scheduled.
- Parameters
timer: timer
-
void
timer_process(void)¶ Process scheduled timers
This function should only be used in architecture dependant timer interrupt.
-
static uint8_t
timer_is_pending(tim_t *timer)¶ Check if timer is pending
- Return
- 0 if timer if not pending, 1 otherwise
- Parameters
timer: timer
Power Management¶
-
power_management_pwr_down_reset¶ Reset inactivity counter
Use this macro to prevent the uC from sleeping when there is a given activity.
-
power_management_set_inactivity(value)¶ Set a sleep delay
The uC will go to sleep after that delay. Unit: second.
-
void
power_management_power_down_init(uint16_t inactivity_timeout, void (*on_sleep)(void *arg), void *arg, )¶ Initialize power management system
- Parameters
inactivity_timeout: Set the uC to sleep after that delay (in seconds)on_sleep: Callback to be called before going to sleeparg: Callback’s argument pointer
-
void
power_management_power_down_enable(void)¶ Enable power down mode
-
void
power_management_power_down_disable(void)¶ Disable power down mode
Buffers¶
Defines
-
buf2sbuf(buf)¶
-
sbuf2buf(sbuf)¶
-
SBUF_INITS(str)¶
-
SBUF_INIT_BIN(__data)¶
-
SBUF_INIT(__data, __len)¶
-
BUF_INIT(__data, __len)¶
-
BUF_INIT_BIN(__data)¶
-
SBUF_INIT_NULL¶
-
BUF(sz)¶
Functions
-
static void *
__memmem(const void *haystack, unsigned haystacklen, const void *needle, unsigned needlelen)¶
-
struct
static_buf¶ - #include <buf.h>
Static buffer
-
struct
buf¶ - #include <buf.h>
Buffer
Circular ring buffers¶
Defines
-
TYPE¶ Single producer - single consumer circular buffer ring implementation. Note: multi prod/cons rings on AVR architecture are not possible as atomic ‘compare & set’ is not provided.
-
RING_DECL(name, size)¶ Ring declaration The following ring declaration can be used for declaring a ring as a global variable in a C file. The size of a ring MUST be a power of 2.
-
STATIC_RING_DECL(name, size)¶ Static ring declaration The following static ring declaration can be used for declaring a ring as a global variable in a C file. The size of a ring MUST be a power of 2.
-
RING_DECL_IN_STRUCT(name, size)¶ Ring declaration in C structures
Use this macro to declare rings in C structures
Example of usage: struct { RING_DECL_IN_STRUCT(my_ring, 4); } a = { .my_ring = RING_INIT(a.my_ring), };
-
RING_INIT(name)¶ Initialize ring at compile time
- Parameters
name: ring name
Functions
-
static void
ring_init(ring_t *ring, int size)¶ Initialize ring
- Parameters
ring: ringsize: ring size
-
static uint8_t
ring_is_full(const ring_t *ring)¶ Check if ring is full
- Return
- 1 if full, 0 otherwise
- Parameters
ring: ring
-
static uint8_t
ring_is_empty(const ring_t *ring)¶ Check if ring is empty
- Return
- 1 if empty, 0 otherwise
- Parameters
ring: ring
-
static int
ring_free_entries(const ring_t *ring)¶ Get ring available entriees
- Return
- number of free entriess in ring
- Parameters
ring: ring
-
static void
__ring_addc(ring_t *ring, uint8_t c)¶ Add byte to ring without checking
- Parameters
ring: ringc: byte to add
-
static int
ring_addc(ring_t *ring, uint8_t c)¶ Add byte to ring
- Return
- 0 on success, -1 on failure
- Parameters
ring: ringc: byte to add
-
static void
__ring_addbuf(ring_t *ring, const buf_t *buf)¶ Add buffer to ring without checking
- Parameters
ring: ringbuf: buffer
-
static int
ring_addbuf(ring_t *ring, const buf_t *buf)¶ Add buffer to ring
- Return
- 0 on success, -1 on failure
- Parameters
ring: ringbuf: buffer
-
static int
ring_add(ring_t *ring, const void *data, int len)¶ Add data to ring
- Return
- 0 on success, -1 on failure
- Parameters
ring: ringdata: pointer to data to addlen: data length
-
static void
__ring_getc_at(ring_t *ring, uint8_t *c, int pos)¶ Get byte at position in ring
- Parameters
ring: ringc: bytepos: position in ring
-
static void
__ring_getc(ring_t *ring, uint8_t *c)¶ Get byte from ring without checking
- Parameters
ring: ringc: byte
-
static int
__ring_get_dont_skip(const ring_t *ring, buf_t *buf, int len)¶ Get buffer from ring without skipping
- Return
- 0 on success, -1 on failure
- Parameters
ring: ringbuf: bufferlen: data length to get
-
static int
ring_getc(ring_t *ring, uint8_t *c)¶ Get byte from ring
- Return
- 0 on success, -1 on failure
- Parameters
ring: ringc: bytes
-
static int
ring_get_last_byte(ring_t *ring, uint8_t *c)¶ Get last byte from ring
- Return
- 0 on sucess, -1 on failure
- Parameters
ring: ringc: byte
-
static void
__ring_skip(ring_t *ring, int len)¶ Skip data in ring without checking
- Parameters
ring: ringlen: length to skip
-
static int
ring_skip_upto(ring_t *ring, uint8_t c)¶ Skip data up to given byte
- Return
- 0 on success, -1 on failure
- Parameters
ring: ringc: byte
-
static int
__ring_get(ring_t *ring, buf_t *buf, int max_len)¶ Get buffer from ring
- Parameters
ring: ringbuf: buffermax_len: data length to get
-
static void
__ring_get_buf(ring_t *ring, buf_t *buf)¶ Get buffer from ring without checking
- Parameters
ring: ringbuf: buffer
-
static int
ring_get(ring_t *ring, buf_t *buf)¶ Get buffer from ring
- Return
- 0 on success, -1 on failure
- Parameters
ring: ringbuf: buffer
-
static void
ring_skip(ring_t *ring, int len)¶ Skip data in ring without checking
- Parameters
ring: ringlen: data length to skip
-
static void
ring_print_limit(const ring_t *ring, int limit, uint8_t hex)¶ Print data limited by size
- Parameters
ring: ringlimit: size to printhex: print in hexadecimal
-
static void
ring_print_hex(const ring_t *ring)¶ Print ring content in hexadecimal
- Parameters
ring: ring
-
static int
ring_cmp(const ring_t *ring, const uint8_t *data, int len)¶ Compare ring content with data in linear buffer
- Return
- 0 if data are identical, -1 otherwise
- Parameters
ring: ringdata: linear bufferlen: length to compare
-
struct
ring¶
Lists¶
Defines
-
LIST_POISON1¶
-
LIST_POISON2¶
-
LIST_HEAD_INIT(name)¶
-
LIST_HEAD(name)¶
-
list_entry(ptr, type, member)¶
-
list_first_entry(ptr, type, member)¶
-
list_last_entry(ptr, type, member)¶
-
list_next_entry(pos, member)¶
-
list_prev_entry(pos, member)¶
-
list_for_each(pos, head)¶
-
list_for_each_prev(pos, head)¶
-
list_for_each_safe(pos, n, head)¶
-
list_for_each_prev_safe(pos, n, head)¶
-
list_for_each_entry(pos, head, member)¶
-
list_for_each_entry_reverse(pos, head, member)¶
-
list_for_each_entry_safe(pos, n, head, member)¶
-
SLIST_HEAD_INIT(name)¶
-
SLIST_HEAD(name)¶
-
slist_first_entry(list, type, member)¶
-
slist_for_each(pos, list)¶
Functions
-
static void
slist_add(slist_node_t *node, slist_t *list)¶
-
static void
slist_add_tail(slist_node_t *node, slist_t *list)¶
-
static slist_node_t *
slist_get_first(slist_t *list)¶
-
struct
list_head¶
-
struct
slist_node¶ Public Members
-
struct slist_node *
next¶
-
struct slist_node *
-
struct
slist_head¶
Misc Utils¶
Defines
-
container_of(ptr, type, member)¶
-
countof(array)¶
-
POWEROF2(x)¶
-
__error__(msg)¶
-
STATIC_ASSERT(cond)¶
-
STATIC_IF(cond, if_statement, else_statement)¶
-
htons(x)¶
-
ntohs(x)¶
-
htonl(l)¶
-
ntohl(x)¶
-
__STR(x)¶
-
MIN(a, b)¶
-
MAX(a, b)¶
-
ROUNDUP_PWR2(x)¶
-
IS_UNSIGNED(a)¶
-
MAX_VALUE_UNSIGNED(a)¶
-
MAX_VALUE_SIGNED(a)¶
-
MAX_VALUE(a)¶
-
__PACKED__¶
Functions
-
static long
map(long x, long in_min, long in_max, long out_min, long out_max)¶
Networking¶
Interface¶
-
void
if_init(iface_t *iface, uint8_t type, ring_t *pkt_pool, ring_t *rx, ring_t *tx, uint8_t is_interrupt_driven)¶ Initialize an interface.
- Parameters
iface: interface to initializetype: type of interface (eg: IF_TYPE_ETHERNET)pkt_pool: driver’s free packet poolrx: inbound packet buckettx: outbound packet bucketis_interrupt_driven: indicates if the driver handle packet reception/sending on hardware interrupt
-
void
if_schedule_receive(iface_t *iface, pkt_t **pkt)¶ Schedule packet reception
- Parameters
iface: network interfacepkt: packet to be scheduled The packet is set to null.
-
void
if_schedule_tx_pkt_free(pkt_t **pkt)¶ Free transmitted packet
- Parameters
pkt: packet to be freed asynchronously
-
void
if_dump_stats(const iface_t *iface)¶ Dump interface statistics
- Parameters
iface: interface
Packet memory pool¶
-
void
pkt_mempool_init(void)¶ Initialize packet memory pool
-
void
pkt_mempool_shutdown(void)¶ Destroy packet memory pool
-
pkt_t *
pkt_get(ring_t *ring)¶ Get a packet from pool
- Return
- packet or NULL if no packets in pool
- Parameters
ring: packet pool
-
int
pkt_put(ring_t *ring, pkt_t *pkt)¶ Put a packet to pool
- Return
- 0 on success, -1 if no more room in pool
- Parameters
ring: packet poolpkt: packet
-
pkt_t *
pkt_alloc(void)¶ Allocate a packet
Note: Allocs and frees can only be called from a task scheduler
- Return
- new packet or NULL if no more packets
-
void
pkt_free(pkt_t *pkt)¶ Free a packet
Note: Allocs and frees can only be called from a task scheduler
- Parameters
pkt: packet to free
-
pkt_t *
pkt_alloc_emergency(void)¶ Allocate an emergency packet
The emergency packet should only be used for sending to avoid a race on packet allocation if all packets are used.
- Return
- packet
-
int
pkt_is_emergency(pkt_t *pkt)¶ Check if a packet is an emergency packet
- Return
- 1 is packet is an emergency packet, 0 otherwise
- Parameters
pkt: packet
-
static int
pkt_len(const pkt_t *pkt)¶ Get packet length
- Return
- packet length
- Parameters
pkt: packet
-
static void
pkt_retain(pkt_t *pkt)¶ Retain packet
Increment packet reference counter
- Parameters
pkt: packet
-
unsigned int
pkt_pool_get_nb_free(void)¶ Get number of available packets
- Return
- number of available packets
-
void
pkt_get_traced_pkts(void)¶ Get last used functions of packets in pool (for debugging)
Socket API¶
-
enum
sock_status¶ Values:
-
SOCK_CLOSED¶
-
SOCK_TCP_SYN_SENT¶
-
SOCK_TCP_SYN_ACK_SENT¶
-
SOCK_TCP_FIN_SENT¶
-
SOCK_CONNECTED¶
-
-
struct
sock_info¶
-
void
socket_event_register(sock_info_t *sock_info, uint8_t events, void (*ev_cb)(event_t *ev, uint8_t events))¶ Register an event on a socket
- Parameters
sock_info: network socketevents: events to wake up onev_cb: function to be called on wake up
-
void
socket_event_register_fd(int fd, uint8_t events, void (*ev_cb)(event_t *ev, uint8_t events))¶ Register an event on a file descriptor
- Parameters
fd: file descriptorevents: events to wake up onev_cb: function to be called on wake up
-
static void
socket_event_unregister(sock_info_t *sock_info)¶ Unregister events on a socket
- Parameters
sock_info: network socket
-
static void
socket_event_set_mask(sock_info_t *sock_info, uint8_t events)¶ Set event mask on a socket
- Parameters
sock_info: network socketevents: event mask to wake up on
-
static void
socket_event_clear_mask(sock_info_t *sock_info, uint8_t events)¶ Clear event mask on a socket
- Parameters
sock_info: network socketevents: events to be cleared
-
static sock_info_t *
socket_event_get_sock_info(event_t *ev)¶ Get socket from event
- Return
- network socket
- Parameters
ev: event
-
int
socket(int family, int type, int protocol)¶ Initialize a BSD compatible network socket
- Return
- a file descriptor on success, -1 on failure
- Parameters
family: family of the sockettype: type of the socketprotocol: used protocol
-
int
close(int fd)¶ Close a BSD compatible network socket
- Return
- 0 on success, -1 on failure
- Parameters
fd: file descriptor
-
int
bind(int fd, const struct sockaddr *addr, socklen_t addrlen)¶ Bind a BSD compatible network socket
- Return
- 0 on success, -1 on failure
- Parameters
fd: file descriptorsockaddr: addressaddrlen: address length
-
int
listen(int fd, int backlog)¶ Listen on a BSD compatible network socket
- Return
- 0 on success, -1 on failure
- Parameters
fd: file descriptorbacklog: maximum number of unhandled client connections
-
int
accept(int fd, struct sockaddr *addr, socklen_t *addrlen)¶ Accept an incoming connection on a BSD compatible network socket
- Return
- 0 on success, -1 on failure
- Parameters
fd: file descriptoraddr: addressaddrlen: address length
-
int
connect(int fd, const struct sockaddr *addr, socklen_t addrlen)¶ Connect a BSD compatible network socket
- Return
- 0 on success, -1 on failure
- Parameters
fd: file descriptoraddr: addressaddrlen: address length
-
ssize_t
sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)¶ Send data on a BSD compatible network socket
- Return
- 0 on success, -1 on failure
- Parameters
fd: file descriptorbuf: data bufferlen: data lengthflags: BSD flagsaddr: dest addressaddrlen: address length
-
ssize_t
recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen)¶ Receive a buffer from a BSD compatible network socket
- Return
- 0 on success, -1 on failure
- Parameters
fd: file descriptorbuf: data bufferlen: data lengthflags: BSD flagsaddr: source addressaddrlen: address length
-
int
socket_get_pkt(int fd, pkt_t **pkt, struct sockaddr_in *addr)¶ Get packet from a BSD compatible network socket
- Return
- 0 on success, -1 on failure
- Parameters
fd: file descriptorpkt: packetaddr: source address
-
int
socket_put_sbuf(int fd, const sbuf_t *sbuf, const struct sockaddr_in *addr)¶ Send data on a BSD compatible network socket
- Return
- 0 on success, -1 on failure
- Parameters
fd: file descriptorsbuf: static bufferaddr: dest sockaddr
-
int
__socket_get_pkt(sock_info_t *sock_info, pkt_t **pkt, uint32_t *src_addr, uint16_t *src_port)¶ Get packet from a network socket
- Return
- 0 on success, -1 on failure
- Parameters
sock_info: network socketpkt: packetsrc_addr: source addresssrc_port: source port
-
int
__socket_put_sbuf(sock_info_t *sock_info, const sbuf_t *sbuf, uint32_t dst_addr, uint16_t dst_port)¶ Send data on a network socket
- Return
- 0 on success, -1 on failure
- Parameters
sock_info: network socketsbuf: static bufferdst_addr: dest addressdst_port: dest port
-
int
sock_info_init(sock_info_t *sock_info, int sock_type)¶ Initialize a network socket
- Return
- 0 on success, -1 on failure
- Parameters
sock_info: network sockettype: type of the socket
-
int
sock_info_bind(sock_info_t *sock_info, uint16_t port)¶ Bind a network socket
- Return
- 0 on success, -1 on failure
- Parameters
sock_info: network socketport: local port
-
int
sock_info_close(sock_info_t *sock_info)¶ Close a network socket
- Return
- 0 on success, -1 on failure
- Parameters
sock_info: network socket
-
int
sock_info_listen(sock_info_t *sock_info, int backlog)¶ Listen on a network socket
- Return
- 0 on success, -1 on failure
- Parameters
sock_info: network socketbacklog: maximum number of unhandled client connections
-
int
sock_info_accept(sock_info_t *sock_info_server, sock_info_t *sock_info_client, uint32_t *src_addr, uint16_t *src_port)¶ Accept a connection on a network socket
- Return
- 0 on success, -1 on failure
- Parameters
sock_info_server: local network socketsock_info_client: client network socketsrc_addr: client addresssrc_port: client port
Swen (level 2) API¶
-
enum
generic_cmd_status¶ Values:
-
GENERIC_CMD_STATUS_OK¶
-
GENERIC_CMD_STATUS_RCV¶
-
GENERIC_CMD_STATUS_LIST¶
-
GENERIC_CMD_STATUS_ERROR_FULL¶
-
GENERIC_CMD_STATUS_ERROR_TIMEOUT¶
-
GENERIC_CMD_STATUS_ERROR_DUPLICATE¶
-
GENERIC_CMD_STATUS_ERROR_NOT_FOUND¶
-
-
int
swen_sendto(iface_t *iface, uint8_t to, const sbuf_t *sbuf)¶ Send buffer to a peer
- Parameters
iface: interfaceto: destination addresssbuf: static buffer to send
-
void
swen_generic_cmds_init(void (*cb)(uint16_t cmd, uint8_t status))¶ Initialize generic commands
- Parameters
cb: callback on reception
-
void
swen_generic_cmds_start_recording(int16_t value)¶ Start recording generic commands
- Parameters
value: command number to record
-
void
swen_ev_set(void (*ev_cb)(uint8_t from, uint8_t events, buf_t *buf))¶ Set event callback
- Parameters
ev_cb: function to be called on wake up
-
void
swen_generic_cmds_delete_recorded_cmd(int number)¶ Delete recorded generic command
- Parameters
number: command number
Swen (level 3) API¶
-
enum
swen_l3_state¶ Values:
-
S_STATE_CLOSED¶
-
S_STATE_CLOSING¶
-
S_STATE_CONNECTING¶
-
S_STATE_CONN_COMPLETE¶
-
S_STATE_CONNECTED¶
-
-
static uint8_t
swen_l3_get_state(swen_l3_assoc_t *assoc)¶ Get association state
- Return
- association state
- Parameters
assoc: association
-
void
swen_l3_assoc_init(swen_l3_assoc_t *assoc, const uint32_t *enc_key)¶ Initialize an association
- Parameters
assoc: associationenc_key: encryption key
-
void
swen_l3_assoc_shutdown(swen_l3_assoc_t *assoc)¶ Shutdown association
- Parameters
assoc: association
-
int
swen_l3_associate(swen_l3_assoc_t *assoc)¶ Associate with peer
- Return
- 0 on success, -1 on failure
- Parameters
assoc: association
-
void
swen_l3_disassociate(swen_l3_assoc_t *assoc)¶ Disassociate from peer
- Parameters
assoc: association
-
static uint8_t
swen_l3_is_assoc_bound(swen_l3_assoc_t *assoc)¶ Check if an association is made
- Return
- 0 if not bound, 1 if bound
- Parameters
assoc: association
-
void
swen_l3_assoc_bind(swen_l3_assoc_t *assoc, uint8_t to, iface_t *iface)¶ Bind
Setup destination address and interface
- Parameters
assoc: associationto: remote addressiface: interface
-
int
swen_l3_send(swen_l3_assoc_t *assoc, const sbuf_t *sbuf)¶ Send static buffer
- Return
- 0 on success, -1 on failure
- Parameters
assoc: associationsbuf: static buffer to send
-
static int
swen_l3_send_buf(swen_l3_assoc_t *assoc, const buf_t *buf)¶ Send buffer
- Return
- 0 on success, -1 on failure
- Parameters
assoc: associationbuf: buffer to send
-
pkt_t *
swen_l3_get_pkt(swen_l3_assoc_t *assoc)¶ Get received packets
- Return
- received packet or NULL if no packet has been received
- Parameters
assoc: association
-
static void
swen_l3_event_register(swen_l3_assoc_t *assoc, uint8_t events, void (*ev_cb)(event_t *ev, uint8_t events))¶ Register events
- Parameters
assoc: associationevents: events to wake up onev_cb: function to be called on wake up
-
static void
swen_l3_event_unregister(swen_l3_assoc_t *assoc)¶ Unregister events
- Parameters
assoc: association
-
static void
swen_l3_event_set_mask(swen_l3_assoc_t *assoc, uint8_t events)¶ Set event mask
- Parameters
assoc: associationevents: event mask to wake up on
-
static void
swen_l3_event_clear_mask(swen_l3_assoc_t *assoc, uint8_t events)¶ Clear event mask
- Parameters
assoc: associationevents: events to be cleared
-
static swen_l3_assoc_t *
swen_l3_event_get_assoc(event_t *ev)¶ Get association from event
- Return
- association
- Parameters
ev: event