eas_host.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*----------------------------------------------------------------------------
  2. *
  3. * File:
  4. * eas_host.c
  5. *
  6. * Contents and purpose:
  7. * This file contains the host wrapper functions for stdio, stdlib, etc.
  8. * This is a sample version that wraps the standard library functions.
  9. * Modify this file to suit the needs of your particular system.
  10. *
  11. * EAS_MAX_FILE_HANDLES sets the maximum number of MIDI streams within
  12. * a MIDI type 1 file that can be played. To maintain efficiency, data
  13. * is buffered locally when byte access is used (EAS_HWGetByte). The
  14. * size of the buffer is set by EAS_FILE_BUFFER_SIZE.
  15. *
  16. * EAS_HW_FILE is a structure to support local file buffering. It
  17. * comprises the OS File handle, some data related to the local file
  18. * buffer, the position of the next byte of data to be read, the dup
  19. * flag which when set, indicates that the handle has been duplicated,
  20. * and the data buffer. Since the data buffer is only used for byte
  21. * access, it does not need to be large.
  22. *
  23. * If the file system supports duplicate file handles and buffering,
  24. * this entire subsystem can be replaced with direct calls to the
  25. * native file I/O routines.
  26. *
  27. * If the system has enough memory to support reading the entire file
  28. * into memory, it will be much more efficient to do so on the call to
  29. * EAS_HWOpenFile and then close the file. Simply substitute a memory
  30. * pointer for the FILE* pointer. Calls to EAS_HW_DupHandle will work
  31. * as they do in this version. In the call to EAS_HWCloseFile, instead
  32. * of calling fclose, free the memory containing the file data.
  33. *
  34. * Copyright 2005 Sonic Network Inc.
  35. * Licensed under the Apache License, Version 2.0 (the "License");
  36. * you may not use this file except in compliance with the License.
  37. * You may obtain a copy of the License at
  38. *
  39. * http://www.apache.org/licenses/LICENSE-2.0
  40. *
  41. * Unless required by applicable law or agreed to in writing, software
  42. * distributed under the License is distributed on an "AS IS" BASIS,
  43. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  44. * See the License for the specific language governing permissions and
  45. * limitations under the License.
  46. *
  47. *----------------------------------------------------------------------------
  48. * Revision Control:
  49. * $Revision: 853 $
  50. * $Date: 2007-09-05 09:54:17 -0700 (Wed, 05 Sep 2007) $
  51. *----------------------------------------------------------------------------
  52. */
  53. #ifdef _lint
  54. #include "lint_stdlib.h"
  55. #else
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #endif
  60. #include "eas_host.h"
  61. // #define DEBUG_FILE_IO
  62. /* Only for debugging LED, vibrate, and backlight functions */
  63. #include "eas_report.h"
  64. #ifndef EAS_MAX_FILE_HANDLES
  65. #define EAS_MAX_FILE_HANDLES 32
  66. #endif
  67. #ifndef EAS_FILE_BUFFER_SIZE
  68. #define EAS_FILE_BUFFER_SIZE 32
  69. #endif
  70. /*
  71. * this structure and the related function are here
  72. * to support the ability to create duplicate handles
  73. * and buffering into a single file. If the OS supports
  74. * duplicate file handles natively, this code can be
  75. * stripped to eliminate double-buffering.
  76. */
  77. typedef struct eas_hw_file_tag
  78. {
  79. FILE *pFile;
  80. EAS_I32 bytesInBuffer;
  81. EAS_I32 readIndex;
  82. EAS_I32 filePos;
  83. EAS_BOOL dup;
  84. EAS_U8 buffer[EAS_FILE_BUFFER_SIZE];
  85. } EAS_HW_FILE;
  86. typedef struct eas_hw_inst_data_tag
  87. {
  88. EAS_HW_FILE files[EAS_MAX_FILE_HANDLES];
  89. } EAS_HW_INST_DATA;
  90. /* local memory for files and streams */
  91. #ifdef _STATIC_MEMORY
  92. EAS_HW_INST_DATA fileData;
  93. #endif
  94. /*----------------------------------------------------------------------------
  95. * EAS_HWInit
  96. *
  97. * Initialize host wrapper interface
  98. *
  99. *----------------------------------------------------------------------------
  100. */
  101. EAS_RESULT EAS_HWInit (EAS_HW_DATA_HANDLE *pHWInstData)
  102. {
  103. /* need to track file opens for duplicate handles */
  104. #ifndef _STATIC_MEMORY
  105. *pHWInstData = malloc(sizeof(EAS_HW_INST_DATA));
  106. if (!(*pHWInstData))
  107. return EAS_ERROR_MALLOC_FAILED;
  108. #else
  109. *pHWInstData = &fileData;
  110. #endif
  111. EAS_HWMemSet(*pHWInstData, 0, sizeof(EAS_HW_INST_DATA));
  112. return EAS_SUCCESS;
  113. }
  114. /*----------------------------------------------------------------------------
  115. * EAS_HWShutdown
  116. *
  117. * Shut down host wrapper interface
  118. *
  119. *----------------------------------------------------------------------------
  120. */
  121. /*lint -esym(715, hwInstData) hwInstData available for customer use */
  122. EAS_RESULT EAS_HWShutdown (EAS_HW_DATA_HANDLE hwInstData)
  123. {
  124. #ifndef _STATIC_MEMORY
  125. free(hwInstData);
  126. #endif
  127. return EAS_SUCCESS;
  128. }
  129. /*----------------------------------------------------------------------------
  130. *
  131. * EAS_HWMalloc
  132. *
  133. * Allocates dynamic memory
  134. *
  135. *----------------------------------------------------------------------------
  136. */
  137. /*lint -esym(715, hwInstData) hwInstData available for customer use */
  138. #ifdef _STATIC_MEMORY
  139. /*lint -esym(715, size) not used in static memory model */
  140. #endif
  141. void *EAS_HWMalloc (EAS_HW_DATA_HANDLE hwInstData, EAS_I32 size)
  142. {
  143. #ifdef _STATIC_MEMORY
  144. return NULL;
  145. #else
  146. return malloc((EAS_U32)size);
  147. #endif
  148. }
  149. /*----------------------------------------------------------------------------
  150. *
  151. * EAS_HWFree
  152. *
  153. * Frees dynamic memory
  154. *
  155. *----------------------------------------------------------------------------
  156. */
  157. /*lint -esym(715, hwInstData) hwInstData available for customer use */
  158. #ifdef _STATIC_MEMORY
  159. /*lint -esym(715, p) not used in static memory model */
  160. #endif
  161. void EAS_HWFree(EAS_HW_DATA_HANDLE hwInstData, void *p)
  162. {
  163. #ifndef _STATIC_MEMORY
  164. free(p);
  165. #endif
  166. }
  167. /*----------------------------------------------------------------------------
  168. *
  169. * EAS_HWMemCpy
  170. *
  171. * Copy memory wrapper
  172. *
  173. *----------------------------------------------------------------------------
  174. */
  175. void *EAS_HWMemCpy (void *dest, const void *src, EAS_I32 amount)
  176. {
  177. return memcpy(dest,src,(size_t) amount);
  178. }
  179. /*----------------------------------------------------------------------------
  180. *
  181. * EAS_HWMemSet
  182. *
  183. * Set memory wrapper
  184. *
  185. *----------------------------------------------------------------------------
  186. */
  187. void *EAS_HWMemSet (void *dest, int val, EAS_I32 amount)
  188. {
  189. return memset(dest,val,(size_t) amount);
  190. }
  191. /*----------------------------------------------------------------------------
  192. *
  193. * EAS_HWMemCmp
  194. *
  195. * Compare memory wrapper
  196. *
  197. *----------------------------------------------------------------------------
  198. */
  199. EAS_I32 EAS_HWMemCmp (const void *s1, const void *s2, EAS_I32 amount)
  200. {
  201. return (EAS_I32) memcmp(s1, s2, (size_t) amount);
  202. }
  203. /*----------------------------------------------------------------------------
  204. *
  205. * EAS_HWOpenFile
  206. *
  207. * Open a file for read or write
  208. *
  209. *----------------------------------------------------------------------------
  210. */
  211. EAS_RESULT EAS_HWOpenFile (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_LOCATOR locator, EAS_FILE_HANDLE *pFile, EAS_FILE_MODE mode)
  212. {
  213. EAS_HW_FILE *file;
  214. int i;
  215. /* set return value to NULL */
  216. *pFile = NULL;
  217. /* only support read mode at this time */
  218. if (mode != EAS_FILE_READ)
  219. return EAS_ERROR_INVALID_FILE_MODE;
  220. /* find an empty entry in the file table */
  221. file = hwInstData->files;
  222. for (i = 0; i < EAS_MAX_FILE_HANDLES; i++)
  223. {
  224. /* is this slot being used? */
  225. if (file->pFile == NULL)
  226. {
  227. /* open the file */
  228. if (locator->path)
  229. file->pFile = fopen((const char*) locator->path, "rb");
  230. if (file->pFile == NULL)
  231. return EAS_ERROR_FILE_OPEN_FAILED;
  232. #ifdef DEBUG_FILE_IO
  233. EAS_ReportX(_EAS_SEVERITY_NOFILTER, "EAS_HWOpenFile: Open file %d\n", i);
  234. #endif
  235. /* initialize some values */
  236. file->bytesInBuffer = 0;
  237. file->readIndex = 0;
  238. file->filePos = 0;
  239. file->dup = EAS_FALSE;
  240. *pFile = file;
  241. return EAS_SUCCESS;
  242. }
  243. file++;
  244. }
  245. /* too many open files */
  246. return EAS_ERROR_MAX_FILES_OPEN;
  247. }
  248. /*----------------------------------------------------------------------------
  249. *
  250. * EAS_HWFillBuffer
  251. *
  252. * Fill buffer from file
  253. *----------------------------------------------------------------------------
  254. */
  255. /*lint -esym(715, hwInstData) hwInstData available for customer use */
  256. EAS_RESULT EAS_HWFillBuffer (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file)
  257. {
  258. /* reposition the file pointer */
  259. if (fseek(file->pFile, file->filePos, SEEK_SET) != 0)
  260. return EAS_ERROR_FILE_SEEK;
  261. /* read some data from the file */
  262. file->bytesInBuffer = (EAS_I32) fread(file->buffer, 1, EAS_FILE_BUFFER_SIZE, file->pFile);
  263. file->readIndex = 0;
  264. if (file->bytesInBuffer == 0)
  265. return EAS_EOF;
  266. return EAS_SUCCESS;
  267. }
  268. /*----------------------------------------------------------------------------
  269. *
  270. * EAS_HWReadFile
  271. *
  272. * Read data from a file
  273. *----------------------------------------------------------------------------
  274. */
  275. /*lint -esym(715, hwInstData) hwInstData available for customer use */
  276. EAS_RESULT EAS_HWReadFile (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, void *pBuffer, EAS_I32 n, EAS_I32 *pBytesRead)
  277. {
  278. EAS_RESULT result;
  279. EAS_I32 temp;
  280. EAS_U8 *p = pBuffer;
  281. EAS_I32 bytesLeft = n;
  282. *pBytesRead = 0;
  283. /* check handle integrity */
  284. if (file->pFile == NULL)
  285. return EAS_ERROR_INVALID_HANDLE;
  286. #ifdef DEBUG_FILE_IO
  287. EAS_ReportX(_EAS_SEVERITY_NOFILTER, "EAS_HWReadFile: Reading %d bytes from position %d\n", n, file->filePos);
  288. #endif
  289. /* try to fulfill request from buffer */
  290. for (;bytesLeft > 0;)
  291. {
  292. /* how many bytes can we get from buffer? */
  293. temp = file->bytesInBuffer - file->readIndex;
  294. if (temp > bytesLeft)
  295. temp = bytesLeft;
  296. /* copy data from buffer */
  297. EAS_HWMemCpy(p, &file->buffer[file->readIndex], temp);
  298. *pBytesRead += temp;
  299. file->readIndex += temp;
  300. file->filePos += temp;
  301. bytesLeft -= temp;
  302. p += temp;
  303. /* don't refill buffer if request is bigger than buffer */
  304. if ((bytesLeft == 0) || (bytesLeft >= EAS_FILE_BUFFER_SIZE))
  305. break;
  306. /* refill buffer */
  307. if ((result = EAS_HWFillBuffer(hwInstData, file)) != EAS_SUCCESS)
  308. return result;
  309. }
  310. /* more to read? do unbuffered read directly to target memory */
  311. if (bytesLeft)
  312. {
  313. /* position the file pointer */
  314. if (fseek(file->pFile, file->filePos, SEEK_SET) != 0)
  315. return EAS_ERROR_FILE_SEEK;
  316. /* read data in the buffer */
  317. /*lint -e{826} lint doesn't like this with STATIC_MEMORY defined for some reason */
  318. temp = (EAS_I32) fread(p, 1, (size_t) bytesLeft, file->pFile);
  319. *pBytesRead += temp;
  320. file->filePos += temp;
  321. /* reset buffer info */
  322. file->bytesInBuffer = 0;
  323. file->readIndex = 0;
  324. }
  325. #ifdef DEBUG_FILE_IO
  326. {
  327. #define BYTES_PER_LINE 16
  328. char str[BYTES_PER_LINE * 3 + 1];
  329. EAS_INT i;
  330. for (i = 0; i < (n > BYTES_PER_LINE ? BYTES_PER_LINE : n) ; i ++)
  331. sprintf(&str[i*3], "%02x ", ((EAS_U8*)pBuffer)[i]);
  332. if (i)
  333. EAS_ReportX(_EAS_SEVERITY_NOFILTER, "%s\n", str);
  334. }
  335. #endif
  336. /* were n bytes read? */
  337. if (*pBytesRead != n)
  338. return EAS_EOF;
  339. return EAS_SUCCESS;
  340. }
  341. /*----------------------------------------------------------------------------
  342. *
  343. * EAS_HWGetByte
  344. *
  345. * Read a byte from a file
  346. *----------------------------------------------------------------------------
  347. */
  348. /*lint -esym(715, hwInstData) hwInstData available for customer use */
  349. EAS_RESULT EAS_HWGetByte (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, void *p)
  350. {
  351. EAS_RESULT result;
  352. /* check handle integrity */
  353. if (file->pFile == NULL)
  354. return EAS_ERROR_INVALID_HANDLE;
  355. /* use local buffer - do we have any data? */
  356. if (file->readIndex >= file->bytesInBuffer)
  357. {
  358. if ((result = EAS_HWFillBuffer(hwInstData, file)) != EAS_SUCCESS)
  359. return result;
  360. /* if nothing to read, return EOF */
  361. if (file->bytesInBuffer == 0)
  362. return EAS_EOF;
  363. }
  364. /* get a character from the buffer */
  365. *((EAS_U8*) p) = file->buffer[file->readIndex++];
  366. #ifdef DEBUG_FILE_IO
  367. EAS_ReportX(_EAS_SEVERITY_NOFILTER, "EAS_HWGetByte: Reading from position %d, byte = 0x%02x\n", file->filePos, *(EAS_U8*)p);
  368. #endif
  369. file->filePos++;
  370. return EAS_SUCCESS;
  371. }
  372. /*----------------------------------------------------------------------------
  373. *
  374. * EAS_HWGetWord
  375. *
  376. * Read a 16-bit value from the file
  377. *----------------------------------------------------------------------------
  378. */
  379. EAS_RESULT EAS_HWGetWord (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, void *p, EAS_BOOL msbFirst)
  380. {
  381. EAS_RESULT result;
  382. EAS_I32 count;
  383. EAS_U8 c[2];
  384. #ifdef DEBUG_FILE_IO
  385. EAS_ReportX(_EAS_SEVERITY_NOFILTER, "EAS_HWGetWord: Reading 2 bytes from position %d\n", file->filePos);
  386. #endif
  387. /* read 2 bytes from the file */
  388. if ((result = EAS_HWReadFile(hwInstData, file, c, 2, &count)) != EAS_SUCCESS)
  389. return result;
  390. /* order them as requested */
  391. if (msbFirst)
  392. *((EAS_U16*) p) = ((EAS_U16) c[0] << 8) | c[1];
  393. else
  394. *((EAS_U16*) p) = ((EAS_U16) c[1] << 8) | c[0];
  395. return EAS_SUCCESS;
  396. }
  397. /*----------------------------------------------------------------------------
  398. *
  399. * EAS_HWGetDWord
  400. *
  401. * Read a 16-bit value from the file
  402. *----------------------------------------------------------------------------
  403. */
  404. EAS_RESULT EAS_HWGetDWord (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, void *p, EAS_BOOL msbFirst)
  405. {
  406. EAS_RESULT result;
  407. EAS_I32 count;
  408. EAS_U8 c[4];
  409. #ifdef DEBUG_FILE_IO
  410. EAS_ReportX(_EAS_SEVERITY_NOFILTER, "EAS_HWGetDWord: Reading 4 bytes from position %d\n", file->filePos);
  411. #endif
  412. /* read 4 bytes from the file */
  413. if ((result = EAS_HWReadFile(hwInstData, file, c, 4, &count)) != EAS_SUCCESS)
  414. return result;
  415. /* order them as requested */
  416. if (msbFirst)
  417. *((EAS_U32*) p) = ((EAS_U32) c[0] << 24) | ((EAS_U32) c[1] << 16) | ((EAS_U32) c[2] << 8) | c[3];
  418. else
  419. *((EAS_U32*) p) = ((EAS_U32) c[3] << 24) | ((EAS_U32) c[2] << 16) | ((EAS_U32) c[1] << 8) | c[0];
  420. return EAS_SUCCESS;
  421. }
  422. /*----------------------------------------------------------------------------
  423. *
  424. * EAS_HWFilePos
  425. *
  426. * Returns the current location in the file
  427. *
  428. *----------------------------------------------------------------------------
  429. */
  430. /*lint -esym(715, hwInstData) hwInstData available for customer use */
  431. EAS_RESULT EAS_HWFilePos (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_I32 *pPosition)
  432. {
  433. /* check handle integrity */
  434. if (file->pFile == NULL)
  435. return EAS_ERROR_INVALID_HANDLE;
  436. *pPosition = file->filePos;
  437. return EAS_SUCCESS;
  438. }
  439. /*----------------------------------------------------------------------------
  440. *
  441. * EAS_HWFileSeek
  442. *
  443. * Seek to a specific location in the file
  444. *
  445. *----------------------------------------------------------------------------
  446. */
  447. /*lint -esym(715, hwInstData) hwInstData available for customer use */
  448. EAS_RESULT EAS_HWFileSeek (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_I32 position)
  449. {
  450. EAS_I32 newIndex;
  451. /* check handle integrity */
  452. if (file->pFile == NULL)
  453. return EAS_ERROR_INVALID_HANDLE;
  454. #ifdef DEBUG_FILE_IO
  455. EAS_ReportX(_EAS_SEVERITY_NOFILTER, "EAS_HWFileSeek: Seeking to new position %d\n", file->filePos);
  456. #endif
  457. /* is new position in current buffer? */
  458. newIndex = position - file->filePos + file->readIndex;
  459. if ((newIndex >= 0) && (newIndex < file->bytesInBuffer))
  460. {
  461. file->readIndex = newIndex;
  462. file->filePos = position;
  463. return EAS_SUCCESS;
  464. }
  465. /* save new position and reset buffer info so EAS_HWGetByte doesn't fail */
  466. file->filePos = position;
  467. file->bytesInBuffer = 0;
  468. file->readIndex = 0;
  469. return EAS_SUCCESS;
  470. }
  471. /*----------------------------------------------------------------------------
  472. *
  473. * EAS_HWFileSeekOfs
  474. *
  475. * Seek forward or back relative to the current position
  476. *
  477. *----------------------------------------------------------------------------
  478. */
  479. /*lint -esym(715, hwInstData) hwInstData available for customer use */
  480. EAS_RESULT EAS_HWFileSeekOfs (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_I32 position)
  481. {
  482. EAS_I32 temp;
  483. #ifdef DEBUG_FILE_IO
  484. EAS_ReportX(_EAS_SEVERITY_NOFILTER, "EAS_HWFileSeekOfs: Seeking to new position %d\n", file->filePos + position);
  485. #endif
  486. /* check handle integrity */
  487. if (file->pFile == NULL)
  488. return EAS_ERROR_INVALID_HANDLE;
  489. /* is new position in current buffer? */
  490. temp = position + file->readIndex;
  491. if ((temp >= 0) && (temp < file->bytesInBuffer))
  492. {
  493. file->readIndex = temp;
  494. file->filePos += position;
  495. return EAS_SUCCESS;
  496. }
  497. /* save new position and reset buffer info so EAS_HWGetByte doesn't fail */
  498. file->filePos += position;
  499. file->bytesInBuffer = 0;
  500. file->readIndex = 0;
  501. return EAS_SUCCESS;
  502. }
  503. /*----------------------------------------------------------------------------
  504. *
  505. * EAS_HWFileLength
  506. *
  507. * Return the file length
  508. *
  509. *----------------------------------------------------------------------------
  510. */
  511. /*lint -esym(715, hwInstData) hwInstData available for customer use */
  512. EAS_RESULT EAS_HWFileLength (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_I32 *pLength)
  513. {
  514. long pos;
  515. /* check handle integrity */
  516. if (file->pFile == NULL)
  517. return EAS_ERROR_INVALID_HANDLE;
  518. if ((pos = ftell(file->pFile)) == -1L)
  519. return EAS_ERROR_FILE_LENGTH;
  520. if (fseek(file->pFile, 0L, SEEK_END) != 0)
  521. return EAS_ERROR_FILE_LENGTH;
  522. if ((*pLength = ftell(file->pFile)) == -1L)
  523. return EAS_ERROR_FILE_LENGTH;
  524. if (fseek(file->pFile, pos, SEEK_SET) != 0)
  525. return EAS_ERROR_FILE_LENGTH;
  526. return EAS_SUCCESS;
  527. }
  528. /*----------------------------------------------------------------------------
  529. *
  530. * EAS_HWDupHandle
  531. *
  532. * Duplicate a file handle
  533. *
  534. *----------------------------------------------------------------------------
  535. */
  536. EAS_RESULT EAS_HWDupHandle (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file, EAS_FILE_HANDLE* pDupFile)
  537. {
  538. EAS_HW_FILE *dupfile;
  539. int i;
  540. /* check handle integrity */
  541. *pDupFile = NULL;
  542. if (file->pFile == NULL)
  543. return EAS_ERROR_INVALID_HANDLE;
  544. /* find an empty entry in the file table */
  545. dupfile = hwInstData->files;
  546. for (i = 0; i < EAS_MAX_FILE_HANDLES; i++)
  547. {
  548. /* is this slot being used? */
  549. if (dupfile->pFile == NULL)
  550. {
  551. /* copy info from the handle to be duplicated */
  552. dupfile->filePos = file->filePos;
  553. dupfile->pFile = file->pFile;
  554. /* set the duplicate handle flag */
  555. dupfile->dup = file->dup = EAS_TRUE;
  556. /* initialize some values */
  557. dupfile->bytesInBuffer = 0;
  558. dupfile->readIndex = 0;
  559. *pDupFile = dupfile;
  560. return EAS_SUCCESS;
  561. }
  562. dupfile++;
  563. }
  564. /* too many open files */
  565. return EAS_ERROR_MAX_FILES_OPEN;
  566. }
  567. /*----------------------------------------------------------------------------
  568. *
  569. * EAS_HWClose
  570. *
  571. * Wrapper for fclose function
  572. *
  573. *----------------------------------------------------------------------------
  574. */
  575. EAS_RESULT EAS_HWCloseFile (EAS_HW_DATA_HANDLE hwInstData, EAS_FILE_HANDLE file1)
  576. {
  577. EAS_HW_FILE *file2,*dupFile;
  578. int i;
  579. /* check handle integrity */
  580. if (file1->pFile == NULL)
  581. return EAS_ERROR_INVALID_HANDLE;
  582. /* check for duplicate handle */
  583. if (file1->dup)
  584. {
  585. dupFile = NULL;
  586. file2 = hwInstData->files;
  587. for (i = 0; i < EAS_MAX_FILE_HANDLES; i++)
  588. {
  589. /* check for duplicate */
  590. if ((file1 != file2) && (file2->pFile == file1->pFile))
  591. {
  592. /* is there more than one duplicate? */
  593. if (dupFile != NULL)
  594. {
  595. /* clear this entry and return */
  596. file1->pFile = NULL;
  597. return EAS_SUCCESS;
  598. }
  599. /* this is the first duplicate found */
  600. dupFile = file2;
  601. }
  602. file2++;
  603. }
  604. /* there is only one duplicate, clear the dup flag */
  605. if (dupFile)
  606. dupFile->dup = EAS_FALSE;
  607. else
  608. /* if we get here, there's a serious problem */
  609. return EAS_ERROR_HANDLE_INTEGRITY;
  610. /* clear this entry and return */
  611. file1->pFile = NULL;
  612. return EAS_SUCCESS;
  613. }
  614. /* no duplicates - close the file */
  615. if (fclose(file1->pFile) != 0)
  616. return EAS_ERROR_CLOSE_FAILED;
  617. /* clear this entry and return */
  618. file1->pFile = NULL;
  619. return EAS_SUCCESS;
  620. }
  621. /*----------------------------------------------------------------------------
  622. *
  623. * EAS_HWVibrate
  624. *
  625. * Turn on/off vibrate function
  626. *
  627. *----------------------------------------------------------------------------
  628. */
  629. /*lint -esym(715, hwInstData) hwInstData available for customer use */
  630. EAS_RESULT EAS_HWVibrate (EAS_HW_DATA_HANDLE hwInstData, EAS_BOOL state)
  631. {
  632. EAS_ReportX(_EAS_SEVERITY_NOFILTER, "Vibrate state: %d\n", state);
  633. return EAS_SUCCESS;
  634. }
  635. /*----------------------------------------------------------------------------
  636. *
  637. * EAS_HWLED
  638. *
  639. * Turn on/off LED
  640. *
  641. *----------------------------------------------------------------------------
  642. */
  643. /*lint -esym(715, hwInstData) hwInstData available for customer use */
  644. EAS_RESULT EAS_HWLED (EAS_HW_DATA_HANDLE hwInstData, EAS_BOOL state)
  645. {
  646. EAS_ReportX(_EAS_SEVERITY_NOFILTER, "LED state: %d\n", state);
  647. return EAS_SUCCESS;
  648. }
  649. /*----------------------------------------------------------------------------
  650. *
  651. * EAS_HWBackLight
  652. *
  653. * Turn on/off backlight
  654. *
  655. *----------------------------------------------------------------------------
  656. */
  657. /*lint -esym(715, hwInstData) hwInstData available for customer use */
  658. EAS_RESULT EAS_HWBackLight (EAS_HW_DATA_HANDLE hwInstData, EAS_BOOL state)
  659. {
  660. EAS_ReportX(_EAS_SEVERITY_NOFILTER, "Backlight state: %d\n", state);
  661. return EAS_SUCCESS;
  662. }
  663. /*----------------------------------------------------------------------------
  664. *
  665. * EAS_HWYield
  666. *
  667. * This function is called periodically by the EAS library to give the
  668. * host an opportunity to allow other tasks to run. There are two ways to
  669. * use this call:
  670. *
  671. * If you have a multi-tasking OS, you can call the yield function in the
  672. * OS to allow other tasks to run. In this case, return EAS_FALSE to tell
  673. * the EAS library to continue processing when control returns from this
  674. * function.
  675. *
  676. * If tasks run in a single thread by sequential function calls (sometimes
  677. * call a "commutator loop"), return EAS_TRUE to cause the EAS Library to
  678. * return to the caller. Be sure to check the number of bytes rendered
  679. * before passing the audio buffer to the codec - it may not be filled.
  680. * The next call to EAS_Render will continue processing until the buffer
  681. * has been filled.
  682. *
  683. *----------------------------------------------------------------------------
  684. */
  685. /*lint -esym(715, hwInstData) hwInstData available for customer use */
  686. EAS_BOOL EAS_HWYield (EAS_HW_DATA_HANDLE hwInstData)
  687. {
  688. /* put your code here */
  689. return EAS_FALSE;
  690. }