SWIG experiments

Squash of the following commits (a full rebase was too hard :( )
* ef309cfdf N - SWIG: create pm3.c (5 months ago)  <Philippe Teuwen>
* 05ba6a73f N - swig pm3_device -> pm3 (5 months ago)  <Philippe Teuwen>
* d59630183 N - swig: move pm3.i (5 months ago)  <Philippe Teuwen>
* 6adcfad75 N - Fix historic make & cmake (5 months ago)  <Philippe Teuwen>
* bcbef2edf N - Fix typo with script_embedded (5 months ago)  <slurdge>
* 538ee4dab N - Better SWIG integration: autogen func & attributes (5 months ago)  <slurdge>
* 756b62466 N - SWIG: few helper scripts (5 months ago)  <Philippe Teuwen>
* 47ba4acd0 N - SWIG %extend (5 months ago)  <Philippe Teuwen>
* 21841cb9d N - simplify scripts (5 months ago)  <Philippe Teuwen>
* 78c4f7929 N - Revert "SWIG experiments: introduce context" (5 months ago)  <Philippe Teuwen>
* 1562b75fc N - SWIG experiments: introduce context (5 months ago)  <Philippe Teuwen>
* a503dfcd9 N - SWIG experiments (5 months ago)  <Philippe Teuwen>
This commit is contained in:
Philippe Teuwen
2020-05-28 02:13:21 +02:00
parent e959d0e229
commit eadacd82f5
42 changed files with 8542 additions and 34 deletions

View File

@@ -666,15 +666,15 @@ static int CmdConnect(const char *Cmd) {
}
if (session.pm3_present) {
CloseProxmark();
CloseProxmark(session.current_device);
}
// 10 second timeout
OpenProxmark(port, false, 10, false, baudrate);
OpenProxmark(&session.current_device, port, false, 10, false, baudrate);
if (session.pm3_present && (TestProxmark() != PM3_SUCCESS)) {
if (session.pm3_present && (TestProxmark(session.current_device) != PM3_SUCCESS)) {
PrintAndLogEx(ERR, _RED_("ERROR:") " cannot communicate with the Proxmark3\n");
CloseProxmark();
CloseProxmark(session.current_device);
return PM3_ENOTTY;
}
return PM3_SUCCESS;

View File

@@ -9,7 +9,6 @@
//-----------------------------------------------------------------------------
// 2020, added Python support (@iceman1001)
#include <stdlib.h>
#include <string.h>
@@ -32,7 +31,15 @@
#include "ui.h"
#include "fileutils.h"
#ifdef HAVE_LUA_SWIG
extern int luaopen_pm3(lua_State* L);
#endif
#ifdef HAVE_PYTHON
#ifdef HAVE_PYTHON_SWIG
extern PyObject* PyInit__pm3(void);
#endif // HAVE_PYTHON_SWIG
// Partly ripped from PyRun_SimpleFileExFlags
// but does not terminate client on sys.exit
// and print exit code only if != 0
@@ -274,7 +281,9 @@ static int CmdScriptRun(const char *Cmd) {
//Add the 'bit' library
set_bit_library(lua_state);
#ifdef HAVE_LUA_SWIG
luaL_requiref(lua_state, "pm3", luaopen_pm3, 1);
#endif
error = luaL_loadfile(lua_state, script_path);
free(script_path);
if (!error) {
@@ -355,6 +364,10 @@ static int CmdScriptRun(const char *Cmd) {
// optional but recommended
Py_SetProgramName(program);
#ifdef HAVE_PYTHON_SWIG
// hook Proxmark3 API
PyImport_AppendInittab("_pm3", PyInit__pm3);
#endif
Py_Initialize();
//int argc, char ** argv

View File

@@ -14,6 +14,7 @@
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "uart/uart.h"
#include "ui.h"
@@ -541,7 +542,7 @@ bool IsCommunicationThreadDead(void) {
return ret;
}
bool OpenProxmark(char *port, bool wait_for_port, int timeout, bool flash_mode, uint32_t speed) {
bool OpenProxmark(pm3_device **dev, char *port, bool wait_for_port, int timeout, bool flash_mode, uint32_t speed) {
if (!wait_for_port) {
PrintAndLogEx(INFO, "Using UART port " _YELLOW_("%s"), port);
@@ -588,15 +589,17 @@ bool OpenProxmark(char *port, bool wait_for_port, int timeout, bool flash_mode,
pthread_create(&communication_thread, NULL, &uart_communication, &conn);
__atomic_clear(&comm_thread_dead, __ATOMIC_SEQ_CST);
session.pm3_present = true;
session.pm3_present = true; // TODO support for multiple devices
fflush(stdout);
*dev = malloc(sizeof(pm3_device));
(*dev)->conn = &conn; // TODO conn shouldn't be global
return true;
}
}
// check if we can communicate with Pm3
int TestProxmark(void) {
int TestProxmark(pm3_device *dev) {
PacketResponseNG resp;
uint16_t len = 32;
@@ -659,8 +662,8 @@ int TestProxmark(void) {
return PM3_SUCCESS;
}
void CloseProxmark(void) {
conn.run = false;
void CloseProxmark(pm3_device *dev) {
dev->conn->run = false;
#ifdef __BIONIC__
if (communication_thread != 0) {

View File

@@ -65,6 +65,12 @@ typedef struct {
extern communication_arg_t conn;
typedef struct pm3_device pm3_device;
struct pm3_device {
communication_arg_t *conn;
int script_embedded;
};
void *uart_receiver(void *targ);
void SendCommandBL(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void *data, size_t len);
void SendCommandOLD(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void *data, size_t len);
@@ -74,9 +80,10 @@ void clearCommandBuffer(void);
#define FLASHMODE_SPEED 460800
bool IsCommunicationThreadDead(void);
bool OpenProxmark(char *port, bool wait_for_port, int timeout, bool flash_mode, uint32_t speed);
int TestProxmark(void);
void CloseProxmark(void);
typedef struct pm3_device pm3_device;
bool OpenProxmark(pm3_device **dev, char *port, bool wait_for_port, int timeout, bool flash_mode, uint32_t speed);
int TestProxmark(pm3_device *dev);
void CloseProxmark(pm3_device *dev);
bool WaitForResponseTimeoutW(uint32_t cmd, PacketResponseNG *response, size_t ms_timeout, bool show_warning);
bool WaitForResponseTimeout(uint32_t cmd, PacketResponseNG *response, size_t ms_timeout);

View File

@@ -373,11 +373,11 @@ static int enter_bootloader(char *serial_port_name) {
PrintAndLogEx(SUCCESS, "Press and hold down button NOW if your bootloader requires it.");
}
msleep(100);
CloseProxmark();
CloseProxmark(session.current_device);
// Let time to OS to make the port disappear
msleep(1000);
if (OpenProxmark(serial_port_name, true, 60, true, FLASHMODE_SPEED)) {
if (OpenProxmark(&session.current_device, serial_port_name, true, 60, true, FLASHMODE_SPEED)) {
PrintAndLogEx(NORMAL, _GREEN_(" found"));
return PM3_SUCCESS;
} else {

55
client/src/pm3.c Normal file
View File

@@ -0,0 +1,55 @@
//-----------------------------------------------------------------------------
// User API
//-----------------------------------------------------------------------------
#include "pm3.h"
#include <stdlib.h>
#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;
}

39
client/src/pm3.i Normal file
View File

@@ -0,0 +1,39 @@
%module pm3
%{
/* Include the header in the wrapper code */
#include "pm3.h"
#include "comms.h"
%}
/* Strip "pm3_" from API functions for SWIG */
%rename("%(strip:[pm3_])s") "";
%feature("immutable","1") pm3_current_dev;
typedef struct {
%extend {
pm3() {
printf("SWIG pm3 constructor, get current pm3\n");
pm3_device * p = pm3_get_current_dev();
p->script_embedded = 1;
return p;
}
pm3(char *port) {
printf("SWIG pm3 constructor with port, open pm3\n");
pm3_device * p = pm3_open(port);
p->script_embedded = 0;
return p;
}
~pm3() {
if ($self->script_embedded) {
printf("SWIG pm3 destructor, nothing to do\n");
} else {
printf("SWIG pm3 destructor, close pm3\n");
pm3_close($self);
}
}
int console(char *cmd);
char const * const name;
}
} pm3;
//%nodefaultctor device;
//%nodefaultdtor device;
/* Parse the header file to generate wrappers */

80
client/src/pm3.py Normal file
View File

@@ -0,0 +1,80 @@
# This file was automatically generated by SWIG (http://www.swig.org).
# Version 4.0.1
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.
from sys import version_info as _swig_python_version_info
if _swig_python_version_info < (2, 7, 0):
raise RuntimeError("Python 2.7 or later required")
# Import the low-level C/C++ module
if __package__ or "." in __name__:
from . import _pm3
else:
import _pm3
try:
import builtins as __builtin__
except ImportError:
import __builtin__
def _swig_repr(self):
try:
strthis = "proxy of " + self.this.__repr__()
except __builtin__.Exception:
strthis = ""
return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,)
def _swig_setattr_nondynamic_instance_variable(set):
def set_instance_attr(self, name, value):
if name == "thisown":
self.this.own(value)
elif name == "this":
set(self, name, value)
elif hasattr(self, name) and isinstance(getattr(type(self), name), property):
set(self, name, value)
else:
raise AttributeError("You cannot add instance attributes to %s" % self)
return set_instance_attr
def _swig_setattr_nondynamic_class_variable(set):
def set_class_attr(cls, name, value):
if hasattr(cls, name) and not isinstance(getattr(cls, name), property):
set(cls, name, value)
else:
raise AttributeError("You cannot add class attributes to %s" % cls)
return set_class_attr
def _swig_add_metaclass(metaclass):
"""Class decorator for adding a metaclass to a SWIG wrapped class - a slimmed down version of six.add_metaclass"""
def wrapper(cls):
return metaclass(cls.__name__, cls.__bases__, cls.__dict__.copy())
return wrapper
class _SwigNonDynamicMeta(type):
"""Meta class to enforce nondynamic attributes (no new attributes) for a class"""
__setattr__ = _swig_setattr_nondynamic_class_variable(type.__setattr__)
class pm3(object):
thisown = property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc="The membership flag")
__repr__ = _swig_repr
def __init__(self, *args):
_pm3.pm3_swiginit(self, _pm3.new_pm3(*args))
__swig_destroy__ = _pm3.delete_pm3
def console(self, cmd):
return _pm3.pm3_console(self, cmd)
name = property(_pm3.pm3_name_get)
# Register pm3 in _pm3:
_pm3.pm3_swigregister(pm3)

3259
client/src/pm3_luawrap.c Normal file

File diff suppressed because it is too large Load Diff

3781
client/src/pm3_pywrap.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -34,6 +34,7 @@
#include "flash.h"
#include "preferences.h"
#ifndef LIBPM3
#define BANNERMSG1 " Iceman :coffee:"
#define BANNERMSG2 " :snowflake: bleeding edge"
#define BANNERMSG3 " https://github.com/rfidresearchgroup/proxmark3/"
@@ -113,6 +114,7 @@ static void showBanner(void) {
fflush(stdout);
g_printAndLog = PRINTANDLOG_PRINT | PRINTANDLOG_LOG;
}
#endif //LIBPM3
static const char *prompt_dev = "";
static const char *prompt_ctx = "";
@@ -134,7 +136,7 @@ static int check_comm(void) {
rl_set_prompt(prompt_filtered);
rl_forced_update_display();
#endif
CloseProxmark();
CloseProxmark(session.current_device);
}
msleep(10);
return 0;
@@ -452,6 +454,7 @@ check_script:
}
}
#ifndef LIBPM3
static void dumpAllHelp(int markdown) {
session.help_dump_mode = true;
PrintAndLogEx(NORMAL, "\n%sProxmark3 command dump%s\n\n", markdown ? "# " : "", markdown ? "" : "\n======================");
@@ -462,6 +465,7 @@ static void dumpAllHelp(int markdown) {
dumpCommandsRecursive(cmds, markdown);
session.help_dump_mode = false;
}
#endif //LIBPM3
static char *my_executable_path = NULL;
static char *my_executable_directory = NULL;
@@ -546,6 +550,7 @@ static void set_my_user_directory(void) {
}
}
#ifndef LIBPM3
static void show_help(bool showFullHelp, char *exec_name) {
PrintAndLogEx(NORMAL, "\nsyntax: %s [-h|-t|-m]", exec_name);
@@ -631,7 +636,7 @@ static int flash_pm3(char *serial_port_name, uint8_t num_files, char *filenames[
PrintAndLogEx(SUCCESS, " "_YELLOW_("%s"), filepaths[i]);
}
if (OpenProxmark(serial_port_name, true, 60, true, FLASHMODE_SPEED)) {
if (OpenProxmark(&session.current_device, serial_port_name, true, 60, true, FLASHMODE_SPEED)) {
PrintAndLogEx(NORMAL, _GREEN_(" found"));
} else {
PrintAndLogEx(ERR, "Could not find Proxmark3 on " _RED_("%s") ".\n", serial_port_name);
@@ -669,7 +674,7 @@ static int flash_pm3(char *serial_port_name, uint8_t num_files, char *filenames[
finish:
ret = flash_stop_flashing();
CloseProxmark();
CloseProxmark(session.current_device);
finish2:
for (int i = 0 ; i < num_files; ++i) {
if (filepaths[i] != NULL)
@@ -682,6 +687,7 @@ finish2:
PrintAndLogEx(NORMAL, "\nHave a nice day!");
return ret;
}
#endif //LIBPM3
#if defined(_WIN32)
static bool DetectWindowsAnsiSupport(void) {
@@ -696,14 +702,28 @@ static bool DetectWindowsAnsiSupport(void) {
return SetConsoleMode(hOut, dwMode) ? true : false;
}
#endif
#endif //_WIN32
int main(int argc, char *argv[]) {
void pm3_init(void) {
srand(time(0));
session.pm3_present = false;
session.help_dump_mode = false;
session.incognito = false;
session.supports_colors = false;
session.emoji_mode = ALTTEXT;
session.stdinOnTTY = false;
session.stdoutOnTTY = false;
// set global variables soon enough to get the log path
set_my_executable_path();
set_my_user_directory();
}
#ifndef LIBPM3
int main(int argc, char *argv[]) {
pm3_init();
bool waitCOMPort = false;
bool addLuaExec = false;
bool stayInCommandLoop = false;
@@ -737,10 +757,6 @@ int main(int argc, char *argv[]) {
int flash_num_files = 0;
char *flash_filenames[FLASH_MAX_FILES];
// set global variables soon enough to get the log path
set_my_executable_path();
set_my_user_directory();
// color management:
// 1. default = no color
// 2. enable colors if OS seems to support colors and if stdin/stdout aren't redirected
@@ -999,12 +1015,12 @@ int main(int argc, char *argv[]) {
// try to open USB connection to Proxmark
if (port != NULL) {
OpenProxmark(port, waitCOMPort, 20, false, speed);
OpenProxmark(&session.current_device, port, waitCOMPort, 20, false, speed);
}
if (session.pm3_present && (TestProxmark() != PM3_SUCCESS)) {
if (session.pm3_present && (TestProxmark(session.current_device) != PM3_SUCCESS)) {
PrintAndLogEx(ERR, _RED_("ERROR:") " cannot communicate with the Proxmark\n");
CloseProxmark();
CloseProxmark(session.current_device);
}
if ((port != NULL) && (!session.pm3_present))
@@ -1060,10 +1076,11 @@ int main(int argc, char *argv[]) {
// Clean up the port
if (session.pm3_present) {
CloseProxmark();
CloseProxmark(session.current_device);
}
if (session.window_changed) // Plot/Overlay moved or resized
preferences_save();
exit(EXIT_SUCCESS);
}
#endif //LIBPM3

View File

@@ -49,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

View File

@@ -13,6 +13,7 @@
#include <pthread.h>
#include "common.h"
#include "comms.h"
#include "ansi.h"
#ifdef __cplusplus
@@ -46,6 +47,7 @@ typedef struct {
clientdebugLevel_t client_debug_level;
// uint8_t device_debug_level;
char *history_path;
pm3_device *current_device;
} session_arg_t;
extern session_arg_t session;