/*
 *               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 <stdio.h>
#include <stdlib.h>

typedef unsigned int u32;
typedef signed int s32;
typedef s32 IOSMessage;
typedef u32 IOSTime;

typedef struct IOSContext {
    u32 cpsr;
    u32 r[13];
    u32 sp;
    u32 lr;
    u32 pc;
} IOSContext;

typedef struct IOSThread {
    IOSContext context;
    struct IOSThread *next;
    s32 priority;
    u32 state;
    u32 pid;
    u32 attribute;
    void *val;
    struct IOSThread *joinQueue;
    struct IOSThread **queue;
    IOSContext extra;
#ifdef IOS_STACK_CHECK
    u32 stackBottom;
#endif
} IOSThread;
#define IOS_MESSAGE_QUEUE_MAX 64
typedef struct {
    IOSThread *emptyQ;
    IOSThread *fullQ;
    u32 owner;
    s32 count;
    s32 rptr;
    s32 size;
    IOSMessage *buffer;
} IOSMessageQueue;


typedef struct IOSTimer {
    IOSTime value;
    IOSTime interval;
    IOSMessageQueue *mq;
    IOSMessage mesg;
    u32 owner;

    struct IOSTimer *prev;
    struct IOSTimer *next;
} IOSTimer;

#define IOS_TIMER_MAX 32
static IOSTimer __timers[IOS_TIMER_MAX];

char *__processes[] = {
    "kernel", "eticket", "fs", "di", "oh0", "oh1", "sdi", "usbeth", "net", "kd", "wd", "wl", "kd", "ncd", "stm", "ppcboot"
};
u32 nqueues;

int
initTimers(FILE *fp)
{
  nqueues = fread(__timers, sizeof(IOSTimer), IOS_TIMER_MAX, fp); 
 if (nqueues <= 0) {
     printf("Did not find any timers in the file\n");
     return -1;
 }

 /* Convert from be to le */
 {
     int i;
     u32 *tmp = (u32 *) __timers;
     for ( i = 0; i < sizeof(__timers)/sizeof(u32); ++i) {
         *tmp =  (((*tmp) & 0xff)    << 24) |
             (((*tmp) & 0xff00)  << 8)  |
             (((*tmp) & 0xff0000) >> 8) |
             (((*tmp) & 0xff000000) >> 24);
         tmp = ((u32 *)__timers) + i;
     }
 }
 return 0;
}

void
__iosGetTimerUsage(int i)
{
    IOSTimer *t = __timers + i;
    printf("Timer %d [%p]: ", i, t);
    if (!t->mq) { 
        printf("Unused\n");
        return;
    }
    printf("\n");
    printf("  value: %d, interval: %d\n", t->value, t->interval);
    printf("  mesg: %08x, mq: %p\n", t->mesg, t->mq);
    printf("  prev: %p, next: %p\n", t->prev, t->next);
    printf("  owner: %d (%s)\n", t->owner, __processes[t->owner]);
}

void
__iosGetTimersUsage(void)
{
    int i;

    for (i = 0; i < IOS_TIMER_MAX; ++i) {
        __iosGetTimerUsage(i);
    }
    printf("\n");
}

int
main(int argc, char *argv[])
{
    FILE *fp;
    if (argc < 2) {
        printf("Usage: %s <file>\n", argv[0]);
        printf("Total size of timers area = %u bytes\n", IOS_TIMER_MAX*sizeof(IOSTimer));
        goto done;
    }
    fp = fopen(argv[1],"r");

    if (initTimers(fp) < 0) goto done;
    __iosGetTimersUsage();
done:
    return 0;
}
