fix network active(False)

This commit is contained in:
lyon 2023-08-07 21:22:59 +08:00
parent e4e458dee6
commit dfea579684
4 changed files with 27 additions and 16 deletions

View File

@ -1,5 +1,5 @@
#include <stdint.h>
#include "PikaObj.h"
#include "BaseObj.h"
#include "dataStrs.h"
#include "driver/gpio.h"
#include "pika_hal_ESP32_common.h"
@ -12,7 +12,6 @@ int pika_hal_platform_GPIO_open(pika_dev* dev, char* name) {
}
memset(gpio, 0, sizeof(platform_data_GPIO));
gpio->gpioPort = getGpioPin(name);
pika_debug("gpio open port: %s -> %d", name, gpio->gpioPort);
if (-1 == gpio->gpioPort) {
pikaFree(gpio, sizeof(platform_data_GPIO));
return -1;
@ -46,7 +45,6 @@ int pika_hal_platform_GPIO_write(pika_dev* dev, void* buf, size_t count) {
return -1;
}
gpio_set_level(gpio->gpioPort, *((uint32_t*)buf));
// pika_debug("gpio write %d -> %d", gpio->gpioPort, *((uint32_t*)buf));
return 0;
}
@ -55,12 +53,6 @@ int pika_hal_platform_GPIO_ioctl_enable(pika_dev* dev) {
if (NULL == gpio) {
return -1;
}
pika_debug("gpio enable: %d", gpio->gpioPort);
pika_debug(" - mode: %d", gpio->io_conf.mode);
pika_debug(" - pull_up_en: %d", gpio->io_conf.pull_up_en);
pika_debug(" - pull_down_en: %d", gpio->io_conf.pull_down_en);
pika_debug(" - intr_type: %d", gpio->io_conf.intr_type);
pika_debug(" - pin_bit_mask: %lld", gpio->io_conf.pin_bit_mask);
gpio_config(&gpio->io_conf);
return 0;
}
@ -93,7 +85,7 @@ int pika_hal_platform_GPIO_ioctl_config(pika_dev* dev,
default:
return -1;
}
gpio->io_conf.pin_bit_mask = 1ULL << gpio->gpioPort;
gpio->io_conf.pin_bit_mask = 1 << gpio->gpioPort;
switch (cfg->pull) {
case PIKA_HAL_GPIO_PULL_UP:
gpio->io_conf.pull_up_en = 1;

View File

@ -16,6 +16,7 @@ static volatile PIKA_HAL_WIFI_STATUS wifi_sta_disconn_reason =
static EventGroupHandle_t wifi_event_group;
static esp_netif_t* sta_netif = NULL;
static esp_netif_t* ap_netif = NULL;
static volatile pika_bool wifi_inited = PIKA_FALSE;
uint32_t _ip_str2u32(char* ip_str) {
uint32_t ip = 0;
@ -73,7 +74,7 @@ static void event_handler(void* event_handler_arg,
}
}
int pika_hal_platform_WIFI_open(pika_dev* dev, char* name) {
int _wifi_init(void){
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
@ -88,6 +89,15 @@ int pika_hal_platform_WIFI_open(pika_dev* dev, char* name) {
return 0;
}
int pika_hal_platform_WIFI_open(pika_dev* dev, char* name) {
// Initialize NVS
if (pika_false == wifi_inited){
_wifi_init();
wifi_inited = pika_true;
}
return 0;
}
int pika_hal_platform_WIFI_close(pika_dev* dev) {
return 0;
}
@ -139,8 +149,12 @@ int pika_hal_platform_WIFI_ioctl_enable(pika_dev* dev) {
IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, dev));
pika_debug("set mode: %d", mode);
esp_wifi_set_mode(mode);
sta_netif = esp_netif_create_default_wifi_sta();
ap_netif = esp_netif_create_default_wifi_ap();
if (NULL == sta_netif){
sta_netif = esp_netif_create_default_wifi_sta();
}
if (NULL == ap_netif){
ap_netif = esp_netif_create_default_wifi_ap();
}
pika_debug("start wifi");
ESP_ERROR_CHECK(esp_wifi_start());
wifi_started = PIKA_TRUE;
@ -150,6 +164,7 @@ int pika_hal_platform_WIFI_ioctl_enable(pika_dev* dev) {
int pika_hal_platform_WIFI_ioctl_disable(pika_dev* dev) {
if (wifi_started) {
esp_wifi_disconnect();
esp_wifi_stop();
esp_wifi_deinit();
wifi_started = PIKA_FALSE;

View File

@ -1,5 +1,5 @@
#ifndef __ESP32__COMMON__H
#define __ESP32__COMMON__H
#ifndef __STM32__COMMON__H
#define __STM32__COMMON__H
#include <stdint.h>
#include "PikaObj.h"

View File

@ -25,7 +25,11 @@ void _network_WLAN_active(PikaObj* self, int is_active) {
if (hal_wifi == NULL) {
return;
}
check_res(pika_hal_ioctl(hal_wifi, PIKA_HAL_IOCTL_ENABLE));
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;
}