app.p 6.12 KB
Newer Older
Mike Mathews's avatar
Mike Mathews committed
/**
Mike Mathews's avatar
Mike Mathews committed
 *  Name:  app.p
Mike Mathews's avatar
Mike Mathews committed
 *
 *  This module is the proprietary property of Codepoint Technologies, Inc.
 *  Copyright (C) 2021 Codepoint Technologies, Inc.
 *  All Rights Reserved
 */

Mike Mathews's avatar
Mike Mathews committed
 #include "app.i"
Mike Mathews's avatar
Mike Mathews committed
 #include "Controller.i"
 #include "version.i" //Compile time generated file.

//System includes.
#include "cpflexver.i"
#include "system.i"
#include "power.i"
#include "battery.i"
#include "TelemMgr.i"

//Include Controller configuration.
#include "config/ConfigApp.i"

/*******************************************************
* Forward Declarations.
*******************************************************/
forward _ProcessSyslogCommand( value  );
forward  ResultCode: _ReportSyslogStatus( seqOut);
#pragma warning disable 213


/*******************************************************
* Constants and static variables.
*******************************************************/


/*******************************************************
* System events.
*******************************************************/

@SysInit()
{


Mike Mathews's avatar
Mike Mathews committed
	Log(   "********** N100 Vibration Sensor **************",	MC_info, MP_med, LO_default);
Mike Mathews's avatar
Mike Mathews committed
	LogFmt("** Version:      %s",								MC_info, MP_med, LO_default, __VERSION__);
	LogFmt("** CP-Flex Ver:  %s",								MC_info, MP_med, LO_default, CPFLEX_VERSION);
	LogFmt("** Build Date:   %s %s",							MC_info, MP_med, LO_default, __DATE__, __TIME__);
	Log(   "***********************************************",	MC_info, MP_med, LO_default);


#ifdef __SYSLOG__
//	//Test mode we automatically enable the System Log.
	SysSetLogFilters( 0xFF, 0xFF);
#endif 

	battery_Init();
	TelemMgr_Init();
	ControllerInit();
}

/*******************************************************
* Handle UI Events.
*******************************************************/
@UiButtonEvent( idButton, ButtonPress: press, ctClicks)
{
	TraceDebugFmt( "Button: id=%d, press=%d, clicks=%d", idButton, press, ctClicks);
	switch(press) {
		case BP_short: 		{	battery_IndicateChargeLevel(); }
		case BP_double:		{   TelemMgr_IndicateNetworkStatus(); }
	}
}

#pragma warning disable 203

/**
* @brief  Application Defined callback handler that indicates entering power savings.
* @remarks Required implementation to catch enter power saving events.
*/
app_EnterPowerSaving(pctCharged)
{
	// Tracking is deactivated to preserve battery.	
	TraceDebug("Enter Power Save Mode");
	ControllerPowerSavingUpdate(false);
}

/**
* @brief  Application Defined callback handler that indicates entering power savings.
* @remarks Required implementation to catch exit power saving events.
*/
app_ExitPowerSaving(pctCharged)
{
	TraceDebug("Exit Power Save Mode");
	ControllerPowerSavingUpdate(true);
}

/*******************************************************
* Communication Implementation
*******************************************************/
#pragma warning enable 203

/**
* @brief Event hander receives notifications of telemtry system changes.
* @remarks Use this event to trigger other actions when telemetry is available or unavailable.
* @param stateTelem The updated change of telemtry state.
* @param datarate  The datarate in data rate units (e.g. dr0 -dr12).
* @param packetsize The uplink/downlink packetsize in bytes.  -1 is unknown.
*/
@TelemStatusEvent( TelemState: stateTelem, datarate, packetsize)
{
	//Forward notification to the controller.
	ControllerTelemetryStatusUpdate( stateTelem, datarate, packetsize);
}



/**
* @brief Received data sequence handler.
* @remarks.  Processes received sequences and reports any requested. data.
*/
@TelemRecvSeq( Sequence: seqIn, ctMsgs )
{
	TraceDebug("TelemRecvSeq");
	//Process the received sequence.
	new Message: msg;
	new MessageType: tmsg;
	new tcode;
	new id;
	new EventPriority: priority;
	new EventType: etype;
	new ResultCode: rc;
	new ct= 0;
	new Sequence:seqOut;

	//Allocate a sequence to send messages.
	rc = TelemSendSeq_Begin( seqOut, 96); 
	if( rc != RC_success)
	{
		TraceError("Could not allocate sequence buffer, ");
	}
	
	//Process each of the received messages.
	while( rc == RC_success 
		&& TelemRecv_NextMsg( seqIn, msg, tmsg, tcode, id, priority, etype)  == RC_success
	) {		
		if( ControllerIsSupportedMessage( tmsg, id)) {
			rc = ControllerProcessReceived( seqOut, tmsg, id, msg);			
			++ct;
		} else if( tmsg == MT_Int32Msg){
			switch( id) {
Mike Mathews's avatar
Mike Mathews committed
				case NID_SensorBattery: 	 {
Mike Mathews's avatar
Mike Mathews committed
					rc = battery_ReportStatus( seqOut); 
					++ct;
				}
Mike Mathews's avatar
Mike Mathews committed
				case NID_SensorBatterylevel: {
Mike Mathews's avatar
Mike Mathews committed
					rc = battery_ReportChargeLevel( seqOut); 
					++ct;  
				}
				case NID_ArchiveSyslog: { 
					new cmdvalue;
					//Set the System Log Archive Filter if defined.
					if( RC_success == TelemRecv_ToInt32Msg( msg,cmdvalue) ) {
						_ProcessSyslogCommand( cmdvalue);
					}
					rc =  _ReportSyslogStatus( seqOut);
					++ct;

				}
				case NID_ArchiveErase: {
					ArchiveErase(false);
				}
				case NID_AdminReset: {
					SysReset();
				}
			}
		}
	}

	//Done with the input sequence.
	TelemRecv_SeqEnd( seqIn);

	//Either send or discard output sequence 
	//send only if there are messages to send and we were successful in creating them.
	TelemSendSeq_End( seqOut, rc != RC_success || ct == 0, TPF_CONFIRM);
	if( rc != RC_success)
		TraceError("Cannot send sequence an error occurred while assembling.");
}

/*******************************************************
* Communication Implementation
*******************************************************/
static _ProcessSyslogCommand( value  ) 
{

	new archive;
	switch(value) {
		case NID_ArchiveSyslogDisable: archive = 0x0;
		case NID_ArchiveSyslogAlert: archive =  0x3F;
		case NID_ArchiveSyslogInfo:	archive = 0x4F;
		case NID_ArchiveSyslogDetail: archive = 0x5F;
		case NID_ArchiveSyslogAll: archive = 0x6F;
	}

	SysSetLogFilters( archive, 0xFF);
}

static ResultCode: _ReportSyslogStatus( seqOut) 
{
	new archive, terminal, telemetry, cmdvalue;
	SysGetLogFilters( archive, terminal, telemetry);

	switch(archive & 0xF0) 
	{
		case MC_unspecified: cmdvalue = NID_ArchiveSyslogDisable;
		case MC_alert: cmdvalue = NID_ArchiveSyslogAlert;
		case MC_info: cmdvalue = NID_ArchiveSyslogInfo;
		case MC_detail: cmdvalue = NID_ArchiveSyslogDetail;
		case MC_debug: cmdvalue = NID_ArchiveSyslogAll;
	}
					
	return TelemSendInt32Msg( seqOut, NID_ArchiveSyslog, cmdvalue);
}