summaryrefslogtreecommitdiff
path: root/lib/avtp_pipeline/avtp/openavb_avtp.h
blob: 71139271dcb2c490372fa84300817cfff7b8336f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*************************************************************************************************************
Copyright (c) 2012-2015, Symphony Teleca Corporation, a Harman International Industries, Incorporated company
Copyright (c) 2016-2017, Harman International Industries, Incorporated
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS LISTED "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS LISTED BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Attributions: The inih library portion of the source code is licensed from
Brush Technology and Ben Hoyt - Copyright (c) 2009, Brush Technology and Copyright (c) 2009, Ben Hoyt.
Complete license and copyright information can be found at
https://github.com/benhoyt/inih/commit/74d2ca064fb293bc60a77b0bd068075b293cf175.
*************************************************************************************************************/

/*
* HEADER SUMMARY : Declare the main functions for AVTP.  Includes
* functions to create/destroy and AVTP stream, and to send or receive
* data from that AVTP stream.
*/

#ifndef AVB_AVTP_H
#define AVB_AVTP_H 1

#include "openavb_platform.h"
#include "openavb_intf_pub.h"
#include "openavb_map_pub.h"
#include "openavb_tl_pub.h"
#include "openavb_rawsock.h"
#include "openavb_timestamp.h"

#define ETHERTYPE_AVTP 0x22F0
#define ETHERTYPE_8021Q 0x8100
#define ETHERNET_8021Q_OCTETS 4
#define TS_PACKET_LEN  188
#define SRC_PACKET_LEN 192
#define CIP_HEADER_LEN 8

// Length of Ethernet frame header, with and without 802.1Q tag
#define ETH_HDR_LEN			14
#define ETH_HDR_LEN_VLAN	18

// AVTP Headers
#define AVTP_COMMON_STREAM_DATA_HDR_LEN	24

//#define OPENAVB_AVTP_REPORT_RX_STATS 1
#define OPENAVB_AVTP_REPORT_INTERVAL 100

typedef struct {
	// These are significant only for RX data
	U32					timestamp;  // delivery timestamp
	bool				bComplete;	// not waiting for more data
#ifdef OPENAVB_AVTP_REPORT_RX_STATS
	U32					rxCnt, lateCnt, earlyCnt;
	U32					maxLate, maxEarly;
	struct timespec		lastTime;
#endif
} avtp_rx_info_t;

typedef struct {
	U8						*data;	// pointer to data
	avtp_rx_info_t			rx;		// re-assembly info
} avtp_info_t;

typedef struct {
	media_q_t 				mediaq;
} avtp_state_t;


/* Info associated with an AVTP stream (RX or TX).
 *
 * The void* handle that is returned to the client
 * really is a pointer to an avtp_stream_t.
 *
 * TODO: This passed around as void * handle can be typed since the avtp_stream_t is
 * now seen by the talker / listern module.
 *
 */
typedef struct
{
	// TX socket?
	bool tx;
	// Interface name for base interface
	char* ifname;
	/// Network interface name for interfaces to TX mirror the AVTP traffic to.
	char* (ifname_mirror[MAX_NUM_INTERFACE_MIRRORS]);
	// Number of rawsock buffers
	U16 nbuffers;
	// The rawsock library handle for the base interface.
	void *rawsock;
	// The rawsock library handle for interfaces to TX mirror the AVTP traffic to.
	void *(rawsock_mirror[MAX_NUM_INTERFACE_MIRRORS]);
	// The streamID - in network form
	U8 streamIDnet[8];
	// The destination address for stream
	struct ether_addr dest_addr;
	// The AVTP subtype; it determines the encapsulation
	U8 subtype;
	// Max Transit - value added to current time to get play time
	U64 max_transit_usec;
	// Max frame size
	U16 frameLen;
	// AVTP sequence number
	U8 avtp_sequence_num;
	// Paused state of the stream
	bool bPause;
	// Encapsulation-specific state information
	avtp_state_t state;
	// RX info for data sample currently being received
	avtp_info_t info;
	// Mapping callbacks
	openavb_map_cb_t *pMapCB;
	// Interface callbacks
	openavb_intf_cb_t *pIntfCB;
	// MediaQ
	media_q_t *pMediaQ;
	bool bRxSignalMode;

	// TX frame buffer
	U8* pBuf;
	// Ethernet header length
	U32 ethHdrLen;

	// Timestamp evaluation related
	openavb_timestamp_eval_t tsEval;

	// Stat related
	// RX frames lost
	int nLost;
	// Bytes sent or recieved
	U64 bytes;

} avtp_stream_t;


typedef void (*avtp_listener_callback_fn)(void *pv, avtp_info_t *data);

// tx/rx
openavbRC openavbAvtpTxInit(media_q_t *pMediaQ,
					openavb_map_cb_t *pMapCB,
					openavb_intf_cb_t *pIntfCB,
					char* (ifname[]),
					AVBStreamID_t *streamID,
					U8* destAddr,
					U32 max_transit_usec,
					U32 fwmark,
					U16 vlanID,
					U8  vlanPCP,
					U16 nbuffers,
					void **pStream_out);

openavbRC openavbAvtpTx(void *pv, bool bSend, bool txBlockingInIntf);

openavbRC openavbAvtpRxInit(media_q_t *pMediaQ,
					openavb_map_cb_t *pMapCB,
					openavb_intf_cb_t *pIntfCB,
					char* ifname,
					AVBStreamID_t *streamID,
					U8* destAddr,
					U16 nbuffers,
					bool rxSignalMode,
					void **pStream_out);

openavbRC openavbAvtpRx(void *handle);

void openavbAvtpConfigTimsstampEval(void *handle, U32 tsInterval, U32 reportInterval, bool smoothing, U32 tsMaxJitter, U32 tsMaxDrift);

void openavbAvtpPause(void *handle, bool bPause);

void openavbAvtpShutdownTalker(void *handle);
void openavbAvtpShutdownListener(void *handle);

int openavbAvtpTxBufferLevel(void *handle);

int openavbAvtpRxBufferLevel(void *handle);

int openavbAvtpLost(void *handle);

U64 openavbAvtpBytes(void *handle);

#endif //AVB_AVTP_H