Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
-Added GPIO controlling in `DroneLogic`. Untested. Features include:
  - Blinking 5 seconds on, 1 second off when waiting for Flight
  - Blinking at 1 Hz when flying
  - Blinking at 1 Hz 10 times when failing to login
  - Blinking at 3 Hz 30 times when error occurs
  - More features to come.

- Restructured `DroneLogic thread` into `DroneLogicSystem class` to simplify and reduce issues.
- Moved multiple Coordinate functions into `AATC_Coordinate`
- Changed `DroneGetDroneInfo` to not require login as `Drone` is already logged in.
-Added more Functions to `AATC_GPIO`
  • Loading branch information
Scratchcat1 authored Oct 5, 2017
1 parent 9816fc7 commit 8d2e3c2
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 173 deletions.
50 changes: 50 additions & 0 deletions AATC_Coordinate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,53 @@ def Print(self):

def __str__(self):
return str((round(self.x,8),round(self.y,8),round(self.z,8)))


def AddCoords(Coord,VectorCoord): #Simulates the drone moving
Coord.x += VectorCoord.x
Coord.y += VectorCoord.y
Coord.z += VectorCoord.z
time.sleep(0.1)
return Coord

def CalculateVector(Coords,TargetCoords,Speed):
dx = TargetCoords.x- Coords.x
dy = TargetCoords.y- Coords.y
dz = TargetCoords.z- Coords.z


yCircumference = 40008000
xCircumference = 40075160
#Converts to metres
mdy = dy * yCircumference /360
mdx = dx * xCircumference * math.cos(toRadian(TargetCoords.y)) /360

v = math.sqrt(mdx**2+mdy**2+ dz**2)
ratio = Speed/v
svx = dx*ratio #Gets Speed vectors
svy = dy*ratio
svz = dz*ratio
return AATC_Drone.Coordinate(svx,svy,svz)

def toRadian(x):
return x*math.pi/180

def toDegree(x):
return 180*x/math.pi


def DeltaCoordToMetres(aCoord,bCoord):
#Formula for dx and dy from : https://stackoverflow.com/questions/3024404/transform-longitude-latitude-into-meters
dx = abs(aCoord.x - bCoord.x)
dy = abs(aCoord.y - bCoord.y) # in degrees
dz = abs(aCoord.z - bCoord.z)


yCircumference = 40008000
xCircumference = 40075160

mdy = dy * yCircumference /360
mdx = dx * xCircumference * math.cos(toRadian(aCoord.y)) /360

Distance = math.sqrt(mdx**2 + mdy**2 + dz**2)
return Distance
128 changes: 93 additions & 35 deletions AATC_Crypto.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#AATC crypto module
# Diffie-Hellman from 'pip install diffiehellman '
import diffiehellman.diffiehellman as DH
import codecs,recvall,ast
#import diffiehellman.diffiehellman as DH
import codecs,recvall,ast,binascii,os
from Crypto.Cipher import AES
from Crypto.PublicKey import RSA

class Crypter:
"""
Expand All @@ -29,7 +30,7 @@ def __init__(self, con, mode = "CLIENT",AutoGenerate = True):
def SetMode(self,mode):
self.mode = mode

def GenerateKey(self,key_size = 256):
def GenerateKey(self,key_size = 2048):
print("Generating encryption keys. Please stand by...") #Generating keys takes a long time and have found no way to shorted key length
if self.mode == "SERVER":
self.ServerGenerateKey()
Expand All @@ -40,51 +41,109 @@ def GenerateKey(self,key_size = 256):
print("Encryption keys generated",self.shared_key)


def ServerGenerateKey(self):
DifHel = DH.DiffieHellman()

def ClientGenerateKey(self,RSA_KeySize,AES_KeySize):
RSAKey = RSA.generate(RSA_KeySize)

privateKey = RSAKey.exportKey("DER")
publicKey = RSAKey.publicKey().exportKey("DER")

self.Send(("ExchangeKey",(publicKey,AES_KeySize))) #Format ("ExchangeKey",("a1b2c3.....",))
data = self.Recv()
Sucess,Message = data[0],data[1] #Format (Sucess,("a2c3d4..."))
if Sucess == False:
raise Exception("Error occured while exchanging keys")

self.Send(("Exit",()))
data = self.Recv()
if data[0] == False:
raise Exception("Server failed to commit to exit")

RSAPrivateObject = RSA.importKey(privateKey)

self.AESKey = RSAPrivateObject.decrypt(Message)
self.AES = AES.new(self.AESKey)



def ServerGenerateKey(self):

Exit = False
while not Exit :
while not Exit:
data = self.Recv()
Command, Arguments = data[0],data[1]

if Command == "PublicKey":
try:
DifHel.generate_public_key()
DifHel.generate_shared_secret(Arguments[0])
self.Send(("PublicKey",(DifHel.public_key,)))
except:
self.Send((False,()))

if Command == "ExchangeKey":
publicKey,AES_KeySize = Arguments[0],Arguments[1]
if AES_KeySize not in [16,24,32]:
AES_KeySize = 32 #If key size is not valid set size to default of 32

self.AESKey = binascii.b2a_hex(os.urandom(AES_KeySize)) # Here to allow regeneration of AES key while still in loop if required.

PublicKeyObject = RSA.importKey(publicKey)
EncryptedAESKey = PublicKeyObject.encrypt(self.AESKey,"x")[0]
self.Send((True,(self.AESKey,)))

elif Command == "Exit":
self.Send((True,()))
Exit = True

self.shared_key = DifHel.shared_key[0:32]
self.AES = AES.new(self.shared_key)




def ClientGenerateKey(self,key_size):
DifHel = DH.DiffieHellman()
DifHel.generate_public_key()
else:
self.Send((False,("Command does not exist",)))

self.AES = AES.new(self.AESKey)



self.Send(("PublicKey",(DifHel.public_key,)))
data = self.Recv()
if data[0] == False:
raise Exception("Error occured while transmitting public key")
DifHel.generate_shared_secret(data[1][0])

self.Send(("Exit",()))
data = self.Recv()
if data[0] == False:
raise Exception("Server failed to commit to exit")

self.shared_key = DifHel.shared_key[0:32]
self.AES = AES.new(self.shared_key)


## def DHServerGenerateKey(self):
## DifHel = DH.DiffieHellman()
##
##
## Exit = False
## while not Exit :
## data = self.Recv()
## Command, Arguments = data[0],data[1]
##
## if Command == "PublicKey":
## try:
## DifHel.generate_public_key()
## DifHel.generate_shared_secret(Arguments[0])
## self.Send(("PublicKey",(DifHel.public_key,)))
## except:
## self.Send((False,()))
##
## elif Command == "Exit":
## self.Send((True,()))
## Exit = True
##
## self.shared_key = DifHel.shared_key[0:32]
## self.AES = AES.new(self.shared_key)
##
##
##
##
## def DHClientGenerateKey(self,key_size):
## DifHel = DH.DiffieHellman()
## DifHel.generate_public_key()
##
##
## self.Send(("PublicKey",(DifHel.public_key,)))
## data = self.Recv()
## if data[0] == False:
## raise Exception("Error occured while transmitting public key")
## DifHel.generate_shared_secret(data[1][0])
##
## self.Send(("Exit",()))
## data = self.Recv()
## if data[0] == False:
## raise Exception("Server failed to commit to exit")
##
## self.shared_key = DifHel.shared_key[0:32]
## self.AES = AES.new(self.shared_key)



Expand All @@ -110,6 +169,5 @@ def Recv(self):
data = ast.literal_eval(codecs.decode(data))
# (Command,Arguments)
return data
#return data[0],data[1],data[2]
except Exception as e:
print("Error in Cryptor while receiving ",e)
9 changes: 2 additions & 7 deletions AATC_DB.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,8 @@ def DroneCheckCredentials(self,DroneID,DronePassword):
return False,"Incorrect Drone Credentials",-1

def DroneGetDroneInfo(self,DroneID,DronePassword):
self.cur.execute("SELECT 1 FROM DroneCredentials WHERE DroneID = %s AND DronePassword = %s",(DroneID,DronePassword))
DroneIDFetch = self.cur.fetchall()
if DroneIDFetch != ():
self.cur.execute("SELECT * FROM Drone WHERE DroneID = %s",(DroneID,))
return True,str(self.Table_Headers("Drone")),self.cur.fetchall()
else:
return False,"Incorrect Drone Credentials",[]
self.cur.execute("SELECT * FROM Drone WHERE DroneID = %s",(DroneID,))
return True,str(self.Table_Headers("Drone")),self.cur.fetchall()



Expand Down
4 changes: 2 additions & 2 deletions AATC_Drone.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def UpdateDroneStatus(self,LastCoords,LastBattery):

############################

def DroneGetDroneInfo(self,DroneID,DronePassword):
self.Send("DroneGetDroneInfo",(DroneID,DronePassword))
def DroneGetDroneInfo(self,DroneID):
self.Send("DroneGetDroneInfo",(DroneID,))
Sucess,Message,DroneInfo = self.Recv()
return Sucess,Message,DroneInfo

Expand Down
Loading

0 comments on commit 8d2e3c2

Please sign in to comment.