-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
foldedtoad
committed
Jul 18, 2015
1 parent
ff8ae62
commit 54e98e0
Showing
2 changed files
with
257 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#!/usr/bin/env python | ||
|
||
#------------------------------------------------------------------------------ | ||
# Device scan | ||
#------------------------------------------------------------------------------ | ||
|
||
from subprocess import call | ||
|
||
import pexpect | ||
import signal | ||
|
||
#------------------------------------------------------------------------------ | ||
# Bluetooth LE scan for advertising peripheral devices | ||
#------------------------------------------------------------------------------ | ||
class HciTool: | ||
|
||
def __init__( self, advert_name ): | ||
self.advert_name = advert_name | ||
return | ||
|
||
def scan( self ): | ||
|
||
try: | ||
self.hcitool = pexpect.spawn('hcitool lescan') | ||
#self.hcitool.logfile = sys.stdout | ||
index = self.hcitool.expect(['LE Scan ...'], 1) | ||
except pexpect.EOF: | ||
self.hcitool.terminate(force=True) | ||
return [] | ||
except Exception as err: | ||
print "scan: exception: {0}".format(sys.exc_info()[0]) | ||
self.hcitool.terminate(force=True) | ||
return [] | ||
|
||
if index != 0: | ||
print "scan: failed" | ||
self.hcitool.terminate(force=True) | ||
return [] | ||
|
||
list = [] | ||
for dummy in range(0, 2): | ||
try: | ||
list.append(self.hcitool.readline()) | ||
except pexpect.TIMEOUT: | ||
break | ||
|
||
if list == []: | ||
return [] | ||
|
||
# Eliminate duplicate items in list | ||
list = set(list) | ||
|
||
# Remove non self.advert_name units | ||
if self.advert_name != None: | ||
list = [item for item in list if self.advert_name in item] | ||
|
||
# remove empty items from list | ||
while '\r\n' in list: | ||
list.remove('\r\n') | ||
|
||
# Strip newline from items in list | ||
list = [item.strip() for item in list] | ||
list = list[0:2] | ||
|
||
# Close pexpect (release device for subsequent use) | ||
self.hcitool.terminate(force=True) | ||
|
||
return list | ||
|
||
#------------------------------------------------------------------------------ | ||
# | ||
#------------------------------------------------------------------------------ | ||
class Scan: | ||
|
||
def __init__( self, advert_name ): | ||
self.advert_name = advert_name | ||
return | ||
|
||
def scan(self): | ||
|
||
scan_list = [] | ||
|
||
try: | ||
hcitool = HciTool(self.advert_name) | ||
scan_list = hcitool.scan() | ||
|
||
except KeyboardInterrupt: | ||
# On Cntl-C | ||
pass; | ||
except pexpect.TIMEOUT: | ||
print "scan: pexpect.TIMEOUT" | ||
pass | ||
except Exception as e: | ||
print "scan: exception: {0} ".format(sys.exc_info()[0]) | ||
pass | ||
|
||
|
||
return scan_list | ||
|
||
#------------------------------------------------------------------------------ | ||
# | ||
#------------------------------------------------------------------------------ | ||
if __name__ == '__main__': | ||
|
||
# Do not litter the world with broken .pyc files. | ||
sys.dont_write_bytecode = True | ||
|
||
#scanner = Scan("DfuTarg") # specific advertisement name | ||
scanner = Scan(None) # any advertising name | ||
|
||
scanner.scan() | ||
|
||
print "scan complete" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#!/usr/bin/env python | ||
|
||
#------------------------------------------------------------------------------ | ||
# Graphical User Interface for DFU Server | ||
#------------------------------------------------------------------------------ | ||
|
||
import os | ||
|
||
from Tkinter import * | ||
|
||
import ttk | ||
import tkMessageBox | ||
|
||
import tkFileDialog | ||
from tkFileDialog import askopenfilename | ||
|
||
from scan import Scan | ||
from dfu2 import * | ||
|
||
#------------------------------------------------------------------------------ | ||
# | ||
#------------------------------------------------------------------------------ | ||
class Application(Frame): | ||
|
||
def __init__(self, master): | ||
Frame.__init__(self, master) | ||
self.file = None | ||
self.addr = None | ||
self.device = None | ||
self.grid() | ||
self.create_widgets() | ||
|
||
def create_widgets(self): | ||
|
||
self.frame1 = Frame(self) | ||
self.frame1['relief'] = RIDGE | ||
self.frame1.grid(row=0, column=0, pady=3, sticky=N) | ||
|
||
self.button1 = Button(self.frame1) | ||
self.button1["width"] = 20 | ||
self.button1["text"] = "Select application.zip file" | ||
self.button1["command"] = self.selectFile | ||
self.button1.grid(row=0, column=0, sticky=N) | ||
|
||
self.text1 = Label(self.frame1) | ||
self.text1["background"] = "white" | ||
self.text1["width"] = 30 | ||
self.text1["text"] = "none" | ||
self.text1.grid(row=1, column=0, pady=5, sticky=N) | ||
|
||
self.frame2 = Frame(self) | ||
self.frame2.grid(row=1, rowspan=2, sticky=N) | ||
|
||
self.button2 = Button(self.frame2) | ||
self.button2["width"] = 20 | ||
self.button2["text"] = "Scan for target devices" | ||
self.button2["command"] = self.get_device_name | ||
self.button2.grid(row=0, column=0, sticky=N) | ||
|
||
self.scrollbar2 = Scrollbar(self.frame2) | ||
self.scrollbar2.grid(row=2, column=0, sticky=N) | ||
|
||
self.listbox2 = Listbox(self.frame2) | ||
self.listbox2['height'] = 5 | ||
self.listbox2.bind("<Double-Button-1>", self.device_selected) | ||
self.listbox2.grid(row=2, column=0, sticky=N) | ||
|
||
self.scrollbar2.config(command=self.listbox2.yview) | ||
self.listbox2.config(yscrollcommand=self.scrollbar2.set) | ||
|
||
self.text2 = Label(self.frame2) | ||
self.text2["text"] = "Double-click to select" | ||
self.text2.grid(row=3, column=0, sticky=N) | ||
|
||
self.frame3 = Frame(self) | ||
self.frame3.grid(row=7, column=0, rowspan=2, pady=5, sticky=N) | ||
|
||
self.progress3 = ttk.Progressbar(self.frame3) | ||
self.progress3['orient'] = 'horizontal' | ||
self.progress3['length'] = 250 | ||
self.progress3['mode'] = 'determinate' | ||
self.progress3.grid(row=0, column=0, sticky=N) | ||
|
||
self.text3 = Label(self.frame3) | ||
self.text3["text"] = "Download progress" | ||
self.text3.grid(row=3, column=0, sticky=N+W+E) | ||
#self.text3.pack(side=BOTTOM, fill="x") | ||
|
||
def selectFile(self): | ||
self.file = askopenfilename(filetypes=[('Zip files', '*.zip')]) | ||
filename = os.path.basename(self.file) | ||
self.text1["text"] = filename | ||
|
||
def get_device_name(self): | ||
scanner = Scan(None) | ||
targets = scanner.scan() | ||
self.listbox2.delete(0, END) | ||
|
||
if targets: | ||
for target in targets: | ||
index = targets.index(target) | ||
addr = targets[index][:17] | ||
self.listbox2.insert("end", addr) | ||
|
||
def device_selected(self, event): | ||
widget = event.widget | ||
selected = widget.curselection() | ||
self.addr = widget.get(selected[0]) | ||
|
||
if self.addr and self.file: | ||
print "addr: {0}".format(self.addr) | ||
print "file: {0}".format(self.file) | ||
|
||
# dfu_server("-z {0} -a {1}".format(self.file, self.addr)) | ||
|
||
else: | ||
tkMessageBox.showwarning("Error", "Missing application file") | ||
|
||
return | ||
|
||
|
||
#------------------------------------------------------------------------------ | ||
# | ||
#------------------------------------------------------------------------------ | ||
def main(): | ||
|
||
root = Tk() | ||
root.title("DFU Server") | ||
root.configure(bg='lightgrey') | ||
root.geometry("250x235") | ||
|
||
app = Application(root) | ||
|
||
root.mainloop() | ||
|
||
#------------------------------------------------------------------------------ | ||
# | ||
#------------------------------------------------------------------------------ | ||
if __name__ == '__main__': | ||
|
||
# Do not litter the world with broken .pyc files. | ||
sys.dont_write_bytecode = True | ||
|
||
main() |