#include "project.h" #define BUFFER_SIZE 32 #define TIMEOUT 500 #define SLEEPY 550 #define BACKOFF 200 #define N_CMDS 4 static char modem_buf[BUFFER_SIZE]; static unsigned modem_ptr; static char *modem_cmds[N_CMDS]; static int modem_cmds_ptr; static int timeout; static int sleepy = SLEEPY; int in_call; int dialtone; void modem_send (void) { if (!modem_cmds_ptr) return; if (sleepy >= SLEEPY) { printf ("modem is sleepy\r\n"); usart2_queue ('\r'); sleepy = 0; timeout = BACKOFF; return; } sleepy = 0; printf ("(re)send %s\r\n",modem_cmds[0]); timeout = TIMEOUT; usart2_queue ('\r'); usart2_tx (modem_cmds[0], strlen (modem_cmds[0])); usart2_queue ('\r'); } void modem_cmd(char *buf) { if (modem_cmds_ptr==N_CMDS) return; modem_cmds[modem_cmds_ptr]=buf; modem_cmds_ptr++; if (modem_cmds_ptr==1) modem_send(); } void modem_dial (char *buf) { in_call=1; modem_cmd(buf); } void modem_tone_off() { // modem_cmd ("AT+STTONE=0"); modem_cmd("AT+SIMTONE=1,400,200,0,10"); } void modem_tone(int i) { static char tone[]={'A','T','+','S','T','T','O','N','E','=','1',',','0','0',',','4','5','0','0','0',0}; tone[12]='0'+(i/10); tone[13]='0'+(i%10); modem_cmd(tone); } void modem_tone_nu(void) { modem_cmd("AT+SIMTONE=1,400,200,0,45000"); } void modem_line () { int i; printf ("Modem said: %s\r\n", modem_buf); sleepy = 0; if (!strncmp (modem_buf, "RDY", 4)) { modem_cmd ("ATZ"); modem_cmd ("AT+CALM=1"); modem_cmd ("AT+CLVL=90"); } else if (!strncmp (modem_buf, "RING", 4)) { dialstr_clear (); dialtone = 0; if (!hook) { answer_call (); } else { ringer_ring (4000); } } else if (!strncmp (modem_buf, "OK", 2) || !strncmp (modem_buf, "ERROR", 2)) { if (modem_cmds_ptr) { for (i=1;i= (sizeof (modem_buf) - 1)) return; modem_buf[modem_ptr] = b; modem_ptr++; modem_buf[modem_ptr] = 0; } void answer_call (void) { printf ("Answering call\r\n"); dialtone = 0; modem_cmd ("ATA"); in_call=1; } void terminate_call (void) { printf ("Terminating any call\r\n"); modem_cmd ("ATH0"); in_call=0; } void dialtone_on (void) { dialtone = 1; modem_tone(20); } void dialtone_off (void) { if (!dialtone) return; modem_tone_off(); dialtone = 0; } void modem_tick (void) { if (sleepy < SLEEPY) sleepy++; if (!timeout) return; timeout--; if (timeout) return; modem_send (); } void modem_init (void) { }