forked from 0xFACE/BlueBasic-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbbloadlines
More file actions
82 lines (69 loc) · 1.85 KB
/
bbloadlines
File metadata and controls
82 lines (69 loc) · 1.85 KB
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
#!/bin/bash
mac_address="$1"
file="$2"
function error() {
cat <<- EOH
$1
Usage: $0 <MAC address> <file>
Eg: $0 78:A5:04:56:E3:B2 wire_dht22.bbasic
This tool will upload the file to the BlueBasic device that has that MAC address
Need gatttool installed
EOH
exit 1
}
function upload_progress() {
progress_character='='
progress_length=10
progress_step=$((100/progress_length))
if (($1 >= progress_step)); then
percent_bar="$(head -c "$(($1/progress_step))" < /dev/zero | tr '\0' $progress_character)"
percent_bar="${percent_bar}$(printf "%$((progress_length-($1/progress_step)))s")"
else
percent_bar="$(printf "%${progress_length}s")"
fi
printf "\rUploading... [%s] (%s%%)" "$percent_bar" "$1"
}
# Load the file line by line into an inetrnal hexadecimal array
#
function load() {
totallc=$(wc -l < $file)
lc=""
hexdump=""
result=""
upload_progress 0
while read line; do
# Read a line
#
hexdump=""
line=$(printf '%s\r' "$line" | xxd -p | tr -d '\n')
hexdump+="$line"
# printf "\n%s\n" "$line"
# Send it
#
result="$(gatttool -i hci0 \
-a 0x0031 \
-b "$mac_address" \
-n "$line" \
--char-write-req 2>&1)"
# Check result
#
if [[ "$result" == "Characteristic value was written successfully" ]]; then
((lc+=1))
percentage=$(bc <<< "scale=2; (($lc)/($totallc))*100")
# printf "$lc $totallc $percentage"
upload_progress "${percentage%.*}"
else
printf "\n%s\n" "$result"
exit 1
fi
done < $file
printf "\n%s %s\n" "$file" " uploaded successfully"
exit 0
}
if ! [[ "$mac_address" =~ ^([a-fA-F0-9]{2}:){5}[a-zA-Z0-9]{2}$ ]]; then
error "Wrong MAC address! Example: AA:BB:CC:DD:EE:FF"
elif ! [[ -r $file && -f $file ]]; then
error "No such file!"
else
load
fi