#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

#include <ios.h>
#include <iostypes.h>
#include <ioslibc.h>

#define MSGQ_SIZE 16
#define STACK_SIZE 16*1024
#define Q_MAX 8
#define NUM_TIMERS 7
IOSMessage msgQ[MSGQ_SIZE];

IOSMessageQueueId qid = -1;
void
timer1(u32 id)
{
  char msg[32];
  char *msg1;
  int i;
  sprintf(msg, "timer_msg");
  qid = IOS_CreateMessageQueue(msgQ, MSGQ_SIZE);
  if ( qid < 0 ) {
      goto done;
  }

  fprintf(stderr,"Setting timer for 2 seconds\n");
  if ( IOS_ERROR_MAX == IOS_CreateTimer(2000, 0, qid, (u32)msg ))
      fprintf(stderr,"Failed to set timer. All timers in use.\n");
  fprintf(stderr,"Setting timer for 3 seconds\n");
  if ( IOS_ERROR_MAX == IOS_CreateTimer(3000, 0, qid, (u32)msg ))
      fprintf(stderr,"Failed to set timer. All timers in use.\n");
  fprintf(stderr,"Setting timer for 4 seconds\n");
  if ( IOS_ERROR_MAX == IOS_CreateTimer(4000, 0, qid, (u32)msg ))
      fprintf(stderr,"Failed to set timer. All timers in use.\n");
  fprintf(stderr,"Setting timer for 5 seconds\n");
  if ( IOS_ERROR_MAX == IOS_CreateTimer(5000, 0, qid, (u32)msg ))
      fprintf(stderr,"Failed to set timer. All timers in use.\n");
  fprintf(stderr,"Setting timer for 6 seconds\n");
  if ( IOS_ERROR_MAX == IOS_CreateTimer(6000, 0, qid, (u32)msg ))
      fprintf(stderr,"Failed to set timer. All timers in use.\n");
  fprintf(stderr,"Setting timer for 7 seconds\n");
  if ( IOS_ERROR_MAX == IOS_CreateTimer(7000, 0, qid, (u32)msg ))
      fprintf(stderr,"Failed to set timer. All timers in use.\n");
  fprintf(stderr,"Setting timer for 15 seconds\n");
  if ( IOS_ERROR_MAX == IOS_CreateTimer(15000, 0, qid, (u32)msg ))
      fprintf(stderr,"Failed to set timer. All timers in use.\n");
  for ( i = 0; i < NUM_TIMERS; ++i) {
      IOS_ReceiveMessage(qid, (IOSMessage *) &msg1, IOS_MESSAGE_BLOCK);
      printf("Read timer msg %d: %s\n", i, msg1);
  } 
  fprintf(stderr,"[timer]: Timer list\n");
  IOS_DebugTimerListDump();

  fprintf(stderr,"[timer]: Timer list should be empty\n");
  IOS_DebugTimerListDump();
  
done:
  IOS_ThreadExit(0);
}
