summaryrefslogtreecommitdiff
path: root/lib/avtp_pipeline/intf_ctrl/openavb_intf_ctrl.c
blob: 3a7ddec0d0b18d47d4d93e18182f454b053cef65 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*************************************************************************************************************
Copyright (c) 2012-2015, Symphony Teleca Corporation, a Harman International Industries, Incorporated company
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.
*************************************************************************************************************/

/*
* MODULE SUMMARY : Control interface module.
* 
* This interface module sends and receives control messages. There are
* two modes this interface module can be configured for normal mode and mux
* mode. In normal mode the control messages are exchanged with the host
* application. In mux mode the control messages will be multiplexed from
* multiple control streams into one out-going talker control stream. This
* is part of the dispatch model for configuring the control message system
* in the OPENAVB AVB stack.
*/

// WORK IN PROGRESS

#include <stdlib.h>
#include <string.h>
#include "openavb_types_pub.h"
#include "openavb_trace_pub.h"
#include "openavb_mediaq_pub.h"
#include "openavb_intf_pub.h"
#include "openavb_intf_ctrl_pub.h"

#define	AVB_LOG_COMPONENT	"Control Interface"
#include "openavb_log_pub.h" 

// Forward Declarations
bool openavbIntfCtrlSendControl(void *pIntfHandle, U8 *pData, U32 dataLength, U32 usecDelay);


typedef struct {
	/////////////
	// Config data
	/////////////
	// Multiplexing mode. Multiple lister streams flowing into one talker stream.
	bool muxMode;

	// Ignore timestamp at listener.
	bool ignoreTimestamp;

	/////////////
	// Variable data
	/////////////
	// Callback into the hosting application when control messages are received.
	openavb_intf_ctrl_receive_cb_t openavbIntfCtrlReceiveControlFn;

	// Handle to pass back to the hosting application.
	void *pTLHandle;

	// Flag indication that the mux talker is owned by this interface instance.
	bool bOwnsCtrlMuxMediaQ;

} pvt_data_t;

// This is the talker mux media queue. The media queue will be set as thread safe mode and loccking for head and tail operations
// will be handled within the MediaQ. This interface module is designed such that there can only be 1 ctrl mux talker in a process.
media_q_t *pCtrlMuxMediaQ = NULL;

openavb_intf_host_cb_list_t openavbIntfHostCBList;

// Each configuration name value pair for this mapping will result in this callback being called.
void openavbIntfCtrlCfgCB(media_q_t *pMediaQ, const char *name, const char *value) 
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);
	if (pMediaQ) {
		char *pEnd;
		long tmp;

		pvt_data_t *pPvtData = pMediaQ->pPvtIntfInfo;
		if (!pPvtData) {
			AVB_LOG_ERROR("Private interface module data not allocated.");
			return;
		}

		if (strcmp(name, "intf_nv_mux_mode") == 0) {
			tmp = strtol(value, &pEnd, 10);
			if (*pEnd == '\0' && tmp == 1) {
				pPvtData->muxMode = (tmp == 1);
			}
		}
		else if (strcmp(name, "intf_nv_ignore_timestamp") == 0) {
			tmp = strtol(value, &pEnd, 10);
			if (*pEnd == '\0' && tmp == 1) {
				pPvtData->ignoreTimestamp = (tmp == 1);
			}
		}
	}

	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

void openavbIntfCtrlGenInitCB(media_q_t *pMediaQ) 
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);
	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

// A call to this callback indicates that this interface module will be
// a talker. Any talker initialization can be done in this function.
void openavbIntfCtrlTxInitCB(media_q_t *pMediaQ) 
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);

	if (pMediaQ) {
		pvt_data_t *pPvtData = pMediaQ->pPvtIntfInfo;
		if (!pPvtData) {
			AVB_LOG_ERROR("Private interface module data not allocated.");
			return;
		}

		// Talker control interfaces will always enable MediaQ thread safety. 
		openavbMediaQThreadSafeOn(pMediaQ);

		if (pPvtData->muxMode) {
			if (!pPvtData->bOwnsCtrlMuxMediaQ) {
				pCtrlMuxMediaQ = pMediaQ;
				pPvtData->bOwnsCtrlMuxMediaQ = TRUE;
			}
			else {
				AVB_LOG_ERROR("Only one MUX talker is allowed per process.");
			}
		}
	}

	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

// Control transmission is handled in openavbIntfCtrlSendControl()
bool openavbIntfCtrlTxCB(media_q_t *pMediaQ)
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF_DETAIL);
	AVB_TRACE_EXIT(AVB_TRACE_INTF_DETAIL);
	return TRUE;
}

// A call to this callback indicates that this interface module will be
// a listener. Any listener initialization can be done in this function.
void openavbIntfCtrlRxInitCB(media_q_t *pMediaQ) 
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);
	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

// This callback is called when acting as a listener.
bool openavbIntfCtrlRxCB(media_q_t *pMediaQ) 
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF_DETAIL);
	if (pMediaQ) {

		pvt_data_t *pPvtData = pMediaQ->pPvtIntfInfo;
		if (!pPvtData) {
			AVB_LOG_ERROR("Private interface module data not allocated.");
			return FALSE;
		}

		media_q_item_t *pMediaQItem = NULL;

		pMediaQItem = openavbMediaQTailLock(pMediaQ, pPvtData->ignoreTimestamp);
		if (pMediaQItem) {
			if (pMediaQItem->dataLen) {
				if (pPvtData->muxMode) {
					if (pCtrlMuxMediaQ) {
						if (!openavbIntfCtrlSendControl(pCtrlMuxMediaQ, pMediaQItem->pPubData, pMediaQItem->dataLen, 0)) {
							openavbMediaQTailPull(pMediaQ);
							AVB_LOG_ERROR("Failed to retransmit the control message.");
							return FALSE;
						}
						else {
							openavbMediaQTailPull(pMediaQ);
							return TRUE;
						}
					}
					else {
						openavbMediaQTailPull(pMediaQ);
						AVB_LOG_ERROR("Control mux talker not found.");
						return FALSE;
					}
				}
				else {
					if (pPvtData->openavbIntfCtrlReceiveControlFn) {
						pPvtData->openavbIntfCtrlReceiveControlFn(pPvtData->pTLHandle, pMediaQItem->pPubData, pMediaQItem->dataLen);
						openavbMediaQTailPull(pMediaQ);
						return TRUE;
					}
					else {
						openavbMediaQTailPull(pMediaQ);
						AVB_LOG_ERROR("Control receive callback not defined.");
						return FALSE;
					}

				}
			}
			openavbMediaQTailPull(pMediaQ);
		}
	}
	AVB_TRACE_EXIT(AVB_TRACE_INTF_DETAIL);
	return FALSE;
}

// This callback will be called when the stream is closing. 
void openavbIntfCtrlEndCB(media_q_t *pMediaQ) 
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);

	if (pMediaQ) {
		pvt_data_t *pPvtData = pMediaQ->pPvtIntfInfo;
		if (!pPvtData) {
			AVB_LOG_ERROR("Private interface module data not allocated.");
			return;
		}

		// If this instance of interface owns the mux talker remove it now
		if (pPvtData->bOwnsCtrlMuxMediaQ) {
			pCtrlMuxMediaQ = NULL;
			pPvtData->bOwnsCtrlMuxMediaQ = FALSE;
		}
	}

	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

// General shutdown callback regardless if a talker or listener. Called once during openavbTLClose()
void openavbIntfCtrlGenEndCB(media_q_t *pMediaQ) 
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);
	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

// Host side callback 
extern bool DLL_EXPORT openavbIntfCtrlRegisterReceiveControlCB(void *pIntfHandle, void *pTLHandle, openavb_intf_ctrl_receive_cb_t openavbIntfCtrlReceiveControlFn)
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);

	media_q_t *pMediaQ = pIntfHandle;

	if (pMediaQ) {
		pvt_data_t *pPvtData = pMediaQ->pPvtIntfInfo;
		if (!pPvtData) {
			AVB_LOG_ERROR("Private interface module data not allocated.");
			return FALSE;
		}

		pPvtData->openavbIntfCtrlReceiveControlFn = openavbIntfCtrlReceiveControlFn;
		pPvtData->pTLHandle = pTLHandle;

		AVB_TRACE_EXIT(AVB_TRACE_INTF);
		return TRUE;
	}
	AVB_TRACE_EXIT(AVB_TRACE_INTF);
	return FALSE;
}

extern bool DLL_EXPORT openavbIntfCtrlUnregisterReceiveControlCB(void *pIntfHandle)
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);

	media_q_t *pMediaQ = pIntfHandle;

	if (pMediaQ) {
		pvt_data_t *pPvtData = pMediaQ->pPvtIntfInfo;
		if (!pPvtData) {
			AVB_LOG_ERROR("Private interface module data not allocated.");
			return FALSE;
		}

		pPvtData->openavbIntfCtrlReceiveControlFn = NULL;
		pPvtData->pTLHandle = NULL;

		AVB_TRACE_EXIT(AVB_TRACE_INTF);
		return TRUE;
	}
	AVB_TRACE_EXIT(AVB_TRACE_INTF);
	return FALSE;
}

// Host side callback 
extern bool DLL_EXPORT openavbIntfCtrlSendControl(void *pIntfHandle, U8 *pData, U32 dataLength, U32 usecDelay)
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF_DETAIL);

	media_q_t *pMediaQ = pIntfHandle;

	if (pMediaQ) {
		pvt_data_t *pPvtData = pMediaQ->pPvtIntfInfo;
		if (!pPvtData) {
			AVB_LOG_ERROR("Private interface module data not allocated.");
			return FALSE;
		}

		media_q_item_t *pMediaQItem = openavbMediaQHeadLock(pMediaQ);
		if (pMediaQItem) {
			if (pMediaQItem->itemSize >= dataLength) {
				memcpy(pMediaQItem->pPubData, pData, dataLength);
				pMediaQItem->dataLen = dataLength;
			}
			else {
				AVB_LOG_ERROR("Control data too large for media queue.");
				openavbMediaQHeadUnlock(pMediaQ);
				AVB_TRACE_EXIT(AVB_TRACE_INTF_DETAIL);
				return FALSE;
			}

			openavbAvtpTimeSetToWallTime(pMediaQItem->pAvtpTime);
			openavbAvtpTimeAddUSec(pMediaQItem->pAvtpTime, usecDelay);
			openavbMediaQHeadPush(pMediaQ);
			AVB_TRACE_EXIT(AVB_TRACE_INTF_DETAIL);
			return TRUE;
		}
		else {
			AVB_TRACE_EXIT(AVB_TRACE_INTF_DETAIL);
			return FALSE;	// Media queue full
		}
	}
	AVB_TRACE_EXIT(AVB_TRACE_INTF_DETAIL);
	return FALSE;
}

// Main initialization entry point into the interface module
extern bool DLL_EXPORT openavbIntfCtrlInitialize(media_q_t *pMediaQ, openavb_intf_cb_t *pIntfCB)
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);

	if (pMediaQ) {
		pMediaQ->pPvtIntfInfo = calloc(1, sizeof(pvt_data_t));		// Memory freed by the media queue when the media queue is destroyed.

		if (!pMediaQ->pPvtIntfInfo) {
			AVB_LOG_ERROR("Unable to allocate memory for AVTP interface module.");
			return FALSE;
		}

		pvt_data_t *pPvtData = pMediaQ->pPvtIntfInfo;

		pIntfCB->intf_cfg_cb = openavbIntfCtrlCfgCB;
		pIntfCB->intf_gen_init_cb = openavbIntfCtrlGenInitCB;
		pIntfCB->intf_tx_init_cb = openavbIntfCtrlTxInitCB;
		pIntfCB->intf_tx_cb = openavbIntfCtrlTxCB;
		pIntfCB->intf_rx_init_cb = openavbIntfCtrlRxInitCB;
		pIntfCB->intf_rx_cb = openavbIntfCtrlRxCB;
		pIntfCB->intf_end_cb = openavbIntfCtrlEndCB;
		pIntfCB->intf_gen_end_cb = openavbIntfCtrlGenEndCB;

		openavbIntfHostCBList.register_receive_control_cb = openavbIntfCtrlRegisterReceiveControlCB;
		openavbIntfHostCBList.unregister_receive_control_cb = openavbIntfCtrlUnregisterReceiveControlCB;
		openavbIntfHostCBList.send_control_cb = openavbIntfCtrlSendControl;
		pIntfCB->intf_host_cb_list = (void *)&openavbIntfHostCBList;

		pPvtData->muxMode = FALSE;
		pPvtData->ignoreTimestamp = FALSE;
		pPvtData->openavbIntfCtrlReceiveControlFn = NULL;
		pPvtData->pTLHandle = NULL;
	}

	AVB_TRACE_EXIT(AVB_TRACE_INTF);
	return TRUE;
}