-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Expand file tree
/
Copy pathlib.rs
More file actions
662 lines (552 loc) · 23.6 KB
/
lib.rs
File metadata and controls
662 lines (552 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
//! WiFi-DensePose WASM Edge — Hot-loadable sensing algorithms for ESP32-S3.
//!
//! ADR-040 Tier 3: Compiled to `wasm32-unknown-unknown`, these modules run
//! inside the WASM3 interpreter on the ESP32-S3 after Tier 2 DSP completes.
//!
//! # Host API (imported from "csi" namespace)
//!
//! The ESP32 firmware exposes CSI data through imported functions:
//! - `csi_get_phase(subcarrier) -> f32`
//! - `csi_get_amplitude(subcarrier) -> f32`
//! - `csi_get_variance(subcarrier) -> f32`
//! - `csi_get_bpm_breathing() -> f32`
//! - `csi_get_bpm_heartrate() -> f32`
//! - `csi_get_presence() -> i32`
//! - `csi_get_motion_energy() -> f32`
//! - `csi_get_n_persons() -> i32`
//! - `csi_get_timestamp() -> i32`
//! - `csi_emit_event(event_type: i32, value: f32)`
//! - `csi_log(ptr: i32, len: i32)`
//! - `csi_get_phase_history(buf_ptr: i32, max_len: i32) -> i32`
//!
//! # Module lifecycle (exported to host)
//!
//! - `on_init()` — called once when module is loaded
//! - `on_frame(n_subcarriers: i32)` — called per CSI frame (~20 Hz)
//! - `on_timer()` — called at configurable interval (default 1 s)
//!
//! # Build
//!
//! ```bash
//! cargo build -p wifi-densepose-wasm-edge --target wasm32-unknown-unknown --release
//! ```
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::missing_safety_doc)]
#![cfg_attr(not(target_arch = "wasm32"), allow(dead_code))]
// ── ADR-040 flagship modules ─────────────────────────────────────────────────
pub mod gesture;
pub mod coherence;
pub mod adversarial;
pub mod rvf;
pub mod occupancy;
pub mod vital_trend;
pub mod intrusion;
// ── Category 1: Medical & Health (ADR-041, event IDs 100-199) ───────────────
pub mod med_sleep_apnea;
pub mod med_cardiac_arrhythmia;
pub mod med_respiratory_distress;
pub mod med_gait_analysis;
pub mod med_seizure_detect;
// ── Category 2: Security & Safety (ADR-041, event IDs 200-299) ──────────────
pub mod sec_perimeter_breach;
pub mod sec_weapon_detect;
pub mod sec_tailgating;
pub mod sec_loitering;
pub mod sec_panic_motion;
// ── Category 3: Smart Building (ADR-041, event IDs 300-399) ─────────────────
pub mod bld_hvac_presence;
pub mod bld_lighting_zones;
pub mod bld_elevator_count;
pub mod bld_meeting_room;
pub mod bld_energy_audit;
// ── Category 4: Retail & Hospitality (ADR-041, event IDs 400-499) ───────────
pub mod ret_queue_length;
pub mod ret_dwell_heatmap;
pub mod ret_customer_flow;
pub mod ret_table_turnover;
pub mod ret_shelf_engagement;
// ── Category 5: Industrial & Specialized (ADR-041, event IDs 500-599) ───────
pub mod ind_forklift_proximity;
pub mod ind_confined_space;
pub mod ind_clean_room;
pub mod ind_livestock_monitor;
pub mod ind_structural_vibration;
// ── Shared vendor utilities (ADR-041) ────────────────────────────────────────
pub mod vendor_common;
// ── Vendor-integrated modules (ADR-041 Category 7) ──────────────────────────
//
// 24 modules organised into 7 sub-categories. Each module file lives in
// `src/` and follows the same pattern as the flagship modules: a no_std
// struct with `const fn new()` and a `process_frame`-style entry point.
//
// Signal Intelligence (wdp-sig-*, event IDs 680-727)
pub mod sig_coherence_gate;
pub mod sig_flash_attention;
pub mod sig_temporal_compress;
pub mod sig_sparse_recovery;
pub mod sig_mincut_person_match;
pub mod sig_optimal_transport;
//
// Adaptive Learning (wdp-lrn-*, event IDs 730-748)
pub mod lrn_dtw_gesture_learn;
pub mod lrn_anomaly_attractor;
pub mod lrn_meta_adapt;
pub mod lrn_ewc_lifelong;
//
// Spatial Reasoning (wdp-spt-*, event IDs 760-773)
pub mod spt_pagerank_influence;
pub mod spt_micro_hnsw;
pub mod spt_spiking_tracker;
//
// Temporal Analysis (wdp-tmp-*, event IDs 790-803)
pub mod tmp_pattern_sequence;
pub mod tmp_temporal_logic_guard;
pub mod tmp_goap_autonomy;
//
// AI Security (wdp-ais-*, event IDs 820-828)
pub mod ais_prompt_shield;
pub mod ais_behavioral_profiler;
//
// Quantum-Inspired (wdp-qnt-*, event IDs 850-857)
pub mod qnt_quantum_coherence;
pub mod qnt_interference_search;
//
// Autonomous Systems (wdp-aut-*, event IDs 880-888)
pub mod aut_psycho_symbolic;
pub mod aut_self_healing_mesh;
//
// Exotic / Research (wdp-exo-*, event IDs 600-699)
pub mod exo_time_crystal;
pub mod exo_hyperbolic_space;
// ── Category 6: Exotic & Research (ADR-041, event IDs 600-699) ──────────────
pub mod exo_dream_stage;
pub mod exo_emotion_detect;
pub mod exo_gesture_language;
pub mod exo_music_conductor;
pub mod exo_plant_growth;
pub mod exo_ghost_hunter;
pub mod exo_rain_detect;
pub mod exo_breathing_sync;
pub mod exo_happiness_score;
// ── Host API FFI bindings ────────────────────────────────────────────────────
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "csi")]
extern "C" {
#[link_name = "csi_get_phase"]
pub fn host_get_phase(subcarrier: i32) -> f32;
#[link_name = "csi_get_amplitude"]
pub fn host_get_amplitude(subcarrier: i32) -> f32;
#[link_name = "csi_get_variance"]
pub fn host_get_variance(subcarrier: i32) -> f32;
#[link_name = "csi_get_bpm_breathing"]
pub fn host_get_bpm_breathing() -> f32;
#[link_name = "csi_get_bpm_heartrate"]
pub fn host_get_bpm_heartrate() -> f32;
#[link_name = "csi_get_presence"]
pub fn host_get_presence() -> i32;
#[link_name = "csi_get_motion_energy"]
pub fn host_get_motion_energy() -> f32;
#[link_name = "csi_get_n_persons"]
pub fn host_get_n_persons() -> i32;
#[link_name = "csi_get_timestamp"]
pub fn host_get_timestamp() -> i32;
#[link_name = "csi_emit_event"]
pub fn host_emit_event(event_type: i32, value: f32);
#[link_name = "csi_log"]
pub fn host_log(ptr: i32, len: i32);
#[link_name = "csi_get_phase_history"]
pub fn host_get_phase_history(buf_ptr: i32, max_len: i32) -> i32;
}
// ── Convenience wrappers ─────────────────────────────────────────────────────
/// Event type constants emitted via `csi_emit_event`.
///
/// Registry (ADR-041):
/// 0-99: Core (gesture, coherence, anomaly, custom)
/// 100-199: Medical (vital trends, apnea, brady/tachycardia)
/// 200-299: Security (intrusion, tamper, perimeter)
/// 300-399: Smart Building (occupancy zones, HVAC, lighting)
/// 400-499: Retail (foot traffic, dwell time)
/// 500-599: Industrial (vibration, proximity)
/// 600-699: Exotic (dream stage 600-603, emotion 610-613, gesture lang 620-623,
/// music conductor 630-634, time crystals 680-682, hyperbolic 685-687)
/// 700-729: Vendor Signal Intelligence
/// 730-759: Vendor Adaptive Learning
/// 760-789: Vendor Spatial Reasoning
/// 790-819: Vendor Temporal Analysis
/// 820-849: Vendor AI Security
/// 850-879: Vendor Quantum-Inspired
/// 880-899: Vendor Autonomous Systems
pub mod event_types {
// ── Core (0-99) ──────────────────────────────────────────────────────
pub const GESTURE_DETECTED: i32 = 1;
pub const COHERENCE_SCORE: i32 = 2;
pub const ANOMALY_DETECTED: i32 = 3;
pub const CUSTOM_METRIC: i32 = 10;
// ── Medical (100-199) ────────────────────────────────────────────────
pub const VITAL_TREND: i32 = 100;
pub const BRADYPNEA: i32 = 101;
pub const TACHYPNEA: i32 = 102;
pub const BRADYCARDIA: i32 = 103;
pub const TACHYCARDIA: i32 = 104;
pub const APNEA: i32 = 105;
// ── Security (200-299) ───────────────────────────────────────────────
pub const INTRUSION_ALERT: i32 = 200;
pub const INTRUSION_ZONE: i32 = 201;
// sec_perimeter_breach (210-213)
pub const PERIMETER_BREACH: i32 = 210;
pub const APPROACH_DETECTED: i32 = 211;
pub const DEPARTURE_DETECTED: i32 = 212;
pub const SEC_ZONE_TRANSITION: i32 = 213;
// sec_weapon_detect (220-222)
pub const METAL_ANOMALY: i32 = 220;
pub const WEAPON_ALERT: i32 = 221;
pub const CALIBRATION_NEEDED: i32 = 222;
// sec_tailgating (230-232)
pub const TAILGATE_DETECTED: i32 = 230;
pub const SINGLE_PASSAGE: i32 = 231;
pub const MULTI_PASSAGE: i32 = 232;
// sec_loitering (240-242)
pub const LOITERING_START: i32 = 240;
pub const LOITERING_ONGOING: i32 = 241;
pub const LOITERING_END: i32 = 242;
// sec_panic_motion (250-252)
pub const PANIC_DETECTED: i32 = 250;
pub const STRUGGLE_PATTERN: i32 = 251;
pub const FLEEING_DETECTED: i32 = 252;
// ── Smart Building (300-399) ─────────────────────────────────────────
pub const ZONE_OCCUPIED: i32 = 300;
pub const ZONE_COUNT: i32 = 301;
pub const ZONE_TRANSITION: i32 = 302;
// bld_hvac_presence (310-312)
pub const HVAC_OCCUPIED: i32 = 310;
pub const ACTIVITY_LEVEL: i32 = 311;
pub const DEPARTURE_COUNTDOWN: i32 = 312;
// bld_lighting_zones (320-322)
pub const LIGHT_ON: i32 = 320;
pub const LIGHT_DIM: i32 = 321;
pub const LIGHT_OFF: i32 = 322;
// bld_elevator_count (330-333)
pub const ELEVATOR_COUNT: i32 = 330;
pub const DOOR_OPEN: i32 = 331;
pub const DOOR_CLOSE: i32 = 332;
pub const OVERLOAD_WARNING: i32 = 333;
// bld_meeting_room (340-343)
pub const MEETING_START: i32 = 340;
pub const MEETING_END: i32 = 341;
pub const PEAK_HEADCOUNT: i32 = 342;
pub const ROOM_AVAILABLE: i32 = 343;
// bld_energy_audit (350-352)
pub const SCHEDULE_SUMMARY: i32 = 350;
pub const AFTER_HOURS_ALERT: i32 = 351;
pub const UTILIZATION_RATE: i32 = 352;
// ── Retail & Hospitality (400-499) ─────────────────────────────────────
// ret_queue_length (400-403)
pub const QUEUE_LENGTH: i32 = 400;
pub const WAIT_TIME_ESTIMATE: i32 = 401;
pub const SERVICE_RATE: i32 = 402;
pub const QUEUE_ALERT: i32 = 403;
// ret_dwell_heatmap (410-413)
pub const DWELL_ZONE_UPDATE: i32 = 410;
pub const HOT_ZONE: i32 = 411;
pub const COLD_ZONE: i32 = 412;
pub const SESSION_SUMMARY: i32 = 413;
// ret_customer_flow (420-423)
pub const INGRESS: i32 = 420;
pub const EGRESS: i32 = 421;
pub const NET_OCCUPANCY: i32 = 422;
pub const HOURLY_TRAFFIC: i32 = 423;
// ret_table_turnover (430-433)
pub const TABLE_SEATED: i32 = 430;
pub const TABLE_VACATED: i32 = 431;
pub const TABLE_AVAILABLE: i32 = 432;
pub const TURNOVER_RATE: i32 = 433;
// ret_shelf_engagement (440-443)
pub const SHELF_BROWSE: i32 = 440;
pub const SHELF_CONSIDER: i32 = 441;
pub const SHELF_ENGAGE: i32 = 442;
pub const REACH_DETECTED: i32 = 443;
// ── Industrial & Specialized (500-599) ────────────────────────────────
// ind_forklift_proximity (500-502)
pub const PROXIMITY_WARNING: i32 = 500;
pub const VEHICLE_DETECTED: i32 = 501;
pub const HUMAN_NEAR_VEHICLE: i32 = 502;
// ind_confined_space (510-514)
pub const WORKER_ENTRY: i32 = 510;
pub const WORKER_EXIT: i32 = 511;
pub const BREATHING_OK: i32 = 512;
pub const EXTRACTION_ALERT: i32 = 513;
pub const IMMOBILE_ALERT: i32 = 514;
// ind_clean_room (520-523)
pub const OCCUPANCY_COUNT: i32 = 520;
pub const OCCUPANCY_VIOLATION: i32 = 521;
pub const TURBULENT_MOTION: i32 = 522;
pub const COMPLIANCE_REPORT: i32 = 523;
// ind_livestock_monitor (530-533)
pub const ANIMAL_PRESENT: i32 = 530;
pub const ABNORMAL_STILLNESS: i32 = 531;
pub const LABORED_BREATHING: i32 = 532;
pub const ESCAPE_ALERT: i32 = 533;
// ind_structural_vibration (540-543)
pub const SEISMIC_DETECTED: i32 = 540;
pub const MECHANICAL_RESONANCE: i32 = 541;
pub const STRUCTURAL_DRIFT: i32 = 542;
pub const VIBRATION_SPECTRUM: i32 = 543;
// ── Exotic / Research (600-699) ──────────────────────────────────────
// exo_dream_stage (600-603)
pub const SLEEP_STAGE: i32 = 600;
pub const SLEEP_QUALITY: i32 = 601;
pub const REM_EPISODE: i32 = 602;
pub const DEEP_SLEEP_RATIO: i32 = 603;
// exo_emotion_detect (610-613)
pub const AROUSAL_LEVEL: i32 = 610;
pub const STRESS_INDEX: i32 = 611;
pub const CALM_DETECTED: i32 = 612;
pub const AGITATION_DETECTED: i32 = 613;
// exo_gesture_language (620-623)
pub const LETTER_RECOGNIZED: i32 = 620;
pub const LETTER_CONFIDENCE: i32 = 621;
pub const WORD_BOUNDARY: i32 = 622;
pub const GESTURE_REJECTED: i32 = 623;
// exo_music_conductor (630-634)
pub const CONDUCTOR_BPM: i32 = 630;
pub const BEAT_POSITION: i32 = 631;
pub const DYNAMIC_LEVEL: i32 = 632;
pub const GESTURE_CUTOFF: i32 = 633;
pub const GESTURE_FERMATA: i32 = 634;
// exo_plant_growth (640-643)
pub const GROWTH_RATE: i32 = 640;
pub const CIRCADIAN_PHASE: i32 = 641;
pub const WILT_DETECTED: i32 = 642;
pub const WATERING_EVENT: i32 = 643;
// exo_ghost_hunter (650-653)
pub const EXO_ANOMALY_DETECTED: i32 = 650;
pub const EXO_ANOMALY_CLASS: i32 = 651;
pub const HIDDEN_PRESENCE: i32 = 652;
pub const ENVIRONMENTAL_DRIFT: i32 = 653;
// exo_happiness_score (690-694)
pub const HAPPINESS_SCORE: i32 = 690;
pub const GAIT_ENERGY: i32 = 691;
pub const AFFECT_VALENCE: i32 = 692;
pub const SOCIAL_ENERGY: i32 = 693;
pub const TRANSIT_DIRECTION: i32 = 694;
// exo_rain_detect (660-662)
pub const RAIN_ONSET: i32 = 660;
pub const RAIN_INTENSITY: i32 = 661;
pub const RAIN_CESSATION: i32 = 662;
// exo_breathing_sync (670-673)
pub const SYNC_DETECTED: i32 = 670;
pub const SYNC_PAIR_COUNT: i32 = 671;
pub const GROUP_COHERENCE: i32 = 672;
pub const SYNC_LOST: i32 = 673;
// exo_time_crystal (680-682)
pub const CRYSTAL_DETECTED: i32 = 680;
pub const CRYSTAL_STABILITY: i32 = 681;
pub const COORDINATION_INDEX: i32 = 682;
// exo_hyperbolic_space (685-687)
pub const HIERARCHY_LEVEL: i32 = 685;
pub const HYPERBOLIC_RADIUS: i32 = 686;
pub const LOCATION_LABEL: i32 = 687;
// ── Signal Intelligence (700-729) ────────────────────────────────────
// sig_flash_attention (700-702)
pub const ATTENTION_PEAK_SC: i32 = 700;
pub const ATTENTION_SPREAD: i32 = 701;
pub const SPATIAL_FOCUS_ZONE: i32 = 702;
// sig_temporal_compress (705-707)
pub const COMPRESSION_RATIO: i32 = 705;
pub const TIER_TRANSITION: i32 = 706;
pub const HISTORY_DEPTH_HOURS: i32 = 707;
// sig_coherence_gate (710-712)
pub const GATE_DECISION: i32 = 710;
pub const SIG_COHERENCE_SCORE: i32 = 711;
pub const RECALIBRATE_NEEDED: i32 = 712;
// sig_sparse_recovery (715-717)
pub const RECOVERY_COMPLETE: i32 = 715;
pub const RECOVERY_ERROR: i32 = 716;
pub const DROPOUT_RATE: i32 = 717;
// sig_mincut_person_match (720-722)
pub const PERSON_ID_ASSIGNED: i32 = 720;
pub const PERSON_ID_SWAP: i32 = 721;
pub const MATCH_CONFIDENCE: i32 = 722;
// sig_optimal_transport (725-727)
pub const WASSERSTEIN_DISTANCE: i32 = 725;
pub const DISTRIBUTION_SHIFT: i32 = 726;
pub const SUBTLE_MOTION: i32 = 727;
// ── Adaptive Learning (730-759) ──────────────────────────────────────
// lrn_dtw_gesture_learn (730-733)
pub const GESTURE_LEARNED: i32 = 730;
pub const GESTURE_MATCHED: i32 = 731;
pub const LRN_MATCH_DISTANCE: i32 = 732;
pub const TEMPLATE_COUNT: i32 = 733;
// lrn_anomaly_attractor (735-738)
pub const ATTRACTOR_TYPE: i32 = 735;
pub const LYAPUNOV_EXPONENT: i32 = 736;
pub const BASIN_DEPARTURE: i32 = 737;
pub const LEARNING_COMPLETE: i32 = 738;
// lrn_meta_adapt (740-743)
pub const PARAM_ADJUSTED: i32 = 740;
pub const ADAPTATION_SCORE: i32 = 741;
pub const ROLLBACK_TRIGGERED: i32 = 742;
pub const META_LEVEL: i32 = 743;
// lrn_ewc_lifelong (745-748)
pub const KNOWLEDGE_RETAINED: i32 = 745;
pub const NEW_TASK_LEARNED: i32 = 746;
pub const FISHER_UPDATE: i32 = 747;
pub const FORGETTING_RISK: i32 = 748;
// ── Spatial Reasoning (760-789) ──────────────────────────────────────
// spt_pagerank_influence (760-762)
pub const DOMINANT_PERSON: i32 = 760;
pub const INFLUENCE_SCORE: i32 = 761;
pub const INFLUENCE_CHANGE: i32 = 762;
// spt_micro_hnsw (765-768)
pub const NEAREST_MATCH_ID: i32 = 765;
pub const HNSW_MATCH_DISTANCE: i32 = 766;
pub const CLASSIFICATION: i32 = 767;
pub const LIBRARY_SIZE: i32 = 768;
// spt_spiking_tracker (770-773)
pub const TRACK_UPDATE: i32 = 770;
pub const TRACK_VELOCITY: i32 = 771;
pub const SPIKE_RATE: i32 = 772;
pub const TRACK_LOST: i32 = 773;
// ── Temporal Analysis (790-819) ──────────────────────────────────────
// tmp_pattern_sequence (790-793)
pub const PATTERN_DETECTED: i32 = 790;
pub const PATTERN_CONFIDENCE: i32 = 791;
pub const ROUTINE_DEVIATION: i32 = 792;
pub const PREDICTION_NEXT: i32 = 793;
// tmp_temporal_logic_guard (795-797)
pub const LTL_VIOLATION: i32 = 795;
pub const LTL_SATISFACTION: i32 = 796;
pub const COUNTEREXAMPLE: i32 = 797;
// tmp_goap_autonomy (800-803)
pub const GOAL_SELECTED: i32 = 800;
pub const MODULE_ACTIVATED: i32 = 801;
pub const MODULE_DEACTIVATED: i32 = 802;
pub const PLAN_COST: i32 = 803;
// ── AI Security (820-849) ────────────────────────────────────────────
// ais_prompt_shield (820-823)
pub const REPLAY_ATTACK: i32 = 820;
pub const INJECTION_DETECTED: i32 = 821;
pub const JAMMING_DETECTED: i32 = 822;
pub const SIGNAL_INTEGRITY: i32 = 823;
// ais_behavioral_profiler (825-828)
pub const BEHAVIOR_ANOMALY: i32 = 825;
pub const PROFILE_DEVIATION: i32 = 826;
pub const NOVEL_PATTERN: i32 = 827;
pub const PROFILE_MATURITY: i32 = 828;
// ── Quantum-Inspired (850-879) ───────────────────────────────────────
// qnt_quantum_coherence (850-852)
pub const ENTANGLEMENT_ENTROPY: i32 = 850;
pub const DECOHERENCE_EVENT: i32 = 851;
pub const BLOCH_DRIFT: i32 = 852;
// qnt_interference_search (855-857)
pub const HYPOTHESIS_WINNER: i32 = 855;
pub const HYPOTHESIS_AMPLITUDE: i32 = 856;
pub const SEARCH_ITERATIONS: i32 = 857;
// ── Autonomous Systems (880-899) ─────────────────────────────────────
// aut_psycho_symbolic (880-883)
pub const INFERENCE_RESULT: i32 = 880;
pub const INFERENCE_CONFIDENCE: i32 = 881;
pub const RULE_FIRED: i32 = 882;
pub const CONTRADICTION: i32 = 883;
// aut_self_healing_mesh (885-888)
pub const NODE_DEGRADED: i32 = 885;
pub const MESH_RECONFIGURE: i32 = 886;
pub const COVERAGE_SCORE: i32 = 887;
pub const HEALING_COMPLETE: i32 = 888;
}
/// Log a message string to the ESP32 console (via host_log import).
#[cfg(target_arch = "wasm32")]
pub fn log_msg(msg: &str) {
unsafe {
host_log(msg.as_ptr() as i32, msg.len() as i32);
}
}
/// Emit a typed event to the host output packet.
#[cfg(target_arch = "wasm32")]
pub fn emit(event_type: i32, value: f32) {
unsafe {
host_emit_event(event_type, value);
}
}
// ── Panic handler (required for no_std WASM) ─────────────────────────────────
#[cfg(target_arch = "wasm32")]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
// ── Default module entry points ──────────────────────────────────────────────
//
// Individual modules (gesture, coherence, adversarial) can define their own
// on_init/on_frame/on_timer. This default implementation demonstrates the
// combined pipeline: gesture detection + coherence monitoring + anomaly check.
//
// Gated behind the "default-pipeline" feature so that standalone module
// binaries (ghost_hunter, etc.) can define their own on_frame without
// symbol collisions.
#[cfg(all(target_arch = "wasm32", feature = "default-pipeline"))]
static mut STATE: CombinedState = CombinedState::new();
#[cfg(feature = "default-pipeline")]
struct CombinedState {
gesture: gesture::GestureDetector,
coherence: coherence::CoherenceMonitor,
adversarial: adversarial::AnomalyDetector,
frame_count: u32,
}
#[cfg(feature = "default-pipeline")]
impl CombinedState {
const fn new() -> Self {
Self {
gesture: gesture::GestureDetector::new(),
coherence: coherence::CoherenceMonitor::new(),
adversarial: adversarial::AnomalyDetector::new(),
frame_count: 0,
}
}
}
#[cfg(all(target_arch = "wasm32", feature = "default-pipeline"))]
#[no_mangle]
pub extern "C" fn on_init() {
log_msg("wasm-edge: combined pipeline init");
}
#[cfg(all(target_arch = "wasm32", feature = "default-pipeline"))]
#[no_mangle]
pub extern "C" fn on_frame(n_subcarriers: i32) {
// M-01 fix: treat negative host values as 0 instead of wrapping to usize::MAX.
let n_sc = if n_subcarriers < 0 { 0 } else { n_subcarriers as usize };
let state = unsafe { &mut *core::ptr::addr_of_mut!(STATE) };
state.frame_count += 1;
// Collect phase/amplitude for top subcarriers (max 32).
let max_sc = if n_sc > 32 { 32 } else { n_sc };
let mut phases = [0.0f32; 32];
let mut amps = [0.0f32; 32];
for i in 0..max_sc {
unsafe {
phases[i] = host_get_phase(i as i32);
amps[i] = host_get_amplitude(i as i32);
}
}
// 1. Gesture detection (DTW template matching).
if let Some(gesture_id) = state.gesture.process_frame(&phases[..max_sc]) {
emit(event_types::GESTURE_DETECTED, gesture_id as f32);
}
// 2. Coherence monitoring (phase phasor).
let coh_score = state.coherence.process_frame(&phases[..max_sc]);
if state.frame_count % 20 == 0 {
emit(event_types::COHERENCE_SCORE, coh_score);
}
// 3. Anomaly detection (signal consistency check).
if state.adversarial.process_frame(&phases[..max_sc], &s[..max_sc]) {
emit(event_types::ANOMALY_DETECTED, 1.0);
}
}
#[cfg(all(target_arch = "wasm32", feature = "default-pipeline"))]
#[no_mangle]
pub extern "C" fn on_timer() {
// Periodic summary.
let state = unsafe { &*core::ptr::addr_of!(STATE) };
let motion = unsafe { host_get_motion_energy() };
emit(event_types::CUSTOM_METRIC, motion);
if state.frame_count % 100 == 0 {
log_msg("wasm-edge: heartbeat");
}
}