Nostrduino
Loading...
Searching...
No Matches
ESP32Platform.h
Go to the documentation of this file.
1#ifndef _NOSTR_ESP32_PLATFORM_H
2#define _NOSTR_ESP32_PLATFORM_H 1
3#include "NostrCommon.h"
4
5#ifdef _ESP32_BOARD_
6#include "ESP32Transport.h"
7#include "NostrString.h"
8#include "NostrUtils.h"
9#include "WiFi.h"
10#include "bootloader_random.h"
11#include "esp_random.h"
12#include "esp_wifi.h"
13#include "time.h"
14#include <initializer_list>
15#include <vector>
16namespace nostr {
17namespace esp32 {
18namespace ESP32Platform {
19
20inline unsigned long getUnixTimestamp() {
21 time_t now;
22 struct tm timeinfo;
23 while (!getLocalTime(&timeinfo)) {
24 Serial.println("Still waiting for ntp sync");
25 delay(10);
26 }
27 time(&now);
28 return now;
29}
30
31inline bool fillRandom(uint8_t *output, size_t outputSize) {
32 if (output == nullptr && outputSize != 0) return false;
33 if (outputSize == 0) return true;
34 // The ESP32 hardware RNG exposes only a 32-bit state register that is
35 // refilled by the physical entropy source at a limited rate; draining it
36 // in one shot (esp_fill_random) can outpace the refill and produce
37 // correlated words, especially when Wi-Fi/BT are off and no RF noise is
38 // mixed in (see Espressif's esp_random docs). Pull one 32-bit word per
39 // esp_random() call and yield ~1 ms between reads so the source can
40 // refill; the yield also adds scheduler timing jitter. Same mitigation
41 // as Blockstream Jade 1.0.41.
42 size_t offset = 0;
43 while (offset < outputSize) {
44 uint32_t word = esp_random();
45 for (size_t i = 0; i < sizeof(word) && offset < outputSize; ++i) {
46 output[offset++] = static_cast<uint8_t>(word & 0xFF);
47 word >>= 8;
48 }
49 if (offset < outputSize) delay(1);
50 }
51 return true;
52}
53inline void serialLogger(const NostrString &str) {
54 Serial.println(str.c_str());
55}
56
60inline void initWifi(NostrString ssid, NostrString passphrase, int unused = 6) {
61 esp_wifi_start();
62 WiFi.persistent(false); // don't wear out the flash by writing wifi credentials to it (also fixes wifi connection on a159x36's QEMU for ESP32)
63 WiFi.begin(ssid, passphrase);
64 Serial.println("Connecting to " + ssid);
65 while (WiFi.status() != WL_CONNECTED) {
66 delay(500);
67 Serial.print(".");
68 }
69 Serial.println("");
70 Serial.println("WiFi connected");
71 Serial.println("IP address: ");
72 Serial.println(WiFi.localIP());
73}
74
78inline void initTime(const char *ntpServer, long unused1 = 0, int unused2 = 3600) {
79 configTime(0, 0, ntpServer);
80}
81
90inline void initNostr(bool withLogger, bool initializeRngWithoutRadio = false) {
91 if (initializeRngWithoutRadio) bootloader_random_enable();
93 Utils::init();
95 if (withLogger)
96 nostr::Utils::setLogger(serialLogger);
97}
98
99inline void close() {
100 Utils::close();
101}
102
106inline ESP32Transport *getTransport() {
107 return new nostr::esp32::ESP32Transport();
108}
109inline ESP32Transport *getTransport(const ESP32TransportConfig &config) {
110 return new nostr::esp32::ESP32Transport(config);
111}
112} // namespace ESP32Platform
113} // namespace esp32
114} // namespace nostr
115#endif
116#endif
#define NostrString
Definition NostrString.h:10
static void setRandomBytesProvider(std::function< bool(uint8_t *, size_t)> randomBytesProvider)
Definition NostrUtils.cpp:87
static void setUnixTimeSecondsProvider(std::function< unsigned long long()> timeSecondsProvider)
Definition NostrUtils.cpp:83
static void setLogger(std::function< void(const NostrString &)> logger)
Definition NostrUtils.cpp:91
Definition CryptoLock.h:6