From ef309cfdf7faf2e87f376633b6fdc60dad041289 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sun, 14 Jun 2020 22:58:43 +0200 Subject: [PATCH] SWIG: create pm3.c --- client/CMakeLists.txt | 1 + client/Makefile | 1 + client/client_with_swig/CMakeLists.txt | 1 + client/lib/CMakeLists.txt | 1 + client/src/pm3.c | 55 ++++++++++++++++++++++++++ client/src/proxmark3.c | 52 +----------------------- client/src/proxmark3.h | 2 +- 7 files changed, 62 insertions(+), 51 deletions(-) create mode 100644 client/src/pm3.c diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 1d4797733..f5970ea45 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -251,6 +251,7 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/graph.c ${PM3_ROOT}/client/src/jansson_path.c ${PM3_ROOT}/client/src/preferences.c + ${PM3_ROOT}/client/src/pm3.c ${PM3_ROOT}/client/src/pm3_binlib.c ${PM3_ROOT}/client/src/pm3_bitlib.c ${PM3_ROOT}/client/src/prng.c diff --git a/client/Makefile b/client/Makefile index a52da9c50..0ce9a3e1d 100644 --- a/client/Makefile +++ b/client/Makefile @@ -512,6 +512,7 @@ SRCS = aidsearch.c \ mifare/mifaredefault.c \ mifare/mifarehost.c \ mifare/ndef.c \ + pm3.c \ pm3_binlib.c \ pm3_bitlib.c \ preferences.c \ diff --git a/client/client_with_swig/CMakeLists.txt b/client/client_with_swig/CMakeLists.txt index ac10a6d90..b74cee0c8 100644 --- a/client/client_with_swig/CMakeLists.txt +++ b/client/client_with_swig/CMakeLists.txt @@ -251,6 +251,7 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/graph.c ${PM3_ROOT}/client/src/jansson_path.c ${PM3_ROOT}/client/src/preferences.c + ${PM3_ROOT}/client/src/pm3.c ${PM3_ROOT}/client/src/pm3_binlib.c ${PM3_ROOT}/client/src/pm3_bitlib.c ${PM3_ROOT}/client/src/prng.c diff --git a/client/lib/CMakeLists.txt b/client/lib/CMakeLists.txt index fc081569f..755014422 100644 --- a/client/lib/CMakeLists.txt +++ b/client/lib/CMakeLists.txt @@ -251,6 +251,7 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/graph.c ${PM3_ROOT}/client/src/jansson_path.c ${PM3_ROOT}/client/src/preferences.c + ${PM3_ROOT}/client/src/pm3.c ${PM3_ROOT}/client/src/pm3_binlib.c ${PM3_ROOT}/client/src/pm3_bitlib.c ${PM3_ROOT}/client/src/prng.c diff --git a/client/src/pm3.c b/client/src/pm3.c new file mode 100644 index 000000000..14f0b00ad --- /dev/null +++ b/client/src/pm3.c @@ -0,0 +1,55 @@ +//----------------------------------------------------------------------------- +// User API +//----------------------------------------------------------------------------- + +#include "pm3.h" + +#include + +#include "proxmark3.h" +#include "cmdmain.h" +#include "ui.h" +#include "usart_defs.h" +#include "util_posix.h" +#include "comms.h" + +pm3_device* pm3_open(char *port) { + pm3_init(); + OpenProxmark(&session.current_device, port, false, 20, false, USART_BAUD_RATE); + if (session.pm3_present && (TestProxmark(session.current_device) != PM3_SUCCESS)) { + PrintAndLogEx(ERR, _RED_("ERROR:") " cannot communicate with the Proxmark\n"); + CloseProxmark(session.current_device); + } + + if ((port != NULL) && (!session.pm3_present)) + exit(EXIT_FAILURE); + + if (!session.pm3_present) + PrintAndLogEx(INFO, "Running in " _YELLOW_("OFFLINE") " mode"); + // For now, there is no real device context: + return session.current_device; +} + +void pm3_close(pm3_device* dev) { + // Clean up the port + if (session.pm3_present) { + clearCommandBuffer(); + SendCommandNG(CMD_QUIT_SESSION, NULL, 0); + msleep(100); // Make sure command is sent before killing client + CloseProxmark(dev); + } +} + +int pm3_console(pm3_device* dev, char *Cmd) { + // For now, there is no real device context: + (void) dev; + return CommandReceived(Cmd); +} + +const char *pm3_name_get(pm3_device* dev) { + return dev->conn->serial_port_name; +} + +pm3_device* pm3_get_current_dev(void) { + return session.current_device; +} diff --git a/client/src/proxmark3.c b/client/src/proxmark3.c index c841f9ef5..a2d0da269 100644 --- a/client/src/proxmark3.c +++ b/client/src/proxmark3.c @@ -690,7 +690,7 @@ static bool DetectWindowsAnsiSupport(void) { } #endif -static void init(void) { +void pm3_init(void) { srand(time(0)); session.pm3_present = false; @@ -706,57 +706,9 @@ static void init(void) { } - -/* ======================================================= */ -/* user API */ - -pm3_device* pm3_open(char *port) { - init(); - OpenProxmark(&session.current_device, port, false, 20, false, USART_BAUD_RATE); - if (session.pm3_present && (TestProxmark(session.current_device) != PM3_SUCCESS)) { - PrintAndLogEx(ERR, _RED_("ERROR:") " cannot communicate with the Proxmark\n"); - CloseProxmark(session.current_device); - } - - if ((port != NULL) && (!session.pm3_present)) - exit(EXIT_FAILURE); - - if (!session.pm3_present) - PrintAndLogEx(INFO, "Running in " _YELLOW_("OFFLINE") " mode"); - // For now, there is no real device context: - return session.current_device; -} - -void pm3_close(pm3_device* dev) { - // For now, there is no real device context: - (void) dev; - // Clean up the port - if (session.pm3_present) { - clearCommandBuffer(); - SendCommandNG(CMD_QUIT_SESSION, NULL, 0); - msleep(100); // Make sure command is sent before killing client - CloseProxmark(dev); - } -} - -int pm3_console(pm3_device* dev, char *Cmd) { - // For now, there is no real device context: - (void) dev; - return CommandReceived(Cmd); -} - -const char *pm3_name_get(pm3_device* dev) { - return dev->conn->serial_port_name; -} - -pm3_device* pm3_get_current_dev(void) { - return session.current_device; -} -/* ======================================================= */ - #ifndef LIBPM3 int main(int argc, char *argv[]) { - init(); + pm3_init(); bool waitCOMPort = false; bool addLuaExec = false; bool stayInCommandLoop = false; diff --git a/client/src/proxmark3.h b/client/src/proxmark3.h index 5782ef08a..dc5bab2b9 100644 --- a/client/src/proxmark3.h +++ b/client/src/proxmark3.h @@ -14,7 +14,6 @@ #include #include "common.h" -#include "pm3.h" #define PROXPROMPT_MAX_SIZE 255 @@ -50,6 +49,7 @@ int push_cmdscriptfile(char *path, bool stayafter); const char *get_my_executable_path(void); const char *get_my_executable_directory(void); const char *get_my_user_directory(void); +void pm3_init(void); void main_loop(char *script_cmds_file, char *script_cmd, bool stayInCommandLoop); #ifdef __cplusplus