Compare commits
92 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a286b5d067 | ||
|
|
f071f57abf | ||
|
|
0e0fe334cd | ||
|
|
23410c618d | ||
|
|
b44898f08d | ||
|
|
165f356439 | ||
|
|
1d0df2936b | ||
|
|
1684d63e04 | ||
|
|
cf5dfb43d6 | ||
|
|
ded6638ef6 | ||
|
|
a8228851d9 | ||
|
|
633289def6 | ||
|
|
f4ff13d6b7 | ||
|
|
0493ec2245 | ||
|
|
4c6bbb8aae | ||
|
|
b625f636be | ||
|
|
3bfd62d075 | ||
|
|
b405adb359 | ||
|
|
accf37de39 | ||
|
|
582eed3be8 | ||
|
|
35d5ec03a3 | ||
|
|
737c627d44 | ||
|
|
84a3120c7f | ||
|
|
99b9ed1181 | ||
|
|
0d636a3285 | ||
|
|
79ccd47f71 | ||
|
|
3ffc70abe5 | ||
|
|
f4f2eda481 | ||
|
|
f4b2c74b0e | ||
|
|
111e5a66b6 | ||
|
|
b90723130d | ||
|
|
e6179761d4 | ||
|
|
b371fd3468 | ||
|
|
c8fccdbb47 | ||
|
|
4876401809 | ||
|
|
45af7c188a | ||
|
|
236e9ccda6 | ||
|
|
94e8b9f87e | ||
|
|
c9580d6627 | ||
|
|
87929373e2 | ||
|
|
4e2aec9ced | ||
|
|
fa7957f5e2 | ||
|
|
4f15805246 | ||
|
|
cf67c815e9 | ||
|
|
5533dc807f | ||
|
|
ac2aabe6e3 | ||
|
|
c6a428ddd4 | ||
|
|
a83a845d5a | ||
|
|
c91b188f96 | ||
|
|
53a6f4593e | ||
|
|
0dda769d0f | ||
|
|
848df066c4 | ||
|
|
b4005d8bad | ||
|
|
34c0df2761 | ||
|
|
cedfc2c8db | ||
|
|
c9fe63ec7b | ||
|
|
2fedcb61b5 | ||
|
|
272b033253 | ||
|
|
cae03d54d6 | ||
|
|
e70f1a591f | ||
|
|
0e768ac6e4 | ||
|
|
bc056eaca2 | ||
|
|
a6ad84a3b8 | ||
|
|
a5b953dc80 | ||
|
|
d446b05387 | ||
|
|
e864e5ef24 | ||
|
|
e669507759 | ||
|
|
e50c04cbe1 | ||
|
|
56dd57f4a0 | ||
|
|
f2abb564c7 | ||
|
|
4e7abb2350 | ||
|
|
540e499389 | ||
|
|
191a2bdd2a | ||
|
|
4d1c8357b4 | ||
|
|
85b5b1df91 | ||
|
|
c3e2c105d4 | ||
|
|
dba602a081 | ||
|
|
c3f2ce57f0 | ||
|
|
bbf8e0b6b6 | ||
|
|
03ab020dc8 | ||
|
|
9c1f3225a6 | ||
|
|
f72dd3b7d7 | ||
|
|
48f6210607 | ||
|
|
6f9be2b139 | ||
|
|
3217525873 | ||
|
|
e26972bd2f | ||
|
|
bf31533306 | ||
|
|
0ca0577667 | ||
|
|
0e797e7e04 | ||
|
|
a661ef3a3f | ||
|
|
17dc1dcf8c | ||
|
|
4c27aa57b1 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -258,3 +258,4 @@ xml/
|
||||
ltapi/
|
||||
ltambz/
|
||||
hashChanges.yaml
|
||||
.piopm
|
||||
|
||||
114
README.md
114
README.md
@@ -12,6 +12,7 @@
|
||||
[](https://registry.platformio.org/platforms/kuba2k2/libretuya)
|
||||
|
||||

|
||||

|
||||
|
||||
</div>
|
||||
|
||||
@@ -34,95 +35,40 @@ LibreTuya also provides a common interface for all family implementations. The i
|
||||
|
||||
## Board List
|
||||
|
||||
A (mostly) complete* list of Tuya wireless module boards.
|
||||
|
||||
| Module Name | MCU | Flash | RAM | Pins** | Wi-Fi | BLE | Family name
|
||||
------------------------------|------------------------------------------------------------------------------------------------|-------------------------|-------|----------|-------------|-------|-----|---------------
|
||||
❌ | [WB1S](https://developer.tuya.com/en/docs/iot/wb1s?id=K9duevbj3ol4x) | BK7231T @ 120 MHz | 2 MiB | 256 KiB | 18 (11 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [WB2L](https://developer.tuya.com/en/docs/iot/wb2l-datasheet?id=K9duegc9bualu) | BK7231T @ 120 MHz | 2 MiB | 256 KiB | 7 (5 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [WB2S](https://developer.tuya.com/en/docs/iot/wb2s-module-datasheet?id=K9ghecl7kc479) | BK7231T @ 120 MHz | 2 MiB | 256 KiB | 11 (8 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [WB3L](https://developer.tuya.com/en/docs/iot/wb3l-module-datasheet?id=K9duiggw2v8sp) | BK7231T @ 120 MHz | 2 MiB | 256 KiB | 16 (12 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [WB3S](https://developer.tuya.com/en/docs/iot/wb3s-module-datasheet?id=K9dx20n6hz5n4) | BK7231T @ 120 MHz | 2 MiB | 256 KiB | 16 (11 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [WB3S-IPEX](https://developer.tuya.com/en/docs/iot/wb3sipex-module-datasheet?id=K9irq0laun21z) | BK7231T @ 120 MHz | 2 MiB | 256 KiB | 16 (11 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [WB8P](https://developer.tuya.com/en/docs/iot/wb8p-module-datasheet?id=K9fwx4f89tvzd) | BK7231T @ 120 MHz | 2 MiB | 256 KiB | 10 (8 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [WBLC5](https://developer.tuya.com/en/docs/iot/wblc5-module-datasheet?id=K9duilns1f3gi) | BK7231T @ 120 MHz | 2 MiB | 256 KiB | 6 (3 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [WBLC9](https://developer.tuya.com/en/docs/iot/wblc9-module-datasheet?id=K9hgglry2jp5h) | BK7231T @ 120 MHz | 2 MiB | 256 KiB | 8 (6 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [CB1S](https://developer.tuya.com/en/docs/iot/cb1s-module-datasheet?id=Kaij1abmwyjq2) | BK7231N @ 120 MHz | 2 MiB | 256 KiB | 18 (11 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [CB2L](https://developer.tuya.com/en/docs/iot/cb2l-module-datasheet?id=Kai2eku1m3pyl) | BK7231N @ 120 MHz | 2 MiB | 256 KiB | 7 (5 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [CB2S](https://developer.tuya.com/en/docs/iot/cb2s-module-datasheet?id=Kafgfsa2aaypq) | BK7231N @ 120 MHz | 2 MiB | 256 KiB | 11 (8 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [CB3L](https://developer.tuya.com/en/docs/iot/cb3l-module-datasheet?id=Kai51ngmrh3qm) | BK7231N @ 120 MHz | 2 MiB | 256 KiB | 16 (11 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [CB3S](https://developer.tuya.com/en/docs/iot/cb3s?id=Kai94mec0s076) | BK7231N @ 120 MHz | 2 MiB | 256 KiB | 22 (14 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [CB3S-NL](https://developer.tuya.com/en/docs/iot/CB3S-NL-module-datasheet?id=Kbaesan0vyoms) | BK7231NL (?) @ 120 MHz | 2 MiB | 256 KiB | 22 (14 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [CB3SE](https://developer.tuya.com/en/docs/iot/CB3SE-Module-Datasheet?id=Kanoiluul7nl2) | BK7231N @ 120 MHz | 2 MiB | 256 KiB | 22 (17 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [CB8P](https://developer.tuya.com/en/docs/iot/cb8p-module-datasheet?id=Kahvig14r1yk9) | BK7231N @ 120 MHz | 2 MiB | 256 KiB | 10 (8 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [CBLC5](https://developer.tuya.com/en/docs/iot/cblc5-module-datasheet?id=Ka07iqyusq1wm) | BK7231N @ 120 MHz | 2 MiB | 256 KiB | 6 (3 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [CBLC9](https://developer.tuya.com/en/docs/iot/cblc9-module-datasheet?id=Ka42cqnj9r0i5) | BK7231N @ 120 MHz | 2 MiB | 256 KiB | 8 (6 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [CBU](https://developer.tuya.com/en/docs/iot/cbu-module-datasheet?id=Ka07pykl5dk4u) | BK7231N @ 120 MHz | 2 MiB | 256 KiB | 21 (18 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [CBU-IPEX](https://developer.tuya.com/en/docs/iot/cbuipex-module-datasheet?id=Kaedsyurckrhu) | BK7231N @ 120 MHz | 2 MiB | 256 KiB | 21 (18 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [CBU-NL](https://developer.tuya.com/en/docs/iot/CBU-NL-module-datasheet?id=Kbaeq6j53y0yg) | BK7231N (?) @ 120 MHz | 2 MiB | 256 KiB | 21 (18 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [WR1](https://developer.tuya.com/en/docs/iot/wifiwr1module?id=K9605tc0k90t3) | RTL8710BN @ 125 MHz | 1 MiB | 256 KiB | 18 (11 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WR1-IPEX](https://developer.tuya.com/en/docs/iot/wifiwr1ipexmodule?id=K9605t977tx5u) | RTL8710BN @ 125 MHz | 1 MiB | 256 KiB | 18 (11 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WR1E](https://developer.tuya.com/en/docs/iot/wr1e?id=K96smbbeycxtf) | RTL8710BN @ 125 MHz | 2 MiB | 256 KiB | 18 (11 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WR2](https://developer.tuya.com/en/docs/iot/wifiwr2module?id=K9605tko0juc3) | RTL8710BN @ 125 MHz | 2 MiB | 256 KiB | 11 (8 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WR2E](https://developer.tuya.com/en/docs/iot/wr2e?id=K97scnsjhue4h) | RTL8710BN @ 125 MHz | 2 MiB | 256 KiB | 11 (8 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WR2L](https://developer.tuya.com/en/docs/iot/wifiwr2lmodule?id=K9605tnbj7gva) | RTL8710BX @ 62.5 MHz | 2 MiB | 256 KiB | 7 (5 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WR2LE](https://developer.tuya.com/en/docs/iot/wr2le?id=K9eio9y9e8i8c) | RTL8710BX @ 62.5 MHz | 2 MiB | 256 KiB | 7 (5 I/O) | ✔️ | ❌ | -
|
||||
[`wr3`](boards/wr3/README.md) | [WR3](https://developer.tuya.com/en/docs/iot/wr3-module-datasheet?id=K9g3ainzbj9z1) | RTL8710BN @ 125 MHz | 2 MiB | 256 KiB | 16 (12 I/O) | ✔️ | ❌ | `realtek-ambz`
|
||||
❌ | [WR3E](https://developer.tuya.com/en/docs/iot/wr3e-module-datasheet?id=K9elwlqbfosbc) | RTL8710BN @ 125 MHz | 2 MiB | 256 KiB | 16 (12 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WR3L](https://developer.tuya.com/en/docs/iot/wifiwr3lmodule?id=K9605tt0kveqm) | RTL8710BX @ 125 MHz (?) | 2 MiB | 256 KiB | 16 (12 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WR3LE](https://developer.tuya.com/en/docs/iot/wr3le?id=K986l7a1ha8tm) | RTL8710BX @ 62.5 MHz | 2 MiB | 256 KiB | 16 (12 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WR3N](https://developer.tuya.com/en/docs/iot/wr3n-datasheet?id=K98zdx31ztdge) | RTL8710BN @ 125 MHz | 2 MiB | 256 KiB | 16 (10 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WR4](https://developer.tuya.com/en/docs/iot/wifiwr4module?id=K9605tvu78p3e) | RTL8710BN @ 125 MHz | 1 MiB | 256 KiB | 16 (12 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WR5E](https://developer.tuya.com/en/docs/iot/wr5e?id=K986r9pxqxa8i) | RTL8710BX @ 62.5 MHz | 2 MiB | 256 KiB | 15 (12 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WR6-H](https://developer.tuya.com/en/docs/iot/wr6h-module-datasheet?id=K9pork8eeowgl) | RTL8710BN @ 125 MHz | 2 MiB | 256 KiB | 18 (12 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WR6](https://developer.tuya.com/en/docs/iot/wr6-datasheet?id=K97rp45u6gff9) | RTL8710BN @ 125 MHz | 2 MiB | 256 KiB | 18 (12 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WRG1](https://developer.tuya.com/en/docs/iot/wr6-datasheet?id=K97rp45u6gff9) | RTL8711AM @ 166 MHz | 4 MiB | 2048 KiB | 25 (20 I/O) | ✔️ | ❌ | -
|
||||
❌ | [XR1](https://developer.tuya.com/en/docs/iot/xr1?id=K9lq3y9xo0zkx) | XR809 @ 160 MHz | 2 MiB | 384 KiB | 18 (11 I/O) | ✔️ | ❌ | -
|
||||
❌ | [XR1-IPEX](https://developer.tuya.com/en/docs/iot/xr1ipex-module-datasheet?id=K9razqu9gqele) | XR809 @ 160 MHz | 2 MiB | 384 KiB | 18 (11 I/O) | ✔️ | ❌ | -
|
||||
❌ | [XR3](https://developer.tuya.com/en/docs/iot/xr3-datasheet?id=K98s9168qi49g) | XR809 @ 160 MHz | 2 MiB | 384 KiB | 22 (17 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WE1S](https://developer.tuya.com/en/docs/iot/wifie1smodule?id=K9605thnvg3e7) | ESP8266EX @ 80/160 MHz | 2 MiB | 36 KiB | 18 (11 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WE2L](https://developer.tuya.com/en/docs/iot/wifie2lmodule?id=K9605ud0gkjmh) | ESP8285 @ 80/160 MHz | 1 MiB | 50 KiB | 7 (5 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WE2S](https://developer.tuya.com/en/docs/iot/wifie2smodule?id=K9605u79tgxug) | ESP8285 @ 80/160 MHz | 1 MiB | 50 KiB | 11 (8 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WE3L](https://developer.tuya.com/en/docs/iot/wifie3lpinmodule?id=K9605uj1ar87n) | ESP8266 @ 80/160 MHz | 2 MiB | 50 KiB | 16 (12 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WE3S](https://developer.tuya.com/en/docs/iot/wifie3smodule?id=K9605ua1cx9tv) | ESP8266 @ 80/160 MHz | 2 MiB | 50 KiB | 16 (12 I/O) | ✔️ | ❌ | -
|
||||
❌ | [WE3SE](https://developer.tuya.com/en/docs/iot/we3se?id=K97qv6ab7oh8d) | ESP32 @ 240 MHz | 4 MiB | 520 KiB | 22 (17 I/O) | ✔️ | ✔️ | -
|
||||
❌ | [WE5P](https://developer.tuya.com/en/docs/iot/wifie5pmodule?id=K9605um3dtjbx) | ESP8266 @ 80/160 MHz | 1 MiB | 50 KiB | 15 (11 I/O) | ✔️ | ❌ | -
|
||||
|
||||
\* Only modules featuring at least Wi-Fi. WBR, JWBR, CR, (TY)JW and (TY)LC Series are not included here
|
||||
|
||||
** I/O count includes GPIOs, ADCs, PWM outputs and UART, but doesn't count CEN/RST and power pins.
|
||||
See [Boards & CPU list](https://kuba2k2.github.io/libretuya/docs/supported/).
|
||||
|
||||
## Arduino Core support status
|
||||
|
||||
Note: this list will probably change with each functionality update.
|
||||
|
||||
| `realtek-ambz`
|
||||
--------------------|---------------
|
||||
Core functions | ✔️
|
||||
GPIO/PWM/IRQ | ✔️/❓/✔️
|
||||
Analog input | ❓
|
||||
UART I/O | ✔️
|
||||
Flash I/O | ❓
|
||||
**CORE LIBRARIES** |
|
||||
SoftwareSerial | ❌
|
||||
SPI | ❌
|
||||
Wire | ❗
|
||||
**OTHER LIBRARIES** |
|
||||
Wi-Fi STA/AP/Mixed | ✔️
|
||||
Wi-Fi Client (SSL) | ✔️ (✔️)
|
||||
Wi-Fi Server | ✔️
|
||||
Wi-Fi Events | ✔️
|
||||
IPv6 | ❌
|
||||
HTTP Client (SSL) | ✔️ (✔️)
|
||||
HTTP Server | ✔️
|
||||
NVS / Preferences | ✔️
|
||||
SPIFFS | ❌
|
||||
BLE | -
|
||||
NTP | ❌
|
||||
OTA | ❓
|
||||
MDNS | ✔️
|
||||
MQTT | ✅
|
||||
SD | ❌
|
||||
| `realtek-ambz` | `beken-72xx`
|
||||
--------------------|----------------|-------------
|
||||
Core functions | ✔️ | ✔️
|
||||
GPIO/PWM/IRQ | ✔️/✔️/✔️ | ❓/✔️/❌
|
||||
Analog input (ADC) | ✔️ | ✔️
|
||||
Serial | ✔️ | ✔️
|
||||
Serial (extra) | ❌ | 1, 2
|
||||
Flash I/O | ✔️ | ✔️
|
||||
**CORE LIBRARIES** | |
|
||||
SoftwareSerial | ❌ | ❌
|
||||
SPI | ❌ | ❌
|
||||
Wire | ❗ | ❌
|
||||
**OTHER LIBRARIES** | |
|
||||
Wi-Fi STA/AP/Mixed | ✔️ | ✔️/❌/❌
|
||||
Wi-Fi Events | ✔️ | ❌
|
||||
TCP Client (SSL) | ✔️ (✔️) | ✔️ (❗)
|
||||
TCP Server | ✔️ | ✔️
|
||||
IPv6 | ❌ | ❌
|
||||
HTTP Client (SSL) | ✔️ (✔️) | ❓
|
||||
HTTP Server | ✔️ | ✔️
|
||||
NVS / Preferences | ❌ | ❌
|
||||
SPIFFS | ❌ | ❌
|
||||
BLE | - | ❌
|
||||
NTP | ❌ | ❌
|
||||
OTA | ✔️ | ❌
|
||||
MDNS | ✔️ | ✔️
|
||||
MQTT | ✅ | ❌
|
||||
SD | ❌ | ❌
|
||||
|
||||
Symbols:
|
||||
|
||||
|
||||
20
SUMMARY.md
20
SUMMARY.md
@@ -1,28 +1,26 @@
|
||||
* [Home](README.md)
|
||||
* [💻 Family list](docs/families.md)
|
||||
* [💻 Boards & CPU list](docs/supported.md)
|
||||
* [✔️ Implementation status](docs/implementation-status.md)
|
||||
* [🔧 Configuration](docs/config.md)
|
||||
* [📁 Project structure](docs/project-structure.md)
|
||||
* 🔖 Code reference
|
||||
* [LibreTuya API](docs/reference/lt-api.md)
|
||||
* [Class reference](ltapi/class_libre_tuya.md)
|
||||
* [Static functions](ltapi/_libre_tuya_a_p_i_8cpp.md)
|
||||
* [LT class reference](ltapi/class_libre_tuya.md)
|
||||
* [Common methods](ltapi/_libre_tuya_a_p_i_8h.md)
|
||||
* [Wiring custom methods](ltapi/_libre_tuya_custom_8h.md)
|
||||
* [Logger](ltapi/lt__logger_8h.md)
|
||||
* [Chip types & UF2 families](ltapi/_chip_type_8h.md)
|
||||
* [Chip & family IDs](ltapi/_chip_type_8h_source.md)
|
||||
* [POSIX utilities](ltapi/lt__posix__api_8h.md)
|
||||
* Common API
|
||||
* [Flash](ltapi/class_i_flash_class.md)
|
||||
* [FS](ltapi/classfs_1_1_f_s.md)
|
||||
* [Preferences](ltapi/class_i_preferences.md)
|
||||
* [WiFi API](ltapi/class_i_wi_fi_generic_class.md)
|
||||
* [Station](ltapi/class_i_wi_fi_s_t_a_class.md)
|
||||
* [Access Point](ltapi/class_i_wi_fi_a_p_class.md)
|
||||
* [Scanning](ltapi/class_i_wi_fi_scan_class.md)
|
||||
* [WiFi API](ltapi/class_wi_fi_class.md)
|
||||
* [TCP Client](ltapi/class_i_wi_fi_client.md)
|
||||
* [SSL Client](ltapi/class_i_wi_fi_client_secure.md)
|
||||
* [TCP Server](ltapi/class_i_wi_fi_server.md)
|
||||
* [LibreTuya libraries](docs/libs-built-in.md)
|
||||
* [base64](ltapi/classbase64.md)
|
||||
* [Flash](ltapi/class_flash_class.md)
|
||||
* [HTTPClient](ltapi/class_h_t_t_p_client.md)
|
||||
* [mDNS](ltapi/classm_d_n_s.md)
|
||||
* NetUtils
|
||||
@@ -44,12 +42,12 @@
|
||||
* [uf2ota.h reference](ltapi/uf2ota_8h.md)
|
||||
* Families
|
||||
* [Realtek - notes](docs/platform/realtek/README.md)
|
||||
* [Beken 72xx - notes](docs/platform/beken-72xx/README.md)
|
||||
* Realtek AmebaZ Series
|
||||
* Boards
|
||||
* [WR3](boards/wr3/README.md)
|
||||
* C library
|
||||
* [Built-in functions](docs/platform/realtek-ambz/stdlib.md)
|
||||
* [Memory management](docs/platform/realtek-ambz/memory-management.md)
|
||||
* [Debugging](docs/platform/realtek/debugging.md)
|
||||
* [Exception decoder](docs/platform/realtek/exception-decoder.md)
|
||||
* [All supported boards](boards/)
|
||||
* [🔗 Resources](docs/resources.md)
|
||||
|
||||
32
arduino/beken-72xx/cores/arduino/Arduino.h
Normal file
32
arduino/beken-72xx/cores/arduino/Arduino.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-14. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include "WCharacterFixup.h"
|
||||
#endif
|
||||
|
||||
#include <api/ArduinoAPI.h>
|
||||
#include <core/LibreTuyaAPI.h>
|
||||
|
||||
// Include family-specific code
|
||||
#include "WVariant.h"
|
||||
// Include board variant
|
||||
#include "variant.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include "SerialClass.h"
|
||||
#ifdef HAS_SERIAL_CLASS
|
||||
// declare instances of available Serial* components
|
||||
// map Serial to Serial2 if available, else to Serial1
|
||||
#ifdef PIN_SERIAL1_RX
|
||||
extern SerialClass Serial1;
|
||||
#endif
|
||||
#ifdef PIN_SERIAL2_RX
|
||||
extern SerialClass Serial2;
|
||||
#define Serial Serial2
|
||||
#else
|
||||
#define Serial Serial1
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
130
arduino/beken-72xx/cores/arduino/LibreTuyaAPI.cpp
Normal file
130
arduino/beken-72xx/cores/arduino/LibreTuyaAPI.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
|
||||
|
||||
#include <LibreTuyaAPI.h>
|
||||
|
||||
// can't include <flash.h> as it collides with <Flash.h> on Windows -_-
|
||||
#define REG_FLASH_BASE 0x00803000
|
||||
#define REG_FLASH_OPERATE_SW (REG_FLASH_BASE + 0 * 4)
|
||||
#define REG_FLASH_RDID (REG_FLASH_BASE + 4 * 4)
|
||||
#define FLASH_BUSY_SW (0x01UL << 31)
|
||||
#define FLASH_WP_VALUE (0x01UL << 30)
|
||||
#define FLASH_OP_SW (0x01UL << 29)
|
||||
#define FLASH_OP_TYPE_POS 24
|
||||
#define FLASH_OP_RDID 20
|
||||
|
||||
extern "C" {
|
||||
#include <flash_pub.h>
|
||||
#include <sys_ctrl.h>
|
||||
#include <sys_rtos.h>
|
||||
#include <wlan_ui_pub.h>
|
||||
}
|
||||
|
||||
void LibreTuya::restart() {
|
||||
bk_reboot();
|
||||
}
|
||||
|
||||
/* CPU-related */
|
||||
|
||||
ChipType LibreTuya::getChipType() {
|
||||
uint8_t chipId = *(uint8_t *)(SCTRL_CHIP_ID);
|
||||
return CHIP_TYPE_ENUM(FAMILY, chipId);
|
||||
}
|
||||
|
||||
const char *LibreTuya::getChipModel() {
|
||||
return STRINGIFY_MACRO(MCU);
|
||||
}
|
||||
|
||||
uint32_t LibreTuya::getChipId() {
|
||||
// TODO determine if this really is unique
|
||||
uint32_t chipId = REG_READ(SCTRL_DEVICE_ID);
|
||||
chipId &= 0xFFFFFF;
|
||||
return chipId;
|
||||
}
|
||||
|
||||
uint8_t LibreTuya::getChipCores() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *LibreTuya::getChipCoreType() {
|
||||
return "ARM968E-S";
|
||||
}
|
||||
|
||||
uint32_t LibreTuya::getCpuFreq() {
|
||||
return configCPU_CLOCK_HZ;
|
||||
}
|
||||
|
||||
uint32_t LibreTuya::getCycleCount() {
|
||||
// TODO
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Flash memory utilities */
|
||||
|
||||
FlashId LibreTuya::getFlashChipId() {
|
||||
uint32_t data = (FLASH_OP_RDID << FLASH_OP_TYPE_POS) | FLASH_OP_SW | FLASH_WP_VALUE;
|
||||
REG_WRITE(REG_FLASH_OPERATE_SW, data);
|
||||
while (REG_READ(REG_FLASH_OPERATE_SW) & FLASH_BUSY_SW) {}
|
||||
FlashId id = {
|
||||
.manufacturerId = REG_RD8(REG_FLASH_RDID, 2),
|
||||
.chipId = REG_RD8(REG_FLASH_RDID, 1),
|
||||
.chipSizeId = REG_RD8(REG_FLASH_RDID, 0),
|
||||
};
|
||||
return id;
|
||||
}
|
||||
|
||||
/* Memory management */
|
||||
|
||||
uint32_t LibreTuya::getRamSize() {
|
||||
return 256 * 1024;
|
||||
}
|
||||
|
||||
uint32_t LibreTuya::getHeapSize() {
|
||||
#if configDYNAMIC_HEAP_SIZE
|
||||
extern unsigned char _empty_ram;
|
||||
#if CFG_SOC_NAME == SOC_BK7231N
|
||||
return (0x00400000 + 192 * 1024) - (uint32_t)(&_empty_ram);
|
||||
#else
|
||||
return (0x00400000 + 256 * 1024) - (uint32_t)(&_empty_ram);
|
||||
#endif
|
||||
#else
|
||||
return configTOTAL_HEAP_SIZE;
|
||||
#endif
|
||||
}
|
||||
|
||||
uint32_t LibreTuya::getFreeHeap() {
|
||||
return xPortGetFreeHeapSize();
|
||||
}
|
||||
|
||||
uint32_t LibreTuya::getMinFreeHeap() {
|
||||
return xPortGetMinimumEverFreeHeapSize();
|
||||
}
|
||||
|
||||
uint32_t LibreTuya::getMaxAllocHeap() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* OTA-related */
|
||||
|
||||
uint8_t LibreTuya::otaGetStoredIndex() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool LibreTuya::otaSupportsDual() {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool LibreTuya::otaHasImage1() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LibreTuya::otaHasImage2() {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool LibreTuya::otaSwitch(bool force) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Global instance */
|
||||
|
||||
LibreTuya LT;
|
||||
75
arduino/beken-72xx/cores/arduino/SerialClass.cpp
Normal file
75
arduino/beken-72xx/cores/arduino/SerialClass.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-23. */
|
||||
|
||||
#include "SerialClass.h"
|
||||
|
||||
extern "C" {
|
||||
#include <uart_pub.h>
|
||||
|
||||
extern void bk_send_byte(uint8_t uport, uint8_t data);
|
||||
extern void uart_hw_set_change(uint8_t uport, bk_uart_config_t *uart_config);
|
||||
extern int uart_rx_callback_set(int uport, uart_callback callback, void *param);
|
||||
}
|
||||
|
||||
#ifdef PIN_SERIAL1_RX
|
||||
SerialClass Serial1(UART1_PORT);
|
||||
#endif
|
||||
#ifdef PIN_SERIAL2_RX
|
||||
SerialClass Serial2(UART2_PORT);
|
||||
#endif
|
||||
|
||||
SerialClass::SerialClass(uint8_t port) {
|
||||
this->port = port;
|
||||
this->buf = NULL;
|
||||
}
|
||||
|
||||
static void callback(int port, void *param) {
|
||||
RingBuffer *buf = (RingBuffer *)param;
|
||||
buf->store_char(uart_read_byte(port));
|
||||
}
|
||||
|
||||
void SerialClass::begin(unsigned long baudrate, uint16_t config) {
|
||||
uint8_t dataWidth = ((config & SERIAL_DATA_MASK) >> 8) - 1; // 0x100..0x400 -> 0..3
|
||||
uint8_t parity = 3 - (config & SERIAL_PARITY_MASK); // 0x3..0x1 -> 0..2
|
||||
uint8_t stopBits = (config & SERIAL_STOP_BIT_MASK) == SERIAL_STOP_BIT_2; // 0x10..0x30 -> 0..1
|
||||
|
||||
bk_uart_config_t cfg = {
|
||||
.baud_rate = baudrate,
|
||||
.data_width = (uart_data_width_t)dataWidth,
|
||||
.parity = (uart_parity_t)parity,
|
||||
.stop_bits = (uart_stop_bits_t)stopBits,
|
||||
.flow_control = FLOW_CTRL_DISABLED,
|
||||
};
|
||||
if (this->buf) {
|
||||
this->buf->clear();
|
||||
} else {
|
||||
this->buf = new RingBuffer();
|
||||
}
|
||||
uart_hw_set_change(port, &cfg);
|
||||
uart_rx_callback_set(port, callback, this->buf);
|
||||
}
|
||||
|
||||
void SerialClass::end() {
|
||||
uart_rx_callback_set(port, NULL, NULL);
|
||||
delete this->buf;
|
||||
}
|
||||
|
||||
int SerialClass::available() {
|
||||
return buf->available();
|
||||
}
|
||||
|
||||
int SerialClass::peek() {
|
||||
return buf->peek();
|
||||
}
|
||||
|
||||
int SerialClass::read() {
|
||||
return buf->read_char();
|
||||
}
|
||||
|
||||
void SerialClass::flush() {
|
||||
uart_wait_tx_over();
|
||||
}
|
||||
|
||||
size_t SerialClass::write(uint8_t c) {
|
||||
bk_send_byte(port, c);
|
||||
return 1;
|
||||
}
|
||||
38
arduino/beken-72xx/cores/arduino/SerialClass.h
Normal file
38
arduino/beken-72xx/cores/arduino/SerialClass.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-23. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <api/HardwareSerial.h>
|
||||
#include <api/RingBuffer.h>
|
||||
|
||||
using namespace arduino;
|
||||
|
||||
class SerialClass : public HardwareSerial {
|
||||
private:
|
||||
uint8_t port;
|
||||
RingBuffer *buf;
|
||||
|
||||
public:
|
||||
SerialClass(uint8_t port);
|
||||
|
||||
inline void begin(unsigned long baudrate) {
|
||||
begin(baudrate, SERIAL_8N1);
|
||||
}
|
||||
|
||||
void begin(unsigned long baudrate, uint16_t config);
|
||||
void end();
|
||||
int available();
|
||||
int peek();
|
||||
int read();
|
||||
void flush();
|
||||
size_t write(uint8_t c);
|
||||
|
||||
operator bool() {
|
||||
return !!buf;
|
||||
}
|
||||
|
||||
using Print::write;
|
||||
};
|
||||
|
||||
#define HAS_SERIAL_CLASS 1
|
||||
21
arduino/beken-72xx/cores/arduino/WVariant.h
Normal file
21
arduino/beken-72xx/cores/arduino/WVariant.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-18. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "sdk_extern.h"
|
||||
#include "sdk_mem.h"
|
||||
|
||||
// allow BDK use its own delay() and let Arduino code use delayMilliseconds()
|
||||
void delayMilliseconds(unsigned long);
|
||||
#define delay delayMilliseconds
|
||||
|
||||
// from fixups/arch_main.c
|
||||
extern unsigned char __bk_rf_is_init;
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
25
arduino/beken-72xx/cores/arduino/main.cpp
Normal file
25
arduino/beken-72xx/cores/arduino/main.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
extern "C" {
|
||||
#include <rtos_pub.h>
|
||||
#include <sys_rtos.h>
|
||||
}
|
||||
|
||||
beken_thread_t mainThread;
|
||||
|
||||
bool startMainTask() {
|
||||
OSStatus ret = rtos_create_thread(
|
||||
&mainThread,
|
||||
THD_APPLICATION_PRIORITY,
|
||||
"main",
|
||||
(beken_thread_function_t)main_task,
|
||||
8192,
|
||||
NULL
|
||||
);
|
||||
if (ret != kNoErr)
|
||||
return false;
|
||||
vTaskStartScheduler();
|
||||
return true;
|
||||
}
|
||||
30
arduino/beken-72xx/cores/arduino/sdk_extern.h
Normal file
30
arduino/beken-72xx/cores/arduino/sdk_extern.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-18. */
|
||||
|
||||
#pragma once
|
||||
|
||||
// for printf() etc (they are wrapped anyway)
|
||||
#include <stdio.h>
|
||||
|
||||
// most stuff is here
|
||||
#include <include.h>
|
||||
// for os_printf
|
||||
#include <uart_pub.h>
|
||||
// for GPIO names
|
||||
#include <gpio_pub.h>
|
||||
|
||||
// conflict with stl_algobase.h
|
||||
#undef min
|
||||
#undef max
|
||||
|
||||
// include printf() wrapper disable methods
|
||||
#include <printf_port.h>
|
||||
|
||||
// make non-SDK code call the proper printf()
|
||||
#undef bk_printf
|
||||
#undef os_printf
|
||||
#undef warning_prf
|
||||
#undef fatal_prf
|
||||
#define bk_printf printf
|
||||
#define os_printf printf
|
||||
#define warning_prf printf
|
||||
#define fatal_prf printf
|
||||
20
arduino/beken-72xx/cores/arduino/sdk_mem.h
Normal file
20
arduino/beken-72xx/cores/arduino/sdk_mem.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-18. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
// Beken SDK is actually pretty good, in terms of declaring
|
||||
// stdlib functions properly! So no need for any #define hell.
|
||||
#include <mem_pub.h>
|
||||
|
||||
// All the MemMang functions are in stdlib, just wrapped
|
||||
// during linking.
|
||||
#include <stdlib.h>
|
||||
// for memcpy etc.
|
||||
#include <string.h>
|
||||
|
||||
// ...except zalloc, which is apparently not in the stdlib
|
||||
#define zalloc os_zalloc
|
||||
|
||||
#define LT_HEAP_FUNC xPortGetFreeHeapSize
|
||||
32
arduino/beken-72xx/cores/arduino/wiring.c
Normal file
32
arduino/beken-72xx/cores/arduino/wiring.c
Normal file
@@ -0,0 +1,32 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <rtos_pub.h>
|
||||
#include <sys_rtos.h>
|
||||
|
||||
void delayMilliseconds(unsigned long ms) {
|
||||
rtos_delay_milliseconds(ms);
|
||||
}
|
||||
|
||||
void delayMicroseconds(unsigned int us) {
|
||||
// TODO implement this properly
|
||||
us /= 10;
|
||||
volatile uint32_t i, j;
|
||||
for (i = 0; i < us; i++) {
|
||||
for (j = 0; j < 100; j++) {}
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long millis() {
|
||||
return xTaskGetTickCount() * portTICK_PERIOD_MS;
|
||||
}
|
||||
|
||||
unsigned long micros() {
|
||||
// TODO implement this properly
|
||||
return millis() * 1000;
|
||||
}
|
||||
|
||||
void yield() {
|
||||
vTaskDelay(1);
|
||||
taskYIELD();
|
||||
}
|
||||
113
arduino/beken-72xx/cores/arduino/wiring_analog.c
Normal file
113
arduino/beken-72xx/cores/arduino/wiring_analog.c
Normal file
@@ -0,0 +1,113 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-20. */
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <pwm_pub.h>
|
||||
#include <saradc_pub.h>
|
||||
|
||||
static GPIO_INDEX pwmToGpio[] = {
|
||||
GPIO6, // PWM0
|
||||
GPIO7, // PWM1
|
||||
GPIO8, // PWM2
|
||||
GPIO9, // PWM3
|
||||
GPIO24, // PWM4
|
||||
GPIO26, // PWM5
|
||||
};
|
||||
|
||||
static GPIO_INDEX adcToGpio[] = {
|
||||
-1, // ADC0 - VBAT
|
||||
GPIO4, // ADC1
|
||||
GPIO5, // ADC2
|
||||
GPIO23, // ADC3
|
||||
GPIO2, // ADC4
|
||||
GPIO3, // ADC5
|
||||
GPIO12, // ADC6
|
||||
GPIO13, // ADC7
|
||||
};
|
||||
|
||||
static uint8_t gpioToPwm(GPIO_INDEX gpio) {
|
||||
for (uint8_t i = 0; i < sizeof(pwmToGpio); i++) {
|
||||
if (pwmToGpio[i] == gpio)
|
||||
return i;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint8_t gpioToAdc(GPIO_INDEX gpio) {
|
||||
for (uint8_t i = 0; i < sizeof(adcToGpio); i++) {
|
||||
if (adcToGpio[i] == gpio)
|
||||
return i;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static pwm_param_t pwm;
|
||||
static uint16_t adcData[1];
|
||||
|
||||
uint16_t analogReadVoltage(pin_size_t pinNumber) {
|
||||
PinInfo *pin = pinInfo(pinNumber);
|
||||
if (!pin)
|
||||
return 0;
|
||||
if (!pinSupported(pin, PIN_ADC))
|
||||
return 0;
|
||||
|
||||
UINT32 status;
|
||||
saradc_desc_t adc;
|
||||
DD_HANDLE handle;
|
||||
saradc_config_param_init(&adc);
|
||||
adc.channel = gpioToAdc(pin->gpio);
|
||||
adc.mode = ADC_CONFIG_MODE_CONTINUE;
|
||||
adc.pData = adcData;
|
||||
adc.data_buff_size = 1;
|
||||
handle = ddev_open(SARADC_DEV_NAME, &status, (uint32_t)&adc);
|
||||
if (status)
|
||||
return 0;
|
||||
// wait for data
|
||||
while (!adc.has_data || adc.current_sample_data_cnt < 1) {
|
||||
delay(1);
|
||||
}
|
||||
ddev_control(handle, SARADC_CMD_RUN_OR_STOP_ADC, (void *)false);
|
||||
ddev_close(handle);
|
||||
return adcData[0];
|
||||
}
|
||||
|
||||
uint16_t analogReadMaxVoltage(pin_size_t pinNumber) {
|
||||
return 3300;
|
||||
}
|
||||
|
||||
void analogWrite(pin_size_t pinNumber, int value) {
|
||||
PinInfo *pin = pinInfo(pinNumber);
|
||||
if (!pin)
|
||||
return;
|
||||
if (!pinSupported(pin, PIN_PWM))
|
||||
return;
|
||||
|
||||
float percent = value * 1.0 / (1 << _analogWriteResolution);
|
||||
uint32_t dutyCycle = percent * _analogWritePeriod * 26 - 1;
|
||||
pwm.channel = gpioToPwm(pin->gpio);
|
||||
pwm.duty_cycle = dutyCycle;
|
||||
|
||||
if (!pinEnabled(pin, PIN_PWM)) {
|
||||
// enable PWM and set its value
|
||||
pwm.cfg.bits.en = PWM_ENABLE;
|
||||
pwm.cfg.bits.int_en = PWM_INT_DIS;
|
||||
pwm.cfg.bits.mode = PWM_PWM_MODE;
|
||||
pwm.cfg.bits.clk = PWM_CLK_26M;
|
||||
pwm.end_value = 26 * _analogWritePeriod - 1;
|
||||
pwm.p_Int_Handler = NULL;
|
||||
__wrap_bk_printf_disable();
|
||||
sddev_control(PWM_DEV_NAME, CMD_PWM_INIT_PARAM, &pwm);
|
||||
__wrap_bk_printf_enable();
|
||||
pin->enabled &= ~PIN_GPIO;
|
||||
pin->enabled |= PIN_PWM;
|
||||
} else if (value == 0) {
|
||||
// disable PWM
|
||||
pwm.cfg.bits.en = PWM_DISABLE;
|
||||
__wrap_bk_printf_disable();
|
||||
sddev_control(PWM_DEV_NAME, CMD_PWM_DEINIT_PARAM, &pwm);
|
||||
__wrap_bk_printf_enable();
|
||||
pin->enabled &= ~PIN_PWM;
|
||||
} else {
|
||||
// update duty cycle
|
||||
sddev_control(PWM_DEV_NAME, CMD_PWM_SET_DUTY_CYCLE, &pwm);
|
||||
}
|
||||
}
|
||||
62
arduino/beken-72xx/cores/arduino/wiring_digital.c
Normal file
62
arduino/beken-72xx/cores/arduino/wiring_digital.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-20. */
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
void pinMode(pin_size_t pinNumber, PinMode pinMode) {
|
||||
PinInfo *pin = pinInfo(pinNumber);
|
||||
if (!pin)
|
||||
return;
|
||||
if (!pinSupported(pin, PIN_GPIO))
|
||||
return;
|
||||
if (pinEnabled(pin, PIN_PWM))
|
||||
// disable PWM before using the pin
|
||||
analogWrite(pinNumber, 0);
|
||||
if (pinEnabled(pin, PIN_GPIO) && pin->mode == pinMode)
|
||||
return;
|
||||
switch (pinMode) {
|
||||
case INPUT:
|
||||
gpio_config(pin->gpio, GMODE_INPUT);
|
||||
break;
|
||||
case OUTPUT:
|
||||
gpio_config(pin->gpio, GMODE_OUTPUT);
|
||||
break;
|
||||
case INPUT_PULLUP:
|
||||
gpio_config(pin->gpio, GMODE_INPUT_PULLUP);
|
||||
break;
|
||||
case INPUT_PULLDOWN:
|
||||
gpio_config(pin->gpio, GMODE_INPUT_PULLDOWN);
|
||||
break;
|
||||
case OUTPUT_OPENDRAIN:
|
||||
gpio_config(pin->gpio, GMODE_SET_HIGH_IMPENDANCE);
|
||||
break;
|
||||
}
|
||||
pin->enabled |= PIN_GPIO;
|
||||
pin->mode = pinMode;
|
||||
}
|
||||
|
||||
void digitalWrite(pin_size_t pinNumber, PinStatus status) {
|
||||
// verify level is 0 or 1
|
||||
if (status > HIGH)
|
||||
return;
|
||||
PinInfo *pin = pinInfo(pinNumber);
|
||||
if (!pin)
|
||||
return;
|
||||
// pin is not GPIO yet or not OUTPUT; enable or disable input pullup
|
||||
if (!pinEnabled(pin, PIN_GPIO) || !pinIsOutput(pin)) {
|
||||
pinMode(pinNumber, status * INPUT_PULLUP);
|
||||
return;
|
||||
}
|
||||
// write the new state
|
||||
gpio_output(pin->gpio, status);
|
||||
}
|
||||
|
||||
PinStatus digitalRead(pin_size_t pinNumber) {
|
||||
PinInfo *pin = pinInfo(pinNumber);
|
||||
if (!pin)
|
||||
return 0;
|
||||
// pin is not GPIO yet or not INPUT; change the mode
|
||||
if (!pinEnabled(pin, PIN_GPIO) || !pinIsOutput(pin))
|
||||
pinMode(pinNumber, INPUT);
|
||||
// read the value
|
||||
return gpio_input(pin->gpio);
|
||||
}
|
||||
56
arduino/beken-72xx/libraries/WiFi/WiFi.cpp
Normal file
56
arduino/beken-72xx/libraries/WiFi/WiFi.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-26. */
|
||||
|
||||
#include "WiFiPriv.h"
|
||||
|
||||
WiFiClass::WiFiClass() {
|
||||
data.scanSem = xSemaphoreCreateBinary();
|
||||
}
|
||||
|
||||
WiFiClass::~WiFiClass() {
|
||||
vSemaphoreDelete(data.scanSem);
|
||||
}
|
||||
|
||||
WiFiStatus eventTypeToStatus(uint8_t type) {
|
||||
// rw_msg_pub.h:9
|
||||
switch (type) {
|
||||
case RW_EVT_STA_IDLE:
|
||||
return WL_IDLE_STATUS;
|
||||
return WL_NO_SSID_AVAIL;
|
||||
case RW_EVT_STA_CONNECTING:
|
||||
case RW_EVT_STA_CONNECTED:
|
||||
return WL_SCAN_COMPLETED;
|
||||
case RW_EVT_STA_GOT_IP:
|
||||
return WL_CONNECTED;
|
||||
case RW_EVT_STA_PASSWORD_WRONG:
|
||||
case RW_EVT_STA_NO_AP_FOUND:
|
||||
case RW_EVT_STA_ASSOC_FULL:
|
||||
case RW_EVT_STA_CONNECT_FAILED:
|
||||
return WL_CONNECT_FAILED;
|
||||
case RW_EVT_STA_BEACON_LOSE:
|
||||
return WL_CONNECTION_LOST;
|
||||
case RW_EVT_STA_DISCONNECTED:
|
||||
return WL_DISCONNECTED;
|
||||
}
|
||||
}
|
||||
|
||||
WiFiAuthMode securityTypeToAuthMode(uint8_t type) {
|
||||
// wlan_ui_pub.h:62
|
||||
switch (type) {
|
||||
case BK_SECURITY_TYPE_NONE:
|
||||
return WIFI_AUTH_OPEN;
|
||||
case BK_SECURITY_TYPE_WEP:
|
||||
return WIFI_AUTH_WEP;
|
||||
case BK_SECURITY_TYPE_WPA_TKIP:
|
||||
case BK_SECURITY_TYPE_WPA_AES:
|
||||
return WIFI_AUTH_WPA_PSK;
|
||||
case BK_SECURITY_TYPE_WPA2_TKIP:
|
||||
case BK_SECURITY_TYPE_WPA2_AES:
|
||||
case BK_SECURITY_TYPE_WPA2_MIXED:
|
||||
return WIFI_AUTH_WPA2_PSK;
|
||||
case BK_SECURITY_TYPE_WPA3_SAE:
|
||||
return WIFI_AUTH_WPA3_PSK;
|
||||
case BK_SECURITY_TYPE_WPA3_WPA2_MIXED:
|
||||
return WIFI_AUTH_WPA2_WPA3_PSK;
|
||||
}
|
||||
return WIFI_AUTH_INVALID;
|
||||
}
|
||||
10
arduino/beken-72xx/libraries/WiFi/WiFi.h
Normal file
10
arduino/beken-72xx/libraries/WiFi/WiFi.h
Normal file
@@ -0,0 +1,10 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-26. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <api/WiFi/WiFi.h>
|
||||
|
||||
#include "WiFiClient.h"
|
||||
#include "WiFiClientSecure.h"
|
||||
#include "WiFiServer.h"
|
||||
8
arduino/beken-72xx/libraries/WiFi/WiFiClient.h
Normal file
8
arduino/beken-72xx/libraries/WiFi/WiFiClient.h
Normal file
@@ -0,0 +1,8 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-27. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <api/WiFi/WiFi.h>
|
||||
#include <lwip/LwIPClient.h>
|
||||
|
||||
typedef LwIPClient WiFiClient;
|
||||
8
arduino/beken-72xx/libraries/WiFi/WiFiClientSecure.h
Normal file
8
arduino/beken-72xx/libraries/WiFi/WiFiClientSecure.h
Normal file
@@ -0,0 +1,8 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-05-04. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <api/WiFi/WiFi.h>
|
||||
#include <ssl/MbedTLSClient.h>
|
||||
|
||||
typedef MbedTLSClient WiFiClientSecure;
|
||||
23
arduino/beken-72xx/libraries/WiFi/WiFiData.h
Normal file
23
arduino/beken-72xx/libraries/WiFi/WiFiData.h
Normal file
@@ -0,0 +1,23 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-26. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
#include <FreeRTOS.h>
|
||||
#include <semphr.h>
|
||||
|
||||
} // extern "C"
|
||||
|
||||
typedef struct {
|
||||
char ssid[32 + 1];
|
||||
char pass[64 + 1];
|
||||
unsigned long scannedAt;
|
||||
uint32_t ipSta[4];
|
||||
uint32_t ipAp[4];
|
||||
SemaphoreHandle_t scanSem;
|
||||
void *statusIp;
|
||||
void *statusLink;
|
||||
} WiFiData;
|
||||
71
arduino/beken-72xx/libraries/WiFi/WiFiGeneric.cpp
Normal file
71
arduino/beken-72xx/libraries/WiFi/WiFiGeneric.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-26. */
|
||||
|
||||
#include "WiFiPriv.h"
|
||||
|
||||
bool WiFiClass::modePriv(WiFiMode mode, WiFiModeAction sta, WiFiModeAction ap) {
|
||||
__wrap_bk_printf_disable();
|
||||
|
||||
if (mode && !data.statusIp) {
|
||||
data.statusIp = (IPStatusTypedef *)malloc(sizeof(IPStatusTypedef));
|
||||
data.statusLink = (LinkStatusTypeDef *)malloc(sizeof(LinkStatusTypeDef));
|
||||
}
|
||||
|
||||
if (!__bk_rf_is_init) {
|
||||
LT_D_WG("Initializing func&app");
|
||||
func_init_extended();
|
||||
app_pre_start();
|
||||
__bk_rf_is_init = true;
|
||||
}
|
||||
|
||||
LT_HEAP_I();
|
||||
|
||||
if (sta == WLMODE_ENABLE) {
|
||||
LT_D_WG("Enabling STA");
|
||||
bk_wlan_sta_init(NULL);
|
||||
} else if (sta == WLMODE_DISABLE) {
|
||||
LT_D_WG("Disabling STA");
|
||||
bk_wlan_stop(BK_STATION);
|
||||
}
|
||||
|
||||
LT_HEAP_I();
|
||||
|
||||
if (ap == WLMODE_ENABLE) {
|
||||
LT_D_WG("Enabling AP");
|
||||
bk_wlan_ap_init(NULL);
|
||||
} else if (ap == WLMODE_DISABLE) {
|
||||
LT_D_WG("Disabling AP");
|
||||
bk_wlan_stop(BK_SOFT_AP);
|
||||
}
|
||||
|
||||
if (!mode) {
|
||||
free(data.statusIp);
|
||||
free(data.statusLink);
|
||||
data.statusIp = NULL;
|
||||
data.statusLink = NULL;
|
||||
}
|
||||
|
||||
LT_HEAP_I();
|
||||
|
||||
__wrap_bk_printf_enable();
|
||||
}
|
||||
|
||||
WiFiMode WiFiClass::getMode() {
|
||||
if (!g_wlan_general_param)
|
||||
return WIFI_MODE_NULL;
|
||||
uint8_t role = g_wlan_general_param->role;
|
||||
// change 1->2, 2->1
|
||||
return (WiFiMode)(role + (role == 1) - (role == 2));
|
||||
}
|
||||
|
||||
WiFiStatus WiFiClass::status() {
|
||||
return eventTypeToStatus(mhdr_get_station_status());
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::hostByName(const char *hostname) {
|
||||
ip_addr_t ip;
|
||||
int ret = netconn_gethostbyname(hostname, &ip);
|
||||
if (ret == ERR_OK) {
|
||||
return ip.addr;
|
||||
}
|
||||
return IPAddress();
|
||||
}
|
||||
57
arduino/beken-72xx/libraries/WiFi/WiFiPriv.h
Normal file
57
arduino/beken-72xx/libraries/WiFi/WiFiPriv.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-26. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <api/WiFi/WiFi.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
#include <lwip/api.h>
|
||||
#include <lwip/ip_addr.h>
|
||||
#include <lwip/netif.h>
|
||||
|
||||
#include <FreeRTOS.h>
|
||||
#include <semphr.h>
|
||||
|
||||
#include <common.h>
|
||||
#include <config.h>
|
||||
#include <main_none.h>
|
||||
#include <param_config.h>
|
||||
#include <rw_msg_rx.h>
|
||||
#include <wlan_ui_pub.h>
|
||||
#include <wpa_supplicant_i.h>
|
||||
|
||||
extern void func_init_extended();
|
||||
extern void app_pre_start();
|
||||
extern void bk_wlan_ap_init(network_InitTypeDef_st *inNetworkInitPara);
|
||||
|
||||
// func/hostapd-2.5/wpa_supplicant/main_supplicant.c
|
||||
extern struct wpa_ssid_value *wpas_connect_ssid;
|
||||
|
||||
// func/lwip_intf/lwip-2.0.2/port/net.c
|
||||
extern struct netif *net_get_sta_handle(void);
|
||||
extern struct netif *net_get_uap_handle(void);
|
||||
|
||||
// app/param_config.c
|
||||
extern general_param_t *g_wlan_general_param;
|
||||
extern ap_param_t *g_ap_param_ptr;
|
||||
extern sta_param_t *g_sta_param_ptr;
|
||||
extern uint8_t system_mac[6];
|
||||
|
||||
// WiFi.cpp
|
||||
WiFiStatus eventTypeToStatus(uint8_t type);
|
||||
WiFiAuthMode securityTypeToAuthMode(uint8_t type);
|
||||
|
||||
#define ADDR_STA_IP data.ipSta[0]
|
||||
#define ADDR_STA_MASK data.ipSta[1]
|
||||
#define ADDR_STA_GW data.ipSta[2]
|
||||
#define ADDR_STA_DNS data.ipSta[3]
|
||||
#define ADDR_AP_IP data.ipAp[0]
|
||||
#define ADDR_AP_MASK data.ipAp[1]
|
||||
#define ADDR_AP_GW data.ipAp[2]
|
||||
#define ADDR_AP_DNS data.ipAp[3]
|
||||
|
||||
#define IP_STATUS ((IPStatusTypedef *)data.statusIp)
|
||||
#define LINK_STATUS ((LinkStatusTypeDef *)data.statusLink)
|
||||
|
||||
} // extern "C"
|
||||
219
arduino/beken-72xx/libraries/WiFi/WiFiSTA.cpp
Normal file
219
arduino/beken-72xx/libraries/WiFi/WiFiSTA.cpp
Normal file
@@ -0,0 +1,219 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-27. */
|
||||
|
||||
#include "WiFiPriv.h"
|
||||
|
||||
WiFiStatus
|
||||
WiFiClass::begin(const char *ssid, const char *passphrase, int32_t channel, const uint8_t *bssid, bool connect) {
|
||||
enableSTA(true);
|
||||
|
||||
if (!ssid || *ssid == 0x00 || strlen(ssid) > 32) {
|
||||
LT_W("SSID not specified or too long");
|
||||
return WL_CONNECT_FAILED;
|
||||
}
|
||||
|
||||
if (passphrase && strlen(passphrase) > 64) {
|
||||
LT_W("Passphrase too long");
|
||||
return WL_CONNECT_FAILED;
|
||||
}
|
||||
|
||||
// store the network data for later
|
||||
strcpy(data.ssid, ssid);
|
||||
if (passphrase)
|
||||
strcpy(data.pass, passphrase);
|
||||
else
|
||||
data.pass[0] = 0;
|
||||
|
||||
if (reconnect(bssid))
|
||||
return WL_CONNECTED;
|
||||
|
||||
return WL_CONNECT_FAILED;
|
||||
}
|
||||
|
||||
bool WiFiClass::config(IPAddress localIP, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2) {
|
||||
ADDR_STA_IP = (uint32_t)localIP;
|
||||
ADDR_STA_GW = (uint32_t)gateway;
|
||||
ADDR_STA_MASK = (uint32_t)subnet;
|
||||
ADDR_STA_DNS = (uint32_t)dns1;
|
||||
|
||||
if (status() == WL_CONNECTED) {
|
||||
IPStatusTypedef config;
|
||||
config.dhcp = !localIP[0];
|
||||
if (localIP) {
|
||||
sprintf(config.ip, "%u.%u.%u.%u", ADDR_STA_IP);
|
||||
sprintf(config.mask, "%u.%u.%u.%u", ADDR_STA_MASK);
|
||||
sprintf(config.gate, "%u.%u.%u.%u", ADDR_STA_GW);
|
||||
}
|
||||
if (dns1)
|
||||
sprintf(config.dns, "%u.%u.%u.%u", ADDR_STA_DNS);
|
||||
bk_wlan_set_ip_status(&config, BK_STATION);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WiFiClass::reconnect(const uint8_t *bssid) {
|
||||
if (!bssid && !data.ssid[0]) {
|
||||
LT_E("(B)SSID not specified");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (bssid) {
|
||||
LT_D_WG("Connecting to " MACSTR, MAC2STR(bssid));
|
||||
} else {
|
||||
LT_D_WG("Connecting to %s", data.ssid);
|
||||
}
|
||||
|
||||
network_InitTypeDef_st config;
|
||||
memset(&config, 0, sizeof(network_InitTypeDef_st));
|
||||
// network_InitTypeDef_adv_st config;
|
||||
// memset(&config, 0, sizeof(network_InitTypeDef_adv_st));
|
||||
|
||||
config.wifi_mode = BK_STATION;
|
||||
config.wifi_retry_interval = 100;
|
||||
// config.ap_info.security = BK_SECURITY_TYPE_WPA2_MIXED;
|
||||
// config.ap_info.channel = 6;
|
||||
strcpy(config.wifi_ssid, data.ssid);
|
||||
// strcpy(config.ap_info.ssid, data.ssid);
|
||||
strcpy(config.wifi_key, data.pass);
|
||||
// strcpy(config.key, data.pass);
|
||||
// config.key_len = strlen(data.pass);
|
||||
if (bssid)
|
||||
memcpy(config.wifi_bssid, bssid, 6);
|
||||
// memcpy(config.ap_info.bssid, bssid, 6);
|
||||
|
||||
if (ADDR_STA_IP && ADDR_STA_MASK && ADDR_STA_GW) {
|
||||
config.dhcp_mode = DHCP_DISABLE;
|
||||
sprintf(config.local_ip_addr, "%u.%u.%u.%u", ADDR_STA_IP);
|
||||
sprintf(config.net_mask, "%u.%u.%u.%u", ADDR_STA_MASK);
|
||||
sprintf(config.gateway_ip_addr, "%u.%u.%u.%u", ADDR_STA_GW);
|
||||
LT_D_WG("Static IP: %s / %s / %s", config.local_ip_addr, config.net_mask, config.gateway_ip_addr);
|
||||
} else {
|
||||
config.dhcp_mode = DHCP_CLIENT;
|
||||
LT_D_WG("Using DHCP");
|
||||
}
|
||||
|
||||
if (ADDR_STA_DNS) {
|
||||
sprintf(config.dns_server_ip_addr, "%u.%u.%u.%u", ADDR_STA_DNS);
|
||||
LT_D_WG("Static DNS: %s", config.dns_server_ip_addr);
|
||||
}
|
||||
|
||||
if (!data.scannedAt || millis() - data.scannedAt > 10000) {
|
||||
LT_D_WG("Scan needed");
|
||||
// apparently a scan must be performed first,
|
||||
// else it hangs at "[sa_sta]MM_START_REQ"
|
||||
scanNetworks(false);
|
||||
}
|
||||
|
||||
LT_D_WG("Starting WiFi...");
|
||||
__wrap_bk_printf_disable();
|
||||
bk_wlan_start_sta(&config);
|
||||
__wrap_bk_printf_enable();
|
||||
LT_D_WG("bk_wlan_start() OK");
|
||||
return true;
|
||||
|
||||
error:
|
||||
return false;
|
||||
}
|
||||
|
||||
bool WiFiClass::disconnect(bool wifiOff) {
|
||||
bk_wlan_connection_loss();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WiFiClass::setAutoReconnect(bool autoReconnect) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool WiFiClass::getAutoReconnect() {
|
||||
return false;
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::localIP() {
|
||||
IPStatusTypedef config;
|
||||
bk_wlan_get_ip_status(&config, BK_STATION);
|
||||
IPAddress ip;
|
||||
ip.fromString(config.ip);
|
||||
return ip;
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::subnetMask() {
|
||||
IPStatusTypedef config;
|
||||
bk_wlan_get_ip_status(&config, BK_STATION);
|
||||
IPAddress ip;
|
||||
ip.fromString(config.mask);
|
||||
return ip;
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::gatewayIP() {
|
||||
IPStatusTypedef config;
|
||||
bk_wlan_get_ip_status(&config, BK_STATION);
|
||||
IPAddress ip;
|
||||
ip.fromString(config.gate);
|
||||
return ip;
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::dnsIP(uint8_t dns_no) {
|
||||
IPStatusTypedef config;
|
||||
bk_wlan_get_ip_status(&config, BK_STATION);
|
||||
IPAddress ip;
|
||||
ip.fromString(config.dns);
|
||||
return ip;
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::broadcastIP() {
|
||||
return calculateBroadcast(localIP(), subnetMask());
|
||||
}
|
||||
|
||||
const char *WiFiClass::getHostname() {
|
||||
struct netif *ifs = net_get_sta_handle();
|
||||
return netif_get_hostname(ifs);
|
||||
}
|
||||
|
||||
bool WiFiClass::setHostname(const char *hostname) {
|
||||
struct netif *ifs = net_get_sta_handle();
|
||||
netif_set_hostname(ifs, (char *)hostname);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *WiFiClass::macAddress(uint8_t *mac) {
|
||||
wifi_get_mac_address((char *)mac, CONFIG_ROLE_STA);
|
||||
return mac;
|
||||
}
|
||||
|
||||
bool WiFiClass::setMacAddress(const uint8_t *mac) {
|
||||
wifi_set_mac_address((char *)mac);
|
||||
return true;
|
||||
}
|
||||
|
||||
const String WiFiClass::SSID() {
|
||||
bk_wlan_get_link_status(LINK_STATUS);
|
||||
return (char *)LINK_STATUS->ssid;
|
||||
}
|
||||
|
||||
const String WiFiClass::psk() {
|
||||
if (!isConnected())
|
||||
return "";
|
||||
struct wpa_supplicant *wpas = wpa_suppliant_ctrl_get_wpas();
|
||||
if (!wpas || !wpas->conf || !wpas->conf->ssid)
|
||||
return "";
|
||||
return (char *)wpas->conf->ssid->passphrase;
|
||||
}
|
||||
|
||||
uint8_t *WiFiClass::BSSID() {
|
||||
bk_wlan_get_link_status(LINK_STATUS);
|
||||
return LINK_STATUS->bssid;
|
||||
}
|
||||
|
||||
int32_t WiFiClass::channel() {
|
||||
bk_wlan_get_link_status(LINK_STATUS);
|
||||
return LINK_STATUS->channel;
|
||||
}
|
||||
|
||||
int8_t WiFiClass::RSSI() {
|
||||
bk_wlan_get_link_status(LINK_STATUS);
|
||||
return LINK_STATUS->wifi_strength;
|
||||
}
|
||||
|
||||
WiFiAuthMode WiFiClass::getEncryption() {
|
||||
bk_wlan_get_link_status(LINK_STATUS);
|
||||
return securityTypeToAuthMode(LINK_STATUS->security);
|
||||
}
|
||||
73
arduino/beken-72xx/libraries/WiFi/WiFiScan.cpp
Normal file
73
arduino/beken-72xx/libraries/WiFi/WiFiScan.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-27. */
|
||||
|
||||
#include "WiFiPriv.h"
|
||||
|
||||
static void scanHandler(void *ctx, uint8_t param) {
|
||||
LT_HEAP_I();
|
||||
WiFiClass *cls = (WiFiClass *)ctx;
|
||||
if (!cls) {
|
||||
LT_W("Called without ctx");
|
||||
return;
|
||||
}
|
||||
WiFiScanData *scan = cls->scan;
|
||||
if (!scan) {
|
||||
LT_W("Called without cls->scan");
|
||||
return;
|
||||
}
|
||||
|
||||
ScanResult_adv result;
|
||||
if (wlan_sta_scan_result(&result)) {
|
||||
LT_E("Failed to get scan result");
|
||||
goto end;
|
||||
}
|
||||
LT_D_WG("Found %d APs", result.ApNum);
|
||||
|
||||
cls->scanAlloc(result.ApNum);
|
||||
if (!scan->ap) {
|
||||
LT_W("scan->ap alloc failed");
|
||||
goto end;
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; i < result.ApNum; i++) {
|
||||
scan->ap[i].ssid = strdup(result.ApList[i].ssid);
|
||||
scan->ap[i].auth = securityTypeToAuthMode(result.ApList[i].security);
|
||||
scan->ap[i].rssi = result.ApList[i].ApPower;
|
||||
scan->ap[i].channel = result.ApList[i].channel;
|
||||
memcpy(scan->ap[i].bssid.addr, result.ApList[i].bssid, 6);
|
||||
}
|
||||
|
||||
cls->data.scannedAt = millis();
|
||||
|
||||
end:
|
||||
scan->running = false;
|
||||
xSemaphoreGive(cls->data.scanSem);
|
||||
LT_HEAP_I();
|
||||
return;
|
||||
}
|
||||
|
||||
int16_t WiFiClass::scanNetworks(bool async, bool showHidden, bool passive, uint32_t maxMsPerChannel, uint8_t channel) {
|
||||
if (scan && scan->running)
|
||||
return WIFI_SCAN_RUNNING;
|
||||
enableSTA(true);
|
||||
scanDelete();
|
||||
scanInit();
|
||||
|
||||
LT_I("Starting WiFi scan");
|
||||
|
||||
__wrap_bk_printf_disable();
|
||||
mhdr_scanu_reg_cb(scanHandler, this);
|
||||
bk_wlan_start_scan();
|
||||
__wrap_bk_printf_enable();
|
||||
|
||||
LT_HEAP_I();
|
||||
|
||||
scan->running = true;
|
||||
|
||||
if (!async) {
|
||||
LT_I("Waiting for results");
|
||||
xSemaphoreTake(data.scanSem, 1); // reset the semaphore quickly
|
||||
xSemaphoreTake(data.scanSem, pdMS_TO_TICKS(maxMsPerChannel * 20));
|
||||
return scan->count;
|
||||
}
|
||||
return WIFI_SCAN_RUNNING;
|
||||
}
|
||||
8
arduino/beken-72xx/libraries/WiFi/WiFiServer.h
Normal file
8
arduino/beken-72xx/libraries/WiFi/WiFiServer.h
Normal file
@@ -0,0 +1,8 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-27. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <api/WiFi/WiFi.h>
|
||||
#include <lwip/LwIPServer.h>
|
||||
|
||||
typedef LwIPServer WiFiServer;
|
||||
45
arduino/beken-72xx/port/flashdb/fal_flash_bk72xx_port.c
Normal file
45
arduino/beken-72xx/port/flashdb/fal_flash_bk72xx_port.c
Normal file
@@ -0,0 +1,45 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
|
||||
|
||||
#include <sdk_extern.h>
|
||||
|
||||
#include <fal.h>
|
||||
#include <flash_pub.h>
|
||||
|
||||
#define FLASH_ERASE_MIN_SIZE (4 * 1024)
|
||||
|
||||
extern uint32_t flash_ctrl(uint32_t cmd, void *param);
|
||||
|
||||
static int init() {
|
||||
__wrap_bk_printf_disable();
|
||||
flash_init();
|
||||
__wrap_bk_printf_enable();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int read(long offset, uint8_t *buf, size_t size) {
|
||||
flash_read((char *)buf, size, offset);
|
||||
return size;
|
||||
}
|
||||
|
||||
static int write(long offset, const uint8_t *buf, size_t size) {
|
||||
flash_write((char *)buf, size, offset);
|
||||
return size;
|
||||
}
|
||||
|
||||
static int erase(long offset, size_t size) {
|
||||
size = ((size - 1) / FLASH_ERASE_MIN_SIZE) + 1;
|
||||
for (uint16_t i = 0; i < size; i++) {
|
||||
uint32_t addr = offset + i * FLASH_ERASE_MIN_SIZE;
|
||||
flash_ctrl(CMD_FLASH_ERASE_SECTOR, &addr);
|
||||
}
|
||||
return size * FLASH_ERASE_MIN_SIZE;
|
||||
}
|
||||
|
||||
const struct fal_flash_dev flash0 = {
|
||||
.name = FAL_FLASH_DEV_NAME,
|
||||
.addr = 0x0,
|
||||
.len = FLASH_LENGTH,
|
||||
.blk_size = FLASH_ERASE_MIN_SIZE,
|
||||
.ops = {init, read, write, erase},
|
||||
.write_gran = 1,
|
||||
};
|
||||
17
arduino/beken-72xx/port/printf/printf.c
Normal file
17
arduino/beken-72xx/port/printf/printf.c
Normal file
@@ -0,0 +1,17 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
|
||||
|
||||
#include <printf_config.h>
|
||||
|
||||
#include <printf/printf.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
extern void bk_send_byte(uint8_t uport, uint8_t data);
|
||||
extern int uart_print_port;
|
||||
|
||||
void putchar_(char c) {
|
||||
bk_send_byte(uart_print_port, c);
|
||||
}
|
||||
|
||||
WRAP_PRINTF(bk_printf);
|
||||
7
arduino/beken-72xx/port/printf/printf_port.h
Normal file
7
arduino/beken-72xx/port/printf/printf_port.h
Normal file
@@ -0,0 +1,7 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-20. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <printf_config.h>
|
||||
|
||||
WRAP_DISABLE_DEF(bk_printf);
|
||||
@@ -1,2 +0,0 @@
|
||||
realtek-ambz Realtek AmebaZ Arduino Core
|
||||
libretuya Interfaces for LibreTuya Arduino cores
|
||||
@@ -22,10 +22,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <api/WiFi/WiFiEvents.h>
|
||||
#include <functional>
|
||||
|
||||
#include "WiFiEvents.h"
|
||||
|
||||
typedef enum {
|
||||
ARDUINO_EVENT_WIFI_READY = 0, /**< ESP32 WiFi ready */
|
||||
ARDUINO_EVENT_WIFI_SCAN_DONE, /**< ESP32 finish scanning AP */
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-04-24. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
uint8_t manufacturerId;
|
||||
uint8_t chipId;
|
||||
uint8_t chipSizeId;
|
||||
} FlashId;
|
||||
|
||||
class IFlashClass {
|
||||
public:
|
||||
IFlashClass() {}
|
||||
|
||||
~IFlashClass() {}
|
||||
|
||||
virtual FlashId getChipId() = 0;
|
||||
virtual uint32_t getSize() = 0;
|
||||
|
||||
virtual bool eraseSector(uint32_t sector) = 0;
|
||||
virtual bool readBlock(uint32_t offset, uint8_t *data, size_t size) = 0;
|
||||
virtual bool writeBlock(uint32_t offset, uint8_t *data, size_t size) = 0;
|
||||
};
|
||||
@@ -1,190 +0,0 @@
|
||||
/*
|
||||
WiFi.h - esp32 Wifi support.
|
||||
Based on WiFi.h from Arduino WiFi shield library.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
Modified by Ivan Grokhotkov, December 2014
|
||||
|
||||
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 <stdint.h>
|
||||
|
||||
#include <api/IPAddress.h>
|
||||
#include <api/IPv6Address.h>
|
||||
#include <api/Print.h>
|
||||
#include <vector>
|
||||
|
||||
#include "Events.h"
|
||||
#include "WiFiType.h"
|
||||
|
||||
class IWiFiClass {
|
||||
public:
|
||||
virtual void printDiag(Print &dest) = 0;
|
||||
};
|
||||
|
||||
class IWiFiGenericClass {
|
||||
public:
|
||||
virtual int32_t channel(void) = 0;
|
||||
virtual bool mode(WiFiMode mode) = 0;
|
||||
virtual WiFiMode getMode() = 0;
|
||||
virtual WiFiStatus status() = 0;
|
||||
|
||||
virtual bool enableSTA(bool enable) = 0;
|
||||
virtual bool enableAP(bool enable) = 0;
|
||||
|
||||
virtual bool setSleep(bool enable) = 0;
|
||||
virtual bool getSleep() = 0;
|
||||
|
||||
virtual bool setTxPower(int power) = 0;
|
||||
virtual int getTxPower() = 0;
|
||||
|
||||
virtual int hostByName(const char *hostname, IPAddress &aResult) {
|
||||
aResult = hostByName(hostname);
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual IPAddress hostByName(const char *hostname) = 0;
|
||||
|
||||
static IPAddress calculateNetworkID(IPAddress ip, IPAddress subnet);
|
||||
static IPAddress calculateBroadcast(IPAddress ip, IPAddress subnet);
|
||||
static uint8_t calculateSubnetCIDR(IPAddress subnetMask);
|
||||
static String macToString(uint8_t *mac);
|
||||
|
||||
protected:
|
||||
static std::vector<EventHandler> handlers;
|
||||
|
||||
public:
|
||||
uint16_t onEvent(EventCb callback, EventId eventId = ARDUINO_EVENT_MAX);
|
||||
uint16_t onEvent(EventFuncCb callback, EventId eventId = ARDUINO_EVENT_MAX);
|
||||
uint16_t onEvent(EventSysCb callback, EventId eventId = ARDUINO_EVENT_MAX);
|
||||
void removeEvent(EventCb callback, EventId eventId);
|
||||
void removeEvent(EventSysCb callback, EventId eventId);
|
||||
void removeEvent(uint16_t id);
|
||||
|
||||
protected:
|
||||
static void postEvent(EventId eventId, EventInfo eventInfo);
|
||||
};
|
||||
|
||||
class IWiFiSTAClass {
|
||||
public:
|
||||
virtual WiFiStatus begin(
|
||||
const char *ssid,
|
||||
const char *passphrase = NULL,
|
||||
int32_t channel = 0,
|
||||
const uint8_t *bssid = NULL,
|
||||
bool connect = true
|
||||
) = 0;
|
||||
virtual WiFiStatus begin(
|
||||
char *ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t *bssid = NULL, bool connect = true
|
||||
) = 0;
|
||||
|
||||
virtual bool config(
|
||||
IPAddress localIP,
|
||||
IPAddress gateway,
|
||||
IPAddress subnet,
|
||||
IPAddress dns1 = (uint32_t)0x00000000,
|
||||
IPAddress dns2 = (uint32_t)0x00000000
|
||||
) = 0;
|
||||
|
||||
virtual bool reconnect() = 0;
|
||||
virtual bool disconnect(bool wifiOff = false) = 0;
|
||||
|
||||
virtual bool isConnected();
|
||||
|
||||
virtual bool setAutoReconnect(bool autoReconnect) = 0;
|
||||
virtual bool getAutoReconnect() = 0;
|
||||
|
||||
virtual WiFiStatus waitForConnectResult(unsigned long timeout) = 0;
|
||||
|
||||
virtual IPAddress localIP() = 0;
|
||||
virtual uint8_t *macAddress(uint8_t *mac) = 0;
|
||||
virtual String macAddress() = 0;
|
||||
virtual IPAddress subnetMask() = 0;
|
||||
virtual IPAddress gatewayIP() = 0;
|
||||
virtual IPAddress dnsIP(uint8_t dns_no = 0) = 0;
|
||||
virtual IPAddress broadcastIP() = 0;
|
||||
virtual IPAddress networkID() = 0;
|
||||
virtual uint8_t subnetCIDR() = 0;
|
||||
virtual bool enableIpV6() = 0;
|
||||
virtual IPv6Address localIPv6() = 0;
|
||||
virtual const char *getHostname() = 0;
|
||||
virtual bool setHostname(const char *hostname) = 0;
|
||||
virtual bool setMacAddress(const uint8_t *mac) = 0;
|
||||
|
||||
inline bool hostname(const String &aHostname) {
|
||||
return setHostname(aHostname.c_str());
|
||||
}
|
||||
|
||||
virtual const String SSID() = 0;
|
||||
virtual const String psk() = 0;
|
||||
virtual uint8_t *BSSID() = 0;
|
||||
virtual String BSSIDstr() = 0;
|
||||
virtual int8_t RSSI() = 0;
|
||||
virtual WiFiAuthMode getEncryption() = 0;
|
||||
};
|
||||
|
||||
class IWiFiScanClass {
|
||||
public:
|
||||
virtual int16_t scanNetworks(
|
||||
bool async = false,
|
||||
bool showHidden = false,
|
||||
bool passive = false,
|
||||
uint32_t maxMsPerChannel = 300,
|
||||
uint8_t channel = 0
|
||||
) = 0;
|
||||
virtual bool getNetworkInfo(
|
||||
uint8_t networkItem,
|
||||
String &ssid,
|
||||
WiFiAuthMode &encryptionType,
|
||||
int32_t &RSSI,
|
||||
uint8_t *&BSSID,
|
||||
int32_t &channel
|
||||
) = 0;
|
||||
|
||||
virtual int16_t scanComplete() = 0;
|
||||
virtual void scanDelete() = 0;
|
||||
|
||||
virtual String SSID(uint8_t networkItem) = 0;
|
||||
virtual WiFiAuthMode encryptionType(uint8_t networkItem) = 0;
|
||||
virtual int32_t RSSI(uint8_t networkItem) = 0;
|
||||
virtual uint8_t *BSSID(uint8_t networkItem) = 0;
|
||||
virtual String BSSIDstr(uint8_t networkItem) = 0;
|
||||
virtual int32_t channel(uint8_t networkItem) = 0;
|
||||
};
|
||||
|
||||
class IWiFiAPClass {
|
||||
public:
|
||||
virtual bool softAP(
|
||||
const char *ssid, const char *passphrase = NULL, int channel = 1, bool ssidHidden = false, int maxClients = 4
|
||||
) = 0;
|
||||
virtual bool softAPConfig(IPAddress localIP, IPAddress gateway, IPAddress subnet) = 0;
|
||||
virtual bool softAPdisconnect(bool wifiOff = false) = 0;
|
||||
|
||||
virtual uint8_t softAPgetStationNum() = 0;
|
||||
|
||||
virtual IPAddress softAPIP() = 0;
|
||||
virtual IPAddress softAPBroadcastIP() = 0;
|
||||
virtual IPAddress softAPNetworkID() = 0;
|
||||
virtual uint8_t softAPSubnetCIDR() = 0;
|
||||
virtual bool softAPenableIpV6() = 0;
|
||||
virtual IPv6Address softAPIPv6() = 0;
|
||||
virtual const char *softAPgetHostname() = 0;
|
||||
virtual bool softAPsetHostname(const char *hostname) = 0;
|
||||
virtual uint8_t *softAPmacAddress(uint8_t *mac) = 0;
|
||||
virtual String softAPmacAddress(void) = 0;
|
||||
virtual const String softAPSSID(void) = 0;
|
||||
};
|
||||
46
arduino/libretuya/api/WiFi/WiFi.cpp
Normal file
46
arduino/libretuya/api/WiFi/WiFi.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-04-25. */
|
||||
|
||||
#include "WiFi.h"
|
||||
|
||||
void WiFiClass::printDiag(Print &dest) {
|
||||
const char *modes[] = {"NULL", "STA", "AP", "STA+AP"};
|
||||
const char *enc[] = {"Open", "WEP", "WPA PSK", "WPA2 PSK", "WPA/WPA2", "WPA", "WPA2"};
|
||||
|
||||
dest.print("Mode: ");
|
||||
dest.println(modes[getMode()]);
|
||||
|
||||
if (getMode() & WIFI_MODE_STA) {
|
||||
dest.println("-- Station --");
|
||||
dest.print("SSID: ");
|
||||
dest.println(SSID());
|
||||
if (isConnected()) {
|
||||
dest.print("BSSID: ");
|
||||
dest.println(BSSIDstr());
|
||||
dest.print("RSSI: ");
|
||||
dest.println(RSSI());
|
||||
dest.print("Encryption: ");
|
||||
dest.println(enc[getEncryption()]);
|
||||
dest.print("IP: ");
|
||||
dest.println(localIP());
|
||||
dest.print("MAC: ");
|
||||
dest.println(macAddress());
|
||||
dest.print("Hostname: ");
|
||||
dest.println(getHostname());
|
||||
}
|
||||
}
|
||||
|
||||
if (getMode() & WIFI_MODE_AP) {
|
||||
dest.println("-- Access Point --");
|
||||
dest.print("SSID: ");
|
||||
dest.println(softAPSSID());
|
||||
dest.print("IP: ");
|
||||
dest.println(softAPIP());
|
||||
dest.print("MAC: ");
|
||||
dest.println(softAPmacAddress());
|
||||
dest.print("Hostname: ");
|
||||
dest.println(softAPgetHostname());
|
||||
}
|
||||
}
|
||||
|
||||
WiFiClass WiFi;
|
||||
WiFiClass *pWiFi = NULL;
|
||||
194
arduino/libretuya/api/WiFi/WiFi.h
Normal file
194
arduino/libretuya/api/WiFi/WiFi.h
Normal file
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
WiFi.h - esp32 Wifi support.
|
||||
Based on WiFi.h from Arduino WiFi shield library.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
Modified by Ivan Grokhotkov, December 2014
|
||||
|
||||
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 <api/Events.h>
|
||||
#include <api/IPAddress.h>
|
||||
#include <api/IPv6Address.h>
|
||||
#include <vector>
|
||||
|
||||
#include "WiFiType.h"
|
||||
|
||||
#ifdef LT_ARD_HAS_WIFI
|
||||
// family's data structure
|
||||
#include <WiFiData.h>
|
||||
#endif
|
||||
|
||||
class WiFiClass {
|
||||
public:
|
||||
#ifdef LT_ARD_HAS_WIFI
|
||||
// must be public for WiFiEvents & WiFiScan static handlers
|
||||
WiFiData data;
|
||||
#endif
|
||||
WiFiScanData *scan = NULL;
|
||||
|
||||
public: /* WiFi.cpp */
|
||||
WiFiClass();
|
||||
~WiFiClass();
|
||||
void printDiag(Print &dest);
|
||||
|
||||
public: /* WiFiGeneric.cpp */
|
||||
bool mode(WiFiMode mode);
|
||||
bool modePriv(WiFiMode mode, WiFiModeAction sta, WiFiModeAction ap);
|
||||
WiFiMode getMode();
|
||||
WiFiStatus status();
|
||||
|
||||
bool enableSTA(bool enable);
|
||||
bool enableAP(bool enable);
|
||||
|
||||
bool setSleep(bool enable);
|
||||
bool getSleep();
|
||||
|
||||
bool setTxPower(int power);
|
||||
int getTxPower();
|
||||
|
||||
int hostByName(const char *hostname, IPAddress &aResult);
|
||||
IPAddress hostByName(const char *hostname);
|
||||
|
||||
static IPAddress calculateNetworkID(IPAddress ip, IPAddress subnet);
|
||||
static IPAddress calculateBroadcast(IPAddress ip, IPAddress subnet);
|
||||
static uint8_t calculateSubnetCIDR(IPAddress subnetMask);
|
||||
static String macToString(uint8_t *mac);
|
||||
|
||||
protected: /* WiFiEvents.cpp */
|
||||
static std::vector<EventHandler> handlers;
|
||||
|
||||
public: /* WiFiEvents.cpp */
|
||||
uint16_t onEvent(EventCb callback, EventId eventId = ARDUINO_EVENT_MAX);
|
||||
uint16_t onEvent(EventFuncCb callback, EventId eventId = ARDUINO_EVENT_MAX);
|
||||
uint16_t onEvent(EventSysCb callback, EventId eventId = ARDUINO_EVENT_MAX);
|
||||
void removeEvent(EventCb callback, EventId eventId);
|
||||
void removeEvent(EventSysCb callback, EventId eventId);
|
||||
void removeEvent(uint16_t id);
|
||||
static void postEvent(EventId eventId, EventInfo eventInfo);
|
||||
|
||||
public: /* WiFiSTA.cpp */
|
||||
WiFiStatus begin(
|
||||
const char *ssid,
|
||||
const char *passphrase = NULL,
|
||||
int32_t channel = 0,
|
||||
const uint8_t *bssid = NULL,
|
||||
bool connect = true
|
||||
);
|
||||
WiFiStatus
|
||||
begin(char *ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t *bssid = NULL, bool connect = true);
|
||||
|
||||
bool config(
|
||||
IPAddress localIP,
|
||||
IPAddress gateway,
|
||||
IPAddress subnet,
|
||||
IPAddress dns1 = (uint32_t)0x00000000,
|
||||
IPAddress dns2 = (uint32_t)0x00000000
|
||||
);
|
||||
|
||||
bool reconnect(const uint8_t *bssid = NULL);
|
||||
bool disconnect(bool wifiOff = false);
|
||||
|
||||
bool isConnected();
|
||||
|
||||
bool setAutoReconnect(bool autoReconnect);
|
||||
bool getAutoReconnect();
|
||||
|
||||
WiFiStatus waitForConnectResult(unsigned long timeout);
|
||||
|
||||
IPAddress localIP();
|
||||
uint8_t *macAddress(uint8_t *mac);
|
||||
String macAddress();
|
||||
IPAddress subnetMask();
|
||||
IPAddress gatewayIP();
|
||||
IPAddress dnsIP(uint8_t dns_no = 0);
|
||||
IPAddress broadcastIP();
|
||||
IPAddress networkID();
|
||||
uint8_t subnetCIDR();
|
||||
bool enableIpV6();
|
||||
IPv6Address localIPv6();
|
||||
const char *getHostname();
|
||||
bool setHostname(const char *hostname);
|
||||
bool setMacAddress(const uint8_t *mac);
|
||||
|
||||
inline bool hostname(const String &aHostname) {
|
||||
return setHostname(aHostname.c_str());
|
||||
}
|
||||
|
||||
const String SSID();
|
||||
const String psk();
|
||||
uint8_t *BSSID();
|
||||
String BSSIDstr();
|
||||
int32_t channel();
|
||||
int8_t RSSI();
|
||||
WiFiAuthMode getEncryption();
|
||||
|
||||
public: /* WiFiScan.cpp */
|
||||
int16_t scanNetworks(
|
||||
bool async = false,
|
||||
bool showHidden = false,
|
||||
bool passive = false,
|
||||
uint32_t maxMsPerChannel = 300,
|
||||
uint8_t channel = 0
|
||||
);
|
||||
bool getNetworkInfo(
|
||||
uint8_t networkItem,
|
||||
String &ssid,
|
||||
WiFiAuthMode &encryptionType,
|
||||
int32_t &RSSI,
|
||||
uint8_t *&BSSID,
|
||||
int32_t &channel
|
||||
);
|
||||
|
||||
int16_t scanComplete();
|
||||
uint8_t scanAlloc(uint8_t count);
|
||||
void scanInit();
|
||||
void scanDelete();
|
||||
|
||||
String SSID(uint8_t networkItem);
|
||||
WiFiAuthMode encryptionType(uint8_t networkItem);
|
||||
int32_t RSSI(uint8_t networkItem);
|
||||
uint8_t *BSSID(uint8_t networkItem);
|
||||
String BSSIDstr(uint8_t networkItem);
|
||||
int32_t channel(uint8_t networkItem);
|
||||
|
||||
public: /* WiFiAP.cpp */
|
||||
bool softAP(
|
||||
const char *ssid, const char *passphrase = NULL, int channel = 1, bool ssidHidden = false, int maxClients = 4
|
||||
);
|
||||
bool softAPConfig(IPAddress localIP, IPAddress gateway, IPAddress subnet);
|
||||
bool softAPdisconnect(bool wifiOff = false);
|
||||
|
||||
uint8_t softAPgetStationNum();
|
||||
|
||||
IPAddress softAPIP();
|
||||
IPAddress softAPBroadcastIP();
|
||||
IPAddress softAPNetworkID();
|
||||
uint8_t softAPSubnetCIDR();
|
||||
IPAddress softAPSubnetMask();
|
||||
bool softAPenableIpV6();
|
||||
IPv6Address softAPIPv6();
|
||||
const char *softAPgetHostname();
|
||||
bool softAPsetHostname(const char *hostname);
|
||||
uint8_t *softAPmacAddress(uint8_t *mac);
|
||||
String softAPmacAddress(void);
|
||||
const String softAPSSID(void);
|
||||
};
|
||||
|
||||
extern WiFiClass WiFi;
|
||||
extern WiFiClass *pWiFi;
|
||||
23
arduino/libretuya/api/WiFi/WiFiAP.cpp
Normal file
23
arduino/libretuya/api/WiFi/WiFiAP.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-04-25. */
|
||||
|
||||
#include "WiFi.h"
|
||||
|
||||
IPAddress WiFiClass::softAPBroadcastIP() {
|
||||
return calculateBroadcast(softAPIP(), softAPSubnetMask());
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::softAPNetworkID() {
|
||||
return calculateNetworkID(softAPIP(), softAPSubnetMask());
|
||||
}
|
||||
|
||||
uint8_t WiFiClass::softAPSubnetCIDR() {
|
||||
return calculateSubnetCIDR(softAPSubnetMask());
|
||||
}
|
||||
|
||||
__attribute__((weak)) bool WiFiClass::softAPenableIpV6() {
|
||||
return false;
|
||||
}
|
||||
|
||||
__attribute__((weak)) IPv6Address WiFiClass::softAPIPv6() {
|
||||
return IPv6Address();
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
#include "WiFi.h"
|
||||
|
||||
std::vector<EventHandler> IWiFiGenericClass::handlers;
|
||||
std::vector<EventHandler> WiFiClass::handlers;
|
||||
|
||||
uint16_t IWiFiGenericClass::onEvent(EventCb callback, EventId eventId) {
|
||||
uint16_t WiFiClass::onEvent(EventCb callback, EventId eventId) {
|
||||
if (!callback)
|
||||
return 0;
|
||||
EventHandler handler;
|
||||
@@ -14,7 +14,7 @@ uint16_t IWiFiGenericClass::onEvent(EventCb callback, EventId eventId) {
|
||||
return handler.id;
|
||||
}
|
||||
|
||||
uint16_t IWiFiGenericClass::onEvent(EventFuncCb callback, EventId eventId) {
|
||||
uint16_t WiFiClass::onEvent(EventFuncCb callback, EventId eventId) {
|
||||
if (!callback)
|
||||
return 0;
|
||||
EventHandler handler;
|
||||
@@ -24,7 +24,7 @@ uint16_t IWiFiGenericClass::onEvent(EventFuncCb callback, EventId eventId) {
|
||||
return handler.id;
|
||||
}
|
||||
|
||||
uint16_t IWiFiGenericClass::onEvent(EventSysCb callback, EventId eventId) {
|
||||
uint16_t WiFiClass::onEvent(EventSysCb callback, EventId eventId) {
|
||||
if (!callback)
|
||||
return 0;
|
||||
EventHandler handler;
|
||||
@@ -34,7 +34,7 @@ uint16_t IWiFiGenericClass::onEvent(EventSysCb callback, EventId eventId) {
|
||||
return handler.id;
|
||||
}
|
||||
|
||||
void IWiFiGenericClass::removeEvent(EventCb callback, EventId eventId) {
|
||||
void WiFiClass::removeEvent(EventCb callback, EventId eventId) {
|
||||
if (!callback)
|
||||
return;
|
||||
for (uint16_t i = 0; i < handlers.size(); i++) {
|
||||
@@ -45,7 +45,7 @@ void IWiFiGenericClass::removeEvent(EventCb callback, EventId eventId) {
|
||||
}
|
||||
}
|
||||
|
||||
void IWiFiGenericClass::removeEvent(EventSysCb callback, EventId eventId) {
|
||||
void WiFiClass::removeEvent(EventSysCb callback, EventId eventId) {
|
||||
if (!callback)
|
||||
return;
|
||||
for (uint16_t i = 0; i < handlers.size(); i++) {
|
||||
@@ -56,7 +56,7 @@ void IWiFiGenericClass::removeEvent(EventSysCb callback, EventId eventId) {
|
||||
}
|
||||
}
|
||||
|
||||
void IWiFiGenericClass::removeEvent(uint16_t id) {
|
||||
void WiFiClass::removeEvent(uint16_t id) {
|
||||
for (uint16_t i = 0; i < handlers.size(); i++) {
|
||||
EventHandler handler = handlers[i];
|
||||
if (handler.id == id) {
|
||||
@@ -65,7 +65,7 @@ void IWiFiGenericClass::removeEvent(uint16_t id) {
|
||||
}
|
||||
}
|
||||
|
||||
void IWiFiGenericClass::postEvent(EventId eventId, EventInfo eventInfo) {
|
||||
void WiFiClass::postEvent(EventId eventId, EventInfo eventInfo) {
|
||||
for (auto handler : handlers) {
|
||||
if (handler.eventId != ARDUINO_EVENT_MAX && handler.eventId != eventId)
|
||||
continue;
|
||||
93
arduino/libretuya/api/WiFi/WiFiGeneric.cpp
Normal file
93
arduino/libretuya/api/WiFi/WiFiGeneric.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-04-25. */
|
||||
|
||||
#include "WiFi.h"
|
||||
|
||||
bool WiFiClass::mode(WiFiMode mode) {
|
||||
// store a pointer to WiFi for WiFiEvents.cpp
|
||||
pWiFi = this;
|
||||
|
||||
WiFiMode currentMode = getMode();
|
||||
LT_D_WG("Mode changing %u -> %u", currentMode, mode);
|
||||
if (mode == currentMode)
|
||||
return true;
|
||||
|
||||
// get mode changes as 0/1
|
||||
WiFiModeAction sta = WiFiModeAction((mode & WIFI_MODE_STA) != (currentMode & WIFI_MODE_STA));
|
||||
WiFiModeAction ap = WiFiModeAction((mode & WIFI_MODE_AP) != (currentMode & WIFI_MODE_AP));
|
||||
// change 0/1 to 1/2
|
||||
sta = WiFiModeAction(sta + sta * (mode & WIFI_MODE_STA));
|
||||
ap = WiFiModeAction(ap + ap * (mode & WIFI_MODE_AP));
|
||||
// actually change the mode
|
||||
LT_HEAP_I();
|
||||
return modePriv(mode, sta, ap);
|
||||
}
|
||||
|
||||
bool WiFiClass::enableSTA(bool enable) {
|
||||
WiFiMode currentMode = getMode();
|
||||
if (((currentMode & WIFI_MODE_STA) != 0) != enable) {
|
||||
return mode((WiFiMode)(currentMode ^ WIFI_MODE_STA));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WiFiClass::enableAP(bool enable) {
|
||||
WiFiMode currentMode = getMode();
|
||||
if (((currentMode & WIFI_MODE_AP) != 0) != enable) {
|
||||
return mode((WiFiMode)(currentMode ^ WIFI_MODE_AP));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int WiFiClass::hostByName(const char *hostname, IPAddress &aResult) {
|
||||
aResult = hostByName(hostname);
|
||||
return true;
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::calculateNetworkID(IPAddress ip, IPAddress subnet) {
|
||||
IPAddress networkID;
|
||||
|
||||
for (size_t i = 0; i < 4; i++)
|
||||
networkID[i] = subnet[i] & ip[i];
|
||||
|
||||
return networkID;
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::calculateBroadcast(IPAddress ip, IPAddress subnet) {
|
||||
IPAddress broadcastIp;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
broadcastIp[i] = ~subnet[i] | ip[i];
|
||||
|
||||
return broadcastIp;
|
||||
}
|
||||
|
||||
uint8_t WiFiClass::calculateSubnetCIDR(IPAddress subnetMask) {
|
||||
uint8_t CIDR = 0;
|
||||
|
||||
for (uint8_t i = 0; i < 4; i++) {
|
||||
if (subnetMask[i] == 0x80) // 128
|
||||
CIDR += 1;
|
||||
else if (subnetMask[i] == 0xC0) // 192
|
||||
CIDR += 2;
|
||||
else if (subnetMask[i] == 0xE0) // 224
|
||||
CIDR += 3;
|
||||
else if (subnetMask[i] == 0xF0) // 242
|
||||
CIDR += 4;
|
||||
else if (subnetMask[i] == 0xF8) // 248
|
||||
CIDR += 5;
|
||||
else if (subnetMask[i] == 0xFC) // 252
|
||||
CIDR += 6;
|
||||
else if (subnetMask[i] == 0xFE) // 254
|
||||
CIDR += 7;
|
||||
else if (subnetMask[i] == 0xFF) // 255
|
||||
CIDR += 8;
|
||||
}
|
||||
|
||||
return CIDR;
|
||||
}
|
||||
|
||||
String WiFiClass::macToString(uint8_t *mac) {
|
||||
char macStr[18]; // 6*2 + 5*':' + '\0'
|
||||
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
return macStr;
|
||||
}
|
||||
48
arduino/libretuya/api/WiFi/WiFiSTA.cpp
Normal file
48
arduino/libretuya/api/WiFi/WiFiSTA.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-04-25. */
|
||||
|
||||
#include "WiFi.h"
|
||||
|
||||
WiFiStatus WiFiClass::begin(char *ssid, char *passphrase, int32_t channel, const uint8_t *bssid, bool connect) {
|
||||
return begin((const char *)ssid, (const char *)passphrase, channel, bssid, connect);
|
||||
}
|
||||
|
||||
bool WiFiClass::isConnected() {
|
||||
return status() == WL_CONNECTED;
|
||||
}
|
||||
|
||||
WiFiStatus WiFiClass::waitForConnectResult(unsigned long timeout) {
|
||||
if ((getMode() & WIFI_MODE_STA) == 0) {
|
||||
return WL_DISCONNECTED;
|
||||
}
|
||||
unsigned long start = millis();
|
||||
while ((!status() || status() >= WL_DISCONNECTED) && (millis() - start) < timeout) {
|
||||
delay(100);
|
||||
}
|
||||
return status();
|
||||
}
|
||||
|
||||
String WiFiClass::macAddress(void) {
|
||||
uint8_t mac[6];
|
||||
macAddress(mac);
|
||||
return macToString(mac);
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::networkID() {
|
||||
return calculateNetworkID(gatewayIP(), subnetMask());
|
||||
}
|
||||
|
||||
uint8_t WiFiClass::subnetCIDR() {
|
||||
return calculateSubnetCIDR(subnetMask());
|
||||
}
|
||||
|
||||
String WiFiClass::BSSIDstr() {
|
||||
return macToString(BSSID());
|
||||
}
|
||||
|
||||
__attribute__((weak)) bool WiFiClass::enableIpV6() {
|
||||
return false;
|
||||
}
|
||||
|
||||
__attribute__((weak)) IPv6Address WiFiClass::localIPv6() {
|
||||
return IPv6Address();
|
||||
}
|
||||
83
arduino/libretuya/api/WiFi/WiFiScan.cpp
Normal file
83
arduino/libretuya/api/WiFi/WiFiScan.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-04-25. */
|
||||
|
||||
#include "WiFi.h"
|
||||
|
||||
bool WiFiClass::getNetworkInfo(
|
||||
uint8_t networkItem, String &ssid, WiFiAuthMode &encType, int32_t &rssi, uint8_t *&bssid, int32_t &channel
|
||||
) {
|
||||
ssid = SSID(networkItem);
|
||||
encType = encryptionType(networkItem);
|
||||
rssi = RSSI(networkItem);
|
||||
bssid = BSSID(networkItem);
|
||||
channel = this->channel(networkItem);
|
||||
return true;
|
||||
}
|
||||
|
||||
int16_t WiFiClass::scanComplete() {
|
||||
if (!scan)
|
||||
return 0;
|
||||
if (scan->running)
|
||||
return WIFI_SCAN_RUNNING;
|
||||
return scan->count;
|
||||
}
|
||||
|
||||
void WiFiClass::scanInit() {
|
||||
if (scan)
|
||||
return;
|
||||
scan = (WiFiScanData *)zalloc(sizeof(WiFiScanData));
|
||||
}
|
||||
|
||||
void WiFiClass::scanDelete() {
|
||||
if (!scan)
|
||||
return;
|
||||
for (uint8_t i = 0; i < scan->count; i++) {
|
||||
free(scan->ap[i].ssid);
|
||||
}
|
||||
free(scan->ap);
|
||||
free(scan);
|
||||
scan = NULL;
|
||||
}
|
||||
|
||||
uint8_t WiFiClass::scanAlloc(uint8_t count) {
|
||||
uint8_t last = scan->count;
|
||||
scan->count = count;
|
||||
scan->ap = (WiFiScanAP *)realloc(scan->ap, count * sizeof(WiFiScanAP));
|
||||
if (!scan->ap)
|
||||
return 255;
|
||||
memset(scan->ap + last, 0, sizeof(WiFiScanAP));
|
||||
return last;
|
||||
}
|
||||
|
||||
String WiFiClass::SSID(uint8_t networkItem) {
|
||||
if (!scan || networkItem >= scan->count)
|
||||
return "";
|
||||
return scan->ap[networkItem].ssid;
|
||||
}
|
||||
|
||||
WiFiAuthMode WiFiClass::encryptionType(uint8_t networkItem) {
|
||||
if (!scan || networkItem >= scan->count)
|
||||
return WIFI_AUTH_INVALID;
|
||||
return scan->ap[networkItem].auth;
|
||||
}
|
||||
|
||||
int32_t WiFiClass::RSSI(uint8_t networkItem) {
|
||||
if (!scan || networkItem >= scan->count)
|
||||
return 0;
|
||||
return scan->ap[networkItem].rssi;
|
||||
}
|
||||
|
||||
uint8_t *WiFiClass::BSSID(uint8_t networkItem) {
|
||||
if (!scan || networkItem >= scan->count)
|
||||
return NULL;
|
||||
return scan->ap[networkItem].bssid.addr;
|
||||
}
|
||||
|
||||
String WiFiClass::BSSIDstr(uint8_t networkItem) {
|
||||
return macToString(BSSID(networkItem));
|
||||
}
|
||||
|
||||
int32_t WiFiClass::channel(uint8_t networkItem) {
|
||||
if (!scan || networkItem >= scan->count)
|
||||
return 0;
|
||||
return scan->ap[networkItem].channel;
|
||||
}
|
||||
@@ -21,6 +21,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#define WIFI_SCAN_RUNNING (-1)
|
||||
#define WIFI_SCAN_FAILED (-2)
|
||||
|
||||
@@ -38,6 +40,10 @@
|
||||
#define WiFiEventInfo_t arduino_event_info_t
|
||||
#define WiFiEventId_t uint16_t
|
||||
|
||||
typedef struct {
|
||||
uint8_t addr[6];
|
||||
} WiFiMacAddr;
|
||||
|
||||
struct esp_ip6_addr {
|
||||
uint32_t addr[4];
|
||||
uint8_t zone;
|
||||
@@ -121,3 +127,23 @@ typedef enum {
|
||||
WIFI_REASON_AP_TSF_RESET = 206,
|
||||
WIFI_REASON_ROAMING = 207,
|
||||
} wifi_err_reason_t;
|
||||
|
||||
typedef struct {
|
||||
char *ssid;
|
||||
WiFiAuthMode auth;
|
||||
int32_t rssi;
|
||||
WiFiMacAddr bssid;
|
||||
int32_t channel;
|
||||
} WiFiScanAP;
|
||||
|
||||
typedef struct {
|
||||
bool running = false;
|
||||
uint8_t count = 0;
|
||||
WiFiScanAP *ap = NULL;
|
||||
} WiFiScanData;
|
||||
|
||||
typedef enum {
|
||||
WLMODE_NONE = 0,
|
||||
WLMODE_DISABLE = 1,
|
||||
WLMODE_ENABLE = 2,
|
||||
} WiFiModeAction;
|
||||
53
arduino/libretuya/common/main.cpp
Normal file
53
arduino/libretuya/common/main.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <api/HardwareSerial.h>
|
||||
|
||||
using namespace arduino;
|
||||
|
||||
extern "C" {
|
||||
#include <fal.h>
|
||||
fal_partition_t fal_root_part = NULL;
|
||||
}
|
||||
|
||||
// Arduino framework initialization.
|
||||
// May be redefined by family files.
|
||||
void init() __attribute__((weak));
|
||||
|
||||
// Weak empty variant initialization function.
|
||||
// May be redefined by variant files.
|
||||
void initVariant() __attribute__((weak));
|
||||
|
||||
// Initialize C library
|
||||
extern "C" void __libc_init_array(void);
|
||||
|
||||
void main_task(const void *arg) {
|
||||
setup();
|
||||
|
||||
for (;;) {
|
||||
loop();
|
||||
if (serialEventRun)
|
||||
serialEventRun();
|
||||
yield();
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
// print a startup banner
|
||||
LT_BANNER();
|
||||
// initialize Arduino framework
|
||||
init();
|
||||
// initialize C library
|
||||
__libc_init_array();
|
||||
// optionally initialize per-variant code
|
||||
initVariant();
|
||||
// initialize FAL
|
||||
fal_init();
|
||||
// provide root partition
|
||||
fal_root_part = (fal_partition_t)fal_partition_find("root");
|
||||
// start the main task and OS kernel
|
||||
startMainTask();
|
||||
|
||||
while (1) {}
|
||||
return 0;
|
||||
}
|
||||
11
arduino/libretuya/common/serial_event.cpp
Normal file
11
arduino/libretuya/common/serial_event.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
void serialEvent() __attribute__((weak));
|
||||
bool Serial_available() __attribute__((weak));
|
||||
|
||||
void serialEventRun(void) {
|
||||
if (Serial_available && serialEvent && Serial_available())
|
||||
serialEvent();
|
||||
}
|
||||
6
arduino/libretuya/compat/md5.h
Normal file
6
arduino/libretuya/compat/md5.h
Normal file
@@ -0,0 +1,6 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-04. */
|
||||
|
||||
#pragma once
|
||||
|
||||
// lowercase "md5.h" to allow including actual MD5.h on case-sensitive OSes
|
||||
#include <MD5.h>
|
||||
@@ -1,23 +1,29 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-05-28. */
|
||||
|
||||
#define CHIP_TYPE(family, chip_id) (((family >> 24) << 8) | chip_id)
|
||||
#define CHIP_TYPE_ENUM(family, chip_id) (ChipType) CHIP_TYPE(family, chip_id)
|
||||
|
||||
enum ChipFamily {
|
||||
// used in UF2 Family ID
|
||||
RTL8710A = 0x9FFFD543, // Realtek Ameba1
|
||||
RTL8710B = 0x22E0D6FC, // Realtek AmebaZ (realtek-ambz)
|
||||
RTL8720C = 0xE08F7564, // Realtek AmebaZ2
|
||||
RTL8720D = 0x3379CFE2, // Realtek AmebaD
|
||||
BK7231T = 0x675A40B0, // Beken 7231T
|
||||
BK7231N = 0x7B3EF230, // Beken 7231N
|
||||
BL602 = 0xDE1270B7, // Boufallo 602
|
||||
XR809 = 0x51E903A8, // Xradiotech 809
|
||||
F_RTL8710A = 0x9FFFD543, // Realtek Ameba1
|
||||
F_RTL8710B = 0x22E0D6FC, // Realtek AmebaZ (realtek-ambz)
|
||||
F_RTL8720C = 0xE08F7564, // Realtek AmebaZ2
|
||||
F_RTL8720D = 0x3379CFE2, // Realtek AmebaD
|
||||
F_BK7231T = 0x675A40B0, // Beken 7231T
|
||||
F_BK7231N = 0x7B3EF230, // Beken 7231N
|
||||
F_BL602 = 0xDE1270B7, // Boufallo 602
|
||||
F_XR809 = 0x51E903A8, // Xradiotech 809
|
||||
F_NATIVE = 0xDEADBEEF, // Host-native
|
||||
};
|
||||
|
||||
enum ChipType {
|
||||
// Realtek AmebaZ
|
||||
// IDs copied from rtl8710b_efuse.h
|
||||
RTL8710BL = ((RTL8710B >> 24) << 8) | 0xE0, // ???
|
||||
RTL8710BN = ((RTL8710B >> 24) << 8) | 0xFF, // CHIPID_8710BN / QFN32
|
||||
RTL8710BU = ((RTL8710B >> 24) << 8) | 0xFE, // CHIPID_8710BU / QFN48
|
||||
RTL8710BX = ((RTL8710B >> 24) << 8) | 0xFB, // CHIPID_8710BN_L0 / QFN32
|
||||
RTL8711BN = ((RTL8710B >> 24) << 8) | 0xFD, // CHIPID_8711BN / QFN48
|
||||
RTL8711BU = ((RTL8710B >> 24) << 8) | 0xFC, // CHIPID_8711BG / QFN68
|
||||
RTL8710BL = CHIP_TYPE(F_RTL8710B, 0xE0), // ???
|
||||
RTL8710BN = CHIP_TYPE(F_RTL8710B, 0xFF), // CHIPID_8710BN / QFN32
|
||||
RTL8710BU = CHIP_TYPE(F_RTL8710B, 0xFE), // CHIPID_8710BU / QFN48
|
||||
RTL8710BX = CHIP_TYPE(F_RTL8710B, 0xF6), // found on an actual RTL8710BX
|
||||
RTL8710L0 = CHIP_TYPE(F_RTL8710B, 0xFB), // CHIPID_8710BN_L0 / QFN32
|
||||
RTL8711BN = CHIP_TYPE(F_RTL8710B, 0xFD), // CHIPID_8711BN / QFN48
|
||||
RTL8711BU = CHIP_TYPE(F_RTL8710B, 0xFC), // CHIPID_8711BG / QFN68
|
||||
};
|
||||
|
||||
@@ -27,6 +27,8 @@ void lt_rand_bytes(uint8_t *buf, size_t len) {
|
||||
}
|
||||
}
|
||||
|
||||
#undef putchar
|
||||
|
||||
/**
|
||||
* @brief Print data pointed to by buf in hexdump-like format (hex+ASCII).
|
||||
*
|
||||
@@ -53,110 +55,9 @@ void hexdump(uint8_t *buf, size_t len, uint32_t offset, uint8_t width) {
|
||||
printf(" |");
|
||||
for (uint8_t i = 0; i < lineWidth; i++) {
|
||||
char c = buf[pos + i];
|
||||
printf("%c", isprint(c) ? c : '.');
|
||||
putchar((c >= 0x20 && c <= 0x7f) ? c : '.');
|
||||
}
|
||||
printf("|\n");
|
||||
puts("|\r");
|
||||
pos += lineWidth;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get LibreTuya version string.
|
||||
*/
|
||||
const char *LibreTuya::getVersion() {
|
||||
return LT_VERSION_STR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get board name.
|
||||
*/
|
||||
const char *LibreTuya::getBoard() {
|
||||
return LT_BOARD_STR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get CPU family ID.
|
||||
*/
|
||||
ChipFamily LibreTuya::getChipFamily() {
|
||||
return FAMILY;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get CPU family name as string.
|
||||
*/
|
||||
const char *LibreTuya::getChipFamilyName() {
|
||||
return STRINGIFY_MACRO(FAMILY);
|
||||
}
|
||||
|
||||
static char *deviceName = NULL;
|
||||
|
||||
/**
|
||||
* @brief Get device friendly name in format "LT-<board>-<chip id>".
|
||||
* Can be used as hostname.
|
||||
*/
|
||||
const char *LibreTuya::getDeviceName() {
|
||||
if (deviceName)
|
||||
return deviceName;
|
||||
uint32_t chipId = getChipId();
|
||||
uint8_t *id = (uint8_t *)&chipId;
|
||||
|
||||
const char *board = getBoard();
|
||||
uint8_t boardLen = strlen(board);
|
||||
deviceName = (char *)malloc(3 + boardLen + 1 + 6 + 1);
|
||||
|
||||
sprintf(deviceName, "LT-%s-%02x%02x%02x", board, id[0], id[1], id[2]);
|
||||
return deviceName;
|
||||
}
|
||||
|
||||
static uint8_t otaRunningIndex = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the currently running firmware OTA index.
|
||||
*/
|
||||
uint8_t LibreTuya::otaGetRunning() {
|
||||
if (otaRunningIndex)
|
||||
return otaRunningIndex;
|
||||
// otaRunningIndex will be correct even after switchOta()
|
||||
return otaRunningIndex = otaGetStoredIndex();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the OTA index for updated firmware.
|
||||
*
|
||||
* Note: returns 1 for chips without dual-OTA.
|
||||
*/
|
||||
uint8_t LibreTuya::otaGetTarget() {
|
||||
if (!otaSupportsDual())
|
||||
return 1;
|
||||
return otaGetRunning() ^ 0b11;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Perform OTA rollback.
|
||||
*
|
||||
* @return false if no second image to run, writing failed or dual-OTA not supported
|
||||
*/
|
||||
bool LibreTuya::otaRollback() {
|
||||
if (!otaCanRollback())
|
||||
return false;
|
||||
if (otaGetRunning() != otaGetStoredIndex())
|
||||
// force switching back to current image
|
||||
return otaSwitch(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if OTA rollback is supported and available (there is another image to run).
|
||||
* @return false if no second image to run or dual-OTA not supported
|
||||
*/
|
||||
bool LibreTuya::otaCanRollback() {
|
||||
if (!otaSupportsDual())
|
||||
return false;
|
||||
if (otaGetRunning() == otaGetStoredIndex())
|
||||
return true;
|
||||
if (otaGetRunning() == 1 && otaHasImage1())
|
||||
return true;
|
||||
if (otaGetRunning() == 2 && otaHasImage2())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
// C standard libraries
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// LibreTuya version macros
|
||||
#ifndef LT_VERSION
|
||||
#define LT_VERSION 1.0.0
|
||||
@@ -15,7 +20,10 @@
|
||||
#define LT_BOARD_STR STRINGIFY_MACRO(LT_BOARD)
|
||||
|
||||
// Includes
|
||||
#include "LibreTuyaConfig.h"
|
||||
#include "LibreTuyaClass.h" // global LT class
|
||||
#include "LibreTuyaCompat.h" // compatibility methods
|
||||
#include "LibreTuyaConfig.h" // configuration macros
|
||||
#include "LibreTuyaCustom.h" // family-defined methods (Wiring custom)
|
||||
#include <Arduino.h>
|
||||
|
||||
// C includes
|
||||
@@ -39,10 +47,6 @@ extern "C" {
|
||||
"LibreTuya v" LT_VERSION_STR " on " LT_BOARD_STR ", compiled at " __DATE__ " " __TIME__ \
|
||||
)
|
||||
|
||||
// ArduinoCore-API doesn't define these anymore
|
||||
#define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
|
||||
#define PGM_VOID_P const void *
|
||||
|
||||
void lt_rand_bytes(uint8_t *buf, size_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -51,131 +55,3 @@ void hexdump(uint8_t *buf, size_t len, uint32_t offset = 0, uint8_t width = 16);
|
||||
#else
|
||||
void hexdump(uint8_t *buf, size_t len, uint32_t offset, uint8_t width);
|
||||
#endif
|
||||
|
||||
// Main class
|
||||
#ifdef __cplusplus
|
||||
|
||||
#include <Flash.h> // for flash inline methods
|
||||
#include <core/ChipType.h>
|
||||
|
||||
/**
|
||||
* @brief Main LibreTuya API class.
|
||||
*
|
||||
* This class contains all functions common amongst all families.
|
||||
* Implementations of these methods may vary between families.
|
||||
*
|
||||
* The class is accessible using the `LT` global object (defined by the family).
|
||||
*/
|
||||
class LibreTuya {
|
||||
public: /* Common methods - note: these are documented in LibreTuyaAPI.cpp */
|
||||
const char *getVersion();
|
||||
const char *getBoard();
|
||||
ChipFamily getChipFamily();
|
||||
const char *getChipFamilyName();
|
||||
const char *getDeviceName();
|
||||
uint8_t otaGetRunning();
|
||||
uint8_t otaGetTarget();
|
||||
bool otaRollback();
|
||||
bool otaCanRollback();
|
||||
|
||||
public: /* Inline methods */
|
||||
inline uint32_t getFlashChipSize() {
|
||||
return Flash.getSize();
|
||||
}
|
||||
|
||||
// inline bool flashEraseSector(uint32_t sector) {}
|
||||
// inline bool flashWrite(uint32_t offset, uint32_t *data, size_t size) {}
|
||||
// inline bool flashRead(uint32_t offset, uint32_t *data, size_t size) {}
|
||||
// inline bool partitionEraseRange(const esp_partition_t *partition, uint32_t offset, size_t size) {}
|
||||
// inline bool partitionWrite(const esp_partition_t *partition, uint32_t offset, uint32_t *data, size_t size) {}
|
||||
// inline bool partitionRead(const esp_partition_t *partition, uint32_t offset, uint32_t *data, size_t size) {}
|
||||
|
||||
public: /* Family-defined methods */
|
||||
/**
|
||||
* @brief Reboot the CPU.
|
||||
*/
|
||||
void restart();
|
||||
|
||||
public: /* CPU-related */
|
||||
/**
|
||||
* @brief Get CPU model ID.
|
||||
*/
|
||||
ChipType getChipType();
|
||||
/**
|
||||
* @brief Get CPU model name as string.
|
||||
*/
|
||||
const char *getChipModel();
|
||||
/**
|
||||
* @brief Get CPU unique ID. This may be based on MAC, eFuse, etc.
|
||||
*/
|
||||
uint32_t getChipId();
|
||||
/**
|
||||
* @brief Get CPU core count.
|
||||
*/
|
||||
uint8_t getChipCores();
|
||||
/**
|
||||
* @brief Get CPU core type name as string.
|
||||
*/
|
||||
const char *getChipCoreType();
|
||||
/**
|
||||
* @brief Get CPU frequency in MHz.
|
||||
*/
|
||||
uint32_t getCpuFreqMHz();
|
||||
/**
|
||||
* @brief Get CPU cycle count.
|
||||
*/
|
||||
inline uint32_t getCycleCount() __attribute__((always_inline));
|
||||
|
||||
public: /* Memory management */
|
||||
/**
|
||||
* @brief Get total RAM size.
|
||||
*/
|
||||
uint32_t getRamSize();
|
||||
/**
|
||||
* @brief Get total heap size.
|
||||
*/
|
||||
uint32_t getHeapSize();
|
||||
/**
|
||||
* @brief Get free heap size.
|
||||
*/
|
||||
uint32_t getFreeHeap();
|
||||
/**
|
||||
* @brief Get lowest level of free heap memory.
|
||||
*/
|
||||
uint32_t getMinFreeHeap();
|
||||
/**
|
||||
* @brief Get largest block of heap that can be allocated at once.
|
||||
*/
|
||||
uint32_t getMaxAllocHeap();
|
||||
|
||||
public: /* OTA-related */
|
||||
/**
|
||||
* @brief Read the currently active OTA index, i.e. the one that will boot upon restart.
|
||||
*/
|
||||
uint8_t otaGetStoredIndex();
|
||||
/**
|
||||
* @brief Check if the chip supports dual-OTA.
|
||||
*/
|
||||
bool otaSupportsDual();
|
||||
/**
|
||||
* @brief Check if OTA1 image is valid.
|
||||
*/
|
||||
bool otaHasImage1();
|
||||
/**
|
||||
* @brief Check if OTA2 image is valid.
|
||||
*/
|
||||
bool otaHasImage2();
|
||||
/**
|
||||
* @brief Try to switch OTA index to the other image.
|
||||
*
|
||||
* Note: should return true for chips without dual-OTA. Should return false if one of two images is not valid.
|
||||
*
|
||||
* @param force switch even if other image already marked as active
|
||||
* @return false if writing failed; true otherwise
|
||||
*/
|
||||
bool otaSwitch(bool force = false);
|
||||
};
|
||||
|
||||
extern LibreTuya LT;
|
||||
extern LibreTuya ESP;
|
||||
#endif
|
||||
|
||||
132
arduino/libretuya/core/LibreTuyaClass.cpp
Normal file
132
arduino/libretuya/core/LibreTuyaClass.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-06. */
|
||||
|
||||
#include "LibreTuyaClass.h"
|
||||
|
||||
/**
|
||||
* @brief Get LibreTuya version string.
|
||||
*/
|
||||
const char *LibreTuya::getVersion() {
|
||||
return LT_VERSION_STR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get board name.
|
||||
*/
|
||||
const char *LibreTuya::getBoard() {
|
||||
return LT_BOARD_STR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get CPU family ID.
|
||||
*/
|
||||
ChipFamily LibreTuya::getChipFamily() {
|
||||
return FAMILY;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get CPU family name as string.
|
||||
*/
|
||||
const char *LibreTuya::getChipFamilyName() {
|
||||
return STRINGIFY_MACRO(FAMILY) + 2;
|
||||
}
|
||||
|
||||
static char *deviceName = NULL;
|
||||
|
||||
/**
|
||||
* @brief Get device friendly name in format "LT-<board>-<chip id>".
|
||||
* Can be used as hostname.
|
||||
*/
|
||||
const char *LibreTuya::getDeviceName() {
|
||||
if (deviceName)
|
||||
return deviceName;
|
||||
uint32_t chipId = getChipId();
|
||||
uint8_t *id = (uint8_t *)&chipId;
|
||||
|
||||
const char *board = getBoard();
|
||||
uint8_t boardLen = strlen(board);
|
||||
deviceName = (char *)malloc(3 + boardLen + 1 + 6 + 1);
|
||||
|
||||
sprintf(deviceName, "LT-%s-%02x%02x%02x", board, id[0], id[1], id[2]);
|
||||
return deviceName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get CPU frequency in MHz.
|
||||
*/
|
||||
uint32_t LibreTuya::getCpuFreqMHz() {
|
||||
return getCpuFreq() / 1000000;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get flash chip total size.
|
||||
* The default implementation uses the least significant
|
||||
* byte of the chip ID to determine the size.
|
||||
*/
|
||||
__attribute__((weak)) uint32_t LibreTuya::getFlashChipSize() {
|
||||
FlashId id = getFlashChipId();
|
||||
if (id.chipSizeId >= 0x14 && id.chipSizeId <= 0x19) {
|
||||
return (1 << id.chipSizeId);
|
||||
}
|
||||
#ifdef FLASH_LENGTH
|
||||
return FLASH_LENGTH;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static uint8_t otaRunningIndex = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the currently running firmware OTA index.
|
||||
*/
|
||||
uint8_t LibreTuya::otaGetRunning() {
|
||||
if (otaRunningIndex)
|
||||
return otaRunningIndex;
|
||||
// otaRunningIndex will be correct even after switchOta()
|
||||
return otaRunningIndex = otaGetStoredIndex();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the OTA index for updated firmware.
|
||||
*
|
||||
* Note: returns 1 for chips without dual-OTA.
|
||||
*/
|
||||
uint8_t LibreTuya::otaGetTarget() {
|
||||
if (!otaSupportsDual())
|
||||
return 1;
|
||||
return otaGetRunning() ^ 0b11;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Perform OTA rollback.
|
||||
*
|
||||
* @return false if no second image to run, writing failed or dual-OTA not supported
|
||||
*/
|
||||
bool LibreTuya::otaRollback() {
|
||||
if (!otaCanRollback())
|
||||
return false;
|
||||
if (otaGetRunning() != otaGetStoredIndex())
|
||||
// force switching back to current image
|
||||
return otaSwitch(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if OTA rollback is supported and available (there is another image to run).
|
||||
* @return false if no second image to run or dual-OTA not supported
|
||||
*/
|
||||
bool LibreTuya::otaCanRollback() {
|
||||
if (!otaSupportsDual())
|
||||
return false;
|
||||
if (otaGetRunning() == otaGetStoredIndex())
|
||||
return true;
|
||||
if (otaGetRunning() == 1 && otaHasImage1())
|
||||
return true;
|
||||
if (otaGetRunning() == 2 && otaHasImage2())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
__attribute__((weak)) void LibreTuya::gpioRecover() {
|
||||
// nop by default
|
||||
}
|
||||
142
arduino/libretuya/core/LibreTuyaClass.h
Normal file
142
arduino/libretuya/core/LibreTuyaClass.h
Normal file
@@ -0,0 +1,142 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-06. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
#include "LibreTuyaAPI.h"
|
||||
#include <core/ChipType.h>
|
||||
|
||||
/**
|
||||
* @brief Flash chip ID structure.
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t manufacturerId;
|
||||
uint8_t chipId;
|
||||
uint8_t chipSizeId;
|
||||
} FlashId;
|
||||
|
||||
/**
|
||||
* @brief Main LibreTuya API class.
|
||||
*
|
||||
* This class contains all functions common amongst all families.
|
||||
* Implementations of these methods may vary between families.
|
||||
*
|
||||
* The class is accessible using the `LT` global object (defined by the family).
|
||||
*/
|
||||
class LibreTuya {
|
||||
public: /* Common methods - note: these are documented in LibreTuyaAPI.cpp */
|
||||
const char *getVersion();
|
||||
const char *getBoard();
|
||||
ChipFamily getChipFamily();
|
||||
const char *getChipFamilyName();
|
||||
const char *getDeviceName();
|
||||
uint32_t getCpuFreqMHz();
|
||||
uint32_t getFlashChipSize();
|
||||
uint8_t otaGetRunning();
|
||||
uint8_t otaGetTarget();
|
||||
bool otaRollback();
|
||||
bool otaCanRollback();
|
||||
|
||||
public: /* Family-defined methods */
|
||||
/**
|
||||
* @brief Reboot the CPU.
|
||||
*/
|
||||
void restart();
|
||||
/**
|
||||
* @brief Reconfigure GPIO pins used for debugging
|
||||
* (SWD/JTAG), so that they can be used as normal I/O.
|
||||
*/
|
||||
void gpioRecover();
|
||||
|
||||
public: /* CPU-related */
|
||||
/**
|
||||
* @brief Get CPU model ID.
|
||||
*/
|
||||
ChipType getChipType();
|
||||
/**
|
||||
* @brief Get CPU model name as string.
|
||||
*/
|
||||
const char *getChipModel();
|
||||
/**
|
||||
* @brief Get CPU unique ID. This may be based on MAC, eFuse, etc.
|
||||
* Note: the number should be 24-bit (with most significant byte being zero).
|
||||
*/
|
||||
uint32_t getChipId();
|
||||
/**
|
||||
* @brief Get CPU core count.
|
||||
*/
|
||||
uint8_t getChipCores();
|
||||
/**
|
||||
* @brief Get CPU core type name as string.
|
||||
*/
|
||||
const char *getChipCoreType();
|
||||
/**
|
||||
* @brief Get CPU frequency in Hz.
|
||||
*/
|
||||
uint32_t getCpuFreq();
|
||||
/**
|
||||
* @brief Get CPU cycle count.
|
||||
*/
|
||||
uint32_t getCycleCount();
|
||||
|
||||
public: /* Flash memory utilities */
|
||||
/**
|
||||
* @brief Read flash chip ID and return a FlashId struct.
|
||||
*/
|
||||
FlashId getFlashChipId();
|
||||
|
||||
public: /* Memory management */
|
||||
/**
|
||||
* @brief Get total RAM size.
|
||||
*/
|
||||
uint32_t getRamSize();
|
||||
/**
|
||||
* @brief Get total heap size.
|
||||
*/
|
||||
uint32_t getHeapSize();
|
||||
/**
|
||||
* @brief Get free heap size.
|
||||
*/
|
||||
uint32_t getFreeHeap();
|
||||
/**
|
||||
* @brief Get lowest level of free heap memory.
|
||||
*/
|
||||
uint32_t getMinFreeHeap();
|
||||
/**
|
||||
* @brief Get largest block of heap that can be allocated at once.
|
||||
*/
|
||||
uint32_t getMaxAllocHeap();
|
||||
|
||||
public: /* OTA-related */
|
||||
/**
|
||||
* @brief Read the currently active OTA index, i.e. the one that will boot upon restart.
|
||||
*/
|
||||
uint8_t otaGetStoredIndex();
|
||||
/**
|
||||
* @brief Check if the chip supports dual-OTA.
|
||||
*/
|
||||
bool otaSupportsDual();
|
||||
/**
|
||||
* @brief Check if OTA1 image is valid.
|
||||
*/
|
||||
bool otaHasImage1();
|
||||
/**
|
||||
* @brief Check if OTA2 image is valid.
|
||||
*/
|
||||
bool otaHasImage2();
|
||||
/**
|
||||
* @brief Try to switch OTA index to the other image.
|
||||
*
|
||||
* Note: should return true for chips without dual-OTA. Should return false if one of two images is not valid.
|
||||
*
|
||||
* @param force switch even if other image already marked as active
|
||||
* @return false if writing failed; true otherwise
|
||||
*/
|
||||
bool otaSwitch(bool force = false);
|
||||
};
|
||||
|
||||
extern LibreTuya LT;
|
||||
extern LibreTuya ESP;
|
||||
|
||||
#endif
|
||||
33
arduino/libretuya/core/LibreTuyaCompat.cpp
Normal file
33
arduino/libretuya/core/LibreTuyaCompat.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-04. */
|
||||
|
||||
#include "LibreTuyaCompat.h"
|
||||
|
||||
#if LT_HAS_FREERTOS
|
||||
BaseType_t xTaskCreateUniversal(
|
||||
TaskFunction_t pxTaskCode,
|
||||
const char *const pcName,
|
||||
const uint32_t usStackDepth,
|
||||
void *const pvParameters,
|
||||
UBaseType_t uxPriority,
|
||||
TaskHandle_t *const pxCreatedTask,
|
||||
const BaseType_t xCoreID
|
||||
) {
|
||||
// #ifndef CONFIG_FREERTOS_UNICORE
|
||||
// if (xCoreID >= 0 && xCoreID < 2) {
|
||||
// return xTaskCreatePinnedToCore(
|
||||
// pxTaskCode,
|
||||
// pcName,
|
||||
// usStackDepth,
|
||||
// pvParameters,
|
||||
// uxPriority,
|
||||
// pxCreatedTask,
|
||||
// xCoreID
|
||||
// );
|
||||
// } else {
|
||||
// #endif
|
||||
return xTaskCreate(pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask);
|
||||
// #ifndef CONFIG_FREERTOS_UNICORE
|
||||
// }
|
||||
// #endif
|
||||
}
|
||||
#endif
|
||||
42
arduino/libretuya/core/LibreTuyaCompat.h
Normal file
42
arduino/libretuya/core/LibreTuyaCompat.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-04. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#if LT_HAS_FREERTOS
|
||||
#include <FreeRTOS.h>
|
||||
#include <task.h>
|
||||
#endif
|
||||
|
||||
// Definitions for error constants.
|
||||
#define esp_err_t int
|
||||
#define ESP_OK 0 /*!< esp_err_t value indicating success (no error) */
|
||||
#define ESP_FAIL -1 /*!< Generic esp_err_t code indicating failure */
|
||||
|
||||
// ArduinoCore-API doesn't define these anymore
|
||||
#define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
|
||||
#define PGM_VOID_P const void *
|
||||
#define vsnprintf_P vsnprintf
|
||||
#define OUTPUT_OPEN_DRAIN OUTPUT_OPENDRAIN
|
||||
#define attachInterruptArg attachInterruptParam
|
||||
#define voidFuncPtrArg voidFuncPtrParam
|
||||
|
||||
// FreeRTOS utilities
|
||||
#if LT_HAS_FREERTOS
|
||||
// if xCoreID < 0 or CPU is unicore, it will use xTaskCreate, else xTaskCreatePinnedToCore
|
||||
// allows to easily handle all possible situations without repetitive code
|
||||
BaseType_t xTaskCreateUniversal(
|
||||
TaskFunction_t pxTaskCode,
|
||||
const char *const pcName,
|
||||
const uint32_t usStackDepth,
|
||||
void *const pvParameters,
|
||||
UBaseType_t uxPriority,
|
||||
TaskHandle_t *const pxCreatedTask,
|
||||
const BaseType_t xCoreID
|
||||
);
|
||||
#define xTaskCreatePinnedToCore xTaskCreateUniversal
|
||||
#endif
|
||||
|
||||
// Default values from sdkconfig.h
|
||||
#define CONFIG_LWIP_MAX_ACTIVE_TCP 16
|
||||
@@ -49,6 +49,11 @@
|
||||
#define LT_LOG_HEAP 0
|
||||
#endif
|
||||
|
||||
// Debug errno values using LT_ERRNO()
|
||||
#ifndef LT_LOG_ERRNO
|
||||
#define LT_LOG_ERRNO 0
|
||||
#endif
|
||||
|
||||
// Per-module debugging
|
||||
#ifndef LT_DEBUG_WIFI
|
||||
#define LT_DEBUG_WIFI 0
|
||||
|
||||
103
arduino/libretuya/core/LibreTuyaCustom.c
Normal file
103
arduino/libretuya/core/LibreTuyaCustom.c
Normal file
@@ -0,0 +1,103 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-20. */
|
||||
|
||||
#include "LibreTuyaCustom.h"
|
||||
|
||||
int _analogReadResolution = 10; // 0-1023
|
||||
int _analogWriteResolution = 8; // 0-255
|
||||
int _analogWritePeriod = 20000; // 50 Hz
|
||||
|
||||
/**
|
||||
* @brief Check if pin is invalid (too low or too high).
|
||||
*/
|
||||
bool pinInvalid(pin_size_t pinNumber) {
|
||||
#ifdef PINS_COUNT
|
||||
return pinNumber < 0 || pinNumber >= PINS_COUNT;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get PinInfo struct for the specified number.
|
||||
* Returns NULL if pin number is invalid.
|
||||
*/
|
||||
PinInfo *pinInfo(pin_size_t pinNumber) {
|
||||
if (pinInvalid(pinNumber))
|
||||
return NULL;
|
||||
return &(pinTable[pinNumber]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if pin supports all features represented by 'mask'.
|
||||
*/
|
||||
bool pinSupported(PinInfo *pin, uint32_t mask) {
|
||||
return (pin->supported & mask) == mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if pin has all features represented by 'mask' enabled.
|
||||
*/
|
||||
bool pinEnabled(PinInfo *pin, uint32_t mask) {
|
||||
return (pin->enabled & mask) == mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if GPIO pin is configured as output.
|
||||
*/
|
||||
bool pinIsOutput(PinInfo *pin) {
|
||||
return pin->mode == OUTPUT || pin->mode == OUTPUT_OPENDRAIN;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if GPIO pin is configured as output.
|
||||
*/
|
||||
bool pinIsInput(PinInfo *pin) {
|
||||
return pin->mode == INPUT || pin->mode == INPUT_PULLUP || pin->mode == INPUT_PULLDOWN;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Read voltage from ADC and return a value between 0 and
|
||||
* the current reading resolution.
|
||||
*/
|
||||
int analogRead(pin_size_t pinNumber) {
|
||||
float voltage = analogReadVoltage(pinNumber);
|
||||
float maxVoltage = analogReadMaxVoltage(pinNumber);
|
||||
uint16_t ret = round((1 << _analogReadResolution) * voltage / maxVoltage);
|
||||
if (ret >= (1 << _analogReadResolution))
|
||||
ret = (1 << _analogReadResolution) - 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set resolution of values (in bits) returned by analogRead().
|
||||
* Defaults to 10 bit (0-1023).
|
||||
*/
|
||||
void analogReadResolution(int res) {
|
||||
_analogReadResolution = res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set resolution of values (in bits) expected by analogWrite().
|
||||
* Defaults to 8 bit (0-255).
|
||||
*/
|
||||
void analogWriteResolution(int res) {
|
||||
_analogWriteResolution = res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set PWM output frequency (in Hz).
|
||||
* Defaults to 50 Hz (20,000 uS).
|
||||
*/
|
||||
void analogWriteFrequency(int hz) {
|
||||
_analogWritePeriod = 1E6 / hz;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set PWM output frequency (cycle period) in microseconds.
|
||||
* Defaults to 20,000 uS (50 Hz).
|
||||
*/
|
||||
void analogWritePeriod(int us) {
|
||||
_analogWritePeriod = us;
|
||||
}
|
||||
|
||||
__attribute__((weak)) void analogReference(uint8_t mode) {}
|
||||
87
arduino/libretuya/core/LibreTuyaCustom.h
Normal file
87
arduino/libretuya/core/LibreTuyaCustom.h
Normal file
@@ -0,0 +1,87 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-06. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "LibreTuyaAPI.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Run main_task & start OS kernel (family-defined)
|
||||
*/
|
||||
extern bool startMainTask();
|
||||
|
||||
/**
|
||||
* @brief Main setup() and loop() task.
|
||||
* Not to be called directly.
|
||||
*/
|
||||
extern void main_task(const void *arg);
|
||||
|
||||
#define PIN_NONE (1 << 0)
|
||||
#define PIN_GPIO (1 << 1)
|
||||
#define PIN_IRQ (1 << 2)
|
||||
#define PIN_PWM (1 << 3)
|
||||
#define PIN_ADC (1 << 4)
|
||||
#define PIN_DAC (1 << 5)
|
||||
#define PIN_I2C (1 << 6)
|
||||
#define PIN_I2S (1 << 7)
|
||||
#define PIN_JTAG (1 << 8)
|
||||
#define PIN_SPI (1 << 9)
|
||||
#define PIN_SWD (1 << 10)
|
||||
#define PIN_UART (1 << 11)
|
||||
|
||||
typedef struct {
|
||||
/**
|
||||
* @brief GPIO name in the family SDK.
|
||||
*/
|
||||
uint32_t gpio;
|
||||
/**
|
||||
* @brief Supported pin functions.
|
||||
*/
|
||||
uint32_t supported;
|
||||
/**
|
||||
* @brief Enabled pin functions. Used values are family-specific.
|
||||
*/
|
||||
uint32_t enabled;
|
||||
/**
|
||||
* @brief Pin mode (direction, IRQ level, etc.).
|
||||
*/
|
||||
uint32_t mode;
|
||||
} PinInfo;
|
||||
|
||||
extern PinInfo pinTable[];
|
||||
|
||||
// Custom Wiring methods
|
||||
|
||||
bool pinInvalid(pin_size_t pinNumber);
|
||||
PinInfo *pinInfo(pin_size_t pinNumber);
|
||||
bool pinSupported(PinInfo *pin, uint32_t mask);
|
||||
bool pinEnabled(PinInfo *pin, uint32_t mask);
|
||||
bool pinIsOutput(PinInfo *pin);
|
||||
bool pinIsInput(PinInfo *pin);
|
||||
|
||||
int analogRead(pin_size_t pinNumber);
|
||||
void analogReadResolution(int res);
|
||||
void analogWriteResolution(int res);
|
||||
void analogWriteFrequency(int hz);
|
||||
void analogWritePeriod(int us);
|
||||
|
||||
extern int _analogReadResolution;
|
||||
extern int _analogWriteResolution;
|
||||
extern int _analogWritePeriod;
|
||||
|
||||
/**
|
||||
* @brief Read voltage from analog input (in millivolts).
|
||||
*/
|
||||
uint16_t analogReadVoltage(pin_size_t pinNumber);
|
||||
|
||||
/**
|
||||
* @brief Get max reading voltage for the specified pin (millivolts).
|
||||
*/
|
||||
uint16_t analogReadMaxVoltage(pin_size_t pinNumber);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#if LT_LOGGER_TASK
|
||||
#if LT_LOGGER_TASK && LT_HAS_FREERTOS
|
||||
#include <FreeRTOS.h>
|
||||
#include <task.h>
|
||||
#endif
|
||||
@@ -57,7 +57,7 @@ void lt_log(const uint8_t level, const char *format, ...) {
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if LT_LOGGER_TASK
|
||||
#if LT_LOGGER_TASK && LT_HAS_FREERTOS
|
||||
char task_colon = ':';
|
||||
TaskHandle_t task = xTaskGetCurrentTaskHandle();
|
||||
char *task_name = pcTaskGetTaskName(task);
|
||||
@@ -90,16 +90,15 @@ void lt_log(const uint8_t level, const char *format, ...) {
|
||||
#if LT_LOGGER_CALLER
|
||||
"%s():%hu: "
|
||||
#endif
|
||||
#if LT_LOGGER_TASK
|
||||
#if LT_LOGGER_TASK && LT_HAS_FREERTOS
|
||||
"%s%c "
|
||||
#endif
|
||||
,
|
||||
levels[level]
|
||||
#if LT_LOGGER_COLOR
|
||||
,
|
||||
c_bright, // whether text is bright
|
||||
c_value // text color
|
||||
c_value, // text color
|
||||
#endif
|
||||
levels[level]
|
||||
#if LT_LOGGER_TIMESTAMP
|
||||
,
|
||||
seconds // float
|
||||
@@ -113,7 +112,7 @@ void lt_log(const uint8_t level, const char *format, ...) {
|
||||
caller,
|
||||
line
|
||||
#endif
|
||||
#if LT_LOGGER_TASK
|
||||
#if LT_LOGGER_TASK && LT_HAS_FREERTOS
|
||||
,
|
||||
task_name,
|
||||
task_colon // printing outside of tasks
|
||||
|
||||
@@ -81,6 +81,8 @@ void lt_log(const uint8_t level, const char *format, ...);
|
||||
#define ESP_EARLY_LOGI(...) LT_I(__VA_ARGS__)
|
||||
#define ESP_EARLY_LOGW(...) LT_W(__VA_ARGS__)
|
||||
#define ESP_EARLY_LOGE(...) LT_E(__VA_ARGS__)
|
||||
#define ets_printf(...) LT_I(__VA_ARGS__)
|
||||
#define ETS_PRINTF(...) LT_I(__VA_ARGS__)
|
||||
|
||||
#define LT_T_MOD(module, ...) \
|
||||
do { \
|
||||
@@ -132,6 +134,16 @@ void lt_log(const uint8_t level, const char *format, ...);
|
||||
return ret; \
|
||||
}
|
||||
|
||||
#if LT_LOG_ERRNO
|
||||
#define LT_ERRNO() \
|
||||
if (errno) { \
|
||||
LT_E("errno=%d", errno); \
|
||||
errno = 0; \
|
||||
}
|
||||
#else
|
||||
#define LT_ERRNO()
|
||||
#endif
|
||||
|
||||
// WiFi.cpp
|
||||
#define LT_T_WG(...) LT_T_MOD(LT_DEBUG_WIFI, __VA_ARGS__)
|
||||
#define LT_V_WG(...) LT_T_MOD(LT_DEBUG_WIFI, __VA_ARGS__)
|
||||
|
||||
30
arduino/libretuya/libraries/Flash/Flash.cpp
Normal file
30
arduino/libretuya/libraries/Flash/Flash.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-04-24. */
|
||||
|
||||
#include "Flash.h"
|
||||
|
||||
extern "C" {
|
||||
#include <fal.h>
|
||||
}
|
||||
|
||||
// Global Flash object.
|
||||
FlashClass Flash;
|
||||
|
||||
FlashId FlashClass::getChipId() {
|
||||
return LT.getFlashChipId();
|
||||
}
|
||||
|
||||
uint32_t FlashClass::getSize() {
|
||||
return LT.getFlashChipSize();
|
||||
}
|
||||
|
||||
bool FlashClass::eraseSector(uint32_t offset) {
|
||||
return fal_partition_erase(fal_root_part, offset, 1) >= 0;
|
||||
}
|
||||
|
||||
bool FlashClass::readBlock(uint32_t offset, uint8_t *data, size_t size) {
|
||||
return fal_partition_read(fal_root_part, offset, data, size) >= 0;
|
||||
}
|
||||
|
||||
bool FlashClass::writeBlock(uint32_t offset, uint8_t *data, size_t size) {
|
||||
return fal_partition_write(fal_root_part, offset, data, size) >= 0;
|
||||
}
|
||||
@@ -2,20 +2,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "api/Flash.h"
|
||||
|
||||
struct flash_s;
|
||||
typedef struct flash_s flash_t;
|
||||
|
||||
class FlashClass : public IFlashClass {
|
||||
private:
|
||||
flash_t *flash;
|
||||
void initialize();
|
||||
#include <Arduino.h>
|
||||
|
||||
class FlashClass {
|
||||
public:
|
||||
FlashClass();
|
||||
~FlashClass();
|
||||
|
||||
FlashId getChipId();
|
||||
uint32_t getSize();
|
||||
|
||||
@@ -25,6 +25,8 @@
|
||||
* Adapted in October 2018
|
||||
*/
|
||||
|
||||
#if LT_ARD_HAS_WIFI
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#ifdef HTTPCLIENT_1_1_COMPATIBLE
|
||||
@@ -1623,3 +1625,5 @@ bool HTTPClient::generateCookieString(String *cookieString) {
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
#endif // LT_ARD_HAS_WIFI
|
||||
|
||||
31
arduino/libretuya/libraries/MD5/MD5.h
Normal file
31
arduino/libretuya/libraries/MD5/MD5.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-03. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <MD5Impl.h>
|
||||
|
||||
// available built-in implementations
|
||||
#if LT_MD5_USE_POLARSSL
|
||||
#include "MD5PolarSSLImpl.h"
|
||||
#endif
|
||||
|
||||
// common API
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef LT_MD5_CTX_T
|
||||
#define LT_MD5_CTX_T void
|
||||
#endif
|
||||
|
||||
// for compatibility with ESP8266
|
||||
typedef LT_MD5_CTX_T md5_context_t;
|
||||
|
||||
void MD5Init(LT_MD5_CTX_T *context);
|
||||
void MD5Update(LT_MD5_CTX_T *context, const unsigned char *buf, unsigned len);
|
||||
void MD5Final(unsigned char digest[16], LT_MD5_CTX_T *context);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
28
arduino/libretuya/libraries/MD5/MD5PolarSSLImpl.cpp
Normal file
28
arduino/libretuya/libraries/MD5/MD5PolarSSLImpl.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-03. */
|
||||
|
||||
#if LT_ARD_HAS_MD5
|
||||
|
||||
#include "MD5.h"
|
||||
|
||||
#if LT_MD5_USE_POLARSSL
|
||||
|
||||
extern "C" {
|
||||
|
||||
void MD5Init(LT_MD5_CTX_T *context) {
|
||||
md5_init(context);
|
||||
md5_starts(context);
|
||||
}
|
||||
|
||||
void MD5Update(LT_MD5_CTX_T *context, const unsigned char *buf, unsigned len) {
|
||||
md5_update(context, buf, len);
|
||||
}
|
||||
|
||||
void MD5Final(unsigned char digest[16], LT_MD5_CTX_T *context) {
|
||||
md5_finish(context, digest);
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
#endif // LT_MD5_USE_POLARSSL
|
||||
|
||||
#endif // LT_ARD_HAS_MD5
|
||||
14
arduino/libretuya/libraries/MD5/MD5PolarSSLImpl.h
Normal file
14
arduino/libretuya/libraries/MD5/MD5PolarSSLImpl.h
Normal file
@@ -0,0 +1,14 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-03. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <polarssl/md5.h>
|
||||
#define LT_MD5_CTX_T md5_context
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
@@ -1,7 +1,29 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-04-26. */
|
||||
|
||||
#include "WiFiClient.h"
|
||||
#include "WiFiPriv.h"
|
||||
#if LT_ARD_HAS_WIFI && LT_HAS_LWIP
|
||||
|
||||
#include "LwIPClient.h"
|
||||
|
||||
#define MAX_SOCK_NUM 4
|
||||
#define WIFI_CLIENT_CONNECT_TIMEOUT 3000
|
||||
#define WIFI_CLIENT_READ_TIMEOUT 3000
|
||||
#define WIFI_CLIENT_WRITE_RETRY 10
|
||||
#define WIFI_CLIENT_SELECT_TIMEOUT 1000
|
||||
#define WIFI_CLIENT_FLUSH_BUF_SIZE 1024
|
||||
|
||||
// disable #defines removing lwip_ prefix
|
||||
#undef LWIP_COMPAT_SOCKETS
|
||||
#define LWIP_COMPAT_SOCKETS 0
|
||||
|
||||
extern "C" {
|
||||
|
||||
#include <lwip/api.h>
|
||||
#include <lwip/dns.h>
|
||||
#include <lwip/err.h>
|
||||
#include <lwip/sockets.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
} // extern "C"
|
||||
|
||||
class SocketHandle {
|
||||
public:
|
||||
@@ -14,28 +36,28 @@ class SocketHandle {
|
||||
}
|
||||
};
|
||||
|
||||
WiFiClient::WiFiClient() {
|
||||
LT_V_WC("WiFiClient()");
|
||||
LwIPClient::LwIPClient() {
|
||||
LT_V_WC("LwIPClient()");
|
||||
_connected = false;
|
||||
_sock = NULL;
|
||||
_rxBuffer = NULL;
|
||||
_timeout = WIFI_CLIENT_CONNECT_TIMEOUT;
|
||||
}
|
||||
|
||||
WiFiClient::WiFiClient(int sock) {
|
||||
LT_V_WC("WiFiClient(%d)", sock);
|
||||
LwIPClient::LwIPClient(int sock) {
|
||||
LT_V_WC("LwIPClient(%d)", sock);
|
||||
_connected = true;
|
||||
_sock = std::make_shared<SocketHandle>(sock);
|
||||
_rxBuffer = std::make_shared<LwIPRxBuffer>(sock);
|
||||
_timeout = WIFI_CLIENT_CONNECT_TIMEOUT;
|
||||
}
|
||||
|
||||
WiFiClient::~WiFiClient() {
|
||||
LT_V_WC("~WiFiClient()");
|
||||
LwIPClient::~LwIPClient() {
|
||||
LT_V_WC("~LwIPClient()");
|
||||
stop();
|
||||
}
|
||||
|
||||
WiFiClient &WiFiClient::operator=(const WiFiClient &other) {
|
||||
LwIPClient &LwIPClient::operator=(const LwIPClient &other) {
|
||||
stop();
|
||||
_connected = other._connected;
|
||||
_sock = other._sock;
|
||||
@@ -47,24 +69,27 @@ bool IWiFiClient::operator==(const IWiFiClient &other) const {
|
||||
return fd() == other.fd() && remoteIP() == other.remoteIP() && remotePort() == other.remotePort();
|
||||
}
|
||||
|
||||
int WiFiClient::connect(IPAddress ip, uint16_t port) {
|
||||
int LwIPClient::connect(IPAddress ip, uint16_t port) {
|
||||
return connect(ip, port, _timeout);
|
||||
}
|
||||
|
||||
int WiFiClient::connect(const char *host, uint16_t port) {
|
||||
int LwIPClient::connect(const char *host, uint16_t port) {
|
||||
return connect(host, port, _timeout);
|
||||
}
|
||||
|
||||
int WiFiClient::connect(const char *host, uint16_t port, int32_t timeout) {
|
||||
int LwIPClient::connect(const char *host, uint16_t port, int32_t timeout) {
|
||||
IPAddress ip = WiFi.hostByName(host);
|
||||
if (!ip)
|
||||
return 0;
|
||||
return connect(ip, port, timeout);
|
||||
}
|
||||
|
||||
int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout) {
|
||||
int LwIPClient::connect(IPAddress ip, uint16_t port, int32_t timeout) {
|
||||
if (_connected)
|
||||
stop();
|
||||
int sock = lwip_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (sock < 0) {
|
||||
LT_D_WC("socket failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -73,6 +98,8 @@ int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout) {
|
||||
|
||||
lwip_fcntl(sock, F_SETFL, lwip_fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
|
||||
|
||||
LT_ERRNO();
|
||||
|
||||
struct sockaddr_in addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
@@ -120,19 +147,23 @@ int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout) {
|
||||
lwip_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable));
|
||||
lwip_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable));
|
||||
|
||||
LT_ERRNO();
|
||||
|
||||
lwip_fcntl(sock, F_SETFL, lwip_fcntl(sock, F_GETFL, 0) & ~O_NONBLOCK);
|
||||
|
||||
LT_ERRNO();
|
||||
|
||||
_connected = true;
|
||||
_sock = std::make_shared<SocketHandle>(sock);
|
||||
_rxBuffer = std::make_shared<LwIPRxBuffer>(sock);
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t WiFiClient::write(uint8_t data) {
|
||||
size_t LwIPClient::write(uint8_t data) {
|
||||
return write(&data, 1);
|
||||
}
|
||||
|
||||
size_t WiFiClient::write(Stream &stream) {
|
||||
size_t LwIPClient::write(Stream &stream) {
|
||||
uint8_t *buf = (uint8_t *)malloc(1360);
|
||||
if (!buf) {
|
||||
return 0;
|
||||
@@ -149,7 +180,7 @@ size_t WiFiClient::write(Stream &stream) {
|
||||
return written;
|
||||
}
|
||||
|
||||
size_t WiFiClient::write(const uint8_t *buf, size_t size) {
|
||||
size_t LwIPClient::write(const uint8_t *buf, size_t size) {
|
||||
if (_sock < 0 || !_connected || !size) {
|
||||
setWriteError();
|
||||
return 0;
|
||||
@@ -192,36 +223,38 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size) {
|
||||
}
|
||||
}
|
||||
}
|
||||
LT_D_WC("wrote %d bytes", written);
|
||||
return written;
|
||||
}
|
||||
|
||||
int WiFiClient::available() {
|
||||
int LwIPClient::available() {
|
||||
if (!_connected || !_rxBuffer)
|
||||
return 0;
|
||||
int res = _rxBuffer->available();
|
||||
if (_rxBuffer->failed()) {
|
||||
LT_ERRNO();
|
||||
stop();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int WiFiClient::fd() const {
|
||||
int LwIPClient::fd() const {
|
||||
if (!_sock)
|
||||
return -1;
|
||||
return _sock->fd;
|
||||
}
|
||||
|
||||
int WiFiClient::socket() {
|
||||
int LwIPClient::socket() {
|
||||
return fd();
|
||||
}
|
||||
|
||||
int WiFiClient::setTimeout(uint32_t seconds) {
|
||||
int LwIPClient::setTimeout(uint32_t seconds) {
|
||||
Client::setTimeout(seconds * 1000);
|
||||
lwip_setsockopt(fd(), SOL_SOCKET, SO_RCVTIMEO, &_timeout, sizeof(_timeout));
|
||||
return lwip_setsockopt(fd(), SOL_SOCKET, SO_SNDTIMEO, &_timeout, sizeof(_timeout));
|
||||
}
|
||||
|
||||
int WiFiClient::read() {
|
||||
int LwIPClient::read() {
|
||||
uint8_t data;
|
||||
int res = read(&data, 1);
|
||||
if (res < 0)
|
||||
@@ -231,7 +264,7 @@ int WiFiClient::read() {
|
||||
return data;
|
||||
}
|
||||
|
||||
int WiFiClient::read(uint8_t *buf, size_t size) {
|
||||
int LwIPClient::read(uint8_t *buf, size_t size) {
|
||||
int res = -1;
|
||||
if (_rxBuffer) {
|
||||
res = _rxBuffer->read(buf, size);
|
||||
@@ -242,7 +275,7 @@ int WiFiClient::read(uint8_t *buf, size_t size) {
|
||||
return res;
|
||||
}
|
||||
|
||||
int WiFiClient::peek() {
|
||||
int LwIPClient::peek() {
|
||||
int res = -1;
|
||||
if (_rxBuffer) {
|
||||
res = _rxBuffer->peek();
|
||||
@@ -253,7 +286,7 @@ int WiFiClient::peek() {
|
||||
return res;
|
||||
}
|
||||
|
||||
void WiFiClient::flush() {
|
||||
void LwIPClient::flush() {
|
||||
int res;
|
||||
size_t len = available();
|
||||
if (!len)
|
||||
@@ -272,14 +305,14 @@ void WiFiClient::flush() {
|
||||
free(buf);
|
||||
}
|
||||
|
||||
void WiFiClient::stop() {
|
||||
LT_V_WC("stop()");
|
||||
void LwIPClient::stop() {
|
||||
LT_V_WC("Stopping TCP");
|
||||
_connected = false;
|
||||
_sock = NULL;
|
||||
_rxBuffer = NULL;
|
||||
}
|
||||
|
||||
uint8_t WiFiClient::connected() {
|
||||
uint8_t LwIPClient::connected() {
|
||||
if (_connected) {
|
||||
uint8_t dummy;
|
||||
if (lwip_recv(fd(), &dummy, 0, MSG_DONTWAIT) <= 0) {
|
||||
@@ -323,34 +356,36 @@ uint16_t __attribute__((noinline)) getport(int sock, int (*func)(int, struct soc
|
||||
return ntohs(s->sin_port);
|
||||
}
|
||||
|
||||
IPAddress WiFiClient::remoteIP() const {
|
||||
IPAddress LwIPClient::remoteIP() const {
|
||||
return getaddr(fd(), lwip_getpeername);
|
||||
}
|
||||
|
||||
IPAddress WiFiClient::remoteIP(int fd) const {
|
||||
IPAddress LwIPClient::remoteIP(int fd) const {
|
||||
return getaddr(fd, lwip_getpeername);
|
||||
}
|
||||
|
||||
uint16_t WiFiClient::remotePort() const {
|
||||
uint16_t LwIPClient::remotePort() const {
|
||||
return getport(fd(), lwip_getpeername);
|
||||
}
|
||||
|
||||
uint16_t WiFiClient::remotePort(int fd) const {
|
||||
uint16_t LwIPClient::remotePort(int fd) const {
|
||||
return getport(fd, lwip_getpeername);
|
||||
}
|
||||
|
||||
IPAddress WiFiClient::localIP() const {
|
||||
IPAddress LwIPClient::localIP() const {
|
||||
return getaddr(fd(), lwip_getsockname);
|
||||
}
|
||||
|
||||
IPAddress WiFiClient::localIP(int fd) const {
|
||||
IPAddress LwIPClient::localIP(int fd) const {
|
||||
return getaddr(fd, lwip_getsockname);
|
||||
}
|
||||
|
||||
uint16_t WiFiClient::localPort() const {
|
||||
uint16_t LwIPClient::localPort() const {
|
||||
return getport(fd(), lwip_getsockname);
|
||||
}
|
||||
|
||||
uint16_t WiFiClient::localPort(int fd) const {
|
||||
uint16_t LwIPClient::localPort(int fd) const {
|
||||
return getport(fd, lwip_getsockname);
|
||||
}
|
||||
|
||||
#endif
|
||||
56
arduino/libretuya/libraries/NetUtils/lwip/LwIPClient.h
Normal file
56
arduino/libretuya/libraries/NetUtils/lwip/LwIPClient.h
Normal file
@@ -0,0 +1,56 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-04-26. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <api/WiFi/WiFi.h>
|
||||
#include <api/WiFiClient.h>
|
||||
#include <lwip/LwIPRxBuffer.h>
|
||||
#include <memory>
|
||||
|
||||
class SocketHandle;
|
||||
|
||||
class LwIPClient : public IWiFiClient {
|
||||
private:
|
||||
bool _connected;
|
||||
std::shared_ptr<SocketHandle> _sock;
|
||||
std::shared_ptr<LwIPRxBuffer> _rxBuffer;
|
||||
|
||||
public:
|
||||
LwIPClient();
|
||||
LwIPClient(int sock);
|
||||
~LwIPClient();
|
||||
|
||||
int connect(IPAddress ip, uint16_t port);
|
||||
int connect(const char *host, uint16_t port);
|
||||
int connect(IPAddress ip, uint16_t port, int32_t timeout);
|
||||
int connect(const char *host, uint16_t port, int32_t timeout);
|
||||
|
||||
size_t write(uint8_t data);
|
||||
size_t write(const uint8_t *buf, size_t size);
|
||||
size_t write(Stream &stream);
|
||||
|
||||
int available();
|
||||
int fd() const;
|
||||
int socket();
|
||||
int setTimeout(uint32_t seconds);
|
||||
|
||||
int read();
|
||||
int read(uint8_t *buf, size_t size);
|
||||
int peek();
|
||||
void flush();
|
||||
void stop();
|
||||
uint8_t connected();
|
||||
|
||||
LwIPClient &operator=(const LwIPClient &other);
|
||||
|
||||
IPAddress remoteIP() const;
|
||||
IPAddress remoteIP(int sock) const;
|
||||
uint16_t remotePort() const;
|
||||
uint16_t remotePort(int sock) const;
|
||||
IPAddress localIP() const;
|
||||
IPAddress localIP(int sock) const;
|
||||
uint16_t localPort() const;
|
||||
uint16_t localPort(int sock) const;
|
||||
|
||||
using Print::write;
|
||||
};
|
||||
@@ -1,26 +1,26 @@
|
||||
#ifdef LT_HAS_LWIP
|
||||
#if LT_HAS_LWIP
|
||||
|
||||
#include "LwIPRxBuffer.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
// disable #defines removing lwip_ prefix
|
||||
#undef LWIP_COMPAT_SOCKETS
|
||||
#define LWIP_COMPAT_SOCKETS 0
|
||||
|
||||
extern "C" {
|
||||
|
||||
#include <lwip/sockets.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
size_t LwIPRxBuffer::r_available() {
|
||||
if (_sock < 0) {
|
||||
LT_D_WC("_sock < 0");
|
||||
return 0;
|
||||
}
|
||||
uint16_t count = 0;
|
||||
int res = lwip_ioctl(_sock, FIONREAD, &count);
|
||||
int count = 0; // must be of same size as in lwip_ioctl()
|
||||
int res = lwip_ioctl(_sock, FIONREAD, &count);
|
||||
if (res < 0) {
|
||||
LT_D_WC("lwip_ioctl()=%d, errno=%d", res, errno);
|
||||
_failed = true;
|
||||
return 0;
|
||||
}
|
||||
@@ -31,7 +31,7 @@ size_t LwIPRxBuffer::fillBuffer() {
|
||||
if (!_buffer) {
|
||||
_buffer = (uint8_t *)malloc(_size);
|
||||
if (!_buffer) {
|
||||
printf("[e] Not enough memory to allocate buffer\r\n");
|
||||
LT_E("buffer alloc failed");
|
||||
_failed = true;
|
||||
return 0;
|
||||
}
|
||||
@@ -46,6 +46,7 @@ size_t LwIPRxBuffer::fillBuffer() {
|
||||
int res = lwip_recv(_sock, _buffer + _fill, _size - _fill, MSG_DONTWAIT);
|
||||
if (res < 0) {
|
||||
if (errno != EWOULDBLOCK) {
|
||||
LT_ERRNO();
|
||||
_failed = true;
|
||||
}
|
||||
return 0;
|
||||
@@ -1,17 +1,30 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-04-26. */
|
||||
|
||||
#include "WiFiServer.h"
|
||||
#include "WiFiPriv.h"
|
||||
#if LT_ARD_HAS_WIFI && LT_HAS_LWIP
|
||||
|
||||
WiFiServer::WiFiServer(uint32_t addr, uint16_t port, uint8_t maxClients)
|
||||
#include "LwIPServer.h"
|
||||
|
||||
// disable #defines removing lwip_ prefix
|
||||
#undef LWIP_COMPAT_SOCKETS
|
||||
#define LWIP_COMPAT_SOCKETS 0
|
||||
|
||||
extern "C" {
|
||||
#include <lwip/api.h>
|
||||
// #include <lwip/dns.h>
|
||||
#include <lwip/err.h>
|
||||
#include <lwip/sockets.h>
|
||||
#include <sys/time.h>
|
||||
}
|
||||
|
||||
LwIPServer::LwIPServer(uint32_t addr, uint16_t port, uint8_t maxClients)
|
||||
: _sock(-1), _sockAccepted(-1), _addr(addr), _port(port), _maxClients(maxClients), _active(false), _noDelay(false) {
|
||||
}
|
||||
|
||||
WiFiServer::operator bool() {
|
||||
LwIPServer::operator bool() {
|
||||
return _active;
|
||||
}
|
||||
|
||||
bool WiFiServer::begin(uint16_t port, bool reuseAddr) {
|
||||
bool LwIPServer::begin(uint16_t port, bool reuseAddr) {
|
||||
if (_active)
|
||||
return true;
|
||||
if (port)
|
||||
@@ -50,7 +63,7 @@ bool WiFiServer::begin(uint16_t port, bool reuseAddr) {
|
||||
_sockAccepted = -1;
|
||||
}
|
||||
|
||||
void WiFiServer::end() {
|
||||
void LwIPServer::end() {
|
||||
if (_sock == -1)
|
||||
return;
|
||||
lwip_close(_sock);
|
||||
@@ -58,7 +71,7 @@ void WiFiServer::end() {
|
||||
_active = -1;
|
||||
}
|
||||
|
||||
WiFiClient WiFiServer::accept() {
|
||||
WiFiClient LwIPServer::accept() {
|
||||
if (!_active)
|
||||
return WiFiClient();
|
||||
|
||||
@@ -96,7 +109,7 @@ WiFiClient WiFiServer::accept() {
|
||||
return WiFiClient();
|
||||
}
|
||||
|
||||
int WiFiServer::setTimeout(uint32_t seconds) {
|
||||
int LwIPServer::setTimeout(uint32_t seconds) {
|
||||
struct timeval tv;
|
||||
tv.tv_sec = seconds;
|
||||
tv.tv_usec = 0;
|
||||
@@ -105,15 +118,15 @@ int WiFiServer::setTimeout(uint32_t seconds) {
|
||||
return lwip_setsockopt(_sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
||||
}
|
||||
|
||||
void WiFiServer::setNoDelay(bool noDelay) {
|
||||
void LwIPServer::setNoDelay(bool noDelay) {
|
||||
_noDelay = noDelay;
|
||||
}
|
||||
|
||||
bool WiFiServer::getNoDelay() {
|
||||
bool LwIPServer::getNoDelay() {
|
||||
return _noDelay;
|
||||
}
|
||||
|
||||
bool WiFiServer::hasClient() {
|
||||
bool LwIPServer::hasClient() {
|
||||
if (_sockAccepted >= 0) {
|
||||
return true;
|
||||
}
|
||||
@@ -125,3 +138,5 @@ bool WiFiServer::hasClient() {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
47
arduino/libretuya/libraries/NetUtils/lwip/LwIPServer.h
Normal file
47
arduino/libretuya/libraries/NetUtils/lwip/LwIPServer.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-04-26. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <api/WiFi/WiFi.h>
|
||||
#include <api/WiFiServer.h>
|
||||
|
||||
#include <WiFiClient.h>
|
||||
|
||||
class LwIPServer : public IWiFiServer<WiFiClient> {
|
||||
private:
|
||||
int _sock;
|
||||
int _sockAccepted;
|
||||
uint32_t _addr;
|
||||
uint16_t _port;
|
||||
uint8_t _maxClients;
|
||||
bool _active;
|
||||
bool _noDelay = false;
|
||||
|
||||
private:
|
||||
LwIPServer(uint32_t addr, uint16_t port = 80, uint8_t maxClients = 4);
|
||||
|
||||
public:
|
||||
LwIPServer(uint16_t port = 80, uint8_t maxClients = 4) : LwIPServer((uint32_t)0, port, maxClients) {}
|
||||
|
||||
LwIPServer(int port = 80, uint8_t maxClients = 4) : LwIPServer((uint32_t)0, port, maxClients) {}
|
||||
|
||||
LwIPServer(const IPAddress &addr, uint16_t port = 80, uint8_t maxClients = 4)
|
||||
: LwIPServer((uint32_t)addr, port, maxClients) {}
|
||||
|
||||
operator bool();
|
||||
|
||||
bool begin(uint16_t port = 0, bool reuseAddr = true);
|
||||
void end();
|
||||
WiFiClient accept();
|
||||
|
||||
size_t write(const uint8_t *buffer, size_t size) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void stopAll() {}
|
||||
|
||||
int setTimeout(uint32_t seconds);
|
||||
void setNoDelay(bool noDelay);
|
||||
bool getNoDelay();
|
||||
bool hasClient();
|
||||
};
|
||||
@@ -1,23 +1,17 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-04-30. */
|
||||
|
||||
#if LT_ARD_HAS_WIFI && LT_HAS_MBEDTLS
|
||||
|
||||
#include "MbedTLSClient.h"
|
||||
|
||||
#include <IPAddress.h>
|
||||
#include <WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
#include <mbedtls/debug.h>
|
||||
#include <mbedtls/platform.h>
|
||||
#include <mbedtls/sha256.h>
|
||||
#include <mbedtls/ssl.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
MbedTLSClient::MbedTLSClient() : WiFiClient() {
|
||||
init(); // ensure the context is zero filled
|
||||
@@ -27,9 +21,13 @@ MbedTLSClient::MbedTLSClient(int sock) : WiFiClient(sock) {
|
||||
init(); // ensure the context is zero filled
|
||||
}
|
||||
|
||||
MbedTLSClient::~MbedTLSClient() {
|
||||
LT_V_WC("~MbedTLSClient()");
|
||||
stop();
|
||||
}
|
||||
|
||||
void MbedTLSClient::stop() {
|
||||
WiFiClient::stop();
|
||||
LT_V_SSL("Closing SSL connection");
|
||||
LT_V_SSL("Stopping SSL");
|
||||
|
||||
if (_sslCfg.ca_chain) {
|
||||
mbedtls_x509_crt_free(&_caCert);
|
||||
@@ -40,6 +38,7 @@ void MbedTLSClient::stop() {
|
||||
}
|
||||
mbedtls_ssl_free(&_sslCtx);
|
||||
mbedtls_ssl_config_free(&_sslCfg);
|
||||
LT_HEAP_I();
|
||||
}
|
||||
|
||||
void MbedTLSClient::init() {
|
||||
@@ -85,7 +84,11 @@ static int ssl_random(void *data, unsigned char *output, size_t len) {
|
||||
}
|
||||
|
||||
void debug_cb(void *ctx, int level, const char *file, int line, const char *str) {
|
||||
LT_I("%04d: |%d| %s", line, level, str);
|
||||
// do not print the trailing \n
|
||||
uint16_t len = strlen(str);
|
||||
char *msg = (char *)str;
|
||||
msg[len - 1] = '\0';
|
||||
LT_I("%04d: |%d| %s", line, level, msg);
|
||||
}
|
||||
|
||||
int MbedTLSClient::connect(
|
||||
@@ -98,7 +101,7 @@ int MbedTLSClient::connect(
|
||||
const char *pskIdent,
|
||||
const char *psk
|
||||
) {
|
||||
LT_D_SSL("Free heap before TLS: TODO");
|
||||
LT_HEAP_I();
|
||||
|
||||
if (!rootCABuf && !pskIdent && !psk && !_insecure && !_useRootCA)
|
||||
return -1;
|
||||
@@ -120,6 +123,7 @@ int MbedTLSClient::connect(
|
||||
|
||||
LT_V_SSL("Init SSL");
|
||||
init();
|
||||
LT_HEAP_I();
|
||||
|
||||
// mbedtls_debug_set_threshold(4);
|
||||
// mbedtls_ssl_conf_dbg(&_sslCfg, debug_cb, NULL);
|
||||
@@ -208,6 +212,8 @@ int MbedTLSClient::connect(
|
||||
mbedtls_ssl_set_bio(&_sslCtx, &_sockTls, mbedtls_net_send, mbedtls_net_recv, NULL);
|
||||
mbedtls_net_set_nonblock((mbedtls_net_context *)&_sockTls);
|
||||
|
||||
LT_HEAP_I();
|
||||
|
||||
LT_V_SSL("SSL handshake");
|
||||
if (_handshakeTimeout == 0)
|
||||
_handshakeTimeout = timeout;
|
||||
@@ -223,6 +229,8 @@ int MbedTLSClient::connect(
|
||||
delay(2);
|
||||
}
|
||||
|
||||
LT_HEAP_I();
|
||||
|
||||
if (clientCert && clientKey) {
|
||||
LT_D_SSL(
|
||||
"Protocol %s, ciphersuite %s",
|
||||
@@ -440,3 +448,5 @@ bool MbedTLSClient::getFingerprintSHA256(uint8_t result[32]) {
|
||||
mbedtls_sha256_finish(&shaCtx, result);
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif // LT_ARD_HAS_WIFI && LT_HAS_MBEDTLS
|
||||
|
||||
@@ -2,20 +2,17 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <api/WiFi/WiFi.h>
|
||||
#include <api/WiFiClient.h>
|
||||
#include <api/WiFiClientSecure.h>
|
||||
|
||||
#include <WiFiClient.h> // extend family's WiFiClient impl
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
#include <mbedtls/net.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
class MbedTLSClient : public WiFiClient, public IWiFiClientSecure {
|
||||
private:
|
||||
@@ -53,6 +50,7 @@ class MbedTLSClient : public WiFiClient, public IWiFiClientSecure {
|
||||
public:
|
||||
MbedTLSClient();
|
||||
MbedTLSClient(int sock);
|
||||
~MbedTLSClient();
|
||||
|
||||
int connect(IPAddress ip, uint16_t port, int32_t timeout);
|
||||
int connect(const char *host, uint16_t port, int32_t timeout);
|
||||
|
||||
66
arduino/libretuya/libraries/StreamString/StreamString.cpp
Normal file
66
arduino/libretuya/libraries/StreamString/StreamString.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
StreamString.cpp
|
||||
|
||||
Copyright (c) 2015 Markus Sattler. All rights reserved.
|
||||
This file 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 <Arduino.h>
|
||||
#include "StreamString.h"
|
||||
|
||||
size_t StreamString::write(const uint8_t *data, size_t size) {
|
||||
if(size && data) {
|
||||
const unsigned int newlen = len + size;
|
||||
if(reserve(newlen + 1)) {
|
||||
memcpy((void *) (buffer + len), (const void *) data, size);
|
||||
changeBuffer(newlen);
|
||||
*(buffer + newlen) = 0x00; // add null for string end
|
||||
return size;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t StreamString::write(uint8_t data) {
|
||||
return concat((char) data);
|
||||
}
|
||||
|
||||
int StreamString::available() {
|
||||
return length();
|
||||
}
|
||||
|
||||
int StreamString::read() {
|
||||
if(length()) {
|
||||
char c = charAt(0);
|
||||
remove(0, 1);
|
||||
return c;
|
||||
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int StreamString::peek() {
|
||||
if(length()) {
|
||||
char c = charAt(0);
|
||||
return c;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void StreamString::flush() {
|
||||
}
|
||||
39
arduino/libretuya/libraries/StreamString/StreamString.h
Normal file
39
arduino/libretuya/libraries/StreamString/StreamString.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
StreamString.h
|
||||
|
||||
Copyright (c) 2015 Markus Sattler. All rights reserved.
|
||||
|
||||
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
|
||||
|
||||
*/
|
||||
|
||||
#ifndef STREAMSTRING_H_
|
||||
#define STREAMSTRING_H_
|
||||
|
||||
|
||||
class StreamString: public Stream, public String
|
||||
{
|
||||
public:
|
||||
size_t write(const uint8_t *buffer, size_t size) override;
|
||||
size_t write(uint8_t data) override;
|
||||
|
||||
int available() override;
|
||||
int read() override;
|
||||
int peek() override;
|
||||
void flush() override;
|
||||
};
|
||||
|
||||
|
||||
#endif /* STREAMSTRING_H_ */
|
||||
@@ -127,6 +127,7 @@ size_t UpdateClass::writeStream(Stream &data) {
|
||||
// return on errors
|
||||
return written;
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -190,6 +191,9 @@ size_t UpdateClass::tryWriteData(uint8_t *data, size_t len) {
|
||||
// call progress callback
|
||||
if (callback)
|
||||
callback(bytesWritten, bytesTotal);
|
||||
// reset the buffer as it's used already
|
||||
if (bufSize() == UF2_BLOCK_SIZE)
|
||||
bufPos = buf;
|
||||
return UF2_BLOCK_SIZE;
|
||||
}
|
||||
|
||||
|
||||
@@ -118,12 +118,12 @@ uf2_err_t uf2_update_parts(uf2_ota_t *ctx, char *part1, char *part2) {
|
||||
ctx->erased_length = 0;
|
||||
|
||||
if (part1[0]) {
|
||||
ctx->part1 = fal_partition_find(part1);
|
||||
ctx->part1 = (fal_partition_t)fal_partition_find(part1);
|
||||
if (!ctx->part1)
|
||||
return UF2_ERR_PART_404;
|
||||
}
|
||||
if (part2[0]) {
|
||||
ctx->part2 = fal_partition_find(part2);
|
||||
ctx->part2 = (fal_partition_t)fal_partition_find(part2);
|
||||
if (!ctx->part2)
|
||||
return UF2_ERR_PART_404;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
|
||||
*/
|
||||
|
||||
#if LT_ARD_HAS_WIFI
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "WebServer.h"
|
||||
@@ -603,3 +605,5 @@ bool WebServer::_parseFormUploadAborted() {
|
||||
_currentHandler->upload(*this, _currentUri, *_currentUpload);
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif // LT_ARD_HAS_WIFI
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
|
||||
*/
|
||||
|
||||
#if LT_ARD_HAS_WIFI
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "FS.h"
|
||||
@@ -41,7 +43,7 @@ WebServer::WebServer(IPAddress addr, int port)
|
||||
_statusChange(0), _nullDelay(true), _currentHandler(nullptr), _firstHandler(nullptr), _lastHandler(nullptr),
|
||||
_currentArgCount(0), _currentArgs(nullptr), _postArgsLen(0), _postArgs(nullptr), _headerKeysCount(0),
|
||||
_currentHeaders(nullptr), _contentLength(0), _chunked(false) {
|
||||
log_v("WebServer::Webserver(addr=%s, port=%d)", addr.toString().c_str(), port);
|
||||
log_v("WebServer::Webserver(addr=%s, port=%d)", ipToString(addr).c_str(), port);
|
||||
}
|
||||
|
||||
WebServer::WebServer(int port)
|
||||
@@ -263,7 +265,7 @@ void WebServer::handleClient() {
|
||||
return;
|
||||
}
|
||||
|
||||
log_v("New client: client.localIP()=%s", client.localIP().toString().c_str());
|
||||
log_v("New client: client.localIP()=%s", ipToString(client.localIP()).c_str());
|
||||
|
||||
_currentClient = client;
|
||||
_currentStatus = HC_WAIT_READ;
|
||||
@@ -715,3 +717,5 @@ String WebServer::_responseCodeToString(int code) {
|
||||
return F("");
|
||||
}
|
||||
}
|
||||
|
||||
#endif // LT_ARD_HAS_WIFI
|
||||
|
||||
@@ -23,6 +23,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#if LT_ARD_HAS_WIFI
|
||||
|
||||
#include "WiFiMulti.h"
|
||||
#include <Arduino.h>
|
||||
#include <limits.h>
|
||||
@@ -203,11 +205,14 @@ uint8_t WiFiMulti::run(uint32_t connectTimeout) {
|
||||
status = WiFi.status();
|
||||
}
|
||||
|
||||
IPAddress ip;
|
||||
switch (status) {
|
||||
case WL_CONNECTED:
|
||||
LT_I("Connecting done");
|
||||
LT_D("SSID: %s", WiFi.SSID().c_str());
|
||||
LT_D("IP: %s", WiFi.localIP().toString().c_str());
|
||||
// TODO fix this after implementing IP format for printf()
|
||||
ip = WiFi.localIP();
|
||||
LT_D("IP: %u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
|
||||
LT_D("MAC: %s", WiFi.BSSIDstr().c_str());
|
||||
LT_D("Channel: %d", WiFi.channel());
|
||||
break;
|
||||
@@ -236,3 +241,5 @@ uint8_t WiFiMulti::run(uint32_t connectTimeout) {
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
#endif // LT_ARD_HAS_WIFI
|
||||
|
||||
2
arduino/libretuya/libraries/cbuf/.clang-format
Normal file
2
arduino/libretuya/libraries/cbuf/.clang-format
Normal file
@@ -0,0 +1,2 @@
|
||||
DisableFormat: true
|
||||
SortIncludes: Never
|
||||
196
arduino/libretuya/libraries/cbuf/cbuf.cpp
Normal file
196
arduino/libretuya/libraries/cbuf/cbuf.cpp
Normal file
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
cbuf.cpp - Circular buffer implementation
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file 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 "cbuf.h"
|
||||
|
||||
cbuf::cbuf(size_t size) :
|
||||
next(NULL), _size(size+1), _buf(new char[size+1]), _bufend(_buf + size + 1), _begin(_buf), _end(_begin)
|
||||
{
|
||||
}
|
||||
|
||||
cbuf::~cbuf()
|
||||
{
|
||||
delete[] _buf;
|
||||
}
|
||||
|
||||
size_t cbuf::resizeAdd(size_t addSize)
|
||||
{
|
||||
return resize(_size + addSize);
|
||||
}
|
||||
|
||||
size_t cbuf::resize(size_t newSize)
|
||||
{
|
||||
|
||||
size_t bytes_available = available();
|
||||
newSize += 1;
|
||||
// not lose any data
|
||||
// if data can be lost use remove or flush before resize
|
||||
if((newSize < bytes_available) || (newSize == _size)) {
|
||||
return _size;
|
||||
}
|
||||
|
||||
char *newbuf = new char[newSize];
|
||||
char *oldbuf = _buf;
|
||||
|
||||
if(!newbuf) {
|
||||
return _size;
|
||||
}
|
||||
|
||||
if(_buf) {
|
||||
read(newbuf, bytes_available);
|
||||
memset((newbuf + bytes_available), 0x00, (newSize - bytes_available));
|
||||
}
|
||||
|
||||
_begin = newbuf;
|
||||
_end = newbuf + bytes_available;
|
||||
_bufend = newbuf + newSize;
|
||||
_size = newSize;
|
||||
|
||||
_buf = newbuf;
|
||||
delete[] oldbuf;
|
||||
|
||||
return _size;
|
||||
}
|
||||
|
||||
size_t cbuf::available() const
|
||||
{
|
||||
if(_end >= _begin) {
|
||||
return _end - _begin;
|
||||
}
|
||||
return _size - (_begin - _end);
|
||||
}
|
||||
|
||||
size_t cbuf::size()
|
||||
{
|
||||
return _size;
|
||||
}
|
||||
|
||||
size_t cbuf::room() const
|
||||
{
|
||||
if(_end >= _begin) {
|
||||
return _size - (_end - _begin) - 1;
|
||||
}
|
||||
return _begin - _end - 1;
|
||||
}
|
||||
|
||||
int cbuf::peek()
|
||||
{
|
||||
if(empty()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return static_cast<int>(*_begin);
|
||||
}
|
||||
|
||||
size_t cbuf::peek(char *dst, size_t size)
|
||||
{
|
||||
size_t bytes_available = available();
|
||||
size_t size_to_read = (size < bytes_available) ? size : bytes_available;
|
||||
size_t size_read = size_to_read;
|
||||
char * begin = _begin;
|
||||
if(_end < _begin && size_to_read > (size_t) (_bufend - _begin)) {
|
||||
size_t top_size = _bufend - _begin;
|
||||
memcpy(dst, _begin, top_size);
|
||||
begin = _buf;
|
||||
size_to_read -= top_size;
|
||||
dst += top_size;
|
||||
}
|
||||
memcpy(dst, begin, size_to_read);
|
||||
return size_read;
|
||||
}
|
||||
|
||||
int cbuf::read()
|
||||
{
|
||||
if(empty()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
char result = *_begin;
|
||||
_begin = wrap_if_bufend(_begin + 1);
|
||||
return static_cast<int>(result);
|
||||
}
|
||||
|
||||
size_t cbuf::read(char* dst, size_t size)
|
||||
{
|
||||
size_t bytes_available = available();
|
||||
size_t size_to_read = (size < bytes_available) ? size : bytes_available;
|
||||
size_t size_read = size_to_read;
|
||||
if(_end < _begin && size_to_read > (size_t) (_bufend - _begin)) {
|
||||
size_t top_size = _bufend - _begin;
|
||||
memcpy(dst, _begin, top_size);
|
||||
_begin = _buf;
|
||||
size_to_read -= top_size;
|
||||
dst += top_size;
|
||||
}
|
||||
memcpy(dst, _begin, size_to_read);
|
||||
_begin = wrap_if_bufend(_begin + size_to_read);
|
||||
return size_read;
|
||||
}
|
||||
|
||||
size_t cbuf::write(char c)
|
||||
{
|
||||
if(full()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
*_end = c;
|
||||
_end = wrap_if_bufend(_end + 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t cbuf::write(const char* src, size_t size)
|
||||
{
|
||||
size_t bytes_available = room();
|
||||
size_t size_to_write = (size < bytes_available) ? size : bytes_available;
|
||||
size_t size_written = size_to_write;
|
||||
if(_end >= _begin && size_to_write > (size_t) (_bufend - _end)) {
|
||||
size_t top_size = _bufend - _end;
|
||||
memcpy(_end, src, top_size);
|
||||
_end = _buf;
|
||||
size_to_write -= top_size;
|
||||
src += top_size;
|
||||
}
|
||||
memcpy(_end, src, size_to_write);
|
||||
_end = wrap_if_bufend(_end + size_to_write);
|
||||
return size_written;
|
||||
}
|
||||
|
||||
void cbuf::flush()
|
||||
{
|
||||
_begin = _buf;
|
||||
_end = _buf;
|
||||
}
|
||||
|
||||
size_t cbuf::remove(size_t size)
|
||||
{
|
||||
size_t bytes_available = available();
|
||||
if(size >= bytes_available) {
|
||||
flush();
|
||||
return 0;
|
||||
}
|
||||
size_t size_to_remove = (size < bytes_available) ? size : bytes_available;
|
||||
if(_end < _begin && size_to_remove > (size_t) (_bufend - _begin)) {
|
||||
size_t top_size = _bufend - _begin;
|
||||
_begin = _buf;
|
||||
size_to_remove -= top_size;
|
||||
}
|
||||
_begin = wrap_if_bufend(_begin + size_to_remove);
|
||||
return available();
|
||||
}
|
||||
79
arduino/libretuya/libraries/cbuf/cbuf.h
Normal file
79
arduino/libretuya/libraries/cbuf/cbuf.h
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
cbuf.h - Circular buffer implementation
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
This file 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
|
||||
*/
|
||||
|
||||
#ifndef __cbuf_h
|
||||
#define __cbuf_h
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
class cbuf
|
||||
{
|
||||
public:
|
||||
cbuf(size_t size);
|
||||
~cbuf();
|
||||
|
||||
size_t resizeAdd(size_t addSize);
|
||||
size_t resize(size_t newSize);
|
||||
size_t available() const;
|
||||
size_t size();
|
||||
|
||||
size_t room() const;
|
||||
|
||||
inline bool empty() const
|
||||
{
|
||||
return _begin == _end;
|
||||
}
|
||||
|
||||
inline bool full() const
|
||||
{
|
||||
return wrap_if_bufend(_end + 1) == _begin;
|
||||
}
|
||||
|
||||
int peek();
|
||||
size_t peek(char *dst, size_t size);
|
||||
|
||||
int read();
|
||||
size_t read(char* dst, size_t size);
|
||||
|
||||
size_t write(char c);
|
||||
size_t write(const char* src, size_t size);
|
||||
|
||||
void flush();
|
||||
size_t remove(size_t size);
|
||||
|
||||
cbuf *next;
|
||||
|
||||
protected:
|
||||
inline char* wrap_if_bufend(char* ptr) const
|
||||
{
|
||||
return (ptr == _bufend) ? _buf : ptr;
|
||||
}
|
||||
|
||||
size_t _size;
|
||||
char* _buf;
|
||||
const char* _bufend;
|
||||
char* _begin;
|
||||
char* _end;
|
||||
|
||||
};
|
||||
|
||||
#endif//__cbuf_h
|
||||
@@ -59,15 +59,16 @@ mDNS::~mDNS() {}
|
||||
bool mDNS::begin(const char *hostname) {
|
||||
mdns_resp_init();
|
||||
struct netif *netif = netif_list;
|
||||
uint8_t enabled = 0;
|
||||
while (netif != NULL) {
|
||||
// TODO: detect mdns_netif_client_id by checking netif_get_client_data()
|
||||
// and finding the requested hostname in struct mdns_host
|
||||
if (netif_is_up(netif) && mdns_resp_add_netif(netif, hostname, 255) != ERR_OK) {
|
||||
return false;
|
||||
if (netif_is_up(netif) && mdns_resp_add_netif(netif, hostname, 255) == ERR_OK) {
|
||||
enabled++;
|
||||
}
|
||||
netif = netif->next;
|
||||
}
|
||||
return true;
|
||||
return enabled > 0;
|
||||
}
|
||||
|
||||
void mDNS::end() {
|
||||
|
||||
@@ -2,6 +2,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
inline void printf_nop(const char *fmt, ...) {}
|
||||
|
||||
#define FAL_PRINTF printf_nop
|
||||
#define FAL_DEBUG 0
|
||||
|
||||
// Flash device configuration
|
||||
extern const struct fal_flash_dev flash0;
|
||||
|
||||
@@ -23,3 +28,11 @@ extern const struct fal_flash_dev flash0;
|
||||
.offset = FLASH_##part_upper##_OFFSET, /* partition offset macro as uppercase string */ \
|
||||
.len = FLASH_##part_upper##_LENGTH, /* partition length macro as uppercase string */ \
|
||||
},
|
||||
|
||||
// for fal_partition_t
|
||||
#include <fal_def.h>
|
||||
|
||||
/**
|
||||
* @brief Root partition table, representing the entire flash.
|
||||
*/
|
||||
extern fal_partition_t fal_root_part;
|
||||
|
||||
86
arduino/libretuya/port/printf/printf_config.h
Normal file
86
arduino/libretuya/port/printf/printf_config.h
Normal file
@@ -0,0 +1,86 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-19. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#define PRINTF_HAS_DISABLE 1
|
||||
|
||||
// make printf.c define wrapper functions
|
||||
#define printf_ __wrap_printf
|
||||
#define sprintf_ __wrap_sprintf
|
||||
#define vsprintf_ __wrap_vsprintf
|
||||
#define snprintf_ __wrap_snprintf
|
||||
#define vsnprintf_ __wrap_vsnprintf
|
||||
#define vprintf_ __wrap_vprintf
|
||||
|
||||
#define WRAP_DISABLE_DEF(name) \
|
||||
extern void __wrap_##name##_disable(); \
|
||||
extern void __wrap_##name##_enable(); \
|
||||
extern void __wrap_##name##_set(unsigned char disabled); \
|
||||
extern unsigned char __wrap_##name##_get();
|
||||
|
||||
#define WRAP_DISABLE_DECL(name) \
|
||||
static unsigned char __wrap_##name##_disabled = 0; \
|
||||
void __wrap_##name##_disable() { \
|
||||
__wrap_##name##_disabled = 1; \
|
||||
} \
|
||||
void __wrap_##name##_enable() { \
|
||||
__wrap_##name##_disabled = 0; \
|
||||
} \
|
||||
void __wrap_##name##_set(unsigned char disabled) { \
|
||||
__wrap_##name##_disabled = disabled; \
|
||||
} \
|
||||
unsigned char __wrap_##name##_get() { \
|
||||
return __wrap_##name##_disabled; \
|
||||
}
|
||||
|
||||
#define WRAP_DISABLE_CHECK(name) \
|
||||
{ \
|
||||
if (__wrap_##name##_disabled) \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
#define WRAP_PRINTF(name) \
|
||||
WRAP_DISABLE_DECL(name) \
|
||||
int __wrap_##name(const char *format, ...) { \
|
||||
WRAP_DISABLE_CHECK(name); \
|
||||
va_list va; \
|
||||
va_start(va, format); \
|
||||
const int ret = vprintf(format, va); \
|
||||
va_end(va); \
|
||||
return ret; \
|
||||
}
|
||||
|
||||
#define WRAP_SPRINTF(name) \
|
||||
int __wrap_##name(char *s, const char *format, ...) { \
|
||||
va_list va; \
|
||||
va_start(va, format); \
|
||||
const int ret = vsprintf(s, format, va); \
|
||||
va_end(va); \
|
||||
return ret; \
|
||||
}
|
||||
|
||||
#define WRAP_SNPRINTF(name) \
|
||||
int __wrap_##name(char *s, size_t count, const char *format, ...) { \
|
||||
va_list va; \
|
||||
va_start(va, format); \
|
||||
const int ret = vsnprintf(s, count, format, va); \
|
||||
va_end(va); \
|
||||
return ret; \
|
||||
}
|
||||
|
||||
#define WRAP_VPRINTF(name) \
|
||||
WRAP_DISABLE_DECL(name) \
|
||||
int __wrap_##name(const char *format, va_list arg) { \
|
||||
WRAP_DISABLE_CHECK(name); \
|
||||
return vprintf(format, arg); \
|
||||
}
|
||||
|
||||
#define WRAP_VSPRINTF(name) \
|
||||
int __wrap_##name(char *s, const char *format, va_list arg) { \
|
||||
return vsprintf(s, format, arg); \
|
||||
}
|
||||
|
||||
#define WRAP_VSNPRINTF(name) \
|
||||
int __wrap_##name(char *s, size_t count, const char *format, va_list arg) { \
|
||||
return vsnprintf(s, count, format, arg); \
|
||||
}
|
||||
9
arduino/libretuya/port/printf/putchar.c
Normal file
9
arduino/libretuya/port/printf/putchar.c
Normal file
@@ -0,0 +1,9 @@
|
||||
// https://github.com/embeddedartistry/libc/blob/master/src/stdio/putchar.c
|
||||
|
||||
#include <printf/printf.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int __wrap_putchar(int c) {
|
||||
putchar_((char)c);
|
||||
return c;
|
||||
}
|
||||
21
arduino/libretuya/port/printf/puts.c
Normal file
21
arduino/libretuya/port/printf/puts.c
Normal file
@@ -0,0 +1,21 @@
|
||||
// https://github.com/embeddedartistry/libc/blob/master/src/stdio/puts.c
|
||||
|
||||
#include <printf/printf.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int __wrap_puts(const char *str) {
|
||||
int r = 0;
|
||||
|
||||
for (const char *c = str; *c != 0; c++) {
|
||||
putchar_((int)*c);
|
||||
r++;
|
||||
}
|
||||
|
||||
// puts adds a newline
|
||||
if (r) {
|
||||
putchar_('\n');
|
||||
r++;
|
||||
}
|
||||
|
||||
return r ? r : EOF;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-05-16. */
|
||||
|
||||
#include <stddef.h>
|
||||
#include <sdk_mem.h>
|
||||
|
||||
__attribute__((weak)) char *strdup(const char *s) {
|
||||
size_t len = strlen(s) + 1;
|
||||
|
||||
@@ -11,19 +11,12 @@
|
||||
#include <core/LibreTuyaAPI.h>
|
||||
#undef PinMode
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" uint32_t SystemCoreClock;
|
||||
#else
|
||||
extern uint32_t SystemCoreClock;
|
||||
#endif
|
||||
#define clockCyclesPerMicrosecond() (SystemCoreClock / 1000000L)
|
||||
#define clockCyclesToMicroseconds(a) (((a)*1000L) / (SystemCoreClock / 1000L))
|
||||
#define microsecondsToClockCycles(a) ((a) * (SystemCoreClock / 1000000L))
|
||||
|
||||
#define interrupts() vPortClearInterruptMask(0)
|
||||
#define noInterrupts() ulPortSetInterruptMask()
|
||||
|
||||
// Include family-specific code
|
||||
#include "WVariant.h"
|
||||
// Include board variant
|
||||
#include "variant.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include "LOGUARTClass.h"
|
||||
extern LOGUARTClass Serial;
|
||||
#endif
|
||||
|
||||
@@ -12,12 +12,19 @@ void LibreTuya::restart() {
|
||||
sys_reset();
|
||||
}
|
||||
|
||||
void LibreTuya::gpioRecover() {
|
||||
// PA14 and PA15 are apparently unusable with SWD enabled
|
||||
sys_jtag_off();
|
||||
Pinmux_Config(PA_14, PINMUX_FUNCTION_GPIO);
|
||||
Pinmux_Config(PA_15, PINMUX_FUNCTION_GPIO);
|
||||
}
|
||||
|
||||
/* CPU-related */
|
||||
|
||||
ChipType LibreTuya::getChipType() {
|
||||
uint8_t chipId;
|
||||
EFUSE_OneByteReadROM(9902, 0xF8, &chipId, L25EOUTVOLTAGE);
|
||||
return (ChipType)(((RTL8710B >> 24) << 8) | chipId);
|
||||
return CHIP_TYPE_ENUM(F_RTL8710B, chipId);
|
||||
}
|
||||
|
||||
const char *LibreTuya::getChipModel() {
|
||||
@@ -28,9 +35,14 @@ uint32_t LibreTuya::getChipId() {
|
||||
uint32_t chipId = 0;
|
||||
uint8_t *id = (uint8_t *)&chipId;
|
||||
// 9902 was extracted from ROM disassembly, probably not needed
|
||||
EFUSE_OneByteReadROM(9902, 0x3B, id + 0, L25EOUTVOLTAGE);
|
||||
/* EFUSE_OneByteReadROM(9902, 0x3B, id + 0, L25EOUTVOLTAGE);
|
||||
EFUSE_OneByteReadROM(9902, 0x3C, id + 1, L25EOUTVOLTAGE);
|
||||
EFUSE_OneByteReadROM(9902, 0x3D, id + 2, L25EOUTVOLTAGE);
|
||||
EFUSE_OneByteReadROM(9902, 0x3D, id + 2, L25EOUTVOLTAGE); */
|
||||
// new method, based on EFUSE logical map
|
||||
uint8_t *efuse = (uint8_t *)malloc(512);
|
||||
// TODO do what EFUSE_LogicalMapRead() does, and read only the used data
|
||||
EFUSE_LogicalMap_Read(efuse);
|
||||
memcpy(id, efuse + 0x11A + 3, 3);
|
||||
return chipId;
|
||||
}
|
||||
|
||||
@@ -42,14 +54,26 @@ const char *LibreTuya::getChipCoreType() {
|
||||
return "ARM Cortex-M4F";
|
||||
}
|
||||
|
||||
uint32_t LibreTuya::getCpuFreqMHz() {
|
||||
return CPU_ClkGet(false) / 1000000;
|
||||
uint32_t LibreTuya::getCpuFreq() {
|
||||
return CPU_ClkGet(false);
|
||||
}
|
||||
|
||||
inline uint32_t LibreTuya::getCycleCount() {
|
||||
uint32_t LibreTuya::getCycleCount() {
|
||||
return microsecondsToClockCycles(micros());
|
||||
}
|
||||
|
||||
/* Flash memory utilities */
|
||||
|
||||
FlashId LibreTuya::getFlashChipId() {
|
||||
FlashId id;
|
||||
uint8_t idBytes[3];
|
||||
flash_read_id(NULL, idBytes, 3);
|
||||
id.manufacturerId = idBytes[0];
|
||||
id.chipId = idBytes[1];
|
||||
id.chipSizeId = idBytes[2];
|
||||
return id;
|
||||
}
|
||||
|
||||
/* Memory management */
|
||||
|
||||
uint32_t LibreTuya::getRamSize() {
|
||||
|
||||
@@ -4,45 +4,53 @@
|
||||
#include <gpio_irq_ex_api.h>
|
||||
|
||||
extern void *gpio_pin_struct[PINS_COUNT];
|
||||
extern void *gpio_irq_handler_list[PINS_COUNT];
|
||||
static void *gpio_irq_handler_list[PINS_COUNT] = {NULL};
|
||||
static void *gpio_irq_handler_args[PINS_COUNT] = {NULL};
|
||||
|
||||
extern bool pinInvalid(pin_size_t pinNumber);
|
||||
extern void pinRemoveMode(pin_size_t pinNumber);
|
||||
|
||||
void gpioIrqHandler(uint32_t id, gpio_irq_event event) {
|
||||
if (gpio_irq_handler_list[id] != NULL) {
|
||||
((void (*)(uint32_t, uint32_t))gpio_irq_handler_list[id])(id, (uint32_t)event);
|
||||
if (gpio_irq_handler_args[id] != NULL)
|
||||
((voidFuncPtr)gpio_irq_handler_list[id])();
|
||||
else
|
||||
((voidFuncPtrParam)gpio_irq_handler_list[id])(gpio_irq_handler_args[id]);
|
||||
}
|
||||
}
|
||||
|
||||
void attachInterrupt(pin_size_t interruptNumber, voidFuncPtr callback, PinStatus mode) {
|
||||
attachInterruptParam(interruptNumber, callback, mode, NULL);
|
||||
}
|
||||
|
||||
void attachInterruptParam(pin_size_t interruptNumber, voidFuncPtrParam callback, PinStatus mode, void *param) {
|
||||
if (pinInvalid(interruptNumber))
|
||||
return;
|
||||
|
||||
gpio_irq_handler_list[interruptNumber] = callback;
|
||||
gpio_irq_handler_args[interruptNumber] = param;
|
||||
|
||||
if (g_APinDescription[interruptNumber].ulPinType == PIO_GPIO_IRQ &&
|
||||
g_APinDescription[interruptNumber].ulPinMode == mode)
|
||||
if (pinTable[interruptNumber].enabled == PIN_IRQ && pinTable[interruptNumber].mode == mode)
|
||||
// Nothing changes in pin mode
|
||||
return;
|
||||
|
||||
if (g_APinDescription[interruptNumber].ulPinType != PIO_GPIO_IRQ)
|
||||
if (pinTable[interruptNumber].enabled != PIN_IRQ)
|
||||
// pin mode changes; deinit gpio and free memory
|
||||
pinRemoveMode(interruptNumber);
|
||||
|
||||
gpio_irq_t *gpio;
|
||||
|
||||
if (g_APinDescription[interruptNumber].ulPinType == NOT_INITIAL) {
|
||||
if (pinTable[interruptNumber].enabled == PIN_NONE) {
|
||||
// allocate memory if pin not used before
|
||||
gpio = malloc(sizeof(gpio_irq_t));
|
||||
gpio_pin_struct[interruptNumber] = gpio;
|
||||
gpio_irq_init(gpio, g_APinDescription[interruptNumber].pinname, gpioIrqHandler, interruptNumber);
|
||||
g_APinDescription[interruptNumber].ulPinType = PIO_GPIO_IRQ;
|
||||
gpio_irq_init(gpio, pinTable[interruptNumber].gpio, gpioIrqHandler, interruptNumber);
|
||||
pinTable[interruptNumber].enabled = PIN_IRQ;
|
||||
} else {
|
||||
// pin already used as irq
|
||||
gpio = (gpio_irq_t *)gpio_pin_struct[interruptNumber];
|
||||
}
|
||||
g_APinDescription[interruptNumber].ulPinMode = mode;
|
||||
pinTable[interruptNumber].mode = mode;
|
||||
|
||||
gpio_irq_event event;
|
||||
|
||||
@@ -71,7 +79,7 @@ void detachInterrupt(pin_size_t interruptNumber) {
|
||||
if (pinInvalid(interruptNumber))
|
||||
return;
|
||||
|
||||
if (g_APinDescription[interruptNumber].ulPinType == PIO_GPIO_IRQ) {
|
||||
if (pinTable[interruptNumber].enabled == PIN_IRQ) {
|
||||
pinRemoveMode(interruptNumber);
|
||||
}
|
||||
gpio_irq_handler_list[interruptNumber] = NULL;
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@@ -12,50 +8,29 @@ extern "C" {
|
||||
#include "sdk_mem.h"
|
||||
#include "sdk_os.h"
|
||||
|
||||
#define NOT_INITIAL (1UL << 0)
|
||||
#define PIO_GPIO (1UL << 1)
|
||||
#define PIO_PWM (1UL << 2)
|
||||
#define PIO_I2C (1UL << 3)
|
||||
#define PIO_ADC (1UL << 4)
|
||||
#define PIO_DAC (1UL << 5)
|
||||
#define PIO_GPIO_IRQ (1UL << 6)
|
||||
|
||||
#define PWM_MODE_ENABLED 1
|
||||
#define PWM_MODE_DISABLED 0
|
||||
|
||||
#define DEFAULT 1
|
||||
#define EXTERNAL 0
|
||||
|
||||
#define round(x) ((x) >= 0 ? (long)((x) + 0.5) : (long)((x)-0.5))
|
||||
|
||||
typedef enum _eAnalogReference {
|
||||
AR_DEFAULT,
|
||||
} eAnalogReference;
|
||||
|
||||
// Types used for the table below
|
||||
typedef struct _PinDescription {
|
||||
// HW PinNames
|
||||
uint32_t pinname;
|
||||
// Current Pin Type
|
||||
uint32_t ulPinType;
|
||||
// Supported Pin Function
|
||||
uint32_t ulPinAttribute;
|
||||
// Current Pin Mode
|
||||
uint32_t ulPinMode;
|
||||
} PinDescription;
|
||||
|
||||
// Pins table to be instantiated into variant.cpp
|
||||
extern PinDescription g_APinDescription[];
|
||||
|
||||
// Additional Wiring functions
|
||||
extern uint32_t digitalPinToPort(uint32_t pinNumber);
|
||||
extern uint32_t digitalPinToBitMask(uint32_t pinNumber);
|
||||
extern void analogReadResolution(int res);
|
||||
extern void analogWriteResolution(int res);
|
||||
extern void analogOutputInit(void);
|
||||
extern void analogWritePeriod(int us);
|
||||
extern void wait_for_debug();
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" uint32_t SystemCoreClock;
|
||||
#else
|
||||
extern uint32_t SystemCoreClock;
|
||||
#endif
|
||||
#define clockCyclesPerMicrosecond() (SystemCoreClock / 1000000L)
|
||||
#define clockCyclesToMicroseconds(a) (a * 1000L / (SystemCoreClock / 1000L))
|
||||
#define microsecondsToClockCycles(a) (a * (SystemCoreClock / 1000000L))
|
||||
|
||||
#define interrupts() vPortClearInterruptMask(0)
|
||||
#define noInterrupts() ulPortSetInterruptMask()
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -22,53 +22,23 @@
|
||||
#include <Arduino.h>
|
||||
#include <cmsis_os.h>
|
||||
|
||||
// Weak empty variant initialization function.
|
||||
// May be redefined by variant files.
|
||||
void initVariant() __attribute__((weak));
|
||||
|
||||
void initVariant() {}
|
||||
|
||||
// Initialize C library
|
||||
extern "C" void __libc_init_array(void);
|
||||
|
||||
osThreadId main_tid = 0;
|
||||
|
||||
void main_task(const void *arg) {
|
||||
setup();
|
||||
extern uint32_t GlobalDebugEnable;
|
||||
extern uint16_t GlobalDebugLevel;
|
||||
extern uint8_t GlobalPrivateLog;
|
||||
|
||||
for (;;) {
|
||||
loop();
|
||||
if (serialEventRun)
|
||||
serialEventRun();
|
||||
yield();
|
||||
}
|
||||
void init() {
|
||||
// make the SDK less verbose by default
|
||||
GlobalDebugEnable = 0;
|
||||
GlobalPrivateLog = 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
LT_BANNER();
|
||||
init();
|
||||
|
||||
__libc_init_array();
|
||||
|
||||
initVariant();
|
||||
|
||||
bool startMainTask() {
|
||||
osThreadDef(main_task, osPriorityRealtime, 1, 4096 * 4);
|
||||
main_tid = osThreadCreate(osThread(main_task), NULL);
|
||||
|
||||
osKernelStart();
|
||||
|
||||
while (1)
|
||||
;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void serialEvent() __attribute__((weak));
|
||||
bool Serial_available() __attribute__((weak));
|
||||
|
||||
void serialEventRun(void) {
|
||||
if (Serial_available && serialEvent && Serial_available())
|
||||
serialEvent();
|
||||
return true;
|
||||
}
|
||||
|
||||
void wait_for_debug() {
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
// va_list is declared by SDK and conflicting
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// disable typedef in basic_types.h
|
||||
#define boolean boolean_rtl
|
||||
@@ -29,9 +28,14 @@
|
||||
// remove previously defined workaround
|
||||
#undef boolean
|
||||
|
||||
// stdio.h
|
||||
#define printf rtl_printf
|
||||
#define sprintf rtl_sprintf
|
||||
// undefine ROM stdio in favor of printf() library (wrappers)
|
||||
#undef printf
|
||||
#undef sprintf
|
||||
#undef vsprintf
|
||||
#undef snprintf
|
||||
#undef vsnprintf
|
||||
#undef vprintf
|
||||
#include <stdio.h>
|
||||
|
||||
// moved from syscalls.h
|
||||
#define _close __rtl_close
|
||||
@@ -51,3 +55,6 @@ extern void DumpForOneBytes(void *addr, int cnt); // cnt max 0x70!
|
||||
extern void SystemCoreClockUpdate(void);
|
||||
|
||||
extern int _sscanf_patch(const char *buf, const char *fmt, ...);
|
||||
|
||||
// include printf() wrapper disable methods
|
||||
#include <printf_port.h>
|
||||
|
||||
@@ -31,9 +31,7 @@ static __inline uint32_t __get_ipsr__(void) {
|
||||
return (__regIPSR);
|
||||
}
|
||||
|
||||
__attribute__((weak)) void init(void) {
|
||||
// nop
|
||||
}
|
||||
void *gpio_pin_struct[PINS_COUNT] = {NULL};
|
||||
|
||||
void delay(uint32_t ms) {
|
||||
/* osStatus ret; */
|
||||
@@ -61,11 +59,11 @@ void delayMicroseconds(unsigned int us) {
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t millis(void) {
|
||||
unsigned long millis(void) {
|
||||
return (__get_ipsr__() == 0) ? xTaskGetTickCount() : xTaskGetTickCountFromISR();
|
||||
}
|
||||
|
||||
uint32_t micros(void) {
|
||||
unsigned long micros(void) {
|
||||
uint32_t tick1, tick2;
|
||||
uint32_t us;
|
||||
uint32_t tick_per_us = F_CPU / 1000;
|
||||
|
||||
@@ -27,66 +27,39 @@ analogin_t adc1;
|
||||
analogin_t adc2;
|
||||
analogin_t adc3;
|
||||
|
||||
static const float ADC_slope1 = (3.12) / (3410.0 - 674.0);
|
||||
static const float ADC_slope2 = (3.3 - 3.12) / (3454.0 - 3410.0);
|
||||
|
||||
bool g_adc_enabled[] = {false, false, false};
|
||||
// from realtek_amebaz_va0_example/example_sources/adc_vbat/src/main.c
|
||||
#define AD2MV(ad, offset, gain) (((ad >> 4) - offset) * 1000 / gain)
|
||||
|
||||
extern void *gpio_pin_struct[];
|
||||
|
||||
extern void pinRemoveMode(pin_size_t pinNumber);
|
||||
|
||||
static int _readResolution = 10;
|
||||
static int _writeResolution = 8;
|
||||
static int _writePeriod = 20000;
|
||||
// TODO implement custom ADC calibration
|
||||
|
||||
void analogReadResolution(int res) {
|
||||
_readResolution = res;
|
||||
}
|
||||
|
||||
void analogWriteResolution(int res) {
|
||||
_writeResolution = res;
|
||||
}
|
||||
|
||||
void analogWritePeriod(int us) {
|
||||
_writePeriod = us;
|
||||
}
|
||||
|
||||
static inline uint32_t mapResolution(uint32_t value, uint32_t from, uint32_t to) {
|
||||
if (from == to)
|
||||
return value;
|
||||
if (from > to)
|
||||
return value >> (from - to);
|
||||
else
|
||||
return value << (to - from);
|
||||
}
|
||||
|
||||
uint8_t analog_reference = AR_DEFAULT;
|
||||
|
||||
void analogReference(uint8_t mode) {
|
||||
analog_reference = mode;
|
||||
}
|
||||
|
||||
int analogRead(pin_size_t pinNumber) {
|
||||
uint32_t ulValue = 0;
|
||||
uint32_t ulChannel;
|
||||
uint16_t analogReadVoltage(pin_size_t pinNumber) {
|
||||
uint16_t ret = 0;
|
||||
float voltage;
|
||||
float adc_value;
|
||||
|
||||
switch (pinNumber) {
|
||||
case PIN_A0:
|
||||
if (g_adc_enabled[0] == false) {
|
||||
analogin_init(&adc1, AD_1);
|
||||
g_adc_enabled[0] = true;
|
||||
}
|
||||
#ifdef PIN_A1
|
||||
case PIN_A1:
|
||||
if (g_adc_enabled[1] == false) {
|
||||
analogin_init(&adc2, AD_2);
|
||||
g_adc_enabled[1] = true;
|
||||
}
|
||||
ret = analogin_read_u16(&adc2);
|
||||
// AD_1 - 0.0V-5.0V
|
||||
return AD2MV(ret, 0x496, 0xBA);
|
||||
#endif
|
||||
#ifdef PIN_A0
|
||||
case PIN_A0:
|
||||
if (g_adc_enabled[0] == false) {
|
||||
analogin_init(&adc1, AD_1);
|
||||
g_adc_enabled[0] = true;
|
||||
}
|
||||
ret = analogin_read_u16(&adc1);
|
||||
break;
|
||||
#endif
|
||||
#ifdef PIN_A2
|
||||
case PIN_A2:
|
||||
if (g_adc_enabled[2] == false) {
|
||||
analogin_init(&adc3, AD_3);
|
||||
@@ -94,58 +67,45 @@ int analogRead(pin_size_t pinNumber) {
|
||||
}
|
||||
ret = analogin_read_u16(&adc3);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
printf("%s : pinNumber %d wrong\n", __FUNCTION__, pinNumber);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret >>= 4;
|
||||
if (ret < 674) {
|
||||
voltage = 0;
|
||||
} else if (ret > 3410) {
|
||||
voltage = (float)(ret - 3410) * ADC_slope2 + 3.12;
|
||||
} else {
|
||||
voltage = (float)(ret - 674) * ADC_slope1;
|
||||
}
|
||||
|
||||
ret = round((1 << _readResolution) * voltage / 3.3);
|
||||
if (ret >= (1 << _readResolution))
|
||||
ret = (1 << _readResolution) - 1;
|
||||
return ret;
|
||||
// AD_0, AD_2 - 0.0V-3.3V
|
||||
return AD2MV(ret, 0x418, 0x342);
|
||||
}
|
||||
|
||||
void analogOutputInit(void) {
|
||||
// nop
|
||||
uint16_t analogReadMaxVoltage(pin_size_t pinNumber) {
|
||||
#ifdef PIN_A1
|
||||
if (pinNumber == PIN_A1)
|
||||
return 5000;
|
||||
#endif
|
||||
return 3300;
|
||||
}
|
||||
|
||||
// Right now, PWM output only works on the pins with
|
||||
// hardware support. These are defined in the appropriate
|
||||
// pins_*.c file. For the rest of the pins, we default
|
||||
// to digital output.
|
||||
void analogWrite(pin_size_t pinNumber, int value) {
|
||||
PinInfo *pin = pinInfo(pinNumber);
|
||||
if (!pin)
|
||||
return;
|
||||
pwmout_t *obj;
|
||||
|
||||
if ((g_APinDescription[pinNumber].ulPinAttribute & PIO_PWM) == PIO_PWM) {
|
||||
/* Handle */
|
||||
if (g_APinDescription[pinNumber].ulPinType != PIO_PWM) {
|
||||
if ((g_APinDescription[pinNumber].ulPinType == PIO_GPIO) ||
|
||||
(g_APinDescription[pinNumber].ulPinType == PIO_GPIO_IRQ)) {
|
||||
if (pinSupported(pin, PIN_PWM)) {
|
||||
float percent = value * 1.0 / (1 << _analogWriteResolution);
|
||||
if (pin->enabled != PIN_PWM) {
|
||||
if ((pin->enabled == PIN_GPIO) || (pin->enabled == PIN_IRQ)) {
|
||||
pinRemoveMode(pinNumber);
|
||||
}
|
||||
gpio_pin_struct[pinNumber] = malloc(sizeof(pwmout_t));
|
||||
pwmout_t *obj = (pwmout_t *)gpio_pin_struct[pinNumber];
|
||||
pwmout_init(obj, g_APinDescription[pinNumber].pinname);
|
||||
pwmout_period_us(obj, _writePeriod);
|
||||
pwmout_write(obj, value * 1.0 / (1 << _writeResolution));
|
||||
g_APinDescription[pinNumber].ulPinType = PIO_PWM;
|
||||
g_APinDescription[pinNumber].ulPinMode = PWM_MODE_ENABLED;
|
||||
pwmout_init(obj, pin->gpio);
|
||||
pwmout_period_us(obj, _analogWritePeriod);
|
||||
pwmout_write(obj, percent);
|
||||
pin->enabled = PIN_PWM;
|
||||
} else {
|
||||
pwmout_t *obj = (pwmout_t *)gpio_pin_struct[pinNumber];
|
||||
pwmout_period_us(obj, _writePeriod);
|
||||
pwmout_write(obj, value * 1.0 / (1 << _writeResolution));
|
||||
/* if ( g_APinDescription[pinNumber].ulPinMode == PWM_MODE_DISABLED ) {
|
||||
HAL_Pwm_Enable( &obj->pwm_hal_adp );
|
||||
} */
|
||||
// pwmout_period_us(obj, _writePeriod);
|
||||
pwmout_write(obj, percent);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,30 +130,26 @@ void _tone_timer_handler(const void *argument) {
|
||||
void _tone(uint32_t ulPin, unsigned int frequency, unsigned long duration) {
|
||||
pwmout_t *obj;
|
||||
|
||||
if ((g_APinDescription[ulPin].ulPinAttribute & PIO_PWM) != PIO_PWM) {
|
||||
if ((pinTable[ulPin].supported & PIN_PWM) != PIN_PWM) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (g_APinDescription[ulPin].ulPinType != PIO_PWM) {
|
||||
if ((g_APinDescription[ulPin].ulPinType == PIO_GPIO) || (g_APinDescription[ulPin].ulPinType == PIO_GPIO_IRQ)) {
|
||||
if (pinTable[ulPin].enabled != PIN_PWM) {
|
||||
if ((pinTable[ulPin].enabled == PIN_GPIO) || (pinTable[ulPin].enabled == PIN_IRQ)) {
|
||||
pinRemoveMode(ulPin);
|
||||
}
|
||||
gpio_pin_struct[ulPin] = malloc(sizeof(pwmout_t));
|
||||
pwmout_t *obj = (pwmout_t *)gpio_pin_struct[ulPin];
|
||||
pwmout_init(obj, g_APinDescription[ulPin].pinname);
|
||||
pwmout_init(obj, pinTable[ulPin].gpio);
|
||||
pwmout_period(obj, 1.0 / frequency);
|
||||
pwmout_pulsewidth(obj, 1.0 / (frequency * 2));
|
||||
g_APinDescription[ulPin].ulPinType = PIO_PWM;
|
||||
g_APinDescription[ulPin].ulPinMode = PWM_MODE_ENABLED;
|
||||
pinTable[ulPin].enabled = PIN_PWM;
|
||||
|
||||
} else {
|
||||
// There is already a PWM configured
|
||||
pwmout_t *obj = (pwmout_t *)gpio_pin_struct[ulPin];
|
||||
pwmout_period(obj, 1.0 / frequency);
|
||||
pwmout_pulsewidth(obj, 1.0 / (frequency * 2));
|
||||
/* if (g_APinDescription[ulPin].ulPinMode == PWM_MODE_DISABLED) {
|
||||
HAL_Pwm_Enable( &obj->pwm_hal_adp );
|
||||
} */
|
||||
}
|
||||
|
||||
if (duration > 0) {
|
||||
|
||||
@@ -5,64 +5,64 @@
|
||||
#include <pwmout_api.h>
|
||||
|
||||
extern void *gpio_pin_struct[PINS_COUNT];
|
||||
extern void *gpio_irq_handler_list[PINS_COUNT];
|
||||
|
||||
bool pinInvalid(pin_size_t pinNumber) {
|
||||
return pinNumber < 0 || pinNumber >= PINS_COUNT || g_APinDescription[pinNumber].pinname == NC;
|
||||
}
|
||||
|
||||
void pinRemoveMode(pin_size_t pinNumber) {
|
||||
if (pinInvalid(pinNumber))
|
||||
PinInfo *pin = pinInfo(pinNumber);
|
||||
if (!pin)
|
||||
return;
|
||||
if (g_APinDescription[pinNumber].ulPinType == PIO_PWM) {
|
||||
if (pinEnabled(pin, PIN_PWM)) {
|
||||
pwmout_t *obj = (pwmout_t *)gpio_pin_struct[pinNumber];
|
||||
pwmout_free(obj);
|
||||
}
|
||||
if (g_APinDescription[pinNumber].ulPinType == PIO_GPIO) {
|
||||
if (pinEnabled(pin, PIN_GPIO)) {
|
||||
gpio_t *obj = (gpio_t *)gpio_pin_struct[pinNumber];
|
||||
gpio_deinit(obj, g_APinDescription[pinNumber].pinname);
|
||||
gpio_deinit(obj, pin->gpio);
|
||||
free(obj);
|
||||
}
|
||||
if (g_APinDescription[pinNumber].ulPinType == PIO_GPIO_IRQ) {
|
||||
if (pinEnabled(pin, PIN_IRQ)) {
|
||||
gpio_irq_t *obj = (gpio_irq_t *)gpio_pin_struct[pinNumber];
|
||||
gpio_irq_deinit(obj);
|
||||
free(obj);
|
||||
}
|
||||
gpio_pin_struct[pinNumber] = NULL;
|
||||
g_APinDescription[pinNumber].ulPinType = NOT_INITIAL;
|
||||
g_APinDescription[pinNumber].ulPinMode = NOT_INITIAL;
|
||||
gpio_pin_struct[pinNumber] = NULL;
|
||||
pin->enabled = PIN_NONE;
|
||||
}
|
||||
|
||||
void pinMode(pin_size_t pinNumber, PinModeArduino pinMode) {
|
||||
if (pinInvalid(pinNumber))
|
||||
PinInfo *pin = pinInfo(pinNumber);
|
||||
if (!pin)
|
||||
return;
|
||||
|
||||
if (g_APinDescription[pinNumber].ulPinType == PIO_GPIO && g_APinDescription[pinNumber].ulPinMode == pinMode)
|
||||
if (pinEnabled(pin, PIN_GPIO) && pin->mode == pinMode)
|
||||
// Nothing changes in pin mode
|
||||
return;
|
||||
|
||||
/* if (g_APinDescription[pinNumber].ulPinType == PIO_PWM) {
|
||||
if (!pinSupported(pin, PIN_GPIO))
|
||||
// cannot set ADC as I/O
|
||||
return;
|
||||
|
||||
/* if (pin->enabled == PIN_PWM) {
|
||||
// If this pin has been configured as PWM, then it cannot change to another mode
|
||||
return;
|
||||
} */
|
||||
|
||||
if (g_APinDescription[pinNumber].ulPinType != PIO_GPIO)
|
||||
if (pin->enabled != PIN_GPIO)
|
||||
// pin mode changes; deinit gpio and free memory
|
||||
pinRemoveMode(pinNumber);
|
||||
|
||||
gpio_t *gpio;
|
||||
|
||||
if (g_APinDescription[pinNumber].ulPinType == NOT_INITIAL) {
|
||||
if (pin->enabled == PIN_NONE) {
|
||||
// allocate memory if pin not used before
|
||||
gpio = malloc(sizeof(gpio_t));
|
||||
gpio_pin_struct[pinNumber] = gpio;
|
||||
gpio_init(gpio, g_APinDescription[pinNumber].pinname);
|
||||
g_APinDescription[pinNumber].ulPinType = PIO_GPIO;
|
||||
gpio_init(gpio, pin->gpio);
|
||||
pin->enabled = PIN_GPIO;
|
||||
} else {
|
||||
// pin already used as gpio
|
||||
gpio = (gpio_t *)gpio_pin_struct[pinNumber];
|
||||
}
|
||||
g_APinDescription[pinNumber].ulPinMode = pinMode;
|
||||
pin->mode = pinMode;
|
||||
|
||||
PinDirection dir;
|
||||
PinMode mode;
|
||||
@@ -97,9 +97,10 @@ void pinMode(pin_size_t pinNumber, PinModeArduino pinMode) {
|
||||
}
|
||||
|
||||
void digitalWrite(pin_size_t pinNumber, PinStatus status) {
|
||||
if (pinInvalid(pinNumber))
|
||||
PinInfo *pin = pinInfo(pinNumber);
|
||||
if (!pin)
|
||||
return;
|
||||
if (g_APinDescription[pinNumber].ulPinType != PIO_GPIO)
|
||||
if (pin->enabled != PIN_GPIO)
|
||||
return;
|
||||
|
||||
gpio_t *gpio = (gpio_t *)gpio_pin_struct[pinNumber];
|
||||
@@ -107,9 +108,10 @@ void digitalWrite(pin_size_t pinNumber, PinStatus status) {
|
||||
}
|
||||
|
||||
PinStatus digitalRead(pin_size_t pinNumber) {
|
||||
if (pinInvalid(pinNumber))
|
||||
PinInfo *pin = pinInfo(pinNumber);
|
||||
if (!pin)
|
||||
return;
|
||||
if (g_APinDescription[pinNumber].ulPinType != PIO_GPIO)
|
||||
if (pin->enabled != PIN_GPIO)
|
||||
return;
|
||||
|
||||
gpio_t *gpio = (gpio_t *)gpio_pin_struct[pinNumber];
|
||||
@@ -122,7 +124,7 @@ uint32_t digitalPinToPort(uint32_t pinNumber) {
|
||||
if (pinInvalid(pinNumber))
|
||||
return 0xFFFFFFFF;
|
||||
|
||||
uint32_t pin_name = HAL_GPIO_GetPinName(g_APinDescription[pinNumber].pinname);
|
||||
uint32_t pin_name = HAL_GPIO_GetPinName(pinTable[pinNumber].gpio);
|
||||
return HAL_GPIO_GET_PORT_BY_NAME(pin_name);
|
||||
}
|
||||
|
||||
@@ -130,6 +132,6 @@ uint32_t digitalPinToBitMask(uint32_t pinNumber) {
|
||||
if (pinInvalid(pinNumber))
|
||||
return 0xFFFFFFFF;
|
||||
|
||||
uint32_t pin_name = HAL_GPIO_GetPinName(g_APinDescription[pinNumber].pinname);
|
||||
uint32_t pin_name = HAL_GPIO_GetPinName(pinTable[pinNumber].gpio);
|
||||
return 1 << (HAL_GPIO_GET_PIN_BY_NAME(pin_name));
|
||||
}
|
||||
|
||||
@@ -36,11 +36,11 @@ extern unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
|
||||
|
||||
uint32_t start_ticks, cur_ticks;
|
||||
|
||||
if (pin < 0 || pin > PINS_COUNT || (g_APinDescription[pin].pinname == NC))
|
||||
if (pin < 0 || pin > PINS_COUNT || (pinTable[pin].gpio == NC))
|
||||
return 0;
|
||||
|
||||
/* Handle */
|
||||
if (g_APinDescription[pin].ulPinType != PIO_GPIO) {
|
||||
if (pinTable[pin].enabled != PIN_GPIO) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-04-24. */
|
||||
|
||||
#include "Flash.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
extern "C" {
|
||||
#include <flash_api.h>
|
||||
}
|
||||
|
||||
// Global Flash object.
|
||||
FlashClass Flash;
|
||||
|
||||
FlashClass::FlashClass() {
|
||||
flash = NULL;
|
||||
}
|
||||
|
||||
FlashClass::~FlashClass() {
|
||||
if (!flash)
|
||||
return;
|
||||
free(flash);
|
||||
flash = NULL;
|
||||
}
|
||||
|
||||
void FlashClass::initialize() {
|
||||
if (flash)
|
||||
return;
|
||||
flash = (flash_t *)malloc(sizeof(flash_t));
|
||||
flash_get_status(flash);
|
||||
}
|
||||
|
||||
FlashId FlashClass::getChipId() {
|
||||
initialize();
|
||||
FlashId id;
|
||||
uint8_t idBytes[3];
|
||||
flash_read_id(flash, idBytes, 3);
|
||||
id.manufacturerId = idBytes[0];
|
||||
id.chipId = idBytes[1];
|
||||
id.chipSizeId = idBytes[2];
|
||||
return id;
|
||||
}
|
||||
|
||||
uint32_t FlashClass::getSize() {
|
||||
initialize();
|
||||
FlashId id = getChipId();
|
||||
if (id.chipSizeId >= 0x14 && id.chipSizeId <= 0x19) {
|
||||
return (1 << id.chipSizeId);
|
||||
}
|
||||
return 1024 * 1024;
|
||||
}
|
||||
|
||||
bool FlashClass::eraseSector(uint32_t offset) {
|
||||
initialize();
|
||||
flash_erase_sector(flash, offset);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FlashClass::readBlock(uint32_t offset, uint8_t *data, size_t size) {
|
||||
initialize();
|
||||
return flash_stream_read(flash, offset, size, data);
|
||||
}
|
||||
|
||||
bool FlashClass::writeBlock(uint32_t offset, uint8_t *data, size_t size) {
|
||||
initialize();
|
||||
return flash_stream_write(flash, offset, size, data);
|
||||
}
|
||||
5
arduino/realtek-ambz/libraries/MD5/MD5Impl.h
Normal file
5
arduino/realtek-ambz/libraries/MD5/MD5Impl.h
Normal file
@@ -0,0 +1,5 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-06-03. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#define LT_MD5_USE_POLARSSL 1
|
||||
@@ -1,6 +1,5 @@
|
||||
/* Copyright (c) Kuba Szczodrzyński 2022-04-25. */
|
||||
|
||||
#include "WiFi.h"
|
||||
#include "WiFiPriv.h"
|
||||
|
||||
rtw_network_info_t wifi = {0};
|
||||
@@ -26,54 +25,14 @@ void reset_wifi_struct(void) {
|
||||
}
|
||||
|
||||
WiFiClass::WiFiClass() {
|
||||
_scanSem = xSemaphoreCreateBinary();
|
||||
data.scanSem = xSemaphoreCreateBinary();
|
||||
}
|
||||
|
||||
WiFiClass::~WiFiClass() {
|
||||
vSemaphoreDelete(_scanSem);
|
||||
vSemaphoreDelete(data.scanSem);
|
||||
}
|
||||
|
||||
void WiFiClass::printDiag(Print &dest) {
|
||||
const char *modes[] = {"NULL", "STA", "AP", "STA+AP"};
|
||||
const char *enc[] = {"Open", "WEP", "WPA PSK", "WPA2 PSK", "WPA/WPA2", "WPA", "WPA2"};
|
||||
|
||||
dest.print("Mode: ");
|
||||
dest.println(modes[getMode()]);
|
||||
|
||||
if (wifi_mode & WIFI_MODE_STA) {
|
||||
dest.println("-- Station --");
|
||||
dest.print("SSID: ");
|
||||
dest.println(SSID());
|
||||
if (isConnected()) {
|
||||
dest.print("BSSID: ");
|
||||
dest.println(BSSIDstr());
|
||||
dest.print("RSSI: ");
|
||||
dest.println(RSSI());
|
||||
dest.print("Encryption: ");
|
||||
dest.println(enc[getEncryption()]);
|
||||
dest.print("IP: ");
|
||||
dest.println(localIP());
|
||||
dest.print("MAC: ");
|
||||
dest.println(macAddress());
|
||||
dest.print("Hostname: ");
|
||||
dest.println(getHostname());
|
||||
}
|
||||
}
|
||||
|
||||
if (wifi_mode & WIFI_MODE_AP) {
|
||||
dest.println("-- Access Point --");
|
||||
dest.print("SSID: ");
|
||||
dest.println(softAPSSID());
|
||||
dest.print("IP: ");
|
||||
dest.println(softAPIP());
|
||||
dest.print("MAC: ");
|
||||
dest.println(softAPmacAddress());
|
||||
dest.print("Hostname: ");
|
||||
dest.println(softAPgetHostname());
|
||||
}
|
||||
}
|
||||
|
||||
WiFiAuthMode WiFiClass::securityTypeToAuthMode(uint8_t type) {
|
||||
WiFiAuthMode securityTypeToAuthMode(uint8_t type) {
|
||||
// the value reported in rtw_scan_result is rtw_encryption_t, even though it's rtw_security_t in the header file
|
||||
switch (type) {
|
||||
case RTW_ENCRYPTION_OPEN:
|
||||
@@ -91,5 +50,3 @@ WiFiAuthMode WiFiClass::securityTypeToAuthMode(uint8_t type) {
|
||||
}
|
||||
return WIFI_AUTH_INVALID;
|
||||
}
|
||||
|
||||
WiFiClass WiFi;
|
||||
|
||||
@@ -3,183 +3,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <api/WiFi.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
#include <FreeRTOS.h>
|
||||
#include <semphr.h>
|
||||
#include <wifi_structures.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
#include <api/WiFi/WiFi.h>
|
||||
|
||||
#include "WiFiClient.h"
|
||||
#include "WiFiClientSecure.h"
|
||||
#include "WiFiServer.h"
|
||||
|
||||
class WiFiClass : public IWiFiClass,
|
||||
public IWiFiGenericClass,
|
||||
public IWiFiSTAClass,
|
||||
public IWiFiScanClass,
|
||||
public IWiFiAPClass {
|
||||
private:
|
||||
static WiFiAuthMode securityTypeToAuthMode(uint8_t type);
|
||||
|
||||
bool _initialized;
|
||||
bool _sleep;
|
||||
|
||||
bool _scanning = false;
|
||||
SemaphoreHandle_t _scanSem;
|
||||
uint8_t _netCount = 0;
|
||||
char **_netSsid = NULL;
|
||||
WiFiAuthMode *_netEncr = NULL;
|
||||
int32_t *_netRssi = NULL;
|
||||
rtw_mac_t *_netBssid = NULL;
|
||||
int32_t *_netChannel = NULL;
|
||||
|
||||
static rtw_result_t scanHandler(rtw_scan_handler_result_t *result);
|
||||
|
||||
public:
|
||||
// IWiFiClass
|
||||
WiFiClass();
|
||||
~WiFiClass();
|
||||
void printDiag(Print &dest);
|
||||
|
||||
public:
|
||||
// IWiFiGenericClass
|
||||
int32_t channel(void);
|
||||
|
||||
bool mode(WiFiMode mode) override;
|
||||
WiFiMode getMode();
|
||||
WiFiStatus status();
|
||||
|
||||
bool enableSTA(bool enable);
|
||||
bool enableAP(bool enable);
|
||||
|
||||
bool setSleep(bool enable);
|
||||
bool getSleep();
|
||||
|
||||
bool setTxPower(int power);
|
||||
int getTxPower();
|
||||
|
||||
IPAddress hostByName(const char *hostname);
|
||||
|
||||
static IPAddress calculateNetworkID(IPAddress ip, IPAddress subnet);
|
||||
static IPAddress calculateBroadcast(IPAddress ip, IPAddress subnet);
|
||||
static uint8_t calculateSubnetCIDR(IPAddress subnetMask);
|
||||
static String macToString(uint8_t *mac);
|
||||
|
||||
static void handleRtwEvent(uint16_t event, char *data, int len, int flags);
|
||||
|
||||
public:
|
||||
// IWiFiSTAClass
|
||||
WiFiStatus begin(
|
||||
const char *ssid,
|
||||
const char *passphrase = NULL,
|
||||
int32_t channel = 0,
|
||||
const uint8_t *bssid = NULL,
|
||||
bool connect = true
|
||||
) override;
|
||||
WiFiStatus
|
||||
begin(char *ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t *bssid = NULL, bool connect = true);
|
||||
|
||||
bool config(
|
||||
IPAddress localIP,
|
||||
IPAddress gateway,
|
||||
IPAddress subnet,
|
||||
IPAddress dns1 = (uint32_t)0x00000000,
|
||||
IPAddress dns2 = (uint32_t)0x00000000
|
||||
);
|
||||
|
||||
inline bool reconnect() {
|
||||
return reconnect(NULL);
|
||||
}
|
||||
|
||||
bool reconnect(const uint8_t *bssid = NULL);
|
||||
bool disconnect(bool wifiOff = false);
|
||||
|
||||
bool isConnected() override;
|
||||
|
||||
bool setAutoReconnect(bool autoReconnect);
|
||||
bool getAutoReconnect();
|
||||
|
||||
WiFiStatus waitForConnectResult(unsigned long timeout);
|
||||
|
||||
IPAddress localIP();
|
||||
uint8_t *macAddress(uint8_t *mac);
|
||||
String macAddress();
|
||||
IPAddress subnetMask();
|
||||
IPAddress gatewayIP();
|
||||
IPAddress dnsIP(uint8_t dns_no);
|
||||
IPAddress broadcastIP();
|
||||
IPAddress networkID();
|
||||
uint8_t subnetCIDR();
|
||||
bool enableIpV6();
|
||||
IPv6Address localIPv6();
|
||||
const char *getHostname();
|
||||
bool setHostname(const char *hostname);
|
||||
bool setMacAddress(const uint8_t *mac);
|
||||
|
||||
const String SSID();
|
||||
const String psk();
|
||||
uint8_t *BSSID();
|
||||
String BSSIDstr();
|
||||
int8_t RSSI();
|
||||
WiFiAuthMode getEncryption();
|
||||
|
||||
public:
|
||||
// IWiFiScanClass
|
||||
int16_t scanNetworks(
|
||||
bool async = false,
|
||||
bool showHidden = false,
|
||||
bool passive = false,
|
||||
uint32_t maxMsPerChannel = 300,
|
||||
uint8_t channel = 0
|
||||
);
|
||||
bool getNetworkInfo(
|
||||
uint8_t networkItem,
|
||||
String &ssid,
|
||||
WiFiAuthMode &encryptionType,
|
||||
int32_t &RSSI,
|
||||
uint8_t *&BSSID,
|
||||
int32_t &channel
|
||||
);
|
||||
|
||||
int16_t scanComplete();
|
||||
void scanDelete();
|
||||
|
||||
String SSID(uint8_t networkItem);
|
||||
WiFiAuthMode encryptionType(uint8_t networkItem);
|
||||
int32_t RSSI(uint8_t networkItem);
|
||||
uint8_t *BSSID(uint8_t networkItem);
|
||||
String BSSIDstr(uint8_t networkItem);
|
||||
int32_t channel(uint8_t networkItem);
|
||||
|
||||
public:
|
||||
// IWiFiAPClass
|
||||
bool softAP(
|
||||
const char *ssid, const char *passphrase = NULL, int channel = 1, bool ssidHidden = false, int maxClients = 4
|
||||
);
|
||||
bool softAPConfig(IPAddress localIP, IPAddress gateway, IPAddress subnet);
|
||||
bool softAPdisconnect(bool wifiOff = false);
|
||||
|
||||
uint8_t softAPgetStationNum();
|
||||
|
||||
IPAddress softAPIP();
|
||||
IPAddress softAPBroadcastIP();
|
||||
IPAddress softAPNetworkID();
|
||||
uint8_t softAPSubnetCIDR();
|
||||
bool softAPenableIpV6();
|
||||
IPv6Address softAPIPv6();
|
||||
const char *softAPgetHostname();
|
||||
bool softAPsetHostname(const char *hostname);
|
||||
uint8_t *softAPmacAddress(uint8_t *mac);
|
||||
String softAPmacAddress(void);
|
||||
const String softAPSSID(void);
|
||||
};
|
||||
|
||||
extern WiFiClass WiFi;
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user