#ifndef F_CPU #define F_CPU 16000000UL #endif #include #include #include #include #include #define EMAIL_CAPACITY 81U static char target_email[EMAIL_CAPACITY]; static char EEMEM stored_email[EMAIL_CAPACITY]; static char line[EMAIL_CAPACITY + 8U]; static uint8_t used; static uint8_t overflow; static void uart_init(void) { UCSR0A = (1U << U2X0); UBRR0H = 0; UBRR0L = 16; /* 16 MHz / (8 * 17) = 117647 baud (+2.12%). */ UCSR0C = (1U << UCSZ01) | (1U << UCSZ00); UCSR0B = (1U << RXEN0) | (1U << TXEN0); } static void uart_puts(const char *text) { while (*text) { while (!(UCSR0A & (1U << UDRE0))) { } UDR0 = (uint8_t)*text++; } } static uint8_t valid_email(const char *text) { uint8_t at = 0; uint8_t length = 0; while (*text) { uint8_t c = (uint8_t)*text++; if (c <= 32U || c >= 127U) { return 0; } if (c == '@') { if (length == 0U || at) { return 0; } at = 1; if (!*text) { return 0; } } ++length; } return at && length < EMAIL_CAPACITY; } static void process_line(void) { line[used] = '\0'; if (overflow) { uart_puts("ERR line too long\r\n"); } else if (strncmp(line, "EMAIL ", 6) == 0 && valid_email(line + 6)) { strcpy(target_email, line + 6); eeprom_update_block(target_email, stored_email, sizeof(target_email)); uart_puts("OK recipient set; press PC1\r\n"); } else if (used) { uart_puts("Use EMAIL user@example.com then press PC1\r\n"); } used = 0; overflow = 0; } int main(void) { DDRC &= (uint8_t)~(1U << PC1); PORTC |= (1U << PC1); eeprom_read_block(target_email, stored_email, sizeof(target_email)); target_email[EMAIL_CAPACITY - 1U] = '\0'; if (!valid_email(target_email)) { target_email[0] = '\0'; } uart_init(); TCCR0A = (1U << WGM01); TCCR0B = (1U << CS01) | (1U << CS00); OCR0A = 249; TIFR0 = (1U << OCF0A); uart_puts("READY: send EMAIL user@example.com, then start smtp-lab\r\n"); uint8_t stable = 1; uint8_t candidate = 1; uint8_t ticks = 0; while (1) { while (UCSR0A & (1U << RXC0)) { uint8_t status = UCSR0A; char c = (char)UDR0; if (status & ((1U << FE0) | (1U << DOR0) | (1U << UPE0))) { overflow = 1; } else if (c == '\n' || c == '\r') { process_line(); } else if (used < sizeof(line) - 1U) { line[used++] = c; } else { overflow = 1; } } if (!(TIFR0 & (1U << OCF0A))) { continue; } TIFR0 = (1U << OCF0A); uint8_t now = (PINC & (1U << PC1)) ? 1U : 0U; if (now != candidate) { candidate = now; ticks = 0; } else if (ticks < 30U) { ++ticks; if (ticks == 30U && stable != candidate) { stable = candidate; if (!stable) { if (target_email[0]) { uart_puts("TARGET_EMAIL:"); uart_puts(target_email); uart_puts("\r\n"); } else { uart_puts("ERR set EMAIL first\r\n"); } } } } } }