summaryrefslogtreecommitdiffstats
path: root/app/modem.c
blob: b53176181fc1a7a2ea9799be96fb87ddc7a117b2 (plain)
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
#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)
{
}