phyphox BLE
Plot your Arduino's data in the phyphox app and control the board from the phone
Loading...
Searching...
No Matches
Transfer.h
Go to the documentation of this file.
1// The experiment transfer, as the phyphox docs specify it (bluetooth-low-energy.md, "Phyphox
2// service"): a header packet `phyphox` + u32 size + u32 CRC-32 (big endian), then the document
3// in MTU-sized notifications on cddf0002. Started by the app subscribing to cddf0002 OR by the
4// app writing 1 to cddf0003, whichever comes first; a second trigger while a transfer runs is
5// ignored; a disconnect aborts.
6//
7// Flow control (plan ยง3.4): the transport reports whether the stack accepted a packet. A
8// refused packet is re-sent after PHYPHOX_BLE_TRANSFER_RETRY_DELAY_MS, the pause doubling per
9// consecutive refusal up to PHYPHOX_BLE_TRANSFER_RETRY_MAX_MS; no retry count and no fixed
10// pacing delay anywhere โ€” the Server's stall watchdog is the only thing that abandons a
11// transfer.
12//
13// This class is transport-independent: it only hands out the next packet and takes the
14// verdict back. The Server drives it from poll() / the transport's callbacks.
15#ifndef PHYPHOX_BLE_CORE_TRANSFER_H
16#define PHYPHOX_BLE_CORE_TRANSFER_H
17
18#include <stdint.h>
19#include "Config.h"
20
21namespace phyphox {
22
26public:
27 virtual ~ByteSource() {}
28 virtual uint32_t size() const = 0;
29 virtual uint32_t crc() const = 0;
30 virtual uint32_t read(uint32_t offset, uint8_t* out, uint32_t n) const = 0;
31};
32
33class BufferSource : public ByteSource {
34public:
35 void set(const uint8_t* bytes, uint32_t len);
36 uint32_t size() const override { return len_; }
37 uint32_t crc() const override { return crc_; }
38 uint32_t read(uint32_t offset, uint8_t* out, uint32_t n) const override;
39private:
40 const uint8_t* bytes_ = nullptr;
41 uint32_t len_ = 0;
42 uint32_t crc_ = 0;
43};
44
46public:
47 enum State : uint8_t { IDLE, HEADER, BODY, DONE, ABORTED };
48
50 void begin(const ByteSource& source, uint16_t payload);
52 void holdUntil(uint32_t untilMs) { retryAt_ = untilMs; }
53 void abort() { state_ = ABORTED; }
54 void reset() { state_ = IDLE; pending_ = false; }
55 bool active() const { return state_ == HEADER || state_ == BODY; }
56 State state() const { return state_; }
57
60 uint16_t nextPacket(uint8_t* out, uint32_t nowMs);
62 void accepted();
63 void refused(uint32_t nowMs);
64
65 uint32_t bytesSent() const { return offset_; }
67 uint32_t lastProgressMs() const { return lastProgress_; }
68 void noteProgress(uint32_t nowMs) { lastProgress_ = nowMs; }
69 uint16_t packetsSent() const { return packets_; }
70 uint16_t retries() const { return retries_; }
71
72private:
73 const ByteSource* source_ = nullptr;
74 State state_ = IDLE;
75 uint16_t payload_ = PHYPHOX_BLE_DEFAULT_MTU;
76 uint32_t offset_ = 0;
77 uint16_t lastLen_ = 0;
78 uint16_t packets_ = 0;
79 uint16_t retries_ = 0;
80 uint32_t retryAt_ = 0;
81 uint32_t lastProgress_ = 0;
82 bool pending_ = false;
83};
84
85} // namespace phyphox
86
87#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
Definition Transfer.h:33
uint32_t size() const override
Definition Transfer.h:36
uint32_t crc() const override
Definition Transfer.h:37
uint32_t read(uint32_t offset, uint8_t *out, uint32_t n) const override
void set(const uint8_t *bytes, uint32_t len)
Anything that can serve document bytes by offset: the Serializer for generated experiments,...
Definition Transfer.h:25
virtual uint32_t size() const =0
virtual uint32_t read(uint32_t offset, uint8_t *out, uint32_t n) const =0
virtual ~ByteSource()
Definition Transfer.h:27
virtual uint32_t crc() const =0
Definition Transfer.h:45
void reset()
back to idle after the bookkeeping
Definition Transfer.h:54
uint16_t retries() const
Definition Transfer.h:70
uint16_t nextPacket(uint8_t *out, uint32_t nowMs)
Fill out with the packet to send now (header first, then body).
void abort()
Definition Transfer.h:53
uint32_t lastProgressMs() const
Milliseconds of the last accepted packet (or of begin()); the Server's stall watchdog.
Definition Transfer.h:67
uint32_t bytesSent() const
Definition Transfer.h:65
State state() const
Definition Transfer.h:56
void begin(const ByteSource &source, uint16_t payload)
Start a transfer of source with payload bytes per packet (MTU โˆ’ 3).
void accepted()
The transport's verdict on the packet last returned by nextPacket().
void holdUntil(uint32_t untilMs)
Do not send before untilMs (the start delay).
Definition Transfer.h:52
State
Definition Transfer.h:47
@ HEADER
Definition Transfer.h:47
@ BODY
Definition Transfer.h:47
@ ABORTED
Definition Transfer.h:47
@ DONE
Definition Transfer.h:47
@ IDLE
Definition Transfer.h:47
bool active() const
Definition Transfer.h:55
uint16_t packetsSent() const
Definition Transfer.h:69
void noteProgress(uint32_t nowMs)
Definition Transfer.h:68
void refused(uint32_t nowMs)
Definition ChannelStore.h:11