summaryrefslogtreecommitdiffstats
path: root/app/modem.c
diff options
context:
space:
mode:
Diffstat (limited to 'app/modem.c')
-rw-r--r--app/modem.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/app/modem.c b/app/modem.c
new file mode 100644
index 0000000..fa6a57a
--- /dev/null
+++ b/app/modem.c
@@ -0,0 +1,99 @@
+#include "project.h"
+
+#define BUFFER_SIZE 32
+
+#define TIMEOUT 500
+#define SLEEPY 550
+#define BACKOFF 200
+
+static char modem_buf[BUFFER_SIZE];
+static unsigned modem_ptr=0;
+static char *modem_cmd;
+static int timeout;
+static int sleepy=SLEEPY;
+
+
+
+
+void modem_send(char *buf)
+{
+modem_cmd=buf;
+
+if (sleepy>=SLEEPY) {
+ printf("modem is sleepy\r\n");
+ usart2_queue('\r');
+ sleepy=0;
+ timeout=BACKOFF;
+ return;
+}
+ sleepy=0;
+
+printf("(re)send\r\n");
+timeout=TIMEOUT;
+usart2_queue('\r');
+usart2_tx(modem_cmd,strlen(modem_cmd));
+usart2_queue('\r');
+}
+
+
+void modem_line()
+{
+printf("Modem said: %s\r\n",modem_buf);
+
+sleepy=0;
+
+if (!strncmp(modem_buf,"RING",4))
+ ring(4000);
+
+if (!strncmp(modem_buf,"OK",2)) {
+ modem_cmd=NULL;
+ timeout=0;
+}
+}
+
+void modem_byte(uint8_t b)
+{
+
+if (b=='\n') return;
+
+if (b=='\r') {
+ if (modem_ptr)
+ modem_line();
+ modem_ptr=0;
+ modem_buf[modem_ptr]=0;
+ return;
+}
+
+if (modem_ptr>=(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");
+ modem_send("ATA");
+}
+
+void terminate_call(void) {
+ printf("Terminating any call\r\n");
+ modem_send("ATH0");
+}
+
+void modem_tick(void) {
+
+if (sleepy<SLEEPY)
+sleepy++;
+
+if (!timeout) return;
+timeout--;
+if (timeout) return;
+
+modem_send(modem_cmd);
+}
+
+void modem_init(void) {
+}