diff options
| author | Your Name <you@example.com> | 2026-05-23 02:32:24 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-23 02:32:24 +0530 |
| commit | 6771956f1647f377007ab61a59b116de6d536706 (patch) | |
| tree | e8b6c74164040339134e33b8616c27c23ec1ade6 /tollgate_core/include/tollgate_platform.h | |
| parent | 10c69b0ddc34a1fafada3f8b68a622b4aa2bea77 (diff) | |
feat: create tollgate_core standalone C library + tollgate_esp IDF component
Phase 1-4: Extract core business logic into reusable library
tollgate_core/ — standalone C library (zero ESP-IDF deps):
- tollgate_core.c — orchestrator (init, process_payment, process_share, tick)
- tollgate_cashu.c — V3 token decode, checkstate via platform callback, allotment math
- tollgate_session.c — session lifecycle state machine (create/find/revoke/extend)
- tollgate_firewall.c — client allowlist, NAT filter, packet filter
- tollgate_mining.c — hashprice, nbits→difficulty, shares-to-allotment, client stats
components/tollgate_esp/ — ESP-IDF component wrapper:
- tollgate_esp_platform.c — implements tollgate_platform_t using ESP-IDF APIs
- CMakeLists.txt — compiles tollgate_core + ESP wrapper, links to ESP-IDF
- idf_component.yml — for IDF Component Registry
Platform interface (tollgate_platform.h):
- Config access: price, step_size, mint_url, metric
- Time: get_time_ms (via FreeRTOS ticks on ESP)
- Logging: log_info/warn/error (via ESP_LOG on ESP)
- Wallet: receive/send/balance (via nucula on ESP)
- HTTP: http_post (via esp_http_client on ESP)
- WiFi/MAC: mac_for_ip, napt_enable
- Mining: stratum config, hashprice override
Build verification:
- tollgate_core compiles standalone: cmake + make (gcc, host)
- tollgate_esp + ESP-IDF full build: passes (idf.py build)
- Unit tests: 22/22 pass (make test-unit)
- No behavioral change to existing main/ code yet (Phase 5)
Diffstat (limited to 'tollgate_core/include/tollgate_platform.h')
| -rw-r--r-- | tollgate_core/include/tollgate_platform.h | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/tollgate_core/include/tollgate_platform.h b/tollgate_core/include/tollgate_platform.h new file mode 100644 index 0000000..b553a83 --- /dev/null +++ b/tollgate_core/include/tollgate_platform.h | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | #ifndef TOLLGATE_PLATFORM_H | ||
| 2 | #define TOLLGATE_PLATFORM_H | ||
| 3 | |||
| 4 | #include <stdint.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | #include <stddef.h> | ||
| 7 | |||
| 8 | typedef struct { | ||
| 9 | uint16_t (*get_price_sats)(void); | ||
| 10 | int32_t (*get_step_ms)(void); | ||
| 11 | int64_t (*get_step_bytes)(void); | ||
| 12 | const char* (*get_mint_url)(void); | ||
| 13 | const char* (*get_metric)(void); | ||
| 14 | |||
| 15 | int64_t (*get_time_ms)(void); | ||
| 16 | |||
| 17 | void (*log_info)(const char *tag, const char *fmt, ...); | ||
| 18 | void (*log_warn)(const char *tag, const char *fmt, ...); | ||
| 19 | void (*log_error)(const char *tag, const char *fmt, ...); | ||
| 20 | |||
| 21 | bool (*wallet_receive)(const char *token); | ||
| 22 | bool (*wallet_send)(uint64_t amount, char *buf, size_t buf_len); | ||
| 23 | uint64_t (*wallet_balance)(void); | ||
| 24 | |||
| 25 | int (*http_post)(const char *url, const char *headers, | ||
| 26 | const char *body, int body_len, | ||
| 27 | char *resp, int resp_len); | ||
| 28 | |||
| 29 | bool (*create_task)(void (*fn)(void*), void *arg, | ||
| 30 | const char *name, int stack_bytes, int priority); | ||
| 31 | |||
| 32 | int (*socket_udp)(void); | ||
| 33 | int (*socket_tcp)(void); | ||
| 34 | int (*socket_bind)(int fd, uint32_t ip, uint16_t port); | ||
| 35 | int (*socket_listen)(int fd, int backlog); | ||
| 36 | int (*socket_accept)(int fd, uint32_t *client_ip, uint16_t *client_port); | ||
| 37 | int (*socket_recvfrom)(int fd, void *buf, int len, | ||
| 38 | uint32_t *src_ip, uint16_t *src_port); | ||
| 39 | int (*socket_sendto)(int fd, const void *buf, int len, | ||
| 40 | uint32_t dest_ip, uint16_t dest_port); | ||
| 41 | int (*socket_read)(int fd, void *buf, int len); | ||
| 42 | int (*socket_write)(int fd, const void *buf, int len); | ||
| 43 | void (*socket_close)(int fd); | ||
| 44 | void (*socket_set_recv_timeout)(int fd, int ms); | ||
| 45 | |||
| 46 | bool (*get_sta_mac_ip_list)(void *list_out, int max, int *count_out); | ||
| 47 | bool (*set_vendor_ie)(bool enable, const void *ie_data, int ie_len); | ||
| 48 | int (*arp_get_mac)(uint32_t ip, uint8_t *mac_out); | ||
| 49 | void (*napt_enable)(uint32_t ip, bool enable); | ||
| 50 | |||
| 51 | bool (*mining_enabled)(void); | ||
| 52 | const char* (*get_stratum_host)(void); | ||
| 53 | uint16_t (*get_stratum_port)(void); | ||
| 54 | const char* (*get_stratum_user)(void); | ||
| 55 | const char* (*get_stratum_pass)(void); | ||
| 56 | uint16_t (*get_mining_port)(void); | ||
| 57 | uint64_t (*get_hashprice_override)(void); | ||
| 58 | |||
| 59 | void (*fill_random)(void *buf, int len); | ||
| 60 | |||
| 61 | int (*get_accepted_mint_count)(void); | ||
| 62 | const char* (*get_accepted_mint)(int index); | ||
| 63 | bool (*is_mint_reachable)(const char *mint_url); | ||
| 64 | bool (*mac_for_ip)(uint32_t ip, char *mac_out, int mac_out_size); | ||
| 65 | |||
| 66 | } tollgate_platform_t; | ||
| 67 | |||
| 68 | #endif | ||