| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | /* | ||
| 3 | * This file is part of the Falcon Player (FPP) and is Copyright (C) | ||
| 4 | * 2022 by the Falcon Player Developers. | ||
| 5 | * | ||
| 6 | * The Falcon Player (FPP) is free software, and is covered under | ||
| 7 | * multiple Open Source licenses. Please see the included 'LICENSES' | ||
| 8 | * file for descriptions of what files are covered by each license. | ||
| 9 | * | ||
| 10 | * This source file is covered under the CC-BY-ND as described in the | ||
| 11 | * included LICENSE.CC-BY-ND file. This file may be modified for | ||
| 12 | * personal use, but modified copies MAY NOT be redistributed in any form. | ||
| 13 | */ | ||
| 14 | |||
| 15 | #include <atomic> | ||
| 16 | #include <cstdint> | ||
| 17 | #include <functional> | ||
| 18 | #include "fpp-json-fwd.h" | ||
| 19 | #include <list> | ||
| 20 | #include <map> | ||
| 21 | #include <mutex> | ||
| 22 | #include <vector> | ||
| 23 | |||
| 24 | class PixelString; | ||
| 25 | class FalconV5Listener; | ||
| 26 | class PinCapabilities; | ||
| 27 | class PRUControl; | ||
| 28 | |||
| 29 | class FalconV5Support { | ||
| 30 | public: | ||
| 31 | FalconV5Support(); | ||
| 32 | ~FalconV5Support(); | ||
| 33 | |||
| 34 | class ReceiverChain { | ||
| 35 | public: | ||
| 36 | ReceiverChain(PixelString* p1, PixelString* p2, PixelString* p3, PixelString* p4, int grp = 0, int m = 0, bool sendOnly = false); | ||
| 37 | bool generateConfigPacket(uint8_t* packet) const; | ||
| 38 | bool generateNumberPackets(uint8_t* packet, uint8_t* packet2) const; | ||
| 39 | bool generateQueryPacket(uint8_t* packet, int receiver) const; | ||
| 40 | bool generateResetFusesPacket(uint8_t* packet) const; | ||
| 41 | bool generateToggleEFusePacket(uint8_t* packet, int receiver, int port) const; | ||
| 42 | bool generateResetEFusePacket(uint8_t* packet, int receiver, int port) const; | ||
| 43 | bool generateSetFusesPacket(uint8_t* packet, bool on) const; | ||
| 44 | |||
| 45 | 2433 | const std::array<const PixelString*, 4>& getPixelStrings() const { return strings; }; | |
| 46 | 31 | uint32_t getReceiverCount() const { return numReceivers; } | |
| 47 | |||
| 48 | // Falcon V4 handling: only the config packet is sent, no queries | ||
| 49 | // are scheduled and no responses are expected | ||
| 50 | 78 | bool isSendOnly() const { return sendOnly; } | |
| 51 | |||
| 52 | 1460 | bool hasMoreQueries() const { return curReceiverQuery < numReceivers; } | |
| 53 | 1457 | void resetQueryCount() { curReceiverQuery = 0; } | |
| 54 | bool generateQueryPacket(uint8_t* packet); | ||
| 55 | void handleQueryResponse(Json::Value& json); | ||
| 56 | bool generatePixelCountPacket(uint8_t* packet) const; | ||
| 57 | |||
| 58 | private: | ||
| 59 | std::array<const PixelString*, 4> strings; | ||
| 60 | uint32_t group; | ||
| 61 | uint32_t mux; | ||
| 62 | uint32_t numReceivers; | ||
| 63 | bool sendOnly; | ||
| 64 | |||
| 65 | uint32_t curReceiverQuery = 0; | ||
| 66 | }; | ||
| 67 | |||
| 68 | ReceiverChain* addReceiverChain(PixelString* p1, PixelString* p2, PixelString* p3, PixelString* p4, int group, int mux, bool sendOnly = false); | ||
| 69 | 92 | const std::list<ReceiverChain*>& getReceiverChains() const { return receiverChains; }; | |
| 70 | |||
| 71 | void addListeners(const Json::Value& config); | ||
| 72 | void setCurrentMux(int i); | ||
| 73 | |||
| 74 | // Copies a newly published listener capture out of the PRU shared RAM | ||
| 75 | // into a staging buffer. Safe to call from any thread, meant to be | ||
| 76 | // called often (the string driver's pump thread polls it every few | ||
| 77 | // hundred microseconds) so the capture is taken well before the next | ||
| 78 | // listen window can overwrite it; processListenerData() also calls it | ||
| 79 | // for platforms without a pump thread. | ||
| 80 | void stageListenerData(); | ||
| 81 | // Decodes the staged capture (if any) and delivers the receiver status | ||
| 82 | // to the OutputMonitor. Output thread only. | ||
| 83 | void processListenerData(); | ||
| 84 | |||
| 85 | bool generateDynamicPacket(std::vector<std::array<uint8_t, 64>>& packets, bool& listen); | ||
| 86 | |||
| 87 | // fraction of frames that carry a query packet and open a listen window | ||
| 88 | // once the receivers are being polled: each mux cycles through its | ||
| 89 | // receivers one frame at a time and then idles 1 + queryRestFrames | ||
| 90 | // frames (the share the scheduler really runs with); 0 without | ||
| 91 | // listeners or queryable chains | ||
| 92 | float queryFrameShare() const; | ||
| 93 | |||
| 94 | // Spacing of the query rounds. BBShiftString calls this whenever the | ||
| 95 | // sequence frame rate changes, with its predicted frame time without | ||
| 96 | // queries and the cost of one query frame: if a query frame fits in the | ||
| 97 | // period's slack every round is followed by the usual single rest frame, | ||
| 98 | // otherwise enough extra rest frames that the average query cost fits the | ||
| 99 | // slack with a margin, capped so a round still starts at least every | ||
| 100 | // ~500 ms. FalconV5QueryRestFrames (>= 0) overrides it for testing. | ||
| 101 | // Returns true when the number of rest frames changed. | ||
| 102 | bool setFrameBudget(float frameRate, int frameTimeWithoutQueriesUs, int queryFrameCostUs); | ||
| 103 | 20 | int getQueryRestFrames() const { return queryRestFrames; } | |
| 104 | |||
| 105 | void sendCountPixelPackets(); | ||
| 106 | |||
| 107 | private: | ||
| 108 | std::list<ReceiverChain*> receiverChains; | ||
| 109 | std::list<FalconV5Listener*> listeners; | ||
| 110 | std::list<const PinCapabilities*> muxPins; | ||
| 111 | PRUControl* pru = nullptr; | ||
| 112 | uint32_t lastCaptures = 0; | ||
| 113 | uint32_t tornCaptures = 0; | ||
| 114 | // staged capture: stageListenerData() fills stageBuf under stageLock, | ||
| 115 | // processListenerData() swaps it with workBuf under the lock and decodes | ||
| 116 | // workBuf outside it | ||
| 117 | std::mutex stageLock; | ||
| 118 | std::vector<uint8_t> stageBuf; | ||
| 119 | std::vector<uint8_t> workBuf; | ||
| 120 | uint32_t stageLen = 0; | ||
| 121 | bool stageValid = false; | ||
| 122 | |||
| 123 | float queryFrameShare(int restFrames) const; | ||
| 124 | // receivers summed over the muxes, and the muxes that have any: a mux | ||
| 125 | // with none is skipped by the scheduler | ||
| 126 | int queriedReceivers() const; | ||
| 127 | int queriedMuxes() const; | ||
| 128 | |||
| 129 | // extra rest frames after every round of queries, on top of the one rest | ||
| 130 | // frame a round always ends with before the mux changes | ||
| 131 | int queryRestFrames = 0; | ||
| 132 | // FalconV5QueryRestFrames, -1 = automatic (setFrameBudget) | ||
| 133 | int queryRestOverride = -1; | ||
| 134 | |||
| 135 | std::vector<std::map<int, std::list<ReceiverChain*>>> queryData; | ||
| 136 | std::vector<int> maxCount; | ||
| 137 | int curMux = 0; | ||
| 138 | int curCount = 0; | ||
| 139 | bool triggerPixelCount = false; | ||
| 140 | |||
| 141 | // A pending smart-receiver eFuse action. Published by the OutputMonitor | ||
| 142 | // callback (API/MQTT/GPIO/scheduler threads) and consumed by | ||
| 143 | // generateDynamicPacket() on the output thread, so it has to be a single | ||
| 144 | // atomic word: as three separate fields the consumer could pair a new | ||
| 145 | // port with the previous command, and the std::string it replaces was | ||
| 146 | // being assigned and compared concurrently, which is a torn read of a | ||
| 147 | // heap pointer. Kept to 4 bytes so the atomic is lock-free on 32-bit ARM | ||
| 148 | // as well. | ||
| 149 | enum class ToggleCommand : uint8_t { | ||
| 150 | None = 0, | ||
| 151 | Toggle, | ||
| 152 | Reset | ||
| 153 | }; | ||
| 154 | struct PendingToggle { | ||
| 155 | int16_t port = -1; | ||
| 156 | uint8_t index = 0; | ||
| 157 | ToggleCommand cmd = ToggleCommand::None; | ||
| 158 | }; | ||
| 159 | // compare_exchange compares the object representation, so padding here | ||
| 160 | // would make the exchange in generateDynamicPacket() fail at random. | ||
| 161 | static_assert(sizeof(PendingToggle) == 4, "PendingToggle must be a single unpadded word"); | ||
| 162 | static_assert(std::atomic<PendingToggle>::is_always_lock_free, "PendingToggle atomic must be lock-free"); | ||
| 163 | std::atomic<PendingToggle> pendingToggle{ PendingToggle() }; | ||
| 164 | |||
| 165 | // Ports a queried chain covers, i.e. the ports a pending toggle can go | ||
| 166 | // out on. The callback drops any other toggle: one that no chain picks | ||
| 167 | // up would stay pending forever and, since a pending toggle cuts the rest | ||
| 168 | // between query rounds short, hold the scheduler at its fastest cadence. | ||
| 169 | // Filled by addReceiverChain() while the callback may already be live. | ||
| 170 | mutable std::mutex queriedPortsLock; | ||
| 171 | std::vector<bool> queriedPorts; | ||
| 172 | }; | ||
| 173 |