PowerBlocks SDK
 
Loading...
Searching...
No Matches
l2cap.h
Go to the documentation of this file.
1
15
16#pragma once
17
18#include <stdint.h>
19
20#include "FreeRTOS.h"
21#include "semphr.h"
22
23// Every L2CAP device comes with 1 channel open
24// That L2CAP uses for opening other channels and such
25// So every device comes with the buffer for this.
26// Must be a power of 2.
27#define L2CAP_SIGNAL_CHANNEL_BUFFER_SIZE 256
28
29#define L2CAP_CHANNEL_SIGNALS 0x0001
30
31#define L2CAP_DEFAULT_MTU 185
32#define L2CAP_DEFAULT_FLUSH_TIMEOUT 0xFFFF
33
34typedef void (*l2cap_channel_event_t)(void* channel, void* user);
35
36// Used internally by L2CAP when handling the signal channel.
37typedef struct {
38 uint8_t code; // Command Code
39 uint8_t id; // Unique id for the request
40 uint16_t length;
41
42 uint8_t data[16]; // Arbitrary max size
44
45typedef struct {
46 void* device; // l2cap_device_t
47 uint16_t sid; // Source ID, ID the endpoint will use when talking to us.
48 uint16_t did; // Destination ID, ID we use when talking to the endpoint
49
50 SemaphoreHandle_t on_complete_packet;
51
52 // Stores multiple packets, each starting with a 16 byte size, then data.
53 uint8_t* buffer;
54 int buffer_length;
55 int fifo_write_head;
56 int fifo_write_head_packet; // Up to the last packet.
57 int fifo_read_head;
58
59 // Track incoming signals
60 volatile l2cap_signal_t* incoming_signal;
61 volatile uint8_t incoming_id;
62 SemaphoreHandle_t signal_waiter;
63
64 // Event for when a new received packet is made available.
65 struct {
66 l2cap_channel_event_t event;
67 void* data;
68 } event_packet_available;
69
70 StaticSemaphore_t semaphore_data[2];
72
73typedef struct {
74 SemaphoreHandle_t lock;
75 uint16_t handle;
76 struct {
77 l2cap_channel_t** array;
78 size_t length;
79 } open_channels;
80
81 // Default open channel L2CAP must use
82 l2cap_channel_t signal_channel;
83 uint8_t signal_channel_buffer[L2CAP_SIGNAL_CHANNEL_BUFFER_SIZE];
84
85 // L2CAP Reading into Handle State
86 l2cap_channel_t* reading_channel;
87 uint16_t reading_remaining;
88
89 uint8_t mac_address[6];
90
91 StaticSemaphore_t semaphore_data;
93
105extern int l2cap_initialize();
106
114extern void l2cap_signal_close();
115
122extern void l2cap_close();
123
137extern int l2cap_open_device(l2cap_device_t* device_handle, uint16_t hci_device_handle, const uint8_t* mac_address);
138
146extern void l2cap_close_device(l2cap_device_t* device_handle);
147
164extern int l2cap_open_channel(l2cap_device_t* device_handle, l2cap_channel_t* channel, uint16_t protocol_id, uint8_t* rx_buffer, int rx_buffer_size);
165
176extern void l2cap_close_channel(l2cap_channel_t* channel);
177
192extern int l2cap_send_channel(l2cap_channel_t* channel, const void* data, uint16_t size);
193
216extern int l2cap_receive_channel(l2cap_channel_t* channel, void* data, uint16_t size);
217
228extern void l2cap_set_channel_receive_event(l2cap_channel_t* channel, l2cap_channel_event_t event, void* param);
int l2cap_open_device(l2cap_device_t *device_handle, uint16_t hci_device_handle, const uint8_t *mac_address)
Opens a L2CAP Connections.
Definition l2cap.c:458
void l2cap_close()
Closes out of L2CAP.
Definition l2cap.c:432
void l2cap_set_channel_receive_event(l2cap_channel_t *channel, l2cap_channel_event_t event, void *param)
Sets the L2CAP on packet receive event.
Definition l2cap.c:747
int l2cap_initialize()
Initalized L2CAP.
Definition l2cap.c:410
int l2cap_send_channel(l2cap_channel_t *channel, const void *data, uint16_t size)
Sends data over a L2CAP Channel.
Definition l2cap.c:618
void l2cap_close_channel(l2cap_channel_t *channel)
Closes a L2CAP Channel.
Definition l2cap.c:612
void l2cap_signal_close()
Begins closing out of L2CAP.
Definition l2cap.c:428
int l2cap_receive_channel(l2cap_channel_t *channel, void *data, uint16_t size)
Receives from a L2CAP Channel.
Definition l2cap.c:674
int l2cap_open_channel(l2cap_device_t *device_handle, l2cap_channel_t *channel, uint16_t protocol_id, uint8_t *rx_buffer, int rx_buffer_size)
Opens a L2CAP Channel.
Definition l2cap.c:555
void l2cap_close_device(l2cap_device_t *device_handle)
Closes a L2CAP Connection.
Definition l2cap.c:495
Definition l2cap.h:45
Definition l2cap.h:73
Definition l2cap.h:37