/*****************************************************************************************
 Copyright (c) 2002-2004 The UbixOS Project
 All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:

 Redistributions of source code must retain the above copyright notice, this list of
 conditions, the following disclaimer and the list of authors.  Redistributions in binary
 form must reproduce the above copyright notice, this list of conditions, the following
 disclaimer and the list of authors in the documentation and/or other materials provided
 with the distribution. Neither the name of the UbixOS Project nor the names of its
 contributors may be used to endorse or promote products derived from this software
 without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 $Id$

*****************************************************************************************/

#include <ubixos/systemtask.h>
#include <ubixos/kpanic.h>
#include <ubixos/vitals.h>
#include <ubixos/exec.h>
#include <lib/kmalloc.h>
#include <lib/kprintf.h>
#include <sde/sde.h>
#include <sys/io.h>
#include <mpi/mpi.h>
#include <string.h>

void systemTask() {
  mpiMessage_t myMsg;
  uInt32       counter = 0x0;

  if (mpiCreateMbox("system") != 0x0) {
    kpanic("Error: Error creating mailbox: system\n");
    }

  while(1) {
    if (mpiFetchMessage("system",&myMsg) == 0x0) {
      switch(myMsg.type) {
        case 1000:
          kprintf("Restarting the system in 5 seconds\n");
          counter = systemVitals->sysUptime + 5;
          while (systemVitals->sysUptime < counter) {
            //asm("hlt");
            schedYield();
            }
          kprintf("Rebooting NOW!!!\n");
          while(inportByte(0x64) & 0x02);
          outportByte(0x64, 0xFE);
          break;
        case 31337:
           kprintf("system: backdoor opened\n");
           break;
        case 0x80:
           if (!kstrcmp(myMsg.data,"sdeStart")) {
             kprintf("Starting SDE\n");
             execThread(sdeThread,(uInt32)(kmalloc(0x2000)+0x2000),0x0);
             }
           break;
        default:
          kprintf("system: Received message %i:%s\n",myMsg.type,myMsg.data);
          break;
        }
      }
    asm("hlt");
    }
  return;
  }

/***
 $Log$
 Revision 1.1  2004/06/04 17:33:33  reddawg
 Changed idle task to system task

 Revision 1.9  2004/06/04 13:29:56  reddawg
 libc: modified mkdir(); interface
 kpanic: kPanic(); now says kPanic: %s
 system: now reboots when receives message for reboot
         also when command start sde is received by system the STD is started

 Revision 1.8  2004/05/26 23:48:33  reddawg
 idletask: tweaked reboot for performance

 Revision 1.7  2004/05/26 23:28:29  reddawg
 idletask: This task now also receives all of the systems message
           We also gained reboot suport that is msg send type 1000

 Revision 1.6  2004/05/25 18:42:28  reddawg
 Idle task now just does what it is suposed to

 Revision 1.5  2004/05/25 17:30:16  reddawg
 IDLE task will be our toy task to test things with

 Revision 1.4  2004/05/25 15:49:03  reddawg
 Added Syscall Interface For MPI

 Revision 1.3  2004/05/21 12:36:53  reddawg
 Cleaned Up

 Revision 1.2  2004/05/15 02:30:28  reddawg
 Lots of changes

 END
 ***/

