#include "logic/page_schedule.h" #include #include #include static bool terminated(const char *text, size_t size) { return memchr(text, '\0', size) != nullptr; } static bool validAttribute(const char *attribute) { for (const char *c = attribute; *c; c++) if (!isalnum((unsigned char)*c) && *c != '_') return false; return true; } const char *validateSchedule(const PageSchedule &s) { if (s.block_count > SCHEDULE_BLOCKS) return "Too many schedule blocks (8 at most)"; if (s.rule_count > TRIGGER_RULES) return "Too many rules (8 at most)"; for (int i = 0; i < s.block_count; i++) { const ScheduleBlock &b = s.blocks[i]; if (b.start >= 24 * 60) return "Blocks must start before 24:00"; if (i > 0 && b.start <= s.blocks[i - 1].start) return "Blocks must be in order, each at a different time"; if (b.item_count == 0 || b.item_count > BLOCK_ITEMS) return "Each block needs 1 to 6 pages"; for (int j = 0; j < b.item_count; j++) { const ScheduleItem &item = b.items[j]; if (!terminated(item.page, sizeof(item.page)) || !validBitmapName(item.page)) return "Choose a page for each item of a block"; if (item.seconds < ITEM_SECONDS_MIN || item.seconds > ITEM_SECONDS_MAX) return "Pages are shown 5 to 3600 seconds"; } } for (int i = 0; i < s.rule_count; i++) { const TriggerRule &r = s.rules[i]; if (!terminated(r.entity, sizeof(r.entity)) || !validEntityId(r.entity)) return "Rules need an entity id like sensor.printer_state"; if (!terminated(r.attribute, sizeof(r.attribute)) || !validAttribute(r.attribute)) return "Invalid attribute"; if (r.compare >= Compare::Count || r.and_compare >= Compare::Count || r.mode >= TriggerMode::Count) return "Unknown rule setting"; if (!terminated(r.value, sizeof(r.value))) return "Rule value too long (32 characters)"; if (!terminated(r.and_entity, sizeof(r.and_entity)) || (r.and_entity[0] && !validEntityId(r.and_entity))) return "Rules need an entity id like sensor.printer_state"; if (!terminated(r.and_attribute, sizeof(r.and_attribute)) || !validAttribute(r.and_attribute)) return "Invalid attribute"; if (!terminated(r.and_value, sizeof(r.and_value))) return "Rule value too long (32 characters)"; if (!terminated(r.page, sizeof(r.page)) || !validBitmapName(r.page)) return "Choose a page for each rule"; if (r.mode != TriggerMode::WhileTrue && (r.seconds < 1 || r.seconds > ITEM_SECONDS_MAX)) return "Rule times must be 1 to 3600 seconds"; if (r.interval < RULE_INTERVAL_MIN || r.interval > RULE_INTERVAL_MAX) return "Rules are read every 2 to 600 seconds"; if (r.blocks >> s.block_count) return "A rule is set for a block that doesn't exist"; } return nullptr; } static bool sameText(const char *a, const char *b) { for (; *a && *b; a++, b++) if (tolower((unsigned char)*a) != tolower((unsigned char)*b)) return false; return *a == *b; } // Whether `part` is somewhere in `text`, without case static bool hasText(const char *text, const char *part) { for (;; text++) { const char *a = text, *b = part; while (*a && *b && tolower((unsigned char)*a) == tolower((unsigned char)*b)) a++, b++; if (!*b) return true; if (!*text) return false; } } static bool number(const char *text, double &value) { char *end; value = strtod(text, &end); return end != text && *end == '\0'; } bool ruleInBlock(const TriggerRule &rule, int block) { return rule.blocks == 0 || block < 0 || (rule.blocks >> block) & 1; } static bool conditionTrue(Compare compare, const char *value, const char *wanted) { if (value == nullptr) return false; double a, b; switch (compare) { case Compare::Equal: return sameText(value, wanted); case Compare::NotEqual: return !sameText(value, wanted); case Compare::Above: return number(value, a) && number(wanted, b) && a > b; case Compare::Below: return number(value, a) && number(wanted, b) && a < b; case Compare::Contains: return hasText(value, wanted); case Compare::NotContains: return !hasText(value, wanted); default: return false; } } bool ruleMatches(const TriggerRule &rule, const char *value, const char *and_value) { return conditionTrue(rule.compare, value, rule.value) && (!rule.and_entity[0] || conditionTrue(rule.and_compare, and_value, rule.and_value)); } bool scheduleUses(const PageSchedule &s, const char *name) { for (int i = 0; i < s.block_count; i++) for (int j = 0; j < s.blocks[i].item_count; j++) if (sameName(s.blocks[i].items[j].page, name)) return true; for (int i = 0; i < s.rule_count; i++) if (sameName(s.rules[i].page, name)) return true; return false; }