Skip to content
This repository has been archived by the owner on Feb 20, 2019. It is now read-only.

READ/NOTIFY functionality #60

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bin/bleah
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def check_args(args):
args.data = binascii.unhexlify(hexdata)

if args.uuid is not None or args.handle is not None:
if args.data is None:
if args.data is None and args.xeadCharacteristic is None and args.waitNotification is None:
print("! Data parameter ( --data ) is required for write operations.\n")
quit()

Expand Down Expand Up @@ -100,6 +100,8 @@ def main():
parser.add_argument('-u', '--uuid', action='store', type=str, default=None, help='Write data to this characteristic UUID (requires --mac and --data).' )
parser.add_argument('-n', '--handle', action='store', type=str, default=None, help='Write data to this characteristic handle (requires --mac and --data).' )
parser.add_argument('-d', '--data', action='store', type=str, default=None, help='Data to be written as a quoted string or as hex ( 0xdeadbeef... ).' )
parser.add_argument('-x', '--xeadCharacteristic', action='store_true', help='Read data from characteristic' )
parser.add_argument('-w', '--waitNotification', action='store_true', help='Wait for notifications/indications from specified characteristic')
parser.add_argument('-r', '--datafile', action='store', type=str, default=None, help='Read data to be written from this file.' )
parser.add_argument('-m', '--mtu', action='store', type=int, default=None, help='Set MTU.' )
parser.add_argument('-j', '--json_log', action='store', type=str, default=None, help='Name of a json logfile' )
Expand Down
68 changes: 68 additions & 0 deletions bleah/read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# This file is part of BLEAH.
#
# Copyleft 2017 Simone Margaritelli
# [email protected]
# http://www.evilsocket.net
#
# This file may be licensed under the terms of of the
# GNU General Public License Version 3 (the ``GPL'').
#
# Software distributed under the License is distributed
# on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
# express or implied. See the GPL for the specific language
# governing rights and limitations.
#
# You should have received a copy of the GPL along with this
# program. If not, go to http://www.gnu.org/licenses/gpl.html
# or write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import sys
from bluepy.btle import Characteristic

from bleah.swag import *


def readChar(dev, args):
char = None
lookingfor = "uuid(%s)" % (args.uuid)
if args.handle:
lookingfor="handle(%d)" % (args.handle)
print("@ Searching for characteristic %s ..." % ( bold(lookingfor) )),
sys.stdout.flush()

for s in dev.services:
if char is not None:
break
elif s.hndStart == s.hndEnd:
continue

for i, c in enumerate( s.getCharacteristics() ):
if args.uuid:
if str(c.uuid) == args.uuid:
char = c
break
if args.handle:
if c.getHandle() == args.handle:
char =c
break

if char is not None:
if "READ" in char.propertiesToString():
print(green("found"))
print("@ Reading ..."),
sys.stdout.flush()

try:
raw = char.read()
print(raw)
print(green('done'))
#"value": deserialize_char( char, char.propertiesToString(), raw ),
#"value_plain": deserialize_char( char, char.propertiesToString(), raw, True ),
except Exception as e:
print(red( str(e) ))

else:
print(red('not readable'))

else:
print(red( bold("NOT FOUND") ))
17 changes: 14 additions & 3 deletions bleah/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from bleah.swag import *
from bleah.enumerate import *
from bleah.write import *
from bleah.read import *
from bleah.waitnotify import *

def macMatchesArgPattern(argpattern, mac):
"""
Expand Down Expand Up @@ -296,9 +298,18 @@ def __init__(self, args):
print(green('connected.'))

if args.uuid or args.handle:
print()
do_write_ops( dev, args )
print()
if args.xeadCharacteristic is True:
print()
readChar( dev , args)
print()
elif args.waitNotification is True:
print()
waitNotify( dev , args)
print()
else:
print()
do_write_ops( dev, args )
print()

if args.enumerate:
print("@ Enumerating all the things "),
Expand Down
97 changes: 97 additions & 0 deletions bleah/waitnotify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# This file is part of BLEAH.
#
# Copyleft 2017 Simone Margaritelli
# [email protected]
# http://www.evilsocket.net
#
# This file may be licensed under the terms of of the
# GNU General Public License Version 3 (the ``GPL'').
#
# Software distributed under the License is distributed
# on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
# express or implied. See the GPL for the specific language
# governing rights and limitations.
#
# You should have received a copy of the GPL along with this
# program. If not, go to http://www.gnu.org/licenses/gpl.html
# or write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import sys
from bluepy import btle

from bleah.swag import *


class NotificationDelegate(btle.DefaultDelegate):
def __init__(self):
btle.DefaultDelegate.__init__(self)
#init part here


def handleNotification(self,cHandle,data):
print("@ Value received: ")
print(data)
#"value": deserialize_char( char, char.propertiesToString(), raw ),
#"value_plain": deserialize_char( char, char.propertiesToString(), raw, True ),



def waitNotify(dev, args):
char = None
lookingfor = "uuid(%s)" % (args.uuid)
if args.handle:
lookingfor="handle(%d)" % (args.handle)
print("@ Searching for characteristic %s ..." % ( bold(lookingfor) )),
sys.stdout.flush()

for s in dev.services:
if char is not None:
break
elif s.hndStart == s.hndEnd:
continue

for i, c in enumerate( s.getCharacteristics() ):
if args.uuid:
if str(c.uuid) == args.uuid:
char = c
break
if args.handle:
if c.getHandle() == args.handle:
char =c
break

if char is not None:
if "NOTIFY" in char.propertiesToString() or "INDICATE" in char.propertiesToString():
print(green("found"))
print("@ Waiting for notifications ..."),
sys.stdout.flush()


try:
#p = btle.Peripheral(dev.addr)
dev.setDelegate(NotificationDelegate())
if "NOTIFY" in char.propertiesToString():
if "WRITE" in char.propertiesToString():
dev.writeCharacteristic(char.valHandle,"\x01\x00")
else:
dev.writeCharacteristic(char.valHandle+1,"\x01\x00")
if "INDICATE" in char.propertiesToString():
if "WRITE" in char.propertiesToString():
dev.writeCharacteristic(char.valHandle,"\x02\x00")
else:
dev.writeCharacteristic(char.valHandle+1,"\x02\x00")
while True:
if dev.waitForNotifications(1.0):
#somthing happened
print()

except Exception as e:
print(red( str(e) ))

else:
print(red('not readable'))

else:
print(red( bold("NOT FOUND") ))


Empty file modified setup.py
100644 → 100755
Empty file.