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
#[cxx::bridge]
pub mod lib {
    #[repr(u32)]
    enum Parity {
        ParityNone,
        ParityOdd,
        ParityEven,
    }

    #[repr(u32)]
    enum StopBit {
        StopBitOne,
        StopBitOneFive,
        StopBitTwo,
    }

    #[derive(Debug, Clone, Default)]
    pub struct FlasherInfoRs {
        name: String,
        chipId: u32,
        extChipId: u32,
        version: String,
        address: u32,
        numPages: u32,
        pageSize: u32,
        totalSize: u32,
        numPlanes: u32,

        security: bool,
        bootFlash: bool,
        bod: bool,
        bor: bool,

        canBootFlash: bool,
        canBod: bool,
        canBor: bool,
        canChipErase: bool,
        canWriteBuffer: bool,
        canChecksumBuffer: bool,

        lockRegions: Vec<bool>,
        uniqueId: Vec<u32>,
    }

    unsafe extern "C++" {
        include!("bossa/src/BossaObserver.h");
        include!("bossa/src/Device.h");
        include!("bossa/src/Flash.h");
        include!("bossa/src/Flasher.h");
        include!("bossa/src/Info.h");
        include!("bossa/src/PortFactory.h");
        include!("bossa/src/Samba.h");
        include!("bossa/src/SerialPort.h");

        /* SerialPort */
        type SerialPort;

        type Parity;
        type StopBit;

        fn open(
            self: Pin<&mut SerialPort>,
            baud: i32,
            data: i32,
            parity: Parity,
            stop: StopBit,
        ) -> bool;
        fn close(self: Pin<&mut SerialPort>);
        fn setDTR(self: Pin<&mut SerialPort>, dtr: bool);
        fn setRTS(self: Pin<&mut SerialPort>, rts: bool);

        /* PortFactory */
        type PortFactory;

        fn new_port_factory() -> UniquePtr<PortFactory>;

        fn default_name(self: Pin<&mut PortFactory>) -> &CxxString;

        fn create_port(
            self: Pin<&mut PortFactory>,
            name: &CxxString,
            is_usb: bool,
        ) -> UniquePtr<SerialPort>;

        /* Device */
        type Device;

        fn new_device(samba: Pin<&mut Samba>) -> UniquePtr<Device>;

        fn create(self: Pin<&mut Device>);
        fn getFlash(self: Pin<&mut Device>) -> &UniquePtr<Flash>;

        /* FlasherObserver */
        type FlasherObserver;

        fn new_bossa_observer() -> UniquePtr<FlasherObserver>;

        /* Flash */
        type Flash;

        /* Flasher */
        type Flasher;

        fn new_flasher(
            samba: Pin<&mut Samba>,
            device: Pin<&mut Device>,
            observer: Pin<&mut FlasherObserver>,
        ) -> UniquePtr<Flasher>;

        fn erase(self: Pin<&mut Flasher>, foffset: u32);
        /// # Safety
        unsafe fn write(self: Pin<&mut Flasher>, filename: *const c_char, foffset: u32);
        /// # Safety
        unsafe fn verify(
            self: Pin<&mut Flasher>,
            filename: *const c_char,
            pageErrors: &mut u32,
            totalErrors: &mut u32,
            foffset: u32,
        ) -> bool;
        /// # Safety
        unsafe fn read(self: Pin<&mut Flasher>, filename: *const c_char, fsize: u32, foffset: u32);
        fn lock(self: Pin<&mut Flasher>, regionArg: &CxxString, enable: bool);
        fn info(self: Pin<&mut Flasher>, info: Pin<&mut FlasherInfo>);

        fn setBod(self: Pin<&mut Flasher>, enable: bool);
        fn setBootFlash(self: Pin<&mut Flasher>, enable: bool);
        fn setBor(self: Pin<&mut Flasher>, enable: bool);
        fn setSecurity(self: Pin<&mut Flasher>);

        fn writeOptions(self: Pin<&mut Flasher>);
        fn reset(self: Pin<&mut Flasher>);

        /* FlasherInfo */
        type FlasherInfo;

        fn print(self: Pin<&mut FlasherInfo>);

        fn new_flasher_info() -> UniquePtr<FlasherInfo>;

        /* FlasherInfoRs */
        fn flasherinfo2flasherinfors(info: &FlasherInfo, rsinfo: &mut FlasherInfoRs);

        /* Samba */
        type Samba;

        pub fn new_samba() -> UniquePtr<Samba>;

        fn connect(self: Pin<&mut Samba>, port: UniquePtr<SerialPort>, bps: i32) -> bool;

        fn setDebug(self: Pin<&mut Samba>, debug: bool);
    }
}

impl lib::FlasherInfoRs {
    /// Print unique id in the same format as bossa does
    pub fn unique_id(&self) -> String {
        let mut id = String::new();
        for i in 0..self.uniqueId.len() {
            id.push_str(&format!("{:08x}", self.uniqueId[i]));
        }
        id
    }
}