upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/main/beacon_price.c
diff options
context:
space:
mode:
Diffstat (limited to 'main/beacon_price.c')
-rw-r--r--main/beacon_price.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/main/beacon_price.c b/main/beacon_price.c
new file mode 100644
index 0000000..b87e289
--- /dev/null
+++ b/main/beacon_price.c
@@ -0,0 +1,103 @@
1#include "beacon_price.h"
2#include "config.h"
3#include "identity.h"
4#include "esp_log.h"
5#include "esp_wifi.h"
6#include "mbedtls/sha256.h"
7#include <string.h>
8
9static const char *TAG = "beacon_price";
10static bool s_active = false;
11
12void beacon_price_hash_mint(const char *mint_url, uint8_t hash_out[4])
13{
14 uint8_t full_hash[32];
15 mbedtls_sha256((const unsigned char *)mint_url, strlen(mint_url), full_hash, 0);
16 memcpy(hash_out, full_hash, 4);
17}
18
19void beacon_price_hash_npub(const char *npub_hex, uint8_t hash_out[4])
20{
21 uint8_t full_hash[32];
22 mbedtls_sha256((const unsigned char *)npub_hex, strlen(npub_hex), full_hash, 0);
23 memcpy(hash_out, full_hash, 4);
24}
25
26void beacon_price_build_ie(tollgate_price_ie_t *ie)
27{
28 const tollgate_config_t *cfg = tollgate_config_get();
29 const tollgate_identity_t *id = identity_get();
30
31 memset(ie, 0, sizeof(*ie));
32 ie->element_id = WIFI_VENDOR_IE_ELEMENT_ID;
33 ie->length = 4 + TOLLGATE_IE_PAYLOAD_SIZE;
34 ie->vendor_oui[0] = TOLLGATE_OUI_0;
35 ie->vendor_oui[1] = TOLLGATE_OUI_1;
36 ie->vendor_oui[2] = TOLLGATE_OUI_2;
37 ie->vendor_oui_type = TOLLGATE_IE_TYPE;
38
39 tollgate_price_payload_t *p = &ie->payload;
40 p->version = TOLLGATE_IE_VERSION;
41 p->metric = (strcmp(cfg->metric, "bytes") == 0) ? 1 : 0;
42 p->price_per_step = (uint16_t)cfg->price_per_step;
43
44 bool is_bytes = (strcmp(cfg->metric, "bytes") == 0);
45 p->step_size = is_bytes ? (uint32_t)cfg->step_size_bytes : (uint32_t)cfg->step_size_ms;
46
47 beacon_price_hash_mint(cfg->mint_url, p->mint_hash);
48
49 p->geohash_len = (uint8_t)strnlen(cfg->nostr_geohash, TOLLGATE_IE_GEOHASH_MAX);
50 memcpy(p->geohash, cfg->nostr_geohash, p->geohash_len);
51 if (p->geohash_len < TOLLGATE_IE_GEOHASH_MAX) {
52 memset(p->geohash + p->geohash_len, 0, TOLLGATE_IE_GEOHASH_MAX - p->geohash_len);
53 }
54
55 if (id && id->initialized) {
56 beacon_price_hash_npub(id->npub_hex, p->npub_hash);
57 }
58
59 ESP_LOGI(TAG, "Built IE: price=%lu sats, step=%lu, metric=%s, geohash=%.*s",
60 (unsigned long)p->price_per_step, (unsigned long)p->step_size,
61 p->metric ? "bytes" : "milliseconds",
62 p->geohash_len, p->geohash);
63}
64
65esp_err_t beacon_price_start(void)
66{
67 if (s_active) {
68 ESP_LOGW(TAG, "Already active");
69 return ESP_OK;
70 }
71
72 static tollgate_price_ie_t s_ie;
73 beacon_price_build_ie(&s_ie);
74
75 esp_err_t ret = esp_wifi_set_vendor_ie(true, WIFI_VND_IE_TYPE_BEACON,
76 WIFI_VND_IE_ID_0, &s_ie);
77 if (ret != ESP_OK) {
78 ESP_LOGE(TAG, "Failed to set beacon vendor IE: %s", esp_err_to_name(ret));
79 return ret;
80 }
81
82 ret = esp_wifi_set_vendor_ie(true, WIFI_VND_IE_TYPE_PROBE_RESP,
83 WIFI_VND_IE_ID_1, &s_ie);
84 if (ret != ESP_OK) {
85 ESP_LOGW(TAG, "Failed to set probe resp vendor IE: %s", esp_err_to_name(ret));
86 }
87
88 s_active = true;
89 ESP_LOGI(TAG, "Price advertising started (beacon + probe response)");
90 return ESP_OK;
91}
92
93esp_err_t beacon_price_stop(void)
94{
95 if (!s_active) return ESP_OK;
96
97 esp_wifi_set_vendor_ie(false, WIFI_VND_IE_TYPE_BEACON, WIFI_VND_IE_ID_0, NULL);
98 esp_wifi_set_vendor_ie(false, WIFI_VND_IE_TYPE_PROBE_RESP, WIFI_VND_IE_ID_1, NULL);
99
100 s_active = false;
101 ESP_LOGI(TAG, "Price advertising stopped");
102 return ESP_OK;
103}