AmiTimeKeeper/val.c

143 lines
4.3 KiB
C

/*-
* Copyright (c) 2017-2020 Carsten Sonne Larsen <cs@innolan.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "config.h"
#include "message.h"
#include "setting.h"
#include "log.h"
#include "logmod.h"
#define MODULENAME "Validation"
static const char *settingChangedLong = "%s changed: %ld -> %ld";
static const char *settingTooLow = "%s < %ld (too low)";
static const char *settingTooHigh = "%s > %ld (too high)";
static const char *settingGreaterThan = "%s * 2 > %s";
static void ValidateInterval(void)
{
if (Settings->Interval < INTERVAL_MIN)
{
LogInfo(settingTooLow,
SettingKeys->Interval,
INTERVAL_MIN);
LogInfo(settingChangedLong,
SettingKeys->Interval,
Settings->Interval,
INTERVAL_MIN);
Settings->Interval = INTERVAL_MIN;
}
}
static void ValidateTimeout(void)
{
if (Settings->Timeout < TIMEOUT_MIN)
{
LogInfo(settingTooLow,
SettingKeys->Timeout,
TIMEOUT_MIN);
LogInfo(settingChangedLong,
SettingKeys->Timeout,
Settings->Timeout,
TIMEOUT_MIN);
Settings->Timeout = TIMEOUT_MIN;
}
if (Settings->Timeout > Settings->Interval / 2)
{
LogInfo(settingGreaterThan,
SettingKeys->Timeout,
SettingKeys->Interval);
LogInfo(settingChangedLong,
SettingKeys->Timeout,
Settings->Timeout,
Settings->Interval / 2);
Settings->Timeout = Settings->Interval / 2;
}
if (Settings->Timeout > TIMEOUT_MAX)
{
LogInfo(settingTooHigh,
SettingKeys->Timeout,
TIMEOUT_MAX);
LogInfo(settingChangedLong,
SettingKeys->Timeout,
Settings->Timeout,
TIMEOUT_MAX);
Settings->Timeout = TIMEOUT_MAX;
}
}
static void ValidatePriority(void)
{
if (Settings->Priority < PRIORITY_MIN)
{
LogInfo(settingTooLow,
SettingKeys->Priority,
PRIORITY_MIN);
LogInfo(settingChangedLong,
SettingKeys->Priority,
Settings->Priority,
PRIORITY_MIN);
Settings->Priority = PRIORITY_MIN;
}
if (Settings->Priority > PRIORITY_MAX)
{
LogInfo(settingTooHigh,
SettingKeys->Priority,
PRIORITY_MAX);
LogInfo(settingChangedLong,
SettingKeys->Priority,
Settings->Priority,
PRIORITY_MAX);
Settings->Priority = PRIORITY_MAX;
}
}
static void ValidateThreshold(void)
{
if (Settings->Threshold < THRESHOLD_MIN)
{
LogInfo(settingTooLow,
SettingKeys->Threshold,
THRESHOLD_MIN);
LogInfo(settingChangedLong,
SettingKeys->Threshold,
Settings->Threshold,
THRESHOLD_MIN);
Settings->Threshold = THRESHOLD_MIN;
}
}
void SanitizeSettings(void)
{
ValidateInterval();
ValidateTimeout();
ValidatePriority();
ValidateThreshold();
}