1 /***************************************************************************
2 * copyright : (C) 2002 by Hendrik Sattler *
3 * mail : post@hendrik-sattler.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 ***************************************************************************/
11
12 #include <atcommand.h>
13 #include <helper.h>
14 #include <intincl.h>
15 #include <w32compat.h>
16
17 #include <stdio.h>
18 #include <stdarg.h>
19 #include <string.h>
20 #include <strings.h>
21 #include <errno.h>
22 #include <unistd.h>
23
at_gen_keypad(char * keylist,uint8_t duration,uint8_t pause)24 char* at_gen_keypad (char* keylist, uint8_t duration, uint8_t pause)
25 {
26 char* parmlist;
27 char* current_keys = NULL;
28 char* retval = NULL;
29 unsigned int i = 0;
30
31 if (str_len(keylist) == 0 || duration == 0) return NULL;
32
33 for (; keylist[i] != 0; ++i) {
34 if (keylist[i] == '+') {
35 if (i == 0) {
36 // replace '+' with '0' and
37 // make duration at least 10 to get the '+'
38 current_keys = str_dup("0");
39 if (duration < 10) duration = 10;
40 /* if (pause < 10) pause = 10; */
41 } else {
42 // Since '+' needs special processing (see above)
43 // process all keys just before the '+'.
44 // The '+' will then be processed by the next call
45 // to this function.
46 current_keys = strn_dup(keylist,i);
47 }
48 retval = keylist + strlen(current_keys);
49 break;
50 }
51 }
52 if (current_keys == NULL) current_keys = str_dup(keylist);
53 parmlist = mem_alloc(1+strlen(current_keys)+1
54 +1+numlen(duration)
55 +1+numlen(pause)+1,1);
56 sprintf(parmlist,"\"%s\",%u,%u",current_keys,duration,pause);
57 mem_realloc(current_keys,0);
58 at_command_send(AT_GEN_KEYPAD,parmlist);
59 mem_realloc(parmlist,0);
60
61 // at least the S55 needs some time after an '+'
62 if (keylist[0] == '+') sleep(1);
63 return retval;
64 }
65
at_gen_password(const char * lock,const char * oldpin,const char * newpin)66 char* at_gen_password (const char* lock,
67 const char* oldpin,
68 const char* newpin)
69 {
70 char* parmlist;
71
72 if (str_len(lock) == 0 ||
73 (oldpin == NULL && newpin == NULL))
74 return NULL;
75 parmlist = mem_alloc(1+strlen(lock)+3+str_len(oldpin)+3
76 +str_len(newpin)+1,1);
77 if (oldpin == NULL) {
78 //there is no pin set, yet
79 sprintf(parmlist,"\"%s\",,\"%s\"",lock,newpin);
80 } else {
81 if (newpin == NULL) {
82 //delete the pin
83 sprintf(parmlist,"\"%s\",\"%s\"",lock,oldpin);
84 } else {
85 sprintf(parmlist,"\"%s\",\"%s\",\"%s\"",lock,oldpin,newpin);
86 }
87 }
88 at_command_send(AT_GEN_PASSWORD,parmlist);
89 mem_realloc(parmlist,0);
90 return AT_GEN_PASSWORD;
91 }
92
at_gen_lock(const char * lock,int mode,const char * password,uint8_t class)93 char* at_gen_lock (const char* lock, int mode,
94 const char* password, uint8_t class)
95 {
96 char* parmlist;
97
98 if (str_len(lock) == 0) return NULL;
99
100 parmlist = mem_alloc(1+strlen(lock)+1
101 +1+numlen(mode)
102 +2+str_len(password)+1
103 +1+numlen(class)+1,1);
104 if (class > 0)
105 if (str_len(password))
106 sprintf(parmlist,"\"%s\",%d,\"%s\",%d",lock,mode,password,class);
107 else
108 sprintf(parmlist,"\"%s\",%d,,%d",lock,mode,class);
109 else
110 if (str_len(password))
111 sprintf(parmlist,"\"%s\",%d,\"%s\"",lock,mode,password);
112 else
113 sprintf(parmlist,"\"%s\",%d",lock,mode);
114 at_command_send(AT_GEN_LOCK,parmlist);
115 mem_realloc(parmlist,0);
116 return AT_GEN_LOCK;
117 }
118
at_gen_phonebook_write(unsigned int slot,const char * number,unsigned int numtype,const char * text)119 void at_gen_phonebook_write (unsigned int slot,
120 const char* number,
121 unsigned int numtype,
122 const char* text)
123 {
124 char* parmlist;
125
126 parmlist = mem_alloc(numlen(slot)+1
127 +1+str_len(number)+1
128 +1+numlen(numtype)+1
129 +1+str_len(text)+1
130 +1,
131 1);
132 if (str_len(number) == 0 && str_len(text) == 0) {
133 sprintf(parmlist,"%d",slot);
134 } else {
135 if (number == NULL) {
136 sprintf(parmlist,"%d,\"\",%d,\"%s\"",
137 slot,numtype,text);
138 } else if (text == NULL) {
139 sprintf(parmlist,"%d,\"%s\",%d,\"\"",
140 slot,number,numtype);
141 } else {
142 sprintf(parmlist,"%d,\"%s\",%d,\"%s\"",
143 slot,number,numtype,text);
144 }
145 }
146 at_command_send(AT_GEN_PB_WRITE,parmlist);
147 mem_realloc(parmlist,0);
148 }
149
at_gen_phonebook_read(unsigned int slot_low,unsigned int slot_high)150 char* at_gen_phonebook_read (unsigned int slot_low,
151 unsigned int slot_high)
152 {
153 char* parmlist;
154
155 parmlist = mem_alloc(numlen(slot_low)+1
156 +numlen(slot_high)+1, 1);
157 sprintf(parmlist,"%d,%d",slot_low,slot_high);
158 at_command_send(AT_GEN_PB_READ,parmlist);
159 mem_realloc(parmlist,0);
160 return AT_GEN_PB_READ;
161 }
162
at_gen_phonebook_select(const char * mem)163 char* at_gen_phonebook_select (const char* mem) {
164 char* parmlist;
165
166 if (mem == NULL) return NULL;
167 parmlist = mem_alloc(1+strlen(mem)+2, 1);
168 sprintf(parmlist,"\"%s\"",mem);
169 at_command_send(AT_GEN_PB_SELECT,parmlist);
170 mem_realloc(parmlist,0);
171 return AT_GEN_PB_SELECT;
172 }
173
at_gen_sms_delete(unsigned int slot)174 void at_gen_sms_delete (unsigned int slot) {
175 char* parmlist;
176
177 parmlist = mem_alloc(numlen(slot)+1, 1);
178 sprintf(parmlist,"%d",slot);
179 at_command_send(AT_GEN_SMS_DELETE,parmlist);
180 mem_realloc(parmlist,0);
181 }
182
at_gen_sms_slot_send(unsigned int slot,const char * number)183 void at_gen_sms_slot_send (unsigned int slot,
184 const char* number)
185 {
186 char* parmlist;
187
188 if (number == NULL) return;
189 parmlist = mem_alloc(numlen(slot)
190 +2+strlen(number)+2
191 +numlen(numtype(number))
192 +1,
193 1);
194 if (str_len(number) == 0) {
195 sprintf(parmlist,"%d",slot);
196 } else {
197 sprintf(parmlist,"%d,\"%s\",%d",slot,number,numtype(number));
198 }
199 at_command_send(AT_GEN_SMS_SLOT_SEND,parmlist);
200 mem_realloc(parmlist,0);
201 }
202
at_gen_sms_send(unsigned int len)203 void at_gen_sms_send (unsigned int len) {
204 char* parmlist;
205
206 parmlist = mem_alloc(numlen(len)+1,1);
207 sprintf(parmlist,"%d",len);
208 at_command_send(AT_GEN_SMS_SEND,parmlist);
209 mem_realloc(parmlist,0);
210 }
211
at_gen_sms_store(unsigned int len)212 void at_gen_sms_store (unsigned int len) {
213 char* parmlist;
214
215 parmlist = mem_alloc(numlen(len)+1,1);
216 sprintf(parmlist,"%d",len);
217 at_command_send(AT_GEN_SMS_STORE,parmlist);
218 mem_realloc(parmlist,0);
219 }
220
at_gen_sms_mem(const char * mem1,const char * mem2,const char * mem3)221 void at_gen_sms_mem (const char* mem1,
222 const char* mem2,
223 const char* mem3)
224 {
225 char* parmlist;
226
227 if (mem1 == NULL) return;
228 parmlist = mem_alloc(1+strlen(mem1)+2
229 +1+str_len(mem2)+2
230 +1+str_len(mem3)+1
231 +1,
232 1);
233 if (mem2 == NULL) {
234 sprintf(parmlist,"\"%s\"",mem1);
235 } else if (mem3 == NULL) {
236 sprintf(parmlist,"\"%s\",\"%s\"",mem1,mem2);
237 } else {
238 sprintf(parmlist,"\"%s\",\"%s\",\"%s\"",mem1,mem2,mem3);
239 }
240 at_command_send(AT_GEN_SMS_MEM,parmlist);
241 mem_realloc(parmlist,0);
242 }
243
at_gen_sms_slot_read(unsigned int slot)244 char* at_gen_sms_slot_read (unsigned int slot) {
245 char* parmlist;
246
247 parmlist = mem_alloc(numlen(slot)+1,1);
248 sprintf(parmlist,"%d",slot);
249 at_command_send(AT_GEN_SMS_SLOT_READ,parmlist);
250 mem_realloc(parmlist,0);
251 return AT_GEN_SMS_SLOT_READ;
252 }
253
at_gen_sms_list_read(unsigned int list_id)254 char* at_gen_sms_list_read (unsigned int list_id) {
255 char* parmlist;
256
257 parmlist = mem_alloc(numlen(list_id)+1,1);
258 sprintf(parmlist,"%d",list_id);
259 at_command_send(AT_GEN_SMS_LIST_READ,parmlist);
260 mem_realloc(parmlist,0);
261 return AT_GEN_SMS_LIST_READ;
262 }
263
at_gen_charset(const char * charset)264 void at_gen_charset (const char* charset) {
265 char* parmlist;
266
267 if (charset == NULL) return;
268 parmlist = mem_alloc(1+strlen(charset)+2, 1);
269 sprintf(parmlist,"\"%s\"",charset);
270 at_command_send(AT_GEN_CHARSET,parmlist);
271 mem_realloc(parmlist,0);
272 }
273
at_gen_smsc(const char * number)274 void at_gen_smsc (const char* number) {
275 char* parmlist;
276
277 if (number == NULL) return;
278 parmlist = mem_alloc(1+strlen(number)+2
279 +numlen(numtype(number))+1,
280 1);
281 sprintf(parmlist,"\"%s\",%d",number,numtype(number));
282 at_command_send(AT_GEN_CHARSET,parmlist);
283 mem_realloc(parmlist,0);
284 }
285
at_gen_pin(const char * pin)286 void at_gen_pin (const char* pin) {
287 char* parmlist;
288
289 if (pin == NULL) return;
290 parmlist = mem_alloc(1+strlen(pin)+2,1);
291 sprintf(parmlist,"\"%s\"",pin);
292 at_command_send(AT_GEN_PIN,parmlist);
293 mem_realloc(parmlist,0);
294 }
295
at_gen_sms_phase(int enable)296 void at_gen_sms_phase (int enable) {
297 char* parmlist;
298
299 if (enable) parmlist = "1";
300 else parmlist = "0";
301 at_command_send(AT_GEN_SMS_PHASE,"1");
302 }
303
at_gen_msg_direct(int mode,int mt,int bm,int ds)304 void at_gen_msg_direct (int mode, int mt, int bm, int ds) {
305 char* parmlist;
306
307 if (mode < 0 || mode > 1) return;
308 if (mt < 0 || mt > 3) return;
309 if (bm != 0 && bm != 2 && bm != 3) return;
310 if (ds < 0 || ds > 2) return;
311
312 parmlist = mem_alloc(numlen(mode)+1+numlen(mt)+1+
313 numlen(bm)+1+numlen(ds)+1+2,1);
314 sprintf(parmlist,"%d,%d,%d,%d,1",mode,mt,bm,ds);
315 at_command_send(AT_GEN_MSG_DIRECT,parmlist);
316 }
317