/*      
  _____     ___ ____ 
   ____|   |    ____|      PSX2 OpenSource Project
  |     ___|   |____       (c) 2008 jMk0Nm 
------------------------------------------------------------------------

 File: main.c
 Version: 1.0
 
------------------------------------------------------------------------ 
*/

#include "mcsp.h"


int __attribute__((unused)) shutdown() { return 0; }

/* function declaration */
void rpcMainThread(void* param);
void *rpcCommandHandler(int command, void *Data, int Size);

static SifRpcDataQueue_t Rpc_Queue __attribute__((aligned(64)));
static SifRpcServerData_t Rpc_Server __attribute((aligned(64)));
static int Rpc_Buffer[1024] __attribute((aligned(64)));

/* Description: Module entry point */
int _start(int argc, char **argv)
{
 iop_thread_t param;
 int id;

 
 printf("MCSP: IOP RPC Initialization.\n"); 
 /*create thread*/
 param.attr      = TH_C;
 param.thread    = rpcMainThread;
 param.priority  = 40;
 param.stacksize = 0x800;
 param.option    = 0;

 if((id = CreateThread(&param)) <= 0)
  return MODULE_NO_RESIDENT_END;

 StartThread(id,0);

 return MODULE_RESIDENT_END;
}

void rpcMainThread(void* param)
{
 SifInitRpc(0);
 SifSetRpcQueue(&Rpc_Queue, GetThreadId());
 SifRegisterRpc(&Rpc_Server, MCSP_IRX, (void *) rpcCommandHandler, (u8 *) &Rpc_Buffer, 0, 0, &Rpc_Queue);
 SifRpcLoop(&Rpc_Queue);
}

void *rpcCommandHandler(int command, void *Data, int Size)
{
	switch(command)
	{
		case 1:
			(void *)Data = MC_OSD_Scan((void *)Data);
		break;
		case 2:
			(void *)Data = MC_Dummies_Patch((void *)Data);		
		break;
		case 3:
			(void *)Data = MC_Dummies_UnPatch((void *)Data);		
		break;		
	}

	return Data;
}
