summaryrefslogtreecommitdiff
path: root/lib/avtp_pipeline/aem/openavb_aem.c
blob: e9f0f4dee9db6b44342ccef6830131bd61f19eaa (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*************************************************************************************************************
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 : AEM - AVDECC Entity Model
 * MODULE SUMMARY : Implements the 1722.1 (AVDECC) Entity Model
 ******************************************************************
 */

#include "stdlib.h"

#define	AVB_LOG_COMPONENT	"AEM"
#include "openavb_log.h"

#include "openavb_avdecc.h"
#include "openavb_aem.h"

MUTEX_HANDLE(openavbAemMutex);
#define AEM_LOCK() { MUTEX_CREATE_ERR(); MUTEX_LOCK(openavbAemMutex); MUTEX_LOG_ERR("Mutex lock failure"); }
#define AEM_UNLOCK() { MUTEX_CREATE_ERR(); MUTEX_UNLOCK(openavbAemMutex); MUTEX_LOG_ERR("Mutex unlock failure"); }

static openavb_avdecc_entity_model_t *pAemEntityModel = NULL;

////////////////////////////////
// Private (internal) functions
////////////////////////////////
openavbRC openavbAemCreate(openavb_aem_descriptor_entity_t *pDescriptorEntity)
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);

	MUTEX_ATTR_HANDLE(mta);
	MUTEX_ATTR_INIT(mta);
	MUTEX_ATTR_SET_TYPE(mta, MUTEX_ATTR_TYPE_DEFAULT);
	MUTEX_ATTR_SET_NAME(mta, "openavbAemMutex");
	MUTEX_CREATE_ERR();
	MUTEX_CREATE(openavbAemMutex, mta);
	MUTEX_LOG_ERR("Could not create/initialize 'openavbAemMutex' mutex");

	if (!pDescriptorEntity) {
		AVB_RC_LOG_TRACE_RET(AVB_RC(OPENAVB_AVDECC_FAILURE | OPENAVBAVDECC_RC_ENTITY_MODEL_MISSING), AVB_TRACE_AEM);
	}

	if (!pAemEntityModel) {
		pAemEntityModel = calloc(1, sizeof(*pAemEntityModel));
	}

	if (!pAemEntityModel) {
		AVB_RC_LOG_TRACE_RET(AVB_RC(OPENAVB_AVDECC_FAILURE | OPENAVB_RC_OUT_OF_MEMORY), AVB_TRACE_AEM);
	}

	pAemEntityModel->pDescriptorEntity = pDescriptorEntity;
	pAemEntityModel->aemConfigurations = openavbArrayNewArray(sizeof(openavb_aem_configuration_t));
	memset(pAemEntityModel->aemNonTopLevelDescriptorsArray, 0, sizeof(pAemEntityModel->aemNonTopLevelDescriptorsArray));

	AVB_RC_TRACE_RET(OPENAVB_AVDECC_SUCCESS, AVB_TRACE_AEM);
}

openavbRC openavbAemDestroy()
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);
	// AVDECC_TODO all deallocations will occur here

	AVB_RC_TRACE_RET(OPENAVB_AVDECC_SUCCESS, AVB_TRACE_AEM);
}

bool openavbAemCheckModel(bool bLog)
{
	if (!pAemEntityModel || !pAemEntityModel->pDescriptorEntity) {
		if (bLog) {
			AVB_RC_LOG(OPENAVB_AVDECC_FAILURE | OPENAVBAVDECC_RC_ENTITY_MODEL_MISSING);
		}
		return FALSE;
	}
	return TRUE;
}

openavbRC openavbAemAddDescriptorToConfiguration(U16 descriptorType, U16 configIdx)
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);
	int i1;

	// Make sure Entity Model is created
	if (!openavbAemCheckModel(FALSE)) {
		AVB_RC_LOG_TRACE_RET(AVB_RC(OPENAVB_AVDECC_FAILURE | OPENAVBAVDECC_RC_ENTITY_MODEL_MISSING), AVB_TRACE_AEM);
	}

	if (configIdx == OPENAVB_AEM_DESCRIPTOR_INVALID) {
		AVB_RC_LOG_TRACE_RET(AVB_RC(OPENAVB_AVDECC_FAILURE | OPENAVBAVDECC_RC_INVALID_CONFIG_IDX), AVB_TRACE_AEM);
	}
	openavb_aem_configuration_t *pConfiguration = openavbArrayDataIdx(pAemEntityModel->aemConfigurations, configIdx);
	if (!pConfiguration) {
		AVB_RC_LOG_TRACE_RET(AVB_RC(OPENAVB_AVDECC_FAILURE | OPENAVBAVDECC_RC_INVALID_CONFIG_IDX), AVB_TRACE_AEM);
	}

	openavb_aem_descriptor_configuration_t *pConfig = pConfiguration->pDescriptorConfiguration;
	if (!pConfig) {
		AVB_RC_LOG_TRACE_RET(AVB_RC(OPENAVB_AVDECC_FAILURE | OPENAVBAVDECC_RC_INVALID_CONFIG_IDX), AVB_TRACE_AEM);
	}

	// Check if the new descriptor type is in the configuration array
	for (i1 = 0; i1 < pConfig->descriptor_counts_count; i1++) {
		if (pConfig->descriptor_counts[i1].descriptor_type == descriptorType) {
			// Found this descriptor type so just increase the count.
			pConfig->descriptor_counts[i1].count++; 
			AVB_RC_TRACE_RET(OPENAVB_AVDECC_SUCCESS, AVB_TRACE_AEM);
		}
	}

	// Update counts
	pConfig->descriptor_counts[pConfig->descriptor_counts_count].descriptor_type = descriptorType;
	pConfig->descriptor_counts[pConfig->descriptor_counts_count].count++; 
	pConfig->descriptor_counts_count++;

	AVB_RC_TRACE_RET(OPENAVB_AVDECC_SUCCESS, AVB_TRACE_AEM);
}

openavb_avdecc_entity_model_t *openavbAemGetModel()
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);
	AVB_TRACE_EXIT(AVB_TRACE_AEM);
	return pAemEntityModel;
}

openavb_array_t openavbAemGetDescriptorArray(U16 configIdx, U16 descriptorType)
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);

	openavb_array_t retDescriptors = NULL;

	// Make sure Entity Model is created
	if (!openavbAemCheckModel(TRUE)) {
		AVB_TRACE_EXIT(AVB_TRACE_AEM);
		return NULL;
	}

	if (descriptorType == OPENAVB_AEM_DESCRIPTOR_ENTITY) {
		// There is always only a single Entity Descriptor and therefore there isn't an array. NULL will be returned.
	}
	else if (descriptorType == OPENAVB_AEM_DESCRIPTOR_CONFIGURATION) {
		// The configuration descriptor is a special descriptor and not return by this function. NULL will be returned.
	}
	else {
		if (configIdx != OPENAVB_AEM_DESCRIPTOR_INVALID) {
			openavb_aem_configuration_t *pConfig = openavbArrayDataIdx(pAemEntityModel->aemConfigurations, configIdx);
			if (pConfig) {
				retDescriptors = pConfig->descriptorsArray[descriptorType];
			}
		}

		// Search the alternate location for non-top-level descriptors.
		if (!retDescriptors) {
			retDescriptors = pAemEntityModel->aemNonTopLevelDescriptorsArray[descriptorType];
		}
	}

	AVB_TRACE_EXIT(AVB_TRACE_AEM);
	return retDescriptors;
}

void *openavbAemFindDescriptor(U16 configIdx, U16 descriptorType, U16 descriptorIdx)
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);

	void *retDescriptor = NULL;

	// Make sure Entity Model is created
	if (!openavbAemCheckModel(TRUE)) {
		AVB_TRACE_EXIT(AVB_TRACE_AEM);
		return NULL;
	}

	if (descriptorType == OPENAVB_AEM_DESCRIPTOR_ENTITY) {
		if (pAemEntityModel->pDescriptorEntity &&
				pAemEntityModel->pDescriptorEntity->descriptor_index == descriptorIdx) {
			retDescriptor = pAemEntityModel->pDescriptorEntity;
		}
	}
	else if (descriptorType == OPENAVB_AEM_DESCRIPTOR_CONFIGURATION) {
		if (configIdx != OPENAVB_AEM_DESCRIPTOR_INVALID) {
			openavb_aem_configuration_t *pConfig = openavbArrayDataIdx(pAemEntityModel->aemConfigurations, configIdx);
			if (pConfig &&
					pConfig->pDescriptorConfiguration &&
					pConfig->pDescriptorConfiguration->descriptor_index == descriptorIdx) {
				retDescriptor = pConfig->pDescriptorConfiguration;
			}
		}
	}
	else {
		openavb_array_t descriptors = NULL;
		if (configIdx != OPENAVB_AEM_DESCRIPTOR_INVALID) {
			openavb_aem_configuration_t *pConfig = openavbArrayDataIdx(pAemEntityModel->aemConfigurations, configIdx);
			if (pConfig) {
				descriptors = pConfig->descriptorsArray[descriptorType];
				if (descriptors) {
					retDescriptor = openavbArrayDataIdx(descriptors, descriptorIdx);
				}
			}
		}

		// Search the alternate location for non-top-level descriptors.
		if (!retDescriptor) {
			descriptors = pAemEntityModel->aemNonTopLevelDescriptorsArray[descriptorType];
			if (descriptors) {
				retDescriptor = openavbArrayDataIdx(descriptors, descriptorIdx);
			}
		}
	}

	AVB_TRACE_EXIT(AVB_TRACE_AEM);
	return retDescriptor;
}

openavbRC openavbAemSerializeDescriptor(U16 configIdx, U16 descriptorType, U16 descriptorIdx, U16 bufSize, U8 *pBuf, U16 *descriptorSize)
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);

	// Make sure Entity Model is created
	if (!openavbAemCheckModel(FALSE)) {
		AVB_RC_LOG_TRACE_RET(AVB_RC(OPENAVB_AVDECC_FAILURE | OPENAVBAVDECC_RC_ENTITY_MODEL_MISSING), AVB_TRACE_AEM);
	}

	if (!pBuf || !descriptorSize) {
		AVB_RC_LOG_TRACE_RET(AVB_RC(OPENAVB_AVDECC_FAILURE | OPENAVB_RC_INVALID_ARGUMENT), AVB_TRACE_AEM);
	}

	*descriptorSize = 0;

	void *pDescriptor = openavbAemFindDescriptor(configIdx, descriptorType, descriptorIdx);
	if (pDescriptor) {
		openavb_aem_descriptor_common_t *pDescriptorCommon = pDescriptor;
		if (IS_OPENAVB_FAILURE(pDescriptorCommon->descriptorPvtPtr->update(pDescriptor))) {
			AVB_RC_TRACE_RET(AVB_RC(OPENAVB_AVDECC_FAILURE | OPENAVBAVDECC_RC_STALE_DATA), AVB_TRACE_AEM);
		}
		if (IS_OPENAVB_FAILURE(pDescriptorCommon->descriptorPvtPtr->toBuf(pDescriptor, bufSize, pBuf, descriptorSize))) {
			AVB_RC_TRACE_RET(AVB_RC(OPENAVB_AVDECC_FAILURE | OPENAVBAVDECC_RC_GENERIC), AVB_TRACE_AEM);
		}
		AVB_RC_TRACE_RET(OPENAVB_AVDECC_SUCCESS, AVB_TRACE_AEM);
	}

	AVB_RC_TRACE_RET(AVB_RC(OPENAVB_AVDECC_FAILURE | OPENAVBAVDECC_RC_UNKNOWN_DESCRIPTOR), AVB_TRACE_AEM);
}

bool openavbAemAddDescriptorConfiguration(openavb_aem_descriptor_configuration_t *pDescriptor, U16 *pResultIdx)
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);

	// Make sure Entity Model is created
	if (!openavbAemCheckModel(TRUE)) {
		AVB_TRACE_EXIT(AVB_TRACE_AEM);
		return FALSE;
	}

	if (!pDescriptor || !pResultIdx) {
		AVB_RC_LOG(OPENAVB_AVDECC_FAILURE | OPENAVB_RC_INVALID_ARGUMENT);
		AVB_TRACE_EXIT(AVB_TRACE_AEM);
		return FALSE;
	}

	// Allocate the new configuration structure
	openavb_aem_configuration_t *pAemConfiguration = openavbArrayDataNew(pAemEntityModel->aemConfigurations);
	if (!pAemConfiguration) {
		AVB_RC_LOG(OPENAVB_AVDECC_FAILURE | OPENAVB_RC_OUT_OF_MEMORY);
		AVB_TRACE_EXIT(AVB_TRACE_AEM);
		return FALSE;
	}

	// Assign the descriptor to the new configuration
	pAemConfiguration->pDescriptorConfiguration = pDescriptor;

	// Set to return descriptor index and increment the count
	S32 retIdx = openavbArrayFindData(pAemEntityModel->aemConfigurations, pAemConfiguration);
	if (retIdx  < 0) {
		return FALSE;
	}
	*pResultIdx = retIdx;

	pAemEntityModel->pDescriptorEntity->configurations_count++;

	AVB_TRACE_EXIT(AVB_TRACE_AEM);
	return TRUE;
}

////////////////////////////////
// Public functions
////////////////////////////////
extern DLL_EXPORT U16 openavbAemGetConfigIdx()
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);

	// This function does NOT check for errors but at a minimum avoid a crash.
	if (!pAemEntityModel || !pAemEntityModel->pDescriptorEntity) {
		AVB_RC_LOG(OPENAVB_AVDECC_FAILURE | OPENAVBAVDECC_RC_ENTITY_MODEL_MISSING);
		AVB_TRACE_EXIT(AVB_TRACE_AEM);
		return 0;
	}
	AVB_TRACE_EXIT(AVB_TRACE_AEM);
	return pAemEntityModel->pDescriptorEntity->current_configuration;
}


extern DLL_EXPORT bool openavbAemAddDescriptor(void *pDescriptor, U16 configIdx, U16 *pResultIdx)
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);

	// Make sure Entity Model is created
	if (!openavbAemCheckModel(TRUE)) {
		AVB_TRACE_EXIT(AVB_TRACE_AEM);
		return FALSE;
	}

	if (!pDescriptor || !pResultIdx) {
		AVB_RC_LOG(OPENAVB_AVDECC_FAILURE | OPENAVB_RC_INVALID_ARGUMENT);
		AVB_TRACE_EXIT(AVB_TRACE_AEM);
		return FALSE;
	}

	openavb_aem_descriptor_common_t *pDescriptorCommon = pDescriptor;

	if (pDescriptorCommon->descriptor_type == OPENAVB_AEM_DESCRIPTOR_ENTITY) {
		// Entity Descriptors aren't handled in this function.
		AVB_RC_LOG(OPENAVB_AVDECC_FAILURE | OPENAVB_RC_INVALID_ARGUMENT);
		AVB_TRACE_EXIT(AVB_TRACE_AEM);
		return FALSE;
	}
	if (pDescriptorCommon->descriptor_type == OPENAVB_AEM_DESCRIPTOR_CONFIGURATION) {
		// Configuration descriptors have special processing
		bool result = openavbAemAddDescriptorConfiguration(pDescriptor, pResultIdx);
		AVB_TRACE_EXIT(AVB_TRACE_AEM);
		return result;
	}

	openavb_array_t descriptors = NULL;
	if (pDescriptorCommon->descriptorPvtPtr->bTopLevel) {
		openavb_aem_configuration_t *pConfig = openavbArrayDataIdx(pAemEntityModel->aemConfigurations, configIdx);
		if (!pConfig) {
			AVB_RC_LOG(OPENAVB_AVDECC_FAILURE | OPENAVB_RC_INVALID_ARGUMENT);
			AVB_LOGF_ERROR("Invalid configuration index %u", configIdx);
			AVB_TRACE_EXIT(AVB_TRACE_AEM);
			return FALSE;
		}
		descriptors = pConfig->descriptorsArray[pDescriptorCommon->descriptor_type];

		if (!descriptors) {
			// Create array for this type of descriptor in this configuration
			AVB_LOGF_DEBUG("Created description array for configuration %u, type 0x%04x", configIdx, pDescriptorCommon->descriptor_type);
			descriptors = openavbArrayNewArray(pDescriptorCommon->descriptorPvtPtr->size);
			pConfig->descriptorsArray[pDescriptorCommon->descriptor_type] = descriptors;
		}
	} else {
		descriptors = pAemEntityModel->aemNonTopLevelDescriptorsArray[pDescriptorCommon->descriptor_type];

		if (!descriptors) {
			// Create array for this type of descriptor in this configuration
			AVB_LOGF_DEBUG("Created non-top-level description array for type 0x%04x", pDescriptorCommon->descriptor_type);
			descriptors = openavbArrayNewArray(pDescriptorCommon->descriptorPvtPtr->size);
			pAemEntityModel->aemNonTopLevelDescriptorsArray[pDescriptorCommon->descriptor_type] = descriptors;
		}
	}

	if (descriptors) {
		if (openavbArrayFindData(descriptors, pDescriptor) >= 0) {
			AVB_LOGF_ERROR("Attempted to add same descriptor (type 0x%04x) twice", pDescriptorCommon->descriptor_type);
			AVB_TRACE_EXIT(AVB_TRACE_AEM);
			return FALSE;
		}

		openavb_array_elem_t elem = openavbArrayAdd(descriptors, pDescriptor);
		if (elem) {
			*pResultIdx = openavbArrayGetIdx(elem);
			pDescriptorCommon->descriptor_index = *pResultIdx;
			if (pDescriptorCommon->descriptorPvtPtr->bTopLevel) {
				if (!IS_OPENAVB_SUCCESS(openavbAemAddDescriptorToConfiguration(pDescriptorCommon->descriptor_type, configIdx))) {
					AVB_TRACE_EXIT(AVB_TRACE_AEM);
					return FALSE;
				}
			}
			AVB_TRACE_EXIT(AVB_TRACE_AEM);
			return TRUE;
		}
	}

	AVB_TRACE_EXIT(AVB_TRACE_AEM);
	return FALSE;
}

extern DLL_EXPORT void *openavbAemGetDescriptor(U16 configIdx, U16 descriptorType, U16 descriptorIdx)
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);
	void *retDescriptor = NULL;

	retDescriptor = openavbAemFindDescriptor(configIdx, descriptorType, descriptorIdx);

	AVB_TRACE_EXIT(AVB_TRACE_AEM);
	return retDescriptor;
}

extern DLL_EXPORT U16 openavbAemGetDescriptorIndex(U16 configIdx, const void *pDescriptor)
{
	const openavb_aem_descriptor_common_t *pDescriptorCommon = pDescriptor;
	U16 descriptorIdx = 0;
	void * pTest;

	if (!pDescriptorCommon) { return OPENAVB_AEM_DESCRIPTOR_INVALID; }

	do {
		pTest = openavbAemFindDescriptor(configIdx, pDescriptorCommon->descriptor_type, descriptorIdx);
		if (pTest == pDescriptor) { return descriptorIdx; }
		descriptorIdx++;
	} while (pTest != NULL);

	return OPENAVB_AEM_DESCRIPTOR_INVALID;
}

extern DLL_EXPORT bool openavbAemSetString(U8 *pMem, const char *pString)
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);

	if (pMem && pString) {
		U32 len = strlen(pString);
		if (len > OPENAVB_AEM_STRLEN_MAX) {
			len = OPENAVB_AEM_STRLEN_MAX;
		} else if (len < OPENAVB_AEM_STRLEN_MAX) {
			memset(pMem + len, 0, OPENAVB_AEM_STRLEN_MAX - len);
		}
		memcpy(pMem, pString, len);		// Per 1722.1 it is allowable that the AEM string may not be null terminated.
		AVB_TRACE_EXIT(AVB_TRACE_AEM);
		return TRUE;
	}

	AVB_TRACE_EXIT(AVB_TRACE_AEM);
	return FALSE;
}

extern DLL_EXPORT bool openavbAemIsAcquired()
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);
	AEM_LOCK();
	bool bResult = pAemEntityModel->entityAcquired;
	AEM_UNLOCK();
	AVB_TRACE_EXIT(AVB_TRACE_AEM);
	return bResult;
}

extern DLL_EXPORT bool openavbAemIsLocked()
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);
	AEM_LOCK();
	bool bResult = pAemEntityModel->entityLocked;
	AEM_UNLOCK();
	AVB_TRACE_EXIT(AVB_TRACE_AEM);
	return bResult;
}

extern DLL_EXPORT bool openavbAemAcquire(bool flag)
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);
	AEM_LOCK();
	bool bResult = FALSE;
	if (flag) {
		if (!pAemEntityModel->entityAcquired) {
			*(pAemEntityModel->acquiredControllerId) = (U64)0x0000000000000000L;
			pAemEntityModel->entityAcquired = TRUE;
			bResult = TRUE;
		}
	}
	else {
		if (pAemEntityModel->entityAcquired) {
			if ((U64)*pAemEntityModel->acquiredControllerId == 0x0000000000000000L) {
				// Only unacquire if controller ID is zero. 
				pAemEntityModel->entityAcquired = FALSE;
				bResult = TRUE;
			}
		}
	}
	AEM_UNLOCK();
	AVB_TRACE_EXIT(AVB_TRACE_AEM);
	return bResult;
}

extern DLL_EXPORT bool openavbAemLock(bool flag)
{
	AVB_TRACE_ENTRY(AVB_TRACE_AEM);
	AEM_LOCK();
	bool bResult = FALSE;
	if (flag) {
		if (!pAemEntityModel->entityLocked) {
			*(pAemEntityModel->lockedControllerId) = (U64)0x0000000000000000L;
			pAemEntityModel->entityLocked = TRUE;
			bResult = TRUE;
		}
	}
	else {
		if (pAemEntityModel->entityLocked) {
			if ((U64)*pAemEntityModel->lockedControllerId == 0x0000000000000000L) {
				// Only unlock if controller ID is zero. 
				pAemEntityModel->entityLocked = FALSE;
				bResult = TRUE;
			}
		}
	}
	AEM_UNLOCK();
	AVB_TRACE_EXIT(AVB_TRACE_AEM);
	return bResult;
}