Project

General

Profile

Revision 277

Added by Kevin Woo almost 14 years ago

New buffer size, prevents buffer overflows in cardreader

View differences:

trunk/cardbox/cardreader.h
11 11
#define CR_ERR_NO_START     -2
12 12
#define CR_ERR_NO_STOP      -3
13 13

  
14
#define CR_BUF_SIZE 800
15

  
14 16
typedef enum {
15 17
    CR_NONE,
16 18
    CR_GOOD,
......
19 21

  
20 22
extern volatile cr_flag_t cr_flag;
21 23
extern volatile uint8_t read_card;
22
extern volatile uint8_t cr_buf[512];
23
extern volatile uint16_t cr_buf_idx;
24
//extern volatile uint8_t cr_buf[512];
25
//extern volatile uint16_t cr_buf_idx;
24 26

  
25 27
void card_reader_setup(void);
26 28
int8_t  parse_card(uint8_t *buf, uint8_t *buflen);
trunk/cardbox/main.c
154 154

  
155 155
int main(void) {
156 156
    uint8_t mbuf[PROGD_PACKET_SIZE];
157
    uint8_t cbuf[100];
157
    uint8_t cbuf[20];
158 158
    uint8_t clen;
159
    int8_t cret;
159 160
    uint8_t resp;
160 161
    uint8_t  c;
161 162
    uint8_t len;
......
177 178
            read_card = 1;
178 179
            if (cr_flag != CR_NONE) {
179 180
                read_card = 0;
180
                if (parse_card(cbuf, &clen) < 0) {
181
                if ((cret = parse_card(cbuf, &clen)) < 0) {
182
                    switch (cret) {
183
                        case CR_ERR_BAD_PARITY:
184
                            toggle_led(LED_GREEN, ON);
185
                            break;
186
                        case CR_ERR_NO_START:
187
                            toggle_led(LED_YELLOW, ON);
188
                            break;
189
                        case CR_ERR_NO_STOP:
190
                            toggle_led(LED_RED, ON);
191
                            break;
192
                    }
181 193
                    rs485_send_byte('F');
182 194
                } else {
183 195
                    for (c = 0; c < clen; c++) {
trunk/cardbox/cardreader.c
19 19
// This is only written in the PCINT2 intterupt or in the main loop
20 20
// but not concurrently. read_card acts as an implicit lock
21 21
volatile cr_flag_t cr_flag;
22
volatile uint8_t cr_buf[512];
22
volatile uint8_t cr_buf[CR_BUF_SIZE];
23 23
volatile uint16_t cr_buf_idx;
24 24
volatile uint8_t read_card;
25 25

  
......
53 53
}
54 54

  
55 55
int8_t parse_card(uint8_t *buf, uint8_t *buflen) {
56
    int32_t i = cr_buf_idx - 1;
56
    int16_t i = cr_buf_idx - 1;
57 57
    uint8_t j = 0;
58 58
    uint8_t start_sentinal = 0;
59 59
    uint8_t byte_idx = 0;
......
142 142
    }
143 143

  
144 144
    // Read data in on the downtick of the clock
145
    if (!(PIND & CR_CLK)) {
145
    if ((!(PIND & CR_CLK))) {
146
        if (cr_buf_idx < CR_BUF_SIZE) {
146 147
        // Read data from card reader and flip since ours inverts
147 148
        cr_buf[cr_buf_idx] = !(PIND & CR_DATA);
148 149
        cr_buf_idx++;
150
        } else {
151
            toggle_led(LED_RED, ON);
152
        }
149 153
    }
150 154
}

Also available in: Unified diff