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

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

#define NUM 4
#define BRKCHAIN_NUM 4
#define NUM1 4
IOSIobDebugInfo d;

static void 
dumpDataRef(void)
{
  u8 *str = (char *) 0x1780a300;
  int i;
  static int dumpnum=0;

  printf("dump num %d\n", dumpnum++);
  for (i=0; i<60; i++)
  {
      if (str[i] != 0)
          printf("index = %d, ref = %d\n", i, str[i]);
  }
}

static void 
alloctst(u32 size, u32 num)
{
  int i;
  IOSIobuf *p[64];
    
  // num < 64
  for (i = 0; i < num; ++i ) {
      p[i]  = IOS_AllocIob(POOL_DEFAULT, size, d);
      if ( NULL == p[i]) {
          printf("allocating iobuf of size %u: FAILED\n", size);
      }
      else
          printf("allocating %d iobuf of size %u: %p PASSED\n", i, size, p[i]);
  }
  for (i = 0; i < num; ++i ) {
      IOS_FreeIob(p[i]);
      printf("freed iobuf: %p\n", p[i]);
  }
}

void
iob_allocfree(void)
{
    alloctst(24, 4);
    alloctst(128, 32);
    alloctst(512, 8 );
    alloctst(1024,4 );
    alloctst(1500, 4 );
    alloctst(2000, 8);
    return;
}

void
iob_breakchain(void)
{
    IOSIobuf *p[BRKCHAIN_NUM], *chain;
    int i;
    int size = 128;

    printf("\n\niob_breakchain test\n\n");
    
    printf("Will allocate %d buffers of size: %d bytes\n", BRKCHAIN_NUM, size);
    IOS_DebugDumpIobFreeBufsList(0, size);
    for ( i = 0; i < BRKCHAIN_NUM; ++i ) {
      p[i] = IOS_AllocIob(POOL_DEFAULT, size, d);
      if ( NULL == p[i] ) {
          printf("allocation of iob failed\n");
          goto done;
      }
  } 
  chain = p[0];
  for ( i = 1; i < NUM; ++i ) 
      IOS_ENCHAIN_IOB(chain, p[i]);
  IOS_DebugDumpIobChain(chain);

  printf("Will break chain [head %p] at buffer 3 [iob = %p]\n", chain, p[2]);
  IOS_BreakIobChain(chain, p[2]);
  printf("Main chain: ");
  IOS_DebugDumpIobChain(chain);
  printf("New chain starting at %p: ", p[2]);
  IOS_DebugDumpIobChain(p[2]);

  printf("Recreating original chain\n");
  IOS_EnchainIob(chain, p[2]);
  printf("New main chain: ");
  IOS_DebugDumpIobChain(chain);

  printf("Breaking chain at head %p\n", p[0]);
  chain = IOS_BreakIobChain(chain, p[0]);
  printf("Main chain: ");
  IOS_DebugDumpIobChain(chain);
  printf("New chain made of only head %p: ", p[0]);
  IOS_DebugDumpIobChain(p[0]);
  
  printf("Recreating original chain\n");
  IOS_EnchainIob(p[0], chain);
  chain = p[0];
  printf("Recreated main chain: ");
  IOS_DebugDumpIobChain(chain);

  printf("Breaking chain at last element %p\n", p[NUM - 1]);
  IOS_BreakIobChain(chain, p[NUM - 1]);
  printf("Main chain: ");
  IOS_DebugDumpIobChain(chain);
  printf("New chain made of only tail %p: ", p[NUM - 1]);
  IOS_DebugDumpIobChain(p[NUM - 1]);

  IOS_FreeIobChain(chain);
  IOS_FreeIobChain(p[BRKCHAIN_NUM-1]);
done:
  return;
}

void
iob_chain(void)
{
  IOSIobuf *p[NUM], *chain;
  int i;
  int size = 35;

  printf("\n\niob_chain test\n\n");
  
  printf("Will allocate %d buffers of size: %d bytes\n", NUM, size);
  for ( i = 0; i < NUM; ++i ) {
      p[i] = IOS_AllocIob(POOL_DEFAULT, size, d);
      if ( NULL == p[i] ) {
          printf("allocation of iob failed\n");
          goto done;
      }
  } 
  chain = p[0];
  for ( i = 1; i < NUM; ++i ) 
      IOS_ENCHAIN_IOB(chain, p[i]);
  IOS_DebugDumpIobChain(chain);
  IOS_FreeIobChain(chain);
done:
  return;
}

static void
makeChain(IOSIobuf **p, IOSIobuf **chain)
{
  int i;
  *chain = NULL;
  for ( i = 0; i < NUM1; ++i ) {
      p[i] = IOS_AllocIob(POOL_DEFAULT, 128, d);
      if ( NULL == p[i] ) {
          printf("allocation of iob failed\n");
          goto done;
      }
  } 
  *chain = p[0];
  for ( i = 1; i < NUM1; ++i ) 
      IOS_ENCHAIN_IOB(*chain, p[i]);
done:
  return;
}

void
iob_queue(void)
{
  IOSIobuf *p[NUM1], *chain0 = NULL;
  IOSIobuf *q[NUM1], *chain1 = NULL;
  IOSIobuf *r[NUM1], *chain2 = NULL;

  printf("\n\niob_queue test\n\n");
  
  printf("*** creating queue of 3 chains \n");
  makeChain(p, &chain0);
  makeChain(q, &chain1);
  makeChain(r, &chain2);
  IOS_ENQUEUE_IOB(chain0, chain1);
  IOS_ENQUEUE_IOB(chain0, chain2);
  IOS_DebugDumpIobQueue(chain0);

  printf("*** de-queueing chain1 from queue \n");
  chain0 = IOS_DEQUEUE_IOB(chain0, chain1);
  IOS_DebugDumpIobQueue(chain0);
 
  printf("*** pre-queueing chain1 to queue \n");
  IOS_PREQUEUE_IOB(chain0, chain1);
  IOS_DebugDumpIobQueue(chain1);

  IOS_FreeIobChain(chain0);
  IOS_FreeIobChain(chain1);
  IOS_FreeIobChain(chain2);
  return;
}

void
iob_freechain(void)
{
  IOSIobuf *p[NUM], *chain;
  int i;
  int size = 35;
  printf("Will allocate %d buffers of size: %d bytes\n", NUM, size);
  for ( i = 0; i < NUM; ++i ) {
      p[i] = IOS_AllocIob(POOL_DEFAULT, size, d);
      if ( NULL == p[i] ) {
          printf("allocation of iob failed\n");
          goto done;
      }
  } 
  chain = p[0];
  for ( i = 1; i < NUM; ++i ) 
      IOS_ENCHAIN_IOB(chain, p[i]);
  IOS_DebugDumpIobChain(chain);
  IOS_FreeIobChain(chain);
  IOS_DebugDumpIobChain(chain);
done:
  return;
}

void
iob_ref(void)
{
  IOSIobuf *iob1, *iob2, *iob3;
  
  printf("\n\niob_ref test\n\n");
  
  if (IOS_ERROR_OK != IOS_AccessIobPool(POOL_DEFAULT)) {
      printf("getting access to iobuf pool failed\n");
      goto done;
  }

  IOS_DebugDumpIobFreeBufsList(0,256);
  printf("\n");

  iob1 = IOS_AllocIob(POOL_DEFAULT, 256, d);
  if ( NULL == iob1 ) {
      printf("allocating iobuf of size 256: FAILED\n");
  }
  else
      printf("allocating iobuf iob1 (%p)  of size 256: PASSED\n", iob1);
  
  iob2 = IOS_AllocIob(POOL_DEFAULT, 256, d);
  if ( NULL == iob2 ) {
      printf("allocating iobuf of size 256: FAILED\n");
  }
  else
      printf("allocating iobuf iob2 (%p) of size 256: PASSED\n", iob2);
  
  iob3 = IOS_AllocIob(POOL_DEFAULT, 256, d);
  if ( NULL == iob3 ) {
      printf("allocating iobuf of size 256: FAILED\n");
  }
  else
      printf("allocating iobuf iob3 (%p) of size 256: PASSED\n", iob3);

  printf("\n");
  printf("iob1 ref (%d), iob2 ref (%d), iob3 ref (%d)\n", iob1->users, iob2->users, iob3->users);

  printf("\n");
  printf("increase ref count of iob2 by 1, and iob3 by 2\n");
  IOS_IOB_INCREF(iob2);
  IOS_IOB_INCREF(iob3);
  IOS_IOB_INCREF(iob3);
  printf("iob1 ref = %d, iob2 ref = %d, iob3 ref = %d\n", iob1->users, iob2->users, iob3->users);

  printf("\n");
  printf("call FreeIob on all three iob\n");
  IOS_FreeIob(iob1);
  IOS_FreeIob(iob2);
  IOS_FreeIob(iob3);
  printf("iob2 ref = %d, iob3 ref = %d\n", iob2->users, iob3->users);
  IOS_DebugDumpIobFreeBufsList(0,256);

  printf("\n");
  printf("call FreeIob on iob2 and iob3\n");
  IOS_FreeIob(iob2);
  IOS_FreeIob(iob3);
  printf("iob3 ref (%d)\n", iob3->users);
  IOS_DebugDumpIobFreeBufsList(0,256);

  printf("\n");
  printf("call FreeIob on iob3\n");
  IOS_FreeIob(iob3);
  IOS_DebugDumpIobFreeBufsList(0,256);

done:
  return;
}

void
iob_data(void)
{
  IOSIobuf *iob;
  u8* data;
 
  printf("\n\niob_data test\n\n");
  
  if (IOS_ERROR_OK != IOS_AccessIobPool(POOL_DEFAULT)) {
      printf("getting access to iobuf pool failed\n");
      goto done;
  }

  iob = IOS_AllocIob(POOL_DEFAULT, 256, d);
  if ( NULL == iob ) {
      printf("allocating iobuf of size 256: FAILED\n");
  }
  else
      printf("allocating iobuf iob (%p)  of size 256: PASSED\n", iob);
  
  printf("iob: head(%p), buflen(%d), data(%p), datalen(%d)\n", iob->head, 
      iob->bufLen, iob->data, iob->dataLen);

  printf("\n");
  printf("reserve 128 bytes\n");
  IOS_IOB_RESERVE(iob, 128);
  printf("iob: head(%p), buflen(%d), data(%p), datalen(%d)\n", iob->head, 
      iob->bufLen, iob->data, iob->dataLen);

  printf("\n");
  printf("put 64 bytes\n");
  data = IOS_PutIob(iob, 64);
  printf("data = %p\n", data);
  printf("iob: head(%p), buflen(%d), data(%p), datalen(%d)\n", iob->head, 
      iob->bufLen, iob->data, iob->dataLen);

  printf("\n");
  printf("push 64 bytes\n");
  data = IOS_PushIob(iob, 64);
  printf("data = %p\n", data);
  printf("iob: head(%p), buflen(%d), data(%p), datalen(%d)\n", iob->head, 
      iob->bufLen, iob->data, iob->dataLen);

  printf("\n");
  printf("pull 64 bytes\n");
  data = IOS_PullIob(iob, 64);
  printf("data = %p\n", data);
  printf("iob: head(%p), buflen(%d), data(%p), datalen(%d)\n", iob->head, 
      iob->bufLen, iob->data, iob->dataLen);

  IOS_FreeIob(iob);

done:
  return;
}

void
iob_share(void)
{
  IOSIobuf *iob1, *iob2;
  char *str;

  printf("\n\niob_share\n\n");
  
  if (IOS_ERROR_OK != IOS_AccessIobPool(POOL_DEFAULT)) {
      printf("getting access to iobuf pool failed\n");
      goto done;
  }

  iob1 = IOS_AllocIob(POOL_DEFAULT, 256, d);
  if ( NULL == iob1 ) {
      printf("allocating iobuf of size 256: FAILED\n");
  }
  else
      printf("allocating iobuf iob1 (%p)  of size 256: PASSED\n", iob1);

  printf("\n");
  //printf("iob1 data buffer ref count = %d\n", IOS_GetIobDataRef(iob1));
  printf("write 0123456789 into iob1 data buffer\n");

  str = (char *) IOS_PutIob(iob1, 24);
  str[0] = '0';
  str[1] = '1';
  str[2] = '2';
  str[3] = '3';
  str[4] = '4';
  str[5] = '5';
  str[6] = '6';
  str[7] = '7';
  str[8] = '8';
  str[9] = '9';
  str[10] = 0;
  printf("iob1 data buffer = %s\n", str);

  printf("\n");
  printf("clone iob1 to iob2\n");
  iob2 = IOS_CloneIob(iob1);
  //printf("iob1 data buffer ref count = %d\n", IOS_GetIobDataRef(iob1));

  printf("\n");
  printf("dump iob1 headers\n");
  printf("head = %p, bufLen = %d, data = %p, dataLen = %d\n",
      iob1->head, iob1->bufLen, iob1->data, iob1->dataLen);

  printf("dump iob2 headers\n");
  printf("head = %p, bufLen = %d, data = %p, dataLen = %d\n",
      iob2->head, iob2->bufLen, iob2->data, iob2->dataLen);
  printf("iob2 data buffer = %s\n", str);

  printf("\n");
  printf("call FreeIob on iob1\n");
  IOS_FreeIob(iob1);
  printf("iob2 data buffer = %s\n", str);
  //printf("iob2 data buffer ref count = %d\n", IOS_GetIobDataRef(iob2));

  printf("\n");
  printf("call FreeIob on iob2\n");
  IOS_FreeIob(iob2);
  printf("iob2 data buffer = %s\n", str);
done:
  return;
}

void
iob_verify(void)
{
  IOSIobuf *iob1, *iob2;
  
  printf("\n\niob_verify\n\n");
  
  if (IOS_ERROR_OK != IOS_AccessIobPool(POOL_DEFAULT)) {
      printf("getting access to iobuf pool failed\n");
      goto done;
  }

  iob1 = IOS_AllocIob(POOL_DEFAULT, 256, d);
  if ( NULL == iob1 ) {
      printf("allocating iobuf of size 256: FAILED\n");
  }
  else
      printf("allocating iobuf iob1 (%p)  of size 256: PASSED\n", iob1);

  printf("\n");
  printf("clone iob1 to iob2\n");
  iob2 = IOS_CloneIob(iob1);

  printf("\n");
  printf("iob1 valid = %d, iob2 valid = %d\n", IOS_IsValidIob(iob1), IOS_IsValidIob(iob2));

  printf("\n");
  printf("free iob1 (validity of iob1 should now be %d)\n", IOS_IsValidIob(NULL));
  IOS_FreeIob(iob1);
  printf("iob1 valid = %d, iob2 valid = %d\n", IOS_IsValidIob(iob1), IOS_IsValidIob(iob2));

  printf("\n");
  printf("set iob1 to NULL\n");
  iob1 = NULL;
  printf("iob1 valid = %d, iob2 valid = %d\n", IOS_IsValidIob(iob1), IOS_IsValidIob(iob2));

  printf("\n");
  printf("iob2: reserve 128\n");
  IOS_IOB_RESERVE(iob2, 128);
  printf("head = %p, bufLen = %d, data = %p, dataLen = %d\n",
      iob2->head, iob2->bufLen, iob2->data, iob2->dataLen);
  printf("iob2 valid = %d\n", IOS_IsValidIob(iob2));

  printf("\n");
  printf("iob2: push 256\n");
  IOS_PushIob(iob2, 256);
  printf("head = %p, bufLen = %d, data = %p, dataLen = %d\n",
      iob2->head, iob2->bufLen, iob2->data, iob2->dataLen);
  printf("iob2 valid = %d\n", IOS_IsValidIob(iob2));

  printf("\n");
  printf("iob2: force pushing 256 (should fail)\n");
  iob2->data -= 256;
  printf("head = %p, bufLen = %d, data = %p, dataLen = %d\n",
      iob2->head, iob2->bufLen, iob2->data, iob2->dataLen);
  printf("iob2 valid = %d\n", IOS_IsValidIob(iob2));

  IOS_FreeIob(iob2);

done:
  return;
}

void
main(void)
{
    if (IOS_ERROR_OK != IOS_AccessIobPool(POOL_DEFAULT)) {
        printf("getting access to iobuf pool failed\n");
        goto done;
    }
    iob_allocfree();
    iob_breakchain();
    iob_chain();
    iob_queue();
    iob_data();
    iob_ref();
    iob_share();
    iob_verify();
    dumpDataRef();
done:
    IOS_DestroyThread(0, NULL);
}
