mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
Merge branch 'master' into mqttdev
This commit is contained in:
commit
83680dce51
15
examples/Device/GPIO_CALLBCK.py
Normal file
15
examples/Device/GPIO_CALLBCK.py
Normal file
@ -0,0 +1,15 @@
|
||||
import PikaStdDevice as std
|
||||
print('hello pikascript')
|
||||
|
||||
io = std.GPIO()
|
||||
io.setPin('P4')
|
||||
io.setMode('in')
|
||||
io.enable()
|
||||
|
||||
def cb1(signal):
|
||||
print('cb1', signal)
|
||||
|
||||
io.setCallBack(cb1, io.SIGNAL_FALLING)
|
||||
|
||||
while True:
|
||||
pass
|
33
examples/Device/KEY_POLL.PY
Normal file
33
examples/Device/KEY_POLL.PY
Normal file
@ -0,0 +1,33 @@
|
||||
from machine import GPIO as STD_GPIO
|
||||
|
||||
print('PikaScript LED KEY')
|
||||
|
||||
|
||||
LED1 = STD_GPIO()
|
||||
LED1.setPin('PE5')
|
||||
LED1.setMode('out')
|
||||
LED1.enable()
|
||||
|
||||
LED2 = STD_GPIO()
|
||||
LED2.setPin('PE6')
|
||||
LED2.setMode('out')
|
||||
LED2.enable()
|
||||
|
||||
KEY1 = STD_GPIO()
|
||||
KEY1.setPin('PE4')
|
||||
# setPull要在setMode前面才行,PikaStdDevice_GPIO.c函数PikaStdDevice_GPIO_setPull不赋值pull
|
||||
# PikaStdDevice_GPIO_platformSetMode中才赋值cfg->pull
|
||||
KEY1.setPull('up')
|
||||
KEY1.setMode('in')
|
||||
|
||||
KEY1.enable()
|
||||
|
||||
LED1.high()
|
||||
LED2.high()
|
||||
print(KEY1.read())
|
||||
|
||||
while True:
|
||||
if KEY1.read() == 1:
|
||||
LED1.high()
|
||||
else:
|
||||
LED1.low()
|
15
examples/Device/UART_CALLBACK.py
Normal file
15
examples/Device/UART_CALLBACK.py
Normal file
@ -0,0 +1,15 @@
|
||||
import PikaStdDevice as std
|
||||
print('hello pikascript')
|
||||
|
||||
uart = std.UART()
|
||||
uart.setId(0)
|
||||
uart.setBaudRate(115200)
|
||||
uart.enable()
|
||||
|
||||
def cb1(signal):
|
||||
print('recv:', uart.read(32))
|
||||
|
||||
uart.setCallBack(cb1, uart.SIGNAL_RX)
|
||||
|
||||
while True:
|
||||
pass
|
16
examples/hmac/test_hmac.py
Normal file
16
examples/hmac/test_hmac.py
Normal file
@ -0,0 +1,16 @@
|
||||
import hmac
|
||||
|
||||
secret = "0123456789"
|
||||
payload = "helloworld"
|
||||
|
||||
h = hmac.new(secret.encode(),digestmod="md5")
|
||||
h.update(payload.encode())
|
||||
print("hmac-md5:",h.hexdigest())
|
||||
|
||||
h = hmac.new(secret.encode(),digestmod="sha1")
|
||||
h.update(payload.encode())
|
||||
print("hmac-sha1:",h.hexdigest())
|
||||
|
||||
h = hmac.new(secret.encode(),digestmod="sha256")
|
||||
h.update(payload.encode())
|
||||
print("hmac-sha256:",h.hexdigest())
|
@ -1,6 +1,7 @@
|
||||
#include <bl_gpio.h>
|
||||
#include <hosal_gpio.h>
|
||||
#include "../PikaStdDevice/pika_hal.h"
|
||||
#include "pika_hal_BLIOT_irq_task.h"
|
||||
|
||||
int pika_hal_platform_GPIO_open(pika_dev* dev, char* name) {
|
||||
dev->platform_data = pikaMalloc(sizeof(hosal_gpio_dev_t));
|
||||
@ -56,6 +57,15 @@ int pika_hal_platform_GPIO_ioctl_disable(pika_dev* dev) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern volatile _BLIOT_irq g_irq;
|
||||
void _hosal_gpio_irq_handler(void* arg) {
|
||||
if (g_irq.gpio_irq_trigger) {
|
||||
return;
|
||||
}
|
||||
g_irq.gpio_irq_arg = arg;
|
||||
g_irq.gpio_irq_trigger = 1;
|
||||
}
|
||||
|
||||
int pika_hal_platform_GPIO_ioctl_config(pika_dev* dev,
|
||||
pika_hal_GPIO_config* cfg) {
|
||||
hosal_gpio_dev_t* platform_gpio = (hosal_gpio_dev_t*)dev->platform_data;
|
||||
@ -84,9 +94,11 @@ int pika_hal_platform_GPIO_ioctl_config(pika_dev* dev,
|
||||
switch (cfg->pull) {
|
||||
case PIKA_HAL_GPIO_PULL_NONE:
|
||||
platform_gpio->config = INPUT_HIGH_IMPEDANCE;
|
||||
break;
|
||||
|
||||
case PIKA_HAL_GPIO_PULL_UP:
|
||||
platform_gpio->config = INPUT_PULL_UP;
|
||||
break;
|
||||
|
||||
case PIKA_HAL_GPIO_PULL_DOWN:
|
||||
/* not supported */
|
||||
@ -94,6 +106,7 @@ int pika_hal_platform_GPIO_ioctl_config(pika_dev* dev,
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
__platform_printf("GPIO set port %d to unknown\r\n",
|
||||
@ -101,5 +114,28 @@ int pika_hal_platform_GPIO_ioctl_config(pika_dev* dev,
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* support event callback */
|
||||
if (NULL != cfg->event_callback &&
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA_ENABLE == cfg->event_callback_ena) {
|
||||
switch (cfg->event_callback_filter) {
|
||||
case PIKA_HAL_GPIO_EVENT_SIGNAL_RISING:
|
||||
hosal_gpio_irq_set(dev->platform_data, HOSAL_IRQ_TRIG_POS_PULSE,
|
||||
_hosal_gpio_irq_handler, dev);
|
||||
break;
|
||||
case PIKA_HAL_GPIO_EVENT_SIGNAL_FALLING:
|
||||
hosal_gpio_irq_set(dev->platform_data, HOSAL_IRQ_TRIG_NEG_PULSE,
|
||||
_hosal_gpio_irq_handler, dev);
|
||||
break;
|
||||
|
||||
default:
|
||||
__platform_printf(
|
||||
"Error: not supported event callback filter %d\r\n",
|
||||
cfg->event_callback_filter);
|
||||
return -1;
|
||||
}
|
||||
/* start irq task thread */
|
||||
_BLIOT_irq_task_start();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -101,7 +101,10 @@ int pika_hal_platform_SPI_ioctl_enable(pika_dev* dev) {
|
||||
platform_spi->port, platform_spi->config.freq,
|
||||
platform_spi->config.mode, platform_spi->config.polar_phase);
|
||||
#endif
|
||||
hosal_spi_init(platform_spi);
|
||||
if (0 != hosal_spi_init(platform_spi)) {
|
||||
__platform_printf("SPI: Open SPI%d failed\r\n", platform_spi->port);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
@ -119,11 +122,25 @@ int pika_hal_platform_SPI_ioctl_disable(pika_dev* dev) {
|
||||
int pika_hal_platform_SPI_write(pika_dev* dev, void* buf, size_t count) {
|
||||
hosal_spi_dev_t* platform_spi = (hosal_spi_dev_t*)dev->platform_data;
|
||||
pika_hal_SPI_config* cfg = (pika_hal_SPI_config*)dev->ioctl_config;
|
||||
return hosal_spi_send(platform_spi, buf, count, cfg->timeout);
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
__platform_printf("SPI: Write %d bytes\r\n", count);
|
||||
#endif
|
||||
int ret = hosal_spi_send(platform_spi, buf, count, cfg->timeout);
|
||||
if (0 != ret) {
|
||||
__platform_printf("SPI: Write %d bytes failed\r\n", count);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int pika_hal_platform_SPI_read(pika_dev* dev, void* buf, size_t count) {
|
||||
hosal_spi_dev_t* platform_spi = (hosal_spi_dev_t*)dev->platform_data;
|
||||
pika_hal_SPI_config* cfg = (pika_hal_SPI_config*)dev->ioctl_config;
|
||||
return hosal_spi_recv(platform_spi, buf, count, cfg->timeout);
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
__platform_printf("SPI: Read %d bytes\r\n", count);
|
||||
#endif
|
||||
int ret = hosal_spi_recv(platform_spi, buf, count, cfg->timeout);
|
||||
if (0 != ret) {
|
||||
__platform_printf("SPI: Read %d bytes failed\r\n", count);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
161
package/BLIOT/pika_hal_BLIOT_UART.c
Normal file
161
package/BLIOT/pika_hal_BLIOT_UART.c
Normal file
@ -0,0 +1,161 @@
|
||||
#include <hosal_uart.h>
|
||||
#include "../PikaStdDevice/pika_hal.h"
|
||||
#include "pika_hal_BLIOT_irq_task.h"
|
||||
|
||||
extern hosal_uart_dev_t uart_stdio;
|
||||
|
||||
int pika_hal_platform_UART_open(pika_dev* dev, char* name) {
|
||||
/* Support UART1 */
|
||||
if (name[0] == 'U' && name[1] == 'A' && name[2] == 'R' && name[3] == 'T') {
|
||||
hosal_uart_dev_t* platform_uart = &uart_stdio;
|
||||
dev->platform_data = platform_uart;
|
||||
int UART_num = fast_atoi(name + 4);
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
__platform_printf("opening UART%d\r\n", UART_num);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int pika_hal_platform_UART_close(pika_dev* dev) {
|
||||
hosal_uart_dev_t* platform_uart = (hosal_uart_dev_t*)dev->platform_data;
|
||||
if (platform_uart == &uart_stdio) {
|
||||
return 0;
|
||||
}
|
||||
if (NULL != platform_uart) {
|
||||
pikaFree(platform_uart, sizeof(hosal_uart_dev_t));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern volatile _BLIOT_irq g_irq;
|
||||
int _hosal_uart_irq_handler(void* p_arg) {
|
||||
if (g_irq.uart_irq_trigger) {
|
||||
return 0;
|
||||
}
|
||||
g_irq.uart_irq_arg = p_arg;
|
||||
g_irq.uart_irq_trigger = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pika_hal_platform_UART_ioctl_config(pika_dev* dev,
|
||||
pika_hal_UART_config* cfg) {
|
||||
hosal_uart_dev_t* platform_uart = (hosal_uart_dev_t*)dev->platform_data;
|
||||
if (platform_uart != &uart_stdio) {
|
||||
platform_uart->config.baud_rate = cfg->baudrate;
|
||||
switch (cfg->data_bits) {
|
||||
case PIKA_HAL_UART_DATA_BITS_5:
|
||||
platform_uart->config.data_width = HOSAL_DATA_WIDTH_5BIT;
|
||||
break;
|
||||
case PIKA_HAL_UART_DATA_BITS_6:
|
||||
platform_uart->config.data_width = HOSAL_DATA_WIDTH_6BIT;
|
||||
break;
|
||||
case PIKA_HAL_UART_DATA_BITS_7:
|
||||
platform_uart->config.data_width = HOSAL_DATA_WIDTH_7BIT;
|
||||
break;
|
||||
case PIKA_HAL_UART_DATA_BITS_8:
|
||||
platform_uart->config.data_width = HOSAL_DATA_WIDTH_8BIT;
|
||||
break;
|
||||
default:
|
||||
platform_uart->config.data_width = HOSAL_DATA_WIDTH_8BIT;
|
||||
break;
|
||||
}
|
||||
switch (cfg->parity) {
|
||||
case PIKA_HAL_UART_PARITY_NONE:
|
||||
platform_uart->config.parity = HOSAL_NO_PARITY;
|
||||
break;
|
||||
case PIKA_HAL_UART_PARITY_ODD:
|
||||
platform_uart->config.parity = HOSAL_ODD_PARITY;
|
||||
break;
|
||||
case PIKA_HAL_UART_PARITY_EVEN:
|
||||
platform_uart->config.parity = HOSAL_EVEN_PARITY;
|
||||
break;
|
||||
default:
|
||||
platform_uart->config.parity = HOSAL_NO_PARITY;
|
||||
break;
|
||||
}
|
||||
switch (cfg->stop_bits) {
|
||||
case PIKA_HAL_UART_STOP_BITS_1:
|
||||
platform_uart->config.stop_bits = HOSAL_STOP_BITS_1;
|
||||
break;
|
||||
case PIKA_HAL_UART_STOP_BITS_2:
|
||||
platform_uart->config.stop_bits = HOSAL_STOP_BITS_2;
|
||||
break;
|
||||
case PIKA_HAL_UART_STOP_BITS_1_5:
|
||||
platform_uart->config.stop_bits = HOSAL_STOP_BITS_1_5;
|
||||
break;
|
||||
default:
|
||||
platform_uart->config.stop_bits = HOSAL_STOP_BITS_1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* support event callback */
|
||||
if (NULL != cfg->event_callback &&
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA_ENABLE == cfg->event_callback_ena) {
|
||||
hosal_uart_ioctl(platform_uart, HOSAL_UART_MODE_SET,
|
||||
(void*)HOSAL_UART_MODE_INT);
|
||||
switch (cfg->event_callback_filter) {
|
||||
/* Configure UART to interrupt mode */
|
||||
case PIKA_HAL_UART_EVENT_SIGNAL_RX:
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
__platform_printf("Setting UART_RX callback\r\n");
|
||||
#endif
|
||||
hosal_uart_callback_set(platform_uart, HOSAL_UART_RX_CALLBACK,
|
||||
_hosal_uart_irq_handler, dev);
|
||||
break;
|
||||
case PIKA_HAL_UART_EVENT_SIGNAL_TX:
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
__platform_printf("Setting UART_TX callback\r\n");
|
||||
#endif
|
||||
hosal_uart_callback_set(platform_uart, HOSAL_UART_TX_CALLBACK,
|
||||
_hosal_uart_irq_handler, dev);
|
||||
break;
|
||||
default:
|
||||
__platform_printf(
|
||||
"Error: not supported event callback filter %d\r\n",
|
||||
cfg->event_callback_filter);
|
||||
return -1;
|
||||
}
|
||||
/* start irq task thread */
|
||||
_BLIOT_irq_task_start();
|
||||
}
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pika_hal_platform_UART_ioctl_enable(pika_dev* dev) {
|
||||
if (!dev->is_enabled) {
|
||||
hosal_uart_dev_t* platform_uart = (hosal_uart_dev_t*)dev->platform_data;
|
||||
if (platform_uart == &uart_stdio) {
|
||||
return 0;
|
||||
}
|
||||
hosal_uart_init(platform_uart);
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int pika_hal_platform_UART_ioctl_disable(pika_dev* dev) {
|
||||
if (dev->is_enabled) {
|
||||
hosal_uart_dev_t* platform_uart = (hosal_uart_dev_t*)dev->platform_data;
|
||||
if (platform_uart == &uart_stdio) {
|
||||
return 0;
|
||||
}
|
||||
hosal_uart_finalize(platform_uart);
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int pika_hal_platform_UART_write(pika_dev* dev, void* buf, size_t count) {
|
||||
hosal_uart_dev_t* platform_uart = (hosal_uart_dev_t*)dev->platform_data;
|
||||
return hosal_uart_send(platform_uart, buf, count);
|
||||
}
|
||||
|
||||
int pika_hal_platform_UART_read(pika_dev* dev, void* buf, size_t count) {
|
||||
hosal_uart_dev_t* platform_uart = (hosal_uart_dev_t*)dev->platform_data;
|
||||
return hosal_uart_receive(platform_uart, buf, count);
|
||||
}
|
52
package/BLIOT/pika_hal_BLIOT_irq_task.c
Normal file
52
package/BLIOT/pika_hal_BLIOT_irq_task.c
Normal file
@ -0,0 +1,52 @@
|
||||
#include "pika_hal_BLIOT_irq_task.h"
|
||||
#include <FreeRTOS.h>
|
||||
#include <task.h>
|
||||
|
||||
volatile _BLIOT_irq g_irq = {0};
|
||||
|
||||
static void _irq_task(void* pvParameters) {
|
||||
while (1) {
|
||||
vTaskDelay(10);
|
||||
if (g_irq.gpio_irq_trigger) {
|
||||
g_irq.gpio_irq_trigger = 0;
|
||||
pika_dev* dev = (pika_dev*)g_irq.gpio_irq_arg;
|
||||
pika_hal_GPIO_config* cfg = dev->ioctl_config;
|
||||
cfg->event_callback(dev, cfg->event_callback_filter);
|
||||
}
|
||||
if (g_irq.uart_irq_trigger) {
|
||||
g_irq.uart_irq_trigger = 0;
|
||||
pika_dev* dev = (pika_dev*)g_irq.uart_irq_arg;
|
||||
pika_hal_UART_config* cfg = dev->ioctl_config;
|
||||
cfg->event_callback(dev, cfg->event_callback_filter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int _BLIOT_irq_init(volatile _BLIOT_irq* irq) {
|
||||
irq->task_created = 0;
|
||||
irq->gpio_irq_trigger = 0;
|
||||
irq->gpio_irq_arg = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _BLIOT_irq_task_start() {
|
||||
/* already started, skip */
|
||||
if (1 == g_irq.task_created) {
|
||||
return 0;
|
||||
}
|
||||
_BLIOT_irq_init(&g_irq);
|
||||
g_irq.task_created = 1;
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
__platform_printf("Creating _irq_task\r\n");
|
||||
#endif
|
||||
BaseType_t ret =
|
||||
xTaskCreate(_irq_task, (char*)"_irq_task", 4096, NULL, 15, NULL);
|
||||
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
if (pdPASS == ret) {
|
||||
__platform_printf("Create _irq_task succeed.\r\n");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
return -1;
|
||||
}
|
12
package/BLIOT/pika_hal_BLIOT_irq_task.h
Normal file
12
package/BLIOT/pika_hal_BLIOT_irq_task.h
Normal file
@ -0,0 +1,12 @@
|
||||
#include "../PikaStdDevice/pika_hal.h"
|
||||
|
||||
typedef struct {
|
||||
uint8_t task_created;
|
||||
uint8_t gpio_irq_trigger;
|
||||
void* gpio_irq_arg;
|
||||
|
||||
uint8_t uart_irq_trigger;
|
||||
void* uart_irq_arg;
|
||||
} _BLIOT_irq;
|
||||
|
||||
int _BLIOT_irq_task_start();
|
@ -68,6 +68,21 @@ class GPIO(BaseDev):
|
||||
def read(self) -> int:
|
||||
"""Read the pin value."""
|
||||
|
||||
SIGNAL_RISING: int
|
||||
SIGNAL_FALLING: int
|
||||
SIGNAL_ANY: int
|
||||
|
||||
def setCallBack(self, eventCallBack: any, filter: int):
|
||||
"""
|
||||
Add a callback function to the pin.
|
||||
Example:
|
||||
``` python
|
||||
def cb1(signal):
|
||||
print("cb1", signal)
|
||||
io.setCallBack(cb1, io.SIGNAL_RISING)
|
||||
```
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def platformHigh(self): ...
|
||||
|
||||
@ -175,10 +190,11 @@ class DAC(BaseDev):
|
||||
def disable(self):
|
||||
"""Disable the DAC."""
|
||||
|
||||
def write(self, val:float):
|
||||
def write(self, val: float):
|
||||
"""write the DAC value."""
|
||||
|
||||
class UART(BaseDev):
|
||||
|
||||
class UART:
|
||||
def __init__(self): ...
|
||||
|
||||
def setBaudRate(self, baudRate: int):
|
||||
@ -186,6 +202,14 @@ class UART(BaseDev):
|
||||
|
||||
def setId(self, id: int):
|
||||
"""Set the id of the UART."""
|
||||
|
||||
FLOW_CONTROL_NONE: int
|
||||
FLOW_CONTROL_RTS: int
|
||||
FLOW_CONTROL_CTS: int
|
||||
FLOW_CONTROL_RTS_CTS: int
|
||||
|
||||
def setFlowControl(self, flowControl: int):
|
||||
"""Set the flow control of the UART."""
|
||||
|
||||
def enable(self):
|
||||
"""Enable the UART."""
|
||||
@ -205,6 +229,21 @@ class UART(BaseDev):
|
||||
def readBytes(self, length: int) -> bytes:
|
||||
"""Read bytes from the UART."""
|
||||
|
||||
|
||||
SIGNAL_RX: int
|
||||
SIGNAL_TX: int
|
||||
|
||||
def setCallBack(self, eventCallBack: any, filter: int):
|
||||
"""
|
||||
Add a callback function to the pin.
|
||||
Example:
|
||||
``` python
|
||||
def cb1(signal):
|
||||
print(uart.read(-1))
|
||||
io.setCallBack(cb1, uart.SIGNAL_RX)
|
||||
```
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def platformEnable(self): ...
|
||||
|
||||
@ -436,7 +475,7 @@ class CAN(BaseDev):
|
||||
def readBytes(self, length: int) -> bytes:
|
||||
"""Read bytes from the CAN."""
|
||||
|
||||
def addFilter(self, id: int, ide: int, rtr: int, mode: int, mask: int, hdr: int):
|
||||
def addFilter(self, id: int, ide: int, rtr: int, mode: int, mask: int, hdr: int):
|
||||
"""Add a filter."""
|
||||
|
||||
@abstractmethod
|
||||
@ -460,7 +499,7 @@ class CAN(BaseDev):
|
||||
|
||||
class BaseDev:
|
||||
@PIKA_C_MACRO_IF("PIKA_EVENT_ENABLE")
|
||||
def addEventCallBack(self, eventCallback: any):
|
||||
def addEventCallBack(self, eventCallback: any):
|
||||
""" Add an event callback. """
|
||||
|
||||
@abstractmethod
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "PikaStdDevice_ADC.h"
|
||||
#include "BaseObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
void PikaStdDevice_ADC_enable(PikaObj* self) {
|
||||
obj_runNativeMethod(self, "platformEnable", NULL);
|
||||
|
@ -1,8 +1,5 @@
|
||||
#include "PikaStdDevice_BaseDev.h"
|
||||
#include "BaseObj.h"
|
||||
#include "PikaObj.h"
|
||||
#include "dataStrs.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
#if !PIKASCRIPT_VERSION_REQUIRE_MINIMUN(1, 10, 4)
|
||||
#error "This library requires PikaScript version 1.10.4 or higher"
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "PikaStdDevice_CAN.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
void PikaStdDevice_CAN___init__(PikaObj* self) {}
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "PikaStdDevice_DAC.h"
|
||||
#include "BaseObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
static pika_dev* _get_dev(PikaObj* self) {
|
||||
pika_dev* dev = obj_getPtr(self, "pika_dev");
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "PikaStdDevice_GPIO.h"
|
||||
#include "BaseObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
void PikaStdDevice_GPIO_init(PikaObj* self) {
|
||||
obj_setInt(self, "isEnable", 0);
|
||||
@ -22,6 +21,9 @@ void PikaStdDevice_GPIO_setId(PikaObj* self, int id) {
|
||||
|
||||
void PikaStdDevice_GPIO___init__(PikaObj* self) {
|
||||
PikaStdDevice_GPIO_init(self);
|
||||
obj_setInt(self, "SIGNAL_RISING", PIKA_HAL_GPIO_EVENT_SIGNAL_RISING);
|
||||
obj_setInt(self, "SIGNAL_FALLING", PIKA_HAL_GPIO_EVENT_SIGNAL_FALLING);
|
||||
obj_setInt(self, "SIGNAL_ANY", PIKA_HAL_GPIO_EVENT_SIGNAL_ANY);
|
||||
}
|
||||
|
||||
void PikaStdDevice_GPIO_disable(PikaObj* self) {
|
||||
@ -145,3 +147,21 @@ void PikaStdDevice_GPIO_platformRead(PikaObj* self) {
|
||||
pika_hal_read(dev, &val, sizeof(val));
|
||||
obj_setInt(self, "readBuff", val);
|
||||
}
|
||||
|
||||
void PikaStdDevice_GPIO_setCallBack(PikaObj* self,
|
||||
Arg* eventCallback,
|
||||
int filter) {
|
||||
pika_dev* dev = _get_dev(self);
|
||||
#if PIKA_EVENT_ENABLE
|
||||
_PikaStdDevice_setCallBack(self, eventCallback, (uintptr_t)dev);
|
||||
/* regist event to pika_hal */
|
||||
pika_hal_GPIO_config cfg_cb = {0};
|
||||
cfg_cb.event_callback = (void*)_PikaStdDevice_event_handler;
|
||||
cfg_cb.event_callback_filter = filter;
|
||||
cfg_cb.event_callback_ena = PIKA_HAL_EVENT_CALLBACK_ENA_ENABLE;
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_CONFIG, &cfg_cb);
|
||||
#else
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] PIKA_EVENT_ENABLE is disabled.");
|
||||
#endif
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
#include "PikaStdDevice_IIC.h"
|
||||
#include "BaseObj.h"
|
||||
#include "PikaObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
void PikaStdDevice_IIC_init(PikaObj* self) {
|
||||
obj_setInt(self, "deviceAddr", 0);
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "PikaStdDevice_PWM.h"
|
||||
#include "BaseObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
void PikaStdDevice_PWM_init(PikaObj* self) {
|
||||
obj_setStr(self, "pin", "none");
|
||||
|
@ -1,7 +1,5 @@
|
||||
#include "PikaStdDevice_SPI.h"
|
||||
#include "BaseObj.h"
|
||||
#include "PikaObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
void PikaStdDevice_SPI___init__(PikaObj* self) {
|
||||
obj_setInt(self, "baudRate", 1000);
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "PikaStdDevice_Time.h"
|
||||
#include "BaseObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
#if defined(__linux)
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
@ -14,7 +13,7 @@ void PikaStdDevice_Time_sleep_ms(PikaObj* self, int ms) {
|
||||
#elif defined(_WIN32)
|
||||
Sleep(ms);
|
||||
#else
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
__platform_sleep_ms(ms);
|
||||
#endif
|
||||
}
|
||||
void PikaStdDevice_Time_sleep_s(PikaObj* self, int s) {
|
||||
@ -23,13 +22,12 @@ void PikaStdDevice_Time_sleep_s(PikaObj* self, int s) {
|
||||
#elif defined(_WIN32)
|
||||
Sleep(s * 1000);
|
||||
#else
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
__platform_sleep_s(s);
|
||||
#endif
|
||||
}
|
||||
|
||||
void PikaStdDevice_Time_platformGetTick(PikaObj* self) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] platformGetTick() need to be override.");
|
||||
obj_setInt(self, "tick", __platform_getTick());
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "PikaStdDevice_UART.h"
|
||||
#include "BaseObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
void PikaStdDevice_UART_enable(PikaObj* self) {
|
||||
obj_runNativeMethod(self, "platformEnable", NULL);
|
||||
@ -11,9 +10,20 @@ void PikaStdDevice_UART_disable(PikaObj* self) {
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_init(PikaObj* self) {
|
||||
/* const */
|
||||
obj_setInt(self, "FLOW_CONTROL_NONE", PIKA_HAL_UART_FLOW_CONTROL_NONE);
|
||||
obj_setInt(self, "FLOW_CONTROL_RTS", PIKA_HAL_UART_FLOW_CONTROL_RTS);
|
||||
obj_setInt(self, "FLOW_CONTROL_CTS", PIKA_HAL_UART_FLOW_CONTROL_CTS);
|
||||
obj_setInt(self, "FLOW_CONTROL_RTS_CTS",
|
||||
PIKA_HAL_UART_FLOW_CONTROL_RTS_CTS);
|
||||
|
||||
obj_setInt(self, "SIGNAL_RX", PIKA_HAL_UART_EVENT_SIGNAL_RX);
|
||||
obj_setInt(self, "SIGNAL_TX", PIKA_HAL_UART_EVENT_SIGNAL_TX);
|
||||
|
||||
obj_setInt(self, "baudRate", 115200);
|
||||
obj_setInt(self, "id", 1);
|
||||
obj_setStr(self, "readBuff", "");
|
||||
obj_setInt(self, "flowControl", PIKA_HAL_UART_FLOW_CONTROL_NONE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART___init__(PikaObj* self) {
|
||||
@ -35,9 +45,15 @@ Arg* PikaStdDevice_UART_readBytes(PikaObj* self, int length) {
|
||||
void PikaStdDevice_UART_setBaudRate(PikaObj* self, int baudRate) {
|
||||
obj_setInt(self, "baudRate", baudRate);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_setFlowControl(PikaObj* self, int flowControl) {
|
||||
obj_setInt(self, "flowControl", flowControl);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_setId(PikaObj* self, int id) {
|
||||
obj_setInt(self, "id", id);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_write(PikaObj* self, char* data) {
|
||||
obj_setStr(self, "writeData", data);
|
||||
obj_runNativeMethod(self, "platformWrite", NULL);
|
||||
@ -48,24 +64,92 @@ void PikaStdDevice_UART_writeBytes(PikaObj* self, uint8_t* data, int length) {
|
||||
obj_runNativeMethod(self, "platformWriteBytes", NULL);
|
||||
}
|
||||
|
||||
static pika_dev* _get_dev(PikaObj* self) {
|
||||
pika_dev* dev = obj_getPtr(self, "pika_dev");
|
||||
if (NULL != dev) {
|
||||
return dev;
|
||||
}
|
||||
int id = obj_getInt(self, "id");
|
||||
char id_str[32] = {0};
|
||||
sprintf(id_str, "UART%d", id);
|
||||
dev = pika_hal_open(PIKA_HAL_UART, id_str);
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open UART '%s' failed.\r\n", id_str);
|
||||
}
|
||||
obj_setPtr(self, "pika_dev", dev);
|
||||
return dev;
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_platformEnable(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
pika_dev* dev = _get_dev(self);
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open UART '%d' failed.\r\n",
|
||||
obj_getInt(self, "id"));
|
||||
return;
|
||||
}
|
||||
pika_hal_UART_config cfg = {0};
|
||||
cfg.baudrate = obj_getInt(self, "baudRate");
|
||||
cfg.flow_control = obj_getInt(self, "flowControl");
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_CONFIG, &cfg);
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_ENABLE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_platformRead(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
int len = obj_getInt(self, "length");
|
||||
obj_setBytes(self, "_readData", NULL, len + 1);
|
||||
char* buff = (char*)obj_getBytes(self, "_readData");
|
||||
pika_dev* dev = _get_dev(self);
|
||||
int len_get = pika_hal_read(dev, buff, len);
|
||||
buff[len_get] = 0;
|
||||
obj_setStr(self, "readData", buff);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_platformWrite(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
char* data = obj_getStr(self, "writeData");
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_write(dev, data, strlen(data));
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_platformDisable(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
pika_dev* dev = _get_dev(self);
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open UART '%d' failed.\r\n",
|
||||
obj_getInt(self, "id"));
|
||||
return;
|
||||
}
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_DISABLE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_platformReadBytes(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
int len = obj_getInt(self, "length");
|
||||
obj_setBytes(self, "_readData", NULL, len + 1);
|
||||
uint8_t* buff = obj_getBytes(self, "_readData");
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_read(dev, buff, len);
|
||||
obj_setBytes(self, "readData", buff, len);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_platformWriteBytes(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
uint8_t* data = obj_getBytes(self, "writeData");
|
||||
int len = obj_getBytesSize(self, "writeData");
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_write(dev, data, len);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_setCallBack(PikaObj* self,
|
||||
Arg* eventCallBack,
|
||||
int filter) {
|
||||
pika_dev* dev = _get_dev(self);
|
||||
#if PIKA_EVENT_ENABLE
|
||||
_PikaStdDevice_setCallBack(self, eventCallBack, (uintptr_t)dev);
|
||||
/* regist event to pika_hal */
|
||||
pika_hal_UART_config cfg_cb = {0};
|
||||
cfg_cb.event_callback = (void*)_PikaStdDevice_event_handler;
|
||||
cfg_cb.event_callback_filter = filter;
|
||||
cfg_cb.event_callback_ena = PIKA_HAL_EVENT_CALLBACK_ENA_ENABLE;
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_CONFIG, &cfg_cb);
|
||||
#else
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] PIKA_EVENT_ENABLE is disabled.");
|
||||
#endif
|
||||
}
|
||||
|
19
package/PikaStdDevice/PikaStdDevice_common.c
Normal file
19
package/PikaStdDevice/PikaStdDevice_common.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
extern PikaEventListener* g_pika_device_event_listener;
|
||||
void _PikaStdDevice_event_handler(pika_dev* dev, int signal) {
|
||||
pks_eventLisener_sendSignal(g_pika_device_event_listener, (uintptr_t)dev,
|
||||
signal);
|
||||
}
|
||||
|
||||
void _PikaStdDevice_setCallBack(PikaObj* self,
|
||||
Arg* eventCallback,
|
||||
uint32_t eventId) {
|
||||
obj_setArg(self, "eventCallBack", eventCallback);
|
||||
/* init event_listener for the first time */
|
||||
if (NULL == g_pika_device_event_listener) {
|
||||
pks_eventLisener_init(&g_pika_device_event_listener);
|
||||
}
|
||||
/* regist event to event listener */
|
||||
pks_eventLicener_registEvent(g_pika_device_event_listener, eventId, self);
|
||||
}
|
12
package/PikaStdDevice/PikaStdDevice_common.h
Normal file
12
package/PikaStdDevice/PikaStdDevice_common.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef _PIKA_STDDEVICE_COMMON_H
|
||||
#define _PIKA_STDDEVICE_COMMON_H
|
||||
#include "PikaObj.h"
|
||||
#include "PikaVM.h"
|
||||
#include "pika_hal.h"
|
||||
void _PikaStdDevice_setCallBack(PikaObj* self,
|
||||
Arg* eventCallback,
|
||||
uint32_t eventId);
|
||||
|
||||
void _PikaStdDevice_event_handler(pika_dev* dev, int signal);
|
||||
|
||||
#endif
|
@ -53,6 +53,7 @@ __exit:
|
||||
__platform_printf("Error: dev_open failed.\r\n");
|
||||
if (dev->ioctl_config) {
|
||||
pikaFree(dev->ioctl_config, _pika_hal_dev_config_size(dev_type));
|
||||
dev->ioctl_config = NULL;
|
||||
}
|
||||
if (dev) {
|
||||
pikaFree(dev, sizeof(pika_dev));
|
||||
@ -71,11 +72,12 @@ int pika_hal_close(pika_dev* dev) {
|
||||
}
|
||||
ret = impl->close(dev);
|
||||
__exit:
|
||||
if (NULL != dev) {
|
||||
pikaFree(dev, sizeof(pika_dev));
|
||||
}
|
||||
if (NULL != dev->ioctl_config) {
|
||||
pikaFree(dev->ioctl_config, _pika_hal_dev_config_size(dev->type));
|
||||
dev->ioctl_config = NULL;
|
||||
}
|
||||
if (NULL != dev) {
|
||||
pikaFree(dev, sizeof(pika_dev));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@ -181,8 +183,11 @@ int pika_hal_GPIO_ioctl_merge_config(pika_hal_GPIO_config* dst,
|
||||
// printf("after merge: dst->dir=%d, src->dir=%d\r\n", dst->dir, src->dir);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(pull, PIKA_HAL_GPIO_PULL_NONE);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(speed, PIKA_HAL_GPIO_SPEED_10M);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_rising, NULL);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_falling, NULL);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback, NULL);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_filter,
|
||||
PIKA_HAL_GPIO_EVENT_SIGNAL_RISING);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_ena,
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA_ENABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -192,7 +197,12 @@ int pika_hal_UART_ioctl_merge_config(pika_hal_UART_config* dst,
|
||||
_IOCTL_CONFIG_USE_DEFAULT(data_bits, PIKA_HAL_UART_DATA_BITS_8);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(stop_bits, PIKA_HAL_UART_STOP_BITS_1);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(parity, PIKA_HAL_UART_PARITY_NONE);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_rx, NULL);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(flow_control, PIKA_HAL_UART_FLOW_CONTROL_NONE);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback, NULL);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_filter,
|
||||
PIKA_HAL_UART_EVENT_SIGNAL_RX);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_ena,
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA_ENABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -58,12 +58,26 @@ typedef enum {
|
||||
PIKA_HAL_GPIO_SPEED_100M = 100000000,
|
||||
} PIKA_HAL_GPIO_SPEED;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_EVENT_CALLBACK_ENA_UNUSED = 0,
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA_ENABLE,
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA_DISABLE,
|
||||
} PIKA_HAL_EVENT_CALLBACK_ENA;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_GPIO_EVENT_SIGNAL_UNUSED = 0,
|
||||
PIKA_HAL_GPIO_EVENT_SIGNAL_RISING,
|
||||
PIKA_HAL_GPIO_EVENT_SIGNAL_FALLING,
|
||||
PIKA_HAL_GPIO_EVENT_SIGNAL_ANY,
|
||||
} PIKA_HAL_GPIO_EVENT_SIGNAL;
|
||||
|
||||
typedef struct {
|
||||
PIKA_HAL_GPIO_DIR dir;
|
||||
PIKA_HAL_GPIO_PULL pull;
|
||||
PIKA_HAL_GPIO_SPEED speed;
|
||||
void (*event_callback_rising)(pika_dev* dev);
|
||||
void (*event_callback_falling)(pika_dev* dev);
|
||||
void (*event_callback)(pika_dev* dev, PIKA_HAL_GPIO_EVENT_SIGNAL signal);
|
||||
PIKA_HAL_GPIO_EVENT_SIGNAL event_callback_filter;
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA event_callback_ena;
|
||||
} pika_hal_GPIO_config;
|
||||
|
||||
typedef enum {
|
||||
@ -89,6 +103,7 @@ typedef enum {
|
||||
_PIKA_HAL_UART_STOP_BITS_UNUSED = 0,
|
||||
PIKA_HAL_UART_STOP_BITS_1 = 1,
|
||||
PIKA_HAL_UART_STOP_BITS_2 = 2,
|
||||
PIKA_HAL_UART_STOP_BITS_1_5 = 3,
|
||||
} PIKA_HAL_UART_STOP_BITS;
|
||||
|
||||
typedef enum {
|
||||
@ -98,12 +113,30 @@ typedef enum {
|
||||
PIKA_HAL_UART_PARITY_EVEN,
|
||||
} PIKA_HAL_UART_PARITY;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_UART_EVENT_SIGNAL_UNUSED = 0,
|
||||
PIKA_HAL_UART_EVENT_SIGNAL_RX,
|
||||
PIKA_HAL_UART_EVENT_SIGNAL_TX,
|
||||
PIKA_HAL_UART_EVENT_SIGNAL_ANY,
|
||||
} PIKA_HAL_UART_EVENT_SIGNAL;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_UART_FLOW_CONTROL_UNUSED = 0,
|
||||
PIKA_HAL_UART_FLOW_CONTROL_NONE,
|
||||
PIKA_HAL_UART_FLOW_CONTROL_RTS,
|
||||
PIKA_HAL_UART_FLOW_CONTROL_CTS,
|
||||
PIKA_HAL_UART_FLOW_CONTROL_RTS_CTS,
|
||||
} PIKA_HAL_UART_FLOW_CONTROL;
|
||||
|
||||
typedef struct {
|
||||
PIKA_HAL_UART_BAUDRATE baudrate;
|
||||
PIKA_HAL_UART_DATA_BITS data_bits;
|
||||
PIKA_HAL_UART_STOP_BITS stop_bits;
|
||||
PIKA_HAL_UART_PARITY parity;
|
||||
void (*event_callback_rx)(pika_dev* dev);
|
||||
PIKA_HAL_UART_FLOW_CONTROL flow_control;
|
||||
void (*event_callback)(pika_dev* dev, PIKA_HAL_UART_EVENT_SIGNAL signal);
|
||||
PIKA_HAL_UART_EVENT_SIGNAL event_callback_filter;
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA event_callback_ena;
|
||||
} pika_hal_UART_config;
|
||||
|
||||
typedef uint32_t PIKA_HAL_IIC_SLAVE_ADDR;
|
||||
|
@ -40,7 +40,7 @@ Arg* PikaStdData_Dict___iter__(PikaObj* self) {
|
||||
Arg* PikaStdData_Dict___next__(PikaObj* self) {
|
||||
int __iter_i = args_getInt(self->list, "__iter_i");
|
||||
PikaDict* keys = obj_getPtr(self, "_keys");
|
||||
Arg* res = arg_copy(args_getArgByidex(&keys->super, __iter_i));
|
||||
Arg* res = arg_copy(args_getArgByIndex(&keys->super, __iter_i));
|
||||
if (NULL == res) {
|
||||
return arg_newNull();
|
||||
}
|
||||
@ -91,7 +91,7 @@ Arg* PikaStdData_dict_keys___next__(PikaObj* self) {
|
||||
int __iter_i = args_getInt(self->list, "__iter_i");
|
||||
PikaObj* dictptr = obj_getPtr(self, "dictptr");
|
||||
PikaDict* keys = obj_getPtr(dictptr, "_keys");
|
||||
Arg* res = arg_copy(args_getArgByidex(&keys->super, __iter_i));
|
||||
Arg* res = arg_copy(args_getArgByIndex(&keys->super, __iter_i));
|
||||
if (NULL == res) {
|
||||
return arg_newNull();
|
||||
}
|
||||
@ -107,7 +107,7 @@ char* PikaStdData_dict_keys___str__(PikaObj* self) {
|
||||
|
||||
int i = 0;
|
||||
while (PIKA_TRUE) {
|
||||
Arg* item = args_getArgByidex(&keys->super, i);
|
||||
Arg* item = args_getArgByIndex(&keys->super, i);
|
||||
if (NULL == item) {
|
||||
break;
|
||||
}
|
||||
@ -141,8 +141,8 @@ char* PikaStdData_Dict___str__(PikaObj* self) {
|
||||
|
||||
int i = 0;
|
||||
while (PIKA_TRUE) {
|
||||
Arg* item_key = args_getArgByidex(&keys->super, i);
|
||||
Arg* item_val = args_getArgByidex(&dict->super, i);
|
||||
Arg* item_key = args_getArgByIndex(&keys->super, i);
|
||||
Arg* item_val = args_getArgByIndex(&dict->super, i);
|
||||
if (NULL == item_key) {
|
||||
break;
|
||||
}
|
||||
@ -186,7 +186,7 @@ int PikaStdData_dict_keys___len__(PikaObj* self) {
|
||||
int dict_contains(PikaDict* dict, Arg* key) {
|
||||
int i = 0;
|
||||
while (PIKA_TRUE) {
|
||||
Arg* item = args_getArgByidex(&dict->super, i);
|
||||
Arg* item = args_getArgByIndex(&dict->super, i);
|
||||
if (NULL == item) {
|
||||
break;
|
||||
}
|
||||
@ -219,8 +219,8 @@ Arg* PikaStdData_dict_items___next__(PikaObj* self) {
|
||||
PikaObj* dictptr = obj_getPtr(self, "dictptr");
|
||||
PikaDict* keys = obj_getPtr(dictptr, "_keys");
|
||||
PikaDict* dict = obj_getPtr(dictptr, "dict");
|
||||
Arg* key = args_getArgByidex(&keys->super, __iter_i);
|
||||
Arg* val = args_getArgByidex(&dict->super, __iter_i);
|
||||
Arg* key = args_getArgByIndex(&keys->super, __iter_i);
|
||||
Arg* val = args_getArgByIndex(&dict->super, __iter_i);
|
||||
if (NULL == key) {
|
||||
return arg_newNull();
|
||||
}
|
||||
|
@ -443,7 +443,11 @@ static char* __print_arg(PikaObj* self, Arg* val) {
|
||||
}
|
||||
if (arg_type == ARG_TYPE_INT) {
|
||||
int64_t value = arg_getInt(val);
|
||||
#if PIKA_PRINT_LLD_ENABLE
|
||||
res = strsFormat(&buffs, 32, "%lld", value);
|
||||
#else
|
||||
res = strsFormat(&buffs, 32, "%d", value);
|
||||
#endif
|
||||
goto __exit;
|
||||
}
|
||||
if (arg_type == ARG_TYPE_FLOAT) {
|
||||
|
@ -10,6 +10,7 @@ class GPIO(PikaStdDevice.GPIO):
|
||||
def platformSetMode(self): ...
|
||||
def platformRead(self): ...
|
||||
def platformGetEventId(self): ...
|
||||
def eventTest(self): ...
|
||||
|
||||
|
||||
class Time(PikaStdDevice.Time):
|
||||
|
@ -14,3 +14,15 @@ void TemplateDevice_GPIO_platformGetEventId(PikaObj* self) {
|
||||
obj_setInt(self, "eventId", GPIO_PA8_EVENT_ID);
|
||||
}
|
||||
}
|
||||
|
||||
extern PikaEventListener* g_pika_device_event_listener;
|
||||
#define EVENT_SIGNAL_IO_RISING_EDGE 0x01
|
||||
#define EVENT_SIGNAL_IO_FALLING_EDGE 0x02
|
||||
#define GPIO_PA8_EVENT_ID 0x08
|
||||
|
||||
void TemplateDevice_GPIO_eventTest(PikaObj* self) {
|
||||
pks_eventLisener_sendSignal(g_pika_device_event_listener, GPIO_PA8_EVENT_ID,
|
||||
EVENT_SIGNAL_IO_FALLING_EDGE);
|
||||
pks_eventLisener_sendSignal(g_pika_device_event_listener, GPIO_PA8_EVENT_ID,
|
||||
EVENT_SIGNAL_IO_RISING_EDGE);
|
||||
}
|
||||
|
@ -1,11 +1,12 @@
|
||||
#include "base64.h"
|
||||
#include "_base64.h"
|
||||
|
||||
#include "mbedtls/base64.h"
|
||||
|
||||
Arg* base64_b64decode(PikaObj* self, Arg* s) {
|
||||
Arg* _base64_b64decode(PikaObj* self, Arg* s) {
|
||||
ArgType t = arg_getType(s);
|
||||
if (ARG_TYPE_BYTES != t) {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
obj_setSysOut(self, "base64.b64decode input not bytes");
|
||||
}
|
||||
|
||||
uint8_t* input_buff = arg_getBytes(s);
|
||||
@ -20,14 +21,15 @@ Arg* base64_b64decode(PikaObj* self, Arg* s) {
|
||||
return r;
|
||||
}
|
||||
|
||||
Arg* base64_b64encode(PikaObj* self, Arg* s) {
|
||||
Arg* _base64_b64encode(PikaObj* self, Arg* s) {
|
||||
ArgType t = arg_getType(s);
|
||||
if (ARG_TYPE_BYTES != t) {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
obj_setSysOut(self, "base64.b64encode input not bytes");
|
||||
}
|
||||
uint8_t* input_buff = arg_getBytes(s);
|
||||
size_t input_len = arg_getBytesSize(s);
|
||||
obj_setBytes(self, "__de_buff", NULL, (size_t)(input_len * 1.5));
|
||||
obj_setBytes(self, "__de_buff", NULL, (size_t)(input_len * 2));
|
||||
uint8_t* output_buff = obj_getBytes(self, "__de_buff");
|
||||
size_t output_len = 0;
|
||||
mbedtls_base64_encode(output_buff, obj_getBytesSize(self, "__de_buff"),
|
2
package/base64/_base64.pyi
Normal file
2
package/base64/_base64.pyi
Normal file
@ -0,0 +1,2 @@
|
||||
def b64encode(s: any) -> any: ...
|
||||
def b64decode(s: any) -> any: ...
|
9
package/base64/base64.py
Normal file
9
package/base64/base64.py
Normal file
@ -0,0 +1,9 @@
|
||||
import _base64
|
||||
|
||||
|
||||
def b64encode(s: any):
|
||||
return _base64.b64encode(s)
|
||||
|
||||
|
||||
def b64decode(s: any):
|
||||
return _base64.b64decode(s)
|
@ -1,2 +0,0 @@
|
||||
def b64encode(self, s:any) -> any: ...
|
||||
def b64decode(self, s:any) -> any: ...
|
@ -1,11 +1,7 @@
|
||||
#include "_hmac_HMAC.h"
|
||||
|
||||
#include "mbedtls/md.h"
|
||||
#include "string.h"
|
||||
|
||||
#include "mbedtls/md5.h"
|
||||
#include "mbedtls/sha1.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
|
||||
enum {
|
||||
PIKA_HMAC_MD5 = 16,
|
||||
PIKA_HMAC_SHA1 = 20,
|
||||
@ -14,220 +10,91 @@ enum {
|
||||
|
||||
static void hmac_to_hex(uint8_t* s, int l, uint8_t* d);
|
||||
|
||||
static void init_buff(PikaObj* self, size_t h) {
|
||||
obj_setBytes(self, "_buff", NULL, h);
|
||||
obj_setBytes(self, "_hexbuff", NULL, (h * 2));
|
||||
memset(obj_getBytes(self, "_buff"), 0, h);
|
||||
memset(obj_getBytes(self, "_hexbuff"), 0, (h * 2));
|
||||
}
|
||||
|
||||
void _hmac_HMAC_new(PikaObj* self, Arg* key, Arg* msg, char* digestmod) {
|
||||
ArgType t;
|
||||
t = arg_getType(key);
|
||||
if (ARG_TYPE_NONE != t) {
|
||||
if (ARG_TYPE_BYTES != t) {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
}
|
||||
if (ARG_TYPE_BYTES != t) {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
obj_setSysOut(self, "hmac.new() key type error");
|
||||
}
|
||||
|
||||
t = arg_getType(msg);
|
||||
if (ARG_TYPE_NONE != t) {
|
||||
if (ARG_TYPE_BYTES != t) {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
obj_setSysOut(self, "hmac.new() msg type error");
|
||||
}
|
||||
}
|
||||
|
||||
obj_setInt(self, "_digest_flags", 0); // flag
|
||||
obj_setBytes(self, "_buff", NULL, PIKA_HMAC_SHA256); // digest buff
|
||||
obj_setBytes(self, "_hexbuff", NULL,
|
||||
PIKA_HMAC_SHA256 * 2); // hexdigest buff
|
||||
obj_setBytes(self, "_k_ipad", NULL, 64); // key ipad
|
||||
obj_setBytes(self, "_k_opad", NULL, 64); // key opad
|
||||
|
||||
size_t key_len = arg_getBytesSize(key);
|
||||
uint8_t* key_data = arg_getBytes(key);
|
||||
size_t msg_len = arg_getBytesSize(msg);
|
||||
uint8_t* msg_data = arg_getBytes(msg);
|
||||
obj_setInt(self, "_digest_flags", 0); // flag
|
||||
mbedtls_md_context_t ctx;
|
||||
mbedtls_md_init(&ctx);
|
||||
|
||||
uint8_t* k_ipad = obj_getBytes(self, "_k_ipad");
|
||||
uint8_t* k_opad = obj_getBytes(self, "_k_opad");
|
||||
|
||||
if (strcmp(digestmod, "hmac-md5") == 0 ||
|
||||
strcmp(digestmod, "HMAC-MD5") == 0) {
|
||||
mbedtls_md5_context context;
|
||||
|
||||
if (key_len > 64) {
|
||||
mbedtls_md5_init(&context);
|
||||
mbedtls_md5_starts(&context);
|
||||
mbedtls_md5_update(&context, key_data, key_len);
|
||||
mbedtls_md5_finish(&context, k_ipad);
|
||||
mbedtls_md5_free(&context);
|
||||
memcpy(k_opad, k_ipad, 64);
|
||||
} else {
|
||||
memcpy(k_ipad, key_data, key_len);
|
||||
memcpy(k_opad, key_data, key_len);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 64; i++) {
|
||||
k_ipad[i] ^= 0x36;
|
||||
k_opad[i] ^= 0x5c;
|
||||
}
|
||||
|
||||
mbedtls_md5_init(&context);
|
||||
mbedtls_md5_starts(&context);
|
||||
mbedtls_md5_update(&context, k_ipad, 64);
|
||||
if (msg_len > 0) {
|
||||
mbedtls_md5_update(&context, msg_data, msg_len);
|
||||
}
|
||||
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HMAC_MD5);
|
||||
} else if (strcmp(digestmod, "hmac-sha1") == 0 ||
|
||||
strcmp(digestmod, "HMAC-SHA1") == 0) {
|
||||
mbedtls_sha1_context context;
|
||||
|
||||
if (key_len > 64) {
|
||||
mbedtls_sha1_init(&context);
|
||||
mbedtls_sha1_starts(&context);
|
||||
mbedtls_sha1_update(&context, key_data, key_len);
|
||||
mbedtls_sha1_finish(&context, k_ipad);
|
||||
mbedtls_sha1_free(&context);
|
||||
memcpy(k_opad, k_ipad, 64);
|
||||
} else {
|
||||
memcpy(k_ipad, key_data, key_len);
|
||||
memcpy(k_opad, key_data, key_len);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 64; i++) {
|
||||
k_ipad[i] ^= 0x36;
|
||||
k_opad[i] ^= 0x5c;
|
||||
}
|
||||
|
||||
mbedtls_sha1_init(&context);
|
||||
mbedtls_sha1_starts(&context);
|
||||
mbedtls_sha1_update(&context, k_ipad, 64);
|
||||
if (msg_len > 0) {
|
||||
mbedtls_sha1_update(&context, msg_data, msg_len);
|
||||
}
|
||||
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HMAC_SHA1);
|
||||
} else if (strcmp(digestmod, "hmac-sha256") == 0 ||
|
||||
strcmp(digestmod, "HMAC-SHA256") == 0) {
|
||||
mbedtls_sha256_context context;
|
||||
|
||||
if (key_len > 64) {
|
||||
mbedtls_sha256_init(&context);
|
||||
mbedtls_sha256_starts(&context, 0);
|
||||
mbedtls_sha256_update(&context, key_data, key_len);
|
||||
mbedtls_sha256_finish(&context, k_ipad);
|
||||
mbedtls_sha256_free(&context);
|
||||
memcpy(k_opad, k_ipad, 64);
|
||||
} else {
|
||||
memcpy(k_ipad, key_data, key_len);
|
||||
memcpy(k_opad, key_data, key_len);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 64; i++) {
|
||||
k_ipad[i] ^= 0x36;
|
||||
k_opad[i] ^= 0x5c;
|
||||
}
|
||||
|
||||
mbedtls_sha256_init(&context);
|
||||
mbedtls_sha256_starts(&context, 0);
|
||||
mbedtls_sha256_update(&context, k_ipad, 64);
|
||||
if (msg_len > 0) {
|
||||
mbedtls_sha256_update(&context, msg_data, msg_len);
|
||||
}
|
||||
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HMAC_SHA256);
|
||||
if (strcmp(digestmod, "md5") == 0 ||
|
||||
strcmp(digestmod, "MD5") == 0) {
|
||||
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_MD5), 1);
|
||||
obj_setInt(self, "_mode", PIKA_HMAC_MD5);
|
||||
init_buff(self, PIKA_HMAC_MD5);
|
||||
} else if (strcmp(digestmod, "sha1") == 0 ||
|
||||
strcmp(digestmod, "SHA1") == 0) {
|
||||
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), 1);
|
||||
obj_setInt(self, "_mode", PIKA_HMAC_SHA1);
|
||||
init_buff(self, PIKA_HMAC_SHA1);
|
||||
} else if (strcmp(digestmod, "sha256") == 0 ||
|
||||
strcmp(digestmod, "SHA256") == 0) {
|
||||
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1);
|
||||
obj_setInt(self, "_mode", PIKA_HMAC_SHA256);
|
||||
init_buff(self, PIKA_HMAC_SHA256);
|
||||
} else {
|
||||
obj_setErrorCode(self, -1); // not support mode
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
obj_setSysOut(self, "hmac.new() not support mode");
|
||||
}
|
||||
mbedtls_md_hmac_starts(&ctx, key_data, key_len);
|
||||
if (msg_len > 0) {
|
||||
mbedtls_md_hmac_update(&ctx, msg_data, msg_len);
|
||||
}
|
||||
obj_setStruct(self, "_context", ctx);
|
||||
}
|
||||
|
||||
void _hmac_HMAC_update(PikaObj* self, Arg* msg) {
|
||||
ArgType t = arg_getType(msg);
|
||||
if (ARG_TYPE_BYTES != t) {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
obj_setSysOut(self, "hmac.update() msg type error");
|
||||
}
|
||||
|
||||
size_t msg_len = arg_getBytesSize(msg);
|
||||
uint8_t* msg_data = arg_getBytes(msg);
|
||||
void* context = obj_getStruct(self, "context");
|
||||
|
||||
if (msg_len > 0) {
|
||||
switch (obj_getInt(self, "mode")) {
|
||||
case PIKA_HMAC_MD5:
|
||||
mbedtls_md5_update((mbedtls_md5_context*)context, msg_data,
|
||||
msg_len);
|
||||
break;
|
||||
case PIKA_HMAC_SHA1:
|
||||
mbedtls_sha1_update((mbedtls_sha1_context*)context, msg_data,
|
||||
msg_len);
|
||||
break;
|
||||
case PIKA_HMAC_SHA256:
|
||||
mbedtls_sha256_update((mbedtls_sha256_context*)context,
|
||||
msg_data, msg_len);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
}
|
||||
mbedtls_md_context_t* ctx = obj_getStruct(self, "_context");
|
||||
mbedtls_md_hmac_update(ctx, msg_data, msg_len);
|
||||
}
|
||||
|
||||
Arg* _hmac_HMAC_digest(PikaObj* self) {
|
||||
uint8_t* k_opad = obj_getBytes(self, "_k_opad");
|
||||
uint8_t* buff = obj_getBytes(self, "_buff");
|
||||
uint8_t flag = obj_getInt(self, "_digest_flags");
|
||||
|
||||
if (flag & 0x01) // already digest
|
||||
{
|
||||
return arg_newBytes(buff, obj_getInt(self, "mode"));
|
||||
goto exit;
|
||||
} else {
|
||||
void* context = obj_getStruct(self, "context");
|
||||
uint8_t mode = obj_getInt(self, "mode");
|
||||
switch (mode) {
|
||||
case PIKA_HMAC_MD5:
|
||||
mbedtls_md5_finish((mbedtls_md5_context*)context, buff);
|
||||
mbedtls_md5_free((mbedtls_md5_context*)context);
|
||||
|
||||
mbedtls_md5_init((mbedtls_md5_context*)context);
|
||||
mbedtls_md5_starts((mbedtls_md5_context*)context);
|
||||
mbedtls_md5_update((mbedtls_md5_context*)context, k_opad, 64);
|
||||
mbedtls_md5_update((mbedtls_md5_context*)context, buff,
|
||||
PIKA_HMAC_MD5);
|
||||
mbedtls_md5_finish((mbedtls_md5_context*)context, buff);
|
||||
mbedtls_md5_free((mbedtls_md5_context*)context);
|
||||
break;
|
||||
case PIKA_HMAC_SHA1:
|
||||
mbedtls_sha1_finish((mbedtls_sha1_context*)context, buff);
|
||||
mbedtls_sha1_free((mbedtls_sha1_context*)context);
|
||||
|
||||
mbedtls_sha1_init((mbedtls_sha1_context*)context);
|
||||
mbedtls_sha1_starts((mbedtls_sha1_context*)context);
|
||||
mbedtls_sha1_update((mbedtls_sha1_context*)context, k_opad, 64);
|
||||
mbedtls_sha1_update((mbedtls_sha1_context*)context, buff,
|
||||
PIKA_HMAC_SHA1);
|
||||
mbedtls_sha1_finish((mbedtls_sha1_context*)context, buff);
|
||||
mbedtls_sha1_free((mbedtls_sha1_context*)context);
|
||||
break;
|
||||
case PIKA_HMAC_SHA256:
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
mbedtls_sha256_free((mbedtls_sha256_context*)context);
|
||||
|
||||
mbedtls_sha256_init((mbedtls_sha256_context*)context);
|
||||
mbedtls_sha256_starts((mbedtls_sha256_context*)context, 0);
|
||||
mbedtls_sha256_update((mbedtls_sha256_context*)context, k_opad,
|
||||
64);
|
||||
mbedtls_sha256_update((mbedtls_sha256_context*)context, buff,
|
||||
PIKA_HMAC_SHA256);
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
mbedtls_sha256_free((mbedtls_sha256_context*)context);
|
||||
break;
|
||||
default:
|
||||
obj_setErrorCode(self, -1); // not support mode
|
||||
return arg_newNull(); // will not actually return to the python
|
||||
break;
|
||||
}
|
||||
mbedtls_md_context_t* ctx = obj_getStruct(self, "_context");
|
||||
mbedtls_md_hmac_finish(ctx, buff);
|
||||
obj_setInt(self, "_digest_flags", flag | 0x01);
|
||||
return arg_newBytes(buff, mode);
|
||||
goto exit;
|
||||
}
|
||||
exit:
|
||||
return arg_newBytes(buff, obj_getInt(self, "_mode"));
|
||||
}
|
||||
|
||||
char* _hmac_HMAC_hexdigest(PikaObj* self) {
|
||||
@ -235,61 +102,20 @@ char* _hmac_HMAC_hexdigest(PikaObj* self) {
|
||||
uint8_t* hexbuff = obj_getBytes(self, "_hexbuff");
|
||||
uint8_t flag = obj_getInt(self, "_digest_flags");
|
||||
|
||||
if (flag & 0x01) { // already digest
|
||||
hmac_to_hex(buff, obj_getInt(self, "mode"), hexbuff);
|
||||
if (flag & 0x01) { // already digest
|
||||
obj_setInt(self, "_digest_flags", flag | 0x02); // set hexdigest flag
|
||||
} else if (flag & 0x02) { // already hexdigest
|
||||
|
||||
goto exit;
|
||||
} else if (flag & 0x02) { // already hexdigest
|
||||
return (char*)hexbuff;
|
||||
} else {
|
||||
void* context = obj_getStruct(self, "context");
|
||||
uint8_t* k_opad = obj_getBytes(self, "_k_opad");
|
||||
|
||||
switch (obj_getInt(self, "mode")) {
|
||||
case PIKA_HMAC_MD5:
|
||||
mbedtls_md5_finish((mbedtls_md5_context*)context, buff);
|
||||
mbedtls_md5_free((mbedtls_md5_context*)context);
|
||||
|
||||
mbedtls_md5_init((mbedtls_md5_context*)context);
|
||||
mbedtls_md5_starts((mbedtls_md5_context*)context);
|
||||
mbedtls_md5_update((mbedtls_md5_context*)context, k_opad, 64);
|
||||
mbedtls_md5_update((mbedtls_md5_context*)context, buff,
|
||||
PIKA_HMAC_MD5);
|
||||
mbedtls_md5_finish((mbedtls_md5_context*)context, buff);
|
||||
mbedtls_md5_free((mbedtls_md5_context*)context);
|
||||
break;
|
||||
case PIKA_HMAC_SHA1:
|
||||
mbedtls_sha1_finish((mbedtls_sha1_context*)context, buff);
|
||||
mbedtls_sha1_free((mbedtls_sha1_context*)context);
|
||||
|
||||
mbedtls_sha1_init((mbedtls_sha1_context*)context);
|
||||
mbedtls_sha1_starts((mbedtls_sha1_context*)context);
|
||||
mbedtls_sha1_update((mbedtls_sha1_context*)context, k_opad, 64);
|
||||
mbedtls_sha1_update((mbedtls_sha1_context*)context, buff,
|
||||
PIKA_HMAC_SHA1);
|
||||
mbedtls_sha1_finish((mbedtls_sha1_context*)context, buff);
|
||||
mbedtls_sha1_free((mbedtls_sha1_context*)context);
|
||||
break;
|
||||
case PIKA_HMAC_SHA256:
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
mbedtls_sha256_free((mbedtls_sha256_context*)context);
|
||||
|
||||
mbedtls_sha256_init((mbedtls_sha256_context*)context);
|
||||
mbedtls_sha256_starts((mbedtls_sha256_context*)context, 0);
|
||||
mbedtls_sha256_update((mbedtls_sha256_context*)context, k_opad,
|
||||
64);
|
||||
mbedtls_sha256_update((mbedtls_sha256_context*)context, buff,
|
||||
PIKA_HMAC_SHA256);
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
mbedtls_sha256_free((mbedtls_sha256_context*)context);
|
||||
break;
|
||||
default:
|
||||
obj_setErrorCode(self, -1); // not support mode
|
||||
break;
|
||||
}
|
||||
hmac_to_hex(buff, PIKA_HMAC_MD5, hexbuff);
|
||||
obj_setInt(self, "_digest_flags",
|
||||
flag | 0x03); // set digest and hexdigest flags
|
||||
mbedtls_md_context_t* ctx = obj_getStruct(self, "_context");
|
||||
mbedtls_md_hmac_finish(ctx, buff);
|
||||
// set digest and hexdigest flags
|
||||
obj_setInt(self, "_digest_flags", flag | 0x03);
|
||||
goto exit;
|
||||
}
|
||||
exit:
|
||||
hmac_to_hex(buff, obj_getInt(self, "_mode"), hexbuff);
|
||||
return (char*)hexbuff;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import _hmac
|
||||
|
||||
def new(key:any, msg=None, digestmod="hmac-md5") -> _hmac.HMAC:
|
||||
def new(key:any, msg=None, digestmod="md5") -> _hmac.HMAC:
|
||||
hmac = _hmac.HMAC()
|
||||
hmac.new(key, msg, digestmod)
|
||||
return hmac
|
||||
|
0
package/pika_libc/pika_libc.pyi
Normal file
0
package/pika_libc/pika_libc.pyi
Normal file
1268
package/pika_libc/pika_vsnprintf.c
Normal file
1268
package/pika_libc/pika_vsnprintf.c
Normal file
File diff suppressed because it is too large
Load Diff
@ -125,7 +125,7 @@ void pika_lvgl_bar___init__(PikaObj* self, PikaObj* parent) {
|
||||
|
||||
void pika_lvgl_bar_set_value(PikaObj* self, int value, int anim) {
|
||||
lv_obj_t* lv_obj = obj_getPtr(self, "lv_obj");
|
||||
lv_bar_set_value(lv_obj, value, value);
|
||||
lv_bar_set_value(lv_obj, value, anim);
|
||||
}
|
||||
|
||||
int pika_lvgl_bar_get_max_value(PikaObj* self) {
|
||||
|
@ -88,8 +88,8 @@ void pika_lvgl_TEXT_DECOR___init__(PikaObj* self) {
|
||||
}
|
||||
|
||||
void pika_lvgl_ANIM___init__(PikaObj* self) {
|
||||
obj_setInt(self, "ON", LV_ANIM_OFF);
|
||||
obj_setInt(self, "OFF", LV_ANIM_ON);
|
||||
obj_setInt(self, "ON", LV_ANIM_ON);
|
||||
obj_setInt(self, "OFF", LV_ANIM_OFF);
|
||||
}
|
||||
|
||||
void pika_lvgl_ALIGN___init__(PikaObj* self) {
|
||||
|
@ -110,7 +110,7 @@ PIKA_WEAK int __platform_setsockopt(int __fd,
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_fcntl(int fd, int cmd, long arg) {
|
||||
#ifdef __linux__
|
||||
#if defined(__linux__) || PIKA_LWIP_ENABLE
|
||||
return fcntl(fd, cmd, arg);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
@ -119,7 +119,7 @@ PIKA_WEAK int __platform_fcntl(int fd, int cmd, long arg) {
|
||||
|
||||
/* os file API */
|
||||
PIKA_WEAK int __platform_close(int __fd) {
|
||||
#ifdef __linux__
|
||||
#if defined(__linux__) || PIKA_LWIP_ENABLE
|
||||
return close(__fd);
|
||||
#elif PIKA_FREERTOS_ENABLE
|
||||
return closesocket(__fd);
|
||||
|
@ -173,7 +173,9 @@ releases = [
|
||||
"v1.11.8 58ff9fe16688be2e77ed220604aa8b1271b72d80",
|
||||
"v2.0.0 e89c585f46ee98811611ea56d5abfc618a41ee3b",
|
||||
"v2.1.0 852daad3c9a95dbce54dbf1d2cdb5b2321826b4f",
|
||||
"v2.1.1 5eadba95fa573e3409f65c6392804d3c60d627ac"
|
||||
"v2.1.1 5eadba95fa573e3409f65c6392804d3c60d627ac",
|
||||
"v2.1.2 36ef746f5791a57212cc1d02fe885814e613e60c",
|
||||
"v2.2.0 747fc1be13d57f920b436a2dbbfce253d8c3546f"
|
||||
]
|
||||
|
||||
[[packages]]
|
||||
@ -445,13 +447,17 @@ releases = [
|
||||
|
||||
[[packages]]
|
||||
name = "hmac"
|
||||
releases = [ "v1.0.0 ca8fe36d68be16722317860394fdf78c620e3e2d" ]
|
||||
releases = [
|
||||
"v1.0.0 ca8fe36d68be16722317860394fdf78c620e3e2d",
|
||||
"v1.0.1 d75ce00c377e48c6058bbe1f824474802383e5e7"
|
||||
]
|
||||
|
||||
[[packages]]
|
||||
name = "BLIOT"
|
||||
releases = [
|
||||
"v0.1.0 4599ce8681d293a5cc7682115205ab5e85e9b5e8",
|
||||
"v0.2.0 eee8e1848eed77f3ebed777230104ebb4dfc3263"
|
||||
"v0.2.0 eee8e1848eed77f3ebed777230104ebb4dfc3263",
|
||||
"v0.3.0 fc69ce4145c053d0a5f9191e8706014c5bee4ca8"
|
||||
]
|
||||
|
||||
[[packages]]
|
||||
@ -460,4 +466,11 @@ releases = [ "v0.0.1 5c45cba0c39d447fbe7a4a082ee3170188c3bcb7" ]
|
||||
|
||||
[[packages]]
|
||||
name = "base64"
|
||||
releases = [ "v0.0.1 1ae3ac23759fb2b5e0f77866af40986ac94e356d" ]
|
||||
releases = [
|
||||
"v0.0.1 1ae3ac23759fb2b5e0f77866af40986ac94e356d",
|
||||
"v1.0.0 29ad2b44817c5a72ace9f6d09cea7df6beb516f9"
|
||||
]
|
||||
|
||||
[[packages]]
|
||||
name = "pika_libc"
|
||||
releases = [ "v1.0.0 42a3cebb085a4a995d80031685dbf630e053aa55" ]
|
||||
|
2
port/linux/.vscode/launch.json
vendored
2
port/linux/.vscode/launch.json
vendored
@ -11,7 +11,7 @@
|
||||
"program": "${workspaceFolder}/build/test/pikascript_test",
|
||||
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
|
||||
"args": [
|
||||
// "--gtest_filter=parser.not_in"
|
||||
// "--gtest_filter=vm.fn_pos_kw_issue2"
|
||||
],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
|
@ -68,6 +68,21 @@ class GPIO(BaseDev):
|
||||
def read(self) -> int:
|
||||
"""Read the pin value."""
|
||||
|
||||
SIGNAL_RISING: int
|
||||
SIGNAL_FALLING: int
|
||||
SIGNAL_ANY: int
|
||||
|
||||
def setCallBack(self, eventCallBack: any, filter: int):
|
||||
"""
|
||||
Add a callback function to the pin.
|
||||
Example:
|
||||
``` python
|
||||
def cb1(signal):
|
||||
print("cb1", signal)
|
||||
io.setCallBack(cb1, io.SIGNAL_RISING)
|
||||
```
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def platformHigh(self): ...
|
||||
|
||||
@ -175,10 +190,11 @@ class DAC(BaseDev):
|
||||
def disable(self):
|
||||
"""Disable the DAC."""
|
||||
|
||||
def write(self, val:float):
|
||||
def write(self, val: float):
|
||||
"""write the DAC value."""
|
||||
|
||||
class UART(BaseDev):
|
||||
|
||||
class UART:
|
||||
def __init__(self): ...
|
||||
|
||||
def setBaudRate(self, baudRate: int):
|
||||
@ -186,6 +202,14 @@ class UART(BaseDev):
|
||||
|
||||
def setId(self, id: int):
|
||||
"""Set the id of the UART."""
|
||||
|
||||
FLOW_CONTROL_NONE: int
|
||||
FLOW_CONTROL_RTS: int
|
||||
FLOW_CONTROL_CTS: int
|
||||
FLOW_CONTROL_RTS_CTS: int
|
||||
|
||||
def setFlowControl(self, flowControl: int):
|
||||
"""Set the flow control of the UART."""
|
||||
|
||||
def enable(self):
|
||||
"""Enable the UART."""
|
||||
@ -205,6 +229,21 @@ class UART(BaseDev):
|
||||
def readBytes(self, length: int) -> bytes:
|
||||
"""Read bytes from the UART."""
|
||||
|
||||
|
||||
SIGNAL_RX: int
|
||||
SIGNAL_TX: int
|
||||
|
||||
def setCallBack(self, eventCallBack: any, filter: int):
|
||||
"""
|
||||
Add a callback function to the pin.
|
||||
Example:
|
||||
``` python
|
||||
def cb1(signal):
|
||||
print(uart.read(-1))
|
||||
io.setCallBack(cb1, uart.SIGNAL_RX)
|
||||
```
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def platformEnable(self): ...
|
||||
|
||||
@ -436,7 +475,7 @@ class CAN(BaseDev):
|
||||
def readBytes(self, length: int) -> bytes:
|
||||
"""Read bytes from the CAN."""
|
||||
|
||||
def addFilter(self, id: int, ide: int, rtr: int, mode: int, mask: int, hdr: int):
|
||||
def addFilter(self, id: int, ide: int, rtr: int, mode: int, mask: int, hdr: int):
|
||||
"""Add a filter."""
|
||||
|
||||
@abstractmethod
|
||||
@ -460,7 +499,7 @@ class CAN(BaseDev):
|
||||
|
||||
class BaseDev:
|
||||
@PIKA_C_MACRO_IF("PIKA_EVENT_ENABLE")
|
||||
def addEventCallBack(self, eventCallback: any):
|
||||
def addEventCallBack(self, eventCallback: any):
|
||||
""" Add an event callback. """
|
||||
|
||||
@abstractmethod
|
||||
|
2
port/linux/package/pikascript/_base64.pyi
Normal file
2
port/linux/package/pikascript/_base64.pyi
Normal file
@ -0,0 +1,2 @@
|
||||
def b64encode(s: any) -> any: ...
|
||||
def b64decode(s: any) -> any: ...
|
9
port/linux/package/pikascript/base64.py
Normal file
9
port/linux/package/pikascript/base64.py
Normal file
@ -0,0 +1,9 @@
|
||||
import _base64
|
||||
|
||||
|
||||
def b64encode(s: any):
|
||||
return _base64.b64encode(s)
|
||||
|
||||
|
||||
def b64decode(s: any):
|
||||
return _base64.b64decode(s)
|
@ -1,2 +0,0 @@
|
||||
def b64encode(self, s:any) -> any: ...
|
||||
def b64decode(self, s:any) -> any: ...
|
@ -1,6 +1,6 @@
|
||||
import _hmac
|
||||
|
||||
def new(key:any, msg=None, digestmod="hmac-md5") -> _hmac.HMAC:
|
||||
def new(key:any, msg=None, digestmod="md5") -> _hmac.HMAC:
|
||||
hmac = _hmac.HMAC()
|
||||
hmac.new(key, msg, digestmod)
|
||||
return hmac
|
||||
|
0
port/linux/package/pikascript/pika_libc.pyi
Normal file
0
port/linux/package/pikascript/pika_libc.pyi
Normal file
@ -1,6 +1,5 @@
|
||||
#include "PikaStdDevice_ADC.h"
|
||||
#include "BaseObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
void PikaStdDevice_ADC_enable(PikaObj* self) {
|
||||
obj_runNativeMethod(self, "platformEnable", NULL);
|
||||
|
@ -1,8 +1,5 @@
|
||||
#include "PikaStdDevice_BaseDev.h"
|
||||
#include "BaseObj.h"
|
||||
#include "PikaObj.h"
|
||||
#include "dataStrs.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
#if !PIKASCRIPT_VERSION_REQUIRE_MINIMUN(1, 10, 4)
|
||||
#error "This library requires PikaScript version 1.10.4 or higher"
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "PikaStdDevice_CAN.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
void PikaStdDevice_CAN___init__(PikaObj* self) {}
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "PikaStdDevice_DAC.h"
|
||||
#include "BaseObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
static pika_dev* _get_dev(PikaObj* self) {
|
||||
pika_dev* dev = obj_getPtr(self, "pika_dev");
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "PikaStdDevice_GPIO.h"
|
||||
#include "BaseObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
void PikaStdDevice_GPIO_init(PikaObj* self) {
|
||||
obj_setInt(self, "isEnable", 0);
|
||||
@ -22,6 +21,9 @@ void PikaStdDevice_GPIO_setId(PikaObj* self, int id) {
|
||||
|
||||
void PikaStdDevice_GPIO___init__(PikaObj* self) {
|
||||
PikaStdDevice_GPIO_init(self);
|
||||
obj_setInt(self, "SIGNAL_RISING", PIKA_HAL_GPIO_EVENT_SIGNAL_RISING);
|
||||
obj_setInt(self, "SIGNAL_FALLING", PIKA_HAL_GPIO_EVENT_SIGNAL_FALLING);
|
||||
obj_setInt(self, "SIGNAL_ANY", PIKA_HAL_GPIO_EVENT_SIGNAL_ANY);
|
||||
}
|
||||
|
||||
void PikaStdDevice_GPIO_disable(PikaObj* self) {
|
||||
@ -145,3 +147,21 @@ void PikaStdDevice_GPIO_platformRead(PikaObj* self) {
|
||||
pika_hal_read(dev, &val, sizeof(val));
|
||||
obj_setInt(self, "readBuff", val);
|
||||
}
|
||||
|
||||
void PikaStdDevice_GPIO_setCallBack(PikaObj* self,
|
||||
Arg* eventCallback,
|
||||
int filter) {
|
||||
pika_dev* dev = _get_dev(self);
|
||||
#if PIKA_EVENT_ENABLE
|
||||
_PikaStdDevice_setCallBack(self, eventCallback, (uintptr_t)dev);
|
||||
/* regist event to pika_hal */
|
||||
pika_hal_GPIO_config cfg_cb = {0};
|
||||
cfg_cb.event_callback = (void*)_PikaStdDevice_event_handler;
|
||||
cfg_cb.event_callback_filter = filter;
|
||||
cfg_cb.event_callback_ena = PIKA_HAL_EVENT_CALLBACK_ENA_ENABLE;
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_CONFIG, &cfg_cb);
|
||||
#else
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] PIKA_EVENT_ENABLE is disabled.");
|
||||
#endif
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
#include "PikaStdDevice_IIC.h"
|
||||
#include "BaseObj.h"
|
||||
#include "PikaObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
void PikaStdDevice_IIC_init(PikaObj* self) {
|
||||
obj_setInt(self, "deviceAddr", 0);
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "PikaStdDevice_PWM.h"
|
||||
#include "BaseObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
void PikaStdDevice_PWM_init(PikaObj* self) {
|
||||
obj_setStr(self, "pin", "none");
|
||||
|
@ -1,7 +1,5 @@
|
||||
#include "PikaStdDevice_SPI.h"
|
||||
#include "BaseObj.h"
|
||||
#include "PikaObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
void PikaStdDevice_SPI___init__(PikaObj* self) {
|
||||
obj_setInt(self, "baudRate", 1000);
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "PikaStdDevice_Time.h"
|
||||
#include "BaseObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
#if defined(__linux)
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
@ -14,7 +13,7 @@ void PikaStdDevice_Time_sleep_ms(PikaObj* self, int ms) {
|
||||
#elif defined(_WIN32)
|
||||
Sleep(ms);
|
||||
#else
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
__platform_sleep_ms(ms);
|
||||
#endif
|
||||
}
|
||||
void PikaStdDevice_Time_sleep_s(PikaObj* self, int s) {
|
||||
@ -23,13 +22,12 @@ void PikaStdDevice_Time_sleep_s(PikaObj* self, int s) {
|
||||
#elif defined(_WIN32)
|
||||
Sleep(s * 1000);
|
||||
#else
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
__platform_sleep_s(s);
|
||||
#endif
|
||||
}
|
||||
|
||||
void PikaStdDevice_Time_platformGetTick(PikaObj* self) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] platformGetTick() need to be override.");
|
||||
obj_setInt(self, "tick", __platform_getTick());
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "PikaStdDevice_UART.h"
|
||||
#include "BaseObj.h"
|
||||
#include "pika_hal.h"
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
void PikaStdDevice_UART_enable(PikaObj* self) {
|
||||
obj_runNativeMethod(self, "platformEnable", NULL);
|
||||
@ -11,9 +10,20 @@ void PikaStdDevice_UART_disable(PikaObj* self) {
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_init(PikaObj* self) {
|
||||
/* const */
|
||||
obj_setInt(self, "FLOW_CONTROL_NONE", PIKA_HAL_UART_FLOW_CONTROL_NONE);
|
||||
obj_setInt(self, "FLOW_CONTROL_RTS", PIKA_HAL_UART_FLOW_CONTROL_RTS);
|
||||
obj_setInt(self, "FLOW_CONTROL_CTS", PIKA_HAL_UART_FLOW_CONTROL_CTS);
|
||||
obj_setInt(self, "FLOW_CONTROL_RTS_CTS",
|
||||
PIKA_HAL_UART_FLOW_CONTROL_RTS_CTS);
|
||||
|
||||
obj_setInt(self, "SIGNAL_RX", PIKA_HAL_UART_EVENT_SIGNAL_RX);
|
||||
obj_setInt(self, "SIGNAL_TX", PIKA_HAL_UART_EVENT_SIGNAL_TX);
|
||||
|
||||
obj_setInt(self, "baudRate", 115200);
|
||||
obj_setInt(self, "id", 1);
|
||||
obj_setStr(self, "readBuff", "");
|
||||
obj_setInt(self, "flowControl", PIKA_HAL_UART_FLOW_CONTROL_NONE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART___init__(PikaObj* self) {
|
||||
@ -35,9 +45,15 @@ Arg* PikaStdDevice_UART_readBytes(PikaObj* self, int length) {
|
||||
void PikaStdDevice_UART_setBaudRate(PikaObj* self, int baudRate) {
|
||||
obj_setInt(self, "baudRate", baudRate);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_setFlowControl(PikaObj* self, int flowControl) {
|
||||
obj_setInt(self, "flowControl", flowControl);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_setId(PikaObj* self, int id) {
|
||||
obj_setInt(self, "id", id);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_write(PikaObj* self, char* data) {
|
||||
obj_setStr(self, "writeData", data);
|
||||
obj_runNativeMethod(self, "platformWrite", NULL);
|
||||
@ -48,24 +64,92 @@ void PikaStdDevice_UART_writeBytes(PikaObj* self, uint8_t* data, int length) {
|
||||
obj_runNativeMethod(self, "platformWriteBytes", NULL);
|
||||
}
|
||||
|
||||
static pika_dev* _get_dev(PikaObj* self) {
|
||||
pika_dev* dev = obj_getPtr(self, "pika_dev");
|
||||
if (NULL != dev) {
|
||||
return dev;
|
||||
}
|
||||
int id = obj_getInt(self, "id");
|
||||
char id_str[32] = {0};
|
||||
sprintf(id_str, "UART%d", id);
|
||||
dev = pika_hal_open(PIKA_HAL_UART, id_str);
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open UART '%s' failed.\r\n", id_str);
|
||||
}
|
||||
obj_setPtr(self, "pika_dev", dev);
|
||||
return dev;
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_platformEnable(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
pika_dev* dev = _get_dev(self);
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open UART '%d' failed.\r\n",
|
||||
obj_getInt(self, "id"));
|
||||
return;
|
||||
}
|
||||
pika_hal_UART_config cfg = {0};
|
||||
cfg.baudrate = obj_getInt(self, "baudRate");
|
||||
cfg.flow_control = obj_getInt(self, "flowControl");
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_CONFIG, &cfg);
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_ENABLE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_platformRead(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
int len = obj_getInt(self, "length");
|
||||
obj_setBytes(self, "_readData", NULL, len + 1);
|
||||
char* buff = (char*)obj_getBytes(self, "_readData");
|
||||
pika_dev* dev = _get_dev(self);
|
||||
int len_get = pika_hal_read(dev, buff, len);
|
||||
buff[len_get] = 0;
|
||||
obj_setStr(self, "readData", buff);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_platformWrite(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
char* data = obj_getStr(self, "writeData");
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_write(dev, data, strlen(data));
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_platformDisable(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
pika_dev* dev = _get_dev(self);
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open UART '%d' failed.\r\n",
|
||||
obj_getInt(self, "id"));
|
||||
return;
|
||||
}
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_DISABLE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_platformReadBytes(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
int len = obj_getInt(self, "length");
|
||||
obj_setBytes(self, "_readData", NULL, len + 1);
|
||||
uint8_t* buff = obj_getBytes(self, "_readData");
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_read(dev, buff, len);
|
||||
obj_setBytes(self, "readData", buff, len);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_platformWriteBytes(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
uint8_t* data = obj_getBytes(self, "writeData");
|
||||
int len = obj_getBytesSize(self, "writeData");
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_write(dev, data, len);
|
||||
}
|
||||
|
||||
void PikaStdDevice_UART_setCallBack(PikaObj* self,
|
||||
Arg* eventCallBack,
|
||||
int filter) {
|
||||
pika_dev* dev = _get_dev(self);
|
||||
#if PIKA_EVENT_ENABLE
|
||||
_PikaStdDevice_setCallBack(self, eventCallBack, (uintptr_t)dev);
|
||||
/* regist event to pika_hal */
|
||||
pika_hal_UART_config cfg_cb = {0};
|
||||
cfg_cb.event_callback = (void*)_PikaStdDevice_event_handler;
|
||||
cfg_cb.event_callback_filter = filter;
|
||||
cfg_cb.event_callback_ena = PIKA_HAL_EVENT_CALLBACK_ENA_ENABLE;
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_CONFIG, &cfg_cb);
|
||||
#else
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] PIKA_EVENT_ENABLE is disabled.");
|
||||
#endif
|
||||
}
|
||||
|
@ -0,0 +1,19 @@
|
||||
#include "PikaStdDevice_common.h"
|
||||
|
||||
extern PikaEventListener* g_pika_device_event_listener;
|
||||
void _PikaStdDevice_event_handler(pika_dev* dev, int signal) {
|
||||
pks_eventLisener_sendSignal(g_pika_device_event_listener, (uintptr_t)dev,
|
||||
signal);
|
||||
}
|
||||
|
||||
void _PikaStdDevice_setCallBack(PikaObj* self,
|
||||
Arg* eventCallback,
|
||||
uint32_t eventId) {
|
||||
obj_setArg(self, "eventCallBack", eventCallback);
|
||||
/* init event_listener for the first time */
|
||||
if (NULL == g_pika_device_event_listener) {
|
||||
pks_eventLisener_init(&g_pika_device_event_listener);
|
||||
}
|
||||
/* regist event to event listener */
|
||||
pks_eventLicener_registEvent(g_pika_device_event_listener, eventId, self);
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
#ifndef _PIKA_STDDEVICE_COMMON_H
|
||||
#define _PIKA_STDDEVICE_COMMON_H
|
||||
#include "PikaObj.h"
|
||||
#include "PikaVM.h"
|
||||
#include "pika_hal.h"
|
||||
void _PikaStdDevice_setCallBack(PikaObj* self,
|
||||
Arg* eventCallback,
|
||||
uint32_t eventId);
|
||||
|
||||
void _PikaStdDevice_event_handler(pika_dev* dev, int signal);
|
||||
|
||||
#endif
|
@ -53,6 +53,7 @@ __exit:
|
||||
__platform_printf("Error: dev_open failed.\r\n");
|
||||
if (dev->ioctl_config) {
|
||||
pikaFree(dev->ioctl_config, _pika_hal_dev_config_size(dev_type));
|
||||
dev->ioctl_config = NULL;
|
||||
}
|
||||
if (dev) {
|
||||
pikaFree(dev, sizeof(pika_dev));
|
||||
@ -71,11 +72,12 @@ int pika_hal_close(pika_dev* dev) {
|
||||
}
|
||||
ret = impl->close(dev);
|
||||
__exit:
|
||||
if (NULL != dev) {
|
||||
pikaFree(dev, sizeof(pika_dev));
|
||||
}
|
||||
if (NULL != dev->ioctl_config) {
|
||||
pikaFree(dev->ioctl_config, _pika_hal_dev_config_size(dev->type));
|
||||
dev->ioctl_config = NULL;
|
||||
}
|
||||
if (NULL != dev) {
|
||||
pikaFree(dev, sizeof(pika_dev));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@ -181,8 +183,11 @@ int pika_hal_GPIO_ioctl_merge_config(pika_hal_GPIO_config* dst,
|
||||
// printf("after merge: dst->dir=%d, src->dir=%d\r\n", dst->dir, src->dir);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(pull, PIKA_HAL_GPIO_PULL_NONE);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(speed, PIKA_HAL_GPIO_SPEED_10M);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_rising, NULL);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_falling, NULL);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback, NULL);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_filter,
|
||||
PIKA_HAL_GPIO_EVENT_SIGNAL_RISING);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_ena,
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA_ENABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -192,7 +197,12 @@ int pika_hal_UART_ioctl_merge_config(pika_hal_UART_config* dst,
|
||||
_IOCTL_CONFIG_USE_DEFAULT(data_bits, PIKA_HAL_UART_DATA_BITS_8);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(stop_bits, PIKA_HAL_UART_STOP_BITS_1);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(parity, PIKA_HAL_UART_PARITY_NONE);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_rx, NULL);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(flow_control, PIKA_HAL_UART_FLOW_CONTROL_NONE);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback, NULL);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_filter,
|
||||
PIKA_HAL_UART_EVENT_SIGNAL_RX);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_ena,
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA_ENABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -58,12 +58,26 @@ typedef enum {
|
||||
PIKA_HAL_GPIO_SPEED_100M = 100000000,
|
||||
} PIKA_HAL_GPIO_SPEED;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_EVENT_CALLBACK_ENA_UNUSED = 0,
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA_ENABLE,
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA_DISABLE,
|
||||
} PIKA_HAL_EVENT_CALLBACK_ENA;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_GPIO_EVENT_SIGNAL_UNUSED = 0,
|
||||
PIKA_HAL_GPIO_EVENT_SIGNAL_RISING,
|
||||
PIKA_HAL_GPIO_EVENT_SIGNAL_FALLING,
|
||||
PIKA_HAL_GPIO_EVENT_SIGNAL_ANY,
|
||||
} PIKA_HAL_GPIO_EVENT_SIGNAL;
|
||||
|
||||
typedef struct {
|
||||
PIKA_HAL_GPIO_DIR dir;
|
||||
PIKA_HAL_GPIO_PULL pull;
|
||||
PIKA_HAL_GPIO_SPEED speed;
|
||||
void (*event_callback_rising)(pika_dev* dev);
|
||||
void (*event_callback_falling)(pika_dev* dev);
|
||||
void (*event_callback)(pika_dev* dev, PIKA_HAL_GPIO_EVENT_SIGNAL signal);
|
||||
PIKA_HAL_GPIO_EVENT_SIGNAL event_callback_filter;
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA event_callback_ena;
|
||||
} pika_hal_GPIO_config;
|
||||
|
||||
typedef enum {
|
||||
@ -89,6 +103,7 @@ typedef enum {
|
||||
_PIKA_HAL_UART_STOP_BITS_UNUSED = 0,
|
||||
PIKA_HAL_UART_STOP_BITS_1 = 1,
|
||||
PIKA_HAL_UART_STOP_BITS_2 = 2,
|
||||
PIKA_HAL_UART_STOP_BITS_1_5 = 3,
|
||||
} PIKA_HAL_UART_STOP_BITS;
|
||||
|
||||
typedef enum {
|
||||
@ -98,12 +113,30 @@ typedef enum {
|
||||
PIKA_HAL_UART_PARITY_EVEN,
|
||||
} PIKA_HAL_UART_PARITY;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_UART_EVENT_SIGNAL_UNUSED = 0,
|
||||
PIKA_HAL_UART_EVENT_SIGNAL_RX,
|
||||
PIKA_HAL_UART_EVENT_SIGNAL_TX,
|
||||
PIKA_HAL_UART_EVENT_SIGNAL_ANY,
|
||||
} PIKA_HAL_UART_EVENT_SIGNAL;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_UART_FLOW_CONTROL_UNUSED = 0,
|
||||
PIKA_HAL_UART_FLOW_CONTROL_NONE,
|
||||
PIKA_HAL_UART_FLOW_CONTROL_RTS,
|
||||
PIKA_HAL_UART_FLOW_CONTROL_CTS,
|
||||
PIKA_HAL_UART_FLOW_CONTROL_RTS_CTS,
|
||||
} PIKA_HAL_UART_FLOW_CONTROL;
|
||||
|
||||
typedef struct {
|
||||
PIKA_HAL_UART_BAUDRATE baudrate;
|
||||
PIKA_HAL_UART_DATA_BITS data_bits;
|
||||
PIKA_HAL_UART_STOP_BITS stop_bits;
|
||||
PIKA_HAL_UART_PARITY parity;
|
||||
void (*event_callback_rx)(pika_dev* dev);
|
||||
PIKA_HAL_UART_FLOW_CONTROL flow_control;
|
||||
void (*event_callback)(pika_dev* dev, PIKA_HAL_UART_EVENT_SIGNAL signal);
|
||||
PIKA_HAL_UART_EVENT_SIGNAL event_callback_filter;
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA event_callback_ena;
|
||||
} pika_hal_UART_config;
|
||||
|
||||
typedef uint32_t PIKA_HAL_IIC_SLAVE_ADDR;
|
||||
|
@ -40,7 +40,7 @@ Arg* PikaStdData_Dict___iter__(PikaObj* self) {
|
||||
Arg* PikaStdData_Dict___next__(PikaObj* self) {
|
||||
int __iter_i = args_getInt(self->list, "__iter_i");
|
||||
PikaDict* keys = obj_getPtr(self, "_keys");
|
||||
Arg* res = arg_copy(args_getArgByidex(&keys->super, __iter_i));
|
||||
Arg* res = arg_copy(args_getArgByIndex(&keys->super, __iter_i));
|
||||
if (NULL == res) {
|
||||
return arg_newNull();
|
||||
}
|
||||
@ -91,7 +91,7 @@ Arg* PikaStdData_dict_keys___next__(PikaObj* self) {
|
||||
int __iter_i = args_getInt(self->list, "__iter_i");
|
||||
PikaObj* dictptr = obj_getPtr(self, "dictptr");
|
||||
PikaDict* keys = obj_getPtr(dictptr, "_keys");
|
||||
Arg* res = arg_copy(args_getArgByidex(&keys->super, __iter_i));
|
||||
Arg* res = arg_copy(args_getArgByIndex(&keys->super, __iter_i));
|
||||
if (NULL == res) {
|
||||
return arg_newNull();
|
||||
}
|
||||
@ -107,7 +107,7 @@ char* PikaStdData_dict_keys___str__(PikaObj* self) {
|
||||
|
||||
int i = 0;
|
||||
while (PIKA_TRUE) {
|
||||
Arg* item = args_getArgByidex(&keys->super, i);
|
||||
Arg* item = args_getArgByIndex(&keys->super, i);
|
||||
if (NULL == item) {
|
||||
break;
|
||||
}
|
||||
@ -141,8 +141,8 @@ char* PikaStdData_Dict___str__(PikaObj* self) {
|
||||
|
||||
int i = 0;
|
||||
while (PIKA_TRUE) {
|
||||
Arg* item_key = args_getArgByidex(&keys->super, i);
|
||||
Arg* item_val = args_getArgByidex(&dict->super, i);
|
||||
Arg* item_key = args_getArgByIndex(&keys->super, i);
|
||||
Arg* item_val = args_getArgByIndex(&dict->super, i);
|
||||
if (NULL == item_key) {
|
||||
break;
|
||||
}
|
||||
@ -186,7 +186,7 @@ int PikaStdData_dict_keys___len__(PikaObj* self) {
|
||||
int dict_contains(PikaDict* dict, Arg* key) {
|
||||
int i = 0;
|
||||
while (PIKA_TRUE) {
|
||||
Arg* item = args_getArgByidex(&dict->super, i);
|
||||
Arg* item = args_getArgByIndex(&dict->super, i);
|
||||
if (NULL == item) {
|
||||
break;
|
||||
}
|
||||
@ -219,8 +219,8 @@ Arg* PikaStdData_dict_items___next__(PikaObj* self) {
|
||||
PikaObj* dictptr = obj_getPtr(self, "dictptr");
|
||||
PikaDict* keys = obj_getPtr(dictptr, "_keys");
|
||||
PikaDict* dict = obj_getPtr(dictptr, "dict");
|
||||
Arg* key = args_getArgByidex(&keys->super, __iter_i);
|
||||
Arg* val = args_getArgByidex(&dict->super, __iter_i);
|
||||
Arg* key = args_getArgByIndex(&keys->super, __iter_i);
|
||||
Arg* val = args_getArgByIndex(&dict->super, __iter_i);
|
||||
if (NULL == key) {
|
||||
return arg_newNull();
|
||||
}
|
||||
|
@ -443,7 +443,11 @@ static char* __print_arg(PikaObj* self, Arg* val) {
|
||||
}
|
||||
if (arg_type == ARG_TYPE_INT) {
|
||||
int64_t value = arg_getInt(val);
|
||||
#if PIKA_PRINT_LLD_ENABLE
|
||||
res = strsFormat(&buffs, 32, "%lld", value);
|
||||
#else
|
||||
res = strsFormat(&buffs, 32, "%d", value);
|
||||
#endif
|
||||
goto __exit;
|
||||
}
|
||||
if (arg_type == ARG_TYPE_FLOAT) {
|
||||
|
@ -1,11 +1,12 @@
|
||||
#include "base64.h"
|
||||
#include "_base64.h"
|
||||
|
||||
#include "mbedtls/base64.h"
|
||||
|
||||
Arg* base64_b64decode(PikaObj* self, Arg* s) {
|
||||
Arg* _base64_b64decode(PikaObj* self, Arg* s) {
|
||||
ArgType t = arg_getType(s);
|
||||
if (ARG_TYPE_BYTES != t) {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
obj_setSysOut(self, "base64.b64decode input not bytes");
|
||||
}
|
||||
|
||||
uint8_t* input_buff = arg_getBytes(s);
|
||||
@ -20,14 +21,15 @@ Arg* base64_b64decode(PikaObj* self, Arg* s) {
|
||||
return r;
|
||||
}
|
||||
|
||||
Arg* base64_b64encode(PikaObj* self, Arg* s) {
|
||||
Arg* _base64_b64encode(PikaObj* self, Arg* s) {
|
||||
ArgType t = arg_getType(s);
|
||||
if (ARG_TYPE_BYTES != t) {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
obj_setSysOut(self, "base64.b64encode input not bytes");
|
||||
}
|
||||
uint8_t* input_buff = arg_getBytes(s);
|
||||
size_t input_len = arg_getBytesSize(s);
|
||||
obj_setBytes(self, "__de_buff", NULL, (size_t)(input_len * 1.5));
|
||||
obj_setBytes(self, "__de_buff", NULL, (size_t)(input_len * 2));
|
||||
uint8_t* output_buff = obj_getBytes(self, "__de_buff");
|
||||
size_t output_len = 0;
|
||||
mbedtls_base64_encode(output_buff, obj_getBytesSize(self, "__de_buff"),
|
@ -98,7 +98,6 @@ void ctypes_c_buffer___init__(PikaObj *self, Arg* value, int size){
|
||||
strGetSize((char*)value_buffer) + 1);
|
||||
} else {
|
||||
__platform_printf("value type is not support!");
|
||||
while (1)
|
||||
;
|
||||
__platform_panic_handle();
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,7 @@
|
||||
#include "_hmac_HMAC.h"
|
||||
|
||||
#include "mbedtls/md.h"
|
||||
#include "string.h"
|
||||
|
||||
#include "mbedtls/md5.h"
|
||||
#include "mbedtls/sha1.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
|
||||
enum {
|
||||
PIKA_HMAC_MD5 = 16,
|
||||
PIKA_HMAC_SHA1 = 20,
|
||||
@ -14,220 +10,91 @@ enum {
|
||||
|
||||
static void hmac_to_hex(uint8_t* s, int l, uint8_t* d);
|
||||
|
||||
static void init_buff(PikaObj* self, size_t h) {
|
||||
obj_setBytes(self, "_buff", NULL, h);
|
||||
obj_setBytes(self, "_hexbuff", NULL, (h * 2));
|
||||
memset(obj_getBytes(self, "_buff"), 0, h);
|
||||
memset(obj_getBytes(self, "_hexbuff"), 0, (h * 2));
|
||||
}
|
||||
|
||||
void _hmac_HMAC_new(PikaObj* self, Arg* key, Arg* msg, char* digestmod) {
|
||||
ArgType t;
|
||||
t = arg_getType(key);
|
||||
if (ARG_TYPE_NONE != t) {
|
||||
if (ARG_TYPE_BYTES != t) {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
}
|
||||
if (ARG_TYPE_BYTES != t) {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
obj_setSysOut(self, "hmac.new() key type error");
|
||||
}
|
||||
|
||||
t = arg_getType(msg);
|
||||
if (ARG_TYPE_NONE != t) {
|
||||
if (ARG_TYPE_BYTES != t) {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
obj_setSysOut(self, "hmac.new() msg type error");
|
||||
}
|
||||
}
|
||||
|
||||
obj_setInt(self, "_digest_flags", 0); // flag
|
||||
obj_setBytes(self, "_buff", NULL, PIKA_HMAC_SHA256); // digest buff
|
||||
obj_setBytes(self, "_hexbuff", NULL,
|
||||
PIKA_HMAC_SHA256 * 2); // hexdigest buff
|
||||
obj_setBytes(self, "_k_ipad", NULL, 64); // key ipad
|
||||
obj_setBytes(self, "_k_opad", NULL, 64); // key opad
|
||||
|
||||
size_t key_len = arg_getBytesSize(key);
|
||||
uint8_t* key_data = arg_getBytes(key);
|
||||
size_t msg_len = arg_getBytesSize(msg);
|
||||
uint8_t* msg_data = arg_getBytes(msg);
|
||||
obj_setInt(self, "_digest_flags", 0); // flag
|
||||
mbedtls_md_context_t ctx;
|
||||
mbedtls_md_init(&ctx);
|
||||
|
||||
uint8_t* k_ipad = obj_getBytes(self, "_k_ipad");
|
||||
uint8_t* k_opad = obj_getBytes(self, "_k_opad");
|
||||
|
||||
if (strcmp(digestmod, "hmac-md5") == 0 ||
|
||||
strcmp(digestmod, "HMAC-MD5") == 0) {
|
||||
mbedtls_md5_context context;
|
||||
|
||||
if (key_len > 64) {
|
||||
mbedtls_md5_init(&context);
|
||||
mbedtls_md5_starts(&context);
|
||||
mbedtls_md5_update(&context, key_data, key_len);
|
||||
mbedtls_md5_finish(&context, k_ipad);
|
||||
mbedtls_md5_free(&context);
|
||||
memcpy(k_opad, k_ipad, 64);
|
||||
} else {
|
||||
memcpy(k_ipad, key_data, key_len);
|
||||
memcpy(k_opad, key_data, key_len);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 64; i++) {
|
||||
k_ipad[i] ^= 0x36;
|
||||
k_opad[i] ^= 0x5c;
|
||||
}
|
||||
|
||||
mbedtls_md5_init(&context);
|
||||
mbedtls_md5_starts(&context);
|
||||
mbedtls_md5_update(&context, k_ipad, 64);
|
||||
if (msg_len > 0) {
|
||||
mbedtls_md5_update(&context, msg_data, msg_len);
|
||||
}
|
||||
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HMAC_MD5);
|
||||
} else if (strcmp(digestmod, "hmac-sha1") == 0 ||
|
||||
strcmp(digestmod, "HMAC-SHA1") == 0) {
|
||||
mbedtls_sha1_context context;
|
||||
|
||||
if (key_len > 64) {
|
||||
mbedtls_sha1_init(&context);
|
||||
mbedtls_sha1_starts(&context);
|
||||
mbedtls_sha1_update(&context, key_data, key_len);
|
||||
mbedtls_sha1_finish(&context, k_ipad);
|
||||
mbedtls_sha1_free(&context);
|
||||
memcpy(k_opad, k_ipad, 64);
|
||||
} else {
|
||||
memcpy(k_ipad, key_data, key_len);
|
||||
memcpy(k_opad, key_data, key_len);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 64; i++) {
|
||||
k_ipad[i] ^= 0x36;
|
||||
k_opad[i] ^= 0x5c;
|
||||
}
|
||||
|
||||
mbedtls_sha1_init(&context);
|
||||
mbedtls_sha1_starts(&context);
|
||||
mbedtls_sha1_update(&context, k_ipad, 64);
|
||||
if (msg_len > 0) {
|
||||
mbedtls_sha1_update(&context, msg_data, msg_len);
|
||||
}
|
||||
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HMAC_SHA1);
|
||||
} else if (strcmp(digestmod, "hmac-sha256") == 0 ||
|
||||
strcmp(digestmod, "HMAC-SHA256") == 0) {
|
||||
mbedtls_sha256_context context;
|
||||
|
||||
if (key_len > 64) {
|
||||
mbedtls_sha256_init(&context);
|
||||
mbedtls_sha256_starts(&context, 0);
|
||||
mbedtls_sha256_update(&context, key_data, key_len);
|
||||
mbedtls_sha256_finish(&context, k_ipad);
|
||||
mbedtls_sha256_free(&context);
|
||||
memcpy(k_opad, k_ipad, 64);
|
||||
} else {
|
||||
memcpy(k_ipad, key_data, key_len);
|
||||
memcpy(k_opad, key_data, key_len);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 64; i++) {
|
||||
k_ipad[i] ^= 0x36;
|
||||
k_opad[i] ^= 0x5c;
|
||||
}
|
||||
|
||||
mbedtls_sha256_init(&context);
|
||||
mbedtls_sha256_starts(&context, 0);
|
||||
mbedtls_sha256_update(&context, k_ipad, 64);
|
||||
if (msg_len > 0) {
|
||||
mbedtls_sha256_update(&context, msg_data, msg_len);
|
||||
}
|
||||
|
||||
obj_setStruct(self, "context", context);
|
||||
obj_setInt(self, "mode", PIKA_HMAC_SHA256);
|
||||
if (strcmp(digestmod, "md5") == 0 ||
|
||||
strcmp(digestmod, "MD5") == 0) {
|
||||
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_MD5), 1);
|
||||
obj_setInt(self, "_mode", PIKA_HMAC_MD5);
|
||||
init_buff(self, PIKA_HMAC_MD5);
|
||||
} else if (strcmp(digestmod, "sha1") == 0 ||
|
||||
strcmp(digestmod, "SHA1") == 0) {
|
||||
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), 1);
|
||||
obj_setInt(self, "_mode", PIKA_HMAC_SHA1);
|
||||
init_buff(self, PIKA_HMAC_SHA1);
|
||||
} else if (strcmp(digestmod, "sha256") == 0 ||
|
||||
strcmp(digestmod, "SHA256") == 0) {
|
||||
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1);
|
||||
obj_setInt(self, "_mode", PIKA_HMAC_SHA256);
|
||||
init_buff(self, PIKA_HMAC_SHA256);
|
||||
} else {
|
||||
obj_setErrorCode(self, -1); // not support mode
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
obj_setSysOut(self, "hmac.new() not support mode");
|
||||
}
|
||||
mbedtls_md_hmac_starts(&ctx, key_data, key_len);
|
||||
if (msg_len > 0) {
|
||||
mbedtls_md_hmac_update(&ctx, msg_data, msg_len);
|
||||
}
|
||||
obj_setStruct(self, "_context", ctx);
|
||||
}
|
||||
|
||||
void _hmac_HMAC_update(PikaObj* self, Arg* msg) {
|
||||
ArgType t = arg_getType(msg);
|
||||
if (ARG_TYPE_BYTES != t) {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
obj_setSysOut(self, "hmac.update() msg type error");
|
||||
}
|
||||
|
||||
size_t msg_len = arg_getBytesSize(msg);
|
||||
uint8_t* msg_data = arg_getBytes(msg);
|
||||
void* context = obj_getStruct(self, "context");
|
||||
|
||||
if (msg_len > 0) {
|
||||
switch (obj_getInt(self, "mode")) {
|
||||
case PIKA_HMAC_MD5:
|
||||
mbedtls_md5_update((mbedtls_md5_context*)context, msg_data,
|
||||
msg_len);
|
||||
break;
|
||||
case PIKA_HMAC_SHA1:
|
||||
mbedtls_sha1_update((mbedtls_sha1_context*)context, msg_data,
|
||||
msg_len);
|
||||
break;
|
||||
case PIKA_HMAC_SHA256:
|
||||
mbedtls_sha256_update((mbedtls_sha256_context*)context,
|
||||
msg_data, msg_len);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
obj_setErrorCode(self, -2); // io error
|
||||
}
|
||||
mbedtls_md_context_t* ctx = obj_getStruct(self, "_context");
|
||||
mbedtls_md_hmac_update(ctx, msg_data, msg_len);
|
||||
}
|
||||
|
||||
Arg* _hmac_HMAC_digest(PikaObj* self) {
|
||||
uint8_t* k_opad = obj_getBytes(self, "_k_opad");
|
||||
uint8_t* buff = obj_getBytes(self, "_buff");
|
||||
uint8_t flag = obj_getInt(self, "_digest_flags");
|
||||
|
||||
if (flag & 0x01) // already digest
|
||||
{
|
||||
return arg_newBytes(buff, obj_getInt(self, "mode"));
|
||||
goto exit;
|
||||
} else {
|
||||
void* context = obj_getStruct(self, "context");
|
||||
uint8_t mode = obj_getInt(self, "mode");
|
||||
switch (mode) {
|
||||
case PIKA_HMAC_MD5:
|
||||
mbedtls_md5_finish((mbedtls_md5_context*)context, buff);
|
||||
mbedtls_md5_free((mbedtls_md5_context*)context);
|
||||
|
||||
mbedtls_md5_init((mbedtls_md5_context*)context);
|
||||
mbedtls_md5_starts((mbedtls_md5_context*)context);
|
||||
mbedtls_md5_update((mbedtls_md5_context*)context, k_opad, 64);
|
||||
mbedtls_md5_update((mbedtls_md5_context*)context, buff,
|
||||
PIKA_HMAC_MD5);
|
||||
mbedtls_md5_finish((mbedtls_md5_context*)context, buff);
|
||||
mbedtls_md5_free((mbedtls_md5_context*)context);
|
||||
break;
|
||||
case PIKA_HMAC_SHA1:
|
||||
mbedtls_sha1_finish((mbedtls_sha1_context*)context, buff);
|
||||
mbedtls_sha1_free((mbedtls_sha1_context*)context);
|
||||
|
||||
mbedtls_sha1_init((mbedtls_sha1_context*)context);
|
||||
mbedtls_sha1_starts((mbedtls_sha1_context*)context);
|
||||
mbedtls_sha1_update((mbedtls_sha1_context*)context, k_opad, 64);
|
||||
mbedtls_sha1_update((mbedtls_sha1_context*)context, buff,
|
||||
PIKA_HMAC_SHA1);
|
||||
mbedtls_sha1_finish((mbedtls_sha1_context*)context, buff);
|
||||
mbedtls_sha1_free((mbedtls_sha1_context*)context);
|
||||
break;
|
||||
case PIKA_HMAC_SHA256:
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
mbedtls_sha256_free((mbedtls_sha256_context*)context);
|
||||
|
||||
mbedtls_sha256_init((mbedtls_sha256_context*)context);
|
||||
mbedtls_sha256_starts((mbedtls_sha256_context*)context, 0);
|
||||
mbedtls_sha256_update((mbedtls_sha256_context*)context, k_opad,
|
||||
64);
|
||||
mbedtls_sha256_update((mbedtls_sha256_context*)context, buff,
|
||||
PIKA_HMAC_SHA256);
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
mbedtls_sha256_free((mbedtls_sha256_context*)context);
|
||||
break;
|
||||
default:
|
||||
obj_setErrorCode(self, -1); // not support mode
|
||||
return arg_newNull(); // will not actually return to the python
|
||||
break;
|
||||
}
|
||||
mbedtls_md_context_t* ctx = obj_getStruct(self, "_context");
|
||||
mbedtls_md_hmac_finish(ctx, buff);
|
||||
obj_setInt(self, "_digest_flags", flag | 0x01);
|
||||
return arg_newBytes(buff, mode);
|
||||
goto exit;
|
||||
}
|
||||
exit:
|
||||
return arg_newBytes(buff, obj_getInt(self, "_mode"));
|
||||
}
|
||||
|
||||
char* _hmac_HMAC_hexdigest(PikaObj* self) {
|
||||
@ -235,61 +102,20 @@ char* _hmac_HMAC_hexdigest(PikaObj* self) {
|
||||
uint8_t* hexbuff = obj_getBytes(self, "_hexbuff");
|
||||
uint8_t flag = obj_getInt(self, "_digest_flags");
|
||||
|
||||
if (flag & 0x01) { // already digest
|
||||
hmac_to_hex(buff, obj_getInt(self, "mode"), hexbuff);
|
||||
if (flag & 0x01) { // already digest
|
||||
obj_setInt(self, "_digest_flags", flag | 0x02); // set hexdigest flag
|
||||
} else if (flag & 0x02) { // already hexdigest
|
||||
|
||||
goto exit;
|
||||
} else if (flag & 0x02) { // already hexdigest
|
||||
return (char*)hexbuff;
|
||||
} else {
|
||||
void* context = obj_getStruct(self, "context");
|
||||
uint8_t* k_opad = obj_getBytes(self, "_k_opad");
|
||||
|
||||
switch (obj_getInt(self, "mode")) {
|
||||
case PIKA_HMAC_MD5:
|
||||
mbedtls_md5_finish((mbedtls_md5_context*)context, buff);
|
||||
mbedtls_md5_free((mbedtls_md5_context*)context);
|
||||
|
||||
mbedtls_md5_init((mbedtls_md5_context*)context);
|
||||
mbedtls_md5_starts((mbedtls_md5_context*)context);
|
||||
mbedtls_md5_update((mbedtls_md5_context*)context, k_opad, 64);
|
||||
mbedtls_md5_update((mbedtls_md5_context*)context, buff,
|
||||
PIKA_HMAC_MD5);
|
||||
mbedtls_md5_finish((mbedtls_md5_context*)context, buff);
|
||||
mbedtls_md5_free((mbedtls_md5_context*)context);
|
||||
break;
|
||||
case PIKA_HMAC_SHA1:
|
||||
mbedtls_sha1_finish((mbedtls_sha1_context*)context, buff);
|
||||
mbedtls_sha1_free((mbedtls_sha1_context*)context);
|
||||
|
||||
mbedtls_sha1_init((mbedtls_sha1_context*)context);
|
||||
mbedtls_sha1_starts((mbedtls_sha1_context*)context);
|
||||
mbedtls_sha1_update((mbedtls_sha1_context*)context, k_opad, 64);
|
||||
mbedtls_sha1_update((mbedtls_sha1_context*)context, buff,
|
||||
PIKA_HMAC_SHA1);
|
||||
mbedtls_sha1_finish((mbedtls_sha1_context*)context, buff);
|
||||
mbedtls_sha1_free((mbedtls_sha1_context*)context);
|
||||
break;
|
||||
case PIKA_HMAC_SHA256:
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
mbedtls_sha256_free((mbedtls_sha256_context*)context);
|
||||
|
||||
mbedtls_sha256_init((mbedtls_sha256_context*)context);
|
||||
mbedtls_sha256_starts((mbedtls_sha256_context*)context, 0);
|
||||
mbedtls_sha256_update((mbedtls_sha256_context*)context, k_opad,
|
||||
64);
|
||||
mbedtls_sha256_update((mbedtls_sha256_context*)context, buff,
|
||||
PIKA_HMAC_SHA256);
|
||||
mbedtls_sha256_finish((mbedtls_sha256_context*)context, buff);
|
||||
mbedtls_sha256_free((mbedtls_sha256_context*)context);
|
||||
break;
|
||||
default:
|
||||
obj_setErrorCode(self, -1); // not support mode
|
||||
break;
|
||||
}
|
||||
hmac_to_hex(buff, PIKA_HMAC_MD5, hexbuff);
|
||||
obj_setInt(self, "_digest_flags",
|
||||
flag | 0x03); // set digest and hexdigest flags
|
||||
mbedtls_md_context_t* ctx = obj_getStruct(self, "_context");
|
||||
mbedtls_md_hmac_finish(ctx, buff);
|
||||
// set digest and hexdigest flags
|
||||
obj_setInt(self, "_digest_flags", flag | 0x03);
|
||||
goto exit;
|
||||
}
|
||||
exit:
|
||||
hmac_to_hex(buff, obj_getInt(self, "_mode"), hexbuff);
|
||||
return (char*)hexbuff;
|
||||
}
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -110,7 +110,7 @@ PIKA_WEAK int __platform_setsockopt(int __fd,
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_fcntl(int fd, int cmd, long arg) {
|
||||
#ifdef __linux__
|
||||
#if defined(__linux__) || PIKA_LWIP_ENABLE
|
||||
return fcntl(fd, cmd, arg);
|
||||
#else
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
@ -119,7 +119,7 @@ PIKA_WEAK int __platform_fcntl(int fd, int cmd, long arg) {
|
||||
|
||||
/* os file API */
|
||||
PIKA_WEAK int __platform_close(int __fd) {
|
||||
#ifdef __linux__
|
||||
#if defined(__linux__) || PIKA_LWIP_ENABLE
|
||||
return close(__fd);
|
||||
#elif PIKA_FREERTOS_ENABLE
|
||||
return closesocket(__fd);
|
||||
|
@ -1635,11 +1635,11 @@ void pks_eventListener_sendSignal(PikaEventListener* self,
|
||||
#else
|
||||
/* push event handler to vm event list */
|
||||
if (PIKA_RES_OK != VMSignal_pushEvent(self, eventId, eventSignal)) {
|
||||
__platform_printf(
|
||||
"OverflowError: event list is full, please use bigger "
|
||||
"PIKA_EVENT_LIST_SIZE\r\n");
|
||||
while (1) {
|
||||
}
|
||||
// __platform_printf(
|
||||
// "OverflowError: event list is full, please use bigger "
|
||||
// "PIKA_EVENT_LIST_SIZE\r\n");
|
||||
// while (1) {
|
||||
// }
|
||||
}
|
||||
if (0 == VMSignal_getVMCnt()) {
|
||||
/* no vm running, pick up event imediately */
|
||||
|
@ -388,7 +388,7 @@ static inline uint8_t obj_refcntNow(PikaObj* self) {
|
||||
#define WEAK_FUNCTION_NEED_OVERRIDE_ERROR(_) \
|
||||
__platform_printf("Error: weak function `%s()` need override.\r\n", \
|
||||
__FUNCTION__); \
|
||||
while (1)
|
||||
__platform_panic_handle();
|
||||
|
||||
char* obj_cacheStr(PikaObj* self, char* str);
|
||||
PikaObj* _arg_to_obj(Arg* self, PIKA_BOOL* pIsTemp);
|
||||
|
@ -2046,9 +2046,15 @@ AST* AST_parseLine_withBlockStack_withBlockDeepth(char* line,
|
||||
AST_setNodeAttr(ast, "global", global_list);
|
||||
goto block_matched;
|
||||
}
|
||||
if (strIsStartWith(line_start, "del ")) {
|
||||
if (strIsStartWith(line_start, "del ") ||
|
||||
strIsStartWith(line_start, "del(")) {
|
||||
stmt = "";
|
||||
char* del_dir = line_start + sizeof("del ") - 1;
|
||||
char* del_dir = NULL;
|
||||
if (line_start[3] == '(') {
|
||||
del_dir = strsCut(&buffs, line_start, '(', ')');
|
||||
} else {
|
||||
del_dir = line_start + sizeof("del ") - 1;
|
||||
}
|
||||
del_dir = strsGetCleanCmd(&buffs, del_dir);
|
||||
AST_setNodeAttr(ast, "del", del_dir);
|
||||
goto block_matched;
|
||||
|
@ -65,6 +65,11 @@ PIKA_WEAK void __platform_error_handle() {
|
||||
return;
|
||||
}
|
||||
|
||||
PIKA_WEAK void __platform_panic_handle() {
|
||||
while (1) {
|
||||
};
|
||||
}
|
||||
|
||||
PIKA_WEAK uint8_t __is_locked_pikaMemory(void) {
|
||||
return 0;
|
||||
}
|
||||
@ -73,17 +78,9 @@ PIKA_WEAK int64_t __platform_getTick(void) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifndef __platform_printf
|
||||
PIKA_WEAK void __platform_printf(char* fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vprintf(fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
#endif
|
||||
|
||||
PIKA_WEAK int __platform_vsprintf(char* buff, char* fmt, va_list args) {
|
||||
return vsprintf(buff, fmt, args);
|
||||
/* vsnprintf */
|
||||
return __platform_vsnprintf(buff, PIKA_SPRINTF_BUFF_SIZE, fmt, args);
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_snprintf(char* buff,
|
||||
@ -97,6 +94,30 @@ PIKA_WEAK int __platform_snprintf(char* buff,
|
||||
return ret;
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_putchar(char ch) {
|
||||
return putchar(ch);
|
||||
}
|
||||
|
||||
PIKA_WEAK int __platform_vprintf(char* fmt, va_list args) {
|
||||
/* vsprintf to vprintf */
|
||||
char buff[PIKA_SPRINTF_BUFF_SIZE];
|
||||
__platform_vsprintf(buff, fmt, args);
|
||||
/* putchar */
|
||||
for (int i = 0; i < strlen(buff); i++) {
|
||||
__platform_putchar(buff[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef __platform_printf
|
||||
PIKA_WEAK void __platform_printf(char* fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
__platform_vprintf(fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
#endif
|
||||
|
||||
PIKA_WEAK char* __platform_strdup(const char* src) {
|
||||
char* dst = (char*)__platform_malloc(strlen(src) + 1);
|
||||
if (dst) {
|
||||
@ -119,7 +140,7 @@ PIKA_WEAK int __platform_vsnprintf(char* buff,
|
||||
PIKA_WEAK int __platform_sprintf(char* buff, char* fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
int res = vsnprintf(buff, PIKA_SPRINTF_BUFF_SIZE, fmt, args);
|
||||
int res = __platform_vsnprintf(buff, PIKA_SPRINTF_BUFF_SIZE, fmt, args);
|
||||
va_end(args);
|
||||
if (res >= PIKA_SPRINTF_BUFF_SIZE) {
|
||||
__platform_printf(
|
||||
@ -248,3 +269,15 @@ PIKA_WEAK PIKA_BOOL __pks_hook_arg_cache_filter(void* self) {
|
||||
PIKA_WEAK void __platform_thread_delay(void) {
|
||||
return;
|
||||
}
|
||||
|
||||
PIKA_WEAK void __platform_sleep_ms(uint32_t ms) {
|
||||
__platform_printf("Error: __platform_sleep_ms need implementation!\r\n");
|
||||
while (1) {
|
||||
}
|
||||
}
|
||||
|
||||
PIKA_WEAK void __platform_sleep_s(uint32_t s) {
|
||||
__platform_printf("Error: __platform_sleep_s need implementation!\r\n");
|
||||
while (1) {
|
||||
}
|
||||
}
|
||||
|
@ -156,6 +156,7 @@ uint8_t __is_locked_pikaMemory(void);
|
||||
|
||||
/* support shell */
|
||||
char __platform_getchar(void);
|
||||
int __platform_putchar(char ch);
|
||||
|
||||
/* file API */
|
||||
FILE* __platform_fopen(const char* filename, const char* modes);
|
||||
@ -168,9 +169,16 @@ long __platform_ftell(FILE* stream);
|
||||
/* error */
|
||||
void __platform_error_handle(void);
|
||||
|
||||
/* panic */
|
||||
void __platform_panic_handle(void);
|
||||
|
||||
void __pks_hook_instruct(void);
|
||||
PIKA_BOOL __pks_hook_arg_cache_filter(void* self);
|
||||
PIKA_WEAK void __platform_thread_delay(void);
|
||||
void __platform_thread_delay(void);
|
||||
int64_t __platform_getTick(void);
|
||||
|
||||
void __platform_sleep_ms(uint32_t ms);
|
||||
void __platform_sleep_s(uint32_t s);
|
||||
|
||||
#if PIKA_FLOAT_TYPE_DOUBLE
|
||||
#define pika_float double
|
||||
|
58
src/PikaVM.c
58
src/PikaVM.c
@ -65,8 +65,7 @@ static PIKA_BOOL _cq_isFull(volatile EventCQ* cq) {
|
||||
void VMSignal_deinit(void) {
|
||||
#if !PIKA_EVENT_ENABLE
|
||||
__platform_printf("PIKA_EVENT_ENABLE is not enable");
|
||||
while (1) {
|
||||
};
|
||||
__platform_panic_handle();
|
||||
#else
|
||||
for (int i = 0; i < PIKA_EVENT_LIST_SIZE; i++) {
|
||||
if (NULL != PikaVMSignal.cq.res[i]) {
|
||||
@ -82,8 +81,7 @@ PIKA_RES VMSignal_pushEvent(PikaEventListener* lisener,
|
||||
int eventSignal) {
|
||||
#if !PIKA_EVENT_ENABLE
|
||||
__platform_printf("PIKA_EVENT_ENABLE is not enable");
|
||||
while (1) {
|
||||
};
|
||||
__platform_panic_handle();
|
||||
#else
|
||||
/* push to event_cq_buff */
|
||||
if (_cq_isFull(&PikaVMSignal.cq)) {
|
||||
@ -107,8 +105,7 @@ PIKA_RES VMSignal_popEvent(PikaEventListener** lisener_p,
|
||||
int* head) {
|
||||
#if !PIKA_EVENT_ENABLE
|
||||
__platform_printf("PIKA_EVENT_ENABLE is not enable");
|
||||
while (1) {
|
||||
};
|
||||
__platform_panic_handle();
|
||||
#else
|
||||
/* pop from event_cq_buff */
|
||||
if (_cq_isEmpty(&PikaVMSignal.cq)) {
|
||||
@ -126,8 +123,7 @@ PIKA_RES VMSignal_popEvent(PikaEventListener** lisener_p,
|
||||
void VMSignale_pickupEvent(void) {
|
||||
#if !PIKA_EVENT_ENABLE
|
||||
__platform_printf("PIKA_EVENT_ENABLE is not enable");
|
||||
while (1) {
|
||||
};
|
||||
__platform_panic_handle();
|
||||
#else
|
||||
PikaObj* event_lisener;
|
||||
uint32_t event_id;
|
||||
@ -851,21 +847,35 @@ Arg* obj_runMethodArg(PikaObj* self,
|
||||
static char* _kw_to_default_all(FunctionArgsInfo* f,
|
||||
char* arg_name,
|
||||
int* argc,
|
||||
Arg* argv[]) {
|
||||
Arg* argv[],
|
||||
Arg* call_arg) {
|
||||
#if PIKA_NANO
|
||||
return arg_name;
|
||||
#endif
|
||||
while (strIsContain(arg_name, '=')) {
|
||||
strPopLastToken(arg_name, '=');
|
||||
Arg* default_arg = NULL;
|
||||
/* load default arg from kws */
|
||||
if (f->kw != NULL) {
|
||||
Arg* default_arg = pikaDict_getArg(f->kw, arg_name);
|
||||
default_arg = pikaDict_getArg(f->kw, arg_name);
|
||||
if (default_arg != NULL) {
|
||||
Arg* arg_new = arg_copy(default_arg);
|
||||
argv[(*argc)++] = arg_new;
|
||||
pikaDict_removeArg(f->kw, default_arg);
|
||||
}
|
||||
}
|
||||
if (f->kw == NULL || default_arg == NULL) {
|
||||
/* can not load defalut from kw */
|
||||
if (NULL != call_arg && f->is_default) {
|
||||
/* load default from pos */
|
||||
if (f->i_arg > f->n_positional) {
|
||||
arg_setNameHash(call_arg,
|
||||
hash_time33EndWith(arg_name, ':'));
|
||||
argv[(*argc)++] = call_arg;
|
||||
return (char*)1;
|
||||
}
|
||||
}
|
||||
}
|
||||
arg_name = strPopLastToken(f->type_list, ',');
|
||||
}
|
||||
return arg_name;
|
||||
@ -984,20 +994,12 @@ static void _load_call_arg(VMState* vm,
|
||||
}
|
||||
}
|
||||
char* arg_name = strPopLastToken(f->type_list, ',');
|
||||
/* load default from pos */
|
||||
if (f->i_arg > f->n_positional) {
|
||||
if (f->is_default) {
|
||||
if (arg_name[strlen(arg_name) - 1] == '=') {
|
||||
/* found default arg*/
|
||||
arg_name[strlen(arg_name) - 1] = '\0';
|
||||
arg_setNameHash(call_arg, hash_time33EndWith(arg_name, ':'));
|
||||
argv[(*argc)++] = call_arg;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
/* load default from kw */
|
||||
arg_name = _kw_to_default_all(f, arg_name, argc, argv);
|
||||
arg_name = _kw_to_default_all(f, arg_name, argc, argv, call_arg);
|
||||
if (((char*)1) == arg_name) {
|
||||
/* load default from pos */
|
||||
return;
|
||||
}
|
||||
/* load position arg */
|
||||
if (_kw_to_pos_one(f, arg_name, argc, argv)) {
|
||||
/* load pos from kw */
|
||||
@ -1090,7 +1092,7 @@ static int _get_n_input_with_unpack(VMState* vm) {
|
||||
PikaDict* dict = obj_getPtr(obj, "dict");
|
||||
int i_item = 0;
|
||||
while (PIKA_TRUE) {
|
||||
Arg* item_val = args_getArgByidex(&dict->super, i_item);
|
||||
Arg* item_val = args_getArgByIndex(&dict->super, i_item);
|
||||
if (NULL == item_val) {
|
||||
break;
|
||||
}
|
||||
@ -1138,8 +1140,7 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
|
||||
__platform_printf(
|
||||
"OverflowError: type list is too long, please use bigger "
|
||||
"PIKA_LINE_BUFF_SIZE\r\n");
|
||||
while (1)
|
||||
;
|
||||
__platform_panic_handle();
|
||||
}
|
||||
f.method_type = arg_getType(method_arg);
|
||||
|
||||
@ -1240,7 +1241,7 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
|
||||
#if !PIKA_NANO_ENABLE
|
||||
if (strIsContain(f.type_list, '=')) {
|
||||
char* arg_name = strPopLastToken(f.type_list, ',');
|
||||
_kw_to_default_all(&f, arg_name, &argc, argv);
|
||||
_kw_to_default_all(&f, arg_name, &argc, argv, NULL);
|
||||
}
|
||||
/* load kw to pos */
|
||||
_kw_to_pos_all(&f, &argc, argv);
|
||||
@ -1527,8 +1528,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
|
||||
__platform_printf(
|
||||
"[ERROR] Too many args in RUN instruction, please use bigger "
|
||||
"#define PIKA_ARG_NUM_MAX\n");
|
||||
while (1) {
|
||||
}
|
||||
__platform_panic_handle();
|
||||
}
|
||||
for (int i = 0; i < n_arg; i++) {
|
||||
stack_tmp[i] = stack_popArg_alloc(&(vm->stack));
|
||||
|
@ -2,4 +2,4 @@
|
||||
#define PIKA_VERSION_MINOR 11
|
||||
#define PIKA_VERSION_MICRO 9
|
||||
|
||||
#define PIKA_EDIT_TIME "2022/12/11 12:01:10"
|
||||
#define PIKA_EDIT_TIME "2022/12/22 15:33:13"
|
||||
|
@ -295,7 +295,7 @@ void arg_singlePrint(Arg* self, PIKA_BOOL in_REPL, char* end) {
|
||||
#if PIKA_PRINT_LLD_ENABLE
|
||||
__platform_printf("%lld%s", (long long int)arg_getInt(self), end);
|
||||
#else
|
||||
__platform_printf("%d%s", (int)arg_getInt(arg), end);
|
||||
__platform_printf("%d%s", (int)arg_getInt(self), end);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
@ -411,7 +411,7 @@ Arg* args_getArg(Args* self, char* name) {
|
||||
return (Arg*)node;
|
||||
}
|
||||
|
||||
Arg* args_getArgByidex(Args* self, int index) {
|
||||
Arg* args_getArgByIndex(Args* self, int index) {
|
||||
pika_assert(NULL != self);
|
||||
LinkNode* nodeNow = self->firstNode;
|
||||
if (NULL == nodeNow) {
|
||||
|
@ -41,7 +41,7 @@ void args_init(Args* self, Args* args);
|
||||
int32_t args_getSize(Args* self);
|
||||
LinkNode* args_getNode(Args* self, char* name);
|
||||
|
||||
Arg* args_getArgByIndex(Args* self, int32_t index);
|
||||
Arg* args_getArgByIndex(Args* self, int index);
|
||||
Arg* args_getArg(Args* self, char* name);
|
||||
PIKA_RES args_removeArg(Args* self, Arg* argNow);
|
||||
PIKA_RES args_moveArg(Args* self, Args* dict, Arg* arg);
|
||||
@ -112,7 +112,6 @@ PIKA_RES args_foreach(Args* self,
|
||||
char* args_getBuff(Args* self, int32_t size);
|
||||
PIKA_RES args_pushArg(Args* self, Arg* arg);
|
||||
PIKA_RES args_pushArg_name(Args* self, char* name, Arg* arg);
|
||||
Arg* args_getArgByidex(Args* self, int index);
|
||||
void* args_getHeapStruct(Args* self, char* name);
|
||||
PIKA_RES args_removeArg_notDeinitArg(Args* self, Arg* argNow);
|
||||
uint8_t* args_getBytes(Args* self, char* name);
|
||||
|
@ -2419,6 +2419,63 @@ TEST(vm, issue_not_in) {
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(vm, def_not_in) {
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
extern unsigned char pikaModules_py_a[];
|
||||
obj_linkLibrary(pikaMain, pikaModules_py_a);
|
||||
obj_run(pikaMain,
|
||||
"t = ['hmac-md5', 'other']\n"
|
||||
"def test(a=\"hmac-md5\"):\n"
|
||||
" print(\"input:\", a)\n"
|
||||
" print(\"table:\", t)\n"
|
||||
" if a not in t:\n"
|
||||
" print(\"a not in t\")\n"
|
||||
" else:\n"
|
||||
" print(\"a in t\")\n"
|
||||
"test()\n");
|
||||
/* assert */
|
||||
EXPECT_STREQ(log_buff[0], "a in t\r\n");
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(vm, fn_pos_kw_issue1) {
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
/* run */
|
||||
__platform_printf("BEGIN\r\n");
|
||||
obj_run(pikaMain,
|
||||
"def new(a=1, b=2):\n"
|
||||
" print(a, b)\n"
|
||||
"new(3, b = 2)\n");
|
||||
/* collect */
|
||||
/* assert */
|
||||
EXPECT_STREQ(log_buff[0], "3 2\r\n");
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(vm, fn_pos_kw_issue2) {
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
/* run */
|
||||
__platform_printf("BEGIN\r\n");
|
||||
obj_run(pikaMain,
|
||||
"def new(key:any, msg=None, digestmod=\"md5\"):\n"
|
||||
" print(key, msg, digestmod)\n"
|
||||
"new('123','456',digestmod=\"md5\")\n");
|
||||
/* collect */
|
||||
/* assert */
|
||||
EXPECT_STREQ(log_buff[0], "123 456 md5\r\n");
|
||||
/* deinit */
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
TEST_END
|
@ -169,8 +169,8 @@ TEST(args, index) {
|
||||
args_pushArg(args, arg_newInt(1));
|
||||
args_pushArg(args, arg_newFloat(2.4));
|
||||
|
||||
int a = arg_getInt(args_getArgByidex(args, 1));
|
||||
float b = arg_getFloat(args_getArgByidex(args, 0));
|
||||
int a = arg_getInt(args_getArgByIndex(args, 1));
|
||||
float b = arg_getFloat(args_getArgByIndex(args, 0));
|
||||
/* assert */
|
||||
EXPECT_EQ(a, 1);
|
||||
EXPECT_FLOAT_EQ(b, 2.4);
|
||||
|
@ -7,7 +7,7 @@ TEST(event, gpio) {
|
||||
/* init */
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
/* run */
|
||||
pikaVM_runSingleFile(pikaMain, "../../examples/TemplateDevice/gpio_cb.py");
|
||||
pikaVM_runSingleFile(pikaMain, "test/python/TemplateDevice/gpio_cb.py");
|
||||
|
||||
#define EVENT_SIGAL_IO_RISING_EDGE 0x01
|
||||
#define EVENT_SIGAL_IO_FALLING_EDGE 0x02
|
||||
|
@ -1,9 +1,19 @@
|
||||
#include "test_common.h"
|
||||
extern "C" {
|
||||
#include "../package/pikascript/pikascript-lib/PikaStdDevice/pika_hal.h"
|
||||
|
||||
int pika_hal_platform_GPIO_open(pika_dev* dev, char* name){
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pika_hal_platform_GPIO_close(pika_dev* dev){
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
TEST(hal, open) {
|
||||
pika_dev* io = pika_hal_open(PIKA_HAL_GPIO, "PA10");
|
||||
EXPECT_EQ(0, (uintptr_t)io);
|
||||
pika_hal_close(io);
|
||||
}
|
||||
|
@ -3644,6 +3644,21 @@ TEST(parser, _del) {
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
TEST(parser, _del_issue1) {
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
Args* buffs = New_strBuff();
|
||||
char* lines = "del(a)\n";
|
||||
__platform_printf("%s\n", lines);
|
||||
char* pikaAsm = Parser_linesToAsm(buffs, lines);
|
||||
__platform_printf("%s", pikaAsm);
|
||||
EXPECT_STREQ(pikaAsm,
|
||||
"B0\n"
|
||||
"0 DEL a\n"
|
||||
"B0\n");
|
||||
args_deinit(buffs);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
#if PIKA_SYNTAX_SLICE_ENABLE
|
||||
TEST(parser, issue_fa13f4) {
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
@ -5061,6 +5076,25 @@ TEST(parser, not_in) {
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
// TEST(parser, def_not_in) {
|
||||
// pikaMemInfo.heapUsedMax = 0;
|
||||
// Args* buffs = New_strBuff();
|
||||
// char* lines =
|
||||
// "def test(a=\"hmac-md5\"):\n"
|
||||
// " print(\"input:\",a)\n"
|
||||
// " print(\"table:\"t )\n"
|
||||
// " if a not in t:\n"
|
||||
// " print(\"a not in t\")else:\n"
|
||||
// " print(\"a in t\")\n"
|
||||
// "test()\n";
|
||||
// printf("%s\r\n", lines);
|
||||
// char* pikaAsm = Parser_linesToAsm(buffs, lines);
|
||||
// printf("%s", pikaAsm);
|
||||
// EXPECT_STREQ(pikaAsm, "");
|
||||
// args_deinit(buffs);
|
||||
// EXPECT_EQ(pikaMemNow(), 0);
|
||||
// }
|
||||
|
||||
#endif
|
||||
|
||||
TEST_END
|
@ -10,15 +10,16 @@ uint32_t log_index = 0;
|
||||
/* save printf content to log_buff */
|
||||
void __platform_printf(char* fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vprintf(fmt, args);
|
||||
va_end(args);
|
||||
for (int i = LOG_BUFF_MAX - 2; i >= 0; i--) {
|
||||
memcpy(log_buff[i + 1], log_buff[i], LOG_SIZE);
|
||||
}
|
||||
va_start(args, fmt);
|
||||
vsnprintf(log_buff[0], LOG_SIZE - 1, fmt, args);
|
||||
__platform_vsnprintf(log_buff[0], LOG_SIZE - 1, fmt, args);
|
||||
va_end(args);
|
||||
/* putchar */
|
||||
for (int i = 0; i < strlen(log_buff[0]); i++) {
|
||||
__platform_putchar(log_buff[0][i]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
21
test/python/TemplateDevice/gpio_cb.py
Normal file
21
test/python/TemplateDevice/gpio_cb.py
Normal file
@ -0,0 +1,21 @@
|
||||
import TemplateDevice
|
||||
|
||||
io1 = TemplateDevice.GPIO()
|
||||
io1.setPin('PA8')
|
||||
io1.setMode('in')
|
||||
io1.enable()
|
||||
|
||||
EVENT_SIGAL_IO_RISING_EDGE = 0x01
|
||||
EVENT_SIGAL_IO_FALLING_EDGE = 0x02
|
||||
|
||||
|
||||
def callBack1(signal):
|
||||
if signal == EVENT_SIGAL_IO_RISING_EDGE:
|
||||
print('get rising edge!')
|
||||
elif signal == EVENT_SIGAL_IO_FALLING_EDGE:
|
||||
print('get falling edge!')
|
||||
else:
|
||||
return signal
|
||||
|
||||
|
||||
io1.addEventCallBack(callBack1)
|
16
test/python/hmac/test_hmac.py
Normal file
16
test/python/hmac/test_hmac.py
Normal file
@ -0,0 +1,16 @@
|
||||
import hmac
|
||||
|
||||
secret = "0123456789"
|
||||
payload = "helloworld"
|
||||
|
||||
h = hmac.new(secret.encode(),digestmod="md5")
|
||||
h.update(payload.encode())
|
||||
print("hmac-md5:",h.hexdigest())
|
||||
|
||||
h = hmac.new(secret.encode(),digestmod="sha1")
|
||||
h.update(payload.encode())
|
||||
print("hmac-sha1:",h.hexdigest())
|
||||
|
||||
h = hmac.new(secret.encode(),digestmod="sha256")
|
||||
h.update(payload.encode())
|
||||
print("hmac-sha256:",h.hexdigest())
|
@ -267,6 +267,8 @@ TEST(requests, get_basic) {
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
|
||||
//! Enable it manually if needed
|
||||
#if 0
|
||||
TEST(requests, post_data) {
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
extern unsigned char pikaModules_py_a[];
|
||||
@ -278,5 +280,6 @@ TEST(requests, post_data) {
|
||||
obj_deinit(pikaMain);
|
||||
EXPECT_EQ(pikaMemNow(), 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user