Linux: add native support for Bluetooth in client

This commit is contained in:
Philippe Teuwen
2020-04-29 01:41:22 +02:00
parent 73e6f0d07e
commit 7ca7677568
8 changed files with 151 additions and 23 deletions

View File

@@ -35,6 +35,9 @@ foreach(_qt_package IN LISTS QT_PACKAGELIST)
endif(NOT ${_qt_package}_FOUND)
endforeach()
find_package(PkgConfig)
pkg_search_module(BLUEZ QUIET bluez)
SET (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
add_subdirectory(deps)
@@ -208,6 +211,12 @@ else (Qt5_FOUND)
${TARGET_SOURCES})
endif (Qt5_FOUND)
if (BLUEZ_FOUND)
message("Bluez library found, building native Bluetooth support :)")
add_definitions("-DHAVE_BLUEZ")
set(ADDITIONAL_LNK bluetooth ${ADDITIONAL_LNK})
endif (BLUEZ_FOUND)
add_executable(
proxmark3
${TARGET_SOURCES}

View File

@@ -90,6 +90,14 @@ else
endif
endif
ifneq ($(SKIPBT),1)
BTLDLIBS = $(shell $(PKG_CONFIG_ENV) pkg-config --libs bluez 2>/dev/null)
endif
ifneq ($(BTLDLIBS),)
PM3CFLAGS += -DHAVE_BLUEZ
endif
ifneq ($(SKIPQT),1)
# Check for correctly configured Qt5
QTINCLUDES = $(shell $(PKG_CONFIG_ENV) pkg-config --cflags Qt5Core Qt5Widgets 2>/dev/null)
@@ -132,6 +140,24 @@ else
QTGUIOBJS = $(OBJDIR)/guidummy.o
endif
$(info ===================================================================)
$(info Client platform: $(platform))
ifeq ($(SKIPQT),1)
$(info GUI support: skipped)
else ifneq ($(QTLDLIBS),)
$(info GUI support: QT found, enabled)
else
$(info GUI support: QT not found, disabled)
endif
ifeq ($(SKIPBT),1)
$(info native BT support: skipped)
else ifneq ($(BTLDLIBS),)
$(info native BT support: Bluez found, enabled)
else
$(info native BT support: Bluez not found, disabled)
endif
$(info ===================================================================)
# Flags to generate temporary dependency files
DEPFLAGS = -MT $@ -MMD -MP -MF $(OBJDIR)/$*.Td
# make temporary to final dependency files after successful compilation
@@ -286,7 +312,7 @@ all: $(BINS)
all-static: LDLIBS:=-static $(LDLIBS)
all-static: $(BINS)
proxmark3: LDLIBS+=$(LUALIB) $(JANSSONLIB) $(MBEDTLSLIB) $(CBORLIB) $(ZLIB) $(REVENGLIB) $(AMIIBOLIB) $(HARDNESTEDLIB) $(CLIPARSERLIB) $(QTLDLIBS)
proxmark3: LDLIBS+=$(LUALIB) $(JANSSONLIB) $(MBEDTLSLIB) $(CBORLIB) $(ZLIB) $(REVENGLIB) $(AMIIBOLIB) $(HARDNESTEDLIB) $(CLIPARSERLIB) $(BTLDLIBS) $(QTLDLIBS)
proxmark3: $(OBJDIR)/proxmark3.o $(COREOBJS) $(CMDOBJS) $(OBJCOBJS) $(QTGUIOBJS) $(LUALIB) $(JANSSONLIB) $(CBORLIB) $(REVENGLIB) $(MBEDTLSLIB) $(ZLIB) $(AMIIBOLIB) $(HARDNESTEDLIB) $(CLIPARSERLIB) lualibs/pm3_cmd.lua lualibs/mfc_default_keys.lua
$(info [=] LD $@)
$(Q)$(LD) $(LDFLAGS) $(OBJDIR)/proxmark3.o $(COREOBJS) $(CMDOBJS) $(OBJCOBJS) $(QTGUIOBJS) $(LDLIBS) -o $@

View File

@@ -639,9 +639,10 @@ int TestProxmark(void) {
conn.send_via_fpc_usart = pm3_capabilities.via_fpc;
conn.uart_speed = pm3_capabilities.baudrate;
PrintAndLogEx(INFO, "Communicating with PM3 over %s%s",
PrintAndLogEx(INFO, "Communicating with PM3 over %s%s%s",
conn.send_via_fpc_usart ? _YELLOW_("FPC UART") : _YELLOW_("USB-CDC"),
memcmp(conn.serial_port_name, "tcp:", 4) == 0 ? " over " _YELLOW_("TCP") : "");
memcmp(conn.serial_port_name, "tcp:", 4) == 0 ? " over " _YELLOW_("TCP") : "",
memcmp(conn.serial_port_name, "bt:", 3) == 0 ? " over " _YELLOW_("BT") : "");
if (conn.send_via_fpc_usart) {
PrintAndLogEx(INFO, "PM3 UART serial baudrate: " _YELLOW_("%u") "\n", conn.uart_speed);

View File

@@ -48,10 +48,16 @@
#include <fcntl.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include "sys/socket.h"
#include "sys/un.h"
#include <sys/socket.h>
#include <sys/un.h>
#ifdef HAVE_BLUEZ
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#endif
#include "comms.h"
#include "ui.h"
// Taken from https://github.com/unbit/uwsgi/commit/b608eb1772641d525bfde268fe9d6d8d0d5efde7
#ifndef SOL_TCP
@@ -158,6 +164,52 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) {
return sp;
}
if (memcmp(pcPortName, "bt:", 3) == 0) {
#ifdef HAVE_BLUEZ
if (strlen(pcPortName) != 20) {
free(sp);
return INVALID_SERIAL_PORT;
}
char *addrstr = strndup(pcPortName + 3, 17);
if (addrstr == NULL) {
printf("Error: malloc\n");
free(sp);
return INVALID_SERIAL_PORT;
}
struct sockaddr_rc addr = { 0 };
addr.rc_family = AF_BLUETOOTH;
addr.rc_channel = (uint8_t) 1;
if (str2ba(addrstr, &addr.rc_bdaddr) != 0) {
PrintAndLogEx(ERR, "Invalid Bluetooth MAC address " _RED_("%s"), addrstr);
free(addrstr);
free(sp);
return INVALID_SERIAL_PORT;
}
int sfd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if (sfd == -1) {
PrintAndLogEx(ERR, "Error opening Bluetooth socket");
free(addrstr);
free(sp);
return INVALID_SERIAL_PORT;
}
if (connect(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
PrintAndLogEx(ERR, "Error: cannot connect device " _YELLOW_("%s") " over Bluetooth", addrstr);
close(sfd);
free(addrstr);
free(sp);
return INVALID_SERIAL_PORT;
}
sp->fd = sfd;
return sp;
#else // HAVE_BLUEZ
PrintAndLogEx(ERR, "Sorry, this client doesn't support native Bluetooth addresses");
free(sp);
return INVALID_SERIAL_PORT;
#endif // HAVE_BLUEZ
}
// The socket for abstract namespace implement.
// Is local socket buffer, not a TCP or any net connection!
// so, you can't connect with address like: 127.0.0.1, or any IP