phyphox BLE
Plot your Arduino's data in the phyphox app and control the board from the phone
Loading...
Searching...
No Matches
Server.h
Go to the documentation of this file.
1// The core orchestrator behind the PhyphoxBLE façade: owns the store, the serializer, the
2// transfer session and the channel store; implements TransportListener; knows nothing about
3// any BLE library. One instance (the radio is one).
4#ifndef PHYPHOX_BLE_CORE_SERVER_H
5#define PHYPHOX_BLE_CORE_SERVER_H
6
7#include "ExperimentStore.h"
8#include "Serializer.h"
9#include "Transfer.h"
10#include "ChannelStore.h"
11#include "../transport/Transport.h"
12
14
15namespace phyphox {
16
20 uint32_t transfersStalled = 0;
22 uint32_t inputWrites = 0, events = 0;
23};
24
25class Server : public TransportListener {
26public:
29 public:
30 explicit SerializerSource(const Serializer& s) : s_(s) {}
31 uint32_t size() const override { return s_.size(); }
32 uint32_t crc() const override { return s_.crc(); }
33 uint32_t read(uint32_t o, uint8_t* out, uint32_t n) const override { return s_.read(o, out, n); }
34 private:
35 const Serializer& s_;
36 };
37
38 Server(Transport& transport) : transport_(transport), serializerSource_(serializer_) {}
41 void setClock(uint32_t (*nowMs)()) { clock_ = nowMs; }
42
43 // ---- configuration (before start)
44 void setDeviceName(const char* name) { deviceName_ = name; }
45 void setMtu(uint16_t payload);
46 void setConnectionParameters(uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout);
52 void setCustomXml(const uint8_t* xml, uint32_t len);
53
54 // ---- lifecycle
58 bool start();
61 void poll();
62 bool started() const { return started_; }
65 bool startFailed() const { return startFailed_; }
66
67 // ---- data to the phone
68 bool writeFloats(const float* values, uint8_t count);
69 bool writeBytes(const uint8_t* bytes, uint16_t len);
70
71 // ---- data from the phone
72 ChannelStore& channels() { return channels_; }
74 void setChannelCallbacks(uint8_t channel, ChangeCallback onChange, PressCallback onPress);
75 const ExperimentEvent& lastEvent() const { return lastEvent_; }
76 void setConfigHandler(void (*h)()) { configHandler_ = h; }
77 void setEventHandler(void (*h)()) { eventHandler_ = h; }
79 const uint8_t* legacyConfig() const { return legacyConfig_; }
80 const TransferSession& transfer() const { return transfer_; }
81 bool customXml() const { return customXml_; }
82 uint16_t mtu() const { return mtu_; }
83
84 // ---- introspection
86 void printXml(Sink& sink);
88 const ExperimentStore& store() const { return store_; }
89 const ServerStats& stats() const { return stats_; }
90 uint16_t connections() const { return transport_.connected() ? 1 : 0; }
91 bool subscribed() const { return dataSubscribed_; }
92
93 // ---- TransportListener
94 void onConnect() override;
95 void onDisconnect() override;
96 void onSubscribe(CharId id, bool enabled) override;
97 void onWrite(CharId id, const uint8_t* data, uint16_t len) override;
98 void onNotifyStatus(CharId id, bool accepted) override;
99 void pump() override { pumpTransfer(); }
100
101private:
102 Transport& transport_;
103 ExperimentStore store_;
104 Serializer serializer_;
105 SerializerSource serializerSource_;
106 BufferSource customSource_;
107 bool customXml_ = false;
108 TransferSession transfer_;
109 ChannelStore channels_;
110 ExperimentEvent lastEvent_;
111 ServerStats stats_;
112 const char* deviceName_ = nullptr;
113 uint16_t mtu_ = PHYPHOX_BLE_DEFAULT_MTU;
114 uint16_t connMin_ = 0, connMax_ = 0, connLatency_ = 0, connTimeout_ = 0;
115 bool dataSubscribed_ = false;
116 void (*configHandler_)() = nullptr;
117 void (*eventHandler_)() = nullptr;
118 uint8_t* packet_ = nullptr;
119 uint16_t packetCap_ = 0;
120 uint8_t* sensorSizes_ = nullptr;
121 uint8_t legacyConfig_[20] = {0};
122 uint8_t legacySlots_[6 * 32] = {0};
123 uint32_t (*clock_)() = nullptr;
124 bool started_ = false;
125 bool startRequested_ = false;
126 bool startFailed_ = false;
127 bool experimentAdded_ = false;
128 uint32_t layoutMask_ = 0;
129 uint8_t layoutSensors_ = 0;
131 ChangeCallback pendingChange_[PHYPHOX_BLE_MAX_INPUT_CHANNEL + 1] = {nullptr};
132 PressCallback pendingPress_[PHYPHOX_BLE_MAX_INPUT_CHANNEL + 1] = {nullptr};
133 uint32_t now() const { return clock_ ? clock_() : 0; }
134
135 void rebuild();
136 bool ensurePacket(uint16_t payload);
137 void startTransfer();
138 void pumpTransfer();
139 void serviceFromWrite();
140 void deliverInput(uint8_t channel, const uint8_t* data, uint16_t len);
141 void deliverSensor(uint8_t sensor, const uint8_t* data, uint16_t len);
142 void deliverLegacyConfig(const uint8_t* data, uint16_t len);
143};
144
145} // namespace phyphox
146
147#endif
#define PHYPHOX_BLE_DEFAULT_MTU
Default MTU payload (ATT_MTU 23 − 3). setMTU() raises it where the transport can negotiate.
Definition Config.h:52
#define PHYPHOX_BLE_MAX_INPUT_CHANNEL
Highest input channel number a sketch may use.
Definition Config.h:31
Definition phyphoxBleExperiment.h:34
Definition Transfer.h:33
Anything that can serve document bytes by offset: the Serializer for generated experiments,...
Definition Transfer.h:25
Definition ChannelStore.h:13
Definition ExperimentStore.h:31
Definition Serializer.h:27
uint32_t size() const
bytes of the whole document
Definition Serializer.h:33
uint32_t read(uint32_t offset, uint8_t *out, uint32_t n) const
Copy document bytes [offset, offset + n) into out; returns bytes produced (fewer at the end).
uint32_t crc() const
CRC-32 of the whole document.
Definition Serializer.h:34
The serializer's byte view, so the transfer can read it.
Definition Server.h:28
SerializerSource(const Serializer &s)
Definition Server.h:30
uint32_t crc() const override
Definition Server.h:32
uint32_t size() const override
Definition Server.h:31
uint32_t read(uint32_t o, uint8_t *out, uint32_t n) const override
Definition Server.h:33
Definition Server.h:25
bool writeBytes(const uint8_t *bytes, uint16_t len)
raw array (≤ MTU payload)
bool subscribed() const
Definition Server.h:91
void poll()
drives the transfer; calls transport_.poll()
const ExperimentStore & store() const
Definition Server.h:88
bool customXml() const
Definition Server.h:81
Server(Transport &transport)
Definition Server.h:38
void setCustomXml(const uint8_t *xml, uint32_t len)
Serve user-supplied XML instead; the 1.x GATT layout (cddf1002 data, cddf1003 config with five floats...
void setConnectionParameters(uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout)
void setEventHandler(void(*h)())
Definition Server.h:77
ChannelStore & channels()
Definition Server.h:72
bool ensureStarted()
Bring the transport up now if start() was requested and it is not up yet.
const uint8_t * legacyConfig() const
User-XML mode: the raw last value of the 1.x config characteristic.
Definition Server.h:79
void printXml(Sink &sink)
The document as served: the user's own bytes in user-XML mode, else the generated one.
void setClock(uint32_t(*nowMs)())
Millisecond clock for the transfer's retry timing (the façade passes millis).
Definition Server.h:41
void ensureDocument()
void onWrite(CharId id, const uint8_t *data, uint16_t len) override
The central wrote len bytes to id.
void setMtu(uint16_t payload)
void setChannelCallbacks(uint8_t channel, ChangeCallback onChange, PressCallback onPress)
Attach callbacks to a channel at any time, before or after the experiment was added.
bool writeFloats(const float *values, uint8_t count)
1…5 floats → data characteristic
void setConfigHandler(void(*h)())
Definition Server.h:76
void onDisconnect() override
void addExperiment(const PhyphoxBleExperiment &exp)
Serve the description (copies it; builds the part table).
bool startFailed() const
The transport refused to come up (no module answered, a service could not be created).
Definition Server.h:65
void setDeviceName(const char *name)
Definition Server.h:44
bool start()
Request the start.
void pump() override
Drive the transfer: send what is due.
Definition Server.h:99
const ExperimentEvent & lastEvent() const
Definition Server.h:75
void onNotifyStatus(CharId id, bool accepted) override
Asynchronous verdict on the last notify() for stacks that report it later.
const ServerStats & stats() const
Definition Server.h:89
bool started() const
Definition Server.h:62
uint16_t connections() const
Definition Server.h:90
uint16_t mtu() const
Definition Server.h:82
void onSubscribe(CharId id, bool enabled) override
The central enabled/disabled notifications on id (only CH_EXPERIMENT and CH_DATA are notifiable).
const TransferSession & transfer() const
Definition Server.h:80
void onConnect() override
Receives the bytes of a serialization.
Definition Sink.h:14
Definition Transfer.h:45
Callbacks from the transport into the core (implemented by Server).
Definition Transport.h:40
Definition Transport.h:59
virtual bool connected() const =0
Definition ChannelStore.h:11
void(* ChangeCallback)(float value)
Callback types. Plain function pointers (captureless lambdas convert), never std::function.
Definition ElementData.h:19
void(* PressCallback)()
Definition ElementData.h:20
Definition Transport.h:23
Definition ChannelStore.h:45
Statistics a bench or a curious sketch can read (PhyphoxBLE::stats()).
Definition Server.h:18
uint32_t transfersStalled
the aborted ones the watchdog gave up on (the rest: disconnect, control 0)
Definition Server.h:20
uint32_t dataRefused
Definition Server.h:21
uint32_t inputWrites
Definition Server.h:22
uint32_t transfersAborted
Definition Server.h:19
uint32_t dataNotifications
Definition Server.h:21
uint32_t packetsRefused
Definition Server.h:21
uint32_t events
Definition Server.h:22
uint32_t transfersCompleted
Definition Server.h:19
uint32_t transfers
Definition Server.h:19