diff --git a/package/ctypes/ctypes.c b/package/ctypes/ctypes.c index 732b001f9..d75cfb349 100644 --- a/package/ctypes/ctypes.c +++ b/package/ctypes/ctypes.c @@ -1,7 +1,7 @@ #include "ctypes_Test.h" +#include "ctypes_c_float.h" #include "ctypes_c_uint.h" #include "ctypes_c_wchar_p.h" -#include "ctypes_c_float.h" #include "ctypes_utils.h" void ctypes_c_uint___init__(PikaObj* self, int value) { @@ -12,7 +12,7 @@ void ctypes_c_wchar_p___init__(PikaObj* self, char* value) { ctypesUtils_setStr(self, value); } -void ctypes_c_float___init__(PikaObj *self, float value){ +void ctypes_c_float___init__(PikaObj* self, float value) { ctypesUtils_setFloat(self, value); } @@ -27,6 +27,18 @@ int ctypes_Test_dc_cpuapdu_hex(PikaObj* self, int slen) { printf("input: slen = %d, sendbuf = %s\n", slen, sendbuf); ctypesUtils_setInt(rlen, 5); - ctypesUtils_setStr(rcvbuf, "testout"); + char rcv[] = {0x01, 0x02, 0x03, 0x00, 0x05, 0x08}; + ctypesUtils_setMem(rcvbuf, rcv, sizeof(rcv)); return 0; } + +void ctypes_Test_print_rcv(PikaObj* self, PikaObj* rcvbuf) { + char* rcv = ctypesUtils_getMem(rcvbuf); + size_t rcv_size = ctypesUtils_getMemSize(rcvbuf); + __platform_printf("{"); + for (size_t i = 0; i < rcv_size - 1; i++) { + __platform_printf("%x, ", rcv[i]); + } + __platform_printf("%x", rcv[rcv_size - 1]); + __platform_printf("}\r\n"); +} diff --git a/package/ctypes/ctypes.py b/package/ctypes/ctypes.py index ad73133ca..d6fbacaf4 100644 --- a/package/ctypes/ctypes.py +++ b/package/ctypes/ctypes.py @@ -32,3 +32,4 @@ class c_longdouble(c_float):... class Test(TinyObj): def add(self, c_uint1:c_uint, c_uint2:c_uint)->int:... def dc_cpuapdu_hex(self, slen:int, sendbuf:str, rlen:c_uint, rcvbuf:c_char_p) -> int:... + def print_rcv(self, rcvbuf: c_char_p):... diff --git a/port/linux/package/pikascript/ctypes.py b/port/linux/package/pikascript/ctypes.py index ad73133ca..d6fbacaf4 100644 --- a/port/linux/package/pikascript/ctypes.py +++ b/port/linux/package/pikascript/ctypes.py @@ -32,3 +32,4 @@ class c_longdouble(c_float):... class Test(TinyObj): def add(self, c_uint1:c_uint, c_uint2:c_uint)->int:... def dc_cpuapdu_hex(self, slen:int, sendbuf:str, rlen:c_uint, rcvbuf:c_char_p) -> int:... + def print_rcv(self, rcvbuf: c_char_p):... diff --git a/port/linux/package/pikascript/pikascript-lib/ctypes/ctypes.c b/port/linux/package/pikascript/pikascript-lib/ctypes/ctypes.c index 732b001f9..d75cfb349 100644 --- a/port/linux/package/pikascript/pikascript-lib/ctypes/ctypes.c +++ b/port/linux/package/pikascript/pikascript-lib/ctypes/ctypes.c @@ -1,7 +1,7 @@ #include "ctypes_Test.h" +#include "ctypes_c_float.h" #include "ctypes_c_uint.h" #include "ctypes_c_wchar_p.h" -#include "ctypes_c_float.h" #include "ctypes_utils.h" void ctypes_c_uint___init__(PikaObj* self, int value) { @@ -12,7 +12,7 @@ void ctypes_c_wchar_p___init__(PikaObj* self, char* value) { ctypesUtils_setStr(self, value); } -void ctypes_c_float___init__(PikaObj *self, float value){ +void ctypes_c_float___init__(PikaObj* self, float value) { ctypesUtils_setFloat(self, value); } @@ -27,6 +27,18 @@ int ctypes_Test_dc_cpuapdu_hex(PikaObj* self, int slen) { printf("input: slen = %d, sendbuf = %s\n", slen, sendbuf); ctypesUtils_setInt(rlen, 5); - ctypesUtils_setStr(rcvbuf, "testout"); + char rcv[] = {0x01, 0x02, 0x03, 0x00, 0x05, 0x08}; + ctypesUtils_setMem(rcvbuf, rcv, sizeof(rcv)); return 0; } + +void ctypes_Test_print_rcv(PikaObj* self, PikaObj* rcvbuf) { + char* rcv = ctypesUtils_getMem(rcvbuf); + size_t rcv_size = ctypesUtils_getMemSize(rcvbuf); + __platform_printf("{"); + for (size_t i = 0; i < rcv_size - 1; i++) { + __platform_printf("%x, ", rcv[i]); + } + __platform_printf("%x", rcv[rcv_size - 1]); + __platform_printf("}\r\n"); +}