cc_talk_core 0.0.1

A no-std implementation of the CCtalk protocol.
Documentation
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
/// ccTalk Standard Manufacturer Strings as defined in the ccTalk Generic Specification
/// Table 6 - ccTalk Standard Manufacturer Strings
///
/// These manufacturer identifiers are returned by the 'Request manufacturer id' command
/// and can be used to help identify a specific product.
///
/// BNVs (Bill Note Validators) are expected to reply with abbreviated names.
/// Other peripherals may return a full name.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Manufacturer {
    /// Aardvark Embedded Solutions Ltd (AES)
    AardvarkEmbeddedSolutions,

    /// Alberici (ALB)
    Alberici,

    /// AlfaNet informatika d.o.o (ANI)
    AlfaNetInformatika,

    /// AstroSystems Ltd (AST)
    AstroSystems,

    /// Azkoyen (AZK)
    Azkoyen,

    /// Comestero Group (CMG)
    ComesteroGroup,

    /// Crane CashCode Company (CCC)
    CraneCashCodeCompany,

    /// Crane Payment Solutions (CPS)
    CranePaymentSolutions,

    /// Encopim SL (ECP)
    Encopim,

    /// Gaming Technology Distribution (GTD)
    GamingTechnologyDistribution,

    /// Himecs (HIM)
    Himecs,

    /// Industrias Lorenzo (IL)
    IndustriasLorenzo,

    /// Innovative Technology Ltd (ITL)
    InnovativeTechnology,

    /// Intergrated Technology Ltd (INT)
    /// Note: "Intergrated" appears to be a typo in the original specification
    IntegratedTechnology,

    /// International Currency Technologies (ICT)
    InternationalCurrencyTechnologies,

    /// Japan Cash Machine (JCM)
    JapanCashMachine,

    /// Jofemar (JOF)
    Jofemar,

    /// Kuky (KUK)
    Kuky,

    /// Mars Electronics International (MEI)
    MarsElectronicsInternational,

    /// Microsystem Controls Pty. Ltd. (Microcoin) (MSC)
    MicrosystemControls,

    /// Money Controls (International) (MCI)
    MoneyControlsInternational,

    /// National Rejectors Inc (NRI)
    NationalRejectorsInc,

    /// Phoenix Mecano Digital (PMD)
    PhoenixMecanoDigital,

    /// Starpoint Electrics Ltd (SEL)
    StarpointElectrics,

    /// Telequip / Crane (TQP)
    TelequipCrane,

    /// Weavefuture Inc (WFT)
    WeavefutureInc,

    /// WH Münzprüfer (WHM)
    WHMunzprufer,

    /// iNOTEK (INK)
    /// This manufacturer is not in the original specification but is included because I created
    /// the library :)
    INOTEK,
}

impl Manufacturer {
    /// Returns the full company name
    pub const fn full_name(&self) -> &'static str {
        match self {
            Manufacturer::AardvarkEmbeddedSolutions => "Aardvark Embedded Solutions Ltd",
            Manufacturer::Alberici => "Alberici",
            Manufacturer::AlfaNetInformatika => "AlfaNet informatika d.o.o",
            Manufacturer::AstroSystems => "AstroSystems Ltd",
            Manufacturer::Azkoyen => "Azkoyen",
            Manufacturer::ComesteroGroup => "Comestero Group",
            Manufacturer::CraneCashCodeCompany => "Crane CashCode Company",
            Manufacturer::CranePaymentSolutions => "Crane Payment Solutions",
            Manufacturer::Encopim => "Encopim SL",
            Manufacturer::GamingTechnologyDistribution => "Gaming Technology Distribution",
            Manufacturer::Himecs => "Himecs",
            Manufacturer::IndustriasLorenzo => "Industrias Lorenzo",
            Manufacturer::InnovativeTechnology => "Innovative Technology Ltd",
            Manufacturer::IntegratedTechnology => "Intergrated Technology Ltd",
            Manufacturer::InternationalCurrencyTechnologies => {
                "International Currency Technologies"
            }
            Manufacturer::JapanCashMachine => "Japan Cash Machine",
            Manufacturer::Jofemar => "Jofemar",
            Manufacturer::Kuky => "Kuky",
            Manufacturer::MarsElectronicsInternational => "Mars Electronics International",
            Manufacturer::MicrosystemControls => "Microsystem Controls Pty. Ltd.",
            Manufacturer::MoneyControlsInternational => "Money Controls (International)",
            Manufacturer::NationalRejectorsInc => "National Rejectors Inc",
            Manufacturer::PhoenixMecanoDigital => "Phoenix Mecano Digital",
            Manufacturer::StarpointElectrics => "Starpoint Electrics Ltd",
            Manufacturer::TelequipCrane => "Telequip / Crane",
            Manufacturer::WeavefutureInc => "Weavefuture Inc",
            Manufacturer::WHMunzprufer => "WH Münzprüfer",
            Manufacturer::INOTEK => "iNOTEK",
        }
    }

    /// Returns the abbreviated name (typically used by BNVs)
    pub const fn abbreviated_name(&self) -> &'static str {
        match self {
            Manufacturer::AardvarkEmbeddedSolutions => "AES",
            Manufacturer::Alberici => "ALB",
            Manufacturer::AlfaNetInformatika => "ANI",
            Manufacturer::AstroSystems => "AST",
            Manufacturer::Azkoyen => "AZK",
            Manufacturer::ComesteroGroup => "CMG",
            Manufacturer::CraneCashCodeCompany => "CCC",
            Manufacturer::CranePaymentSolutions => "CPS",
            Manufacturer::Encopim => "ECP",
            Manufacturer::GamingTechnologyDistribution => "GTD",
            Manufacturer::Himecs => "HIM",
            Manufacturer::IndustriasLorenzo => "IL",
            Manufacturer::InnovativeTechnology => "ITL",
            Manufacturer::IntegratedTechnology => "INT",
            Manufacturer::InternationalCurrencyTechnologies => "ICT",
            Manufacturer::JapanCashMachine => "JCM",
            Manufacturer::Jofemar => "JOF",
            Manufacturer::Kuky => "KUK",
            Manufacturer::MarsElectronicsInternational => "MEI",
            Manufacturer::MicrosystemControls => "MSC",
            Manufacturer::MoneyControlsInternational => "MCI",
            Manufacturer::NationalRejectorsInc => "NRI",
            Manufacturer::PhoenixMecanoDigital => "PMD",
            Manufacturer::StarpointElectrics => "SEL",
            Manufacturer::TelequipCrane => "TQP",
            Manufacturer::WeavefutureInc => "WFT",
            Manufacturer::WHMunzprufer => "WHM",
            Manufacturer::INOTEK => "INK",
        }
    }

    /// Returns all known manufacturers as a slice
    pub const fn all() -> &'static [Manufacturer] {
        &[
            Manufacturer::AardvarkEmbeddedSolutions,
            Manufacturer::Alberici,
            Manufacturer::AlfaNetInformatika,
            Manufacturer::AstroSystems,
            Manufacturer::Azkoyen,
            Manufacturer::ComesteroGroup,
            Manufacturer::CraneCashCodeCompany,
            Manufacturer::CranePaymentSolutions,
            Manufacturer::Encopim,
            Manufacturer::GamingTechnologyDistribution,
            Manufacturer::Himecs,
            Manufacturer::IndustriasLorenzo,
            Manufacturer::InnovativeTechnology,
            Manufacturer::IntegratedTechnology,
            Manufacturer::InternationalCurrencyTechnologies,
            Manufacturer::JapanCashMachine,
            Manufacturer::Jofemar,
            Manufacturer::Kuky,
            Manufacturer::MarsElectronicsInternational,
            Manufacturer::MicrosystemControls,
            Manufacturer::MoneyControlsInternational,
            Manufacturer::NationalRejectorsInc,
            Manufacturer::PhoenixMecanoDigital,
            Manufacturer::StarpointElectrics,
            Manufacturer::TelequipCrane,
            Manufacturer::WeavefutureInc,
            Manufacturer::WHMunzprufer,
            Manufacturer::INOTEK,
        ]
    }

    /// Attempts to parse a manufacturer from a full name string
    pub fn from_full_name(name: &str) -> Option<Self> {
        Self::all()
            .iter()
            .find(|manufacturer| manufacturer.full_name() == name)
            .copied()
    }

    /// Attempts to parse a manufacturer from an abbreviated name string
    pub fn from_abbreviated_name(name: &str) -> Option<Self> {
        Self::all()
            .iter()
            .find(|manufacturer| manufacturer.abbreviated_name() == name)
            .copied()
    }

    /// Attempts to parse a manufacturer from either full or abbreviated name
    pub fn from_name(name: &str) -> Option<Self> {
        Self::from_full_name(name).or_else(|| Self::from_abbreviated_name(name))
    }
}

impl core::fmt::Display for Manufacturer {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.full_name())
    }
}

/// Represents a manufacturer identifier that could be either a known manufacturer
/// or an unrecognized string from a new/unlisted manufacturer
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ManufacturerIdentifier {
    /// A known, registered manufacturer
    Known(Manufacturer),

    /// An unrecognized manufacturer string
    ///
    /// This allows for new manufacturers that haven't been registered yet
    /// or custom manufacturer strings.
    #[cfg(not(feature = "std"))]
    Unknown(heapless::String<64>), // Using heapless for no_std compatibility

    #[cfg(feature = "std")]
    Unknown(std::string::String), // Using std::String when available
}

impl ManufacturerIdentifier {
    /// Creates a new manufacturer identifier from a string
    ///
    /// First attempts to match against known manufacturers (both full and abbreviated names),
    /// falling back to storing as an unknown manufacturer if no match is found.
    pub fn new(name: &str) -> Self {
        match Manufacturer::from_name(name) {
            Some(manufacturer) => ManufacturerIdentifier::Known(manufacturer),
            None => {
                #[cfg(not(feature = "std"))]
                {
                    match heapless::String::try_from(name) {
                        Ok(unknown) => ManufacturerIdentifier::Unknown(unknown),
                        Err(_) => {
                            // If the string is too long, truncate it
                            let truncated = &name[..name.len().min(64)];
                            let truncated_string = heapless::String::try_from(truncated)
                                .unwrap_or_else(|_| heapless::String::new());
                            ManufacturerIdentifier::Unknown(truncated_string)
                        }
                    }
                }
                #[cfg(feature = "std")]
                {
                    use std::string::ToString;
                    ManufacturerIdentifier::Unknown(name.to_string())
                }
            }
        }
    }

    /// Returns the manufacturer name as a string
    pub fn name(&self) -> &str {
        match self {
            ManufacturerIdentifier::Known(manufacturer) => manufacturer.full_name(),
            ManufacturerIdentifier::Unknown(name) => name.as_str(),
        }
    }

    /// Returns true if this is a known/registered manufacturer
    pub fn is_known(&self) -> bool {
        matches!(self, ManufacturerIdentifier::Known(_))
    }

    /// Returns the known manufacturer if available
    pub fn known_manufacturer(&self) -> Option<Manufacturer> {
        match self {
            ManufacturerIdentifier::Known(manufacturer) => Some(*manufacturer),
            ManufacturerIdentifier::Unknown(_) => None,
        }
    }
}

impl core::fmt::Display for ManufacturerIdentifier {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.name())
    }
}

impl From<Manufacturer> for ManufacturerIdentifier {
    fn from(manufacturer: Manufacturer) -> Self {
        ManufacturerIdentifier::Known(manufacturer)
    }
}

#[cfg(test)]
mod tests {
    use std::string::ToString;

    use super::*;

    #[test]
    fn test_full_names() {
        assert_eq!(
            Manufacturer::CranePaymentSolutions.full_name(),
            "Crane Payment Solutions"
        );
        assert_eq!(
            Manufacturer::InnovativeTechnology.full_name(),
            "Innovative Technology Ltd"
        );
    }

    #[test]
    fn test_abbreviated_names() {
        assert_eq!(
            Manufacturer::CranePaymentSolutions.abbreviated_name(),
            "CPS"
        );
        assert_eq!(Manufacturer::InnovativeTechnology.abbreviated_name(), "ITL");
    }

    #[test]
    fn test_from_full_name() {
        assert_eq!(
            Manufacturer::from_full_name("Crane Payment Solutions"),
            Some(Manufacturer::CranePaymentSolutions)
        );
        assert_eq!(Manufacturer::from_full_name("Unknown Company"), None);
    }

    #[test]
    fn test_from_abbreviated_name() {
        assert_eq!(
            Manufacturer::from_abbreviated_name("CPS"),
            Some(Manufacturer::CranePaymentSolutions)
        );
        assert_eq!(Manufacturer::from_abbreviated_name("UNK"), None);
    }

    #[test]
    fn test_from_name() {
        assert_eq!(
            Manufacturer::from_name("CPS"),
            Some(Manufacturer::CranePaymentSolutions)
        );
        assert_eq!(
            Manufacturer::from_name("Crane Payment Solutions"),
            Some(Manufacturer::CranePaymentSolutions)
        );
        assert_eq!(Manufacturer::from_name("Unknown"), None);
    }

    #[test]
    fn test_manufacturer_identifier_known() {
        let id = ManufacturerIdentifier::new("CPS");
        assert!(id.is_known());
        assert_eq!(
            id.known_manufacturer(),
            Some(Manufacturer::CranePaymentSolutions)
        );
        assert_eq!(id.name(), "Crane Payment Solutions");
    }

    #[test]
    fn test_manufacturer_identifier_unknown() {
        let id = ManufacturerIdentifier::new("Custom Manufacturer");
        assert!(!id.is_known());
        assert_eq!(id.known_manufacturer(), None);
        assert_eq!(id.name(), "Custom Manufacturer");
    }

    #[test]
    fn test_display() {
        assert_eq!(
            Manufacturer::CranePaymentSolutions.to_string(),
            "Crane Payment Solutions"
        );

        let id = ManufacturerIdentifier::new("ITL");
        assert_eq!(id.to_string(), "Innovative Technology Ltd");
    }

    #[test]
    fn test_all_manufacturers_count() {
        assert_eq!(Manufacturer::all().len(), 28);
    }
}