summaryrefslogtreecommitdiffstats
path: root/app/modem.c
blob: 170c7bade93674699bfdcdb10f1109ea5c09c130 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#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<modem_cmds_ptr;++i) {
	modem_cmds[i-1]=modem_cmds[i];
      }
	modem_cmds_ptr--;
	modem_cmds[modem_cmds_ptr]=NULL;

      timeout = 0;

	if (modem_cmds_ptr) modem_send();
    }
  }
  else
   if (!strncmp (modem_buf, "NO CARRIER", 10)) {
        in_call=0;
	if (!hook) 
   	  modem_tone_nu();
   }
}

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");
  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)
{
}