2018-04-01 06:53:37 +00:00
|
|
|
/*
|
2019-02-24 13:08:51 +00:00
|
|
|
Copyright (C) 2017-2019 Fredrik Öhrström
|
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>
|
2017-09-02 21:26:57 +00:00
|
|
|
#include<stdint.h>
|
2019-02-23 12:41:17 +00:00
|
|
|
#include<string>
|
2017-08-09 10:00:11 +00:00
|
|
|
#include<functional>
|
|
|
|
#include<vector>
|
|
|
|
|
2019-10-29 20:25:10 +00:00
|
|
|
enum class MeterType;
|
|
|
|
|
2017-08-09 10:00:11 +00:00
|
|
|
void onExit(std::function<void()> cb);
|
2019-06-11 16:49:26 +00:00
|
|
|
void restoreSignalHandlers();
|
|
|
|
bool gotHupped();
|
2019-04-03 15:31:51 +00:00
|
|
|
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;
|
|
|
|
|
2018-12-28 17:35:32 +00:00
|
|
|
#define call(A,B) ([&](){A->B();})
|
|
|
|
#define calll(A,B,T) ([&](T t){A->B(t);})
|
2017-08-09 10:00:11 +00:00
|
|
|
|
2017-08-30 12:10:23 +00:00
|
|
|
bool hex2bin(const char* src, std::vector<uchar> *target);
|
2018-04-01 06:53:37 +00:00
|
|
|
bool hex2bin(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);
|
2017-08-09 10:00:11 +00:00
|
|
|
std::string bin2hex(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);
|
2019-04-04 16:18:27 +00:00
|
|
|
std::string safeString(std::vector<uchar> &target);
|
2018-04-01 06:53:37 +00:00
|
|
|
void strprintf(std::string &s, const char* fmt, ...);
|
2019-03-01 14:41:11 +00:00
|
|
|
// Return for example: 2010-03-21
|
|
|
|
std::string strdate(struct tm *date);
|
|
|
|
// Return for example: 2010-03-21 15:22:03
|
|
|
|
std::string strdatetime(struct tm *date);
|
2017-08-09 10:00:11 +00:00
|
|
|
|
|
|
|
void xorit(uchar *srca, uchar *srcb, uchar *dest, int len);
|
2019-05-04 06:52:25 +00:00
|
|
|
std::string format3fdot3f(double v);
|
2019-02-26 22:40:44 +00:00
|
|
|
bool enableLogfile(std::string logfile, bool daemon);
|
2019-06-11 16:49:26 +00:00
|
|
|
void disableLogfile();
|
2019-02-23 12:41:17 +00:00
|
|
|
void enableSyslog();
|
2017-08-09 10:00:11 +00:00
|
|
|
void error(const char* fmt, ...);
|
|
|
|
void verbose(const char* fmt, ...);
|
2018-02-28 21:14:16 +00:00
|
|
|
void debug(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, ...);
|
2017-08-09 10:00:11 +00:00
|
|
|
|
2018-02-28 21:14:16 +00:00
|
|
|
void warningSilenced(bool b);
|
2017-08-09 10:00:11 +00:00
|
|
|
void verboseEnabled(bool b);
|
2018-02-28 21:14:16 +00:00
|
|
|
void debugEnabled(bool b);
|
2018-03-05 10:29:25 +00:00
|
|
|
void logTelegramsEnabled(bool b);
|
2018-02-28 21:14:16 +00:00
|
|
|
|
|
|
|
bool isVerboseEnabled();
|
|
|
|
bool isDebugEnabled();
|
2018-03-05 10:29:25 +00:00
|
|
|
bool isLogTelegramsEnabled();
|
2018-02-28 21:14:16 +00:00
|
|
|
|
|
|
|
void debugPayload(std::string intro, std::vector<uchar> &payload);
|
2018-03-05 10:29:25 +00:00
|
|
|
void logTelegram(std::string intro, std::vector<uchar> &header, std::vector<uchar> &content);
|
2017-08-09 10:00:11 +00:00
|
|
|
|
2019-09-16 14:32:24 +00:00
|
|
|
bool isValidMatchExpression(std::string id, bool non_compliant);
|
|
|
|
bool isValidMatchExpressions(std::string ids, bool non_compliant);
|
|
|
|
bool doesIdMatchExpression(std::string id, std::string match);
|
|
|
|
bool doesIdMatchExpressions(std::string& id, std::vector<std::string>& ids);
|
|
|
|
|
2019-10-29 20:25:10 +00:00
|
|
|
bool isValidKey(std::string& key, MeterType mt);
|
2019-02-26 21:47:12 +00:00
|
|
|
bool isFrequency(std::string& fq);
|
2019-10-15 12:32:32 +00:00
|
|
|
bool isNumber(std::string& fq);
|
2017-08-09 10:00:11 +00:00
|
|
|
|
2019-09-16 14:32:24 +00:00
|
|
|
std::vector<std::string> splitMatchExpressions(std::string& mes);
|
2019-03-05 20:19:05 +00:00
|
|
|
|
2017-08-30 18:08:02 +00:00
|
|
|
void incrementIV(uchar *iv, size_t len);
|
|
|
|
|
2018-02-28 21:14:16 +00:00
|
|
|
bool checkCharacterDeviceExists(const char *tty, bool fail_if_not);
|
2019-11-03 15:31:30 +00:00
|
|
|
bool checkFileExists(const char *file);
|
2018-03-05 10:29:25 +00:00
|
|
|
bool checkIfSimulationFile(const char *file);
|
2018-03-05 14:12:58 +00:00
|
|
|
bool checkIfDirExists(const char *dir);
|
2019-02-23 20:21:17 +00:00
|
|
|
bool listFiles(std::string dir, std::vector<std::string> *files);
|
|
|
|
bool loadFile(std::string file, std::vector<char> *buf);
|
2018-03-05 10:29:25 +00:00
|
|
|
|
|
|
|
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);
|
2017-08-09 10:00:11 +00:00
|
|
|
|
2018-03-05 14:12:58 +00:00
|
|
|
int parseTime(std::string time);
|
|
|
|
|
2018-04-01 06:53:37 +00:00
|
|
|
uint16_t crc16_EN13757(uchar *data, size_t len);
|
2018-03-16 10:48:04 +00:00
|
|
|
|
2018-11-29 21:25:50 +00:00
|
|
|
// 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);
|
|
|
|
|
2019-02-23 12:41:17 +00:00
|
|
|
// 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);
|
2019-04-03 16:57:18 +00:00
|
|
|
// Returns true if device exists and this programs user, belongs
|
|
|
|
// to the same group that the device belongs to.
|
|
|
|
enum class AccessCheck { NotThere, NotSameGroup, OK };
|
|
|
|
AccessCheck checkIfExistsAndSameGroup(std::string device);
|
2019-06-06 15:28:20 +00:00
|
|
|
// Count the number of 1:s in the binary number v.
|
|
|
|
int countSetBits(int v);
|
2019-02-23 12:41:17 +00:00
|
|
|
|
2019-10-20 17:19:17 +00:00
|
|
|
bool startsWith(std::string &s, const char *prefix);
|
|
|
|
|
|
|
|
// Given alfa=beta it returns "alfa":"beta"
|
|
|
|
std::string makeQuotedJson(std::string &s);
|
|
|
|
|
2018-02-28 21:14:16 +00:00
|
|
|
#endif
|