Skip to main content

voltage_j1939/
database.rs

1//! J1939 SPN Database
2//!
3//! Complete database of common engine/generator SPNs based on SAE J1939 standard.
4//! Point ID = SPN (globally unique per SAE J1939 standard).
5//!
6//! This database covers the most commonly used PGNs for diesel generators and
7//! industrial engines. Data is automatically decoded when matching PGNs are received.
8
9use std::collections::HashMap;
10
11use crate::types::{SpnDataType, SpnDef};
12
13// ============================================================================
14// SPN Database - Complete definitions for common engine PGNs
15// ============================================================================
16
17/// All SPN definitions in the database.
18pub static SPN_DEFINITIONS: &[SpnDef] = &[
19    // ========================================================================
20    // EEC1 - Electronic Engine Controller 1 (PGN 61444 / 0xF004)
21    // Broadcast rate: 10-100ms (engine dependent)
22    // ========================================================================
23    SpnDef {
24        spn: 899,
25        name: "engine_torque_mode",
26        pgn: 61444,
27        start_byte: 0,
28        start_bit: 0,
29        bit_length: 4,
30        scale: 1.0,
31        offset: 0.0,
32        unit: "",
33        data_type: SpnDataType::Uint8,
34    },
35    SpnDef {
36        spn: 4154,
37        name: "actual_engine_retarder_percent",
38        pgn: 61444,
39        start_byte: 1,
40        start_bit: 0,
41        bit_length: 8,
42        scale: 1.0,
43        offset: -125.0,
44        unit: "%",
45        data_type: SpnDataType::Uint8,
46    },
47    SpnDef {
48        spn: 512,
49        name: "drivers_demand_engine_percent",
50        pgn: 61444,
51        start_byte: 1,
52        start_bit: 0,
53        bit_length: 8,
54        scale: 1.0,
55        offset: -125.0,
56        unit: "%",
57        data_type: SpnDataType::Uint8,
58    },
59    SpnDef {
60        spn: 513,
61        name: "actual_engine_percent_torque",
62        pgn: 61444,
63        start_byte: 2,
64        start_bit: 0,
65        bit_length: 8,
66        scale: 1.0,
67        offset: -125.0,
68        unit: "%",
69        data_type: SpnDataType::Uint8,
70    },
71    SpnDef {
72        spn: 190,
73        name: "engine_speed",
74        pgn: 61444,
75        start_byte: 3,
76        start_bit: 0,
77        bit_length: 16,
78        scale: 0.125,
79        offset: 0.0,
80        unit: "RPM",
81        data_type: SpnDataType::Uint16,
82    },
83    SpnDef {
84        spn: 1483,
85        name: "eec1_source_address",
86        pgn: 61444,
87        start_byte: 5,
88        start_bit: 0,
89        bit_length: 8,
90        scale: 1.0,
91        offset: 0.0,
92        unit: "",
93        data_type: SpnDataType::Uint8,
94    },
95    SpnDef {
96        spn: 1675,
97        name: "engine_starter_mode",
98        pgn: 61444,
99        start_byte: 6,
100        start_bit: 0,
101        bit_length: 4,
102        scale: 1.0,
103        offset: 0.0,
104        unit: "",
105        data_type: SpnDataType::Uint8,
106    },
107    SpnDef {
108        spn: 2432,
109        name: "engine_demand_percent_torque",
110        pgn: 61444,
111        start_byte: 7,
112        start_bit: 0,
113        bit_length: 8,
114        scale: 1.0,
115        offset: -125.0,
116        unit: "%",
117        data_type: SpnDataType::Uint8,
118    },
119    // ========================================================================
120    // EEC2 - Electronic Engine Controller 2 (PGN 61443 / 0xF003)
121    // Broadcast rate: 50ms
122    // ========================================================================
123    SpnDef {
124        spn: 558,
125        name: "accelerator_pedal_1_low_switch",
126        pgn: 61443,
127        start_byte: 0,
128        start_bit: 0,
129        bit_length: 2,
130        scale: 1.0,
131        offset: 0.0,
132        unit: "",
133        data_type: SpnDataType::Uint8,
134    },
135    SpnDef {
136        spn: 559,
137        name: "accelerator_pedal_kickdown",
138        pgn: 61443,
139        start_byte: 0,
140        start_bit: 2,
141        bit_length: 2,
142        scale: 1.0,
143        offset: 0.0,
144        unit: "",
145        data_type: SpnDataType::Uint8,
146    },
147    SpnDef {
148        spn: 1437,
149        name: "road_speed_limit_status",
150        pgn: 61443,
151        start_byte: 0,
152        start_bit: 4,
153        bit_length: 2,
154        scale: 1.0,
155        offset: 0.0,
156        unit: "",
157        data_type: SpnDataType::Uint8,
158    },
159    SpnDef {
160        spn: 2970,
161        name: "accelerator_pedal_2_low_switch",
162        pgn: 61443,
163        start_byte: 0,
164        start_bit: 6,
165        bit_length: 2,
166        scale: 1.0,
167        offset: 0.0,
168        unit: "",
169        data_type: SpnDataType::Uint8,
170    },
171    SpnDef {
172        spn: 91,
173        name: "accelerator_pedal_position_1",
174        pgn: 61443,
175        start_byte: 1,
176        start_bit: 0,
177        bit_length: 8,
178        scale: 0.4,
179        offset: 0.0,
180        unit: "%",
181        data_type: SpnDataType::Uint8,
182    },
183    SpnDef {
184        spn: 92,
185        name: "percent_load_current_speed",
186        pgn: 61443,
187        start_byte: 2,
188        start_bit: 0,
189        bit_length: 8,
190        scale: 1.0,
191        offset: 0.0,
192        unit: "%",
193        data_type: SpnDataType::Uint8,
194    },
195    SpnDef {
196        spn: 974,
197        name: "remote_accelerator_position",
198        pgn: 61443,
199        start_byte: 3,
200        start_bit: 0,
201        bit_length: 8,
202        scale: 0.4,
203        offset: 0.0,
204        unit: "%",
205        data_type: SpnDataType::Uint8,
206    },
207    SpnDef {
208        spn: 29,
209        name: "accelerator_pedal_position_2",
210        pgn: 61443,
211        start_byte: 4,
212        start_bit: 0,
213        bit_length: 8,
214        scale: 0.4,
215        offset: 0.0,
216        unit: "%",
217        data_type: SpnDataType::Uint8,
218    },
219    SpnDef {
220        spn: 2979,
221        name: "vehicle_acceleration_rate_limit",
222        pgn: 61443,
223        start_byte: 5,
224        start_bit: 0,
225        bit_length: 8,
226        scale: 1.0,
227        offset: 0.0,
228        unit: "",
229        data_type: SpnDataType::Uint8,
230    },
231    SpnDef {
232        spn: 5021,
233        name: "momentary_engine_max_power_enable",
234        pgn: 61443,
235        start_byte: 6,
236        start_bit: 0,
237        bit_length: 2,
238        scale: 1.0,
239        offset: 0.0,
240        unit: "",
241        data_type: SpnDataType::Uint8,
242    },
243    // ========================================================================
244    // EEC3 - Electronic Engine Controller 3 (PGN 65247 / 0xFEDF)
245    // Broadcast rate: 250ms
246    // ========================================================================
247    SpnDef {
248        spn: 514,
249        name: "nominal_friction_percent_torque",
250        pgn: 65247,
251        start_byte: 0,
252        start_bit: 0,
253        bit_length: 8,
254        scale: 1.0,
255        offset: -125.0,
256        unit: "%",
257        data_type: SpnDataType::Uint8,
258    },
259    SpnDef {
260        spn: 515,
261        name: "engine_desired_operating_speed",
262        pgn: 65247,
263        start_byte: 1,
264        start_bit: 0,
265        bit_length: 16,
266        scale: 0.125,
267        offset: 0.0,
268        unit: "RPM",
269        data_type: SpnDataType::Uint16,
270    },
271    SpnDef {
272        spn: 519,
273        name: "engine_operating_speed_asymmetry_adjust",
274        pgn: 65247,
275        start_byte: 3,
276        start_bit: 0,
277        bit_length: 8,
278        scale: 1.0,
279        offset: 0.0,
280        unit: "",
281        data_type: SpnDataType::Uint8,
282    },
283    SpnDef {
284        spn: 2978,
285        name: "estimated_engine_parasitic_losses",
286        pgn: 65247,
287        start_byte: 4,
288        start_bit: 0,
289        bit_length: 8,
290        scale: 1.0,
291        offset: -125.0,
292        unit: "%",
293        data_type: SpnDataType::Uint8,
294    },
295    SpnDef {
296        spn: 6595,
297        name: "aftertreatment_1_exhaust_gas_mass_flow",
298        pgn: 65247,
299        start_byte: 5,
300        start_bit: 0,
301        bit_length: 16,
302        scale: 0.2,
303        offset: 0.0,
304        unit: "kg/h",
305        data_type: SpnDataType::Uint16,
306    },
307    // ========================================================================
308    // ET1 - Engine Temperature 1 (PGN 65262 / 0xFEEE)
309    // Broadcast rate: 1000ms
310    // ========================================================================
311    SpnDef {
312        spn: 110,
313        name: "engine_coolant_temperature",
314        pgn: 65262,
315        start_byte: 0,
316        start_bit: 0,
317        bit_length: 8,
318        scale: 1.0,
319        offset: -40.0,
320        unit: "C",
321        data_type: SpnDataType::Uint8,
322    },
323    SpnDef {
324        spn: 174,
325        name: "fuel_temperature",
326        pgn: 65262,
327        start_byte: 1,
328        start_bit: 0,
329        bit_length: 8,
330        scale: 1.0,
331        offset: -40.0,
332        unit: "C",
333        data_type: SpnDataType::Uint8,
334    },
335    SpnDef {
336        spn: 175,
337        name: "engine_oil_temperature_1",
338        pgn: 65262,
339        start_byte: 2,
340        start_bit: 0,
341        bit_length: 16,
342        scale: 0.03125,
343        offset: -273.0,
344        unit: "C",
345        data_type: SpnDataType::Uint16,
346    },
347    SpnDef {
348        spn: 176,
349        name: "turbo_oil_temperature",
350        pgn: 65262,
351        start_byte: 4,
352        start_bit: 0,
353        bit_length: 16,
354        scale: 0.03125,
355        offset: -273.0,
356        unit: "C",
357        data_type: SpnDataType::Uint16,
358    },
359    SpnDef {
360        spn: 52,
361        name: "engine_intercooler_temperature",
362        pgn: 65262,
363        start_byte: 6,
364        start_bit: 0,
365        bit_length: 8,
366        scale: 1.0,
367        offset: -40.0,
368        unit: "C",
369        data_type: SpnDataType::Uint8,
370    },
371    SpnDef {
372        spn: 1134,
373        name: "engine_intercooler_thermostat_opening",
374        pgn: 65262,
375        start_byte: 7,
376        start_bit: 0,
377        bit_length: 8,
378        scale: 0.4,
379        offset: 0.0,
380        unit: "%",
381        data_type: SpnDataType::Uint8,
382    },
383    // ========================================================================
384    // EFL/P1 - Engine Fluid Level/Pressure 1 (PGN 65263 / 0xFEEF)
385    // Broadcast rate: 500ms
386    // ========================================================================
387    SpnDef {
388        spn: 94,
389        name: "fuel_delivery_pressure",
390        pgn: 65263,
391        start_byte: 0,
392        start_bit: 0,
393        bit_length: 8,
394        scale: 4.0,
395        offset: 0.0,
396        unit: "kPa",
397        data_type: SpnDataType::Uint8,
398    },
399    SpnDef {
400        spn: 22,
401        name: "extended_crankcase_blowby_pressure",
402        pgn: 65263,
403        start_byte: 1,
404        start_bit: 0,
405        bit_length: 8,
406        scale: 0.05,
407        offset: 0.0,
408        unit: "kPa",
409        data_type: SpnDataType::Uint8,
410    },
411    SpnDef {
412        spn: 98,
413        name: "engine_oil_level",
414        pgn: 65263,
415        start_byte: 2,
416        start_bit: 0,
417        bit_length: 8,
418        scale: 0.4,
419        offset: 0.0,
420        unit: "%",
421        data_type: SpnDataType::Uint8,
422    },
423    SpnDef {
424        spn: 100,
425        name: "engine_oil_pressure",
426        pgn: 65263,
427        start_byte: 3,
428        start_bit: 0,
429        bit_length: 8,
430        scale: 4.0,
431        offset: 0.0,
432        unit: "kPa",
433        data_type: SpnDataType::Uint8,
434    },
435    SpnDef {
436        spn: 101,
437        name: "crankcase_pressure",
438        pgn: 65263,
439        start_byte: 4,
440        start_bit: 0,
441        bit_length: 16,
442        scale: 0.0078125,
443        offset: -250.0,
444        unit: "kPa",
445        data_type: SpnDataType::Uint16,
446    },
447    SpnDef {
448        spn: 109,
449        name: "coolant_pressure",
450        pgn: 65263,
451        start_byte: 6,
452        start_bit: 0,
453        bit_length: 8,
454        scale: 2.0,
455        offset: 0.0,
456        unit: "kPa",
457        data_type: SpnDataType::Uint8,
458    },
459    SpnDef {
460        spn: 111,
461        name: "coolant_level",
462        pgn: 65263,
463        start_byte: 7,
464        start_bit: 0,
465        bit_length: 8,
466        scale: 0.4,
467        offset: 0.0,
468        unit: "%",
469        data_type: SpnDataType::Uint8,
470    },
471    // ========================================================================
472    // IC1 - Inlet/Exhaust Conditions 1 (PGN 65270 / 0xFEF6)
473    // Broadcast rate: 500ms
474    // ========================================================================
475    SpnDef {
476        spn: 81,
477        name: "particulate_trap_inlet_pressure",
478        pgn: 65270,
479        start_byte: 0,
480        start_bit: 0,
481        bit_length: 8,
482        scale: 0.5,
483        offset: 0.0,
484        unit: "kPa",
485        data_type: SpnDataType::Uint8,
486    },
487    SpnDef {
488        spn: 102,
489        name: "boost_pressure",
490        pgn: 65270,
491        start_byte: 1,
492        start_bit: 0,
493        bit_length: 8,
494        scale: 2.0,
495        offset: 0.0,
496        unit: "kPa",
497        data_type: SpnDataType::Uint8,
498    },
499    SpnDef {
500        spn: 105,
501        name: "intake_manifold_temperature",
502        pgn: 65270,
503        start_byte: 2,
504        start_bit: 0,
505        bit_length: 8,
506        scale: 1.0,
507        offset: -40.0,
508        unit: "C",
509        data_type: SpnDataType::Uint8,
510    },
511    SpnDef {
512        spn: 106,
513        name: "air_inlet_pressure",
514        pgn: 65270,
515        start_byte: 3,
516        start_bit: 0,
517        bit_length: 8,
518        scale: 2.0,
519        offset: 0.0,
520        unit: "kPa",
521        data_type: SpnDataType::Uint8,
522    },
523    SpnDef {
524        spn: 107,
525        name: "air_filter_differential_pressure",
526        pgn: 65270,
527        start_byte: 4,
528        start_bit: 0,
529        bit_length: 8,
530        scale: 0.05,
531        offset: 0.0,
532        unit: "kPa",
533        data_type: SpnDataType::Uint8,
534    },
535    SpnDef {
536        spn: 173,
537        name: "exhaust_gas_temperature",
538        pgn: 65270,
539        start_byte: 5,
540        start_bit: 0,
541        bit_length: 16,
542        scale: 0.03125,
543        offset: -273.0,
544        unit: "C",
545        data_type: SpnDataType::Uint16,
546    },
547    SpnDef {
548        spn: 112,
549        name: "coolant_filter_differential_pressure",
550        pgn: 65270,
551        start_byte: 7,
552        start_bit: 0,
553        bit_length: 8,
554        scale: 0.5,
555        offset: 0.0,
556        unit: "kPa",
557        data_type: SpnDataType::Uint8,
558    },
559    // ========================================================================
560    // VEP1 - Vehicle Electrical Power 1 (PGN 65271 / 0xFEF7)
561    // Broadcast rate: 1000ms
562    // ========================================================================
563    SpnDef {
564        spn: 114,
565        name: "net_battery_current",
566        pgn: 65271,
567        start_byte: 0,
568        start_bit: 0,
569        bit_length: 16,
570        scale: 1.0,
571        offset: -125.0,
572        unit: "A",
573        data_type: SpnDataType::Int16,
574    },
575    SpnDef {
576        spn: 115,
577        name: "alternator_current",
578        pgn: 65271,
579        start_byte: 2,
580        start_bit: 0,
581        bit_length: 16,
582        scale: 1.0,
583        offset: 0.0,
584        unit: "A",
585        data_type: SpnDataType::Uint16,
586    },
587    SpnDef {
588        spn: 168,
589        name: "battery_potential",
590        pgn: 65271,
591        start_byte: 4,
592        start_bit: 0,
593        bit_length: 16,
594        scale: 0.05,
595        offset: 0.0,
596        unit: "V",
597        data_type: SpnDataType::Uint16,
598    },
599    SpnDef {
600        spn: 158,
601        name: "keyswitch_battery_potential",
602        pgn: 65271,
603        start_byte: 6,
604        start_bit: 0,
605        bit_length: 16,
606        scale: 0.05,
607        offset: 0.0,
608        unit: "V",
609        data_type: SpnDataType::Uint16,
610    },
611    // ========================================================================
612    // AMB - Ambient Conditions (PGN 65269 / 0xFEF5)
613    // Broadcast rate: 1000ms
614    // ========================================================================
615    SpnDef {
616        spn: 108,
617        name: "barometric_pressure",
618        pgn: 65269,
619        start_byte: 0,
620        start_bit: 0,
621        bit_length: 8,
622        scale: 0.5,
623        offset: 0.0,
624        unit: "kPa",
625        data_type: SpnDataType::Uint8,
626    },
627    SpnDef {
628        spn: 170,
629        name: "cab_interior_temperature",
630        pgn: 65269,
631        start_byte: 1,
632        start_bit: 0,
633        bit_length: 16,
634        scale: 0.03125,
635        offset: -273.0,
636        unit: "C",
637        data_type: SpnDataType::Uint16,
638    },
639    SpnDef {
640        spn: 171,
641        name: "ambient_air_temperature",
642        pgn: 65269,
643        start_byte: 3,
644        start_bit: 0,
645        bit_length: 16,
646        scale: 0.03125,
647        offset: -273.0,
648        unit: "C",
649        data_type: SpnDataType::Uint16,
650    },
651    SpnDef {
652        spn: 172,
653        name: "air_inlet_temperature",
654        pgn: 65269,
655        start_byte: 5,
656        start_bit: 0,
657        bit_length: 8,
658        scale: 1.0,
659        offset: -40.0,
660        unit: "C",
661        data_type: SpnDataType::Uint8,
662    },
663    SpnDef {
664        spn: 79,
665        name: "road_surface_temperature",
666        pgn: 65269,
667        start_byte: 6,
668        start_bit: 0,
669        bit_length: 16,
670        scale: 0.03125,
671        offset: -273.0,
672        unit: "C",
673        data_type: SpnDataType::Uint16,
674    },
675    // ========================================================================
676    // LFE - Liquid Fuel Economy (PGN 65266 / 0xFEF2)
677    // Broadcast rate: 100ms
678    // ========================================================================
679    SpnDef {
680        spn: 183,
681        name: "fuel_rate",
682        pgn: 65266,
683        start_byte: 0,
684        start_bit: 0,
685        bit_length: 16,
686        scale: 0.05,
687        offset: 0.0,
688        unit: "L/h",
689        data_type: SpnDataType::Uint16,
690    },
691    SpnDef {
692        spn: 184,
693        name: "instantaneous_fuel_economy",
694        pgn: 65266,
695        start_byte: 2,
696        start_bit: 0,
697        bit_length: 16,
698        scale: 0.001953125,
699        offset: 0.0,
700        unit: "km/L",
701        data_type: SpnDataType::Uint16,
702    },
703    SpnDef {
704        spn: 185,
705        name: "average_fuel_economy",
706        pgn: 65266,
707        start_byte: 4,
708        start_bit: 0,
709        bit_length: 16,
710        scale: 0.001953125,
711        offset: 0.0,
712        unit: "km/L",
713        data_type: SpnDataType::Uint16,
714    },
715    SpnDef {
716        spn: 51,
717        name: "throttle_position",
718        pgn: 65266,
719        start_byte: 6,
720        start_bit: 0,
721        bit_length: 8,
722        scale: 0.4,
723        offset: 0.0,
724        unit: "%",
725        data_type: SpnDataType::Uint8,
726    },
727    // ========================================================================
728    // HOURS - Engine Hours, Revolutions (PGN 65253 / 0xFEE5)
729    // Broadcast rate: On request or 1000ms
730    // ========================================================================
731    SpnDef {
732        spn: 247,
733        name: "engine_total_hours_of_operation",
734        pgn: 65253,
735        start_byte: 0,
736        start_bit: 0,
737        bit_length: 32,
738        scale: 0.05,
739        offset: 0.0,
740        unit: "h",
741        data_type: SpnDataType::Uint32,
742    },
743    SpnDef {
744        spn: 249,
745        name: "engine_total_revolutions",
746        pgn: 65253,
747        start_byte: 4,
748        start_bit: 0,
749        bit_length: 32,
750        scale: 1000.0,
751        offset: 0.0,
752        unit: "r",
753        data_type: SpnDataType::Uint32,
754    },
755    // ========================================================================
756    // FC - Fuel Consumption (PGN 65257 / 0xFEE9)
757    // Broadcast rate: 1000ms
758    // ========================================================================
759    SpnDef {
760        spn: 182,
761        name: "engine_trip_fuel",
762        pgn: 65257,
763        start_byte: 0,
764        start_bit: 0,
765        bit_length: 32,
766        scale: 0.5,
767        offset: 0.0,
768        unit: "L",
769        data_type: SpnDataType::Uint32,
770    },
771    SpnDef {
772        spn: 250,
773        name: "engine_total_fuel_used",
774        pgn: 65257,
775        start_byte: 4,
776        start_bit: 0,
777        bit_length: 32,
778        scale: 0.5,
779        offset: 0.0,
780        unit: "L",
781        data_type: SpnDataType::Uint32,
782    },
783    // ========================================================================
784    // VH - Vehicle Hours (PGN 65217 / 0xFEC1)
785    // Broadcast rate: 1000ms
786    // ========================================================================
787    SpnDef {
788        spn: 246,
789        name: "engine_total_idle_hours",
790        pgn: 65217,
791        start_byte: 0,
792        start_bit: 0,
793        bit_length: 32,
794        scale: 0.05,
795        offset: 0.0,
796        unit: "h",
797        data_type: SpnDataType::Uint32,
798    },
799    SpnDef {
800        spn: 248,
801        name: "engine_total_pto_hours",
802        pgn: 65217,
803        start_byte: 4,
804        start_bit: 0,
805        bit_length: 32,
806        scale: 0.05,
807        offset: 0.0,
808        unit: "h",
809        data_type: SpnDataType::Uint32,
810    },
811    // ========================================================================
812    // DD - Distance (PGN 65248 / 0xFEE0)
813    // Broadcast rate: 1000ms
814    // ========================================================================
815    SpnDef {
816        spn: 244,
817        name: "trip_distance",
818        pgn: 65248,
819        start_byte: 0,
820        start_bit: 0,
821        bit_length: 32,
822        scale: 0.125,
823        offset: 0.0,
824        unit: "km",
825        data_type: SpnDataType::Uint32,
826    },
827    SpnDef {
828        spn: 245,
829        name: "total_vehicle_distance",
830        pgn: 65248,
831        start_byte: 4,
832        start_bit: 0,
833        bit_length: 32,
834        scale: 0.125,
835        offset: 0.0,
836        unit: "km",
837        data_type: SpnDataType::Uint32,
838    },
839    // ========================================================================
840    // CCVS - Cruise Control/Vehicle Speed (PGN 65265 / 0xFEF1)
841    // Broadcast rate: 100ms
842    // ========================================================================
843    SpnDef {
844        spn: 69,
845        name: "two_speed_axle_switch",
846        pgn: 65265,
847        start_byte: 0,
848        start_bit: 0,
849        bit_length: 2,
850        scale: 1.0,
851        offset: 0.0,
852        unit: "",
853        data_type: SpnDataType::Uint8,
854    },
855    SpnDef {
856        spn: 70,
857        name: "parking_brake_switch",
858        pgn: 65265,
859        start_byte: 0,
860        start_bit: 2,
861        bit_length: 2,
862        scale: 1.0,
863        offset: 0.0,
864        unit: "",
865        data_type: SpnDataType::Uint8,
866    },
867    SpnDef {
868        spn: 84,
869        name: "wheel_based_vehicle_speed",
870        pgn: 65265,
871        start_byte: 1,
872        start_bit: 0,
873        bit_length: 16,
874        scale: 0.00390625,
875        offset: 0.0,
876        unit: "km/h",
877        data_type: SpnDataType::Uint16,
878    },
879    SpnDef {
880        spn: 595,
881        name: "cruise_control_active",
882        pgn: 65265,
883        start_byte: 3,
884        start_bit: 0,
885        bit_length: 2,
886        scale: 1.0,
887        offset: 0.0,
888        unit: "",
889        data_type: SpnDataType::Uint8,
890    },
891    SpnDef {
892        spn: 596,
893        name: "cruise_control_enable_switch",
894        pgn: 65265,
895        start_byte: 3,
896        start_bit: 2,
897        bit_length: 2,
898        scale: 1.0,
899        offset: 0.0,
900        unit: "",
901        data_type: SpnDataType::Uint8,
902    },
903    SpnDef {
904        spn: 86,
905        name: "cruise_control_set_speed",
906        pgn: 65265,
907        start_byte: 5,
908        start_bit: 0,
909        bit_length: 8,
910        scale: 1.0,
911        offset: 0.0,
912        unit: "km/h",
913        data_type: SpnDataType::Uint8,
914    },
915    SpnDef {
916        spn: 976,
917        name: "pto_state",
918        pgn: 65265,
919        start_byte: 6,
920        start_bit: 0,
921        bit_length: 5,
922        scale: 1.0,
923        offset: 0.0,
924        unit: "",
925        data_type: SpnDataType::Uint8,
926    },
927];
928
929// ============================================================================
930// Database lookup functions
931// ============================================================================
932
933/// Build PGN to SPNs mapping.
934pub fn build_pgn_database() -> HashMap<u32, Vec<&'static SpnDef>> {
935    let mut map: HashMap<u32, Vec<&'static SpnDef>> = HashMap::new();
936    for spn in SPN_DEFINITIONS {
937        map.entry(spn.pgn).or_default().push(spn);
938    }
939    map
940}
941
942/// Build SPN to definition mapping.
943pub fn build_spn_database() -> HashMap<u32, &'static SpnDef> {
944    let mut map = HashMap::new();
945    for spn in SPN_DEFINITIONS {
946        map.insert(spn.spn, spn);
947    }
948    map
949}
950
951/// Get all SPNs for a given PGN.
952///
953/// # Example
954///
955/// ```
956/// use voltage_j1939::database::get_spns_for_pgn;
957///
958/// // EEC1 (PGN 61444) contains engine speed, torque, etc.
959/// if let Some(spns) = get_spns_for_pgn(61444) {
960///     for spn in spns {
961///         println!("SPN {}: {}", spn.spn, spn.name);
962///     }
963/// }
964/// ```
965pub fn get_spns_for_pgn(pgn: u32) -> Option<Vec<&'static SpnDef>> {
966    let result: Vec<_> = SPN_DEFINITIONS.iter().filter(|s| s.pgn == pgn).collect();
967    if result.is_empty() {
968        None
969    } else {
970        Some(result)
971    }
972}
973
974/// Get a specific SPN definition by SPN number.
975///
976/// # Example
977///
978/// ```
979/// use voltage_j1939::database::get_spn_def;
980///
981/// // SPN 190 = Engine Speed
982/// if let Some(spn) = get_spn_def(190) {
983///     println!("Name: {}, Scale: {}", spn.name, spn.scale);
984/// }
985/// ```
986pub fn get_spn_def(spn: u32) -> Option<&'static SpnDef> {
987    SPN_DEFINITIONS.iter().find(|s| s.spn == spn)
988}
989
990/// Get statistics about the database.
991///
992/// Returns (number of unique PGNs, total number of SPNs).
993pub fn database_stats() -> (usize, usize) {
994    let pgn_count = build_pgn_database().len();
995    let spn_count = SPN_DEFINITIONS.len();
996    (pgn_count, spn_count)
997}
998
999/// List all supported PGNs.
1000pub fn list_supported_pgns() -> Vec<u32> {
1001    let mut pgns: Vec<u32> = build_pgn_database().keys().copied().collect();
1002    pgns.sort();
1003    pgns
1004}
1005
1006#[cfg(test)]
1007mod tests {
1008    use super::*;
1009
1010    #[test]
1011    fn test_database_stats() {
1012        let (pgn_count, spn_count) = database_stats();
1013        assert!(pgn_count >= 10, "Should have at least 10 PGNs");
1014        assert!(spn_count >= 50, "Should have at least 50 SPNs");
1015    }
1016
1017    #[test]
1018    fn test_get_spns_for_pgn() {
1019        // EEC1 should have multiple SPNs
1020        let spns = get_spns_for_pgn(61444);
1021        assert!(spns.is_some());
1022        let spns = spns.unwrap();
1023        assert!(spns.len() >= 5, "EEC1 should have at least 5 SPNs");
1024
1025        // Check engine speed is present
1026        assert!(
1027            spns.iter().any(|s| s.spn == 190),
1028            "Should have SPN 190 (engine_speed)"
1029        );
1030    }
1031
1032    #[test]
1033    fn test_get_spn_def() {
1034        // Engine speed
1035        let spn = get_spn_def(190);
1036        assert!(spn.is_some());
1037        let spn = spn.unwrap();
1038        assert_eq!(spn.name, "engine_speed");
1039        assert_eq!(spn.pgn, 61444);
1040        assert_eq!(spn.scale, 0.125);
1041
1042        // Coolant temperature
1043        let spn = get_spn_def(110);
1044        assert!(spn.is_some());
1045        let spn = spn.unwrap();
1046        assert_eq!(spn.name, "engine_coolant_temperature");
1047        assert_eq!(spn.offset, -40.0);
1048    }
1049
1050    #[test]
1051    fn test_list_supported_pgns() {
1052        let pgns = list_supported_pgns();
1053        assert!(!pgns.is_empty());
1054        assert!(pgns.contains(&61444)); // EEC1
1055        assert!(pgns.contains(&65262)); // ET1
1056    }
1057}