-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_sms_send.c
78 lines (66 loc) · 1.78 KB
/
client_sms_send.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
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<limits.h>
#include"client_protocol.h"
int main(int argc, char **argv) {
char *buf = calloc(PIPE_BUF, sizeof(char));
char *cmdbuf;
char *msgbuf;
char ctrlz[4];
int in_fd, out_fd;
if(argc != 3) {
printf("Usage: %s phone-number \"message\"\n", argv[0]);
return 1;
}
/* Construct AT command to initiate and send sms */
cmdbuf = calloc(9+strlen(argv[1])+3+1, sizeof(char));
sprintf(cmdbuf, "AT+CMGS=\"%s\"\r\n", argv[1]);
/* Construct message how fona expects */
msgbuf = calloc(strlen(argv[2])+2+1, sizeof(char));
sprintf(msgbuf, "%s\r\n", argv[2]);
/* We need ctrl+z character to signal the end of a message */
ctrlz[0] = 0x1A;
ctrlz[1] = '\r';
ctrlz[2] = '\n';
ctrlz[3] = 0;
init_fone_client();
if(send_hello(&in_fd, &out_fd) != 0) {
return 1;
}
write(out_fd, "AT+CMGF=1\r\n", 11);
read(in_fd, buf, PIPE_BUF);
if(strstr(buf, "OK") == NULL) {
fprintf(stderr, "[Error] Couldn't set text mode\n");
send_finish(&in_fd, &out_fd);
return 1;
}
memset(buf, 0, PIPE_BUF);
write(out_fd, cmdbuf, strlen(cmdbuf));
free(cmdbuf);
read(in_fd, buf, PIPE_BUF);
if(strncmp(buf, "\r\n>", 3) == 0) {
memset(buf, 0, PIPE_BUF);
write(out_fd, msgbuf, strlen(msgbuf));
write(out_fd, ctrlz, strlen(ctrlz));
read(in_fd, buf, PIPE_BUF);
printf("%s\n", buf);
} else {
fprintf(stderr, "[Info] Wasn't prompted for message input. Exiting.\n");
send_finish(&in_fd, &out_fd);
return 1;
}
free(msgbuf);
/* Set PDU mode */
memset(buf, 0, PIPE_BUF);
write(out_fd, "AT+CMGF=0\r\n", 11);
read(in_fd, buf, PIPE_BUF);
if(strstr(buf, "OK") == NULL) {
fprintf(stderr, "[Error] Couldn't set PDU mode\n");
send_finish(&in_fd, &out_fd);
return 1;
}
free(buf);
return send_finish(&in_fd, &out_fd);
}