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