1 /*
2  * SpanDSP - a series of DSP components for telephony
3  *
4  * udptl.c
5  *
6  * Written by Steve Underwood <steveu@coppice.org>
7  *
8  * Copyright (C) 2009 Steve Underwood
9  *
10  * All rights reserved.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2, as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25 
26 #if !defined(_SPANDSP_UDPTL_H_)
27 #define _SPANDSP_UDPTL_H_
28 
29 #define LOCAL_FAX_MAX_DATAGRAM      400
30 #define LOCAL_FAX_MAX_FEC_PACKETS   5
31 
32 #define UDPTL_BUF_MASK              15
33 
34 typedef int (*udptl_rx_packet_handler_t) (void *user_data, const uint8_t msg[], int len, int seq_no);
35 
36 typedef struct
37 {
38     int buf_len;
39     uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
40 } udptl_fec_tx_buffer_t;
41 
42 typedef struct
43 {
44     int buf_len;
45     uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
46     int fec_len[LOCAL_FAX_MAX_FEC_PACKETS];
47     uint8_t fec[LOCAL_FAX_MAX_FEC_PACKETS][LOCAL_FAX_MAX_DATAGRAM];
48     int fec_span;
49     int fec_entries;
50 } udptl_fec_rx_buffer_t;
51 
52 struct udptl_state_s
53 {
54     udptl_rx_packet_handler_t rx_packet_handler;
55     void *user_data;
56 
57     /*! This option indicates the error correction scheme used in transmitted UDPTL
58         packets. */
59     int error_correction_scheme;
60 
61     /*! This option indicates the number of error correction entries transmitted in
62         UDPTL packets. */
63     int error_correction_entries;
64 
65     /*! This option indicates the span of the error correction entries in transmitted
66         UDPTL packets (FEC only). */
67     int error_correction_span;
68 
69     /*! This option indicates the maximum size of a datagram that can be accepted by
70         the remote device. */
71     int far_max_datagram_size;
72 
73     /*! This option indicates the maximum size of a datagram that we are prepared to
74         accept. */
75     int local_max_datagram_size;
76 
77     int verbose;
78 
79     int tx_seq_no;
80     int rx_seq_no;
81     int rx_expected_seq_no;
82 
83     udptl_fec_tx_buffer_t tx[UDPTL_BUF_MASK + 1];
84     udptl_fec_rx_buffer_t rx[UDPTL_BUF_MASK + 1];
85 };
86 
87 enum
88 {
89     UDPTL_ERROR_CORRECTION_NONE,
90     UDPTL_ERROR_CORRECTION_FEC,
91     UDPTL_ERROR_CORRECTION_REDUNDANCY
92 };
93 
94 typedef struct udptl_state_s udptl_state_t;
95 
96 #if defined(__cplusplus)
97 extern "C" {
98 #endif
99 
100 /*! \brief Process an arriving UDPTL packet.
101     \param s The UDPTL context.
102     \param buf The UDPTL packet buffer.
103     \param len The length of the packet.
104     \return 0 for OK. */
105 int udptl_rx_packet(udptl_state_t *s, const uint8_t buf[], int len);
106 
107 /*! \brief Construct a UDPTL packet, ready for transmission.
108     \param s The UDPTL context.
109     \param buf The UDPTL packet buffer.
110     \param msg The primary packet.
111     \param len The length of the primary packet.
112     \return The length of the constructed UDPTL packet. */
113 int udptl_build_packet(udptl_state_t *s, uint8_t buf[], const uint8_t msg[], int msg_len);
114 
115 /*! \brief Change the error correction settings of a UDPTL context.
116     \param s The UDPTL context.
117     \param ec_scheme One of the optional error correction schemes.
118     \param span The packet span over which error correction should be applied.
119     \param entries The number of error correction entries to include in packets.
120     \return 0 for OK. */
121 int udptl_set_error_correction(udptl_state_t *s, int ec_scheme, int span, int entries);
122 
123 /*! \brief Check the error correction settings of a UDPTL context.
124     \param s The UDPTL context.
125     \param ec_scheme One of the optional error correction schemes.
126     \param span The packet span over which error correction is being applied.
127     \param entries The number of error correction being included in packets.
128     \return 0 for OK. */
129 int udptl_get_error_correction(udptl_state_t *s, int *ec_scheme, int *span, int *entries);
130 
131 int udptl_set_local_max_datagram(udptl_state_t *s, int max_datagram);
132 
133 int udptl_get_local_max_datagram(udptl_state_t *s);
134 
135 int udptl_set_far_max_datagram(udptl_state_t *s, int max_datagram);
136 
137 int udptl_get_far_max_datagram(udptl_state_t *s);
138 
139 /*! \brief Initialise a UDPTL context.
140     \param s The UDPTL context.
141     \param ec_scheme One of the optional error correction schemes.
142     \param span The packet span over which error correction should be applied.
143     \param entries The number of error correction entries to include in packets.
144     \param rx_packet_handler The callback function, used to report arriving IFP packets.
145     \param user_data An opaque pointer supplied to rx_packet_handler.
146     \return A pointer to the UDPTL context, or NULL if there was a problem. */
147 udptl_state_t *udptl_init(udptl_state_t *s, int ec_scheme, int span, int entries, udptl_rx_packet_handler_t rx_packet_handler, void *user_data);
148 
149 /*! \brief Release a UDPTL context.
150     \param s The UDPTL context.
151     \return 0 for OK. */
152 int udptl_release(udptl_state_t *s);
153 
154 #if defined(__cplusplus)
155 }
156 #endif
157 #endif
158 /*- End of file ------------------------------------------------------------*/
159