-
Notifications
You must be signed in to change notification settings - Fork 11
/
warpigui.py
369 lines (302 loc) · 9.35 KB
/
warpigui.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/python3
# encoding=utf-8
# Menu for the wigle/replacement device
# https://www.designer2k2.at 2021-2022
#
# This is working on a rpi4 with kali 64bit os
#
# Libs:
# gpsd https://github.com/MartijnBraam/gpsd-py3
#
#
# kismet conf must be correct!
# gpsd will be called, check that it works with UART
#
# it expects a USB drive on /media/usb/ with the folder kismet there.
# Logs will be written to /media/usb/
#
# Warning:
# there are only some failsafes, it will stop working on error!
# The username and password must match with kismet_site.conf
httpd_username = "root"
httpd_password = "toor"
import logging
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M",
filename="/media/usb/warpi.log",
)
logging.info("Startup")
# Sync HW Clock::
import subprocess
subprocess.run(["hwclock", "-s"])
logging.debug("HW Clock synced")
import board
import busio
from digitalio import DigitalInOut, Direction, Pull
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
from time import sleep, localtime, strftime
import gpsd
import psutil
import os
import signal
import RPi.GPIO as GPIO
import json
import requests
import socket
logging.debug("All imports done")
# Turn some logger to only show warnings:
logging.getLogger("gpsd").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)
# Create the SSD1306 OLED class.
disp = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
# flip screen that the usb ports from the rpi are on top
disp.rotation = 2
# Input Pin:
GPIO.setmode(GPIO.BCM)
logging.debug("IO Setup")
# Page:
Page = 1
def InterruptLeft(_):
global Page
# Loop over Pager 1,2,3
if Page > 2:
Page = 1
else:
Page = Page + 1
print(f"Page to be shown: {Page}")
def InterruptB(_):
fshutdown()
def InterruptA(_):
freboot()
def InterruptUp(_):
startservice()
def InterruptDown(_):
stopservice()
# 5 button A reboot
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(5, GPIO.RISING, callback=InterruptA, bouncetime=300)
# 6 button B shutdown
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(6, GPIO.RISING, callback=InterruptB, bouncetime=300)
# Up dir button start
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(22, GPIO.RISING, callback=InterruptUp, bouncetime=300)
# Down dir button stop
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(17, GPIO.RISING, callback=InterruptDown, bouncetime=300)
# Left dir button (switch display info)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(23, GPIO.RISING, callback=InterruptLeft, bouncetime=300)
logging.debug("GPIO Setup done")
# Clear display.
disp.fill(0)
disp.show()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new("1", (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=0)
# Load a font
font = ImageFont.truetype("/home/kali/Minecraftia.ttf", 8)
fontbig = ImageFont.truetype("/home/kali/arial.ttf", 24)
logging.debug("Display setup done")
# set country code
# call("iw reg set AT", shell=True)
gpsrun = False
life = True
sleeptime = 1
# globals for the log:
kisuselog = open("/media/usb/kisuselog.log", "w") # new every time
kiserrlog = open("/media/usb/kiserrlog.log", "a+") # append
kissubproc = 0
# this delay will be waited, then it starts automatically
autostart = 10
autostarted = False
def startservice():
logging.info("Starting GPSD / Kismet")
subprocess.Popen(["gpsd", "/dev/serial0", "-s", "9600"])
global kisuselog, kiserrlog, gpsrun, kissubproc
kissubproc = subprocess.Popen(["kismet"], stdout=kisuselog, stderr=kiserrlog)
gpsrun = True
def stopservice():
logging.info("Stopping GPSD / Kismet")
global gpsrun, kissubproc
gpsrun = False
# Send a polite INT (CTRL+C)
kissubproc.send_signal(signal.SIGINT)
try:
kissubproc.wait(10) # wait max 10sec to close
except subprocess.TimeoutExpired:
logging.debug("timeout during kill kismet happened")
try:
subprocess.run(
["killall", "gpsd", "--verbose", "--wait", "--signal", "QUIT"], timeout=5
)
except subprocess.TimeoutExpired:
logging.debug("timeout during kill gpsd happened")
def freboot():
logging.info("Rebooting")
global looping
looping = False
disp.fill(0)
disp.show()
subprocess.Popen(["reboot"])
quit()
def fshutdown():
global looping, kisuselog, kiserrlog
looping = False
logging.info("Shutdown")
stopservice()
kisuselog.close()
kiserrlog.close()
logging.debug("Kismet shutdown")
draw.rectangle((0, 0, width, height), outline=0, fill=0)
draw.text((0, 20), "Shutdown", font=fontbig, fill=255)
disp.image(image)
disp.show()
logging.debug("LCD Black")
subprocess.call("sudo shutdown -h now", shell=True)
logging.debug("shutdown -h triggered")
quit()
logging.debug("All setup, go into loop")
looping = True
while looping:
draw.rectangle((0, 0, width, height), outline=0, fill=0)
if life:
draw.rectangle((120, 56, width, height), outline=0, fill=255)
life = False
else:
draw.rectangle((120, 56, width, height), outline=0, fill=0)
life = True
cpu = psutil.cpu_percent()
mem = dict(psutil.virtual_memory()._asdict())["percent"]
swp = dict(psutil.swap_memory()._asdict())["percent"]
f = open("/sys/class/thermal/thermal_zone0/temp")
t = f.read()
f.close()
ct = int(t) / 1000.0
if cpu > 50:
subprocess.call(
"ps aux | sort -nrk 3,3 | head -n 10 >> /media/usb/highcpu.log", shell=True
)
logging.debug(f"High CPU: {cpu}")
sleeptime = 3
else:
sleeptime = 1
if Page == 1:
# Page 1 is the main screen, it shows information while the device runs.
draw.text(
(0, 0),
f"CPU: {cpu / 100:>4.0%} M: {mem / 100:>4.0%} T: {ct:5.1f}",
font=font,
fill=255,
)
draw.text(
(0, 54), strftime("%Y-%m-%d %H:%M:%S", localtime()), font=font, fill=255
)
if gpsrun:
try:
gpsd.connect()
packet = gpsd.get_current()
draw.text(
(0, 10),
f"GPS: {packet.mode} SAT: {packet.sats:>3} Use: {packet.sats_valid:>3}",
font=font,
fill=255,
)
if packet.mode == 0:
draw.rectangle((115, 10, width - 2, 20), outline=0, fill=0)
if packet.mode == 1:
draw.rectangle((120, 14, width - 4, 18), outline=255, fill=0)
if packet.mode == 2:
draw.rectangle((120, 14, width - 4, 18), outline=255, fill=1)
if packet.mode == 3:
draw.rectangle((115, 10, width - 2, 20), outline=255, fill=1)
resp = requests.get(
"http://127.0.0.1:2501/system/status.json",
auth=(httpd_username, httpd_password),
)
data = resp.json()
devices = data["kismet.system.devices.count"]
kismetmemory = data["kismet.system.memory.rss"] / 1024
draw.text((0, 20), f"D {devices:>7}", font=fontbig, fill=255)
draw.text(
(0, 44),
f"Kismet mem: {kismetmemory:>4.0f}mb",
font=font,
fill=255,
)
except Exception as e:
logging.error(f"An exception occurred {e}")
if Page == 2:
# Page 2 shows the IP from the system
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
s.connect(("10.254.254.254", 1))
rpiIP = s.getsockname()[0]
except Exception:
rpiIP = "127.0.0.1"
finally:
s.close()
draw.text(
(0, 0),
f"SSH IP: {rpiIP}",
font=font,
fill=255,
)
if Page == 3:
# Page 3 gives a short info about the buttons
draw.text(
(0, 0),
f"#5 button = reboot",
font=font,
fill=255,
)
draw.text(
(0, 10),
f"#6 button = shutdown",
font=font,
fill=255,
)
draw.text(
(0, 20),
f"up arrow = start",
font=font,
fill=255,
)
draw.text(
(0, 30),
f"down arrow = stop",
font=font,
fill=255,
)
draw.text(
(0, 40),
f"left arrow = screen",
font=font,
fill=255,
)
if not autostarted:
if autostart > 0:
autostart = autostart - 1
else:
autostarted = True
if not gpsrun:
startservice()
# draw the screen:
disp.image(image)
disp.show()
# wait a bit:
sleep(sleeptime)
while True:
sleep(10)