-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.sh
executable file
·217 lines (166 loc) · 3.95 KB
/
functions.sh
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
#!/bin/bash
function setup_tty()
{
dev=${1:-/dev/ttyUSB0}
stty -F $dev 115200 raw -echo
}
function emit()
{
echo "emit: $@" >&2
}
function get_len_dec()
{
echo -ne "$1" | wc -c
}
function dec_to_hex1()
{
len="$1"
printf '\\x%02X' $len
}
function dec_to_hex2()
{
len="$1"
printf '\\x%02X\\x%02X' $(($len >> 8)) $(($len & 0xff))
}
function get_frame_len()
{
len="$(get_len_dec "$1")"
len=$(($len+2))
dec_to_hex2 "$len"
}
function get_len()
{
dec_to_hex1 $(get_len_dec "$1")
}
function rev()
{
sed 's/^/\n/;:a; s/^\(.*\n\)\(.\)/\2\1/;/\n$/!ba;s/\n//'
}
function ascii_to_gsm7_map()
{
# TODO: not implemented yet
echo -n "$1"
}
function ascii_to_gsm7()
{
local data=''
local bin_data=''
local hex_byte=0
local byte=0
for hex_byte in $(echo -n "$1" | xxd -p -u -c 256 | sed 's/\(..\)/\1 /g')
do
hex_byte="$(ascii_to_gsm7_map $hex_byte)"
byte="$(printf "%d" "0x$hex_byte")"
bits="$(printf '%07d' "$(echo "obase=2;$byte" | bc)" | rev)"
bin_data="$bin_data$bits"
done
pad_bits=$(( 8 - $(echo -n $bin_data | wc -c) % 8 ))
bin_data="$bin_data$(head -c $pad_bits </dev/zero | tr '\0' '0' )"
for bits in $(echo $bin_data | sed 's/\(........\)/\1\n/g')
do
bits=$(echo "$bits" | rev)
byte=$(echo "obase=16;ibase=2;$bits" | bc)
printf '\\x%02X' "0x$byte"
done
}
function num_to_oct()
{
num=${1//[^0-9]/}
data=''
for two_digits in $(echo $num | sed 's/\(..\)/\1 /g')
do
two_digits=${two_digits}F
lower=${two_digits:0:1}
upper=${two_digits:1:1}
data="$data\x$upper$lower"
done
echo "$data"
}
function encode_phone_number() {
num="$1"
data="\x82\x0C${2}\x08${3}"
# number type
data="${data}\x91"
# encode number itself
data="$data$(num_to_oct $num)"
echo -n "$data"
}
function get_fbus_crc()
{
pkg="$1"
byte_num=0
odd=0
even=0
for byte in $(echo "$pkg" | tr '\\' ' ')
do
((byte_num++))
dec_byte="$(printf '%d' "0$byte")"
if [ $(($byte_num & 0x01)) -eq 1 ]
then
odd=$(($odd ^ $dec_byte))
else
even=$(($even ^ $dec_byte))
fi
done
printf '\\x%02X\\x%02X' $odd $even
}
function build_message_block() {
msg="$1"
data="$(ascii_to_gsm7 "$msg")"
len="$(( $(get_len_dec "$data") + 4))"
len2="$(get_len "$data")"
len3="$(get_len "$msg")"
while [ $(( $(( $(get_len_dec "$data") + 4)) % 8 )) -ne 0 ]
do
data="${data}\x55"
done
len1=$( dec_to_hex1 $(( $(get_len_dec "$data") + 4)) )
data="\x80${len1}${len2}${len3}${data}"
echo -n "$data"
}
function build_sms_frame()
{
# globals $smsc $destnum $message
# Byte 6~14: Documentation is a LIE! https://github.com/pkot/gnokii/blob/master/common/phones/nk6510.c#L2060
sms_header="\x00\x01\x00\x02\x00\x00\x00\x55\x55"
# magic follows
sms_header="${sms_header}\x01\x02"
# more magic
sms_frame="\x11\x00\x00\x00\x00\x04"
sms_frame="${sms_frame}$(encode_phone_number "$destnum" '\x01' '\x0b')"
sms_frame="${sms_frame}$(encode_phone_number "$smsc" '\x02' '\x07')"
sms_frame="${sms_frame}$(build_message_block "$message")"
sms_frame="${sms_frame}\x08\x04\x01\xA9"
len=$(( $(get_len_dec "$sms_frame") + 2 ))
len=$(dec_to_hex1 "$len")
sms_frame="${sms_header}${len}${sms_frame}"
echo -n "$sms_frame"
}
function fbus_encapsulate()
{
local sms_frame="$1"
# Byte 0: Frame ID (0x1E Cable)
# Byte 1: Destination address. (0x00 Phone)
# Byte 2: Source address. (0x0C Terminal)
# Byte 3: Message Type or 'command'. (0x02 SMS Handling).
local fbus_frame="\x1E\x00\x0C\x02"
# Byte 4-5: Message length.
fbus_frame="${fbus_frame}$(get_frame_len "$sms_frame")"
# add payload
fbus_frame="${fbus_frame}${sms_frame}"
# frames to go 0x01 means last frame
fbus_frame="${fbus_frame}\x01"
# sequence number
fbus_frame="${fbus_frame}\x60"
# padd in case frame length is odd
fbus_bytes="$(echo -ne "$fbus_frame" | wc -c)"
if [ $(($fbus_bytes & 0x01)) -eq 1 ]
then
fbus_frame="${fbus_frame}\x00"
fi
# add crc
fbus_frame="${fbus_frame}$(get_fbus_crc "$fbus_frame")"
# add preamble
fbus_frame="$(printf '\x55%.0s' {1..512})$fbus_frame"
echo -n "$fbus_frame"
}