summaryrefslogtreecommitdiffstats
path: root/arm-wt-22k/jetcreator_lib_src/darwin-x86/EASLibVst.c
blob: 58c87700d7c559f7df90d7f9e245d113e44e3b4e (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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
/*
 *  EASLib.c
 *  EASLIb
 *
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include "eas.h"
#include "eas_report.h"
#include "eas_host.h"

#ifdef JET_INTERFACE
#include "jet.h"
#endif


#define EAS_EXPORT __attribute__((visibility("default")))

// #define DEBUG_FILE_IO

/* include debug interface */
#include "eas_host_debug.h"

#ifdef AUX_MIXER
#include "eas_auxmix.h"
#endif

/* this module requires dynamic memory support */
#ifdef _STATIC_MEMORY
#error "eas_hostmm.c requires the dynamic memory model!\n"
#endif

#ifndef EAS_MAX_FILE_HANDLES
#define EAS_MAX_FILE_HANDLES    32
#endif

#ifndef EAS_FILE_BUFFER_SIZE
#define EAS_FILE_BUFFER_SIZE    32
#endif

/* 
 * this structure and the related function are here
 * to support the ability to create duplicate handles
 * and buffering it in memory. If your system uses
 * in-memory resources, you can eliminate the calls
 * to malloc and free, the dup flag, and simply track
 * the file size and read position.
 */
 #ifdef BUFFERED_FILE_ACCESS
typedef struct eas_hw_file_tag
{
    FILE *pFile;
    EAS_I32 bytesInBuffer;
    EAS_I32 readIndex;
    EAS_I32 filePos;
    EAS_I32 fileSize;
    EAS_BOOL dup;
    EAS_U8 buffer[EAS_FILE_BUFFER_SIZE];
} EAS_HW_FILE;
#else
typedef struct eas_hw_file_tag
{
    EAS_I32         fileSize;
    EAS_I32         filePos;
    EAS_BOOL        dup;
    EAS_U8          *buffer;
} EAS_HW_FILE;
#endif

typedef struct eas_hw_inst_data_tag
{
    EAS_HW_FILE     files[EAS_MAX_FILE_HANDLES];
} EAS_HW_INST_DATA;

EAS_BOOL errorConditions[eNumErrorConditions];
EAS_BOOL ledState;
EAS_BOOL vibState;
EAS_BOOL backlightState;

#define MAX_DEBUG_MSG_LEN 1024

typedef void (*EAS_LOG_FUNC)(EAS_INT severity, char *msg);

static EAS_LOG_FUNC logCallback = NULL;
static char messageBuffer[MAX_DEBUG_MSG_LEN];

/* error counts */
static EAS_INT eas_fatalErrors;
static EAS_INT eas_errors;
static EAS_INT eas_warnings;
static int severityLevel = 9999;

/* wave out device */
#ifndef MAX_WAVE_OUT_BUFFERS
#define MAX_WAVE_OUT_BUFFERS            8
#endif

#ifndef EAS_BUFFERS_PER_WAVE_BUFFER
#define EAS_BUFFERS_PER_WAVE_BUFFER     8
#endif

/*----------------------------------------------------------------------------
 * ResetErrorCounters()
 *----------------------------------------------------------------------------
*/
EAS_EXPORT void ResetErrorCounters()
{
    eas_fatalErrors = 0;
    eas_errors = 0;
    eas_warnings = 0;
}

/*----------------------------------------------------------------------------
 * SetLogCallback()
 *----------------------------------------------------------------------------
*/
EAS_EXPORT void SetLogCallback (EAS_LOG_FUNC callback)
{
    logCallback = callback;
}   
    
#ifndef _NO_DEBUG_PREPROCESSOR
static S_DEBUG_MESSAGES debugMessages[] =
{
#ifdef UNIFIED_DEBUG_MESSAGES
#include "eas_debugmsgs.h"
#endif
    { 0,0,0 }
};

/*----------------------------------------------------------------------------
 * EAS_ReportEx()
 *----------------------------------------------------------------------------
*/
void EAS_ReportEx (int severity, unsigned long hashCode, int serialNum, ...)
{
    va_list vargs;
    int i;

    switch (severity)
    {
        case _EAS_SEVERITY_FATAL:
            eas_fatalErrors++;
            break;
            
        case _EAS_SEVERITY_ERROR:
            eas_errors++;
            break;

        case _EAS_SEVERITY_WARNING:
            eas_warnings++;
            break;

        default:
            break;
    }

    /* check severity level */
    if (severity > severityLevel)
        return;

    /* check for callback */
    if (logCallback == NULL)
        return;

    /* find the error message and output to stdout */
    for (i = 0; debugMessages[i].m_pDebugMsg; i++)
    {
        if ((debugMessages[i].m_nHashCode == hashCode) &&
        (debugMessages[i].m_nSerialNum == serialNum))
        {
            va_start(vargs, serialNum);
#ifdef WIN32
            vsprintf_s(messageBuffer, sizeof(messageBuffer), fmt, vargs);
#else
            vsprintf(messageBuffer, debugMessages[i].m_pDebugMsg, vargs);
#endif          
            logCallback(severity, messageBuffer);
            va_end(vargs);
            return;
        }
    }
    printf("Unrecognized error: Severity=%d; HashCode=%lu; SerialNum=%d\n", severity, hashCode, serialNum);
} /* end EAS_ReportEx */

#else
/*----------------------------------------------------------------------------
 * EAS_Report()
 *----------------------------------------------------------------------------
*/
void EAS_Report (int severity, const char *fmt, ...)
{
    va_list vargs;
    
    switch (severity)
    {
        case _EAS_SEVERITY_FATAL:
            eas_fatalErrors++;
            break;
            
        case _EAS_SEVERITY_ERROR:
            eas_errors++;
            break;

        case _EAS_SEVERITY_WARNING:
            eas_warnings++;
            break;

        default:
            break;
    }

    /* check severity level */
    if (severity > severityLevel)
        return;

    /* check for callback */
    if (logCallback == NULL)
        return;

    va_start(vargs, fmt);
#ifdef _WIN32
    vsprintf_s(messageBuffer, sizeof(messageBuffer), fmt, vargs);
#else
    vsprintf(messageBuffer, fmt, vargs);
#endif
    logCallback(severity, messageBuffer);
    va_end(vargs);
} /* end EAS_Report */

/*----------------------------------------------------------------------------
 * EAS_ReportX()
 *----------------------------------------------------------------------------
*/
void EAS_ReportX (int severity, const char *fmt, ...)
{
    va_list vargs;
    
    switch (severity)
    {
        case _EAS_SEVERITY_FATAL:
            eas_fatalErrors++;
            break;
            
        case _EAS_SEVERITY_ERROR:
            eas_errors++;
            break;

        case _EAS_SEVERITY_WARNING:
            eas_warnings++;
            break;

        default:
            break;
    }

    /* check severity level */
    if (severity > severityLevel)
        return;

    /* check for callback */
    if (logCallback == NULL)
        return;

    va_start(vargs, fmt);
#ifdef _WIN32
    vsprintf_s(messageBuffer, sizeof(messageBuffer), fmt, vargs);
#else
    vsprintf(messageBuffer, fmt, vargs);
#endif
    logCallback(severity, messageBuffer);
    va_end(vargs);
}
#endif

/*----------------------------------------------------------------------------
 * EAS_DLLSetDebugLevel()
 *----------------------------------------------------------------------------
*/
EAS_EXPORT void EAS_DLLSetDebugLevel (int severity)
{
    severityLevel = severity;
}

/*----------------------------------------------------------------------------
 * EAS_ExSetDebugLevel()
 *----------------------------------------------------------------------------
*/
void EAS_SetDebugLevel (int severity)
{
    severityLevel = severity;
}

/*----------------------------------------------------------------------------
 * EAS_SelectLibrary()
 *----------------------------------------------------------------------------
*/
EAS_EXPORT EAS_RESULT EAS_SelectLib (EAS_DATA_HANDLE pEASData, EAS_HANDLE streamHandle, EAS_BOOL testLib)
{
    extern EAS_SNDLIB_HANDLE VMGetLibHandle(EAS_INT libNum);
    return EAS_SetSoundLibrary(pEASData, streamHandle, VMGetLibHandle(testLib ? 1 : 0));
}


#if defined(_DEBUG) && !defined(MSC)
#include <crtdbg.h>
/*----------------------------------------------------------------------------
 * EnableHeapDebug()
 *----------------------------------------------------------------------------
*/
static void EnableHeapDebug (void)
{
    int temp;
    temp = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
    temp |= _CRTDBG_ALLOC_MEM_DF;
    temp |= _CRTDBG_CHECK_ALWAYS_DF;
    temp |= _CRTDBG_LEAK_CHECK_DF;
//  temp |= _CRTDBG_DELAY_FREE_MEM_DF;
    _CrtSetDbgFlag(temp);
}

/*----------------------------------------------------------------------------
 * HeapCheck()
 *----------------------------------------------------------------------------
 * Check heap status
 *----------------------------------------------------------------------------
*/
void HeapCheck (void)
{
    int heapStatus;

    /* Check heap status */
    heapStatus = _heapchk();
    if ((heapStatus == _HEAPOK) || (heapStatus == _HEAPEMPTY))
        return;

    EAS_ReportX(_EAS_SEVERITY_FATAL, "Heap corrupt\n" );
}
#endif


/*----------------------------------------------------------------------------
 * EAS_HWInit
 *
 * Initialize host wrapper interface
 *
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_HWInit (EAS_HW_DATA_HANDLE *pHWInstData)
{

#if defined(_DEBUG) && !defined(MSC)
    EnableHeapDebug();
#endif

 #ifdef BUFFERED_FILE_ACCESS
    EAS_ReportX(_EAS_SEVERITY_INFO, "EAS_HWInit: Buffered file access\n");
 #else
    EAS_ReportX(_EAS_SEVERITY_INFO, "EAS_HWInit: Memory mapped file access\n");
 #endif

    /* simulate failure */
    if (errorConditions[eInitError])
        return EAS_FAILURE;

    /* need to track file opens for duplicate handles */
    *pHWInstData = malloc(sizeof(EAS_HW_INST_DATA));
    if (!(*pHWInstData))
        return EAS_ERROR_MALLOC_FAILED;
    
    EAS_HWMemSet(*pHWInstData, 0, sizeof(EAS_HW_INST_DATA));
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * EAS_HWShutdown
 *
 * Shut down host wrapper interface
 *
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_HWShutdown (EAS_HW_DATA_HANDLE hwInstData)
{

    /* simulate failure */
    if (errorConditions[eShutdownError])
        return EAS_FAILURE;

    free(hwInstData);

#if defined(_DEBUG) && !defined(MSC)
    HeapCheck();
#endif
    
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWMalloc
 *
 * Allocates dynamic memory
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) available for customer use */
void *EAS_HWMalloc (EAS_HW_DATA_HANDLE hwInstData, EAS_I32 size)
{
    /* simulate failure */
    if (errorConditions[eMallocError])
        return NULL;

    return malloc((size_t) size);
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWFree
 *
 * Frees dynamic memory
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) available for customer use */
void EAS_HWFree (EAS_HW_DATA_HANDLE hwInstData, void *p)
{
    free(p);
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWMemCpy
 *
 * Copy memory wrapper
 *
 *----------------------------------------------------------------------------
*/
void *EAS_HWMemCpy (void *dest, const void *src, EAS_I32 amount) 
{
    return memcpy(dest, src, (size_t) amount);
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWMemSet
 *
 * Set memory wrapper
 *
 *----------------------------------------------------------------------------
*/
void *EAS_HWMemSet (void *dest, int val, EAS_I32 amount) 
{
    return memset(dest, val, (size_t) amount);
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWMemCmp
 *
 * Compare memory wrapper
 *
 *----------------------------------------------------------------------------
*/
EAS_I32 EAS_HWMemCmp (const void *s1, const void *s2, EAS_I32 amount) 
{
    return (EAS_I32) memcmp(s1, s2, (size_t) amount);
}

#ifdef BUFFERED_FILE_ACCESS
/*----------------------------------------------------------------------------
 *
 * EAS_HWOpenFile
 *
 * Open a file for read or write
 *
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_HWOpenFile (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_LOCATOR locator, EAS_FILE_HANDLE *pFile, EAS_FILE_MODE mode)
{
    EAS_HW_FILE *file;
    int i;

    /* set return value to NULL */
    *pFile = NULL;

    /* only support read mode at this time */
    if (mode != EAS_FILE_READ)
        return EAS_ERROR_INVALID_FILE_MODE;

    /* find an empty entry in the file table */
    file = hwInstData->files;
    for (i = 0; i < EAS_MAX_FILE_HANDLES; i++)
    {
        /* is this slot being used? */
        if (file->pFile == NULL)
        {
            EAS_RESULT result;

            /* open the file */
            file->pFile = fopen((const char*) locator, "rb");
            if (file->pFile == NULL)
                return EAS_ERROR_FILE_OPEN_FAILED;

            /* get file length */
            if ((result = EAS_HWFileLength(hwInstData, file, &file->fileSize)) != EAS_SUCCESS)
            {
                EAS_HWCloseFile(hwInstData, file);
                return result;
            }

#ifdef DEBUG_FILE_IO            
            EAS_ReportX(_EAS_SEVERITY_NOFILTER, "EAS_HWOpenFile: Open file %d\n", i);
#endif

            /* initialize some values */
            file->bytesInBuffer = 0;
            file->readIndex = 0;
            file->filePos = 0;
            file->dup = EAS_FALSE;

            *pFile = file;
            return EAS_SUCCESS;
        }
        file++;
    }

    /* too many open files */
    return EAS_ERROR_MAX_FILES_OPEN;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWFillBuffer
 *
 * Fill buffer from file
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) hwInstData available for customer use */
EAS_RESULT EAS_HWFillBuffer (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file)
{
    /* reposition the file pointer */
    if (fseek(file->pFile, file->filePos, SEEK_SET) != 0)
        return EAS_ERROR_FILE_SEEK;

    /* read some data from the file */
    file->bytesInBuffer = (EAS_I32) fread(file->buffer, 1, EAS_FILE_BUFFER_SIZE, file->pFile);
    file->readIndex = 0;
    if (file->bytesInBuffer == 0)
        return EAS_EOF;
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWReadFile
 *
 * Read data from a file
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) hwInstData available for customer use */
EAS_RESULT EAS_HWReadFile (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, void *pBuffer, EAS_I32 n, EAS_I32 *pBytesRead)
{
    EAS_RESULT result;
    EAS_I32 temp;
    EAS_U8 *p = pBuffer;
    EAS_I32 bytesLeft = n;

    *pBytesRead = 0;

    /* check handle integrity */    
    if (file->pFile == NULL)
        return EAS_ERROR_INVALID_HANDLE;

#ifdef DEBUG_FILE_IO
    EAS_ReportX(_EAS_SEVERITY_NOFILTER, "EAS_HWReadFile: Reading %d bytes from position %d\n", n, file->filePos);
#endif

    /* try to fulfill request from buffer */
    for (;bytesLeft > 0;)
    {
        /* how many bytes can we get from buffer? */
        temp = file->bytesInBuffer - file->readIndex;
        if (temp > bytesLeft)
            temp = bytesLeft;
        
        /* copy data from buffer */
        EAS_HWMemCpy(p, &file->buffer[file->readIndex], temp);
        *pBytesRead += temp;
        file->readIndex += temp;
        file->filePos += temp;
        p += temp;
        bytesLeft -= temp;

        /* don't refill buffer if request is bigger than buffer */
        if ((bytesLeft == 0) || (bytesLeft >= EAS_FILE_BUFFER_SIZE))
            break;

        /* refill buffer */
        if ((result = EAS_HWFillBuffer(hwInstData, file)) != EAS_SUCCESS)
            return result;
    }

    /* more to read? do unbuffered read directly to target memory */
    if (bytesLeft)
    {

        /* position the file pointer */
        if (fseek(file->pFile, file->filePos, SEEK_SET) != 0)
            return EAS_ERROR_FILE_SEEK;

        /* read data in the buffer */
        temp = (EAS_I32) fread(p, 1, (size_t) bytesLeft, file->pFile);
        *pBytesRead += temp;
        file->filePos += temp;

        /* reset buffer info */
        file->bytesInBuffer = 0;
        file->readIndex = 0;
    }

#ifdef DEBUG_FILE_IO
    {
#define BYTES_PER_LINE 16
        char str[BYTES_PER_LINE * 3 + 1];
        EAS_INT i;
        for (i = 0; i < (n > BYTES_PER_LINE ? BYTES_PER_LINE : n) ; i ++)
            sprintf(&str[i*3], "%02x ", ((EAS_U8*)pBuffer)[i]);
        if (i)
            EAS_ReportX(_EAS_SEVERITY_NOFILTER, "%s\n", str);
    }
#endif

    /* were n bytes read? */
    if (*pBytesRead != n)
        return EAS_EOF;

    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWGetByte
 *
 * Read a byte from a file
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) hwInstData available for customer use */
EAS_RESULT EAS_HWGetByte (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, void *p)
{
    EAS_RESULT result;

    /* check handle integrity */    
    if (file->pFile == NULL)
        return EAS_ERROR_INVALID_HANDLE;

    /* use local buffer - do we have any data? */
    if (file->readIndex >= file->bytesInBuffer)
    {
        if ((result = EAS_HWFillBuffer(hwInstData, file)) != EAS_SUCCESS)
            return result;

        /* if nothing to read, return EOF */
        if (file->bytesInBuffer == 0)
            return EAS_EOF;
    }

    /* get a character from the buffer */
    *((EAS_U8*) p) = file->buffer[file->readIndex++];

#ifdef DEBUG_FILE_IO
    EAS_ReportX(_EAS_SEVERITY_NOFILTER, "EAS_HWGetByte: Reading from position %d, byte = 0x%02x\n", file->filePos, *(EAS_U8*)p);
#endif

    file->filePos++;
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWGetWord
 *
 * Read a 16-bit value from the file
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_HWGetWord (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, void *p, EAS_BOOL msbFirst)
{
    EAS_RESULT result;
    EAS_I32 count;
    EAS_U8 c[2];

#ifdef DEBUG_FILE_IO
    EAS_ReportX(_EAS_SEVERITY_NOFILTER, "EAS_HWGetWord: Reading 2 bytes from position %d\n", file->filePos);
#endif

    /* read 2 bytes from the file */
    if ((result = EAS_HWReadFile(hwInstData, file, c, 2, &count)) != EAS_SUCCESS)
        return result;

    /* order them as requested */
    if (msbFirst)
        *((EAS_U16*) p) = ((EAS_U16) c[0] << 8) | c[1];
    else
        *((EAS_U16*) p) = ((EAS_U16) c[1] << 8) | c[0];

    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWGetDWord
 *
 * Read a 16-bit value from the file
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_HWGetDWord (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, void *p, EAS_BOOL msbFirst)
{
    EAS_RESULT result;
    EAS_I32 count;
    EAS_U8 c[4];

#ifdef DEBUG_FILE_IO
    EAS_ReportX(_EAS_SEVERITY_NOFILTER, "EAS_HWGetDWord: Reading 4 bytes from position %d\n", file->filePos);
#endif

    /* read 4 bytes from the file */
    if ((result = EAS_HWReadFile(hwInstData, file, c, 4, &count)) != EAS_SUCCESS)
        return result;

    /* order them as requested */
    if (msbFirst)
        *((EAS_U32*) p) = ((EAS_U32) c[0] << 24) | ((EAS_U32) c[1] << 16) | ((EAS_U32) c[2] << 8) | c[3];
    else
        *((EAS_U32*) p) = ((EAS_U32) c[3] << 24) | ((EAS_U32) c[2] << 16) | ((EAS_U32) c[1] << 8) | c[0];
    
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWFilePos
 *
 * Returns the current location in the file 
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) hwInstData available for customer use */
EAS_RESULT EAS_HWFilePos (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_I32 *pPosition)
{

    /* check handle integrity */    
    if (file->pFile == NULL)
        return EAS_ERROR_INVALID_HANDLE;
    
    *pPosition = file->filePos;
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWFileSeek
 *
 * Seek to a specific location in the file
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) hwInstData available for customer use */
EAS_RESULT EAS_HWFileSeek (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_I32 position)
{
    EAS_I32 newIndex;

    /* check handle integrity */    
    if (file->pFile == NULL)
        return EAS_ERROR_INVALID_HANDLE;

    /* check for seek past end */
    if ((position < 0) || (position > file->fileSize))
        return EAS_ERROR_FILE_SEEK;

#ifdef DEBUG_FILE_IO
    EAS_ReportX(_EAS_SEVERITY_NOFILTER, "EAS_HWFileSeek: Seeking to new position %d\n", file->filePos);
#endif

    /* is new position in current buffer? */
    newIndex = position - file->filePos + file->readIndex;
    if ((newIndex >= 0) && (newIndex < file->bytesInBuffer))
    {
        file->readIndex = newIndex;
        file->filePos = position;
        return EAS_SUCCESS;
    }

    /* save new position and reset buffer info so EAS_HWGetByte doesn't fail */
    file->filePos = position;
    file->bytesInBuffer = 0;
    file->readIndex = 0;
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWFileSeekOfs
 *
 * Seek forward or back relative to the current position
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) hwInstData available for customer use */
EAS_RESULT EAS_HWFileSeekOfs (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_I32 position)
{
    EAS_I32 temp;

#ifdef DEBUG_FILE_IO
    EAS_ReportX(_EAS_SEVERITY_NOFILTER, "EAS_HWFileSeekOfs: Seeking to new position %d\n", file->filePos + position);
#endif

    /* check handle integrity */    
    if (file->pFile == NULL)
        return EAS_ERROR_INVALID_HANDLE;

    /* check for seek past end */
    temp = file->filePos + position;
    if ((temp < 0) || (temp > file->fileSize))
        return EAS_ERROR_FILE_SEEK;

    /* is new position in current buffer? */
    temp = position + file->readIndex;
    if ((temp >= 0) && (temp < file->bytesInBuffer))
    {
        file->readIndex = temp;
        file->filePos += position;
        return EAS_SUCCESS;
    }

    /* save new position and reset buffer info so EAS_HWGetByte doesn't fail */
    file->filePos += position;
    file->bytesInBuffer = 0;
    file->readIndex = 0;
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWFileLength
 *
 * Return the file length
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) hwInstData available for customer use */
EAS_RESULT EAS_HWFileLength (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_I32 *pLength)
{
    long pos;

    /* check handle integrity */    
    if (file->pFile == NULL)
        return EAS_ERROR_INVALID_HANDLE;

    if ((pos = ftell(file->pFile)) == -1L)
        return EAS_ERROR_FILE_LENGTH;
    if (fseek(file->pFile, 0L, SEEK_END) != 0)
        return EAS_ERROR_FILE_LENGTH;
    if ((*pLength = ftell(file->pFile)) == -1L)
        return EAS_ERROR_FILE_LENGTH;
    if (fseek(file->pFile, pos, SEEK_SET) != 0)
        return EAS_ERROR_FILE_LENGTH;
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWDupHandle
 *
 * Duplicate a file handle
 *
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_HWDupHandle (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_FILE_HANDLE* pDupFile)
{
    EAS_HW_FILE *dupfile;
    int i;

    /* check handle integrity */
    *pDupFile = NULL;
    if (file->pFile == NULL)
        return EAS_ERROR_INVALID_HANDLE;

    /* find an empty entry in the file table */
    dupfile = hwInstData->files;
    for (i = 0; i < EAS_MAX_FILE_HANDLES; i++)
    {
        /* is this slot being used? */
        if (dupfile->pFile == NULL)
        {

            /* copy info from the handle to be duplicated */
            dupfile->filePos = file->filePos;
            dupfile->pFile = file->pFile;
            dupfile->fileSize = file->fileSize;

            /* set the duplicate handle flag */
            dupfile->dup = file->dup = EAS_TRUE;

            /* initialize some values */
            dupfile->bytesInBuffer = 0;
            dupfile->readIndex = 0;

            *pDupFile = dupfile;
            return EAS_SUCCESS;
        }
        dupfile++;
    }

    /* too many open files */
    return EAS_ERROR_MAX_FILES_OPEN;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWClose
 *
 * Wrapper for fclose function
 *
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_HWCloseFile (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file1)
{
    EAS_HW_FILE *file2,*dupFile;
    int i;

    /* check handle integrity */    
    if (file1->pFile == NULL)
        return EAS_ERROR_INVALID_HANDLE;

    /* check for duplicate handle */
    if (file1->dup)
    {
        dupFile = NULL;
        file2 = hwInstData->files;
        for (i = 0; i < EAS_MAX_FILE_HANDLES; i++)
        {
            /* check for duplicate */
            if ((file1 != file2) && (file2->pFile == file1->pFile))
            {
                /* is there more than one duplicate? */
                if (dupFile != NULL)
                {
                    /* clear this entry and return */
                    file1->pFile = NULL;
                    return EAS_SUCCESS;
                }

                /* this is the first duplicate found */
                dupFile = file2;
            }
            file2++;
        }

        /* there is only one duplicate, clear the dup flag */
        if (dupFile)
            dupFile->dup = EAS_FALSE;
        else
            /* if we get here, there's a serious problem */
            return EAS_ERROR_HANDLE_INTEGRITY;

        /* clear this entry and return */
        file1->pFile = NULL;
        return EAS_SUCCESS;
    }

    /* no duplicates - close the file */
    if (fclose(file1->pFile) != 0)
        return EAS_ERROR_CLOSE_FAILED;

    /* clear this entry and return */
    file1->pFile = NULL;
    return EAS_SUCCESS;
}
#else
/*----------------------------------------------------------------------------
 *
 * EAS_HWOpenFile
 *
 * Open a file for read or write
 *
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_HWOpenFile (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_LOCATOR locator, EAS_FILE_HANDLE *pFile, EAS_FILE_MODE mode)
{
    EAS_HW_FILE *file;
    FILE *ioFile;
    int i, temp;

    /* set return value to NULL */
    *pFile = NULL;

    /* simulate failure */
    if (errorConditions[eOpenError])
        return EAS_FAILURE;

    /* only support read mode at this time */
    if (mode != EAS_FILE_READ)
        return EAS_ERROR_INVALID_FILE_MODE;

    /* find an empty entry in the file table */
    file = hwInstData->files;
    for (i = 0; i < EAS_MAX_FILE_HANDLES; i++)
    {
        /* is this slot being used? */
        if (file->buffer == NULL)
        {
            /* open the file */
            if ((ioFile = fopen(locator,"rb")) == NULL)
                return EAS_ERROR_FILE_OPEN_FAILED;

            /* determine the file size */
            if (fseek(ioFile, 0L, SEEK_END) != 0)
                return EAS_ERROR_FILE_LENGTH;
            if ((file->fileSize = ftell(ioFile)) == -1L)
                return EAS_ERROR_FILE_LENGTH;
            if (fseek(ioFile, 0L, SEEK_SET) != 0)
                return EAS_ERROR_FILE_LENGTH;

            /* allocate a buffer */
            file->buffer = EAS_HWMalloc(hwInstData, file->fileSize);
            if (file->buffer == NULL)
            {
                fclose(ioFile);
                return EAS_ERROR_MALLOC_FAILED;
            }

            /* read the file into memory */
            temp = (int) fread(file->buffer, (size_t) file->fileSize, 1, ioFile);
            
            /* close the file - don't need it any more */
            fclose(ioFile);

            /* check for error reading file */
            if (temp != 1)
                return EAS_ERROR_FILE_READ_FAILED;
            
            /* initialize some values */
            file->filePos = 0;
            file->dup = EAS_FALSE;

            *pFile = file;
            return EAS_SUCCESS;
        }
        file++;
    }

    /* too many open files */
    return EAS_ERROR_MAX_FILES_OPEN;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWReadFile
 *
 * Read data from a file
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) available for customer use */
EAS_RESULT EAS_HWReadFile (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, void *pBuffer, EAS_I32 n, EAS_I32 *pBytesRead)
{
    EAS_I32 count;

    /* simulate failure */
    if (errorConditions[eReadError])
        return EAS_FAILURE;

    /* make sure we have a valid handle */
    if (file->buffer == NULL)
        return EAS_ERROR_INVALID_HANDLE;

    /* calculate the bytes to read */
    count = file->fileSize - file->filePos;
    if (n < count)
        count = n;

    /* copy the data to the requested location, and advance the pointer */
    if (count)
        EAS_HWMemCpy(pBuffer, &file->buffer[file->filePos], count);
    file->filePos += count;
    *pBytesRead = count;

    /* were n bytes read? */
    if (count!= n)
        return EAS_EOF;
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWGetByte
 *
 * Read a byte from a file
 *
 *----------------------------------------------------------------------------
*/
/*lint -e{715} hwInstData available for customer use */
EAS_RESULT EAS_HWGetByte (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, void *p)
{

    /* simulate failure */
    if (errorConditions[eReadError])
        return EAS_FAILURE;

    /* make sure we have a valid handle */
    if (file->buffer == NULL)
        return EAS_ERROR_INVALID_HANDLE;

    /* check for end of file */
    if (file->filePos >= file->fileSize)
    {
        *((EAS_U8*) p) = 0;
        return EAS_EOF;
    }

    /* get a character from the buffer */
    *((EAS_U8*) p) = file->buffer[file->filePos++];
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWGetWord
 *
 * Returns the current location in the file 
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) available for customer use */
EAS_RESULT EAS_HWGetWord (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, void *p, EAS_BOOL msbFirst)
{
    EAS_RESULT result;
    EAS_U8 c1, c2;

    /* read 2 bytes from the file */
    if ((result = EAS_HWGetByte(hwInstData, file, &c1)) != EAS_SUCCESS)
        return result;
    if ((result = EAS_HWGetByte(hwInstData, file, &c2)) != EAS_SUCCESS)
        return result;

    /* order them as requested */
    if (msbFirst)
        *((EAS_U16*) p) = ((EAS_U16) c1 << 8) | c2;
    else
        *((EAS_U16*) p) = ((EAS_U16) c2 << 8) | c1;
    
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWGetDWord
 *
 * Returns the current location in the file 
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) available for customer use */
EAS_RESULT EAS_HWGetDWord (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, void *p, EAS_BOOL msbFirst)
{
    EAS_RESULT result;
    EAS_U8 c1, c2,c3,c4;

    /* read 4 bytes from the file */
    if ((result = EAS_HWGetByte(hwInstData, file, &c1)) != EAS_SUCCESS)
        return result;
    if ((result = EAS_HWGetByte(hwInstData, file, &c2)) != EAS_SUCCESS)
        return result;
    if ((result = EAS_HWGetByte(hwInstData, file, &c3)) != EAS_SUCCESS)
        return result;
    if ((result = EAS_HWGetByte(hwInstData, file, &c4)) != EAS_SUCCESS)
        return result;

    /* order them as requested */
    if (msbFirst)
        *((EAS_U32*) p) = ((EAS_U32) c1 << 24) | ((EAS_U32) c2 << 16) | ((EAS_U32) c3 << 8) | c4;
    else
        *((EAS_U32*) p)= ((EAS_U32) c4 << 24) | ((EAS_U32) c3 << 16) | ((EAS_U32) c2 << 8) | c1;
    
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWFilePos
 *
 * Returns the current location in the file 
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) available for customer use */
EAS_RESULT EAS_HWFilePos (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_I32 *pPosition)
{

    /* simulate failure */
    if (errorConditions[ePosError])
        return EAS_FAILURE;

    /* make sure we have a valid handle */
    if (file->buffer == NULL)
        return EAS_ERROR_INVALID_HANDLE;
    
    *pPosition = file->filePos;
    return EAS_SUCCESS;
} /* end EAS_HWFilePos */

/*----------------------------------------------------------------------------
 *
 * EAS_HWFileSeek
 *
 * Seek to a specific location in the file
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) available for customer use */
EAS_RESULT EAS_HWFileSeek (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_I32 position)
{

    /* simulate failure */
    if (errorConditions[eSeekError])
        return EAS_FAILURE;

    /* make sure we have a valid handle */
    if (file->buffer == NULL)
        return EAS_ERROR_INVALID_HANDLE;

    /* validate new position */ 
    if ((position < 0) || (position > file->fileSize))
        return EAS_ERROR_FILE_SEEK;

    /* save new position */
    file->filePos = position;
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWFileSeekOfs
 *
 * Seek forward or back relative to the current position
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) available for customer use */
EAS_RESULT EAS_HWFileSeekOfs (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_I32 position)
{

    /* simulate failure */
    if (errorConditions[eSeekError])
        return EAS_FAILURE;

    /* make sure we have a valid handle */
    if (file->buffer == NULL)
        return EAS_ERROR_INVALID_HANDLE;

    /* determine the file position */   
    position += file->filePos;
    if ((position < 0) || (position > file->fileSize))
        return EAS_ERROR_FILE_SEEK;

    /* save new position */
    file->filePos = position;
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWFileLength
 *
 * Return the file length
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) available for customer use */
EAS_RESULT EAS_HWFileLength (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_I32 *pLength)
{
    
    /* simulate failure */
    if (errorConditions[eLengthError])
        return EAS_FAILURE;

    /* make sure we have a valid handle */
    if (file->buffer == NULL)
        return EAS_ERROR_INVALID_HANDLE;
    
    *pLength = file->fileSize;
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWDupHandle
 *
 * Duplicate a file handle
 *
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_HWDupHandle (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_FILE_HANDLE *pDupFile)
{
    EAS_HW_FILE *dupFile;
    int i;

    /* simulate failure */
    if (errorConditions[eDupError])
        return EAS_FAILURE;

    /* make sure we have a valid handle */
    if (file->buffer == NULL)
        return EAS_ERROR_INVALID_HANDLE;

    /* find an empty entry in the file table */
    dupFile = hwInstData->files;
    for (i = 0; i < EAS_MAX_FILE_HANDLES; i++)
    {
        /* is this slot being used? */
        if (dupFile->buffer == NULL)
        {

            /* copy info from the handle to be duplicated */
            dupFile->filePos = file->filePos;
            dupFile->fileSize = file->fileSize;
            dupFile->buffer = file->buffer;

            /* set the duplicate handle flag */
            dupFile->dup = file->dup = EAS_TRUE;

            *pDupFile = dupFile;
            return EAS_SUCCESS;
        }
        dupFile++;
    }

    /* too many open files */
    return EAS_ERROR_MAX_FILES_OPEN;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWClose
 *
 * Wrapper for fclose function
 *
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_HWCloseFile (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file1)
{
    EAS_HW_FILE *file2,*dupFile;
    int i;

    /* simulate failure */
    if (errorConditions[eCloseError])
        return EAS_FAILURE;

    /* make sure we have a valid handle */
    if (file1->buffer == NULL)
        return EAS_ERROR_INVALID_HANDLE;

    /* check for duplicate handle */
    if (file1->dup)
    {
        dupFile = NULL;
        file2 = hwInstData->files;
        for (i = 0; i < EAS_MAX_FILE_HANDLES; i++)
        {
            /* check for duplicate */
            if ((file1 != file2) && (file2->buffer == file1->buffer))
            {
                /* is there more than one duplicate? */
                if (dupFile != NULL)
                {
                    /* clear this entry and return */
                    file1->buffer = NULL;
                    return EAS_SUCCESS;
                }

                /* this is the first duplicate found */
                else
                    dupFile = file2;
            }
            file2++;
        }

        /* there is only one duplicate, clear the dup flag */
        if (dupFile)
            dupFile->dup = EAS_FALSE;
        else
            /* if we get here, there's a serious problem */
            return EAS_ERROR_HANDLE_INTEGRITY;

        /* clear this entry and return */
        file1->buffer = NULL;
        return EAS_SUCCESS;
    }

    /* no duplicates -free the buffer */
    EAS_HWFree(hwInstData, file1->buffer);

    /* clear this entry and return */
    file1->buffer = NULL;
    return EAS_SUCCESS;
}
#endif

/*----------------------------------------------------------------------------
 *
 * EAS_HWVibrate
 *
 * Turn on/off vibrate function
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) available for customer use */
EAS_RESULT EAS_HWVibrate (EAS_HW_DATA_HANDLE hwInstData, EAS_BOOL state)
{
    vibState = state;
//  EAS_ReportX(_EAS_SEVERITY_NOFILTER, "Vibrate state: %d\n", state);
    return EAS_SUCCESS;
} /* end EAS_HWVibrate */

/*----------------------------------------------------------------------------
 *
 * EAS_HWLED
 *
 * Turn on/off LED
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) available for customer use */
EAS_RESULT EAS_HWLED (EAS_HW_DATA_HANDLE hwInstData, EAS_BOOL state)
{
    ledState = state;
//  EAS_ReportX(_EAS_SEVERITY_NOFILTER, "LED state: %d\n", state);
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWBackLight
 *
 * Turn on/off backlight
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) available for customer use */
EAS_RESULT EAS_HWBackLight (EAS_HW_DATA_HANDLE hwInstData, EAS_BOOL state)
{
    backlightState = state;
//  EAS_ReportX(_EAS_SEVERITY_NOFILTER, "Backlight state: %d\n", state);
    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 *
 * EAS_HWYield
 *
 * This function is called periodically by the EAS library to give the
 * host an opportunity to allow other tasks to run. There are two ways to
 * use this call:
 *
 * If you have a multi-tasking OS, you can call the yield function in the
 * OS to allow other tasks to run. In this case, return EAS_FALSE to tell
 * the EAS library to continue processing when control returns from this
 * function.
 *
 * If tasks run in a single thread by sequential function calls (sometimes
 * call a "commutator loop"), return EAS_TRUE to cause the EAS Library to
 * return to the caller. Be sure to check the number of bytes rendered
 * before passing the audio buffer to the codec - it may not be filled.
 * The next call to EAS_Render will continue processing until the buffer
 * has been filled.
 *
 *----------------------------------------------------------------------------
*/
/*lint -esym(715, hwInstData) available for customer use */
EAS_BOOL EAS_HWYield (EAS_HW_DATA_HANDLE hwInstData)
{
    /* put your code here */
    return EAS_FALSE;
}