@@ -13,7 +13,8 @@ extern "C"
1313#endif
1414
1515#ifdef __cplusplus
16- namespace BLE {
16+ namespace BLE
17+ {
1718 class Packet ;
1819};
1920
@@ -25,26 +26,59 @@ constexpr uint32_t ConnIntervalMS = 100;
2526constexpr uint32_t ConnPeripheralLatency = 0 ;
2627constexpr uint32_t TransmitWindowSizeMS = 10 ; // Must be at most 10
2728constexpr uint32_t ConnSupervisionTimeoutMS = (1 + ConnPeripheralLatency) * ConnIntervalMS * 2 + 100 ;
28- constexpr std::array<uint8_t , 3 > FakeCRC = { 0xFF , 0xFF , 0xFF };
29+ constexpr std::array<uint8_t , 3 > FakeCRC = {0xFF , 0xFF , 0xFF };
2930
3031constexpr uint16_t WantATT_MTU = 247 ;
3132
33+ enum Stage
34+ {
35+ NONE,
36+ CONNECTED,
37+ EXCHANGED_MTU,
38+ REQUESTED_INFO,
39+ };
40+
41+ struct pending_packet_t
42+ {
43+ uint64_t send_at;
44+ std::unique_ptr<BLE::Packet> packet;
45+ };
46+
47+ class PendingCompare
48+ {
49+ public:
50+ bool operator ()(pending_packet_t &a, pending_packet_t &b)
51+ {
52+ // Lower send_at should come first
53+ return a.send_at > b.send_at ;
54+ }
55+ };
56+
3257struct bluetooth_t
3358{
3459 RADIO_t *radio;
3560 event_queue_t *ev_queue;
3661 NRF52832_t *nrf;
3762 bool connected = false ;
3863
39- bool exchanged_mtu = false ;
64+ bool sent_req = false ;
65+ Stage stage = NONE;
4066 uint16_t att_mtu = 23 ; // Default MTU
4167
68+ struct
69+ {
70+ bool in_fragmented = false ;
71+ uint32_t total_length;
72+ uint8_t channel;
73+ std::vector<uint8_t > buffer;
74+ } l2cap_frag;
75+
4276 uint64_t last_conn_event_cycles = 0 ;
4377
4478 unsigned int transmitSeqNum : 1 ;
4579 unsigned int nextExpectedSeqNum : 1 ;
4680
47- std::queue< std::unique_ptr<BLE::Packet> > pending_packets;
81+ std::priority_queue< pending_packet_t , std::vector< pending_packet_t >, PendingCompare > pending_packets;
4882
4983 bluetooth_t (pinetime_t *pt) : radio(static_cast <RADIO_t *>(nrf52832_get_peripheral(pinetime_get_nrf52832(pt), INSTANCE_RADIO))),
5084 ev_queue (pinetime_get_event_queue(pt)),
@@ -54,7 +88,11 @@ struct bluetooth_t
5488
5589 void Send (const BLE::Packet &packet);
5690
57- void Enqueue (std::unique_ptr<BLE::Packet> packet);
91+ void Enqueue (std::unique_ptr<BLE::Packet> packet, size_t delay_ms);
92+ void Enqueue (std::unique_ptr<BLE::Packet> packet)
93+ {
94+ Enqueue (std::move (packet), 0 );
95+ }
5896};
5997
6098extern " C"
0 commit comments