-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms.c
315 lines (239 loc) · 6.28 KB
/
sms.c
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "common.h"
#include "modem.h"
#include "sms.h"
#include "task.h"
#include "device.h"
const unsigned char hex_lookup[] = "0123456789ABCDEF";
const unsigned char mask[] = {1,3,7,15,31,63,127};
const unsigned char power[] = {128,64,32,16,8,4,2};
int sms_compress( unsigned char *in, unsigned char *discarded, unsigned char ret[] )
{
int i,ii,iii;
unsigned char encode_c, now_c, next_c;
*discarded = 0;
for( i = ii = iii = 0; ( in[i] != '\0' ) && ( i < SMS_TEXT_LENGTH ); )
{
now_c = in[ i++ ];
next_c = in[ i ];
//Last char?
/*
if( next_c == '\0' )
{
next_c = 0;
}
*/
encode_c = ( now_c >> ii )+ ( mask[ ii ] & next_c )*power[ ii ];
ret[ iii++ ] = hex_lookup[ ( encode_c >> 4 ) & 0x0F ];
ret[ iii++ ] = hex_lookup[ ( encode_c & 0x0F ) ];
if( ii == 6 )
{
ii = 0;
i++;
(*discarded)++;
}
else
{
ii++;
}
}
ret[iii] = '\0';
return i;
}
int sms_decompress( unsigned char *compressed, unsigned char *decompressed )
{
int i,ii,iii;
unsigned char rest_c, ans_c ,dec_c , this_c, next_c;
for( i = ii = iii = rest_c = 0; (this_c = compressed[i++]) != '\0'; )
{
next_c = compressed[i++];
dec_c = 16 * sms_htoi( this_c ) + sms_htoi( next_c );
ans_c = dec_c & mask[6 - ii];
ans_c <<= ii;
decompressed[iii++] = ans_c + rest_c;
rest_c = (dec_c & ~mask[6 - ii]) >> ( 7 - ii );
if( ii == 6)
{
ii = 0;
decompressed[ iii++ ] = rest_c;
rest_c = 0;
}
else
{
ii++;
}
}
decompressed[ iii ] = '\0';
return iii;
}
int sms_atoi( unsigned char *a )
{
int i, n;
n = 0;
for( i=0; ( a[ i ] >= '0' ) && ( a[ i ] <= '9' ); ++i )
{
n = 10*n + ( a[ i ] - '0' );
}
return n;
}
int sms_htoi( unsigned char hex )
{
if( ( hex >= 'A' ) && ( hex <= 'F' ) )
{
return hex - 'A' + 10;
}
else
{
return hex - '0';
}
}
void sms_itoh( int n, unsigned char *ret )
{
ret[ 0 ] = hex_lookup[ ( n >> 4 ) & 0x0F ];
ret[ 1 ] = hex_lookup[ ( n & 0x0F ) ];
ret[ 2 ] = '\0';
}
void sms_get(unsigned char index)
{
unsigned char cmd[16];
sprintf(cmd, "AT+CMGR=%c", index);
modem_sends(cmd);
}
void sms_process()
{
unsigned char index = 0;
unsigned char sender_index = 0;
unsigned char sender_len = 0;
unsigned char sender[16];
unsigned char sms_len = 0;
unsigned char text[141];
// unsigned char buf[80];
sender[0] = 0;
index = sms_htoi(modem_status.buff[0]) << 4;
index += sms_htoi(modem_status.buff[1]);
index += 1; /* length of smsc info */
index *= 2; /* because of hex to text */
index += 2; /* first octet of deliver */
sender_index = index;
sender_len = sms_htoi(modem_status.buff[index]) << 4;
sender_len += sms_htoi(modem_status.buff[index+1]);
index += 2; /* sender len itself */
index += 2; /* type of sender */
modem_str_to_no(&modem_status.buff[index], sender);
if ( !!strcmp(sender, task_sm.lord_no) )
{
task_sm.piezo0_count = 200;
return;
}
if (sender_len % 2 == 0)
{
index += sender_len;
}
else
{
index += sender_len + 1;
}
index += 2; /* TP-PID */
index += 2; /* TP-DCS */
index += 14; /* TP-SCTS */
sms_len = sms_htoi(modem_status.buff[index]) << 4;
sms_len += sms_htoi(modem_status.buff[index]+1);
index += 2; /* text len */
sms_decompress(&modem_status.buff[index], text);
if ( text[0] == '*' )
{
task_change_irled(0);
}
else if ( text[0] == '#' )
{
task_set_pump(1);
}
else if ( text[0] == '?' )
{
sprintf(text, "temp: %.4f\n batt: %.3f\n ldr: %d\n csq: %d\n",
task_sm.temperature, task_sm.bat*(3.3/1024),
task_sm.ldr, modem_status.last_csq);
sms_send(task_sm.lord_no, text);
// sms_send(task_sm.lord_no, "you ask..");
task_sm.piezo0_count = 10;
}
else
{
task_sm.piezo0_count = 100;
}
// sprintf(tmp, "AT:%s", text);
// modem_sends(tmp);
}
void sms_send(unsigned char *no, unsigned char *text)
{
int payload_len = 0;
int len = 0;
unsigned char no_len;
unsigned char payload_len_c[3];
unsigned char jump = 0;
unsigned char pdu_no[16];
unsigned char pdu[256];
unsigned char cmd[16];
unsigned char last_modem_status;
payload_len_c[0] = 0;
payload_len = sms_compress(text, &jump, pdu);
if (payload_len == 0)
{
return;
}
modem_no_to_str(no, pdu_no);
no_len = strlen(pdu_no) / 2;
sms_itoh(payload_len, &payload_len_c[0]);
len = 8 + no_len + payload_len - jump;
/* */
last_modem_status = modem_status.status;
modem_status.status = MS_SMSING;
sprintf(cmd, "AT+CMGS=%d", len);
modem_sends(cmd);
/* */
_delay_ms(15); /* wait prompt, TODO use sm later */
/* */
// modem_sends_np("0011000BA18168784017F60000AA");
modem_sends_np("0011000BA1");
modem_sends_np(pdu_no);
modem_sends_np("0000AA");
modem_sends_np(payload_len_c);
modem_sends_np(pdu);
modem_sendc(26);
modem_status.status = last_modem_status;
}
void modem_str_to_no(const unsigned char *no, unsigned char *ret)
{
unsigned char i = 0;
unsigned char len = strlen(no);
for (i=0; i<len; i+=2)
{
ret[i] = no[i+1];
if (no[i] != 'F')
{
ret[i+1] = no[i];
}
else
{
ret[i+1] = 0;
break;
}
}
}
void modem_no_to_str(const unsigned char *no, unsigned char *ret)
{
ret[0] = 0;
unsigned char len = strlen(no);
unsigned char i;
unsigned char padding = len % 2;
for (i=0; i<len-padding; i+=2)
{
ret[i] = no[i+1];
ret[i+1] = no[i];
}
if (padding)
{
ret[i] = 'F';
ret[i+1] = no[len-1];
ret[i+2] = 0;
}
}