7.2. Network API

7.2.1. Reference implementations

Anjay Lite provides a reference implementation of its network API, designed to work with systems that support the BSD-style socket API. You can find the full implementation in src/anj/compat/posix/anj_socket.c. If your target platform does not support BSD-style sockets, you must implement a custom network compatibility layer that Anjay Lite can use.

To help with this process, Anjay Lite includes tutorial examples featuring minimal and compact implementations:

7.2.2. Functions to implement

When building a custom networking layer, you must provide implementations for a set of functions. This tutorial assumes support for UDP connections, and therefore uses the anj_udp_* naming convention.

Note

Currently Anjay Lite supports only UDP binding. We may add support for other bindings in the future.

If POSIX socket API is not available:

  • Use ANJ_WITH_SOCKET_POSIX_COMPAT=OFF when running CMake on Anjay Lite,

  • Implement the following mandatory functions:

Function

Purpose

anj_udp_create_ctx

Create and initialize a network context.

anj_udp_connect

Connect the socket to a server address.

anj_udp_send

Send data through the socket.

anj_udp_recv

Receive data from the socket.

anj_udp_shutdown

Shut down socket communication.

anj_udp_close

Close the socket.

anj_udp_cleanup_ctx

Clean up and free the network context.

anj_udp_get_inner_mtu

Returns the maximum size of a buffer that can be passed to anj_udp_send().

anj_udp_get_state

Return the current state of the socket.

7.2.3. Optional functions

Implementing the following additional functions enables enhanced features:

Function

Purpose

anj_udp_reuse_last_port

Required to support Queue Mode. If Queue Mode is not needed, implement a stub that returns ANJ_NET_ENOTSUP.

anj_udp_get_bytes_received

Not used directly by Anjay Lite. Useful for gathering statistics on incoming traffic.

anj_udp_get_bytes_sent

Not used directly by Anjay Lite. Useful for gathering statistics on outgoing traffic.

anj_udp_get_system_socket

Not used by Anjay Lite. Can be used to suspend the device until a packet is received.

Note

For signatures and detailed description of listed functions, see include_public/anj/compat/net/anj_net_api.h