1c87c5fbaSopenharmony_ci/*
2c87c5fbaSopenharmony_ci * coap_layers_internal.h -- Internal layer functions for libcoap
3c87c5fbaSopenharmony_ci *
4c87c5fbaSopenharmony_ci * Copyright (C) 2023 Jon Shallow <supjps-libcoap@jpshallow.com>
5c87c5fbaSopenharmony_ci *
6c87c5fbaSopenharmony_ci * SPDX-License-Identifier: BSD-2-Clause
7c87c5fbaSopenharmony_ci *
8c87c5fbaSopenharmony_ci * This file is part of the CoAP library libcoap. Please see README for terms
9c87c5fbaSopenharmony_ci * of use.
10c87c5fbaSopenharmony_ci */
11c87c5fbaSopenharmony_ci
12c87c5fbaSopenharmony_ci/**
13c87c5fbaSopenharmony_ci * @file coap_layers_internal.h
14c87c5fbaSopenharmony_ci * @brief Internal layer I/O functions
15c87c5fbaSopenharmony_ci */
16c87c5fbaSopenharmony_ci
17c87c5fbaSopenharmony_ci#ifndef COAP_LAYERS_INTERNAL_H_
18c87c5fbaSopenharmony_ci#define COAP_LAYERS_INTERNAL_H_
19c87c5fbaSopenharmony_ci
20c87c5fbaSopenharmony_ci#include "coap_internal.h"
21c87c5fbaSopenharmony_ci
22c87c5fbaSopenharmony_citypedef enum {
23c87c5fbaSopenharmony_ci  COAP_LAYER_SESSION,
24c87c5fbaSopenharmony_ci  COAP_LAYER_WS,
25c87c5fbaSopenharmony_ci  COAP_LAYER_TLS,
26c87c5fbaSopenharmony_ci  COAP_LAYER_LAST
27c87c5fbaSopenharmony_ci} coap_layer_t;
28c87c5fbaSopenharmony_ci
29c87c5fbaSopenharmony_ci/**
30c87c5fbaSopenharmony_ci * Function read interface for layer data receiving.
31c87c5fbaSopenharmony_ci *
32c87c5fbaSopenharmony_ci * If a called lower layer returned value is 0 or less, this must get passed
33c87c5fbaSopenharmony_ci * back to the caller.
34c87c5fbaSopenharmony_ci *
35c87c5fbaSopenharmony_ci * If the layer function consumes all the data (i.e. to handle the protocol
36c87c5fbaSopenharmony_ci * layer requirements), then the function must return 0 to indicate no data.
37c87c5fbaSopenharmony_ci *
38c87c5fbaSopenharmony_ci * Otherwise data must get updated (limited by datalen) and the number of bytes
39c87c5fbaSopenharmony_ci * available returned.
40c87c5fbaSopenharmony_ci *
41c87c5fbaSopenharmony_ci * Note: If the number of returned bytes is less that read in, then
42c87c5fbaSopenharmony_ci * COAP_SOCKET_CAN_READ must be dropped from session->sock.flags.
43c87c5fbaSopenharmony_ci *
44c87c5fbaSopenharmony_ci * @param session  Session to receive data on.
45c87c5fbaSopenharmony_ci * @param data     The data to receive.
46c87c5fbaSopenharmony_ci * @param datalen  The maximum length of @p data.
47c87c5fbaSopenharmony_ci *
48c87c5fbaSopenharmony_ci * @return         >=0 Number of bytes read.
49c87c5fbaSopenharmony_ci *                 -1  Error error in errno).
50c87c5fbaSopenharmony_ci *                 -2  Recieved ICMP unreachable.
51c87c5fbaSopenharmony_ci */
52c87c5fbaSopenharmony_citypedef ssize_t (*coap_layer_read_t)(coap_session_t *session,
53c87c5fbaSopenharmony_ci                                     uint8_t *data, size_t datalen);
54c87c5fbaSopenharmony_ci
55c87c5fbaSopenharmony_ci/**
56c87c5fbaSopenharmony_ci * Function write interface for layer data sending.
57c87c5fbaSopenharmony_ci *
58c87c5fbaSopenharmony_ci * If a called lower layer returned value is 0 or less, this must get passed
59c87c5fbaSopenharmony_ci * back to the caller.
60c87c5fbaSopenharmony_ci *
61c87c5fbaSopenharmony_ci * If the layer function cannot transmit any data (congestion control etc.),
62c87c5fbaSopenharmony_ci * then the function must return 0 to indicate no data sent.
63c87c5fbaSopenharmony_ci *
64c87c5fbaSopenharmony_ci * It is possible that not all the data is sent (congestion control etc.),
65c87c5fbaSopenharmony_ci * and bytes written is less that datalen.
66c87c5fbaSopenharmony_ci *
67c87c5fbaSopenharmony_ci * Note: If the number of returned bytes is less that to be written,
68c87c5fbaSopenharmony_ci * COAP_SOCKET_WANT_WRITE must be added to session->sock.flags.
69c87c5fbaSopenharmony_ci *
70c87c5fbaSopenharmony_ci * @param session  Session to receive data on.
71c87c5fbaSopenharmony_ci * @param data     The data to write out.
72c87c5fbaSopenharmony_ci * @param datalen  The maximum length of @p data.
73c87c5fbaSopenharmony_ci *
74c87c5fbaSopenharmony_ci * @return         >=0 Number of bytes written.
75c87c5fbaSopenharmony_ci *                 -1  Error error in errno).
76c87c5fbaSopenharmony_ci */
77c87c5fbaSopenharmony_citypedef ssize_t (*coap_layer_write_t)(coap_session_t *session,
78c87c5fbaSopenharmony_ci                                      const uint8_t *data, size_t datalen);
79c87c5fbaSopenharmony_ci/**
80c87c5fbaSopenharmony_ci * Function establish interface for layer establish handling.
81c87c5fbaSopenharmony_ci *
82c87c5fbaSopenharmony_ci * If this layer is properly established on invocation, then the next layer
83c87c5fbaSopenharmony_ci * must get called by calling
84c87c5fbaSopenharmony_ci *   session->lfunc[_this_layer_].l_establish(session)
85c87c5fbaSopenharmony_ci * (or done at any point when layer is established).
86c87c5fbaSopenharmony_ci * If the establishment of a layer fails, then
87c87c5fbaSopenharmony_ci *   coap_session_disconnected(session, COAP_NACK_xxx_LAYER_FAILED) must be
88c87c5fbaSopenharmony_ci *   called.
89c87c5fbaSopenharmony_ci *
90c87c5fbaSopenharmony_ci * @param session Session being established
91c87c5fbaSopenharmony_ci */
92c87c5fbaSopenharmony_citypedef void (*coap_layer_establish_t)(coap_session_t *session);
93c87c5fbaSopenharmony_ci
94c87c5fbaSopenharmony_ci/**
95c87c5fbaSopenharmony_ci * Function close interface for layer closing.
96c87c5fbaSopenharmony_ci *
97c87c5fbaSopenharmony_ci * When this layer is properly closed, then the next layer
98c87c5fbaSopenharmony_ci * must get called by calling
99c87c5fbaSopenharmony_ci *   session->lfunc[_this_layer_].l_close(session)
100c87c5fbaSopenharmony_ci * (or done at any point when layer is closed).
101c87c5fbaSopenharmony_ci *
102c87c5fbaSopenharmony_ci * @param session Session being closed.
103c87c5fbaSopenharmony_ci */
104c87c5fbaSopenharmony_citypedef void (*coap_layer_close_t)(coap_session_t *session);
105c87c5fbaSopenharmony_ci
106c87c5fbaSopenharmony_citypedef struct {
107c87c5fbaSopenharmony_ci  coap_layer_read_t l_read;   /* Get data from next layer (TCP) */
108c87c5fbaSopenharmony_ci  coap_layer_write_t l_write; /* Output data to next layer */
109c87c5fbaSopenharmony_ci  coap_layer_establish_t l_establish; /* Layer establish */
110c87c5fbaSopenharmony_ci  coap_layer_close_t l_close; /* Connection close */
111c87c5fbaSopenharmony_ci} coap_layer_func_t;
112c87c5fbaSopenharmony_ci
113c87c5fbaSopenharmony_ciextern coap_layer_func_t coap_layers_coap[COAP_PROTO_LAST][COAP_LAYER_LAST];
114c87c5fbaSopenharmony_ci
115c87c5fbaSopenharmony_ci#endif /* COAP_LAYERS_INTERNAL_H_ */
116