-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwatch-util
executable file
·318 lines (292 loc) · 8.1 KB
/
watch-util
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/bin/bash
VERSION="1.2"
function showVersion {
echo "watch-util v${VERSION}"
}
function showHelp {
showVersion
cat << EOF
./watch-util [option] [command...]
Utility functions for AsteroidOS device. By default, uses "SSH Mode"
but can also use "ADB Mode."
Available options:
-h or --help prints this help screen and quits
-a or --adb uses ADB command to communicate with watch
-p or --port specifies a port to use for ssh and scp commands
-r or --remote specifies the remote (watch) name or address for ssh and scp commands
-q or --qemu communicates with QEMU emulated watch (same as -r localhost -p 2222 )
-v or --version displays the version of this program
Available commands:
backup BD backs up settings of currently connected watch to directory BD
restore BD restores previously saved settings from dir BD to connected watch
fastboot reboots into bootloader mode
forgetkeys forgets the ssh keys for both "watch" and "192.168.2.15"
present return true if watch is present, otherwise false
snap takes a screenshot and downloads it to a local jpg file
settime sets the time on the watch according to the current time on host
listtimezones lists the timezones known to the watch
settimezone TZ sets the timezone on the watch to the passed timezone
wallpaper WP sets the wallpaper on the watch to named file WP
restart restarts the ceres user on the watch
reboot reboots the watch
wait MSG wait for a watch to connect while displaying MSG
routing sets up the routing to go through the local host and DNS 1.1.1.1
pushface WF pushes the named watchface to the watch (point to WF directory)
watchface WF sets the active watchface to the named watchface
screen on|off sets the screen always on or normal mode
EOF
}
function doWatchCommand {
local user="$1"
local cmd=$2
case ${user} in
root)
if [ "${ADB}" == true ] ; then
adb shell "${cmd}"
else
ssh -p "${WATCHPORT}" -t root@"${WATCHADDR}" ${cmd}
fi
;;
ceres)
if [ "${ADB}" == true ] ; then
printf -v cmd %q "${cmd}"
adb shell "su -l -c ${cmd} ceres"
else
ssh -p "${WATCHPORT}" -t ceres@"${WATCHADDR}" ${cmd}
fi
;;
*)
echo "Error: unknown watch user ${user}"
;;
esac
}
function setDconf {
local dconfsetting="$1"
local filename="$2"
doWatchCommand "ceres" "dconf write ${dconfsetting} '\"file://${filename}\"'"
}
function pushFiles {
local user="$1"
local sourcedir="$2"
local destdir="$3"
if [ "${ADB}" = true ] ; then
if [ "${user}" != "root" ] && [[ "${destdir::1}" != '/' ]] ; then
destdir="/home/${user}/${destdir}"
fi
adb push ${sourcedir} "${destdir}"
else
scp -P"${WATCHPORT}" -r ${sourcedir} "${user}@${WATCHADDR}:${destdir}"
fi
}
function pullFiles {
local user="$1"
local sourcedir="$2"
local destdir="$3"
if [ "${ADB}" = true ] ; then
if [ "${user}" != "root" ] && [[ "${sourcedir::1}" != '/' ]] ; then
sourcedir="/home/${user}/${sourcedir}"
fi
adb pull ${sourcedir} "${destdir}"
else
scp -P"${WATCHPORT}" -r "${user}@${WATCHADDR}:${sourcedir}" "${destdir}"
fi
}
function pushWatchface {
pushFiles "root" "${1}"'/usr/share/*' "/usr/share/"
}
function pushWallpaper {
local source="$1"
local wallpaper
local destination="/usr/share/asteroid-launcher/wallpapers/full/${wallpaper}"
wallpaper="$(basename "$1")"
pushFiles "root" "${source}" "${destination}"
setDconf "/desktop/asteroid/background-filename" "${destination}"
}
function restartCeres {
doWatchCommand "root" "systemctl restart user@1000"
}
function activateWatchface {
setDconf "/desktop/asteroid/watchface" "/usr/share/asteroid-launcher/watchfaces/${1}.qml"
}
function setTimeZone {
doWatchCommand "root" "timedatectl set-timezone $1"
}
function forgetKeys {
ssh-keygen -R ${WATCHADDR}:${WATCHPORT}
ssh-keygen -R watch
}
function watchPresent {
local result=false
if [ "${ADB}" == true ] ; then
if [ "$(adb get-state 2>/dev/null)" = "device" ] ; then
result=true
fi
else
if route |grep 192.168.2.0 > /dev/null ; then
result=true
fi
fi
echo "${result}"
}
function waitWatch {
echo "$1"
if [ "${ADB}" == true ] ; then
adb wait-for-usb-device
else
while ! route |grep 192.168.2.0 > /dev/null ; do
sleep 1
done
fi
}
function setTime {
doWatchCommand "root" "date -s @`(date -u +"%s" )`"
}
function getScreenshot {
doWatchCommand "ceres" "screenshottool /home/ceres/screenshot.jpg 0"
pullFiles "ceres" "/home/ceres/screenshot.jpg" "$(date +%Y%m%d_%H%M%S).jpg"
}
function setRouting {
# get this host's IPv4 address
MYADDR=$(doWatchCommand "root" "who" | awk '{print $NF}')
doWatchCommand "root" "route add default gw ${MYADDR}"
doWatchCommand "root" 'echo "nameserver 1.1.1.1" >> /etc/resolv.conf'
}
function backupSettings {
local backupdir="$1"
pullFiles "ceres" "/home/ceres/.config" "${backupdir}/ceresconfig"
pullFiles "root" "/var/lib/connman" "${backupdir}/connman"
}
function restoreSettings {
local backupdir="$1"
pushd "${backupdir}/ceresconfig" || exit
pushFiles "ceres" "*" "/home/ceres/.config/"
cd "${backupdir}/connman" || exit
pushFiles "root" "*" "/var/lib/connman"
popd || exit
}
function screen {
if [ "$1" = "on" ] ; then
doWatchCommand "root" "mcetool -D on"
else
doWatchCommand "root" "mcetool -D off"
fi
}
# These are the defaults for SSH access
WATCHPORT=22
WATCHADDR=192.168.2.15
# These are the defaults for local QEMU target
QEMUPORT=2222
QEMUADDR=localhost
# Assume no ADB unless told otherwise
ADB=false
while [[ $# -gt 0 ]] ; do
case $1 in
-a|--adb)
ADB=true
shift
;;
-q|--qemu)
WATCHPORT=${QEMUPORT}
WATCHADDR=${QEMUADDR}
shift
;;
-p|--port)
WATCHPORT="$2"
shift
shift
;;
-r|--remote)
WATCHADDR="$2"
shift
shift
;;
-h|--help)
showHelp
exit 1
;;
-v|--version)
showVersion
shift
;;
backup)
backupSettings "$2"
shift
shift
;;
restore)
restoreSettings "$2"
shift
shift
;;
present)
watchPresent
shift
;;
fastboot)
doWatchCommand "root" "reboot bootloader"
shift
;;
screen)
screen "$2"
shift
shift
;;
snap)
getScreenshot
shift
;;
settime)
setTime
shift
;;
settimezone)
setTimeZone "$2"
shift
shift
;;
pushface)
pushWatchface "$2"
shift
shift
;;
forgetkeys)
forgetKeys
shift
;;
listtimezones)
doWatchCommand "root" "timedatectl list-timezones"
shift
;;
wallpaper)
pushWallpaper "$2"
shift
shift
;;
reboot)
doWatchCommand "root" "reboot"
shift
;;
restart)
restartCeres
shift
;;
wait)
waitWatch "$2"
shift
shift
;;
routing)
setRouting
shift
;;
watchface)
activateWatchface "$2"
shift
shift
;;
*)
echo "Ignoring unknown option $1"
shift
;;
esac
done