-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASHV3.py
127 lines (112 loc) · 4.38 KB
/
ASHV3.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
import asyncio
import serial
from time import sleep
import hmac
import hashlib
""" Creates packets in proper formats for transmission and requests that format from satellite """
def packetSelect(typeOfPacket, dataType):
if(typeOfPacket == 'Window'):
# Window packet
print("Window Packet")
packet = '00000000'
packet += int4tobin(30) # 'Input the number of seconds until window start: '
packet += int2tobin(30) # 'Input the duration of the window in seconds: '
packet += int1tobin(dataType)
print("30 seconds until window start, 30 second window")
# 'Number from 0-4 corresponding to requested data type.\n0 - Attitude, 1 - TTNC, 2 - Deploy, 3 - HQ, 4 - LQ: '
packet += int2tobin(0) # picture number
packet += int4tobin(1) # start from line 1
print(packet)
print(hex(int(packet, 2))[2:].zfill(28))
return hex(int(packet, 2))[2:].zfill(28)
else:
#Command Packet
print("Command Packet")
commandsList = []
content = '00000001'
commandsList.append(1) # Input 0 for disable TX, 1 for enable TX
commandsList.append(0) # Input 0 for do nothing, 1 for erase all TX windows and progress
commandsList.append(1) # Input 0 for do nothing, 1 for take a picture
commandsList.append(0) # dont deploy boom
commandsList.append(0) # dont reboot
commandsList.append(0) # dont enable AX25
commandsList.append(1) # skip to postBoomDeploy
commandsList.append(1) # delete pictures
commandsList.append(0) # dont delete data
commandsList.append(0) # disable beacon
commandsList.append(0) # disable audio beacon
commandsList.append(0)
commandsList.append(0)
commandsList.append(0)
commandsList.append(0)
# If more commands are added, we must take out some of the dead bits
for command in commandsList:
if command == '0':
content += '00000000'
else:
content += '00000001'
# This adds 4 dead bits to the end
return hex(int(content, 2))[2:].zfill(32)
def transmitPacket(packet, AX25):
serialPort = serial.Serial('/dev/serial0', 115200)
serialPort.write(b'ES+W23003321\r') # Changed based on which is transmitting
sleep(1)
for i in range(50):
serialPort.write(b'ES+W22003321\r')
sleep(.120)
print(packet)
if AX25:
data = bytearray.fromhex(packet)
else:
data = bytearray.fromhex(b'GASPACS'.hex() + packet + b'GASPACS'.hex())
print(b'GASPACS'.hex() + packet + b'GASPACS'.hex())
print('Sending Data')
print(data)
serialPort.write(data)
def int4tobin(num):
"""takes a 4 byte int, returns a binary representation of it"""
return str(format(num, '032b'))[-32:]
def int1tobin(num):
"""takes a 1 byte integer, returns a binary representation of it"""
return str(format(num, '08b'))[-8:]
def int2tobin(num):
"""takes a 2 byte integer, returns a binary representation of it"""
return str(format(num, '016b'))[-16:]
def encrypt(packet):
"""encrypt packet using hmac and append hash to the end of the packet"""
key = b'SECRETKEY'
binaryPacketLength = len(packet) * 4
binaryPacket = bytes(format(int(packet,16), 'b').zfill(binaryPacketLength), 'utf8')
hash = hmac.new(key, binaryPacket, digestmod=hashlib.md5)
hashhex = hash.hexdigest()
fullpacket = packet + hashhex
return fullpacket
def main():
print("Started running program")
timeBetweenPasses = 1 * 60
print(str(timeBetweenPasses) + " seconds between passes")
timeElasped = 50
typeOfPacket = "Window"
dataType = 0
while True:
if (timeElasped >= timeBetweenPasses - 2 and timeElasped <= timeBetweenPasses + 2):
packet = packetSelect(typeOfPacket, dataType)
encryptedPacket = encrypt(packet)
transmitPacket(encryptedPacket, False)
if (typeOfPacket == "Window" and dataType >= 4):
typeOfPacket = "Command"
dataType = -1
else:
typeOfPacket = "Window"
dataType += 1
if (dataType == 2):
dataType = 4
if (typeOfPacket == "Window"):
print("Data Type: ", dataType)
timeElasped = 0
else:
sleep(1)
timeElasped += 1
print("Time Elapsed: " + str(timeElasped))
if __name__ == '__main__':
main()