From 1562b75fc9bb8d43041dcc58ef2324233004d42f Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 13 Jun 2020 01:50:10 +0200 Subject: [PATCH] SWIG experiments: introduce context --- client/client_with_swig/testembedded.lua | 3 +- client/client_with_swig/testembedded.py | 13 +- client/include/pm3.h | 12 +- client/lib/example_c/test.c | 5 +- client/lib/example_lua/test.lua | 4 +- client/lib/example_py/test.py | 4 +- client/src/pm3.py | 19 +- client/src/pm3_luawrap.c | 134 +++++++++-- client/src/pm3_pywrap.c | 293 ++++++++++++++++++++--- client/src/proxmark3.c | 24 +- client/src/proxmark3.h | 12 +- 11 files changed, 435 insertions(+), 88 deletions(-) diff --git a/client/client_with_swig/testembedded.lua b/client/client_with_swig/testembedded.lua index 8d007c390..dbf49da48 100755 --- a/client/client_with_swig/testembedded.lua +++ b/client/client_with_swig/testembedded.lua @@ -1,3 +1,4 @@ local pm3 = require("pm3") -p=pm3.get_current_dev() +ctx=pm3.get_current_context() +p=pm3.get_dev(ctx, 0) pm3.console(p, "hw status") diff --git a/client/client_with_swig/testembedded.py b/client/client_with_swig/testembedded.py index acdf49859..e008a0c84 100755 --- a/client/client_with_swig/testembedded.py +++ b/client/client_with_swig/testembedded.py @@ -1,12 +1,5 @@ -import sys import pm3 -def main(): - print('Hello world') - #p=pm3.open("/dev/ttyACM0") - p=pm3.get_current_dev() - pm3.console(p, "hw status") - #pm3.close(p) - -if __name__ == "__main__": - main() +ctx=pm3.get_current_context() +p=pm3.get_dev(ctx, 0) +pm3.console(p, "hw status") diff --git a/client/include/pm3.h b/client/include/pm3.h index ff635f239..8e227c46a 100644 --- a/client/include/pm3.h +++ b/client/include/pm3.h @@ -15,9 +15,13 @@ /* Parse the header file to generate wrappers */ #endif +typedef struct pm3_context pm3_context; +pm3_context *pm3_init(void); +void pm3_exit(pm3_context *ctx); +pm3_context *pm3_get_current_context(void); typedef struct pm3_device pm3_device; -pm3_device* pm3_open(char *port); -int pm3_console(pm3_device* dev, char *cmd); -void pm3_close(pm3_device* dev); -pm3_device* pm3_get_current_dev(void); +pm3_device *pm3_open(pm3_context *ctx, char *port); +pm3_device *pm3_get_dev(pm3_context *ctx, int n); +int pm3_console(pm3_device *dev, char *cmd); +void pm3_close(pm3_device *dev); #endif diff --git a/client/lib/example_c/test.c b/client/lib/example_c/test.c index 392bd9608..c6a3d263b 100644 --- a/client/lib/example_c/test.c +++ b/client/lib/example_c/test.c @@ -1,8 +1,11 @@ #include "pm3.h" int main(int argc, char *argv[]) { + pm3_context *ctx; + ctx = pm3_init(); pm3_device *p; - p = pm3_open("/dev/ttyACM0"); + p = pm3_open(ctx, "/dev/ttyACM0"); pm3_console(p, "hw status"); pm3_close(p); + pm3_exit(ctx); } diff --git a/client/lib/example_lua/test.lua b/client/lib/example_lua/test.lua index 5a2c066f1..bcbae3877 100755 --- a/client/lib/example_lua/test.lua +++ b/client/lib/example_lua/test.lua @@ -1,4 +1,6 @@ local pm3 = require("pm3") -p=pm3.open("/dev/ttyACM0") +ctx=pm3.init() +p=pm3.open(ctx, "/dev/ttyACM0") pm3.console(p, "hw status") pm3.close(p) +pm3.exit(ctx) diff --git a/client/lib/example_py/test.py b/client/lib/example_py/test.py index 2932637f6..8df3104ec 100755 --- a/client/lib/example_py/test.py +++ b/client/lib/example_py/test.py @@ -2,6 +2,8 @@ import pm3 -p=pm3.open("/dev/ttyACM0") +ctx=pm3.init() +p=pm3.open(ctx, "/dev/ttyACM0") pm3.console(p, "hw status") pm3.close(p) +pm3.exit(ctx) diff --git a/client/src/pm3.py b/client/src/pm3.py index 8fb59c777..221673a1b 100644 --- a/client/src/pm3.py +++ b/client/src/pm3.py @@ -62,8 +62,20 @@ class _SwigNonDynamicMeta(type): -def open(port): - return _pm3.open(port) +def init(): + return _pm3.init() + +def exit(ctx): + return _pm3.exit(ctx) + +def get_current_context(): + return _pm3.get_current_context() + +def open(ctx, port): + return _pm3.open(ctx, port) + +def get_dev(ctx, n): + return _pm3.get_dev(ctx, n) def console(dev, cmd): return _pm3.console(dev, cmd) @@ -71,7 +83,4 @@ def console(dev, cmd): def close(dev): return _pm3.close(dev) -def get_current_dev(): - return _pm3.get_current_dev() - diff --git a/client/src/pm3_luawrap.c b/client/src/pm3_luawrap.c index 17c3c4376..4cb2d4911 100644 --- a/client/src/pm3_luawrap.c +++ b/client/src/pm3_luawrap.c @@ -2668,9 +2668,10 @@ SWIG_Lua_dostring(lua_State *L, const char *str) { /* -------- TYPES TABLE (BEGIN) -------- */ -#define SWIGTYPE_p_pm3_device swig_types[0] -static swig_type_info *swig_types[2]; -static swig_module_info swig_module = {swig_types, 1, 0, 0, 0, 0}; +#define SWIGTYPE_p_pm3_context swig_types[0] +#define SWIGTYPE_p_pm3_device swig_types[1] +static swig_type_info *swig_types[3]; +static swig_module_info swig_module = {swig_types, 2, 0, 0, 0, 0}; #define SWIG_TypeQuery(name) SWIG_TypeQueryModule(&swig_module, &swig_module, name) #define SWIG_MangledTypeQuery(name) SWIG_MangledTypeQueryModule(&swig_module, &swig_module, name) @@ -2696,15 +2697,106 @@ SWIGINTERN int SWIG_lua_isnilstring(lua_State *L, int idx) { #ifdef __cplusplus extern "C" { #endif +static int _wrap_init(lua_State* L) { + int SWIG_arg = 0; + pm3_context *result = 0 ; + + SWIG_check_num_args("pm3_init",0,0) + result = (pm3_context *)pm3_init(); + SWIG_NewPointerObj(L,result,SWIGTYPE_p_pm3_context,0); SWIG_arg++; + return SWIG_arg; + + if(0) SWIG_fail; + +fail: + lua_error(L); + return SWIG_arg; +} + + +static int _wrap_exit(lua_State* L) { + int SWIG_arg = 0; + pm3_context *arg1 = (pm3_context *) 0 ; + + SWIG_check_num_args("pm3_exit",1,1) + if(!SWIG_isptrtype(L,1)) SWIG_fail_arg("pm3_exit",1,"pm3_context *"); + + if (!SWIG_IsOK(SWIG_ConvertPtr(L,1,(void**)&arg1,SWIGTYPE_p_pm3_context,0))){ + SWIG_fail_ptr("exit",1,SWIGTYPE_p_pm3_context); + } + + pm3_exit(arg1); + + return SWIG_arg; + + if(0) SWIG_fail; + +fail: + lua_error(L); + return SWIG_arg; +} + + +static int _wrap_get_current_context(lua_State* L) { + int SWIG_arg = 0; + pm3_context *result = 0 ; + + SWIG_check_num_args("pm3_get_current_context",0,0) + result = (pm3_context *)pm3_get_current_context(); + SWIG_NewPointerObj(L,result,SWIGTYPE_p_pm3_context,0); SWIG_arg++; + return SWIG_arg; + + if(0) SWIG_fail; + +fail: + lua_error(L); + return SWIG_arg; +} + + static int _wrap_open(lua_State* L) { int SWIG_arg = 0; - char *arg1 = (char *) 0 ; + pm3_context *arg1 = (pm3_context *) 0 ; + char *arg2 = (char *) 0 ; pm3_device *result = 0 ; - SWIG_check_num_args("pm3_open",1,1) - if(!SWIG_lua_isnilstring(L,1)) SWIG_fail_arg("pm3_open",1,"char *"); - arg1 = (char *)lua_tostring(L, 1); - result = (pm3_device *)pm3_open(arg1); + SWIG_check_num_args("pm3_open",2,2) + if(!SWIG_isptrtype(L,1)) SWIG_fail_arg("pm3_open",1,"pm3_context *"); + if(!SWIG_lua_isnilstring(L,2)) SWIG_fail_arg("pm3_open",2,"char *"); + + if (!SWIG_IsOK(SWIG_ConvertPtr(L,1,(void**)&arg1,SWIGTYPE_p_pm3_context,0))){ + SWIG_fail_ptr("open",1,SWIGTYPE_p_pm3_context); + } + + arg2 = (char *)lua_tostring(L, 2); + result = (pm3_device *)pm3_open(arg1,arg2); + SWIG_NewPointerObj(L,result,SWIGTYPE_p_pm3_device,0); SWIG_arg++; + return SWIG_arg; + + if(0) SWIG_fail; + +fail: + lua_error(L); + return SWIG_arg; +} + + +static int _wrap_get_dev(lua_State* L) { + int SWIG_arg = 0; + pm3_context *arg1 = (pm3_context *) 0 ; + int arg2 ; + pm3_device *result = 0 ; + + SWIG_check_num_args("pm3_get_dev",2,2) + if(!SWIG_isptrtype(L,1)) SWIG_fail_arg("pm3_get_dev",1,"pm3_context *"); + if(!lua_isnumber(L,2)) SWIG_fail_arg("pm3_get_dev",2,"int"); + + if (!SWIG_IsOK(SWIG_ConvertPtr(L,1,(void**)&arg1,SWIGTYPE_p_pm3_context,0))){ + SWIG_fail_ptr("get_dev",1,SWIGTYPE_p_pm3_context); + } + + arg2 = (int)lua_tonumber(L, 2); + result = (pm3_device *)pm3_get_dev(arg1,arg2); SWIG_NewPointerObj(L,result,SWIGTYPE_p_pm3_device,0); SWIG_arg++; return SWIG_arg; @@ -2766,23 +2858,6 @@ fail: } -static int _wrap_get_current_dev(lua_State* L) { - int SWIG_arg = 0; - pm3_device *result = 0 ; - - SWIG_check_num_args("pm3_get_current_dev",0,0) - result = (pm3_device *)pm3_get_current_dev(); - SWIG_NewPointerObj(L,result,SWIGTYPE_p_pm3_device,0); SWIG_arg++; - return SWIG_arg; - - if(0) SWIG_fail; - -fail: - lua_error(L); - return SWIG_arg; -} - - static swig_lua_attribute swig_SwigModule_attributes[] = { {0,0,0} }; @@ -2790,10 +2865,13 @@ static swig_lua_const_info swig_SwigModule_constants[]= { {0,0,0,0,0,0} }; static swig_lua_method swig_SwigModule_methods[]= { + { "init", _wrap_init}, + { "exit", _wrap_exit}, + { "get_current_context", _wrap_get_current_context}, { "open", _wrap_open}, + { "get_dev", _wrap_get_dev}, { "console", _wrap_console}, { "close", _wrap_close}, - { "get_current_dev", _wrap_get_current_dev}, {0,0} }; static swig_lua_class* swig_SwigModule_classes[]= { @@ -2817,15 +2895,19 @@ static swig_lua_namespace swig_SwigModule = { /* -------- TYPE CONVERSION AND EQUIVALENCE RULES (BEGIN) -------- */ +static swig_type_info _swigt__p_pm3_context = {"_p_pm3_context", "struct pm3_context *|pm3_context *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_pm3_device = {"_p_pm3_device", "struct pm3_device *|pm3_device *", 0, 0, (void*)0, 0}; static swig_type_info *swig_type_initial[] = { + &_swigt__p_pm3_context, &_swigt__p_pm3_device, }; +static swig_cast_info _swigc__p_pm3_context[] = { {&_swigt__p_pm3_context, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info _swigc__p_pm3_device[] = { {&_swigt__p_pm3_device, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info *swig_cast_initial[] = { + _swigc__p_pm3_context, _swigc__p_pm3_device, }; diff --git a/client/src/pm3_pywrap.c b/client/src/pm3_pywrap.c index 47edc9c6a..7d6ee59bc 100644 --- a/client/src/pm3_pywrap.c +++ b/client/src/pm3_pywrap.c @@ -2634,9 +2634,10 @@ SWIGINTERN PyObject *SWIG_PyStaticMethod_New(PyObject *SWIGUNUSEDPARM(self), PyO /* -------- TYPES TABLE (BEGIN) -------- */ #define SWIGTYPE_p_char swig_types[0] -#define SWIGTYPE_p_pm3_device swig_types[1] -static swig_type_info *swig_types[3]; -static swig_module_info swig_module = {swig_types, 2, 0, 0, 0, 0}; +#define SWIGTYPE_p_pm3_context swig_types[1] +#define SWIGTYPE_p_pm3_device swig_types[2] +static swig_type_info *swig_types[4]; +static swig_module_info swig_module = {swig_types, 3, 0, 0, 0, 0}; #define SWIG_TypeQuery(name) SWIG_TypeQueryModule(&swig_module, &swig_module, name) #define SWIG_MangledTypeQuery(name) SWIG_MangledTypeQueryModule(&swig_module, &swig_module, name) @@ -2794,6 +2795,157 @@ SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc) +#include +#if !defined(SWIG_NO_LLONG_MAX) +# if !defined(LLONG_MAX) && defined(__GNUC__) && defined (__LONG_LONG_MAX__) +# define LLONG_MAX __LONG_LONG_MAX__ +# define LLONG_MIN (-LLONG_MAX - 1LL) +# define ULLONG_MAX (LLONG_MAX * 2ULL + 1ULL) +# endif +#endif + + +SWIGINTERN int +SWIG_AsVal_double (PyObject *obj, double *val) +{ + int res = SWIG_TypeError; + if (PyFloat_Check(obj)) { + if (val) *val = PyFloat_AsDouble(obj); + return SWIG_OK; +#if PY_VERSION_HEX < 0x03000000 + } else if (PyInt_Check(obj)) { + if (val) *val = (double) PyInt_AsLong(obj); + return SWIG_OK; +#endif + } else if (PyLong_Check(obj)) { + double v = PyLong_AsDouble(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_OK; + } else { + PyErr_Clear(); + } + } +#ifdef SWIG_PYTHON_CAST_MODE + { + int dispatch = 0; + double d = PyFloat_AsDouble(obj); + if (!PyErr_Occurred()) { + if (val) *val = d; + return SWIG_AddCast(SWIG_OK); + } else { + PyErr_Clear(); + } + if (!dispatch) { + long v = PyLong_AsLong(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_AddCast(SWIG_AddCast(SWIG_OK)); + } else { + PyErr_Clear(); + } + } + } +#endif + return res; +} + + +#include + + +#include + + +SWIGINTERNINLINE int +SWIG_CanCastAsInteger(double *d, double min, double max) { + double x = *d; + if ((min <= x && x <= max)) { + double fx = floor(x); + double cx = ceil(x); + double rd = ((x - fx) < 0.5) ? fx : cx; /* simple rint */ + if ((errno == EDOM) || (errno == ERANGE)) { + errno = 0; + } else { + double summ, reps, diff; + if (rd < x) { + diff = x - rd; + } else if (rd > x) { + diff = rd - x; + } else { + return 1; + } + summ = rd + x; + reps = diff/summ; + if (reps < 8*DBL_EPSILON) { + *d = rd; + return 1; + } + } + } + return 0; +} + + +SWIGINTERN int +SWIG_AsVal_long (PyObject *obj, long* val) +{ +#if PY_VERSION_HEX < 0x03000000 + if (PyInt_Check(obj)) { + if (val) *val = PyInt_AsLong(obj); + return SWIG_OK; + } else +#endif + if (PyLong_Check(obj)) { + long v = PyLong_AsLong(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_OK; + } else { + PyErr_Clear(); + return SWIG_OverflowError; + } + } +#ifdef SWIG_PYTHON_CAST_MODE + { + int dispatch = 0; + long v = PyInt_AsLong(obj); + if (!PyErr_Occurred()) { + if (val) *val = v; + return SWIG_AddCast(SWIG_OK); + } else { + PyErr_Clear(); + } + if (!dispatch) { + double d; + int res = SWIG_AddCast(SWIG_AsVal_double (obj,&d)); + if (SWIG_IsOK(res) && SWIG_CanCastAsInteger(&d, LONG_MIN, LONG_MAX)) { + if (val) *val = (long)(d); + return res; + } + } + } +#endif + return SWIG_TypeError; +} + + +SWIGINTERN int +SWIG_AsVal_int (PyObject * obj, int *val) +{ + long v; + int res = SWIG_AsVal_long (obj, &v); + if (SWIG_IsOK(res)) { + if ((v < INT_MIN || v > INT_MAX)) { + return SWIG_OverflowError; + } else { + if (val) *val = (int)(v); + } + } + return res; +} + + SWIGINTERNINLINE PyObject* SWIG_From_int (int value) { @@ -2803,28 +2955,113 @@ SWIGINTERNINLINE PyObject* #ifdef __cplusplus extern "C" { #endif -SWIGINTERN PyObject *_wrap_open(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { +SWIGINTERN PyObject *_wrap_init(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; - char *arg1 = (char *) 0 ; - int res1 ; - char *buf1 = 0 ; - int alloc1 = 0 ; + pm3_context *result = 0 ; + + if (!SWIG_Python_UnpackTuple(args, "init", 0, 0, 0)) SWIG_fail; + result = (pm3_context *)pm3_init(); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_pm3_context, 0 | 0 ); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_exit(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + pm3_context *arg1 = (pm3_context *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; PyObject *swig_obj[1] ; - pm3_device *result = 0 ; if (!args) SWIG_fail; swig_obj[0] = args; - res1 = SWIG_AsCharPtrAndSize(swig_obj[0], &buf1, NULL, &alloc1); + res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_pm3_context, 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "open" "', argument " "1"" of type '" "char *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "exit" "', argument " "1"" of type '" "pm3_context *""'"); } - arg1 = (char *)(buf1); - result = (pm3_device *)pm3_open(arg1); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_pm3_device, 0 | 0 ); - if (alloc1 == SWIG_NEWOBJ) free((char*)buf1); + arg1 = (pm3_context *)(argp1); + pm3_exit(arg1); + resultobj = SWIG_Py_Void(); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_get_current_context(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + pm3_context *result = 0 ; + + if (!SWIG_Python_UnpackTuple(args, "get_current_context", 0, 0, 0)) SWIG_fail; + result = (pm3_context *)pm3_get_current_context(); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_pm3_context, 0 | 0 ); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_open(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + pm3_context *arg1 = (pm3_context *) 0 ; + char *arg2 = (char *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + int res2 ; + char *buf2 = 0 ; + int alloc2 = 0 ; + PyObject *swig_obj[2] ; + pm3_device *result = 0 ; + + if (!SWIG_Python_UnpackTuple(args, "open", 2, 2, swig_obj)) SWIG_fail; + res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_pm3_context, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "open" "', argument " "1"" of type '" "pm3_context *""'"); + } + arg1 = (pm3_context *)(argp1); + res2 = SWIG_AsCharPtrAndSize(swig_obj[1], &buf2, NULL, &alloc2); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "open" "', argument " "2"" of type '" "char *""'"); + } + arg2 = (char *)(buf2); + result = (pm3_device *)pm3_open(arg1,arg2); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_pm3_device, 0 | 0 ); + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); + return resultobj; +fail: + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); + return NULL; +} + + +SWIGINTERN PyObject *_wrap_get_dev(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + pm3_context *arg1 = (pm3_context *) 0 ; + int arg2 ; + void *argp1 = 0 ; + int res1 = 0 ; + int val2 ; + int ecode2 = 0 ; + PyObject *swig_obj[2] ; + pm3_device *result = 0 ; + + if (!SWIG_Python_UnpackTuple(args, "get_dev", 2, 2, swig_obj)) SWIG_fail; + res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_pm3_context, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "get_dev" "', argument " "1"" of type '" "pm3_context *""'"); + } + arg1 = (pm3_context *)(argp1); + ecode2 = SWIG_AsVal_int(swig_obj[1], &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "get_dev" "', argument " "2"" of type '" "int""'"); + } + arg2 = (int)(val2); + result = (pm3_device *)pm3_get_dev(arg1,arg2); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_pm3_device, 0 | 0 ); return resultobj; fail: - if (alloc1 == SWIG_NEWOBJ) free((char*)buf1); return NULL; } @@ -2884,25 +3121,15 @@ fail: } -SWIGINTERN PyObject *_wrap_get_current_dev(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { - PyObject *resultobj = 0; - pm3_device *result = 0 ; - - if (!SWIG_Python_UnpackTuple(args, "get_current_dev", 0, 0, 0)) SWIG_fail; - result = (pm3_device *)pm3_get_current_dev(); - resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_pm3_device, 0 | 0 ); - return resultobj; -fail: - return NULL; -} - - static PyMethodDef SwigMethods[] = { { "SWIG_PyInstanceMethod_New", SWIG_PyInstanceMethod_New, METH_O, NULL}, - { "open", _wrap_open, METH_O, NULL}, + { "init", _wrap_init, METH_NOARGS, NULL}, + { "exit", _wrap_exit, METH_O, NULL}, + { "get_current_context", _wrap_get_current_context, METH_NOARGS, NULL}, + { "open", _wrap_open, METH_VARARGS, NULL}, + { "get_dev", _wrap_get_dev, METH_VARARGS, NULL}, { "console", _wrap_console, METH_VARARGS, NULL}, { "close", _wrap_close, METH_O, NULL}, - { "get_current_dev", _wrap_get_current_dev, METH_NOARGS, NULL}, { NULL, NULL, 0, NULL } }; @@ -2914,18 +3141,22 @@ static PyMethodDef SwigMethods_proxydocs[] = { /* -------- TYPE CONVERSION AND EQUIVALENCE RULES (BEGIN) -------- */ static swig_type_info _swigt__p_char = {"_p_char", "char *", 0, 0, (void*)0, 0}; +static swig_type_info _swigt__p_pm3_context = {"_p_pm3_context", "struct pm3_context *|pm3_context *", 0, 0, (void*)0, 0}; static swig_type_info _swigt__p_pm3_device = {"_p_pm3_device", "struct pm3_device *|pm3_device *", 0, 0, (void*)0, 0}; static swig_type_info *swig_type_initial[] = { &_swigt__p_char, + &_swigt__p_pm3_context, &_swigt__p_pm3_device, }; static swig_cast_info _swigc__p_char[] = { {&_swigt__p_char, 0, 0, 0},{0, 0, 0, 0}}; +static swig_cast_info _swigc__p_pm3_context[] = { {&_swigt__p_pm3_context, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info _swigc__p_pm3_device[] = { {&_swigt__p_pm3_device, 0, 0, 0},{0, 0, 0, 0}}; static swig_cast_info *swig_cast_initial[] = { _swigc__p_char, + _swigc__p_pm3_context, _swigc__p_pm3_device, }; diff --git a/client/src/proxmark3.c b/client/src/proxmark3.c index 2ef695f37..d5773f49f 100644 --- a/client/src/proxmark3.c +++ b/client/src/proxmark3.c @@ -710,7 +710,26 @@ static void init(void) { /* ======================================================= */ /* user API */ -pm3_device* pm3_open(char *port) { +typedef struct pm3_context pm3_context; +pm3_context *pm3_init(void){ + pm3_context *ctx = (pm3_context *)&session; + return ctx; +} +void pm3_exit(pm3_context *ctx){ + free(ctx); +} +pm3_context *pm3_get_current_context(void){ + pm3_context *ctx = (pm3_context *)&session; + return ctx; +} + +pm3_device *pm3_get_dev(pm3_context *ctx, int n) { + return ((session_arg_t *)ctx)->current_device; +} + +pm3_device* pm3_open(pm3_context *ctx, char *port) { + // For now, there is no real session context: + (void) ctx; init(); OpenProxmark(session.current_device, port, false, 20, false, USART_BAUD_RATE); if (session.pm3_present && (TestProxmark() != PM3_SUCCESS)) { @@ -745,9 +764,6 @@ int pm3_console(pm3_device* dev, char *Cmd) { return CommandReceived(Cmd); } -pm3_device* pm3_get_current_dev(void) { - return session.current_device; -} /* ======================================================= */ #ifndef LIBPM3 diff --git a/client/src/proxmark3.h b/client/src/proxmark3.h index b9b71bb4b..3983cd9cd 100644 --- a/client/src/proxmark3.h +++ b/client/src/proxmark3.h @@ -51,11 +51,15 @@ const char *get_my_executable_directory(void); const char *get_my_user_directory(void); void main_loop(char *script_cmds_file, char *script_cmd, bool stayInCommandLoop); +typedef struct pm3_context pm3_context; +pm3_context *pm3_init(void); +void pm3_exit(pm3_context *ctx); +pm3_context *pm3_get_current_context(void); typedef struct pm3_device pm3_device; -pm3_device* pm3_open(char *port); -int pm3_console(pm3_device* dev, char *cmd); -void pm3_close(pm3_device* dev); -pm3_device* pm3_get_current_dev(void); +pm3_device *pm3_open(pm3_context *ctx, char *port); +pm3_device *pm3_get_dev(pm3_context *ctx, int n); +int pm3_console(pm3_device *dev, char *cmd); +void pm3_close(pm3_device *dev); #ifdef __cplusplus }