/*
 * remove_before_flight.cpp
 *
 */


#include "remove_before_flight.h"

#include "mex.h"

#define RBF_MAX_FEATURES	32

#define RBF_ALWAYS_ENABLED

struct rbf_feature_t
{
	const char* name;
	const char* description;
	RemoveBeforeFlight::Feature* f;
};

struct rbf_context_t
{
	int master_enable;

	unsigned int ac_id;
	const char* ftb_name;

	rbf_feature_t db[RBF_MAX_FEATURES];

    static void __monitor_invokable_list() __attribute__((used,noinline));
    static void __monitor_invokable_enable() __attribute__((used,noinline));
    static void __monitor_invokable_disable() __attribute__((used,noinline));
};

static rbf_context_t autoMonitorable_rbf={1, 0, 0, {}};

void rbf_context_t::__monitor_invokable_enable()
{
	autoMonitorable_rbf.master_enable=1;
}

void rbf_context_t::__monitor_invokable_disable()
{
	autoMonitorable_rbf.master_enable=0;
}

void rbf_context_t::__monitor_invokable_list()
{
	for(int i=0; i<RBF_MAX_FEATURES; ++i)
	{
		rbf_feature_t& f=autoMonitorable_rbf.db[i];
		if (f.f)
		{
			mex_printf("%2d: %-16.16s %s", i,
					f.name,
					f.f->_enabled ? "ON " : "OFF");
			if (f.description)
			{
				mex_printf("%s", f.description);
			}
			mex_printf("\n");
		}
	}
}

RemoveBeforeFlight::Feature::Feature(const char* name, const char* description, bool start_enabled):
	_enabled(start_enabled)
{
	for(int i=0; i<RBF_MAX_FEATURES; ++i)
	{
		if (autoMonitorable_rbf.db[i].f==0)
		{
			autoMonitorable_rbf.db[i].f=this;
			autoMonitorable_rbf.db[i].name=name;
			autoMonitorable_rbf.db[i].description=description;
			break;
		}
	}
}

bool RemoveBeforeFlight::Feature::enabled() const
{
	return RemoveBeforeFlight::experimental() && _enabled;
}

unsigned int RemoveBeforeFlight::experimental()
{
#ifdef RBF_ALWAYS_ENABLED
	return autoMonitorable_rbf.master_enable;
#else
	return autoMonitorable_rbf.master_enable && (autoMonitorable_rbf.ac_id);
#endif
}


const char* RemoveBeforeFlight::ftbName()
{
	return autoMonitorable_rbf.ftb_name;
}

void RemoveBeforeFlight::setFtbCode(unsigned int id, const char* /*name*/)
{
	enum aircraft_t { ac_default=0, ac_pod=1, ac_test_bench=250, ac_tbay, ac_qualification_bench};
	if (autoMonitorable_rbf.ac_id!=id)
	{
		switch(id)
		{
		case ac_pod:
			autoMonitorable_rbf.ftb_name="POD";
			break;
		case ac_test_bench:
			autoMonitorable_rbf.ftb_name="TST";
			break;
		case ac_tbay:
			autoMonitorable_rbf.ftb_name="TBAY";
			break;
		case ac_qualification_bench:
			autoMonitorable_rbf.ftb_name="QUAL";
			break;

		default:
			autoMonitorable_rbf.ftb_name=0;
			break;
		}

		autoMonitorable_rbf.ac_id=id;
	}
}







