/*
 * debugging macros
 */

int itest = 0;

#define TEST(expect,msg) TEST2(rv == expect, msg)

#define TEST2(expr,msg)                                                 \
    {                                                                   \
        itest++;                                                        \
        if ( !(expr) ) {                                                \
            APP_LOG2("%d: %s: " #expr ": rv = %d (0x%08x)\n",itest,msg,rv,rv); \
            goto out;                                                   \
        }                                                               \
    }

#define APP_DEBUG 

#ifdef APP_DEBUG
#define APP_LOG(...) printf("APP: " __VA_ARGS__)
#define APP_LOG_CONT(...) printf(__VA_ARGS__)
#define APP_LOG2(...) printf("APP: " __VA_ARGS__)
#define APP_ERROR(msg) printf("APP: Error: %s failed\n", msg)
#else
#define APP_LOG(...)
#define APP_LOG_CONT(...)
#define APP_LOG2(...) printf("APP: " __VA_ARGS__) 
//#define APP_ERROR(msg) /* printf("APP: Error: %s failed\n", msg) */
#define APP_ERROR(msg) printf("APP: Error: %s failed\n", msg)
#endif

void printTime(char *msg, s32 t)
{
    u32 tt = t;
    u32 t_sec_frac = tt % 1000; tt /= 1e3;
    u32 t_sec = tt % 60; tt /= 60;
    u32 t_min = tt % 60; tt /= 60;
    u32 t_hr = tt;

    printf("%s %d:%02d:%02d.%03d (%d)\n",msg,t_hr,t_min,t_sec,t_sec_frac,t);
}


    
int testProc(u32 arg) 
{
    IOSError rv;
    IOSFd fd;
    s32 offset;
    s32 i=0;
    s32 pos = 0;
    pos = 0;

#if 0
    float f=1.0;
    u32 *u;
    u = (u32*)&f;
    printf("float = 1.0 = 0x%08x\n",*u);
    f = 2.0;
    printf("float = 2.0 = 0x%08x\n",*u);
    return 0;
#endif
    
#if 0
    s32 val;
    val = MAX_S32;
    printf("max: %d %x\n",val,val);
    val = MIN_S32;
    printf("min: %d %x\n",val,val);
#endif

#if 0
    u8 teststr[] = "0123456789";
    printf("length of string \"%s\" is %d\n",teststr,sizeof(teststr));
    return 0;
#endif
    
    // good open
//    fd = rv = IOS_Open("/dev/media/mp3/music/VBRI_44S.mp3", O_RDONLY);
//    fd = rv = IOS_Open("/dev/media/mp3/music/us_blues.mp3", O_RDONLY);
//    fd = rv = IOS_Open("/dev/media/mp3/music/friend.mp3", O_RDONLY);
//    fd = rv = IOS_Open("/dev/media/mp3/music/bubble.mp3", O_RDONLY);
//    fd = rv = IOS_Open("/dev/media/mp3/music/closer.mp3", O_RDONLY);
//    fd = rv = IOS_Open("/dev/media/mp3/music/s1.mp3", O_RDONLY);
//    fd = rv = IOS_Open("/dev/media/mp3/music/closerl.mp3", O_RDONLY);
//    fd = rv = IOS_Open("/dev/media/mp3/music/t2.mp3", O_RDONLY);
    fd = rv = IOS_Open("/dev/media/mp3/music/l108.mp3", O_RDONLY); // rlo_lhi_s44.1_bf640_ms.mp3
//    fd = rv = IOS_Open("/dev/media/mp3/music/l72.mp3", O_RDONLY); // rlo_lhi_s44.1_bf129_ms.mp3
//    fd = rv = IOS_Open("/dev/media/mp3/music/l35.mp3", O_RDONLY); // rlo_lhi_s44.1_bc128_mm.mp3
//    fd = rv = IOS_Open("/dev/media/mp3/music/tid3v2.mp3", O_RDONLY);
//    fd = rv = IOS_Open("/dev/media/mp3/music/rlo_lhi.mp3", O_RDONLY);
    TEST2(rv>=0,"open of mp3 file");

#if 1
    // good seek
    rv = IOS_Seek(fd,0,IOS_SEEK_END);
    TEST2(rv>0,"seek to end");
    printTime("duration",rv);

    rv = IOS_Seek(fd,0,IOS_SEEK_SET);
    TEST(0,"seek to beginning");    
#endif

#if 1
    // good async play
    rv = IOS_Ioctl(fd,MP3_PLAY,0,0,0,0);
    TEST(IOS_ERROR_OK,"MP3 play");
    printf("good open: fd = 0x%08x\n",(u32)fd);
    
    while (1) {
        APP_LOG("*************************************** i=%d\n",i);
        s32 status;
        rv = IOS_Ioctl(fd,MP3_GET_STATUS,0,0,&status,sizeof(status));
        TEST(IOS_ERROR_OK,"status");
        if ( status <= 0 ) {
            APP_LOG("--------------- playback completed (%d) ----------------\n", status);
            break;
        } else {
            APP_LOG("playing continues\n");
        }

        
        if ( i == 0 ) {
            offset = 0;
            APP_LOG("seek current + %d\n",offset);
            rv = IOS_Seek(fd,offset,IOS_SEEK_CURRENT);
            TEST2(rv>0,"seek set");
            printTime("current",rv);

        } else if ( i == 9999 ) {
            APP_LOG("stopping ...\n");
            rv = IOS_Ioctl(fd,MP3_STOP,0,0,0,0);
            TEST(IOS_ERROR_OK,"stopping");

            printTime("current",rv);
            rv = IOS_Seek(fd,0,IOS_SEEK_CURRENT);
            TEST2(rv>0,"seek current");
            printTime("current",rv);

            offset = 39000;
            APP_LOG("seek set %d\n",offset);
            rv = IOS_Seek(fd,offset,IOS_SEEK_SET);
            TEST2(rv>0,"seek forward");
            printTime("new",rv);

            APP_LOG("restarting ...\n");
            rv = IOS_Ioctl(fd,MP3_PLAY,0,0,0,0);
            TEST(IOS_ERROR_OK,"restarting");

        } else if ( i == 19000 ) {
            APP_LOG("ending ...\n");
            rv = IOS_Ioctl(fd,MP3_STOP,0,0,0,0);
            TEST(IOS_ERROR_OK,"stopping");

            APP_LOG("closing\n");
            rv = IOS_Close(fd);
            TEST(IOS_ERROR_OK,rv);
            
        } else {
            APP_LOG("seek current + 0\n");
            rv = IOS_Seek(fd,0,IOS_SEEK_CURRENT);
            TEST2(rv>=0,"seek current");
            printTime("current",rv);
        }
        i++;
    }
#else
    // read
    u8 rbuf[4096];
    
    while (1) {
        APP_LOG("*************************************** i=%d\n",i);
        s32 status;
        status = IOS_Read(fd,rbuf,sizeof(rbuf));
        TEST(IOS_ERROR_OK,"status");
        if ( status <= 0 ) {
            APP_LOG("--------------- read completed (%d) ----------------\n", status);
            break;
        } else {
            int j, k, l;
            for (j=0; j<status; j+=16) {
                APP_LOG("0x%08x: ",pos+j);
                k = status - j;
                if ( k > 16 )
                    k = 16;
                for (l=0; l<k; l++) {
                    APP_LOG_CONT("%02x ",rbuf[j+l]);
                }
                APP_LOG_CONT("\n");
            }
            pos += status;
            APP_LOG("reading continues\n");
        }

        if ( i == 0 ) {
            offset = 0;
            APP_LOG("seek current + %d\n",offset);
            rv = IOS_Seek(fd,offset,IOS_SEEK_CURRENT);
            TEST2(rv>=0,"seek set");
            printTime("current",rv);

        } else if ( i == 999 ) {
            rv = IOS_Seek(fd,0,IOS_SEEK_CURRENT);
            TEST2(rv>=0,"seek current");
            printTime("current",rv);

            offset = 39000;
            APP_LOG("seek set %d\n",offset);
            rv = IOS_Seek(fd,offset,IOS_SEEK_SET);
            TEST2(rv>=0,"seek forward");
            printTime("new",rv);

        } else if ( i == 1000 ) {
            APP_LOG("closing\n");
            rv = IOS_Close(fd);
            TEST(IOS_ERROR_OK,rv);
            
        } else {
            APP_LOG("seek current + 0\n");
            rv = IOS_Seek(fd,0,IOS_SEEK_CURRENT);
            TEST2(rv>=0,"seek current");
            printTime("current",rv);
        }
        i++;
    }
#endif
    
//        APP_LOG("testProc: last msg = 0x%08x (seek=0x%08x, play=0x%08x, stop=0x%08x, ioctl=0x%08x)\n",
//               m, (u32)&seekReply, (u32)&playReply, (u32)&stopReply, (u32)&ioctlReply);
//        APP_LOG("          status = %d\n",((IOSResourceRequest *)m)->status);
    // good async play
//    APP_LOG("testProc: play again\n");
//    vol = MP3_VOLUME_MAX;
//    rv = IOS_Ioctl(fd,MP3_SET_VOLUME,&vol,sizeof(vol),0,0);
//    TEST(IOS_ERROR_OK,"sync vol");
//    rv = IOS_Ioctl(fd,MP3_PLAY,0,0,0,0);
//    TEST(IOS_ERROR_OK,"sync MP3 play");
    
out:
    printf("testProc: position = %d\n",pos);
    printf("testProc: exiting with rv=%d\n",rv);
    return rv;
}
