-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_call_initiate.c
95 lines (81 loc) · 2.13 KB
/
client_call_initiate.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
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<limits.h>
#include<pthread.h>
#include<signal.h>
#include"client_protocol.h"
int in_fd, out_fd;
int ongoing_call = 0;
static pthread_t thread_subscription;
void *listen_for_subscription(void *arg);
void hangup_sender();
void hangup_sender() {
write(out_fd, "ATH\r\n", 5);
ongoing_call = 2;
pthread_cancel(thread_subscription);
}
void *listen_for_subscription(void *arg) {
char *buf = calloc(PIPE_BUF, sizeof(char));
int in_fd, out_fd;
/* Subscribe to "NO CARRIER" */
if(send_subscribe(&in_fd, &out_fd) != 0) {
fprintf(stderr, "[Fatal] Unable to subscribe to serial\n");
return NULL;
}
write(out_fd, "NO CARRIER", 10);
while(ongoing_call == 0 || ongoing_call == 1) {
if(read(in_fd, buf, PIPE_BUF) == 0) {
fprintf(stderr, "[INFO] Lost connection with server\n");
break;
}
if(strstr(buf, "NO CARRIER") != NULL) {
printf("call dropped\n");
ongoing_call = 2;
break;
}
memset(buf, 0, PIPE_BUF);
sleep(1);
}
free(buf);
send_finish(&in_fd, &out_fd);
return NULL;
}
int main(int argc, char **argv) {
char *serial_cmd = calloc(17, sizeof(char));
char *buf = calloc(PIPE_BUF, sizeof(char));
if(argc != 2) {
printf("Usage: %s <phone number>\n", argv[0]);
return 1;
}
signal(SIGINT, hangup_sender);
init_fone_client();
if(pthread_create(&thread_subscription, NULL, listen_for_subscription, NULL)) {
fprintf(stderr, "[Fatal] Cannot create pthread for listening\n");
return 1;
}
sleep(1);
if(send_hello(&in_fd, &out_fd) != 0) {
fprintf(stderr, "[Fatal] Can't connect to server\n");
} else {
write(out_fd, "AT+CMGF=0\r\n", 11);
sleep(1);
read(in_fd, buf, PIPE_BUF);
if(strstr(buf, "OK") != NULL) {
memset(buf, 0, PIPE_BUF);
snprintf(serial_cmd, 16, "ATD%s;\r\n", argv[1]);
write(out_fd, serial_cmd, strlen(serial_cmd));
sleep(1);
read(in_fd, buf, PIPE_BUF);
if(strstr(buf, "OK") != NULL) {
ongoing_call = 1;
}
} else {
fprintf(stderr, "[Error] Couldn't set PDU mode\n");
pthread_cancel(thread_subscription);
}
}
pthread_join(thread_subscription, NULL);
return send_finish(&in_fd, &out_fd);
}