1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-04 12:24:11 +01:00
LibrePilot/flight/libraries/alarms.c

247 lines
6.3 KiB
C
Raw Normal View History

2013-04-05 22:46:56 +02:00
/**
******************************************************************************
* @addtogroup OpenPilotSystem OpenPilot System
* @{
* @addtogroup OpenPilotLibraries OpenPilot System Libraries
* @brief OpenPilot System libraries are available to all OP modules.
* @{
* @file alarms.c
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief Library for setting and clearing system alarms
* @see The GNU Public License (GPL) Version 3
*
*****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <openpilot.h>
#include "inc/alarms.h"
2013-04-05 22:46:56 +02:00
// Private constants
// Private types
// Private variables
static xSemaphoreHandle lock;
// Private functions
static int32_t hasSeverity(SystemAlarmsAlarmOptions severity);
/**
* Initialize the alarms library
*/
int32_t AlarmsInitialize(void)
{
SystemAlarmsInitialize();
lock = xSemaphoreCreateRecursiveMutex();
// do not change the default states of the alarms, let the init code generated by the uavobjectgenerator handle that
// AlarmsClearAll();
// AlarmsDefaultAll();
return 0;
2013-04-05 22:46:56 +02:00
}
/**
* Set an alarm
* @param alarm The system alarm to be modified
* @param severity The alarm severity
* @return 0 if success, -1 if an error
*/
int32_t AlarmsSet(SystemAlarmsAlarmElem alarm, SystemAlarmsAlarmOptions severity)
{
SystemAlarmsData alarms;
2013-04-05 22:46:56 +02:00
// Check that this is a valid alarm
if (alarm >= SYSTEMALARMS_ALARM_NUMELEM) {
return -1;
}
2013-04-05 22:46:56 +02:00
// Lock
xSemaphoreTakeRecursive(lock, portMAX_DELAY);
2013-04-05 22:46:56 +02:00
// Read alarm and update its severity only if it was changed
SystemAlarmsGet(&alarms);
if (alarms.Alarm[alarm] != severity) {
alarms.Alarm[alarm] = severity;
SystemAlarmsSet(&alarms);
2013-04-05 22:46:56 +02:00
}
// Release lock
xSemaphoreGiveRecursive(lock);
return 0;
}
/**
* Set an Extended Alarm
* @param alarm The system alarm to be modified
* @param severity The alarm severity
* @param status The Extended alarm status field
* @param subStatus The Extended alarm substatus field
* @return 0 if success, -1 if an error
*/
int32_t ExtendedAlarmsSet(SystemAlarmsAlarmElem alarm,
SystemAlarmsAlarmOptions severity,
SystemAlarmsExtendedAlarmStatusOptions status,
uint8_t subStatus)
{
SystemAlarmsData alarms;
// Check that this is a valid alarm
if (alarm >= SYSTEMALARMS_EXTENDEDALARMSTATUS_NUMELEM) {
return -1;
}
// Lock
xSemaphoreTakeRecursive(lock, portMAX_DELAY);
// Read alarm and update its severity only if it was changed
SystemAlarmsGet(&alarms);
if (alarms.Alarm[alarm] != severity) {
alarms.ExtendedAlarmStatus[alarm] = status;
alarms.ExtendedAlarmSubStatus[alarm] = subStatus;
alarms.Alarm[alarm] = severity;
SystemAlarmsSet(&alarms);
}
// Release lock
xSemaphoreGiveRecursive(lock);
return 0;
}
2013-04-05 22:46:56 +02:00
/**
* Get an alarm
* @param alarm The system alarm to be read
* @return Alarm severity
*/
SystemAlarmsAlarmOptions AlarmsGet(SystemAlarmsAlarmElem alarm)
{
SystemAlarmsData alarms;
2013-04-05 22:46:56 +02:00
// Check that this is a valid alarm
if (alarm >= SYSTEMALARMS_ALARM_NUMELEM) {
return 0;
}
2013-04-05 22:46:56 +02:00
// Read alarm
SystemAlarmsGet(&alarms);
return alarms.Alarm[alarm];
}
/**
* Set an alarm to it's default value
* @param alarm The system alarm to be modified
* @return 0 if success, -1 if an error
*/
int32_t AlarmsDefault(SystemAlarmsAlarmElem alarm)
{
return AlarmsSet(alarm, SYSTEMALARMS_ALARM_DEFAULT);
2013-04-05 22:46:56 +02:00
}
/**
* Default all alarms
*/
void AlarmsDefaultAll()
{
for (uint32_t n = 0; n < SYSTEMALARMS_ALARM_NUMELEM; ++n) {
AlarmsDefault(n);
2013-04-05 22:46:56 +02:00
}
}
/**
* Clear an alarm
* @param alarm The system alarm to be modified
* @return 0 if success, -1 if an error
*/
int32_t AlarmsClear(SystemAlarmsAlarmElem alarm)
{
if (alarm < SYSTEMALARMS_EXTENDEDALARMSTATUS_NUMELEM) {
return ExtendedAlarmsSet(alarm, SYSTEMALARMS_ALARM_OK, SYSTEMALARMS_EXTENDEDALARMSTATUS_NONE, 0);
} else {
return AlarmsSet(alarm, SYSTEMALARMS_ALARM_OK);
}
2013-04-05 22:46:56 +02:00
}
/**
* Clear all alarms
*/
void AlarmsClearAll()
{
for (uint32_t n = 0; n < SYSTEMALARMS_ALARM_NUMELEM; ++n) {
AlarmsClear(n);
2013-04-05 22:46:56 +02:00
}
}
/**
* Check if there are any alarms with the given or higher severity
* @return 0 if no alarms are found, 1 if at least one alarm is found
*/
int32_t AlarmsHasWarnings()
{
return hasSeverity(SYSTEMALARMS_ALARM_WARNING);
2013-04-05 22:46:56 +02:00
}
/**
* Check if there are any alarms with error or higher severity
* @return 0 if no alarms are found, 1 if at least one alarm is found
*/
int32_t AlarmsHasErrors()
{
return hasSeverity(SYSTEMALARMS_ALARM_ERROR);
}
2013-04-05 22:46:56 +02:00
/**
* Check if there are any alarms with critical or higher severity
* @return 0 if no alarms are found, 1 if at least one alarm is found
*/
int32_t AlarmsHasCritical()
{
return hasSeverity(SYSTEMALARMS_ALARM_CRITICAL);
}
2013-04-05 22:46:56 +02:00
/**
* Check if there are any alarms with the given or higher severity
* @return 0 if no alarms are found, 1 if at least one alarm is found
*/
static int32_t hasSeverity(SystemAlarmsAlarmOptions severity)
{
SystemAlarmsData alarms;
2013-04-05 22:46:56 +02:00
// Lock
2013-04-05 22:46:56 +02:00
xSemaphoreTakeRecursive(lock, portMAX_DELAY);
// Read alarms
SystemAlarmsGet(&alarms);
// Go through alarms and check if any are of the given severity or higher
for (uint32_t n = 0; n < SYSTEMALARMS_ALARM_NUMELEM; ++n) {
if (alarms.Alarm[n] >= severity) {
xSemaphoreGiveRecursive(lock);
return 1;
}
2013-04-05 22:46:56 +02:00
}
// If this point is reached then no alarms found
xSemaphoreGiveRecursive(lock);
return 0;
}
2013-04-05 22:46:56 +02:00
/**
* @}
* @}
*/