summaryrefslogtreecommitdiff
path: root/BeaconTxController.hpp
blob: 02dabb30131414c671fb688e5d309a9ffa284a5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#ifndef BEACONTXCONTROLLER_HPP
#define BEACONTXCONTROLLER_HPP

#include <cstdint>
#include <string>
#include <vector>

namespace BeaconTx
{
  // Identifies one UTC period within a scheduling epoch so delayed outcomes from
  // an earlier mode, period, or clock timeline cannot affect the current schedule.
  struct PlanId
  {
    std::uint64_t epoch {0};
    std::int64_t periodSerial {-1};

    bool isValid () const {return epoch != 0 && periodSerial >= 0;}
  };

  inline bool operator== (PlanId const& lhs, PlanId const& rhs)
  {
    return lhs.epoch == rhs.epoch && lhs.periodSerial == rhs.periodSerial;
  }

  inline bool operator!= (PlanId const& lhs, PlanId const& rhs)
  {
    return !(lhs == rhs);
  }

  enum class Disposition {Receive, Transmit};
  enum class PlanSource {Percentage, BandHop, TxNext};
  enum class ApplicationStatus {AwaitingProposal, Ready, Applying, Applied, Failed};
  // A requested and accepted start is not yet a beacon transmission. MessageStarted
  // means the audio source committed, which is when Tx Next and history may change.
  enum class TxLifecycle {None, Decided, StartRequested, MessageStarted};
  enum class TuneKind {None, Manual, Automatic};

  struct RoundRobinPolicy
  {
    enum class Kind {Random, Fixed};

    Kind kind {Kind::Random};
    int selectedSlot {0};
    int slotCount {0};

    static RoundRobinPolicy random () {return {};}
    static RoundRobinPolicy fixed (int selected, int count)
    {
      RoundRobinPolicy result;
      result.kind = Kind::Fixed;
      result.selectedSlot = selected;
      result.slotCount = count;
      return result;
    }
  };

  RoundRobinPolicy parseRoundRobinPolicy (std::string const& text);
  std::string formatRoundRobinPolicy (RoundRobinPolicy const& policy);

  struct HoppingProposal
  {
    int frequenciesIndex {-1};
    bool tuneRequired {false};
    std::string periodName;
  };

  // A provider's scheduling choice, containing no rig or UI effects. BeaconPlan
  // is the controller-owned copy tied to a target period and tracks application.
  struct ScheduleProposal
  {
    Disposition disposition {Disposition::Receive};
    PlanSource source {PlanSource::Percentage};
    bool hasHoppingProposal {false};
    HoppingProposal hopping;
  };

  struct BeaconPlan
  {
    PlanId id;
    Disposition disposition {Disposition::Receive};
    PlanSource source {PlanSource::Percentage};
    bool hasHoppingProposal {false};
    HoppingProposal hopping;
    ApplicationStatus applicationStatus {ApplicationStatus::AwaitingProposal};
  };

  enum class ActionKind
  {
    SetTransmitWindow,
    RequestScheduleProposal,
    ApplyBandChange,
    StartAutomaticTune,
    RestoreAuto,
    ClearTxNextUi,
    RecordBeaconTransmission
  };

  struct Action
  {
    ActionKind kind {ActionKind::SetTransmitWindow};
    PlanId planId;
    bool enabled {false};
    HoppingProposal hopping;

    static Action setTransmitWindow (PlanId id, bool enabled);
    static Action forPlan (ActionKind kind, PlanId id);
  };

  // Owns WSPR/FST4W period plans and Tx/Tune lifecycle state. It reads no clock and
  // performs no UI or rig I/O; callers supply events, apply actions, and report
  // outcomes by PlanId.
  class Controller final
  {
  public:
    using Actions = std::vector<Action>;

    Actions enterMode (std::int64_t periodMilliseconds,
                       RoundRobinPolicy policy = RoundRobinPolicy::random ());
    Actions exitMode ();
    Actions setPeriod (std::int64_t periodMilliseconds);
    void setAutoEnabled (bool enabled, bool explicitOperatorChange = true);
    void setRoundRobinPolicy (RoundRobinPolicy policy);
    Actions setTxNext (bool enabled);

    Actions observeUtc (std::int64_t utcMilliseconds);
    Actions receiveCompleted ();
    Actions tuneStarted (TuneKind kind, PlanId planId = {});
    Actions tuneCompleted (PlanId planId);
    Actions txStartRequested ();
    Actions txStartResult (PlanId planId, bool accepted);
    Actions messageStarted (PlanId planId);
    Actions transmitWindowEnded ();
    Actions txStopped (PlanId planId);
    Actions proposalDelivered (PlanId planId, ScheduleProposal const& proposal);
    Actions bandChangeOutcome (PlanId planId, bool succeeded);

    bool active () const {return m_active;}
    bool autoEnabled () const {return m_autoEnabled;}
    bool txNextPending () const {return m_txNextPending;}
    bool transmitWindow () const {return m_transmitWindow;}
    TuneKind tuneKind () const {return m_tuneKind;}
    TxLifecycle txLifecycle () const {return m_txLifecycle;}
    PlanSource currentPlanSource () const {return m_currentPlanSource;}
    PlanId currentPlanId () const {return m_currentPlanId;}
    PlanId txPlanId () const {return m_txPlanId;}
    PlanId tunePlanId () const {return m_tunePlanId;}
    BeaconPlan const * futurePlan () const {return m_hasFuturePlan ? &m_futurePlan : nullptr;}

  private:
    Actions resynchronize (std::int64_t periodSerial);
    void appendPreparation (Actions& actions);
    bool fixedRoundRobinTransmits (std::int64_t utcMilliseconds) const;
    void clearFuturePlan ();

    bool m_active {false};
    bool m_autoEnabled {false};
    bool m_txNextPending {false};
    bool m_initialized {false};
    bool m_transmitWindow {false};
    bool m_currentCompleted {false};
    bool m_hasFuturePlan {false};
    bool m_preparationSuppressed {false};
    bool m_restoreAuto {false};
    bool m_messageSessionActive {false};
    bool m_txStartAccepted {false};
    bool m_currentFromTxNext {false};
    std::uint64_t m_epoch {0};
    std::int64_t m_periodMilliseconds {0};
    std::int64_t m_currentPeriodSerial {-1};
    std::int64_t m_lastObservedUtc {-1};
    RoundRobinPolicy m_roundRobinPolicy;
    Disposition m_txNextUnderlyingDisposition {Disposition::Receive};
    PlanSource m_txNextUnderlyingSource {PlanSource::Percentage};
    PlanSource m_currentPlanSource {PlanSource::Percentage};
    BeaconPlan m_futurePlan;
    PlanId m_currentPlanId;
    PlanId m_txPlanId;
    TxLifecycle m_txLifecycle {TxLifecycle::None};
    TuneKind m_tuneKind {TuneKind::None};
    PlanId m_tunePlanId;
  };
}

#endif