diff --git a/client/client_with_swig/testembedded.lua b/client/client_with_swig/testembedded.lua index 8d007c390..27361f1d0 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() -pm3.console(p, "hw status") +p=pm3.device() +--p.console("hw status") ?? +p.console(p, "hw status") diff --git a/client/client_with_swig/testembedded.py b/client/client_with_swig/testembedded.py index 795dd3f6d..5f58c2bb6 100755 --- a/client/client_with_swig/testembedded.py +++ b/client/client_with_swig/testembedded.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python3 + import pm3 -p=pm3.get_current_dev() -pm3.console(p, "hw status") +p=pm3.device() +p.console("hw status") +print("Device:", p.get_name()) diff --git a/client/include/pm3.h b/client/include/pm3.h index ff635f239..228a4dcb6 100644 --- a/client/include/pm3.h +++ b/client/include/pm3.h @@ -11,13 +11,46 @@ /* Strip "pm3_" from API functions for SWIG */ %rename("%(strip:[pm3_])s") ""; %feature("immutable","1") pm3_current_dev; +struct pm3_device { }; +%extend pm3_device { + pm3_device() { + printf("SWIG pm3_device constructor, get current pm3\n"); + _embedded = 1; + return pm3_get_current_dev(); + } + pm3_device(char *port) { + printf("SWIG pm3_device constructor with port, open pm3\n"); + _embedded = 0; + return pm3_open(port); + } + ~pm3_device() { + if (_embedded) { + printf("SWIG pm3_device destructor, nothing to do\n"); + } else { + printf("SWIG pm3_device destructor, close pm3\n"); + pm3_close($self); + } + } + int console(char *cmd) { + return pm3_console($self, cmd); + } + char *get_name() { + return pm3_get_name($self); + } +} +//%nodefaultctor pm3_device; +//%nodefaultdtor pm3_device; /* Parse the header file to generate wrappers */ -#endif +#endif // SWIG + +// TODO better than this global? +int _embedded; typedef struct pm3_device pm3_device; pm3_device* pm3_open(char *port); int pm3_console(pm3_device* dev, char *cmd); +char *pm3_get_name(pm3_device* dev); void pm3_close(pm3_device* dev); pm3_device* pm3_get_current_dev(void); -#endif +#endif // LIBPM3_H diff --git a/client/lib/example_lua/test.lua b/client/lib/example_lua/test.lua index 0f77f82a3..2acd38657 100755 --- a/client/lib/example_lua/test.lua +++ b/client/lib/example_lua/test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env lua local pm3 = require("pm3") -p=pm3.open("/dev/ttyACM0") -pm3.console(p, "hw status") -pm3.close(p) +p=pm3.device("/dev/ttyACM0") +--p.console("hw status") ?? +p.console(p, "hw status") diff --git a/client/lib/example_py/test.py b/client/lib/example_py/test.py index f633e507b..93154269a 100755 --- a/client/lib/example_py/test.py +++ b/client/lib/example_py/test.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 import pm3 -p=pm3.open("/dev/ttyACM0") -pm3.console(p, "hw status") -pm3.close(p) +p=pm3.device("/dev/ttyACM0") +p.console("hw status") +print("Device:", p.get_name()) diff --git a/client/src/cmdhw.c b/client/src/cmdhw.c index 8b8adcd92..c55180b68 100644 --- a/client/src/cmdhw.c +++ b/client/src/cmdhw.c @@ -587,15 +587,15 @@ static int CmdConnect(const char *Cmd) { } if (session.pm3_present) { - CloseProxmark(); + CloseProxmark(session.current_device); } // 10 second timeout - OpenProxmark(session.current_device, 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; diff --git a/client/src/comms.c b/client/src/comms.c index 5e164b9ed..423bd830c 100644 --- a/client/src/comms.c +++ b/client/src/comms.c @@ -14,6 +14,7 @@ #include #include #include +#include #include "uart/uart.h" #include "ui.h" @@ -536,7 +537,7 @@ bool IsCommunicationThreadDead(void) { return ret; } -bool OpenProxmark(pm3_device *current_device, 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); @@ -583,15 +584,17 @@ bool OpenProxmark(pm3_device *current_device, char *port, bool wait_for_port, in 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; @@ -654,8 +657,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) { diff --git a/client/src/comms.h b/client/src/comms.h index 7cbe40765..e35183a52 100644 --- a/client/src/comms.h +++ b/client/src/comms.h @@ -65,6 +65,11 @@ typedef struct { extern communication_arg_t conn; +typedef struct pm3_device pm3_device; +struct pm3_device { + communication_arg_t *conn; +}; + 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); @@ -75,9 +80,9 @@ void clearCommandBuffer(void); #define FLASHMODE_SPEED 460800 bool IsCommunicationThreadDead(void); typedef struct pm3_device pm3_device; -bool OpenProxmark(pm3_device *current_device, char *port, bool wait_for_port, int timeout, bool flash_mode, uint32_t speed); -int TestProxmark(void); -void CloseProxmark(void); +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); diff --git a/client/src/flash.c b/client/src/flash.c index 58a0b7014..ba7861511 100644 --- a/client/src/flash.c +++ b/client/src/flash.c @@ -370,11 +370,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(session.current_device, 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 { diff --git a/client/src/pm3.py b/client/src/pm3.py index 8fb59c777..5807bde8a 100644 --- a/client/src/pm3.py +++ b/client/src/pm3.py @@ -61,6 +61,23 @@ class _SwigNonDynamicMeta(type): __setattr__ = _swig_setattr_nondynamic_class_variable(type.__setattr__) +class device(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.device_swiginit(self, _pm3.new_device(*args)) + __swig_destroy__ = _pm3.delete_device + + def console(self, cmd): + return _pm3.device_console(self, cmd) + + def get_name(self): + return _pm3.device_get_name(self) + +# Register device in _pm3: +_pm3.device_swigregister(device) + def open(port): return _pm3.open(port) @@ -68,10 +85,14 @@ def open(port): def console(dev, cmd): return _pm3.console(dev, cmd) +def get_name(dev): + return _pm3.get_name(dev) + def close(dev): return _pm3.close(dev) def get_current_dev(): return _pm3.get_current_dev() +cvar = _pm3.cvar diff --git a/client/src/pm3_luawrap.c b/client/src/pm3_luawrap.c index 17c3c4376..e87261a6d 100644 --- a/client/src/pm3_luawrap.c +++ b/client/src/pm3_luawrap.c @@ -2685,6 +2685,11 @@ static swig_module_info swig_module = {swig_types, 1, 0, 0, 0, 0}; /* Include the header in the wrapper code */ #include "pm3.h" +SWIGINTERN struct pm3_device *new_pm3_device__SWIG_0(void){ + printf("SWIG pm3_device constructor, get current pm3\n"); + _embedded = 1; + return pm3_get_current_dev(); + } SWIGINTERN int SWIG_lua_isnilstring(lua_State *L, int idx) { int ret = lua_isstring(L, idx); @@ -2693,9 +2698,229 @@ SWIGINTERN int SWIG_lua_isnilstring(lua_State *L, int idx) { return ret; } +SWIGINTERN struct pm3_device *new_pm3_device__SWIG_1(char *port){ + printf("SWIG pm3_device constructor with port, open pm3\n"); + _embedded = 0; + return pm3_open(port); + } +SWIGINTERN void delete_pm3_device(struct pm3_device *self){ + if (_embedded) { + printf("SWIG pm3_device destructor, nothing to do\n"); + } else { + printf("SWIG pm3_device destructor, close pm3\n"); + pm3_close(self); + } + } +SWIGINTERN int pm3_device_console(struct pm3_device *self,char *cmd){ + return pm3_console(self, cmd); + } +SWIGINTERN char *pm3_device_get_name(struct pm3_device *self){ + return pm3_get_name(self); + } #ifdef __cplusplus extern "C" { #endif +static int _wrap_new_device__SWIG_0(lua_State* L) { + int SWIG_arg = 0; + struct pm3_device *result = 0 ; + + SWIG_check_num_args("pm3_device::pm3_device",0,0) + result = (struct pm3_device *)new_pm3_device__SWIG_0(); + SWIG_NewPointerObj(L,result,SWIGTYPE_p_pm3_device,1); SWIG_arg++; + return SWIG_arg; + + if(0) SWIG_fail; + +fail: + lua_error(L); + return SWIG_arg; +} + + +static int _wrap_new_device__SWIG_1(lua_State* L) { + int SWIG_arg = 0; + char *arg1 = (char *) 0 ; + struct pm3_device *result = 0 ; + + SWIG_check_num_args("pm3_device::pm3_device",1,1) + if(!SWIG_lua_isnilstring(L,1)) SWIG_fail_arg("pm3_device::pm3_device",1,"char *"); + arg1 = (char *)lua_tostring(L, 1); + result = (struct pm3_device *)new_pm3_device__SWIG_1(arg1); + SWIG_NewPointerObj(L,result,SWIGTYPE_p_pm3_device,1); SWIG_arg++; + return SWIG_arg; + + if(0) SWIG_fail; + +fail: + lua_error(L); + return SWIG_arg; +} + + +static int _wrap_new_device(lua_State* L) { + int argc; + int argv[2]={ + 1,2 + }; + + argc = lua_gettop(L); + if (argc == 0) { + return _wrap_new_device__SWIG_0(L); + } + if (argc == 1) { + int _v; + { + _v = SWIG_lua_isnilstring(L,argv[0]); + } + if (_v) { + return _wrap_new_device__SWIG_1(L); + } + } + + SWIG_Lua_pusherrstring(L,"Wrong arguments for overloaded function 'new_device'\n" + " Possible C/C++ prototypes are:\n" + " pm3_device::pm3_device()\n" + " pm3_device::pm3_device(char *)\n"); + lua_error(L);return 0; +} + + +static int _wrap_device_console(lua_State* L) { + int SWIG_arg = 0; + struct pm3_device *arg1 = (struct pm3_device *) 0 ; + char *arg2 = (char *) 0 ; + int result; + + SWIG_check_num_args("pm3_device::console",2,2) + if(!SWIG_isptrtype(L,1)) SWIG_fail_arg("pm3_device::console",1,"struct pm3_device *"); + if(!SWIG_lua_isnilstring(L,2)) SWIG_fail_arg("pm3_device::console",2,"char *"); + + if (!SWIG_IsOK(SWIG_ConvertPtr(L,1,(void**)&arg1,SWIGTYPE_p_pm3_device,0))){ + SWIG_fail_ptr("device_console",1,SWIGTYPE_p_pm3_device); + } + + arg2 = (char *)lua_tostring(L, 2); + result = (int)pm3_device_console(arg1,arg2); + lua_pushnumber(L, (lua_Number) result); SWIG_arg++; + return SWIG_arg; + + if(0) SWIG_fail; + +fail: + lua_error(L); + return SWIG_arg; +} + + +static int _wrap_device_get_name(lua_State* L) { + int SWIG_arg = 0; + struct pm3_device *arg1 = (struct pm3_device *) 0 ; + char *result = 0 ; + + SWIG_check_num_args("pm3_device::get_name",1,1) + if(!SWIG_isptrtype(L,1)) SWIG_fail_arg("pm3_device::get_name",1,"struct pm3_device *"); + + if (!SWIG_IsOK(SWIG_ConvertPtr(L,1,(void**)&arg1,SWIGTYPE_p_pm3_device,0))){ + SWIG_fail_ptr("device_get_name",1,SWIGTYPE_p_pm3_device); + } + + result = (char *)pm3_device_get_name(arg1); + lua_pushstring(L,(const char *)result); SWIG_arg++; + return SWIG_arg; + + if(0) SWIG_fail; + +fail: + lua_error(L); + return SWIG_arg; +} + + +static void swig_delete_device(void *obj) { +struct pm3_device *arg1 = (struct pm3_device *) obj; +delete_pm3_device(arg1); +} +static int _proxy__wrap_new_device(lua_State *L) { + assert(lua_istable(L,1)); + lua_pushcfunction(L,_wrap_new_device); + assert(!lua_isnil(L,-1)); + lua_replace(L,1); /* replace our table with real constructor */ + lua_call(L,lua_gettop(L)-1,1); + return 1; +} +static swig_lua_attribute swig_device_attributes[] = { + {0,0,0} +}; +static swig_lua_method swig_device_methods[]= { + { "console", _wrap_device_console}, + { "get_name", _wrap_device_get_name}, + {0,0} +}; +static swig_lua_method swig_device_meta[] = { + {0,0} +}; + +static swig_lua_attribute swig_device_Sf_SwigStatic_attributes[] = { + {0,0,0} +}; +static swig_lua_const_info swig_device_Sf_SwigStatic_constants[]= { + {0,0,0,0,0,0} +}; +static swig_lua_method swig_device_Sf_SwigStatic_methods[]= { + {0,0} +}; +static swig_lua_class* swig_device_Sf_SwigStatic_classes[]= { + 0 +}; + +static swig_lua_namespace swig_device_Sf_SwigStatic = { + "device", + swig_device_Sf_SwigStatic_methods, + swig_device_Sf_SwigStatic_attributes, + swig_device_Sf_SwigStatic_constants, + swig_device_Sf_SwigStatic_classes, + 0 +}; +static swig_lua_class *swig_device_bases[] = {0}; +static const char *swig_device_base_names[] = {0}; +static swig_lua_class _wrap_class_device = { "device", "device", &SWIGTYPE_p_pm3_device,_proxy__wrap_new_device, swig_delete_device, swig_device_methods, swig_device_attributes, &swig_device_Sf_SwigStatic, swig_device_meta, swig_device_bases, swig_device_base_names }; + +static int _wrap__embedded_set(lua_State* L) { + int SWIG_arg = 0; + int arg1 ; + + SWIG_check_num_args("_embedded",1,1) + if(!lua_isnumber(L,1)) SWIG_fail_arg("_embedded",1,"int"); + arg1 = (int)lua_tonumber(L, 1); + _embedded = arg1; + + return SWIG_arg; + + if(0) SWIG_fail; + +fail: + lua_error(L); + return SWIG_arg; +} + + +static int _wrap__embedded_get(lua_State* L) { + int SWIG_arg = 0; + int result; + + SWIG_check_num_args("_embedded",0,0) + result = (int)_embedded; + lua_pushnumber(L, (lua_Number) result); 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 ; @@ -2743,6 +2968,30 @@ fail: } +static int _wrap_get_name(lua_State* L) { + int SWIG_arg = 0; + pm3_device *arg1 = (pm3_device *) 0 ; + char *result = 0 ; + + SWIG_check_num_args("pm3_get_name",1,1) + if(!SWIG_isptrtype(L,1)) SWIG_fail_arg("pm3_get_name",1,"pm3_device *"); + + if (!SWIG_IsOK(SWIG_ConvertPtr(L,1,(void**)&arg1,SWIGTYPE_p_pm3_device,0))){ + SWIG_fail_ptr("get_name",1,SWIGTYPE_p_pm3_device); + } + + result = (char *)pm3_get_name(arg1); + lua_pushstring(L,(const char *)result); SWIG_arg++; + return SWIG_arg; + + if(0) SWIG_fail; + +fail: + lua_error(L); + return SWIG_arg; +} + + static int _wrap_close(lua_State* L) { int SWIG_arg = 0; pm3_device *arg1 = (pm3_device *) 0 ; @@ -2784,6 +3033,7 @@ fail: static swig_lua_attribute swig_SwigModule_attributes[] = { + { "_embedded", _wrap__embedded_get, _wrap__embedded_set }, {0,0,0} }; static swig_lua_const_info swig_SwigModule_constants[]= { @@ -2792,11 +3042,13 @@ static swig_lua_const_info swig_SwigModule_constants[]= { static swig_lua_method swig_SwigModule_methods[]= { { "open", _wrap_open}, { "console", _wrap_console}, + { "get_name", _wrap_get_name}, { "close", _wrap_close}, { "get_current_dev", _wrap_get_current_dev}, {0,0} }; static swig_lua_class* swig_SwigModule_classes[]= { +&_wrap_class_device, 0 }; static swig_lua_namespace* swig_SwigModule_namespaces[] = { @@ -2817,7 +3069,7 @@ static swig_lua_namespace swig_SwigModule = { /* -------- TYPE CONVERSION AND EQUIVALENCE RULES (BEGIN) -------- */ -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 _swigt__p_pm3_device = {"_p_pm3_device", "struct pm3_device *|pm3_device *", 0, 0, (void*)&_wrap_class_device, 0}; static swig_type_info *swig_type_initial[] = { &_swigt__p_pm3_device, diff --git a/client/src/pm3_pywrap.c b/client/src/pm3_pywrap.c index 47edc9c6a..544850243 100644 --- a/client/src/pm3_pywrap.c +++ b/client/src/pm3_pywrap.c @@ -2670,6 +2670,11 @@ static swig_module_info swig_module = {swig_types, 2, 0, 0, 0, 0}; /* Include the header in the wrapper code */ #include "pm3.h" +SWIGINTERN struct pm3_device *new_pm3_device__SWIG_0(void){ + printf("SWIG pm3_device constructor, get current pm3\n"); + _embedded = 1; + return pm3_get_current_dev(); + } SWIGINTERN swig_type_info* SWIG_pchar_descriptor(void) @@ -2793,6 +2798,22 @@ SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc) +SWIGINTERN struct pm3_device *new_pm3_device__SWIG_1(char *port){ + printf("SWIG pm3_device constructor with port, open pm3\n"); + _embedded = 0; + return pm3_open(port); + } +SWIGINTERN void delete_pm3_device(struct pm3_device *self){ + if (_embedded) { + printf("SWIG pm3_device destructor, nothing to do\n"); + } else { + printf("SWIG pm3_device destructor, close pm3\n"); + pm3_close(self); + } + } +SWIGINTERN int pm3_device_console(struct pm3_device *self,char *cmd){ + return pm3_console(self, cmd); + } SWIGINTERNINLINE PyObject* SWIG_From_int (int value) @@ -2800,9 +2821,373 @@ SWIGINTERNINLINE PyObject* return PyInt_FromLong((long) value); } +SWIGINTERN char *pm3_device_get_name(struct pm3_device *self){ + return pm3_get_name(self); + } + +SWIGINTERNINLINE PyObject * +SWIG_FromCharPtrAndSize(const char* carray, size_t size) +{ + if (carray) { + if (size > INT_MAX) { + swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); + return pchar_descriptor ? + SWIG_InternalNewPointerObj((char *)(carray), pchar_descriptor, 0) : SWIG_Py_Void(); + } else { +#if PY_VERSION_HEX >= 0x03000000 +#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR) + return PyBytes_FromStringAndSize(carray, (Py_ssize_t)(size)); +#else + return PyUnicode_DecodeUTF8(carray, (Py_ssize_t)(size), "surrogateescape"); +#endif +#else + return PyString_FromStringAndSize(carray, (Py_ssize_t)(size)); +#endif + } + } else { + return SWIG_Py_Void(); + } +} + + +SWIGINTERNINLINE PyObject * +SWIG_FromCharPtr(const char *cptr) +{ + return SWIG_FromCharPtrAndSize(cptr, (cptr ? strlen(cptr) : 0)); +} + + +#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; +} + #ifdef __cplusplus extern "C" { #endif +SWIGINTERN PyObject *_wrap_new_device__SWIG_0(PyObject *SWIGUNUSEDPARM(self), Py_ssize_t nobjs, PyObject **SWIGUNUSEDPARM(swig_obj)) { + PyObject *resultobj = 0; + struct pm3_device *result = 0 ; + + if ((nobjs < 0) || (nobjs > 0)) SWIG_fail; + result = (struct pm3_device *)new_pm3_device__SWIG_0(); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_pm3_device, SWIG_POINTER_NEW | 0 ); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_new_device__SWIG_1(PyObject *SWIGUNUSEDPARM(self), Py_ssize_t nobjs, PyObject **swig_obj) { + PyObject *resultobj = 0; + char *arg1 = (char *) 0 ; + int res1 ; + char *buf1 = 0 ; + int alloc1 = 0 ; + struct pm3_device *result = 0 ; + + if ((nobjs < 1) || (nobjs > 1)) SWIG_fail; + res1 = SWIG_AsCharPtrAndSize(swig_obj[0], &buf1, NULL, &alloc1); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_device" "', argument " "1"" of type '" "char *""'"); + } + arg1 = (char *)(buf1); + result = (struct pm3_device *)new_pm3_device__SWIG_1(arg1); + resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_pm3_device, SWIG_POINTER_NEW | 0 ); + if (alloc1 == SWIG_NEWOBJ) free((char*)buf1); + return resultobj; +fail: + if (alloc1 == SWIG_NEWOBJ) free((char*)buf1); + return NULL; +} + + +SWIGINTERN PyObject *_wrap_new_device(PyObject *self, PyObject *args) { + Py_ssize_t argc; + PyObject *argv[2] = { + 0 + }; + + if (!(argc = SWIG_Python_UnpackTuple(args, "new_device", 0, 1, argv))) SWIG_fail; + --argc; + if (argc == 0) { + return _wrap_new_device__SWIG_0(self, argc, argv); + } + if (argc == 1) { + int _v; + int res = SWIG_AsCharPtrAndSize(argv[0], 0, NULL, 0); + _v = SWIG_CheckState(res); + if (_v) { + return _wrap_new_device__SWIG_1(self, argc, argv); + } + } + +fail: + SWIG_Python_RaiseOrModifyTypeError("Wrong number or type of arguments for overloaded function 'new_device'.\n" + " Possible C/C++ prototypes are:\n" + " pm3_device::pm3_device()\n" + " pm3_device::pm3_device(char *)\n"); + return 0; +} + + +SWIGINTERN PyObject *_wrap_delete_device(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct pm3_device *arg1 = (struct pm3_device *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject *swig_obj[1] ; + + if (!args) SWIG_fail; + swig_obj[0] = args; + res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_pm3_device, SWIG_POINTER_DISOWN | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "delete_device" "', argument " "1"" of type '" "struct pm3_device *""'"); + } + arg1 = (struct pm3_device *)(argp1); + delete_pm3_device(arg1); + resultobj = SWIG_Py_Void(); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_device_console(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct pm3_device *arg1 = (struct pm3_device *) 0 ; + char *arg2 = (char *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + int res2 ; + char *buf2 = 0 ; + int alloc2 = 0 ; + PyObject *swig_obj[2] ; + int result; + + if (!SWIG_Python_UnpackTuple(args, "device_console", 2, 2, swig_obj)) SWIG_fail; + res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_pm3_device, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "device_console" "', argument " "1"" of type '" "struct pm3_device *""'"); + } + arg1 = (struct pm3_device *)(argp1); + res2 = SWIG_AsCharPtrAndSize(swig_obj[1], &buf2, NULL, &alloc2); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "device_console" "', argument " "2"" of type '" "char *""'"); + } + arg2 = (char *)(buf2); + result = (int)pm3_device_console(arg1,arg2); + resultobj = SWIG_From_int((int)(result)); + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); + return resultobj; +fail: + if (alloc2 == SWIG_NEWOBJ) free((char*)buf2); + return NULL; +} + + +SWIGINTERN PyObject *_wrap_device_get_name(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + struct pm3_device *arg1 = (struct pm3_device *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject *swig_obj[1] ; + char *result = 0 ; + + if (!args) SWIG_fail; + swig_obj[0] = args; + res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_pm3_device, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "device_get_name" "', argument " "1"" of type '" "struct pm3_device *""'"); + } + arg1 = (struct pm3_device *)(argp1); + result = (char *)pm3_device_get_name(arg1); + resultobj = SWIG_FromCharPtr((const char *)result); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *device_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *obj; + if (!SWIG_Python_UnpackTuple(args, "swigregister", 1, 1, &obj)) return NULL; + SWIG_TypeNewClientData(SWIGTYPE_p_pm3_device, SWIG_NewClientData(obj)); + return SWIG_Py_Void(); +} + +SWIGINTERN PyObject *device_swiginit(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + return SWIG_Python_InitShadowInstance(args); +} + +SWIGINTERN int Swig_var__embedded_set(PyObject *_val) { + { + int val; + int res = SWIG_AsVal_int(_val, &val); + if (!SWIG_IsOK(res)) { + SWIG_exception_fail(SWIG_ArgError(res), "in variable '""_embedded""' of type '""int""'"); + } + _embedded = (int)(val); + } + return 0; +fail: + return 1; +} + + +SWIGINTERN PyObject *Swig_var__embedded_get(void) { + PyObject *pyobj = 0; + + pyobj = SWIG_From_int((int)(_embedded)); + return pyobj; +} + + SWIGINTERN PyObject *_wrap_open(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; char *arg1 = (char *) 0 ; @@ -2862,6 +3247,29 @@ fail: } +SWIGINTERN PyObject *_wrap_get_name(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + pm3_device *arg1 = (pm3_device *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject *swig_obj[1] ; + char *result = 0 ; + + if (!args) SWIG_fail; + swig_obj[0] = args; + res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_pm3_device, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "get_name" "', argument " "1"" of type '" "pm3_device *""'"); + } + arg1 = (pm3_device *)(argp1); + result = (char *)pm3_get_name(arg1); + resultobj = SWIG_FromCharPtr((const char *)result); + return resultobj; +fail: + return NULL; +} + + SWIGINTERN PyObject *_wrap_close(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; pm3_device *arg1 = (pm3_device *) 0 ; @@ -2899,8 +3307,15 @@ fail: static PyMethodDef SwigMethods[] = { { "SWIG_PyInstanceMethod_New", SWIG_PyInstanceMethod_New, METH_O, NULL}, + { "new_device", _wrap_new_device, METH_VARARGS, NULL}, + { "delete_device", _wrap_delete_device, METH_O, NULL}, + { "device_console", _wrap_device_console, METH_VARARGS, NULL}, + { "device_get_name", _wrap_device_get_name, METH_O, NULL}, + { "device_swigregister", device_swigregister, METH_O, NULL}, + { "device_swiginit", device_swiginit, METH_VARARGS, NULL}, { "open", _wrap_open, METH_O, NULL}, { "console", _wrap_console, METH_VARARGS, NULL}, + { "get_name", _wrap_get_name, METH_O, NULL}, { "close", _wrap_close, METH_O, NULL}, { "get_current_dev", _wrap_get_current_dev, METH_NOARGS, NULL}, { NULL, NULL, 0, NULL } @@ -3656,6 +4071,18 @@ SWIG_init(void) { SWIG_InstallConstants(d,swig_const_table); + globals = SWIG_globals(); + if (!globals) { + PyErr_SetString(PyExc_TypeError, "Failure to create SWIG globals."); +#if PY_VERSION_HEX >= 0x03000000 + return NULL; +#else + return; +#endif + } + PyDict_SetItemString(md, "cvar", globals); + Py_DECREF(globals); + SWIG_addvarlink(globals, "_embedded", Swig_var__embedded_get, Swig_var__embedded_set); #if PY_VERSION_HEX >= 0x03000000 return m; #else diff --git a/client/src/proxmark3.c b/client/src/proxmark3.c index 2ef695f37..2b6e4bf8b 100644 --- a/client/src/proxmark3.c +++ b/client/src/proxmark3.c @@ -130,7 +130,7 @@ static int check_comm(void) { memcpy_filter_ansi(prompt_filtered, prompt, sizeof(prompt_filtered), !session.supports_colors); rl_set_prompt(prompt_filtered); rl_forced_update_display(); - CloseProxmark(); + CloseProxmark(session.current_device); PrintAndLogEx(INFO, "Running in " _YELLOW_("OFFLINE") " mode. Use "_YELLOW_("\"hw connect\"") " to reconnect\n"); } return 0; @@ -582,7 +582,7 @@ static int flash_pm3(char *serial_port_name, uint8_t num_files, char *filenames[ PrintAndLogEx(SUCCESS, " "_YELLOW_("%s"), filepaths[i]); } - if (OpenProxmark(session.current_device, 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); @@ -620,7 +620,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) @@ -712,10 +712,10 @@ static void init(void) { pm3_device* pm3_open(char *port) { init(); - OpenProxmark(session.current_device, port, false, 20, false, USART_BAUD_RATE); - if (session.pm3_present && (TestProxmark() != PM3_SUCCESS)) { + 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(); + CloseProxmark(session.current_device); } if ((port != NULL) && (!session.pm3_present)) @@ -735,7 +735,7 @@ void pm3_close(pm3_device* dev) { clearCommandBuffer(); SendCommandNG(CMD_QUIT_SESSION, NULL, 0); msleep(100); // Make sure command is sent before killing client - CloseProxmark(); + CloseProxmark(dev); } } @@ -745,6 +745,10 @@ int pm3_console(pm3_device* dev, char *Cmd) { return CommandReceived(Cmd); } +char *pm3_get_name(pm3_device* dev) { + return dev->conn->serial_port_name; +} + pm3_device* pm3_get_current_dev(void) { return session.current_device; } @@ -1038,12 +1042,12 @@ int main(int argc, char *argv[]) { // try to open USB connection to Proxmark if (port != NULL) { - OpenProxmark(session.current_device, 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)) @@ -1099,7 +1103,7 @@ 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 diff --git a/client/src/proxmark3.h b/client/src/proxmark3.h index b9b71bb4b..683bed22f 100644 --- a/client/src/proxmark3.h +++ b/client/src/proxmark3.h @@ -54,6 +54,7 @@ void main_loop(char *script_cmds_file, char *script_cmd, bool stayInCommandLoop) typedef struct pm3_device pm3_device; pm3_device* pm3_open(char *port); int pm3_console(pm3_device* dev, char *cmd); +char *pm3_get_name(pm3_device* dev); void pm3_close(pm3_device* dev); pm3_device* pm3_get_current_dev(void); diff --git a/client/src/ui.h b/client/src/ui.h index 8910bf836..b43c8e298 100644 --- a/client/src/ui.h +++ b/client/src/ui.h @@ -14,6 +14,8 @@ #include #include "common.h" #include "ansi.h" +#define DEVICE_NAME_LENGTH 256 +#include "comms.h" // pm3_device #ifdef __cplusplus extern "C" { @@ -27,7 +29,6 @@ typedef enum clientdebugLevel {cdbOFF, cdbSIMPLE, cdbFULL} clientdebugLevel_t; // typedef enum devicedebugLevel {ddbOFF, ddbERROR, ddbINFO, ddbDEBUG, ddbEXTENDED} devicedebugLevel_t; //typedef enum savePaths {spDefault, spDump, spTrace, spItemCount} savePaths_t; // last item spItemCount used to auto map to number of files typedef struct {int x; int y; int h; int w;} qtWindow_t; -typedef struct pm3_device pm3_device; typedef struct { bool preferences_loaded;