28 # error This is a userspace-only header, not allowed by the current build.
31 #include <sifteo/abi.h>
88 if ((_SYS_getFeatures() & _SYS_FEATURE_BLUETOOTH) == 0) {
92 return _SYS_bt_isAvailable();
110 return _SYS_bt_isConnected();
137 _SYS_bt_advertiseState(bytes, length);
148 template <
typename T>
151 advertiseState(reinterpret_cast<const uint8_t*>(&
object),
sizeof object);
164 _SYSBluetoothPacket sys;
167 operator _SYSBluetoothPacket* () {
return &sys; }
168 operator const _SYSBluetoothPacket* ()
const {
return &sys; }
172 return _SYS_BT_PACKET_BYTES;
201 const uint8_t *
bytes()
const {
235 template <
unsigned tCapacity >
239 _SYSBluetoothQueueHeader header;
242 _SYSBluetoothPacket packets[tCapacity + 1];
246 operator _SYSBluetoothQueue* () {
return reinterpret_cast<_SYSBluetoothQueue*
>(&sys); }
247 operator const _SYSBluetoothQueue* ()
const {
return reinterpret_cast<const _SYSBluetoothQueue*
>(&sys); }
252 unsigned lastIndex = tCapacity;
258 sys.header32 = lastIndex << 16;
260 ASSERT(sys.header.head == 0);
261 ASSERT(sys.header.tail == 0);
262 ASSERT(sys.header.last == lastIndex);
277 unsigned size = tCapacity + 1;
278 unsigned head = sys.header.head;
279 unsigned tail = sys.header.tail;
282 return (tail - head) % size;
294 return tCapacity -
count();
327 unsigned head = sys.header.head;
328 ASSERT(head <= tCapacity);
341 unsigned head = sys.header.head + 1;
342 if (head > tCapacity)
344 sys.header.head = head;
356 _SYS_bt_queueReadHint();
390 unsigned tail = sys.header.tail;
391 ASSERT(tail <= tCapacity);
404 unsigned tail = sys.header.tail + 1;
405 if (tail > tCapacity)
407 sys.header.tail = tail;
420 _SYS_bt_queueWriteHint();
465 template <
unsigned tSendCapacity = 4,
unsigned tReceiveCapacity = 4 >
493 receiveQueue.
clear();
494 _SYS_bt_setPipe(sendQueue, receiveQueue);
505 _SYS_bt_setPipe(0, 0);
520 sendQueue.
write(buffer);
535 receiveQueue.
read(buffer);
570 _SYSBluetoothCounters current;
571 _SYSBluetoothCounters base;
580 _SYS_bt_counters(&base,
sizeof base);
587 _SYS_bt_counters(¤t,
sizeof current);
592 return current.rxPackets - base.rxPackets;
597 return current.txPackets - base.txPackets;
602 return current.rxBytes - base.rxBytes;
607 return current.txBytes - base.txBytes;
618 return current.rxUserDropped - base.rxUserDropped;
static bool isAvailable()
Is Bluetooth support available on this hardware?
Definition: bluetooth.h:86
void resize(unsigned bytes)
Change the size of this packet, in bytes.
Definition: bluetooth.h:187
unsigned count() const
How many packets are sitting in the queue right now?
Definition: bluetooth.h:275
uint32_t sentPackets()
Total sent packets.
Definition: bluetooth.h:596
uint32_t sentBytes()
Total sent bytes.
Definition: bluetooth.h:606
uint32_t userPacketsDropped()
Total user-defined packets dropped.
Definition: bluetooth.h:617
static unsigned capacity()
Retrieve the capacity of a packet, in bytes. (19)
Definition: bluetooth.h:171
void write(const BluetoothPacket &buffer)
Copy a new packet into the queue, from a provided buffer.
Definition: bluetooth.h:369
#define ASSERT(_x)
Runtime debug assertion.
Definition: macros.h:205
static void advertiseState(const uint8_t *bytes, unsigned length)
Set the current advertised game state.
Definition: bluetooth.h:135
unsigned type() const
Return the packet's 7-bit type code.
Definition: bluetooth.h:206
unsigned readAvailable() const
How many packets are available to read from this queue right now?
Definition: bluetooth.h:542
BluetoothQueue< tReceiveCapacity > receiveQueue
Queue for packets that have been received but not yet processed.
Definition: bluetooth.h:472
bool empty() const
Is this packet's payload empty?
Definition: bluetooth.h:193
A memory buffer which holds a queue of Bluetooth packets.
Definition: bluetooth.h:236
void attach()
Attach this pipe to the system.
Definition: bluetooth.h:491
uint32_t receivedPackets()
Total received packets.
Definition: bluetooth.h:591
A container for one Bluetooth packet's data.
Definition: bluetooth.h:163
Global Bluetooth operations.
Definition: bluetooth.h:72
void commit()
Finish writing a packet that was started with reserve()
Definition: bluetooth.h:401
void reset()
Reset all counters back to zero.
Definition: bluetooth.h:579
void pop()
Dequeue the oldest packet.
Definition: bluetooth.h:338
void detach()
Detach all pipes from the system.
Definition: bluetooth.h:504
Diagnostic counters for the Bluetooth subsystem.
Definition: bluetooth.h:568
static void advertiseState(const T &object)
Template wrapper around advertiseState()
Definition: bluetooth.h:149
unsigned size() const
Retrieve the size of this packet, in bytes.
Definition: bluetooth.h:182
static bool isConnected()
Is a device currently connected via the Sifteo Bluetooth API?
Definition: bluetooth.h:108
void clear()
Set the packet's length and type to zero.
Definition: bluetooth.h:176
unsigned writeAvailable() const
How many free buffers are available for writing right now?
Definition: bluetooth.h:292
void read(BluetoothPacket &buffer)
Read the oldest queued packet into a provided buffer.
Definition: bluetooth.h:307
bool write(const BluetoothPacket &buffer)
Write one packet to the send queue, if space is available.
Definition: bluetooth.h:517
uint8_t * bytes()
Return a pointer to this packet's payload bytes.
Definition: bluetooth.h:198
void setType(unsigned type)
Set a packet's 7-bit type.
Definition: bluetooth.h:211
BluetoothPacket & reserve()
Access a buffer slot where a new packet can be written.
Definition: bluetooth.h:387
void capture()
Update the state of all counters.
Definition: bluetooth.h:586
unsigned writeAvailable() const
How many free buffers are available for writing right now?
Definition: bluetooth.h:548
void clear()
Initializes this queue's header, and marks it as empty.
Definition: bluetooth.h:250
unsigned readAvailable() const
How many packets are available to read from this queue right now?
Definition: bluetooth.h:286
uint32_t receivedBytes()
Total received bytes.
Definition: bluetooth.h:601
#define STATIC_ASSERT(_x)
Definition: macros.h:342
A memory buffer for bidirectional Bluetooth communications.
Definition: bluetooth.h:466
BluetoothQueue< tSendCapacity > sendQueue
Queue for packets we're waiting to send.
Definition: bluetooth.h:469
const BluetoothPacket & peek() const
Access the oldest queued packet without copying it.
Definition: bluetooth.h:324
bool read(BluetoothPacket &buffer)
If a packet is available in the queue, read it.
Definition: bluetooth.h:532