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