11#ifndef ALICAT_BASIS2_ASCII_HPP
12#define ALICAT_BASIS2_ASCII_HPP
34inline constexpr char kCr =
'\r';
68inline constexpr int64_t
kMilliCap = 999'999'999'999LL;
74inline constexpr std::array<CommandSpec, kCommandRows>
kCommands{
79 .controller_only =
false,
91 .controller_only =
false,
103 .controller_only =
true,
115 .controller_only =
true,
127 .controller_only =
true,
139 .controller_only =
true,
151 .controller_only =
true,
163 .controller_only =
true,
175 .controller_only =
true,
187 .controller_only =
true,
199 .controller_only =
true,
211 .controller_only =
true,
223 .controller_only =
true,
235 .controller_only =
true,
247 .controller_only =
false,
259 .controller_only =
false,
271 .controller_only =
false,
283 .controller_only =
false,
295 .controller_only =
true,
307 .controller_only =
true,
319 .controller_only =
false,
331 .controller_only =
false,
343 .controller_only =
false,
355 .controller_only =
false,
367 .controller_only =
true,
379 .controller_only =
true,
391 .controller_only =
false,
403 .controller_only =
false,
415 .controller_only =
false,
427 .controller_only =
false,
439 .controller_only =
true,
451 .controller_only =
true,
463 .controller_only =
false,
475 .controller_only =
false,
487 .controller_only =
false,
499 .controller_only =
false,
506 .letters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
511 .controller_only =
false,
523 .controller_only =
false,
535 .controller_only =
false,
547 .controller_only =
false,
559 .controller_only =
false,
571 .controller_only =
false,
583 .controller_only =
false,
595 .controller_only =
false,
607 .controller_only =
false,
616 .token =
"FACTORY RESTORE",
619 .controller_only =
false,
634 const auto i =
static_cast<std::size_t
>(c);
651 std::array<uint8_t, kMaxRequest>& buf;
655 void Put(
char ch)
noexcept {
656 if (pos < buf.size()) {
657 buf[pos] = ::alicat_basis2::detail::ByteOf(ch);
664 void PutText(
const char* s)
noexcept {
665 for (std::size_t i = 0U; !::alicat_basis2::detail::SameChar(s[i],
'\0'); ++i) {
672[[nodiscard]]
constexpr int64_t Pow10(uint32_t n)
noexcept {
674 for (uint32_t i = 0U; i < n; ++i) {
681inline void PutUnsigned(Writer& w, uint64_t v)
noexcept {
682 std::array<char, 20> tmp{};
685 tmp[n] = ::alicat_basis2::detail::DigitChar(v);
688 }
while ((v != 0U) && (n < tmp.size()));
696[[nodiscard]]
constexpr int64_t Rescale(int64_t milli, uint32_t dec)
noexcept {
698 return milli * Pow10(dec - 3U);
700 const int64_t div = Pow10(3U - dec);
701 const int64_t half = div / 2;
702 return (milli >= 0) ? ((milli + half) / div) : -((-milli + half) / div);
707[[nodiscard]]
constexpr int64_t WireMilli(int64_t milli, uint32_t dec)
noexcept {
708 return (dec >= 3U) ? milli : (Rescale(milli, dec) * Pow10(3U - dec));
712inline void PutMilli(Writer& w, int64_t milli, uint32_t dec,
bool trim)
noexcept {
713 const int64_t v = Rescale(milli, dec);
714 const bool neg = v < 0;
715 const auto mag =
static_cast<uint64_t
>(neg ? -v : v);
716 const auto scale =
static_cast<uint64_t
>(Pow10(dec));
717 if (neg && (mag != 0U)) {
720 PutUnsigned(w, mag / scale);
721 uint64_t frac = mag % scale;
722 uint32_t digits = dec;
724 while ((digits > 0U) && ((frac % 10U) == 0U)) {
731 for (uint32_t i = digits; i > 0U; --i) {
732 w.Put(::alicat_basis2::detail::DigitChar(frac /
static_cast<uint64_t
>(Pow10(i - 1U))));
738[[nodiscard]]
constexpr bool IsRead(
Command c)
noexcept {
760 return std::ranges::any_of(kReads, [c](
Command r) {
return r == c; });
764[[nodiscard]]
constexpr bool BaudValid(int64_t b)
noexcept {
765 return (b == 4800) || (b == 9600) || (b == 19200) || (b == 38400) || (b == 57600) ||
770[[nodiscard]]
constexpr bool InSet(
char ch,
const char* set)
noexcept {
771 for (std::size_t i = 0U; !::alicat_basis2::detail::SameChar(set[i],
'\0'); ++i) {
772 if (::alicat_basis2::detail::SameChar(set[i], ch)) {
780[[nodiscard]]
constexpr bool ArgsValid(
const CommandSpec& s,
const Args& a)
noexcept {
781 const bool in_range = (a.a >= s.lo) && (a.a <= s.hi);
789 return in_range && (a.dec <= 6U);
791 return (!::alicat_basis2::detail::SameChar(a.c,
'\0')) && InSet(a.c, s.letters);
793 return in_range && (a.dec <= 6U) && (a.b >= 3) && (a.b <= 5);
795 return in_range && (a.b >= s.lo) && (a.b <= s.hi);
803inline void PutArgs(Writer& w,
const CommandSpec& s,
const Args& a)
noexcept {
806 PutUnsigned(w,
static_cast<uint64_t
>(a.a));
809 PutMilli(w, a.a, a.dec, a.trim);
818 PutMilli(w, a.a, a.dec, a.trim);
820 PutUnsigned(w,
static_cast<uint64_t
>(a.b));
823 PutUnsigned(w,
static_cast<uint64_t
>(a.a));
825 PutUnsigned(w,
static_cast<uint64_t
>(a.b));
850 bool allow_guarded)
noexcept {
851 if (
static_cast<std::size_t
>(command) >=
kCommandRows) {
855 if (!::alicat_basis2::detail::IsUpper(
id) || (s.
guarded && !allow_guarded) ||
856 !detail::ArgsValid(s, args)) {
859 ::alicat_basis2::detail::RequestData d{};
860 detail::Writer w{.buf = d.bytes};
867 detail::PutArgs(w, s, args);
873 d.length =
static_cast<uint8_t
>(w.pos);
879 d.idempotent = detail::IsRead(command);
889namespace detail::legal {
892using View = std::span<const char>;
895[[nodiscard]]
constexpr bool Eq(View v,
const char* s)
noexcept {
897 while ((!::alicat_basis2::detail::SameChar(s[i],
'\0')) && (i < v.size()) &&
898 (::alicat_basis2::detail::SameChar(v[i], s[i]))) {
901 return (::alicat_basis2::detail::SameChar(s[i],
'\0')) && (i == v.size());
905[[nodiscard]]
constexpr bool After(View v,
const char* p, View& rest)
noexcept {
907 while ((!::alicat_basis2::detail::SameChar(p[i],
'\0')) && (i < v.size()) &&
908 (::alicat_basis2::detail::SameChar(v[i], p[i]))) {
911 if (!::alicat_basis2::detail::SameChar(p[i],
'\0')) {
919[[nodiscard]]
constexpr bool IntIn(View v, int64_t lo, int64_t hi)
noexcept {
920 if (v.empty() || (v.size() > 12U) ||
921 ((::alicat_basis2::detail::SameChar(v[0],
'0')) && (v.size() > 1U))) {
925 for (
const char ch : v) {
926 const int32_t digit = ::alicat_basis2::detail::DigitValue(ch);
930 n = (n * 10) + digit;
932 return (n >= lo) && (n <= hi);
938[[nodiscard]]
constexpr bool DecIn(View v, int64_t lo_milli, int64_t hi_milli)
noexcept {
939 std::size_t dot = v.size();
940 for (std::size_t i = 0U; i < v.size(); ++i) {
941 if (::alicat_basis2::detail::SameChar(v[i],
'.')) {
946 const View ip = v.first(dot);
947 if (!IntIn(ip, 0, 999'999'999'999LL)) {
951 for (
const char ch : ip) {
952 milli = (milli * 10) + ::alicat_basis2::detail::DigitValue(ch);
955 if (dot < v.size()) {
956 const View fr = v.subspan(dot + 1U);
957 if (fr.empty() || (fr.size() > 6U)) {
961 for (
const char ch : fr) {
962 const int32_t digit = ::alicat_basis2::detail::DigitValue(ch);
966 milli += (scale >= 1) ? (digit * scale) : 0;
970 if ((fr.size() > 3U) && (::alicat_basis2::detail::DigitValue(fr[3]) >= 5)) {
974 return (milli >= lo_milli) && (milli <= hi_milli);
978[[nodiscard]]
constexpr bool NoArg(View r)
noexcept {
979 constexpr std::array<const char*, 22> kWords{
980 "",
"C",
"T",
"SN",
"VE",
"MA",
"NCB",
"P",
"P2",
"LSS",
"SR",
981 "WD",
"LCG",
"RT",
"DCA",
"MT",
"DVAA",
"DVAR",
"ZCA",
"GS",
"TC",
"FACTORY RESTORE"};
982 return std::ranges::any_of(kWords, [r](
const char* w) {
return Eq(r, w); });
993[[nodiscard]]
constexpr bool OneInt(View r)
noexcept {
994 constexpr std::array<IntForm, 14> kForms{{{.prefix =
"DV ", .lo = 1, .hi = 255},
995 {.prefix =
"DVAS ", .lo = 1, .hi = 163'837},
996 {.prefix =
"WD ", .lo = 0, .hi = 5000},
997 {.prefix =
"DCA ", .lo = 0, .hi = 2500},
998 {.prefix =
"MT ", .lo = 0, .hi = 7},
999 {.prefix =
"V ", .lo = 1, .hi = 32767},
1000 {.prefix =
"ZCA ", .lo = 0, .hi = 1},
1001 {.prefix =
"GS ", .lo = 0, .hi = 255},
1002 {.prefix =
"TC ", .lo = 0, .hi = 3},
1003 {.prefix =
"FPF ", .lo = 0, .hi = 2},
1004 {.prefix =
"MA ", .lo = 1, .hi = 247},
1005 {.prefix =
"P ", .lo = 1, .hi = 1},
1006 {.prefix =
"SR ", .lo = 0, .hi = 0},
1007 {.prefix =
"TB ", .lo = 0, .hi = 0}}};
1009 for (
const IntForm& f : kForms) {
1010 if (After(r, f.prefix, rest) && IntIn(rest, f.lo, f.hi)) {
1018[[nodiscard]]
constexpr bool DecimalArg(View r)
noexcept {
1020 if (After(r,
"S ", rest)) {
1021 return DecIn(rest, 0, 999'999'999'999LL);
1023 if (After(r,
"HPUR ", rest)) {
1024 return DecIn(rest, 0, 100'000);
1026 if (After(r,
"RT ", rest)) {
1027 return DecIn(rest, 0, 30'000);
1029 return After(r,
"TB ", rest) && DecIn(rest, 1, 999'999'999'999LL);
1033[[nodiscard]]
constexpr bool BaudArg(View r)
noexcept {
1035 if (!After(r,
"NCB ", rest)) {
1038 return Eq(rest,
"4800") || Eq(rest,
"9600") || Eq(rest,
"19200") || Eq(rest,
"38400") ||
1039 Eq(rest,
"57600") || Eq(rest,
"115200");
1043[[nodiscard]]
constexpr bool OtherArg(View r)
noexcept {
1045 if (DecimalArg(r) || BaudArg(r) || Eq(r,
"GS *")) {
1048 if (After(r,
"LSS ", rest)) {
1049 return (rest.size() == 1U) && ((::alicat_basis2::detail::SameChar(rest[0],
'A')) ||
1050 (::alicat_basis2::detail::SameChar(rest[0],
'S')) ||
1051 (::alicat_basis2::detail::SameChar(rest[0],
'U')));
1053 return After(r,
"@=", rest) && (rest.size() == 1U) && ::alicat_basis2::detail::IsUpper(rest[0]);
1057[[nodiscard]]
constexpr bool AllowedChar(
char ch)
noexcept {
1058 const bool alnum = ::alicat_basis2::detail::IsUpper(ch) || ::alicat_basis2::detail::IsDigit(ch);
1059 return alnum || (::alicat_basis2::detail::SameChar(ch,
' ')) ||
1060 (::alicat_basis2::detail::SameChar(ch,
'+')) ||
1061 (::alicat_basis2::detail::SameChar(ch,
'-')) ||
1062 (::alicat_basis2::detail::SameChar(ch,
'.')) ||
1063 (::alicat_basis2::detail::SameChar(ch,
'*')) ||
1064 (::alicat_basis2::detail::SameChar(ch,
'@')) ||
1065 (::alicat_basis2::detail::SameChar(ch,
'='));
1069[[nodiscard]]
constexpr bool TwoArgs(View r)
noexcept {
1071 std::size_t sp = 0U;
1072 if (After(r,
"SR ", rest)) {
1073 while ((sp < rest.size()) && (!::alicat_basis2::detail::SameChar(rest[sp],
' '))) {
1076 return (sp < rest.size()) && DecIn(rest.first(sp), 1, 999'999'999'999LL) &&
1077 IntIn(rest.subspan(sp + 1U), 3, 5);
1079 if (After(r,
"LCG ", rest)) {
1080 while ((sp < rest.size()) && (!::alicat_basis2::detail::SameChar(rest[sp],
' '))) {
1083 return (sp < rest.size()) && IntIn(rest.first(sp), 0, 65535) &&
1084 IntIn(rest.subspan(sp + 1U), 0, 65535);
1103 if ((bytes.size() < 2U) || (bytes.size() >
kMaxRequest) ||
1104 (bytes.back() != ::alicat_basis2::detail::ByteOf(
kCr))) {
1107 std::array<char, kMaxRequest> text{};
1108 const std::size_t n = bytes.size() - 1U;
1109 for (std::size_t i = 0U; i < n; ++i) {
1110 const char ch = ::alicat_basis2::detail::CharOf(bytes[i]);
1111 if (!detail::legal::AllowedChar(ch)) {
1116 if (!::alicat_basis2::detail::IsUpper(text[0])) {
1119 const detail::legal::View rest{text.data() + 1, n - 1U};
1120 return detail::legal::NoArg(rest) || detail::legal::OneInt(rest) ||
1121 detail::legal::OtherArg(rest) || detail::legal::TwoArgs(rest);
1155 while (i < line.size()) {
1156 const uint8_t ch = ::alicat_basis2::detail::ByteOf(line[i]);
1157 if (::alicat_basis2::detail::SameChar(line[i],
' ')) {
1159 }
else if ((ch > 0x20U) && (ch < 0x7FU)) {
1160 const std::size_t start = i;
1161 while ((i < line.size()) && (!::alicat_basis2::detail::SameChar(line[i],
' '))) {
1162 const uint8_t c2 = ::alicat_basis2::detail::ByteOf(line[i]);
1163 if ((c2 <= 0x20U) || (c2 >= 0x7FU)) {
1171 out.items[out.count] =
1172 Token{.
offset =
static_cast<uint8_t
>(start), .length =
static_cast<uint8_t
>(i - start)};
1187[[nodiscard]]
inline std::span<const char>
View(std::span<const char> line,
1188 const Token& t)
noexcept {
1189 return line.subspan(t.offset, t.length);
1195inline std::size_t ScanDigits(std::span<const char> tok, std::size_t& i, int64_t& m,
1196 std::size_t cap)
noexcept {
1198 while ((i < tok.size()) && (n < cap) && ::alicat_basis2::detail::IsDigit(tok[i])) {
1199 m = (m * 10) + ::alicat_basis2::detail::DigitValue(tok[i]);
1217 if (!tok.empty() && ((::alicat_basis2::detail::SameChar(tok[0],
'+')) ||
1218 (::alicat_basis2::detail::SameChar(tok[0],
'-')))) {
1219 neg = ::alicat_basis2::detail::SameChar(tok[0],
'-');
1225 if (detail::ScanDigits(tok, i, m, 12U) == 0U) {
1229 if ((i < tok.size()) && (::alicat_basis2::detail::SameChar(tok[i],
'.'))) {
1231 dec =
static_cast<uint8_t
>(detail::ScanDigits(tok, i, m, 6U));
1236 if (i != tok.size()) {
1245[[nodiscard]]
constexpr char Upper(
char c)
noexcept {
1246 const int32_t place = ::alicat_basis2::detail::PlaceOf(::alicat_basis2::detail::kLowerChars, c);
1247 return (place < 0) ? c : ::alicat_basis2::detail::kUpperChars[
static_cast<std::size_t
>(place)];
1256[[nodiscard]]
inline bool EqualsNoCase(std::span<const char> tok,
const char* word)
noexcept {
1258 while ((!::alicat_basis2::detail::SameChar(word[i],
'\0')) && (i < tok.size()) &&
1259 (::alicat_basis2::detail::SameChar(
Upper(tok[i]),
Upper(word[i])))) {
1262 return (::alicat_basis2::detail::SameChar(word[i],
'\0')) && (i == tok.size());
1274 return (toks.count > 0U) && (toks.items[0].length == 1U) &&
1275 (::alicat_basis2::detail::SameChar(
Upper(line[toks.items[0].offset]),
id));
1293inline void CopyToken(std::span<const char> tok,
char* dst, std::size_t cap)
noexcept {
1294 std::size_t n = (tok.size() < (cap - 1U)) ? tok.size() : (cap - 1U);
1295 for (std::size_t i = 0U; i < n; ++i) {
1302[[nodiscard]]
inline bool ParseGasName(std::span<const char> tok, Frame& f)
noexcept {
1303 if (tok.empty() || (tok.size() > 8U)) {
1306 for (
const char ch : tok) {
1307 if (!::alicat_basis2::detail::IsAlnum(ch)) {
1311 CopyToken(tok, f.gas_name.data(), f.gas_name.size());
1312 f.gas_known =
false;
1313 for (uint8_t n = 0U; n < 9U; ++n) {
1323[[nodiscard]]
inline bool SetFlag(std::span<const char> name, StatusFlags& s)
noexcept {
1324 bool* target =
nullptr;
1326 target = &s.temperature_over;
1328 target = &s.mass_flow_over;
1330 target = &s.totalizer_over;
1332 target = &s.valve_hold;
1334 target = &s.valve_thermal;
1336 if (s.unknown_count < 255U) {
1349[[nodiscard]]
inline bool ParseFlagToken(std::span<const char> tok, StatusFlags& s)
noexcept {
1353 if (!::alicat_basis2::detail::SameChar(tok[0],
'[')) {
1354 return SetFlag(tok, s);
1357 while (i < tok.size()) {
1358 if (!::alicat_basis2::detail::SameChar(tok[i],
'[')) {
1361 std::size_t j = i + 1U;
1362 while ((j < tok.size()) && (!::alicat_basis2::detail::SameChar(tok[j],
']'))) {
1365 if ((j >= tok.size()) || (j == (i + 1U)) || !SetFlag(tok.subspan(i + 1U, j - i - 1U), s)) {
1374[[nodiscard]]
inline bool Carries(
const Frame& f,
DataItem item)
noexcept {
1375 return (
static_cast<uint32_t
>(f.present) &
static_cast<uint32_t
>(item)) != 0U;
1379[[nodiscard]]
inline Error CheckMeasured(
const Frame& f,
const FrameRules& r)
noexcept {
1381 const int64_t t_milli = f.temperature.ToMilli();
1382 if ((t_milli < (
static_cast<int64_t
>(r.temperature_min_c) * 1000)) ||
1383 (t_milli > (
static_cast<int64_t
>(r.temperature_max_c) * 1000))) {
1388 const int64_t limit = (r.full_scale_milli * r.flow_margin_permille) / 1000;
1389 const int64_t flow = f.flow.ToMilli();
1390 if ((flow > limit) || (flow < -limit)) {
1395 const int64_t sp = f.setpoint.ToMilli();
1396 if ((sp < 0) || (sp > ((r.full_scale_milli * 1025) / 1000))) {
1404[[nodiscard]]
inline Error CheckFrame(
const Frame& f,
const FrameRules& r)
noexcept {
1409 ((f.valve_drive.ToMilli() < -500) || (f.valve_drive.ToMilli() > 100'500))) {
1422[[nodiscard]]
inline bool ParseNumbers(std::span<const char> line,
const Tokens& t,
1423 std::size_t first, std::size_t n, Decimal* out)
noexcept {
1424 for (std::size_t k = 0U; k < n; ++k) {
1433[[nodiscard]]
inline bool ParseFlags(std::span<const char> line,
const Tokens& t, std::size_t first,
1435 for (std::size_t k = first; k < t.count; ++k) {
1436 if (!ParseFlagToken(
View(line, t.items[k]), s)) {
1447[[nodiscard]]
inline std::size_t LeadingNumbers(std::span<const char> line,
const Tokens& t,
1448 std::array<Decimal, 5>& v)
noexcept {
1449 std::size_t numbers = 0U;
1450 while ((numbers < 5U) && ((numbers + 1U) < t.count) &&
1476 std::array<Decimal, 5> v{};
1477 const std::size_t numbers = detail::LeadingNumbers(line, t, v);
1478 if (((numbers != 3U) && (numbers != 5U)) || ((numbers + 1U) >= t.count)) {
1487 if (!detail::ParseGasName(
View(line, t.items[numbers + 1U]), f) ||
1488 !detail::ParseFlags(line, t, numbers + 2U, rules.flags, f.status)) {
1491 f.temperature = v[0];
1496 if (numbers == 5U) {
1498 f.valve_drive = v[4];
1501 if (
const Error e = detail::CheckFrame(f, rules); e !=
Error::Ok) {
1512[[nodiscard]]
inline Decimal* DataField(
Frame& f, uint32_t bit)
noexcept {
1519 return &f.temperature;
1521 return &f.valve_drive;
1525 return &f.batch_remaining;
1536[[nodiscard]]
inline bool ParseStatusTail(std::span<const char> line,
const Tokens& t,
1537 std::size_t k,
const FrameRules& rules,
1538 Frame& f)
noexcept {
1541 (bits.decimals == 0U) && (bits.mantissa >= 0) && (bits.mantissa < 256)) {
1545 return ParseFlags(line, t, k, rules.flags, f.status);
1574 for (uint32_t bit = 1U; bit <= 64U; bit <<= 1U) {
1575 if ((
static_cast<uint32_t
>(mask) & bit) == 0U) {
1581 Decimal* field = detail::DataField(f, bit);
1582 const bool ok = (field !=
nullptr) ?
ParseDecimal(
View(line, t.items[k]), *field)
1583 : detail::ParseGasName(
View(line, t.items[k]), f);
1589 if ((
static_cast<uint32_t
>(mask) &
static_cast<uint32_t
>(
DataItem::Status)) != 0U) {
1590 if (!detail::ParseStatusTail(line, t, k, rules, f)) {
1593 }
else if (k != t.count) {
1598 if (
const Error e = detail::CheckFrame(f, rules); e !=
Error::Ok) {
1607 std::array<Decimal, 5>
v{};
1628 if ((count < 1U) || (count > 5U) || (t.count != (count + 1U))) {
1632 if (!detail::ParseNumbers(line, t, 1U, count, vals.v.data())) {
1649 char& out)
noexcept {
1657 if ((t.count != 2U) || (t.items[1].length != 1U) ||
1658 !detail::InSet(
Upper(line[t.items[1].offset]), allowed)) {
1661 out =
Upper(line[t.items[1].offset]);
1684 const std::size_t start = t.
items[1].offset;
1686 std::size_t n = line.size() - start;
1688 while ((n > 0U) && (::alicat_basis2::detail::SameChar(line[start + n - 1U],
' '))) {
1691 detail::CopyToken(line.subspan(start, n), x.chars.data(), x.chars.size());
1692 x.length =
static_cast<uint8_t
>(n);
1714 if ((t.count != 3U) || !
ParseDecimal(
View(line, t.items[1]), n) || (n.decimals != 0U) ||
1715 (n.mantissa < 0) || (n.mantissa > 255) || !detail::ParseGasName(
View(line, t.items[2]), f)) {
1718 f.
gas =
GasId{
static_cast<uint8_t
>(n.mantissa)};
1746 detail::CopyToken(
View(line, t.items[2]), fs.unit.chars.data(), fs.unit.chars.size());
1747 fs.unit.length = t.items[2].length;
1759 for (uint16_t i = 0U; i < 20U; ++i) {
1784 bool parsed = (t.count > 1U) && (((t.count - 1U) % 2U) == 0U) && (((t.count - 1U) / 2U) <= 12U);
1785 for (std::size_t k = 1U; parsed && ((k + 1U) < t.count); k += 2U) {
1788 parsed =
ParseDecimal(
View(line, t.items[k]), n) && (n.decimals == 0U) && (n.mantissa >= 0) &&
1789 (n.mantissa <= 255) && detail::ParseGasName(
View(line, t.items[k + 1U]), f);
1791 g.numbers[g.count] =
static_cast<uint8_t
>(n.mantissa);
1799 detail::CopyToken(line.first(n), g.text.chars.data(), g.text.chars.size());
1800 g.text.length =
static_cast<uint8_t
>(n);
1816 uint32_t& time_ms,
Values& out)
noexcept {
1818 if (
const Error e =
DecodeValues(line,
id,
static_cast<uint8_t
>(count + 1U), all);
1826 time_ms =
static_cast<uint32_t
>(tm.
mantissa);
1828 for (uint8_t i = 0U; i < count; ++i) {
1829 out.
v[i] = all.v[i + 1U];
1841[[nodiscard]]
inline bool IsErrorToken(std::span<const char> line,
char id)
noexcept {
1846 const bool alone = (t.count == 1U) && (t.items[0].length == 1U) &&
1847 (::alicat_basis2::detail::SameChar(line[t.items[0].offset],
'?'));
1848 const bool after_id = (t.count == 2U) &&
StartsWithId(line, t,
id) && (t.items[1].length == 1U) &&
1849 (::alicat_basis2::detail::SameChar(line[t.items[1].offset],
'?'));
1850 return alone || after_id;
ALICAT_BASIS2 Modbus register map, magic values, firmware gates and unit tables (M pp....
Request and Reply: the only things the driver puts on and takes off the wire, and the timing formulas...
Public types of the ALICAT_BASIS2 driver: errors, Result<T>, configuration, enumerations and the valu...
bool EqualsNoCase(std::span< const char > tok, const char *word) noexcept
Case-insensitive equality of a token and a word.
Definition alicat_basis2_ascii.hpp:1256
FlowUnit FlowUnitFromLabel(std::span< const char > label) noexcept
The flow unit of an instrument's label (case-insensitive), Other when it is not one of the twenty.
Definition alicat_basis2_ascii.hpp:1758
Result< Request > Make(Command command, char id, const Args &args, bool allow_guarded) noexcept
Build a request: validate every argument, write the wire bytes.
Definition alicat_basis2_ascii.hpp:849
Error DecodeGasList(std::span< const char > line, char id, GasList &out) noexcept
Decode a gas list ID n name n name ... (C28, A5); sets raw when it does not parse.
Definition alicat_basis2_ascii.hpp:1774
bool IsLegalRequest(std::span< const uint8_t > bytes) noexcept
Whether bytes is exactly one request of the grammar of M pp.10 to 17: unit ID A to Z,...
Definition alicat_basis2_ascii.hpp:1102
constexpr const CommandSpec & Spec(Command c) noexcept
The row of a command.
Definition alicat_basis2_ascii.hpp:633
Error DecodeFullScale(std::span< const char > line, char id, FullScale &out) noexcept
Decode a full-scale reply ID value units (C35).
Definition alicat_basis2_ascii.hpp:1733
std::span< const char > View(std::span< const char > line, const Token &t) noexcept
The characters of a token.
Definition alicat_basis2_ascii.hpp:1187
ArgKind
What a command's argument looks like.
Definition alicat_basis2_ascii.hpp:37
Error DecodeText(std::span< const char > line, char id, Text &out) noexcept
Decode ID text...: the rest of the line after the ID, truncated to kTextMax characters.
Definition alicat_basis2_ascii.hpp:1673
constexpr char Upper(char c) noexcept
Upper-case a letter.
Definition alicat_basis2_ascii.hpp:1245
constexpr std::size_t kCommandRows
Number of rows of the command table.
Definition alicat_basis2_ascii.hpp:64
Error DecodeGas(std::span< const char > line, char id, Frame &out) noexcept
Decode a gas reply ID number name (C27, C29).
Definition alicat_basis2_ascii.hpp:1704
Error DecodeValues(std::span< const char > line, char id, uint8_t count, Values &out) noexcept
Decode ID followed by exactly count numbers.
Definition alicat_basis2_ascii.hpp:1619
constexpr std::array< CommandSpec, kCommandRows > kCommands
The command table, indexed by Command (data; one row per command of the coverage map).
Definition alicat_basis2_ascii.hpp:74
Error DecodeData(std::span< const char > line, uint8_t mask, const FrameRules &rules, Frame &out) noexcept
Decode a DV n reply: ID, then the selected fields in table order (M p.11).
Definition alicat_basis2_ascii.hpp:1561
Error DecodeLetter(std::span< const char > line, char id, const char *allowed, char &out) noexcept
Decode ID x where x is one character out of allowed (case-insensitive).
Definition alicat_basis2_ascii.hpp:1648
Error Tokenize(std::span< const char > line, Tokens &out) noexcept
Split a line on runs of spaces.
Definition alicat_basis2_ascii.hpp:1152
constexpr char kCr
Carriage return, the terminator of every request and reply (M p.10).
Definition alicat_basis2_ascii.hpp:34
bool IsErrorToken(std::span< const char > line, char id) noexcept
Whether a reply line is the error token: ? alone, or ? after the unit ID.
Definition alicat_basis2_ascii.hpp:1841
constexpr std::size_t kMaxTokens
Tokens in a line, at most.
Definition alicat_basis2_ascii.hpp:1129
constexpr std::size_t kMaxToken
Longest token, in characters.
Definition alicat_basis2_ascii.hpp:1131
constexpr int64_t kMeasureMaxMs
Longest measurement duration in ms the driver sends (derived: 65535 samples of 2.5 ms,...
Definition alicat_basis2_ascii.hpp:70
bool StartsWithId(std::span< const char > line, const Tokens &toks, char id) noexcept
Whether the first token is exactly one letter equal (any case) to the unit ID.
Definition alicat_basis2_ascii.hpp:1272
Error DecodeFrame(std::span< const char > line, const FrameRules &rules, Frame &out, FrameLayout &layout_seen) noexcept
Decode a data frame (C01): ID, three or five numbers, the gas, zero or more flags (M p....
Definition alicat_basis2_ascii.hpp:1467
constexpr int64_t kMilliCap
Definition alicat_basis2_ascii.hpp:68
Error DecodeWindow(std::span< const char > line, char id, uint8_t count, uint32_t &time_ms, Values &out) noexcept
Decode ID time ... replies of DVAA (two values after the time) and DVAR (four).
Definition alicat_basis2_ascii.hpp:1815
bool ParseDecimal(std::span< const char > tok, Decimal &out) noexcept
Parse ‘[+-]? digit{1,12} ( ’.' digit{1,6} )?: no exponent, no hex, nonan`, no leading or trailing poi...
Definition alicat_basis2_ascii.hpp:1214
@ Milli
One decimal number, given in thousandths, in lo to hi.
@ Int
One integer in lo to hi.
@ Letter
One character out of letters.
@ IntPair
Two integers, each in lo to hi.
@ Ramp
A decimal rate then a time unit 3 to 5.
constexpr std::array< const char *, 20 > kFlowUnitLabels
Flow unit labels in the order of register 49 (M p.26, p.31).
Definition alicat_basis2_registers.hpp:391
constexpr uint16_t kFw305
3.0.5: gas table and GS *.
Definition alicat_basis2_registers.hpp:88
constexpr std::size_t kMaxRequest
Longest request the driver builds, in bytes.
Definition alicat_basis2_request.hpp:83
Command
The 46 ASCII commands of M pp.10 to 17 (rows C01 to C46 of the coverage map), plus the Modbus kind.
Definition alicat_basis2_request.hpp:32
@ SwitchToProtocol1
C44 aP 1 (guarded).
@ ReadFullScale
C35 aFPF v.
@ SetSetpointSource
C05 aLSS x.
@ ReadReferenceTemperature
C15 aRT.
@ SwitchToProtocol2
C45 aP2.
@ ReadSetpointSource
C04 aLSS.
@ StartMeasurement
C21 aDVAS t.
@ SetTotalizerLimit
C34 aTC m.
@ ChangeBaud
C42 aNCB b (guarded).
@ ReadModbusAddress
C39 aMA.
@ FactoryRestore
C46 aFACTORY RESTORE (guarded).
@ ReadTotalizerLimit
C33 aTC.
@ SetModbusAddress
C40 aMA n (guarded).
@ SetReferenceTemperature
C16 aRT t.
@ SetAveraging
C18 aDCA t.
@ SetAutotare
C26 aZCA 1 or aZCA 0.
@ ChangeUnitId
C36 a@=b (guarded).
@ ReadSerialNumber
C37 aSN.
Error
Why a call failed. Every call of the driver returns one of these in its Result.
Definition alicat_basis2_types.hpp:37
constexpr const char * GasName(uint8_t number) noexcept
The name of a gas of the nine of M p.15.
Definition alicat_basis2_types.hpp:438
constexpr std::size_t kTextMax
Maximum characters kept of a text reply.
Definition alicat_basis2_types.hpp:615
UnknownFlagPolicy
What happens with a flag token the manual does not list.
Definition alicat_basis2_types.hpp:242
FrameLayout
Which data frame layout is accepted (assumption A2: a meter's frame is not shown in the manual).
Definition alicat_basis2_types.hpp:235
DataItem
Items of DV (a bit mask, M p.11).
Definition alicat_basis2_types.hpp:495
FlowUnit
Flow units (Modbus register 49, M p.26; M p.31).
Definition alicat_basis2_types.hpp:540
constexpr StatusFlags StatusFromBits(uint32_t bits) noexcept
Decode status bits (Modbus register 2101 or an integer DV status): +1 MOV, +2 TOV,...
Definition alicat_basis2_types.hpp:587
@ InvalidArgument
An argument or a Config field is out of range; nothing was sent.
@ OutOfRange
A decoded value is outside what the instrument can physically report.
@ DeviceIdMismatch
The reply carried another unit ID or Modbus address than the one addressed.
@ BadReply
The line does not match the command's reply grammar.
@ Report
Count it in StatusFlags::unknown_count and return the frame.
@ Auto
Learn from the first valid frame, reject a later frame of the other layout.
@ Meter
Three numbers, then the gas, then the flags.
@ Controller
Five numbers, then the gas, then the flags.
@ Other
A label not in the table of M p.31; the raw text is kept.
The ASCII grammar: table, encoder, validator, decoders.
Definition alicat_basis2_ascii.hpp:25
An exact decimal number as the instrument printed it: +0021513.0 is {215130, 1}.
Definition alicat_basis2_types.hpp:375
uint8_t decimals
Digits after the point.
Definition alicat_basis2_types.hpp:377
int64_t mantissa
The number without its decimal point.
Definition alicat_basis2_types.hpp:376
One data frame (A) or a DV selection; present says which fields were read.
Definition alicat_basis2_types.hpp:599
GasId gas
Valid when gas_known.
Definition alicat_basis2_types.hpp:608
char unit_id
The unit ID of the reply, upper case.
Definition alicat_basis2_types.hpp:601
One reading of FPF: a value and its unit text.
Definition alicat_basis2_types.hpp:666
A gas number. Not a closed enumeration: the V3.0.5 gas table allows numbers up to 255 (A5).
Definition alicat_basis2_types.hpp:414
Result of ReadGasList(): the parsed pairs, or the raw text when the grammar did not match (A5).
Definition alicat_basis2_types.hpp:685
bool raw
The text did not parse; text holds it.
Definition alicat_basis2_types.hpp:688
Result of ReadRamp(): rate and time unit as the instrument reported them.
Definition alicat_basis2_types.hpp:624
The value of a call, or why there is none.
Definition alicat_basis2_types.hpp:151
static constexpr Result Failure(Error e) noexcept
A failed result.
Definition alicat_basis2_types.hpp:177
static constexpr Result Success(T v) noexcept
A successful result.
Definition alicat_basis2_types.hpp:169
A short text value (serial number, firmware, unit label), NUL terminated.
Definition alicat_basis2_types.hpp:618
The arguments of a request before encoding.
Definition alicat_basis2_ascii.hpp:639
bool trim
Drop trailing zeros of the fraction.
Definition alicat_basis2_ascii.hpp:644
int64_t b
Second number (Ramp time unit, IntPair second value).
Definition alicat_basis2_ascii.hpp:641
int64_t a
First number: an integer, or thousandths for a decimal argument.
Definition alicat_basis2_ascii.hpp:640
uint8_t dec
Digits printed after the point of a decimal argument (0 to 6).
Definition alicat_basis2_ascii.hpp:643
char c
Character argument.
Definition alicat_basis2_ascii.hpp:642
One row of the command table (rows C01 to C46).
Definition alicat_basis2_ascii.hpp:48
int64_t hi
Highest argument value.
Definition alicat_basis2_ascii.hpp:59
const char * letters
Allowed characters of a Letter argument.
Definition alicat_basis2_ascii.hpp:60
int64_t lo
Lowest argument value.
Definition alicat_basis2_ascii.hpp:58
const char * token
The text after the unit ID.
Definition alicat_basis2_ascii.hpp:50
uint16_t min_firmware
Firmware gate for the ASCII command, 0 for none.
Definition alicat_basis2_ascii.hpp:54
bool slow
May write non-volatile memory (longer reply wait).
Definition alicat_basis2_ascii.hpp:56
bool guarded
Needs a guard token.
Definition alicat_basis2_ascii.hpp:57
ArgKind arg
The argument form.
Definition alicat_basis2_ascii.hpp:51
bool controller_only
The manual says controllers only.
Definition alicat_basis2_ascii.hpp:53
uint16_t reply_max
Most characters of the reply line.
Definition alicat_basis2_ascii.hpp:55
Command id
The command.
Definition alicat_basis2_ascii.hpp:49
bool space
A space between the token and the argument.
Definition alicat_basis2_ascii.hpp:52
The rules a decoded frame is checked against.
Definition alicat_basis2_ascii.hpp:1279
UnknownFlagPolicy flags
Unknown flag policy.
Definition alicat_basis2_ascii.hpp:1282
char unit_id
Addressed ID.
Definition alicat_basis2_ascii.hpp:1280
int64_t full_scale_milli
Full scale flow in thousandths.
Definition alicat_basis2_ascii.hpp:1284
uint16_t flow_margin_permille
Flow plausibility.
Definition alicat_basis2_ascii.hpp:1285
int16_t temperature_min_c
Temperature plausibility.
Definition alicat_basis2_ascii.hpp:1286
FrameLayout layout
Layout accepted (learned or configured).
Definition alicat_basis2_ascii.hpp:1281
int16_t temperature_max_c
Temperature plausibility.
Definition alicat_basis2_ascii.hpp:1287
bool full_scale_known
full_scale_milli is valid.
Definition alicat_basis2_ascii.hpp:1283
One token: where it starts and how long it is.
Definition alicat_basis2_ascii.hpp:1134
uint8_t offset
Index of the first character.
Definition alicat_basis2_ascii.hpp:1135
uint8_t length
Number of characters.
Definition alicat_basis2_ascii.hpp:1136
The tokens of a line.
Definition alicat_basis2_ascii.hpp:1140
uint8_t count
Their number.
Definition alicat_basis2_ascii.hpp:1142
std::array< Token, kMaxTokens > items
The tokens.
Definition alicat_basis2_ascii.hpp:1141
The numbers of a short reply: ID v1 [v2 ...].
Definition alicat_basis2_ascii.hpp:1606
std::array< Decimal, 5 > v
The numbers.
Definition alicat_basis2_ascii.hpp:1607
uint8_t count
How many.
Definition alicat_basis2_ascii.hpp:1608