From d317da20c28a30caee76eabafaa3088a962f9da9 Mon Sep 17 00:00:00 2001 From: lyon Date: Wed, 27 Dec 2023 22:28:48 +0800 Subject: [PATCH 1/5] patch for modbus network lvgl --- package/pika_lvgl/pika_lvgl.c | 36 +++- package/pika_lvgl/pika_lvgl.pyi | 8 +- package/pika_lvgl/pika_lvgl_lv_obj.c | 2 +- port/linux/package/pikascript/_network.pyi | 31 +-- port/linux/package/pikascript/modbus.py | 3 +- port/linux/package/pikascript/network.py | 34 ++++ .../PikaStdDevice/pika_hal_table.h | 1 + .../pikascript-lib/network/_network_WLAN.c | 184 +++++------------- src/PikaPlatform.c | 10 +- 9 files changed, 145 insertions(+), 164 deletions(-) diff --git a/package/pika_lvgl/pika_lvgl.c b/package/pika_lvgl/pika_lvgl.c index a395a4633..02a7700ab 100644 --- a/package/pika_lvgl/pika_lvgl.c +++ b/package/pika_lvgl/pika_lvgl.c @@ -1,4 +1,4 @@ -#if defined(LV_LVGL_H_INCLUDE_SIMPLE) +#if defined(LV_LVGL_H_INCLUDE_SIMPLE) #include "lvgl.h" #else #include "../../lvgl.h" @@ -13,9 +13,9 @@ #include "pika_lvgl_FLEX_ALIGN.h" #include "pika_lvgl_FLEX_FLOW.h" #include "pika_lvgl_LAYOUT_FLEX.h" -#include "pika_lvgl_SIZE.h" #include "pika_lvgl_OPA.h" #include "pika_lvgl_PALETTE.h" +#include "pika_lvgl_SIZE.h" #include "pika_lvgl_STATE.h" #include "pika_lvgl_TEXT_DECOR.h" #include "pika_lvgl_arc.h" @@ -27,11 +27,14 @@ PikaObj* pika_lv_event_listener_g; Args* pika_lv_id_register_g; +pika_platform_thread_mutex_t pika_lv_global_mutex_g = {0}; -#if !PIKASCRIPT_VERSION_REQUIRE_MINIMUN(1, 11, 7) -#error "pikascript version must be greater than 1.11.7" +#if !PIKASCRIPT_VERSION_REQUIRE_MINIMUN(1, 13, 1) +#error "pikascript version must be greater than 1.13.1" #endif +volatile int g_lvgl_inited = 0; + void pika_lvgl_STATE___init__(PikaObj* self) { obj_setInt(self, "DEFAULT", LV_STATE_DEFAULT); obj_setInt(self, "CHECKED", LV_STATE_CHECKED); @@ -49,6 +52,20 @@ void pika_lvgl_STATE___init__(PikaObj* self) { obj_setInt(self, "ANY", LV_STATE_ANY); } +void pika_lvgl_lock(PikaObj* self) { + if (!g_lvgl_inited) { + return; + } + pika_platform_thread_mutex_lock(&pika_lv_global_mutex_g); +} + +void pika_lvgl_unlock(PikaObj* self) { + if (!g_lvgl_inited) { + return; + } + pika_platform_thread_mutex_unlock(&pika_lv_global_mutex_g); +} + void pika_lvgl_flag_t___init__(PikaObj* self) { obj_setInt(self, "HIDDEN", LV_OBJ_FLAG_HIDDEN); obj_setInt(self, "CLICKABLE", LV_OBJ_FLAG_CLICKABLE); @@ -201,9 +218,8 @@ PikaObj* pika_lvgl_scr_act(PikaObj* self) { return new_obj; } -volatile int g_lvgl_inited = 0; void pika_lvgl___init__(PikaObj* self) { - if (!lv_is_initialized()){ + if (!lv_is_initialized()) { pika_debug("Error: lvgl is not initialized"); return; } @@ -211,7 +227,11 @@ void pika_lvgl___init__(PikaObj* self) { pika_lv_event_listener_g = obj_getObj(self, "lv_event_listener"); pika_lv_id_register_g = New_args(NULL); if (!g_lvgl_inited) { + pika_debug("Init pika_lv_global_mutex_g"); + pika_platform_thread_mutex_init(&pika_lv_global_mutex_g); + pika_lvgl_lock(NULL); lv_png_init(); + pika_lvgl_unlock(NULL); g_lvgl_inited = 1; } } @@ -301,11 +321,11 @@ void pika_lvgl_LAYOUT_FLEX___init__(PikaObj* self) { obj_setInt(self, "value", LV_LAYOUT_FLEX); } -void pika_lvgl_SIZE___init__(PikaObj *self){ +void pika_lvgl_SIZE___init__(PikaObj* self) { obj_setInt(self, "CONTENT", LV_SIZE_CONTENT); } -int pika_lvgl_pct(PikaObj *self, int x){ +int pika_lvgl_pct(PikaObj* self, int x) { return LV_PCT(x); } diff --git a/package/pika_lvgl/pika_lvgl.pyi b/package/pika_lvgl/pika_lvgl.pyi index c70e11b4e..95f096ca9 100644 --- a/package/pika_lvgl/pika_lvgl.pyi +++ b/package/pika_lvgl/pika_lvgl.pyi @@ -8,6 +8,10 @@ def __init__(): ... def __del__(): ... +def lock(): ... +def unlock(): ... + + class EVENT: ALL: int PRESSED: int @@ -383,7 +387,7 @@ class lv_obj: # def area_is_visible(self, area: lv_area_t) -> int: ... def is_visible(self) -> int: ... def set_ext_click_area(self, size: int): ... - def set_style_size(self, value:int, selector: int): ... + def set_style_size(self, value: int, selector: int): ... # def get_click_area(self, area: lv_area_t): ... def hit_test(self, point: point_t) -> int: ... def set_flex_flow(self, flow: int): ... @@ -606,6 +610,7 @@ class canvas(lv_obj): class chart_series_t: ... + class chart(lv_obj): def __init__(self, parent: lv_obj): ... def set_point_count(self, cnt: int): ... @@ -617,6 +622,7 @@ class chart(lv_obj): def set_ext_y_array(self, ser: chart_series_t, array: any): ... def refresh(self): ... + def scr_act() -> lv_obj: ... def pct(x: int) -> int: ... def timer_create_basic() -> lv_timer_t: ... diff --git a/package/pika_lvgl/pika_lvgl_lv_obj.c b/package/pika_lvgl/pika_lvgl_lv_obj.c index ca929b74d..bec301cfc 100644 --- a/package/pika_lvgl/pika_lvgl_lv_obj.c +++ b/package/pika_lvgl/pika_lvgl_lv_obj.c @@ -1,4 +1,4 @@ -#if defined(LV_LVGL_H_INCLUDE_SIMPLE) +#if defined(LV_LVGL_H_INCLUDE_SIMPLE) #include "lvgl.h" #else #include "../../lvgl.h" diff --git a/port/linux/package/pikascript/_network.pyi b/port/linux/package/pikascript/_network.pyi index ccb7f0482..b9dba2b45 100644 --- a/port/linux/package/pikascript/_network.pyi +++ b/port/linux/package/pikascript/_network.pyi @@ -7,37 +7,44 @@ STAT_NO_AP_FOUND: int STAT_CONNECT_FAIL: int STAT_GOT_IP: int - -class WLAN: +class NET: def __init__(self, interface_id: int): ... def active(self, is_active: int): ... def checkActive(self) -> int: ... - def connect(self, ssid: str, key: str): ... - - def connectWithBssid(self, ssid: str, key: str, bssid: str): ... - - def disconnect(self): ... - def status(self) -> int: ... def statusWithParam(self, param: str) -> int: ... + def ifconfig(self, ip: str, mask: str, gateway: str, dns: str): ... + + def checkIfconfig(self) -> list: ... + + def close(self): ... + + +class WLAN(NET): + def __init__(self, interface_id: int): ... + + def connect(self, ssid: str, key: str): ... + def isconnected(self) -> int: ... def config(self, **kwargs): ... def checkConfig(self, param: str) -> any: ... - def ifconfig(self, ip: str, mask: str, gateway: str, dns: str): ... - - def checkIfconfig(self) -> list: ... + def connectWithBssid(self, ssid: str, key: str, bssid: str): ... def scan(self) -> list: ... - def close(self): ... + +class LAN(NET): + def __init__(self, interface_id: int): ... + + def isconnected(self) -> int: ... def __init__(): ... diff --git a/port/linux/package/pikascript/modbus.py b/port/linux/package/pikascript/modbus.py index a95af950b..077e55ddb 100644 --- a/port/linux/package/pikascript/modbus.py +++ b/port/linux/package/pikascript/modbus.py @@ -378,7 +378,8 @@ class ModBusRTU(ModBus): def setUart(self, uart: PikaStdDevice.UART): self._uart = uart - uart.setCallBack(self.recvCallback, uart.SIGNAL_RX) + debug('set uart', uart) + uart.setCallback(self.recvCallback, uart.SIGNAL_RX) def recv(self, count: int = 10): """ diff --git a/port/linux/package/pikascript/network.py b/port/linux/package/pikascript/network.py index 7bcd644c2..6f13cc007 100644 --- a/port/linux/package/pikascript/network.py +++ b/port/linux/package/pikascript/network.py @@ -11,6 +11,40 @@ STAT_CONNECT_FAIL = _network.STAT_CONNECT_FAIL STAT_GOT_IP = _network.STAT_GOT_IP _instence:"WLAN" = None + +class LAN(_network.LAN): + def __init__(self, interface_id: int): + super().__init__(interface_id) + + def active(self, is_active=None): + if is_active is None: + return super().checkActive() + else: + return super().active(is_active) + + def status(self, param=None): + if param is None: + return super().status() + else: + return super().statusWithParam(param) + + def isconnected(self) -> int: + return super().isconnected() + + def config(self, *para, **kwargs): + if len(para) == 1: + return super().checkConfig(para[0]) + else: + return super().config(**kwargs) + + def ifconfig(self, config=None): + if config is None: + t = super().checkIfconfig() + return (t[0], t[1], t[2], t[3]) + else: + return super().ifconfig( + config[0], config[1], config[2], config[3]) + class WLAN(_network.WLAN): def __init__(self, interface_id: int): global _instence diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal_table.h b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal_table.h index aa76666d4..dc7867ce1 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal_table.h +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal_table.h @@ -12,6 +12,7 @@ pika_hal_table_add(PWM) pika_hal_table_add(SOFT_SPI) pika_hal_table_add(SOFT_IIC) pika_hal_table_add(WIFI) +pika_hal_table_add(LAN) pika_hal_table_add(TIM) pika_hal_table_add(SOFT_TIM) pika_hal_table_add(CAM) diff --git a/port/linux/package/pikascript/pikascript-lib/network/_network_WLAN.c b/port/linux/package/pikascript/pikascript-lib/network/_network_WLAN.c index 604c8cad6..019a2388f 100644 --- a/port/linux/package/pikascript/pikascript-lib/network/_network_WLAN.c +++ b/port/linux/package/pikascript/pikascript-lib/network/_network_WLAN.c @@ -1,13 +1,9 @@ #include "_network_WLAN.h" +#include "_network_NET.h" #include "../pikascript-lib/PikaStdDevice/pika_hal.h" #include "PikaStdData_List.h" #include "PikaStdData_Tuple.h" - -#define check_res(res) \ - if (res != 0) { \ - pika_platform_printf("check_res failed: %d, at %s:%d:%s()\r\n", res, \ - __FILE__, __LINE__, __FUNCTION__); \ - } +#include "_network_common.h" void _network_WLAN___init__(PikaObj* self, int interface_id) { pika_dev* hal_wifi = pika_hal_open(PIKA_HAL_WIFI, "WLAN0"); @@ -16,78 +12,19 @@ void _network_WLAN___init__(PikaObj* self, int interface_id) { } pika_hal_WIFI_config cfg = {0}; cfg.mode = interface_id; - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_CONFIG, &cfg)); - obj_setPtr(self, "hal_wifi", hal_wifi); -} - -void _network_WLAN_active(PikaObj* self, int is_active) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { - return; - } - if (is_active) { - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_ENABLE)); - return; - } - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_DISABLE)); - return; -} - -int _network_WLAN_checkActive(PikaObj* self) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { - return -1; - } - int is_active = 0; - check_res( - pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_GET_ACTIVE, &is_active)); - return is_active; -} - -void _network_WLAN_config(PikaObj* self, PikaDict* kwargs) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { - return; - } - pika_hal_WIFI_config cfg = {0}; - if (pikaDict_isArgExist(kwargs, "ssid")) { - strcpy(cfg.ap_ssid, pikaDict_getStr(kwargs, "ssid")); - } - if (pikaDict_isArgExist(kwargs, "password")) { - strcpy(cfg.ap_password, pikaDict_getStr(kwargs, "password")); - } - if (pikaDict_isArgExist(kwargs, "channel")) { - cfg.channel = pikaDict_getInt(kwargs, "channel") + 1; - } - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_CONFIG, &cfg)); -} - -Arg* _network_WLAN_checkConfig(PikaObj* self, char* param) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { - return NULL; - } - pika_hal_WIFI_config* cfg = hal_wifi->ioctl_config; - if (strcmp(param, "ssid") == 0) { - return arg_newStr(cfg->ap_ssid); - } else if (strcmp(param, "password") == 0) { - return arg_newStr(cfg->ap_password); - } else if (strcmp(param, "channel") == 0) { - return arg_newInt(cfg->channel - 1); - } else { - return NULL; - } + _net_check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_CONFIG, &cfg)); + obj_setPtr(self, "hal_dev", hal_wifi); } void _network_WLAN_connect(PikaObj* self, char* ssid, char* key) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); + pika_dev* hal_wifi = _NET_OBJ_2DEV(self); if (hal_wifi == NULL) { return; } pika_hal_WIFI_connect_config conncfg = {0}; strcpy(conncfg.ssid, ssid); strcpy(conncfg.password, key); - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); + _net_check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); return; } @@ -95,7 +32,7 @@ void _network_WLAN_connectWithBssid(PikaObj* self, char* ssid, char* key, char* bssid) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); + pika_dev* hal_wifi = _NET_OBJ_2DEV(self); if (hal_wifi == NULL) { return; } @@ -103,91 +40,32 @@ void _network_WLAN_connectWithBssid(PikaObj* self, strcpy(conncfg.ssid, ssid); strcpy(conncfg.password, key); strcpy(conncfg.bssid, bssid); - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); + _net_check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); } void _network_WLAN_disconnect(PikaObj* self) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); + pika_dev* hal_wifi = _NET_OBJ_2DEV(self); if (hal_wifi == NULL) { return; } - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_DISCONNECT)); -} - -int _network_WLAN_status(PikaObj* self) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { - return -1; - } - PIKA_HAL_WIFI_STATUS status = 0; - check_res( - pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_GET_STATUS, &status)); - return status; + _net_check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_DISCONNECT)); } int _network_WLAN_isconnected(PikaObj* self) { - if (_network_WLAN_status(self) == PIKA_HAL_WIFI_STATUS_GOT_IP) { + if (_network_NET_status(self) == PIKA_HAL_WIFI_STATUS_GOT_IP) { return 1; } else { return 0; } } -int _network_WLAN_statusWithParam(PikaObj* self, char* param) { - return _network_WLAN_status(self); -} - -PikaObj* _network_WLAN_checkIfconfig(PikaObj* self) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { - return NULL; - } - pika_hal_WIFI_ifconfig ifconfig = {0}; - check_res( - pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_GET_IFCONFIG, &ifconfig)); - PikaObj* ifconfig_list = newNormalObj(New_PikaStdData_List); - PikaStdData_List___init__(ifconfig_list); - Arg* arg = arg_newStr(ifconfig.ip); - PikaStdData_List_append(ifconfig_list, arg); - arg_deinit(arg); - arg = arg_newStr(ifconfig.netmask); - PikaStdData_List_append(ifconfig_list, arg); - arg_deinit(arg); - arg = arg_newStr(ifconfig.gateway); - PikaStdData_List_append(ifconfig_list, arg); - arg_deinit(arg); - arg = arg_newStr(ifconfig.dns); - PikaStdData_List_append(ifconfig_list, arg); - arg_deinit(arg); - return ifconfig_list; -} - -void _network_WLAN_ifconfig(PikaObj* self, - char* ip, - char* mask, - char* gateway, - char* dns) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { - return; - } - pika_hal_WIFI_ifconfig ifconfig = {0}; - strcpy(ifconfig.ip, ip); - strcpy(ifconfig.netmask, mask); - strcpy(ifconfig.gateway, gateway); - strcpy(ifconfig.dns, dns); - check_res( - pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_SET_IFCONFIG, &ifconfig)); - return; -} - PikaObj* _network_WLAN_scan(PikaObj* self) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); + pika_dev* hal_wifi = _NET_OBJ_2DEV(self); if (hal_wifi == NULL) { return NULL; } pika_hal_WIFI_scan_result* result = NULL; - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_SCAN, &result)); + _net_check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_SCAN, &result)); if (NULL == result) { return NULL; } @@ -224,10 +102,38 @@ PikaObj* _network_WLAN_scan(PikaObj* self) { return scan_list; } -void _network_WLAN_close(PikaObj* self) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { + +void _network_WLAN_config(PikaObj* self, PikaDict* kwargs) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { return; } - check_res(pika_hal_close(hal_wifi)); + pika_hal_WIFI_config cfg = {0}; + if (pikaDict_isArgExist(kwargs, "ssid")) { + strcpy(cfg.ap_ssid, pikaDict_getStr(kwargs, "ssid")); + } + if (pikaDict_isArgExist(kwargs, "password")) { + strcpy(cfg.ap_password, pikaDict_getStr(kwargs, "password")); + } + if (pikaDict_isArgExist(kwargs, "channel")) { + cfg.channel = pikaDict_getInt(kwargs, "channel") + 1; + } + _net_check_res(pika_hal_ioctl(hal_net, PIKA_HAL_IOCTL_CONFIG, &cfg)); +} + +Arg* _network_WLAN_checkConfig(PikaObj* self, char* param) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { + return NULL; + } + pika_hal_WIFI_config* cfg = hal_net->ioctl_config; + if (strcmp(param, "ssid") == 0) { + return arg_newStr(cfg->ap_ssid); + } else if (strcmp(param, "password") == 0) { + return arg_newStr(cfg->ap_password); + } else if (strcmp(param, "channel") == 0) { + return arg_newInt(cfg->channel - 1); + } else { + return NULL; + } } diff --git a/src/PikaPlatform.c b/src/PikaPlatform.c index 0fff63e99..14f87c9e6 100644 --- a/src/PikaPlatform.c +++ b/src/PikaPlatform.c @@ -715,7 +715,10 @@ PIKA_WEAK int pika_platform_thread_mutex_lock(pika_platform_thread_mutex_t* m) { #ifdef __linux return pthread_mutex_lock(&(m->mutex)); #elif PIKA_FREERTOS_ENABLE - return xSemaphoreTake(m->mutex, portMAX_DELAY); + if (pdTRUE == xSemaphoreTake(m->mutex, portMAX_DELAY)){ + return 0; + } + return -1; #elif PIKA_RTTHREAD_ENABLE return rt_mutex_take((m->mutex), RT_WAITING_FOREVER); #else @@ -729,7 +732,10 @@ PIKA_WEAK int pika_platform_thread_mutex_trylock( #ifdef __linux return pthread_mutex_trylock(&(m->mutex)); #elif PIKA_FREERTOS_ENABLE - return xSemaphoreTake(m->mutex, 0); + if (pdTRUE == xSemaphoreTake(m->mutex, 0)){ + return 0; + } + return -1; #elif PIKA_RTTHREAD_ENABLE return rt_mutex_take((m->mutex), 0); #else From 0ce3e4c7a470ccfc1547c70cbb4f4d06a63e1276 Mon Sep 17 00:00:00 2001 From: lyon Date: Wed, 27 Dec 2023 22:35:04 +0800 Subject: [PATCH 2/5] sync pika_hal --- .../pikascript-lib/PikaStdDevice/pika_hal.c | 8 ++++- .../pikascript-lib/PikaStdDevice/pika_hal.h | 36 +++++++++++++++---- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal.c b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal.c index 1ab242189..003360a39 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal.c @@ -335,6 +335,12 @@ int pika_hal_WIFI_ioctl_merge_config(pika_hal_WIFI_config* dst, _IOCTL_CONFIG_USE_DEFAULT(user_data, NULL); return 0; } + +int pika_hal_LAN_ioctl_merge_config(pika_hal_LAN_config* dst, + pika_hal_LAN_config* src) { + _IOCTL_CONFIG_USE_DEFAULT(user_data, NULL); + return 0; +} int pika_hal_SG_ioctl_merge_config(pika_hal_SG_config* dst, pika_hal_SG_config* src) { @@ -480,7 +486,7 @@ int pika_hal_circularQueue_isFull(pika_hal_CircularQueue* cb) { int pika_hal_circularQueue_peek(pika_hal_CircularQueue* cb, uint8_t* value) { if (cb->count == 0) { - return -1; + return -1; // 缓冲区为空 } #if PIKA_HAL_CIRCULAR_QUEUE_MUTEX_ENABLE diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal.h b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal.h index e11b78697..3cc96de75 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal.h +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal.h @@ -32,17 +32,27 @@ typedef enum { PIKA_HAL_IOCTL_CONFIG, PIKA_HAL_IOCTL_ENABLE, PIKA_HAL_IOCTL_DISABLE, - PIKA_HAL_IOCTL_WIFI_GET_ACTIVE, - PIKA_HAL_IOCTL_WIFI_GET_STATUS, + PIKA_HAL_IOCTL_NET_GET_ACTIVE, + PIKA_HAL_IOCTL_NET_GET_STATUS, PIKA_HAL_IOCTL_WIFI_SCAN, PIKA_HAL_IOCTL_WIFI_CONNECT, PIKA_HAL_IOCTL_WIFI_DISCONNECT, - PIKA_HAL_IOCTL_WIFI_SET_IFCONFIG, - PIKA_HAL_IOCTL_WIFI_GET_IFCONFIG, + PIKA_HAL_IOCTL_NET_SET_IFCONFIG, + PIKA_HAL_IOCTL_NET_GET_IFCONFIG, PIKA_HAL_IOCTL_CAM_CAPTURE, _ = 0xFFFFFFFF, // make sure it is 4 byte width } PIKA_HAL_IOCTL_CMD; +#define PIKA_HAL_IOCTL_WIFI_GET_ACTIVE PIKA_HAL_IOCTL_NET_GET_ACTIVE +#define PIKA_HAL_IOCTL_WIFI_GET_STATUS PIKA_HAL_IOCTL_NET_GET_STATUS +#define PIKA_HAL_IOCTL_WIFI_SET_IFCONFIG PIKA_HAL_IOCTL_NET_SET_IFCONFIG +#define PIKA_HAL_IOCTL_WIFI_GET_IFCONFIG PIKA_HAL_IOCTL_NET_GET_IFCONFIG + +#define PIKA_HAL_IOCTL_LAN_GET_ACTIVE PIKA_HAL_IOCTL_NET_GET_ACTIVE +#define PIKA_HAL_IOCTL_LAN_GET_STATUS PIKA_HAL_IOCTL_NET_GET_STATUS +#define PIKA_HAL_IOCTL_LAN_SET_IFCONFIG PIKA_HAL_IOCTL_NET_SET_IFCONFIG +#define PIKA_HAL_IOCTL_LAN_GET_IFCONFIG PIKA_HAL_IOCTL_NET_GET_IFCONFIG + /* posix file like API */ pika_dev* pika_hal_open(PIKA_HAL_DEV_TYPE dev_type, char* name); int pika_hal_close(pika_dev* dev); @@ -432,6 +442,13 @@ typedef enum { PIKA_HAL_WIFI_STATUS_GOT_IP, } PIKA_HAL_WIFI_STATUS; +typedef enum { + _PIKA_HAL_LAN_STATUS_UNUSED = 0, + PIKA_HAL_LAN_STATUS_IDLE, + PIKA_HAL_LAN_STATUS_CONNECTING, + PIKA_HAL_LAN_STATUS_GOT_IP, +} PIKA_HAL_LAN_STATUS; + typedef enum { _PIKA_HAL_WIFI_CHANNEL_UNUSED = 0, PIKA_HAL_WIFI_CHANNEL_0, @@ -479,12 +496,15 @@ typedef struct pika_hal_WIFI_connect_config { char password[PIKA_HAL_WIFI_PARAM_MAX_LEN]; } pika_hal_WIFI_connect_config; -typedef struct pika_hal_WIFI_ifconfig { +typedef struct pika_hal_NET_ifconfig { char ip[PIKA_HAL_WIFI_PARAM_MAX_LEN]; char netmask[PIKA_HAL_WIFI_PARAM_MAX_LEN]; char gateway[PIKA_HAL_WIFI_PARAM_MAX_LEN]; char dns[PIKA_HAL_WIFI_PARAM_MAX_LEN]; -} pika_hal_WIFI_ifconfig; +} pika_hal_NET_ifconfig; + +#define pika_hal_WIFI_ifconfig pika_hal_NET_ifconfig +#define pika_hal_LAN_ifconfig pika_hal_NET_ifconfig typedef struct pika_hal_WIFI_record { char ssid[PIKA_HAL_WIFI_PARAM_MAX_LEN]; @@ -500,6 +520,10 @@ typedef struct pika_hal_WIFI_scan_result { pika_hal_WIFI_record records[]; } pika_hal_WIFI_scan_result; +typedef struct pika_hal_LAN_config { + void* user_data; +} pika_hal_LAN_config; + typedef enum { _PIKA_HAL_PWM_PERIOD_UNUSED = 0, PIKA_HAL_TIM_PERIOD_1NS = 1, From 07065ac3cf46a36cec5b9ac09103a9939ae9fa95 Mon Sep 17 00:00:00 2001 From: lyon1998 Date: Wed, 27 Dec 2023 22:37:26 +0800 Subject: [PATCH 3/5] sync to package --- package/PikaStdDevice/pika_hal.c | 6 + package/PikaStdDevice/pika_hal.h | 36 +++- package/PikaStdDevice/pika_hal_table.h | 1 + package/modbus/modbus.py | 3 +- package/network/_network.pyi | 31 +-- package/network/_network_WLAN.c | 184 +++++------------- package/network/network.py | 34 ++++ .../pikascript-lib/PikaStdDevice/pika_hal.c | 6 +- .../pikascript-lib/network/_network_WLAN.c | 7 +- src/PikaParser.c | 17 +- src/PikaPlatform.c | 10 +- src/PikaVersion.h | 2 +- 12 files changed, 149 insertions(+), 188 deletions(-) diff --git a/package/PikaStdDevice/pika_hal.c b/package/PikaStdDevice/pika_hal.c index 1ab242189..c8783d072 100644 --- a/package/PikaStdDevice/pika_hal.c +++ b/package/PikaStdDevice/pika_hal.c @@ -336,6 +336,12 @@ int pika_hal_WIFI_ioctl_merge_config(pika_hal_WIFI_config* dst, return 0; } +int pika_hal_LAN_ioctl_merge_config(pika_hal_LAN_config* dst, + pika_hal_LAN_config* src) { + _IOCTL_CONFIG_USE_DEFAULT(user_data, NULL); + return 0; +} + int pika_hal_SG_ioctl_merge_config(pika_hal_SG_config* dst, pika_hal_SG_config* src) { _IOCTL_CONFIG_USE_DEFAULT(waveform, PIKA_HAL_SG_WAVEFORM_SINE); diff --git a/package/PikaStdDevice/pika_hal.h b/package/PikaStdDevice/pika_hal.h index e11b78697..3cc96de75 100644 --- a/package/PikaStdDevice/pika_hal.h +++ b/package/PikaStdDevice/pika_hal.h @@ -32,17 +32,27 @@ typedef enum { PIKA_HAL_IOCTL_CONFIG, PIKA_HAL_IOCTL_ENABLE, PIKA_HAL_IOCTL_DISABLE, - PIKA_HAL_IOCTL_WIFI_GET_ACTIVE, - PIKA_HAL_IOCTL_WIFI_GET_STATUS, + PIKA_HAL_IOCTL_NET_GET_ACTIVE, + PIKA_HAL_IOCTL_NET_GET_STATUS, PIKA_HAL_IOCTL_WIFI_SCAN, PIKA_HAL_IOCTL_WIFI_CONNECT, PIKA_HAL_IOCTL_WIFI_DISCONNECT, - PIKA_HAL_IOCTL_WIFI_SET_IFCONFIG, - PIKA_HAL_IOCTL_WIFI_GET_IFCONFIG, + PIKA_HAL_IOCTL_NET_SET_IFCONFIG, + PIKA_HAL_IOCTL_NET_GET_IFCONFIG, PIKA_HAL_IOCTL_CAM_CAPTURE, _ = 0xFFFFFFFF, // make sure it is 4 byte width } PIKA_HAL_IOCTL_CMD; +#define PIKA_HAL_IOCTL_WIFI_GET_ACTIVE PIKA_HAL_IOCTL_NET_GET_ACTIVE +#define PIKA_HAL_IOCTL_WIFI_GET_STATUS PIKA_HAL_IOCTL_NET_GET_STATUS +#define PIKA_HAL_IOCTL_WIFI_SET_IFCONFIG PIKA_HAL_IOCTL_NET_SET_IFCONFIG +#define PIKA_HAL_IOCTL_WIFI_GET_IFCONFIG PIKA_HAL_IOCTL_NET_GET_IFCONFIG + +#define PIKA_HAL_IOCTL_LAN_GET_ACTIVE PIKA_HAL_IOCTL_NET_GET_ACTIVE +#define PIKA_HAL_IOCTL_LAN_GET_STATUS PIKA_HAL_IOCTL_NET_GET_STATUS +#define PIKA_HAL_IOCTL_LAN_SET_IFCONFIG PIKA_HAL_IOCTL_NET_SET_IFCONFIG +#define PIKA_HAL_IOCTL_LAN_GET_IFCONFIG PIKA_HAL_IOCTL_NET_GET_IFCONFIG + /* posix file like API */ pika_dev* pika_hal_open(PIKA_HAL_DEV_TYPE dev_type, char* name); int pika_hal_close(pika_dev* dev); @@ -432,6 +442,13 @@ typedef enum { PIKA_HAL_WIFI_STATUS_GOT_IP, } PIKA_HAL_WIFI_STATUS; +typedef enum { + _PIKA_HAL_LAN_STATUS_UNUSED = 0, + PIKA_HAL_LAN_STATUS_IDLE, + PIKA_HAL_LAN_STATUS_CONNECTING, + PIKA_HAL_LAN_STATUS_GOT_IP, +} PIKA_HAL_LAN_STATUS; + typedef enum { _PIKA_HAL_WIFI_CHANNEL_UNUSED = 0, PIKA_HAL_WIFI_CHANNEL_0, @@ -479,12 +496,15 @@ typedef struct pika_hal_WIFI_connect_config { char password[PIKA_HAL_WIFI_PARAM_MAX_LEN]; } pika_hal_WIFI_connect_config; -typedef struct pika_hal_WIFI_ifconfig { +typedef struct pika_hal_NET_ifconfig { char ip[PIKA_HAL_WIFI_PARAM_MAX_LEN]; char netmask[PIKA_HAL_WIFI_PARAM_MAX_LEN]; char gateway[PIKA_HAL_WIFI_PARAM_MAX_LEN]; char dns[PIKA_HAL_WIFI_PARAM_MAX_LEN]; -} pika_hal_WIFI_ifconfig; +} pika_hal_NET_ifconfig; + +#define pika_hal_WIFI_ifconfig pika_hal_NET_ifconfig +#define pika_hal_LAN_ifconfig pika_hal_NET_ifconfig typedef struct pika_hal_WIFI_record { char ssid[PIKA_HAL_WIFI_PARAM_MAX_LEN]; @@ -500,6 +520,10 @@ typedef struct pika_hal_WIFI_scan_result { pika_hal_WIFI_record records[]; } pika_hal_WIFI_scan_result; +typedef struct pika_hal_LAN_config { + void* user_data; +} pika_hal_LAN_config; + typedef enum { _PIKA_HAL_PWM_PERIOD_UNUSED = 0, PIKA_HAL_TIM_PERIOD_1NS = 1, diff --git a/package/PikaStdDevice/pika_hal_table.h b/package/PikaStdDevice/pika_hal_table.h index aa76666d4..dc7867ce1 100644 --- a/package/PikaStdDevice/pika_hal_table.h +++ b/package/PikaStdDevice/pika_hal_table.h @@ -12,6 +12,7 @@ pika_hal_table_add(PWM) pika_hal_table_add(SOFT_SPI) pika_hal_table_add(SOFT_IIC) pika_hal_table_add(WIFI) +pika_hal_table_add(LAN) pika_hal_table_add(TIM) pika_hal_table_add(SOFT_TIM) pika_hal_table_add(CAM) diff --git a/package/modbus/modbus.py b/package/modbus/modbus.py index a95af950b..077e55ddb 100644 --- a/package/modbus/modbus.py +++ b/package/modbus/modbus.py @@ -378,7 +378,8 @@ class ModBusRTU(ModBus): def setUart(self, uart: PikaStdDevice.UART): self._uart = uart - uart.setCallBack(self.recvCallback, uart.SIGNAL_RX) + debug('set uart', uart) + uart.setCallback(self.recvCallback, uart.SIGNAL_RX) def recv(self, count: int = 10): """ diff --git a/package/network/_network.pyi b/package/network/_network.pyi index ccb7f0482..b9dba2b45 100644 --- a/package/network/_network.pyi +++ b/package/network/_network.pyi @@ -7,37 +7,44 @@ STAT_NO_AP_FOUND: int STAT_CONNECT_FAIL: int STAT_GOT_IP: int - -class WLAN: +class NET: def __init__(self, interface_id: int): ... def active(self, is_active: int): ... def checkActive(self) -> int: ... - def connect(self, ssid: str, key: str): ... - - def connectWithBssid(self, ssid: str, key: str, bssid: str): ... - - def disconnect(self): ... - def status(self) -> int: ... def statusWithParam(self, param: str) -> int: ... + def ifconfig(self, ip: str, mask: str, gateway: str, dns: str): ... + + def checkIfconfig(self) -> list: ... + + def close(self): ... + + +class WLAN(NET): + def __init__(self, interface_id: int): ... + + def connect(self, ssid: str, key: str): ... + def isconnected(self) -> int: ... def config(self, **kwargs): ... def checkConfig(self, param: str) -> any: ... - def ifconfig(self, ip: str, mask: str, gateway: str, dns: str): ... - - def checkIfconfig(self) -> list: ... + def connectWithBssid(self, ssid: str, key: str, bssid: str): ... def scan(self) -> list: ... - def close(self): ... + +class LAN(NET): + def __init__(self, interface_id: int): ... + + def isconnected(self) -> int: ... def __init__(): ... diff --git a/package/network/_network_WLAN.c b/package/network/_network_WLAN.c index 604c8cad6..019a2388f 100644 --- a/package/network/_network_WLAN.c +++ b/package/network/_network_WLAN.c @@ -1,13 +1,9 @@ #include "_network_WLAN.h" +#include "_network_NET.h" #include "../pikascript-lib/PikaStdDevice/pika_hal.h" #include "PikaStdData_List.h" #include "PikaStdData_Tuple.h" - -#define check_res(res) \ - if (res != 0) { \ - pika_platform_printf("check_res failed: %d, at %s:%d:%s()\r\n", res, \ - __FILE__, __LINE__, __FUNCTION__); \ - } +#include "_network_common.h" void _network_WLAN___init__(PikaObj* self, int interface_id) { pika_dev* hal_wifi = pika_hal_open(PIKA_HAL_WIFI, "WLAN0"); @@ -16,78 +12,19 @@ void _network_WLAN___init__(PikaObj* self, int interface_id) { } pika_hal_WIFI_config cfg = {0}; cfg.mode = interface_id; - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_CONFIG, &cfg)); - obj_setPtr(self, "hal_wifi", hal_wifi); -} - -void _network_WLAN_active(PikaObj* self, int is_active) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { - return; - } - if (is_active) { - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_ENABLE)); - return; - } - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_DISABLE)); - return; -} - -int _network_WLAN_checkActive(PikaObj* self) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { - return -1; - } - int is_active = 0; - check_res( - pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_GET_ACTIVE, &is_active)); - return is_active; -} - -void _network_WLAN_config(PikaObj* self, PikaDict* kwargs) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { - return; - } - pika_hal_WIFI_config cfg = {0}; - if (pikaDict_isArgExist(kwargs, "ssid")) { - strcpy(cfg.ap_ssid, pikaDict_getStr(kwargs, "ssid")); - } - if (pikaDict_isArgExist(kwargs, "password")) { - strcpy(cfg.ap_password, pikaDict_getStr(kwargs, "password")); - } - if (pikaDict_isArgExist(kwargs, "channel")) { - cfg.channel = pikaDict_getInt(kwargs, "channel") + 1; - } - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_CONFIG, &cfg)); -} - -Arg* _network_WLAN_checkConfig(PikaObj* self, char* param) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { - return NULL; - } - pika_hal_WIFI_config* cfg = hal_wifi->ioctl_config; - if (strcmp(param, "ssid") == 0) { - return arg_newStr(cfg->ap_ssid); - } else if (strcmp(param, "password") == 0) { - return arg_newStr(cfg->ap_password); - } else if (strcmp(param, "channel") == 0) { - return arg_newInt(cfg->channel - 1); - } else { - return NULL; - } + _net_check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_CONFIG, &cfg)); + obj_setPtr(self, "hal_dev", hal_wifi); } void _network_WLAN_connect(PikaObj* self, char* ssid, char* key) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); + pika_dev* hal_wifi = _NET_OBJ_2DEV(self); if (hal_wifi == NULL) { return; } pika_hal_WIFI_connect_config conncfg = {0}; strcpy(conncfg.ssid, ssid); strcpy(conncfg.password, key); - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); + _net_check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); return; } @@ -95,7 +32,7 @@ void _network_WLAN_connectWithBssid(PikaObj* self, char* ssid, char* key, char* bssid) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); + pika_dev* hal_wifi = _NET_OBJ_2DEV(self); if (hal_wifi == NULL) { return; } @@ -103,91 +40,32 @@ void _network_WLAN_connectWithBssid(PikaObj* self, strcpy(conncfg.ssid, ssid); strcpy(conncfg.password, key); strcpy(conncfg.bssid, bssid); - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); + _net_check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); } void _network_WLAN_disconnect(PikaObj* self) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); + pika_dev* hal_wifi = _NET_OBJ_2DEV(self); if (hal_wifi == NULL) { return; } - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_DISCONNECT)); -} - -int _network_WLAN_status(PikaObj* self) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { - return -1; - } - PIKA_HAL_WIFI_STATUS status = 0; - check_res( - pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_GET_STATUS, &status)); - return status; + _net_check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_DISCONNECT)); } int _network_WLAN_isconnected(PikaObj* self) { - if (_network_WLAN_status(self) == PIKA_HAL_WIFI_STATUS_GOT_IP) { + if (_network_NET_status(self) == PIKA_HAL_WIFI_STATUS_GOT_IP) { return 1; } else { return 0; } } -int _network_WLAN_statusWithParam(PikaObj* self, char* param) { - return _network_WLAN_status(self); -} - -PikaObj* _network_WLAN_checkIfconfig(PikaObj* self) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { - return NULL; - } - pika_hal_WIFI_ifconfig ifconfig = {0}; - check_res( - pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_GET_IFCONFIG, &ifconfig)); - PikaObj* ifconfig_list = newNormalObj(New_PikaStdData_List); - PikaStdData_List___init__(ifconfig_list); - Arg* arg = arg_newStr(ifconfig.ip); - PikaStdData_List_append(ifconfig_list, arg); - arg_deinit(arg); - arg = arg_newStr(ifconfig.netmask); - PikaStdData_List_append(ifconfig_list, arg); - arg_deinit(arg); - arg = arg_newStr(ifconfig.gateway); - PikaStdData_List_append(ifconfig_list, arg); - arg_deinit(arg); - arg = arg_newStr(ifconfig.dns); - PikaStdData_List_append(ifconfig_list, arg); - arg_deinit(arg); - return ifconfig_list; -} - -void _network_WLAN_ifconfig(PikaObj* self, - char* ip, - char* mask, - char* gateway, - char* dns) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { - return; - } - pika_hal_WIFI_ifconfig ifconfig = {0}; - strcpy(ifconfig.ip, ip); - strcpy(ifconfig.netmask, mask); - strcpy(ifconfig.gateway, gateway); - strcpy(ifconfig.dns, dns); - check_res( - pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_SET_IFCONFIG, &ifconfig)); - return; -} - PikaObj* _network_WLAN_scan(PikaObj* self) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); + pika_dev* hal_wifi = _NET_OBJ_2DEV(self); if (hal_wifi == NULL) { return NULL; } pika_hal_WIFI_scan_result* result = NULL; - check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_SCAN, &result)); + _net_check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_SCAN, &result)); if (NULL == result) { return NULL; } @@ -224,10 +102,38 @@ PikaObj* _network_WLAN_scan(PikaObj* self) { return scan_list; } -void _network_WLAN_close(PikaObj* self) { - pika_dev* hal_wifi = obj_getPtr(self, "hal_wifi"); - if (hal_wifi == NULL) { + +void _network_WLAN_config(PikaObj* self, PikaDict* kwargs) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { return; } - check_res(pika_hal_close(hal_wifi)); + pika_hal_WIFI_config cfg = {0}; + if (pikaDict_isArgExist(kwargs, "ssid")) { + strcpy(cfg.ap_ssid, pikaDict_getStr(kwargs, "ssid")); + } + if (pikaDict_isArgExist(kwargs, "password")) { + strcpy(cfg.ap_password, pikaDict_getStr(kwargs, "password")); + } + if (pikaDict_isArgExist(kwargs, "channel")) { + cfg.channel = pikaDict_getInt(kwargs, "channel") + 1; + } + _net_check_res(pika_hal_ioctl(hal_net, PIKA_HAL_IOCTL_CONFIG, &cfg)); +} + +Arg* _network_WLAN_checkConfig(PikaObj* self, char* param) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { + return NULL; + } + pika_hal_WIFI_config* cfg = hal_net->ioctl_config; + if (strcmp(param, "ssid") == 0) { + return arg_newStr(cfg->ap_ssid); + } else if (strcmp(param, "password") == 0) { + return arg_newStr(cfg->ap_password); + } else if (strcmp(param, "channel") == 0) { + return arg_newInt(cfg->channel - 1); + } else { + return NULL; + } } diff --git a/package/network/network.py b/package/network/network.py index 7bcd644c2..6f13cc007 100644 --- a/package/network/network.py +++ b/package/network/network.py @@ -11,6 +11,40 @@ STAT_CONNECT_FAIL = _network.STAT_CONNECT_FAIL STAT_GOT_IP = _network.STAT_GOT_IP _instence:"WLAN" = None + +class LAN(_network.LAN): + def __init__(self, interface_id: int): + super().__init__(interface_id) + + def active(self, is_active=None): + if is_active is None: + return super().checkActive() + else: + return super().active(is_active) + + def status(self, param=None): + if param is None: + return super().status() + else: + return super().statusWithParam(param) + + def isconnected(self) -> int: + return super().isconnected() + + def config(self, *para, **kwargs): + if len(para) == 1: + return super().checkConfig(para[0]) + else: + return super().config(**kwargs) + + def ifconfig(self, config=None): + if config is None: + t = super().checkIfconfig() + return (t[0], t[1], t[2], t[3]) + else: + return super().ifconfig( + config[0], config[1], config[2], config[3]) + class WLAN(_network.WLAN): def __init__(self, interface_id: int): global _instence diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal.c b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal.c index 003360a39..c8783d072 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/pika_hal.c @@ -335,9 +335,9 @@ int pika_hal_WIFI_ioctl_merge_config(pika_hal_WIFI_config* dst, _IOCTL_CONFIG_USE_DEFAULT(user_data, NULL); return 0; } - + int pika_hal_LAN_ioctl_merge_config(pika_hal_LAN_config* dst, - pika_hal_LAN_config* src) { + pika_hal_LAN_config* src) { _IOCTL_CONFIG_USE_DEFAULT(user_data, NULL); return 0; } @@ -486,7 +486,7 @@ int pika_hal_circularQueue_isFull(pika_hal_CircularQueue* cb) { int pika_hal_circularQueue_peek(pika_hal_CircularQueue* cb, uint8_t* value) { if (cb->count == 0) { - return -1; // 缓冲区为空 + return -1; } #if PIKA_HAL_CIRCULAR_QUEUE_MUTEX_ENABLE diff --git a/port/linux/package/pikascript/pikascript-lib/network/_network_WLAN.c b/port/linux/package/pikascript/pikascript-lib/network/_network_WLAN.c index 019a2388f..9b79c6c86 100644 --- a/port/linux/package/pikascript/pikascript-lib/network/_network_WLAN.c +++ b/port/linux/package/pikascript/pikascript-lib/network/_network_WLAN.c @@ -24,7 +24,8 @@ void _network_WLAN_connect(PikaObj* self, char* ssid, char* key) { pika_hal_WIFI_connect_config conncfg = {0}; strcpy(conncfg.ssid, ssid); strcpy(conncfg.password, key); - _net_check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); + _net_check_res( + pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); return; } @@ -40,7 +41,8 @@ void _network_WLAN_connectWithBssid(PikaObj* self, strcpy(conncfg.ssid, ssid); strcpy(conncfg.password, key); strcpy(conncfg.bssid, bssid); - _net_check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); + _net_check_res( + pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); } void _network_WLAN_disconnect(PikaObj* self) { @@ -102,7 +104,6 @@ PikaObj* _network_WLAN_scan(PikaObj* self) { return scan_list; } - void _network_WLAN_config(PikaObj* self, PikaDict* kwargs) { pika_dev* hal_net = _NET_OBJ_2DEV(self); if (hal_net == NULL) { diff --git a/src/PikaParser.c b/src/PikaParser.c index 12e24b91b..7d951205e 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -612,19 +612,6 @@ char* Lexer_getTokenStream(Args* outBuffs, char* sStmt) { */ if (('>' == c0) || ('<' == c0) || ('*' == c0) || ('/' == c0)) { if (c0 == c1) { - /* >>=, <<=, **=, //= */ - if ('=' == c2) { - char sContent[4] = {0}; - sContent[0] = c0; - sContent[1] = c1; - sContent[2] = '='; - aTokenStream = Lexer_setSymbel(aTokenStream, sStmt, i, - &iSymbolStartIndex); - aTokenStream = Lexer_setToken(aTokenStream, - TOKEN_operator, sContent); - i = i + 2; - continue; - } char content[3] = {0}; content[0] = c0; content[1] = c1; @@ -637,11 +624,11 @@ char* Lexer_getTokenStream(Args* outBuffs, char* sStmt) { } } /* - >=, <=, *=, /=, +=, -=, !=, ==, %=, |=, ^=, &= + >=, <=, *=, /=, +=, -=, !=, ==, %= */ if (('>' == c0) || ('<' == c0) || ('*' == c0) || ('/' == c0) || ('+' == c0) || ('-' == c0) || ('!' == c0) || ('=' == c0) || - ('%' == c0) || ('|' == c0) || ('&' == c0) || ('^' == c0)) { + ('%' == c0) || ('^' == c0)) { if ('=' == c1) { char content[3] = {0}; content[0] = c0; diff --git a/src/PikaPlatform.c b/src/PikaPlatform.c index 14f87c9e6..0fff63e99 100644 --- a/src/PikaPlatform.c +++ b/src/PikaPlatform.c @@ -715,10 +715,7 @@ PIKA_WEAK int pika_platform_thread_mutex_lock(pika_platform_thread_mutex_t* m) { #ifdef __linux return pthread_mutex_lock(&(m->mutex)); #elif PIKA_FREERTOS_ENABLE - if (pdTRUE == xSemaphoreTake(m->mutex, portMAX_DELAY)){ - return 0; - } - return -1; + return xSemaphoreTake(m->mutex, portMAX_DELAY); #elif PIKA_RTTHREAD_ENABLE return rt_mutex_take((m->mutex), RT_WAITING_FOREVER); #else @@ -732,10 +729,7 @@ PIKA_WEAK int pika_platform_thread_mutex_trylock( #ifdef __linux return pthread_mutex_trylock(&(m->mutex)); #elif PIKA_FREERTOS_ENABLE - if (pdTRUE == xSemaphoreTake(m->mutex, 0)){ - return 0; - } - return -1; + return xSemaphoreTake(m->mutex, 0); #elif PIKA_RTTHREAD_ENABLE return rt_mutex_take((m->mutex), 0); #else diff --git a/src/PikaVersion.h b/src/PikaVersion.h index 96e0bb6ea..db1d70a09 100644 --- a/src/PikaVersion.h +++ b/src/PikaVersion.h @@ -2,4 +2,4 @@ #define PIKA_VERSION_MINOR 13 #define PIKA_VERSION_MICRO 1 -#define PIKA_EDIT_TIME "2023/12/26 10:36:08" +#define PIKA_EDIT_TIME "2023/12/27 22:37:14" From 2a90b21d35d51794dc521ea15dfa63ae70537720 Mon Sep 17 00:00:00 2001 From: lyon1998 Date: Thu, 28 Dec 2023 00:14:01 +0800 Subject: [PATCH 4/5] sync LAN support for network --- package/network/_network_LAN.c | 26 +++++ package/network/_network_NET.c | 102 ++++++++++++++++++ package/network/_network_WLAN.c | 7 +- package/network/_network_common.c | 1 + package/network/_network_common.h | 13 +++ .../pikascript-lib/network/_network_LAN.c | 26 +++++ .../pikascript-lib/network/_network_NET.c | 102 ++++++++++++++++++ .../pikascript-lib/network/_network_common.c | 1 + .../pikascript-lib/network/_network_common.h | 13 +++ src/PikaParser.c | 17 ++- src/PikaVersion.h | 2 +- 11 files changed, 304 insertions(+), 6 deletions(-) create mode 100644 package/network/_network_LAN.c create mode 100644 package/network/_network_NET.c create mode 100644 package/network/_network_common.c create mode 100644 package/network/_network_common.h create mode 100644 port/linux/package/pikascript/pikascript-lib/network/_network_LAN.c create mode 100644 port/linux/package/pikascript/pikascript-lib/network/_network_NET.c create mode 100644 port/linux/package/pikascript/pikascript-lib/network/_network_common.c create mode 100644 port/linux/package/pikascript/pikascript-lib/network/_network_common.h diff --git a/package/network/_network_LAN.c b/package/network/_network_LAN.c new file mode 100644 index 000000000..635b7cd22 --- /dev/null +++ b/package/network/_network_LAN.c @@ -0,0 +1,26 @@ +#include "_network_LAN.h" +#include "../pikascript-lib/PikaStdDevice/pika_hal.h" +#include "PikaStdData_List.h" +#include "PikaStdData_Tuple.h" +#include "_network_common.h" + +void _network_LAN___init__(PikaObj* self, int interface_id) { + char LAN_NAME[] = "LAN0"; + LAN_NAME[3] = '0' + interface_id; + pika_debug("LAN_NAME: %s\n", LAN_NAME); + pika_dev* hal_lan = pika_hal_open(PIKA_HAL_LAN, LAN_NAME); + if (hal_lan == NULL) { + return; + } + pika_hal_LAN_config cfg = {0}; + _net_check_res(pika_hal_ioctl(hal_lan, PIKA_HAL_IOCTL_CONFIG, &cfg)); + obj_setPtr(self, "hal_dev", hal_lan); +} + +int _network_LAN_isconnected(PikaObj* self) { + if (_network_NET_status(self) == PIKA_HAL_LAN_STATUS_GOT_IP) { + return 1; + } else { + return 0; + } +} diff --git a/package/network/_network_NET.c b/package/network/_network_NET.c new file mode 100644 index 000000000..d3d2037c7 --- /dev/null +++ b/package/network/_network_NET.c @@ -0,0 +1,102 @@ +#include "_network_NET.h" +#include "../pikascript-lib/PikaStdDevice/pika_hal.h" +#include "PikaStdData_List.h" +#include "PikaStdData_Tuple.h" +#include "_network_common.h" + +void _network_NET___init__(PikaObj* self, int interface_id) { + obj_setErrorCode(self, -__LINE__); + obj_setSysOut(self, "Error: NET class need inhert"); +} + +void _network_NET_active(PikaObj* self, int is_active) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { + return; + } + if (is_active) { + _net_check_res(pika_hal_ioctl(hal_net, PIKA_HAL_IOCTL_ENABLE)); + return; + } + _net_check_res(pika_hal_ioctl(hal_net, PIKA_HAL_IOCTL_DISABLE)); + return; +} + +int _network_NET_checkActive(PikaObj* self) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { + return -1; + } + int is_active = 0; + _net_check_res( + pika_hal_ioctl(hal_net, PIKA_HAL_IOCTL_NET_GET_ACTIVE, &is_active)); + return is_active; +} + +int _network_NET_status(PikaObj* self) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { + return -1; + } + int status = 0; + _net_check_res( + pika_hal_ioctl(hal_net, PIKA_HAL_IOCTL_NET_GET_STATUS, &status)); + return status; +} + +int _network_NET_statusWithParam(PikaObj* self, char* param) { + return _network_NET_status(self); +} + +PikaObj* _network_NET_checkIfconfig(PikaObj* self) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { + return NULL; + } + pika_hal_NET_ifconfig ifconfig = {0}; + _net_check_res( + pika_hal_ioctl(hal_net, PIKA_HAL_IOCTL_NET_GET_IFCONFIG, &ifconfig)); + PikaObj* ifconfig_list = newNormalObj(New_PikaStdData_List); + PikaStdData_List___init__(ifconfig_list); + Arg* arg = arg_newStr(ifconfig.ip); + PikaStdData_List_append(ifconfig_list, arg); + arg_deinit(arg); + arg = arg_newStr(ifconfig.netmask); + PikaStdData_List_append(ifconfig_list, arg); + arg_deinit(arg); + arg = arg_newStr(ifconfig.gateway); + PikaStdData_List_append(ifconfig_list, arg); + arg_deinit(arg); + arg = arg_newStr(ifconfig.dns); + PikaStdData_List_append(ifconfig_list, arg); + arg_deinit(arg); + return ifconfig_list; +} + +void _network_NET_ifconfig(PikaObj* self, + char* ip, + char* mask, + char* gateway, + char* dns) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { + return; + } + pika_hal_NET_ifconfig ifconfig = {0}; + strcpy(ifconfig.ip, ip); + strcpy(ifconfig.netmask, mask); + strcpy(ifconfig.gateway, gateway); + strcpy(ifconfig.dns, dns); + pika_debug("ip:%s, mask:%s, gateway:%s, dns:%s\n", ip, mask, gateway, dns); + _net_check_res( + pika_hal_ioctl(hal_net, PIKA_HAL_IOCTL_NET_SET_IFCONFIG, &ifconfig)); + return; +} + +void _network_NET_close(PikaObj* self) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { + return; + } + _net_check_res(pika_hal_close(hal_net)); +} diff --git a/package/network/_network_WLAN.c b/package/network/_network_WLAN.c index 019a2388f..9b79c6c86 100644 --- a/package/network/_network_WLAN.c +++ b/package/network/_network_WLAN.c @@ -24,7 +24,8 @@ void _network_WLAN_connect(PikaObj* self, char* ssid, char* key) { pika_hal_WIFI_connect_config conncfg = {0}; strcpy(conncfg.ssid, ssid); strcpy(conncfg.password, key); - _net_check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); + _net_check_res( + pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); return; } @@ -40,7 +41,8 @@ void _network_WLAN_connectWithBssid(PikaObj* self, strcpy(conncfg.ssid, ssid); strcpy(conncfg.password, key); strcpy(conncfg.bssid, bssid); - _net_check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); + _net_check_res( + pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_WIFI_CONNECT, &conncfg)); } void _network_WLAN_disconnect(PikaObj* self) { @@ -102,7 +104,6 @@ PikaObj* _network_WLAN_scan(PikaObj* self) { return scan_list; } - void _network_WLAN_config(PikaObj* self, PikaDict* kwargs) { pika_dev* hal_net = _NET_OBJ_2DEV(self); if (hal_net == NULL) { diff --git a/package/network/_network_common.c b/package/network/_network_common.c new file mode 100644 index 000000000..25150d328 --- /dev/null +++ b/package/network/_network_common.c @@ -0,0 +1 @@ +#include "_network_common.h" diff --git a/package/network/_network_common.h b/package/network/_network_common.h new file mode 100644 index 000000000..19e43c3e9 --- /dev/null +++ b/package/network/_network_common.h @@ -0,0 +1,13 @@ +#ifndef _H_NETOWRK_COMMON_H_ +#define _H_NETOWRK_COMMON_H_ +#include "_network_NET.h" + +#define _net_check_res(res) \ + if (res != 0) { \ + pika_platform_printf("_net_check_res failed: %d, at %s:%d:%s()\r\n", \ + res, __FILE__, __LINE__, __FUNCTION__); \ + } + +#define _NET_OBJ_2DEV(_self) obj_getPtr((_self), "hal_dev"); + +#endif diff --git a/port/linux/package/pikascript/pikascript-lib/network/_network_LAN.c b/port/linux/package/pikascript/pikascript-lib/network/_network_LAN.c new file mode 100644 index 000000000..635b7cd22 --- /dev/null +++ b/port/linux/package/pikascript/pikascript-lib/network/_network_LAN.c @@ -0,0 +1,26 @@ +#include "_network_LAN.h" +#include "../pikascript-lib/PikaStdDevice/pika_hal.h" +#include "PikaStdData_List.h" +#include "PikaStdData_Tuple.h" +#include "_network_common.h" + +void _network_LAN___init__(PikaObj* self, int interface_id) { + char LAN_NAME[] = "LAN0"; + LAN_NAME[3] = '0' + interface_id; + pika_debug("LAN_NAME: %s\n", LAN_NAME); + pika_dev* hal_lan = pika_hal_open(PIKA_HAL_LAN, LAN_NAME); + if (hal_lan == NULL) { + return; + } + pika_hal_LAN_config cfg = {0}; + _net_check_res(pika_hal_ioctl(hal_lan, PIKA_HAL_IOCTL_CONFIG, &cfg)); + obj_setPtr(self, "hal_dev", hal_lan); +} + +int _network_LAN_isconnected(PikaObj* self) { + if (_network_NET_status(self) == PIKA_HAL_LAN_STATUS_GOT_IP) { + return 1; + } else { + return 0; + } +} diff --git a/port/linux/package/pikascript/pikascript-lib/network/_network_NET.c b/port/linux/package/pikascript/pikascript-lib/network/_network_NET.c new file mode 100644 index 000000000..d3d2037c7 --- /dev/null +++ b/port/linux/package/pikascript/pikascript-lib/network/_network_NET.c @@ -0,0 +1,102 @@ +#include "_network_NET.h" +#include "../pikascript-lib/PikaStdDevice/pika_hal.h" +#include "PikaStdData_List.h" +#include "PikaStdData_Tuple.h" +#include "_network_common.h" + +void _network_NET___init__(PikaObj* self, int interface_id) { + obj_setErrorCode(self, -__LINE__); + obj_setSysOut(self, "Error: NET class need inhert"); +} + +void _network_NET_active(PikaObj* self, int is_active) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { + return; + } + if (is_active) { + _net_check_res(pika_hal_ioctl(hal_net, PIKA_HAL_IOCTL_ENABLE)); + return; + } + _net_check_res(pika_hal_ioctl(hal_net, PIKA_HAL_IOCTL_DISABLE)); + return; +} + +int _network_NET_checkActive(PikaObj* self) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { + return -1; + } + int is_active = 0; + _net_check_res( + pika_hal_ioctl(hal_net, PIKA_HAL_IOCTL_NET_GET_ACTIVE, &is_active)); + return is_active; +} + +int _network_NET_status(PikaObj* self) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { + return -1; + } + int status = 0; + _net_check_res( + pika_hal_ioctl(hal_net, PIKA_HAL_IOCTL_NET_GET_STATUS, &status)); + return status; +} + +int _network_NET_statusWithParam(PikaObj* self, char* param) { + return _network_NET_status(self); +} + +PikaObj* _network_NET_checkIfconfig(PikaObj* self) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { + return NULL; + } + pika_hal_NET_ifconfig ifconfig = {0}; + _net_check_res( + pika_hal_ioctl(hal_net, PIKA_HAL_IOCTL_NET_GET_IFCONFIG, &ifconfig)); + PikaObj* ifconfig_list = newNormalObj(New_PikaStdData_List); + PikaStdData_List___init__(ifconfig_list); + Arg* arg = arg_newStr(ifconfig.ip); + PikaStdData_List_append(ifconfig_list, arg); + arg_deinit(arg); + arg = arg_newStr(ifconfig.netmask); + PikaStdData_List_append(ifconfig_list, arg); + arg_deinit(arg); + arg = arg_newStr(ifconfig.gateway); + PikaStdData_List_append(ifconfig_list, arg); + arg_deinit(arg); + arg = arg_newStr(ifconfig.dns); + PikaStdData_List_append(ifconfig_list, arg); + arg_deinit(arg); + return ifconfig_list; +} + +void _network_NET_ifconfig(PikaObj* self, + char* ip, + char* mask, + char* gateway, + char* dns) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { + return; + } + pika_hal_NET_ifconfig ifconfig = {0}; + strcpy(ifconfig.ip, ip); + strcpy(ifconfig.netmask, mask); + strcpy(ifconfig.gateway, gateway); + strcpy(ifconfig.dns, dns); + pika_debug("ip:%s, mask:%s, gateway:%s, dns:%s\n", ip, mask, gateway, dns); + _net_check_res( + pika_hal_ioctl(hal_net, PIKA_HAL_IOCTL_NET_SET_IFCONFIG, &ifconfig)); + return; +} + +void _network_NET_close(PikaObj* self) { + pika_dev* hal_net = _NET_OBJ_2DEV(self); + if (hal_net == NULL) { + return; + } + _net_check_res(pika_hal_close(hal_net)); +} diff --git a/port/linux/package/pikascript/pikascript-lib/network/_network_common.c b/port/linux/package/pikascript/pikascript-lib/network/_network_common.c new file mode 100644 index 000000000..25150d328 --- /dev/null +++ b/port/linux/package/pikascript/pikascript-lib/network/_network_common.c @@ -0,0 +1 @@ +#include "_network_common.h" diff --git a/port/linux/package/pikascript/pikascript-lib/network/_network_common.h b/port/linux/package/pikascript/pikascript-lib/network/_network_common.h new file mode 100644 index 000000000..19e43c3e9 --- /dev/null +++ b/port/linux/package/pikascript/pikascript-lib/network/_network_common.h @@ -0,0 +1,13 @@ +#ifndef _H_NETOWRK_COMMON_H_ +#define _H_NETOWRK_COMMON_H_ +#include "_network_NET.h" + +#define _net_check_res(res) \ + if (res != 0) { \ + pika_platform_printf("_net_check_res failed: %d, at %s:%d:%s()\r\n", \ + res, __FILE__, __LINE__, __FUNCTION__); \ + } + +#define _NET_OBJ_2DEV(_self) obj_getPtr((_self), "hal_dev"); + +#endif diff --git a/src/PikaParser.c b/src/PikaParser.c index 7d951205e..12e24b91b 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -612,6 +612,19 @@ char* Lexer_getTokenStream(Args* outBuffs, char* sStmt) { */ if (('>' == c0) || ('<' == c0) || ('*' == c0) || ('/' == c0)) { if (c0 == c1) { + /* >>=, <<=, **=, //= */ + if ('=' == c2) { + char sContent[4] = {0}; + sContent[0] = c0; + sContent[1] = c1; + sContent[2] = '='; + aTokenStream = Lexer_setSymbel(aTokenStream, sStmt, i, + &iSymbolStartIndex); + aTokenStream = Lexer_setToken(aTokenStream, + TOKEN_operator, sContent); + i = i + 2; + continue; + } char content[3] = {0}; content[0] = c0; content[1] = c1; @@ -624,11 +637,11 @@ char* Lexer_getTokenStream(Args* outBuffs, char* sStmt) { } } /* - >=, <=, *=, /=, +=, -=, !=, ==, %= + >=, <=, *=, /=, +=, -=, !=, ==, %=, |=, ^=, &= */ if (('>' == c0) || ('<' == c0) || ('*' == c0) || ('/' == c0) || ('+' == c0) || ('-' == c0) || ('!' == c0) || ('=' == c0) || - ('%' == c0) || ('^' == c0)) { + ('%' == c0) || ('|' == c0) || ('&' == c0) || ('^' == c0)) { if ('=' == c1) { char content[3] = {0}; content[0] = c0; diff --git a/src/PikaVersion.h b/src/PikaVersion.h index db1d70a09..17b699372 100644 --- a/src/PikaVersion.h +++ b/src/PikaVersion.h @@ -2,4 +2,4 @@ #define PIKA_VERSION_MINOR 13 #define PIKA_VERSION_MICRO 1 -#define PIKA_EDIT_TIME "2023/12/27 22:37:14" +#define PIKA_EDIT_TIME "2023/12/28 00:13:54" From edf3f0d2261e953a63780c919372145620d9f78e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=82?= Date: Wed, 27 Dec 2023 16:17:27 +0000 Subject: [PATCH 5/5] fix patch tool for utf-8 encode --- tools/pika-patch-tool.py | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/tools/pika-patch-tool.py b/tools/pika-patch-tool.py index 4592db9e3..1bddb1dbe 100644 --- a/tools/pika-patch-tool.py +++ b/tools/pika-patch-tool.py @@ -42,14 +42,17 @@ for root, apply_dirs, files in os.walk('.'): src_file = os.path.join(root, file) rel_path = os.path.relpath(root, '.') target_file = os.path.join(base_path, rel_path, file) - print(f"Processing file: {src_file}") + print(f"Processing file: {src_file}") if os.path.exists(target_file): - diff_command = ["git", "diff", "--no-index", target_file, src_file] + diff_command = ["git", "diff", + "--no-index", target_file, src_file] result = subprocess.run(diff_command, stdout=subprocess.PIPE) - if result.stdout: - diff_output = result.stdout.decode() - print(diff_output) - with open("/tmp/base/changes.patch", "a") as patch_file: + if result.stdout: + # 确保使用UTF-8编码解码stdout + diff_output = result.stdout.decode('utf-8') + print(diff_output) + # 使用UTF-8编码打开文件进行写入 + with open("/tmp/base/changes.patch", "a", encoding='utf-8') as patch_file: patch_file.write(diff_output) else: print(f"No base file, skipped: {target_file}") @@ -61,7 +64,7 @@ if not os.path.exists("/tmp/base/changes.patch"): exit() # read the patch -with open("/tmp/base/changes.patch") as patch_file: +with open("/tmp/base/changes.patch", encoding='utf-8') as patch_file: patch_data = patch_file.read() # split the patch into a list of patches @@ -116,7 +119,7 @@ for patch in patches: for dir in search_dirs: file_name = os.path.basename(from_file) # find file - #if is_found: + # if is_found: # break for root, apply_dirs, files in os.walk(dir): @@ -133,11 +136,12 @@ for patch in patches: print(f"Applying patch: {patch}") # Strip the trailing whitespace for each line lines = [line for line in patch.split('\n')] - with open("/tmp/base/temp.patch", 'w') as f: + with open("/tmp/base/temp.patch", 'w', encoding='utf-8') as f: for line in lines: f.write(line + '\n') apply_patch_command = ["git", "apply", "/tmp/base/temp.patch"] - result = subprocess.run(apply_patch_command, stdout=subprocess.PIPE, cwd=main_repo_path) + result = subprocess.run( + apply_patch_command, stdout=subprocess.PIPE, cwd=main_repo_path) # check the result if result.returncode != 0: fail_count += 1 @@ -146,7 +150,8 @@ for patch in patches: else: success_count += 1 success_list.append(apply_file) - print("\033[92mSuccessfully applied patch\033[0m", apply_file) + print( + "\033[92mSuccessfully applied patch\033[0m", apply_file) break print("\n\n===========================================\n\n") @@ -162,12 +167,15 @@ if fail_count > 0: print(file) # setup remote url -repo_url = input("Please enter your remote repository URL (default: '{}'): ".format(default_repo_url)) or default_repo_url -subprocess.run(["git", "remote", "set-url", "origin", repo_url], cwd = main_repo_path) +repo_url = input("Please enter your remote repository URL (default: '{}'): ".format( + default_repo_url)) or default_repo_url +subprocess.run(["git", "remote", "set-url", "origin", + repo_url], cwd=main_repo_path) # setup commit message -commit_msg = input("Please enter your commit message (default: 'Apply patches'): ") or "Apply patches" -subprocess.run(["git", "commit", "-a" ,"-m", commit_msg], cwd=main_repo_path) +commit_msg = input( + "Please enter your commit message (default: 'Apply patches'): ") or "Apply patches" +subprocess.run(["git", "commit", "-a", "-m", commit_msg], cwd=main_repo_path) # confirm push confirm = input("Are you sure you want to push to the repository ([Y]/N)? ")