Compare commits
@@ -6,35 +6,23 @@ name: Arduino-Pico CI
|
||||
on:
|
||||
pull_request:
|
||||
|
||||
env:
|
||||
TRAVIS_BUILD_DIR: ${{ github.workspace }}
|
||||
TRAVIS_TAG: ${{ github.ref }}
|
||||
|
||||
|
||||
jobs:
|
||||
|
||||
# Me no spell so good
|
||||
code-spell:
|
||||
name: Check spelling
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: true
|
||||
- name: Run codespell
|
||||
uses: codespell-project/actions-codespell@master
|
||||
with:
|
||||
skip: ./ArduinoCore-API,./libraries/ESP8266SdFat,./libraries/Adafruit_TinyUSB_Arduino,./libraries/LittleFS/lib,./tools/pyserial,./pico-sdk,./.github,./docs/i2s.rst,./cores/rp2040/api,./libraries/FreeRTOS,./tools/libbearssl/bearssl,./include,./libraries/WiFi/examples/BearSSL_Server,./ota/uzlib,./libraries/http-parser/lib,./libraries/WebServer/examples/HelloServerBearSSL/HelloServerBearSSL.ino,./libraries/HTTPUpdateServer/examples/SecureBearSSLUpdater/SecureBearSSLUpdater.ino,./.git,./libraries/FatFS/lib/fatfs,./libraries/FatFS/src/diskio.h,./libraries/FatFS/src/ff.cpp,./libraries/FatFS/src/ffconf.h,./libraries/FatFS/src/ffsystem.cpp,./libraries/FatFS/src/ff.h,./libraries/lwIP_WINC1500/src/driver,./libraries/lwIP_WINC1500/src/common,./libraries/lwIP_WINC1500/src/bus_wrapper,./libraries/lwIP_WINC1500/src/spi_flash
|
||||
ignore_words_list: ser,dout,shiftIn
|
||||
|
||||
# Consistent style
|
||||
# Consistent style, spelling
|
||||
astyle:
|
||||
name: Style, Boards, Package
|
||||
name: Spelling, Style, Boards, Package
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: false
|
||||
- name: Run codespell
|
||||
uses: codespell-project/actions-codespell@master
|
||||
with:
|
||||
skip: ./ArduinoCore-API,./libraries/ESP8266SdFat,./libraries/Adafruit_TinyUSB_Arduino,./libraries/LittleFS/lib,./tools/pyserial,./pico-sdk,./.github,./docs/i2s.rst,./cores/rp2040/api,./libraries/FreeRTOS,./tools/libbearssl/bearssl,./include,./libraries/WiFi/examples/BearSSL_Server,./ota/uzlib,./libraries/http-parser/lib,./libraries/WebServer/examples/HelloServerBearSSL/HelloServerBearSSL.ino,./libraries/HTTPUpdateServer/examples/SecureBearSSLUpdater/SecureBearSSLUpdater.ino,./.git,./libraries/FatFS/lib/fatfs,./libraries/FatFS/src/diskio.h,./libraries/FatFS/src/ff.cpp,./libraries/FatFS/src/ffconf.h,./libraries/FatFS/src/ffsystem.cpp,./libraries/FatFS/src/ff.h,./libraries/lwIP_WINC1500/src/driver,./libraries/lwIP_WINC1500/src/common,./libraries/lwIP_WINC1500/src/bus_wrapper,./libraries/lwIP_WINC1500/src/spi_flash
|
||||
ignore_words_list: ser,dout,shiftIn,acount
|
||||
- name: Get submodules for following tests
|
||||
run: git submodule update --init
|
||||
- name: Check package references
|
||||
run: |
|
||||
./tests/ci/pkgrefs_test.sh
|
||||
@@ -223,3 +211,55 @@ jobs:
|
||||
run: pio ci --board=rpipicow -O "platform_packages=framework-arduinopico@symlink:///home/runner/work/arduino-pico/arduino-pico" libraries/ArduinoOTA/examples/SignedOTA/SignedOTA.ino
|
||||
- name: Build Bluetooth Example
|
||||
run: pio ci --board=rpipicow -O "platform_packages=framework-arduinopico@symlink:///home/runner/work/arduino-pico/arduino-pico" -O "build_flags=-DPIO_FRAMEWORK_ARDUINO_ENABLE_BLUETOOTH" libraries/MouseBLE/examples/BLECircle/BLECircle.ino
|
||||
|
||||
# Build every variant using PIO for simplicity
|
||||
build-variants:
|
||||
name: Build Every Variant ${{ matrix.chunk }}
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
chunk: [0, 1]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: 'true'
|
||||
- name: Initialize needed submodules
|
||||
run: |
|
||||
cd pico-sdk
|
||||
git submodule update --init
|
||||
cd ../libraries/Adafruit_TinyUSB_Arduino
|
||||
git submodule update --init
|
||||
cd ../..
|
||||
- name: Cache pip
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: ~/.cache/pip
|
||||
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-pip-
|
||||
- name: Cache PlatformIO
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: ~/.platformio
|
||||
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- name: Install PlatformIO
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install --upgrade platformio
|
||||
pio pkg install --global --platform https://github.com/maxgerhardt/platform-raspberrypi.git
|
||||
pio pkg install --global --tool symlink://.
|
||||
cp -f /home/runner/work/arduino-pico/arduino-pico/tools/json/*.json /home/runner/.platformio/platforms/raspberrypi/boards/.
|
||||
- name: Build Every Variant
|
||||
run: |
|
||||
cnt=0
|
||||
for b in $(cut -f1 -d. /home/runner/work/arduino-pico/arduino-pico/boards.txt | sed 's/#.*//' | sed 's/^menu$//' | sort -u); do
|
||||
cnt=$((cnt + 1))
|
||||
rem=$((cnt % 2))
|
||||
if [ $rem == ${{ matrix.chunk }} ]; then
|
||||
pio ci --board=$b -O "platform_packages=framework-arduinopico@symlink:///home/runner/work/arduino-pico/arduino-pico" libraries/rp2040/examples/Bootsel/Bootsel.ino
|
||||
fi
|
||||
done
|
||||
|
||||
@@ -33,7 +33,6 @@ jobs:
|
||||
# pip install --upgrade platformio
|
||||
- name: Deploy updated JSON
|
||||
env:
|
||||
TRAVIS_BUILD_DIR: ${{ github.workspace }}
|
||||
BUILD_TYPE: package
|
||||
CI_GITHUB_API_KEY: ${{ secrets.GITHUB_TOKEN }}
|
||||
PLATFORMIO_AUTH_TOKEN: ${{ secrets.PLATFORMIO_AUTH_TOKEN }}
|
||||
|
||||
@@ -24,7 +24,6 @@ jobs:
|
||||
python-version: '3.x'
|
||||
- name: Build package JSON
|
||||
env:
|
||||
TRAVIS_BUILD_DIR: ${{ github.workspace }}
|
||||
BUILD_TYPE: package
|
||||
CI_GITHUB_API_KEY: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
|
||||
@@ -40,3 +40,6 @@
|
||||
[submodule "libraries/FatFS/lib/SPIFTL"]
|
||||
path = libraries/FatFS/lib/SPIFTL
|
||||
url = https://github.com/earlephilhower/SPIFTL.git
|
||||
[submodule "libraries/AsyncUDP"]
|
||||
path = libraries/AsyncUDP
|
||||
url = https://github.com/earlephilhower/AsyncUDP.git
|
||||
|
||||
@@ -25,9 +25,15 @@ Read the [Contributing Guide](https://github.com/earlephilhower/arduino-pico/blo
|
||||
* Adafruit QTPy RP2040
|
||||
* Adafruit STEMMA Friend RP2040
|
||||
* Adafruit Trinkey RP2040 QT
|
||||
* Amken Bunny
|
||||
* Amken Revelop
|
||||
* Amken Revelop Plus
|
||||
* Amken Revelop eS
|
||||
* Arduino Nano RP2040 Connect
|
||||
* ArtronShop RP2 Nano
|
||||
* Breadstick Raspberry
|
||||
* BridgeTek IDM2040-7A
|
||||
* BridgeTek IDM2040-43A
|
||||
* Cytron Maker Pi RP2040
|
||||
* Cytron Maker Nano RP2040
|
||||
* Cytron Maker Uno RP2040
|
||||
@@ -36,7 +42,9 @@ Read the [Contributing Guide](https://github.com/earlephilhower/arduino-pico/blo
|
||||
* DeRuiLab FlyBoard2040 Core
|
||||
* DFRobot Beetle RP2040
|
||||
* ElectronicCats Hunter Cat NFC
|
||||
* EVN Alpha
|
||||
* ExtremeElectronics RC2040
|
||||
* GroundStudio Marble Pico
|
||||
* Invector Labs Challenger RP2040 WiFi
|
||||
* Invector Labs Challenger RP2040 WiFi/BLE
|
||||
* Invector Labs Challenger RP2040 WiFi6/BLE
|
||||
@@ -49,6 +57,7 @@ Read the [Contributing Guide](https://github.com/earlephilhower/arduino-pico/blo
|
||||
* Invector Labs RPICO32
|
||||
* Melopero Cookie RP2040
|
||||
* Melopero Shake RP2040
|
||||
* METE HOCA Akana R1
|
||||
* Neko Systems BL2040 Mini
|
||||
* Olimex RP2040-Pico30
|
||||
* Newsan Archi
|
||||
@@ -56,6 +65,7 @@ Read the [Contributing Guide](https://github.com/earlephilhower/arduino-pico/blo
|
||||
* Pimoroni PGA2040
|
||||
* Pimoroni Plasma2040
|
||||
* Pimoroni Tiny2040
|
||||
* Pintronix PinMax
|
||||
* RAKwireless RAK11300
|
||||
* Redscorp RP2040-Eins
|
||||
* Redscorp RP2040-ProMini
|
||||
@@ -85,6 +95,7 @@ Read the [Contributing Guide](https://github.com/earlephilhower/arduino-pico/blo
|
||||
# Features
|
||||
* Adafruit TinyUSB Arduino (USB mouse, keyboard, flash drive, generic HID, CDC Serial, MIDI, WebUSB, others)
|
||||
* Bluetooth on the PicoW (Classic and BLE) with Keyboard, Mouse, Joystick, and Virtual Serial
|
||||
* Bluetooth Classic and BLE HID master mode (connect to BT keyboard, mouse, or joystick)
|
||||
* Generic Arduino USB Serial, Keyboard, Joystick, and Mouse emulation
|
||||
* WiFi (Pico W, ESP32-based ESPHost, Atmel WINC1500)
|
||||
* Ethernet (Wired W5500, W5100, ENC28J60)
|
||||
@@ -98,7 +109,7 @@ Read the [Contributing Guide](https://github.com/earlephilhower/arduino-pico/blo
|
||||
* digitalWrite/Read, shiftIn/Out, tone, analogWrite(PWM)/Read, temperature
|
||||
* Analog stereo audio in using DMA and the built-in ADC
|
||||
* Analog stereo audio out using PWM hardware
|
||||
* Bluetooth A2DP audio source (output) on the PicoW
|
||||
* Bluetooth A2DP audio source (output) and sink (input) on the PicoW
|
||||
* USB drive mode for data loggers (SingleFileDrive, FatFSUSB)
|
||||
* Peripherals: SPI master/slave, Wire(I2C) master/slave, dual UART, emulated EEPROM, I2S audio input/output, Servo
|
||||
* printf (i.e. debug) output over USB serial
|
||||
|
||||
+4487
-349
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,23 @@
|
||||
// Padded and checksummed version of: generated\Winbond\W25Q32JVxQ\boot2.bin
|
||||
|
||||
.cpu cortex-m0plus
|
||||
.thumb
|
||||
|
||||
.section .boot2, "ax"
|
||||
|
||||
.byte 0xf7, 0xb5, 0x73, 0x46, 0x21, 0x22, 0x02, 0x25, 0x01, 0x93, 0x29, 0x4b, 0xc0, 0x24, 0x5a, 0x60
|
||||
.byte 0x9a, 0x68, 0x01, 0x26, 0xaa, 0x43, 0xda, 0x60, 0x9a, 0x60, 0x1a, 0x61, 0x5a, 0x61, 0x00, 0x23
|
||||
.byte 0x64, 0x05, 0xa3, 0x60, 0x04, 0x33, 0x63, 0x61, 0x22, 0x4b, 0x28, 0x00, 0x1e, 0x60, 0xe0, 0x23
|
||||
.byte 0xdb, 0x02, 0x23, 0x60, 0x35, 0x23, 0xa6, 0x60, 0x23, 0x66, 0x23, 0x66, 0x00, 0xf0, 0x4a, 0xf8
|
||||
.byte 0xc0, 0xb2, 0xa8, 0x42, 0x12, 0xd0, 0x06, 0x23, 0x30, 0x00, 0x23, 0x66, 0x00, 0xf0, 0x42, 0xf8
|
||||
.byte 0x31, 0x23, 0x28, 0x00, 0x23, 0x66, 0x25, 0x66, 0x00, 0xf0, 0x3c, 0xf8, 0x03, 0x35, 0x25, 0x66
|
||||
.byte 0x02, 0x20, 0x25, 0x66, 0x00, 0xf0, 0x36, 0xf8, 0x30, 0x42, 0xf8, 0xd1, 0x00, 0x25, 0x12, 0x4b
|
||||
.byte 0xa5, 0x60, 0x12, 0x4f, 0x23, 0x60, 0x12, 0x4b, 0x65, 0x60, 0x01, 0x26, 0x3b, 0x60, 0xeb, 0x23
|
||||
.byte 0xa6, 0x60, 0x23, 0x66, 0x4b, 0x3b, 0x23, 0x66, 0x02, 0x20, 0x00, 0xf0, 0x23, 0xf8, 0x0d, 0x4b
|
||||
.byte 0xa5, 0x60, 0x3b, 0x60, 0xa6, 0x60, 0x01, 0x9b, 0xab, 0x42, 0x08, 0xd1, 0x0a, 0x4b, 0x0b, 0x4a
|
||||
.byte 0x13, 0x60, 0x1b, 0x68, 0x83, 0xf3, 0x08, 0x88, 0x09, 0x4b, 0x1b, 0x68, 0x18, 0x47, 0xf7, 0xbd
|
||||
.byte 0x00, 0x00, 0x02, 0x40, 0xf0, 0x00, 0x00, 0x18, 0x00, 0x03, 0x5f, 0x00, 0xf4, 0x00, 0x00, 0x18
|
||||
.byte 0x21, 0x22, 0x00, 0x00, 0x22, 0x20, 0x00, 0xa0, 0x00, 0x01, 0x00, 0x10, 0x08, 0xed, 0x00, 0xe0
|
||||
.byte 0x04, 0x01, 0x00, 0x10, 0xc0, 0x23, 0x02, 0x00, 0x04, 0x21, 0x5b, 0x05, 0x98, 0x6a, 0x08, 0x42
|
||||
.byte 0xfc, 0xd0, 0x01, 0x21, 0x98, 0x6a, 0x08, 0x42, 0xfc, 0xd1, 0x18, 0x6e, 0x01, 0x2a, 0x00, 0xd0
|
||||
.byte 0x18, 0x6e, 0x70, 0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x2d, 0x68, 0xca
|
||||
@@ -65,9 +65,9 @@ void noInterrupts();
|
||||
#define digitalPinToTimer(pin) (0)
|
||||
#define digitalPinToInterrupt(pin) (pin)
|
||||
#define NOT_AN_INTERRUPT (-1)
|
||||
#define portOutputRegister(port) ((volatile uint32_t*) sio_hw->gpio_out)
|
||||
#define portInputRegister(port) ((volatile uint32_t*) sio_hw->gpio_in)
|
||||
#define portModeRegister(port) ((volatile uint32_t*) sio_hw->gpio_oe)
|
||||
#define portOutputRegister(port) ((volatile uint32_t *)&(sio_hw->gpio_out))
|
||||
#define portInputRegister(port) ((volatile uint32_t *)&(sio_hw->gpio_in))
|
||||
#define portModeRegister(port) ((volatile uint32_t *)&(sio_hw->gpio_oe))
|
||||
#define digitalWriteFast(pin, val) (val ? sio_hw->gpio_set = (1 << pin) : sio_hw->gpio_clr = (1 << pin))
|
||||
#define digitalReadFast(pin) ((1 << pin) & sio_hw->gpio_in)
|
||||
#define sei() interrupts()
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
#include <hardware/watchdog.h>
|
||||
#include <pico/bootrom.h>
|
||||
#include "sdkoverride/tusb_absmouse.h"
|
||||
#include "sdkoverride/tusb_gamepad16.h"
|
||||
#include <device/usbd_pvt.h>
|
||||
|
||||
// Big, global USB mutex, shared with all USB devices to make sure we don't
|
||||
@@ -159,7 +160,7 @@ void __SetupDescHIDReport() {
|
||||
//allocate memory for the HID report descriptors. We don't use them, but need the size here.
|
||||
uint8_t desc_hid_report_mouse[] = { TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(1)) };
|
||||
uint8_t desc_hid_report_absmouse[] = { TUD_HID_REPORT_DESC_ABSMOUSE(HID_REPORT_ID(1)) };
|
||||
uint8_t desc_hid_report_joystick[] = { TUD_HID_REPORT_DESC_GAMEPAD(HID_REPORT_ID(1)) };
|
||||
uint8_t desc_hid_report_joystick[] = { TUD_HID_REPORT_DESC_GAMEPAD16(HID_REPORT_ID(1)) };
|
||||
uint8_t desc_hid_report_keyboard[] = { TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1)), TUD_HID_REPORT_DESC_CONSUMER(HID_REPORT_ID(2)) };
|
||||
int size = 0;
|
||||
|
||||
@@ -229,7 +230,7 @@ void __SetupDescHIDReport() {
|
||||
reportid++;
|
||||
offset += sizeof(desc_hid_report_absmouse);
|
||||
}
|
||||
uint8_t desc_local[] = { TUD_HID_REPORT_DESC_GAMEPAD(HID_REPORT_ID(reportid)) };
|
||||
uint8_t desc_local[] = { TUD_HID_REPORT_DESC_GAMEPAD16(HID_REPORT_ID(reportid)) };
|
||||
memcpy(__hid_report + offset, desc_local, sizeof(desc_local));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#define ARDUINO_PICO_MAJOR 3
|
||||
#define ARDUINO_PICO_MINOR 9
|
||||
#define ARDUINO_PICO_REVISION 2
|
||||
#define ARDUINO_PICO_VERSION_STR "3.9.2"
|
||||
#define ARDUINO_PICO_REVISION 4
|
||||
#define ARDUINO_PICO_VERSION_STR "3.9.4"
|
||||
|
||||
@@ -31,12 +31,10 @@ extern bool __isFreeRTOS;
|
||||
extern volatile bool __freeRTOSinitted;
|
||||
|
||||
extern "C" {
|
||||
#ifndef INC_FREERTOS_H
|
||||
struct QueueDefinition; /* Using old naming convention so as not to break kernel aware debuggers. */
|
||||
typedef struct QueueDefinition * QueueHandle_t;
|
||||
typedef QueueHandle_t SemaphoreHandle_t;
|
||||
typedef int32_t BaseType_t;
|
||||
#endif
|
||||
|
||||
extern bool __freertos_check_if_in_isr() __attribute__((weak));
|
||||
|
||||
|
||||
+260
-54
@@ -30,55 +30,16 @@
|
||||
#include <pico/mutex.h>
|
||||
#include <sys/lock.h>
|
||||
#include "_xoshiro.h"
|
||||
#include "lwip_wrap.h"
|
||||
|
||||
extern void ethernet_arch_lwip_begin() __attribute__((weak));
|
||||
extern void ethernet_arch_lwip_end() __attribute__((weak));
|
||||
extern void ethernet_arch_lwip_gpio_mask() __attribute__((weak));
|
||||
extern void ethernet_arch_lwip_gpio_unmask() __attribute__((weak));
|
||||
|
||||
auto_init_recursive_mutex(__lwipMutex); // Only for case with no Ethernet or PicoW, but still doing LWIP (PPP?)
|
||||
|
||||
class LWIPMutex {
|
||||
public:
|
||||
LWIPMutex() {
|
||||
if (ethernet_arch_lwip_gpio_mask) {
|
||||
ethernet_arch_lwip_gpio_mask();
|
||||
}
|
||||
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
|
||||
if (rp2040.isPicoW()) {
|
||||
cyw43_arch_lwip_begin();
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
if (ethernet_arch_lwip_begin) {
|
||||
ethernet_arch_lwip_begin();
|
||||
} else {
|
||||
recursive_mutex_enter_blocking(&__lwipMutex);
|
||||
}
|
||||
}
|
||||
|
||||
~LWIPMutex() {
|
||||
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
|
||||
if (rp2040.isPicoW()) {
|
||||
cyw43_arch_lwip_end();
|
||||
} else {
|
||||
#endif
|
||||
if (ethernet_arch_lwip_end) {
|
||||
ethernet_arch_lwip_end();
|
||||
} else {
|
||||
recursive_mutex_exit(&__lwipMutex);
|
||||
}
|
||||
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
|
||||
}
|
||||
#endif
|
||||
if (ethernet_arch_lwip_gpio_unmask) {
|
||||
ethernet_arch_lwip_gpio_unmask();
|
||||
}
|
||||
}
|
||||
};
|
||||
//auto_init_recursive_mutex(__lwipMutex); // Only for case with no Ethernet or PicoW, but still doing LWIP (PPP?)
|
||||
recursive_mutex_t __lwipMutex;
|
||||
|
||||
extern "C" {
|
||||
|
||||
extern void __lwip(__lwip_op op, void *req) __attribute((weak));
|
||||
extern bool __isLWIPThread();
|
||||
|
||||
static XoshiroCpp::Xoshiro256PlusPlus *_lwip_rng = nullptr;
|
||||
// Random number generator for LWIP
|
||||
unsigned long __lwip_rand() {
|
||||
@@ -89,6 +50,7 @@ extern "C" {
|
||||
extern void __real_lwip_init();
|
||||
void __wrap_lwip_init() {
|
||||
if (!_lwip_rng) {
|
||||
recursive_mutex_init(&__lwipMutex);
|
||||
_lwip_rng = new XoshiroCpp::Xoshiro256PlusPlus(micros() * rp2040.getCycleCount());
|
||||
__real_lwip_init();
|
||||
}
|
||||
@@ -97,215 +59,404 @@ extern "C" {
|
||||
extern u8_t __real_pbuf_header(struct pbuf *p, s16_t header_size);
|
||||
u8_t __wrap_pbuf_header(struct pbuf *p, s16_t header_size) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
u8_t ret;
|
||||
__pbuf_header_req req = { p, header_size, &ret };
|
||||
__lwip(__pbuf_header, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_pbuf_header(p, header_size);
|
||||
}
|
||||
|
||||
extern u8_t __real_pbuf_free(struct pbuf *p);
|
||||
u8_t __wrap_pbuf_free(struct pbuf *p) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
u8_t ret;
|
||||
__pbuf_free_req req = { p, &ret };
|
||||
__lwip(__pbuf_free, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_pbuf_free(p);
|
||||
}
|
||||
|
||||
extern struct pbuf *__real_pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type);
|
||||
struct pbuf *__wrap_pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
struct pbuf *ret;
|
||||
__pbuf_alloc_req req = {l, length, type, &ret };
|
||||
__lwip(__pbuf_alloc, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_pbuf_alloc(l, length, type);
|
||||
}
|
||||
|
||||
extern err_t __real_pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len);
|
||||
err_t __wrap_pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
err_t ret;
|
||||
__pbuf_take_req req = { buf, dataptr, len, &ret };
|
||||
__lwip(__pbuf_take, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_pbuf_take(buf, dataptr, len);
|
||||
}
|
||||
|
||||
extern u16_t __real_pbuf_copy_partial(const struct pbuf *p, void *dataptr, u16_t len, u16_t offset);
|
||||
u16_t __wrap_pbuf_copy_partial(const struct pbuf *p, void *dataptr, u16_t len, u16_t offset) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
u16_t ret;
|
||||
__pbuf_copy_partial_req req = { p, dataptr, len, offset, &ret };
|
||||
__lwip(__pbuf_copy_partial, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_pbuf_copy_partial(p, dataptr, len, offset);
|
||||
}
|
||||
|
||||
extern void __real_pbuf_ref(struct pbuf *p);
|
||||
void __wrap_pbuf_ref(struct pbuf *p) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__pbuf_ref_req req = { p };
|
||||
__lwip(__pbuf_ref, &req);
|
||||
return;
|
||||
}
|
||||
__real_pbuf_ref(p);
|
||||
}
|
||||
|
||||
extern u8_t __real_pbuf_get_at(const struct pbuf* p, u16_t offset);
|
||||
u8_t __wrap_pbuf_get_at(const struct pbuf* p, u16_t offset) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
u8_t ret;
|
||||
__pbuf_get_at_req req = { p, offset, &ret };
|
||||
__lwip(__pbuf_get_at, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_pbuf_get_at(p, offset);
|
||||
}
|
||||
|
||||
extern void *__real_pbuf_get_contiguous(const struct pbuf *p, void *buffer, size_t bufsize, u16_t len, u16_t offset);
|
||||
void *__wrap_pbuf_get_contiguous(const struct pbuf *p, void *buffer, size_t bufsize, u16_t len, u16_t offset) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
void *ret;
|
||||
__pbuf_get_contiguous_req req = { p, buffer, bufsize, len, offset, &ret };
|
||||
__lwip(__pbuf_get_contiguous, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_pbuf_get_contiguous(p, buffer, bufsize, len, offset);
|
||||
}
|
||||
|
||||
extern void __real_pbuf_cat(struct pbuf *head, struct pbuf *tail);
|
||||
void __wrap_pbuf_cat(struct pbuf *head, struct pbuf *tail) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__pbuf_cat_req req = { head, tail };
|
||||
__lwip(__pbuf_cat, &req);
|
||||
return;
|
||||
}
|
||||
__real_pbuf_cat(head, tail);
|
||||
}
|
||||
|
||||
extern void __real_tcp_arg(struct tcp_pcb *pcb, void *arg);
|
||||
void __wrap_tcp_arg(struct tcp_pcb *pcb, void *arg) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__tcp_arg_req req = { pcb, arg };
|
||||
__lwip(__tcp_arg, &req);
|
||||
return;
|
||||
}
|
||||
__real_tcp_arg(pcb, arg);
|
||||
}
|
||||
|
||||
extern struct tcp_pcb *__real_tcp_new(void);
|
||||
struct tcp_pcb *__wrap_tcp_new(void) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
struct tcp_pcb *ret;
|
||||
__tcp_new_req req = { &ret };
|
||||
__lwip(__tcp_new, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_tcp_new();
|
||||
}
|
||||
|
||||
extern err_t __real_tcp_bind(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port);
|
||||
err_t __wrap_tcp_bind(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
err_t ret;
|
||||
__tcp_bind_req req = { pcb, ipaddr, port, &ret };
|
||||
__lwip(__tcp_bind, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_tcp_bind(pcb, ipaddr, port);
|
||||
}
|
||||
|
||||
extern struct tcp_pcb *__real_tcp_listen(struct tcp_pcb *pcb);
|
||||
struct tcp_pcb *__wrap_tcp_listen(struct tcp_pcb *pcb) {
|
||||
LWIPMutex m;
|
||||
return __real_tcp_listen(pcb);
|
||||
}
|
||||
|
||||
extern struct tcp_pcb *__real_tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog);
|
||||
struct tcp_pcb *__wrap_tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
struct tcp_pcb *ret;
|
||||
__tcp_listen_with_backlog_req req = { pcb, backlog, &ret };
|
||||
__lwip(__tcp_listen_with_backlog, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_tcp_listen_with_backlog(pcb, backlog);
|
||||
}
|
||||
|
||||
extern void __real_tcp_accept(struct tcp_pcb *pcb, err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err));
|
||||
void __wrap_tcp_accept(struct tcp_pcb *pcb, err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err)) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__tcp_accept_req req = { pcb, accept };
|
||||
__lwip(__tcp_accept, &req);
|
||||
return;
|
||||
}
|
||||
__real_tcp_accept(pcb, accept);
|
||||
}
|
||||
|
||||
extern err_t __real_tcp_connect(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port, err_t (* connected)(void *arg, struct tcp_pcb *tpcb, err_t err));
|
||||
err_t __wrap_tcp_connect(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port, err_t (* connected)(void *arg, struct tcp_pcb *tpcb, err_t err)) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
err_t ret;
|
||||
__tcp_connect_req req = { pcb, ipaddr, port, connected, &ret };
|
||||
__lwip(__tcp_connect, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_tcp_connect(pcb, ipaddr, port, connected);
|
||||
}
|
||||
|
||||
extern err_t __real_tcp_write(struct tcp_pcb *pcb, const void *dataptr, u16_t len, u8_t apiflags);
|
||||
err_t __wrap_tcp_write(struct tcp_pcb *pcb, const void *dataptr, u16_t len, u8_t apiflags) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
err_t ret;
|
||||
__tcp_write_req req = { pcb, dataptr, len, apiflags, &ret };
|
||||
__lwip(__tcp_write, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_tcp_write(pcb, dataptr, len, apiflags);
|
||||
}
|
||||
|
||||
extern void __real_tcp_sent(struct tcp_pcb *pcb, err_t (* sent)(void *arg, struct tcp_pcb *tpcb, u16_t len));
|
||||
void __wrap_tcp_sent(struct tcp_pcb *pcb, err_t (* sent)(void *arg, struct tcp_pcb *tpcb, u16_t len)) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__tcp_sent_req req = { pcb, sent };
|
||||
__lwip(__tcp_sent, &req);
|
||||
return;
|
||||
}
|
||||
__real_tcp_sent(pcb, sent);
|
||||
}
|
||||
|
||||
extern void __real_tcp_recv(struct tcp_pcb *pcb, err_t (* recv)(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err));
|
||||
void __wrap_tcp_recv(struct tcp_pcb *pcb, err_t (* recv)(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__tcp_recv_req req = { pcb, recv };
|
||||
__lwip(__tcp_recv, &req);
|
||||
return;
|
||||
}
|
||||
__real_tcp_recv(pcb, recv);
|
||||
}
|
||||
|
||||
extern void __real_tcp_recved(struct tcp_pcb *pcb, u16_t len);
|
||||
void __wrap_tcp_recved(struct tcp_pcb *pcb, u16_t len) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__tcp_recved_req req = { pcb, len };
|
||||
__lwip(__tcp_recved, &req);
|
||||
return;
|
||||
}
|
||||
__real_tcp_recved(pcb, len);
|
||||
}
|
||||
|
||||
extern void __real_tcp_poll(struct tcp_pcb *pcb, err_t (* poll)(void *arg, struct tcp_pcb *tpcb), u8_t interval);
|
||||
void __wrap_tcp_poll(struct tcp_pcb *pcb, err_t (* poll)(void *arg, struct tcp_pcb *tpcb), u8_t interval) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__tcp_poll_req req = { pcb, poll, interval };
|
||||
__lwip(__tcp_poll, &req);
|
||||
return;
|
||||
}
|
||||
__real_tcp_poll(pcb, poll, interval);
|
||||
}
|
||||
|
||||
extern err_t __real_tcp_close(struct tcp_pcb *pcb);
|
||||
err_t __wrap_tcp_close(struct tcp_pcb *pcb) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
err_t ret;
|
||||
__tcp_close_req req = { pcb, &ret };
|
||||
__lwip(__tcp_close, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_tcp_close(pcb);
|
||||
}
|
||||
|
||||
extern void __real_tcp_abort(struct tcp_pcb *pcb);
|
||||
void __wrap_tcp_abort(struct tcp_pcb *pcb) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__tcp_abort_req req = { pcb };
|
||||
__lwip(__tcp_abort, &req);
|
||||
return;
|
||||
}
|
||||
__real_tcp_abort(pcb);
|
||||
}
|
||||
|
||||
extern void __real_tcp_err(struct tcp_pcb *pcb, void (* err)(void *arg, err_t err));
|
||||
void __wrap_tcp_err(struct tcp_pcb *pcb, void (* err)(void *arg, err_t err)) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__tcp_err_req req = { pcb, err };
|
||||
__lwip(__tcp_err, &req);
|
||||
return;
|
||||
}
|
||||
__real_tcp_err(pcb, err);
|
||||
}
|
||||
|
||||
extern err_t __real_tcp_output(struct tcp_pcb *pcb);
|
||||
err_t __wrap_tcp_output(struct tcp_pcb *pcb) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
err_t ret;
|
||||
__tcp_output_req req = { pcb, &ret };
|
||||
__lwip(__tcp_output, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_tcp_output(pcb);
|
||||
}
|
||||
|
||||
extern void __real_tcp_setprio(struct tcp_pcb *pcb, u8_t prio);
|
||||
void __wrap_tcp_setprio(struct tcp_pcb *pcb, u8_t prio) {
|
||||
LWIPMutex m;
|
||||
return __real_tcp_setprio(pcb, prio);
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__tcp_setprio_req req = { pcb, prio };
|
||||
__lwip(__tcp_setprio, &req);
|
||||
return;
|
||||
}
|
||||
__real_tcp_setprio(pcb, prio);
|
||||
}
|
||||
|
||||
extern void __real_tcp_backlog_delayed(struct tcp_pcb* pcb);
|
||||
void __wrap_tcp_backlog_delayed(struct tcp_pcb* pcb) {
|
||||
LWIPMutex m;
|
||||
return __real_tcp_backlog_delayed(pcb);
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__tcp_backlog_delayed_req req = { pcb };
|
||||
__lwip(__tcp_backlog_delayed, &req);
|
||||
return;
|
||||
}
|
||||
__real_tcp_backlog_delayed(pcb);
|
||||
}
|
||||
|
||||
extern void __real_tcp_backlog_accepted(struct tcp_pcb* pcb);
|
||||
void __wrap_tcp_backlog_accepted(struct tcp_pcb* pcb) {
|
||||
LWIPMutex m;
|
||||
return __real_tcp_backlog_accepted(pcb);
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__tcp_backlog_accepted_req req = { pcb };
|
||||
__lwip(__tcp_backlog_accepted, &req);
|
||||
return;
|
||||
}
|
||||
__real_tcp_backlog_accepted(pcb);
|
||||
}
|
||||
extern struct udp_pcb *__real_udp_new(void);
|
||||
struct udp_pcb *__wrap_udp_new(void) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
struct udp_pcb *ret;
|
||||
__udp_new_req req = { &ret };
|
||||
__lwip(__udp_new, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_udp_new();
|
||||
}
|
||||
|
||||
extern void __real_udp_remove(struct udp_pcb *pcb);
|
||||
void __wrap_udp_remove(struct udp_pcb *pcb) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__udp_remove_req req = { pcb };
|
||||
__lwip(__udp_remove, &req);
|
||||
return;
|
||||
}
|
||||
__real_udp_remove(pcb);
|
||||
}
|
||||
|
||||
extern err_t __real_udp_bind(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port);
|
||||
err_t __wrap_udp_bind(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
err_t ret;
|
||||
__udp_bind_req req = { pcb, ipaddr, port, &ret };
|
||||
__lwip(__udp_bind, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_udp_bind(pcb, ipaddr, port);
|
||||
}
|
||||
|
||||
extern err_t __real_udp_connect(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port);
|
||||
err_t __wrap_udp_connect(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
err_t ret;
|
||||
__udp_connect_req req = { pcb, ipaddr, port, &ret };
|
||||
__lwip(__udp_connect, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_udp_connect(pcb, ipaddr, port);
|
||||
}
|
||||
|
||||
extern err_t __real_udp_disconnect(struct udp_pcb *pcb);
|
||||
err_t __wrap_udp_disconnect(struct udp_pcb *pcb) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
err_t ret;
|
||||
__udp_disconnect_req req = { pcb, &ret };
|
||||
__lwip(__udp_disconnect, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_udp_disconnect(pcb);
|
||||
}
|
||||
|
||||
extern err_t __real_udp_send(struct udp_pcb *pcb, struct pbuf *p);
|
||||
err_t __wrap_udp_send(struct udp_pcb *pcb, struct pbuf *p) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
err_t ret;
|
||||
__udp_send_req req = { pcb, p, &ret };
|
||||
__lwip(__udp_send, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_udp_send(pcb, p);
|
||||
}
|
||||
|
||||
extern void __real_udp_recv(struct udp_pcb *pcb, void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p, ip_addr_t *addr, u16_t port), void *recv_arg);
|
||||
void __wrap_udp_recv(struct udp_pcb *pcb, void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p, ip_addr_t *addr, u16_t port), void *recv_arg) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__udp_recv_req req = { pcb, recv, recv_arg };
|
||||
__lwip(__udp_recv, &req);
|
||||
return;
|
||||
}
|
||||
__real_udp_recv(pcb, recv, recv_arg);
|
||||
}
|
||||
|
||||
extern err_t __real_udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif);
|
||||
err_t __wrap_udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
err_t ret;
|
||||
__udp_sendto_if_req req = { pcb, p, dst_ip, dst_port, netif, &ret };
|
||||
__lwip(__udp_sendto_if, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
|
||||
}
|
||||
|
||||
@@ -313,60 +464,115 @@ extern "C" {
|
||||
extern void __real_sys_check_timeouts();
|
||||
void __wrap_sys_check_timeouts(void) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__lwip(__sys_check_timeouts, nullptr);
|
||||
return;
|
||||
}
|
||||
__real_sys_check_timeouts();
|
||||
}
|
||||
|
||||
extern err_t __real_dns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback found, void *callback_arg);
|
||||
err_t __wrap_dns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback found, void *callback_arg) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
err_t ret;
|
||||
__dns_gethostbyname_req req = { hostname, addr, found, callback_arg, &ret };
|
||||
__lwip(__dns_gethostbyname, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_dns_gethostbyname(hostname, addr, found, callback_arg);
|
||||
}
|
||||
|
||||
extern err_t __real_dns_gethostbyname_addrtype(const char *hostname, ip_addr_t *addr, dns_found_callback found, void *callback_arg, u8_t dns_addrtype);
|
||||
err_t __wrap_dns_gethostbyname_addrtype(const char *hostname, ip_addr_t *addr, dns_found_callback found, void *callback_arg, u8_t dns_addrtype) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
err_t ret;
|
||||
__dns_gethostbyname_addrtype_req req = { hostname, addr, found, callback_arg, dns_addrtype, &ret };
|
||||
__lwip(__dns_gethostbyname_addrtype, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_dns_gethostbyname_addrtype(hostname, addr, found, callback_arg, dns_addrtype);
|
||||
}
|
||||
|
||||
extern struct raw_pcb *__real_raw_new(u8_t proto);
|
||||
struct raw_pcb *__wrap_raw_new(u8_t proto) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
struct raw_pcb *ret;
|
||||
__raw_new_req req = { proto, &ret };
|
||||
__lwip(__raw_new, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_raw_new(proto);
|
||||
}
|
||||
|
||||
extern void __real_raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg);
|
||||
void __wrap_raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__raw_recv_req req = { pcb, recv, recv_arg };
|
||||
__lwip(__raw_recv, &req);
|
||||
return;
|
||||
}
|
||||
__real_raw_recv(pcb, recv, recv_arg);
|
||||
}
|
||||
|
||||
extern err_t __real_raw_bind(struct raw_pcb *pcb, const ip_addr_t *ipaddr);
|
||||
err_t __wrap_raw_bind(struct raw_pcb *pcb, const ip_addr_t *ipaddr) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
err_t ret;
|
||||
__raw_bind_req req = { pcb, ipaddr, &ret };
|
||||
__lwip(__raw_bind, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_raw_bind(pcb, ipaddr);
|
||||
}
|
||||
|
||||
extern err_t __real_raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr);
|
||||
err_t __wrap_raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
err_t ret;
|
||||
__raw_sendto_req req = { pcb, p, ipaddr, &ret };
|
||||
__lwip(__raw_sendto, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_raw_sendto(pcb, p, ipaddr);
|
||||
}
|
||||
|
||||
extern void __real_raw_remove(struct raw_pcb *pcb);
|
||||
void __wrap_raw_remove(struct raw_pcb *pcb) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__raw_remove_req req = { pcb };
|
||||
__lwip(__raw_remove, &req);
|
||||
return;
|
||||
}
|
||||
__real_raw_remove(pcb);
|
||||
}
|
||||
|
||||
extern struct netif *__real_netif_add(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input);
|
||||
struct netif *__wrap_netif_add(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
struct netif *ret;
|
||||
__netif_add_req req = { netif, ipaddr, netmask, gw, state, init, input, &ret };
|
||||
__lwip(__netif_add, &req);
|
||||
return ret;
|
||||
}
|
||||
return __real_netif_add(netif, ipaddr, netmask, gw, state, init, input);
|
||||
}
|
||||
|
||||
extern void __real_netif_remove(struct netif *netif);
|
||||
void __wrap_netif_remove(struct netif *netif) {
|
||||
LWIPMutex m;
|
||||
if (__isFreeRTOS && !__isLWIPThread()) {
|
||||
__netif_remove_req req = { netif };
|
||||
__lwip(__netif_remove, &req);
|
||||
return;
|
||||
}
|
||||
__real_netif_remove(netif);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,470 @@
|
||||
/*
|
||||
FreeRTOS LWIP wrappers, implement a single LWIP work task
|
||||
|
||||
Copyright (c) 2024 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <lwip/pbuf.h>
|
||||
#include <lwip/udp.h>
|
||||
#include <lwip/tcp.h>
|
||||
#include <lwip/dns.h>
|
||||
#include <lwip/raw.h>
|
||||
#include <lwip/timeouts.h>
|
||||
|
||||
|
||||
extern void ethernet_arch_lwip_begin() __attribute__((weak));
|
||||
extern void ethernet_arch_lwip_end() __attribute__((weak));
|
||||
extern void ethernet_arch_lwip_gpio_mask() __attribute__((weak));
|
||||
extern void ethernet_arch_lwip_gpio_unmask() __attribute__((weak));
|
||||
|
||||
//auto_init_recursive_mutex(__lwipMutex); // Only for case with no Ethernet or PicoW, but still doing LWIP (PPP?)
|
||||
extern recursive_mutex_t __lwipMutex;
|
||||
|
||||
|
||||
class LWIPMutex {
|
||||
public:
|
||||
LWIPMutex() {
|
||||
if (ethernet_arch_lwip_gpio_mask) {
|
||||
ethernet_arch_lwip_gpio_mask();
|
||||
}
|
||||
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
|
||||
if (rp2040.isPicoW()) {
|
||||
cyw43_arch_lwip_begin();
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
if (ethernet_arch_lwip_begin) {
|
||||
ethernet_arch_lwip_begin();
|
||||
} else {
|
||||
recursive_mutex_enter_blocking(&__lwipMutex);
|
||||
}
|
||||
}
|
||||
|
||||
~LWIPMutex() {
|
||||
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
|
||||
if (rp2040.isPicoW()) {
|
||||
cyw43_arch_lwip_end();
|
||||
} else {
|
||||
#endif
|
||||
if (ethernet_arch_lwip_end) {
|
||||
ethernet_arch_lwip_end();
|
||||
} else {
|
||||
recursive_mutex_exit(&__lwipMutex);
|
||||
}
|
||||
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
|
||||
}
|
||||
#endif
|
||||
if (ethernet_arch_lwip_gpio_unmask) {
|
||||
ethernet_arch_lwip_gpio_unmask();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Implement all LWIP operations in a single FreeRTOS task. Calls to LWIP will
|
||||
// actually post work on the work queue and wait until the LWIP task indicates
|
||||
// completion
|
||||
|
||||
// Enumerated type for LWIP request
|
||||
typedef enum {
|
||||
__lwip_init = 1000,
|
||||
|
||||
__pbuf_header = 2000,
|
||||
__pbuf_free,
|
||||
__pbuf_alloc,
|
||||
__pbuf_take,
|
||||
__pbuf_copy_partial,
|
||||
__pbuf_ref,
|
||||
__pbuf_get_at,
|
||||
__pbuf_get_contiguous,
|
||||
__pbuf_cat,
|
||||
|
||||
__tcp_arg = 3000,
|
||||
__tcp_new,
|
||||
__tcp_bind,
|
||||
__tcp_listen,
|
||||
__tcp_listen_with_backlog,
|
||||
__tcp_accept,
|
||||
__tcp_connect,
|
||||
__tcp_write,
|
||||
__tcp_sent,
|
||||
__tcp_recv,
|
||||
__tcp_recved,
|
||||
__tcp_poll,
|
||||
__tcp_close,
|
||||
__tcp_abort,
|
||||
__tcp_err,
|
||||
__tcp_output,
|
||||
__tcp_setprio,
|
||||
__tcp_backlog_delayed,
|
||||
__tcp_backlog_accepted,
|
||||
|
||||
__udp_new = 4000,
|
||||
__udp_remove,
|
||||
__udp_bind,
|
||||
__udp_connect,
|
||||
__udp_disconnect,
|
||||
__udp_send,
|
||||
__udp_recv,
|
||||
__udp_sendto_if,
|
||||
|
||||
__sys_check_timeouts = 5000,
|
||||
|
||||
__dns_gethostbyname = 6000,
|
||||
__dns_gethostbyname_addrtype,
|
||||
|
||||
__raw_new = 7000,
|
||||
__raw_recv,
|
||||
__raw_bind,
|
||||
__raw_sendto,
|
||||
__raw_remove,
|
||||
|
||||
__netif_add = 8000,
|
||||
__netif_remove
|
||||
} __lwip_op;
|
||||
|
||||
// Set up a local request buffer and call this to add to lwip work queue. Will only return once lwip operation completed
|
||||
// LWIP callbacks will happen from the LWIP task at some future time
|
||||
void __lwip(__lwip_op op, void *req);
|
||||
void __lwipISR(__lwip_op op, void *req);
|
||||
|
||||
extern void __real_lwip_init();
|
||||
extern u8_t __real_pbuf_header(struct pbuf *p, s16_t header_size);
|
||||
extern u8_t __real_pbuf_free(struct pbuf *p);
|
||||
extern struct pbuf *__real_pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type);
|
||||
extern err_t __real_pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len);
|
||||
extern u16_t __real_pbuf_copy_partial(const struct pbuf *p, void *dataptr, u16_t len, u16_t offset);
|
||||
extern void __real_pbuf_ref(struct pbuf *p);
|
||||
extern u8_t __real_pbuf_get_at(const struct pbuf* p, u16_t offset);
|
||||
extern void *__real_pbuf_get_contiguous(const struct pbuf *p, void *buffer, size_t bufsize, u16_t len, u16_t offset);
|
||||
extern void __real_pbuf_cat(struct pbuf *head, struct pbuf *tail);
|
||||
extern void __real_tcp_arg(struct tcp_pcb *pcb, void *arg);
|
||||
extern struct tcp_pcb *__real_tcp_new(void);
|
||||
extern err_t __real_tcp_bind(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port);
|
||||
extern struct tcp_pcb *__real_tcp_listen(struct tcp_pcb *pcb);
|
||||
extern struct tcp_pcb *__real_tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog);
|
||||
extern void __real_tcp_accept(struct tcp_pcb *pcb, err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err));
|
||||
extern err_t __real_tcp_connect(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port, err_t (* connected)(void *arg, struct tcp_pcb *tpcb, err_t err));
|
||||
extern err_t __real_tcp_write(struct tcp_pcb *pcb, const void *dataptr, u16_t len, u8_t apiflags);
|
||||
extern void __real_tcp_sent(struct tcp_pcb *pcb, err_t (* sent)(void *arg, struct tcp_pcb *tpcb, u16_t len));
|
||||
extern void __real_tcp_recv(struct tcp_pcb *pcb, err_t (* recv)(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err));
|
||||
extern void __real_tcp_recved(struct tcp_pcb *pcb, u16_t len);
|
||||
extern void __real_tcp_poll(struct tcp_pcb *pcb, err_t (* poll)(void *arg, struct tcp_pcb *tpcb), u8_t interval);
|
||||
extern err_t __real_tcp_close(struct tcp_pcb *pcb);
|
||||
extern void __real_tcp_abort(struct tcp_pcb *pcb);
|
||||
extern void __real_tcp_err(struct tcp_pcb *pcb, void (* err)(void *arg, err_t err));
|
||||
extern err_t __real_tcp_output(struct tcp_pcb *pcb);
|
||||
extern void __real_tcp_setprio(struct tcp_pcb *pcb, u8_t prio);
|
||||
extern void __real_tcp_backlog_delayed(struct tcp_pcb* pcb);
|
||||
extern void __real_tcp_backlog_accepted(struct tcp_pcb* pcb);
|
||||
extern struct udp_pcb *__real_udp_new(void);
|
||||
extern void __real_udp_remove(struct udp_pcb *pcb);
|
||||
extern err_t __real_udp_bind(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port);
|
||||
extern err_t __real_udp_connect(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port);
|
||||
extern err_t __real_udp_disconnect(struct udp_pcb *pcb);
|
||||
extern err_t __real_udp_send(struct udp_pcb *pcb, struct pbuf *p);
|
||||
extern void __real_udp_recv(struct udp_pcb *pcb, void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p, ip_addr_t *addr, u16_t port), void *recv_arg);
|
||||
extern err_t __real_udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif);
|
||||
extern void __real_sys_check_timeouts();
|
||||
extern err_t __real_dns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback found, void *callback_arg);
|
||||
extern err_t __real_dns_gethostbyname_addrtype(const char *hostname, ip_addr_t *addr, dns_found_callback found, void *callback_arg, u8_t dns_addrtype);
|
||||
extern struct raw_pcb *__real_raw_new(u8_t proto);
|
||||
extern void __real_raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg);
|
||||
extern err_t __real_raw_bind(struct raw_pcb *pcb, const ip_addr_t *ipaddr);
|
||||
extern err_t __real_raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr);
|
||||
extern void __real_raw_remove(struct raw_pcb *pcb);
|
||||
extern struct netif *__real_netif_add(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input);
|
||||
extern void __real_netif_remove(struct netif *netif);
|
||||
|
||||
typedef struct {
|
||||
struct pbuf *p;
|
||||
s16_t header_size;
|
||||
u8_t *ret;
|
||||
} __pbuf_header_req;
|
||||
|
||||
typedef struct {
|
||||
struct pbuf *p;
|
||||
u8_t *ret;
|
||||
} __pbuf_free_req;
|
||||
|
||||
typedef struct {
|
||||
pbuf_layer l;
|
||||
u16_t length;
|
||||
pbuf_type type;
|
||||
struct pbuf **ret;
|
||||
} __pbuf_alloc_req;
|
||||
|
||||
typedef struct {
|
||||
struct pbuf *buf;
|
||||
const void *dataptr;
|
||||
u16_t len;
|
||||
err_t *ret;
|
||||
} __pbuf_take_req;
|
||||
|
||||
typedef struct {
|
||||
const struct pbuf *p;
|
||||
void *dataptr;
|
||||
u16_t len;
|
||||
u16_t offset;
|
||||
u16_t *ret;
|
||||
} __pbuf_copy_partial_req;
|
||||
|
||||
typedef struct {
|
||||
struct pbuf *p;
|
||||
} __pbuf_ref_req;
|
||||
|
||||
typedef struct {
|
||||
const struct pbuf* p;
|
||||
u16_t offset;
|
||||
u8_t *ret;
|
||||
} __pbuf_get_at_req;
|
||||
|
||||
typedef struct {
|
||||
const struct pbuf *p;
|
||||
void *buffer;
|
||||
size_t bufsize;
|
||||
u16_t len;
|
||||
u16_t offset;
|
||||
void **ret;
|
||||
} __pbuf_get_contiguous_req;
|
||||
|
||||
typedef struct {
|
||||
struct pbuf *head;
|
||||
struct pbuf *tail;
|
||||
} __pbuf_cat_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
void *arg;
|
||||
} __tcp_arg_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb **ret;
|
||||
} __tcp_new_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
ip_addr_t *ipaddr;
|
||||
u16_t port;
|
||||
err_t *ret;
|
||||
} __tcp_bind_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
struct tcp_pcb **ret;
|
||||
} __tcp_listen_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
u8_t backlog;
|
||||
struct tcp_pcb **ret;
|
||||
} __tcp_listen_with_backlog_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
err_t (* accept)(void *arg, struct tcp_pcb *newpcb, err_t err);
|
||||
} __tcp_accept_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
ip_addr_t *ipaddr;
|
||||
u16_t port;
|
||||
err_t (* connected)(void *arg, struct tcp_pcb *tpcb, err_t err);
|
||||
err_t *ret;
|
||||
} __tcp_connect_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
const void *dataptr;
|
||||
u16_t len;
|
||||
u8_t apiflags;
|
||||
err_t *ret;
|
||||
} __tcp_write_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
err_t (* sent)(void *arg, struct tcp_pcb *tpcb, u16_t len);
|
||||
} __tcp_sent_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
err_t (* recv)(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
|
||||
} __tcp_recv_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
u16_t len;
|
||||
} __tcp_recved_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
err_t (* poll)(void *arg, struct tcp_pcb *tpcb);
|
||||
u8_t interval;
|
||||
} __tcp_poll_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
err_t *ret;
|
||||
} __tcp_close_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
} __tcp_abort_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
void (* err)(void *arg, err_t err);
|
||||
} __tcp_err_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
err_t *ret;
|
||||
} __tcp_output_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb *pcb;
|
||||
u8_t prio;
|
||||
} __tcp_setprio_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb* pcb;
|
||||
} __tcp_backlog_delayed_req;
|
||||
|
||||
typedef struct {
|
||||
struct tcp_pcb* pcb;
|
||||
} __tcp_backlog_accepted_req;
|
||||
|
||||
typedef struct {
|
||||
struct udp_pcb **ret;
|
||||
} __udp_new_req;
|
||||
|
||||
typedef struct {
|
||||
struct udp_pcb *pcb;
|
||||
} __udp_remove_req;
|
||||
|
||||
typedef struct {
|
||||
struct udp_pcb *pcb;
|
||||
ip_addr_t *ipaddr;
|
||||
u16_t port;
|
||||
err_t *ret;
|
||||
} __udp_bind_req;
|
||||
|
||||
typedef struct {
|
||||
struct udp_pcb *pcb;
|
||||
ip_addr_t *ipaddr;
|
||||
u16_t port;
|
||||
err_t *ret;
|
||||
} __udp_connect_req;
|
||||
|
||||
typedef struct {
|
||||
struct udp_pcb *pcb;
|
||||
err_t *ret;
|
||||
} __udp_disconnect_req;
|
||||
|
||||
typedef struct {
|
||||
struct udp_pcb *pcb;
|
||||
struct pbuf *p;
|
||||
err_t *ret;
|
||||
} __udp_send_req;
|
||||
|
||||
typedef struct {
|
||||
struct udp_pcb *pcb;
|
||||
void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p, ip_addr_t *addr, u16_t port);
|
||||
void *recv_arg;
|
||||
} __udp_recv_req;
|
||||
|
||||
typedef struct {
|
||||
struct udp_pcb *pcb;
|
||||
struct pbuf *p;
|
||||
const ip_addr_t *dst_ip;
|
||||
u16_t dst_port;
|
||||
struct netif *netif;
|
||||
err_t *ret;
|
||||
} __udp_sendto_if_req;
|
||||
|
||||
typedef struct {
|
||||
const char *hostname;
|
||||
ip_addr_t *addr;
|
||||
dns_found_callback found;
|
||||
void *callback_arg;
|
||||
err_t *ret;
|
||||
} __dns_gethostbyname_req;
|
||||
|
||||
typedef struct {
|
||||
const char *hostname;
|
||||
ip_addr_t *addr;
|
||||
dns_found_callback found;
|
||||
void *callback_arg;
|
||||
u8_t dns_addrtype;
|
||||
err_t *ret;
|
||||
} __dns_gethostbyname_addrtype_req;
|
||||
|
||||
typedef struct {
|
||||
u8_t proto;
|
||||
struct raw_pcb **ret;
|
||||
} __raw_new_req;
|
||||
|
||||
typedef struct {
|
||||
struct raw_pcb *pcb;
|
||||
raw_recv_fn recv;
|
||||
void *recv_arg;
|
||||
} __raw_recv_req;
|
||||
|
||||
typedef struct {
|
||||
struct raw_pcb *pcb;
|
||||
const ip_addr_t *ipaddr;
|
||||
err_t *ret;
|
||||
} __raw_bind_req;
|
||||
|
||||
typedef struct {
|
||||
struct raw_pcb *pcb;
|
||||
struct pbuf *p;
|
||||
const ip_addr_t *ipaddr;
|
||||
err_t *ret;
|
||||
} __raw_sendto_req;
|
||||
|
||||
typedef struct {
|
||||
struct raw_pcb *pcb;
|
||||
} __raw_remove_req;
|
||||
|
||||
typedef struct {
|
||||
struct netif *netif;
|
||||
const ip4_addr_t *ipaddr;
|
||||
const ip4_addr_t *netmask;
|
||||
const ip4_addr_t *gw;
|
||||
void *state;
|
||||
netif_init_fn init;
|
||||
netif_input_fn input;
|
||||
struct netif **ret;
|
||||
} __netif_add_req;
|
||||
|
||||
typedef struct {
|
||||
struct netif *netif;
|
||||
} __netif_remove_req;
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
16-bit axis gamepad definition
|
||||
Copyright (c) 2024 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "tusb.h"
|
||||
#include "class/hid/hid_device.h"
|
||||
|
||||
#define TUD_HID_REPORT_DESC_GAMEPAD16(...) \
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_GAMEPAD ) ,\
|
||||
HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\
|
||||
/* Report ID if any */\
|
||||
__VA_ARGS__ \
|
||||
/* 16 bit X, Y, Z, Rz, Rx, Ry (min -32767, max 32767 ) */ \
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_X ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_Y ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_Z ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_RZ ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_RX ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_RY ) ,\
|
||||
HID_LOGICAL_MIN_N ( -32767, 2 ) ,\
|
||||
HID_LOGICAL_MAX_N ( 32767, 2 ) ,\
|
||||
HID_REPORT_COUNT ( 6 ) ,\
|
||||
HID_REPORT_SIZE ( 16 ) ,\
|
||||
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
|
||||
/* 8 bit DPad/Hat Button Map */ \
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_HAT_SWITCH ) ,\
|
||||
HID_LOGICAL_MIN ( 1 ) ,\
|
||||
HID_LOGICAL_MAX ( 8 ) ,\
|
||||
HID_PHYSICAL_MIN ( 0 ) ,\
|
||||
HID_PHYSICAL_MAX_N ( 315, 2 ) ,\
|
||||
HID_REPORT_COUNT ( 1 ) ,\
|
||||
HID_REPORT_SIZE ( 8 ) ,\
|
||||
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
|
||||
/* 32 bit Button Map */ \
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\
|
||||
HID_USAGE_MIN ( 1 ) ,\
|
||||
HID_USAGE_MAX ( 32 ) ,\
|
||||
HID_LOGICAL_MIN ( 0 ) ,\
|
||||
HID_LOGICAL_MAX ( 1 ) ,\
|
||||
HID_REPORT_COUNT ( 32 ) ,\
|
||||
HID_REPORT_SIZE ( 1 ) ,\
|
||||
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
|
||||
HID_COLLECTION_END \
|
||||
|
||||
// HID Gamepad Protocol Report.
|
||||
typedef struct TU_ATTR_PACKED {
|
||||
int16_t x; ///< Delta x movement of left analog-stick
|
||||
int16_t y; ///< Delta y movement of left analog-stick
|
||||
int16_t z; ///< Delta z movement of right analog-joystick
|
||||
int16_t rz; ///< Delta Rz movement of right analog-joystick
|
||||
int16_t rx; ///< Delta Rx movement of analog left trigger
|
||||
int16_t ry; ///< Delta Ry movement of analog right trigger
|
||||
uint8_t hat; ///< Buttons mask for currently pressed buttons in the DPad/hat
|
||||
uint32_t buttons; ///< Buttons mask for currently pressed buttons
|
||||
} hid_gamepad16_report_t;
|
||||
@@ -0,0 +1,39 @@
|
||||
Bluetooth Audio (A2DP Source and Sink)
|
||||
======================================
|
||||
|
||||
The PicoW can be used as a Bluetooth Audio sink or source with the ``BluetoothAudio`` class.
|
||||
Operation is generally handled "automatically" in the background so while the audio is
|
||||
playing or streaming the main application can perform other operations (like displaying
|
||||
playback info, polling buttons for controls, etc.)
|
||||
|
||||
.. code :: cpp
|
||||
|
||||
#include <BluetoothAudio.h>
|
||||
...
|
||||
|
||||
**Note about CPU usage:** Bluetooth SBC audio is a compressed format. That means
|
||||
that it takes non-trivial amounts of CPU to compress on send, or decompress on receive.
|
||||
Transmitting precompressed audio from, say, MP3 or AAC, requires first decompressing
|
||||
the source file into raw PCM and then re-compressing them in the SBC format. You may
|
||||
want to consider overclocking in this case to avoid underflow.
|
||||
|
||||
A2DPSink
|
||||
--------
|
||||
|
||||
This class implements slave sink-mode operation with player control (play, pause, etc.) and
|
||||
can play the received and decoded SBC audio to ``PWMAudio``, ``I2S``, or a user-created
|
||||
`BluetoothAudioConsumer`` class.
|
||||
|
||||
The ``A2DPSink.ino`` example demonstrates turning a PicoW into a Bluetooth headset with
|
||||
``PWMAudio``.
|
||||
|
||||
A2DPSource
|
||||
-----------
|
||||
|
||||
This class implements a master source-mode SBC Bluetooth A2DP audio connection which
|
||||
transmits audio using the standard ``Stream`` interface (like ``I2S`` or ``PWMAudio``.
|
||||
The main application connects to a Bluetooth speaker and then writes samples into a buffer
|
||||
that's automatically transmitted behind the scenes.
|
||||
|
||||
The ``A2DPSource.ino`` example shows how to connect to a Bluetooth speaker, transmit
|
||||
data, and respond to commands from the speaker.
|
||||
+5
-3
@@ -2,9 +2,6 @@ Bluetooth on PicoW Support
|
||||
==========================
|
||||
|
||||
As of the Pico-SDK version 1.5.0, the PicoW has **BETA** Bluetooth support.
|
||||
So, since this core builds off the SDK the best that can be suggested it
|
||||
that we have **ALPHA** Bluetooth support. As such, bug reports are welcome,
|
||||
but Pull Requests fixing problems you find are seriously appreciated.
|
||||
|
||||
Enabling Bluetooth
|
||||
------------------
|
||||
@@ -24,6 +21,11 @@ Bluetooth Low Energy (BLE) HID device using the same API as their USB versions.
|
||||
The ``SerialBT`` library implements a very simple SPP (Serial Port Profile)
|
||||
Serial-compatible port.
|
||||
|
||||
Connect and use Bluetooth peripherals with the PicoW using the
|
||||
``BluetoothHIDMaster`` library.
|
||||
|
||||
``BluetoothAudio`` (A2DP) is also supported, both sink and source.
|
||||
|
||||
Writing Custom Bluetooth Applications
|
||||
-------------------------------------
|
||||
You may also write full applications using the ``BTStack`` standard callback
|
||||
|
||||
+2
-2
@@ -54,9 +54,9 @@ author = u'Earle F. Philhower, III'
|
||||
# built documents.
|
||||
#
|
||||
# The short X.Y version.
|
||||
version = u'3.9.2'
|
||||
version = u'3.9.4'
|
||||
# The full version, including alpha/beta/rc tags.
|
||||
release = u'3.9.2'
|
||||
release = u'3.9.4'
|
||||
|
||||
# The language for content autogenerated by Sphinx. Refer to documentation
|
||||
# for a list of supported languages.
|
||||
|
||||
+2
-2
@@ -598,7 +598,7 @@ Close the file. No other operations should be performed on *File* object
|
||||
after ``close`` function was called.
|
||||
|
||||
openNextFile (compatibility method, not recommended for new code)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. code:: cpp
|
||||
|
||||
@@ -610,7 +610,7 @@ Opens the next file in the directory pointed to by the File. Only valid
|
||||
when ``File.isDirectory() == true``.
|
||||
|
||||
rewindDirectory (compatibility method, not recommended for new code)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. code:: cpp
|
||||
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
Bluetooth HID Master
|
||||
====================
|
||||
|
||||
The PicoW can connect to a Bluetooth Classic or Bluetooth BLE keyboard,
|
||||
mouse, or joystick and receive input events from it. As opposed to
|
||||
the ``Keyboard``, ``Mouse``, and ``Joystick`` libraries, which make
|
||||
the PicoW into a peripheral others can use, this lets the PicoW use the
|
||||
same kinds of peripherals in a master rols.
|
||||
|
||||
BTDeviceInfo Class
|
||||
------------------
|
||||
|
||||
The ``BluetoothHCI`` class implements a scanning function for classic
|
||||
and BLE devices and can return a ``std::vector`` of discovered devices to an application.
|
||||
Iterate over the list using any of the STL iteration methods (i.e. ``for (auto e : list)``).
|
||||
The elements of this list are ``BTDeviceInfo`` objects which have the following
|
||||
member functions:
|
||||
|
||||
``BTDeviceInfo::deviceClass()`` returns the Bluetooth BLE UUID or the Blustooth device
|
||||
class for the device. This specifies the general class of the device (keyboard, mouse,
|
||||
etc.).
|
||||
|
||||
``BTDeviceInfo::address()`` and ``BTDeviceInfo::addressString()`` return the
|
||||
Bluetooth address as a binary array or a string that can be used to ``print``.
|
||||
|
||||
``BTDeviceInfo::addressType()`` returns whether the BLE address is random or not, and
|
||||
is not generally needed by a user application.
|
||||
|
||||
``BTDeviceInfo::rssi()`` returns an approximate dB level for the device. Less
|
||||
negative is stronger signal.
|
||||
|
||||
``BTDeviceInfo::name()`` returns the advertised name of the device, if present. Some
|
||||
devices or scans do not return names for valid devices.
|
||||
|
||||
|
||||
BluetoothHCI Class
|
||||
------------------
|
||||
|
||||
The ``BluetoothHCI`` class is responsible for scanning for devices and the lower-level
|
||||
housekeeping for a master-mode Bluetooth application. Most applications will not need
|
||||
to access it directly, but it can be used to discover nearby BT devices. As
|
||||
part of the application Bluetooth setup, call ``BluetoothHCI::install()`` and then
|
||||
``BluetoothHCI::begin()`` to start BT processing. Your application is still responsible
|
||||
for all the non-default HCI initialization and customization. See the ``BluetoothScanner.ino``
|
||||
example for more info.
|
||||
|
||||
|
||||
BluetoothHIDMaster Operation
|
||||
----------------------------
|
||||
|
||||
Most applications will use the ``BluetoothHIDMaster`` class and, after connecting, receive
|
||||
callbacks from the Bluetooth stack when input events (key presses, mouse moves, button
|
||||
mashes) occur.
|
||||
|
||||
Application flow will generally be:
|
||||
1. Install the appropriate callbacks using the ``BluetoothHIDMaster::onXXX`` methods
|
||||
2. Start the Bluetooth processing with ``BluetoothHIDMaster::begin()`` or ``BluetoothHIDMaster::beginBLE()``
|
||||
3. Connect to the first device found with ``BluetoothHIDMaster::connectXXX()`` and start receiving callbacks.
|
||||
4. Callbacks will come at interrupt time and set global state variables, which the main ``loop()`` will process
|
||||
|
||||
Callback Event Handlers
|
||||
-----------------------
|
||||
The main application is informed of new inputs via a standard callback mechanism. These callbacks run at
|
||||
interrupt time and should not do significant work, ``delay()``, or allocate or free memory. The most common
|
||||
way of handling this is setting global ``volatile`` flags and variables that the main ``loop()`` will poll
|
||||
and process.
|
||||
|
||||
Mouse Callbacks
|
||||
~~~~~~~~~~~~~~~
|
||||
The ``BluetoothHIDMaster::onMouseMove`` callback gets the delta X, Y, and wheel reported by the device.
|
||||
The ``BluetoothHIDMaster::onMouseButton`` gets a button number and state (up or down) and will be called
|
||||
each time an individual button is reported changed by the mouse.
|
||||
|
||||
.. code :: cpp
|
||||
|
||||
void mouseMoveCB(void *cbdata, int dx, int dy, int dw) {
|
||||
// Process the deltas, adjust global mouse state
|
||||
}
|
||||
|
||||
void mouseButtonCB(void *cbdata, int butt, bool down) {
|
||||
// Set the global button array with this new info
|
||||
}
|
||||
|
||||
|
||||
Meyboard Callbacks
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
The `BluetoothHIDMaster::onKeyDown`` callback receives the raw HID key (**NOT ASCII**) sent by the device on a key press
|
||||
while `BluetoothHIDMaster::onKeyUp`` gets the same when a key is released. Note that up to 6 keys can be pressed at any
|
||||
one time. For media keys ("consumer keys" in the USB HID documentation) the ``BluetoothHIDMaster::onConsumerKeyDown`` and
|
||||
``BluetoothHIDMaster::onConsumerKeyUp`` perform the same function (and receive the consumer key IDs defined by the
|
||||
USB HID spec and not ASCII).
|
||||
|
||||
To convert the key press and release (including SHIFT handling), use a ``HIDKeyStream`` object. Simply write the raw
|
||||
HID key and the up/down state to the stream and read back the ASCII for use in an application.
|
||||
|
||||
.. code :: cpp
|
||||
|
||||
HIDKeyStream keystream;
|
||||
|
||||
void keyDownCB(void *cbdata, int key) {
|
||||
keystream.write((uint8_t )key);
|
||||
keystream.write((uint8_t) true); // Keystream now has 1 ASCII character to read out and use
|
||||
char ascii = keystream.read();
|
||||
// ....
|
||||
}
|
||||
|
||||
void keyUpCB(void *cbdata, int key) {
|
||||
// Normally don't do anything on this, the character was read in the keyDownCB
|
||||
}
|
||||
|
||||
void consumerKeyDownCB(void *cbdata, int key) {
|
||||
// switch(key) and use cases from the USB Consumer Key page
|
||||
}
|
||||
|
||||
void consumerKeyUpCB(void *cbdata, int key) {
|
||||
// switch(key) and use cases from the USB Consumer Key page
|
||||
}
|
||||
|
||||
|
||||
Joystick Callbacks
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
A single ``BluetoothHIDMaster::onJoystick`` callback gets activated every time a report from a joystick is processed.
|
||||
It receives (potentially, if supported by the device) 4 analog axes, one 8-way digital hat switch position, and up
|
||||
to 32 button states at a time.
|
||||
|
||||
.. code :: cpp
|
||||
|
||||
void joystickCB(void *cbdata, int x, int y, int z, int rz, uint8_t hat, uint32_t buttons) {
|
||||
// HAT 0 = UP and continues clockwise. If no hat direction it is set to 0x0f.
|
||||
// Use "buttons & (1 << buttonNumber)" to look at the individual button states
|
||||
// ...
|
||||
}
|
||||
|
||||
PianoKeyboard Example
|
||||
~~~~~~~~~~~~~~~~~~~~~
|
||||
See the ``PianoKeyboard.ino`` and ``PianoKeyboardBLE.ino`` examples for more information on callback operation.
|
||||
|
||||
|
||||
BluetoothHIDMaster::onXXX Callback Installers
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. code :: cpp
|
||||
|
||||
void BluetoothHIDMaster::onMouseMove(void (*)(void *, int, int, int), void *cbData = nullptr);
|
||||
void BluetoothHIDMaster::onMouseButton(void (*)(void *, int, bool), void *cbData = nullptr);
|
||||
void BluetoothHIDMaster::onKeyDown(void (*)(void *, int), void *cbData = nullptr);
|
||||
void BluetoothHIDMaster::onKeyUp(void (*)(void *, int), void *cbData = nullptr);
|
||||
void BluetoothHIDMaster::onConsumerKeyDown(void (*)(void *, int), void *cbData = nullptr);
|
||||
void BluetoothHIDMaster::onConsumerKeyUp(void (*)(void *, int), void *cbData = nullptr);
|
||||
void BluetoothHIDMaster::onJoystick(void (*)(void *, int, int, int, int, uint8_t, uint32_t), void *cbData = nullptr);
|
||||
|
||||
BluetoothHIDMaster Class
|
||||
------------------------
|
||||
|
||||
bool BluetoothHIDMaster::begin()
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Installs and configures the Bluetooth Classic stack and starts processing events. No connections are made at this point.
|
||||
When running in Classic mode, no BLE devices can be detected or used.
|
||||
|
||||
|
||||
bool BluetoothHIDMaster::begin(const char *BLEName)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Installs and configures the Bluetooth BLE stack and starts processing events. No connections are made at this point.
|
||||
When running in BLE mode, no Classic devices can be detected or used.
|
||||
|
||||
bool BluetoothHIDMaster::connected()
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Returns if the Bluetooth stack is up and running and a connection to a device is currently active.
|
||||
|
||||
void BluetoothHIDMaster::end()
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Disables the Bluetooth stack. Note that with the current Bluetooth implementation restarting the stack (i.e. calling ``begin()`` after ``end()``) is not stable and will not work. Consider storing state and rebooting completely if this is necessary.
|
||||
|
||||
bool BluetoothHIDMaster::running()
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Returns if the Bluetooth stack is running at all. Does not indicate if there is an active connection or not.
|
||||
|
||||
bool BluetoothHIDMaster::hciRunning()
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Returns if the Bluetooth stack has passed the initial HCI start up phase. Until this returns ``true`` no Bluetooth operations can be performed.
|
||||
|
||||
std::vector<BTDeviceInfo> BluetoothHIDMaster::scan(uint32_t mask, int scanTimeSec, bool async)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Passes through the ``BluetoothHCI::scan()`` function to manually scan for a list of nearby devices. If you want to connect to the first found device, this is not needed.
|
||||
|
||||
bool BluetoothHIDMaster::connect(const uint8_t *addr)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Start the connection process to the Bluetooth Classic device with the given MAC. Note that this returns immediately, but it may take several seconds until ``connected()`` reports that the connection has been established.
|
||||
|
||||
bool BluetoothHIDMaster::connectKeyboard(), connectMouse(), connectJoystick(), connectAny()
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Connect to the first found specified Bluetooth Classic device type (or any HID device) in pairing mode. No need to call ``scan()`` or have an address.
|
||||
|
||||
bool BluetoothHIDMaster::connectBLE(const uint8_t *addr, int addrType)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Start the connection process to the Bluetooth BLE device with the given MAC. Note that this returns immediately, but it may take several seconds until ``connected()`` reports that the connection has been established.
|
||||
|
||||
bool BluetoothHIDMaster::connectBLE()
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Connect to the first found BLE device that has a HID service UUID (keyboard, mouse, or joystick)
|
||||
|
||||
bool BluetoothHIDMaster::disconnect()
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Shuts down the connection to the currently connected device.
|
||||
|
||||
void BluetoothHIDMaster::clearPairing()
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Erases all Bluetooth keys from memory. This effectively "forgets" all pairing between devices and can help avoid issues with the beta Bluetooth stack in the SDK.
|
||||
+4
-1
@@ -44,9 +44,12 @@ For the latest version, always check https://github.com/earlephilhower/arduino-p
|
||||
USB (Arduino and Adafruit_TinyUSB) <usb>
|
||||
Multicore Processing <multicore>
|
||||
|
||||
Bluetooth (Alpha/Beta) <bluetooth>
|
||||
Bluetooth <bluetooth>
|
||||
Bluetooth HID Master <hidmaster>
|
||||
Bluetooth Audio (A2DP) <a2dp>
|
||||
|
||||
Single File USB Drive <singlefile>
|
||||
FatFSUSB - full FS access over USB <fatfsusb>
|
||||
|
||||
FreeRTOS SMP (multicore) <freertos>
|
||||
|
||||
|
||||
+5
-8
@@ -38,10 +38,14 @@ extern unsigned long __lwip_rand(void);
|
||||
#define TCP_MSS 1460
|
||||
#define TCP_SND_BUF (8 * TCP_MSS)
|
||||
#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS))
|
||||
#define TCP_LISTEN_BACKLOG 1
|
||||
#define TCP_DEFAULT_LISTEN_BACKLOG 2
|
||||
#define LWIP_NETIF_STATUS_CALLBACK 1
|
||||
#define LWIP_NETIF_LINK_CALLBACK 1
|
||||
#define LWIP_NETIF_HOSTNAME 1
|
||||
#define LWIP_NETCONN 0
|
||||
#define LWIP_STATS 0
|
||||
#define LWIP_STATS_DISPLAY 0
|
||||
#define MEM_STATS 0
|
||||
#define SYS_STATS 0
|
||||
#define MEMP_STATS 0
|
||||
@@ -72,14 +76,7 @@ extern void __setSystemTime(unsigned long long sec, unsigned long us);
|
||||
//#define SNTP_SERVER_ADDRESS "pool.ntp.org"
|
||||
#define SNTP_SERVER_DNS 1
|
||||
|
||||
#ifndef NDEBUG
|
||||
#define LWIP_DEBUG 1
|
||||
#define LWIP_STATS 1
|
||||
#define LWIP_STATS_DISPLAY 1
|
||||
#define MEMP_STATS 1
|
||||
#define MEM_STATS 1
|
||||
#endif
|
||||
|
||||
#define LWIP_DEBUG 0
|
||||
#define ETHARP_DEBUG LWIP_DBG_OFF
|
||||
#define NETIF_DEBUG LWIP_DBG_OFF
|
||||
#define PBUF_DEBUG LWIP_DBG_OFF
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -166,7 +166,7 @@
|
||||
|
||||
-Wl,--wrap=tcp_arg
|
||||
-Wl,--wrap=tcp_new
|
||||
-Wl,--wrap=tcp_listen
|
||||
-Wl,--wrap=tcp_bind
|
||||
-Wl,--wrap=tcp_listen_with_backlog
|
||||
-Wl,--wrap=tcp_accept
|
||||
-Wl,--wrap=tcp_connect
|
||||
|
||||
Submodule libraries/Adafruit_TinyUSB_Arduino updated: d629476752...c953968c46
@@ -22,23 +22,22 @@
|
||||
#include <functional>
|
||||
#include <WiFiUdp.h>
|
||||
#include "ArduinoOTA.h"
|
||||
#include "MD5Builder.h"
|
||||
#include <MD5Builder.h>
|
||||
#include <PicoOTA.h>
|
||||
#include <StreamString.h>
|
||||
|
||||
#include "lwip/udp.h"
|
||||
#include "include/UdpContext.h"
|
||||
#include <lwip/udp.h>
|
||||
#include <include/UdpContext.h>
|
||||
|
||||
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_MDNS)
|
||||
#include <LEAmDNS.h>
|
||||
#endif
|
||||
|
||||
//#ifdef DEBUG_ESP_OTA
|
||||
//#ifdef DEBUG_ESP_PORT
|
||||
//#define OTA_DEBUG DEBUG_ESP_PORT
|
||||
//#endif
|
||||
//#endif
|
||||
#define OTA_DEBUG Serial
|
||||
#ifdef DEBUG_RP2040_CORE
|
||||
#ifdef DEBUG_RP2040_PORT
|
||||
#define OTA_DEBUG DEBUG_RP2040_PORT
|
||||
#endif
|
||||
#endif
|
||||
|
||||
ArduinoOTAClass::ArduinoOTAClass() {
|
||||
}
|
||||
|
||||
Submodule
+1
Submodule libraries/AsyncUDP added at 9a718fb3dc
@@ -526,6 +526,10 @@ const gatt_client_characteristic_t * BLECharacteristic::getCharacteristic(void)
|
||||
return &characteristic;
|
||||
}
|
||||
|
||||
gatt_client_notification_t *BLECharacteristic::getNotifier() {
|
||||
return ¬ify;
|
||||
}
|
||||
|
||||
|
||||
BLEService::BLEService(void) {
|
||||
}
|
||||
@@ -703,6 +707,7 @@ int BTstackManager::writeCharacteristicWithoutResponse(BLEDevice * device, BLEC
|
||||
}
|
||||
int BTstackManager::subscribeForNotifications(BLEDevice * device, BLECharacteristic * characteristic) {
|
||||
gattAction = gattActionSubscribe;
|
||||
gatt_client_listen_for_characteristic_value_updates(characteristic->getNotifier(), gatt_client_callback, device->getHandle(), (gatt_client_characteristic_t*) characteristic->getCharacteristic());
|
||||
return gatt_client_write_client_characteristic_configuration(gatt_client_callback, device->getHandle(), (gatt_client_characteristic_t*) characteristic->getCharacteristic(),
|
||||
GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
|
||||
}
|
||||
@@ -713,6 +718,7 @@ int BTstackManager::subscribeForIndications(BLEDevice * device, BLECharacteristi
|
||||
}
|
||||
int BTstackManager::unsubscribeFromNotifications(BLEDevice * device, BLECharacteristic * characteristic) {
|
||||
gattAction = gattActionUnsubscribe;
|
||||
gatt_client_stop_listening_for_characteristic_value_updates(characteristic->getNotifier());
|
||||
return gatt_client_write_client_characteristic_configuration(gatt_client_callback, device->getHandle(), (gatt_client_characteristic_t*) characteristic->getCharacteristic(),
|
||||
GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NONE);
|
||||
}
|
||||
|
||||
@@ -84,6 +84,7 @@ extern "C" {
|
||||
|
||||
class BLECharacteristic {
|
||||
private:
|
||||
gatt_client_notification_t notify;
|
||||
gatt_client_characteristic_t characteristic;
|
||||
UUID uuid;
|
||||
public:
|
||||
@@ -93,6 +94,7 @@ extern "C" {
|
||||
bool matches(UUID * uuid);
|
||||
bool isValueHandle(uint16_t value_handle);
|
||||
const gatt_client_characteristic_t * getCharacteristic();
|
||||
gatt_client_notification_t *getNotifier();
|
||||
};
|
||||
|
||||
class BLEService {
|
||||
|
||||
@@ -104,7 +104,7 @@ public:
|
||||
|
||||
bool begin();
|
||||
|
||||
std::list<BTDeviceInfo> scan(uint32_t mask = BluetoothHCI::speaker_cod, int scanTimeSec = 5, bool async = false) {
|
||||
std::vector<BTDeviceInfo> scan(uint32_t mask = BluetoothHCI::speaker_cod, int scanTimeSec = 5, bool async = false) {
|
||||
return _hci.scan(mask, scanTimeSec, async);
|
||||
}
|
||||
|
||||
|
||||
@@ -75,10 +75,8 @@ void BluetoothAudioConsumerI2S::fill() {
|
||||
_a2dpSink->playback_handler((int16_t *) buff, 32);
|
||||
num_samples -= 64;
|
||||
for (int i = 0; i < 64; i++) {
|
||||
int32_t tmp = buff[i];
|
||||
tmp *= _gain;
|
||||
tmp >>= 8;
|
||||
_i2s->write((int16_t)tmp);
|
||||
buff[i] = (((int32_t)buff[i]) * _gain) >> 8;
|
||||
}
|
||||
_i2s->write((uint8_t *)buff, 64 * 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,10 +76,8 @@ void BluetoothAudioConsumerPWM::fill() {
|
||||
_a2dpSink->playback_handler((int16_t *) buff, 32);
|
||||
num_samples -= 64;
|
||||
for (int i = 0; i < 64; i++) {
|
||||
int32_t tmp = buff[i];
|
||||
tmp *= _gain;
|
||||
tmp >>= 8;
|
||||
_pwm->write((int16_t)tmp);
|
||||
buff[i] = (((int32_t)buff[i]) * _gain) >> 8;
|
||||
}
|
||||
_pwm->write((uint8_t *)buff, 64 * 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <BluetoothHCI.h>
|
||||
|
||||
BluetoothHCI hci;
|
||||
|
||||
void BTBasicSetup() {
|
||||
l2cap_init();
|
||||
gatt_client_init();
|
||||
sm_init();
|
||||
sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
|
||||
gap_set_default_link_policy_settings(LM_LINK_POLICY_ENABLE_SNIFF_MODE | LM_LINK_POLICY_ENABLE_ROLE_SWITCH);
|
||||
hci_set_master_slave_policy(HCI_ROLE_MASTER);
|
||||
hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR);
|
||||
|
||||
hci.setBLEName("Pico BLE Scanner");
|
||||
hci.install();
|
||||
hci.begin();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
delay(5000);
|
||||
BTBasicSetup();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.printf("BEGIN BLE SCAN @%lu ...", millis());
|
||||
auto l = hci.scanBLE(BluetoothHCI::any_cod);
|
||||
Serial.printf("END BLE SCAN @%lu\n\n", millis());
|
||||
Serial.printf("%-8s | %-17s | %-4s | %s\n", "Class", "Address", "RSSI", "Name");
|
||||
Serial.printf("%-8s | %-17s | %-4s | %s\n", "--------", "-----------------", "----", "----------------");
|
||||
for (auto e : l) {
|
||||
Serial.printf("%08lx | %17s | %4d | %s\n", e.deviceClass(), e.addressString(), e.rssi(), e.name());
|
||||
}
|
||||
Serial.printf("\n\n\n");
|
||||
}
|
||||
@@ -20,9 +20,13 @@ scan KEYWORD2
|
||||
scanAsyncDone KEYWORD2
|
||||
scanAsyncResult KEYWORD2
|
||||
|
||||
setName KEYWORD2
|
||||
scanBLE KEYWORD2
|
||||
|
||||
# BTDeviceInfo
|
||||
deviceClass KEYWORD2
|
||||
address KEYWORD2
|
||||
addressType KEYWORD2
|
||||
addressString KEYWORD2
|
||||
rssi KEYWORD2
|
||||
name KEYWORD2
|
||||
|
||||
@@ -24,43 +24,60 @@
|
||||
|
||||
class BTDeviceInfo {
|
||||
public:
|
||||
// Classic Bluetooth Device
|
||||
BTDeviceInfo(uint32_t dc, const uint8_t addr[6], int rssi, const char *name) {
|
||||
_deviceClass = dc;
|
||||
memcpy(_address, addr, sizeof(_address));
|
||||
_addressType = -1;
|
||||
sprintf(_addressString, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
|
||||
_rssi = rssi;
|
||||
_name = strdup(name);
|
||||
strncpy(_name, name, sizeof(_name));
|
||||
_name[sizeof(_name) - 1] = 0;
|
||||
}
|
||||
// Copy constructor to ensure we deep-copy the string
|
||||
BTDeviceInfo(const BTDeviceInfo &b) {
|
||||
_deviceClass = b._deviceClass;
|
||||
memcpy(_address, b._address, sizeof(_address));
|
||||
memcpy(_addressString, b._addressString, sizeof(_addressString));
|
||||
_rssi = b._rssi;
|
||||
_name = strdup(b._name);
|
||||
|
||||
// Bluetooth BLE Device
|
||||
BTDeviceInfo(uint32_t dc, const uint8_t addr[6], int addressType, int rssi, const char *name, size_t nameLen) {
|
||||
_deviceClass = dc;
|
||||
memcpy(_address, addr, sizeof(_address));
|
||||
sprintf(_addressString, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
|
||||
_addressType = addressType;
|
||||
_rssi = rssi;
|
||||
memcpy(_name, name, std::min(nameLen, sizeof(_name)));
|
||||
_name[std::min(nameLen, sizeof(_name) - 1)] = 0;
|
||||
}
|
||||
|
||||
~BTDeviceInfo() {
|
||||
free(_name);
|
||||
}
|
||||
|
||||
uint32_t deviceClass() {
|
||||
return _deviceClass;
|
||||
}
|
||||
|
||||
const uint8_t *address() {
|
||||
return _address;
|
||||
}
|
||||
|
||||
const char *addressString() {
|
||||
return _addressString;
|
||||
}
|
||||
|
||||
int rssi() {
|
||||
return _rssi;
|
||||
}
|
||||
|
||||
const char *name() {
|
||||
return _name;
|
||||
}
|
||||
|
||||
int addressType() {
|
||||
return _addressType;
|
||||
}
|
||||
|
||||
private:
|
||||
uint32_t _deviceClass;
|
||||
uint8_t _address[6];
|
||||
int _addressType;
|
||||
char _addressString[18];
|
||||
int8_t _rssi;
|
||||
char *_name;
|
||||
char _name[241];
|
||||
};
|
||||
|
||||
@@ -34,13 +34,58 @@
|
||||
(_BTHCICB<void(uint8_t, uint16_t, uint8_t*, uint16_t), __COUNTER__>::func = std::bind(&class::cbFcn, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4), \
|
||||
static_cast<btstack_packet_handler_t>(_BTHCICB<void(uint8_t, uint16_t, uint8_t*, uint16_t), __COUNTER__ - 1>::callback))
|
||||
|
||||
void BluetoothHCI::setBLEName(const char *name) {
|
||||
if (_att) {
|
||||
free(_att);
|
||||
_att = nullptr;
|
||||
}
|
||||
_att = (uint8_t *)malloc(1 + 0x0a + 0x0d + 0x08 + strlen(name) + 0x02);
|
||||
uint8_t *ptr = _att;
|
||||
const uint8_t head[] = {
|
||||
// ATT DB Version
|
||||
1,
|
||||
// 0x0001 PRIMARY_SERVICE-GAP_SERVICE
|
||||
0x0a, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x28, 0x00, 0x18,
|
||||
// 0x0002 CHARACTERISTIC-GAP_DEVICE_NAME - READ
|
||||
0x0d, 0x00, 0x02, 0x00, 0x02, 0x00, 0x03, 0x28, 0x02, 0x03, 0x00, 0x00, 0x2a
|
||||
};
|
||||
memcpy(ptr, head, sizeof(head));
|
||||
ptr += sizeof(head);
|
||||
*ptr++ = 8 + strlen(name); // len of item
|
||||
*ptr++ = 0x00;
|
||||
*ptr++ = 0x02;
|
||||
*ptr++ = 0x00;
|
||||
*ptr++ = 0x03;
|
||||
*ptr++ = 0x00;
|
||||
*ptr++ = 0x00;
|
||||
*ptr++ = 0x2a;
|
||||
memcpy(ptr, name, strlen(name));
|
||||
ptr += strlen(name);
|
||||
// End it
|
||||
*ptr++ = 0x00;
|
||||
*ptr++ = 0x00;
|
||||
|
||||
DEBUGV("ATTDB: ");
|
||||
for (size_t i = 0; i < 1 + 0x0a + 0x0d + 0x08 + strlen(name) + 0x02; i++) {
|
||||
DEBUGV("%02X ", _att[i]);
|
||||
}
|
||||
DEBUGV("\n");
|
||||
}
|
||||
|
||||
void BluetoothHCI::install() {
|
||||
hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR);
|
||||
|
||||
// Register for HCI events.
|
||||
hci_event_callback_registration.callback = PACKETHANDLERCB(BluetoothHCI, hci_packet_handler);
|
||||
hci_add_event_handler(&hci_event_callback_registration);
|
||||
|
||||
// BLE set our visible name
|
||||
if (_att) {
|
||||
att_server_init(_att, nullptr, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BluetoothHCI::begin() {
|
||||
_running = true;
|
||||
hci_power_control(HCI_POWER_ON);
|
||||
@@ -50,14 +95,17 @@ void BluetoothHCI::uninstall() {
|
||||
BluetoothLock b;
|
||||
hci_remove_event_handler(&hci_event_callback_registration);
|
||||
_running = false;
|
||||
free(_att);
|
||||
_att = nullptr;
|
||||
}
|
||||
|
||||
bool BluetoothHCI::running() {
|
||||
return _hciRunning;
|
||||
}
|
||||
|
||||
std::list<BTDeviceInfo> BluetoothHCI::scan(uint32_t mask, int scanTimeSec, bool async) {
|
||||
std::vector<BTDeviceInfo> BluetoothHCI::scan(uint32_t mask, int scanTimeSec, bool async) {
|
||||
_scanMask = mask;
|
||||
_btdList.reserve(MAX_DEVICES_TO_DISCOVER);
|
||||
_btdList.clear();
|
||||
if (!_running) {
|
||||
return _btdList;
|
||||
@@ -88,13 +136,215 @@ std::list<BTDeviceInfo> BluetoothHCI::scan(uint32_t mask, int scanTimeSec, bool
|
||||
return _btdList;
|
||||
}
|
||||
|
||||
bool BluetoothHCI::scanAsyncDone() {
|
||||
return _scanning;
|
||||
}
|
||||
std::list<BTDeviceInfo> BluetoothHCI::scanAsyncResult() {
|
||||
std::vector<BTDeviceInfo> BluetoothHCI::scanBLE(uint32_t uuid, int scanTimeSec) {
|
||||
_scanMask = uuid;
|
||||
_btdList.reserve(MAX_DEVICES_TO_DISCOVER);
|
||||
_btdList.clear();
|
||||
if (!_running) {
|
||||
return _btdList;
|
||||
}
|
||||
_scanning = true;
|
||||
while (!_hciRunning) {
|
||||
DEBUGV("HCI::scanBLE(): Waiting for HCI to come up\n");
|
||||
delay(10);
|
||||
}
|
||||
uint32_t inquiryTime = scanTimeSec * 1000;
|
||||
if (!inquiryTime) {
|
||||
inquiryTime = 1000;
|
||||
}
|
||||
DEBUGV("HCI::scan(): BLE advertise inquiry start\n");
|
||||
// Only need to lock around the inquiry start command, not the wait
|
||||
{
|
||||
BluetoothLock b;
|
||||
gap_set_scan_params(0, 75, 50, 0); // TODO - anything better for these params?
|
||||
gap_start_scan();
|
||||
}
|
||||
uint32_t scanStart = millis();
|
||||
|
||||
while (_scanning && ((millis() - scanStart) < inquiryTime)) {
|
||||
delay(10);
|
||||
}
|
||||
DEBUGV("HCI::scanBLE(): inquiry end\n");
|
||||
gap_stop_scan();
|
||||
|
||||
return _btdList;
|
||||
}
|
||||
|
||||
void BluetoothHCI::scanFree() {
|
||||
_btdList.clear();
|
||||
_btdList.shrink_to_fit();
|
||||
}
|
||||
|
||||
void BluetoothHCI::parse_advertisement_data(uint8_t *packet) {
|
||||
bd_addr_t address;
|
||||
gap_event_advertising_report_get_address(packet, address);
|
||||
//int event_type = gap_event_advertising_report_get_advertising_event_type(packet);
|
||||
int address_type = gap_event_advertising_report_get_address_type(packet);
|
||||
int8_t rssi = gap_event_advertising_report_get_rssi(packet);
|
||||
uint8_t adv_size = gap_event_advertising_report_get_data_length(packet);
|
||||
const uint8_t * adv_data = gap_event_advertising_report_get_data(packet);
|
||||
uint16_t uuid = 0;
|
||||
const char *nameptr = nullptr;
|
||||
int namelen = 0;
|
||||
|
||||
ad_context_t context;
|
||||
uint8_t uuid_128[16];
|
||||
for (ad_iterator_init(&context, adv_size, (uint8_t *)adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) {
|
||||
uint8_t data_type = ad_iterator_get_data_type(&context);
|
||||
uint8_t size = ad_iterator_get_data_len(&context);
|
||||
const uint8_t * data = ad_iterator_get_data(&context);
|
||||
|
||||
// if (data_type > 0 && data_type < 0x1B){
|
||||
// printf(" %s: ", ad_types[data_type]);
|
||||
// }
|
||||
int i;
|
||||
// Assigned Numbers GAP
|
||||
|
||||
switch (data_type) {
|
||||
case BLUETOOTH_DATA_TYPE_FLAGS:
|
||||
// show only first octet, ignore rest
|
||||
//for (i=0; i<8;i++){
|
||||
// if (data[0] & (1<<i)){
|
||||
// printf("%s; ", flags[i]);
|
||||
// }
|
||||
|
||||
//}
|
||||
break;
|
||||
case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS:
|
||||
case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS:
|
||||
case BLUETOOTH_DATA_TYPE_LIST_OF_16_BIT_SERVICE_SOLICITATION_UUIDS:
|
||||
for (i = 0; i < size; i += 2) {
|
||||
uint16_t thisuuid = little_endian_read_16(data, i);
|
||||
if (!_scanMask || (_scanMask == thisuuid)) {
|
||||
uuid = thisuuid;
|
||||
}
|
||||
DEBUGV("uuid %02X ", thisuuid);
|
||||
}
|
||||
break;
|
||||
case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_32_BIT_SERVICE_CLASS_UUIDS:
|
||||
case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_32_BIT_SERVICE_CLASS_UUIDS:
|
||||
case BLUETOOTH_DATA_TYPE_LIST_OF_32_BIT_SERVICE_SOLICITATION_UUIDS:
|
||||
for (i = 0; i < size; i += 4) {
|
||||
uint32_t thisuuid = little_endian_read_32(data, i);
|
||||
if (!_scanMask || (_scanMask == thisuuid)) {
|
||||
uuid = thisuuid;
|
||||
}
|
||||
DEBUGV("%04" PRIX32, thisuuid);
|
||||
}
|
||||
break;
|
||||
case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS:
|
||||
case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS:
|
||||
case BLUETOOTH_DATA_TYPE_LIST_OF_128_BIT_SERVICE_SOLICITATION_UUIDS:
|
||||
// Unhandled yet
|
||||
reverse_128(data, uuid_128);
|
||||
DEBUGV("%s", uuid128_to_str(uuid_128));
|
||||
break;
|
||||
case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME:
|
||||
if (!nameptr) {
|
||||
nameptr = (const char *)data;
|
||||
namelen = size;
|
||||
}
|
||||
for (i = 0; i < size; i++) {
|
||||
DEBUGV("%c", (char)(data[i]));
|
||||
}
|
||||
break;
|
||||
|
||||
case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME:
|
||||
nameptr = (const char *)data;
|
||||
namelen = size;
|
||||
for (i = 0; i < size; i++) {
|
||||
DEBUGV("%c", (char)(data[i]));
|
||||
}
|
||||
break;
|
||||
case BLUETOOTH_DATA_TYPE_TX_POWER_LEVEL:
|
||||
DEBUGV("%d dBm", *(int8_t*)data);
|
||||
break;
|
||||
case BLUETOOTH_DATA_TYPE_SLAVE_CONNECTION_INTERVAL_RANGE:
|
||||
DEBUGV("Connection Interval Min = %u ms, Max = %u ms", little_endian_read_16(data, 0) * 5 / 4, little_endian_read_16(data, 2) * 5 / 4);
|
||||
break;
|
||||
case BLUETOOTH_DATA_TYPE_SERVICE_DATA:
|
||||
//printf_hexdump(data, size);
|
||||
break;
|
||||
case BLUETOOTH_DATA_TYPE_PUBLIC_TARGET_ADDRESS:
|
||||
case BLUETOOTH_DATA_TYPE_RANDOM_TARGET_ADDRESS:
|
||||
reverse_bd_addr(data, address);
|
||||
DEBUGV("%s", bd_addr_to_str(address));
|
||||
break;
|
||||
case BLUETOOTH_DATA_TYPE_APPEARANCE:
|
||||
// https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.gap.appearance.xml
|
||||
DEBUGV("%02X", little_endian_read_16(data, 0));
|
||||
break;
|
||||
case BLUETOOTH_DATA_TYPE_ADVERTISING_INTERVAL:
|
||||
DEBUGV("%u ms", little_endian_read_16(data, 0) * 5 / 8);
|
||||
break;
|
||||
case BLUETOOTH_DATA_TYPE_3D_INFORMATION_DATA:
|
||||
//printf_hexdump(data, size);
|
||||
break;
|
||||
case BLUETOOTH_DATA_TYPE_MANUFACTURER_SPECIFIC_DATA: // Manufacturer Specific Data
|
||||
break;
|
||||
case BLUETOOTH_DATA_TYPE_CLASS_OF_DEVICE:
|
||||
case BLUETOOTH_DATA_TYPE_SIMPLE_PAIRING_HASH_C:
|
||||
case BLUETOOTH_DATA_TYPE_SIMPLE_PAIRING_RANDOMIZER_R:
|
||||
case BLUETOOTH_DATA_TYPE_DEVICE_ID:
|
||||
case BLUETOOTH_DATA_TYPE_SECURITY_MANAGER_OUT_OF_BAND_FLAGS:
|
||||
default:
|
||||
DEBUGV("Advertising Data Type 0x%2x not handled yet", data_type);
|
||||
break;
|
||||
}
|
||||
DEBUGV("\n");
|
||||
}
|
||||
DEBUGV("\n");
|
||||
|
||||
if (uuid) {
|
||||
bool seen = false;
|
||||
for (auto itr = _btdList.begin(); (itr != _btdList.end()) && !seen; itr++) {
|
||||
if (!memcmp(itr->address(), address, sizeof(address))) {
|
||||
seen = true;
|
||||
}
|
||||
}
|
||||
if (!seen) {
|
||||
BTDeviceInfo btd(uuid, address, address_type, rssi, nameptr ? nameptr : "", nameptr ? namelen : 0);
|
||||
if (_btdList.size() < MAX_DEVICES_TO_DISCOVER) {
|
||||
_btdList.push_back(btd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
|
||||
UNUSED(packet_type);
|
||||
UNUSED(channel);
|
||||
UNUSED(size);
|
||||
|
||||
gatt_client_service_t service;
|
||||
gatt_client_characteristic_t characteristic;
|
||||
switch (hci_event_packet_get_type(packet)) {
|
||||
case GATT_EVENT_SERVICE_QUERY_RESULT:
|
||||
gatt_event_service_query_result_get_service(packet, &service);
|
||||
// dump_service(&service);
|
||||
// services[service_count++] = service;
|
||||
break;
|
||||
case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
|
||||
gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
|
||||
// dump_characteristic(&characteristic);
|
||||
break;
|
||||
case GATT_EVENT_QUERY_COMPLETE:
|
||||
// GATT_EVENT_QUERY_COMPLETE of search characteristics
|
||||
// if (service_index < service_count) {
|
||||
// service = services[service_index++];
|
||||
// printf("\nGATT browser - CHARACTERISTIC for SERVICE %s, [0x%04x-0x%04x]\n",
|
||||
// uuid128_to_str(service.uuid128), service.start_group_handle, service.end_group_handle);
|
||||
// gatt_client_discover_characteristics_for_service(handle_gatt_client_event, connection_handle, &service);
|
||||
// break;
|
||||
// }
|
||||
// service_index = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BluetoothHCI::hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
|
||||
(void)channel;
|
||||
@@ -152,12 +402,41 @@ void BluetoothHCI::hci_packet_handler(uint8_t packet_type, uint16_t channel, uin
|
||||
}
|
||||
}
|
||||
BTDeviceInfo btd(cod, address, rssi, name);
|
||||
_btdList.push_back(btd);
|
||||
if (_btdList.size() < MAX_DEVICES_TO_DISCOVER) {
|
||||
_btdList.push_back(btd);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case GAP_EVENT_INQUIRY_COMPLETE:
|
||||
_scanning = false;
|
||||
break;
|
||||
|
||||
case GAP_EVENT_ADVERTISING_REPORT:
|
||||
if (_scanning) {
|
||||
parse_advertisement_data(packet);
|
||||
}
|
||||
break;
|
||||
|
||||
case HCI_EVENT_DISCONNECTION_COMPLETE:
|
||||
_hciConn = HCI_CON_HANDLE_INVALID;
|
||||
DEBUGV("HCI Disconnected\n");
|
||||
break;
|
||||
|
||||
case HCI_EVENT_LE_META:
|
||||
// wait for connection complete
|
||||
if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) {
|
||||
break;
|
||||
}
|
||||
DEBUGV("HCI Connected\n");
|
||||
_hciConn = hci_subevent_le_connection_complete_get_connection_handle(packet);
|
||||
if (_smPair) {
|
||||
sm_request_pairing(_hciConn);
|
||||
} else {
|
||||
// query primary services - not used yet
|
||||
gatt_client_discover_primary_services(handle_gatt_client_event, _hciConn);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -32,22 +32,47 @@
|
||||
class BluetoothHCI {
|
||||
public:
|
||||
void install();
|
||||
void setBLEName(const char *bleMasterName);
|
||||
void begin();
|
||||
void uninstall();
|
||||
bool running();
|
||||
|
||||
static const uint32_t speaker_cod = 0x200000 | 0x040000 | 0x000400; // Service Class: Rendering | Audio, Major Device Class: Audio
|
||||
static const uint32_t any_cod = 0;
|
||||
std::list<BTDeviceInfo> scan(uint32_t mask, int scanTimeSec = 5, bool async = false);
|
||||
std::vector<BTDeviceInfo> scan(uint32_t mask, int scanTimeSec = 5, bool async = false);
|
||||
bool scanAsyncDone();
|
||||
std::list<BTDeviceInfo> scanAsyncResult();
|
||||
std::vector<BTDeviceInfo> scanAsyncResult();
|
||||
|
||||
std::vector<BTDeviceInfo> scanBLE(uint32_t uuid, int scanTimeSec = 5);
|
||||
|
||||
void scanFree(); // Free allocated scan buffers
|
||||
|
||||
friend class BluetoothHIDMaster;
|
||||
|
||||
protected:
|
||||
hci_con_handle_t getHCIConn() {
|
||||
return _hciConn;
|
||||
}
|
||||
void setPairOnMeta(bool v) {
|
||||
_smPair = v;
|
||||
}
|
||||
|
||||
private:
|
||||
void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
|
||||
btstack_packet_callback_registration_t hci_event_callback_registration;
|
||||
volatile bool _hciRunning = false;
|
||||
uint32_t _scanMask;
|
||||
std::list<BTDeviceInfo> _btdList;
|
||||
// Use a .reserve()'d vector to avoid any memory allocations in the
|
||||
// HCI callback, since the callback happens at IRQ time. Any more
|
||||
// elements than MAX_DEVICES_TO_DISCOVER will be thrown out
|
||||
enum { MAX_DEVICES_TO_DISCOVER = 32 };
|
||||
std::vector<BTDeviceInfo> _btdList;
|
||||
volatile bool _scanning = false;
|
||||
bool _running = false;
|
||||
|
||||
// BLE specific
|
||||
uint8_t *_att = nullptr;
|
||||
void parse_advertisement_data(uint8_t *packet);
|
||||
volatile hci_con_handle_t _hciConn = HCI_CON_HANDLE_INVALID;
|
||||
bool _smPair = false;
|
||||
};
|
||||
|
||||
@@ -158,6 +158,18 @@ void ckb(void *cbdata, int key) {
|
||||
}
|
||||
|
||||
|
||||
// Joystick can get reports of 4 analog axes, 1 d-pad bitfield, and up to 32 buttons
|
||||
// Axes and hats that aren't reported by the joystick are read as 0
|
||||
void joy(void *cbdata, int x, int y, int z, int rz, uint8_t hat, uint32_t buttons) {
|
||||
(void) cbdata;
|
||||
const char *hats[16] = { "U", "UR", "R", "DR", "D", "DL", "L", "UL", "", "", "", "", "", "", "", "." };
|
||||
Serial.printf("Joystick: (%4d, %4d) (%4d, %4d), Hat: %-2s, Buttons:", x, y, z, rz, hats[hat & 15]);
|
||||
for (int i = 0; i < 32; i++) {
|
||||
Serial.printf(" %c", (buttons & 1 << i) ? '*' : '.');
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin();
|
||||
delay(3000);
|
||||
@@ -190,9 +202,11 @@ void setup() {
|
||||
hid.onConsumerKeyDown(ckb, (void *)true);
|
||||
hid.onConsumerKeyUp(ckb, (void *)false);
|
||||
|
||||
hid.onJoystick(joy);
|
||||
|
||||
hid.begin();
|
||||
|
||||
hid.connectKeyboard();
|
||||
hid.connectAny();
|
||||
// or hid.connectMouse();
|
||||
}
|
||||
|
||||
@@ -204,6 +218,6 @@ void loop() {
|
||||
hid.disconnect();
|
||||
hid.clearPairing();
|
||||
Serial.printf("Restarting HID master, put your device in pairing mode now.\n");
|
||||
hid.connectKeyboard();
|
||||
hid.connectAny();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
// KeyboardPiano example - Released to the public domain in 2024 by Earle F. Philhower, III
|
||||
//
|
||||
// Demonstrates using the BluetoothHIDMaster class to use a Bluetooth keyboard as a
|
||||
// piano keyboard on the PicoW
|
||||
//
|
||||
// Hook up a phono plug to GP0 and GP1 (and GND of course...the 1st 3 pins on the PCB)
|
||||
// Connect wired earbuds up and connect over BT from your keyboard and play some music.
|
||||
|
||||
|
||||
#include <BluetoothHIDMaster.h>
|
||||
#include <PWMAudio.h>
|
||||
|
||||
// We need the inverse map, borrow from the Keyboard library
|
||||
#include <HID_Keyboard.h>
|
||||
extern const uint8_t KeyboardLayout_en_US[128];
|
||||
|
||||
BluetoothHIDMaster hid;
|
||||
PWMAudio pwm;
|
||||
HIDKeyStream keystream;
|
||||
|
||||
|
||||
int16_t sine[1000]; // One complete precalculated sine wave for oscillator use
|
||||
void precalculateSine() {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
sine[i] = (int16_t)(2000.0 * sin(i * 2 * 3.14159 / 1000.0)); // Only make amplitude ~1/16 max so we can sum up w/o clipping
|
||||
}
|
||||
}
|
||||
|
||||
// Simple variable frequency resampling oscillator state
|
||||
typedef struct {
|
||||
uint32_t key; // Identifier of which key started this tone
|
||||
uint32_t pos; // Current sine offset
|
||||
uint32_t step; // Delta in fixed point 16p16 format
|
||||
} Oscillator;
|
||||
Oscillator osc[6]; // Look, ma! 6-note polyphony!
|
||||
|
||||
// Quiet down, now!
|
||||
void silenceOscillators() {
|
||||
noInterrupts();
|
||||
for (int i = 0; i < 6; i++) {
|
||||
osc[i].pos = 0;
|
||||
osc[i].step = 0;
|
||||
}
|
||||
interrupts();
|
||||
}
|
||||
|
||||
// PWM callback, generates sum of online oscillators
|
||||
void fill() {
|
||||
int num_samples = pwm.availableForWrite() / 2;
|
||||
int16_t buff[32 * 2];
|
||||
|
||||
while (num_samples > 63) {
|
||||
// Run in 32 LR sample chunks for speed, less loop overhead
|
||||
for (int o = 0; o < 32; o++) {
|
||||
int32_t sum = 0;
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (osc[i].step) {
|
||||
sum += sine[osc[i].pos >> 16];
|
||||
osc[i].pos += osc[i].step;
|
||||
while (osc[i].pos >= 1000 << 16) {
|
||||
osc[i].pos -= 1000 << 16;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sum > 32767) {
|
||||
sum = 32767;
|
||||
} else if (sum < -32767) {
|
||||
sum = -32767;
|
||||
}
|
||||
buff[o * 2] = (int16_t) sum;
|
||||
buff[o * 2 + 1] = (int16_t) sum;
|
||||
}
|
||||
pwm.write((const uint8_t *)buff, sizeof(buff));
|
||||
num_samples -= 64;
|
||||
}
|
||||
}
|
||||
|
||||
// Mouse callbacks. Could keep track of global mouse position, update a cursor, etc.
|
||||
void mm(void *cbdata, int dx, int dy, int dw) {
|
||||
(void) cbdata;
|
||||
Serial.printf("Mouse: X:%d Y:%d Wheel:%d\n", dx, dy, dw);
|
||||
}
|
||||
|
||||
// Buttons are sent separately from movement
|
||||
void mb(void *cbdata, int butt, bool down) {
|
||||
(void) cbdata;
|
||||
Serial.printf("Mouse: Button %d %s\n", butt, down ? "DOWN" : "UP");
|
||||
}
|
||||
|
||||
// Convert a hertz floating point into a step fixed point 16p16
|
||||
inline uint32_t stepForHz(float hz) {
|
||||
const float stepHz = 1000.0 / 44100.0;
|
||||
const float step = hz * stepHz;
|
||||
return (uint32_t)(step * 65536.0);
|
||||
}
|
||||
|
||||
uint32_t keyStepMap[128]; // The frequency of any raw HID key
|
||||
void setupKeyStepMap() {
|
||||
for (int i = 0; i < 128; i++) {
|
||||
keyStepMap[i] = 0;
|
||||
}
|
||||
// Implements the "standard" PC keyboard to piano setup
|
||||
// https://ux.stackexchange.com/questions/46669/mapping-piano-keys-to-computer-keyboard
|
||||
keyStepMap[KeyboardLayout_en_US['a']] = stepForHz(261.6256);
|
||||
keyStepMap[KeyboardLayout_en_US['w']] = stepForHz(277.1826);
|
||||
keyStepMap[KeyboardLayout_en_US['s']] = stepForHz(293.6648);
|
||||
keyStepMap[KeyboardLayout_en_US['e']] = stepForHz(311.1270);
|
||||
keyStepMap[KeyboardLayout_en_US['d']] = stepForHz(329.6276);
|
||||
keyStepMap[KeyboardLayout_en_US['f']] = stepForHz(349.2282);
|
||||
keyStepMap[KeyboardLayout_en_US['t']] = stepForHz(369.9944);
|
||||
keyStepMap[KeyboardLayout_en_US['g']] = stepForHz(391.9954);
|
||||
keyStepMap[KeyboardLayout_en_US['y']] = stepForHz(415.3047);
|
||||
keyStepMap[KeyboardLayout_en_US['h']] = stepForHz(440.0000);
|
||||
keyStepMap[KeyboardLayout_en_US['u']] = stepForHz(466.1638);
|
||||
keyStepMap[KeyboardLayout_en_US['j']] = stepForHz(493.8833);
|
||||
keyStepMap[KeyboardLayout_en_US['k']] = stepForHz(523.2511);
|
||||
keyStepMap[KeyboardLayout_en_US['o']] = stepForHz(554.3653);
|
||||
keyStepMap[KeyboardLayout_en_US['l']] = stepForHz(587.3295);
|
||||
keyStepMap[KeyboardLayout_en_US['p']] = stepForHz(622.2540);
|
||||
keyStepMap[KeyboardLayout_en_US[';']] = stepForHz(659.2551);
|
||||
keyStepMap[KeyboardLayout_en_US['\'']] = stepForHz(698.4565);
|
||||
}
|
||||
|
||||
// We get make/break for every key which lets us hold notes while a key is depressed
|
||||
void kb(void *cbdata, int key) {
|
||||
bool state = (bool)cbdata;
|
||||
if (state && key < 128) {
|
||||
// Starting a new note
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (osc[i].step == 0) {
|
||||
// This one is free
|
||||
osc[i].key = key;
|
||||
osc[i].pos = 0;
|
||||
osc[i].step = keyStepMap[key];
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (osc[i].key == (uint32_t)key) {
|
||||
osc[i].step = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// The HIDKeyStream object converts a key and state into ASCII. HID key IDs do not map 1:1 to ASCII!
|
||||
// Write the key and make/break state, then read 1 ASCII char back out.
|
||||
keystream.write((uint8_t)key);
|
||||
keystream.write((uint8_t)state);
|
||||
Serial.printf("Keyboard: %02x %s = '%c'\n", key, state ? "DOWN" : "UP", state ? keystream.read() : '-');
|
||||
}
|
||||
|
||||
|
||||
// Consumer keys are the special media keys on most modern keyboards (mute/etc.)
|
||||
void ckb(void *cbdata, int key) {
|
||||
bool state = (bool)cbdata;
|
||||
Serial.printf("Consumer: %02x %s\n", key, state ? "DOWN" : "UP");
|
||||
}
|
||||
|
||||
// Joystick can get reports of 4 analog axes, 1 d-pad bitfield, and up to 32 buttons
|
||||
// Axes and hats that aren't reported by the joystick are read as 0
|
||||
void joy(void *cbdata, int x, int y, int z, int rz, uint8_t hat, uint32_t buttons) {
|
||||
(void) cbdata;
|
||||
const char *hats[16] = { "U", "UR", "R", "DR", "D", "DL", "L", "UL", "", "", "", "", "", "", "", "." };
|
||||
Serial.printf("Joystick: (%4d, %4d) (%4d, %4d), Hat: %-2s, Buttons:", x, y, z, rz, hats[hat & 15]);
|
||||
for (int i = 0; i < 32; i++) {
|
||||
Serial.printf(" %c", (buttons & 1 << i) ? '*' : '.');
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin();
|
||||
delay(3000);
|
||||
|
||||
Serial.printf("Starting HID master, put your device in pairing mode now.\n");
|
||||
|
||||
// Init the sound generator
|
||||
precalculateSine();
|
||||
silenceOscillators();
|
||||
setupKeyStepMap();
|
||||
|
||||
// Setup the HID key to ASCII conversion
|
||||
keystream.begin();
|
||||
|
||||
// Init the PWM audio output
|
||||
pwm.setStereo(true);
|
||||
pwm.setBuffers(16, 64);
|
||||
pwm.onTransmit(fill);
|
||||
pwm.begin(44100);
|
||||
|
||||
// Mouse buttons and movement reported separately
|
||||
hid.onMouseMove(mm);
|
||||
hid.onMouseButton(mb);
|
||||
|
||||
// We can use the cbData as a flag to see if we're making or breaking a key
|
||||
hid.onKeyDown(kb, (void *)true);
|
||||
hid.onKeyUp(kb, (void *)false);
|
||||
|
||||
// Consumer keys are the special function ones like "mute" or "home"
|
||||
hid.onConsumerKeyDown(ckb, (void *)true);
|
||||
hid.onConsumerKeyUp(ckb, (void *)false);
|
||||
|
||||
hid.onJoystick(joy);
|
||||
|
||||
hid.begin(true);
|
||||
|
||||
hid.connectBLE();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (BOOTSEL) {
|
||||
while (BOOTSEL) {
|
||||
delay(1);
|
||||
}
|
||||
hid.disconnect();
|
||||
hid.clearPairing();
|
||||
Serial.printf("Restarting HID master, put your device in pairing mode now.\n");
|
||||
hid.connectBLE();
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,8 @@ scanAsyncResult KEYWORD2
|
||||
|
||||
connectKeyboard KEYWORD2
|
||||
connectMouse KEYWORD2
|
||||
connectJoypad KEYWORD2
|
||||
connectAny KEYWORD2
|
||||
|
||||
hidConnected KEYWORD2
|
||||
onMouseMove KEYWORD2
|
||||
@@ -29,6 +31,7 @@ onKeyDown KEYWORD2
|
||||
onKeyUp KEYWORD2
|
||||
onConsumerKeyDown KEYWORD2
|
||||
onConsumerKeyUp KEYWORD2
|
||||
onJoypad KEYWORD2
|
||||
|
||||
# BTDeviceInfo
|
||||
deviceClass KEYWORD2
|
||||
|
||||
@@ -2,8 +2,8 @@ name=BluetoothHIDMaster
|
||||
version=1.0
|
||||
author=Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
maintainer=Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
sentence=Classic Bluetooth HID (Keyboard/Mouse/Joystick) master mode
|
||||
paragraph=Classic Bluetooth HID (Keyboard/Mouse/Joystick) master mode
|
||||
sentence=Bluetooth Classic and BLE HID (Keyboard/Mouse/Joystick) master mode
|
||||
paragraph=Bluetooth Classic and BLE HID (Keyboard/Mouse/Joystick) master mode
|
||||
category=Communication
|
||||
url=http://github.com/earlephilhower/arduino-pico
|
||||
architectures=rp2040
|
||||
|
||||
@@ -73,22 +73,41 @@
|
||||
static_cast<btstack_packet_handler_t>(CCALLBACKNAME<void(uint8_t, uint16_t, uint8_t*, uint16_t), __COUNTER__ - 1>::callback))
|
||||
|
||||
|
||||
void BluetoothHIDMaster::begin() {
|
||||
// Initialize HID Host
|
||||
hid_host_init(_hid_descriptor_storage, sizeof(_hid_descriptor_storage));
|
||||
hid_host_register_packet_handler(PACKETHANDLERCB(BluetoothHIDMaster, hid_packet_handler));
|
||||
void BluetoothHIDMaster::begin(bool ble, const char *bleName) {
|
||||
_ble = ble;
|
||||
if (!ble) {
|
||||
// Initialize HID Host
|
||||
hid_host_init(_hid_descriptor_storage, sizeof(_hid_descriptor_storage));
|
||||
hid_host_register_packet_handler(PACKETHANDLERCB(BluetoothHIDMaster, hid_packet_handler));
|
||||
} else {
|
||||
if (bleName) {
|
||||
_hci.setBLEName(bleName);
|
||||
}
|
||||
_hci.setPairOnMeta(true);
|
||||
}
|
||||
|
||||
// Initialize L2CAP
|
||||
l2cap_init();
|
||||
|
||||
// Initialize LE Security Manager. Needed for cross-transport key derivation
|
||||
if (ble) {
|
||||
// register for events from Security Manager
|
||||
_sm_event_callback_registration.callback = PACKETHANDLERCB(BluetoothHIDMaster, sm_packet_handler);
|
||||
sm_add_event_handler(&_sm_event_callback_registration);
|
||||
}
|
||||
sm_init();
|
||||
|
||||
// Allow sniff mode requests by HID device and support role switch
|
||||
gap_set_default_link_policy_settings(LM_LINK_POLICY_ENABLE_SNIFF_MODE | LM_LINK_POLICY_ENABLE_ROLE_SWITCH);
|
||||
if (ble) {
|
||||
gatt_client_init();
|
||||
hids_client_init(_hid_descriptor_storage, sizeof(_hid_descriptor_storage));
|
||||
} else {
|
||||
// Allow sniff mode requests by HID device and support role switch
|
||||
gap_set_default_link_policy_settings(LM_LINK_POLICY_ENABLE_SNIFF_MODE | LM_LINK_POLICY_ENABLE_ROLE_SWITCH);
|
||||
|
||||
// try to become master on incoming connections
|
||||
hci_set_master_slave_policy(HCI_ROLE_MASTER);
|
||||
}
|
||||
|
||||
// try to become master on incoming connections
|
||||
hci_set_master_slave_policy(HCI_ROLE_MASTER);
|
||||
// enabled EIR
|
||||
hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR);
|
||||
|
||||
@@ -142,14 +161,19 @@ void BluetoothHIDMaster::onConsumerKeyUp(void (*cb)(void *, int), void *cbData)
|
||||
_consumerKeyUpData = cbData;
|
||||
}
|
||||
|
||||
std::list<BTDeviceInfo> BluetoothHIDMaster::scan(uint32_t mask, int scanTimeSec, bool async) {
|
||||
void BluetoothHIDMaster::onJoystick(void (*cb)(void *, int, int, int, int, uint8_t, uint32_t), void *cbData) {
|
||||
_joystickCB = cb;
|
||||
_joystickData = cbData;
|
||||
}
|
||||
|
||||
std::vector<BTDeviceInfo> BluetoothHIDMaster::scan(uint32_t mask, int scanTimeSec, bool async) {
|
||||
return _hci.scan(mask, scanTimeSec, async);
|
||||
}
|
||||
|
||||
bool BluetoothHIDMaster::scanAsyncDone() {
|
||||
return _hci.scanAsyncDone();
|
||||
}
|
||||
std::list<BTDeviceInfo> BluetoothHIDMaster::scanAsyncResult() {
|
||||
std::vector<BTDeviceInfo> BluetoothHIDMaster::scanAsyncResult() {
|
||||
return _hci.scanAsyncResult();
|
||||
}
|
||||
|
||||
@@ -158,7 +182,7 @@ bool BluetoothHIDMaster::connected() {
|
||||
}
|
||||
|
||||
bool BluetoothHIDMaster::connect(const uint8_t *addr) {
|
||||
if (!_running) {
|
||||
if (!_running || _ble) {
|
||||
return false;
|
||||
}
|
||||
while (!_hci.running()) {
|
||||
@@ -170,7 +194,7 @@ bool BluetoothHIDMaster::connect(const uint8_t *addr) {
|
||||
}
|
||||
|
||||
bool BluetoothHIDMaster::connectCOD(uint32_t cod) {
|
||||
if (!_running) {
|
||||
if (!_running || _ble) {
|
||||
return false;
|
||||
}
|
||||
while (!_hci.running()) {
|
||||
@@ -192,6 +216,39 @@ bool BluetoothHIDMaster::connectCOD(uint32_t cod) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool BluetoothHIDMaster::connectBLE(const uint8_t *addr, int addrType) {
|
||||
if (!_running || !_ble) {
|
||||
return false;
|
||||
}
|
||||
while (!_hci.running()) {
|
||||
delay(10);
|
||||
}
|
||||
uint8_t a[6];
|
||||
memcpy(a, addr, sizeof(a));
|
||||
return ERROR_CODE_SUCCESS == gap_connect(a, (bd_addr_type_t)addrType);
|
||||
}
|
||||
|
||||
bool BluetoothHIDMaster::connectBLE() {
|
||||
if (!_running || !_ble) {
|
||||
return false;
|
||||
}
|
||||
while (!_hci.running()) {
|
||||
delay(10);
|
||||
}
|
||||
|
||||
clearPairing();
|
||||
auto l = _hci.scanBLE(0x1812 /* HID */);
|
||||
for (auto e : l) {
|
||||
DEBUGV("Scan connecting %s at %s ... ", e.name(), e.addressString());
|
||||
if (connectBLE(e.address(), e.addressType())) {
|
||||
DEBUGV("Connection established\n");
|
||||
return true;
|
||||
}
|
||||
DEBUGV("Failed\n");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool BluetoothHIDMaster::connectKeyboard() {
|
||||
return connectCOD(0x2540);
|
||||
}
|
||||
@@ -200,14 +257,24 @@ bool BluetoothHIDMaster::connectMouse() {
|
||||
return connectCOD(0x2580);
|
||||
}
|
||||
|
||||
bool BluetoothHIDMaster::connectJoystick() {
|
||||
return connectCOD(0x2508);
|
||||
}
|
||||
|
||||
bool BluetoothHIDMaster::connectAny() {
|
||||
return connectCOD(0x2500);
|
||||
}
|
||||
|
||||
bool BluetoothHIDMaster::disconnect() {
|
||||
BluetoothLock b;
|
||||
if (connected()) {
|
||||
hid_host_disconnect(_hid_host_cid);
|
||||
}
|
||||
if (!_running || !connected()) {
|
||||
return false;
|
||||
}
|
||||
if (!_ble && connected()) {
|
||||
hid_host_disconnect(_hid_host_cid);
|
||||
} else if (_ble && connected()) {
|
||||
gap_disconnect(_hci.getHCIConn());
|
||||
}
|
||||
_hid_host_descriptor_available = false;
|
||||
return true;
|
||||
}
|
||||
@@ -215,7 +282,11 @@ bool BluetoothHIDMaster::disconnect() {
|
||||
void BluetoothHIDMaster::clearPairing() {
|
||||
BluetoothLock b;
|
||||
if (connected()) {
|
||||
hid_host_disconnect(_hid_host_cid);
|
||||
if (_ble) {
|
||||
gap_disconnect(_hci.getHCIConn());
|
||||
} else {
|
||||
hid_host_disconnect(_hid_host_cid);
|
||||
}
|
||||
}
|
||||
gap_delete_all_link_keys();
|
||||
_hid_host_descriptor_available = false;
|
||||
@@ -229,17 +300,21 @@ void BluetoothHIDMaster::hid_host_handle_interrupt_report(btstack_hid_parser_t *
|
||||
int new_keys_count = 0;
|
||||
|
||||
uint16_t new_consumer_key = 0;
|
||||
uint8_t newMB = 0;
|
||||
uint32_t newMB = 0;
|
||||
bool noMB = false;
|
||||
|
||||
bool updCons = false;
|
||||
bool updKey = false;
|
||||
bool updMB = false;
|
||||
bool updJoy = false;
|
||||
|
||||
bool updMouse = false;
|
||||
int dx = 0;
|
||||
int dy = 0;
|
||||
int dz = 0;
|
||||
int rz = 0;
|
||||
int dwheel = 0;
|
||||
uint8_t hat = 0;
|
||||
|
||||
while (btstack_hid_parser_has_more(parser)) {
|
||||
uint16_t usage_page;
|
||||
@@ -248,19 +323,26 @@ void BluetoothHIDMaster::hid_host_handle_interrupt_report(btstack_hid_parser_t *
|
||||
btstack_hid_parser_get_field(parser, &usage_page, &usage, &value);
|
||||
if (usage_page == 0x01) {
|
||||
updMouse = true;
|
||||
updJoy = true;
|
||||
if (usage == 0x30) {
|
||||
dx = value;
|
||||
} else if (usage == 0x31) {
|
||||
dy = value;
|
||||
} else if (usage == 0x32) {
|
||||
dz = value;
|
||||
} else if (usage == 0x35) {
|
||||
rz = value;
|
||||
} else if (usage == 0x38) {
|
||||
dwheel = value;
|
||||
} else if (usage == 0x39) {
|
||||
hat = value & 0xff;
|
||||
}
|
||||
} else if (usage_page == 0x09) {
|
||||
updMB = true;
|
||||
if (usage == 0) {
|
||||
noMB = true;
|
||||
}
|
||||
if (!noMB && value && (usage > 0) && (usage < 9)) {
|
||||
if (!noMB && value && (usage > 0)) {
|
||||
newMB |= 1 << (usage - 1);
|
||||
}
|
||||
|
||||
@@ -328,13 +410,13 @@ void BluetoothHIDMaster::hid_host_handle_interrupt_report(btstack_hid_parser_t *
|
||||
if (updCons) {
|
||||
last_consumer_key = new_consumer_key;
|
||||
}
|
||||
if (updMB) {
|
||||
if (updMB && _mouseButtonCB) {
|
||||
if (lastMB != newMB) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
int mask = 1 << i;
|
||||
if ((lastMB & mask) && !(newMB & mask) && _mouseButtonCB) {
|
||||
if ((lastMB & mask) && !(newMB & mask)) {
|
||||
_mouseButtonCB(_mouseButtonData, i, false);
|
||||
} else if (!(lastMB & mask) && (newMB & mask) && _mouseButtonCB) {
|
||||
} else if (!(lastMB & mask) && (newMB & mask)) {
|
||||
_mouseButtonCB(_mouseButtonData, i, true);
|
||||
}
|
||||
}
|
||||
@@ -345,6 +427,9 @@ void BluetoothHIDMaster::hid_host_handle_interrupt_report(btstack_hid_parser_t *
|
||||
if (updMouse && _mouseMoveCB) {
|
||||
_mouseMoveCB(_mouseMoveData, dx, dy, dwheel);
|
||||
}
|
||||
if (updJoy && _joystickCB) {
|
||||
_joystickCB(_joystickData, dx, dy, dz, rz, hat, newMB);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -453,6 +538,102 @@ void BluetoothHIDMaster::hid_packet_handler(uint8_t packet_type, uint16_t channe
|
||||
|
||||
|
||||
|
||||
void BluetoothHIDMaster::sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
|
||||
UNUSED(channel);
|
||||
UNUSED(size);
|
||||
|
||||
if (packet_type != HCI_EVENT_PACKET) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (hci_event_packet_get_type(packet)) {
|
||||
case SM_EVENT_JUST_WORKS_REQUEST:
|
||||
DEBUGV("Just works requested\n");
|
||||
sm_just_works_confirm(sm_event_just_works_request_get_handle(packet));
|
||||
break;
|
||||
case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
|
||||
DEBUGV("Confirming numeric comparison: %" PRIu32 "\n", sm_event_numeric_comparison_request_get_passkey(packet));
|
||||
sm_numeric_comparison_confirm(sm_event_passkey_display_number_get_handle(packet));
|
||||
break;
|
||||
case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
|
||||
DEBUGV("Display Passkey: %" PRIu32 "\n", sm_event_passkey_display_number_get_passkey(packet));
|
||||
break;
|
||||
case SM_EVENT_PAIRING_COMPLETE:
|
||||
switch (sm_event_pairing_complete_get_status(packet)) {
|
||||
case ERROR_CODE_SUCCESS:
|
||||
DEBUGV("Pairing complete, success\n");
|
||||
// continue - query primary services
|
||||
DEBUGV("Search for HID service.\n");
|
||||
//app_state = W4_HID_CLIENT_CONNECTED;
|
||||
hids_client_connect(_hci.getHCIConn(), PACKETHANDLERCB(BluetoothHIDMaster, handle_gatt_client_event), HID_PROTOCOL_MODE_REPORT, &_hid_host_cid);
|
||||
break;
|
||||
case ERROR_CODE_CONNECTION_TIMEOUT:
|
||||
DEBUGV("Pairing failed, timeout\n");
|
||||
break;
|
||||
case ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION:
|
||||
DEBUGV("Pairing failed, disconnected\n");
|
||||
break;
|
||||
case ERROR_CODE_AUTHENTICATION_FAILURE:
|
||||
DEBUGV("Pairing failed, reason = %u\n", sm_event_pairing_complete_get_reason(packet));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BluetoothHIDMaster::handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
|
||||
UNUSED(packet_type);
|
||||
UNUSED(channel);
|
||||
UNUSED(size);
|
||||
|
||||
uint8_t status;
|
||||
int idx;
|
||||
|
||||
if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (hci_event_gattservice_meta_get_subevent_code(packet)) {
|
||||
case GATTSERVICE_SUBEVENT_HID_SERVICE_CONNECTED:
|
||||
status = gattservice_subevent_hid_service_connected_get_status(packet);
|
||||
switch (status) {
|
||||
case ERROR_CODE_SUCCESS:
|
||||
DEBUGV("HID service client connected, found %d services\n", gattservice_subevent_hid_service_connected_get_num_instances(packet));
|
||||
_hidConnected = true;
|
||||
_hid_host_descriptor_available = true;
|
||||
break;
|
||||
default:
|
||||
DEBUGV("HID service client connection failed, status 0x%02x.\n", status);
|
||||
gap_disconnect(_hci.getHCIConn());
|
||||
//handle_outgoing_connection_error();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case GATTSERVICE_SUBEVENT_HID_REPORT:
|
||||
idx = gattservice_subevent_hid_report_get_service_index(packet);
|
||||
btstack_hid_parser_t parser;
|
||||
btstack_hid_parser_init(&parser, hids_client_descriptor_storage_get_descriptor_data(_hid_host_cid, idx), hids_client_descriptor_storage_get_descriptor_len(_hid_host_cid, idx), HID_REPORT_TYPE_INPUT, gattservice_subevent_hid_report_get_report(packet), gattservice_subevent_hid_report_get_report_len(packet));
|
||||
hid_host_handle_interrupt_report(&parser);
|
||||
//hid_handle_input_report(
|
||||
// gattservice_subevent_hid_report_get_service_index(packet),
|
||||
// gattservice_subevent_hid_report_get_report(packet),
|
||||
// gattservice_subevent_hid_report_get_report_len(packet));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Simplified US Keyboard with Shift modifier
|
||||
|
||||
#define CHAR_ILLEGAL 0xff
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
Bluetooth HID Master class, can connect to keyboards, mice, and joypads
|
||||
Bluetooth HID Master class, can connect to keyboards, mice, and joysticks
|
||||
Works with Bluetooth Classic and BLE devices
|
||||
|
||||
Copyright (c) 2024 Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
@@ -66,7 +67,10 @@ private:
|
||||
|
||||
class BluetoothHIDMaster {
|
||||
public:
|
||||
void begin();
|
||||
void begin(const char *bleName) {
|
||||
begin(true, bleName);
|
||||
}
|
||||
void begin(bool ble = false, const char *bleName = nullptr);
|
||||
bool connected();
|
||||
void end();
|
||||
bool hciRunning();
|
||||
@@ -74,14 +78,21 @@ public:
|
||||
|
||||
static const uint32_t keyboard_cod = 0x2540;
|
||||
static const uint32_t mouse_cod = 0x2540;
|
||||
static const uint32_t joystick_cod = 0x2508;
|
||||
static const uint32_t any_cod = 0;
|
||||
std::list<BTDeviceInfo> scan(uint32_t mask, int scanTimeSec = 5, bool async = false);
|
||||
std::vector<BTDeviceInfo> scan(uint32_t mask, int scanTimeSec = 5, bool async = false);
|
||||
bool scanAsyncDone();
|
||||
std::list<BTDeviceInfo> scanAsyncResult();
|
||||
std::vector<BTDeviceInfo> scanAsyncResult();
|
||||
|
||||
bool connect(const uint8_t *addr);
|
||||
bool connectKeyboard();
|
||||
bool connectMouse();
|
||||
bool connectJoystick();
|
||||
bool connectAny();
|
||||
|
||||
bool connectBLE(const uint8_t *addr, int addrType);
|
||||
bool connectBLE();
|
||||
|
||||
bool disconnect();
|
||||
void clearPairing();
|
||||
|
||||
@@ -91,8 +102,10 @@ public:
|
||||
void onKeyUp(void (*)(void *, int), void *cbData = nullptr);
|
||||
void onConsumerKeyDown(void (*)(void *, int), void *cbData = nullptr);
|
||||
void onConsumerKeyUp(void (*)(void *, int), void *cbData = nullptr);
|
||||
void onJoystick(void (*)(void *, int, int, int, int, uint8_t, uint32_t), void *cbData = nullptr);
|
||||
|
||||
private:
|
||||
bool _ble = false;
|
||||
bool connectCOD(uint32_t cod);
|
||||
BluetoothHCI _hci;
|
||||
void hid_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
|
||||
@@ -121,4 +134,12 @@ private:
|
||||
void *_consumerKeyDownData;
|
||||
void (*_consumerKeyUpCB)(void *, int) = nullptr;
|
||||
void *_consumerKeyUpData;
|
||||
|
||||
void (*_joystickCB)(void *, int, int, int, int, uint8_t, uint32_t) = nullptr;
|
||||
void *_joystickData;
|
||||
|
||||
|
||||
btstack_packet_callback_registration_t _sm_event_callback_registration;
|
||||
void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
|
||||
void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
|
||||
};
|
||||
|
||||
@@ -24,8 +24,8 @@
|
||||
#include <lwip/def.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
#ifdef DEBUG_ESP_PORT
|
||||
#define CONSOLE DEBUG_ESP_PORT
|
||||
#ifdef DEBUG_RP2040_PORT
|
||||
#define CONSOLE DEBUG_RP2040_PORT
|
||||
#else
|
||||
#define CONSOLE Serial
|
||||
#endif
|
||||
|
||||
Submodule libraries/FreeRTOS/lib/FreeRTOS-Kernel updated: e1d1aab5c0...6a303231ec
@@ -1,10 +1,10 @@
|
||||
#define configNUM_CORES 2
|
||||
#define configUSE_CORE_AFFINITY 1
|
||||
#define configRUN_MULTIPLE_PRIORITIES 1
|
||||
#define configNUMBER_OF_CORES 2
|
||||
#define configUSE_CORE_AFFINITY 1
|
||||
#define configRUN_MULTIPLE_PRIORITIES 1
|
||||
|
||||
#define configUSE_PREEMPTION 1
|
||||
#define configUSE_IDLE_HOOK 1
|
||||
#define configUSE_MINIMAL_IDLE_HOOK 1
|
||||
#define configUSE_PASSIVE_IDLE_HOOK 1
|
||||
#define configUSE_TICK_HOOK 1
|
||||
#define configCPU_CLOCK_HZ ( ( unsigned long ) F_CPU )
|
||||
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
|
||||
@@ -26,6 +26,7 @@
|
||||
#define configSUPPORT_STATIC_ALLOCATION 1
|
||||
#define configSTACK_DEPTH_TYPE uint32_t
|
||||
#define configUSE_TASK_PREEMPTION_DISABLE 1
|
||||
#define configTASK_NOTIFICATION_ARRAY_ENTRIES 4
|
||||
|
||||
#define configUSE_NEWLIB_REENTRANT 1
|
||||
#define configNEWLIB_REENTRANT_IS_DYNAMIC 1
|
||||
@@ -38,7 +39,7 @@ extern unsigned long ulMainGetRunTimeCounterValue(void);
|
||||
#define portGET_RUN_TIME_COUNTER_VALUE() ulMainGetRunTimeCounterValue()
|
||||
|
||||
/* Co-routine definitions. */
|
||||
#define configUSE_CO_ROUTINES 1
|
||||
#define configUSE_CO_ROUTINES 0
|
||||
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )
|
||||
|
||||
/* Software timer definitions. */
|
||||
@@ -99,7 +100,14 @@ extern unsigned long ulMainGetRunTimeCounterValue(void);
|
||||
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
|
||||
#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
|
||||
|
||||
extern void rtosFatalError(void);
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
void rtosFatalError(void);
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
|
||||
#define configASSERT( x ) \
|
||||
if( ( x ) == 0 ) { portDISABLE_INTERRUPTS(); rtosFatalError(); }
|
||||
|
||||
@@ -107,4 +115,6 @@ extern void rtosFatalError(void);
|
||||
#define configSUPPORT_PICO_SYNC_INTEROP 1
|
||||
#define configSUPPORT_PICO_TIME_INTEROP 1
|
||||
|
||||
#define LIB_PICO_MULTICORE 1
|
||||
|
||||
#include "rp2040_config.h"
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
#include "../lib/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/idle_task_static_memory.c"
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "../lib/FreeRTOS-Kernel/tasks.c"
|
||||
|
||||
//See https://github.com/FreeRTOS/FreeRTOS-Kernel/pull/496
|
||||
// See https://github.com/FreeRTOS/FreeRTOS-Kernel/pull/496
|
||||
struct _reent* __wrap___getreent(void) {
|
||||
// No lock needed because if this changes, we won't be running anymore.
|
||||
TCB_t *pxCurTask = xTaskGetCurrentTaskHandle();
|
||||
@@ -9,6 +9,6 @@ struct _reent* __wrap___getreent(void) {
|
||||
return _GLOBAL_REENT;
|
||||
} else {
|
||||
// We have a task; return its reentrant struct.
|
||||
return &pxCurTask->xNewLib_reent;
|
||||
return &pxCurTask->xTLSBlock;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,6 +193,354 @@ extern "C" void __no_inline_not_in_flash_func(__freertos_resume_other_core)() {
|
||||
vTaskPreemptionEnable(nullptr);
|
||||
}
|
||||
|
||||
#include <lwip_wrap.h>
|
||||
|
||||
static TaskHandle_t __lwipTask;
|
||||
static QueueHandle_t __lwipQueue;
|
||||
typedef struct {
|
||||
__lwip_op op;
|
||||
void *req;
|
||||
TaskHandle_t wakeup;
|
||||
} LWIPWork;
|
||||
|
||||
#define TASK_NOTIFY_LWIP_WAKEUP (configTASK_NOTIFICATION_ARRAY_ENTRIES - 1)
|
||||
|
||||
extern "C" void __lwip(__lwip_op op, void *req) {
|
||||
LWIPWork w;
|
||||
TaskStatus_t t;
|
||||
vTaskGetInfo(nullptr, &t, pdFALSE, eInvalid); // TODO - can we speed this up???
|
||||
w.op = op;
|
||||
w.req = req;
|
||||
w.wakeup = t.xHandle;
|
||||
if (!xQueueSend(__lwipQueue, &w, portMAX_DELAY)) {
|
||||
panic("LWIP task send failed");
|
||||
}
|
||||
ulTaskNotifyTakeIndexed(TASK_NOTIFY_LWIP_WAKEUP, pdTRUE, portMAX_DELAY);
|
||||
}
|
||||
|
||||
extern "C" bool __isLWIPThread() {
|
||||
TaskStatus_t t;
|
||||
vTaskGetInfo(nullptr, &t, pdFALSE, eInvalid); // TODO - can we speed this up???
|
||||
return t.xHandle == __lwipTask;
|
||||
}
|
||||
|
||||
|
||||
static void lwipThread(void *params) {
|
||||
(void) params;
|
||||
LWIPWork w;
|
||||
assert(__isLWIPThread());
|
||||
|
||||
while (true) {
|
||||
if (xQueueReceive(__lwipQueue, &w, portMAX_DELAY)) {
|
||||
switch (w.op) {
|
||||
case __lwip_init:
|
||||
{
|
||||
__real_lwip_init();
|
||||
break;
|
||||
}
|
||||
case __pbuf_header:
|
||||
{
|
||||
__pbuf_header_req *r = (__pbuf_header_req *)w.req;
|
||||
*(r->ret) = __real_pbuf_header(r->p, r->header_size);
|
||||
break;
|
||||
}
|
||||
case __pbuf_free:
|
||||
{
|
||||
__pbuf_free_req *r = (__pbuf_free_req *)w.req;
|
||||
*(r->ret) = __real_pbuf_free(r->p);
|
||||
break;
|
||||
}
|
||||
case __pbuf_alloc:
|
||||
{
|
||||
__pbuf_alloc_req *r = (__pbuf_alloc_req *)w.req;
|
||||
*(r->ret) = __real_pbuf_alloc(r->l, r->length, r->type);
|
||||
break;
|
||||
}
|
||||
case __pbuf_take:
|
||||
{
|
||||
__pbuf_take_req *r = (__pbuf_take_req *)w.req;
|
||||
*(r->ret) = __real_pbuf_take(r->buf, r->dataptr, r->len);
|
||||
break;
|
||||
}
|
||||
case __pbuf_copy_partial:
|
||||
{
|
||||
__pbuf_copy_partial_req *r = (__pbuf_copy_partial_req *)w.req;
|
||||
*(r->ret) = __real_pbuf_copy_partial(r->p, r->dataptr, r->len, r->offset);
|
||||
break;
|
||||
}
|
||||
case __pbuf_ref:
|
||||
{
|
||||
__pbuf_ref_req *r = (__pbuf_ref_req *)w.req;
|
||||
__real_pbuf_ref(r->p);
|
||||
break;
|
||||
}
|
||||
case __pbuf_get_at:
|
||||
{
|
||||
__pbuf_get_at_req *r = (__pbuf_get_at_req *)w.req;
|
||||
*(r->ret) = __real_pbuf_get_at(r->p, r->offset);
|
||||
break;
|
||||
}
|
||||
case __pbuf_get_contiguous:
|
||||
{
|
||||
__pbuf_get_contiguous_req *r = (__pbuf_get_contiguous_req *)w.req;
|
||||
*(r->ret) = __real_pbuf_get_contiguous(r->p, r->buffer, r->bufsize, r->len, r->offset);
|
||||
break;
|
||||
}
|
||||
case __pbuf_cat:
|
||||
{
|
||||
__pbuf_cat_req *r = (__pbuf_cat_req *)w.req;
|
||||
__real_pbuf_cat(r->head, r->tail);
|
||||
break;
|
||||
}
|
||||
case __tcp_arg:
|
||||
{
|
||||
__tcp_arg_req *r = (__tcp_arg_req *)w.req;
|
||||
__real_tcp_arg(r->pcb, r->arg);
|
||||
break;
|
||||
}
|
||||
case __tcp_new:
|
||||
{
|
||||
__tcp_new_req *r = (__tcp_new_req *)w.req;
|
||||
*(r->ret) = __real_tcp_new();
|
||||
break;
|
||||
}
|
||||
case __tcp_bind:
|
||||
{
|
||||
__tcp_bind_req *r = (__tcp_bind_req *)w.req;
|
||||
*(r->ret) = __real_tcp_bind(r->pcb, r->ipaddr, r->port);
|
||||
break;
|
||||
}
|
||||
case __tcp_listen_with_backlog:
|
||||
{
|
||||
__tcp_listen_with_backlog_req *r = (__tcp_listen_with_backlog_req *)w.req;
|
||||
*(r->ret) = __real_tcp_listen_with_backlog(r->pcb, r->backlog);
|
||||
break;
|
||||
}
|
||||
case __tcp_accept:
|
||||
{
|
||||
__tcp_accept_req *r = (__tcp_accept_req *)w.req;
|
||||
__real_tcp_accept(r->pcb, r->accept);
|
||||
break;
|
||||
}
|
||||
case __tcp_connect:
|
||||
{
|
||||
__tcp_connect_req *r = (__tcp_connect_req *)w.req;
|
||||
*(r->ret) = __real_tcp_connect(r->pcb, r->ipaddr, r->port, r->connected);
|
||||
break;
|
||||
}
|
||||
case __tcp_write:
|
||||
{
|
||||
__tcp_write_req *r = (__tcp_write_req *)w.req;
|
||||
*(r->ret) = __real_tcp_write(r->pcb, r->dataptr, r->len, r->apiflags);
|
||||
break;
|
||||
}
|
||||
case __tcp_sent:
|
||||
{
|
||||
__tcp_sent_req *r = (__tcp_sent_req *)w.req;
|
||||
__real_tcp_sent(r->pcb, r->sent);
|
||||
break;
|
||||
}
|
||||
case __tcp_recv:
|
||||
{
|
||||
__tcp_recv_req *r = (__tcp_recv_req *)w.req;
|
||||
__real_tcp_recv(r->pcb, r->recv);
|
||||
break;
|
||||
}
|
||||
case __tcp_recved:
|
||||
{
|
||||
__tcp_recved_req *r = (__tcp_recved_req *)w.req;
|
||||
__real_tcp_recved(r->pcb, r->len);
|
||||
break;
|
||||
}
|
||||
case __tcp_poll:
|
||||
{
|
||||
__tcp_poll_req *r = (__tcp_poll_req *)w.req;
|
||||
__real_tcp_poll(r->pcb, r->poll, r->interval);
|
||||
break;
|
||||
}
|
||||
case __tcp_close:
|
||||
{
|
||||
__tcp_close_req *r = (__tcp_close_req *)w.req;
|
||||
*(r->ret) = __real_tcp_close(r->pcb);
|
||||
break;
|
||||
}
|
||||
case __tcp_abort:
|
||||
{
|
||||
__tcp_abort_req *r = (__tcp_abort_req *)w.req;
|
||||
__real_tcp_abort(r->pcb);
|
||||
break;
|
||||
}
|
||||
case __tcp_err:
|
||||
{
|
||||
__tcp_err_req *r = (__tcp_err_req *)w.req;
|
||||
__real_tcp_err(r->pcb, r->err);
|
||||
break;
|
||||
}
|
||||
case __tcp_output:
|
||||
{
|
||||
__tcp_output_req *r = (__tcp_output_req *)w.req;
|
||||
*(r->ret) = __real_tcp_output(r->pcb);
|
||||
break;
|
||||
}
|
||||
case __tcp_setprio:
|
||||
{
|
||||
__tcp_setprio_req *r = (__tcp_setprio_req *)w.req;
|
||||
__real_tcp_setprio(r->pcb, r->prio);
|
||||
break;
|
||||
}
|
||||
case __tcp_backlog_delayed:
|
||||
{
|
||||
__tcp_backlog_delayed_req *r = (__tcp_backlog_delayed_req *)w.req;
|
||||
__real_tcp_backlog_delayed(r->pcb);
|
||||
break;
|
||||
}
|
||||
case __tcp_backlog_accepted:
|
||||
{
|
||||
__tcp_backlog_accepted_req *r = (__tcp_backlog_accepted_req *)w.req;
|
||||
__real_tcp_backlog_accepted(r->pcb);
|
||||
break;
|
||||
}
|
||||
case __udp_new:
|
||||
{
|
||||
__udp_new_req *r = (__udp_new_req *)w.req;
|
||||
*(r->ret) = __real_udp_new();
|
||||
break;
|
||||
}
|
||||
case __udp_remove:
|
||||
{
|
||||
__udp_remove_req *r = (__udp_remove_req *)w.req;
|
||||
__real_udp_remove(r->pcb);
|
||||
break;
|
||||
}
|
||||
case __udp_bind:
|
||||
{
|
||||
__udp_bind_req *r = (__udp_bind_req *)w.req;
|
||||
*(r->ret) = __real_udp_bind(r->pcb, r->ipaddr, r->port);
|
||||
break;
|
||||
}
|
||||
case __udp_connect:
|
||||
{
|
||||
__udp_connect_req *r = (__udp_connect_req *)w.req;
|
||||
*(r->ret) = __real_udp_connect(r->pcb, r->ipaddr, r->port);
|
||||
break;
|
||||
}
|
||||
case __udp_disconnect:
|
||||
{
|
||||
__udp_disconnect_req *r = (__udp_disconnect_req *)w.req;
|
||||
*(r->ret) = __real_udp_disconnect(r->pcb);
|
||||
break;
|
||||
}
|
||||
case __udp_send:
|
||||
{
|
||||
__udp_send_req *r = (__udp_send_req *)w.req;
|
||||
*(r->ret) = __real_udp_send(r->pcb, r->p);
|
||||
break;
|
||||
}
|
||||
case __udp_recv:
|
||||
{
|
||||
__udp_recv_req *r = (__udp_recv_req *)w.req;
|
||||
__real_udp_recv(r->pcb, r->recv, r->recv_arg);
|
||||
break;
|
||||
}
|
||||
case __udp_sendto_if:
|
||||
{
|
||||
__udp_sendto_if_req *r = (__udp_sendto_if_req *)w.req;
|
||||
*(r->ret) = __real_udp_sendto_if(r->pcb, r->p, r->dst_ip, r->dst_port, r->netif);
|
||||
break;
|
||||
}
|
||||
case __sys_check_timeouts:
|
||||
{
|
||||
__real_sys_check_timeouts();
|
||||
break;
|
||||
}
|
||||
case __dns_gethostbyname:
|
||||
{
|
||||
__dns_gethostbyname_req *r = (__dns_gethostbyname_req *)w.req;
|
||||
*(r->ret) = __real_dns_gethostbyname(r->hostname, r->addr, r->found, r->callback_arg);
|
||||
break;
|
||||
}
|
||||
case __dns_gethostbyname_addrtype:
|
||||
{
|
||||
__dns_gethostbyname_addrtype_req *r = (__dns_gethostbyname_addrtype_req *)w.req;
|
||||
*(r->ret) = __real_dns_gethostbyname_addrtype(r->hostname, r->addr, r->found, r->callback_arg, r->dns_addrtype);
|
||||
break;
|
||||
}
|
||||
case __raw_new:
|
||||
{
|
||||
__raw_new_req *r = (__raw_new_req *)w.req;
|
||||
*(r->ret) = __real_raw_new(r->proto);
|
||||
break;
|
||||
}
|
||||
case __raw_recv:
|
||||
{
|
||||
__raw_recv_req *r = (__raw_recv_req *)w.req;
|
||||
__real_raw_recv(r->pcb, r->recv, r->recv_arg);
|
||||
break;
|
||||
}
|
||||
case __raw_bind:
|
||||
{
|
||||
__raw_bind_req *r = (__raw_bind_req *)w.req;
|
||||
*(r->ret) = __real_raw_bind(r->pcb, r->ipaddr);
|
||||
break;
|
||||
}
|
||||
case __raw_sendto:
|
||||
{
|
||||
__raw_sendto_req *r = (__raw_sendto_req *)w.req;
|
||||
*(r->ret) = __real_raw_sendto(r->pcb, r->p, r->ipaddr);
|
||||
break;
|
||||
}
|
||||
case __raw_remove:
|
||||
{
|
||||
__raw_remove_req *r = (__raw_remove_req *)w.req;
|
||||
__real_raw_remove(r->pcb);
|
||||
break;
|
||||
}
|
||||
case __netif_add:
|
||||
{
|
||||
__netif_add_req *r = (__netif_add_req *)w.req;
|
||||
*(r->ret) = __real_netif_add(r->netif, r->ipaddr, r->netmask, r->gw, r->state, r->init, r->input);
|
||||
break;
|
||||
}
|
||||
case __netif_remove:
|
||||
{
|
||||
__netif_remove_req *r = (__netif_remove_req *)w.req;
|
||||
__real_netif_remove(r->netif);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
// Any new unimplemented calls = ERROR!!!
|
||||
panic("Unimplemented LWIP thread action");
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Work done, return value set, just tickle the calling task
|
||||
xTaskNotifyGiveIndexed(w.wakeup, TASK_NOTIFY_LWIP_WAKEUP);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
extern "C" void ethernet_timeout_reached(void *context, void *worker);
|
||||
static TaskHandle_t __ethernetTask;
|
||||
SemaphoreHandle_t __hwMutex;
|
||||
|
||||
static void ethernetTask(void *param) {
|
||||
(void) param;
|
||||
while (true) {
|
||||
delay(20);
|
||||
{
|
||||
LWIPMutex m;
|
||||
ethernet_timeout_reached(nullptr, nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void __startEthernetTask() {
|
||||
xTaskCreate(ethernetTask, "Ethernet", 1024, 0, configMAX_PRIORITIES / 2 - 1, &__ethernetTask);
|
||||
vTaskCoreAffinitySet(__ethernetTask, 1 << 0);
|
||||
}
|
||||
|
||||
extern mutex_t __usb_mutex;
|
||||
static TaskHandle_t __usbTask;
|
||||
@@ -216,6 +564,12 @@ void startFreeRTOS(void) {
|
||||
xTaskCreate(IdleThisCore, "IdleCore1", 128, 0, configMAX_PRIORITIES - 1, __idleCoreTask + 1);
|
||||
vTaskCoreAffinitySet(__idleCoreTask[1], 1 << 1);
|
||||
|
||||
// LWIP runs on core 0 only
|
||||
__lwipQueue = xQueueCreate(16, sizeof(LWIPWork));
|
||||
__hwMutex = xSemaphoreCreateMutex();
|
||||
xTaskCreate(lwipThread, "LWIP", 1024, 0, configMAX_PRIORITIES / 2 - 1, &__lwipTask);
|
||||
vTaskCoreAffinitySet(__lwipTask, 1 << 0);
|
||||
|
||||
// Initialise and run the freeRTOS scheduler. Execution should never return here.
|
||||
__freeRTOSinitted = true;
|
||||
vTaskStartScheduler();
|
||||
@@ -258,7 +612,7 @@ void vApplicationIdleHook(void) {
|
||||
#endif /* configUSE_IDLE_HOOK == 1 */
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
#if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
|
||||
//#if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
|
||||
/*
|
||||
Call the user defined minimalIdle() function from within the idle task.
|
||||
This allows the application designer to add background functionality
|
||||
@@ -267,17 +621,17 @@ void vApplicationIdleHook(void) {
|
||||
NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES, CALL A FUNCTION THAT MIGHT BLOCK.
|
||||
|
||||
*/
|
||||
void minimalIdle(void) __attribute__((weak));
|
||||
void minimalIdle() {} //Empty minimalIdle function
|
||||
void passiveIdle(void) __attribute__((weak));
|
||||
void passiveIdle() {} //Empty minimalIdle function
|
||||
|
||||
extern "C"
|
||||
void vApplicationMinimalIdleHook(void) __attribute__((weak));
|
||||
//void vApplicationPassiveIdleHook(void) __attribute__((weak));
|
||||
|
||||
void vApplicationMinimalIdleHook(void) {
|
||||
minimalIdle();
|
||||
void vApplicationPassiveIdleHook(void) {
|
||||
passiveIdle();
|
||||
}
|
||||
|
||||
#endif /* configUSE_MINIMAL_IDLE_HOOK == 1 */
|
||||
//#endif /* configUSE_MINIMAL_IDLE_HOOK == 1 */
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
#if ( configUSE_TICK_HOOK == 1 )
|
||||
@@ -394,6 +748,22 @@ void vApplicationStackOverflowHook(TaskHandle_t xTask __attribute__((unused)),
|
||||
#endif /* configCHECK_FOR_STACK_OVERFLOW >= 1 */
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
|
||||
|
||||
extern "C" void vApplicationGetPassiveIdleTaskMemory(StaticTask_t ** ppxIdleTaskTCBBuffer,
|
||||
StackType_t ** ppxIdleTaskStackBuffer,
|
||||
configSTACK_DEPTH_TYPE * puxIdleTaskStackSize,
|
||||
BaseType_t xPassiveIdleTaskIndex) {
|
||||
static StaticTask_t xIdleTaskTCBs[ configNUMBER_OF_CORES ];
|
||||
static StackType_t uxIdleTaskStacks[ configNUMBER_OF_CORES ][ configMINIMAL_STACK_SIZE ];
|
||||
|
||||
*ppxIdleTaskTCBBuffer = &(xIdleTaskTCBs[ xPassiveIdleTaskIndex ]);
|
||||
*ppxIdleTaskStackBuffer = &(uxIdleTaskStacks[ xPassiveIdleTaskIndex ][ 0 ]);
|
||||
*puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#if ( configSUPPORT_STATIC_ALLOCATION >= 1 )
|
||||
|
||||
extern "C"
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
This folder contains FreeRTOS functionality tests, not generally useful for users.
|
||||
@@ -0,0 +1,32 @@
|
||||
// Simple stress test to ensure each thread has its own Newlib reent structure
|
||||
// The random numbers from each task should be identical.
|
||||
|
||||
#include <FreeRTOS.h>
|
||||
#include <task.h>
|
||||
|
||||
void go(void *param) {
|
||||
(void) param;
|
||||
srand(0);
|
||||
int i = 0;
|
||||
while(1) {
|
||||
char buff[100];
|
||||
TaskStatus_t st;
|
||||
vTaskGetInfo(NULL, &st, pdFALSE, eInvalid);
|
||||
sprintf(buff, "task %ld: %d = %d\n", st.xTaskNumber, i++, rand());
|
||||
Serial.print(buff);
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
delay(5000);
|
||||
// put your setup code here, to run once:
|
||||
xTaskCreate(go, "c1", 1024, nullptr, 1, nullptr);
|
||||
xTaskCreate(go, "c2", 1024, nullptr, 1, nullptr);
|
||||
xTaskCreate(go, "c3", 1024, nullptr, 1, nullptr);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
}
|
||||
@@ -0,0 +1,319 @@
|
||||
// FreeRTOS system call/mutex stress test
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <FreeRTOS.h>
|
||||
#include <task.h>
|
||||
#include <semphr.h>
|
||||
#define DELAY 1
|
||||
#define SERIAL_DEBUG Serial1
|
||||
#define STACK_SIZE 512
|
||||
#define CORE_0 (1 << 0)
|
||||
#define CORE_1 (1 << 1)
|
||||
|
||||
void semphrTakeConditional(bool useMutexOn, SemaphoreHandle_t xSemaphore);
|
||||
void semphrGiveConditional(bool useMutexOn, SemaphoreHandle_t xSemaphore);
|
||||
|
||||
/*
|
||||
I want to keep the possibility of using different and independent
|
||||
mutexes for each of the functions that operate on the heap.
|
||||
|
||||
If you want to enable the use of these mutexes remove the defines
|
||||
on lines 30 and 31 and enable the their initialization in setup()
|
||||
*/
|
||||
SemaphoreHandle_t xSemaphoreMalloc = NULL;
|
||||
// SemaphoreHandle_t xSemaphoreRealloc = NULL;
|
||||
// SemaphoreHandle_t xSemaphoreFree = NULL;
|
||||
|
||||
/*
|
||||
A lazy way to use the same mutex for malloc, realloc and free
|
||||
in order to bring us back to the same situation as the MCVE
|
||||
posted here: https://github.com/earlephilhower/arduino-pico/issues/795#issuecomment-1227122082
|
||||
*/
|
||||
#define xSemaphoreRealloc xSemaphoreMalloc
|
||||
#define xSemaphoreFree xSemaphoreMalloc
|
||||
|
||||
const bool useMutexOnMalloc = false;
|
||||
const bool useMutexOnRealloc = false;
|
||||
const bool useMutexOnFree = false;
|
||||
|
||||
/*
|
||||
Enabling this, a realloc will be performed and the string "_realloc"
|
||||
will be concateneted to *tmp
|
||||
*/
|
||||
const bool tryRealloc = true;
|
||||
|
||||
TaskHandle_t loop2Handle = NULL;
|
||||
TaskHandle_t loop3Handle = NULL;
|
||||
TaskHandle_t loop4Handle = NULL;
|
||||
TaskHandle_t loop5Handle = NULL;
|
||||
TaskHandle_t loop6Handle = NULL;
|
||||
TaskHandle_t loop7Handle = NULL;
|
||||
|
||||
void loop2(void *pvPramaters);
|
||||
void loop3(void *pvPramaters);
|
||||
void loop4(void *pvPramaters);
|
||||
void loop5(void *pvPramaters);
|
||||
void loop6(void *pvPramaters);
|
||||
void loop7(void *pvPramaters);
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
|
||||
xSemaphoreMalloc = xSemaphoreCreateMutex();
|
||||
// xSemaphoreRealloc = xSemaphoreCreateMutex();
|
||||
// xSemaphoreFree = xSemaphoreCreateMutex();
|
||||
|
||||
xTaskCreate(loop2, "loop2", STACK_SIZE, NULL, 1, &loop2Handle);
|
||||
vTaskCoreAffinitySet(loop2Handle, CORE_0);
|
||||
xTaskCreate(loop3, "loop3", STACK_SIZE, NULL, 1, &loop3Handle);
|
||||
vTaskCoreAffinitySet(loop3Handle, CORE_1);
|
||||
xTaskCreate(loop4, "loop4", STACK_SIZE, NULL, 1, &loop4Handle);
|
||||
vTaskCoreAffinitySet(loop4Handle, CORE_0);
|
||||
xTaskCreate(loop5, "loop5", STACK_SIZE, NULL, 1, &loop5Handle);
|
||||
vTaskCoreAffinitySet(loop5Handle, CORE_1);
|
||||
xTaskCreate(loop6, "loop6", STACK_SIZE, NULL, 1, &loop6Handle);
|
||||
vTaskCoreAffinitySet(loop6Handle, CORE_0);
|
||||
xTaskCreate(loop7, "loop7", STACK_SIZE, NULL, 1, &loop7Handle);
|
||||
vTaskCoreAffinitySet(loop7Handle, CORE_1);
|
||||
}
|
||||
static int _loop[8];
|
||||
|
||||
void loop()
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
_loop[0]++;
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
delay(500);
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
delay(500);
|
||||
for (int i=0; i<8; i++) Serial.printf("%d ", _loop[i]);
|
||||
Serial.println("");
|
||||
}
|
||||
}
|
||||
|
||||
void loop1()
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
_loop[1]++;
|
||||
char *tmp;
|
||||
|
||||
semphrTakeConditional(useMutexOnMalloc, xSemaphoreMalloc);
|
||||
tmp = (char *)malloc(10 * sizeof(char));
|
||||
semphrGiveConditional(useMutexOnMalloc, xSemaphoreMalloc);
|
||||
|
||||
strcpy(tmp, "foo");
|
||||
|
||||
if (tryRealloc)
|
||||
{
|
||||
semphrTakeConditional(useMutexOnRealloc, xSemaphoreRealloc);
|
||||
tmp = (char *)realloc(tmp, 20 * sizeof(char));
|
||||
semphrGiveConditional(useMutexOnRealloc, xSemaphoreRealloc);
|
||||
strcat(tmp, "_realloc");
|
||||
}
|
||||
|
||||
semphrTakeConditional(useMutexOnFree, xSemaphoreFree);
|
||||
free(tmp);
|
||||
semphrGiveConditional(useMutexOnFree, xSemaphoreFree);
|
||||
|
||||
delay(DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
void loop2(void *pvPramaters)
|
||||
{
|
||||
(void) pvPramaters;
|
||||
while (1)
|
||||
{
|
||||
_loop[2]++;
|
||||
char *tmp;
|
||||
|
||||
semphrTakeConditional(useMutexOnMalloc, xSemaphoreMalloc);
|
||||
tmp = (char *)malloc(10 * sizeof(char));
|
||||
semphrGiveConditional(useMutexOnMalloc, xSemaphoreMalloc);
|
||||
|
||||
strcpy(tmp, "bar");
|
||||
|
||||
if (tryRealloc)
|
||||
{
|
||||
semphrTakeConditional(useMutexOnRealloc, xSemaphoreRealloc);
|
||||
tmp = (char *)realloc(tmp, 20 * sizeof(char));
|
||||
semphrGiveConditional(useMutexOnRealloc, xSemaphoreRealloc);
|
||||
strcat(tmp, "_realloc");
|
||||
}
|
||||
|
||||
semphrTakeConditional(useMutexOnFree, xSemaphoreFree);
|
||||
free(tmp);
|
||||
semphrGiveConditional(useMutexOnFree, xSemaphoreFree);
|
||||
|
||||
delay(DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
void loop3(void *pvPramaters)
|
||||
{
|
||||
(void) pvPramaters;
|
||||
while (1)
|
||||
{
|
||||
_loop[3]++;
|
||||
char *tmp;
|
||||
|
||||
semphrTakeConditional(useMutexOnMalloc, xSemaphoreMalloc);
|
||||
tmp = (char *)malloc(10 * sizeof(char));
|
||||
semphrGiveConditional(useMutexOnMalloc, xSemaphoreMalloc);
|
||||
|
||||
strcpy(tmp, "yeah");
|
||||
|
||||
if (tryRealloc)
|
||||
{
|
||||
semphrTakeConditional(useMutexOnRealloc, xSemaphoreRealloc);
|
||||
tmp = (char *)realloc(tmp, 20 * sizeof(char));
|
||||
semphrGiveConditional(useMutexOnRealloc, xSemaphoreRealloc);
|
||||
strcat(tmp, "_realloc");
|
||||
}
|
||||
|
||||
semphrTakeConditional(useMutexOnFree, xSemaphoreFree);
|
||||
free(tmp);
|
||||
semphrGiveConditional(useMutexOnFree, xSemaphoreFree);
|
||||
|
||||
delay(DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
void loop4(void *pvPramaters)
|
||||
{
|
||||
(void) pvPramaters;
|
||||
while (1)
|
||||
{
|
||||
_loop[4]++;
|
||||
char *tmp;
|
||||
|
||||
semphrTakeConditional(useMutexOnMalloc, xSemaphoreMalloc);
|
||||
tmp = (char *)malloc(10 * sizeof(char));
|
||||
semphrGiveConditional(useMutexOnMalloc, xSemaphoreMalloc);
|
||||
|
||||
strcpy(tmp, "baz");
|
||||
|
||||
if (tryRealloc)
|
||||
{
|
||||
semphrTakeConditional(useMutexOnRealloc, xSemaphoreRealloc);
|
||||
tmp = (char *)realloc(tmp, 20 * sizeof(char));
|
||||
semphrGiveConditional(useMutexOnRealloc, xSemaphoreRealloc);
|
||||
strcat(tmp, "_realloc");
|
||||
}
|
||||
|
||||
semphrTakeConditional(useMutexOnFree, xSemaphoreFree);
|
||||
free(tmp);
|
||||
semphrGiveConditional(useMutexOnFree, xSemaphoreFree);
|
||||
|
||||
delay(DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
void loop5(void *pvPramaters)
|
||||
{
|
||||
(void) pvPramaters;
|
||||
while (1)
|
||||
{
|
||||
_loop[5]++;
|
||||
char *tmp;
|
||||
|
||||
semphrTakeConditional(useMutexOnMalloc, xSemaphoreMalloc);
|
||||
tmp = (char *)malloc(10 * sizeof(char));
|
||||
semphrGiveConditional(useMutexOnMalloc, xSemaphoreMalloc);
|
||||
|
||||
strcpy(tmp, "asd");
|
||||
|
||||
if (tryRealloc)
|
||||
{
|
||||
semphrTakeConditional(useMutexOnRealloc, xSemaphoreRealloc);
|
||||
tmp = (char *)realloc(tmp, 20 * sizeof(char));
|
||||
semphrGiveConditional(useMutexOnRealloc, xSemaphoreRealloc);
|
||||
strcat(tmp, "_realloc");
|
||||
}
|
||||
|
||||
semphrTakeConditional(useMutexOnFree, xSemaphoreFree);
|
||||
free(tmp);
|
||||
semphrGiveConditional(useMutexOnFree, xSemaphoreFree);
|
||||
|
||||
delay(DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
void loop6(void *pvPramaters)
|
||||
{
|
||||
(void) pvPramaters;
|
||||
while (1)
|
||||
{
|
||||
_loop[6]++;
|
||||
char *tmp;
|
||||
|
||||
semphrTakeConditional(useMutexOnMalloc, xSemaphoreMalloc);
|
||||
tmp = (char *)malloc(10 * sizeof(char));
|
||||
semphrGiveConditional(useMutexOnMalloc, xSemaphoreMalloc);
|
||||
|
||||
strcpy(tmp, "lol");
|
||||
|
||||
if (tryRealloc)
|
||||
{
|
||||
semphrTakeConditional(useMutexOnRealloc, xSemaphoreRealloc);
|
||||
tmp = (char *)realloc(tmp, 20 * sizeof(char));
|
||||
semphrGiveConditional(useMutexOnRealloc, xSemaphoreRealloc);
|
||||
strcat(tmp, "_realloc");
|
||||
}
|
||||
|
||||
semphrTakeConditional(useMutexOnFree, xSemaphoreFree);
|
||||
free(tmp);
|
||||
semphrGiveConditional(useMutexOnFree, xSemaphoreFree);
|
||||
|
||||
delay(DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
void loop7(void *pvPramaters)
|
||||
{
|
||||
(void) pvPramaters;
|
||||
while (1)
|
||||
{
|
||||
_loop[7]++;
|
||||
char *tmp;
|
||||
|
||||
semphrTakeConditional(useMutexOnMalloc, xSemaphoreMalloc);
|
||||
tmp = (char *)malloc(10 * sizeof(char));
|
||||
semphrGiveConditional(useMutexOnMalloc, xSemaphoreMalloc);
|
||||
|
||||
strcpy(tmp, "yay");
|
||||
|
||||
if (tryRealloc)
|
||||
{
|
||||
semphrTakeConditional(useMutexOnRealloc, xSemaphoreRealloc);
|
||||
tmp = (char *)realloc(tmp, 20 * sizeof(char));
|
||||
semphrGiveConditional(useMutexOnRealloc, xSemaphoreRealloc);
|
||||
strcat(tmp, "_realloc");
|
||||
}
|
||||
|
||||
semphrTakeConditional(useMutexOnFree, xSemaphoreFree);
|
||||
free(tmp);
|
||||
semphrGiveConditional(useMutexOnFree, xSemaphoreFree);
|
||||
|
||||
delay(DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
void semphrTakeConditional(bool useMutexOn, SemaphoreHandle_t xSemaphore)
|
||||
{
|
||||
if (useMutexOn)
|
||||
{
|
||||
xSemaphoreTake(xSemaphore, TickType_t(portMAX_DELAY));
|
||||
}
|
||||
}
|
||||
|
||||
void semphrGiveConditional(bool useMutexOn, SemaphoreHandle_t xSemaphore)
|
||||
{
|
||||
if (useMutexOn)
|
||||
{
|
||||
xSemaphoreGive(xSemaphore);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "HID_Bluetooth.h"
|
||||
#include <sdkoverride/tusb_gamepad16.h>
|
||||
|
||||
//setup the report map.
|
||||
//more generic function to be used with BLE & BT Classis
|
||||
@@ -6,7 +7,7 @@ void __SetupHIDreportmap(void (*WeakMouse)(), void (*WeakKeyboard)(), void (*Wea
|
||||
//allocate memory for the HID report descriptors. We don't use them, but need the size here.
|
||||
uint8_t desc_hid_report_mouse[] = { TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(1)) };
|
||||
uint8_t desc_hid_report_absmouse[] = { TUD_HID_REPORT_DESC_ABSMOUSE(HID_REPORT_ID(1)) };
|
||||
uint8_t desc_hid_report_joystick[] = { TUD_HID_REPORT_DESC_GAMEPAD(HID_REPORT_ID(1)) };
|
||||
uint8_t desc_hid_report_joystick[] = { TUD_HID_REPORT_DESC_GAMEPAD16(HID_REPORT_ID(1)) };
|
||||
uint8_t desc_hid_report_keyboard[] = { TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1)), TUD_HID_REPORT_DESC_CONSUMER(HID_REPORT_ID(2)) };
|
||||
int size = 0;
|
||||
|
||||
@@ -111,7 +112,7 @@ void __SetupHIDreportmap(void (*WeakMouse)(), void (*WeakKeyboard)(), void (*Wea
|
||||
reportid++;
|
||||
offset += sizeof(desc_hid_report_absmouse);
|
||||
}
|
||||
uint8_t desc_local[] = { TUD_HID_REPORT_DESC_GAMEPAD(HID_REPORT_ID(reportid)) };
|
||||
uint8_t desc_local[] = { TUD_HID_REPORT_DESC_GAMEPAD16(HID_REPORT_ID(reportid)) };
|
||||
memcpy(*reportmap + offset, desc_local, sizeof(desc_local));
|
||||
}
|
||||
|
||||
|
||||
@@ -178,11 +178,13 @@ public:
|
||||
while (connected() && _needToSend) {
|
||||
/* noop busy wait */
|
||||
}
|
||||
_needToSend = true;
|
||||
_sendReport = rpt;
|
||||
_sendReportLen = len;
|
||||
__lockBluetooth();
|
||||
hids_device_request_can_send_now_event(_con_handle);
|
||||
if (connected()) {
|
||||
_needToSend = true;
|
||||
_sendReport = rpt;
|
||||
_sendReportLen = len;
|
||||
hids_device_request_can_send_now_event(_con_handle);
|
||||
}
|
||||
__unlockBluetooth();
|
||||
while (connected() && _needToSend) {
|
||||
/* noop busy wait */
|
||||
|
||||
@@ -142,12 +142,14 @@ public:
|
||||
}
|
||||
|
||||
bool send(int id, void *rpt, int len) {
|
||||
_needToSend = true;
|
||||
_sendReportID = id;
|
||||
_sendReport = rpt;
|
||||
_sendReportLen = len;
|
||||
__lockBluetooth();
|
||||
hid_device_request_can_send_now_event(getCID());
|
||||
if (connected()) {
|
||||
_needToSend = true;
|
||||
_sendReportID = id;
|
||||
_sendReport = rpt;
|
||||
_sendReportLen = len;
|
||||
hid_device_request_can_send_now_event(getCID());
|
||||
}
|
||||
__unlockBluetooth();
|
||||
while (connected() && _needToSend) {
|
||||
/* noop busy wait */
|
||||
|
||||
+1
-1
Submodule libraries/HID_Joystick updated: 063dbde1ae...be266c0252
@@ -35,9 +35,9 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#ifdef DEBUG_ESP_HTTP_CLIENT
|
||||
#ifdef DEBUG_ESP_PORT
|
||||
#define DEBUG_HTTPCLIENT(fmt, ...) DEBUG_ESP_PORT.printf_P( (PGM_P)PSTR(fmt), ## __VA_ARGS__ )
|
||||
#ifdef DEBUG_RP2040_CORE
|
||||
#ifdef DEBUG_RP2040_PORT
|
||||
#define DEBUG_HTTPCLIENT(fmt, ...) DEBUG_RP2040_PORT.printf_P( (PGM_P)PSTR(fmt), ## __VA_ARGS__ )
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -179,7 +179,11 @@ typedef std::vector<Cookie> CookieJar;
|
||||
class HTTPClient {
|
||||
public:
|
||||
HTTPClient() = default;
|
||||
~HTTPClient() = default;
|
||||
~HTTPClient() {
|
||||
if (_clientMade) {
|
||||
delete _clientMade;
|
||||
}
|
||||
}
|
||||
HTTPClient(HTTPClient&&) = default;
|
||||
HTTPClient& operator=(HTTPClient&&) = default;
|
||||
|
||||
|
||||
@@ -31,9 +31,9 @@
|
||||
#include <WiFiUdp.h>
|
||||
#include <HTTPClient.h>
|
||||
|
||||
#ifdef DEBUG_ESP_HTTP_UPDATE
|
||||
#ifdef DEBUG_ESP_PORT
|
||||
#define DEBUG_HTTP_UPDATE(fmt, ...) DEBUG_ESP_PORT.printf_P( (PGM_P)PSTR(fmt), ## __VA_ARGS__ )
|
||||
#ifdef DEBUG_RP2040_CORE
|
||||
#ifdef DEBUG_RP2040_PORT
|
||||
#define DEBUG_HTTP_UPDATE(fmt, ...) DEBUG_RP2040_PORT.printf_P( (PGM_P)PSTR(fmt), ## __VA_ARGS__ )
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
@@ -156,8 +156,6 @@ Switch axis value range between 10bit and 8bit.
|
||||
* Default: 10bit, range for an axis from 0 to 1023
|
||||
* 8bit mode: range from -127 to 127.
|
||||
|
||||
__Note:__ due to the gamepad descriptor of tinyUSB, the maximum range is -127/127. 10bit mode enables mapping, not a higher resolution.
|
||||
|
||||
|
||||
#### Syntax
|
||||
|
||||
@@ -216,6 +214,11 @@ void loop() {
|
||||
* [Joystick.sliderRight()](#joysticksliderright)
|
||||
|
||||
|
||||
### `Joystick.use10bit()`
|
||||
### `Joystick.use16bit()`
|
||||
|
||||
Set axis value range to 10-bit (0...1024) or 16-bit (-32767...32767).
|
||||
|
||||
### `Joystick.useManualSend()`
|
||||
|
||||
To fully control transmitting the USB-HID reports, enable manual sending.
|
||||
|
||||
@@ -33,8 +33,8 @@ void loop() {
|
||||
}
|
||||
Joystick.useManualSend(false);
|
||||
|
||||
Joystick.use10bit();
|
||||
// Iterate all joystick axis
|
||||
// Note: although you can use 0-1023 here (10bit), internally 8bits are used (-127 to 127)
|
||||
Serial.println("Joystick X");
|
||||
for (uint16_t i = 0; i < 1023; i++) {
|
||||
Joystick.X(i);
|
||||
@@ -74,7 +74,7 @@ void loop() {
|
||||
// Use int8 mode for the axis.
|
||||
// Note: hat is not used differently.
|
||||
Serial.println("Now all axis in 8bit mode, -127 to 127");
|
||||
Joystick.use8bit(true);
|
||||
Joystick.use8bit();
|
||||
Serial.println("Joystick X");
|
||||
for (int16_t i = -127; i < 128; i++) {
|
||||
Joystick.X(i);
|
||||
@@ -105,6 +105,6 @@ void loop() {
|
||||
Joystick.sliderRight(i);
|
||||
delay(2);
|
||||
} Joystick.sliderRight(0);
|
||||
Joystick.use8bit(false);
|
||||
Joystick.use10bit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#include <Joystick.h>
|
||||
|
||||
// Set 1KHz polling frequency (1000/second)
|
||||
int usb_hid_poll_interval = 1;
|
||||
|
||||
void setup() {
|
||||
Joystick.use16bit();
|
||||
Joystick.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
static int16_t delta = 1;
|
||||
static int16_t p = 0;
|
||||
if (p == 32767) {
|
||||
delta = -1;
|
||||
} else if (p == -32767) {
|
||||
delta = 1;
|
||||
}
|
||||
p += delta;
|
||||
Joystick.X(p);
|
||||
Joystick.Y(-p);
|
||||
}
|
||||
@@ -65,7 +65,7 @@ void JoystickBLE_::setBattery(int lvl) {
|
||||
|
||||
void JoystickBLE_::send_now() {
|
||||
//insert report ID; not part of the hid_gamepad_report_t
|
||||
uint8_t *report = (uint8_t *)malloc(sizeof(hid_gamepad_report_t) +1);
|
||||
uint8_t *report = (uint8_t *)malloc(sizeof(hid_gamepad16_report_t) + 1);
|
||||
if (report) {
|
||||
report[0] = __BLEGetJoystickReportID();
|
||||
memcpy(&report[1], (uint8_t*)&data, sizeof(data));
|
||||
|
||||
@@ -68,10 +68,10 @@ void KeyboardBLE_::sendReport(KeyReport* keys) {
|
||||
memcpy(data.keycode, keys->keys, sizeof(data.keycode));
|
||||
|
||||
//stitch in report id
|
||||
static uint8_t report[sizeof(hid_keyboard_report_t) +1];
|
||||
static uint8_t report[sizeof(hid_keyboard_report_t) + 1];
|
||||
report[0] = __BLEGetKeyboardReportID();
|
||||
memcpy(&report[1], (uint8_t*)&data, sizeof(hid_keyboard_report_t));
|
||||
PicoBluetoothBLEHID.send(&report, sizeof(hid_keyboard_report_t) +1);
|
||||
PicoBluetoothBLEHID.send(&report, sizeof(hid_keyboard_report_t) + 1);
|
||||
}
|
||||
|
||||
void KeyboardBLE_::sendConsumerReport(uint16_t key) {
|
||||
|
||||
@@ -68,7 +68,7 @@ void MouseBLE_::setAbsolute(bool absolute) {
|
||||
}
|
||||
|
||||
void MouseBLE_::move(int x, int y, signed char wheel) {
|
||||
static uint8_t report[sizeof(hid_abs_mouse_report_t) +1];
|
||||
static uint8_t report[sizeof(hid_abs_mouse_report_t) + 1];
|
||||
|
||||
if (!_absolute) {
|
||||
hid_mouse_report_t data;
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
// Modified from the ESP32-NetBIOS example
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <NetBIOS.h>
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char* ssid = STASSID;
|
||||
const char* password = STAPSK;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
// Connect to WiFi network
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
Serial.println("");
|
||||
|
||||
// Wait for connection
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.print("Connected to ");
|
||||
Serial.println(ssid);
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
// Windows (and other OSes using WINS/NetBIOS resolution should now be able to find the device by name)
|
||||
NBNS.begin("PicoW");
|
||||
}
|
||||
|
||||
void loop() {}
|
||||
Executable
+25
@@ -0,0 +1,25 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map For ESPNBNS
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
NetBIOS KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
begin KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Instances (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
NBNS KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
@@ -0,0 +1,9 @@
|
||||
name=NetBIOS
|
||||
version=2.0.0
|
||||
author=Pablo@xpablo.cz
|
||||
maintainer=Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
sentence=Enables NBNS (NetBIOS) name resolution.
|
||||
paragraph=With this library you can connect to your ESP from Windows using a short name
|
||||
category=Communication
|
||||
url=http://www.xpablo.cz/?p=751#more-751
|
||||
architectures=rp2040
|
||||
@@ -0,0 +1,132 @@
|
||||
#include "NetBIOS.h"
|
||||
#include <functional>
|
||||
#include <lwip/ip.h>
|
||||
#include <lwip/netif.h>
|
||||
|
||||
#define NBNS_PORT 137
|
||||
#define NBNS_MAX_HOSTNAME_LEN 32
|
||||
|
||||
typedef struct {
|
||||
uint16_t id;
|
||||
uint8_t flags1;
|
||||
uint8_t flags2;
|
||||
uint16_t qcount;
|
||||
uint16_t acount;
|
||||
uint16_t nscount;
|
||||
uint16_t adcount;
|
||||
uint8_t name_len;
|
||||
char name[NBNS_MAX_HOSTNAME_LEN + 1];
|
||||
uint16_t type;
|
||||
uint16_t clas;
|
||||
} __attribute__((packed)) nbns_question_t;
|
||||
|
||||
typedef struct {
|
||||
uint16_t id;
|
||||
uint8_t flags1;
|
||||
uint8_t flags2;
|
||||
uint16_t qcount;
|
||||
uint16_t acount;
|
||||
uint16_t nscount;
|
||||
uint16_t adcount;
|
||||
uint8_t name_len;
|
||||
char name[NBNS_MAX_HOSTNAME_LEN + 1];
|
||||
uint16_t type;
|
||||
uint16_t clas;
|
||||
uint32_t ttl;
|
||||
uint16_t data_len;
|
||||
uint16_t flags;
|
||||
uint32_t addr;
|
||||
} __attribute__((packed)) nbns_answer_t;
|
||||
|
||||
static void _getnbname(const char *nbname, char *name, uint8_t maxlen) {
|
||||
uint8_t b;
|
||||
uint8_t c = 0;
|
||||
|
||||
while ((*nbname) && (c < maxlen)) {
|
||||
b = (*nbname++ - 'A') << 4;
|
||||
c++;
|
||||
if (*nbname) {
|
||||
b |= *nbname++ - 'A';
|
||||
c++;
|
||||
}
|
||||
if (!b || b == ' ') {
|
||||
break;
|
||||
}
|
||||
*name++ = b;
|
||||
}
|
||||
*name = 0;
|
||||
}
|
||||
|
||||
static void append_16(void *dst, uint16_t value) {
|
||||
uint8_t *d = (uint8_t *)dst;
|
||||
*d++ = (value >> 8) & 0xFF;
|
||||
*d++ = value & 0xFF;
|
||||
}
|
||||
|
||||
static void append_32(void *dst, uint32_t value) {
|
||||
uint8_t *d = (uint8_t *)dst;
|
||||
*d++ = (value >> 24) & 0xFF;
|
||||
*d++ = (value >> 16) & 0xFF;
|
||||
*d++ = (value >> 8) & 0xFF;
|
||||
*d++ = value & 0xFF;
|
||||
}
|
||||
|
||||
void NetBIOS::_onPacket(AsyncUDPPacket &packet) {
|
||||
if (packet.length() >= sizeof(nbns_question_t)) {
|
||||
nbns_question_t *question = (nbns_question_t *)packet.data();
|
||||
if (0 == (question->flags1 & 0x80)) {
|
||||
char name[NBNS_MAX_HOSTNAME_LEN + 1];
|
||||
_getnbname(&question->name[0], (char *)&name, question->name_len);
|
||||
if (_name.equals(name)) {
|
||||
nbns_answer_t nbnsa;
|
||||
nbnsa.id = question->id;
|
||||
nbnsa.flags1 = 0x85;
|
||||
nbnsa.flags2 = 0;
|
||||
append_16((void *)&nbnsa.qcount, 0);
|
||||
append_16((void *)&nbnsa.acount, 1);
|
||||
append_16((void *)&nbnsa.nscount, 0);
|
||||
append_16((void *)&nbnsa.adcount, 0);
|
||||
nbnsa.name_len = question->name_len;
|
||||
memcpy(&nbnsa.name[0], &question->name[0], question->name_len + 1);
|
||||
append_16((void *)&nbnsa.type, 0x20);
|
||||
append_16((void *)&nbnsa.clas, 1);
|
||||
append_32((void *)&nbnsa.ttl, 300000);
|
||||
append_16((void *)&nbnsa.data_len, 6);
|
||||
append_16((void *)&nbnsa.flags, 0);
|
||||
nbnsa.addr = ip4_addr_get_u32(&ip_current_netif()->ip_addr);
|
||||
_udp.writeTo((uint8_t *)&nbnsa, sizeof(nbnsa), packet.remoteIP(), NBNS_PORT);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NetBIOS::NetBIOS() {}
|
||||
|
||||
NetBIOS::~NetBIOS() {
|
||||
end();
|
||||
}
|
||||
|
||||
bool NetBIOS::begin(const char *name) {
|
||||
_name = name;
|
||||
_name.toUpperCase();
|
||||
|
||||
if (_udp.connected()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
_udp.onPacket(
|
||||
[](void *arg, AsyncUDPPacket & packet) {
|
||||
((NetBIOS *)(arg))->_onPacket(packet);
|
||||
},
|
||||
this
|
||||
);
|
||||
return _udp.listen(NBNS_PORT);
|
||||
}
|
||||
|
||||
void NetBIOS::end() {
|
||||
if (_udp.connected()) {
|
||||
_udp.close();
|
||||
}
|
||||
}
|
||||
|
||||
NetBIOS NBNS;
|
||||
@@ -0,0 +1,21 @@
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <AsyncUDP.h>
|
||||
|
||||
class NetBIOS {
|
||||
protected:
|
||||
AsyncUDP _udp;
|
||||
String _name;
|
||||
void _onPacket(AsyncUDPPacket &packet);
|
||||
|
||||
public:
|
||||
NetBIOS();
|
||||
~NetBIOS();
|
||||
bool begin(const char *name);
|
||||
void end();
|
||||
};
|
||||
|
||||
extern NetBIOS NBNS;
|
||||
@@ -242,8 +242,8 @@ size_t PWMAudio::write(const uint8_t *buffer, size_t size) {
|
||||
return writtenSize;
|
||||
} else {
|
||||
p++;
|
||||
size -= 4;
|
||||
writtenSize += 4;
|
||||
size -= 2;
|
||||
writtenSize += 2;
|
||||
}
|
||||
}
|
||||
return writtenSize;
|
||||
@@ -288,4 +288,4 @@ void PWMAudio::find_pacer_fraction(int target, uint16_t *numerator, uint16_t *de
|
||||
last_target = target;
|
||||
*numerator = bestNum;
|
||||
*denominator = bestDenom;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
SD.h - A thin shim for Arduino ESP8266 Filesystems
|
||||
SD.h - A thin shim for Arduino RP2040 Filesystems
|
||||
Copyright (c) 2019 Earle F. Philhower, III. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
@@ -43,8 +43,9 @@ public:
|
||||
|
||||
void end(bool endSPI = true) {
|
||||
SDFS.end();
|
||||
if (endSPI) {
|
||||
if (endSPI && _spi) {
|
||||
_spi->end();
|
||||
_spi = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,9 +139,9 @@ public:
|
||||
|
||||
size_t size() {
|
||||
uint64_t sz = size64();
|
||||
#ifdef DEBUG_ESP_PORT
|
||||
#ifdef DEBUG_RP2040_PORT
|
||||
if (sz > std::numeric_limits<uint32_t>::max()) {
|
||||
DEBUG_ESP_PORT.printf_P(PSTR("WARNING: SD card size overflow (%lld >= 4GB). Please update source to use size64().\n"), (long long)sz);
|
||||
DEBUG_RP2040_PORT.printf_P(PSTR("WARNING: SD card size overflow (%lld >= 4GB). Please update source to use size64().\n"), (long long)sz);
|
||||
}
|
||||
#endif
|
||||
return (size_t)sz;
|
||||
@@ -205,7 +206,7 @@ private:
|
||||
return time(nullptr);
|
||||
}
|
||||
|
||||
HardwareSPI *_spi;
|
||||
HardwareSPI *_spi = nullptr;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -115,10 +115,10 @@ public:
|
||||
info.pageSize = i.pageSize;
|
||||
info.maxOpenFiles = i.maxOpenFiles;
|
||||
info.maxPathLength = i.maxPathLength;
|
||||
#ifdef DEBUG_ESP_PORT
|
||||
#ifdef DEBUG_RP2040_PORT
|
||||
if (i.totalBytes > std::numeric_limits<uint32_t>::max()) {
|
||||
// This catches both total and used cases, since used must always be < total.
|
||||
DEBUG_ESP_PORT.printf_P(PSTR("WARNING: SD card size overflow (%lld >= 4GB). Please update source to use info64().\n"), (long long)i.totalBytes);
|
||||
DEBUG_RP2040_PORT.printf_P(PSTR("WARNING: SD card size overflow (%lld >= 4GB). Please update source to use info64().\n"), (long long)i.totalBytes);
|
||||
}
|
||||
#endif
|
||||
info.totalBytes = (size_t)i.totalBytes;
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <Arduino.h>
|
||||
#include <Ticker.h>
|
||||
|
||||
#define LED_PIN LED_BUILTIN
|
||||
|
||||
Ticker blinker;
|
||||
Ticker toggler;
|
||||
Ticker changer;
|
||||
float blinkerPace = 0.1; //seconds
|
||||
const float togglePeriod = 5; //seconds
|
||||
|
||||
void change() {
|
||||
blinkerPace = 0.5;
|
||||
}
|
||||
|
||||
void blink() {
|
||||
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
|
||||
}
|
||||
|
||||
void toggle() {
|
||||
static bool isBlinking = false;
|
||||
if (isBlinking) {
|
||||
blinker.detach();
|
||||
isBlinking = false;
|
||||
} else {
|
||||
blinker.attach(blinkerPace, blink);
|
||||
isBlinking = true;
|
||||
}
|
||||
digitalWrite(LED_PIN, LOW); //make sure LED on on after toggling (pin LOW = led ON)
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
toggler.attach(togglePeriod, toggle);
|
||||
changer.once(30, change);
|
||||
}
|
||||
|
||||
void loop() {}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
Basic Ticker usage
|
||||
|
||||
Ticker is an object that will call a given function with a certain period.
|
||||
Each Ticker calls one function. You can have as many Tickers as you like,
|
||||
memory being the only limitation.
|
||||
|
||||
A function may be attached to a ticker and detached from the ticker.
|
||||
There are two variants of the attach function: attach and attach_ms.
|
||||
The first one takes period in seconds, the second one in milliseconds.
|
||||
|
||||
The built-in LED will be blinking.
|
||||
*/
|
||||
|
||||
#include <Ticker.h>
|
||||
|
||||
#ifndef LED_BUILTIN
|
||||
#define LED_BUILTIN 13
|
||||
#endif
|
||||
|
||||
Ticker flipper;
|
||||
|
||||
int count = 0;
|
||||
|
||||
void flip() {
|
||||
int state = digitalRead(LED_BUILTIN); // get the current state of GPIO1 pin
|
||||
digitalWrite(LED_BUILTIN, !state); // set pin to the opposite state
|
||||
|
||||
++count;
|
||||
// when the counter reaches a certain value, start blinking like crazy
|
||||
if (count == 20) {
|
||||
flipper.attach(0.1, flip);
|
||||
}
|
||||
// when the counter reaches yet another value, stop blinking
|
||||
else if (count == 120) {
|
||||
flipper.detach();
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
|
||||
// flip the pin every 0.3s
|
||||
flipper.attach(0.3, flip);
|
||||
}
|
||||
|
||||
void loop() {}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
Passing parameters to Ticker callbacks
|
||||
|
||||
Apart from void(void) functions, the Ticker library supports
|
||||
functions taking one argument. This argument's size has to be less or
|
||||
equal to 4 bytes (so char, short, int, float, void*, char* types will do).
|
||||
|
||||
This sample runs two tickers that both call one callback function,
|
||||
but with different arguments.
|
||||
|
||||
The built-in LED will be pulsing.
|
||||
*/
|
||||
|
||||
#include <Ticker.h>
|
||||
|
||||
#ifndef LED_BUILTIN
|
||||
#define LED_BUILTIN 13
|
||||
#endif
|
||||
|
||||
Ticker tickerSetLow;
|
||||
Ticker tickerSetHigh;
|
||||
Ticker tickerSetChar;
|
||||
|
||||
void setPinLow() {
|
||||
digitalWrite(LED_BUILTIN, 0);
|
||||
}
|
||||
|
||||
void setPinHigh() {
|
||||
digitalWrite(LED_BUILTIN, 1);
|
||||
}
|
||||
|
||||
void setPin(int state) {
|
||||
digitalWrite(LED_BUILTIN, state);
|
||||
}
|
||||
|
||||
void setPinChar(char state) {
|
||||
digitalWrite(LED_BUILTIN, state);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
|
||||
// every 25 ms, call setPinLow()
|
||||
tickerSetLow.attach_ms(25, setPinLow);
|
||||
|
||||
// every 26 ms, call setPinHigh()
|
||||
tickerSetHigh.attach_ms(26, setPinHigh);
|
||||
|
||||
// every 54 ms, call setPinChar(1)
|
||||
tickerSetChar.attach_ms(26, setPinChar, (char)1);
|
||||
}
|
||||
|
||||
void loop() {}
|
||||
@@ -0,0 +1,18 @@
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
Ticker KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
attach KEYWORD2
|
||||
attach_ms KEYWORD2
|
||||
attach_us KEYWORD2
|
||||
once KEYWORD2
|
||||
once_ms KEYWORD2
|
||||
once_us KEYWORD2
|
||||
detach KEYWORD2
|
||||
active KEYWORD2
|
||||
@@ -0,0 +1,9 @@
|
||||
name=Ticker
|
||||
version=2.0.0
|
||||
author=Bert Melis
|
||||
maintainer=Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
sentence=Allows to call functions with a given interval.
|
||||
paragraph=
|
||||
category=Timing
|
||||
url=
|
||||
architectures=rp2040
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
Ticker.cpp - RP2040 library that calls functions periodically
|
||||
Ported 2024 to the RP2040 by Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
Copyright (c) 2017 Bert Melis. All rights reserved.
|
||||
|
||||
Based on the original work of:
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
The original version is part of the esp8266 core for Arduino environment.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "Ticker.h"
|
||||
|
||||
Ticker::Ticker() : _callback_function(nullptr), _callback_with_arg(nullptr), _alarm_id(-1), _repeating_timer(nullptr) { }
|
||||
|
||||
Ticker::~Ticker() {
|
||||
detach();
|
||||
}
|
||||
|
||||
bool Ticker::_attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void *arg) {
|
||||
detach();
|
||||
_callback_with_arg = callback;
|
||||
_callback_arg = arg;
|
||||
if (repeat) {
|
||||
_repeating_timer = new repeating_timer_t;
|
||||
_repeating_timer->user_data = (void *)this;
|
||||
return add_repeating_timer_us(micros, _repeating_callback, this, _repeating_timer);
|
||||
} else {
|
||||
_alarm_id = add_alarm_in_us(micros, _alarm_callback, this, true);
|
||||
return _alarm_id != -1;
|
||||
}
|
||||
}
|
||||
|
||||
void Ticker::detach() {
|
||||
if (_repeating_timer) {
|
||||
cancel_repeating_timer(_repeating_timer);
|
||||
delete _repeating_timer;
|
||||
} else if (_alarm_id != -1) {
|
||||
cancel_alarm(_alarm_id);
|
||||
}
|
||||
_repeating_timer = nullptr;
|
||||
_alarm_id = -1;
|
||||
_callback_with_arg = nullptr;
|
||||
}
|
||||
|
||||
bool Ticker::active() const {
|
||||
if (!_repeating_timer && (_alarm_id == -1)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Ticker::_static_callback(void *arg) {
|
||||
Ticker *_this = reinterpret_cast<Ticker *>(arg);
|
||||
if (_this && _this->_callback_function) {
|
||||
_this->_callback_function();
|
||||
}
|
||||
}
|
||||
|
||||
bool Ticker::_repeating_callback(repeating_timer_t *arg) {
|
||||
Ticker *_this = reinterpret_cast<Ticker *>(arg->user_data);
|
||||
if (_this && _this->_callback_function) {
|
||||
_this->_callback_with_arg(_this->_callback_arg);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int64_t Ticker::_alarm_callback(alarm_id_t id, void *user_data) {
|
||||
(void) id;
|
||||
Ticker *_this = reinterpret_cast<Ticker *>(user_data);
|
||||
if (_this && _this->_callback_function) {
|
||||
_this->_callback_with_arg(_this->_callback_arg);
|
||||
}
|
||||
_this->_alarm_id = -1;
|
||||
_this->_callback_with_arg = nullptr;
|
||||
return 0; // These are only a 1-shot
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
Ticker.h - esp32 library that calls functions periodically
|
||||
Ported 2024 to the RP2040 by Earle F. Philhower, III <earlephilhower@yahoo.com>
|
||||
|
||||
Copyright (c) 2017 Bert Melis. All rights reserved.
|
||||
|
||||
Based on the original work of:
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
The original version is part of the esp8266 core for Arduino environment.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <functional>
|
||||
|
||||
// The templated function casting <4 bytes to a 4-byte variable causes
|
||||
// warnings on GCC 12 (but no actual problem since the smallest arg
|
||||
// that can be physically passed is 32-bits). Disable that specific
|
||||
// warning for this file only.
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wcast-function-type"
|
||||
|
||||
class Ticker {
|
||||
public:
|
||||
Ticker();
|
||||
~Ticker();
|
||||
|
||||
typedef void (*callback_with_arg_t)(void *);
|
||||
typedef std::function<void(void)> callback_function_t;
|
||||
|
||||
bool attach(float seconds, callback_function_t callback) {
|
||||
_callback_function = std::move(callback);
|
||||
return _attach_us(1000000ULL * seconds, true, _static_callback, this);
|
||||
}
|
||||
|
||||
bool attach_ms(uint64_t milliseconds, callback_function_t callback) {
|
||||
_callback_function = std::move(callback);
|
||||
return _attach_us(1000ULL * milliseconds, true, _static_callback, this);
|
||||
}
|
||||
|
||||
bool attach_us(uint64_t micros, callback_function_t callback) {
|
||||
_callback_function = std::move(callback);
|
||||
return _attach_us(micros, true, _static_callback, this);
|
||||
}
|
||||
|
||||
template<typename TArg> bool attach(float seconds, void (*callback)(TArg), TArg arg) {
|
||||
static_assert(sizeof(TArg) <= sizeof(void *), "attach() callback argument size must be <= sizeof(void*)");
|
||||
// C-cast serves two purposes:
|
||||
// static_cast for smaller integer types,
|
||||
// reinterpret_cast + const_cast for pointer types
|
||||
return _attach_us(1000000ULL * seconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
|
||||
}
|
||||
|
||||
template<typename TArg> bool attach_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg) {
|
||||
static_assert(sizeof(TArg) <= sizeof(void *), "attach() callback argument size must be <= sizeof(void*)");
|
||||
return _attach_us(1000ULL * milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
|
||||
}
|
||||
|
||||
template<typename TArg> bool attach_us(uint64_t micros, void (*callback)(TArg), TArg arg) {
|
||||
static_assert(sizeof(TArg) <= sizeof(void *), "attach() callback argument size must be <= sizeof(void*)");
|
||||
return _attach_us(micros, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
|
||||
}
|
||||
|
||||
bool once(float seconds, callback_function_t callback) {
|
||||
_callback_function = std::move(callback);
|
||||
return _attach_us(1000000ULL * seconds, false, _static_callback, this);
|
||||
}
|
||||
|
||||
bool once_ms(uint64_t milliseconds, callback_function_t callback) {
|
||||
_callback_function = std::move(callback);
|
||||
return _attach_us(1000ULL * milliseconds, false, _static_callback, this);
|
||||
}
|
||||
|
||||
bool once_us(uint64_t micros, callback_function_t callback) {
|
||||
_callback_function = std::move(callback);
|
||||
return _attach_us(micros, false, _static_callback, this);
|
||||
}
|
||||
|
||||
template<typename TArg> bool once(float seconds, void (*callback)(TArg), TArg arg) {
|
||||
static_assert(sizeof(TArg) <= sizeof(void *), "attach() callback argument size must be <= sizeof(void*)");
|
||||
return _attach_us(1000000ULL * seconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
|
||||
}
|
||||
|
||||
template<typename TArg> bool once_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg) {
|
||||
static_assert(sizeof(TArg) <= sizeof(void *), "attach() callback argument size must be <= sizeof(void*)");
|
||||
return _attach_us(1000ULL * milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
|
||||
}
|
||||
|
||||
template<typename TArg> bool once_us(uint64_t micros, void (*callback)(TArg), TArg arg) {
|
||||
static_assert(sizeof(TArg) <= sizeof(void *), "attach() callback argument size must be <= sizeof(void*)");
|
||||
return _attach_us(micros, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
|
||||
}
|
||||
|
||||
void detach();
|
||||
bool active() const;
|
||||
|
||||
protected:
|
||||
static void _static_callback(void *arg);
|
||||
static bool _repeating_callback(repeating_timer_t *arg);
|
||||
static int64_t _alarm_callback(alarm_id_t id, void *user_data);
|
||||
|
||||
// No arguments
|
||||
callback_function_t _callback_function = nullptr;
|
||||
// Arguments
|
||||
callback_with_arg_t _callback_with_arg = nullptr;
|
||||
void * _callback_arg;
|
||||
|
||||
alarm_id_t _alarm_id;
|
||||
repeating_timer_t *_repeating_timer;
|
||||
|
||||
private:
|
||||
bool _attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void *arg);
|
||||
};
|
||||
#pragma GCC diagnostic pop
|
||||
@@ -0,0 +1,100 @@
|
||||
#include <WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <WebServer.h>
|
||||
#include <LEAmDNS.h>
|
||||
|
||||
// Your STA WiFi Credentials
|
||||
// ( This is the AP your RP2040 will connect to )
|
||||
const char *ssid = "........";
|
||||
const char *password = "........";
|
||||
|
||||
// Your AP WiFi Credentials
|
||||
// ( This is the AP your RP2040 will broadcast )
|
||||
const char *ap_ssid = "RP2040_Demo";
|
||||
const char *ap_password = "";
|
||||
|
||||
WebServer server(80);
|
||||
|
||||
const int led = LED_BUILTIN;
|
||||
|
||||
// ON_STA_FILTER - Only accept requests coming from STA interface
|
||||
bool ON_STA_FILTER(HTTPServer &server) {
|
||||
return WiFi.localIP() == server.client().localIP();
|
||||
}
|
||||
|
||||
// ON_AP_FILTER - Only accept requests coming from AP interface
|
||||
bool ON_AP_FILTER(HTTPServer &server) {
|
||||
return WiFi.softAPIP() == server.client().localIP();
|
||||
}
|
||||
|
||||
void handleNotFound() {
|
||||
digitalWrite(led, 1);
|
||||
String message = "File Not Found\n\n";
|
||||
message += "URI: ";
|
||||
message += server.uri();
|
||||
message += "\nMethod: ";
|
||||
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
||||
message += "\nArguments: ";
|
||||
message += server.args();
|
||||
message += "\n";
|
||||
for (uint8_t i = 0; i < server.args(); i++) {
|
||||
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
||||
}
|
||||
server.send(404, "text/plain", message);
|
||||
digitalWrite(led, 0);
|
||||
}
|
||||
|
||||
void setup(void) {
|
||||
pinMode(led, OUTPUT);
|
||||
digitalWrite(led, 0);
|
||||
Serial.begin(115200);
|
||||
WiFi.mode(WIFI_AP_STA);
|
||||
// Connect to STA
|
||||
WiFi.begin(ssid, password);
|
||||
// Start AP
|
||||
WiFi.softAP(ap_ssid, ap_password);
|
||||
Serial.println("");
|
||||
|
||||
// Wait for connection
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.print("Connected to ");
|
||||
Serial.println(ssid);
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
if (MDNS.begin("picow")) {
|
||||
Serial.println("MDNS responder started");
|
||||
}
|
||||
|
||||
// This route will be accessible by STA clients only
|
||||
server.on("/", [&]() {
|
||||
digitalWrite(led, 1);
|
||||
server.send(200, "text/plain", "Hi!, This route is accessible for STA clients only");
|
||||
digitalWrite(led, 0);
|
||||
}).setFilter(ON_STA_FILTER);
|
||||
|
||||
// This route will be accessible by AP clients only
|
||||
server.on("/", [&]() {
|
||||
digitalWrite(led, 1);
|
||||
server.send(200, "text/plain", "Hi!, This route is accessible for AP clients only");
|
||||
digitalWrite(led, 0);
|
||||
}).setFilter(ON_AP_FILTER);
|
||||
|
||||
server.on("/inline", []() {
|
||||
server.send(200, "text/plain", "this works as well");
|
||||
});
|
||||
|
||||
server.onNotFound(handleNotFound);
|
||||
|
||||
server.begin();
|
||||
Serial.println("HTTP server started");
|
||||
}
|
||||
|
||||
void loop(void) {
|
||||
server.handleClient();
|
||||
MDNS.update();
|
||||
}
|
||||
@@ -207,22 +207,65 @@ void HTTPServer::requestAuthentication(HTTPAuthMethod mode, const char* realm, c
|
||||
send(401, String(FPSTR(mimeTable[html].mimeType)), authFailMsg);
|
||||
}
|
||||
|
||||
void HTTPServer::on(const Uri &uri, HTTPServer::THandlerFunction handler) {
|
||||
on(uri, HTTP_ANY, handler);
|
||||
RequestHandler& HTTPServer::on(const Uri &uri, HTTPServer::THandlerFunction handler) {
|
||||
return on(uri, HTTP_ANY, handler);
|
||||
}
|
||||
|
||||
void HTTPServer::on(const Uri &uri, HTTPMethod method, HTTPServer::THandlerFunction fn) {
|
||||
on(uri, method, fn, _fileUploadHandler);
|
||||
RequestHandler& HTTPServer::on(const Uri &uri, HTTPMethod method, HTTPServer::THandlerFunction fn) {
|
||||
return on(uri, method, fn, _fileUploadHandler);
|
||||
}
|
||||
|
||||
void HTTPServer::on(const Uri &uri, HTTPMethod method, HTTPServer::THandlerFunction fn, HTTPServer::THandlerFunction ufn) {
|
||||
_addRequestHandler(new FunctionRequestHandler(fn, ufn, uri, method));
|
||||
RequestHandler& HTTPServer::on(const Uri &uri, HTTPMethod method, HTTPServer::THandlerFunction fn, HTTPServer::THandlerFunction ufn) {
|
||||
FunctionRequestHandler *handler = new FunctionRequestHandler(fn, ufn, uri, method);
|
||||
_addRequestHandler(handler);
|
||||
return *handler;
|
||||
}
|
||||
|
||||
bool HTTPServer::removeRoute(const char *uri) {
|
||||
return removeRoute(String(uri), HTTP_ANY);
|
||||
}
|
||||
|
||||
bool HTTPServer::removeRoute(const char *uri, HTTPMethod method) {
|
||||
return removeRoute(String(uri), method);
|
||||
}
|
||||
|
||||
bool HTTPServer::removeRoute(const String &uri) {
|
||||
return removeRoute(uri, HTTP_ANY);
|
||||
}
|
||||
|
||||
bool HTTPServer::removeRoute(const String &uri, HTTPMethod method) {
|
||||
bool anyHandlerRemoved = false;
|
||||
RequestHandler *handler = _firstHandler;
|
||||
RequestHandler *previousHandler = nullptr;
|
||||
|
||||
while (handler) {
|
||||
if (handler->canHandle(method, uri)) {
|
||||
if (_removeRequestHandler(handler)) {
|
||||
anyHandlerRemoved = true;
|
||||
// Move to the next handler
|
||||
if (previousHandler) {
|
||||
handler = previousHandler->next();
|
||||
} else {
|
||||
handler = _firstHandler;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
previousHandler = handler;
|
||||
handler = handler->next();
|
||||
}
|
||||
|
||||
return anyHandlerRemoved;
|
||||
}
|
||||
|
||||
void HTTPServer::addHandler(RequestHandler* handler) {
|
||||
_addRequestHandler(handler);
|
||||
}
|
||||
|
||||
bool HTTPServer::removeHandler(RequestHandler *handler) {
|
||||
return _removeRequestHandler(handler);
|
||||
}
|
||||
|
||||
void HTTPServer::_addRequestHandler(RequestHandler* handler) {
|
||||
if (!_lastHandler) {
|
||||
_firstHandler = handler;
|
||||
@@ -233,6 +276,32 @@ void HTTPServer::_addRequestHandler(RequestHandler* handler) {
|
||||
}
|
||||
}
|
||||
|
||||
bool HTTPServer::_removeRequestHandler(RequestHandler *handler) {
|
||||
RequestHandler *current = _firstHandler;
|
||||
RequestHandler *previous = nullptr;
|
||||
|
||||
while (current != nullptr) {
|
||||
if (current == handler) {
|
||||
if (previous == nullptr) {
|
||||
_firstHandler = current->next();
|
||||
} else {
|
||||
previous->next(current->next());
|
||||
}
|
||||
|
||||
if (current == _lastHandler) {
|
||||
_lastHandler = previous;
|
||||
}
|
||||
|
||||
// Delete 'matching' handler
|
||||
delete current;
|
||||
return true;
|
||||
}
|
||||
previous = current;
|
||||
current = current->next();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void HTTPServer::serveStatic(const char* uri, FS& fs, const char* path, const char* cache_header) {
|
||||
_addRequestHandler(new StaticRequestHandler(fs, path, uri, cache_header));
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ enum HTTPAuthMethod { BASIC_AUTH, DIGEST_AUTH };
|
||||
#define HTTP_MAX_DATA_AVAILABLE_WAIT 30 //ms to wait for the client to send the request when there is another client with data available
|
||||
#define HTTP_MAX_POST_WAIT 5000 //ms to wait for POST data to arrive
|
||||
#define HTTP_MAX_SEND_WAIT 5000 //ms to wait for data chunk to be ACKed
|
||||
#define HTTP_MAX_CLOSE_WAIT 2000 //ms to wait for the client to close the connection
|
||||
#define HTTP_MAX_CLOSE_WAIT 5000 //ms to wait for the client to close the connection
|
||||
|
||||
#define CONTENT_LENGTH_UNKNOWN ((size_t) -1)
|
||||
#define CONTENT_LENGTH_NOT_SET ((size_t) -2)
|
||||
@@ -73,7 +73,7 @@ typedef struct {
|
||||
HTTPRawStatus status;
|
||||
size_t totalSize; // content size
|
||||
size_t currentSize; // size of data currently in buf
|
||||
uint8_t buf[HTTP_UPLOAD_BUFLEN];
|
||||
uint8_t buf[HTTP_RAW_BUFLEN];
|
||||
void *data; // additional data
|
||||
} HTTPRaw;
|
||||
|
||||
@@ -95,10 +95,16 @@ public:
|
||||
void requestAuthentication(HTTPAuthMethod mode = BASIC_AUTH, const char* realm = nullptr, const String& authFailMsg = String(""));
|
||||
|
||||
typedef std::function<void(void)> THandlerFunction;
|
||||
void on(const Uri &uri, THandlerFunction fn);
|
||||
void on(const Uri &uri, HTTPMethod method, THandlerFunction fn);
|
||||
void on(const Uri &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn); //ufn handles file uploads
|
||||
typedef std::function<bool(HTTPServer &server)> FilterFunction;
|
||||
RequestHandler& on(const Uri &uri, THandlerFunction fn);
|
||||
RequestHandler& on(const Uri &uri, HTTPMethod method, THandlerFunction fn);
|
||||
RequestHandler& on(const Uri &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn); //ufn handles file uploads
|
||||
bool removeRoute(const char *uri);
|
||||
bool removeRoute(const char *uri, HTTPMethod method);
|
||||
bool removeRoute(const String &uri);
|
||||
bool removeRoute(const String &uri, HTTPMethod method);
|
||||
void addHandler(RequestHandler* handler);
|
||||
bool removeHandler(RequestHandler *handler);
|
||||
void serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_header = nullptr);
|
||||
void onNotFound(THandlerFunction fn); //called when handler is not assigned
|
||||
void onFileUpload(THandlerFunction ufn); //handle file uploads
|
||||
@@ -109,6 +115,9 @@ public:
|
||||
HTTPMethod method() {
|
||||
return _currentMethod;
|
||||
}
|
||||
WiFiClient& client() {
|
||||
return *_currentClient;
|
||||
}
|
||||
HTTPUpload& upload() {
|
||||
return *_currentUpload;
|
||||
}
|
||||
@@ -230,6 +239,7 @@ protected:
|
||||
return _currentClient->write(b, l);
|
||||
}
|
||||
void _addRequestHandler(RequestHandler* handler);
|
||||
bool _removeRequestHandler(RequestHandler *handler);
|
||||
void _handleRequest();
|
||||
void _finalizeResponse();
|
||||
ClientFuture _parseRequest(WiFiClient* client);
|
||||
|
||||
@@ -136,7 +136,7 @@ HTTPServer::ClientFuture HTTPServer::_parseRequest(WiFiClient* client) {
|
||||
//attach handler
|
||||
RequestHandler* handler;
|
||||
for (handler = _firstHandler; handler; handler = handler->next()) {
|
||||
if (handler->canHandle(_currentMethod, _currentUri)) {
|
||||
if (handler->canHandle(*this, _currentMethod, _currentUri)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -188,7 +188,7 @@ HTTPServer::ClientFuture HTTPServer::_parseRequest(WiFiClient* client) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!isForm && _currentHandler && _currentHandler->canRaw(_currentUri)) {
|
||||
if (!isForm && _currentHandler && _currentHandler->canRaw(*this, _currentUri)) {
|
||||
log_v("Parse raw");
|
||||
_currentRaw.reset(new HTTPRaw());
|
||||
_currentRaw->status = RAW_START;
|
||||
@@ -347,7 +347,7 @@ void HTTPServer::_parseArguments(String data) {
|
||||
|
||||
void HTTPServer::_uploadWriteByte(uint8_t b) {
|
||||
if (_currentUpload->currentSize == HTTP_UPLOAD_BUFLEN) {
|
||||
if (_currentHandler && _currentHandler->canUpload(_currentUri)) {
|
||||
if (_currentHandler && _currentHandler->canUpload(*this, _currentUri)) {
|
||||
_currentHandler->upload(*this, _currentUri, *_currentUpload);
|
||||
}
|
||||
_currentUpload->totalSize += _currentUpload->currentSize;
|
||||
@@ -360,13 +360,42 @@ int HTTPServer::_uploadReadByte(WiFiClient * client) {
|
||||
int res = client->read();
|
||||
|
||||
if (res < 0) {
|
||||
while (!client->available() && client->connected()) {
|
||||
delay(2);
|
||||
// keep trying until you either read a valid byte or timeout
|
||||
const unsigned long startMillis = millis();
|
||||
const unsigned long timeoutIntervalMillis = client->getTimeout();
|
||||
bool timedOut = false;
|
||||
for (;;) {
|
||||
if (!client->connected()) {
|
||||
return -1;
|
||||
}
|
||||
// loosely modeled after blinkWithoutDelay pattern
|
||||
while (!timedOut && !client->available() && client->connected()) {
|
||||
delay(2);
|
||||
timedOut = (millis() - startMillis) >= timeoutIntervalMillis;
|
||||
}
|
||||
|
||||
res = client->read();
|
||||
if (res >= 0) {
|
||||
return res; // exit on a valid read
|
||||
}
|
||||
// NOTE: it is possible to get here and have all of the following
|
||||
// assertions hold true
|
||||
//
|
||||
// -- client.available() > 0
|
||||
// -- client.connected == true
|
||||
// -- res == -1
|
||||
//
|
||||
// a simple retry strategy overcomes this which is to say the
|
||||
// assertion is not permanent, but the reason that this works
|
||||
// is elusive, and possibly indicative of a more subtle underlying
|
||||
// issue
|
||||
|
||||
timedOut = (millis() - startMillis) >= timeoutIntervalMillis;
|
||||
if (timedOut) {
|
||||
return res; // exit on a timeout
|
||||
}
|
||||
}
|
||||
|
||||
res = client->read();
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -462,7 +491,7 @@ bool HTTPServer::_parseForm(WiFiClient * client, String boundary, uint32_t len)
|
||||
_currentUpload->totalSize = 0;
|
||||
_currentUpload->currentSize = 0;
|
||||
log_v("Start File: %s Type: %s", _currentUpload->filename.c_str(), _currentUpload->type.c_str());
|
||||
if (_currentHandler && _currentHandler->canUpload(_currentUri)) {
|
||||
if (_currentHandler && _currentHandler->canUpload(*this, _currentUri)) {
|
||||
_currentHandler->upload(*this, _currentUri, *_currentUpload);
|
||||
}
|
||||
_currentUpload->status = UPLOAD_FILE_WRITE;
|
||||
@@ -501,12 +530,12 @@ bool HTTPServer::_parseForm(WiFiClient * client, String boundary, uint32_t len)
|
||||
}
|
||||
}
|
||||
// Found the boundary string, finish processing this file upload
|
||||
if (_currentHandler && _currentHandler->canUpload(_currentUri)) {
|
||||
if (_currentHandler && _currentHandler->canUpload(*this, _currentUri)) {
|
||||
_currentHandler->upload(*this, _currentUri, *_currentUpload);
|
||||
}
|
||||
_currentUpload->totalSize += _currentUpload->currentSize;
|
||||
_currentUpload->status = UPLOAD_FILE_END;
|
||||
if (_currentHandler && _currentHandler->canUpload(_currentUri)) {
|
||||
if (_currentHandler && _currentHandler->canUpload(*this, _currentUri)) {
|
||||
_currentHandler->upload(*this, _currentUri, *_currentUpload);
|
||||
}
|
||||
log_v("End File: %s Type: %s Size: %d", _currentUpload->filename.c_str(), _currentUpload->type.c_str(), (int)_currentUpload->totalSize);
|
||||
@@ -580,7 +609,7 @@ String HTTPServer::urlDecode(const String & text) {
|
||||
|
||||
bool HTTPServer::_parseFormUploadAborted() {
|
||||
_currentUpload->status = UPLOAD_FILE_ABORTED;
|
||||
if (_currentHandler && _currentHandler->canUpload(_currentUri)) {
|
||||
if (_currentHandler && _currentHandler->canUpload(*this, _currentUri)) {
|
||||
_currentHandler->upload(*this, _currentUri, *_currentUpload);
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -111,10 +111,9 @@ void WebServerTemplate<ServerType, DefaultPort>::handleClient() {
|
||||
case HC_WAIT_READ:
|
||||
// Wait for data from client to become available
|
||||
if (_currentClient->available()) {
|
||||
_currentClient->setTimeout(HTTP_MAX_SEND_WAIT);
|
||||
switch (_parseRequest(_currentClient)) {
|
||||
case CLIENT_REQUEST_CAN_CONTINUE:
|
||||
// Because HTTP_MAX_SEND_WAIT is expressed in milliseconds, it must be divided by 1000
|
||||
_currentClient->setTimeout(HTTP_MAX_SEND_WAIT / 1000);
|
||||
_contentLength = CONTENT_LENGTH_NOT_SET;
|
||||
_handleRequest();
|
||||
/* fallthrough */
|
||||
|
||||
@@ -6,17 +6,43 @@
|
||||
class RequestHandler {
|
||||
public:
|
||||
virtual ~RequestHandler() { }
|
||||
|
||||
/*
|
||||
note: old handler API for backward compatibility
|
||||
*/
|
||||
|
||||
virtual bool canHandle(HTTPMethod method, String uri) {
|
||||
(void) method;
|
||||
(void) uri;
|
||||
(void)method;
|
||||
(void)uri;
|
||||
return false;
|
||||
}
|
||||
virtual bool canUpload(String uri) {
|
||||
(void) uri;
|
||||
(void)uri;
|
||||
return false;
|
||||
}
|
||||
virtual bool canRaw(String uri) {
|
||||
(void) uri;
|
||||
(void)uri;
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
note: new handler API with support for filters etc.
|
||||
*/
|
||||
|
||||
virtual bool canHandle(HTTPServer &server, HTTPMethod method, String uri) {
|
||||
(void)server;
|
||||
(void)method;
|
||||
(void)uri;
|
||||
return false;
|
||||
}
|
||||
virtual bool canUpload(HTTPServer &server, String uri) {
|
||||
(void)server;
|
||||
(void)uri;
|
||||
return false;
|
||||
}
|
||||
virtual bool canRaw(HTTPServer &server, String uri) {
|
||||
(void)server;
|
||||
(void)uri;
|
||||
return false;
|
||||
}
|
||||
virtual bool handle(HTTPServer& server, HTTPMethod requestMethod, String requestUri) {
|
||||
@@ -43,6 +69,11 @@ public:
|
||||
_next = r;
|
||||
}
|
||||
|
||||
virtual RequestHandler& setFilter(std::function<bool(HTTPServer&)> filter) {
|
||||
(void)filter;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
RequestHandler* _next = nullptr;
|
||||
|
||||
|
||||
@@ -51,9 +51,31 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
bool canHandle(HTTPServer &server, HTTPMethod requestMethod, String requestUri) override {
|
||||
if (_method != HTTP_ANY && _method != requestMethod) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return _uri->canHandle(requestUri, pathArgs) && (_filter != NULL ? _filter(server) : true);
|
||||
}
|
||||
bool canUpload(HTTPServer &server, String requestUri) override {
|
||||
if (!_ufn || !canHandle(server, HTTP_POST, requestUri)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
bool canRaw(HTTPServer &server, String requestUri) override {
|
||||
(void) requestUri;
|
||||
if (!_ufn || _method == HTTP_GET || (_filter != NULL ? _filter(server) == false : false)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool handle(HTTPServer& server, HTTPMethod requestMethod, String requestUri) override {
|
||||
(void) server;
|
||||
if (!canHandle(requestMethod, requestUri)) {
|
||||
if (!canHandle(server, requestMethod, requestUri)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -62,9 +84,8 @@ public:
|
||||
}
|
||||
|
||||
void upload(HTTPServer& server, String requestUri, HTTPUpload& upload) override {
|
||||
(void) server;
|
||||
(void) upload;
|
||||
if (canUpload(requestUri)) {
|
||||
if (canUpload(server, requestUri)) {
|
||||
_ufn();
|
||||
}
|
||||
}
|
||||
@@ -72,14 +93,22 @@ public:
|
||||
void raw(HTTPServer& server, String requestUri, HTTPRaw& raw) override {
|
||||
(void)server;
|
||||
(void)raw;
|
||||
if (canRaw(requestUri)) {
|
||||
if (canRaw(server, requestUri)) {
|
||||
_ufn();
|
||||
}
|
||||
}
|
||||
|
||||
FunctionRequestHandler& setFilter(HTTPServer::FilterFunction filter) {
|
||||
_filter = filter;
|
||||
return *this;
|
||||
}
|
||||
|
||||
protected:
|
||||
HTTPServer::THandlerFunction _fn;
|
||||
HTTPServer::THandlerFunction _ufn;
|
||||
// _filter should return 'true' when the request should be handled
|
||||
// and 'false' when the request should be ignored
|
||||
HTTPServer::FilterFunction _filter;
|
||||
Uri *_uri;
|
||||
HTTPMethod _method;
|
||||
};
|
||||
@@ -109,8 +138,24 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
bool canHandle(HTTPServer &server, HTTPMethod requestMethod, String requestUri) override {
|
||||
if (requestMethod != HTTP_GET) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((_isFile && requestUri != _uri) || !requestUri.startsWith(_uri)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_filter != NULL ? _filter(server) == false : false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool handle(HTTPServer& server, HTTPMethod requestMethod, String requestUri) override {
|
||||
if (!canHandle(requestMethod, requestUri)) {
|
||||
if (!canHandle(server, requestMethod, requestUri)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -169,7 +214,15 @@ public:
|
||||
return String(buff);
|
||||
}
|
||||
|
||||
StaticRequestHandler& setFilter(HTTPServer::FilterFunction filter) {
|
||||
_filter = filter;
|
||||
return *this;
|
||||
}
|
||||
|
||||
protected:
|
||||
// _filter should return 'true' when the request should be handled
|
||||
// and 'false' when the request should be ignored
|
||||
HTTPServer::FilterFunction _filter;
|
||||
FS _fs;
|
||||
String _uri;
|
||||
String _path;
|
||||
|
||||
@@ -21,8 +21,8 @@
|
||||
#include <memory>
|
||||
|
||||
|
||||
#if defined(DEBUG_ESP_SSL) && defined(DEBUG_ESP_PORT)
|
||||
#define DEBUG_BSSL(fmt, ...) DEBUG_ESP_PORT.printf_P((PGM_P)PSTR( "BSSL:" fmt), ## __VA_ARGS__)
|
||||
#if defined(DEBUG_RP2040_CORE) && defined(DEBUG_RP2040_PORT)
|
||||
#define DEBUG_BSSL(fmt, ...) DEBUG_RP2040_PORT.printf_P((PGM_P)PSTR( "BSSL:" fmt), ## __VA_ARGS__)
|
||||
#else
|
||||
#define DEBUG_BSSL(...)
|
||||
#endif
|
||||
|
||||
@@ -29,18 +29,14 @@
|
||||
#include "WiFiClientSecureBearSSL.h"
|
||||
#include "WiFiNTP.h"
|
||||
#include "StackThunk.h"
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/ip.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include "lwip/inet.h"
|
||||
#include "lwip/netif.h"
|
||||
#include <lwip/opt.h>
|
||||
#include <lwip/ip.h>
|
||||
#include <lwip/tcp.h>
|
||||
#include <lwip/inet.h>
|
||||
#include <lwip/netif.h>
|
||||
#include <include/ClientContext.h>
|
||||
//#include "c_types.h"
|
||||
//#include <mmu_iram.h>
|
||||
//#include <umm_malloc/umm_malloc.h>
|
||||
//#include <umm_malloc/umm_heap_select.h>
|
||||
#if 1
|
||||
#if !CORE_MOCK
|
||||
|
||||
#if !defined(CORE_MOCK)
|
||||
|
||||
// The BearSSL thunks in use for now
|
||||
#define br_ssl_engine_recvapp_ack thunk_br_ssl_engine_recvapp_ack
|
||||
@@ -52,11 +48,10 @@
|
||||
#define br_ssl_engine_sendrec_ack thunk_br_ssl_engine_sendrec_ack
|
||||
#define br_ssl_engine_sendrec_buf thunk_br_ssl_engine_sendrec_buf
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(DEBUG_ESP_SSL) && defined(DEBUG_ESP_PORT)
|
||||
#define DEBUG_BSSL(fmt, ...) DEBUG_ESP_PORT.printf_P((PGM_P)PSTR( "BSSL:" fmt), ## __VA_ARGS__)
|
||||
#if defined(DEBUG_RP2040_CORE) && defined(DEBUG_RP2040_PORT)
|
||||
#define DEBUG_BSSL(fmt, ...) DEBUG_RP2040_PORT.printf_P((PGM_P)PSTR( "BSSL:" fmt), ## __VA_ARGS__)
|
||||
#else
|
||||
#define DEBUG_BSSL(...)
|
||||
#endif
|
||||
@@ -123,6 +118,10 @@ WiFiClientSecureCtx::~WiFiClientSecureCtx() {
|
||||
_client->unref();
|
||||
_client = nullptr;
|
||||
}
|
||||
if (_esp32_ta) {
|
||||
delete _esp32_ta;
|
||||
}
|
||||
|
||||
_cipher_list = nullptr; // std::shared will free if last reference
|
||||
_freeSSL();
|
||||
stack_thunk_del_ref();
|
||||
@@ -717,12 +716,12 @@ extern "C" {
|
||||
if (!xc->done_cert) {
|
||||
br_sha1_update(&xc->sha1_cert, buf, len);
|
||||
br_x509_decoder_push(&xc->ctx, (const void*)buf, len);
|
||||
#if defined(DEBUG_ESP_SSL) && defined(DEBUG_ESP_PORT)
|
||||
#if defined(DEBUG_RP2040_CORE) && defined(DEBUG_RP2040_PORT)
|
||||
DEBUG_BSSL("CERT: ");
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
DEBUG_ESP_PORT.printf_P(PSTR("%02x "), buf[i] & 0xff);
|
||||
DEBUG_RP2040_PORT.printf_P(PSTR("%02x "), buf[i] & 0xff);
|
||||
}
|
||||
DEBUG_ESP_PORT.printf_P(PSTR("\n"));
|
||||
DEBUG_RP2040_PORT.printf_P(PSTR("\n"));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -746,7 +745,7 @@ extern "C" {
|
||||
char res[20];
|
||||
br_sha1_out(&xc->sha1_cert, res);
|
||||
if (xc->match_fingerprint && memcmp(res, xc->match_fingerprint, sizeof(res))) {
|
||||
#ifdef DEBUG_ESP_SSL
|
||||
#if defined(DEBUG_RP2040_CORE) && defined(DEBUG_RP2040_PORT)
|
||||
DEBUG_BSSL("insecure_end_chain: Received cert FP doesn't match\n");
|
||||
char buff[3 * sizeof(res) + 1]; // 3 chars per byte XX_, and null
|
||||
buff[0] = 0;
|
||||
@@ -1139,7 +1138,7 @@ bool WiFiClientSecureCtx::_connectSSL(const char* hostName) {
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef DEBUG_ESP_SSL
|
||||
#if defined(DEBUG_RP2040_CORE) && defined(DEBUG_RP2040_PORT)
|
||||
// BearSSL will reject all connections unless an authentication option is set, warn in DEBUG builds
|
||||
if (!_use_insecure && !_use_fingerprint && !_use_self_signed && !_knownkey && !_certStore && !_ta) {
|
||||
DEBUG_BSSL("Connection *will* fail, no authentication method is setup\n");
|
||||
@@ -1209,7 +1208,7 @@ bool WiFiClientSecureCtx::_connectSSL(const char* hostName) {
|
||||
}
|
||||
|
||||
auto ret = _wait_for_handshake();
|
||||
#ifdef DEBUG_ESP_SSL
|
||||
#if defined(DEBUG_RP2040_CORE) && defined(DEBUG_RP2040_PORT)
|
||||
if (!ret) {
|
||||
char err[256];
|
||||
getLastSSLError(err, sizeof(err));
|
||||
|
||||
@@ -171,7 +171,7 @@ public:
|
||||
_on_rx = handler;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_ESP_CORE
|
||||
#ifdef DEBUG_RP2040_CORE
|
||||
// this helper is ready to be used when debugging UDP
|
||||
void printChain(const pbuf* pb, const char* msg, size_t n) const {
|
||||
// printf the pb pbuf chain, buffered and all at once
|
||||
|
||||
@@ -170,8 +170,8 @@ static async_context_t *lwip_ethernet_init_default_async_context(void) {
|
||||
uint32_t __ethernet_timeout_reached_calls = 0;
|
||||
static uint32_t _pollingPeriod = 20;
|
||||
// This will only be called under the protection of the async context mutex, so no re-entrancy checks needed
|
||||
static void ethernet_timeout_reached(__unused async_context_t *context, __unused async_at_time_worker_t *worker) {
|
||||
assert(worker == ðernet_timeout_worker);
|
||||
extern "C" void ethernet_timeout_reached(__unused async_context_t *context, __unused async_at_time_worker_t *worker) {
|
||||
// assert(worker == ðernet_timeout_worker);
|
||||
__ethernet_timeout_reached_calls++;
|
||||
ethernet_arch_lwip_gpio_mask(); // Ensure non-polled devices won't interrupt us
|
||||
for (auto handlePacket : _handlePacketList) {
|
||||
@@ -192,6 +192,7 @@ static void update_next_timeout(async_context_t *context, async_when_pending_wor
|
||||
worker->work_pending = true;
|
||||
async_context_add_at_time_worker_in_ms(context, ðernet_timeout_worker, _pollingPeriod);
|
||||
}
|
||||
extern void __startEthernetTask() __attribute((weak));
|
||||
|
||||
void __startEthernetContext() {
|
||||
if (__ethernetContextInitted) {
|
||||
@@ -209,7 +210,11 @@ void __startEthernetContext() {
|
||||
ethernet_timeout_worker.do_work = ethernet_timeout_reached;
|
||||
always_pending_update_timeout_worker.work_pending = true;
|
||||
always_pending_update_timeout_worker.do_work = update_next_timeout;
|
||||
async_context_add_when_pending_worker(_context, &always_pending_update_timeout_worker);
|
||||
if (__isFreeRTOS) {
|
||||
__startEthernetTask();
|
||||
} else {
|
||||
async_context_add_when_pending_worker(_context, &always_pending_update_timeout_worker);
|
||||
}
|
||||
__ethernetContextInitted = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ extern "C" void netif_status_changed(struct netif* netif) {
|
||||
|
||||
bool LwipIntf::stateChangeSysCB(LwipIntf::CBType&& cb) {
|
||||
if (netifStatusChangeListLength >= NETIF_STATUS_CB_SIZE) {
|
||||
#if defined(DEBUG_ESP_CORE)
|
||||
#if defined(DEBUG_RP2040_CORE)
|
||||
DEBUGV("NETIF_STATUS_CB_SIZE is too low\n");
|
||||
#endif
|
||||
return false;
|
||||
|
||||
@@ -40,6 +40,9 @@
|
||||
#include <lwip/inet_chksum.h>
|
||||
#include <lwip/apps/sntp.h>
|
||||
|
||||
#include <_freertos.h>
|
||||
extern SemaphoreHandle_t __hwMutex;
|
||||
|
||||
|
||||
#include "SPI.h"
|
||||
#include "LwipIntf.h"
|
||||
@@ -369,9 +372,12 @@ bool LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu) {
|
||||
return false;
|
||||
}
|
||||
|
||||
__freertos_mutex_take(__hwMutex);
|
||||
if (!RawDev::begin(_macAddress, &_netif)) {
|
||||
__freertos_mutex_give(__hwMutex);
|
||||
return false;
|
||||
}
|
||||
__freertos_mutex_give(__hwMutex);
|
||||
|
||||
if (_intrPin < 0) {
|
||||
_phID = __addEthernetPacketHandler([this] { this->handlePackets(); });
|
||||
@@ -427,18 +433,22 @@ bool LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu) {
|
||||
|
||||
template<class RawDev>
|
||||
void LwipIntfDev<RawDev>::end() {
|
||||
if (_intrPin < 0) {
|
||||
__removeEthernetPacketHandler(_phID);
|
||||
} else {
|
||||
detachInterrupt(_intrPin);
|
||||
__removeEthernetGPIO(_intrPin);
|
||||
if (_started) {
|
||||
if (_intrPin < 0) {
|
||||
__removeEthernetPacketHandler(_phID);
|
||||
} else {
|
||||
detachInterrupt(_intrPin);
|
||||
__removeEthernetGPIO(_intrPin);
|
||||
}
|
||||
|
||||
__freertos_mutex_take(__hwMutex);
|
||||
RawDev::end();
|
||||
__freertos_mutex_give(__hwMutex);
|
||||
|
||||
netif_remove(&_netif);
|
||||
|
||||
_started = false;
|
||||
}
|
||||
|
||||
RawDev::end();
|
||||
|
||||
netif_remove(&_netif);
|
||||
|
||||
_started = false;
|
||||
}
|
||||
|
||||
template<class RawDev>
|
||||
@@ -464,7 +474,9 @@ template<class RawDev>
|
||||
err_t LwipIntfDev<RawDev>::linkoutput_s(netif* netif, struct pbuf* pbuf) {
|
||||
LwipIntfDev* lid = (LwipIntfDev*)netif->state;
|
||||
ethernet_arch_lwip_begin();
|
||||
__freertos_mutex_take(__hwMutex);
|
||||
uint16_t len = lid->sendFrame((const uint8_t*)pbuf->payload, pbuf->len);
|
||||
__freertos_mutex_give(__hwMutex);
|
||||
lid->_packetsSent++;
|
||||
#if PHY_HAS_CAPTURE
|
||||
if (phy_capture) {
|
||||
@@ -548,7 +560,9 @@ err_t LwipIntfDev<RawDev>::handlePackets() {
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
__freertos_mutex_take(__hwMutex);
|
||||
uint16_t tot_len = RawDev::readFrameSize();
|
||||
__freertos_mutex_give(__hwMutex);
|
||||
if (!tot_len) {
|
||||
return ERR_OK;
|
||||
}
|
||||
@@ -566,11 +580,15 @@ err_t LwipIntfDev<RawDev>::handlePackets() {
|
||||
if (pbuf) {
|
||||
pbuf_free(pbuf);
|
||||
}
|
||||
__freertos_mutex_take(__hwMutex);
|
||||
RawDev::discardFrame(tot_len);
|
||||
__freertos_mutex_give(__hwMutex);
|
||||
return ERR_BUF;
|
||||
}
|
||||
|
||||
__freertos_mutex_take(__hwMutex);
|
||||
uint16_t len = RawDev::readFrameData((uint8_t*)pbuf->payload, tot_len);
|
||||
__freertos_mutex_give(__hwMutex);
|
||||
if (len != tot_len) {
|
||||
// tot_len is given by readFrameSize()
|
||||
// and is supposed to be honoured by readFrameData()
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "framework-arduinopico",
|
||||
"version": "1.30902.0",
|
||||
"version": "1.30904.0",
|
||||
"description": "Arduino Wiring-based Framework (RPi Pico RP2040)",
|
||||
"keywords": [
|
||||
"framework",
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user