From ebd89ed8916ca907d2b5f138426ce60f2fff1f19 Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Wed, 4 Nov 2020 10:24:04 +0100 Subject: [PATCH 1/2] Add python script to make the output of serial debug analyzable through wireshark This python script allows to encode the Serial debug output into a format that can be analyzed by wireshark and hcidump Steps to debug: - modify the sketch attaching Serial to BLE debug 'BLE.debug(Serial)' - execute the sketch and open the arduino serial monitor - save the content of the serial monitor in a file (here 'inputFile') - execute the script with the following arguments: -i to specify the input file, -o to specify the output file 'python your/script/path/arduino-ble-parser.py -i inputFile -o outputFile' - using wireshark, simply open outputFile --- extras/arduino-ble-parser.py | 88 ++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 extras/arduino-ble-parser.py diff --git a/extras/arduino-ble-parser.py b/extras/arduino-ble-parser.py new file mode 100644 index 00000000..a8a12ad1 --- /dev/null +++ b/extras/arduino-ble-parser.py @@ -0,0 +1,88 @@ +''' +Convert ArduinoBLE debug files into Btsnoop files ready to be analyzed using wireshark or hcidump +Btsnoop file format reference + https://www.fte.com/WebHelpII/Sodera/Content/Technical_Information/BT_Snoop_File_Format.htm +''' + +import os +import argparse + +DEBUG = False + +parser = argparse.ArgumentParser() +parser.add_argument('-i', dest='inputPath', type=str, required=True, help='input file containing debug log') +parser.add_argument('-o', dest='outputPath', type=str, required=True, help='result file that will contain the btsnoop encoded debug file') +args = parser.parse_args() + +# Extract only hci debug messages +def extractHCIDebugPrint(inputPath, outputPath): + inputFile = open(inputPath, 'r') + outputFile = open(outputPath, 'w') + for inputLine in inputFile: + lineItems = inputLine.split() + if (len(lineItems) < 7) or (lineItems[1] != "->") or (lineItems[2] != "HCI"): + if (len(lineItems) < 4) or (lineItems[0] != "HCI") or ((lineItems[3] != "<-") and (lineItems[3] != "->")): + continue + outputFile.write(inputLine) + outputFile.close() + +# Return packet in btsnoop format +def buildBinaryPacket(hciMessage, hciDirection, hciType): + commandFlag = 1 if (hciType == "COMMAND" or hciType == "EVENT") else 0 + directionFlag = 0 if (hciDirection == "TX") else 1 + flagHex = ("0" * 7) + str((commandFlag * 2) + directionFlag) + timestampHex = "0" * 16 + packetDropHex = "0" * 8 + dataLengthHex = format( (len(hciMessage) / 2), 'x') + packetLengthHex = ("0" * (8 - len(dataLengthHex))) + dataLengthHex + binaryPacket = bytearray.fromhex(packetLengthHex + packetLengthHex + flagHex + packetDropHex + timestampHex + hciMessage) + if DEBUG: + print(binaryPacket) + print + print(len(hciMessage)) + print(dataLengthHex) + print(packetLengthHex) + print (flagHex) + print + print(binaryPacket) + return binaryPacket + +def buildBinaryHeader(): + defaultHeader = "6274736e6f6f700000000001000003ea" + binaryHeader = bytearray.fromhex(defaultHeader) + return binaryHeader; + +def convertToBtsnoop(inputPath, outputPath): + # Open output file and write the Btsnoop header + outputFile = open(outputPath,'wb') + header = buildBinaryHeader() + outputFile.write(header) + + # Open input file containing HCI debug packets + inputFile = open(inputPath, 'r') + for inputLine in inputFile: + lineItems = inputLine.split() + # For a safer script, do not use indexes but look for symbols in the line + baseIndex = lineItems.index("HCI") + hciMessage = lineItems[baseIndex + 4] + hciDirection = lineItems[baseIndex + 2] + hciType = lineItems[baseIndex + 1] + # Build and write the encoded line + btsnoopPacket = buildBinaryPacket(hciMessage, hciDirection, hciType) + outputFile.write(btsnoopPacket) + if DEBUG: + print(hciDirection) + print(hciMessage) + print(hciType) + print() + outputFile.close() + +inputPath = args.inputPath +outputPath = args.outputPath +tempFile = "temp-debug-print.txt" +# Run +extractHCIDebugPrint(inputPath,tempFile) +convertToBtsnoop(tempFile, outputPath) +# Delete temp file +os.remove(tempFile) + From 37e1f0ea228d0dc5cecb5d998b7b28f31ba0e44b Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Fri, 6 Nov 2020 15:08:52 +0100 Subject: [PATCH 2/2] debug script - Fix python3 compatibility --- extras/arduino-ble-parser.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/extras/arduino-ble-parser.py b/extras/arduino-ble-parser.py index a8a12ad1..8f678711 100644 --- a/extras/arduino-ble-parser.py +++ b/extras/arduino-ble-parser.py @@ -33,24 +33,21 @@ def buildBinaryPacket(hciMessage, hciDirection, hciType): flagHex = ("0" * 7) + str((commandFlag * 2) + directionFlag) timestampHex = "0" * 16 packetDropHex = "0" * 8 - dataLengthHex = format( (len(hciMessage) / 2), 'x') + dataLengthHex = format( (int(len(hciMessage) / 2)), 'x') packetLengthHex = ("0" * (8 - len(dataLengthHex))) + dataLengthHex binaryPacket = bytearray.fromhex(packetLengthHex + packetLengthHex + flagHex + packetDropHex + timestampHex + hciMessage) if DEBUG: - print(binaryPacket) - print print(len(hciMessage)) print(dataLengthHex) print(packetLengthHex) - print (flagHex) - print - print(binaryPacket) + print(flagHex) + print('\n') return binaryPacket def buildBinaryHeader(): defaultHeader = "6274736e6f6f700000000001000003ea" binaryHeader = bytearray.fromhex(defaultHeader) - return binaryHeader; + return binaryHeader def convertToBtsnoop(inputPath, outputPath): # Open output file and write the Btsnoop header @@ -74,7 +71,7 @@ def convertToBtsnoop(inputPath, outputPath): print(hciDirection) print(hciMessage) print(hciType) - print() + print('\n') outputFile.close() inputPath = args.inputPath