4#ifndef PHYPHOX_BLE_CORE_SERVER_H
5#define PHYPHOX_BLE_CORE_SERVER_H
11#include "../transport/Transport.h"
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); }
38 Server(
Transport& transport) : transport_(transport), serializerSource_(serializer_) {}
41 void setClock(uint32_t (*nowMs)()) { clock_ = nowMs; }
82 uint16_t
mtu()
const {
return mtu_; }
99 void pump()
override { pumpTransfer(); }
105 SerializerSource serializerSource_;
107 bool customXml_ =
false;
112 const char* deviceName_ =
nullptr;
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;
133 uint32_t now()
const {
return clock_ ? clock_() : 0; }
136 bool ensurePacket(uint16_t payload);
137 void startTransfer();
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);
#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
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
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 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
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