RSS Git Download  Clone
Raw Blame History
/*******************************************************************************
 *	Control.h	Control process
 *			T.Barnaby,	BEAM Ltd,	2007-02-07
 *******************************************************************************
 */
#ifndef Control_H
#define Control_H	1

#include <stdint.h>
#include <BError.h>
#include <BThread.h>
#include <TmsPuApi.h>
#include <TmsC.h>
#include <TmsEventServerList.h>
#include <Pupe.h>
#include <PupeGeog.h>
#include <BMutex.h>
#include <BRWLock.h>
#include <BSema.h>
#include <main.h>
#include <signal.h>

class Control;

class CycleParams {
public:
				CycleParams(BString cycleType, CycleParam& params);
	BString			cycleType;	// The Cycle type name
	BList<CycleParam>	params;		// Parameters for channels
};

/// A resource test class. this implements a simple timer operated function called every second, that can be used to check on system resources.
class ControlTimer : public BThread {
public:
			ControlTimer(Control& control);
	void		sync();				///< Synchronises timer
	void*		function();
	void		tick();
private:
	Control&		ocontrol;
	BSema			otick;
public:
	static ControlTimer*	timer;
	static void		sigFunc(int sig, siginfo_t* sigInfo, void* data);
};

/// The main system control class
class Control {
public:
			Control(int dbFixed);
			~Control();

	BError		init(int number);			///< Initialise the system

	// Main functionality
	BError		initCmd();
	BError		setProcessPriority(BUInt32 priority);
	BError		configure(ConfigInfo configInfo);	///< Configure the system for use. This includes mapping the individual physical PickUp channels to logical pickup channels.
	BError		test(BList<BError>& errors);
	BError		getStatus(BList<NameValue>& statusList);
	BError		getStatistics(BList<NameValue>& statsList);

	BError		addEventServer(BString name);
	BError		delEventServer(BString name);
	BError		setControlInfo(CycleParam params);
	BError		setNextCycle(UInt32 cycleNumber, BString cycleType);
	BError		getStatus(PuChannel puChannel, PuStatus& puStatus);
	BError		getCycleInformation(UInt32 cycleNumber, CycleInformation& cycleInformation);
	BError		getData(PuChannel puChannel, DataInfo dataInfo, Data& data);
	BError		requestData(PuChannel puChannel, DataInfo dataInfo);

	BError		setTestMode(PuChannel puChannel, UInt32 testOutput, UInt32 timingDisableMask);
	BError		setTimingSignals(PuChannel puChannel, UInt32 timingSignals);
	BError		captureTestData(PuChannel puChannel, TestCaptureInfo captureInfo, BArray<UInt64>& data);
	BError		setTestData(PuChannel puChannel, Int32 on, BArray<UInt32> data);	///< This function will set a PU channel to sample data from memory rather than the ADC's
	BError		setPupeConfig(PuChannel puPhysChannel, PupeConfig pupeConfig);
	BError		getPupeConfig(PuChannel puPhysChannel, PupeConfig& pupeConfig);

	void		run();				///< Start the system running
	
	// System utilities
	void		timer();

	void		cycleStart(UInt32 cycleNumber);		///< The CYCLE_START event has occured
	void		cycleStop(UInt32 cycleNumber);		///< The CYCLE_STOP event has occured
	void		cycleError(UInt32 cycleNumber, BError error);	///< A Cycle error occurred
	BError		getPuChannel(PuChannel puPhysChannel, UInt32& puChannel);
	BError		getPuPhysChannel(UInt32 puChannel, PuChannel& puPhysChannel);

	int		moduleNum();				///< Returns the module controller number
	
public:
	// General data
	int			osimulate;		///< Simulation mode
	int			osimulateTiming;	///< Simulate timing inputs
	BMutex			odmaLock;		///< DMA lock for all PUPE's

private:
	// Low level bits
	BError			initPupeEngines();
	BString			getName();
	BError			mergeError(BError err);

	// Control data
	int			odebugFixed;		///< Fixed debug level from command line
	int			onum;			///< The Module controller number
	int			oinitialised;		///< The system has been initialised
	int			omaster;		///< The master PUPE
	
	BMutex			olock;			///< Lock for data access
	ControlTimer		otimer;
	
	BString			oserverName;		///< The BOAP Servers host name
	BoapServer		oboapServer;		///< The overal Boap server class
	PuControlServer		opuControlServer;	///< The PuControl API interface
	PuProcessServer		opuProcessServer;	///< The PuProcess API interface

	PupeGeog		opupeGeog;		///< The physical location of Pupe boards
	ConfigInfo		oconfigInfo;		///< The configuration of PickUp channels
	BMutex			oconfigInfoLock;
	BList<Pupe*>		opupeEngines;		///< The list of PUPE engines
	BRWLock			opupeEnginesLock;
	BMutex			opupeLock;		///< Locks access to the pupe engines
	TmsEventServerList	oeventServers;		///< The list of event servers
	
	BList<CycleParams>	ocycleParms;		///< The table of cycle parameters
	BMutex			ocycleParmsLock;	

	UInt32			ocycleNumber;		///< The current cycle number
	BString			ocycleType;		///< The current cycle type

	UInt32			ocycleNumberNext;	///< The next cycle number
	BString			ocycleTypeNext;		///< The next cycle type

	UInt32			oprocessingCycle;	///< We are processing a cycle
	UInt32			ocycleTick;		///< Cycle tick for software simulatted timing signals
	
	// Statistics
	double			ostartTime;		///< The start time
	UInt64			ocycleProcessed;	///< The number of cycles processed
};

#endif