From a7613dd09734f8e49514d244284eb6a16ff6e729 Mon Sep 17 00:00:00 2001 From: lyon Date: Tue, 24 May 2022 15:26:12 +0800 Subject: [PATCH] fix readBytes memory error --- package/PikaStdDevice/PikaStdDevice_CAN.c | 2 +- package/PikaStdDevice/PikaStdDevice_IIC.c | 2 +- package/PikaStdDevice/PikaStdDevice_SPI.c | 11 ++++---- package/PikaStdDevice/PikaStdDevice_UART.c | 2 +- package/TemplateDevice/TemplateDevice_UART.c | 27 +++++++------------ port/linux/package/pikascript/TempDevTest.py | 24 +++++++++++++++++ port/linux/package/pikascript/main.py | 2 ++ .../PikaStdDevice/PikaStdDevice_CAN.c | 2 +- .../PikaStdDevice/PikaStdDevice_IIC.c | 2 +- .../PikaStdDevice/PikaStdDevice_SPI.c | 11 ++++---- .../PikaStdDevice/PikaStdDevice_UART.c | 2 +- .../TemplateDevice/TemplateDevice_UART.c | 27 +++++++------------ 12 files changed, 62 insertions(+), 52 deletions(-) create mode 100644 port/linux/package/pikascript/TempDevTest.py diff --git a/package/PikaStdDevice/PikaStdDevice_CAN.c b/package/PikaStdDevice/PikaStdDevice_CAN.c index cd507db91..5d403bc9d 100644 --- a/package/PikaStdDevice/PikaStdDevice_CAN.c +++ b/package/PikaStdDevice/PikaStdDevice_CAN.c @@ -46,7 +46,7 @@ char* PikaStdDevice_CAN_read(PikaObj* self, int length) { Arg* PikaStdDevice_CAN_readBytes(PikaObj *self, int length){ obj_setInt(self, "length", length); obj_runNativeMethod(self, "platformReadBytes", NULL); - return obj_getArg(self, "readData"); + return arg_copy(obj_getArg(self, "readData")); } void PikaStdDevice_CAN_write(PikaObj* self, char* data) { diff --git a/package/PikaStdDevice/PikaStdDevice_IIC.c b/package/PikaStdDevice/PikaStdDevice_IIC.c index 51908eb7a..2e8c2b49e 100644 --- a/package/PikaStdDevice/PikaStdDevice_IIC.c +++ b/package/PikaStdDevice/PikaStdDevice_IIC.c @@ -57,7 +57,7 @@ Arg* PikaStdDevice_IIC_readBytes(PikaObj* self, int addr, int length) { obj_setInt(self, "length", length); obj_setInt(self, "readAddr", addr); obj_runNativeMethod(self, "platformReadBytes", NULL); - return obj_getArg(self, "readData"); + return arg_copy(obj_getArg(self, "readData")); } void PikaStdDevice_IIC_platformWrite(PikaObj* self) { diff --git a/package/PikaStdDevice/PikaStdDevice_SPI.c b/package/PikaStdDevice/PikaStdDevice_SPI.c index e0421e350..4493026ef 100644 --- a/package/PikaStdDevice/PikaStdDevice_SPI.c +++ b/package/PikaStdDevice/PikaStdDevice_SPI.c @@ -25,7 +25,7 @@ void PikaStdDevice_SPI_write(PikaObj* self, char* data) { obj_runNativeMethod(self, "platformWrite", NULL); } -void PikaStdDevice_SPI_writeBytes(PikaObj *self, uint8_t* data, int length){ +void PikaStdDevice_SPI_writeBytes(PikaObj* self, uint8_t* data, int length) { obj_setBytes(self, "writeData", data, length); obj_runNativeMethod(self, "platformWriteBytes", NULL); } @@ -36,10 +36,10 @@ char* PikaStdDevice_SPI_read(PikaObj* self, int length) { return obj_getStr(self, "readData"); } -Arg* PikaStdDevice_SPI_readBytes(PikaObj *self, int length){ +Arg* PikaStdDevice_SPI_readBytes(PikaObj* self, int length) { obj_setInt(self, "length", length); obj_runNativeMethod(self, "platformReadBytes", NULL); - return obj_getArg(self, "readData"); + return arg_copy(obj_getArg(self, "readData")); } void PikaStdDevice_SPI_setBaudRate(PikaObj* self, int baudRate) { @@ -94,13 +94,12 @@ void PikaStdDevice_SPI_platformRead(PikaObj* self) { obj_setSysOut(self, "[error] platform method need to be override."); } -void PikaStdDevice_SPI_platformWriteBytes(PikaObj *self){ +void PikaStdDevice_SPI_platformWriteBytes(PikaObj* self) { obj_setErrorCode(self, 1); obj_setSysOut(self, "[error] platform method need to be override."); } -void PikaStdDevice_SPI_platformReadBytes(PikaObj *self){ +void PikaStdDevice_SPI_platformReadBytes(PikaObj* self) { obj_setErrorCode(self, 1); obj_setSysOut(self, "[error] platform method need to be override."); } - diff --git a/package/PikaStdDevice/PikaStdDevice_UART.c b/package/PikaStdDevice/PikaStdDevice_UART.c index 8b8e651b7..45b446b25 100644 --- a/package/PikaStdDevice/PikaStdDevice_UART.c +++ b/package/PikaStdDevice/PikaStdDevice_UART.c @@ -28,7 +28,7 @@ char* PikaStdDevice_UART_read(PikaObj* self, int length) { Arg* PikaStdDevice_UART_readBytes(PikaObj *self, int length){ obj_setInt(self, "length", length); obj_runNativeMethod(self, "platformReadBytes", NULL); - return obj_getArg(self, "readData"); + return arg_copy(obj_getArg(self, "readData")); } void PikaStdDevice_UART_setBaudRate(PikaObj* self, int baudRate) { diff --git a/package/TemplateDevice/TemplateDevice_UART.c b/package/TemplateDevice/TemplateDevice_UART.c index 0e1dd43c6..0a4513262 100644 --- a/package/TemplateDevice/TemplateDevice_UART.c +++ b/package/TemplateDevice/TemplateDevice_UART.c @@ -1,20 +1,13 @@ #include "TemplateDevice_UART.h" -void TemplateDevice_UART_platformDisable(PikaObj *self){ - +void TemplateDevice_UART_platformDisable(PikaObj* self) {} +void TemplateDevice_UART_platformEnable(PikaObj* self) {} +void TemplateDevice_UART_platformRead(PikaObj* self) {} +void TemplateDevice_UART_platformReadBytes(PikaObj* self) { + int length = obj_getInt(self, "length"); + uint8_t bytes_buff[] = {0x00, 0x01, 0x02, 0x03, 0x04, + 0x05, 0x06, 0x07, 0x08}; + obj_setBytes(self, "readData", bytes_buff, length); } -void TemplateDevice_UART_platformEnable(PikaObj *self){ - -} -void TemplateDevice_UART_platformRead(PikaObj *self){ - -} -void TemplateDevice_UART_platformReadBytes(PikaObj *self){ - -} -void TemplateDevice_UART_platformWrite(PikaObj *self){ - -} -void TemplateDevice_UART_platformWriteBytes(PikaObj *self){ - -} \ No newline at end of file +void TemplateDevice_UART_platformWrite(PikaObj* self) {} +void TemplateDevice_UART_platformWriteBytes(PikaObj* self) {} \ No newline at end of file diff --git a/port/linux/package/pikascript/TempDevTest.py b/port/linux/package/pikascript/TempDevTest.py new file mode 100644 index 000000000..29a104bb2 --- /dev/null +++ b/port/linux/package/pikascript/TempDevTest.py @@ -0,0 +1,24 @@ +import TemplateDevice + +def EXPECT_EQ(test_name, input, expected): + print('-----TEST-----') + print(test_name) + if input != expected: + print('[Error]') + print(test_name) + print('[info] Expected:') + print(expected) + print('[info] Input:') + print(input) + # error + while True: + is_error = 1 + else: + print('[ OK ]') + +def test(): + uart = TemplateDevice.UART() + uart.setId(1) + uart.setBaudRate(115200) + bt = uart.readBytes(5) + EXPECT_EQ('uart.readBytes', bt, b'\x00\x01\x02\x03\x04') diff --git a/port/linux/package/pikascript/main.py b/port/linux/package/pikascript/main.py index a6a6e6adf..0127628f8 100644 --- a/port/linux/package/pikascript/main.py +++ b/port/linux/package/pikascript/main.py @@ -11,3 +11,5 @@ import TempDevTest as dev import TemplateDevice print('hello pikascript!') + +dev.test() diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_CAN.c b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_CAN.c index cd507db91..5d403bc9d 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_CAN.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_CAN.c @@ -46,7 +46,7 @@ char* PikaStdDevice_CAN_read(PikaObj* self, int length) { Arg* PikaStdDevice_CAN_readBytes(PikaObj *self, int length){ obj_setInt(self, "length", length); obj_runNativeMethod(self, "platformReadBytes", NULL); - return obj_getArg(self, "readData"); + return arg_copy(obj_getArg(self, "readData")); } void PikaStdDevice_CAN_write(PikaObj* self, char* data) { diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_IIC.c b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_IIC.c index 51908eb7a..2e8c2b49e 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_IIC.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_IIC.c @@ -57,7 +57,7 @@ Arg* PikaStdDevice_IIC_readBytes(PikaObj* self, int addr, int length) { obj_setInt(self, "length", length); obj_setInt(self, "readAddr", addr); obj_runNativeMethod(self, "platformReadBytes", NULL); - return obj_getArg(self, "readData"); + return arg_copy(obj_getArg(self, "readData")); } void PikaStdDevice_IIC_platformWrite(PikaObj* self) { diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_SPI.c b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_SPI.c index e0421e350..4493026ef 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_SPI.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_SPI.c @@ -25,7 +25,7 @@ void PikaStdDevice_SPI_write(PikaObj* self, char* data) { obj_runNativeMethod(self, "platformWrite", NULL); } -void PikaStdDevice_SPI_writeBytes(PikaObj *self, uint8_t* data, int length){ +void PikaStdDevice_SPI_writeBytes(PikaObj* self, uint8_t* data, int length) { obj_setBytes(self, "writeData", data, length); obj_runNativeMethod(self, "platformWriteBytes", NULL); } @@ -36,10 +36,10 @@ char* PikaStdDevice_SPI_read(PikaObj* self, int length) { return obj_getStr(self, "readData"); } -Arg* PikaStdDevice_SPI_readBytes(PikaObj *self, int length){ +Arg* PikaStdDevice_SPI_readBytes(PikaObj* self, int length) { obj_setInt(self, "length", length); obj_runNativeMethod(self, "platformReadBytes", NULL); - return obj_getArg(self, "readData"); + return arg_copy(obj_getArg(self, "readData")); } void PikaStdDevice_SPI_setBaudRate(PikaObj* self, int baudRate) { @@ -94,13 +94,12 @@ void PikaStdDevice_SPI_platformRead(PikaObj* self) { obj_setSysOut(self, "[error] platform method need to be override."); } -void PikaStdDevice_SPI_platformWriteBytes(PikaObj *self){ +void PikaStdDevice_SPI_platformWriteBytes(PikaObj* self) { obj_setErrorCode(self, 1); obj_setSysOut(self, "[error] platform method need to be override."); } -void PikaStdDevice_SPI_platformReadBytes(PikaObj *self){ +void PikaStdDevice_SPI_platformReadBytes(PikaObj* self) { obj_setErrorCode(self, 1); obj_setSysOut(self, "[error] platform method need to be override."); } - diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_UART.c b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_UART.c index 8b8e651b7..45b446b25 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_UART.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_UART.c @@ -28,7 +28,7 @@ char* PikaStdDevice_UART_read(PikaObj* self, int length) { Arg* PikaStdDevice_UART_readBytes(PikaObj *self, int length){ obj_setInt(self, "length", length); obj_runNativeMethod(self, "platformReadBytes", NULL); - return obj_getArg(self, "readData"); + return arg_copy(obj_getArg(self, "readData")); } void PikaStdDevice_UART_setBaudRate(PikaObj* self, int baudRate) { diff --git a/port/linux/package/pikascript/pikascript-lib/TemplateDevice/TemplateDevice_UART.c b/port/linux/package/pikascript/pikascript-lib/TemplateDevice/TemplateDevice_UART.c index 0e1dd43c6..0a4513262 100644 --- a/port/linux/package/pikascript/pikascript-lib/TemplateDevice/TemplateDevice_UART.c +++ b/port/linux/package/pikascript/pikascript-lib/TemplateDevice/TemplateDevice_UART.c @@ -1,20 +1,13 @@ #include "TemplateDevice_UART.h" -void TemplateDevice_UART_platformDisable(PikaObj *self){ - +void TemplateDevice_UART_platformDisable(PikaObj* self) {} +void TemplateDevice_UART_platformEnable(PikaObj* self) {} +void TemplateDevice_UART_platformRead(PikaObj* self) {} +void TemplateDevice_UART_platformReadBytes(PikaObj* self) { + int length = obj_getInt(self, "length"); + uint8_t bytes_buff[] = {0x00, 0x01, 0x02, 0x03, 0x04, + 0x05, 0x06, 0x07, 0x08}; + obj_setBytes(self, "readData", bytes_buff, length); } -void TemplateDevice_UART_platformEnable(PikaObj *self){ - -} -void TemplateDevice_UART_platformRead(PikaObj *self){ - -} -void TemplateDevice_UART_platformReadBytes(PikaObj *self){ - -} -void TemplateDevice_UART_platformWrite(PikaObj *self){ - -} -void TemplateDevice_UART_platformWriteBytes(PikaObj *self){ - -} \ No newline at end of file +void TemplateDevice_UART_platformWrite(PikaObj* self) {} +void TemplateDevice_UART_platformWriteBytes(PikaObj* self) {} \ No newline at end of file