-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkCall.py
84 lines (66 loc) · 2.92 KB
/
checkCall.py
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
import time
import serial
import os
import subprocess
import datetime
# DateTime for log
now = datetime.datetime.now()
# Declare the device address
serialDevice = "/dev/ttyACM0"
# Target phone number after validation
targetPhoneNumber = "555555555"
# Open phone device, baudrate and connection timeout
# Some devices are a little slow getting the initial connection
# So we declare 5s for timeout
phone = serial.Serial(serialDevice, 19200, timeout=5)
# Try catch function
try:
time.sleep(1)
# Waiting for voice call loop
while(1):
# Get phone responses
x = phone.readline()
# Debug, printing phone responses
# print(x)
# If we get active connection line with RING response, do the job
# NOTES:
# 1. Some old GSM Modems doesn't have RING response.
# 2. If this is the case, we need to loop AT+CLCC to check if there is a active Caller_ID
if (x == b'RING\r\n'):
# Connection line is active
# Log the ring event
print('Ringing - Someone is calling')
# Lets RING one more time on the original traffic subject
time.sleep(5)
# Log connection date/time
print("Connection Date/Time: ", now)
# Run atcom AT commands
proc = subprocess.Popen(["atcom --port " + str(serialDevice) + " -b 19200 AT+CLCC"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
# Log the command object output
print("CID Output: ", out)
# Convert output object to string so we can use it later
word = str(out)
# Hangup the call
os.system("atcom --port " + str(serialDevice) + " -b 19200 AT+CHUP >/dev/null 2>&1")
# Clear the Caller ID string for unwanted chars
new_string = word.replace('\r\n','')
# All done ... Lets check the permited numbers file
with open('numbers_permited.txt') as f:
while True:
line = f.readline()
if line.strip():
if new_string.find(line.strip()) != -1:
# Log the event if caller id found
print("Caller ID Found", line.strip())
os.system('atcom --port ' + str(serialDevice) + ' -b 115200 ATDT"' + str(targetPhoneNumber) + ';" >/dev/null 2>&1')
time.sleep(10)
os.system("atcom --port " + str(serialDevice) + " -b 19200 AT+CHUP >/dev/null 2>&1")
if not line:
break
# Wait 3s for the nest loop
print("-----------------------\n\n")
time.sleep(3)
finally:
# We don't have connection. We must signal the state to the kernel, and close the device
phone.close()