wmbusmeters/src/util.h

316 wiersze
12 KiB
C
Czysty Zwykły widok Historia

2018-04-01 06:53:37 +00:00
/*
Copyright (C) 2017-2022 Fredrik Öhrström (gpl-3.0-or-later)
2018-04-01 06:53:37 +00:00
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, see <http://www.gnu.org/licenses/>.
*/
2017-08-09 10:00:11 +00:00
#ifndef UTIL_H
#define UTIL_H
#include<signal.h>
#include<cstdint>
#include<string>
2017-08-09 10:00:11 +00:00
#include<functional>
2021-03-06 15:28:42 +00:00
#include<map>
#include<set>
2017-08-09 10:00:11 +00:00
#include<vector>
void setVersion(const char *v);
const char *getVersion();
2017-08-09 10:00:11 +00:00
void onExit(std::function<void()> cb);
void restoreSignalHandlers();
bool gotHupped();
void wakeMeUpOnSigChld(pthread_t t);
2019-11-03 15:31:30 +00:00
bool signalsInstalled();
2017-08-09 10:00:11 +00:00
typedef unsigned char uchar;
#define call(A,B) ([&](){A->B();})
#define calll(A,B,T) ([&](T t){A->B(t);})
2017-08-09 10:00:11 +00:00
2022-05-21 12:22:56 +00:00
enum class TestBit
{
Unknown,
2022-05-21 12:22:56 +00:00
Set,
NotSet
};
uchar bcd2bin(uchar c);
uchar revbcd2bin(uchar c);
uchar reverse(uchar c);
// A BCD string 102030405060 is reversed to 605040302010
2022-11-01 11:14:48 +00:00
std::string reverseBCD(const std::string& v);
// A hex string encoding ascii chars is reversed and safely translated into a readble string.
2022-11-01 11:14:48 +00:00
std::string reverseBinaryAsciiSafeToString(const std::string& v);
// Check if hex string is likely to be ascii
2022-11-01 11:14:48 +00:00
bool isLikelyAscii(const std::string& v);
bool isHexChar(uchar c);
// Flex strings contain hexadecimal digits and permit # | and whitespace.
bool isHexStringFlex(const char* txt, bool *invalid);
bool isHexStringFlex(const std::string &txt, bool *invalid);
// Strict strings contain only hexadecimal digits.
bool isHexStringStrict(const char* txt, bool *invalid);
bool isHexStringStrict(const std::string &txt, bool *invalid);
int char2int(char input);
bool hex2bin(const char* src, std::vector<uchar> *target);
2022-11-01 11:14:48 +00:00
bool hex2bin(const std::string &src, std::vector<uchar> *target);
2019-02-25 21:03:20 +00:00
bool hex2bin(std::vector<uchar> &src, std::vector<uchar> *target);
std::string bin2hex(const std::vector<uchar> &target);
2019-03-15 13:21:50 +00:00
std::string bin2hex(std::vector<uchar>::iterator data, std::vector<uchar>::iterator end, int len);
std::string bin2hex(std::vector<uchar> &data, int offset, int len);
std::string safeString(std::vector<uchar> &target);
2022-11-01 11:14:48 +00:00
void strprintf(std::string *s, const char* fmt, ...);
std::string tostrprintf(const char* fmt, ...);
2022-11-01 11:14:48 +00:00
std::string tostrprintf(const std::string &fmt, ...);
bool endsWith(const std::string &str, const std::string &suffix);
2019-03-01 14:41:11 +00:00
// Return for example: 2010-03-21
std::string strdate(struct tm *date);
std::string strdate(double v);
// Return for example: 2010-03-21 15:22
2019-03-01 14:41:11 +00:00
std::string strdatetime(struct tm *date);
std::string strdatetime(double v);
// Return for example: 2010-03-21 15:22:03
std::string strdatetimesec(struct tm *date);
std::string strdatetimesec(double v);
// Return UTC timestamp:
std::string strTimestampUTC(double v);
2021-01-26 21:22:59 +00:00
void addMonths(struct tm* date, int m);
double addMonths(double t, int m);
2022-11-01 11:14:48 +00:00
bool stringFoundCaseIgnored(const std::string& haystack, const std::string& needle);
2017-08-09 10:00:11 +00:00
void xorit(uchar *srca, uchar *srcb, uchar *dest, int len);
2020-01-28 15:15:11 +00:00
void shiftLeft(uchar *srca, uchar *srcb, int len);
2019-05-04 06:52:25 +00:00
std::string format3fdot3f(double v);
2022-11-01 11:14:48 +00:00
bool enableLogfile(const std::string& logfile, bool daemon);
void disableLogfile();
void enableSyslog();
2017-08-09 10:00:11 +00:00
void error(const char* fmt, ...);
#define verbose(...) { if (isVerboseEnabled()) { verbose_int(__VA_ARGS__); } }
void verbose_int(const char* fmt, ...);
#define trace(...) { if (isTraceEnabled()) { trace_int(__VA_ARGS__); } }
void trace_int(const char* fmt, ...);
#define debug(...) { if (isDebugEnabled()) { debug_int(__VA_ARGS__); } }
void debug_int(const char* fmt, ...);
void warning(const char* fmt, ...);
2019-02-23 22:11:09 +00:00
void info(const char* fmt, ...);
void notice(const char* fmt, ...);
void notice_always(const char* fmt, ...);
2021-03-13 07:54:08 +00:00
void notice_timestamp(const char* fmt, ...);
2017-08-09 10:00:11 +00:00
void silentLogging(bool b);
2017-08-09 10:00:11 +00:00
void verboseEnabled(bool b);
void debugEnabled(bool b);
2020-08-01 19:56:46 +00:00
void traceEnabled(bool b);
2021-03-13 07:54:08 +00:00
enum class AddLogTimestamps
{
2021-03-13 09:28:58 +00:00
NotSet, Never, Always, Important
2021-03-13 07:54:08 +00:00
};
void setLogTimestamps(AddLogTimestamps ts);
void stderrEnabled(bool b);
void logTelegramsEnabled(bool b);
2020-08-01 19:56:46 +00:00
void internalTestingEnabled(bool b);
bool isInternalTestingEnabled();
bool isVerboseEnabled();
bool isDebugEnabled();
bool isTraceEnabled();
bool isLogTelegramsEnabled();
2022-11-01 11:14:48 +00:00
void debugPayload(const std::string& intro, std::vector<uchar> &payload);
void debugPayload(const std::string& intro, std::vector<uchar> &payload, std::vector<uchar>::iterator &pos);
void logTelegram(std::vector<uchar> &original, std::vector<uchar> &parsed, int header_size, int suffix_size);
2017-08-09 10:00:11 +00:00
enum class Alarm
{
DeviceFailure,
RegularResetFailure,
DeviceInactivity,
SpecifiedDeviceNotFound
};
const char* toString(Alarm type);
void logAlarm(Alarm type, std::string info);
2020-08-01 19:56:46 +00:00
void setAlarmShells(std::vector<std::string> &alarm_shells);
2022-11-01 11:14:48 +00:00
bool isValidAlias(const std::string& alias);
bool isValidBps(const std::string& b);
2022-11-01 11:14:48 +00:00
bool isFrequency(const std::string& fq);
bool isNumber(const std::string& fq);
2017-08-09 10:00:11 +00:00
// Split s into strings separated by c.
2022-11-01 11:14:48 +00:00
std::vector<std::string> splitString(const std::string &s, char c);
// Split s into strings separated by c and store inte set.
std::set<std::string> splitStringIntoSet(const std::string &s, char c);
// Split device string cul:c1:CMD(bar 1:2) into cul c1 CMD(bar 1:2)
// I.e. the : colon inside CMD is not used for splitting.
2022-11-01 11:14:48 +00:00
std::vector<std::string> splitDeviceString(const std::string &s);
void incrementIV(uchar *iv, size_t len);
bool checkCharacterDeviceExists(const char *tty, bool fail_if_not);
2019-11-03 15:31:30 +00:00
bool checkFileExists(const char *file);
bool checkIfSimulationFile(const char *file);
bool checkIfDirExists(const char *dir);
2022-11-01 11:14:48 +00:00
bool listFiles(const std::string& dir, std::vector<std::string> *files);
int loadFile(const std::string& file, std::vector<std::string> *lines);
bool loadFile(const std::string& file, std::vector<char> *buf);
std::string eatTo(std::vector<uchar> &v, std::vector<uchar>::iterator &i, int c, size_t max, bool *eof, bool *err);
void padWithZeroesTo(std::vector<uchar> *content, size_t len, std::vector<uchar> *full_content);
2022-11-01 11:14:48 +00:00
std::string padLeft(const std::string& input, int width);
2017-08-09 10:00:11 +00:00
2020-08-01 19:56:46 +00:00
// Parse text string into seconds, 5h = (3600*5) 2m = (60*2) 1s = 1
2022-11-01 11:14:48 +00:00
int parseTime(const std::string& time);
2020-08-01 19:56:46 +00:00
// Test if current time is inside any of the specified periods.
// For example: mon-sun(00-24) is always true!
// mon-fri(08-20) is true monday to friday from 08.00 to 19.59
// tue(09-10),sat(00-24) is true tuesday 09.00 to 09.59 and whole of saturday.
bool isInsideTimePeriod(time_t now, std::string periods);
2022-11-01 11:14:48 +00:00
bool isValidTimePeriod(const std::string& periods);
2020-08-01 19:56:46 +00:00
2018-04-01 06:53:37 +00:00
uint16_t crc16_EN13757(uchar *data, size_t len);
// This crc is used by im871a for its serial communication.
uint16_t crc16_CCITT(uchar *data, uint16_t length);
bool crc16_CCITT_check(uchar *data, uint16_t length);
void addSlipFraming(std::vector<uchar>& from, std::vector<uchar> &to);
// Frame length is set to zero if no frame was found.
void removeSlipFraming(std::vector<uchar>& from, size_t *frame_length, std::vector<uchar> &to);
// Eat characters from the vector v, iterating using i, until the end char c is found.
// If end char == -1, then do not expect any end char, get all until eof.
// If the end char is not found, return error.
// If the maximum length is reached without finding the end char, return error.
std::string eatTo(std::vector<char> &v, std::vector<char>::iterator &i, int c, size_t max, bool *eof, bool *err);
// Eat whitespace (space and tab, not end of lines).
void eatWhitespace(std::vector<char> &v, std::vector<char>::iterator &i, bool *eof);
// First eat whitespace, then start eating until c is found or eof. The found string is trimmed from beginning and ending whitespace.
std::string eatToSkipWhitespace(std::vector<char> &v, std::vector<char>::iterator &i, int c, size_t max, bool *eof, bool *err);
// Remove leading and trailing white space
void trimWhitespace(std::string *s);
// Returns AccessOK if device exists and is accessible.
// NotSameGroup means that there is no permission and the groups do not match.
// NoPermission means some other reason for no access. (missing rw etc)
// Locked means that some other process has locked the tty.
// NoSuchDevice means the tty does not exist.
// NoProperResponse means that we talked to something, but we do not know what it is.
enum class AccessCheck { NoSuchDevice, NoProperResponse, NoPermission, NotSameGroup, AccessOK };
const char* toString(AccessCheck ac);
2022-11-01 11:14:48 +00:00
AccessCheck checkIfExistsAndHasAccess(const std::string& device);
// Count the number of 1:s in the binary number v.
int countSetBits(int v);
2022-11-01 11:14:48 +00:00
bool startsWith(const std::string &s, const char *prefix);
bool startsWith(const std::string &s, std::string &prefix);
// Given alfa=beta it returns "alfa":"beta"
2022-11-01 11:14:48 +00:00
std::string makeQuotedJson(const std::string &s);
2021-02-07 16:35:14 +00:00
std::string currentYear();
2019-12-11 17:56:34 +00:00
std::string currentDay();
std::string currentHour();
std::string currentMinute();
2021-03-13 07:54:08 +00:00
std::string currentSeconds();
2019-12-11 17:56:34 +00:00
std::string currentMicros();
2020-01-31 11:44:23 +00:00
#define CHECK(n) if (!hasBytes(n, pos, frame)) return expectedMore(__LINE__);
bool hasBytes(int n, std::vector<uchar>::iterator &pos, std::vector<uchar> &frame);
2022-11-01 11:14:48 +00:00
bool startsWith(const std::string& s, std::vector<uchar> &data);
2020-03-23 17:58:34 +00:00
2020-08-01 19:56:46 +00:00
// Sum the memory used by the heap and stack.
size_t memoryUsage();
std::string humanReadableTwoDecimals(size_t s);
2022-11-01 11:14:48 +00:00
uint32_t indexFromRtlSdrName(const std::string& s);
bool check_if_rtlwmbus_exists_in_path();
bool check_if_rtlsdr_exists_in_path();
// Return the actual executable binary that is running.
std::string currentProcessExe();
2022-11-01 11:14:48 +00:00
std::string dirname(const std::string& p);
2022-11-01 11:14:48 +00:00
std::string lookForExecutable(const std::string& prog, std::string bin_dir, std::string default_dir);
2021-03-06 15:28:42 +00:00
// Extract from "ppm=5 radix=7" and put key values into map.
2022-11-01 11:14:48 +00:00
bool parseExtras(const std::string& s, std::map<std::string,std::string> *extras);
2021-03-09 21:00:11 +00:00
void checkIfMultipleWmbusMetersRunning();
2021-03-06 15:28:42 +00:00
bool findBytes(std::vector<uchar> &v, uchar a, uchar b, uchar c, size_t *out);
enum class OutputFormat
{
2022-03-06 21:42:44 +00:00
NONE, PLAIN, TERMINAL, JSON, HTML
};
// Joing two status strings with a space, but merge OKs.
// I.e. "OK" + "OK" --> "OK"
// "ERROR" + "OK" --> "ERROR"
// "OK" + "ERROR FLOW" --> "ERROR FLOW"
// "ERROR" + "FLOW" --> "ERROR FLOW"
// It also translates empty strings into OK.
// "" + "OK" --> "OK"
// "" + "" --> "OK"
std::string joinStatusOKStrings(const std::string &a, const std::string &b);
// Same but do not introduce OK, keep empty strings empty.
std::string joinStatusEmptyStrings(const std::string &a, const std::string &b);
// Sort the words in a status string: "GAMMA BETA ALFA" --> "ALFA BETA GAMMA"
// Also identical flags are merged: "BETA ALFA ALFA" --> "ALFA BETA"
2022-11-01 11:14:48 +00:00
// Finally ~ is replaced with a space, this is only used for backwards compatibilty for deprecated fields.
std::string sortStatusString(const std::string &a);
// If a vector is empty, then there will be an assert (with some compiler flags) if we use &v[0]
// even if we do not intend to actually use the pointer to uchars!
// So provide safeVectorPtr which will return NULL instead of assert-crashing.
uchar *safeButUnsafeVectorPtr(std::vector<uchar> &v);
2022-11-01 11:14:48 +00:00
// Count utf8 unicode code points.
int strlen_utf8(const char *s);
2023-10-13 14:30:29 +00:00
int toMfctCode(char a, char b, char c);
bool is_lowercase_alnum_text(const char *text);
2023-11-12 11:36:22 +00:00
// The language that the user expects driver and other messages in.
const std::string &language();
TestBit toTestBit(const char *s);
#ifndef FUZZING
#define FUZZING false
#endif
#endif