/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */
#include <iostypes.h>
#include <ios.h>
#include <iop.h>
#include <ioslibc.h>

#define STACK_SIZE 2048
const u8        _initStack[STACK_SIZE];
const u32       _initStackSize = sizeof(_initStack);
const u32       _initPriority = 20;

#include "iosusb.h"
#define ASYNC_MAX 32 /* should match IOS_ASYNC_PER_THREAD_MAX in ios/resource.c */

void
main(u32 arg)
{
    IOSError rc;
    IOSFd fd;
    IOSMessage msgQ[1];
    IOSMessageQueueId qid;
    IOSResourceRequest reply; 
    int i;
    qid = IOS_CreateMessageQueue(msgQ, 1);
    
    fd = IOS_Open("/dev/usb/oh0", 0);
    if (fd < 0) {
        printf("open failed: %d\n", fd);
        goto out;
    }
    for (i = 0; i < ASYNC_MAX; ++i) {
        rc = IOS_IoctlAsync(fd, OHC_IPCTEST_MSG, 0, 0, 0, 0, qid, &reply);
        printf("ioctl async [%d] returned %d\n", i, rc);
    }
    rc = IOS_IoctlAsync(fd, OHC_IPCTEST_MSG, 0, 0, 0, 0, qid, &reply);
    if (rc != IOS_ERROR_MAX) {
        printf("FAILED: async calls > limit (%d) did not returned expected error: %d\n",
                ASYNC_MAX, IOS_ERROR_MAX);
    } else {
        printf("PASSED: async call %d returned expected error: %d\n", ASYNC_MAX, IOS_ERROR_MAX);
    }
out:
    return;
}
