#ifndef _MICROAPP_H
#define _MICROAPP_H

#include "errorhandler.h"
#include "ostask.h"

// Forward declarations
class TaskSynchronizer;
class MicroPeriodicHandler;
class MicroEventHandler;
 
/*-------------------------------------------------------
CLASS 
MicroApp

DESCRIPTION
MicroApp has the following responsibilities:

Initialize microapp
Spawn subsidiary tasks

MicroApp is a virtual class.

AUTHOR
Tom O'Reilly
Modified by D.Cline for the LRS Project
-------------------------------------------------------*/
class MicroApp : 
public ErrorHandler, public TaskBase
{
  public:					   	 

  // Constructor
  // [input] systemName:  Name of system (e.g. "EMBEDDED")
  // [input] appName:  Name of application (e.g. "LRS")
  MicroApp(const char *systemName,  const char *appName);

  virtual ~MicroApp();

  // Stop microperiodichandler and eventhandler tasks and exit 
  // appframework run() method.
  // Call this before deleting microperiodichandler and eventhandler
  // objects you own in your MicroApp superclass
  // returns -1 if error exiting the application
  virtual int exit();

  // Create TaskSyncronizer and create and run periodic and event
  // handler tasks. 
  void * run(int argc, char *argv[]);

  // Reset subsidiary tasks
  void reset();

  // TaskSynchronizer (inter-task synchronization, etc.)
  TaskSynchronizer *_taskSync;
  
  // Pointer to created PeriodicHandler
  MicroPeriodicHandler *_periodicHandler;

  // Pointer to created EventHandler
  MicroEventHandler *_eventHandler;

  // Create PeriodicHandler subclass object
  virtual MicroPeriodicHandler *createPeriodicHandler() = 0;

  // Create EventHandler subclass object
  virtual MicroEventHandler *createEventHandler() = 0;  

  // Entry point for application specific run loop
  virtual int runApp();

  protected:

  // Task parameters for subsidiary tasks
  // [] priority: Task priority
  // [] floatingPoint: TRUE if task uses floatingPoint operations
  // [] stackSize: stacksize for task
  struct TaskParams
  {
    int priority;
    MBool floatingPoint;
    int stackSize;
  };

  // Fill in TasksParams structure for DmEvent and PeriodicHandler tasks
  virtual void setTaskParams() = 0;

  // Create TaskSynchronizer. Can be called by overriding function, but overriding
  // function must call this class when done to init TaskSynch
  virtual int initialize(int argc, char *argv[]);

  // System name
  const char *_systemName;

  // Application name
  const char *_appName; 

  // TaskParams for EventHandler; must be filled in by setTaskParams() 
  TaskParams _eventHandlerParams;

  // TaskParams for PeriodicHandler; must be filled in by setTaskParams() 
  TaskParams _periodicHandlerParams;

  private:

  int spawnTasks();

};

#endif




