2727# include " nimble/nimble/include/nimble/nimble_npl.h"
2828# endif
2929
30+ # include < functional>
3031# include < type_traits>
3132# include < cstdarg>
3233
@@ -61,6 +62,8 @@ class Stream : public Print {
6162
6263class NimBLEStream : public Stream {
6364 public:
65+ typedef std::function<bool (const uint8_t * data, size_t len)> rx_overflow_callback_t ;
66+
6467 NimBLEStream () = default ;
6568 virtual ~NimBLEStream () { end (); }
6669
@@ -87,6 +90,13 @@ class NimBLEStream : public Stream {
8790 virtual int peek () override ;
8891 virtual bool ready () const = 0;
8992
93+ /* *
94+ * @brief Set a callback to be invoked when incoming data exceeds RX buffer capacity.
95+ * @param cb The callback function, which should return true to drop older buffered data and
96+ * make room for the new data, or false to drop the new data instead.
97+ */
98+ void setRxOverflowCallback (rx_overflow_callback_t cb) { m_rxOverflowCallback = cb; }
99+
90100 operator bool () const { return ready (); }
91101
92102 using Print::write;
@@ -102,15 +112,16 @@ class NimBLEStream : public Stream {
102112 static void txDrainEventCb (struct ble_npl_event * ev);
103113 static void txDrainCalloutCb (struct ble_npl_event * ev);
104114
105- ByteRingBuffer* m_txBuf{nullptr };
106- ByteRingBuffer* m_rxBuf{nullptr };
107- uint8_t m_txChunkBuf[MYNEWT_VAL(BLE_ATT_PREFERRED_MTU)];
108- uint32_t m_txBufSize{1024 };
109- uint32_t m_rxBufSize{1024 };
110- ble_npl_event m_txDrainEvent{};
111- ble_npl_callout m_txDrainCallout{};
112- bool m_coInitialized{false };
113- bool m_eventInitialized{false };
115+ ByteRingBuffer* m_txBuf{nullptr };
116+ ByteRingBuffer* m_rxBuf{nullptr };
117+ uint8_t m_txChunkBuf[MYNEWT_VAL(BLE_ATT_PREFERRED_MTU)];
118+ uint32_t m_txBufSize{1024 };
119+ uint32_t m_rxBufSize{1024 };
120+ ble_npl_event m_txDrainEvent{};
121+ ble_npl_callout m_txDrainCallout{};
122+ bool m_coInitialized{false };
123+ bool m_eventInitialized{false };
124+ rx_overflow_callback_t m_rxOverflowCallback{nullptr };
114125};
115126
116127# if MYNEWT_VAL(BLE_ROLE_PERIPHERAL)
0 commit comments