/*
 *               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 "hw.h"
#include "ios.h"
#include "ioslibc.h"

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

extern u32 timerIntrCnt;

#define WAIT_LIMIT 20
int ctr = 0;
#define ITER_LIMIT WAIT_LIMIT

u32 timeStart[WAIT_LIMIT];
u32 timeStop[WAIT_LIMIT];
IOSMessage message[WAIT_LIMIT];
	
void
main(u32 arg)
{
    IOSMessageQueueId mq;
    IOSMessage array[4];
    IOSMessage m;
    int i = 0;
    IOSTimerId t1, t2, t3;

    printf("Creating message queue\n");
    mq = IOS_CreateMessageQueue(array, 4);
    printf("Set one-time timer [0xbabecafe]\n");
    t1 = IOS_CreateTimer(300, 0, mq, 0xbabecafe);
    printf("Set periodic timer [0xdeadbeef]\n");
    t2 = IOS_CreateTimer(0, 1000, mq, 0xdeadbeef);
    printf("Set one-time timer [0xbeeff00d]\n");
    t3 = IOS_CreateTimer(10000, 0, mq, 0xbeeff00d);

    /* 
     * Timer values must be stored in an array, because the 'printf' statements
     *   take much longer than the timer intervals. 
     *
     * Each 'printf' statement is on the scale of 300,000 ticks.
     */

    do {
      //        printf("waiting for timer: %d @ %u\n", i++, IOS_GetTimer());
      timeStart[i] = IOS_GetTimer();
      IOS_ReceiveMessage(mq, &m, IOS_MESSAGE_BLOCK);
      //        printf("Received timer message %x @ %u\n", m, IOS_GetTimer());
      timeStop[i] = IOS_GetTimer();
      message[i] = m;
      if (m == 0xbeeff00d) break;
      i++;
    } while (i < WAIT_LIMIT);
    if (i >= WAIT_LIMIT) {
      printf("FAIL:  0xbeeff00d not received past %d messages\n", WAIT_LIMIT);
      exit(-1);
    }

    for(i = 0; i < WAIT_LIMIT; i ++) {
      printf("waiting for timer: %d @ %u\n", i, timeStart[i]);
      printf("Received timer message %x @ %u\n", message[i], timeStop[i]);
      if(message[i] == 0xbeeff00d) break;
    }
    printf("Destroy all timers\n");
    IOS_DestroyTimer(t1);
    IOS_DestroyTimer(t2);
    IOS_DestroyTimer(t3);
    printf("Allocate new timer and see if we re-use ID\n");
    t2 = IOS_CreateTimer(30, 0, mq, 0xbabecafe);
    if (t1 != t2) {
        printf("Expected timer ID: %d, got %d\n", t1, t2);
        exit(-1);
    }
    IOS_DestroyTimer(t2);
    printf("pass\n");

    t1 = IOS_CreateTimer(5000, 0, mq, 0xbabecafe);
    t2 = IOS_CreateTimer(5000, 0, mq, 0xbabecafe);
    t3 = IOS_CreateTimer(5000, 0, mq, 0xdeadbeef);
    IOS_DestroyTimer(t3);
    t3 = IOS_CreateTimer(5000, 0, mq, 0xdeadbeef);
    if (t3 == t1 || t3 == t2) {
        printf("timer ID %d was re-used\n", t3);
        exit(-1);
    }

    i = 0;
    printf("Set periodic timer [0xdeadbeef]\n");
    t2 = IOS_CreateTimer(0, 100000, mq, 0xdeadbeef);
    do {
      timeStart[ctr] = IOS_GetTimer();
      IOS_ReceiveMessage(mq, &m, IOS_MESSAGE_BLOCK);
      timeStop[ctr] = IOS_GetTimer();
      message[ctr] = m;
      ctr ++;
    } while (ctr < ITER_LIMIT);

    for(i = 0; i < WAIT_LIMIT; i ++) {
      printf("waiting for timer: %d @ %u\n", i, timeStart[i]);
      printf("Received timer message %x @ %u\n", message[i], timeStop[i]);
    }
    exit(0);
}
