summaryrefslogtreecommitdiff
path: root/lib/avtp_pipeline/intf_echo/openavb_intf_echo.c
blob: 9385bb9f664c22a786f0525b64ca1e40c7bc122c (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
/*************************************************************************************************************
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.
*************************************************************************************************************/

/*
* MODULE SUMMARY : Echo interface module.
* 
* This interface module as a talker to push a configured string out. As
*  a listener it will echo the data to stdout. This is strickly for
*  testing purposes and is generally intented to work with the Pipe
*  mapping module.
*/

#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"

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

typedef struct {
	/////////////
	// Config data
	/////////////
	// intf_nv_echo_string: sets a string that will be sent by the talker.
	char *pEchoString;
	
	// String repeat
	U32 echoStringRepeat;

	// calculated value from pEchoString.
	U32 echoStringLen;

	// Append an incrementing number to the string.
	bool increment;

	// Output locally at the talker.
	bool txLocalEcho;

	// No new line.
	bool noNewline;

	// Ignore timestamp at listener.
	bool ignoreTimestamp;

	/////////////
	// Variable data
	/////////////
	// When increment is enable this is the counter
	U32 Counter;

	bool fixedTimestampEnabled;
	U32 tsIncrement;
	avtp_time_t walltime;
} pvt_data_t;


// Each configuration name value pair for this mapping will result in this callback being called.
void openavbIntfEchoCfgCB(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_echo_string") == 0) {
			if (pPvtData->pEchoString)
				free(pPvtData->pEchoString);
			pPvtData->pEchoString = strdup(value);
			if (pPvtData->pEchoString) {
				pPvtData->echoStringLen = strlen(pPvtData->pEchoString);
			}
		}
		else if (strcmp(name, "intf_nv_echo_string_repeat") == 0) {
			pPvtData->echoStringRepeat = strtol(value, &pEnd, 10);

			// Repeat the string if needed
			if (pPvtData->echoStringRepeat > 1 && pPvtData->pEchoString) {
				char *pEchoStringRepeat = calloc(1, (pPvtData->echoStringLen * pPvtData->echoStringRepeat) + 1); 

				int i1;
				for (i1 = 0; i1 < pPvtData->echoStringRepeat; i1++) {
					strcat(pEchoStringRepeat, pPvtData->pEchoString);
				}

				free(pPvtData->pEchoString);
				pPvtData->pEchoString = pEchoStringRepeat;
				pPvtData->echoStringLen = strlen(pPvtData->pEchoString);
			}
		}
		else if (strcmp(name, "intf_nv_echo_increment") == 0) {
			tmp = strtol(value, &pEnd, 10);
			if (*pEnd == '\0' && tmp == 1) {
				pPvtData->increment = (tmp == 1);
			}
		}
		else if (strcmp(name, "intf_nv_tx_local_echo") == 0) {
			tmp = strtol(value, &pEnd, 10);
			if (*pEnd == '\0' && tmp == 1) {
				pPvtData->txLocalEcho = (tmp == 1);
			}
		}
		else if (strcmp(name, "intf_nv_echo_no_newline") == 0) {
			tmp = strtol(value, &pEnd, 10);
			if (*pEnd == '\0' && tmp == 1) {
				pPvtData->noNewline = (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 openavbIntfEchoGenInitCB(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 openavbIntfEchoTxInitCB(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;
		}

		pPvtData->Counter = 0;

		if (pPvtData->fixedTimestampEnabled)
			openavbAvtpTimeSetToWallTime(&pPvtData->walltime);
	}

	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

// This callback will be called for each AVB transmit interval. Commonly this will be
// 4000 or 8000 times  per second.
bool openavbIntfEchoTxCB(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 = openavbMediaQHeadLock(pMediaQ);
		if (pMediaQItem) {
			if (pMediaQItem->itemSize >= (pPvtData->increment ? pPvtData->echoStringLen + 16 : pPvtData->echoStringLen)) {
				if (pPvtData->increment) {
					int len = sprintf(pMediaQItem->pPubData, "%s %u", pPvtData->pEchoString, pPvtData->Counter++);
					pMediaQItem->dataLen = len;
				}
				else {
					memcpy(pMediaQItem->pPubData, pPvtData->pEchoString, pPvtData->echoStringLen);
					pMediaQItem->dataLen = pPvtData->echoStringLen;
				}
			}
			else {
				memcpy(pMediaQItem->pPubData, pPvtData->pEchoString, pMediaQItem->itemSize);
				pMediaQItem->dataLen = pMediaQItem->itemSize;
			}

			if (pPvtData->txLocalEcho) {
				if (pPvtData->noNewline)
					printf("%s ", (char *)pMediaQItem->pPubData); 
				else
					printf("%s\n\r", (char *)pMediaQItem->pPubData); 
			}

			if (pPvtData->fixedTimestampEnabled) {
				openavbAvtpTimeAddUSec(&pPvtData->walltime, pPvtData->tsIncrement);
				*pMediaQItem->pAvtpTime = pPvtData->walltime;
			} else {
				openavbAvtpTimeSetToWallTime(pMediaQItem->pAvtpTime);
			}
			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;
}

// A call to this callback indicates that this interface module will be
// a listener. Any listener initialization can be done in this function.
void openavbIntfEchoRxInitCB(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;
		}

		pPvtData->Counter = 0;
	}

	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

// This callback is called when acting as a listener.
bool openavbIntfEchoRxCB(media_q_t *pMediaQ) 
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF_DETAIL);
	if (pMediaQ) {
		bool moreItems = TRUE;
		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;
		while (moreItems) {
			pMediaQItem = openavbMediaQTailLock(pMediaQ, pPvtData->ignoreTimestamp);
			if (pMediaQItem) {
				if (pMediaQItem->dataLen) {
					if (pMediaQItem->itemSize > pMediaQItem->dataLen) {
						*((char*)pMediaQItem->pPubData + pMediaQItem->dataLen) = 0; //End string with 0
					}
					if (pPvtData->noNewline) {
						printf("%s ", (char *)pMediaQItem->pPubData); 
					}
					else {
						if (pPvtData->increment) {
							printf("%s : %u\n\r", (char *)pMediaQItem->pPubData, pPvtData->Counter++); 
						}
						else {
							printf("%s\n\r", (char *)pMediaQItem->pPubData); 
						}
					}
				}
				openavbMediaQTailPull(pMediaQ);
			}
			else {
				moreItems = FALSE;
			}
		}
	}
	AVB_TRACE_EXIT(AVB_TRACE_INTF_DETAIL);
	return FALSE;
}

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

// General shutdown callback regardless if a talker or listener. Called once during openavbTLClose()
void openavbIntfEchoGenEndCB(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 (pPvtData->pEchoString) {
			free(pPvtData->pEchoString);
			pPvtData->pEchoString = NULL;
		}
	}
	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

void openavbIntfEchoEnableFixedTimestamp(media_q_t *pMediaQ, bool enabled, U32 transmitInterval, U32 batchFactor)
{
	AVB_TRACE_ENTRY(AVB_TRACE_INTF);
	if (pMediaQ && pMediaQ->pPvtIntfInfo) {
		pvt_data_t *pPvtData = (pvt_data_t *)pMediaQ->pPvtIntfInfo;

		pPvtData->fixedTimestampEnabled = enabled;
		if (pPvtData->fixedTimestampEnabled) {
				pPvtData->tsIncrement = NANOSECONDS_PER_SECOND/transmitInterval;
		}

		if (batchFactor != 1) {
			AVB_LOGF_WARNING("batchFactor of %d ignored (must be 1)", batchFactor);
		}
	}

	AVB_TRACE_EXIT(AVB_TRACE_INTF);
}

// Main initialization entry point into the interface module
extern bool DLL_EXPORT openavbIntfEchoInitialize(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 = openavbIntfEchoCfgCB;
		pIntfCB->intf_gen_init_cb = openavbIntfEchoGenInitCB;
		pIntfCB->intf_tx_init_cb = openavbIntfEchoTxInitCB;
		pIntfCB->intf_tx_cb = openavbIntfEchoTxCB;
		pIntfCB->intf_rx_init_cb = openavbIntfEchoRxInitCB;
		pIntfCB->intf_rx_cb = openavbIntfEchoRxCB;
		pIntfCB->intf_end_cb = openavbIntfEchoEndCB;
		pIntfCB->intf_gen_end_cb = openavbIntfEchoGenEndCB;
		pIntfCB->intf_enable_fixed_timestamp = openavbIntfEchoEnableFixedTimestamp;

		pPvtData->pEchoString = NULL;
		pPvtData->echoStringRepeat = 1; 
		pPvtData->echoStringLen = 0;
		pPvtData->increment = FALSE;
		pPvtData->txLocalEcho = FALSE;
		pPvtData->noNewline = FALSE;
		pPvtData->ignoreTimestamp = FALSE;
		pPvtData->fixedTimestampEnabled = FALSE;
	}

	AVB_TRACE_EXIT(AVB_TRACE_INTF);
	return TRUE;
}