diff options
| author | xuzhuoyi <[email protected]> | 2018-11-16 20:09:46 +0800 |
|---|---|---|
| committer | xuzhuoyi <[email protected]> | 2018-11-16 20:09:46 +0800 |
| commit | 37db67d94a269e1a9893d076ab2bb6b9883cfa8c (patch) | |
| tree | faaea1c497b8b50e638f768ed6fcc4ec83e6a90f /components/drivers | |
| parent | 305307c90c8723dcdb79663f65a8933b5cb1b934 (diff) | |
| parent | 63a6f6c4d2540b003296594c4b916cc1ba8d0b16 (diff) | |
Merge remote-tracking branch 'upstream/master'
# Conflicts:
# src/thread.c
Diffstat (limited to 'components/drivers')
148 files changed, 8946 insertions, 2872 deletions
diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index f27c70c481..f3b814fa08 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -90,6 +90,24 @@ config RT_USING_MTD_NAND default n endif +config RT_USING_MTD + bool "Using Memory Technology Device (MTD)" + default n + + if RT_USING_MTD + config MTD_USING_NOR + bool "Using MTD Nor Flash device" + default n + + config MTD_USING_NAND + bool "Using MTD Nand Flash device" + default n + endif + +config RT_USING_PM + bool "Using Power Management device drivers" + default n + config RT_USING_RTC bool "Using RTC device drivers" default n @@ -100,9 +118,8 @@ config RT_USING_RTC default n config RTC_SYNC_USING_NTP bool "Using NTP auto sync RTC time" - select PKG_USING_NETUTILS - select PKG_NETUTILS_NTP - default n + depends on PKG_NETUTILS_NTP + default y if RTC_SYNC_USING_NTP config RTC_NTP_FIRST_SYNC_DELAY @@ -138,6 +155,9 @@ config RT_USING_SDIO config RT_MMCSD_MAX_PARTITION int "mmcsd max partition" default 16 + config RT_SDIO_DEBUG + bool "Enable SDIO debug log output" + default n endif config RT_USING_SPI @@ -190,31 +210,103 @@ config RT_USING_WDT bool "Using Watch Dog device drivers" default n -config RT_USING_WIFI - bool "Using Wi-Fi network" +config RT_USING_AUDIO + bool "Using Audio device drivers" + default n + +menu "Using WiFi" + config RT_USING_WIFI + bool "Using Wi-Fi framework" default n if RT_USING_WIFI - config RT_USING_WLAN_STA - bool "Using station mode" + config RT_WLAN_DEVICE_STA_NAME + string "The WiFi device name for station" + default "wlan0" + + config RT_WLAN_DEVICE_AP_NAME + string "The WiFi device name for ap" + default "wlan1" + + config RT_WLAN_DEFAULT_PROT + string "Default transport protocol" + default "lwip" + + config RT_WLAN_SCAN_WAIT_MS + int "Set scan timeout time(ms)" + default 10000 + + config RT_WLAN_CONNECT_WAIT_MS + int "Set connect timeout time(ms)" + default 10000 + + config RT_WLAN_SSID_MAX_LENGTH + int "SSID name maximum length" + default 32 + + config RT_WLAN_PASSWORD_MAX_LENGTH + int "Maximum password length" + default 32 + + config RT_WLAN_SCAN_SORT + bool "Automatic sorting of scan results" default y - config RT_USING_WLAN_AP - bool "Using ap mode" + config RT_WLAN_CFG_INFO_MAX + int "Maximum number of WiFi information automatically saved" + default 3 + + config RT_WLAN_WORKQUEUE_THREAD_NAME + string "WiFi work queue thread name" + default "wlan_job" + + config RT_WLAN_WORKQUEUE_THREAD_SIZE + int "wifi work queue thread size" + default 2048 + + config RT_WLAN_WORKQUEUE_THREAD_PRIO + int "WiFi work queue thread priority" + default 22 + + config RT_WLAN_DEV_EVENT_NUM + int "Maximum number of driver events" + default 2 + + config RT_WLAN_PROT_LWIP_PBUF_FORCE + bool "Forced use of PBUF transmission" default n - config WIFI_DEVICE_STA_NAME - string "the wifi device name for station" - default "w0" + menuconfig RT_WLAN_DEBUG + bool "Enable WLAN Debugging Options" + default n - config WIFI_DEVICE_AP_NAME - string "the wifi device name for ap" - default "ap" - endif + if RT_WLAN_DEBUG + config RT_WLAN_CMD_DEBUG + bool "Enable Debugging of wlan_cmd.c" + default n -config RT_USING_AUDIO - bool "Using Audio device drivers" - default n + config RT_WLAN_MGNT_DEBUG + bool "Enable Debugging of wlan_mgnt.c" + default n + + config RT_WLAN_DEV_DEBUG + bool "Enable Debugging of wlan_dev.c" + default n + + config RT_WLAN_PROT_DEBUG + bool "Enable Debugging of wlan_prot.c" + default n + + config RT_WLAN_CFG_DEBUG + bool "Enable Debugging of wlan_cfg.c" + default n + + config RT_WLAN_LWIP_DEBUG + bool "Enable Debugging of wlan_lwip.c" + default n + endif + endif +endmenu menu "Using USB" config RT_USING_USB_HOST @@ -303,6 +395,23 @@ menu "Using USB" bool "Enable to use device as winusb device" default n endif + if RT_USB_DEVICE_CDC + config RT_VCOM_TASK_STK_SIZE + int "virtual com thread stack size" + default 512 + config RT_VCOM_TX_USE_DMA + bool "Enable to use dma for vcom tx" + default n + config RT_VCOM_SERNO + string "serial number of virtual com" + default "32021919830108" + config RT_VCOM_SER_LEN + int "serial number length of virtual com" + default 14 + config RT_VCOM_TX_TIMEOUT + int "tx timeout(ticks) of virtual com" + default 1000 + endif if RT_USB_DEVICE_WINUSB config RT_WINUSB_GUID string "Guid for winusb" diff --git a/components/drivers/audio/audio.c b/components/drivers/audio/audio.c index 220a000df1..14dddb38ea 100644 --- a/components/drivers/audio/audio.c +++ b/components/drivers/audio/audio.c @@ -1,21 +1,7 @@ /* - * File : audio.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/audio/audio_pipe.c b/components/drivers/audio/audio_pipe.c index 9dc296831e..242305e204 100644 --- a/components/drivers/audio/audio_pipe.c +++ b/components/drivers/audio/audio_pipe.c @@ -1,21 +1,7 @@ /* - * File : pipe.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/audio/audio_pipe.h b/components/drivers/audio/audio_pipe.h index d40d4234b6..c34f72b019 100644 --- a/components/drivers/audio/audio_pipe.h +++ b/components/drivers/audio/audio_pipe.h @@ -1,3 +1,11 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ #ifndef __AUDIO_PIPE_H__ #define __AUDIO_PIPE_H__ diff --git a/components/drivers/can/can.c b/components/drivers/can/can.c index 98d76cd5b3..e20fcddcff 100644 --- a/components/drivers/can/can.c +++ b/components/drivers/can/can.c @@ -1,21 +1,7 @@ /* - * File : can.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2015, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/cputime/cputime.c b/components/drivers/cputime/cputime.c index 20f41b6dac..3ec860d5c5 100644 --- a/components/drivers/cputime/cputime.c +++ b/components/drivers/cputime/cputime.c @@ -1,21 +1,7 @@ /* - * File : cputime.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2017, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -68,7 +54,7 @@ uint32_t clock_cpu_microsecond(uint32_t cpu_tick) { float unit = clock_cpu_getres(); - return (cpu_tick * unit) / 1000; + return (uint32_t)((cpu_tick * unit) / 1000); } /** @@ -83,7 +69,7 @@ uint32_t clock_cpu_millisecond(uint32_t cpu_tick) { float unit = clock_cpu_getres(); - return (cpu_tick * unit) / (1000 * 1000); + return (uint32_t)((cpu_tick * unit) / (1000 * 1000)); } /** diff --git a/components/drivers/cputime/cputime_cortexm.c b/components/drivers/cputime/cputime_cortexm.c index 99dc8b013e..05ed6ed447 100644 --- a/components/drivers/cputime/cputime_cortexm.c +++ b/components/drivers/cputime/cputime_cortexm.c @@ -1,21 +1,7 @@ /* - * File : cputime_cortexm.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2017, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/hwtimer/hwtimer.c b/components/drivers/hwtimer/hwtimer.c index c3e5076343..cd85b3baef 100644 --- a/components/drivers/hwtimer/hwtimer.c +++ b/components/drivers/hwtimer/hwtimer.c @@ -1,21 +1,7 @@ /* - * File : hwtimer.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -30,7 +16,7 @@ rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv) float overflow; float timeout; rt_uint32_t counter; - int i, index; + int i, index = 0; float tv_sec; float devi_min = 1; float devi; diff --git a/components/drivers/i2c/fm24clxx.c b/components/drivers/i2c/fm24clxx.c index bb77d707bf..eecf3d455a 100644 --- a/components/drivers/i2c/fm24clxx.c +++ b/components/drivers/i2c/fm24clxx.c @@ -1,21 +1,7 @@ /* - * File : fm24clxx.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/i2c/fm24clxx.h b/components/drivers/i2c/fm24clxx.h index fa84dd5e29..74ee065470 100644 --- a/components/drivers/i2c/fm24clxx.h +++ b/components/drivers/i2c/fm24clxx.h @@ -1,21 +1,7 @@ /* - * File : fm24clxx.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/i2c/i2c-bit-ops.c b/components/drivers/i2c/i2c-bit-ops.c index d4a26ea72a..26479a1d68 100644 --- a/components/drivers/i2c/i2c-bit-ops.c +++ b/components/drivers/i2c/i2c-bit-ops.c @@ -1,21 +1,7 @@ /* - * File : i2c-bit-ops.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/i2c/i2c_core.c b/components/drivers/i2c/i2c_core.c index 67778ce0d3..f245abb9ca 100644 --- a/components/drivers/i2c/i2c_core.c +++ b/components/drivers/i2c/i2c_core.c @@ -1,21 +1,7 @@ /* - * File : i2c_core.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/i2c/i2c_dev.c b/components/drivers/i2c/i2c_dev.c index 042b7b743a..6c7df8e427 100644 --- a/components/drivers/i2c/i2c_dev.c +++ b/components/drivers/i2c/i2c_dev.c @@ -1,21 +1,7 @@ /* - * File : i2c_dev.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/alarm.h b/components/drivers/include/drivers/alarm.h index 99dd8ef9b8..b5ebbc86d9 100644 --- a/components/drivers/include/drivers/alarm.h +++ b/components/drivers/include/drivers/alarm.h @@ -1,21 +1,7 @@ /* - * File : alarm.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/audio.h b/components/drivers/include/drivers/audio.h index 04821c2bc8..7d2cb36728 100644 --- a/components/drivers/include/drivers/audio.h +++ b/components/drivers/include/drivers/audio.h @@ -1,21 +1,7 @@ /* - * File : audio.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -94,7 +80,7 @@ /* Support Dsp(input/output) Units controls */ #define AUDIO_DSP_PARAM 0 /* get/set all params */ -#define AUDIO_DSP_SAMPLERATE 1 /* ����Ƶ�� */ +#define AUDIO_DSP_SAMPLERATE 1 /* 采样频率 */ #define AUDIO_DSP_FMT 2 #define AUDIO_DSP_CHANNELS 3 diff --git a/components/drivers/include/drivers/can.h b/components/drivers/include/drivers/can.h index ecf3b5f51e..c61fb19eaa 100644 --- a/components/drivers/include/drivers/can.h +++ b/components/drivers/include/drivers/can.h @@ -1,21 +1,7 @@ /* - * File : can.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2015, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/cputime.h b/components/drivers/include/drivers/cputime.h index c3dd243f11..78c22fc1da 100644 --- a/components/drivers/include/drivers/cputime.h +++ b/components/drivers/include/drivers/cputime.h @@ -1,21 +1,7 @@ /* - * File : cputime.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2017, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/hwtimer.h b/components/drivers/include/drivers/hwtimer.h index 24cec74d32..cbfeec2bf8 100644 --- a/components/drivers/include/drivers/hwtimer.h +++ b/components/drivers/include/drivers/hwtimer.h @@ -1,3 +1,11 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ #ifndef __HWTIMER_H__ #define __HWTIMER_H__ diff --git a/components/drivers/include/drivers/i2c-bit-ops.h b/components/drivers/include/drivers/i2c-bit-ops.h index 6360b99a92..ddf9a36d73 100644 --- a/components/drivers/include/drivers/i2c-bit-ops.h +++ b/components/drivers/include/drivers/i2c-bit-ops.h @@ -1,21 +1,7 @@ /* - * File : i2c-bit-ops.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/i2c.h b/components/drivers/include/drivers/i2c.h index b6f5273722..e2a9e7d0b8 100644 --- a/components/drivers/include/drivers/i2c.h +++ b/components/drivers/include/drivers/i2c.h @@ -1,21 +1,7 @@ /* - * File : i2c.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/i2c_dev.h b/components/drivers/include/drivers/i2c_dev.h index 81e9896bcf..5ef9dd1a8c 100644 --- a/components/drivers/include/drivers/i2c_dev.h +++ b/components/drivers/include/drivers/i2c_dev.h @@ -1,21 +1,7 @@ /* - * File : i2c_dev.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/mmc.h b/components/drivers/include/drivers/mmc.h index 6800c2a29e..95dce7178a 100644 --- a/components/drivers/include/drivers/mmc.h +++ b/components/drivers/include/drivers/mmc.h @@ -1,21 +1,7 @@ /* - * File : mmc.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/mmcsd_card.h b/components/drivers/include/drivers/mmcsd_card.h index 18470a5ea9..3ee5a20465 100644 --- a/components/drivers/include/drivers/mmcsd_card.h +++ b/components/drivers/include/drivers/mmcsd_card.h @@ -1,21 +1,7 @@ /* - * File : mmcsd_card.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/mmcsd_cmd.h b/components/drivers/include/drivers/mmcsd_cmd.h index 2700771531..7c2f26e892 100644 --- a/components/drivers/include/drivers/mmcsd_cmd.h +++ b/components/drivers/include/drivers/mmcsd_cmd.h @@ -1,21 +1,7 @@ /* - * File : mmcsd_cmd.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/mmcsd_core.h b/components/drivers/include/drivers/mmcsd_core.h index 2be758bf82..05a5af6d79 100644 --- a/components/drivers/include/drivers/mmcsd_core.h +++ b/components/drivers/include/drivers/mmcsd_core.h @@ -1,21 +1,7 @@ /* - * File : mmcsd_core.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/mmcsd_host.h b/components/drivers/include/drivers/mmcsd_host.h index 877a04d8e0..1253062aef 100644 --- a/components/drivers/include/drivers/mmcsd_host.h +++ b/components/drivers/include/drivers/mmcsd_host.h @@ -1,21 +1,7 @@ /* - * File : mmcsd_host.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/mtd.h b/components/drivers/include/drivers/mtd.h new file mode 100644 index 0000000000..fb7ea4389d --- /dev/null +++ b/components/drivers/include/drivers/mtd.h @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + 2018-09-10 heyuanjie87 first version + + */ + +#ifndef __MTD_H__ +#define __MTD_H__ + +#include <rtthread.h> +#include <stddef.h> +#include <stdint.h> + +#define MTD_TYPE_NOR 1 +#define MTD_TYPE_NAND 2 + + /** + * MTD operation modes + * + * @MTD_OPM_PLACE_OOB: OOB data are placed at the given offset (default) + * @MTD_OPM_AUTO_OOB: OOB data are automatically placed at the free areas + * @MTD_OPM_RAW: data are transferred as-is, with no error correction; + */ +enum mtd_opm +{ + MTD_OPM_PLACE_OOB = 0, + MTD_OPM_AUTO_OOB = 1, + MTD_OPM_RAW = 2, +}; + +#ifndef loff_t +typedef long loff_t; +#endif + +struct mtd_oob_region +{ + uint8_t offset; + uint8_t length; +}; + +typedef struct mtd_info +{ + struct rt_device parent; + + const struct mtd_ops *ops; + + uint16_t oob_size; + uint16_t sector_size; /* Minimal writable flash unit size */ + uint32_t block_size:28; /* Erase size for the device */ + uint32_t type:4; + + size_t size; /* Total size of the MTD */ + loff_t offset; /* At which this MTD starts, from the beginning of the MEMORY */ + struct mtd_info *master; + + void *priv; +}rt_mtd_t; + +struct mtd_io_desc +{ + uint8_t mode; /* operation mode(enum mtd_opm) */ + uint8_t ooblen; /* number of oob bytes to write/read */ + uint8_t oobretlen; /* number of oob bytes written/read */ + uint8_t ooboffs; /* offset in the oob area */ + uint8_t *oobbuf; + + size_t datlen; /* number of data bytes to write/read */ + size_t datretlen; /* number of data bytes written/read */ + uint8_t *datbuf; /* if NULL only oob are read/written */ +}; + +struct mtd_ops +{ + int(*erase)(rt_mtd_t *mtd, loff_t addr, size_t len); /* return 0 if success */ + int(*read) (rt_mtd_t *mtd, loff_t from, struct mtd_io_desc *ops); /* return 0 if success */ + int(*write) (rt_mtd_t *mtd, loff_t to, struct mtd_io_desc *ops); /* return 0 if success */ + int(*isbad) (rt_mtd_t *mtd, uint32_t block); /* return 1 if bad, 0 not bad */ + int(*markbad) (rt_mtd_t *mtd, uint32_t block); /* return 0 if success */ +}; + +struct mtd_part +{ + const char *name; /* name of the MTD partion */ + loff_t offset; /* start addr of partion */ + size_t size; /* size of partion */ +}; + +int rt_mtd_erase(rt_mtd_t *mtd, loff_t addr, size_t size); +int rt_mtd_block_erase(rt_mtd_t *mtd, uint32_t block); +int rt_mtd_read_oob(rt_mtd_t *mtd, loff_t from, struct mtd_io_desc *desc); +int rt_mtd_write_oob(rt_mtd_t *mtd, loff_t to, struct mtd_io_desc *desc); +int rt_mtd_read(rt_mtd_t *mtd, loff_t from, uint8_t *buf, size_t len); +int rt_mtd_write(rt_mtd_t *mtd, loff_t to, const uint8_t *buf, size_t len); +int rt_mtd_block_markbad(rt_mtd_t *mtd, uint32_t block); +int rt_mtd_block_isbad(rt_mtd_t *mtd, uint32_t block); + +rt_mtd_t* rt_mtd_get(const char *name); +int rt_mtd_register(rt_mtd_t *master, const struct mtd_part *parts, int np); + +#endif diff --git a/components/drivers/include/drivers/mtd_nand.h b/components/drivers/include/drivers/mtd_nand.h index fb14eb0dcc..8afed000dd 100644 --- a/components/drivers/include/drivers/mtd_nand.h +++ b/components/drivers/include/drivers/mtd_nand.h @@ -1,21 +1,7 @@ /* - * File : mtd_nand.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/mtd_nor.h b/components/drivers/include/drivers/mtd_nor.h index c51ce87f41..32a952f374 100644 --- a/components/drivers/include/drivers/mtd_nor.h +++ b/components/drivers/include/drivers/mtd_nor.h @@ -1,21 +1,7 @@ /* - * File : mtd_nor.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, Shanghai Real-Thread Technology Co., Ltd - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * COPYRIGHT (C) 2018, Real-Thread Information Technology Ltd + * + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/mtdnand.h b/components/drivers/include/drivers/mtdnand.h new file mode 100644 index 0000000000..2e836cb2fb --- /dev/null +++ b/components/drivers/include/drivers/mtdnand.h @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + 2018-09-10 heyuanjie87 first version + + */ + +#ifndef _MTDNAND_H_ +#define _MTDNAND_H_ + +#include "mtd.h" + + /* Status bits */ +#define NAND_STATUS_FAIL 0x01 +#define NAND_STATUS_FAIL_N1 0x02 +#define NAND_STATUS_WP 0x80 + +typedef enum +{ + NAND_CMD_PAGE_RD, /* read data to chip's page buffer,do WaitBusy after this cmd in low driver */ + NAND_CMD_PAGE_WR0, /* write data to chip's page buffer */ + NAND_CMD_PAGE_WR1, /* do flash programe */ + NAND_CMD_BLK_ERASE, /* erase block */ + NAND_CMD_ECC_EN, /* enable gen HWECC */ + NAND_CMD_ECC_DIS /* disable gen HWECC */ +} nand_cmd_t; + +typedef enum +{ + NAND_ECCM_NONE, + NAND_ECCM_HW, +} nand_eccmode_t; + +struct nand_chip; +struct nand_ops; + +/** +* struct nand_buffers - buffer structure for read/write +* @ecccalc: buffer pointer for calculated ECC, size is oobsize. +* @ecccode: buffer pointer for ECC read from flash, size is oobsize. +* +*/ +struct nand_buffers +{ + uint8_t *ecccalc; + uint8_t *ecccode; +}; + +struct nand_ecc +{ + uint8_t mode; /* nand_eccmode_t */ + uint8_t bytes; /* gen ecc bytes per ecc step(usually 3) */ + uint16_t stepsize:12; /* min 256 */ + uint16_t _step:4; /* */ + + /* driver must set the two interface if HWECC */ + void (*calculate)(struct nand_chip *chip, const uint8_t *dat, uint8_t *ecc_code); + /* return max bit flips if can't correct,return -1 ECC is error(0 success) */ + int(*correct)(struct nand_chip *chip, uint8_t *dat, uint8_t *read_ecc, uint8_t *calc_ecc); + /* ignore if NONECC */ + const struct mtd_oob_region *layout; +}; + +typedef struct nand_chip +{ + rt_mtd_t parent; + + /* driver must init these */ + const struct nand_ops *ops; + struct nand_ecc ecc; + const struct mtd_oob_region *freelayout; + + /* driver do not touch */ + struct nand_buffers buffers; + uint8_t *oob_poi; + uint8_t *pagebuf; + uint32_t size; + uint16_t oobsize; + uint8_t pages_pb; + uint16_t page_size; + int(*read_page)(struct nand_chip *chip, uint8_t *buf, int oob_required, int page); + int(*write_page)(struct nand_chip *chip, const uint8_t *buf, int oob_required, int page); +}rt_nand_t; + +struct nand_ops +{ + int(*cmdfunc)(rt_nand_t *nand, int cmd, int page, int offset); /* send nand operation cmd, return Status bits(0 success), + if nand is busy please wait in low driver */ + int(*read_buf)(rt_nand_t *nand, uint8_t *buf, int len); /* read data from nand chip's page buffer */ + int(*write_buf)(rt_nand_t *nand, const uint8_t *buf, int len);/* write data to nand chip's page buffer */ + int(*isbad)(rt_nand_t *nand, uint32_t blk); /* if NULL OOB[0] used as bad mark(not 0xff is bad) */ + int(*markbad)(rt_nand_t *nand, uint32_t blk); /* if NULL OOB[0] used as bad mark(set to 0x00) */ +}; + +int rt_mtd_nand_init(rt_nand_t *nand, int blk_size, int page_size, int oob_size); + +#endif /* MTD_NAND_H_ */ diff --git a/components/drivers/include/drivers/mtdnor.h b/components/drivers/include/drivers/mtdnor.h new file mode 100644 index 0000000000..ccb7466b4b --- /dev/null +++ b/components/drivers/include/drivers/mtdnor.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + 2018-09-10 heyuanjie87 first version + + */ + +#ifndef __MTDNOR_H__ +#define __MTDNOR_H__ + +#include <drivers/mtd.h> + +struct nor_ops; + +typedef struct +{ + rt_mtd_t parent; + + const struct nor_ops *ops; /* operations interface */ +}rt_nor_t; + +struct nor_ops +{ + int (*erase)(rt_nor_t *nor, loff_t addr, size_t len); /* return success erased len or error code */ + int (*read)(rt_nor_t *nor, loff_t addr, uint8_t *buf, size_t len); /* return success data size or error code */ + int (*write)(rt_nor_t *nor, loff_t addr, const uint8_t *buf, size_t len); /* return success data size or error code */ +}; + +int rt_mtd_nor_init(rt_nor_t *nor, int blksize); + +#endif diff --git a/components/drivers/include/drivers/pin.h b/components/drivers/include/drivers/pin.h index de8fd21acc..ca35407932 100644 --- a/components/drivers/include/drivers/pin.h +++ b/components/drivers/include/drivers/pin.h @@ -1,21 +1,7 @@ /* - * File : pin.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2015, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/pm.h b/components/drivers/include/drivers/pm.h new file mode 100644 index 0000000000..94d4c8559b --- /dev/null +++ b/components/drivers/include/drivers/pm.h @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2012-06-02 Bernard the first version + * 2018-08-02 Tanek split run and sleep modes, support custom mode + */ + +#ifndef __PM_H__ +#define __PM_H__ + +#include <rtthread.h> + +#ifndef PM_HAS_CUSTOM_CONFIG + +/* All modes used for rt_pm_request() adn rt_pm_release() */ +enum +{ + /* run modes */ + PM_RUN_MODE_NORMAL = 0, + + /* sleep modes */ + PM_SLEEP_MODE_SLEEP, + PM_SLEEP_MODE_TIMER, + PM_SLEEP_MODE_SHUTDOWN, +}; + +/* The name of all modes used in the msh command "pm_dump" */ +#define PM_MODE_NAMES \ +{ \ + "Running Mode", \ + \ + "Sleep Mode", \ + "Timer Mode", \ + "Shutdown Mode", \ +} + +/* run mode count : 1 */ +#define PM_RUN_MODE_COUNT 1 +/* sleep mode count : 3 */ +#define PM_SLEEP_MODE_COUNT 3 + +/* support redefining default run mode */ +#ifndef PM_RUN_MODE_DEFAULT +#define PM_RUN_MODE_DEFAULT 0 +#endif + +/* support redefining default sleep mode */ +#ifndef PM_SLEEP_MODE_DEFAULT +#define PM_SLEEP_MODE_DEFAULT (PM_SLEEP_MODE_START) +#endif + +/* support redefining the minimum tick into sleep mode */ +#ifndef PM_MIN_ENTER_SLEEP_TICK +#define PM_MIN_ENTER_SLEEP_TICK (1) +#endif + +#else /* PM_HAS_CUSTOM_CONFIG */ + +#include <pm_cfg.h> + +#ifndef PM_RUN_MODE_COUNT +#error "You must defined PM_RUN_MODE_COUNT on pm_cfg.h" +#endif + +#ifndef PM_SLEEP_MODE_COUNT +#error "You must defined PM_SLEEP_MODE_COUNT on pm_cfg.h" +#endif + +#ifndef PM_MODE_DEFAULT +#error "You must defined PM_MODE_DEFAULT on pm_cfg.h" +#endif + +#ifndef PM_MODE_NAMES +#error "You must defined PM_MODE_NAMES on pm_cfg.h" +#endif + +#ifndef PM_RUN_MODE_DEFAULT +#error "You must defined PM_RUN_MODE_DEFAULT on pm_cfg.h" +#endif + +/* The default sleep mode(PM_SLEEP_MODE_DEFAULT) are not required. + * If the default mode is defined, it is requested once in rt_system_pm_init() + */ + +#endif /* PM_HAS_CUSTOM_CONFIG */ + +/* run mode must start at 0 */ +#define PM_RUN_MODE_START 0 +/* the values of the run modes and sleep mode must be consecutive */ +#define PM_SLEEP_MODE_START PM_RUN_MODE_COUNT +/* all mode count */ +#define PM_MODE_COUNT (PM_RUN_MODE_COUNT + PM_SLEEP_MODE_COUNT) +/* The last mode, will be request in rt_system_pm_init() */ +#define PM_MODE_MAX (PM_RUN_MODE_COUNT + PM_SLEEP_MODE_COUNT - 1) + +#if PM_MODE_COUNT > 32 +#error "The number of modes cannot exceed 32" +#endif + +/** + * device control flag to request or release power + */ +#define RT_PM_DEVICE_CTRL_REQUEST 0x01 +#define RT_PM_DEVICE_CTRL_RELEASE 0x02 + +struct rt_pm; + +/** + * low power mode operations + */ +struct rt_pm_ops +{ + void (*enter)(struct rt_pm *pm); + void (*exit)(struct rt_pm *pm); + +#if PM_RUN_MODE_COUNT > 1 + void (*frequency_change)(struct rt_pm *pm, rt_uint32_t frequency); +#endif + + void (*timer_start)(struct rt_pm *pm, rt_uint32_t timeout); + void (*timer_stop)(struct rt_pm *pm); + rt_tick_t (*timer_get_tick)(struct rt_pm *pm); +}; + +struct rt_device_pm_ops +{ +#if PM_RUN_MODE_COUNT > 1 + void (*frequency_change)(const struct rt_device* device); +#endif + + void (*suspend)(const struct rt_device* device); + void (*resume) (const struct rt_device* device); +}; + +struct rt_device_pm +{ + const struct rt_device* device; + const struct rt_device_pm_ops* ops; +}; + +/** + * power management + */ +struct rt_pm +{ + struct rt_device parent; + + /* modes */ + rt_uint8_t modes[PM_MODE_COUNT]; + rt_uint8_t current_mode; /* current pm mode */ + rt_uint8_t exit_count; + + /* the list of device, which has PM feature */ + rt_uint8_t device_pm_number; + struct rt_device_pm* device_pm; + struct rt_semaphore device_lock; + + /* if the mode has timer, the corresponding bit is 1*/ + rt_uint32_t timer_mask; + + const struct rt_pm_ops *ops; +}; + +void rt_pm_enter(void); +void rt_pm_exit(void); + +void rt_pm_request(rt_ubase_t mode); +void rt_pm_release(rt_ubase_t mode); + +void rt_pm_register_device(struct rt_device* device, const struct rt_device_pm_ops* ops); +void rt_pm_unregister_device(struct rt_device* device); + +void rt_system_pm_init(const struct rt_pm_ops *ops, + rt_uint8_t timer_mask, + void *user_data); + +#endif /* __PM_H__ */ diff --git a/components/drivers/include/drivers/rt_drv_pwm.h b/components/drivers/include/drivers/rt_drv_pwm.h index ffd08edabb..16de4c6ab8 100644 --- a/components/drivers/include/drivers/rt_drv_pwm.h +++ b/components/drivers/include/drivers/rt_drv_pwm.h @@ -1,21 +1,7 @@ /* - * File : rt_drv_pwm.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2018, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -25,6 +11,9 @@ #ifndef __DRV_PWM_H_INCLUDE__ #define __DRV_PWM_H_INCLUDE__ +#include <rtthread.h> +#include <rtdevice.h> + #define PWM_CMD_ENABLE (128 + 0) #define PWM_CMD_DISABLE (128 + 1) #define PWM_CMD_SET (128 + 2) @@ -49,6 +38,10 @@ struct rt_device_pwm const struct rt_pwm_ops *ops; }; -extern rt_err_t rt_device_pwm_register(struct rt_device_pwm *device, const char *name, const struct rt_pwm_ops *ops, const void *user_data); +rt_err_t rt_device_pwm_register(struct rt_device_pwm *device, const char *name, const struct rt_pwm_ops *ops, const void *user_data); + +rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel); +rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel); +rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse); #endif /* __DRV_PWM_H_INCLUDE__ */ diff --git a/components/drivers/include/drivers/rtc.h b/components/drivers/include/drivers/rtc.h index 7e89bd3704..1cd88a7be3 100644 --- a/components/drivers/include/drivers/rtc.h +++ b/components/drivers/include/drivers/rtc.h @@ -1,21 +1,7 @@ /* - * File : rtc.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/sd.h b/components/drivers/include/drivers/sd.h index 7d5fcacade..e096df9372 100644 --- a/components/drivers/include/drivers/sd.h +++ b/components/drivers/include/drivers/sd.h @@ -1,21 +1,7 @@ /* - * File : sd.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/sdio.h b/components/drivers/include/drivers/sdio.h index 2cd1c7dfd1..e4bf4a048f 100644 --- a/components/drivers/include/drivers/sdio.h +++ b/components/drivers/include/drivers/sdio.h @@ -1,21 +1,7 @@ /* - * File : sdio.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/sdio_func_ids.h b/components/drivers/include/drivers/sdio_func_ids.h index 9fee7c238a..79085a5933 100644 --- a/components/drivers/include/drivers/sdio_func_ids.h +++ b/components/drivers/include/drivers/sdio_func_ids.h @@ -1,21 +1,7 @@ /* - * File : sdio_func_ids.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/serial.h b/components/drivers/include/drivers/serial.h index 2abf925854..af5d626251 100644 --- a/components/drivers/include/drivers/serial.h +++ b/components/drivers/include/drivers/serial.h @@ -1,21 +1,7 @@ /* - * File : serial.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/spi.h b/components/drivers/include/drivers/spi.h index 0fbaabe5dd..84b589ace0 100644 --- a/components/drivers/include/drivers/spi.h +++ b/components/drivers/include/drivers/spi.h @@ -1,21 +1,7 @@ /* - * File : spi.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/usb_common.h b/components/drivers/include/drivers/usb_common.h index 922682b1af..c26d83391e 100644 --- a/components/drivers/include/drivers/usb_common.h +++ b/components/drivers/include/drivers/usb_common.h @@ -1,21 +1,7 @@ /* - * File : usb_common.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -230,6 +216,19 @@ extern "C" { #define HID_REPORT_ID_GENERAL 5 #define HID_REPORT_ID_MOUSE 6 +/* + * Time of usb timeout + */ +#ifndef USB_TIMEOUT_BASIC +#define USB_TIMEOUT_BASIC (RT_TICK_PER_SECOND) /* 1s */ +#endif +#ifndef USB_TIMEOUT_LONG +#define USB_TIMEOUT_LONG (RT_TICK_PER_SECOND * 5) /* 5s */ +#endif +#ifndef USB_DEBOUNCE_TIME +#define USB_DEBOUNCE_TIME (RT_TICK_PER_SECOND / 5) /* 0.2s */ +#endif + #define uswap_32(x) \ ((((x) & 0xff000000) >> 24) | \ (((x) & 0x00ff0000) >> 8) | \ diff --git a/components/drivers/include/drivers/usb_device.h b/components/drivers/include/drivers/usb_device.h index 572c7d0dff..26f0c4cf4a 100644 --- a/components/drivers/include/drivers/usb_device.h +++ b/components/drivers/include/drivers/usb_device.h @@ -1,21 +1,7 @@ /* - * File : usb_device.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -31,6 +17,10 @@ #include <rtthread.h> #include "drivers/usb_common.h" +#ifdef __cplusplus +extern "C" { +#endif + /* Vendor ID */ #ifdef USB_VENDOR_ID #define _VENDOR_ID USB_VENDOR_ID @@ -467,4 +457,9 @@ rt_inline void usbd_os_proerty_descriptor_send(ufunction_t func, ureq_t setup, u } rt_usbd_ep0_write(func->device, data, setup->wLength); } + +#ifdef __cplusplus +} +#endif + #endif diff --git a/components/drivers/include/drivers/usb_host.h b/components/drivers/include/drivers/usb_host.h index 92adaa3d47..2a68f4edb5 100644 --- a/components/drivers/include/drivers/usb_host.h +++ b/components/drivers/include/drivers/usb_host.h @@ -1,21 +1,7 @@ /* - * File : usb_host.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/watchdog.h b/components/drivers/include/drivers/watchdog.h index 5c74dc82be..f80ded58aa 100644 --- a/components/drivers/include/drivers/watchdog.h +++ b/components/drivers/include/drivers/watchdog.h @@ -1,21 +1,7 @@ /* - * File : watchdog.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012-2014, Shanghai Real-Thread Electronic Technology Co.,Ltd - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * COPYRIGHT (C) 2018, Real-Thread Information Technology Ltd + * + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/drivers/wlan.h b/components/drivers/include/drivers/wlan.h new file mode 100644 index 0000000000..724e61da78 --- /dev/null +++ b/components/drivers/include/drivers/wlan.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-09-15 tyx the first version + */ + +#ifndef __WLAN_H__ +#define __WLAN_H__ + +#include <rtthread.h> +#include <wlan_dev.h> +#include <wlan_cfg.h> +#include <wlan_mgnt.h> +#include <wlan_prot.h> +#include <wlan_workqueue.h> + +#endif diff --git a/components/drivers/include/ipc/completion.h b/components/drivers/include/ipc/completion.h index e3e883ba8b..acfcaa3995 100644 --- a/components/drivers/include/ipc/completion.h +++ b/components/drivers/include/ipc/completion.h @@ -1,3 +1,11 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ #ifndef COMPLETION_H_ #define COMPLETION_H_ diff --git a/components/drivers/include/ipc/dataqueue.h b/components/drivers/include/ipc/dataqueue.h index 44ffed655e..f584c267bf 100644 --- a/components/drivers/include/ipc/dataqueue.h +++ b/components/drivers/include/ipc/dataqueue.h @@ -1,3 +1,11 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ #ifndef DATAQUEUE_H__ #define DATAQUEUE_H__ diff --git a/components/drivers/include/ipc/pipe.h b/components/drivers/include/ipc/pipe.h index ca20c2a453..25b19f540d 100644 --- a/components/drivers/include/ipc/pipe.h +++ b/components/drivers/include/ipc/pipe.h @@ -1,3 +1,11 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ #ifndef PIPE_H__ #define PIPE_H__ diff --git a/components/drivers/include/ipc/poll.h b/components/drivers/include/ipc/poll.h index 0279fe3b5d..8c68a96af8 100644 --- a/components/drivers/include/ipc/poll.h +++ b/components/drivers/include/ipc/poll.h @@ -1,21 +1,7 @@ /* - * File : poll.h - * This file is part of Device File System in RT-Thread RTOS - * COPYRIGHT (C) 2006-2017, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/ipc/ringblk_buf.h b/components/drivers/include/ipc/ringblk_buf.h new file mode 100644 index 0000000000..653fa2a60b --- /dev/null +++ b/components/drivers/include/ipc/ringblk_buf.h @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-08-25 armink the first version + */ + +#ifndef _RINGBLK_BUF_H_ +#define _RINGBLK_BUF_H_ + +/* + * Introduction: + * The rbb is the ring buffer which is composed with many blocks. It is different from the ring buffer. + * The ring buffer is only composed with chars. The rbb put and get supported zero copies. So the rbb + * is very suitable for put block and get block by a certain order. Such as DMA block transmit, + * communicate frame send/recv, and so on. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +enum rt_rbb_status +{ + /* unused status when first initialize or after blk_free() */ + RT_RBB_BLK_UNUSED, + /* initialized status after blk_alloc() */ + RT_RBB_BLK_INITED, + /* put status after blk_put() */ + RT_RBB_BLK_PUT, + /* get status after blk_get() */ + RT_RBB_BLK_GET, +}; +typedef enum rt_rbb_status rt_rbb_status_t; + +/** + * the block of rbb + */ +struct rt_rbb_blk +{ + rt_rbb_status_t status :8; + /* less then 2^24 */ + rt_size_t size :24; + rt_uint8_t *buf; + rt_slist_t list; +}; +typedef struct rt_rbb_blk *rt_rbb_blk_t; + +/** + * Rbb block queue: the blocks (from block1->buf to blockn->buf) memory which on this queue is continuous. + */ +struct rt_rbb_blk_queue +{ + rt_rbb_blk_t blocks; + rt_size_t blk_num; +}; +typedef struct rt_rbb_blk_queue *rt_rbb_blk_queue_t; + +/** + * ring block buffer + */ +struct rt_rbb +{ + rt_uint8_t *buf; + rt_size_t buf_size; + /* all of blocks */ + rt_rbb_blk_t blk_set; + rt_size_t blk_max_num; + /* saved the initialized and put status blocks */ + rt_slist_t blk_list; +}; +typedef struct rt_rbb *rt_rbb_t; + +/* rbb (ring block buffer) API */ +void rt_rbb_init(rt_rbb_t rbb, rt_uint8_t *buf, rt_size_t buf_size, rt_rbb_blk_t block_set, rt_size_t blk_max_num); +rt_rbb_t rt_rbb_create(rt_size_t buf_size, rt_size_t blk_max_num); +void rt_rbb_destroy(rt_rbb_t rbb); +rt_size_t rt_rbb_get_buf_size(rt_rbb_t rbb); + +/* rbb block API */ +rt_rbb_blk_t rt_rbb_blk_alloc(rt_rbb_t rbb, rt_size_t blk_size); +void rt_rbb_blk_put(rt_rbb_blk_t block); +rt_rbb_blk_t rt_rbb_blk_get(rt_rbb_t rbb); +void rt_rbb_blk_free(rt_rbb_t rbb, rt_rbb_blk_t block); + +/* rbb block queue API */ +rt_size_t rt_rbb_blk_queue_get(rt_rbb_t rbb, rt_size_t queue_data_len, rt_rbb_blk_queue_t blk_queue); +rt_size_t rt_rbb_blk_queue_len(rt_rbb_blk_queue_t blk_queue); +rt_uint8_t *rt_rbb_blk_queue_buf(rt_rbb_blk_queue_t blk_queue); +void rt_rbb_blk_queue_free(rt_rbb_t rbb, rt_rbb_blk_queue_t blk_queue); +rt_size_t rt_rbb_next_blk_queue_len(rt_rbb_t rbb); + + +#ifdef __cplusplus +} +#endif + +#endif /* _RINGBLK_BUF_H_ */ diff --git a/components/drivers/include/ipc/ringbuffer.h b/components/drivers/include/ipc/ringbuffer.h index 4cfab6d69c..4487d02c47 100644 --- a/components/drivers/include/ipc/ringbuffer.h +++ b/components/drivers/include/ipc/ringbuffer.h @@ -1,3 +1,11 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ #ifndef RINGBUFFER_H__ #define RINGBUFFER_H__ diff --git a/components/drivers/include/ipc/waitqueue.h b/components/drivers/include/ipc/waitqueue.h index 8cb0989f73..e26ec1e896 100644 --- a/components/drivers/include/ipc/waitqueue.h +++ b/components/drivers/include/ipc/waitqueue.h @@ -1,21 +1,7 @@ /* - * File : waitqueue.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/include/ipc/workqueue.h b/components/drivers/include/ipc/workqueue.h index 0b044b5ad3..9e23e181b1 100644 --- a/components/drivers/include/ipc/workqueue.h +++ b/components/drivers/include/ipc/workqueue.h @@ -1,3 +1,11 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ #ifndef WORKQUEUE_H__ #define WORKQUEUE_H__ diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index e3c826fe53..1843432768 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -1,21 +1,7 @@ /* - * File : rtdevice.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -35,6 +21,7 @@ #include "ipc/waitqueue.h" #include "ipc/pipe.h" #include "ipc/poll.h" +#include "ipc/ringblk_buf.h" #ifdef __cplusplus extern "C" { @@ -116,6 +103,21 @@ extern "C" { #include "drivers/rt_drv_pwm.h" #endif +#ifdef RT_USING_PM +#include "drivers/pm.h" +#endif + +#ifdef RT_USING_WIFI +#include "drivers/wlan.h" +#endif + +#ifdef MTD_USING_NOR +#include "drivers/mtdnor.h" +#endif +#ifdef MTD_USING_NAND +#include "drivers/mtdnand.h" +#endif + #ifdef __cplusplus } #endif diff --git a/components/drivers/misc/pin.c b/components/drivers/misc/pin.c index ce78f97f87..aca58ee21b 100644 --- a/components/drivers/misc/pin.c +++ b/components/drivers/misc/pin.c @@ -1,21 +1,7 @@ /* - * File : pin.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2015, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/misc/rt_drv_pwm.c b/components/drivers/misc/rt_drv_pwm.c index 6884f8cfe4..6890e0e793 100644 --- a/components/drivers/misc/rt_drv_pwm.c +++ b/components/drivers/misc/rt_drv_pwm.c @@ -1,21 +1,7 @@ /* - * File : rt_drv_pwm.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2018, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -24,9 +10,7 @@ #include <string.h> -#include <rtthread.h> -#include <rtdevice.h> - +#include <drivers/rt_drv_pwm.h> static rt_err_t _pwm_control(rt_device_t dev, int cmd, void *args) { @@ -99,7 +83,6 @@ static rt_size_t _pwm_write(rt_device_t dev, rt_off_t pos, const void *buffer, r { return 0; } - } return size; @@ -128,10 +111,9 @@ rt_err_t rt_device_pwm_register(struct rt_device_pwm *device, const char *name, return result; } -rt_err_t rt_pwm_enable(int channel) +rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel) { rt_err_t result = RT_EOK; - struct rt_device *device = rt_device_find("pwm"); struct rt_pwm_configuration configuration = {0}; if (!device) @@ -140,15 +122,30 @@ rt_err_t rt_pwm_enable(int channel) } configuration.channel = channel; - result = rt_device_control(device, PWM_CMD_ENABLE, &configuration); + result = rt_device_control(&device->parent, PWM_CMD_ENABLE, &configuration); return result; } -rt_err_t rt_pwm_set(int channel, rt_uint32_t period, rt_uint32_t pulse) +rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel) +{ + rt_err_t result = RT_EOK; + struct rt_pwm_configuration configuration = {0}; + + if (!device) + { + return -RT_EIO; + } + + configuration.channel = channel; + result = rt_device_control(&device->parent, PWM_CMD_DISABLE, &configuration); + + return result; +} + +rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse) { rt_err_t result = RT_EOK; - struct rt_device *device = rt_device_find("pwm"); struct rt_pwm_configuration configuration = {0}; if (!device) @@ -159,12 +156,11 @@ rt_err_t rt_pwm_set(int channel, rt_uint32_t period, rt_uint32_t pulse) configuration.channel = channel; configuration.period = period; configuration.pulse = pulse; - result = rt_device_control(device, PWM_CMD_SET, &configuration); + result = rt_device_control(&device->parent, PWM_CMD_SET, &configuration); return result; } - #ifdef RT_USING_FINSH #include <finsh.h> @@ -175,33 +171,75 @@ FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_set, pwm_set, set pwm.); static int pwm_enable(int argc, char **argv) { int result = 0; + struct rt_device_pwm *device = RT_NULL; - if (argc != 2) + if (argc != 3) { - rt_kprintf("Usage: pwm_enable 1\n"); + rt_kprintf("Usage: pwm_enable pwm1 1\n"); result = -RT_ERROR; goto _exit; } - result = rt_pwm_enable(atoi(argv[1])); + device = (struct rt_device_pwm *)rt_device_find(argv[1]); + if (!device) + { + result = -RT_EIO; + goto _exit; + } + + result = rt_pwm_enable(device, atoi(argv[2])); _exit: return result; } -MSH_CMD_EXPORT(pwm_enable, pwm_enable 1); +MSH_CMD_EXPORT(pwm_enable, pwm_enable pwm1 1); + +static int pwm_disable(int argc, char **argv) +{ + int result = 0; + struct rt_device_pwm *device = RT_NULL; + + if (argc != 3) + { + rt_kprintf("Usage: pwm_enable pwm1 1\n"); + result = -RT_ERROR; + goto _exit; + } + + device = (struct rt_device_pwm *)rt_device_find(argv[1]); + if (!device) + { + result = -RT_EIO; + goto _exit; + } + + result = rt_pwm_disable(device, atoi(argv[2])); + +_exit: + return result; +} +MSH_CMD_EXPORT(pwm_disable, pwm_disable pwm1 1); static int pwm_set(int argc, char **argv) { int result = 0; + struct rt_device_pwm *device = RT_NULL; - if (argc != 4) + if (argc != 5) { - rt_kprintf("Usage: pwm_set 1 100 50\n"); + rt_kprintf("Usage: pwm_set pwm1 1 100 50\n"); result = -RT_ERROR; goto _exit; } - result = rt_pwm_set(atoi(argv[1]), atoi(argv[2]), atoi(argv[3])); + device = (struct rt_device_pwm *)rt_device_find(argv[1]); + if (!device) + { + result = -RT_EIO; + goto _exit; + } + + result = rt_pwm_set(device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4])); _exit: return result; @@ -209,6 +247,4 @@ _exit: MSH_CMD_EXPORT(pwm_set, pwm_set 1 100 50); #endif /* FINSH_USING_MSH */ - - #endif /* RT_USING_FINSH */ diff --git a/components/drivers/mtd/SConscript b/components/drivers/mtd/SConscript index bc2bd54218..462c38194c 100644 --- a/components/drivers/mtd/SConscript +++ b/components/drivers/mtd/SConscript @@ -9,6 +9,17 @@ depend = [] CPPPATH = [cwd + '/../include'] group = [] +if GetDepend(['RT_USING_MTD']): + src += ['mtd.c'] + depend += ['RT_USING_MTD'] + + if GetDepend(['MTD_USING_NOR']): + src += ['mtdnor.c'] + depend += ['MTD_USING_NOR'] + if GetDepend(['MTD_USING_NAND']): + src += ['mtdnand.c'] + depend += ['MTD_USING_NAND'] + if GetDepend(['RT_USING_MTD_NOR']): src += ['mtd_nor.c'] depend += ['RT_USING_MTD_NOR'] diff --git a/components/drivers/mtd/mtd.c b/components/drivers/mtd/mtd.c new file mode 100644 index 0000000000..3e7b4f2b54 --- /dev/null +++ b/components/drivers/mtd/mtd.c @@ -0,0 +1,236 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + 2018-09-10 heyuanjie87 first version + + */ + +#include <drivers/mtd.h> + +static rt_mtd_t* mtd_part_alloc(rt_mtd_t *master, const struct mtd_part *part) +{ + rt_mtd_t *slave; + + slave = rt_malloc(sizeof(rt_mtd_t)); + if (slave == RT_NULL) + goto out; + + slave->master = master; + + *slave = *master; + slave->size = part->size; + slave->offset = part->offset; + +out: + + return slave; +} + +rt_mtd_t* rt_mtd_get(const char *name) +{ + rt_mtd_t *mtd; + + mtd = (rt_mtd_t *)rt_device_find(name); + if (mtd == RT_NULL) + return RT_NULL; + + if (mtd->parent.type != RT_Device_Class_MTD) + return RT_NULL; + + return mtd; +} + +/* + * Register MTD driver + * + * @parts partion description + * @np number of partitions + * @return number of unregistered partitions + * +*/ +int rt_mtd_register(rt_mtd_t *master, const struct mtd_part *parts, int np) +{ + int ret; + rt_mtd_t *slave; + + master->master = master; + master->parent.type = RT_Device_Class_MTD; + + if (np > 0) + { + master->offset = parts->offset; + master->size = parts->size; + + ret = rt_device_register((rt_device_t)master, parts->name, 0); + if (ret != 0) + goto _out; + + np --; + parts ++; + } + + while (np > 0) + { + slave = mtd_part_alloc(master, parts); + if (!slave) + break; + ret = rt_device_register((rt_device_t)slave, parts->name, 0); + if (ret) + break; + parts ++; + np --; + } + +_out: + return np; +} + +int rt_mtd_block_erase(rt_mtd_t *mtd, uint32_t block) +{ + uint32_t total_blks; + loff_t addr; + + total_blks = mtd->size/mtd->block_size; + if (block >= total_blks) + return -EINVAL; + addr = mtd->offset + mtd->block_size * block; + + return mtd->ops->erase(mtd->master, addr, mtd->block_size); +} + +int rt_mtd_block_isbad(rt_mtd_t *mtd, uint32_t block) +{ + uint32_t total_blks, offset_blk; + + if (!mtd->ops->isbad) + return 0; + + total_blks = mtd->size / mtd->block_size; + if (block >= total_blks) + return -EINVAL; + offset_blk = mtd->offset / mtd->block_size; + + return mtd->ops->isbad(mtd->master, block + offset_blk); +} + +int rt_mtd_block_markbad(rt_mtd_t *mtd, uint32_t block) +{ + uint32_t total_blks, offset_blk; + + if (!mtd->ops->markbad) + return -EOPNOTSUPP; + + total_blks = mtd->size / mtd->block_size; + if (block >= total_blks) + return -EINVAL; + offset_blk = mtd->offset / mtd->block_size; + + return mtd->ops->markbad(mtd->master, block + offset_blk); +} + +int rt_mtd_erase(rt_mtd_t *mtd, loff_t addr, size_t size) +{ + if (addr > mtd->size || (addr + size) > mtd->size) + return -EINVAL; + addr += mtd->offset; + + return mtd->ops->erase(mtd->master, addr, size); +} + +/* +* Read data only +* +* @from offset to read from +* @return success size or error code +*/ +int rt_mtd_read(rt_mtd_t *mtd, loff_t from, uint8_t *buf, size_t len) +{ + int ret; + struct mtd_io_desc desc = {0}; + + if (from < 0 || from >= (loff_t)mtd->size || len > mtd->size - from) + return -EINVAL; + if (!len) + return 0; + + desc.datbuf = buf; + desc.datlen = len; + ret = mtd->ops->read(mtd->master, from + mtd->offset, &desc); + if (ret) + return ret; + + return desc.datretlen; +} + +/** +* Write data only +* +* @to offset to write from +* @return success size or error code +*/ +int rt_mtd_write(rt_mtd_t *mtd, loff_t to, const uint8_t *buf, size_t len) +{ + int ret; + struct mtd_io_desc desc = {0}; + + if (to < 0 || to >= (loff_t)mtd->size || len > mtd->size - to) + return -EINVAL; + if (!mtd->ops->write) + return -EROFS; + if (!len) + return 0; + + desc.datbuf = (uint8_t*)buf; + desc.datlen = len; + ret = mtd->ops->write(mtd->master, to + mtd->offset, &desc); + if (ret) + return ret; + + return desc.datretlen; +} + +/** +* Read data and/or out-of-band +* +* @from offset to read from +* @desc sector operation description structure +* @return error code, 0 success +*/ +int rt_mtd_read_oob(rt_mtd_t *mtd, loff_t from, struct mtd_io_desc *desc) +{ + desc->datretlen = 0; + desc->oobretlen = 0; + + if (from < 0 || from >= (loff_t)mtd->size) + return -EINVAL; + + if (desc->datbuf && (desc->datlen > (mtd->size - from))) + return -EINVAL; + + return mtd->ops->read(mtd->master, from + mtd->offset, desc); +} + +/** +* Write data and/or out-of-band +* +* @to offset to read from +* @desc sector operation description structure +* @return error code, 0 success +*/ +int rt_mtd_write_oob(rt_mtd_t *mtd, loff_t to, struct mtd_io_desc *desc) +{ + desc->datretlen = 0; + desc->oobretlen = 0; + + if (to < 0 || to >= (loff_t)mtd->size) + return -EINVAL; + + if (desc->datbuf && (desc->datlen >(mtd->size - to))) + return -EINVAL; + + return mtd->ops->write(mtd->master, to + mtd->offset, desc); +} diff --git a/components/drivers/mtd/mtd_nand.c b/components/drivers/mtd/mtd_nand.c index bf22d889ce..3e4582ceaf 100644 --- a/components/drivers/mtd/mtd_nand.c +++ b/components/drivers/mtd/mtd_nand.c @@ -1,21 +1,7 @@ /* - * File : mtd_core.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/mtd/mtd_nor.c b/components/drivers/mtd/mtd_nor.c index 8f37e95f09..b941f9236d 100644 --- a/components/drivers/mtd/mtd_nor.c +++ b/components/drivers/mtd/mtd_nor.c @@ -1,21 +1,7 @@ /* - * File : mtd_nor.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, Shanghai Real-Thread Technology Co., Ltd - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * COPYRIGHT (C) 2018, Real-Thread Information Technology Ltd + * + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/mtd/mtdnand.c b/components/drivers/mtd/mtdnand.c new file mode 100644 index 0000000000..50012219fb --- /dev/null +++ b/components/drivers/mtd/mtdnand.c @@ -0,0 +1,645 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-09-10 heyuanjie87 first version + */ + +#include <rtdevice.h> + +#define MTDTONAND(x) ((rt_nand_t*)(x)) +#define NOTALIGNED(x) ((x & (chip->page_size - 1)) != 0) +#ifndef min +#define min(a,b) (a>b? b:a) +#endif + +static uint8_t *nand_fill_oob(rt_nand_t *chip, uint8_t *oob, size_t len, struct mtd_io_desc *desc) +{ + rt_memset(chip->oob_poi, 0xff, chip->oobsize); + + switch (desc->mode) + { + case MTD_OPM_PLACE_OOB: + case MTD_OPM_RAW: + rt_memcpy(chip->oob_poi + desc->ooboffs, oob, len); + return oob + len; + + case MTD_OPM_AUTO_OOB: + { + const struct mtd_oob_region *free = chip->freelayout; + uint32_t boffs; + size_t bytes; + + bytes = min(len, free->length); + boffs = free->offset; + + rt_memcpy(chip->oob_poi + boffs, oob, bytes); + oob += bytes; + + return oob; + } + } + + return NULL; +} + +static uint8_t *nand_transfer_oob(rt_nand_t *chip, uint8_t *oob, struct mtd_io_desc *desc, size_t len) +{ + switch (desc->mode) + { + case MTD_OPM_PLACE_OOB: + case MTD_OPM_RAW: + rt_memcpy(oob, chip->oob_poi + desc->ooboffs, len); + return oob + len; + + case MTD_OPM_AUTO_OOB: + { + struct mtd_oob_region *free = (struct mtd_oob_region *)chip->freelayout; + uint32_t boffs = 0, roffs = desc->ooboffs; + size_t bytes = 0; + + for (; free->length && len; free++, len -= bytes) + { + /* Read request not from offset 0? */ + if (roffs) + { + if (roffs >= free->length) + { + roffs -= free->length; + continue; + } + boffs = free->offset + roffs; + bytes = min(len, (free->length - roffs)); + roffs = 0; + } + else + { + bytes = min(len, free->length); + boffs = free->offset; + } + + rt_memcpy(oob, chip->oob_poi + boffs, bytes); + oob += bytes; + } + + return oob; + } + } + + return NULL; +} + +static int nand_read_page_raw(rt_nand_t *chip, uint8_t *buf, int oob_required, int page) +{ + chip->ops->read_buf(chip, buf, chip->page_size); + + if (oob_required) + chip->ops->read_buf(chip, chip->oob_poi, chip->oobsize); + + return 0; +} + +static int nand_write_page_raw(rt_nand_t *chip, const uint8_t *buf, int oob_required, int page) +{ + chip->ops->write_buf(chip, buf, chip->page_size); + + if (oob_required) + chip->ops->write_buf(chip, chip->oob_poi, chip->oobsize); + + return 0; +} + +static int nand_write_page_hwecc(rt_nand_t *chip, const uint8_t *buf, int oob_required, int page) +{ + uint16_t i; + uint16_t stepsize = chip->ecc.stepsize; + uint16_t eccbytes = chip->ecc.bytes; + uint16_t eccsteps = chip->ecc._step; + uint16_t eccpos = chip->ecc.layout->offset; + uint8_t *ecc_calc = chip->buffers.ecccalc; + const uint8_t *p = buf; + + for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += stepsize) + { + chip->ops->cmdfunc(chip, NAND_CMD_ECC_EN, 0, 0); + chip->ops->write_buf(chip, p, stepsize); + chip->ecc.calculate(chip, p, &ecc_calc[i]); + chip->ops->cmdfunc(chip, NAND_CMD_ECC_DIS, 0, 0); + } + + rt_memcpy(&chip->oob_poi[eccpos], ecc_calc, chip->ecc.layout->length); + + chip->ops->write_buf(chip, chip->oob_poi, chip->oobsize); + + return 0; +} + +static int nand_read_page_hwecc(rt_nand_t *chip, uint8_t *buf, int oob_required, int page) +{ + uint16_t i; + uint16_t eccsize = chip->ecc.stepsize; + uint16_t eccbytes = chip->ecc.bytes; + uint16_t eccsteps = chip->ecc._step; + uint16_t eccpos = chip->ecc.layout->offset; + uint8_t *p = buf; + uint8_t *ecc_calc = chip->buffers.ecccalc; + uint8_t *ecc_code = chip->buffers.ecccode; + int ret = 0; + + for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) + { + chip->ops->cmdfunc(chip, NAND_CMD_ECC_EN, 0, 0); + chip->ops->read_buf(chip, p, eccsize); + chip->ecc.calculate(chip, p, &ecc_calc[i]); + chip->ops->cmdfunc(chip, NAND_CMD_ECC_DIS, 0, 0); + } + + chip->ops->read_buf(chip, chip->oob_poi, chip->oobsize); + rt_memcpy(ecc_code, &chip->oob_poi[eccpos], chip->ecc.layout->length); + + eccsteps = chip->ecc._step; + p = buf; + + for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) + { + int stat; + + stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]); + if (stat != 0) + ret = -1; + } + + return ret; +} + +static int nand_write_page(rt_nand_t *chip, const uint8_t *buf, + int oob_required, int page, int raw) +{ + int status; + + chip->ops->cmdfunc(chip, NAND_CMD_PAGE_WR0, page, 0x00); + + if (raw) + { + nand_write_page_raw(chip, buf, oob_required, page); + } + else + { + chip->write_page(chip, buf, oob_required, page); + } + + status = chip->ops->cmdfunc(chip, NAND_CMD_PAGE_WR1, -1, -1); + + return status; +} + +static int nand_do_read_desc(rt_nand_t *chip, loff_t from, struct mtd_io_desc *desc) +{ + int page, bytes; + char oob_required; + char ecc_fail = 0; + int ret = 0; + uint32_t readlen = desc->datlen; + uint16_t oobreadlen = desc->ooblen; + uint16_t max_oobsize = desc->mode == MTD_OPM_AUTO_OOB ? + chip->freelayout->length : chip->oobsize; + + uint8_t *oob, *buf, *notalign = 0; + + /* Reject reads, which are not page aligned */ + if (NOTALIGNED(from)) + { + return -EINVAL; + } + + buf = desc->datbuf; + if (NOTALIGNED(desc->datlen) && !chip->pagebuf) + { + chip->pagebuf = rt_malloc(chip->page_size); + if (!chip->pagebuf) + return -ENOMEM; + } + + page = (int)(from / chip->page_size); + + oob = desc->oobbuf; + oob_required = oob ? 1 : 0; + + while (1) + { + bytes = min(chip->page_size, readlen); + + chip->ops->cmdfunc(chip, NAND_CMD_PAGE_RD, page, 0x00); + if (NOTALIGNED(bytes)) + { + notalign = buf; + buf = chip->pagebuf; + } + /* + * Now read the page into the buffer. Absent an error, + * the read methods return max bitflips per ecc step. + */ + if (desc->mode == MTD_OPM_RAW) + { + ret = nand_read_page_raw(chip, buf, oob_required, page); + } + else + { + ret = chip->read_page(chip, buf, oob_required, page); + } + + if (ret != 0) + { + ret = -EBADMSG; + break; + } + + if (oob) + { + int toread = min(oobreadlen, max_oobsize); + + if (toread) + { + oob = nand_transfer_oob(chip, oob, desc, toread); + oobreadlen -= toread; + } + } + + if (notalign) + { + rt_memcpy(notalign, buf, bytes); + } + + buf += bytes; + readlen -= bytes; + + if (!readlen) + break; + + page++; + } + + desc->datretlen = desc->datlen - (size_t)readlen; + if (oob) + desc->oobretlen = desc->ooblen - oobreadlen; + + return ret; +} + +/* + * write with ECC + * +*/ +static int nand_do_write_desc(rt_nand_t *chip, loff_t to, struct mtd_io_desc *desc) +{ + int page; + uint16_t writelen = desc->datlen; + uint16_t oob_required = desc->oobbuf ? 1 : 0; + uint16_t oobwritelen = desc->ooblen; + uint16_t oobmaxlen = desc->mode == MTD_OPM_AUTO_OOB ? + chip->freelayout->length : chip->oobsize; + + uint8_t *oob = desc->oobbuf; + uint8_t *buf = desc->datbuf; + int ret; + + if (!writelen) + return 0; + + /* Reject writes, which are not page aligned */ + if (NOTALIGNED(to)) + { + return -EINVAL; + } + + page = (int)(to / chip->page_size); + + /* Don't allow multipage oob writes with offset */ + if (oob && desc->ooboffs && (desc->ooboffs + desc->ooblen > oobmaxlen)) + { + ret = -EINVAL; + goto err_out; + } + + if (NOTALIGNED(desc->datlen) && !chip->pagebuf) + { + chip->pagebuf = rt_malloc(chip->page_size); + if (!chip->pagebuf) + return -ENOMEM; + } + + while (1) + { + uint16_t bytes = min(chip->page_size, writelen); + + if (oob) + { + size_t len = min(oobwritelen, oobmaxlen); + oob = nand_fill_oob(chip, oob, len, desc); + oobwritelen -= len; + } + else + { + /* We still need to erase leftover OOB data */ + rt_memset(chip->oob_poi, 0xff, chip->oobsize); + } + + if (NOTALIGNED(bytes)) + { + uint8_t *dbtmp = buf; + buf = chip->pagebuf; + rt_memset(&buf[bytes], 0xff, chip->page_size - bytes); + rt_memcpy(buf, dbtmp, bytes); + } + ret = nand_write_page(chip, buf, oob_required, page, (desc->mode == MTD_OPM_RAW)); + if (ret) + break; + + writelen -= bytes; + if (!writelen) + break; + + buf += bytes; + page++; + } + + desc->datretlen = desc->datlen - writelen; + if (oob) + desc->oobretlen = desc->ooblen; + +err_out: + + return ret; +} + +static int nand_read_oob_std(rt_nand_t *chip, int page) +{ + chip->ops->cmdfunc(chip, NAND_CMD_PAGE_RD, page, chip->page_size); + chip->ops->read_buf(chip, chip->oob_poi, chip->oobsize); + + return 0; +} + +/* + * read one page of OOB +*/ +static int nand_only_read_oob(rt_nand_t *chip, loff_t from, struct mtd_io_desc *desc) +{ + int page; + int readlen = desc->ooblen; + int len; + uint8_t *buf = desc->oobbuf; + int ret = 0; + + if (desc->mode == MTD_OPM_AUTO_OOB) + len = chip->freelayout->length; + else + len = chip->oobsize; + + if (desc->ooboffs >= len) //attempt to start read outside oob + { + return -EINVAL; + } + + page = (int)(from / chip->page_size); + + ret = nand_read_oob_std(chip, page); + if (ret == 0) + { + len = min(len, readlen); + buf = nand_transfer_oob(chip, buf, desc, len); + desc->oobretlen = len; + } + + return ret; +} + +static int nand_write_oob_std(rt_nand_t *chip, int page) +{ + int status; + + chip->ops->cmdfunc(chip, NAND_CMD_PAGE_WR0, page, chip->page_size); + chip->ops->write_buf(chip, chip->oob_poi, chip->oobsize); + /* Send command to program the OOB data */ + status = chip->ops->cmdfunc(chip, NAND_CMD_PAGE_WR1, -1, -1); + + return status & NAND_STATUS_FAIL ? -EIO : 0; +} + +static int nand_only_write_oob(rt_nand_t *chip, loff_t to, struct mtd_io_desc *desc) +{ + int page, ret, len; + + if (desc->mode == MTD_OPM_AUTO_OOB) + len = chip->freelayout->length; + else + len = chip->oobsize; + + /* Do not allow write past end of page */ + if ((desc->ooboffs + desc->ooblen) > len) + { + return -EINVAL; + } + + if (desc->ooblen == 0) + { + return -EINVAL; + } + + /* get page */ + page = (int)(to / chip->page_size); + + nand_fill_oob(chip, desc->oobbuf, desc->ooblen, desc); + + ret = nand_write_oob_std(chip, page); + if (ret == 0) + desc->oobretlen = len; + + return ret; +} + +static int nand_erase(rt_mtd_t *mtd, loff_t addr, size_t size) +{ + rt_nand_t *chip; + int status; + int page; + uint32_t blksize; + + chip = MTDTONAND(mtd); + blksize = mtd->block_size; + page = addr / chip->page_size; + + while (size >= blksize) + { + status = chip->ops->cmdfunc(chip, NAND_CMD_BLK_ERASE, page, 0); + if (status & NAND_STATUS_FAIL) + { + break; + } + size -= blksize; + page += chip->pages_pb; + } + + return size; +} + +static int nand_read(rt_mtd_t *mtd, loff_t from, struct mtd_io_desc *desc) +{ + int ret = -ENOTSUP; + rt_nand_t *chip; + + chip = MTDTONAND(mtd); + + switch (desc->mode) + { + case MTD_OPM_PLACE_OOB: + case MTD_OPM_AUTO_OOB: + case MTD_OPM_RAW: + break; + + default: + goto out; + } + + if (!desc->datbuf || !desc->datlen) + ret = nand_only_read_oob(chip, from, desc); + else + ret = nand_do_read_desc(chip, from, desc); + +out: + + return ret; +} + +static int nand_write(rt_mtd_t *mtd, loff_t to, struct mtd_io_desc *desc) +{ + int ret = -ENOTSUP; + rt_nand_t *chip; + + chip = MTDTONAND(mtd); + + switch (desc->mode) + { + case MTD_OPM_PLACE_OOB: + case MTD_OPM_AUTO_OOB: + case MTD_OPM_RAW: + break; + + default: + goto out; + } + + if (!desc->datbuf || !desc->datlen) + ret = nand_only_write_oob(chip, to, desc); + else + ret = nand_do_write_desc(chip, to, desc); + +out: + + return ret; +} + +static int nand_block_isbad(rt_mtd_t *mtd, uint32_t blk) +{ + int ret; + rt_nand_t *chip = MTDTONAND(mtd); + + if (chip->ops->isbad) + { + ret = chip->ops->isbad(chip, blk); + } + else + { + int page; + + page = blk * chip->pages_pb; + nand_read_oob_std(chip, page); + ret = chip->oob_poi[0] != 0xFF; + } + + return ret; +} + +static int nand_block_markbad(rt_mtd_t *mtd, uint32_t blk) +{ + int ret; + rt_nand_t *chip; + + chip = MTDTONAND(mtd); + + if (chip->ops->markbad) + { + ret = chip->ops->markbad(chip, blk); + } + else + { + int page; + + page = blk * chip->pages_pb; + rt_memset(chip->oob_poi, 0xff, chip->oobsize); + chip->oob_poi[0] = 0; + ret = nand_write_oob_std(chip, page); + } + + return ret; +} + +static const struct mtd_ops _ops = +{ + nand_erase, + nand_read, + nand_write, + nand_block_isbad, + nand_block_markbad, +}; + +int rt_mtd_nand_init(rt_nand_t *nand, int blk_size, int page_size, int oob_size) +{ + uint8_t *buf; + + buf = rt_malloc(oob_size * 3); + if (buf == RT_NULL) + return -ENOMEM; + + nand->oob_poi = buf; + buf += oob_size; + nand->buffers.ecccalc = buf; + buf += oob_size; + nand->buffers.ecccode = buf; + nand->pagebuf = 0; /* alloc when unaligen access */ + + nand->pages_pb = blk_size / page_size; + nand->ecc._step = page_size / nand->ecc.stepsize; + nand->page_size = page_size; + nand->oobsize = oob_size; + + nand->parent.type = MTD_TYPE_NAND; + nand->parent.ops = &_ops; + nand->parent.sector_size = page_size; + nand->parent.block_size = blk_size; + nand->parent.oob_size = oob_size; + + switch (nand->ecc.mode) + { + case NAND_ECCM_NONE: + { + nand->read_page = nand_read_page_raw; + nand->write_page = nand_write_page_raw; + }break; + case NAND_ECCM_HW: + { + nand->read_page = nand_read_page_hwecc; + nand->write_page = nand_write_page_hwecc; + }break; + default: + { + rt_free(buf); + return -1; + } + } + + return 0; +} diff --git a/components/drivers/mtd/mtdnor.c b/components/drivers/mtd/mtdnor.c new file mode 100644 index 0000000000..481d26167e --- /dev/null +++ b/components/drivers/mtd/mtdnor.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + 2018-09-10 heyuanjie87 first version + + */ + +#include <drivers/mtdnor.h> + +#ifdef MTD_USING_NOR +static int _nor_erase(rt_mtd_t *mtd, loff_t addr, size_t len) +{ + rt_nor_t *nor; + + nor = (rt_nor_t *)mtd; + return nor->ops->erase(nor, addr, len); +} + +static int _nor_read(rt_mtd_t *mtd, loff_t from, struct mtd_io_desc *desc) +{ + rt_nor_t *nor; + int ret; + + nor = (rt_nor_t *)mtd; + ret = nor->ops->read(nor, from, desc->datbuf, desc->datlen); + if (ret > 0) + { + desc->datretlen = ret; + ret = 0; + } + + return ret; +} + +static int _nor_write(rt_mtd_t *mtd, loff_t to, struct mtd_io_desc *desc) +{ + rt_nor_t *nor; + int ret; + + nor = (rt_nor_t *)mtd; + ret = nor->ops->write(nor, to, desc->datbuf, desc->datlen); + if (ret > 0) + { + desc->datretlen = ret; + ret = 0; + } + + return ret; +} + +static const struct mtd_ops _ops = +{ + _nor_erase, + _nor_read, + _nor_write, + 0, + 0 +}; + +int rt_mtd_nor_init(rt_nor_t *nor, int blksize) +{ + nor->parent.sector_size = 1; + nor->parent.block_size = blksize; + nor->parent.ops = &_ops; + nor->parent.type = MTD_TYPE_NOR; + nor->parent.oob_size = 0; + + return 0; +} +#endif diff --git a/components/drivers/pm/SConscript b/components/drivers/pm/SConscript new file mode 100644 index 0000000000..a94fc11f57 --- /dev/null +++ b/components/drivers/pm/SConscript @@ -0,0 +1,14 @@ +from building import * + +cwd = GetCurrentDir() +src = [] +CPPPATH = [cwd + '/../include'] +group = [] + +if GetDepend(['RT_USING_PM']): + src = src + ['pm.c'] + +if len(src): + group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/components/drivers/pm/pm.c b/components/drivers/pm/pm.c new file mode 100644 index 0000000000..655c4b4c02 --- /dev/null +++ b/components/drivers/pm/pm.c @@ -0,0 +1,551 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2012-06-02 Bernard the first version + * 2018-08-02 Tanek split run and sleep modes, support custom mode + */ + +#include <rthw.h> +#include <rtthread.h> +#include <drivers/pm.h> + +#ifdef RT_USING_PM + +static struct rt_pm _pm; + +/** + * This function will suspend all registered devices + */ +static void _pm_device_suspend(void) +{ + int index; + + for (index = 0; index < _pm.device_pm_number; index++) + { + if (_pm.device_pm[index].ops->suspend != RT_NULL) + { + _pm.device_pm[index].ops->suspend(_pm.device_pm[index].device); + } + } +} + +/** + * This function will resume all registered devices + */ +static void _pm_device_resume(void) +{ + int index; + + for (index = 0; index < _pm.device_pm_number; index++) + { + if (_pm.device_pm[index].ops->resume != RT_NULL) + { + _pm.device_pm[index].ops->resume(_pm.device_pm[index].device); + } + } +} + +#if PM_RUN_MODE_COUNT > 1 +/** + * This function will update the frequency of all registered devices + */ +static void _pm_device_frequency_change(void) +{ + rt_uint32_t index; + + /* make the frequency change */ + for (index = 0; index < _pm.device_pm_number; index ++) + { + if (_pm.device_pm[index].ops->frequency_change != RT_NULL) + _pm.device_pm[index].ops->frequency_change(_pm.device_pm[index].device); + } +} +#endif + +/** + * This function will enter corresponding power mode. + */ +void rt_pm_enter(void) +{ + rt_ubase_t level; + struct rt_pm *pm; + rt_uint32_t index; + rt_tick_t timeout_tick; + + pm = &_pm; + + /* disable interrupt before check run modes */ + level = rt_hw_interrupt_disable(); + /* check each run mode, and decide to swithc to run mode or sleep mode */ + for (index = 0; index < PM_RUN_MODE_COUNT; index++) + { + if (pm->modes[index]) + { + if (index > pm->current_mode) + { + pm->ops->exit(pm); + pm->current_mode = index; + pm->ops->enter(pm); +#if PM_RUN_MODE_COUNT > 1 + pm->ops->frequency_change(pm, 0); + _pm_device_frequency_change(); +#endif + } + + rt_hw_interrupt_enable(level); + /* The current mode is run mode, no need to check sleep mode */ + return ; + } + } + /* enable interrupt after check run modes */ + rt_hw_interrupt_enable(level); + + level = rt_hw_interrupt_disable(); + /* check each sleep mode to decide which mode can system sleep. */ + for (index = PM_SLEEP_MODE_START; index < PM_SLEEP_MODE_START + PM_SLEEP_MODE_COUNT; index++) + { + if (pm->modes[index]) + { + /* let mcu sleep when system is idle */ + + /* run mode to sleep mode */ + if (pm->current_mode < PM_SLEEP_MODE_START) + { + /* exit run mode */ + pm->ops->exit(pm); + } + + /* set current power mode */ + pm->current_mode = index; + pm->exit_count = 1; + + /* suspend all of devices with PM feature */ + _pm_device_suspend(); + + /* should start pm timer */ + if (pm->timer_mask & (1 << index)) + { + /* get next os tick */ + timeout_tick = rt_timer_next_timeout_tick(); + if (timeout_tick != RT_TICK_MAX) + { + timeout_tick -= rt_tick_get(); + +#if defined(PM_MIN_ENTER_SLEEP_TICK) && PM_MIN_ENTER_SLEEP_TICK > 0 + if (timeout_tick < PM_MIN_ENTER_SLEEP_TICK) + { + rt_hw_interrupt_enable(level); + /* limit the minimum time to enter timer sleep mode */ + return ; + } +#endif + } + /* startup pm timer */ + pm->ops->timer_start(pm, timeout_tick); + } + + /* enter sleep and wait to be waken up */ + pm->ops->enter(pm); + + rt_hw_interrupt_enable(level); + + /* exit from low power mode */ + rt_pm_exit(); + + return ; + } + } + + rt_hw_interrupt_enable(level); +} + +/** + * This function exits from sleep mode. + */ +void rt_pm_exit(void) +{ + rt_ubase_t level; + struct rt_pm *pm; + rt_tick_t delta_tick; + + pm = &_pm; + + level = rt_hw_interrupt_disable(); + + if (pm->exit_count) + { + pm->exit_count = 0; + + if (pm->current_mode >= PM_SLEEP_MODE_START) + { + /* sleep mode with timer */ + if (pm->timer_mask & (1 << pm->current_mode)) + { + /* get the tick of pm timer */ + delta_tick = pm->ops->timer_get_tick(pm); + + /* stop pm timer */ + pm->ops->timer_stop(pm); + + if (delta_tick) + { + /* adjust OS tick */ + rt_tick_set(rt_tick_get() + delta_tick); + /* check system timer */ + rt_timer_check(); + } + } + + /* exit from sleep mode */ + pm->ops->exit(pm); + /* resume the device with PM feature */ + _pm_device_resume(); + } + } + + rt_hw_interrupt_enable(level); +} + +/** + * Upper application or device driver requests the system + * stall in corresponding power mode. + * + * @param parameter the parameter of run mode or sleep mode + */ +void rt_pm_request(rt_ubase_t mode) +{ + rt_ubase_t level; + struct rt_pm *pm; + + pm = &_pm; + + if (mode > PM_MODE_MAX) + return; + + level = rt_hw_interrupt_disable(); + + /* update pm modes table */ + pm->modes[mode] ++; + + /* request higter mode with a smaller mode value*/ + if (mode < pm->current_mode) + { + /* the old current mode is RUN mode, need to all pm->ops->exit(), + * if not, it has already called in rt_pm_exit() + */ + if (pm->current_mode < PM_SLEEP_MODE_START) + { + pm->ops->exit(pm); + } + else if (pm->exit_count) + { + /* call exeit when global interrupt is disable */ + pm->ops->exit(pm); + pm->exit_count = 0; + } + + /* update current mode */ + pm->current_mode = mode; + + /* current mode is higher run mode */ + if (mode < PM_SLEEP_MODE_START) + { + /* enter run mode */ + pm->ops->enter(pm); +#if PM_RUN_MODE_COUNT > 1 + /* frequency change */ + pm->ops->frequency_change(pm, 0); + _pm_device_frequency_change(); +#endif + } + else + { + /* do nothing when request higher sleep mode, + * and swithc to new sleep mode in rt_pm_enter() + */ + } + } + + rt_hw_interrupt_enable(level); +} + +/** + * Upper application or device driver releases the stall + * of corresponding power mode. + * + * @param parameter the parameter of run mode or sleep mode + * + */ +void rt_pm_release(rt_ubase_t mode) +{ + rt_ubase_t level; + struct rt_pm *pm; + + pm = &_pm; + + if (mode > PM_MODE_MAX) + return; + + level = rt_hw_interrupt_disable(); + + if (pm->modes[mode] > 0) + pm->modes[mode] --; + + rt_hw_interrupt_enable(level); +} + +/** + * Register a device with PM feature + * + * @param device the device with PM feature + * @param ops the PM ops for device + */ +void rt_pm_register_device(struct rt_device *device, const struct rt_device_pm_ops *ops) +{ + rt_ubase_t level; + struct rt_device_pm *device_pm; + + RT_DEBUG_NOT_IN_INTERRUPT; + + level = rt_hw_interrupt_disable(); + + device_pm = (struct rt_device_pm *)RT_KERNEL_REALLOC(_pm.device_pm, + (_pm.device_pm_number + 1) * sizeof(struct rt_device_pm)); + if (device_pm != RT_NULL) + { + _pm.device_pm = device_pm; + _pm.device_pm[_pm.device_pm_number].device = device; + _pm.device_pm[_pm.device_pm_number].ops = ops; + _pm.device_pm_number += 1; + } + + rt_sem_release(&(_pm.device_lock)); + + rt_hw_interrupt_enable(level); +} + +/** + * Unregister device from PM manager. + * + * @param device the device with PM feature + */ +void rt_pm_unregister_device(struct rt_device *device) +{ + rt_ubase_t level; + rt_uint32_t index; + RT_DEBUG_NOT_IN_INTERRUPT; + + level = rt_hw_interrupt_disable(); + + for (index = 0; index < _pm.device_pm_number; index ++) + { + if (_pm.device_pm[index].device == device) + { + /* remove current entry */ + for (; index < _pm.device_pm_number - 1; index ++) + { + _pm.device_pm[index] = _pm.device_pm[index + 1]; + } + + _pm.device_pm[_pm.device_pm_number - 1].device = RT_NULL; + _pm.device_pm[_pm.device_pm_number - 1].ops = RT_NULL; + + _pm.device_pm_number -= 1; + /* break out and not touch memory */ + break; + } + } + + rt_hw_interrupt_enable(level); +} + +/** + * RT-Thread device interface for PM device + */ +static rt_size_t _rt_pm_device_read(rt_device_t dev, + rt_off_t pos, + void *buffer, + rt_size_t size) +{ + struct rt_pm *pm; + rt_size_t length; + + length = 0; + pm = (struct rt_pm *)dev; + RT_ASSERT(pm != RT_NULL); + + if (pos <= PM_MODE_MAX) + { + int mode; + + mode = pm->modes[pos]; + length = rt_snprintf(buffer, size, "%d", mode); + } + + return length; +} + +static rt_size_t _rt_pm_device_write(rt_device_t dev, + rt_off_t pos, + const void *buffer, + rt_size_t size) +{ + unsigned char request; + + if (size) + { + /* get request */ + request = *(unsigned char *)buffer; + if (request == '1') + { + rt_pm_request(pos); + } + else if (request == '0') + { + rt_pm_release(pos); + } + } + + return 1; +} + +static rt_err_t _rt_pm_device_control(rt_device_t dev, + int cmd, + void *args) +{ + rt_uint32_t mode; + + switch (cmd) + { + case RT_PM_DEVICE_CTRL_REQUEST: + mode = (rt_uint32_t)args; + rt_pm_request(mode); + break; + + case RT_PM_DEVICE_CTRL_RELEASE: + mode = (rt_uint32_t)args; + rt_pm_release(mode); + break; + } + + return RT_EOK; +} + +/** + * This function will initialize power management. + * + * @param ops the PM operations. + * @param timer_mask indicates which mode has timer feature. + * @param user_data user data + */ +void rt_system_pm_init(const struct rt_pm_ops *ops, + rt_uint8_t timer_mask, + void *user_data) +{ + struct rt_device *device; + struct rt_pm *pm; + + pm = &_pm; + device = &(_pm.parent); + + device->type = RT_Device_Class_PM; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + + device->init = RT_NULL; + device->open = RT_NULL; + device->close = RT_NULL; + device->read = _rt_pm_device_read; + device->write = _rt_pm_device_write; + device->control = _rt_pm_device_control; + device->user_data = user_data; + + /* register PM device to the system */ + rt_device_register(device, "pm", RT_DEVICE_FLAG_RDWR); + + /* todo : add to kernel source code */ + rt_thread_idle_sethook(rt_pm_enter); + + rt_memset(pm->modes, 0, sizeof(pm->modes)); + pm->current_mode = PM_RUN_MODE_DEFAULT; + + pm->timer_mask = timer_mask; + + pm->ops = ops; + + pm->device_pm = RT_NULL; + pm->device_pm_number = 0; + + /* initialize semaphore */ + rt_sem_init(&(pm->device_lock), "pm", 1, RT_IPC_FLAG_FIFO); + + /* request in default running mode */ + rt_pm_request(PM_RUN_MODE_DEFAULT); + +#ifdef PM_SLEEP_MODE_DEFAULT + /* request in default sleep mode */ + rt_pm_request(PM_SLEEP_MODE_DEFAULT); +#endif + + /* must hold on deep shutdown mode */ + rt_pm_request(PM_MODE_MAX); +} + +#ifdef RT_USING_FINSH +#include <finsh.h> + +static void rt_pm_release_mode(int argc, char **argv) +{ + int mode = 0; + if (argc >= 2) + { + mode = atoi(argv[1]); + } + + rt_pm_release(mode); +} +MSH_CMD_EXPORT_ALIAS(rt_pm_release_mode, pm_release, release power management mode); + +static void rt_pm_request_mode(int argc, char **argv) +{ + int mode = 0; + if (argc >= 2) + { + mode = atoi(argv[1]); + } + + rt_pm_request(mode); +} +MSH_CMD_EXPORT_ALIAS(rt_pm_request_mode, pm_request, request power management mode); + +static void rt_pm_dump_status(void) +{ + static const char *pm_str[] = PM_MODE_NAMES; + rt_uint32_t index; + struct rt_pm *pm; + + pm = &_pm; + + rt_kprintf("| Power Management Mode | Counter | Timer |\n"); + rt_kprintf("+-----------------------+---------+-------+\n"); + for (index = 0; index <= PM_MODE_MAX; index ++) + { + int has_timer = 0; + if (pm->timer_mask & (1 << index)) + has_timer = 1; + + rt_kprintf("| %021s | %7d | %5d |\n", pm_str[index], pm->modes[index], has_timer); + } + rt_kprintf("+-----------------------+---------+-------+\n"); + + rt_kprintf("pm current mode: %s\n", pm_str[pm->current_mode]); +} +FINSH_FUNCTION_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status); +MSH_CMD_EXPORT_ALIAS(rt_pm_dump_status, pm_dump, dump power management status); +#endif + +#endif /* RT_USING_PM */ diff --git a/components/drivers/rtc/alarm.c b/components/drivers/rtc/alarm.c index 6660128603..c497ebf727 100644 --- a/components/drivers/rtc/alarm.c +++ b/components/drivers/rtc/alarm.c @@ -1,21 +1,7 @@ /* - * File : alarm.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/rtc/rtc.c b/components/drivers/rtc/rtc.c index 000358c111..e17c0fe7dd 100644 --- a/components/drivers/rtc/rtc.c +++ b/components/drivers/rtc/rtc.c @@ -1,21 +1,7 @@ /* - * File : rtc.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/rtc/soft_rtc.c b/components/drivers/rtc/soft_rtc.c index c828e43ebc..adcbf81fe3 100644 --- a/components/drivers/rtc/soft_rtc.c +++ b/components/drivers/rtc/soft_rtc.c @@ -1,21 +1,7 @@ /* - * File : soft_rtc.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/sdio/block_dev.c b/components/drivers/sdio/block_dev.c index 14a01acd90..23be319fc6 100644 --- a/components/drivers/sdio/block_dev.c +++ b/components/drivers/sdio/block_dev.c @@ -1,21 +1,7 @@ /* - * File : block_dev.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -27,8 +13,20 @@ #include <drivers/mmcsd_core.h> +#define DBG_ENABLE +#define DBG_SECTION_NAME "SDIO" +#ifdef RT_SDIO_DEBUG +#define DBG_LEVEL DBG_LOG +#else +#define DBG_LEVEL DBG_INFO +#endif /* RT_SDIO_DEBUG */ +#define DBG_COLOR +#include <rtdbg.h> + static rt_list_t blk_devices = RT_LIST_OBJECT_INIT(blk_devices); +#define BLK_MIN(a, b) ((a) < (b) ? (a) : (b)) + struct mmcsd_blk_device { struct rt_mmcsd_card *card; @@ -36,6 +34,7 @@ struct mmcsd_blk_device struct rt_device dev; struct dfs_partition part; struct rt_device_blk_geometry geometry; + rt_size_t max_req_size; }; #ifndef RT_MMCSD_MAX_PARTITION @@ -179,7 +178,7 @@ static rt_err_t rt_mmcsd_req_blk(struct rt_mmcsd_card *card, err = mmcsd_send_cmd(card->host, &cmd, 5); if (err) { - rt_kprintf("error %d requesting status\n", err); + LOG_E("error %d requesting status", err); break; } /* @@ -195,8 +194,8 @@ static rt_err_t rt_mmcsd_req_blk(struct rt_mmcsd_card *card, if (cmd.err || data.err || stop.err) { - rt_kprintf("mmcsd request blocks error\n"); - rt_kprintf("%d,%d,%d, 0x%08x,0x%08x\n", + LOG_E("mmcsd request blocks error"); + LOG_E("%d,%d,%d, 0x%08x,0x%08x", cmd.err, data.err, stop.err, data.flags, sector); return -RT_ERROR; @@ -239,19 +238,31 @@ static rt_size_t rt_mmcsd_read(rt_device_t dev, void *buffer, rt_size_t size) { - rt_err_t err; + rt_err_t err = 0; + rt_size_t offset = 0; + rt_size_t req_size = 0; + rt_size_t remain_size = size; + void *rd_ptr = (void *)buffer; struct mmcsd_blk_device *blk_dev = (struct mmcsd_blk_device *)dev->user_data; struct dfs_partition *part = &blk_dev->part; if (dev == RT_NULL) { rt_set_errno(-EINVAL); - return 0; } rt_sem_take(part->lock, RT_WAITING_FOREVER); - err = rt_mmcsd_req_blk(blk_dev->card, part->offset + pos, buffer, size, 0); + while (remain_size) + { + req_size = (remain_size > blk_dev->max_req_size) ? blk_dev->max_req_size : remain_size; + err = rt_mmcsd_req_blk(blk_dev->card, part->offset + pos + offset, rd_ptr, req_size, 0); + if (err) + break; + offset += req_size; + rd_ptr = (void *)((rt_uint8_t *)rd_ptr + (req_size << 9)); + remain_size -= req_size; + } rt_sem_release(part->lock); /* the length of reading must align to SECTOR SIZE */ @@ -260,7 +271,7 @@ static rt_size_t rt_mmcsd_read(rt_device_t dev, rt_set_errno(-EIO); return 0; } - return size; + return size - remain_size; } static rt_size_t rt_mmcsd_write(rt_device_t dev, @@ -268,19 +279,31 @@ static rt_size_t rt_mmcsd_write(rt_device_t dev, const void *buffer, rt_size_t size) { - rt_err_t err; + rt_err_t err = 0; + rt_size_t offset = 0; + rt_size_t req_size = 0; + rt_size_t remain_size = size; + void *wr_ptr = (void *)buffer; struct mmcsd_blk_device *blk_dev = (struct mmcsd_blk_device *)dev->user_data; struct dfs_partition *part = &blk_dev->part; if (dev == RT_NULL) { rt_set_errno(-EINVAL); - return 0; } rt_sem_take(part->lock, RT_WAITING_FOREVER); - err = rt_mmcsd_req_blk(blk_dev->card, part->offset + pos, (void *)buffer, size, 1); + while (remain_size) + { + req_size = (remain_size > blk_dev->max_req_size) ? blk_dev->max_req_size : remain_size; + err = rt_mmcsd_req_blk(blk_dev->card, part->offset + pos + offset, wr_ptr, req_size, 1); + if (err) + break; + offset += req_size; + wr_ptr = (void *)((rt_uint8_t *)wr_ptr + (req_size << 9)); + remain_size -= req_size; + } rt_sem_release(part->lock); /* the length of reading must align to SECTOR SIZE */ @@ -290,7 +313,7 @@ static rt_size_t rt_mmcsd_write(rt_device_t dev, return 0; } - return size; + return size - remain_size; } static rt_int32_t mmcsd_set_blksize(struct rt_mmcsd_card *card) @@ -311,7 +334,7 @@ static rt_int32_t mmcsd_set_blksize(struct rt_mmcsd_card *card) if (err) { - rt_kprintf("MMCSD: unable to set block size to %d: %d\n", cmd.arg, err); + LOG_E("MMCSD: unable to set block size to %d: %d", cmd.arg, err); return -RT_ERROR; } @@ -346,13 +369,13 @@ rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card) return err; } - rt_kprintf("probe mmcsd block device!\n"); + LOG_I("probe mmcsd block device!"); /* get the first sector to read partition table */ sector = (rt_uint8_t *)rt_malloc(SECTOR_SIZE); if (sector == RT_NULL) { - rt_kprintf("allocate partition sector buffer failed\n"); + LOG_E("allocate partition sector buffer failed!"); return -RT_ENOMEM; } @@ -365,10 +388,15 @@ rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card) blk_dev = rt_calloc(1, sizeof(struct mmcsd_blk_device)); if (!blk_dev) { - rt_kprintf("mmcsd:malloc memory failed!\n"); + LOG_E("mmcsd:malloc memory failed!"); break; } + blk_dev->max_req_size = BLK_MIN((card->host->max_dma_segs * + card->host->max_seg_size) >> 9, + (card->host->max_blk_count * + card->host->max_blk_size) >> 9); + /* get the first partition */ status = dfs_filesystem_get_partition(&blk_dev->part, sector, i); if (status == RT_EOK) @@ -448,7 +476,7 @@ rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card) #ifdef RT_USING_DFS_MNTTABLE if (0) // if (blk_dev) { - rt_kprintf("try to mount file system!\n"); + LOG_I("try to mount file system!"); /* try to mount file system on this block device */ dfs_mount_device(&(blk_dev->dev)); } @@ -457,7 +485,7 @@ rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card) } else { - rt_kprintf("read mmcsd first sector failed\n"); + LOG_E("read mmcsd first sector failed"); err = -RT_ERROR; } diff --git a/components/drivers/sdio/mmc.c b/components/drivers/sdio/mmc.c index 138d9565a1..bbf0ff838c 100644 --- a/components/drivers/sdio/mmc.c +++ b/components/drivers/sdio/mmc.c @@ -1,21 +1,7 @@ /* - * File : mmc.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -25,6 +11,16 @@ #include <drivers/mmcsd_core.h> #include <drivers/mmc.h> +#define DBG_ENABLE +#define DBG_SECTION_NAME "SDIO" +#ifdef RT_SDIO_DEBUG +#define DBG_LEVEL DBG_LOG +#else +#define DBG_LEVEL DBG_INFO +#endif /* RT_SDIO_DEBUG */ +#define DBG_COLOR +#include <rtdbg.h> + static const rt_uint32_t tran_unit[] = { 10000, 100000, 1000000, 10000000, @@ -81,7 +77,7 @@ static rt_int32_t mmcsd_parse_csd(struct rt_mmcsd_card *card) */ csd->csd_structure = GET_BITS(resp, 126, 2); if (csd->csd_structure == 0) { - rt_kprintf("unrecognised CSD structure version %d\n", csd->csd_structure); + LOG_E("unrecognised CSD structure version %d!", csd->csd_structure); return -RT_ERROR; } @@ -137,7 +133,7 @@ static int mmc_get_ext_csd(struct rt_mmcsd_card *card, rt_uint8_t **new_ext_csd) */ ext_csd = rt_malloc(512); if (!ext_csd) { - rt_kprintf("alloc memory failed when get ext csd\n"); + LOG_E("alloc memory failed when get ext csd!"); return -RT_ENOMEM; } @@ -188,13 +184,19 @@ static int mmc_get_ext_csd(struct rt_mmcsd_card *card, rt_uint8_t **new_ext_csd) */ static int mmc_parse_ext_csd(struct rt_mmcsd_card *card, rt_uint8_t *ext_csd) { + if(card == RT_NULL || ext_csd == RT_NULL) + { + LOG_E("emmc parse ext csd fail, invaild args"); + return -1; + } + card->flags |= CARD_FLAG_HIGHSPEED; card->hs_max_data_rate = 200000000; card->card_capacity = *((rt_uint32_t *)&ext_csd[EXT_CSD_SEC_CNT]); card->card_capacity *= card->card_blksize; card->card_capacity >>= 10; /* unit:KB */ - rt_kprintf("emmc card capacity %d KB\n", card->card_capacity); + LOG_I("emmc card capacity %d KB.", card->card_capacity); return 0; } @@ -333,13 +335,13 @@ static int mmc_select_bus_width(struct rt_mmcsd_card *card, rt_uint8_t *ext_csd) } else { switch(ext_csd_bits[idx]){ case 0: - rt_kprintf("switch to bus width 1 bit failed\n"); + LOG_E("switch to bus width 1 bit failed!"); break; case 1: - rt_kprintf("switch to bus width 4 bit failed\n"); + LOG_E("switch to bus width 4 bit failed!"); break; case 2: - rt_kprintf("switch to bus width 8 bit failed\n"); + LOG_E("switch to bus width 8 bit failed!"); break; default: break; @@ -414,7 +416,7 @@ static rt_int32_t mmcsd_mmc_init_card(struct rt_mmcsd_host *host, { rt_int32_t err; rt_uint32_t resp[4]; - rt_uint32_t rocr; + rt_uint32_t rocr = 0; rt_uint32_t max_data_rate; rt_uint8_t *ext_csd = RT_NULL; struct rt_mmcsd_card *card = RT_NULL; @@ -443,7 +445,7 @@ static rt_int32_t mmcsd_mmc_init_card(struct rt_mmcsd_host *host, card = rt_malloc(sizeof(struct rt_mmcsd_card)); if (!card) { - rt_kprintf("malloc card failed\n"); + LOG_E("malloc card failed!"); err = -RT_ENOMEM; goto err; } @@ -580,7 +582,7 @@ remove_card: host->card = RT_NULL; err: - rt_kprintf("init MMC card failed\n"); + LOG_E("init MMC card failed!"); return err; } diff --git a/components/drivers/sdio/mmcsd_core.c b/components/drivers/sdio/mmcsd_core.c index edf4002ac7..d1f5978374 100644 --- a/components/drivers/sdio/mmcsd_core.c +++ b/components/drivers/sdio/mmcsd_core.c @@ -1,21 +1,7 @@ /* - * File : mmcsd_core.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -28,6 +14,16 @@ #include <drivers/mmc.h> #include <drivers/sdio.h> +#define DBG_ENABLE +#define DBG_SECTION_NAME "SDIO" +#ifdef RT_SDIO_DEBUG +#define DBG_LEVEL DBG_LOG +#else +#define DBG_LEVEL DBG_INFO +#endif /* RT_SDIO_DEBUG */ +#define DBG_COLOR +#include <rtdbg.h> + #ifndef RT_MMCSD_STACK_SIZE #define RT_MMCSD_STACK_SIZE 1024 #endif @@ -206,7 +202,7 @@ rt_int32_t mmcsd_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid) buf = (rt_uint32_t *)rt_malloc(16); if (!buf) { - rt_kprintf("allocate memory failed\n"); + LOG_E("allocate memory failed!"); return -RT_ENOMEM; } @@ -282,7 +278,7 @@ rt_int32_t mmcsd_get_csd(struct rt_mmcsd_card *card, rt_uint32_t *csd) buf = (rt_uint32_t*)rt_malloc(16); if (!buf) { - rt_kprintf("allocate memory failed\n"); + LOG_E("allocate memory failed!"); return -RT_ENOMEM; } @@ -418,7 +414,7 @@ void mmcsd_set_clock(struct rt_mmcsd_host *host, rt_uint32_t clk) { if (clk < host->freq_min) { - rt_kprintf("clock too low\n"); + LOG_W("clock too low!"); } host->io_cfg.clock = clk; @@ -539,7 +535,7 @@ rt_uint32_t mmcsd_select_voltage(struct rt_mmcsd_host *host, rt_uint32_t ocr) } else { - rt_kprintf("host doesn't support card's voltages\n"); + LOG_W("host doesn't support card's voltages!"); ocr = 0; } @@ -679,7 +675,7 @@ void mmcsd_detect(void *param) mmcsd_host_lock(host); if (host->card->sdio_function_num != 0) { - rt_kprintf("unsupport sdio card plug out!\n"); + LOG_W("unsupport sdio card plug out!"); } else { @@ -702,7 +698,7 @@ struct rt_mmcsd_host *mmcsd_alloc_host(void) host = rt_malloc(sizeof(struct rt_mmcsd_host)); if (!host) { - rt_kprintf("alloc host failed\n"); + LOG_E("alloc host failed"); return RT_NULL; } diff --git a/components/drivers/sdio/sd.c b/components/drivers/sdio/sd.c index a42aa17531..69a4561fb0 100644 --- a/components/drivers/sdio/sd.c +++ b/components/drivers/sdio/sd.c @@ -1,21 +1,7 @@ /* - * File : sd.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -25,6 +11,16 @@ #include <drivers/mmcsd_core.h> #include <drivers/sd.h> +#define DBG_ENABLE +#define DBG_SECTION_NAME "SDIO" +#ifdef RT_SDIO_DEBUG +#define DBG_LEVEL DBG_LOG +#else +#define DBG_LEVEL DBG_INFO +#endif /* RT_SDIO_DEBUG */ +#define DBG_COLOR +#include <rtdbg.h> + static const rt_uint32_t tran_unit[] = { 10000, 100000, 1000000, 10000000, @@ -167,11 +163,11 @@ static rt_int32_t mmcsd_parse_csd(struct rt_mmcsd_card *card) #endif break; default: - rt_kprintf("unrecognised CSD structure version %d\n", csd->csd_structure); + LOG_E("unrecognised CSD structure version %d!", csd->csd_structure); return -RT_ERROR; } - rt_kprintf("SD card capacity %d KB\n", card->card_capacity); + LOG_I("SD card capacity %d KB.", card->card_capacity); return 0; } @@ -201,7 +197,7 @@ static rt_int32_t mmcsd_switch(struct rt_mmcsd_card *card) buf = (rt_uint8_t*)rt_malloc(64); if (!buf) { - rt_kprintf("alloc memory failed\n"); + LOG_E("alloc memory failed!"); return -RT_ENOMEM; } @@ -270,7 +266,7 @@ static rt_int32_t mmcsd_switch(struct rt_mmcsd_card *card) if ((buf[16] & 0xF) != 1) { - rt_kprintf("switching card to high speed failed\n"); + LOG_E("switching card to high speed failed!"); goto err; } @@ -588,7 +584,7 @@ static rt_int32_t mmcsd_sd_init_card(struct rt_mmcsd_host *host, card = rt_malloc(sizeof(struct rt_mmcsd_card)); if (!card) { - rt_kprintf("malloc card failed\n"); + LOG_E("malloc card failed!"); err = -RT_ENOMEM; goto err; } @@ -703,9 +699,9 @@ rt_int32_t init_sd(struct rt_mmcsd_host *host, rt_uint32_t ocr) if (ocr & VDD_165_195) { - rt_kprintf(" SD card claims to support the " + LOG_I(" SD card claims to support the " "incompletely defined 'low voltage range'. This " - "will be ignored.\n"); + "will be ignored."); ocr &= ~VDD_165_195; } @@ -743,7 +739,7 @@ remove_card: host->card = RT_NULL; err: - rt_kprintf("init SD card failed\n"); + LOG_D("init SD card failed!"); return err; } diff --git a/components/drivers/sdio/sdio.c b/components/drivers/sdio/sdio.c index b105e65109..6045c4ade8 100644 --- a/components/drivers/sdio/sdio.c +++ b/components/drivers/sdio/sdio.c @@ -1,21 +1,7 @@ /* - * File : sdio.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -26,6 +12,16 @@ #include <drivers/sdio.h> #include <drivers/sd.h> +#define DBG_ENABLE +#define DBG_SECTION_NAME "SDIO" +#ifdef RT_SDIO_DEBUG +#define DBG_LEVEL DBG_LOG +#else +#define DBG_LEVEL DBG_INFO +#endif /* RT_SDIO_DEBUG */ +#define DBG_COLOR +#include <rtdbg.h> + #ifndef RT_SDIO_STACK_SIZE #define RT_SDIO_STACK_SIZE 512 #endif @@ -130,6 +126,7 @@ rt_int32_t sdio_io_rw_direct(struct rt_mmcsd_card *card, RT_ASSERT(card != RT_NULL); RT_ASSERT(fn <= SDIO_MAX_FUNCTIONS); + RT_ASSERT(pdata != RT_NULL); if (reg_addr & ~SDIO_ARG_CMD53_REG_MASK) return -RT_ERROR; @@ -306,7 +303,7 @@ rt_uint8_t sdio_io_readb(struct rt_sdio_function *func, rt_uint32_t reg, rt_int32_t *err) { - rt_uint8_t data; + rt_uint8_t data = 0; rt_int32_t ret; ret = sdio_io_rw_direct(func->card, 0, func->num, reg, &data, 0); @@ -432,7 +429,7 @@ static rt_int32_t sdio_read_cccr(struct rt_mmcsd_card *card) if (cccr_version > SDIO_CCCR_REV_3_00) { - rt_kprintf("unrecognised CCCR structure version %d\n", cccr_version); + LOG_E("unrecognised CCCR structure version %d", cccr_version); return -RT_ERROR; } @@ -579,7 +576,7 @@ static rt_int32_t sdio_read_cis(struct rt_sdio_function *func) case CISTPL_MANFID: if (tpl_link < 4) { - rt_kprintf("bad CISTPL_MANFID length\n"); + LOG_D("bad CISTPL_MANFID length"); break; } if (func->num != 0) @@ -605,15 +602,15 @@ static rt_int32_t sdio_read_cis(struct rt_sdio_function *func) if (ret) { - rt_kprintf("bad CISTPL_FUNCE size %u " - "type %u\n", tpl_link, curr->data[0]); + LOG_D("bad CISTPL_FUNCE size %u " + "type %u", tpl_link, curr->data[0]); } break; case CISTPL_VERS_1: if (tpl_link < 2) { - rt_kprintf("CISTPL_VERS_1 too short\n"); + LOG_D("CISTPL_VERS_1 too short"); } break; default: @@ -623,7 +620,7 @@ static rt_int32_t sdio_read_cis(struct rt_sdio_function *func) curr->size = tpl_link; *prev = curr; prev = &curr->next; - rt_kprintf( "function %d, CIS tuple code %#x, length %d\n", + LOG_D( "function %d, CIS tuple code %#x, length %d", func->num, tpl_code, tpl_link); break; } @@ -686,7 +683,6 @@ err: return ret; } - static rt_int32_t sdio_initialize_function(struct rt_mmcsd_card *card, rt_uint32_t func_num) { @@ -698,7 +694,7 @@ static rt_int32_t sdio_initialize_function(struct rt_mmcsd_card *card, func = rt_malloc(sizeof(struct rt_sdio_function)); if (!func) { - rt_kprintf("malloc rt_sdio_function failed\n"); + LOG_E("malloc rt_sdio_function failed"); ret = -RT_ENOMEM; goto err; } @@ -788,7 +784,7 @@ static rt_int32_t sdio_register_card(struct rt_mmcsd_card *card) sc = rt_malloc(sizeof(struct sdio_card)); if (sc == RT_NULL) { - rt_kprintf("malloc sdio card failed\n"); + LOG_E("malloc sdio card failed"); return -RT_ENOMEM; } @@ -836,7 +832,7 @@ static rt_int32_t sdio_init_card(struct rt_mmcsd_host *host, rt_uint32_t ocr) card = rt_malloc(sizeof(struct rt_mmcsd_card)); if (!card) { - rt_kprintf("malloc card failed\n"); + LOG_E("malloc card failed"); err = -RT_ENOMEM; goto err; } @@ -850,7 +846,7 @@ static rt_int32_t sdio_init_card(struct rt_mmcsd_host *host, rt_uint32_t ocr) card->sdio_function[0] = rt_malloc(sizeof(struct rt_sdio_function)); if (!card->sdio_function[0]) { - rt_kprintf("malloc sdio_func0 failed\n"); + LOG_E("malloc sdio_func0 failed"); err = -RT_ENOMEM; goto err1; } @@ -928,6 +924,7 @@ err3: host->card->sdio_function[i] = RT_NULL; rt_free(host->card); host->card = RT_NULL; + break; } } } @@ -944,7 +941,7 @@ err1: rt_free(host->card); } err: - rt_kprintf("error %d while initialising SDIO card\n", err); + LOG_E("error %d while initialising SDIO card", err); return err; } @@ -958,13 +955,13 @@ rt_int32_t init_sdio(struct rt_mmcsd_host *host, rt_uint32_t ocr) if (ocr & 0x7F) { - rt_kprintf("Card ocr below the defined voltage rang.\n"); + LOG_W("Card ocr below the defined voltage rang."); ocr &= ~0x7F; } if (ocr & VDD_165_195) { - rt_kprintf("Can't support the low voltage SDIO card.\n"); + LOG_W("Can't support the low voltage SDIO card."); ocr &= ~VDD_165_195; } @@ -987,7 +984,7 @@ remove_card: host->card = RT_NULL; err: - rt_kprintf("init SDIO card failed\n"); + LOG_E("init SDIO card failed"); return err; } @@ -1341,7 +1338,7 @@ rt_int32_t sdio_register_driver(struct rt_sdio_driver *driver) sd = rt_malloc(sizeof(struct sdio_driver)); if (sd == RT_NULL) { - rt_kprintf("malloc sdio driver failed\n"); + LOG_E("malloc sdio driver failed"); return -RT_ENOMEM; } @@ -1378,7 +1375,7 @@ rt_int32_t sdio_unregister_driver(struct rt_sdio_driver *driver) if (sd == RT_NULL) { - rt_kprintf("SDIO driver %s not register\n", driver->name); + LOG_E("SDIO driver %s not register", driver->name); return -RT_ERROR; } diff --git a/components/drivers/sensors/bmi055_sensor.cpp b/components/drivers/sensors/bmi055_sensor.cpp index 705895f368..b6e7f99da5 100644 --- a/components/drivers/sensors/bmi055_sensor.cpp +++ b/components/drivers/sensors/bmi055_sensor.cpp @@ -1,11 +1,7 @@ /* - * File : bmi055_sensor.cpp - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2014, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -137,7 +133,7 @@ BMI055_Accelerometer::BMI055_Accelerometer(const char* iic_name, int addr) write_reg(BMI055_BGW_SOFTRESET, 0xB6); /* reset of the sensor P57 */ write_reg(BMI055_PMU_LPW, 0x00); /* PMU_LPW NORMAL mode P55 */ write_reg(BMI055_PMU_BW, 0x0A); /* 01010b 31.25 Hz P55 */ - write_reg(BMI055_PMU_RANGE, 0x05); /* 0101b ±4g range PMU_RANGE set acc +-4g/s P54 */ + write_reg(BMI055_PMU_RANGE, 0x05); /* 0101b 卤4g range PMU_RANGE set acc +-4g/s P54 */ x_offset = y_offset = z_offset = 0; diff --git a/components/drivers/sensors/bmi055_sensor.h b/components/drivers/sensors/bmi055_sensor.h index 7ec6d9e5cf..713aa74ad0 100644 --- a/components/drivers/sensors/bmi055_sensor.h +++ b/components/drivers/sensors/bmi055_sensor.h @@ -1,11 +1,7 @@ /* - * File : bmi055_sensor.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2014, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/sensors/mpu6050_sensor.cpp b/components/drivers/sensors/mpu6050_sensor.cpp index 8f6557a7ce..358796bc30 100644 --- a/components/drivers/sensors/mpu6050_sensor.cpp +++ b/components/drivers/sensors/mpu6050_sensor.cpp @@ -1,11 +1,7 @@ /* - * File : MPU6050_sensor.cpp - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2014, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/sensors/mpu6050_sensor.h b/components/drivers/sensors/mpu6050_sensor.h index c7e28ba28e..8f70c3e5ed 100644 --- a/components/drivers/sensors/mpu6050_sensor.h +++ b/components/drivers/sensors/mpu6050_sensor.h @@ -1,11 +1,7 @@ /* - * File : MPU6050_sensor.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2014, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/sensors/sensor.cpp b/components/drivers/sensors/sensor.cpp index abeecdb883..f986510f63 100644 --- a/components/drivers/sensors/sensor.cpp +++ b/components/drivers/sensors/sensor.cpp @@ -1,11 +1,7 @@ /* - * File : sensors.cpp - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2014, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/sensors/sensor.h b/components/drivers/sensors/sensor.h index fd44611f5a..916360d34b 100644 --- a/components/drivers/sensors/sensor.h +++ b/components/drivers/sensors/sensor.h @@ -1,11 +1,7 @@ /* - * File : sensors.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2014, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/serial/serial.c b/components/drivers/serial/serial.c index 856e33e979..959d8444c1 100644 --- a/components/drivers/serial/serial.c +++ b/components/drivers/serial/serial.c @@ -1,21 +1,7 @@ /* - * File : serial.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -84,19 +70,19 @@ static int serial_fops_open(struct dfs_fd *fd) switch (fd->flags & O_ACCMODE) { case O_RDONLY: - dbg_log(DBG_LOG, "fops open: O_RDONLY!\n"); + LOG_D("fops open: O_RDONLY!"); flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDONLY; break; case O_WRONLY: - dbg_log(DBG_LOG, "fops open: O_WRONLY!\n"); + LOG_D("fops open: O_WRONLY!"); flags = RT_DEVICE_FLAG_WRONLY; break; case O_RDWR: - dbg_log(DBG_LOG, "fops open: O_RDWR!\n"); + LOG_D("fops open: O_RDWR!"); flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDWR; break; default: - dbg_log(DBG_ERROR, "fops open: unknown mode - %d!\n", fd->flags & O_ACCMODE); + LOG_E("fops open: unknown mode - %d!", fd->flags & O_ACCMODE); break; } @@ -573,7 +559,7 @@ static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag) RT_ASSERT(dev != RT_NULL); serial = (struct rt_serial_device *)dev; - dbg_log(DBG_LOG, "open serial device: 0x%08x with open flag: 0x%04x\n", + LOG_D("open serial device: 0x%08x with open flag: 0x%04x", dev, oflag); /* check device flag with the open flag */ if ((oflag & RT_DEVICE_FLAG_DMA_RX) && !(dev->flag & RT_DEVICE_FLAG_DMA_RX)) diff --git a/components/drivers/spi/enc28j60.c b/components/drivers/spi/enc28j60.c index f579dd8fdd..a8bfe3a159 100644 --- a/components/drivers/spi/enc28j60.c +++ b/components/drivers/spi/enc28j60.c @@ -1,3 +1,11 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ #include "enc28j60.h" /* #define NET_TRACE */ diff --git a/components/drivers/spi/enc28j60.h b/components/drivers/spi/enc28j60.h index 1207efdec6..adff774748 100644 --- a/components/drivers/spi/enc28j60.h +++ b/components/drivers/spi/enc28j60.h @@ -1,3 +1,11 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ #ifndef EN28J60_H_INCLUDED #define EN28J60_H_INCLUDED diff --git a/components/drivers/spi/spi_core.c b/components/drivers/spi/spi_core.c index 4ab843a9fd..9e404d7c94 100644 --- a/components/drivers/spi/spi_core.c +++ b/components/drivers/spi/spi_core.c @@ -1,21 +1,7 @@ /* - * File : spi_core.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -360,7 +346,6 @@ struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device, { /* configure SPI bus failed */ rt_set_errno(-RT_EIO); - result = 0; goto __exit; } } diff --git a/components/drivers/spi/spi_dev.c b/components/drivers/spi/spi_dev.c index 1e69ebe591..e334e7aeb5 100644 --- a/components/drivers/spi/spi_dev.c +++ b/components/drivers/spi/spi_dev.c @@ -1,21 +1,7 @@ /* - * File : spi_dev.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/spi/spi_flash.h b/components/drivers/spi/spi_flash.h index d4beb57b45..30db32474a 100644 --- a/components/drivers/spi/spi_flash.h +++ b/components/drivers/spi/spi_flash.h @@ -1,21 +1,7 @@ /* - * File : spi_flash.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2016, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/spi/spi_flash_at45dbxx.c b/components/drivers/spi/spi_flash_at45dbxx.c index 8798e35cf6..0a30f0a328 100644 --- a/components/drivers/spi/spi_flash_at45dbxx.c +++ b/components/drivers/spi/spi_flash_at45dbxx.c @@ -1,21 +1,7 @@ /* - * File : spi_flash_at45dbxx.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -34,7 +20,7 @@ #define FLASH_TRACE(...) #endif /**< #ifdef FLASH_DEBUG */ -/* JEDEC Manufacturer��s ID */ +/* JEDEC Manufacturer’s ID */ #define MF_ID (0x1F) /* atmel */ #define DENSITY_CODE_011D (0x02) /* AT45DB011D Density Code : 00010 = 1-Mbit */ #define DENSITY_CODE_021D (0x03) /* AT45DB021D Density Code : 00011 = 2-Mbit */ @@ -345,7 +331,7 @@ rt_err_t at45dbxx_init(const char * flash_device_name, const char * spi_device_n /**< 001 = Atmel DataFlash */ if(JEDEC_ID->manufacturer_id != 0x1F || JEDEC_ID->family_code != 0x01) { - FLASH_TRACE("Manufacturer��s ID or Memory Type error!\r\n"); + FLASH_TRACE("Manufacturer’s ID or Memory Type error!\r\n"); FLASH_TRACE("JEDEC Read-ID Data : %02X %02X %02X\r\n", id_recv[0], id_recv[1], id_recv[2]); return -RT_ENOSYS; } diff --git a/components/drivers/spi/spi_flash_at45dbxx.h b/components/drivers/spi/spi_flash_at45dbxx.h index 203a1d3ba9..d1bc1a2aeb 100644 --- a/components/drivers/spi/spi_flash_at45dbxx.h +++ b/components/drivers/spi/spi_flash_at45dbxx.h @@ -1,21 +1,7 @@ /* - * File : spi_flash_at45dbxx.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/spi/spi_flash_gd.c b/components/drivers/spi/spi_flash_gd.c index a8e8f9a4b8..3c52035372 100644 --- a/components/drivers/spi/spi_flash_gd.c +++ b/components/drivers/spi/spi_flash_gd.c @@ -1,22 +1,7 @@ /* - * File : spi_flash_gd.c - * This file is part of RT-Thread RTOS - * Copyright (c) 2016 Shanghai Fullhan Microelectronics Co., Ltd. - * All rights reserved + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/spi/spi_flash_gd.h b/components/drivers/spi/spi_flash_gd.h index 4a12100b74..904d976ed0 100644 --- a/components/drivers/spi/spi_flash_gd.h +++ b/components/drivers/spi/spi_flash_gd.h @@ -1,21 +1,7 @@ /* - * File : spi_flash_gd.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2016, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/spi/spi_flash_sfud.c b/components/drivers/spi/spi_flash_sfud.c index 9859cb0141..c0e42b3cd8 100644 --- a/components/drivers/spi/spi_flash_sfud.c +++ b/components/drivers/spi/spi_flash_sfud.c @@ -1,21 +1,7 @@ /* - * File : spi_flash_sfud.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2016, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -416,14 +402,17 @@ static void sf(uint8_t argc, char **argv) { } else { char *spi_dev_name = argv[2]; rtt_dev_bak = rtt_dev; + + /* delete the old SPI flash device */ + if(rtt_dev_bak) { + rt_sfud_flash_delete(rtt_dev_bak); + } + rtt_dev = rt_sfud_flash_probe("sf_cmd", spi_dev_name); if (!rtt_dev) { return; } - /* already probe then delete the old SPI flash device */ - if(rtt_dev_bak) { - rt_sfud_flash_delete(rtt_dev_bak); - } + sfud_dev = (sfud_flash_t)rtt_dev->user_data; if (sfud_dev->chip.capacity < 1024 * 1024) { rt_kprintf("%d KB %s is current selected device.\n", sfud_dev->chip.capacity / 1024, sfud_dev->name); diff --git a/components/drivers/spi/spi_flash_sfud.h b/components/drivers/spi/spi_flash_sfud.h index 2c7f8e7f54..3173b9786d 100644 --- a/components/drivers/spi/spi_flash_sfud.h +++ b/components/drivers/spi/spi_flash_sfud.h @@ -1,21 +1,7 @@ /* - * File : spi_flash_sfud.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2016, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/spi/spi_flash_sst25vfxx.c b/components/drivers/spi/spi_flash_sst25vfxx.c index 35dcf3fff7..bd618a8e9b 100644 --- a/components/drivers/spi/spi_flash_sst25vfxx.c +++ b/components/drivers/spi/spi_flash_sst25vfxx.c @@ -1,21 +1,7 @@ /* - * File : spi_flash_sst25vfxx.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -33,7 +19,7 @@ #define FLASH_TRACE(...) #endif /* #ifdef FLASH_DEBUG */ -/* JEDEC Manufacturer��s ID */ +/* JEDEC Manufacturer’s ID */ #define MF_ID (0xBF) /* JEDEC Device ID : Memory Type */ #define MT_ID (0x25) @@ -302,7 +288,7 @@ rt_err_t sst25vfxx_init(const char * flash_device_name, const char * spi_device_ if(id_recv[0] != MF_ID || id_recv[1] != MT_ID) { - FLASH_TRACE("Manufacturer��s ID or Memory Type error!\r\n"); + FLASH_TRACE("Manufacturer’s ID or Memory Type error!\r\n"); FLASH_TRACE("JEDEC Read-ID Data : %02X %02X %02X\r\n", id_recv[0], id_recv[1], id_recv[2]); return -RT_ENOSYS; } diff --git a/components/drivers/spi/spi_flash_sst25vfxx.h b/components/drivers/spi/spi_flash_sst25vfxx.h index 2502188b91..7a7283f753 100644 --- a/components/drivers/spi/spi_flash_sst25vfxx.h +++ b/components/drivers/spi/spi_flash_sst25vfxx.h @@ -1,21 +1,7 @@ /* - * File : spi_flash_sst25vxx.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/spi/spi_flash_w25qxx.c b/components/drivers/spi/spi_flash_w25qxx.c index 5a16ffbae2..bba9c0b5cc 100644 --- a/components/drivers/spi/spi_flash_w25qxx.c +++ b/components/drivers/spi/spi_flash_w25qxx.c @@ -1,21 +1,7 @@ /* - * File : spi_flash_w25qxx.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -41,7 +27,7 @@ #define PAGE_SIZE 4096 -/* JEDEC Manufacturer��s ID */ +/* JEDEC Manufacturer ID */ #define MF_ID (0xEF) /* JEDEC Device ID: Memory type and Capacity */ diff --git a/components/drivers/spi/spi_flash_w25qxx.h b/components/drivers/spi/spi_flash_w25qxx.h index 737ca70ed2..14837bc87c 100644 --- a/components/drivers/spi/spi_flash_w25qxx.h +++ b/components/drivers/spi/spi_flash_w25qxx.h @@ -1,21 +1,7 @@ /* - * File : spi_flash_w25qxx.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/spi/spi_flash_w25qxx_mtd.c b/components/drivers/spi/spi_flash_w25qxx_mtd.c index dd3e59f409..9f1ef802f6 100644 --- a/components/drivers/spi/spi_flash_w25qxx_mtd.c +++ b/components/drivers/spi/spi_flash_w25qxx_mtd.c @@ -1,3 +1,11 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ #include <rtthread.h> #include <rtdevice.h> @@ -17,7 +25,7 @@ #define FLASH_TRACE(...) #endif /* #ifdef FLASH_DEBUG */ -/* JEDEC Manufacturer��s ID */ +/* JEDEC Manufacturer’s ID */ #define MF_ID (0xEF) /* JEDEC Device ID: Memory type and Capacity */ #define MTC_W25Q80_BV (0x4014) /* W25Q80BV */ diff --git a/components/drivers/spi/spi_flash_w25qxx_mtd.h b/components/drivers/spi/spi_flash_w25qxx_mtd.h index 9157524673..28f6828ffb 100644 --- a/components/drivers/spi/spi_flash_w25qxx_mtd.h +++ b/components/drivers/spi/spi_flash_w25qxx_mtd.h @@ -1,21 +1,7 @@ /* - * File : spi_flash_w25qxx.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/spi/spi_msd.c b/components/drivers/spi/spi_msd.c index a3046e14ba..ed121e184d 100644 --- a/components/drivers/spi/spi_msd.c +++ b/components/drivers/spi/spi_msd.c @@ -1,12 +1,7 @@ /* - * File : msd.c - * SPI mode SD Card Driver - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/spi/spi_msd.h b/components/drivers/spi/spi_msd.h index 98722711d3..6370170923 100644 --- a/components/drivers/spi/spi_msd.h +++ b/components/drivers/spi/spi_msd.h @@ -1,12 +1,7 @@ /* - * File : msd.h - * SPI mode SD Card Driver - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/spi/spi_wifi_rw009.c b/components/drivers/spi/spi_wifi_rw009.c index 0b92802383..4ddd18ca12 100644 --- a/components/drivers/spi/spi_wifi_rw009.c +++ b/components/drivers/spi/spi_wifi_rw009.c @@ -1,21 +1,7 @@ /* - * File : spi_wifi_rw009.c - * This file is part of RT-Thread RTOS - * Copyright by Shanghai Real-Thread Electronic Technology Co.,Ltd - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * COPYRIGHT (C) 2018, Real-Thread Information Technology Ltd + * + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/spi/spi_wifi_rw009.h b/components/drivers/spi/spi_wifi_rw009.h index 4b684b9b24..049b0a8486 100644 --- a/components/drivers/spi/spi_wifi_rw009.h +++ b/components/drivers/spi/spi_wifi_rw009.h @@ -1,21 +1,7 @@ /* - * File : spi_wifi_rw009.h - * This file is part of RT-Thread RTOS - * Copyright by Shanghai Real-Thread Electronic Technology Co.,Ltd - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * COPYRIGHT (C) 2018, Real-Thread Information Technology Ltd + * + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/src/completion.c b/components/drivers/src/completion.c index ef0b2911a7..9e461e49ce 100644 --- a/components/drivers/src/completion.c +++ b/components/drivers/src/completion.c @@ -1,21 +1,7 @@ /* - * File : completion.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/src/dataqueue.c b/components/drivers/src/dataqueue.c index a10b0805b4..7553986b9f 100644 --- a/components/drivers/src/dataqueue.c +++ b/components/drivers/src/dataqueue.c @@ -1,21 +1,7 @@ /* - * File : dataqueue.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/src/pipe.c b/components/drivers/src/pipe.c index 80d74657e0..065fd21dc1 100644 --- a/components/drivers/src/pipe.c +++ b/components/drivers/src/pipe.c @@ -1,21 +1,7 @@ /* - * File : pipe.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012-2017, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/src/ringblk_buf.c b/components/drivers/src/ringblk_buf.c new file mode 100644 index 0000000000..1a74bf1d24 --- /dev/null +++ b/components/drivers/src/ringblk_buf.c @@ -0,0 +1,520 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-08-25 armink the first version + */ + +#include <rthw.h> +#include <rtthread.h> +#include <rtdevice.h> + +/** + * ring block buffer object initialization + * + * @param rbb ring block buffer object + * @param buf buffer + * @param buf_size buffer size + * @param block_set + * @param blk_max_num + */ +void rt_rbb_init(rt_rbb_t rbb, rt_uint8_t *buf, rt_size_t buf_size, rt_rbb_blk_t block_set, rt_size_t blk_max_num) +{ + rt_size_t i; + + RT_ASSERT(rbb); + RT_ASSERT(buf); + RT_ASSERT(block_set); + + rbb->buf = buf; + rbb->buf_size = buf_size; + rbb->blk_set = block_set; + rbb->blk_max_num = blk_max_num; + rt_slist_init(&rbb->blk_list); + /* initialize block status */ + for (i = 0; i < blk_max_num; i++) + { + block_set[i].status = RT_RBB_BLK_UNUSED; + } +} +RTM_EXPORT(rt_rbb_init); + +/** + * ring block buffer object create + * + * @param buf_size buffer size + * @param blk_max_num max block number + * + * @return != NULL: ring block buffer object + * NULL: create failed + */ +rt_rbb_t rt_rbb_create(rt_size_t buf_size, rt_size_t blk_max_num) +{ + rt_rbb_t rbb = NULL; + rt_uint8_t *buf; + rt_rbb_blk_t blk_set; + + rbb = (rt_rbb_t)rt_malloc(sizeof(struct rt_rbb)); + if (!rbb) + { + return NULL; + } + + buf = (rt_uint8_t *)rt_malloc(buf_size); + if (!buf) + { + rt_free(rbb); + return NULL; + } + + blk_set = (rt_rbb_blk_t)rt_malloc(sizeof(struct rt_rbb_blk) * blk_max_num); + if (!blk_set) + { + rt_free(buf); + rt_free(rbb); + return NULL; + } + + rt_rbb_init(rbb, buf, buf_size, blk_set, blk_max_num); + + return rbb; +} +RTM_EXPORT(rt_rbb_create); + +/** + * ring block buffer object destroy + * + * @param rbb ring block buffer object + */ +void rt_rbb_destroy(rt_rbb_t rbb) +{ + RT_ASSERT(rbb); + + rt_free(rbb); + rt_free(rbb->buf); + rt_free(rbb->blk_set); + +} +RTM_EXPORT(rt_rbb_destroy); + +static rt_rbb_blk_t find_empty_blk_in_set(rt_rbb_t rbb) +{ + rt_size_t i; + + RT_ASSERT(rbb); + + for (i = 0; i < rbb->blk_max_num; i ++) + { + if (rbb->blk_set[i].status == RT_RBB_BLK_UNUSED) + { + return &rbb->blk_set[i]; + } + } + + return NULL; +} + +/** + * Allocate a block by given size. The block will add to blk_list when allocate success. + * + * @param rbb ring block buffer object + * @param blk_size block size + * + * @return != NULL: allocated block + * NULL: allocate failed + */ +rt_rbb_blk_t rt_rbb_blk_alloc(rt_rbb_t rbb, rt_size_t blk_size) +{ + rt_base_t level; + rt_size_t empty1 = 0, empty2 = 0; + rt_rbb_blk_t head, tail, new = NULL; + + RT_ASSERT(rbb); + RT_ASSERT(blk_size < 1L << 24); + + level = rt_hw_interrupt_disable(); + + new = find_empty_blk_in_set(rbb); + + if (rt_slist_len(&rbb->blk_list) < rbb->blk_max_num && new) + { + if (rt_slist_len(&rbb->blk_list) > 0) + { + head = rt_slist_first_entry(&rbb->blk_list, struct rt_rbb_blk, list); + tail = rt_slist_tail_entry(&rbb->blk_list, struct rt_rbb_blk, list); + if (head->buf <= tail->buf) + { + /** + * head tail + * +--------------------------------------+-----------------+------------------+ + * | empty2 | block1 | block2 | block3 | empty1 | + * +--------------------------------------+-----------------+------------------+ + * rbb->buf + */ + empty1 = (rbb->buf + rbb->buf_size) - (tail->buf + tail->size); + empty2 = head->buf - rbb->buf; + + if (empty1 >= blk_size) + { + rt_slist_append(&rbb->blk_list, &new->list); + new->status = RT_RBB_BLK_INITED; + new->buf = tail->buf + tail->size; + new->size = blk_size; + } + else if (empty2 >= blk_size) + { + rt_slist_append(&rbb->blk_list, &new->list); + new->status = RT_RBB_BLK_INITED; + new->buf = rbb->buf; + new->size = blk_size; + } + else + { + /* no space */ + new = NULL; + } + } + else + { + /** + * tail head + * +----------------+-------------------------------------+--------+-----------+ + * | block3 | empty1 | block1 | block2 | + * +----------------+-------------------------------------+--------+-----------+ + * rbb->buf + */ + empty1 = head->buf - (tail->buf + tail->size); + + if (empty1 >= blk_size) + { + rt_slist_append(&rbb->blk_list, &new->list); + new->status = RT_RBB_BLK_INITED; + new->buf = tail->buf + tail->size; + new->size = blk_size; + } + else + { + /* no space */ + new = NULL; + } + } + } + else + { + /* the list is empty */ + rt_slist_append(&rbb->blk_list, &new->list); + new->status = RT_RBB_BLK_INITED; + new->buf = rbb->buf; + new->size = blk_size; + } + } + else + { + new = NULL; + } + + rt_hw_interrupt_enable(level); + + return new; +} +RTM_EXPORT(rt_rbb_blk_alloc); + +/** + * put a block to ring block buffer object + * + * @param block the block + */ +void rt_rbb_blk_put(rt_rbb_blk_t block) +{ + RT_ASSERT(block); + RT_ASSERT(block->status == RT_RBB_BLK_INITED); + + block->status = RT_RBB_BLK_PUT; +} +RTM_EXPORT(rt_rbb_blk_put); + +/** + * get a block from the ring block buffer object + * + * @param rbb ring block buffer object + * + * @return != NULL: block + * NULL: get failed + */ +rt_rbb_blk_t rt_rbb_blk_get(rt_rbb_t rbb) +{ + rt_base_t level; + rt_rbb_blk_t block = NULL; + rt_slist_t *node; + + RT_ASSERT(rbb); + + if (rt_slist_isempty(&rbb->blk_list)) + return 0; + + level = rt_hw_interrupt_disable(); + + for (node = rt_slist_first(&rbb->blk_list); node; node = rt_slist_next(node)) + { + block = rt_slist_entry(node, struct rt_rbb_blk, list); + if (block->status == RT_RBB_BLK_PUT) + { + block->status = RT_RBB_BLK_GET; + goto __exit; + } + } + /* not found */ + block = NULL; + +__exit: + + rt_hw_interrupt_enable(level); + + return block; +} +RTM_EXPORT(rt_rbb_blk_get); + +/** + * free the block + * + * @param rbb ring block buffer object + * @param block the block + */ +void rt_rbb_blk_free(rt_rbb_t rbb, rt_rbb_blk_t block) +{ + rt_base_t level; + + RT_ASSERT(rbb); + RT_ASSERT(block); + RT_ASSERT(block->status != RT_RBB_BLK_UNUSED); + + level = rt_hw_interrupt_disable(); + + /* remove it on rbb block list */ + rt_slist_remove(&rbb->blk_list, &block->list); + + block->status = RT_RBB_BLK_UNUSED; + + rt_hw_interrupt_enable(level); +} +RTM_EXPORT(rt_rbb_blk_free); + +/** + * get a continuous block to queue by given size + * + * tail head + * +------------------+---------------+--------+----------+--------+ + * | block3 | empty1 | block1 | block2 |fragment| + * +------------------+------------------------+----------+--------+ + * |<-- return_size -->| | + * |<--- queue_data_len --->| + * + * tail head + * +------------------+---------------+--------+----------+--------+ + * | block3 | empty1 | block1 | block2 |fragment| + * +------------------+------------------------+----------+--------+ + * |<-- return_size -->| out of len(b1+b2+b3) | + * |<-------------------- queue_data_len -------------------->| + * + * @param rbb ring block buffer object + * @param queue_data_len The max queue data size, and the return size must less then it. + * @param queue continuous block queue + * + * @return the block queue data total size + */ +rt_size_t rt_rbb_blk_queue_get(rt_rbb_t rbb, rt_size_t queue_data_len, rt_rbb_blk_queue_t blk_queue) +{ + rt_base_t level; + rt_size_t data_total_size = 0; + rt_slist_t *node; + rt_rbb_blk_t last_block = NULL, block; + + RT_ASSERT(rbb); + RT_ASSERT(blk_queue); + + if (rt_slist_isempty(&rbb->blk_list)) + return 0; + + level = rt_hw_interrupt_disable(); + + for (node = rt_slist_first(&rbb->blk_list); node; node = rt_slist_next(node)) + { + if (!last_block) + { + last_block = rt_slist_entry(node, struct rt_rbb_blk, list); + if (last_block->status == RT_RBB_BLK_PUT) + { + /* save the first put status block to queue */ + blk_queue->blocks = last_block; + blk_queue->blk_num = 0; + } + else + { + /* the first block must be put status */ + last_block = NULL; + continue; + } + } + else + { + block = rt_slist_entry(node, struct rt_rbb_blk, list); + /* + * these following conditions will break the loop: + * 1. the current block is not put status + * 2. the last block and current block is not continuous + * 3. the data_total_size will out of range + */ + if (block->status != RT_RBB_BLK_PUT || + last_block->buf > block->buf || + data_total_size + block->size > queue_data_len) + { + break; + } + /* backup last block */ + last_block = block; + } + /* remove current block */ + rt_slist_remove(&rbb->blk_list, &last_block->list); + data_total_size += last_block->size; + last_block->status = RT_RBB_BLK_GET; + blk_queue->blk_num++; + } + + rt_hw_interrupt_enable(level); + + return data_total_size; +} +RTM_EXPORT(rt_rbb_blk_queue_get); + +/** + * get all block length on block queue + * + * @param blk_queue the block queue + * + * @return total length + */ +rt_size_t rt_rbb_blk_queue_len(rt_rbb_blk_queue_t blk_queue) +{ + rt_size_t i, data_total_size = 0; + + RT_ASSERT(blk_queue); + + for (i = 0; i < blk_queue->blk_num; i++) + { + data_total_size += blk_queue->blocks[i].size; + } + + return data_total_size; +} +RTM_EXPORT(rt_rbb_blk_queue_len); + +/** + * return the block queue buffer + * + * @param blk_queue the block queue + * + * @return block queue buffer + */ +rt_uint8_t *rt_rbb_blk_queue_buf(rt_rbb_blk_queue_t blk_queue) +{ + RT_ASSERT(blk_queue); + + return blk_queue->blocks[0].buf; +} +RTM_EXPORT(rt_rbb_blk_queue_buf); + +/** + * free the block queue + * + * @param rbb ring block buffer object + * @param blk_queue the block queue + */ +void rt_rbb_blk_queue_free(rt_rbb_t rbb, rt_rbb_blk_queue_t blk_queue) +{ + rt_size_t i; + + RT_ASSERT(rbb); + RT_ASSERT(blk_queue); + + for (i = 0; i < blk_queue->blk_num; i++) + { + rt_rbb_blk_free(rbb, &blk_queue->blocks[i]); + } +} +RTM_EXPORT(rt_rbb_blk_queue_free); + +/** + * The put status and buffer continuous blocks can be make a block queue. + * This function will return the length which from next can be make block queue. + * + * @param rbb ring block buffer object + * + * @return the next can be make block queue's length + */ +rt_size_t rt_rbb_next_blk_queue_len(rt_rbb_t rbb) +{ + rt_base_t level; + rt_size_t data_len = 0; + rt_slist_t *node; + rt_rbb_blk_t last_block = NULL, block; + + RT_ASSERT(rbb); + + if (rt_slist_isempty(&rbb->blk_list)) + return 0; + + level = rt_hw_interrupt_disable(); + + for (node = rt_slist_first(&rbb->blk_list); node; node = rt_slist_next(node)) + { + if (!last_block) + { + last_block = rt_slist_entry(node, struct rt_rbb_blk, list); + if (last_block->status != RT_RBB_BLK_PUT) + { + /* the first block must be put status */ + last_block = NULL; + continue; + } + } + else + { + block = rt_slist_entry(node, struct rt_rbb_blk, list); + /* + * these following conditions will break the loop: + * 1. the current block is not put status + * 2. the last block and current block is not continuous + */ + if (block->status != RT_RBB_BLK_PUT || last_block->buf > block->buf) + { + break; + } + /* backup last block */ + last_block = block; + } + data_len += last_block->size; + } + + rt_hw_interrupt_enable(level); + + return data_len; +} +RTM_EXPORT(rt_rbb_next_blk_queue_len); + +/** + * get the ring block buffer object buffer size + * + * @param rbb ring block buffer object + * + * @return buffer size + */ +rt_size_t rt_rbb_get_buf_size(rt_rbb_t rbb) +{ + RT_ASSERT(rbb); + + return rbb->buf_size; +} +RTM_EXPORT(rt_rbb_get_buf_size); diff --git a/components/drivers/src/ringbuffer.c b/components/drivers/src/ringbuffer.c index 48bf590f8c..2e078ae053 100644 --- a/components/drivers/src/ringbuffer.c +++ b/components/drivers/src/ringbuffer.c @@ -1,21 +1,7 @@ /* - * File : ringbuffer.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/src/waitqueue.c b/components/drivers/src/waitqueue.c index a4c392a27c..02240edbdf 100644 --- a/components/drivers/src/waitqueue.c +++ b/components/drivers/src/waitqueue.c @@ -1,21 +1,7 @@ /* - * File : waitqueue.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/src/workqueue.c b/components/drivers/src/workqueue.c index 35aaafe133..158ea8b69b 100644 --- a/components/drivers/src/workqueue.c +++ b/components/drivers/src/workqueue.c @@ -1,21 +1,7 @@ /* - * File : workqueue.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/usb/usbdevice/class/cdc.h b/components/drivers/usb/usbdevice/class/cdc.h index dd919f51fe..4e07f16266 100644 --- a/components/drivers/usb/usbdevice/class/cdc.h +++ b/components/drivers/usb/usbdevice/class/cdc.h @@ -1,21 +1,7 @@ /* - * File : cdc.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/usb/usbdevice/class/cdc_vcom.c b/components/drivers/usb/usbdevice/class/cdc_vcom.c index 784f968772..336cfcb152 100644 --- a/components/drivers/usb/usbdevice/class/cdc_vcom.c +++ b/components/drivers/usb/usbdevice/class/cdc_vcom.c @@ -1,21 +1,7 @@ /* - * File : cdc_vcom.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -36,7 +22,12 @@ #ifdef RT_USB_DEVICE_CDC -#define TX_TIMEOUT 1000 +#ifdef RT_VCOM_TX_TIMEOUT +#define VCOM_TX_TIMEOUT RT_VCOM_TX_TIMEOUT +#else /*!RT_VCOM_TX_TIMEOUT*/ +#define VCOM_TX_TIMEOUT 1000 +#endif /*RT_VCOM_TX_TIMEOUT*/ + #define CDC_RX_BUFSIZE 128 #define CDC_MAX_PACKET_SIZE 64 #define VCOM_DEVICE "vcom" @@ -96,6 +87,7 @@ struct vcom_tx_msg rt_size_t size; }; +ALIGN(4) static struct udevice_descriptor dev_desc = { USB_DESC_LENGTH_DEVICE, //bLength; @@ -115,6 +107,7 @@ static struct udevice_descriptor dev_desc = }; //FS and HS needed +ALIGN(4) static struct usb_qualifier_descriptor dev_qualifier = { sizeof(dev_qualifier), //bLength @@ -129,89 +122,111 @@ static struct usb_qualifier_descriptor dev_qualifier = }; /* communcation interface descriptor */ +ALIGN(4) const static struct ucdc_comm_descriptor _comm_desc = { #ifdef RT_USB_DEVICE_COMPOSITE /* Interface Association Descriptor */ - USB_DESC_LENGTH_IAD, - USB_DESC_TYPE_IAD, - USB_DYNAMIC, - 0x02, - USB_CDC_CLASS_COMM, - USB_CDC_SUBCLASS_ACM, - USB_CDC_PROTOCOL_V25TER, - 0x00, + { + USB_DESC_LENGTH_IAD, + USB_DESC_TYPE_IAD, + USB_DYNAMIC, + 0x02, + USB_CDC_CLASS_COMM, + USB_CDC_SUBCLASS_ACM, + USB_CDC_PROTOCOL_V25TER, + 0x00, + }, #endif /* Interface Descriptor */ - USB_DESC_LENGTH_INTERFACE, - USB_DESC_TYPE_INTERFACE, - USB_DYNAMIC, - 0x00, - 0x01, - USB_CDC_CLASS_COMM, - USB_CDC_SUBCLASS_ACM, - USB_CDC_PROTOCOL_V25TER, - 0x00, + { + USB_DESC_LENGTH_INTERFACE, + USB_DESC_TYPE_INTERFACE, + USB_DYNAMIC, + 0x00, + 0x01, + USB_CDC_CLASS_COMM, + USB_CDC_SUBCLASS_ACM, + USB_CDC_PROTOCOL_V25TER, + 0x00, + }, /* Header Functional Descriptor */ - 0x05, - USB_CDC_CS_INTERFACE, - USB_CDC_SCS_HEADER, - 0x0110, + { + 0x05, + USB_CDC_CS_INTERFACE, + USB_CDC_SCS_HEADER, + 0x0110, + }, /* Call Management Functional Descriptor */ - 0x05, - USB_CDC_CS_INTERFACE, - USB_CDC_SCS_CALL_MGMT, - 0x00, - USB_DYNAMIC, + { + 0x05, + USB_CDC_CS_INTERFACE, + USB_CDC_SCS_CALL_MGMT, + 0x00, + USB_DYNAMIC, + }, /* Abstract Control Management Functional Descriptor */ - 0x04, - USB_CDC_CS_INTERFACE, - USB_CDC_SCS_ACM, - 0x02, + { + 0x04, + USB_CDC_CS_INTERFACE, + USB_CDC_SCS_ACM, + 0x02, + }, /* Union Functional Descriptor */ - 0x05, - USB_CDC_CS_INTERFACE, - USB_CDC_SCS_UNION, - USB_DYNAMIC, - USB_DYNAMIC, + { + 0x05, + USB_CDC_CS_INTERFACE, + USB_CDC_SCS_UNION, + USB_DYNAMIC, + USB_DYNAMIC, + }, /* Endpoint Descriptor */ - USB_DESC_LENGTH_ENDPOINT, - USB_DESC_TYPE_ENDPOINT, - USB_DYNAMIC | USB_DIR_IN, - USB_EP_ATTR_INT, - 0x08, - 0xFF, + { + USB_DESC_LENGTH_ENDPOINT, + USB_DESC_TYPE_ENDPOINT, + USB_DYNAMIC | USB_DIR_IN, + USB_EP_ATTR_INT, + 0x08, + 0xFF, + }, }; /* data interface descriptor */ +ALIGN(4) const static struct ucdc_data_descriptor _data_desc = { /* interface descriptor */ - USB_DESC_LENGTH_INTERFACE, - USB_DESC_TYPE_INTERFACE, - USB_DYNAMIC, - 0x00, - 0x02, - USB_CDC_CLASS_DATA, - 0x00, - 0x00, - 0x00, + { + USB_DESC_LENGTH_INTERFACE, + USB_DESC_TYPE_INTERFACE, + USB_DYNAMIC, + 0x00, + 0x02, + USB_CDC_CLASS_DATA, + 0x00, + 0x00, + 0x00, + }, /* endpoint, bulk out */ - USB_DESC_LENGTH_ENDPOINT, - USB_DESC_TYPE_ENDPOINT, - USB_DYNAMIC | USB_DIR_OUT, - USB_EP_ATTR_BULK, - USB_CDC_BUFSIZE, - 0x00, + { + USB_DESC_LENGTH_ENDPOINT, + USB_DESC_TYPE_ENDPOINT, + USB_DYNAMIC | USB_DIR_OUT, + USB_EP_ATTR_BULK, + USB_CDC_BUFSIZE, + 0x00, + }, /* endpoint, bulk in */ - USB_DESC_LENGTH_ENDPOINT, - USB_DESC_TYPE_ENDPOINT, - USB_DYNAMIC | USB_DIR_IN, - USB_EP_ATTR_BULK, - USB_CDC_BUFSIZE, - 0x00, + { + USB_DESC_LENGTH_ENDPOINT, + USB_DESC_TYPE_ENDPOINT, + USB_DYNAMIC | USB_DIR_IN, + USB_EP_ATTR_BULK, + USB_CDC_BUFSIZE, + 0x00, + }, }; - +ALIGN(4) static char serno[_SER_NO_LEN + 1] = {'\0'}; RT_WEAK rt_err_t vcom_get_stored_serno(char *serno, int size); @@ -219,7 +234,7 @@ rt_err_t vcom_get_stored_serno(char *serno, int size) { return RT_ERROR; } - +ALIGN(4) const static char* _ustring[] = { "Language", @@ -829,7 +844,7 @@ static void vcom_tx_thread_entry(void* parameter) { continue; } - if(!res & CDC_TX_HAS_DATE) + if(!(res & CDC_TX_HAS_DATE)) { continue; } @@ -870,7 +885,7 @@ static void vcom_tx_thread_entry(void* parameter) rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request); - if (rt_completion_wait(&data->wait, TX_TIMEOUT) != RT_EOK) + if (rt_completion_wait(&data->wait, VCOM_TX_TIMEOUT) != RT_EOK) { RT_DEBUG_LOG(RT_DEBUG_USB, ("vcom tx timeout\n")); } diff --git a/components/drivers/usb/usbdevice/class/ecm.c b/components/drivers/usb/usbdevice/class/ecm.c index 6f19a0099e..cda1a8bf1b 100644 --- a/components/drivers/usb/usbdevice/class/ecm.c +++ b/components/drivers/usb/usbdevice/class/ecm.c @@ -1,11 +1,7 @@ /* - * File : ecm.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006-2013, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -80,52 +76,64 @@ const static struct ucdc_eth_descriptor _comm_desc = { #ifdef RT_USB_DEVICE_COMPOSITE /* Interface Association Descriptor */ - USB_DESC_LENGTH_IAD, - USB_DESC_TYPE_IAD, - USB_DYNAMIC, - 0x02, - USB_CDC_CLASS_COMM, - USB_CDC_SUBCLASS_ETH, - USB_CDC_PROTOCOL_NONE, - 0x00, + { + USB_DESC_LENGTH_IAD, + USB_DESC_TYPE_IAD, + USB_DYNAMIC, + 0x02, + USB_CDC_CLASS_COMM, + USB_CDC_SUBCLASS_ETH, + USB_CDC_PROTOCOL_NONE, + 0x00, + }, #endif /* Interface Descriptor */ - USB_DESC_LENGTH_INTERFACE, - USB_DESC_TYPE_INTERFACE, - USB_DYNAMIC, - 0x00, - 0x01, - USB_CDC_CLASS_COMM, - USB_CDC_SUBCLASS_ETH, - USB_CDC_PROTOCOL_NONE, - 0x00, + { + USB_DESC_LENGTH_INTERFACE, + USB_DESC_TYPE_INTERFACE, + USB_DYNAMIC, + 0x00, + 0x01, + USB_CDC_CLASS_COMM, + USB_CDC_SUBCLASS_ETH, + USB_CDC_PROTOCOL_NONE, + 0x00, + }, /* Header Functional Descriptor */ - sizeof(struct ucdc_header_descriptor), - USB_CDC_CS_INTERFACE, - USB_CDC_SCS_HEADER, - 0x0110, + { + sizeof(struct ucdc_header_descriptor), + USB_CDC_CS_INTERFACE, + USB_CDC_SCS_HEADER, + 0x0110, + }, /* Union Functional Descriptor */ - sizeof(struct ucdc_union_descriptor), - USB_CDC_CS_INTERFACE, - USB_CDC_SCS_UNION, - USB_DYNAMIC, - USB_DYNAMIC, + { + sizeof(struct ucdc_union_descriptor), + USB_CDC_CS_INTERFACE, + USB_CDC_SCS_UNION, + USB_DYNAMIC, + USB_DYNAMIC, + }, /* Abstract Control Management Functional Descriptor */ - sizeof(struct ucdc_enet_descriptor), - USB_CDC_CS_INTERFACE, - USB_CDC_SCS_ETH, - USB_STRING_SERIAL_INDEX, - {0,0,0,0}, - USB_ETH_MTU, - 0x00, - 0x00, + { + sizeof(struct ucdc_enet_descriptor), + USB_CDC_CS_INTERFACE, + USB_CDC_SCS_ETH, + USB_STRING_SERIAL_INDEX, + {0,0,0,0}, + USB_ETH_MTU, + 0x00, + 0x00, + }, /* Endpoint Descriptor */ - USB_DESC_LENGTH_ENDPOINT, - USB_DESC_TYPE_ENDPOINT, - USB_DIR_IN | USB_DYNAMIC, - USB_EP_ATTR_INT, - 0x08, - 0xFF, + { + USB_DESC_LENGTH_ENDPOINT, + USB_DESC_TYPE_ENDPOINT, + USB_DIR_IN | USB_DYNAMIC, + USB_EP_ATTR_INT, + 0x08, + 0xFF, + }, }; /* data interface descriptor */ @@ -133,29 +141,35 @@ ALIGN(4) const static struct ucdc_data_descriptor _data_desc = { /* interface descriptor */ - USB_DESC_LENGTH_INTERFACE, - USB_DESC_TYPE_INTERFACE, - USB_DYNAMIC, - 0x00, - 0x02, - USB_CDC_CLASS_DATA, - USB_CDC_SUBCLASS_ETH, - 0x00, - 0x00, + { + USB_DESC_LENGTH_INTERFACE, + USB_DESC_TYPE_INTERFACE, + USB_DYNAMIC, + 0x00, + 0x02, + USB_CDC_CLASS_DATA, + USB_CDC_SUBCLASS_ETH, + 0x00, + 0x00, + }, /* endpoint, bulk out */ - USB_DESC_LENGTH_ENDPOINT, - USB_DESC_TYPE_ENDPOINT, - USB_DIR_OUT | USB_DYNAMIC, - USB_EP_ATTR_BULK, - USB_DYNAMIC, - 0x00, + { + USB_DESC_LENGTH_ENDPOINT, + USB_DESC_TYPE_ENDPOINT, + USB_DIR_OUT | USB_DYNAMIC, + USB_EP_ATTR_BULK, + USB_DYNAMIC, + 0x00, + }, /* endpoint, bulk in */ - USB_DESC_LENGTH_ENDPOINT, - USB_DESC_TYPE_ENDPOINT, - USB_DYNAMIC | USB_DIR_IN, - USB_EP_ATTR_BULK, - USB_DYNAMIC, - 0x00, + { + USB_DESC_LENGTH_ENDPOINT, + USB_DESC_TYPE_ENDPOINT, + USB_DYNAMIC | USB_DIR_IN, + USB_EP_ATTR_BULK, + USB_DYNAMIC, + 0x00, + }, }; ALIGN(4) diff --git a/components/drivers/usb/usbdevice/class/hid.c b/components/drivers/usb/usbdevice/class/hid.c index bbaf7d23ef..aea6d802d1 100644 --- a/components/drivers/usb/usbdevice/class/hid.c +++ b/components/drivers/usb/usbdevice/class/hid.c @@ -1,7 +1,9 @@ /* * File : hid.c - * COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team + * COPYRIGHT (C) 2008 - 2018, RT-Thread Development Team * + * SPDX-License-Identifier: Apache-2.0 + * * Change Logs: * Date Author Notes * 2017-03-13 Urey the first version @@ -33,6 +35,7 @@ struct hid_s }; /* CustomHID_ConfigDescriptor */ +ALIGN(4) const rt_uint8_t _report_desc[]= { #ifdef RT_USB_DEVICE_HID_KEYBOARD @@ -237,6 +240,7 @@ const rt_uint8_t _report_desc[]= #endif }; /* CustomHID_ReportDescriptor */ +ALIGN(4) static struct udevice_descriptor _dev_desc = { USB_DESC_LENGTH_DEVICE, //bLength; @@ -256,6 +260,7 @@ static struct udevice_descriptor _dev_desc = }; //FS and HS needed +ALIGN(4) static struct usb_qualifier_descriptor dev_qualifier = { sizeof(dev_qualifier), //bLength @@ -271,78 +276,92 @@ static struct usb_qualifier_descriptor dev_qualifier = /* hid interface descriptor */ +ALIGN(4) const static struct uhid_comm_descriptor _hid_comm_desc = { #ifdef RT_USB_DEVICE_COMPOSITE /* Interface Association Descriptor */ - USB_DESC_LENGTH_IAD, - USB_DESC_TYPE_IAD, - USB_DYNAMIC, - 0x01, - 0x03, /* bInterfaceClass: HID */ + { + USB_DESC_LENGTH_IAD, + USB_DESC_TYPE_IAD, + USB_DYNAMIC, + 0x01, + 0x03, /* bInterfaceClass: HID */ #if defined(RT_USB_DEVICE_HID_KEYBOARD)||defined(RT_USB_DEVICE_HID_MOUSE) - USB_HID_SUBCLASS_BOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ + USB_HID_SUBCLASS_BOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ #else - USB_HID_SUBCLASS_NOBOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ + USB_HID_SUBCLASS_NOBOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ #endif #if !defined(RT_USB_DEVICE_HID_KEYBOARD)||!defined(RT_USB_DEVICE_HID_MOUSE)||!defined(RT_USB_DEVICE_HID_MEDIA) - USB_HID_PROTOCOL_NONE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ + USB_HID_PROTOCOL_NONE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ #elif !defined(RT_USB_DEVICE_HID_MOUSE) - USB_HID_PROTOCOL_KEYBOARD, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ + USB_HID_PROTOCOL_KEYBOARD, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ #else - USB_HID_PROTOCOL_MOUSE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ + USB_HID_PROTOCOL_MOUSE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ #endif - 0x00, + 0x00, + }, #endif - /* Interface Descriptor */ - USB_DESC_LENGTH_INTERFACE, - USB_DESC_TYPE_INTERFACE, - USB_DYNAMIC, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints */ - 0x03, /* bInterfaceClass: HID */ + { + USB_DESC_LENGTH_INTERFACE, + USB_DESC_TYPE_INTERFACE, + USB_DYNAMIC, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints */ + 0x03, /* bInterfaceClass: HID */ #if defined(RT_USB_DEVICE_HID_KEYBOARD)||defined(RT_USB_DEVICE_HID_MOUSE) - USB_HID_SUBCLASS_BOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ + USB_HID_SUBCLASS_BOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ #else - USB_HID_SUBCLASS_NOBOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ + USB_HID_SUBCLASS_NOBOOT, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ #endif #if !defined(RT_USB_DEVICE_HID_KEYBOARD)||!defined(RT_USB_DEVICE_HID_MOUSE)||!defined(RT_USB_DEVICE_HID_MEDIA) - USB_HID_PROTOCOL_NONE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ + USB_HID_PROTOCOL_NONE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ #elif !defined(RT_USB_DEVICE_HID_MOUSE) - USB_HID_PROTOCOL_KEYBOARD, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ + USB_HID_PROTOCOL_KEYBOARD, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ #else - USB_HID_PROTOCOL_MOUSE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ + USB_HID_PROTOCOL_MOUSE, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ #endif - 0, /* iInterface: Index of string descriptor */ + 0, /* iInterface: Index of string descriptor */ + }, /* HID Descriptor */ - HID_DESCRIPTOR_SIZE, /* bLength: HID Descriptor size */ - HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */ - 0x0110, /* bcdHID: HID Class Spec release number */ - 0x00, /* bCountryCode: Hardware target country */ - 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ - 0x22, /* bDescriptorType */ - sizeof(_report_desc), /* wItemLength: Total length of Report descriptor */ + { + HID_DESCRIPTOR_SIZE, /* bLength: HID Descriptor size */ + HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */ + 0x0110, /* bcdHID: HID Class Spec release number */ + 0x00, /* bCountryCode: Hardware target country */ + 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ + { + { + 0x22, /* bDescriptorType */ + sizeof(_report_desc), /* wItemLength: Total length of Report descriptor */ + }, + }, + }, /* Endpoint Descriptor IN */ - USB_DESC_LENGTH_ENDPOINT, - USB_DESC_TYPE_ENDPOINT, - USB_DYNAMIC | USB_DIR_IN, - USB_EP_ATTR_INT, - 0x40, - 0x01, + { + USB_DESC_LENGTH_ENDPOINT, + USB_DESC_TYPE_ENDPOINT, + USB_DYNAMIC | USB_DIR_IN, + USB_EP_ATTR_INT, + 0x40, + 0x01, + }, /* Endpoint Descriptor OUT */ - USB_DESC_LENGTH_ENDPOINT, - USB_DESC_TYPE_ENDPOINT, - USB_DYNAMIC | USB_DIR_OUT, - USB_EP_ATTR_INT, - 0x40, - 0x01, + { + USB_DESC_LENGTH_ENDPOINT, + USB_DESC_TYPE_ENDPOINT, + USB_DYNAMIC | USB_DIR_OUT, + USB_EP_ATTR_INT, + 0x40, + 0x01, + }, }; - +ALIGN(4) const static char* _ustring[] = { "Language", @@ -480,12 +499,7 @@ static rt_err_t _interface_handler(ufunction_t func, ureq_t setup) rt_usbd_ep0_read(func->device, data->report_buf, setup->wLength, _hid_set_report_callback); break; case USB_HID_REQ_SET_IDLE: - { - int duration = (setup->wValue >> 8); - int report_id = (setup->wValue & 0xFF); - dcd_ep0_send_status(func->device->dcd); - } break; case USB_HID_REQ_SET_PROTOCOL: data->protocol = setup->wValue; diff --git a/components/drivers/usb/usbdevice/class/hid.h b/components/drivers/usb/usbdevice/class/hid.h index 4e5dac964b..4d0f314a11 100644 --- a/components/drivers/usb/usbdevice/class/hid.h +++ b/components/drivers/usb/usbdevice/class/hid.h @@ -1,6 +1,7 @@ /* - * File : hid.h - * COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/usb/usbdevice/class/mstorage.c b/components/drivers/usb/usbdevice/class/mstorage.c index 63d7b0f9ad..6a6e92415b 100644 --- a/components/drivers/usb/usbdevice/class/mstorage.c +++ b/components/drivers/usb/usbdevice/class/mstorage.c @@ -1,21 +1,7 @@ /* - * File : mstorage.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -82,6 +68,7 @@ struct mstorage struct rt_device_blk_geometry geometry; }; +ALIGN(4) static struct udevice_descriptor dev_desc = { USB_DESC_LENGTH_DEVICE, //bLength; @@ -101,6 +88,7 @@ static struct udevice_descriptor dev_desc = }; //FS and HS needed +ALIGN(4) static struct usb_qualifier_descriptor dev_qualifier = { sizeof(dev_qualifier), //bLength @@ -115,45 +103,54 @@ static struct usb_qualifier_descriptor dev_qualifier = }; - +ALIGN(4) const static struct umass_descriptor _mass_desc = { #ifdef RT_USB_DEVICE_COMPOSITE /* Interface Association Descriptor */ - USB_DESC_LENGTH_IAD, - USB_DESC_TYPE_IAD, - USB_DYNAMIC, - 0x01, - USB_CLASS_MASS_STORAGE, - 0x06, - 0x50, - 0x00, + { + USB_DESC_LENGTH_IAD, + USB_DESC_TYPE_IAD, + USB_DYNAMIC, + 0x01, + USB_CLASS_MASS_STORAGE, + 0x06, + 0x50, + 0x00, + }, #endif - USB_DESC_LENGTH_INTERFACE, //bLength; - USB_DESC_TYPE_INTERFACE, //type; - USB_DYNAMIC, //bInterfaceNumber; - 0x00, //bAlternateSetting; - 0x02, //bNumEndpoints - USB_CLASS_MASS_STORAGE, //bInterfaceClass; - 0x06, //bInterfaceSubClass; - 0x50, //bInterfaceProtocol; - 0x00, //iInterface; + { + USB_DESC_LENGTH_INTERFACE, //bLength; + USB_DESC_TYPE_INTERFACE, //type; + USB_DYNAMIC, //bInterfaceNumber; + 0x00, //bAlternateSetting; + 0x02, //bNumEndpoints + USB_CLASS_MASS_STORAGE, //bInterfaceClass; + 0x06, //bInterfaceSubClass; + 0x50, //bInterfaceProtocol; + 0x00, //iInterface; + }, - USB_DESC_LENGTH_ENDPOINT, //bLength; - USB_DESC_TYPE_ENDPOINT, //type; - USB_DYNAMIC | USB_DIR_OUT, //bEndpointAddress; - USB_EP_ATTR_BULK, //bmAttributes; - USB_DYNAMIC, //wMaxPacketSize; - 0x00, //bInterval; + { + USB_DESC_LENGTH_ENDPOINT, //bLength; + USB_DESC_TYPE_ENDPOINT, //type; + USB_DYNAMIC | USB_DIR_OUT, //bEndpointAddress; + USB_EP_ATTR_BULK, //bmAttributes; + USB_DYNAMIC, //wMaxPacketSize; + 0x00, //bInterval; + }, - USB_DESC_LENGTH_ENDPOINT, //bLength; - USB_DESC_TYPE_ENDPOINT, //type; - USB_DYNAMIC | USB_DIR_IN, //bEndpointAddress; - USB_EP_ATTR_BULK, //bmAttributes; - USB_DYNAMIC, //wMaxPacketSize; - 0x00, //bInterval; + { + USB_DESC_LENGTH_ENDPOINT, //bLength; + USB_DESC_TYPE_ENDPOINT, //type; + USB_DYNAMIC | USB_DIR_IN, //bEndpointAddress; + USB_EP_ATTR_BULK, //bmAttributes; + USB_DYNAMIC, //wMaxPacketSize; + 0x00, //bInterval; + }, }; +ALIGN(4) const static char* _ustring[] = { "Language", @@ -176,6 +173,7 @@ static rt_size_t _read_10(ufunction_t func, ustorage_cbw_t cbw); static rt_size_t _write_10(ufunction_t func, ustorage_cbw_t cbw); static rt_size_t _verify_10(ufunction_t func, ustorage_cbw_t cbw); +ALIGN(4) static struct scsi_cmd cmd_data[] = { {SCSI_TEST_UNIT_READY, _test_unit_ready, 6, FIXED, 0, DIR_NONE}, @@ -751,8 +749,8 @@ static rt_bool_t _cbw_verify(ufunction_t func, struct scsi_cmd* cmd, return RT_FALSE; } - if((cbw->dflags & USB_DIR_IN) && cmd->dir == DIR_OUT || - !(cbw->dflags & USB_DIR_IN) && cmd->dir == DIR_IN) + if(((cbw->dflags & USB_DIR_IN) && (cmd->dir == DIR_OUT)) || + (!(cbw->dflags & USB_DIR_IN) && (cmd->dir == DIR_IN))) { rt_kprintf("dir error\n"); return RT_FALSE; diff --git a/components/drivers/usb/usbdevice/class/mstorage.h b/components/drivers/usb/usbdevice/class/mstorage.h index 25068270bd..254038195b 100644 --- a/components/drivers/usb/usbdevice/class/mstorage.h +++ b/components/drivers/usb/usbdevice/class/mstorage.h @@ -1,21 +1,7 @@ /* - * File : mstorage.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/usb/usbdevice/class/ndis.h b/components/drivers/usb/usbdevice/class/ndis.h index 8ad16637c2..6d6943366e 100644 --- a/components/drivers/usb/usbdevice/class/ndis.h +++ b/components/drivers/usb/usbdevice/class/ndis.h @@ -1,4 +1,12 @@ /* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ +/* * ndis.h * * Modified by Colin O'Flynn <[email protected]> diff --git a/components/drivers/usb/usbdevice/class/rndis.c b/components/drivers/usb/usbdevice/class/rndis.c index 0f680e9066..8996b728e6 100644 --- a/components/drivers/usb/usbdevice/class/rndis.c +++ b/components/drivers/usb/usbdevice/class/rndis.c @@ -1,11 +1,7 @@ /* - * File : rndis.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006-2013, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -106,54 +102,68 @@ const static struct ucdc_comm_descriptor _comm_desc = { #ifdef RT_USB_DEVICE_COMPOSITE /* Interface Association Descriptor */ - USB_DESC_LENGTH_IAD, - USB_DESC_TYPE_IAD, - USB_DYNAMIC, - 0x02, - USB_CDC_CLASS_COMM, - USB_CDC_SUBCLASS_ACM, - USB_CDC_PROTOCOL_VENDOR, - 0x00, + { + USB_DESC_LENGTH_IAD, + USB_DESC_TYPE_IAD, + USB_DYNAMIC, + 0x02, + USB_CDC_CLASS_COMM, + USB_CDC_SUBCLASS_ACM, + USB_CDC_PROTOCOL_VENDOR, + 0x00, + }, #endif /* Interface Descriptor */ - USB_DESC_LENGTH_INTERFACE, - USB_DESC_TYPE_INTERFACE, - USB_DYNAMIC, - 0x00, - 0x01, - USB_CDC_CLASS_COMM, - USB_CDC_SUBCLASS_ACM, - USB_CDC_PROTOCOL_VENDOR, - 0x00, + { + USB_DESC_LENGTH_INTERFACE, + USB_DESC_TYPE_INTERFACE, + USB_DYNAMIC, + 0x00, + 0x01, + USB_CDC_CLASS_COMM, + USB_CDC_SUBCLASS_ACM, + USB_CDC_PROTOCOL_VENDOR, + 0x00, + }, /* Header Functional Descriptor */ - 0x05, - USB_CDC_CS_INTERFACE, - USB_CDC_SCS_HEADER, - 0x0110, + { + 0x05, + USB_CDC_CS_INTERFACE, + USB_CDC_SCS_HEADER, + 0x0110, + }, /* Call Management Functional Descriptor */ - 0x05, - USB_CDC_CS_INTERFACE, - USB_CDC_SCS_CALL_MGMT, - 0x00, - USB_DYNAMIC, + { + 0x05, + USB_CDC_CS_INTERFACE, + USB_CDC_SCS_CALL_MGMT, + 0x00, + USB_DYNAMIC, + }, /* Abstract Control Management Functional Descriptor */ - 0x04, - USB_CDC_CS_INTERFACE, - USB_CDC_SCS_ACM, - 0x02, + { + 0x04, + USB_CDC_CS_INTERFACE, + USB_CDC_SCS_ACM, + 0x02, + }, /* Union Functional Descriptor */ - 0x05, - USB_CDC_CS_INTERFACE, - USB_CDC_SCS_UNION, - USB_DYNAMIC, - USB_DYNAMIC, + { + 0x05, + USB_CDC_CS_INTERFACE, + USB_CDC_SCS_UNION, + USB_DYNAMIC, + USB_DYNAMIC, + }, /* Endpoint Descriptor */ - USB_DESC_LENGTH_ENDPOINT, - USB_DESC_TYPE_ENDPOINT, - USB_DIR_IN | USB_DYNAMIC, - USB_EP_ATTR_INT, - 0x08, - 0x0A, + { + USB_DESC_LENGTH_ENDPOINT, + USB_DESC_TYPE_ENDPOINT, + USB_DIR_IN | USB_DYNAMIC, + USB_EP_ATTR_INT, + 0x08, + 0x0A, + }, }; /* data interface descriptor */ @@ -161,29 +171,35 @@ ALIGN(4) const static struct ucdc_data_descriptor _data_desc = { /* interface descriptor */ - USB_DESC_LENGTH_INTERFACE, - USB_DESC_TYPE_INTERFACE, - USB_DYNAMIC, - 0x00, - 0x02, - USB_CDC_CLASS_DATA, - 0x00, - 0x00, - 0x00, + { + USB_DESC_LENGTH_INTERFACE, + USB_DESC_TYPE_INTERFACE, + USB_DYNAMIC, + 0x00, + 0x02, + USB_CDC_CLASS_DATA, + 0x00, + 0x00, + 0x00, + }, /* endpoint, bulk out */ - USB_DESC_LENGTH_ENDPOINT, - USB_DESC_TYPE_ENDPOINT, - USB_DIR_OUT | USB_DYNAMIC, - USB_EP_ATTR_BULK, - USB_DYNAMIC, - 0x00, + { + USB_DESC_LENGTH_ENDPOINT, + USB_DESC_TYPE_ENDPOINT, + USB_DIR_OUT | USB_DYNAMIC, + USB_EP_ATTR_BULK, + USB_DYNAMIC, + 0x00, + }, /* endpoint, bulk in */ - USB_DESC_LENGTH_ENDPOINT, - USB_DESC_TYPE_ENDPOINT, - USB_DYNAMIC | USB_DIR_IN, - USB_EP_ATTR_BULK, - USB_DYNAMIC, - 0x00, + { + USB_DESC_LENGTH_ENDPOINT, + USB_DESC_TYPE_ENDPOINT, + USB_DYNAMIC | USB_DIR_IN, + USB_EP_ATTR_BULK, + USB_DYNAMIC, + 0x00, + }, }; ALIGN(4) @@ -198,6 +214,7 @@ const static char* _ustring[] = USB_STRING_OS }; +ALIGN(4) struct usb_os_function_comp_id_descriptor rndis_func_comp_id_desc = { .bFirstInterfaceNumber = USB_DYNAMIC, @@ -206,7 +223,9 @@ struct usb_os_function_comp_id_descriptor rndis_func_comp_id_desc = .subCompatibleID = {'5', '1', '6', '2', '0', '0', '1', 0x00}, .reserved2 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} }; + //FS and HS needed +ALIGN(4) static struct usb_qualifier_descriptor dev_qualifier = { sizeof(dev_qualifier), //bLength diff --git a/components/drivers/usb/usbdevice/class/rndis.h b/components/drivers/usb/usbdevice/class/rndis.h index 1374338c14..000ea62baf 100644 --- a/components/drivers/usb/usbdevice/class/rndis.h +++ b/components/drivers/usb/usbdevice/class/rndis.h @@ -1,11 +1,7 @@ /* - * File : rndis.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/usb/usbdevice/class/winusb.c b/components/drivers/usb/usbdevice/class/winusb.c index 9e7c957e7c..bf4b353d73 100644 --- a/components/drivers/usb/usbdevice/class/winusb.c +++ b/components/drivers/usb/usbdevice/class/winusb.c @@ -1,6 +1,7 @@ /* - * File : winusb.c - * COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -23,6 +24,7 @@ struct winusb_device typedef struct winusb_device * winusb_device_t; +ALIGN(4) static struct udevice_descriptor dev_desc = { USB_DESC_LENGTH_DEVICE, //bLength; @@ -40,7 +42,9 @@ static struct udevice_descriptor dev_desc = USB_STRING_SERIAL_INDEX, //iSerialNumber; USB_DYNAMIC, //bNumConfigurations; }; + //FS and HS needed +ALIGN(4) static struct usb_qualifier_descriptor dev_qualifier = { sizeof(dev_qualifier), //bLength @@ -54,46 +58,55 @@ static struct usb_qualifier_descriptor dev_qualifier = 0, }; +ALIGN(4) struct winusb_descriptor _winusb_desc = { #ifdef RT_USB_DEVICE_COMPOSITE /* Interface Association Descriptor */ - USB_DESC_LENGTH_IAD, - USB_DESC_TYPE_IAD, - USB_DYNAMIC, - 0x01, - 0xFF, - 0x00, - 0x00, - 0x00, + { + USB_DESC_LENGTH_IAD, + USB_DESC_TYPE_IAD, + USB_DYNAMIC, + 0x01, + 0xFF, + 0x00, + 0x00, + 0x00, + }, #endif /*interface descriptor*/ - USB_DESC_LENGTH_INTERFACE, //bLength; - USB_DESC_TYPE_INTERFACE, //type; - USB_DYNAMIC, //bInterfaceNumber; - 0x00, //bAlternateSetting; - 0x02, //bNumEndpoints - 0xFF, //bInterfaceClass; - 0x00, //bInterfaceSubClass; - 0x00, //bInterfaceProtocol; - 0x00, //iInterface; + { + USB_DESC_LENGTH_INTERFACE, //bLength; + USB_DESC_TYPE_INTERFACE, //type; + USB_DYNAMIC, //bInterfaceNumber; + 0x00, //bAlternateSetting; + 0x02, //bNumEndpoints + 0xFF, //bInterfaceClass; + 0x00, //bInterfaceSubClass; + 0x00, //bInterfaceProtocol; + 0x00, //iInterface; + }, /*endpoint descriptor*/ - USB_DESC_LENGTH_ENDPOINT, - USB_DESC_TYPE_ENDPOINT, - USB_DYNAMIC | USB_DIR_OUT, - USB_EP_ATTR_BULK, - USB_DYNAMIC, - 0x00, + { + USB_DESC_LENGTH_ENDPOINT, + USB_DESC_TYPE_ENDPOINT, + USB_DYNAMIC | USB_DIR_OUT, + USB_EP_ATTR_BULK, + USB_DYNAMIC, + 0x00, + }, /*endpoint descriptor*/ - USB_DESC_LENGTH_ENDPOINT, - USB_DESC_TYPE_ENDPOINT, - USB_DYNAMIC | USB_DIR_IN, - USB_EP_ATTR_BULK, - USB_DYNAMIC, - 0x00, + { + USB_DESC_LENGTH_ENDPOINT, + USB_DESC_TYPE_ENDPOINT, + USB_DYNAMIC | USB_DIR_IN, + USB_EP_ATTR_BULK, + USB_DYNAMIC, + 0x00, + }, }; - +ALIGN(4) const static char* _ustring[] = { "Language", @@ -104,11 +117,14 @@ const static char* _ustring[] = "Interface", USB_STRING_OS//must be }; + +ALIGN(4) struct usb_os_proerty winusb_proerty[] = { USB_OS_PROERTY_DESC(USB_OS_PROERTY_TYPE_REG_SZ,"DeviceInterfaceGUID",RT_WINUSB_GUID), }; +ALIGN(4) struct usb_os_function_comp_id_descriptor winusb_func_comp_id_desc = { .bFirstInterfaceNumber = USB_DYNAMIC, diff --git a/components/drivers/usb/usbdevice/class/winusb.h b/components/drivers/usb/usbdevice/class/winusb.h index 86aa7f413c..b5f87b4e56 100644 --- a/components/drivers/usb/usbdevice/class/winusb.h +++ b/components/drivers/usb/usbdevice/class/winusb.h @@ -1,6 +1,7 @@ /* - * File : winusb.h - * COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/usb/usbdevice/core/core.c b/components/drivers/usb/usbdevice/core/core.c index 297e1ab48d..fb850d6d2b 100644 --- a/components/drivers/usb/usbdevice/core/core.c +++ b/components/drivers/usb/usbdevice/core/core.c @@ -1,21 +1,7 @@ /* - * File : core.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -36,7 +22,7 @@ static rt_list_t device_list; static rt_size_t rt_usbd_ep_write(udevice_t device, uep_t ep, void *buffer, rt_size_t size); static rt_size_t rt_usbd_ep_read_prepare(udevice_t device, uep_t ep, void *buffer, rt_size_t size); static rt_err_t rt_usbd_ep_assign(udevice_t device, uep_t ep); -static rt_err_t rt_usbd_ep_unassign(udevice_t device, uep_t ep); +rt_err_t rt_usbd_ep_unassign(udevice_t device, uep_t ep); /** * This function will handle get_device_descriptor bRequest. @@ -1865,7 +1851,7 @@ static rt_err_t rt_usbd_ep_assign(udevice_t device, uep_t ep) return -RT_ERROR; } -static rt_err_t rt_usbd_ep_unassign(udevice_t device, uep_t ep) +rt_err_t rt_usbd_ep_unassign(udevice_t device, uep_t ep) { RT_ASSERT(device != RT_NULL); RT_ASSERT(device->dcd != RT_NULL); diff --git a/components/drivers/usb/usbdevice/core/usbdevice.c b/components/drivers/usb/usbdevice/core/usbdevice.c index 72f80dc254..2455bd8017 100644 --- a/components/drivers/usb/usbdevice/core/usbdevice.c +++ b/components/drivers/usb/usbdevice/core/usbdevice.c @@ -1,21 +1,8 @@ /* - * File : usbdevice.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, RT-Thread Development Team + * File : hid.c + * COPYRIGHT (C) 2008 - 2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -78,11 +65,13 @@ static struct usb_qualifier_descriptor dev_qualifier = struct usb_os_comp_id_descriptor usb_comp_id_desc = { //head section - USB_DYNAMIC, - 0x0100, - 0x04, - USB_DYNAMIC, - {0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + { + USB_DYNAMIC, + 0x0100, + 0x04, + USB_DYNAMIC, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + }, }; static rt_list_t class_list; int rt_usbd_class_list_init(void) @@ -94,6 +83,13 @@ INIT_BOARD_EXPORT(rt_usbd_class_list_init); rt_err_t rt_usbd_class_register(udclass_t udclass) { +#ifndef RT_USB_DEVICE_COMPOSITE + if(!rt_list_isempty(&class_list)) + { + rt_kprintf("[D/USBD] If you want to use usb composite device please define RT_USB_DEVICE_COMPOSITE\n"); + return RT_ERROR; + } +#endif rt_list_insert_before(&class_list,&udclass->list); return RT_EOK; } @@ -107,6 +103,11 @@ rt_err_t rt_usb_device_init(void) rt_list_t *i; udclass_t udclass; + if(rt_list_isempty(&class_list)) + { + rt_kprintf("[D/USBD] No class register on usb device\n"); + return RT_ERROR; + } /* create and startup usb device thread */ rt_usbd_core_init(); diff --git a/components/drivers/usb/usbhost/class/adk.c b/components/drivers/usb/usbhost/class/adk.c index e7eed45127..2425de7bba 100644 --- a/components/drivers/usb/usbhost/class/adk.c +++ b/components/drivers/usb/usbhost/class/adk.c @@ -1,21 +1,7 @@ /* - * File : adk.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -69,7 +55,7 @@ static rt_err_t rt_usbh_adk_get_protocol(struct uintf* intf, rt_uint16_t *protoc { struct urequest setup; uinst_t device; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* parameter check */ RT_ASSERT(intf != RT_NULL); @@ -103,7 +89,7 @@ static rt_err_t rt_usbh_adk_send_string(struct uintf* intf, rt_uint16_t index, { struct urequest setup; uinst_t device; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* parameter check */ RT_ASSERT(intf != RT_NULL); @@ -136,7 +122,7 @@ static rt_err_t rt_usbh_adk_start(struct uintf* intf) { struct urequest setup; uinst_t device; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* parameter check */ RT_ASSERT(intf != RT_NULL); diff --git a/components/drivers/usb/usbhost/class/adk.h b/components/drivers/usb/usbhost/class/adk.h index f08bea5f59..041d47c674 100644 --- a/components/drivers/usb/usbhost/class/adk.h +++ b/components/drivers/usb/usbhost/class/adk.h @@ -1,21 +1,7 @@ /* - * File : adk.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/usb/usbhost/class/hid.c b/components/drivers/usb/usbhost/class/hid.c index 2c226ca963..e16096877f 100644 --- a/components/drivers/usb/usbhost/class/hid.c +++ b/components/drivers/usb/usbhost/class/hid.c @@ -1,11 +1,7 @@ /* - * File : hid.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -34,7 +30,7 @@ rt_err_t rt_usbh_hid_set_idle(struct uintf* intf, int duration, int report_id) { struct urequest setup; struct uinstance* device; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* parameter check */ RT_ASSERT(intf != RT_NULL); @@ -68,7 +64,7 @@ rt_err_t rt_usbh_hid_get_report(struct uintf* intf, rt_uint8_t type, { struct urequest setup; struct uinstance* device; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* parameter check */ RT_ASSERT(intf != RT_NULL); @@ -101,7 +97,7 @@ rt_err_t rt_usbh_hid_set_report(struct uintf* intf, rt_uint8_t *buffer, rt_size_ { struct urequest setup; struct uinstance* device; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* parameter check */ RT_ASSERT(intf != RT_NULL); @@ -133,7 +129,7 @@ rt_err_t rt_usbh_hid_set_protocal(struct uintf* intf, int protocol) { struct urequest setup; struct uinstance* device; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* parameter check */ RT_ASSERT(intf != RT_NULL); @@ -168,7 +164,7 @@ rt_err_t rt_usbh_hid_get_report_descriptor(struct uintf* intf, { struct urequest setup; struct uinstance* device; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* parameter check */ RT_ASSERT(intf != RT_NULL); @@ -218,7 +214,7 @@ static void rt_usbh_hid_callback(void* context) { upipe_t pipe; struct uhid* hid; - int timeout = 300; + int timeout = USB_TIMEOUT_LONG; /* parameter check */ RT_ASSERT(context != RT_NULL); @@ -273,7 +269,7 @@ static rt_err_t rt_usbh_hid_enable(void* arg) uprotocal_t protocal; struct uhid* hid; struct uintf* intf = (struct uintf*)arg; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; upipe_t pipe; /* parameter check */ diff --git a/components/drivers/usb/usbhost/class/hid.h b/components/drivers/usb/usbhost/class/hid.h index 22dcbabde8..8e446c8cea 100644 --- a/components/drivers/usb/usbhost/class/hid.h +++ b/components/drivers/usb/usbhost/class/hid.h @@ -1,11 +1,7 @@ /* - * File : hid.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/usb/usbhost/class/mass.c b/components/drivers/usb/usbhost/class/mass.c index ff52f8d6f5..20048873f5 100644 --- a/components/drivers/usb/usbhost/class/mass.c +++ b/components/drivers/usb/usbhost/class/mass.c @@ -1,11 +1,7 @@ /* - * File : mass.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -194,7 +190,7 @@ rt_err_t rt_usbh_storage_get_max_lun(struct uhintf* intf, rt_uint8_t* max_lun) { struct uinstance* device; struct urequest setup; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; if(intf == RT_NULL) { @@ -244,7 +240,7 @@ rt_err_t rt_usbh_storage_reset(struct uhintf* intf) { struct urequest setup; struct uinstance* device; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* parameter check */ if(intf == RT_NULL) @@ -381,7 +377,7 @@ rt_err_t rt_usbh_storage_write10(struct uhintf* intf, rt_uint8_t *buffer, rt_err_t rt_usbh_storage_request_sense(struct uhintf* intf, rt_uint8_t* buffer) { struct ustorage_cbw cmd; - int timeout = 200; + int timeout = USB_TIMEOUT_LONG; /* parameter check */ if(intf == RT_NULL) @@ -417,7 +413,7 @@ rt_err_t rt_usbh_storage_request_sense(struct uhintf* intf, rt_uint8_t* buffer) rt_err_t rt_usbh_storage_test_unit_ready(struct uhintf* intf) { struct ustorage_cbw cmd; - int timeout = 200; + int timeout = USB_TIMEOUT_LONG; /* parameter check */ if(intf == RT_NULL) @@ -453,7 +449,7 @@ rt_err_t rt_usbh_storage_test_unit_ready(struct uhintf* intf) rt_err_t rt_usbh_storage_inquiry(struct uhintf* intf, rt_uint8_t* buffer) { struct ustorage_cbw cmd; - int timeout = 200; + int timeout = USB_TIMEOUT_LONG; /* parameter check */ if(intf == RT_NULL) @@ -490,7 +486,7 @@ rt_err_t rt_usbh_storage_inquiry(struct uhintf* intf, rt_uint8_t* buffer) rt_err_t rt_usbh_storage_get_capacity(struct uhintf* intf, rt_uint8_t* buffer) { struct ustorage_cbw cmd; - int timeout = 200; + int timeout = USB_TIMEOUT_LONG; /* parameter check */ if(intf == RT_NULL) diff --git a/components/drivers/usb/usbhost/class/mass.h b/components/drivers/usb/usbhost/class/mass.h index 38cdb32399..770e64a03f 100644 --- a/components/drivers/usb/usbhost/class/mass.h +++ b/components/drivers/usb/usbhost/class/mass.h @@ -1,11 +1,7 @@ /* - * File : mass.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/usb/usbhost/class/udisk.c b/components/drivers/usb/usbhost/class/udisk.c index cdd2551c96..1509a26105 100644 --- a/components/drivers/usb/usbhost/class/udisk.c +++ b/components/drivers/usb/usbhost/class/udisk.c @@ -1,11 +1,7 @@ /* - * File : udisk.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -74,13 +70,13 @@ static rt_size_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_err_t ret; struct uhintf* intf; struct ustor_data* data; - int timeout = 500; + int timeout = USB_TIMEOUT_LONG; /* check parameter */ RT_ASSERT(dev != RT_NULL); RT_ASSERT(buffer != RT_NULL); - if(size > 4096) timeout = 800; + if(size > 4096) timeout *= 2; data = (struct ustor_data*)dev->user_data; intf = data->intf; @@ -112,13 +108,13 @@ static rt_size_t rt_udisk_write (rt_device_t dev, rt_off_t pos, const void* buff rt_err_t ret; struct uhintf* intf; struct ustor_data* data; - int timeout = 500; + int timeout = USB_TIMEOUT_LONG; /* check parameter */ RT_ASSERT(dev != RT_NULL); RT_ASSERT(buffer != RT_NULL); - if(size * SECTOR_SIZE > 4096) timeout = 800; + if(size * SECTOR_SIZE > 4096) timeout *= 2; data = (struct ustor_data*)dev->user_data; intf = data->intf; @@ -297,7 +293,7 @@ rt_err_t rt_udisk_run(struct uhintf* intf) RT_DEBUG_LOG(RT_DEBUG_USB, ("read partition table\n")); /* get the partition table */ - ret = rt_usbh_storage_read10(intf, sector, 0, 1, 500); + ret = rt_usbh_storage_read10(intf, sector, 0, 1, USB_TIMEOUT_LONG); if(ret != RT_EOK) { rt_kprintf("read parition table error\n"); diff --git a/components/drivers/usb/usbhost/class/ukbd.c b/components/drivers/usb/usbhost/class/ukbd.c index 9f7d4bda3a..7d608de259 100644 --- a/components/drivers/usb/usbhost/class/ukbd.c +++ b/components/drivers/usb/usbhost/class/ukbd.c @@ -1,11 +1,7 @@ /* - * File : ukbd.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/usb/usbhost/class/umouse.c b/components/drivers/usb/usbhost/class/umouse.c index 0fb529f598..e769475a98 100644 --- a/components/drivers/usb/usbhost/class/umouse.c +++ b/components/drivers/usb/usbhost/class/umouse.c @@ -1,11 +1,7 @@ /* - * File : umouse.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/usb/usbhost/core/core.c b/components/drivers/usb/usbhost/core/core.c index 854c3815af..71341a33c3 100644 --- a/components/drivers/usb/usbhost/core/core.c +++ b/components/drivers/usb/usbhost/core/core.c @@ -1,21 +1,7 @@ /* - * File : core.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -274,18 +260,20 @@ rt_err_t rt_usbh_detach_instance(uinst_t device) } /* free configration descriptor */ - for(i=0; i<device->cfg_desc->bNumInterfaces; i++) - { - if(device->intf[i] == RT_NULL) continue; - if(device->intf[i]->drv == RT_NULL) continue; + if (device->cfg_desc) { + for (i = 0; i < device->cfg_desc->bNumInterfaces; i++) + { + if (device->intf[i] == RT_NULL) continue; + if (device->intf[i]->drv == RT_NULL) continue; - RT_ASSERT(device->intf[i]->device == device); + RT_ASSERT(device->intf[i]->device == device); - RT_DEBUG_LOG(RT_DEBUG_USB, ("free interface instance %d\n", i)); - rt_usbh_class_driver_disable(device->intf[i]->drv, (void*)device->intf[i]); - rt_free(device->intf[i]); + RT_DEBUG_LOG(RT_DEBUG_USB, ("free interface instance %d\n", i)); + rt_usbh_class_driver_disable(device->intf[i]->drv, (void*)device->intf[i]); + rt_free(device->intf[i]); + } + rt_free(device->cfg_desc); } - if(device->cfg_desc) rt_free(device->cfg_desc); rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_out); rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_in); @@ -315,7 +303,7 @@ rt_err_t rt_usbh_get_descriptor(uinst_t device, rt_uint8_t type, void* buffer, int nbytes) { struct urequest setup; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; RT_ASSERT(device != RT_NULL); @@ -349,7 +337,7 @@ rt_err_t rt_usbh_get_descriptor(uinst_t device, rt_uint8_t type, void* buffer, rt_err_t rt_usbh_set_address(uinst_t device) { struct urequest setup; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; RT_ASSERT(device != RT_NULL); @@ -385,7 +373,7 @@ rt_err_t rt_usbh_set_address(uinst_t device) rt_err_t rt_usbh_set_configure(uinst_t device, int config) { struct urequest setup; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* check parameter */ RT_ASSERT(device != RT_NULL); @@ -419,7 +407,7 @@ rt_err_t rt_usbh_set_configure(uinst_t device, int config) rt_err_t rt_usbh_set_interface(uinst_t device, int intf) { struct urequest setup; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* check parameter */ RT_ASSERT(device != RT_NULL); @@ -450,7 +438,7 @@ rt_err_t rt_usbh_set_interface(uinst_t device, int intf) rt_err_t rt_usbh_clear_feature(uinst_t device, int endpoint, int feature) { struct urequest setup; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* check parameter */ RT_ASSERT(device != RT_NULL); diff --git a/components/drivers/usb/usbhost/core/driver.c b/components/drivers/usb/usbhost/core/driver.c index 19c8805732..37327108c1 100644 --- a/components/drivers/usb/usbhost/core/driver.c +++ b/components/drivers/usb/usbhost/core/driver.c @@ -1,21 +1,7 @@ /* - * File : driver.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/usb/usbhost/core/hub.c b/components/drivers/usb/usbhost/core/hub.c index 56e40bb0d5..80ed9b4e3f 100644 --- a/components/drivers/usb/usbhost/core/hub.c +++ b/components/drivers/usb/usbhost/core/hub.c @@ -1,21 +1,7 @@ /* - * File : hub.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes @@ -132,7 +118,7 @@ void rt_usbh_root_hub_disconnect_handler(struct uhcd *hcd, rt_uint8_t port) rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer, rt_size_t nbytes) { struct urequest setup; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* parameter check */ RT_ASSERT(device != RT_NULL); @@ -165,7 +151,7 @@ rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint32_t* buffer) { struct urequest setup; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* parameter check */ RT_ASSERT(device != RT_NULL); @@ -198,7 +184,7 @@ rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint32_t* buffer) rt_err_t rt_usbh_hub_get_port_status(uhub_t hub, rt_uint16_t port, rt_uint32_t* buffer) { struct urequest setup; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* parameter check */ RT_ASSERT(hub != RT_NULL); @@ -240,7 +226,7 @@ rt_err_t rt_usbh_hub_get_port_status(uhub_t hub, rt_uint16_t port, rt_uint32_t* rt_err_t rt_usbh_hub_clear_port_feature(uhub_t hub, rt_uint16_t port, rt_uint16_t feature) { struct urequest setup; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* parameter check */ RT_ASSERT(hub != RT_NULL); @@ -249,7 +235,7 @@ rt_err_t rt_usbh_hub_clear_port_feature(uhub_t hub, rt_uint16_t port, rt_uint16_ if(hub->is_roothub) { root_hub_ctrl(hub->hcd, port, RH_CLEAR_PORT_FEATURE, - (void*)feature); + (void*)(rt_uint32_t)feature); return RT_EOK; } @@ -281,7 +267,7 @@ rt_err_t rt_usbh_hub_set_port_feature(uhub_t hub, rt_uint16_t port, rt_uint16_t feature) { struct urequest setup; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; /* parameter check */ RT_ASSERT(hub != RT_NULL); @@ -290,7 +276,7 @@ rt_err_t rt_usbh_hub_set_port_feature(uhub_t hub, rt_uint16_t port, if(hub->is_roothub) { root_hub_ctrl(hub->hcd, port, RH_SET_PORT_FEATURE, - (void*)feature); + (void*)(rt_uint32_t)feature); return RT_EOK; } @@ -359,6 +345,9 @@ rt_err_t rt_usbh_hub_port_debounce(uhub_t hub, rt_uint16_t port) int i = 0, times = 20; rt_uint32_t pstatus; rt_bool_t connect = RT_TRUE; + int delayticks = USB_DEBOUNCE_TIME / times; + if (delayticks < 1) + delayticks = 1; /* parameter check */ RT_ASSERT(hub != RT_NULL); @@ -374,7 +363,7 @@ rt_err_t rt_usbh_hub_port_debounce(uhub_t hub, rt_uint16_t port) break; } - rt_thread_delay(1); + rt_thread_delay(delayticks); } if(connect) return RT_EOK; @@ -413,7 +402,7 @@ static rt_err_t rt_usbh_hub_port_change(uhub_t hub) RT_DEBUG_LOG(RT_DEBUG_USB, ("port %d status 0x%x\n", i + 1, pstatus)); /* check port status change */ - if ((pstatus & PORT_CCSC)) + if (pstatus & PORT_CCSC) { /* clear port status change feature */ rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_CONNECTION); @@ -442,6 +431,7 @@ static rt_err_t rt_usbh_hub_port_change(uhub_t hub) device->speed = (pstatus & PORT_LSDA) ? 1 : 0; device->parent_hub = hub; device->hcd = hub->hcd; + device->port = i + 1; hub->child[i] = device; /* reset usb roothub port */ @@ -466,7 +456,7 @@ static void rt_usbh_hub_irq(void* context) { upipe_t pipe; uhub_t hub; - int timeout = 100; + int timeout = USB_TIMEOUT_BASIC; RT_ASSERT(context != RT_NULL); @@ -502,12 +492,12 @@ static rt_err_t rt_usbh_hub_enable(void *arg) { int i = 0; rt_err_t ret = RT_EOK; - uep_desc_t ep_desc; + uep_desc_t ep_desc = RT_NULL; uhub_t hub; struct uinstance* device; struct uhintf* intf = (struct uhintf*)arg; - upipe_t pipe_in; - int timeout = 300; + upipe_t pipe_in = RT_NULL; + int timeout = USB_TIMEOUT_LONG; /* paremeter check */ RT_ASSERT(intf != RT_NULL); diff --git a/components/drivers/usb/usbhost/core/usbhost.c b/components/drivers/usb/usbhost/core/usbhost.c index 15ac0b76cb..f0294238c4 100644 --- a/components/drivers/usb/usbhost/core/usbhost.c +++ b/components/drivers/usb/usbhost/core/usbhost.c @@ -1,21 +1,7 @@ /* - * File : usbhost.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2011, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/watchdog/watchdog.c b/components/drivers/watchdog/watchdog.c index e5453f8e43..196f02039d 100644 --- a/components/drivers/watchdog/watchdog.c +++ b/components/drivers/watchdog/watchdog.c @@ -1,21 +1,7 @@ /* - * File : watchdog.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012-2014, Shanghai Real-Thread Electronic Technology Co.,Ltd - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * COPYRIGHT (C) 2018, Real-Thread Information Technology Ltd + * + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes diff --git a/components/drivers/wlan/wlan_cfg.c b/components/drivers/wlan/wlan_cfg.c new file mode 100644 index 0000000000..27da9bc24e --- /dev/null +++ b/components/drivers/wlan/wlan_cfg.c @@ -0,0 +1,463 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-08-06 tyx the first version + */ + +#include <rtthread.h> +#include <wlan_cfg.h> + +#define DBG_ENABLE +#ifdef RT_WLAN_CFG_DEBUG +#define DBG_LEVEL DBG_LOG +#else +#define DBG_LEVEL DBG_INFO +#endif +#define DBG_SECTION_NAME "WLAN.cfg" +#define DBG_COLOR +#include <rtdbg.h> + +#define WLAN_CFG_LOCK() (rt_mutex_take(&cfg_mutex, RT_WAITING_FOREVER)) +#define WLAN_CFG_UNLOCK() (rt_mutex_release(&cfg_mutex)) + +#if RT_WLAN_CFG_INFO_MAX < 1 +#error "The minimum configuration is 1" +#endif + +struct cfg_save_info_head +{ + rt_uint32_t magic; + rt_uint32_t len; + rt_uint32_t num; + rt_uint32_t crc; +}; + +struct rt_wlan_cfg_des +{ + rt_uint32_t num; + struct rt_wlan_cfg_info *cfg_info; +}; + +static struct rt_wlan_cfg_des *cfg_cache; +static const struct rt_wlan_cfg_ops *cfg_ops; +static struct rt_mutex cfg_mutex; + +/* + * CRC16_CCITT + */ +static rt_uint16_t rt_wlan_cal_crc(rt_uint8_t *buff, int len) +{ + rt_uint16_t wCRCin = 0x0000; + rt_uint16_t wCPoly = 0x1021; + rt_uint8_t wChar = 0; + + while (len--) + { + wChar = *(buff++); + wCRCin ^= (wChar << 8); + for (int i = 0; i < 8; i++) + { + if (wCRCin & 0x8000) + wCRCin = (wCRCin << 1) ^ wCPoly; + else + wCRCin = wCRCin << 1; + } + } + return wCRCin; +} + +void rt_wlan_cfg_init(void) +{ + /* init cache memory */ + if (cfg_cache == RT_NULL) + { + cfg_cache = rt_malloc(sizeof(struct rt_wlan_cfg_des)); + if (cfg_cache != RT_NULL) + { + rt_memset(cfg_cache, 0, sizeof(struct rt_wlan_cfg_des)); + } + /* init mutex lock */ + rt_mutex_init(&cfg_mutex, "wlan_cfg", RT_IPC_FLAG_FIFO); + } +} + +void rt_wlan_cfg_set_ops(const struct rt_wlan_cfg_ops *ops) +{ + rt_wlan_cfg_init(); + + WLAN_CFG_LOCK(); + /* save ops pointer */ + cfg_ops = ops; + WLAN_CFG_UNLOCK(); +} + +/* save config data */ +rt_err_t rt_wlan_cfg_cache_save(void) +{ + rt_err_t err = RT_EOK; + struct cfg_save_info_head *info_pkg; + int len = 0; + + if ((cfg_ops == RT_NULL) || (cfg_ops->write_cfg == RT_NULL)) + return RT_EOK; + + WLAN_CFG_LOCK(); + len = sizeof(struct cfg_save_info_head) + sizeof(struct rt_wlan_cfg_info) * cfg_cache->num; + info_pkg = rt_malloc(len); + if (info_pkg == RT_NULL) + { + WLAN_CFG_UNLOCK(); + return -RT_ENOMEM; + } + info_pkg->magic = RT_WLAN_CFG_MAGIC; + info_pkg->len = len; + info_pkg->num = cfg_cache->num; + /* CRC */ + info_pkg->crc = rt_wlan_cal_crc((rt_uint8_t *)cfg_cache->cfg_info, sizeof(struct rt_wlan_cfg_info) * cfg_cache->num); + rt_memcpy(((rt_uint8_t *)info_pkg) + sizeof(struct cfg_save_info_head), + cfg_cache->cfg_info, sizeof(struct rt_wlan_cfg_info) * cfg_cache->num); + if (cfg_ops->write_cfg(info_pkg, len) != len) + err = -RT_ERROR; + rt_free(info_pkg); + WLAN_CFG_UNLOCK(); + return err; +} + +rt_err_t rt_wlan_cfg_cache_refresh(void) +{ + int len = 0, i, j; + struct cfg_save_info_head *head; + void *data; + struct rt_wlan_cfg_info *t_info, *cfg_info; + rt_uint32_t crc; + rt_bool_t equal_flag; + + /* cache is full! exit */ + if (cfg_cache == RT_NULL || cfg_cache->num >= RT_WLAN_CFG_INFO_MAX) + return -RT_ERROR; + + /* check callback */ + if ((cfg_ops == RT_NULL) || + (cfg_ops->get_len == RT_NULL) || + (cfg_ops->read_cfg == RT_NULL)) + return -RT_ERROR; + + WLAN_CFG_LOCK(); + /* get data len */ + if ((len = cfg_ops->get_len()) <= 0) + { + WLAN_CFG_UNLOCK(); + return -RT_ERROR; + } + + head = rt_malloc(len); + if (head == RT_NULL) + { + WLAN_CFG_UNLOCK(); + return -RT_ERROR; + } + /* get data */ + if (cfg_ops->read_cfg(head, len) != len) + { + rt_free(head); + WLAN_CFG_UNLOCK(); + return -RT_ERROR; + } + /* get config */ + data = ((rt_uint8_t *)head) + sizeof(struct cfg_save_info_head); + crc = rt_wlan_cal_crc((rt_uint8_t *)data, len - sizeof(struct cfg_save_info_head)); + LOG_D("head->magic:0x%08x RT_WLAN_CFG_MAGIC:0x%08x", head->magic, RT_WLAN_CFG_MAGIC); + LOG_D("head->len:%d len:%d", head->len, len); + LOG_D("head->num:%d num:%d", head->num, (len - sizeof(struct cfg_save_info_head)) / sizeof(struct rt_wlan_cfg_info)); + LOG_D("hred->crc:0x%04x crc:0x%04x", head->crc, crc); + /* check */ + if ((head->magic != RT_WLAN_CFG_MAGIC) || + (head->len != len) || + (head->num != (len - sizeof(struct cfg_save_info_head)) / sizeof(struct rt_wlan_cfg_info)) || + (head->crc != crc)) + { + rt_free(head); + WLAN_CFG_UNLOCK(); + return -RT_ERROR; + } + + /* remove duplicate config */ + cfg_info = (struct rt_wlan_cfg_info *)data; + for (i = 0; i < head->num; i++) + { + equal_flag = RT_FALSE; + for (j = 0; j < cfg_cache->num; j++) + { + if ((cfg_cache->cfg_info[j].info.ssid.len == cfg_info[i].info.ssid.len) && + (rt_memcmp(&cfg_cache->cfg_info[j].info.ssid.val[0], &cfg_info[i].info.ssid.val[0], + cfg_cache->cfg_info[j].info.ssid.len) == 0) && + (rt_memcmp(&cfg_cache->cfg_info[j].info.bssid[0], &cfg_info[i].info.bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0)) + { + equal_flag = RT_TRUE; + break; + } + } + + if (cfg_cache->num >= RT_WLAN_CFG_INFO_MAX) + { + break; + } + + if (equal_flag == RT_FALSE) + { + t_info = rt_realloc(cfg_cache->cfg_info, sizeof(struct rt_wlan_cfg_info) * (cfg_cache->num + 1)); + if (t_info == RT_NULL) + { + rt_free(head); + WLAN_CFG_UNLOCK(); + return -RT_ERROR; + } + cfg_cache->cfg_info = t_info; + cfg_cache->cfg_info[cfg_cache->num] = cfg_info[i]; + cfg_cache->num ++; + } + } + + rt_free(head); + WLAN_CFG_UNLOCK(); + return RT_EOK; +} + +int rt_wlan_cfg_get_num(void) +{ + rt_wlan_cfg_init(); + + return cfg_cache->num; +} + +int rt_wlan_cfg_read(struct rt_wlan_cfg_info *cfg_info, int num) +{ + rt_wlan_cfg_init(); + + if ((cfg_info == RT_NULL) || (num <= 0)) + return 0; + /* copy data */ + WLAN_CFG_LOCK(); + num = cfg_cache->num > num ? num : cfg_cache->num; + rt_memcpy(&cfg_cache->cfg_info[0], cfg_info, sizeof(struct rt_wlan_cfg_info) * num); + WLAN_CFG_UNLOCK(); + + return num; +} + +rt_err_t rt_wlan_cfg_save(struct rt_wlan_cfg_info *cfg_info) +{ + rt_err_t err = RT_EOK; + struct rt_wlan_cfg_info *t_info; + int idx = -1, i = 0; + + rt_wlan_cfg_init(); + + /* parameter check */ + if ((cfg_info == RT_NULL) || (cfg_info->info.ssid.len == 0)) + { + return -RT_EINVAL; + } + /* if (iteam == cache) exit */ + WLAN_CFG_LOCK(); + for (i = 0; i < cfg_cache->num; i++) + { + if ((cfg_cache->cfg_info[i].info.ssid.len == cfg_info->info.ssid.len) && + (rt_memcmp(&cfg_cache->cfg_info[i].info.ssid.val[0], &cfg_info->info.ssid.val[0], + cfg_cache->cfg_info[i].info.ssid.len) == 0) && + (rt_memcmp(&cfg_cache->cfg_info[i].info.bssid[0], &cfg_info->info.bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0)) + { + idx = i; + break; + } + } + + if ((idx == 0) && (cfg_cache->cfg_info[i].key.len == cfg_info->key.len) && + (rt_memcmp(&cfg_cache->cfg_info[i].key.val[0], &cfg_info->key.val[0], cfg_info->key.len) == 0)) + { + WLAN_CFG_UNLOCK(); + return RT_EOK; + } + + /* not find iteam with cache, Add iteam to the head */ + if ((idx == -1) && (cfg_cache->num < RT_WLAN_CFG_INFO_MAX)) + { + t_info = rt_realloc(cfg_cache->cfg_info, sizeof(struct rt_wlan_cfg_info) * (cfg_cache->num + 1)); + if (t_info == RT_NULL) + { + WLAN_CFG_UNLOCK(); + return -RT_ENOMEM; + } + cfg_cache->cfg_info = t_info; + cfg_cache->num ++; + } + + /* move cache info */ + i = (i >= RT_WLAN_CFG_INFO_MAX ? RT_WLAN_CFG_INFO_MAX - 1 : i); + for (; i; i--) + { + cfg_cache->cfg_info[i] = cfg_cache->cfg_info[i - 1]; + } + /* add iteam to head */ + cfg_cache->cfg_info[i] = *cfg_info; + WLAN_CFG_UNLOCK(); + + /* save info to flash */ + err = rt_wlan_cfg_cache_save(); + + return err; +} + +int rt_wlan_cfg_read_index(struct rt_wlan_cfg_info *cfg_info, int index) +{ + rt_wlan_cfg_init(); + + if ((cfg_info == RT_NULL) || (index < 0)) + return 0; + + WLAN_CFG_LOCK(); + if (index >= cfg_cache->num) + { + WLAN_CFG_UNLOCK(); + return 0; + } + /* copy data */ + *cfg_info = cfg_cache->cfg_info[index]; + WLAN_CFG_UNLOCK(); + return 1; +} + +int rt_wlan_cfg_delete_index(int index) +{ + struct rt_wlan_cfg_info *cfg_info; + int i; + + rt_wlan_cfg_init(); + + if (index < 0) + return -1; + + WLAN_CFG_LOCK(); + if (index >= cfg_cache->num) + { + WLAN_CFG_UNLOCK(); + return -1; + } + + /* malloc new mem */ + cfg_info = rt_malloc(sizeof(struct rt_wlan_cfg_info) * (cfg_cache->num - 1)); + if (cfg_info == RT_NULL) + { + WLAN_CFG_UNLOCK(); + return -1; + } + /* copy data to new mem */ + for (i = 0; i < cfg_cache->num; i++) + { + if (i < index) + { + cfg_info[i] = cfg_cache->cfg_info[i]; + } + else if (i > index) + { + cfg_info[i - 1] = cfg_cache->cfg_info[i]; + } + } + rt_free(cfg_cache->cfg_info); + cfg_cache->cfg_info = cfg_info; + cfg_cache->num --; + WLAN_CFG_UNLOCK(); + + return 0; +} + +void rt_wlan_cfg_delete_all(void) +{ + rt_wlan_cfg_init(); + + /* delete all iteam */ + WLAN_CFG_LOCK(); + cfg_cache->num = 0; + rt_free(cfg_cache->cfg_info); + cfg_cache->cfg_info = RT_NULL; + WLAN_CFG_UNLOCK(); +} + +void rt_wlan_cfg_dump(void) +{ + int index = 0; + struct rt_wlan_info *info; + struct rt_wlan_key *key; + char *security; + + rt_wlan_cfg_init(); + + rt_kprintf(" SSID PASSWORD MAC security chn\n"); + rt_kprintf("------------------------------- ------------------------------- ----------------- -------------- ---\n"); + for (index = 0; index < cfg_cache->num; index ++) + { + info = &cfg_cache->cfg_info[index].info; + key = &cfg_cache->cfg_info[index].key; + + if (info->ssid.len) + rt_kprintf("%-32.32s", &info->ssid.val[0]); + else + rt_kprintf("%-32.32s", " "); + + if (key->len) + rt_kprintf("%-32.32s", &key->val[0]); + else + rt_kprintf("%-32.32s", " "); + + rt_kprintf("%02x:%02x:%02x:%02x:%02x:%02x ", + info->bssid[0], + info->bssid[1], + info->bssid[2], + info->bssid[3], + info->bssid[4], + info->bssid[5] + ); + switch (info->security) + { + case SECURITY_OPEN: + security = "OPEN"; + break; + case SECURITY_WEP_PSK: + security = "WEP_PSK"; + break; + case SECURITY_WEP_SHARED: + security = "WEP_SHARED"; + break; + case SECURITY_WPA_TKIP_PSK: + security = "WPA_TKIP_PSK"; + break; + case SECURITY_WPA_AES_PSK: + security = "WPA_AES_PSK"; + break; + case SECURITY_WPA2_AES_PSK: + security = "WPA2_AES_PSK"; + break; + case SECURITY_WPA2_TKIP_PSK: + security = "WPA2_TKIP_PSK"; + break; + case SECURITY_WPA2_MIXED_PSK: + security = "WPA2_MIXED_PSK"; + break; + case SECURITY_WPS_OPEN: + security = "WPS_OPEN"; + break; + case SECURITY_WPS_SECURE: + security = "WPS_SECURE"; + break; + default: + security = "UNKNOWN"; + break; + } + rt_kprintf("%-14.14s ", security); + rt_kprintf("%3d \n", info->channel); + } +} diff --git a/components/drivers/wlan/wlan_cfg.h b/components/drivers/wlan/wlan_cfg.h new file mode 100644 index 0000000000..3fae3f3e98 --- /dev/null +++ b/components/drivers/wlan/wlan_cfg.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-08-06 tyx the first version + */ + +#ifndef __WLAN_CFG_H__ +#define __WLAN_CFG_H__ + +#include <wlan_dev.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef RT_WLAN_CFG_INFO_MAX +#define RT_WLAN_CFG_INFO_MAX (3) /* min is 1 */ +#endif + +#define RT_WLAN_CFG_MAGIC (0x426f6d62) + +struct rt_wlan_cfg_info +{ + struct rt_wlan_info info; + struct rt_wlan_key key; +}; + +typedef int (*rt_wlan_wr)(void *buff, int len); + +struct rt_wlan_cfg_ops +{ + int (*read_cfg)(void *buff, int len); + int (*get_len)(void); + int (*write_cfg)(void *buff, int len); +}; + +void rt_wlan_cfg_init(void); + +void rt_wlan_cfg_set_ops(const struct rt_wlan_cfg_ops *ops); + +int rt_wlan_cfg_get_num(void); + +int rt_wlan_cfg_read(struct rt_wlan_cfg_info *cfg_info, int num); + +int rt_wlan_cfg_read_index(struct rt_wlan_cfg_info *cfg_info, int index); + +rt_err_t rt_wlan_cfg_save(struct rt_wlan_cfg_info *cfg_info); + +rt_err_t rt_wlan_cfg_cache_refresh(void); + +rt_err_t rt_wlan_cfg_cache_save(void); + +int rt_wlan_cfg_delete_index(int index); + +void rt_wlan_cfg_delete_all(void); + +void rt_wlan_cfg_dump(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/components/drivers/wlan/wlan_cmd.c b/components/drivers/wlan/wlan_cmd.c index eda628a357..b962b63947 100644 --- a/components/drivers/wlan/wlan_cmd.c +++ b/components/drivers/wlan/wlan_cmd.c @@ -1,527 +1,566 @@ /* - * File : wlan_cmd.c - * Wi-Fi common commands - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team + * Copyright (c) 2006-2018, RT-Thread Development Team * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes - * 2016-03-12 Bernard first version + * 2018-08-13 tyx the first version */ #include <rtthread.h> -#include <wlan_dev.h> - -#include <finsh.h> - -#include <lwip/dhcp.h> -#include "wlan_cmd.h" - -#ifdef LWIP_USING_DHCPD -#include <dhcp_server.h> -#endif - -struct rt_wlan_info info; -static char wifi_ssid[32] = {0}; -static char wifi_key[32] = {0}; -static int network_mode = WIFI_STATION; +#include <wlan_mgnt.h> +#include <wlan_cfg.h> +#include <wlan_prot.h> -#define WLAN_DEBUG 1 -#if WLAN_DEBUG -#define WLAN_DBG(...) rt_kprintf("[WLAN]"),rt_kprintf(__VA_ARGS__) -#else -#define WLAN_DBG(...) -#endif +struct wifi_cmd_des +{ + const char *cmd; + int (*fun)(int argc, char *argv[]); +}; -#ifndef WIFI_SETTING_FN -#define WIFI_SETTING_FN "/appfs/setting.json" -#endif +static int wifi_help(int argc, char *argv[]); +static int wifi_scan(int argc, char *argv[]); +static int wifi_status(int argc, char *argv[]); +static int wifi_join(int argc, char *argv[]); +static int wifi_ap(int argc, char *argv[]); +static int wifi_list_sta(int argc, char *argv[]); +static int wifi_disconnect(int argc, char *argv[]); +static int wifi_ap_stop(int argc, char *argv[]); -#ifndef WIFI_DEVICE_STA_NAME -#define WIFI_DEVICE_STA_NAME "w0" -#endif -#ifndef WIFI_DEVICE_AP_NAME -#define WIFI_DEVICE_AP_NAME "ap" +#ifdef RT_WLAN_CMD_DEBUG +/* just for debug */ +static int wifi_debug(int argc, char *argv[]); +static int wifi_debug_save_cfg(int argc, char *argv[]); +static int wifi_debug_dump_cfg(int argc, char *argv[]); +static int wifi_debug_clear_cfg(int argc, char *argv[]); +static int wifi_debug_dump_prot(int argc, char *argv[]); +static int wifi_debug_set_mode(int argc, char *argv[]); +static int wifi_debug_set_prot(int argc, char *argv[]); +static int wifi_debug_set_autoconnect(int argc, char *argv[]); #endif -#ifdef RT_USING_DFS -#include <dfs_posix.h> -#ifdef PKG_USING_CJSON -#include <cJSON_util.h> +/* cmd table */ +static const struct wifi_cmd_des cmd_tab[] = +{ + {"scan", wifi_scan}, + {"help", wifi_help}, + {"status", wifi_status}, + {"join", wifi_join}, + {"ap", wifi_ap}, + {"list_sta", wifi_list_sta}, + {"disc", wifi_disconnect}, + {"ap_stop", wifi_ap_stop}, + {"smartconfig", RT_NULL}, +#ifdef RT_WLAN_CMD_DEBUG + {"-d", wifi_debug}, #endif +}; -int wifi_get_mode(void) +#ifdef RT_WLAN_CMD_DEBUG +/* debug cmd table */ +static const struct wifi_cmd_des debug_tab[] = { - return network_mode; -} + {"save_cfg", wifi_debug_save_cfg}, + {"dump_cfg", wifi_debug_dump_cfg}, + {"clear_cfg", wifi_debug_clear_cfg}, + {"dump_prot", wifi_debug_dump_prot}, + {"mode", wifi_debug_set_mode}, + {"prot", wifi_debug_set_prot}, + {"auto", wifi_debug_set_autoconnect}, +}; +#endif -int wifi_set_mode(int mode) +static int wifi_help(int argc, char *argv[]) { - network_mode = mode; - - return network_mode; + rt_kprintf("wifi\n"); + rt_kprintf("wifi help\n"); + rt_kprintf("wifi scan\n"); + rt_kprintf("wifi join [SSID] [PASSWORD]\n"); + rt_kprintf("wifi ap SSID [PASSWORD]\n"); + rt_kprintf("wifi disc\n"); + rt_kprintf("wifi ap_stop\n"); + rt_kprintf("wifi status\n"); + rt_kprintf("wifi smartconfig\n"); +#ifdef RT_WLAN_CMD_DEBUG + rt_kprintf("wifi -d debug command\n"); +#endif + return 0; } -int wifi_set_setting(const char *ssid, const char *pwd) +static int wifi_status(int argc, char *argv[]) { - if (!ssid) return -1; + int rssi; + struct rt_wlan_info info; - strncpy(wifi_ssid, ssid, sizeof(wifi_ssid)); - wifi_ssid[sizeof(wifi_ssid) - 1] = '\0'; + if (argc > 2) + return -1; - if (pwd) + if (rt_wlan_is_connected() == 1) + { + rssi = rt_wlan_get_rssi(); + rt_wlan_get_info(&info); + rt_kprintf("Wi-Fi STA Info\n"); + rt_kprintf("SSID : %-.32s\n", &info.ssid.val[0]); + rt_kprintf("MAC Addr: %02x:%02x:%02x:%02x:%02x:%02x\n", info.bssid[0], + info.bssid[1], + info.bssid[2], + info.bssid[3], + info.bssid[4], + info.bssid[5]); + rt_kprintf("Channel: %d\n", info.channel); + rt_kprintf("DataRate: %dMbps\n", info.datarate / 1000000); + rt_kprintf("RSSI: %d\n", rssi); + } + else { - strncpy(wifi_key, pwd, sizeof(wifi_key)); - wifi_key[sizeof(wifi_key) - 1] = '\0'; + rt_kprintf("wifi disconnected!\n"); } - else wifi_key[0] = '\0'; + if (rt_wlan_ap_is_active() == 1) + { + rt_wlan_ap_get_info(&info); + rt_kprintf("Wi-Fi AP Info\n"); + rt_kprintf("SSID : %-.32s\n", &info.ssid.val[0]); + rt_kprintf("MAC Addr: %02x:%02x:%02x:%02x:%02x:%02x\n", info.bssid[0], + info.bssid[1], + info.bssid[2], + info.bssid[3], + info.bssid[4], + info.bssid[5]); + rt_kprintf("Channel: %d\n", info.channel); + rt_kprintf("DataRate: %dMbps\n", info.datarate / 1000000); + rt_kprintf("hidden: %s\n", info.hidden ? "Enable" : "Disable"); + } + else + { + rt_kprintf("wifi ap not start!\n"); + } + rt_kprintf("Auto Connect status:%s!\n", (rt_wlan_get_autoreconnect_mode() ? "Enable" : "Disable")); return 0; } -#ifdef PKG_USING_CJSON -int wifi_read_cfg(const char *filename) +static int wifi_scan(int argc, char *argv[]) { - int fd; - cJSON *json = RT_NULL; + struct rt_wlan_scan_result *scan_result = RT_NULL; - fd = open(filename, O_RDONLY, 0); - if (fd < 0) - { - /* no setting file */ + if (argc > 2) return -1; - } - if (fd >= 0) + /* scan ap info */ + scan_result = rt_wlan_scan_sync(); + if (scan_result) { - int length; + int index, num; + char *security; - length = lseek(fd, 0, SEEK_END); - if (length) + num = scan_result->num; + rt_kprintf(" SSID MAC security rssi chn Mbps\n"); + rt_kprintf("------------------------------- ----------------- -------------- ---- --- ----\n"); + for (index = 0; index < num; index ++) { - char *json_str = (char *) rt_malloc(length); - if (json_str) + rt_kprintf("%-32.32s", &scan_result->info[index].ssid.val[0]); + rt_kprintf("%02x:%02x:%02x:%02x:%02x:%02x ", + scan_result->info[index].bssid[0], + scan_result->info[index].bssid[1], + scan_result->info[index].bssid[2], + scan_result->info[index].bssid[3], + scan_result->info[index].bssid[4], + scan_result->info[index].bssid[5] + ); + switch (scan_result->info[index].security) { - lseek(fd, 0, SEEK_SET); - read(fd, json_str, length); - - json = cJSON_Parse(json_str); - rt_free(json_str); + case SECURITY_OPEN: + security = "OPEN"; + break; + case SECURITY_WEP_PSK: + security = "WEP_PSK"; + break; + case SECURITY_WEP_SHARED: + security = "WEP_SHARED"; + break; + case SECURITY_WPA_TKIP_PSK: + security = "WPA_TKIP_PSK"; + break; + case SECURITY_WPA_AES_PSK: + security = "WPA_AES_PSK"; + break; + case SECURITY_WPA2_AES_PSK: + security = "WPA2_AES_PSK"; + break; + case SECURITY_WPA2_TKIP_PSK: + security = "WPA2_TKIP_PSK"; + break; + case SECURITY_WPA2_MIXED_PSK: + security = "WPA2_MIXED_PSK"; + break; + case SECURITY_WPS_OPEN: + security = "WPS_OPEN"; + break; + case SECURITY_WPS_SECURE: + security = "WPS_SECURE"; + break; + default: + security = "UNKNOWN"; + break; } + rt_kprintf("%-14.14s ", security); + rt_kprintf("%-4d ", scan_result->info[index].rssi); + rt_kprintf("%3d ", scan_result->info[index].channel); + rt_kprintf("%4d\n", scan_result->info[index].datarate / 1000000); } - close(fd); + rt_wlan_scan_result_clean(); } - - if (json) + else { - cJSON *wifi = cJSON_GetObjectItem(json, "wifi"); - cJSON *ssid = cJSON_GetObjectItem(wifi, "SSID"); - cJSON *key = cJSON_GetObjectItem(wifi, "Key"); - cJSON *mode = cJSON_GetObjectItem(wifi, "Mode"); + rt_kprintf("wifi scan result is null\n"); + } + return 0; +} - if (ssid) - { - memset(wifi_ssid, 0x0, sizeof(wifi_ssid)); - rt_strncpy(wifi_ssid, ssid->valuestring, sizeof(wifi_ssid) - 1); - } +static int wifi_join(int argc, char *argv[]) +{ + const char *ssid = RT_NULL; + const char *key = RT_NULL; + struct rt_wlan_cfg_info cfg_info; - if (key) + if (argc == 2) + { + /* get info to connect */ + if (rt_wlan_cfg_read_index(&cfg_info, 0) == 1) { - memset(wifi_key, 0x0, sizeof(wifi_key)); - rt_strncpy(wifi_key, key->valuestring, sizeof(wifi_key) - 1); + ssid = (char *)(&cfg_info.info.ssid.val[0]); + if (cfg_info.key.len) + key = (char *)(&cfg_info.key.val[0]); } - - if (mode) + else { - network_mode = mode->valueint; + rt_kprintf("not find info\n"); } - - cJSON_Delete(json); } - + else if (argc == 3) + { + /* ssid */ + ssid = argv[2]; + } + else if (argc == 4) + { + ssid = argv[2]; + /* password */ + key = argv[3]; + } + else + { + return -1; + } + rt_wlan_connect(ssid, key); return 0; } -int wifi_save_cfg(const char *filename) +static int wifi_ap(int argc, char *argv[]) { - int fd; - cJSON *json = RT_NULL; + const char *ssid = RT_NULL; + const char *key = RT_NULL; - fd = open(filename, O_RDONLY, 0); - if (fd >= 0) + if (argc == 3) { - int length; - - length = lseek(fd, 0, SEEK_END); - if (length) - { - char *json_str = (char *) rt_malloc(length); - if (json_str) - { - lseek(fd, 0, SEEK_SET); - read(fd, json_str, length); - - json = cJSON_Parse(json_str); - rt_free(json_str); - } - } - close(fd); + ssid = argv[2]; + } + else if (argc == 4) + { + ssid = argv[2]; + key = argv[3]; } else { - /* create a new setting.json */ - fd = open(filename, O_WRONLY | O_TRUNC, 0); - if (fd >= 0) - { - json = cJSON_CreateObject(); - if (json) - { - cJSON *wifi = cJSON_CreateObject(); - - if (wifi) - { - char *json_str; + return -1; + } - cJSON_AddItemToObject(json, "wifi", wifi); - cJSON_AddStringToObject(wifi, "SSID", wifi_ssid); - cJSON_AddStringToObject(wifi, "Key", wifi_key); - cJSON_AddNumberToObject(wifi, "Mode", network_mode); + rt_wlan_start_ap(ssid, key); + return 0; +} - json_str = cJSON_Print(json); - if (json_str) - { - write(fd, json_str, rt_strlen(json_str)); - cJSON_free(json_str); - } - } - } - } - close(fd); +static int wifi_list_sta(int argc, char *argv[]) +{ + struct rt_wlan_info *sta_info; + int num, i; + if (argc > 2) + return -1; + num = rt_wlan_ap_get_sta_num(); + sta_info = rt_malloc(sizeof(struct rt_wlan_info) * num); + if (sta_info == RT_NULL) + { + rt_kprintf("num:%d\n", num); return 0; } - - if (json) + rt_wlan_ap_get_sta_info(sta_info, num); + rt_kprintf("num:%d\n", num); + for (i = 0; i < num; i++) { - cJSON *wifi = cJSON_GetObjectItem(json, "wifi"); - if (!wifi) - { - wifi = cJSON_CreateObject(); - cJSON_AddItemToObject(json, "wifi", wifi); - } - - if (cJSON_GetObjectItem(wifi, "SSID"))cJSON_ReplaceItemInObject(wifi, "SSID", cJSON_CreateString(wifi_ssid)); - else cJSON_AddStringToObject(wifi, "SSID", wifi_ssid); - if (cJSON_GetObjectItem(wifi, "Key")) cJSON_ReplaceItemInObject(wifi, "Key", cJSON_CreateString(wifi_key)); - else cJSON_AddStringToObject(wifi, "Key", wifi_key); - if (cJSON_GetObjectItem(wifi, "Mode")) cJSON_ReplaceItemInObject(wifi, "Mode", cJSON_CreateNumber(network_mode)); - else cJSON_AddNumberToObject(wifi, "Mode", network_mode); - - fd = open(filename, O_WRONLY | O_TRUNC, 0); - if (fd >= 0) - { - char *json_str = cJSON_Print(json); - if (json_str) - { - write(fd, json_str, rt_strlen(json_str)); - cJSON_free(json_str); - } - close(fd); - } - cJSON_Delete(json); + rt_kprintf("sta mac %02x:%02x:%02x:%02x:%02x:%02x\n", + sta_info[i].bssid[0], sta_info[i].bssid[1], sta_info[i].bssid[2], + sta_info[i].bssid[3], sta_info[i].bssid[4], sta_info[i].bssid[5]); } - + rt_free(sta_info); return 0; } -#endif -int wifi_save_setting(void) +static int wifi_disconnect(int argc, char *argv[]) { -#ifdef PKG_USING_CJSON - wifi_save_cfg(WIFI_SETTING_FN); -#endif + if (argc != 2) + { + return -1; + } + rt_wlan_disconnect(); return 0; } -#endif -int wifi_softap_setup_netif(struct netif *netif) +static int wifi_ap_stop(int argc, char *argv[]) { - if (netif) + if (argc != 2) { -#ifdef RT_LWIP_DHCP - /* Stop DHCP Client */ - dhcp_stop(netif); -#endif - -#ifdef LWIP_USING_DHCPD - { - char name[8]; - memset(name, 0, sizeof(name)); - strncpy(name, netif->name, sizeof(name) > sizeof(netif->name) ? sizeof(netif->name) : sizeof(name)); - dhcpd_start(name); - } -#endif + return -1; } + rt_wlan_ap_stop(); return 0; } -int wifi_default(void) +#ifdef RT_WLAN_CMD_DEBUG +/* just for debug */ +static int wifi_debug_help(int argc, char *argv[]) { - int result = 0; - struct rt_wlan_device *wlan; + rt_kprintf("save_cfg ssid [password]\n"); + rt_kprintf("dump_cfg\n"); + rt_kprintf("clear_cfg\n"); + rt_kprintf("dump_prot\n"); + rt_kprintf("mode sta/ap dev_name\n"); + rt_kprintf("prot lwip dev_name\n"); + rt_kprintf("auto enable/disable\n"); + return 0; +} -#ifdef PKG_USING_CJSON - /* read default setting for wifi */ - wifi_read_cfg(WIFI_SETTING_FN); -#endif +static int wifi_debug_save_cfg(int argc, char *argv[]) +{ + struct rt_wlan_cfg_info cfg_info; + int len; + char *ssid = RT_NULL, *password = RT_NULL; - if (network_mode == WIFI_STATION) + rt_memset(&cfg_info, 0, sizeof(cfg_info)); + INVALID_INFO(&cfg_info.info); + if (argc == 2) { - /* get wlan device */ - wlan = (struct rt_wlan_device *)rt_device_find(WIFI_DEVICE_STA_NAME); - if (!wlan) - { - rt_kprintf("no wlan:%s device\n", WIFI_DEVICE_STA_NAME); - return -1; - } - - /* wifi station */ - rt_wlan_info_init(&info, WIFI_STATION, SECURITY_WPA2_MIXED_PSK, wifi_ssid); - result = rt_wlan_init(wlan, WIFI_STATION); - if (result == RT_EOK) - { - result = rt_wlan_connect(wlan, &info, wifi_key); - } + ssid = argv[1]; + } + else if (argc == 3) + { + ssid = argv[1]; + password = argv[2]; } else { - /* wifi AP */ - /* get wlan device */ - wlan = (struct rt_wlan_device *)rt_device_find(WIFI_DEVICE_AP_NAME); - if (!wlan) + return -1; + } + + if (ssid != RT_NULL) + { + len = rt_strlen(ssid); + if (len > RT_WLAN_SSID_MAX_LENGTH) { - rt_kprintf("no wlan:%s device\n", WIFI_DEVICE_AP_NAME); - return -1; + rt_kprintf("ssid is to long"); + return 0; } + rt_memcpy(&cfg_info.info.ssid.val[0], ssid, len); + cfg_info.info.ssid.len = len; + } - rt_wlan_info_init(&info, WIFI_AP, SECURITY_WPA2_AES_PSK, wifi_ssid); - info.channel = 11; - - /* wifi soft-AP */ - result = rt_wlan_init(wlan, WIFI_AP); - if (result == RT_EOK) + if (password != RT_NULL) + { + len = rt_strlen(password); + if (len > RT_WLAN_PASSWORD_MAX_LENGTH) { - result = rt_wlan_softap(wlan, &info, wifi_key); + rt_kprintf("password is to long"); + return 0; } + rt_memcpy(&cfg_info.key.val[0], password, len); + cfg_info.key.len = len; } - return result; + rt_wlan_cfg_save(&cfg_info); + return 0; } -static void wifi_usage(void) +static int wifi_debug_dump_cfg(int argc, char *argv[]) { - rt_kprintf("wifi help - Help information\n"); - rt_kprintf("wifi cfg SSID PASSWORD - Setting your router AP ssid and pwd\n"); - rt_kprintf("wifi - Do the default wifi action\n"); - rt_kprintf("wifi wlan_dev scan\n"); - rt_kprintf("wifi wlan_dev join SSID PASSWORD\n"); - rt_kprintf("wifi wlan_dev ap SSID [PASSWORD]\n"); - rt_kprintf("wifi wlan_dev up\n"); - rt_kprintf("wifi wlan_dev down\n"); - rt_kprintf("wifi wlan_dev rssi\n"); - rt_kprintf("wifi wlan_dev status\n"); + if (argc == 1) + { + rt_wlan_cfg_dump(); + } + else + { + return -1; + } + return 0; } -int wifi(int argc, char **argv) +static int wifi_debug_clear_cfg(int argc, char *argv[]) { - struct rt_wlan_device *wlan; - if (argc == 1) { - wifi_default(); - return 0; + rt_wlan_cfg_delete_all(); + rt_wlan_cfg_cache_save(); } - - if (strcmp(argv[1], "help") == 0) + else { - wifi_usage(); - return 0; + return -1; } + return 0; +} - if (strcmp(argv[1], "cfg") == 0) +static int wifi_debug_dump_prot(int argc, char *argv[]) +{ + if (argc == 1) { - /* configure wifi setting */ - memset(wifi_ssid, 0x0, sizeof(wifi_ssid)); - rt_strncpy(wifi_ssid, argv[2], sizeof(wifi_ssid) - 1); - - memset(wifi_key, 0x0, sizeof(wifi_key)); - rt_strncpy(wifi_key, argv[3], sizeof(wifi_key) - 1); + rt_wlan_prot_dump(); + } + else + { + return -1; + } + return 0; +} - network_mode = WIFI_STATION; +static int wifi_debug_set_mode(int argc, char *argv[]) +{ + rt_wlan_mode_t mode; -#ifdef PKG_USING_CJSON - wifi_save_cfg(WIFI_SETTING_FN); -#endif + if (argc != 3) + return -1; - return 0; + if (rt_strcmp("sta", argv[1]) == 0) + { + mode = RT_WLAN_STATION; } - - /* get wlan device */ - wlan = (struct rt_wlan_device *)rt_device_find(argv[1]); - if (!wlan) + else if (rt_strcmp("ap", argv[1]) == 0) { - rt_kprintf("no wlan:%s device\n", argv[1]); - return 0; + mode = RT_WLAN_AP; } + else + return -1; - if (argc < 3) + rt_wlan_set_mode(argv[2], mode); + return 0; +} + +static int wifi_debug_set_prot(int argc, char *argv[]) +{ + if (argc != 3) { - wifi_usage(); - return 0; + return -1; } - if (strcmp(argv[2], "join") == 0) - { - rt_wlan_init(wlan, WIFI_STATION); - network_mode = WIFI_STATION; + rt_wlan_prot_attach(argv[2], argv[1]); + return 0; +} - /* TODO: use easy-join to replace */ - rt_wlan_info_init(&info, WIFI_STATION, SECURITY_WPA2_MIXED_PSK, argv[3]); - rt_wlan_connect(wlan, &info, argv[4]); - rt_wlan_info_deinit(&info); - } - else if (strcmp(argv[2], "up") == 0) +static int wifi_debug_set_autoconnect(int argc, char *argv[]) +{ + if (argc == 2) { - /* the key was saved in wlan device */ - rt_wlan_connect(wlan, RT_NULL, wlan->key); + if (rt_strcmp(argv[1], "enable") == 0) + rt_wlan_config_autoreconnect(RT_TRUE); + else if (rt_strcmp(argv[1], "disable") == 0) + rt_wlan_config_autoreconnect(RT_FALSE); } - else if (strcmp(argv[2], "down") == 0) + else { - rt_wlan_disconnect(wlan); - rt_wlan_info_deinit(&info); + return -1; } - else if (strcmp(argv[2], "scan") == 0) - { - struct rt_wlan_scan_result *scan_result = RT_NULL; + return 0; +} - rt_wlan_scan(wlan, &scan_result); - if (scan_result) - { - int index, num; +static int wifi_debug(int argc, char *argv[]) +{ + int i, result = 0; + const struct wifi_cmd_des *run_cmd = RT_NULL; - num = scan_result->ap_num; - rt_kprintf(" SSID MAC rssi chn Mbps\n"); - rt_kprintf("------------------------------- ----------------- ---- --- ----\n"); - for (index = 0; index < num; index ++) - { - rt_kprintf("%-32.32s", scan_result->ap_table[index].ssid); - rt_kprintf("%02x:%02x:%02x:%02x:%02x:%02x ", - scan_result->ap_table[index].bssid[0], - scan_result->ap_table[index].bssid[1], - scan_result->ap_table[index].bssid[2], - scan_result->ap_table[index].bssid[3], - scan_result->ap_table[index].bssid[4], - scan_result->ap_table[index].bssid[5] - ); - rt_kprintf("%4d ", scan_result->ap_table[index].rssi); - rt_kprintf("%2d ", scan_result->ap_table[index].channel); - rt_kprintf("%d\n", scan_result->ap_table[index].datarate / 1000000); - } - } - rt_wlan_release_scan_result(&scan_result); - } - else if (strcmp(argv[2], "rssi") == 0) + if (argc < 3) { - int rssi; - - rssi = rt_wlan_get_rssi(wlan); - rt_kprintf("rssi=%d\n", rssi); + wifi_debug_help(0, RT_NULL); + return 0; } - else if (strcmp(argv[2], "ap") == 0) - { - rt_err_t result = RT_EOK; - if (argc == 4) + for (i = 0; i < sizeof(debug_tab) / sizeof(debug_tab[0]); i++) + { + if (rt_strcmp(debug_tab[i].cmd, argv[2]) == 0) { - // open soft-AP - rt_wlan_info_init(&info, WIFI_AP, SECURITY_OPEN, argv[3]); - info.channel = 11; - - result = rt_wlan_init(wlan, WIFI_AP); - /* start soft ap */ - result = rt_wlan_softap(wlan, &info, NULL); - if (result == RT_EOK) - { - network_mode = WIFI_AP; - } + run_cmd = &debug_tab[i]; + break; } - else if (argc == 5) - { - // WPA2 with password - rt_wlan_info_init(&info, WIFI_AP, SECURITY_WPA2_AES_PSK, argv[3]); - info.channel = 11; + } - result = rt_wlan_init(wlan, WIFI_AP); - /* start soft ap */ - result = rt_wlan_softap(wlan, &info, argv[4]); - if (result == RT_EOK) - { - network_mode = WIFI_AP; - } - } - else - { - wifi_usage(); - } + if (run_cmd == RT_NULL) + { + wifi_debug_help(0, RT_NULL); + return 0; + } - if (result != RT_EOK) - { - rt_kprintf("wifi start failed! result=%d\n", result); - } + if (run_cmd->fun != RT_NULL) + { + result = run_cmd->fun(argc - 2, &argv[2]); } - else if (strcmp(argv[2], "status") == 0) + + if (result) { - int rssi; + wifi_debug_help(argc - 2, &argv[2]); + } + return 0; +} +#endif - if (netif_is_link_up(wlan->parent.netif)) - { - rssi = rt_wlan_get_rssi(wlan); +static int wifi_msh(int argc, char *argv[]) +{ + int i, result = 0; + const struct wifi_cmd_des *run_cmd = RT_NULL; - rt_kprintf("Wi-Fi AP: %-.32s\n", wlan->info->ssid); - rt_kprintf("MAC Addr: %02x:%02x:%02x:%02x:%02x:%02x\n", wlan->info->bssid[0], - wlan->info->bssid[1], - wlan->info->bssid[2], - wlan->info->bssid[3], - wlan->info->bssid[4], - wlan->info->bssid[5]); - rt_kprintf(" Channel: %d\n", wlan->info->channel); - rt_kprintf("DataRate: %dMbps\n", wlan->info->datarate / 1000000); - rt_kprintf(" RSSI: %d\n", rssi); - } - else + if (argc == 1) + { + wifi_help(argc, argv); + return 0; + } + + /* find fun */ + for (i = 0; i < sizeof(cmd_tab) / sizeof(cmd_tab[0]); i++) + { + if (rt_strcmp(cmd_tab[i].cmd, argv[1]) == 0) { - rt_kprintf("wifi disconnected!\n"); + run_cmd = &cmd_tab[i]; + break; } + } + /* not find fun, print help */ + if (run_cmd == RT_NULL) + { + wifi_help(argc, argv); return 0; } + /* run fun */ + if (run_cmd->fun != RT_NULL) + { + result = run_cmd->fun(argc, argv); + } + + if (result) + { + wifi_help(argc, argv); + } return 0; } -MSH_CMD_EXPORT(wifi, wifi command); + +#if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) +FINSH_FUNCTION_EXPORT_ALIAS(wifi_msh, __cmd_wifi, wifi command.); +#endif diff --git a/components/drivers/wlan/wlan_cmd.h b/components/drivers/wlan/wlan_cmd.h deleted file mode 100644 index a981601f5e..0000000000 --- a/components/drivers/wlan/wlan_cmd.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * File : wlan_cmd.h - * Wi-Fi common commands - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Change Logs: - * Date Author Notes - * 2018-06-05 RT-Thread first version - */ - -#ifndef WLAN_CMD_H__ -#define WLAN_CMD_H__ - -struct netif; - -int wifi_get_mode(void); -int wifi_set_mode(int mode); - -/* do the wifi default action: read wifi setting and then join or start soft-AP */ -int wifi_default(void); -/* setup netif for soft-ap */ -int wifi_softap_setup_netif(struct netif *netif); - -int wifi_set_setting(const char *ssid, const char *pwd); - -#ifdef PKG_USING_CJSON -int wifi_read_cfg(const char *filename); -int wifi_save_cfg(const char *filename); -#endif -/* save wifi setting with default storage file */ -int wifi_save_setting(void); - -extern struct rt_wlan_info info; - -#endif diff --git a/components/drivers/wlan/wlan_dev.c b/components/drivers/wlan/wlan_dev.c index a42072692c..d86dfb25d3 100644 --- a/components/drivers/wlan/wlan_dev.c +++ b/components/drivers/wlan/wlan_dev.c @@ -1,328 +1,773 @@ /* - * RT-Thread Wi-Fi Device + * Copyright (c) 2006-2018, RT-Thread Development Team * - * COPYRIGHT (C) 2014 - 2018, Shanghai Real-Thread Technology Co., Ltd - * - * This file is part of RT-Thread (http://www.rt-thread.org) - * - * All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes - * 2014-09-11 Bernard the first verion + * 2018-08-03 tyx the first version */ +#include <rthw.h> #include <rtthread.h> -#include <rtdevice.h> +#include <wlan_dev.h> +#include <wlan_prot.h> + +#define DBG_ENABLE +#ifdef RT_WLAN_DEV_DEBUG +#define DBG_LEVEL DBG_LOG +#else +#define DBG_LEVEL DBG_INFO +#endif +#define DBG_SECTION_NAME "WLAN.dev" +#define DBG_COLOR +#include <rtdbg.h> + +#ifndef RT_DEVICE +#define RT_DEVICE(__device) ((rt_device_t)__device) +#endif + +#define WLAN_DEV_LOCK(_wlan) (rt_mutex_take(&(_wlan)->lock, RT_WAITING_FOREVER)) +#define WLAN_DEV_UNLOCK(_wlan) (rt_mutex_release(&(_wlan)->lock)) -#include "wlan_dev.h" -#include "wlan_cmd.h" +#if RT_WLAN_SSID_MAX_LENGTH < 1 +#error "SSID length is too short" +#endif -#define NIOCTL_SADDR 0x02 +#if RT_WLAN_BSSID_MAX_LENGTH < 1 +#error "BSSID length is too short" +#endif -void rt_wlan_info_init(struct rt_wlan_info *info, rt_wlan_mode_t mode, rt_wlan_security_t security, - char *ssid) +#if RT_WLAN_PASSWORD_MAX_LENGTH < 1 +#error "password length is too short" +#endif + +#if RT_WLAN_DEV_EVENT_NUM < 2 +#error "dev num Too little" +#endif + +rt_err_t rt_wlan_dev_init(struct rt_wlan_device *device, rt_wlan_mode_t mode) { - if (info == RT_NULL) return ; + rt_err_t result = RT_EOK; - memset(info, 0x0, sizeof(struct rt_wlan_info)); - info->mode = mode; - info->security = security; - if (ssid) + /* init wlan device */ + LOG_D("F:%s L:%d is run device:0x%08x mode:%d", __FUNCTION__, __LINE__, device, mode); + if ((device == RT_NULL) || (mode >= RT_WLAN_MODE_MAX)) { - info->ssid = rt_malloc(strlen((char *)ssid) + 1); - if (info->ssid) - { - strncpy((char *)info->ssid, (char *)ssid, strlen((char *)ssid) + 1); - } + LOG_E("F:%s L:%d Parameter Wrongful device:0x%08x mode:%d", __FUNCTION__, __LINE__, device, mode); + return -RT_ERROR; } + + result = rt_device_init(RT_DEVICE(device)); + if (result != RT_EOK) + { + LOG_E("L:%d wlan init failed", __LINE__); + return -RT_ERROR; + } + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_MODE, (void *)&mode); + if (result != RT_EOK) + { + LOG_E("L:%d wlan config mode failed", __LINE__); + return -RT_ERROR; + } + device->mode = mode; + return result; } -void rt_wlan_info_deinit(struct rt_wlan_info *info) +rt_err_t rt_wlan_dev_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len) { - if (info->ssid) + rt_err_t result = RT_EOK; + struct rt_sta_info sta_info; + + if (device == RT_NULL) + { + return -RT_EIO; + } + if (info == RT_NULL) { - rt_free(info->ssid); - info->ssid = RT_NULL; + return -RT_ERROR; } - memset(info, 0x0, sizeof(struct rt_wlan_info)); + if ((password_len > RT_WLAN_PASSWORD_MAX_LENGTH) || + (info->ssid.len > RT_WLAN_SSID_MAX_LENGTH)) + { + LOG_E("L:%d password or ssid is to long", __LINE__); + return -RT_ERROR; + } + rt_memset(&sta_info, 0, sizeof(struct rt_sta_info)); + rt_memcpy(&sta_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t)); + rt_memcpy(sta_info.bssid, info->bssid, RT_WLAN_BSSID_MAX_LENGTH); + if (password != RT_NULL) + { + rt_memcpy(sta_info.key.val, password, password_len); + sta_info.key.len = password_len; + } + sta_info.channel = info->channel; + sta_info.security = info->security; + + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_JOIN, &sta_info); + return result; } -int rt_wlan_init(struct rt_wlan_device *device, rt_wlan_mode_t mode) +rt_err_t rt_wlan_dev_disconnect(struct rt_wlan_device *device) { - int result; + rt_err_t result = RT_EOK; + + if (device == RT_NULL) + { + return -RT_EIO; + } + + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_DISCONNECT, RT_NULL); + return result; +} - if (device == RT_NULL) return 0; +rt_err_t rt_wlan_dev_ap_start(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len) +{ + rt_err_t result = RT_EOK; + struct rt_ap_info ap_info; - if (device->info == RT_NULL) + if (device == RT_NULL) + { + return -RT_EIO; + } + if (info == RT_NULL) { - struct rt_wlan_info *info; - char *ssid; + return -RT_ERROR; + } - info = rt_malloc(sizeof(struct rt_wlan_info)); - if (info) - { - ssid = rt_malloc(SSID_LENGTH_MAX_SIZE); - info->ssid = ssid; - } - device->info = info; + if ((password_len >= RT_WLAN_PASSWORD_MAX_LENGTH) || + (info->ssid.len >= RT_WLAN_SSID_MAX_LENGTH)) + { + LOG_E("L:%d password or ssid is to long", __LINE__); + return -RT_ERROR; } - result = rt_device_control(RT_DEVICE(device), WIFI_INIT, (void *)&mode); + rt_memset(&ap_info, 0, sizeof(struct rt_ap_info)); + rt_memcpy(&ap_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t)); + rt_memcpy(ap_info.key.val, password, password_len); + ap_info.key.len = password_len; + ap_info.hidden = info->hidden; + ap_info.channel = info->channel; + ap_info.security = info->security; + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SOFTAP, &ap_info); return result; } -int rt_wlan_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, char *password) +rt_err_t rt_wlan_dev_ap_stop(struct rt_wlan_device *device) { - int result = 0; + rt_err_t result = RT_EOK; - if (device == RT_NULL) return -RT_EIO; - - if (info != RT_NULL) + if (device == RT_NULL) { - rt_wlan_set_info(device, info); + return -RT_EIO; } - rt_strncpy((char *)device->key, (char *)password, sizeof(device->key) - 1); + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_AP_STOP, RT_NULL); + return result; +} - result = rt_device_control(RT_DEVICE(device), WIFI_EASYJOIN, (void *)password); +rt_err_t rt_wlan_dev_ap_deauth(struct rt_wlan_device *device, rt_uint8_t mac[6]) +{ + rt_err_t result = RT_EOK; + if (device == RT_NULL) + { + return -RT_EIO; + } + + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_AP_DEAUTH, mac); return result; } -int rt_wlan_softap(struct rt_wlan_device *device, struct rt_wlan_info *info, char *password) +int rt_wlan_dev_get_rssi(struct rt_wlan_device *device) { - int result = RT_EOK; - - if (device == RT_NULL) return -RT_EIO; + int rssi = 0; - if (info != RT_NULL) + if (device == RT_NULL) { - rt_wlan_set_info(device, info); + return -RT_EIO; } - if (password == NULL) + rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_RSSI, &rssi); + return rssi; +} + +rt_err_t rt_wlan_dev_get_mac(struct rt_wlan_device *device, rt_uint8_t mac[6]) +{ + rt_err_t result = RT_EOK; + + if (device == RT_NULL) { - memset(device->key, 0, sizeof(device->key)); + return -RT_EIO; } - else + + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_MAC, &mac[0]); + return result; +} + +rt_err_t rt_wlan_dev_set_mac(struct rt_wlan_device *device, rt_uint8_t mac[6]) +{ + rt_err_t result = RT_EOK; + + if (device == RT_NULL) { - if (rt_strlen(password) > sizeof(device->key) - 1) - { - rt_kprintf("WARN input password is longer than %d bytes.", sizeof(device->key) - 1); - } - rt_strncpy((char *)device->key, (char *)password, sizeof(device->key) - 1); + return -RT_EIO; } - result = rt_device_control(RT_DEVICE(device), WIFI_SOFTAP, (void *)password); - + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_MAC, &mac[0]); return result; } -int rt_wlan_disconnect(struct rt_wlan_device *device) +rt_err_t rt_wlan_dev_set_powersave(struct rt_wlan_device *device, int level) { - int result = 0; - - if (device == RT_NULL) return -RT_EIO; + rt_err_t result = RT_EOK; - /* save event handler */ - result = rt_device_control(RT_DEVICE(device), WIFI_DISCONNECT, RT_NULL); + if (device == RT_NULL) + { + return -RT_EIO; + } + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_POWERSAVE, &level); return result; } -int rt_wlan_set_info(struct rt_wlan_device *device, struct rt_wlan_info *info) +int rt_wlan_dev_get_powersave(struct rt_wlan_device *device) { - if (device == RT_NULL) return -RT_EIO; - if (device->info == RT_NULL) return -RT_EIO; + int level = 0; - device->info->mode = info->mode; - device->info->security = info->security; - memset(device->info->ssid, 0, SSID_LENGTH_MAX_SIZE); - memcpy(device->info->ssid, info->ssid, strlen(info->ssid)); - memcpy(device->info->bssid, info->bssid, 6); - device->info->datarate = info->datarate; - device->info->channel = info->channel; - device->info->rssi = info->rssi; + if (device == RT_NULL) + { + return -1; + } - return RT_EOK; + rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_POWERSAVE, &level); + + return level; } -struct rt_wlan_info *rt_wlan_get_info(struct rt_wlan_device *device) +rt_err_t rt_wlan_dev_register_event_handler(struct rt_wlan_device *device, rt_wlan_dev_event_t event, rt_wlan_dev_event_handler handler, void *parameter) { - struct rt_wlan_info *info = RT_NULL; + int i = 0; + rt_base_t level; - if (device != RT_NULL) + if (device == RT_NULL) { - info = device->info; + return -RT_EIO; + } + if (event >= RT_WLAN_DEV_EVT_MAX) + { + return -RT_EINVAL; } - return info; + level = rt_hw_interrupt_disable(); + for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++) + { + if (device->handler_table[event][i].handler == RT_NULL) + { + device->handler_table[event][i].handler = handler; + device->handler_table[event][i].parameter = parameter; + rt_hw_interrupt_enable(level); + return RT_EOK; + } + } + rt_hw_interrupt_enable(level); + + /* No space found */ + return -RT_ERROR; } -int rt_wlan_scan(struct rt_wlan_device *device, struct rt_wlan_scan_result **scan_result) +rt_err_t rt_wlan_dev_unregister_event_handler(struct rt_wlan_device *device, rt_wlan_dev_event_t event, rt_wlan_dev_event_handler handler) { - int result; + int i = 0; + rt_base_t level; - result = rt_device_control(RT_DEVICE(device), WIFI_SCAN, scan_result); + if (device == RT_NULL) + { + return -RT_EIO; + } + if (event >= RT_WLAN_DEV_EVT_MAX) + { + return -RT_EINVAL; + } - return result; + level = rt_hw_interrupt_disable(); + for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++) + { + if (device->handler_table[event][i].handler == handler) + { + rt_memset(&device->handler_table[event][i], 0, sizeof(struct rt_wlan_dev_event_desc)); + rt_hw_interrupt_enable(level); + return RT_EOK; + } + } + rt_hw_interrupt_enable(level); + /* not find iteam */ + return -RT_ERROR; } -int rt_wlan_get_rssi(struct rt_wlan_device *device) +void rt_wlan_dev_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff) { - int rssi; - int result; + void *parameter[RT_WLAN_DEV_EVENT_NUM]; + rt_wlan_dev_event_handler handler[RT_WLAN_DEV_EVENT_NUM]; + int i; + rt_base_t level; - if (device == RT_NULL) return 0; - result = rt_device_control(RT_DEVICE(device), WIFI_GET_RSSI, (void *)&rssi); + if (device == RT_NULL) + { + return; + } + if (event >= RT_WLAN_DEV_EVT_MAX) + { + return; + } - if (result == RT_EOK) return rssi; + /* get callback handle */ + level = rt_hw_interrupt_disable(); + for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++) + { + handler[i] = device->handler_table[event][i].handler; + parameter[i] = device->handler_table[event][i].parameter; + } + rt_hw_interrupt_enable(level); - return result; + /* run callback */ + for (i = 0; i < RT_WLAN_DEV_EVENT_NUM; i++) + { + if (handler[i] != RT_NULL) + { + handler[i](device, event, buff, parameter[i]); + } + } } -int rt_wlan_get_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6]) +rt_err_t rt_wlan_dev_enter_promisc(struct rt_wlan_device *device) { - int result; - if (device == RT_NULL) return 0; - result = rt_device_control(RT_DEVICE(device), NIOCTL_GADDR, (void *)hwaddr); + rt_err_t result = RT_EOK; + int enable = 1; + + if (device == RT_NULL) + { + return -RT_EIO; + } + + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_PROMISC, &enable); return result; } -int rt_wlan_set_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6]) +rt_err_t rt_wlan_dev_exit_promisc(struct rt_wlan_device *device) { - int result; - if (device == RT_NULL) return 0; - result = rt_device_control(RT_DEVICE(device), NIOCTL_SADDR, (void *)hwaddr); + rt_err_t result = RT_EOK; + int enable = 0; + + if (device == RT_NULL) + { + return -RT_EIO; + } + + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_PROMISC, &enable); return result; } -int rt_wlan_enter_powersave(struct rt_wlan_device *device, int level) +rt_err_t rt_wlan_dev_set_promisc_callback(struct rt_wlan_device *device, rt_wlan_pormisc_callback_t callback) { - int result = 0; + if (device == RT_NULL) + { + return -RT_EIO; + } + device->pormisc_callback = callback; - if (device == RT_NULL) return -RT_EIO; + return RT_EOK; +} + +void rt_wlan_dev_promisc_handler(struct rt_wlan_device *device, void *data, int len) +{ + rt_wlan_pormisc_callback_t callback; - result = rt_device_control(RT_DEVICE(device), WIFI_ENTER_POWERSAVE, (void *)&level); + if (device == RT_NULL) + { + return; + } + + callback = device->pormisc_callback; + + if (callback != RT_NULL) + { + callback(device, data, len); + } +} +rt_err_t rt_wlan_dev_cfg_filter(struct rt_wlan_device *device, struct rt_wlan_filter *filter) +{ + rt_err_t result = RT_EOK; + + if (device == RT_NULL) + { + return -RT_EIO; + } + if (filter == RT_NULL) + { + return -RT_ERROR; + } + + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_CFG_FILTER, filter); return result; } -int rt_wlan_register_event_handler(struct rt_wlan_device *device, rt_wlan_event_t event, - rt_wlan_event_handler handler) +rt_err_t rt_wlan_dev_set_channel(struct rt_wlan_device *device, int channel) { - if (device == RT_NULL) return -RT_EIO; - if (event >= WIFI_EVT_MAX) return -RT_EINVAL; + rt_err_t result = RT_EOK; - device->handler[event] = handler; + if (device == RT_NULL) + { + return -RT_EIO; + } + if (channel < 0) + { + return -RT_ERROR; + } - return RT_EOK; + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_CHANNEL, &channel); + return result; } -int rt_wlan_unregister_event_handler(struct rt_wlan_device *device, rt_wlan_event_t event) +rt_err_t rt_wlan_dev_get_channel(struct rt_wlan_device *device) { - if (device == RT_NULL) return -RT_EIO; - if (event >= WIFI_EVT_MAX) return -RT_EINVAL; + rt_err_t result = RT_EOK; + int channel; - device->handler[event] = RT_NULL; + if (device == RT_NULL) + { + return -RT_EIO; + } - return RT_EOK; + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_CHANNEL, &channel); + if (result != RT_EOK) + { + return -1; + } + + return channel; } -int rt_wlan_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data) +rt_err_t rt_wlan_dev_set_country(struct rt_wlan_device *device, rt_country_code_t country_code) { - if (device == RT_NULL) return -RT_EIO; - if (event >= WIFI_EVT_MAX) return -RT_EINVAL; + int result = RT_EOK; - if (device->handler[event] != RT_NULL) - device->handler[event](device, event, user_data); + if (device == RT_NULL) + { + return -RT_EIO; + } - return RT_EOK; + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SET_COUNTRY, &country_code); + return result; } -int rt_wlan_cfg_monitor(struct rt_wlan_device *device, rt_wlan_monitor_opition_t opition) +rt_country_code_t rt_wlan_dev_get_country(struct rt_wlan_device *device) { int result = 0; + rt_country_code_t country_code = RT_COUNTRY_UNKNOWN; - if (device == RT_NULL) return -RT_EIO; + if (device == RT_NULL) + { + return country_code; + } - result = rt_device_control(RT_DEVICE(device), WIFI_CFG_MONITOR, (void *)&opition); + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_GET_COUNTRY, &country_code); + if (result != RT_EOK) + { + return RT_COUNTRY_UNKNOWN; + } - return result; + return country_code; } -int rt_wlan_set_monitor_callback(struct rt_wlan_device *device, rt_wlan_monitor_callback_t callback) +rt_err_t rt_wlan_dev_scan(struct rt_wlan_device *device, struct rt_wlan_info *info) { - int result = 0; + struct rt_scan_info scan_info = { 0 }; + struct rt_scan_info *p_scan_info = RT_NULL; + rt_err_t result = 0; - if (device == RT_NULL) return -RT_EIO; + if (device == RT_NULL) + { + return -RT_EIO; + } - result = rt_device_control(RT_DEVICE(device), WIFI_SET_MONITOR_CALLBACK, (void *)callback); + if (info != RT_NULL) + { + if (info->ssid.len >= RT_WLAN_SSID_MAX_LENGTH) + { + LOG_E("L:%d ssid is to long", __LINE__); + return -RT_EINVAL; + } + rt_memcpy(&scan_info.ssid, &info->ssid, sizeof(rt_wlan_ssid_t)); + rt_memcpy(scan_info.bssid, info->bssid, RT_WLAN_BSSID_MAX_LENGTH); + scan_info.channel_min = -1; + scan_info.channel_max = -1; + p_scan_info = &scan_info; + } + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN, p_scan_info); + return result; +} + +rt_err_t rt_wlan_dev_scan_stop(struct rt_wlan_device *device) +{ + rt_err_t result = 0; + if (device == RT_NULL) + { + return -RT_EIO; + } + + result = rt_device_control(RT_DEVICE(device), RT_WLAN_CMD_SCAN_STOP, RT_NULL); return result; } -int rt_wlan_set_channel(struct rt_wlan_device *device, int channel) +rt_err_t rt_wlan_dev_report_data(struct rt_wlan_device *device, void *buff, int len) { - int result = 0; + return rt_wlan_dev_transfer_prot(device, buff, len); +} + +static rt_err_t _rt_wlan_dev_init(rt_device_t dev) +{ + struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev; + rt_err_t result = RT_EOK; - if (device == RT_NULL) return -RT_EIO; + rt_mutex_init(&wlan->lock, "wlan_dev", RT_IPC_FLAG_FIFO); - result = rt_device_control(RT_DEVICE(device), WIFI_SET_CHANNEL, (void *)&channel); + if (wlan->ops->wlan_init) + result = wlan->ops->wlan_init(wlan); + + if (result == RT_EOK) + { + LOG_I("wlan init success"); + } + else + { + LOG_I("wlan init failed"); + } return result; } -int rt_wlan_get_channel(struct rt_wlan_device *device) +static rt_err_t _rt_wlan_dev_control(rt_device_t dev, int cmd, void *args) { - int channel = 0; + struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev; + rt_err_t err = RT_EOK; - if (device == RT_NULL) return -RT_EIO; + RT_ASSERT(dev != RT_NULL); - rt_device_control(RT_DEVICE(device), WIFI_GET_CHANNEL, &channel); + WLAN_DEV_LOCK(wlan); - return channel; + switch (cmd) + { + case RT_WLAN_CMD_MODE: + { + rt_wlan_mode_t mode = *((rt_wlan_mode_t *)args); + + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_MODE, "RT_WLAN_CMD_MODE"); + if (wlan->ops->wlan_mode) + err = wlan->ops->wlan_mode(wlan, mode); + break; + } + case RT_WLAN_CMD_SCAN: + { + struct rt_scan_info *scan_info = args; + + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN, "RT_WLAN_CMD_SCAN"); + if (wlan->ops->wlan_scan) + err = wlan->ops->wlan_scan(wlan, scan_info); + break; + } + case RT_WLAN_CMD_JOIN: + { + struct rt_sta_info *sta_info = args; + + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_JOIN, "RT_WLAN_CMD_JOIN"); + if (wlan->ops->wlan_join) + err = wlan->ops->wlan_join(wlan, sta_info); + break; + } + case RT_WLAN_CMD_SOFTAP: + { + struct rt_ap_info *ap_info = args; + + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SOFTAP, "RT_WLAN_CMD_SOFTAP"); + if (wlan->ops->wlan_softap) + err = wlan->ops->wlan_softap(wlan, ap_info); + break; + } + case RT_WLAN_CMD_DISCONNECT: + { + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_DISCONNECT, "RT_WLAN_CMD_DISCONNECT"); + if (wlan->ops->wlan_disconnect) + err = wlan->ops->wlan_disconnect(wlan); + break; + } + case RT_WLAN_CMD_AP_STOP: + { + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_STOP, "RT_WLAN_CMD_AP_STOP"); + if (wlan->ops->wlan_ap_stop) + err = wlan->ops->wlan_ap_stop(wlan); + break; + } + case RT_WLAN_CMD_AP_DEAUTH: + { + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_AP_DEAUTH, "RT_WLAN_CMD_AP_DEAUTH"); + if (wlan->ops->wlan_ap_deauth) + err = wlan->ops->wlan_ap_deauth(wlan, args); + break; + } + case RT_WLAN_CMD_SCAN_STOP: + { + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SCAN_STOP, "RT_WLAN_CMD_SCAN_STOP"); + if (wlan->ops->wlan_scan_stop) + err = wlan->ops->wlan_scan_stop(wlan); + break; + } + case RT_WLAN_CMD_GET_RSSI: + { + int *rssi = args; + + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_RSSI, "RT_WLAN_CMD_GET_RSSI"); + if (wlan->ops->wlan_get_rssi) + *rssi = wlan->ops->wlan_get_rssi(wlan); + break; + } + case RT_WLAN_CMD_SET_POWERSAVE: + { + int level = *((int *)args); + + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_POWERSAVE, "RT_WLAN_CMD_SET_POWERSAVE"); + if (wlan->ops->wlan_set_powersave) + wlan->ops->wlan_set_powersave(wlan, level); + break; + } + case RT_WLAN_CMD_GET_POWERSAVE: + { + int *level = args; + + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_POWERSAVE, "RT_WLAN_CMD_GET_POWERSAVE"); + if (wlan->ops->wlan_get_powersave) + *level = wlan->ops->wlan_get_powersave(wlan); + break; + } + case RT_WLAN_CMD_CFG_PROMISC: + { + rt_bool_t start = *((rt_bool_t *)args); + + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_PROMISC, "RT_WLAN_CMD_CFG_PROMISC"); + if (wlan->ops->wlan_cfg_promisc) + wlan->ops->wlan_cfg_promisc(wlan, start); + break; + } + case RT_WLAN_CMD_CFG_FILTER: + { + struct rt_wlan_filter *filter = args; + + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_CFG_FILTER, "RT_WLAN_CMD_CFG_FILTER"); + if (wlan->ops->wlan_cfg_filter) + wlan->ops->wlan_cfg_filter(wlan, filter); + break; + } + case RT_WLAN_CMD_SET_CHANNEL: + { + int channel = *(int *)args; + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_CHANNEL, "RT_WLAN_CMD_SET_CHANNEL"); + if (wlan->ops->wlan_set_channel) + wlan->ops->wlan_set_channel(wlan, channel); + break; + } + case RT_WLAN_CMD_GET_CHANNEL: + { + int *channel = args; + + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_CHANNEL, "RT_WLAN_CMD_GET_CHANNEL"); + if (wlan->ops->wlan_get_channel) + *channel = wlan->ops->wlan_get_channel(wlan); + break; + } + case RT_WLAN_CMD_SET_COUNTRY: + { + rt_country_code_t country = *(rt_country_code_t *)args; + + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_COUNTRY, "RT_WLAN_CMD_SET_COUNTRY"); + if (wlan->ops->wlan_set_country) + wlan->ops->wlan_set_country(wlan, country); + break; + } + case RT_WLAN_CMD_GET_COUNTRY: + { + rt_country_code_t *country = args; + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_COUNTRY, "RT_WLAN_CMD_GET_COUNTRY"); + if (wlan->ops->wlan_get_country) + *country = wlan->ops->wlan_get_country(wlan); + break; + } + case RT_WLAN_CMD_SET_MAC: + { + rt_uint8_t *mac = args; + + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_SET_MAC, "RT_WLAN_CMD_SET_MAC"); + if (wlan->ops->wlan_set_mac) + wlan->ops->wlan_set_mac(wlan, mac); + break; + } + case RT_WLAN_CMD_GET_MAC: + { + rt_uint8_t *mac = args; + + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, RT_WLAN_CMD_GET_MAC, "RT_WLAN_CMD_GET_MAC"); + if (wlan->ops->wlan_get_mac) + wlan->ops->wlan_get_mac(wlan, mac); + break; + } + default: + LOG_D("%s %d cmd[%d]:%s run......", __FUNCTION__, __LINE__, -1, "UNKUOWN"); + break; + } + + WLAN_DEV_UNLOCK(wlan); + + return err; } -void rt_wlan_release_scan_result(struct rt_wlan_scan_result **scan_result) +struct rt_wlan_device *rt_wlan_dev_register(const char *name, const struct rt_wlan_dev_ops *ops, rt_uint32_t flag, void *user_data) { - int i, ap_num; - struct rt_wlan_scan_result *_scan_result; + struct rt_wlan_device *wlan; - if (*scan_result != RT_NULL) + if (name == RT_NULL || ops == RT_NULL) { - _scan_result = *scan_result; - ap_num = _scan_result->ap_num; - for (i = 0; i < ap_num; i++) - { - if (_scan_result->ap_table[i].ssid != RT_NULL) - { - rt_free(_scan_result->ap_table[i].ssid); - _scan_result->ap_table[i].ssid = RT_NULL; - } - } - _scan_result->ap_num = 0; - rt_free(_scan_result->ap_table); - _scan_result->ap_table = RT_NULL; + LOG_E("F:%s L:%d parameter Wrongful", __FUNCTION__, __LINE__); + return RT_NULL; } - rt_free(*scan_result); - *scan_result = RT_NULL; - scan_result = RT_NULL; + + wlan = rt_malloc(sizeof(struct rt_wlan_device)); + if (wlan == RT_NULL) + { + LOG_E("F:%s L:%d", __FUNCTION__, __LINE__); + return RT_NULL; + } + rt_memset(wlan, 0, sizeof(struct rt_wlan_device)); + + wlan->device.init = _rt_wlan_dev_init; + wlan->device.open = RT_NULL; + wlan->device.close = RT_NULL; + wlan->device.read = RT_NULL; + wlan->device.write = RT_NULL; + wlan->device.control = _rt_wlan_dev_control; + wlan->device.user_data = RT_NULL; + + wlan->device.type = RT_Device_Class_NetIf; + + wlan->ops = ops; + wlan->user_data = user_data; + + wlan->flags = flag; + rt_device_register(&wlan->device, name, RT_DEVICE_FLAG_RDWR); + + LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__); + + return wlan; } diff --git a/components/drivers/wlan/wlan_dev.h b/components/drivers/wlan/wlan_dev.h index df7244fd6f..f46f3c001f 100644 --- a/components/drivers/wlan/wlan_dev.h +++ b/components/drivers/wlan/wlan_dev.h @@ -1,72 +1,66 @@ /* - * RT-Thread Wi-Fi Device + * Copyright (c) 2006-2018, RT-Thread Development Team * - * COPYRIGHT (C) 2014 - 2018, Shanghai Real-Thread Technology Co., Ltd - * - * This file is part of RT-Thread (http://www.rt-thread.org) - * - * All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes - * 2014-09-11 Bernard the first verion + * 2018-08-03 tyx the first version */ -#ifndef WIFI_DEVICE_H__ -#define WIFI_DEVICE_H__ +#ifndef __WLAN_DEVICE_H__ +#define __WLAN_DEVICE_H__ -#include <stdint.h> -#include <netif/ethernetif.h> +#ifdef __cplusplus +extern "C" { +#endif typedef enum { - WIFI_NONE, - WIFI_STATION, - WIFI_AP, + RT_WLAN_NONE, + RT_WLAN_STATION, + RT_WLAN_AP, + RT_WLAN_MODE_MAX } rt_wlan_mode_t; typedef enum { - WIFI_INIT = 0x10, - WIFI_SCAN, /* trigger scanning (list cells) */ - WIFI_JOIN, - WIFI_EASYJOIN, /* join network with less information */ - WIFI_SOFTAP, /* start soft-AP */ - WIFI_DISCONNECT, - WIFI_GET_RSSI, /* get sensitivity (dBm) */ - WIFI_ENTER_POWERSAVE, - WIFI_CFG_MONITOR, /* start/stop minitor */ - WIFI_SET_CHANNEL, - WIFI_GET_CHANNEL, - WIFI_SET_MONITOR_CALLBACK, + RT_WLAN_CMD_MODE = 0x10, + RT_WLAN_CMD_SCAN, /* trigger scanning (list cells) */ + RT_WLAN_CMD_JOIN, + RT_WLAN_CMD_SOFTAP, /* start soft-AP */ + RT_WLAN_CMD_DISCONNECT, + RT_WLAN_CMD_AP_STOP, /* stop soft-AP */ + RT_WLAN_CMD_AP_DEAUTH, + RT_WLAN_CMD_SCAN_STOP, + RT_WLAN_CMD_GET_RSSI, /* get sensitivity (dBm) */ + RT_WLAN_CMD_SET_POWERSAVE, + RT_WLAN_CMD_GET_POWERSAVE, + RT_WLAN_CMD_CFG_PROMISC, /* start/stop minitor */ + RT_WLAN_CMD_CFG_FILTER, + RT_WLAN_CMD_SET_CHANNEL, + RT_WLAN_CMD_GET_CHANNEL, + RT_WLAN_CMD_SET_COUNTRY, + RT_WLAN_CMD_GET_COUNTRY, + RT_WLAN_CMD_SET_MAC, + RT_WLAN_CMD_GET_MAC } rt_wlan_cmd_t; typedef enum { - WIFI_PWR_OFF, - WIFI_PWR_SLEEP, - WIFI_PWR_NORMAL -} rt_wlan_powersave_t; - -typedef enum -{ - WIFI_MONITOR_START, - WIFI_MONITOR_STOP -} rt_wlan_monitor_opition_t; + RT_WLAN_DEV_EVT_INIT_DONE = 0, + RT_WLAN_DEV_EVT_CONNECT, + RT_WLAN_DEV_EVT_CONNECT_FAIL, + RT_WLAN_DEV_EVT_DISCONNECT, + RT_WLAN_DEV_EVT_AP_START, + RT_WLAN_DEV_EVT_AP_STOP, + RT_WLAN_DEV_EVT_AP_ASSOCIATED, + RT_WLAN_DEV_EVT_AP_DISASSOCIATED, + RT_WLAN_DEV_EVT_AP_ASSOCIATE_FAILED, + RT_WLAN_DEV_EVT_SCAN_REPORT, + RT_WLAN_DEV_EVT_SCAN_DONE, + RT_WLAN_DEV_EVT_MAX, +} rt_wlan_dev_event_t; #define SHARED_ENABLED 0x00008000 #define WPA_SECURITY 0x00200000 @@ -77,144 +71,515 @@ typedef enum #define AES_ENABLED 0x0004 #define WSEC_SWFLAG 0x0008 -#define KEY_ARRAY_SIZE 32 -#define SSID_LENGTH_MAX_SIZE 32 + 1 +#define RT_WLAN_FLAG_STA_ONLY (0x1 << 0) +#define RT_WLAN_FLAG_AP_ONLY (0x1 << 1) + +#ifndef RT_WLAN_SSID_MAX_LENGTH +#define RT_WLAN_SSID_MAX_LENGTH (32) /* SSID MAX LEN */ +#endif + +#ifndef RT_WLAN_BSSID_MAX_LENGTH +#define RT_WLAN_BSSID_MAX_LENGTH (6) /* BSSID MAX LEN (default is 6) */ +#endif + +#ifndef RT_WLAN_PASSWORD_MAX_LENGTH +#define RT_WLAN_PASSWORD_MAX_LENGTH (32) /* PASSWORD MAX LEN*/ +#endif + +#ifndef RT_WLAN_DEV_EVENT_NUM +#define RT_WLAN_DEV_EVENT_NUM (2) /* EVENT GROUP MAX NUM */ +#endif + /** * Enumeration of Wi-Fi security modes */ typedef enum { - SECURITY_OPEN = 0, /**< Open security */ - SECURITY_WEP_PSK = WEP_ENABLED, /**< WEP Security with open authentication */ - SECURITY_WEP_SHARED = (WEP_ENABLED | SHARED_ENABLED), /**< WEP Security with shared authentication */ - SECURITY_WPA_TKIP_PSK = (WPA_SECURITY | TKIP_ENABLED), /**< WPA Security with TKIP */ - SECURITY_WPA_AES_PSK = (WPA_SECURITY | AES_ENABLED), /**< WPA Security with AES */ - SECURITY_WPA2_AES_PSK = (WPA2_SECURITY | AES_ENABLED), /**< WPA2 Security with AES */ - SECURITY_WPA2_TKIP_PSK = (WPA2_SECURITY | TKIP_ENABLED), /**< WPA2 Security with TKIP */ - SECURITY_WPA2_MIXED_PSK = (WPA2_SECURITY | AES_ENABLED | TKIP_ENABLED), /**< WPA2 Security with AES & TKIP */ - SECURITY_WPS_OPEN = WPS_ENABLED, /**< WPS with open security */ - SECURITY_WPS_SECURE = (WPS_ENABLED | AES_ENABLED), /**< WPS with AES security */ - SECURITY_UNKNOWN = -1, /**< May be returned by scan function if security is unknown. - Do not pass this to the join function! */ + SECURITY_OPEN = 0, /* Open security */ + SECURITY_WEP_PSK = WEP_ENABLED, /* WEP Security with open authentication */ + SECURITY_WEP_SHARED = (WEP_ENABLED | SHARED_ENABLED), /* WEP Security with shared authentication */ + SECURITY_WPA_TKIP_PSK = (WPA_SECURITY | TKIP_ENABLED), /* WPA Security with TKIP */ + SECURITY_WPA_AES_PSK = (WPA_SECURITY | AES_ENABLED), /* WPA Security with AES */ + SECURITY_WPA2_AES_PSK = (WPA2_SECURITY | AES_ENABLED), /* WPA2 Security with AES */ + SECURITY_WPA2_TKIP_PSK = (WPA2_SECURITY | TKIP_ENABLED), /* WPA2 Security with TKIP */ + SECURITY_WPA2_MIXED_PSK = (WPA2_SECURITY | AES_ENABLED | TKIP_ENABLED), /* WPA2 Security with AES & TKIP */ + SECURITY_WPS_OPEN = WPS_ENABLED, /* WPS with open security */ + SECURITY_WPS_SECURE = (WPS_ENABLED | AES_ENABLED), /* WPS with AES security */ + SECURITY_UNKNOWN = -1, /* May be returned by scan function if security is unknown. + Do not pass this to the join function! */ } rt_wlan_security_t; typedef enum { - WIFI_EVT_INIT_DONE = 0, - WIFI_EVT_LINK_DOWN, - WIFI_EVT_LINK_UP, - WIFI_EVT_CONNECT, - WIFI_EVT_DISCONNECT, - WIFI_EVT_AP_START, - WIFI_EVT_AP_STOP, - WIFI_EVENT_STA_ASSOC, - WIFI_EVENT_STA_DISASSOC, - WIFI_EVT_SCAN_DONE, - WIFI_EVT_MAX, -} rt_wlan_event_t; + RT_802_11_BAND_5GHZ = 0, /* Denotes 5GHz radio band */ + RT_802_11_BAND_2_4GHZ = 1, /* Denotes 2.4GHz radio band */ + RT_802_11_BAND_UNKNOWN = 0x7fffffff, /* unknown */ +} rt_802_11_band_t; -/* wifi network information */ -struct rt_wlan_info +typedef enum { - rt_wlan_mode_t mode; /* wifi mode */ - rt_wlan_security_t security; + RT_COUNTRY_AFGHANISTAN, + RT_COUNTRY_ALBANIA, + RT_COUNTRY_ALGERIA, + RT_COUNTRY_AMERICAN_SAMOA, + RT_COUNTRY_ANGOLA, + RT_COUNTRY_ANGUILLA, + RT_COUNTRY_ANTIGUA_AND_BARBUDA, + RT_COUNTRY_ARGENTINA, + RT_COUNTRY_ARMENIA, + RT_COUNTRY_ARUBA, + RT_COUNTRY_AUSTRALIA, + RT_COUNTRY_AUSTRIA, + RT_COUNTRY_AZERBAIJAN, + RT_COUNTRY_BAHAMAS, + RT_COUNTRY_BAHRAIN, + RT_COUNTRY_BAKER_ISLAND, + RT_COUNTRY_BANGLADESH, + RT_COUNTRY_BARBADOS, + RT_COUNTRY_BELARUS, + RT_COUNTRY_BELGIUM, + RT_COUNTRY_BELIZE, + RT_COUNTRY_BENIN, + RT_COUNTRY_BERMUDA, + RT_COUNTRY_BHUTAN, + RT_COUNTRY_BOLIVIA, + RT_COUNTRY_BOSNIA_AND_HERZEGOVINA, + RT_COUNTRY_BOTSWANA, + RT_COUNTRY_BRAZIL, + RT_COUNTRY_BRITISH_INDIAN_OCEAN_TERRITORY, + RT_COUNTRY_BRUNEI_DARUSSALAM, + RT_COUNTRY_BULGARIA, + RT_COUNTRY_BURKINA_FASO, + RT_COUNTRY_BURUNDI, + RT_COUNTRY_CAMBODIA, + RT_COUNTRY_CAMEROON, + RT_COUNTRY_CANADA, + RT_COUNTRY_CAPE_VERDE, + RT_COUNTRY_CAYMAN_ISLANDS, + RT_COUNTRY_CENTRAL_AFRICAN_REPUBLIC, + RT_COUNTRY_CHAD, + RT_COUNTRY_CHILE, + RT_COUNTRY_CHINA, + RT_COUNTRY_CHRISTMAS_ISLAND, + RT_COUNTRY_COLOMBIA, + RT_COUNTRY_COMOROS, + RT_COUNTRY_CONGO, + RT_COUNTRY_CONGO_THE_DEMOCRATIC_REPUBLIC_OF_THE, + RT_COUNTRY_COSTA_RICA, + RT_COUNTRY_COTE_DIVOIRE, + RT_COUNTRY_CROATIA, + RT_COUNTRY_CUBA, + RT_COUNTRY_CYPRUS, + RT_COUNTRY_CZECH_REPUBLIC, + RT_COUNTRY_DENMARK, + RT_COUNTRY_DJIBOUTI, + RT_COUNTRY_DOMINICA, + RT_COUNTRY_DOMINICAN_REPUBLIC, + RT_COUNTRY_DOWN_UNDER, + RT_COUNTRY_ECUADOR, + RT_COUNTRY_EGYPT, + RT_COUNTRY_EL_SALVADOR, + RT_COUNTRY_EQUATORIAL_GUINEA, + RT_COUNTRY_ERITREA, + RT_COUNTRY_ESTONIA, + RT_COUNTRY_ETHIOPIA, + RT_COUNTRY_FALKLAND_ISLANDS_MALVINAS, + RT_COUNTRY_FAROE_ISLANDS, + RT_COUNTRY_FIJI, + RT_COUNTRY_FINLAND, + RT_COUNTRY_FRANCE, + RT_COUNTRY_FRENCH_GUINA, + RT_COUNTRY_FRENCH_POLYNESIA, + RT_COUNTRY_FRENCH_SOUTHERN_TERRITORIES, + RT_COUNTRY_GABON, + RT_COUNTRY_GAMBIA, + RT_COUNTRY_GEORGIA, + RT_COUNTRY_GERMANY, + RT_COUNTRY_GHANA, + RT_COUNTRY_GIBRALTAR, + RT_COUNTRY_GREECE, + RT_COUNTRY_GRENADA, + RT_COUNTRY_GUADELOUPE, + RT_COUNTRY_GUAM, + RT_COUNTRY_GUATEMALA, + RT_COUNTRY_GUERNSEY, + RT_COUNTRY_GUINEA, + RT_COUNTRY_GUINEA_BISSAU, + RT_COUNTRY_GUYANA, + RT_COUNTRY_HAITI, + RT_COUNTRY_HOLY_SEE_VATICAN_CITY_STATE, + RT_COUNTRY_HONDURAS, + RT_COUNTRY_HONG_KONG, + RT_COUNTRY_HUNGARY, + RT_COUNTRY_ICELAND, + RT_COUNTRY_INDIA, + RT_COUNTRY_INDONESIA, + RT_COUNTRY_IRAN_ISLAMIC_REPUBLIC_OF, + RT_COUNTRY_IRAQ, + RT_COUNTRY_IRELAND, + RT_COUNTRY_ISRAEL, + RT_COUNTRY_ITALY, + RT_COUNTRY_JAMAICA, + RT_COUNTRY_JAPAN, + RT_COUNTRY_JERSEY, + RT_COUNTRY_JORDAN, + RT_COUNTRY_KAZAKHSTAN, + RT_COUNTRY_KENYA, + RT_COUNTRY_KIRIBATI, + RT_COUNTRY_KOREA_REPUBLIC_OF, + RT_COUNTRY_KOSOVO, + RT_COUNTRY_KUWAIT, + RT_COUNTRY_KYRGYZSTAN, + RT_COUNTRY_LAO_PEOPLES_DEMOCRATIC_REPUBIC, + RT_COUNTRY_LATVIA, + RT_COUNTRY_LEBANON, + RT_COUNTRY_LESOTHO, + RT_COUNTRY_LIBERIA, + RT_COUNTRY_LIBYAN_ARAB_JAMAHIRIYA, + RT_COUNTRY_LIECHTENSTEIN, + RT_COUNTRY_LITHUANIA, + RT_COUNTRY_LUXEMBOURG, + RT_COUNTRY_MACAO, + RT_COUNTRY_MACEDONIA_FORMER_YUGOSLAV_REPUBLIC_OF, + RT_COUNTRY_MADAGASCAR, + RT_COUNTRY_MALAWI, + RT_COUNTRY_MALAYSIA, + RT_COUNTRY_MALDIVES, + RT_COUNTRY_MALI, + RT_COUNTRY_MALTA, + RT_COUNTRY_MAN_ISLE_OF, + RT_COUNTRY_MARTINIQUE, + RT_COUNTRY_MAURITANIA, + RT_COUNTRY_MAURITIUS, + RT_COUNTRY_MAYOTTE, + RT_COUNTRY_MEXICO, + RT_COUNTRY_MICRONESIA_FEDERATED_STATES_OF, + RT_COUNTRY_MOLDOVA_REPUBLIC_OF, + RT_COUNTRY_MONACO, + RT_COUNTRY_MONGOLIA, + RT_COUNTRY_MONTENEGRO, + RT_COUNTRY_MONTSERRAT, + RT_COUNTRY_MOROCCO, + RT_COUNTRY_MOZAMBIQUE, + RT_COUNTRY_MYANMAR, + RT_COUNTRY_NAMIBIA, + RT_COUNTRY_NAURU, + RT_COUNTRY_NEPAL, + RT_COUNTRY_NETHERLANDS, + RT_COUNTRY_NETHERLANDS_ANTILLES, + RT_COUNTRY_NEW_CALEDONIA, + RT_COUNTRY_NEW_ZEALAND, + RT_COUNTRY_NICARAGUA, + RT_COUNTRY_NIGER, + RT_COUNTRY_NIGERIA, + RT_COUNTRY_NORFOLK_ISLAND, + RT_COUNTRY_NORTHERN_MARIANA_ISLANDS, + RT_COUNTRY_NORWAY, + RT_COUNTRY_OMAN, + RT_COUNTRY_PAKISTAN, + RT_COUNTRY_PALAU, + RT_COUNTRY_PANAMA, + RT_COUNTRY_PAPUA_NEW_GUINEA, + RT_COUNTRY_PARAGUAY, + RT_COUNTRY_PERU, + RT_COUNTRY_PHILIPPINES, + RT_COUNTRY_POLAND, + RT_COUNTRY_PORTUGAL, + RT_COUNTRY_PUETO_RICO, + RT_COUNTRY_QATAR, + RT_COUNTRY_REUNION, + RT_COUNTRY_ROMANIA, + RT_COUNTRY_RUSSIAN_FEDERATION, + RT_COUNTRY_RWANDA, + RT_COUNTRY_SAINT_KITTS_AND_NEVIS, + RT_COUNTRY_SAINT_LUCIA, + RT_COUNTRY_SAINT_PIERRE_AND_MIQUELON, + RT_COUNTRY_SAINT_VINCENT_AND_THE_GRENADINES, + RT_COUNTRY_SAMOA, + RT_COUNTRY_SANIT_MARTIN_SINT_MARTEEN, + RT_COUNTRY_SAO_TOME_AND_PRINCIPE, + RT_COUNTRY_SAUDI_ARABIA, + RT_COUNTRY_SENEGAL, + RT_COUNTRY_SERBIA, + RT_COUNTRY_SEYCHELLES, + RT_COUNTRY_SIERRA_LEONE, + RT_COUNTRY_SINGAPORE, + RT_COUNTRY_SLOVAKIA, + RT_COUNTRY_SLOVENIA, + RT_COUNTRY_SOLOMON_ISLANDS, + RT_COUNTRY_SOMALIA, + RT_COUNTRY_SOUTH_AFRICA, + RT_COUNTRY_SPAIN, + RT_COUNTRY_SRI_LANKA, + RT_COUNTRY_SURINAME, + RT_COUNTRY_SWAZILAND, + RT_COUNTRY_SWEDEN, + RT_COUNTRY_SWITZERLAND, + RT_COUNTRY_SYRIAN_ARAB_REPUBLIC, + RT_COUNTRY_TAIWAN_PROVINCE_OF_CHINA, + RT_COUNTRY_TAJIKISTAN, + RT_COUNTRY_TANZANIA_UNITED_REPUBLIC_OF, + RT_COUNTRY_THAILAND, + RT_COUNTRY_TOGO, + RT_COUNTRY_TONGA, + RT_COUNTRY_TRINIDAD_AND_TOBAGO, + RT_COUNTRY_TUNISIA, + RT_COUNTRY_TURKEY, + RT_COUNTRY_TURKMENISTAN, + RT_COUNTRY_TURKS_AND_CAICOS_ISLANDS, + RT_COUNTRY_TUVALU, + RT_COUNTRY_UGANDA, + RT_COUNTRY_UKRAINE, + RT_COUNTRY_UNITED_ARAB_EMIRATES, + RT_COUNTRY_UNITED_KINGDOM, + RT_COUNTRY_UNITED_STATES, + RT_COUNTRY_UNITED_STATES_REV4, + RT_COUNTRY_UNITED_STATES_NO_DFS, + RT_COUNTRY_UNITED_STATES_MINOR_OUTLYING_ISLANDS, + RT_COUNTRY_URUGUAY, + RT_COUNTRY_UZBEKISTAN, + RT_COUNTRY_VANUATU, + RT_COUNTRY_VENEZUELA, + RT_COUNTRY_VIET_NAM, + RT_COUNTRY_VIRGIN_ISLANDS_BRITISH, + RT_COUNTRY_VIRGIN_ISLANDS_US, + RT_COUNTRY_WALLIS_AND_FUTUNA, + RT_COUNTRY_WEST_BANK, + RT_COUNTRY_WESTERN_SAHARA, + RT_COUNTRY_WORLD_WIDE_XX, + RT_COUNTRY_YEMEN, + RT_COUNTRY_ZAMBIA, + RT_COUNTRY_ZIMBABWE, + RT_COUNTRY_UNKNOWN +} rt_country_code_t; - char *ssid; - uint8_t bssid[6]; +struct rt_wlan_device; +struct rt_wlan_buff; + +typedef void (*rt_wlan_dev_event_handler)(struct rt_wlan_device *device, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff, void *parameter); + +typedef void (*rt_wlan_pormisc_callback_t)(struct rt_wlan_device *device, void *data, int len); + +struct rt_wlan_ssid +{ + rt_uint8_t len; + rt_uint8_t val[RT_WLAN_SSID_MAX_LENGTH + 1]; +}; +typedef struct rt_wlan_ssid rt_wlan_ssid_t; + +struct rt_wlan_key +{ + rt_uint8_t len; + rt_uint8_t val[RT_WLAN_PASSWORD_MAX_LENGTH + 1]; +}; +typedef struct rt_wlan_key rt_wlan_key_t; + +#define INVALID_INFO(_info) do { \ + rt_memset((_info), 0, sizeof(struct rt_wlan_info)); \ + (_info)->band = RT_802_11_BAND_UNKNOWN; \ + (_info)->security = SECURITY_UNKNOWN; \ + (_info)->channel = -1; \ + } while(0) +struct rt_wlan_info +{ + /* security type */ + rt_wlan_security_t security; + /* 2.4G/5G */ + rt_802_11_band_t band; /* maximal data rate */ - uint32_t datarate; + rt_uint32_t datarate; /* radio channel */ - uint16_t channel; + rt_int16_t channel; /* signal strength */ - int16_t rssi; + rt_int16_t rssi; + /* ssid */ + rt_wlan_ssid_t ssid; + /* hwaddr */ + rt_uint8_t bssid[RT_WLAN_BSSID_MAX_LENGTH]; + rt_uint8_t hidden; }; -struct rt_wlan_info_request +struct rt_wlan_buff { - uint16_t req_number; /* the number of information item for request */ - uint16_t rsp_number; /* the number of information item for response */ + void *data; + rt_int32_t len; +}; - struct rt_wlan_info *infos;/* the array of information to save response */ +struct rt_filter_pattern +{ + rt_uint16_t offset; /* Offset in bytes to start filtering (referenced to the start of the ethernet packet) */ + rt_uint16_t mask_size; /* Size of the mask in bytes */ + rt_uint8_t *mask; /* Pattern mask bytes to be ANDed with the pattern eg. "\xff00" (must be in network byte order) */ + rt_uint8_t *pattern; /* Pattern bytes used to filter eg. "\x0800" (must be in network byte order) */ }; -typedef struct rt_wlan_scan_result +typedef enum { - char ap_num; - struct rt_wlan_info *ap_table; -} rt_wlan_scan_result_t; + RT_POSITIVE_MATCHING = 0, /* Receive the data matching with this pattern and discard the other data */ + RT_NEGATIVE_MATCHING = 1 /* Discard the data matching with this pattern and receive the other data */ +} rt_filter_rule_t; -struct rt_wlan_device; -typedef void (*rt_wlan_event_handler)(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data); -typedef void (*rt_wlan_monitor_callback_t)(uint8_t *data, int len, void *user_data); -struct rt_wlan_device +struct rt_wlan_filter { - struct eth_device parent; + struct rt_filter_pattern patt; + rt_filter_rule_t rule; + rt_uint8_t enable; +}; - struct rt_wlan_info *info; - char key[KEY_ARRAY_SIZE + 1]; +struct rt_wlan_dev_event_desc +{ + rt_wlan_dev_event_handler handler; + void *parameter; +}; - rt_wlan_event_handler handler[WIFI_EVT_MAX]; +struct rt_wlan_device +{ + struct rt_device device; + rt_wlan_mode_t mode; + struct rt_mutex lock; + struct rt_wlan_dev_event_desc handler_table[RT_WLAN_DEV_EVT_MAX][RT_WLAN_DEV_EVENT_NUM]; + rt_wlan_pormisc_callback_t pormisc_callback; + const struct rt_wlan_dev_ops *ops; + rt_uint32_t flags; + void *prot; void *user_data; - int interface; +}; + +struct rt_sta_info +{ + rt_wlan_ssid_t ssid; + rt_wlan_key_t key; + rt_uint8_t bssid[6]; + rt_uint16_t channel; + rt_wlan_security_t security; +}; + +struct rt_ap_info +{ + rt_wlan_ssid_t ssid; + rt_wlan_key_t key; + rt_bool_t hidden; + rt_uint16_t channel; + rt_wlan_security_t security; +}; + +struct rt_scan_info +{ + rt_wlan_ssid_t ssid; + rt_uint8_t bssid[6]; + rt_int16_t channel_min; + rt_int16_t channel_max; +}; + +struct rt_wlan_dev_ops +{ + rt_err_t (*wlan_init)(struct rt_wlan_device *wlan); + rt_err_t (*wlan_mode)(struct rt_wlan_device *wlan, rt_wlan_mode_t mode); + rt_err_t (*wlan_scan)(struct rt_wlan_device *wlan, struct rt_scan_info *scan_info); + rt_err_t (*wlan_join)(struct rt_wlan_device *wlan, struct rt_sta_info *sta_info); + rt_err_t (*wlan_softap)(struct rt_wlan_device *wlan, struct rt_ap_info *ap_info); + rt_err_t (*wlan_disconnect)(struct rt_wlan_device *wlan); + rt_err_t (*wlan_ap_stop)(struct rt_wlan_device *wlan); + rt_err_t (*wlan_ap_deauth)(struct rt_wlan_device *wlan, rt_uint8_t mac[]); + rt_err_t (*wlan_scan_stop)(struct rt_wlan_device *wlan); + int (*wlan_get_rssi)(struct rt_wlan_device *wlan); + rt_err_t (*wlan_set_powersave)(struct rt_wlan_device *wlan, int level); + int (*wlan_get_powersave)(struct rt_wlan_device *wlan); + rt_err_t (*wlan_cfg_promisc)(struct rt_wlan_device *wlan, rt_bool_t start); + rt_err_t (*wlan_cfg_filter)(struct rt_wlan_device *wlan, struct rt_wlan_filter *filter); + rt_err_t (*wlan_set_channel)(struct rt_wlan_device *wlan, int channel); + int (*wlan_get_channel)(struct rt_wlan_device *wlan); + rt_err_t (*wlan_set_country)(struct rt_wlan_device *wlan, rt_country_code_t country_code); + rt_country_code_t (*wlan_get_country)(struct rt_wlan_device *wlan); + rt_err_t (*wlan_set_mac)(struct rt_wlan_device *wlan, rt_uint8_t mac[]); + rt_err_t (*wlan_get_mac)(struct rt_wlan_device *wlan, rt_uint8_t mac[]); + int (*wlan_recv)(struct rt_wlan_device *wlan, void *buff, int len); + int (*wlan_send)(struct rt_wlan_device *wlan, void *buff, int len); }; /* - * Wi-Fi Information APIs + * wlan device init */ -void rt_wlan_info_init(struct rt_wlan_info *info, rt_wlan_mode_t mode, rt_wlan_security_t security, - char *ssid); -void rt_wlan_info_deinit(struct rt_wlan_info *info); +rt_err_t rt_wlan_dev_init(struct rt_wlan_device *device, rt_wlan_mode_t mode); /* - * Wi-Fi Manager APIs + * wlan device station interface */ -int rt_wlan_init(struct rt_wlan_device *device, rt_wlan_mode_t mode); - -int rt_wlan_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, - char *password); -int rt_wlan_disconnect(struct rt_wlan_device *device); +rt_err_t rt_wlan_dev_connect(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len); +rt_err_t rt_wlan_dev_disconnect(struct rt_wlan_device *device); +int rt_wlan_dev_get_rssi(struct rt_wlan_device *device); -int rt_wlan_softap(struct rt_wlan_device *device, struct rt_wlan_info *info, - char *password); +/* + * wlan device ap interface + */ +rt_err_t rt_wlan_dev_ap_start(struct rt_wlan_device *device, struct rt_wlan_info *info, const char *password, int password_len); +rt_err_t rt_wlan_dev_ap_stop(struct rt_wlan_device *device); +rt_err_t rt_wlan_dev_ap_deauth(struct rt_wlan_device *device, rt_uint8_t mac[6]); -/* set wifi information for AP */ -int rt_wlan_set_info(struct rt_wlan_device *device, struct rt_wlan_info *info); -/* get wifi information for AP */ -struct rt_wlan_info *rt_wlan_get_info(struct rt_wlan_device *device); +/* + * wlan device scan interface + */ +rt_err_t rt_wlan_dev_scan(struct rt_wlan_device *device, struct rt_wlan_info *info); +rt_err_t rt_wlan_dev_scan_stop(struct rt_wlan_device *device); -/* get the AP result which were scaned in station */ -int rt_wlan_scan(struct rt_wlan_device *device, struct rt_wlan_scan_result **scan_result); +/* + * wlan device mac interface + */ +rt_err_t rt_wlan_dev_get_mac(struct rt_wlan_device *device, rt_uint8_t mac[6]); +rt_err_t rt_wlan_dev_set_mac(struct rt_wlan_device *device, rt_uint8_t mac[6]); -/* get rssi */ -int rt_wlan_get_rssi(struct rt_wlan_device *device); -/* Get/Set MAC */ -int rt_wlan_get_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6]); -int rt_wlan_set_mac(struct rt_wlan_device *device, rt_uint8_t hwaddr[6]); +/* + * wlan device powersave interface + */ +rt_err_t rt_wlan_dev_set_powersave(struct rt_wlan_device *device, int level); +int rt_wlan_dev_get_powersave(struct rt_wlan_device *device); -/* enter power save level */ -int rt_wlan_enter_powersave(struct rt_wlan_device *device, int level); +/* + * wlan device event interface + */ +rt_err_t rt_wlan_dev_register_event_handler(struct rt_wlan_device *device, rt_wlan_dev_event_t event, rt_wlan_dev_event_handler handler, void *parameter); +rt_err_t rt_wlan_dev_unregister_event_handler(struct rt_wlan_device *device, rt_wlan_dev_event_t event, rt_wlan_dev_event_handler handler); +void rt_wlan_dev_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff); -/* register the event handler */ -int rt_wlan_register_event_handler(struct rt_wlan_device *device, rt_wlan_event_t event, - rt_wlan_event_handler handler); +/* + * wlan device promisc interface + */ +rt_err_t rt_wlan_dev_enter_promisc(struct rt_wlan_device *device); +rt_err_t rt_wlan_dev_exit_promisc(struct rt_wlan_device *device); +rt_err_t rt_wlan_dev_set_promisc_callback(struct rt_wlan_device *device, rt_wlan_pormisc_callback_t callback); +void rt_wlan_dev_promisc_handler(struct rt_wlan_device *device, void *data, int len); -/* un-register the event handler */ -int rt_wlan_unregister_event_handler(struct rt_wlan_device *device, rt_wlan_event_t event); +/* + * wlan device filter interface + */ +rt_err_t rt_wlan_dev_cfg_filter(struct rt_wlan_device *device, struct rt_wlan_filter *filter); -/* wlan driver indicate event to upper layer through wifi_indication. */ -int rt_wlan_indicate_event_handle(struct rt_wlan_device *device, rt_wlan_event_t event, - void *user_data); +/* + * wlan device channel interface + */ +rt_err_t rt_wlan_dev_set_channel(struct rt_wlan_device *device, int channel); +rt_err_t rt_wlan_dev_get_channel(struct rt_wlan_device *device); -/* start or stop monitor */ -int rt_wlan_cfg_monitor(struct rt_wlan_device *device, rt_wlan_monitor_opition_t opition); +/* + * wlan device country interface + */ +rt_err_t rt_wlan_dev_set_country(struct rt_wlan_device *device, rt_country_code_t country_code); +rt_country_code_t rt_wlan_dev_get_country(struct rt_wlan_device *device); -/* set callback function for monitor mode*/ -int rt_wlan_set_monitor_callback(struct rt_wlan_device *device, rt_wlan_monitor_callback_t callback); +/* + * wlan device datat transfer interface + */ +rt_err_t rt_wlan_dev_report_data(struct rt_wlan_device *device, void *buff, int len); +// void rt_wlan_dev_data_ready(struct rt_wlan_device *device, int len); -/* Set the monitor channel */ -int rt_wlan_set_channel(struct rt_wlan_device *device, int channel); +/* + * wlan device register interface + */ +struct rt_wlan_device *rt_wlan_dev_register(const char *name, const struct rt_wlan_dev_ops *ops, rt_uint32_t flag, void *user_data); -void rt_wlan_release_scan_result(struct rt_wlan_scan_result **scan_result); +#ifdef __cplusplus +} +#endif #endif diff --git a/components/drivers/wlan/wlan_lwip.c b/components/drivers/wlan/wlan_lwip.c new file mode 100644 index 0000000000..ec6ca23845 --- /dev/null +++ b/components/drivers/wlan/wlan_lwip.c @@ -0,0 +1,488 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-08-14 tyx the first version + */ + +#include <rthw.h> +#include <rtthread.h> +#include <wlan_dev.h> +#include <wlan_prot.h> +#include <wlan_workqueue.h> + +#ifdef RT_USING_LWIP +#include <netif/ethernetif.h> +#include <lwip/netifapi.h> +#ifdef LWIP_USING_DHCPD +#include <dhcp_server.h> +#endif + +#define DBG_ENABLE +#ifdef RT_WLAN_LWIP_DEBUG +#define DBG_LEVEL DBG_LOG +#else +#define DBG_LEVEL DBG_INFO +#endif +#define DBG_SECTION_NAME "WLAN.lwip" +#define DBG_COLOR +#include <rtdbg.h> + +#ifndef IPADDR_STRLEN_MAX +#define IPADDR_STRLEN_MAX (32) +#endif + +struct lwip_prot_des +{ + struct rt_wlan_prot prot; + struct eth_device eth; + rt_int8_t connected_flag; + struct rt_timer timer; + struct rt_work work; +}; + +static void netif_is_ready(struct rt_work *work, void *parameter) +{ + ip_addr_t ip_addr_zero = { 0 }; + struct rt_wlan_device *wlan = parameter; + struct lwip_prot_des *lwip_prot = (struct lwip_prot_des *)wlan->prot; + struct eth_device *eth_dev = &lwip_prot->eth; + rt_base_t level; + struct rt_wlan_buff buff; + rt_uint32_t ip_addr[4]; + char str[IPADDR_STRLEN_MAX]; + + rt_timer_stop(&lwip_prot->timer); + if (ip_addr_cmp(&(eth_dev->netif->ip_addr), &ip_addr_zero) != 0) + { + rt_timer_start(&lwip_prot->timer); + goto exit; + } + rt_memset(&ip_addr, 0, sizeof(ip_addr)); +#if LWIP_IPV4 && LWIP_IPV6 + if (eth_dev->netif->ip_addr.type == IPADDR_TYPE_V4) + { + ip_addr[0] = ip4_addr_get_u32(ð_dev->netif->ip_addr.u_addr.ip4); + buff.data = &ip_addr[0]; + buff.len = sizeof(ip_addr[0]); + } + else if (eth_dev->netif->ip_addr.type == IPADDR_TYPE_V6) + { + *(ip6_addr_t *)(&ip_addr[0]) = eth_dev->netif->ip_addr.u_addr.ip6; + buff.data = ip_addr; + buff.len = sizeof(ip_addr); + } + else + { + LOG_W("F:%s L:%d ip addr type not support", __FUNCTION__, __LINE__); + } +#else +#if LWIP_IPV4 + ip_addr[0] = ip4_addr_get_u32(ð_dev->netif->ip_addr); + buff.data = &ip_addr[0]; + buff.len = sizeof(ip_addr[0]); +#else + *(ip_addr_t *)(&ip_addr[0]) = eth_dev->netif->ip_addr; + buff.data = ip_addr; + buff.len = sizeof(ip_addr); +#endif +#endif + if (rt_wlan_prot_ready(wlan, &buff) != 0) + { + rt_timer_start(&lwip_prot->timer); + goto exit; + } + rt_memset(str, 0, IPADDR_STRLEN_MAX); + rt_enter_critical(); + rt_memcpy(str, ipaddr_ntoa(&(eth_dev->netif->ip_addr)), IPADDR_STRLEN_MAX); + rt_exit_critical(); + LOG_I("Got IP address : %s", str); +exit: + level = rt_hw_interrupt_disable(); + rt_memset(work, 0, sizeof(struct rt_work)); + rt_hw_interrupt_enable(level); +} + +static void timer_callback(void *parameter) +{ + struct rt_workqueue *workqueue; + struct rt_wlan_device *wlan = parameter; + struct lwip_prot_des *lwip_prot = (struct lwip_prot_des *)wlan->prot; + struct rt_work *work = &lwip_prot->work; + rt_base_t level; + + workqueue = rt_wlan_get_workqueue(); + if (workqueue != RT_NULL) + { + level = rt_hw_interrupt_disable(); + rt_work_init(work, netif_is_ready, parameter); + rt_hw_interrupt_enable(level); + if (rt_workqueue_dowork(workqueue, work) != RT_EOK) + { + level = rt_hw_interrupt_disable(); + rt_memset(work, 0, sizeof(struct rt_work)); + rt_hw_interrupt_enable(level); + } + } +} + +static void netif_set_connected(void *parameter) +{ + struct rt_wlan_device *wlan = parameter; + struct lwip_prot_des *lwip_prot = wlan->prot; + struct eth_device *eth_dev = &lwip_prot->eth; + + if (lwip_prot->connected_flag) + { + if (wlan->mode == RT_WLAN_STATION) + { + LOG_D("F:%s L:%d dhcp start run", __FUNCTION__, __LINE__); + netifapi_netif_common(eth_dev->netif, netif_set_link_up, NULL); +#ifdef RT_LWIP_DHCP + dhcp_start(eth_dev->netif); +#endif + rt_timer_start(&lwip_prot->timer); + } + else if (wlan->mode == RT_WLAN_AP) + { + LOG_D("F:%s L:%d dhcpd start run", __FUNCTION__, __LINE__); + + netifapi_netif_common(eth_dev->netif, netif_set_link_up, NULL); +#ifdef LWIP_USING_DHCPD + { + char netif_name[8]; + int i; + + rt_memset(netif_name, 0, sizeof(netif_name)); + for (i = 0; i < sizeof(eth_dev->netif->name); i++) + { + netif_name[i] = eth_dev->netif->name[i]; + } + dhcpd_start(netif_name); + } +#endif + } + } + else + { + if (wlan->mode == RT_WLAN_STATION) + { + LOG_D("F:%s L:%d dhcp stop run", __FUNCTION__, __LINE__); + netifapi_netif_common(eth_dev->netif, netif_set_link_down, NULL); +#ifdef RT_LWIP_DHCP + { + ip_addr_t ip_addr = { 0 }; + dhcp_stop(eth_dev->netif); + netif_set_addr(eth_dev->netif, &ip_addr, &ip_addr, &ip_addr); + } +#endif + rt_timer_stop(&lwip_prot->timer); + } + else if (wlan->mode == RT_WLAN_AP) + { + LOG_D("F:%s L:%d dhcpd stop run", __FUNCTION__, __LINE__); + netifapi_netif_common(eth_dev->netif, netif_set_link_down, NULL); + } + } +} + +static void rt_wlan_lwip_event_handle(struct rt_wlan_prot *port, struct rt_wlan_device *wlan, int event) +{ + struct lwip_prot_des *lwip_prot = (struct lwip_prot_des *)wlan->prot; + rt_bool_t flag_old; + + flag_old = lwip_prot->connected_flag; + + switch (event) + { + case RT_WLAN_PROT_EVT_CONNECT: + { + LOG_D("event: CONNECT"); + lwip_prot->connected_flag = RT_TRUE; + break; + } + case RT_WLAN_PROT_EVT_DISCONNECT: + { + LOG_D("event: DISCONNECT"); + lwip_prot->connected_flag = RT_FALSE; + break; + } + case RT_WLAN_PROT_EVT_AP_START: + { + LOG_D("event: AP_START"); + lwip_prot->connected_flag = RT_TRUE; + break; + } + case RT_WLAN_PROT_EVT_AP_STOP: + { + LOG_D("event: AP_STOP"); + lwip_prot->connected_flag = RT_FALSE; + break; + } + case RT_WLAN_PROT_EVT_AP_ASSOCIATED: + { + LOG_D("event: ASSOCIATED"); + break; + } + case RT_WLAN_PROT_EVT_AP_DISASSOCIATED: + { + LOG_D("event: DISASSOCIATED"); + break; + } + default : + { + LOG_D("event: UNKNOWN"); + break; + } + } + if (flag_old != lwip_prot->connected_flag) + { + rt_wlan_workqueue_dowork(netif_set_connected, wlan); + // netif_set_connected(wlan); + } +} + +static rt_err_t rt_wlan_lwip_protocol_control(rt_device_t device, int cmd, void *args) +{ + struct eth_device *eth_dev = (struct eth_device *)device; + struct rt_wlan_device *wlan; + rt_err_t err = RT_EOK; + + RT_ASSERT(eth_dev != RT_NULL); + + LOG_D("F:%s L:%d device:0x%08x user_data:0x%08x", __FUNCTION__, __LINE__, eth_dev, eth_dev->parent.user_data); + + switch (cmd) + { + case NIOCTL_GADDR: + /* get MAC address */ + wlan = eth_dev->parent.user_data; + err = rt_device_control((rt_device_t)wlan, RT_WLAN_CMD_GET_MAC, args); + break; + default : + break; + } + return err; +} + +static rt_err_t rt_wlan_lwip_protocol_recv(struct rt_wlan_device *wlan, void *buff, int len) +{ + struct eth_device *eth_dev = &((struct lwip_prot_des *)wlan->prot)->eth; + struct pbuf *p = RT_NULL; + + LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__); + + if (eth_dev == RT_NULL) + { + return -RT_ERROR; + } +#ifdef RT_WLAN_PROT_LWIP_PBUF_FORCE + { + p = buff; + if ((eth_dev->netif->input(p, eth_dev->netif)) != ERR_OK) + { + return -RT_ERROR; + } + return RT_EOK; + } +#else + { + int count = 0; + + while (p == RT_NULL) + { + p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); + if (p != RT_NULL) + break; + + p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM); + if (p != RT_NULL) + break; + + LOG_D("F:%s L:%d wait for pbuf_alloc!", __FUNCTION__, __LINE__); + rt_thread_delay(1); + count++; + + //wait for 10ms or give up!! + if (count >= 10) + { + LOG_W("F:%s L:%d pbuf allocate fail!!!", __FUNCTION__, __LINE__); + return -RT_ENOMEM; + } + } + /*copy data dat -> pbuf*/ + pbuf_take(p, buff, len); + if ((eth_dev->netif->input(p, eth_dev->netif)) != ERR_OK) + { + LOG_D("F:%s L:%d IP input error", __FUNCTION__, __LINE__); + pbuf_free(p); + p = RT_NULL; + } + LOG_D("F:%s L:%d netif iput success! len:%d", __FUNCTION__, __LINE__, len); + return RT_EOK; + } +#endif +} + +static rt_err_t rt_wlan_lwip_protocol_send(rt_device_t device, struct pbuf *p) +{ + struct rt_wlan_device *wlan = ((struct eth_device *)device)->parent.user_data; + + LOG_D("F:%s L:%d run", __FUNCTION__, __LINE__); + + if (wlan == RT_NULL) + { + return RT_EOK; + } + +#ifdef RT_WLAN_PROT_LWIP_PBUF_FORCE + { + rt_wlan_prot_transfer_dev(wlan, p, p->tot_len); + return RT_EOK; + } +#else + { + rt_uint8_t *frame; + + /* sending data directly */ + if (p->len == p->tot_len) + { + frame = (rt_uint8_t *)p->payload; + rt_wlan_prot_transfer_dev(wlan, frame, p->tot_len); + LOG_D("F:%s L:%d run len:%d", __FUNCTION__, __LINE__, p->tot_len); + return RT_EOK; + } + frame = rt_malloc(p->tot_len); + if (frame == RT_NULL) + { + LOG_E("F:%s L:%d malloc out_buf fail\n", __FUNCTION__, __LINE__); + return -RT_ENOMEM; + } + /*copy pbuf -> data dat*/ + pbuf_copy_partial(p, frame, p->tot_len, 0); + /* send data */ + rt_wlan_prot_transfer_dev(wlan, frame, p->tot_len); + LOG_D("F:%s L:%d run len:%d", __FUNCTION__, __LINE__, p->tot_len); + rt_free(frame); + return RT_EOK; + } +#endif +} + +static struct rt_wlan_prot *rt_wlan_lwip_protocol_register(struct rt_wlan_prot *prot, struct rt_wlan_device *wlan) +{ + struct eth_device *eth = RT_NULL; + static rt_uint8_t id = 0; + char eth_name[4], timer_name[16]; + rt_device_t device = RT_NULL; + struct lwip_prot_des *lwip_prot; + + if (wlan == RT_NULL || prot == RT_NULL) + return RT_NULL;; + + LOG_D("F:%s L:%d is run wlan:0x%08x", __FUNCTION__, __LINE__, wlan); + + do + { + /* find ETH device name */ + eth_name[0] = 'w'; + eth_name[1] = '0' + id++; + eth_name[2] = '\0'; + device = rt_device_find(eth_name); + } + while (device); + + if (id > 9) + { + LOG_E("F:%s L:%d not find Empty name", __FUNCTION__, __LINE__, eth_name); + return RT_NULL; + } + + if (rt_device_open((rt_device_t)wlan, RT_DEVICE_OFLAG_RDWR) != RT_EOK) + { + LOG_E("F:%s L:%d open wlan failed", __FUNCTION__, __LINE__); + return RT_NULL; + } + + lwip_prot = rt_malloc(sizeof(struct lwip_prot_des)); + if (lwip_prot == RT_NULL) + { + LOG_E("F:%s L:%d malloc mem failed", __FUNCTION__, __LINE__); + rt_device_close((rt_device_t)wlan); + return RT_NULL; + } + rt_memset(lwip_prot, 0, sizeof(struct lwip_prot_des)); + + eth = &lwip_prot->eth; + eth->parent.init = RT_NULL; + eth->parent.open = RT_NULL; + eth->parent.close = RT_NULL; + eth->parent.read = RT_NULL; + eth->parent.write = RT_NULL; + eth->parent.control = rt_wlan_lwip_protocol_control; + eth->parent.user_data = wlan; + eth->eth_rx = RT_NULL; + eth->eth_tx = rt_wlan_lwip_protocol_send; + + /* register ETH device */ + if (eth_device_init(eth, eth_name) != RT_EOK) + { + LOG_E("eth device init failed"); + rt_device_close((rt_device_t)wlan); + rt_free(lwip_prot); + return RT_NULL; + } + rt_memcpy(&lwip_prot->prot, prot, sizeof(struct rt_wlan_prot)); + if (wlan->mode == RT_WLAN_STATION) + { + rt_sprintf(timer_name, "timer_%s", eth_name); + rt_timer_init(&lwip_prot->timer, timer_name, timer_callback, wlan, rt_tick_from_millisecond(1000), + RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_ONE_SHOT); + } + netif_set_up(eth->netif); + LOG_I("eth device init ok name:%s", eth_name); + + return &lwip_prot->prot; +} + +static void rt_wlan_lwip_protocol_unregister(struct rt_wlan_prot *prot, struct rt_wlan_device *wlan) +{ + /*TODO*/ + LOG_D("F:%s L:%d is run wlan:0x%08x", __FUNCTION__, __LINE__, wlan); +} + +static struct rt_wlan_prot_ops ops = +{ + rt_wlan_lwip_protocol_recv, + rt_wlan_lwip_protocol_register, + rt_wlan_lwip_protocol_unregister +}; + +int rt_wlan_lwip_init(void) +{ + static struct rt_wlan_prot prot; + rt_wlan_prot_event_t event; + + rt_memset(&prot, 0, sizeof(prot)); + rt_strncpy(&prot.name[0], RT_WLAN_PROT_LWIP, RT_WLAN_PROT_NAME_LEN); + prot.ops = &ops; + + if (rt_wlan_prot_regisetr(&prot) != RT_EOK) + { + LOG_E("F:%s L:%d protocol regisetr failed", __FUNCTION__, __LINE__); + return -1; + } + + for (event = RT_WLAN_PROT_EVT_INIT_DONE; event < RT_WLAN_PROT_EVT_MAX; event++) + { + rt_wlan_prot_event_register(&prot, event, rt_wlan_lwip_event_handle); + } + + return 0; +} +INIT_PREV_EXPORT(rt_wlan_lwip_init); + +#endif diff --git a/components/drivers/wlan/wlan_mgnt.c b/components/drivers/wlan/wlan_mgnt.c index 2a728747f2..1d0b0c4aa7 100644 --- a/components/drivers/wlan/wlan_mgnt.c +++ b/components/drivers/wlan/wlan_mgnt.c @@ -1,142 +1,1887 @@ /* - * RT-Thread Wi-Fi Device + * Copyright (c) 2006-2018, RT-Thread Development Team * - * COPYRIGHT (C) 2014 - 2018, Shanghai Real-Thread Technology Co., Ltd - * - * This file is part of RT-Thread (http://www.rt-thread.org) - * - * All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes - * 2018-02-27 EvalZero the first verion + * 2018-08-06 tyx the first version */ +#include <rthw.h> #include <rtthread.h> -#include <rtdevice.h> +#include <wlan_dev.h> +#include <wlan_cfg.h> +#include <wlan_mgnt.h> +#include <wlan_prot.h> +#include <wlan_workqueue.h> -#include <lwip/netifapi.h> - -#include "wlan_dev.h" -#include "wlan_cmd.h" - -#define WLAN_MGNT_DEBUG 1 - -#if WLAN_MGNT_DEBUG -#define WLAN_MGNT_DBG(...) rt_kprintf("[WLAN_MGNT]"),rt_kprintf(__VA_ARGS__) +#define DBG_ENABLE +#ifdef RT_WLAN_MGNT_DEBUG +#define DBG_LEVEL DBG_LOG #else -#define WLAN_MGNT_DBG(...) +#define DBG_LEVEL DBG_INFO #endif +#define DBG_SECTION_NAME "WLAN.mgnt" +#define DBG_COLOR +#include <rtdbg.h> -#ifndef WIFI_DEVICE_STA_NAME -#define WIFI_DEVICE_STA_NAME "w0" +#ifndef RT_WLAN_DEVICE +#define RT_WLAN_DEVICE(__device) ((struct rt_wlan_device *)__device) #endif -#ifndef WIFI_DEVICE_AP_NAME -#define WIFI_DEVICE_AP_NAME "ap" + +#define RT_WLAN_LOG_D(_fmt, ...) LOG_D("L:%d "_fmt"", __LINE__, ##__VA_ARGS__) +#define RT_WLAN_LOG_I(...) LOG_I(__VA_ARGS__) +#define RT_WLAN_LOG_W(_fmt, ...) LOG_W("F:%s L:%d "_fmt"", __FUNCTION__, __LINE__, ##__VA_ARGS__) +#define RT_WLAN_LOG_E(_fmt, ...) LOG_E("F:%s L:%d "_fmt"", __FUNCTION__, __LINE__, ##__VA_ARGS__) + +#define STA_DEVICE() (_sta_mgnt.device) +#define AP_DEVICE() (_ap_mgnt.device) + +#define SRESULT_LOCK() (rt_mutex_take(&scan_result_mutex, RT_WAITING_FOREVER)) +#define SRESULT_UNLOCK() (rt_mutex_release(&scan_result_mutex)) + +#define STAINFO_LOCK() (rt_mutex_take(&sta_info_mutex, RT_WAITING_FOREVER)) +#define STAINFO_UNLOCK() (rt_mutex_release(&sta_info_mutex)) + +#define MGNT_LOCK() (rt_mutex_take(&mgnt_mutex, RT_WAITING_FOREVER)) +#define MGNT_UNLOCK() (rt_mutex_release(&mgnt_mutex)) + +#define COMPLETE_LOCK() (rt_mutex_take(&complete_mutex, RT_WAITING_FOREVER)) +#define COMPLETE_UNLOCK() (rt_mutex_release(&complete_mutex)) + +#define TIME_STOP() (rt_timer_stop(&reconnect_time)) +#define TIME_START() (rt_timer_start(&reconnect_time)) + +#define DISCONNECT_RESPONSE_TICK (2000) + +#if RT_WLAN_EBOX_NUM < 1 +#error "event box num Too little" #endif -static void wlan_mgnt_init_done_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data) +struct rt_wlan_mgnt_des +{ + struct rt_wlan_device *device; + struct rt_wlan_info info; + struct rt_wlan_key key; + rt_uint8_t state; + rt_uint8_t flags; +}; + +struct rt_wlan_event_desc { - WLAN_MGNT_DBG("wlan init done event callback \n"); + rt_wlan_event_handler handler; + void *parameter; +}; + +struct rt_wlan_sta_list +{ + struct rt_wlan_sta_list *next; + struct rt_wlan_info info; +}; + +struct rt_wlan_sta_des +{ + int num; + struct rt_wlan_sta_list *node; +}; + +struct rt_wlan_msg +{ + rt_int32_t event; + rt_int32_t len; + void *buff; +}; + +struct rt_wlan_complete_des +{ + struct rt_event complete; + rt_uint32_t event_flag; + int index; +}; + +static struct rt_mutex mgnt_mutex; + +static struct rt_wlan_mgnt_des _sta_mgnt; +static struct rt_wlan_mgnt_des _ap_mgnt; + +static struct rt_wlan_scan_result scan_result; +static struct rt_mutex scan_result_mutex; + +static struct rt_wlan_sta_des sta_info; +static struct rt_mutex sta_info_mutex; + +static struct rt_wlan_event_desc event_tab[RT_WLAN_EVT_MAX]; + +static struct rt_wlan_complete_des *complete_tab[5]; +static struct rt_mutex complete_mutex; + +static struct rt_timer reconnect_time; + +rt_inline int _sta_is_null(void) +{ + if (_sta_mgnt.device == RT_NULL) + { + return 1; + } + return 0; } -static void wlan_mgnt_link_up_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data) +rt_inline int _ap_is_null(void) { - WLAN_MGNT_DBG("wlan link up event callback \n"); + if (_ap_mgnt.device == RT_NULL) + { + return 1; + } + return 0; } -static void wlan_mgnt_link_down_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data) +rt_inline rt_bool_t _is_do_connect(void) { - WLAN_MGNT_DBG("wlan link down event callback \n"); + if ((rt_wlan_get_autoreconnect_mode() == RT_FALSE) || + (rt_wlan_is_connected() == RT_TRUE) || + (_sta_mgnt.state & RT_WLAN_STATE_CONNECTING)) + { + return RT_FALSE; + } + return RT_TRUE; } -static void wlan_mgnt_sta_connect_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data) +static void rt_wlan_mgnt_work(void *parameter); + +static rt_err_t rt_wlan_send_msg(rt_wlan_dev_event_t event, void *buff, int len) { - WLAN_MGNT_DBG("wlan sta connect event callback \n"); - struct netif *netif = device->parent.netif; + struct rt_wlan_msg *msg; - netifapi_netif_set_up(netif); - netifapi_netif_set_link_up(netif); -#ifdef RT_LWIP_DHCP - /* start DHCP */ - dhcp_start(netif); + RT_WLAN_LOG_D("F:%s is run event:%d", __FUNCTION__, event); + + /* Event packing */ + msg = rt_malloc(sizeof(struct rt_wlan_msg) + len); + if (msg == RT_NULL) + { + RT_WLAN_LOG_E("wlan mgnt send msg err! No memory"); + return -RT_ENOMEM; + } + rt_memset(msg, 0, sizeof(struct rt_wlan_msg) + len); + msg->event = event; + if (len != 0) + { + msg->buff = ((char *)msg) + sizeof(struct rt_wlan_msg); + msg->len = len; + } + + /* send event to wlan thread */ + if (rt_wlan_workqueue_dowork(rt_wlan_mgnt_work, msg) != RT_EOK) + { + rt_free(msg); + RT_WLAN_LOG_E("wlan mgnt do work fail"); + return -RT_ERROR; + } + return RT_EOK; +} + +static rt_err_t rt_wlan_scan_result_cache(struct rt_wlan_info *info, int timeout) +{ + struct rt_wlan_info *ptable; + rt_err_t err = RT_EOK; + int i, insert = -1; + + if (_sta_is_null() || (info == RT_NULL)) return RT_EOK; + + RT_WLAN_LOG_D("ssid:%s len:%d mac:%02x:%02x:%02x:%02x:%02x:%02x", info->ssid.val, info->ssid.len, + info->bssid[0], info->bssid[1], info->bssid[2], info->bssid[3], info->bssid[4], info->bssid[5]); + + err = rt_mutex_take(&scan_result_mutex, rt_tick_from_millisecond(timeout)); + if (err != RT_EOK) + return err; + + /* de-duplicatio */ + for (i = 0; i < scan_result.num; i++) + { + if ((info->ssid.len == scan_result.info[i].ssid.len) && + (rt_memcmp(&info->bssid[0], &scan_result.info[i].bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0)) + { + rt_mutex_release(&scan_result_mutex); + return RT_EOK; + } +#ifdef RT_WLAN_SCAN_SORT + if (insert >= 0) + { + continue; + } + /* Signal intensity comparison */ + if ((info->rssi < 0) && (scan_result.info[i].rssi < 0)) + { + if (info->rssi > scan_result.info[i].rssi) + { + insert = i; + continue; + } + else if (info->rssi < scan_result.info[i].rssi) + { + continue; + } + } + + /* Channel comparison */ + if (info->channel < scan_result.info[i].channel) + { + insert = i; + continue; + } + else if (info->channel > scan_result.info[i].channel) + { + continue; + } + + /* data rate comparison */ + if ((info->datarate > scan_result.info[i].datarate)) + { + insert = i; + continue; + } + else if (info->datarate < scan_result.info[i].datarate) + { + continue; + } #endif + } + + /* Insert the end */ + if (insert == -1) + insert = scan_result.num; + + if (scan_result.num >= RT_WLAN_SCAN_CACHE_NUM) + return RT_EOK; + + /* malloc memory */ + ptable = rt_malloc(sizeof(struct rt_wlan_info) * (scan_result.num + 1)); + if (ptable == RT_NULL) + { + rt_mutex_release(&scan_result_mutex); + RT_WLAN_LOG_E("wlan info malloc failed!"); + return -RT_ENOMEM; + } + scan_result.num ++; + + /* copy info */ + for (i = 0; i < scan_result.num; i++) + { + if (i < insert) + { + ptable[i] = scan_result.info[i]; + } + else if (i > insert) + { + ptable[i] = scan_result.info[i - 1]; + } + else if (i == insert) + { + ptable[i] = *info; + } + } + rt_free(scan_result.info); + scan_result.info = ptable; + rt_mutex_release(&scan_result_mutex); + return err; +} + +static rt_err_t rt_wlan_sta_info_add(struct rt_wlan_info *info, int timeout) +{ + struct rt_wlan_sta_list *sta_list; + rt_err_t err = RT_EOK; + + if (_ap_is_null() || (info == RT_NULL)) return RT_EOK; + + err = rt_mutex_take(&sta_info_mutex, rt_tick_from_millisecond(timeout)); + if (err == RT_EOK) + { + /* malloc memory */ + sta_list = rt_malloc(sizeof(struct rt_wlan_sta_list)); + if (sta_list == RT_NULL) + { + rt_mutex_release(&sta_info_mutex); + RT_WLAN_LOG_E("sta list malloc failed!"); + return -RT_ENOMEM; + } + sta_list->next = RT_NULL; + sta_list->info = *info; + + /* Append sta info */ + sta_list->next = sta_info.node; + sta_info.node = sta_list; + /* num++ */ + sta_info.num ++; + rt_mutex_release(&sta_info_mutex); + RT_WLAN_LOG_I("sta associated mac:%02x:%02x:%02x:%02x:%02x:%02x", + info->bssid[0], info->bssid[1], info->bssid[2], + info->bssid[3], info->bssid[4], info->bssid[5]); + } + return err; +} + +static rt_err_t rt_wlan_sta_info_del(struct rt_wlan_info *info, int timeout) +{ + struct rt_wlan_sta_list *sta_list, *sta_prve; + rt_err_t err = RT_EOK; + + if (_ap_is_null() || (info == RT_NULL)) return RT_EOK; + + err = rt_mutex_take(&sta_info_mutex, rt_tick_from_millisecond(timeout)); + if (err == RT_EOK) + { + /* traversing the list */ + for (sta_list = sta_info.node, sta_prve = RT_NULL; sta_list != RT_NULL; + sta_prve = sta_list, sta_list = sta_list->next) + { + /* find mac addr */ + if (rt_memcmp(&sta_list->info.bssid[0], &info->bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0) + { + if (sta_prve == RT_NULL) + { + sta_info.node = sta_list->next; + } + else + { + sta_prve->next = sta_list->next; + } + sta_info.num --; + rt_free(sta_list); + break; + } + } + rt_mutex_release(&sta_info_mutex); + RT_WLAN_LOG_I("sta exit mac:%02x:%02x:%02x:%02x:%02x:%02x", + info->bssid[0], info->bssid[1], info->bssid[2], + info->bssid[3], info->bssid[4], info->bssid[5]); + } + return err; } -static void wlan_mgnt_sta_disconnect_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data) +static rt_err_t rt_wlan_sta_info_del_all(int timeout) { - WLAN_MGNT_DBG("wlan sta disconnect event callback \n"); - netifapi_netif_set_down(device->parent.netif); - netifapi_netif_set_link_down(device->parent.netif); - rt_memset(&device->parent.netif->ip_addr, 0, sizeof(ip_addr_t)); - rt_memset(&device->parent.netif->netmask, 0, sizeof(ip_addr_t)); - rt_memset(&device->parent.netif->gw, 0, sizeof(ip_addr_t)); -#ifdef RT_LWIP_DHCP - dhcp_stop(device->parent.netif); + struct rt_wlan_sta_list *sta_list, *sta_next; + rt_err_t err = RT_EOK; + + err = rt_mutex_take(&sta_info_mutex, rt_tick_from_millisecond(timeout)); + if (err == RT_EOK) + { + /* traversing the list */ + for (sta_list = sta_info.node; sta_list != RT_NULL; sta_list = sta_next) + { + sta_next = sta_list->next; + sta_info.num --; + rt_free(sta_list); + } + rt_mutex_release(&sta_info_mutex); + } + if (sta_info.num != 0) + { + RT_WLAN_LOG_W("\n\n!!!Program runing exception!!!\n\n"); + } + sta_info.num = 0; + sta_info.node = RT_NULL; + return err; +} + +static void rt_wlan_auto_connect_run(struct rt_work *work, void *parameter) +{ + static rt_uint32_t id = 0; + struct rt_wlan_cfg_info cfg_info; + char *password = RT_NULL; + rt_base_t level; + + RT_WLAN_LOG_D("F:%s is run", __FUNCTION__); + + if (rt_mutex_take(&mgnt_mutex, 0) != RT_EOK) + goto exit; + + /* auto connect status is disable or wifi is connect or connecting, exit */ + if (_is_do_connect() == RT_FALSE) + { + id = 0; + RT_WLAN_LOG_D("not connection"); + goto exit; + } + + /* Read the next configuration */ + rt_memset(&cfg_info, 0, sizeof(struct rt_wlan_cfg_info)); + if (rt_wlan_cfg_read_index(&cfg_info, id ++) == 0) + { + RT_WLAN_LOG_D("read cfg fail"); + id = 0; + goto exit; + } + + if (id >= rt_wlan_cfg_get_num()) id = 0; + + if ((cfg_info.key.len > 0) && (cfg_info.key.len < RT_WLAN_PASSWORD_MAX_LENGTH)) + { + cfg_info.key.val[cfg_info.key.len] = '\0'; + password = (char *)(&cfg_info.key.val[0]); + } + rt_wlan_connect_adv(&cfg_info.info, password); +exit: + rt_mutex_release(&mgnt_mutex); + level = rt_hw_interrupt_disable(); + rt_memset(work, 0, sizeof(struct rt_work)); + rt_hw_interrupt_enable(level); +} + +static void rt_wlan_cyclic_check(void *parameter) +{ + struct rt_workqueue *workqueue; + static struct rt_work work; + rt_base_t level; + + if ((_is_do_connect() == RT_TRUE) && (work.work_func == RT_NULL)) + { + workqueue = rt_wlan_get_workqueue(); + if (workqueue != RT_NULL) + { + level = rt_hw_interrupt_disable(); + rt_work_init(&work, rt_wlan_auto_connect_run, RT_NULL); + rt_hw_interrupt_enable(level); + if (rt_workqueue_dowork(workqueue, &work) != RT_EOK) + { + level = rt_hw_interrupt_disable(); + rt_memset(&work, 0, sizeof(struct rt_work)); + rt_hw_interrupt_enable(level); + } + } + } +} + +static void rt_wlan_mgnt_work(void *parameter) +{ + struct rt_wlan_msg *msg = parameter; + + switch (msg->event) + { + case RT_WLAN_DEV_EVT_CONNECT: + { + struct rt_wlan_cfg_info cfg_info; + + /* save config */ + if (rt_wlan_is_connected() == RT_TRUE) + { + rt_enter_critical(); + cfg_info.info = _sta_mgnt.info; + cfg_info.key = _sta_mgnt.key; + rt_exit_critical(); + RT_WLAN_LOG_D("run save config! ssid:%s len%d", _sta_mgnt.info.ssid.val, _sta_mgnt.info.ssid.len); + rt_wlan_cfg_save(&cfg_info); + } + break; + } + default : + break; + } + + rt_free(msg); +} + +static void rt_wlan_event_dispatch(struct rt_wlan_device *device, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff, void *parameter) +{ + rt_base_t level; + void *user_parameter; + rt_wlan_event_handler handler = RT_NULL; + rt_err_t err = RT_NULL; + rt_wlan_event_t user_event = RT_WLAN_EVT_MAX; + int i; + struct rt_wlan_buff user_buff = { 0 }; + + if (buff) + { + user_buff = *buff; + } + /* Event Handle */ + switch (event) + { + case RT_WLAN_DEV_EVT_CONNECT: + { + RT_WLAN_LOG_D("event: CONNECT"); + _sta_mgnt.state |= RT_WLAN_STATE_CONNECT; + _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECTING; + user_event = RT_WLAN_EVT_STA_CONNECTED; + TIME_STOP(); + rt_wlan_send_msg(event, RT_NULL, 0); + user_buff.data = &_sta_mgnt.info; + user_buff.len = sizeof(struct rt_wlan_info); + RT_WLAN_LOG_I("wifi connect success ssid:%s", &_sta_mgnt.info.ssid.val[0]); + break; + } + case RT_WLAN_DEV_EVT_CONNECT_FAIL: + { + RT_WLAN_LOG_D("event: CONNECT_FAIL"); + _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECT; + _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECTING; + _sta_mgnt.state &= ~RT_WLAN_STATE_READY; + user_event = RT_WLAN_EVT_STA_CONNECTED_FAIL; + user_buff.data = &_sta_mgnt.info; + user_buff.len = sizeof(struct rt_wlan_info); + TIME_START(); + break; + } + case RT_WLAN_DEV_EVT_DISCONNECT: + { + RT_WLAN_LOG_D("event: DISCONNECT"); + _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECT; + _sta_mgnt.state &= ~RT_WLAN_STATE_READY; + user_event = RT_WLAN_EVT_STA_DISCONNECTED; + user_buff.data = &_sta_mgnt.info; + user_buff.len = sizeof(struct rt_wlan_info); + TIME_START(); + break; + } + case RT_WLAN_DEV_EVT_AP_START: + { + RT_WLAN_LOG_D("event: AP_START"); + _ap_mgnt.state |= RT_WLAN_STATE_ACTIVE; + user_event = RT_WLAN_EVT_AP_START; + user_buff.data = &_ap_mgnt.info; + user_buff.len = sizeof(struct rt_wlan_info); + break; + } + case RT_WLAN_DEV_EVT_AP_STOP: + { + RT_WLAN_LOG_D("event: AP_STOP"); + _ap_mgnt.state &= ~RT_WLAN_STATE_ACTIVE; + user_event = RT_WLAN_EVT_AP_STOP; + err = rt_wlan_sta_info_del_all(RT_WAITING_FOREVER); + if (err != RT_NULL) + { + RT_WLAN_LOG_W("AP_STOP event handle fail"); + } + user_buff.data = &_ap_mgnt.info; + user_buff.len = sizeof(struct rt_wlan_info); + break; + } + case RT_WLAN_DEV_EVT_AP_ASSOCIATED: + { + RT_WLAN_LOG_D("event: ASSOCIATED"); + user_event = RT_WLAN_EVT_AP_ASSOCIATED; + if (user_buff.len != sizeof(struct rt_wlan_info)) + break; + err = rt_wlan_sta_info_add(user_buff.data, RT_WAITING_FOREVER); + if (err != RT_EOK) + { + RT_WLAN_LOG_W("AP_ASSOCIATED event handle fail"); + } + break; + } + case RT_WLAN_DEV_EVT_AP_DISASSOCIATED: + { + RT_WLAN_LOG_D("event: DISASSOCIATED"); + user_event = RT_WLAN_EVT_AP_DISASSOCIATED; + if (user_buff.len != sizeof(struct rt_wlan_info)) + break; + err = rt_wlan_sta_info_del(user_buff.data, RT_WAITING_FOREVER); + if (err != RT_EOK) + { + RT_WLAN_LOG_W("AP_DISASSOCIATED event handle fail"); + } + break; + } + case RT_WLAN_DEV_EVT_AP_ASSOCIATE_FAILED: + { + RT_WLAN_LOG_D("event: AP_ASSOCIATE_FAILED"); + break; + } + case RT_WLAN_DEV_EVT_SCAN_REPORT: + { + RT_WLAN_LOG_D("event: SCAN_REPORT"); + user_event = RT_WLAN_EVT_SCAN_REPORT; + if (user_buff.len != sizeof(struct rt_wlan_info)) + break; + rt_wlan_scan_result_cache(user_buff.data, 0); + break; + } + case RT_WLAN_DEV_EVT_SCAN_DONE: + { + RT_WLAN_LOG_D("event: SCAN_DONE"); + user_buff.data = &scan_result; + user_buff.len = sizeof(scan_result); + user_event = RT_WLAN_EVT_SCAN_DONE; + break; + } + default : + { + RT_WLAN_LOG_D("event: UNKNOWN"); + return; + } + } + + /* send event */ + COMPLETE_LOCK(); + for (i = 0; i < sizeof(complete_tab) / sizeof(complete_tab[0]); i++) + { + if ((complete_tab[i] != RT_NULL)) + { + complete_tab[i]->event_flag |= 0x1 << event; + rt_event_send(&complete_tab[i]->complete, 0x1 << event); + RT_WLAN_LOG_D("&complete_tab[i]->complete:0x%08x", &complete_tab[i]->complete); + } + } + COMPLETE_UNLOCK(); + /* Get user callback */ + if (user_event < RT_WLAN_EVT_MAX) + { + level = rt_hw_interrupt_disable(); + handler = event_tab[user_event].handler; + user_parameter = event_tab[user_event].parameter; + rt_hw_interrupt_enable(level); + } + + /* run user callback fun */ + if (handler) + { + handler(user_event, &user_buff, user_parameter); + } +} + +static struct rt_wlan_complete_des *rt_wlan_complete_create(const char *name) +{ + struct rt_wlan_complete_des *complete; + int i; + + complete = rt_malloc(sizeof(struct rt_wlan_complete_des)); + if (complete == RT_NULL) + { + RT_WLAN_LOG_E("complete event create failed"); + MGNT_UNLOCK(); + return complete; + } + rt_event_init(&complete->complete, name, RT_IPC_FLAG_FIFO); + complete->event_flag = 0; + //protect + COMPLETE_LOCK(); + for (i = 0; i < sizeof(complete_tab) / sizeof(complete_tab[0]); i++) + { + if (complete_tab[i] == RT_NULL) + { + complete->index = i; + complete_tab[i] = complete; + break; + } + } + COMPLETE_UNLOCK(); + + if (i >= sizeof(complete_tab) / sizeof(complete_tab[0])) + { + rt_event_detach(&complete->complete); + rt_free(complete); + complete = RT_NULL; + } + + return complete; +} + +static rt_err_t rt_wlan_complete_wait(struct rt_wlan_complete_des *complete, rt_uint32_t event, + rt_uint32_t timeout, rt_uint32_t *recved) +{ + if (complete == RT_NULL) + { + return -RT_ERROR; + } + + /* Check whether there is a waiting event */ + if (complete->event_flag & event) + { + *recved = complete->event_flag; + return RT_EOK; + } + else + { + return rt_event_recv(&complete->complete, event, RT_EVENT_FLAG_OR, + rt_tick_from_millisecond(timeout), recved); + } +} + +static void rt_wlan_complete_delete(struct rt_wlan_complete_des *complete) +{ + if (complete == RT_NULL) + { + return; + } + COMPLETE_LOCK(); + complete_tab[complete->index] = RT_NULL; + COMPLETE_UNLOCK(); + rt_event_detach(&complete->complete); + rt_free(complete); +} + +rt_err_t rt_wlan_set_mode(const char *dev_name, rt_wlan_mode_t mode) +{ + rt_device_t device = RT_NULL; + rt_err_t err; + rt_int8_t up_event_flag = 0; + rt_wlan_dev_event_handler handler = RT_NULL; + + if ((dev_name == RT_NULL) || (mode >= RT_WLAN_MODE_MAX)) + { + RT_WLAN_LOG_E("Parameter Wrongful name:%s mode:%d", dev_name, mode); + return -RT_EINVAL; + } + + RT_WLAN_LOG_D("%s is run dev_name:%s mode:%s%s%s", __FUNCTION__, dev_name, + mode == RT_WLAN_NONE ? "NONE" : "", + mode == RT_WLAN_STATION ? "STA" : "", + mode == RT_WLAN_AP ? "AP" : "" + ); + + /* find device */ + device = rt_device_find(dev_name); + if (device == RT_NULL) + { + RT_WLAN_LOG_E("not find device, set mode failed! name:%s", dev_name); + return -RT_EIO; + } + + MGNT_LOCK(); + if (RT_WLAN_DEVICE(device)->mode == mode) + { + RT_WLAN_LOG_D("L:%d this device mode is set"); + MGNT_UNLOCK(); + return RT_EOK; + } + + if ((mode == RT_WLAN_STATION) && + (RT_WLAN_DEVICE(device)->flags & RT_WLAN_FLAG_AP_ONLY)) + { + RT_WLAN_LOG_I("this device ap mode only"); + MGNT_UNLOCK(); + return -RT_ERROR; + } + else if ((mode == RT_WLAN_AP) && + (RT_WLAN_DEVICE(device)->flags & RT_WLAN_FLAG_STA_ONLY)) + { + RT_WLAN_LOG_I("this device sta mode only"); + MGNT_UNLOCK(); + return -RT_ERROR; + } + + /* + * device == sta and change to ap, should deinit + * device == ap and change to sta, should deinit + */ + if (((mode == RT_WLAN_STATION) && (RT_WLAN_DEVICE(device) == AP_DEVICE())) || + ((mode == RT_WLAN_AP) && (RT_WLAN_DEVICE(device) == STA_DEVICE()))) + { + err = rt_wlan_set_mode(dev_name, RT_WLAN_NONE); + if (err != RT_EOK) + { + RT_WLAN_LOG_E("change mode failed!"); + MGNT_UNLOCK(); + return err; + } + } + + /* init device */ + err = rt_wlan_dev_init(RT_WLAN_DEVICE(device), mode); + if (err != RT_EOK) + { + RT_WLAN_LOG_E("F:%s L:%d wlan init failed", __FUNCTION__, __LINE__); + MGNT_UNLOCK(); + return err; + } + + /* the mode is none */ + if (mode == RT_WLAN_NONE) + { + if (_sta_mgnt.device == RT_WLAN_DEVICE(device)) + { + _sta_mgnt.device = RT_NULL; + _sta_mgnt.state = 0; + up_event_flag = 1; + handler = RT_NULL; + } + else if (_ap_mgnt.device == RT_WLAN_DEVICE(device)) + { + _ap_mgnt.state = 0; + _ap_mgnt.device = RT_NULL; + up_event_flag = 1; + handler = RT_NULL; + } + } + /* save sta device */ + else if (mode == RT_WLAN_STATION) + { + up_event_flag = 1; + handler = rt_wlan_event_dispatch; + _sta_mgnt.device = RT_WLAN_DEVICE(device); + } + /* save ap device */ + else if (mode == RT_WLAN_AP) + { + up_event_flag = 1; + handler = rt_wlan_event_dispatch; + _ap_mgnt.device = RT_WLAN_DEVICE(device); + } + + /* update dev event handle */ + if (up_event_flag == 1) + { + rt_wlan_dev_event_t event; + for (event = RT_WLAN_DEV_EVT_INIT_DONE; event < RT_WLAN_DEV_EVT_MAX; event++) + { + if (handler) + { + rt_wlan_dev_register_event_handler(RT_WLAN_DEVICE(device), event, handler, RT_NULL); + } + else + { + rt_wlan_dev_unregister_event_handler(RT_WLAN_DEVICE(device), event, handler); + } + } + } + MGNT_UNLOCK(); + + /* Mount protocol */ +#ifdef RT_WLAN_DEFAULT_PROT + rt_wlan_prot_attach(dev_name, RT_WLAN_DEFAULT_PROT); #endif + return err; +} + +rt_wlan_mode_t rt_wlan_get_mode(const char *dev_name) +{ + rt_device_t device = RT_NULL; + rt_wlan_mode_t mode; + + if (dev_name == RT_NULL) + { + RT_WLAN_LOG_E("name is null"); + return RT_WLAN_NONE; + } + + /* find device */ + device = rt_device_find(dev_name); + if (device == RT_NULL) + { + RT_WLAN_LOG_E("device not find! name:%s", dev_name); + return RT_WLAN_NONE; + } + + /* get mode */ + mode = RT_WLAN_DEVICE(device)->mode; + RT_WLAN_LOG_D("%s is run dev_name:%s mode:%s%s%s", __FUNCTION__, dev_name, + mode == RT_WLAN_NONE ? "NONE" : "", + mode == RT_WLAN_STATION ? "STA" : "", + mode == RT_WLAN_AP ? "AP" : ""); + + return mode; +} + +rt_bool_t rt_wlan_find_best_by_cache(const char *ssid, struct rt_wlan_info *info) +{ + int i, ssid_len; + struct rt_wlan_info *info_best; + struct rt_wlan_scan_result *result; + + ssid_len = rt_strlen(ssid); + result = &scan_result; + info_best = RT_NULL; + + SRESULT_LOCK(); + for (i = 0; i < result->num; i++) + { + /* SSID is equal. */ + if ((result->info[i].ssid.len == ssid_len) && + (rt_memcmp((char *)&result->info[i].ssid.val[0], ssid, ssid_len) == 0)) + { + if (info_best == RT_NULL) + { + info_best = &result->info[i]; + continue; + } + /* Signal strength effective */ + if ((result->info[i].rssi < 0) && (info_best->rssi < 0)) + { + /* Find the strongest signal. */ + if (result->info[i].rssi > info_best->rssi) + { + info_best = &result->info[i]; + continue; + } + else if (result->info[i].rssi < info_best->rssi) + { + continue; + } + } + + /* Finding the fastest signal */ + if (result->info[i].datarate > info_best->datarate) + { + info_best = &result->info[i]; + continue; + } + } + } + SRESULT_UNLOCK(); + + if (info_best == RT_NULL) + return RT_FALSE; + + *info = *info_best; + return RT_TRUE; +} + +rt_err_t rt_wlan_connect(const char *ssid, const char *password) +{ + rt_err_t err = RT_EOK; + int ssid_len = 0; + struct rt_wlan_info info; + struct rt_wlan_complete_des *complete; + rt_uint32_t set = 0, recved = 0; + + /* sta dev Can't be NULL */ + if (_sta_is_null()) + { + return -RT_EIO; + } + RT_WLAN_LOG_D("%s is run ssid:%s password:%s", __FUNCTION__, ssid, password); + if (ssid == RT_NULL) + { + RT_WLAN_LOG_E("ssid is null!"); + return -RT_EINVAL; + } + ssid_len = rt_strlen(ssid); + if (ssid_len > RT_WLAN_SSID_MAX_LENGTH) + { + RT_WLAN_LOG_E("ssid is to long! ssid:%s len:%d", ssid, ssid_len); + return -RT_EINVAL; + } + + if ((rt_wlan_is_connected() == RT_TRUE) && + (rt_strcmp((char *)&_sta_mgnt.info.ssid.val[0], ssid) == 0)) + { + RT_WLAN_LOG_I("wifi is connect ssid:%s", ssid); + return RT_EOK; + } + /* get info from cache */ + INVALID_INFO(&info); + MGNT_LOCK(); + if (rt_wlan_find_best_by_cache(ssid, &info) != RT_TRUE) + { + rt_wlan_scan_sync(); + rt_wlan_find_best_by_cache(ssid, &info); + rt_wlan_scan_result_clean(); + } + + if (info.ssid.len <= 0) + { + RT_WLAN_LOG_W("not find ap! ssid:%s", ssid); + MGNT_UNLOCK(); + return -RT_ERROR; + } + + RT_WLAN_LOG_D("find best info ssid:%s mac: %02x %02x %02x %02x %02x %02x", + info.ssid.val, info.bssid[0], info.bssid[1], info.bssid[2], info.bssid[3], info.bssid[4], info.bssid[5]); + + /* create event wait complete */ + complete = rt_wlan_complete_create("join"); + if (complete == RT_NULL) + { + MGNT_UNLOCK(); + return -RT_ENOMEM; + } + /* run connect adv */ + err = rt_wlan_connect_adv(&info, password); + if (err != RT_EOK) + { + rt_wlan_complete_delete(complete); + MGNT_UNLOCK(); + return err; + } + + /* Initializing events that need to wait */ + set |= 0x1 << RT_WLAN_DEV_EVT_CONNECT; + set |= 0x1 << RT_WLAN_DEV_EVT_CONNECT_FAIL; + /* Check whether there is a waiting event */ + rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved); + rt_wlan_complete_delete(complete); + /* check event */ + set = 0x1 << RT_WLAN_DEV_EVT_CONNECT; + if (!(recved & set)) + { + RT_WLAN_LOG_I("wifi connect failed!"); + MGNT_UNLOCK(); + return -RT_ERROR; + } + MGNT_UNLOCK(); + return err; +} + +rt_err_t rt_wlan_connect_adv(struct rt_wlan_info *info, const char *password) +{ + int password_len = 0; + rt_err_t err = RT_EOK; + + if (_sta_is_null()) + { + return -RT_EIO; + } + if (info == RT_NULL) + { + RT_WLAN_LOG_E("info is null!"); + return -RT_EINVAL; + } + RT_WLAN_LOG_D("%s is run ssid:%s password:%s", __FUNCTION__, info->ssid.val, password); + /* Parameter checking */ + if (password != RT_NULL) + { + password_len = rt_strlen(password); + if (password_len > RT_WLAN_PASSWORD_MAX_LENGTH) + { + RT_WLAN_LOG_E("password is to long! password:%s len:%d", password, password_len); + return -RT_EINVAL; + } + } + if (info->ssid.len == 0 || info->ssid.len > RT_WLAN_SSID_MAX_LENGTH) + { + RT_WLAN_LOG_E("ssid is zero or to long! ssid:%s len:%d", info->ssid.val, info->ssid.len); + return -RT_EINVAL; + } + /* is connect ? */ + MGNT_LOCK(); + if (rt_wlan_is_connected()) + { + if ((_sta_mgnt.info.ssid.len == info->ssid.len) && + (_sta_mgnt.key.len == password_len) && + (rt_memcmp(&_sta_mgnt.info.ssid.val[0], &info->ssid.val[0], info->ssid.len) == 0) && + (rt_memcmp(&_sta_mgnt.info.bssid[0], &info->bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0) && + (rt_memcmp(&_sta_mgnt.key.val[0], password, password_len) == 0)) + { + RT_WLAN_LOG_I("wifi Already Connected"); + MGNT_UNLOCK(); + return RT_EOK; + } + + err = rt_wlan_disconnect(); + if (err != RT_EOK) + { + MGNT_UNLOCK(); + return err; + } + } + + /* save info */ + rt_enter_critical(); + _sta_mgnt.info = *info; + rt_memcpy(&_sta_mgnt.key.val, password, password_len); + _sta_mgnt.key.len = password_len; + _sta_mgnt.key.val[password_len] = '\0'; + rt_exit_critical(); + /* run wifi connect */ + _sta_mgnt.state |= RT_WLAN_STATE_CONNECTING; + err = rt_wlan_dev_connect(_sta_mgnt.device, info, password, password_len); + if (err != RT_EOK) + { + rt_enter_critical(); + rt_memset(&_sta_mgnt.info, 0, sizeof(struct rt_wlan_ssid)); + rt_memset(&_sta_mgnt.key, 0, sizeof(struct rt_wlan_key)); + rt_exit_critical(); + _sta_mgnt.state &= ~RT_WLAN_STATE_CONNECTING; + MGNT_UNLOCK(); + return err; + } + + MGNT_UNLOCK(); + return err; } -static void wlan_mgnt_ap_start_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data) +rt_err_t rt_wlan_disconnect(void) { - WLAN_MGNT_DBG("wlan ap start event callback \n"); - netifapi_netif_set_up(device->parent.netif); - netifapi_netif_set_link_up(device->parent.netif); + rt_err_t err; + struct rt_wlan_complete_des *complete; + rt_uint32_t recved = 0, set = 0; + + /* ap dev Can't be empty */ + if (_sta_is_null()) + { + return -RT_EIO; + } + RT_WLAN_LOG_D("%s is run", __FUNCTION__); - wifi_softap_setup_netif(device->parent.netif); + /* run disconnect */ + MGNT_LOCK(); + /* create event wait complete */ + complete = rt_wlan_complete_create("disc"); + if (complete == RT_NULL) + { + MGNT_UNLOCK(); + return -RT_ENOMEM; + } + err = rt_wlan_dev_disconnect(_sta_mgnt.device); + if (err != RT_EOK) + { + RT_WLAN_LOG_E("wifi disconnect fail"); + rt_wlan_complete_delete(complete); + MGNT_UNLOCK(); + return err; + } + /* Initializing events that need to wait */ + set |= 0x1 << RT_WLAN_DEV_EVT_DISCONNECT; + /* Check whether there is a waiting event */ + rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved); + rt_wlan_complete_delete(complete); + /* check event */ + set = 0x1 << RT_WLAN_DEV_EVT_DISCONNECT; + if (!(recved & set)) + { + RT_WLAN_LOG_E("disconnect failed!"); + MGNT_UNLOCK(); + return -RT_ERROR; + } + RT_WLAN_LOG_I("disconnect success!"); + MGNT_UNLOCK(); + return err; } +rt_bool_t rt_wlan_is_connected(void) +{ + rt_bool_t _connect; + + if (_sta_is_null()) + { + return RT_FALSE; + } + _connect = _sta_mgnt.state & RT_WLAN_STATE_CONNECT ? RT_TRUE : RT_FALSE; + RT_WLAN_LOG_D("%s is run : %s", __FUNCTION__, _connect ? "connect" : "disconnect"); + return _connect; +} -static void wlan_mgnt_ap_stop_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data) +rt_bool_t rt_wlan_is_ready(void) { - WLAN_MGNT_DBG("wlan ap stop event callback \n"); + rt_bool_t _ready; - netifapi_netif_set_down(device->parent.netif); - netifapi_netif_set_link_down(device->parent.netif); + if (_sta_is_null()) + { + return RT_FALSE; + } + _ready = _sta_mgnt.state & RT_WLAN_STATE_READY ? RT_TRUE : RT_FALSE; + RT_WLAN_LOG_D("%s is run : %s", __FUNCTION__, _ready ? "ready" : "not ready"); + return _ready; } -static void wlan_mgnt_ap_associate_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data) +rt_err_t rt_wlan_set_mac(rt_uint8_t mac[6]) { - WLAN_MGNT_DBG("wlan ap associate event callback \n"); + rt_err_t err = RT_EOK; + + if (_sta_is_null()) + { + return -RT_EIO; + } + RT_WLAN_LOG_D("%s is run mac: %02x:%02x:%02x:%02x:%02x:%02x", + __FUNCTION__, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + + MGNT_LOCK(); + err = rt_wlan_dev_set_mac(STA_DEVICE(), mac); + if (err != RT_EOK) + { + RT_WLAN_LOG_E("set sta mac addr fail"); + MGNT_UNLOCK(); + return err; + } + MGNT_UNLOCK(); + return err; } -static void wlan_mgnt_ap_disassociate_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data) +rt_err_t rt_wlan_get_mac(rt_uint8_t mac[6]) { - WLAN_MGNT_DBG("wlan ap disassociate event callback \n"); + rt_err_t err = RT_EOK; + + if (_sta_is_null()) + { + return -RT_EIO; + } + MGNT_LOCK(); + err = rt_wlan_dev_get_mac(STA_DEVICE(), mac); + if (err != RT_EOK) + { + RT_WLAN_LOG_E("get sta mac addr fail"); + MGNT_UNLOCK(); + return err; + } + RT_WLAN_LOG_D("%s is run mac: %02x:%02x:%02x:%02x:%02x:%02x", + __FUNCTION__, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + MGNT_UNLOCK(); + return err; } -static void wlan_mgnt_scan_done_event(struct rt_wlan_device *device, rt_wlan_event_t event, void *user_data) +rt_err_t rt_wlan_get_info(struct rt_wlan_info *info) { - WLAN_MGNT_DBG("wlan scan done event callback \n"); + if (_sta_is_null()) + { + return -RT_EIO; + } + RT_WLAN_LOG_D("%s is run", __FUNCTION__); + + rt_enter_critical(); + *info = _sta_mgnt.info; + rt_exit_critical(); + return RT_EOK; } -int rt_wlan_mgnt_attach(struct rt_wlan_device *device, void *user_data) +int rt_wlan_get_rssi(void) { - RT_ASSERT(device != RT_NULL); + int rssi = 0; + + if (_sta_is_null()) + { + return -RT_EIO; + } + + MGNT_LOCK(); + rssi = rt_wlan_dev_get_rssi(STA_DEVICE()); + RT_WLAN_LOG_D("%s is run rssi:%d", __FUNCTION__, rssi); + MGNT_UNLOCK(); + return rssi; +} + +rt_err_t rt_wlan_start_ap(const char *ssid, const char *password) +{ + rt_err_t err = RT_EOK; + int ssid_len = 0; + struct rt_wlan_info info; + struct rt_wlan_complete_des *complete; + rt_uint32_t set = 0, recved = 0; + + if (_ap_is_null()) + { + return -RT_EIO; + } + if (ssid == RT_NULL) return -RT_EINVAL; + + rt_memset(&info, 0, sizeof(struct rt_wlan_info)); + RT_WLAN_LOG_D("%s is run ssid:%s password:%s", __FUNCTION__, ssid, password); + if (password) + { + info.security = SECURITY_WPA2_AES_PSK; + } + ssid_len = rt_strlen(ssid); + if (ssid_len > RT_WLAN_SSID_MAX_LENGTH) + { + RT_WLAN_LOG_E("ssid is to long! len:%d", ssid_len); + } + + /* copy info */ + rt_memcpy(&info.ssid.val, ssid, ssid_len); + info.ssid.len = ssid_len; + info.channel = 6; - rt_wlan_register_event_handler(device, WIFI_EVT_INIT_DONE, wlan_mgnt_init_done_event); - rt_wlan_register_event_handler(device, WIFI_EVT_LINK_DOWN, wlan_mgnt_link_up_event); - rt_wlan_register_event_handler(device, WIFI_EVT_LINK_UP, wlan_mgnt_link_down_event); - rt_wlan_register_event_handler(device, WIFI_EVT_CONNECT, wlan_mgnt_sta_connect_event); - rt_wlan_register_event_handler(device, WIFI_EVT_DISCONNECT, wlan_mgnt_sta_disconnect_event); - rt_wlan_register_event_handler(device, WIFI_EVT_AP_START, wlan_mgnt_ap_start_event); - rt_wlan_register_event_handler(device, WIFI_EVT_AP_STOP, wlan_mgnt_ap_stop_event); - rt_wlan_register_event_handler(device, WIFI_EVENT_STA_ASSOC, wlan_mgnt_ap_associate_event); - rt_wlan_register_event_handler(device, WIFI_EVENT_STA_DISASSOC, wlan_mgnt_ap_disassociate_event); - rt_wlan_register_event_handler(device, WIFI_EVT_SCAN_DONE, wlan_mgnt_scan_done_event); + /* Initializing events that need to wait */ + MGNT_LOCK(); + /* create event wait complete */ + complete = rt_wlan_complete_create("start_ap"); + if (complete == RT_NULL) + { + MGNT_UNLOCK(); + return -RT_ENOMEM; + } + /* start ap */ + err = rt_wlan_start_ap_adv(&info, password); + if (err != RT_EOK) + { + rt_wlan_complete_delete(complete); + RT_WLAN_LOG_I("start ap failed!"); + MGNT_UNLOCK(); + return err; + } + + /* Initializing events that need to wait */ + set |= 0x1 << RT_WLAN_DEV_EVT_AP_START; + set |= 0x1 << RT_WLAN_DEV_EVT_AP_STOP; + /* Check whether there is a waiting event */ + rt_wlan_complete_wait(complete, set, RT_WLAN_START_AP_WAIT_MS, &recved); + rt_wlan_complete_delete(complete); + /* check event */ + set = 0x1 << RT_WLAN_DEV_EVT_AP_START; + if (!(recved & set)) + { + RT_WLAN_LOG_I("start ap failed!"); + MGNT_UNLOCK(); + return -RT_ERROR; + } + RT_WLAN_LOG_I("start ap successs!"); + MGNT_UNLOCK(); + return err; +} + +rt_err_t rt_wlan_start_ap_adv(struct rt_wlan_info *info, const char *password) +{ + rt_err_t err = RT_EOK; + int password_len = 0; + + if (_ap_is_null()) + { + return -RT_EIO; + } + RT_WLAN_LOG_D("%s is run", __FUNCTION__); + password_len = rt_strlen(password); + if (password_len > RT_WLAN_PASSWORD_MAX_LENGTH) + { + RT_WLAN_LOG_E("key is to long! len:%d", password_len); + return -RT_EINVAL; + } + /* is start up ? */ + MGNT_LOCK(); + if (rt_wlan_ap_is_active()) + { + if ((_ap_mgnt.info.ssid.len == info->ssid.len) && + (_ap_mgnt.info.security == info->security) && + (_ap_mgnt.info.channel == info->channel) && + (_ap_mgnt.info.hidden == info->hidden) && + (_ap_mgnt.key.len == password_len) && + (rt_memcmp(&_ap_mgnt.info.ssid.val[0], &info->ssid.val[0], info->ssid.len) == 0) && + (rt_memcmp(&_ap_mgnt.key.val[0], password, password_len))) + { + RT_WLAN_LOG_D("wifi Already Start"); + MGNT_UNLOCK(); + return RT_EOK; + } + } + + err = rt_wlan_dev_ap_start(AP_DEVICE(), info, password, password_len); + if (err != RT_EOK) + { + MGNT_UNLOCK(); + return err; + } + rt_memcpy(&_ap_mgnt.info, info, sizeof(struct rt_wlan_info)); + rt_memcpy(&_ap_mgnt.key.val, password, password_len); + _ap_mgnt.key.len = password_len; + + MGNT_UNLOCK(); + return err; +} + +int rt_wlan_ap_is_active(void) +{ + int _active = 0; + + if (_ap_is_null()) + { + return 0; + } + + _active = _ap_mgnt.state & RT_WLAN_STATE_ACTIVE ? 1 : 0; + RT_WLAN_LOG_D("%s is run active:%s", __FUNCTION__, _active ? "Active" : "Inactive"); + return _active; +} + +rt_err_t rt_wlan_ap_stop(void) +{ + rt_err_t err = RT_EOK; + struct rt_wlan_complete_des *complete; + rt_uint32_t set = 0, recved = 0; + + if (_ap_is_null()) + { + return -RT_EIO; + } + RT_WLAN_LOG_D("%s is run", __FUNCTION__); + + MGNT_LOCK(); + /* create event wait complete */ + complete = rt_wlan_complete_create("stop_ap"); + if (complete == RT_NULL) + { + MGNT_UNLOCK(); + return -RT_ENOMEM; + } + err = rt_wlan_dev_ap_stop(AP_DEVICE()); + if (err != RT_EOK) + { + RT_WLAN_LOG_E("ap stop fail"); + rt_wlan_complete_delete(complete); + MGNT_UNLOCK(); + return err; + } + /* Initializing events that need to wait */ + set |= 0x1 << RT_WLAN_DEV_EVT_AP_STOP; + /* Check whether there is a waiting event */ + rt_wlan_complete_wait(complete, set, RT_WLAN_START_AP_WAIT_MS, &recved); + rt_wlan_complete_delete(complete); + /* check event */ + set = 0x1 << RT_WLAN_DEV_EVT_AP_STOP; + if (!(recved & set)) + { + RT_WLAN_LOG_I("ap stop failed!"); + MGNT_UNLOCK(); + return -RT_ERROR; + } + RT_WLAN_LOG_I("ap stop success!"); + MGNT_UNLOCK(); + return err; +} + +rt_err_t rt_wlan_ap_get_info(struct rt_wlan_info *info) +{ + if (_ap_is_null()) + { + return -RT_EIO; + } + RT_WLAN_LOG_D("%s is run", __FUNCTION__); + + *info = _ap_mgnt.info; return RT_EOK; } + +/* get sta number */ +int rt_wlan_ap_get_sta_num(void) +{ + int sta_num = 0; + + STAINFO_LOCK(); + sta_num = sta_info.num; + STAINFO_UNLOCK(); + RT_WLAN_LOG_D("%s is run num:%d", __FUNCTION__, sta_num); + return sta_num; +} + +/* get sta info */ +int rt_wlan_ap_get_sta_info(struct rt_wlan_info *info, int num) +{ + int sta_num = 0, i = 0; + struct rt_wlan_sta_list *sta_list; + + STAINFO_LOCK(); + /* sta_num = min(sta_info.num, num) */ + sta_num = sta_info.num > num ? num : sta_info.num; + for (sta_list = sta_info.node; sta_list != RT_NULL && i < sta_num; sta_list = sta_list->next) + { + info[i] = sta_list->info; + i ++; + } + STAINFO_UNLOCK(); + RT_WLAN_LOG_D("%s is run num:%d", __FUNCTION__, i); + return i; +} + +/* deauth sta */ +rt_err_t rt_wlan_ap_deauth_sta(rt_uint8_t *mac) +{ + rt_err_t err = RT_EOK; + struct rt_wlan_sta_list *sta_list; + rt_bool_t find_flag = RT_FALSE; + + if (_ap_is_null()) + { + return -RT_EIO; + } + RT_WLAN_LOG_D("%s is run mac: %02x:%02x:%02x:%02x:%02x:%02x:%d", + __FUNCTION__, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + + if (mac == RT_NULL) + { + RT_WLAN_LOG_E("mac addr is null"); + return -RT_EINVAL; + } + + MGNT_LOCK(); + if (sta_info.node == RT_NULL || sta_info.num == 0) + { + RT_WLAN_LOG_E("No AP"); + MGNT_UNLOCK(); + return -RT_ERROR; + } + + STAINFO_LOCK(); + /* Search for MAC address from sta list */ + for (sta_list = sta_info.node; sta_list != RT_NULL; sta_list = sta_list->next) + { + if (rt_memcmp(&sta_list->info.bssid[0], &mac[0], RT_WLAN_BSSID_MAX_LENGTH) == 0) + { + find_flag = RT_TRUE; + break; + } + } + STAINFO_UNLOCK(); + + /* No MAC address was found. return */ + if (find_flag != RT_TRUE) + { + RT_WLAN_LOG_E("Not find mac addr"); + MGNT_UNLOCK(); + return -RT_ERROR; + } + + /* Kill STA */ + err = rt_wlan_dev_ap_deauth(AP_DEVICE(), mac); + if (err != RT_NULL) + { + RT_WLAN_LOG_E("deauth sta failed"); + MGNT_UNLOCK(); + return err; + } + + MGNT_UNLOCK(); + return err; +} + +rt_err_t rt_wlan_ap_set_country(rt_country_code_t country_code) +{ + rt_err_t err = RT_EOK; + + if (_ap_is_null()) + { + return -RT_EIO; + } + RT_WLAN_LOG_D("%s is run country:%d", __FUNCTION__, country_code); + MGNT_LOCK(); + err = rt_wlan_dev_set_country(AP_DEVICE(), country_code); + MGNT_UNLOCK(); + return err; +} + +rt_country_code_t rt_wlan_ap_get_country(void) +{ + rt_country_code_t country_code = RT_COUNTRY_UNKNOWN; + + if (_ap_is_null()) + { + return country_code; + } + MGNT_LOCK(); + country_code = rt_wlan_dev_get_country(AP_DEVICE()); + RT_WLAN_LOG_D("%s is run country:%d", __FUNCTION__, country_code); + MGNT_UNLOCK(); + return country_code; +} + +void rt_wlan_config_autoreconnect(rt_bool_t enable) +{ + RT_WLAN_LOG_D("%s is run enable:%d", __FUNCTION__, enable); + + MGNT_LOCK(); + if (enable) + { + _sta_mgnt.flags |= RT_WLAN_STATE_AUTOEN; + } + else + { + _sta_mgnt.flags &= ~RT_WLAN_STATE_AUTOEN; + } + MGNT_UNLOCK(); +} + +rt_bool_t rt_wlan_get_autoreconnect_mode(void) +{ + rt_bool_t enable = 0; + + enable = _sta_mgnt.flags & RT_WLAN_STATE_AUTOEN ? 1 : 0; + RT_WLAN_LOG_D("%s is run enable:%d", __FUNCTION__, enable); + return enable; +} + +/* Call the underlying scan function, which is asynchronous. +The hotspots scanned are returned by callbacks */ +rt_err_t rt_wlan_scan(void) +{ + rt_err_t err = RT_EOK; + + if (_sta_is_null()) + { + return -RT_EIO; + } + RT_WLAN_LOG_D("%s is run", __FUNCTION__); + + MGNT_LOCK(); + err = rt_wlan_dev_scan(STA_DEVICE(), RT_NULL); + MGNT_UNLOCK(); + return err; +} + +struct rt_wlan_scan_result *rt_wlan_scan_sync(void) +{ + struct rt_wlan_scan_result *result; + + /* Execute synchronous scan function */ + MGNT_LOCK(); + result = rt_wlan_scan_with_info(RT_NULL); + MGNT_UNLOCK(); + return result; +} + +struct rt_wlan_scan_result *rt_wlan_scan_with_info(struct rt_wlan_info *info) +{ + rt_err_t err = RT_EOK; + struct rt_wlan_complete_des *complete; + rt_uint32_t set = 0, recved = 0; + + if (_sta_is_null()) + { + return RT_NULL; + } + RT_WLAN_LOG_D("%s is run", __FUNCTION__); + if (info != RT_NULL && info->ssid.len > RT_WLAN_SSID_MAX_LENGTH) + { + RT_WLAN_LOG_E("ssid is to long!"); + return RT_NULL; + } + + /* Create an event that needs to wait. */ + MGNT_LOCK(); + complete = rt_wlan_complete_create("scan"); + if (complete == RT_NULL) + { + MGNT_UNLOCK(); + return &scan_result; + } + + /* run scan */ + err = rt_wlan_dev_scan(STA_DEVICE(), info); + if (err != RT_EOK) + { + rt_wlan_complete_delete(complete); + RT_WLAN_LOG_E("scan sync fail"); + MGNT_UNLOCK(); + return RT_NULL; + } + + /* Initializing events that need to wait */ + set |= 0x1 << RT_WLAN_DEV_EVT_SCAN_DONE; + /* Check whether there is a waiting event */ + rt_wlan_complete_wait(complete, set, RT_WLAN_CONNECT_WAIT_MS, &recved); + rt_wlan_complete_delete(complete); + /* check event */ + set = 0x1 << RT_WLAN_DEV_EVT_SCAN_DONE; + if (!(recved & set)) + { + RT_WLAN_LOG_E("scan wait timeout!"); + MGNT_UNLOCK(); + return &scan_result; + } + + MGNT_UNLOCK(); + return &scan_result; +} + +int rt_wlan_scan_get_info_num(void) +{ + int num = 0; + + num = scan_result.num; + RT_WLAN_LOG_D("%s is run num:%d", __FUNCTION__, num); + return num; +} + +int rt_wlan_scan_get_info(struct rt_wlan_info *info, int num) +{ + int _num = 0; + + SRESULT_LOCK(); + if (scan_result.num && num > 0) + { + _num = scan_result.num > num ? num : scan_result.num; + rt_memcpy(info, scan_result.info, _num * sizeof(struct rt_wlan_info)); + } + SRESULT_UNLOCK(); + return _num; +} + +struct rt_wlan_scan_result *rt_wlan_scan_get_result(void) +{ + return &scan_result; +} + +void rt_wlan_scan_result_clean(void) +{ + MGNT_LOCK(); + SRESULT_LOCK(); + + /* If there is data */ + if (scan_result.num) + { + scan_result.num = 0; + rt_free(scan_result.info); + scan_result.info = RT_NULL; + } + SRESULT_UNLOCK(); + MGNT_UNLOCK(); +} + +int rt_wlan_scan_find_cache(struct rt_wlan_info *info, struct rt_wlan_info *out_info, int num) +{ + int i = 0, count = 0; + struct rt_wlan_info *scan_info; + rt_bool_t is_equ = 1; + rt_uint8_t bssid_zero[RT_WLAN_BSSID_MAX_LENGTH] = { 0 }; + + if ((out_info == RT_NULL) || (info == RT_NULL) || (num <= 0)) + { + return 0; + } + SRESULT_LOCK(); + /* Traversing the cache to find a qualified hot spot information */ + for (i = 0; (i < scan_result.num) && (count < num); i++) + { + scan_info = &scan_result.info[i]; + + if (is_equ && (info->security != SECURITY_UNKNOWN)) + { + is_equ &= info->security == scan_info->security; + } + if (is_equ && ((info->ssid.len > 0) && (info->ssid.len == scan_info->ssid.len))) + { + is_equ &= rt_memcmp(&info->ssid.val[0], &scan_info->ssid.val[0], scan_info->ssid.len) == 0; + } + if (is_equ && (rt_memcmp(&info->bssid[0], bssid_zero, RT_WLAN_BSSID_MAX_LENGTH))) + { + is_equ &= rt_memcmp(&info->bssid[0], &scan_info->bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0; + } + if (is_equ && info->datarate) + { + is_equ &= info->datarate == scan_info->datarate; + } + if (is_equ && (info->channel >= 0)) + { + is_equ &= info->channel == scan_info->channel; + } + if (is_equ && (info->rssi < 0)) + { + is_equ &= info->rssi == scan_info->rssi; + } + /* Determine whether to find */ + if (is_equ) + { + rt_memcpy(&out_info[count], scan_info, sizeof(struct rt_wlan_info)); + count ++; + } + } + SRESULT_UNLOCK(); + + return count; +} + +rt_err_t rt_wlan_set_powersave(int level) +{ + rt_err_t err = RT_EOK; + + if (_sta_is_null()) + { + return -RT_EIO; + } + RT_WLAN_LOG_D("%s is run", __FUNCTION__); + MGNT_LOCK(); + err = rt_wlan_dev_set_powersave(STA_DEVICE(), level); + MGNT_UNLOCK(); + return err; +} + +int rt_wlan_get_powersave(void) +{ + int level; + + if (_sta_is_null()) + { + return -1; + } + RT_WLAN_LOG_D("%s is run", __FUNCTION__); + MGNT_LOCK(); + level = rt_wlan_dev_get_powersave(STA_DEVICE()); + MGNT_UNLOCK(); + return level; +} + +rt_err_t rt_wlan_register_event_handler(rt_wlan_event_t event, rt_wlan_event_handler handler, void *parameter) +{ + rt_base_t level; + + if (event >= RT_WLAN_EVT_MAX) + { + return RT_EINVAL; + } + RT_WLAN_LOG_D("%s is run event:%d", __FUNCTION__, event); + + MGNT_UNLOCK(); + /* Registering Callbacks */ + level = rt_hw_interrupt_disable(); + event_tab[event].handler = handler; + event_tab[event].parameter = parameter; + rt_hw_interrupt_enable(level); + MGNT_UNLOCK(); + return RT_EOK; +} + +rt_err_t rt_wlan_unregister_event_handler(rt_wlan_event_t event) +{ + rt_base_t level; + + if (event >= RT_WLAN_EVT_MAX) + { + return RT_EINVAL; + } + RT_WLAN_LOG_D("%s is run event:%d", __FUNCTION__, event); + MGNT_LOCK(); + /* unregister*/ + level = rt_hw_interrupt_disable(); + event_tab[event].handler = RT_NULL; + event_tab[event].parameter = RT_NULL; + rt_hw_interrupt_enable(level); + MGNT_UNLOCK(); + return RT_EOK; +} + +void rt_wlan_mgnt_lock(void) +{ + MGNT_LOCK(); +} + +void rt_wlan_mgnt_unlock(void) +{ + MGNT_UNLOCK(); +} + +int rt_wlan_prot_ready_event(struct rt_wlan_device *wlan, struct rt_wlan_buff *buff) +{ + rt_base_t level; + void *user_parameter; + rt_wlan_event_handler handler = RT_NULL; + + if ((wlan == RT_NULL) || (_sta_mgnt.device != wlan) || + (!(_sta_mgnt.state & RT_WLAN_STATE_CONNECT))) + { + return -1; + } + if (_sta_mgnt.state & RT_WLAN_STATE_READY) + { + return 0; + } + level = rt_hw_interrupt_disable(); + _sta_mgnt.state |= RT_WLAN_STATE_READY; + handler = event_tab[RT_WLAN_EVT_READY].handler; + user_parameter = event_tab[RT_WLAN_EVT_READY].parameter; + rt_hw_interrupt_enable(level); + if (handler) + { + handler(RT_WLAN_EVT_READY, buff, user_parameter); + } + return 0; +} + +int rt_wlan_init(void) +{ + static rt_int8_t _init_flag = 0; + + /* Execute only once */ + if (_init_flag == 0) + { + rt_memset(&_sta_mgnt, 0, sizeof(struct rt_wlan_mgnt_des)); + rt_memset(&_ap_mgnt, 0, sizeof(struct rt_wlan_mgnt_des)); + rt_memset(&scan_result, 0, sizeof(struct rt_wlan_scan_result)); + rt_memset(&sta_info, 0, sizeof(struct rt_wlan_sta_des)); + rt_mutex_init(&mgnt_mutex, "mgnt", RT_IPC_FLAG_FIFO); + rt_mutex_init(&scan_result_mutex, "scan", RT_IPC_FLAG_FIFO); + rt_mutex_init(&sta_info_mutex, "sta", RT_IPC_FLAG_FIFO); + rt_mutex_init(&complete_mutex, "complete", RT_IPC_FLAG_FIFO); + rt_timer_init(&reconnect_time, "wifi_tim", rt_wlan_cyclic_check, RT_NULL, DISCONNECT_RESPONSE_TICK, RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER); + rt_timer_start(&reconnect_time); + _init_flag = 1; + } + return 0; +} +INIT_PREV_EXPORT(rt_wlan_init); diff --git a/components/drivers/wlan/wlan_mgnt.h b/components/drivers/wlan/wlan_mgnt.h index b00414c6e2..d11d9ee347 100644 --- a/components/drivers/wlan/wlan_mgnt.h +++ b/components/drivers/wlan/wlan_mgnt.h @@ -1,34 +1,150 @@ /* - * RT-Thread Wi-Fi Device + * Copyright (c) 2006-2018, RT-Thread Development Team * - * COPYRIGHT (C) 2014 - 2018, Shanghai Real-Thread Technology Co., Ltd - * - * This file is part of RT-Thread (http://www.rt-thread.org) - * - * All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes - * 2018-02-27 EvalZero the first verion + * 2018-08-06 tyx the first version */ #ifndef __WLAN_MGNT_H__ #define __WLAN_MGNT_H__ -int rt_wlan_mgnt_attach(struct rt_wlan_device *device, void *user_data); +#include <wlan_dev.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef RT_WLAN_SCAN_WAIT_MS +#define RT_WLAN_SCAN_WAIT_MS (10 * 1000) +#endif + +#ifndef RT_WLAN_SCAN_CACHE_NUM +#define RT_WLAN_SCAN_CACHE_NUM (50) +#endif + +#ifndef RT_WLAN_CONNECT_WAIT_MS +#define RT_WLAN_CONNECT_WAIT_MS (10 * 1000) +#endif + +#ifndef RT_WLAN_START_AP_WAIT_MS +#define RT_WLAN_START_AP_WAIT_MS (10 * 1000) +#endif + +#ifndef RT_WLAN_EBOX_NUM +#define RT_WLAN_EBOX_NUM (10) +#endif + +/*state fot station*/ +#define RT_WLAN_STATE_CONNECT (1UL << 0) +#define RT_WLAN_STATE_CONNECTING (1UL << 1) +#define RT_WLAN_STATE_READY (1UL << 2) +#define RT_WLAN_STATE_POWERSAVE (1UL << 3) + +/*flags fot station*/ +#define RT_WLAN_STATE_AUTOEN (1UL << 0) + +/*state fot ap*/ +#define RT_WLAN_STATE_ACTIVE (1UL << 0) + +typedef enum +{ + RT_WLAN_EVT_READY = 0, /* connect and prot is ok, You can send data*/ + RT_WLAN_EVT_SCAN_DONE, /* Scan a info */ + RT_WLAN_EVT_SCAN_REPORT, /* Scan end */ + RT_WLAN_EVT_STA_CONNECTED, /* connect success */ + RT_WLAN_EVT_STA_CONNECTED_FAIL, /* connection failed */ + RT_WLAN_EVT_STA_DISCONNECTED, /* disconnect */ + RT_WLAN_EVT_AP_START, /* AP start */ + RT_WLAN_EVT_AP_STOP, /* AP stop */ + RT_WLAN_EVT_AP_ASSOCIATED, /* sta associated */ + RT_WLAN_EVT_AP_DISASSOCIATED, /* sta disassociated */ + RT_WLAN_EVT_MAX +} rt_wlan_event_t; + +typedef void (*rt_wlan_event_handler)(int event, struct rt_wlan_buff *buff, void *parameter); + +struct rt_wlan_scan_result +{ + rt_int32_t num; + struct rt_wlan_info *info; +}; + +/* + * wifi init interface + */ +int rt_wlan_init(void); +rt_err_t rt_wlan_set_mode(const char *dev_name, rt_wlan_mode_t mode); +rt_wlan_mode_t rt_wlan_get_mode(const char *dev_name); + +/* + * wifi station mode interface + */ +rt_err_t rt_wlan_connect(const char *ssid, const char *password); +rt_err_t rt_wlan_connect_adv(struct rt_wlan_info *info, const char *password); +rt_err_t rt_wlan_disconnect(void); +rt_bool_t rt_wlan_is_connected(void); +rt_bool_t rt_wlan_is_ready(void); +rt_err_t rt_wlan_set_mac(rt_uint8_t *mac); +rt_err_t rt_wlan_get_mac(rt_uint8_t *mac); +rt_err_t rt_wlan_get_info(struct rt_wlan_info *info); +int rt_wlan_get_rssi(void); + +/* + * wifi ap mode interface + */ +rt_err_t rt_wlan_start_ap(const char *ssid, const char *password); +rt_err_t rt_wlan_start_ap_adv(struct rt_wlan_info *info, const char *password); +int rt_wlan_ap_is_active(void); +rt_err_t rt_wlan_ap_stop(void); +rt_err_t rt_wlan_ap_get_info(struct rt_wlan_info *info); +int rt_wlan_ap_get_sta_num(void); +int rt_wlan_ap_get_sta_info(struct rt_wlan_info *info, int num); +rt_err_t rt_wlan_ap_deauth_sta(rt_uint8_t *mac); +rt_err_t rt_wlan_ap_set_country(rt_country_code_t country_code); +rt_country_code_t rt_wlan_ap_get_country(void); + +/* + * wifi scan interface + */ +rt_err_t rt_wlan_scan(void); +struct rt_wlan_scan_result *rt_wlan_scan_sync(void); +struct rt_wlan_scan_result *rt_wlan_scan_with_info(struct rt_wlan_info *info); +int rt_wlan_scan_get_info_num(void); +int rt_wlan_scan_get_info(struct rt_wlan_info *info, int num); +struct rt_wlan_scan_result *rt_wlan_scan_get_result(void); +void rt_wlan_scan_result_clean(void); +int rt_wlan_scan_find_cache(struct rt_wlan_info *info, struct rt_wlan_info *out_info, int num); +rt_bool_t rt_wlan_find_best_by_cache(const char *ssid, struct rt_wlan_info *info); + +/* + * wifi auto connect interface + */ +void rt_wlan_config_autoreconnect(rt_bool_t enable); +rt_bool_t rt_wlan_get_autoreconnect_mode(void); + +/* + * wifi power management interface + */ +rt_err_t rt_wlan_set_powersave(int level); +int rt_wlan_get_powersave(void); + +/* + * wifi event management interface + */ +rt_err_t rt_wlan_register_event_handler(rt_wlan_event_t event, rt_wlan_event_handler handler, void *parameter); +rt_err_t rt_wlan_unregister_event_handler(rt_wlan_event_t event); + +/* + * wifi management lock interface + */ +void rt_wlan_mgnt_lock(void); +void rt_wlan_mgnt_unlock(void); + +#ifdef __cplusplus +} +#endif #endif diff --git a/components/drivers/wlan/wlan_prot.c b/components/drivers/wlan/wlan_prot.c new file mode 100644 index 0000000000..ec5e419bc4 --- /dev/null +++ b/components/drivers/wlan/wlan_prot.c @@ -0,0 +1,364 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-08-14 tyx the first version + */ + +#include <rthw.h> +#include <rtthread.h> +#include <wlan_dev.h> +#include <wlan_prot.h> + +#define DBG_ENABLE +#ifdef RT_WLAN_PROT_DEBUG +#define DBG_LEVEL DBG_LOG +#else +#define DBG_LEVEL DBG_INFO +#endif +#define DBG_SECTION_NAME "WLAN.prot" +#define DBG_COLOR +#include <rtdbg.h> + +#if RT_WLAN_PROT_NAME_LEN < 4 +#error "The name is too short" +#endif + +struct rt_wlan_prot_event_des +{ + rt_wlan_prot_event_handler handler; + struct rt_wlan_prot *prot; +}; + +static struct rt_wlan_prot *_prot[RT_WLAN_PROT_MAX]; + +static struct rt_wlan_prot_event_des prot_event_tab[RT_WLAN_PROT_EVT_MAX][RT_WLAN_PROT_MAX]; + +static void rt_wlan_prot_event_handle(struct rt_wlan_device *wlan, rt_wlan_dev_event_t event, struct rt_wlan_buff *buff, void *parameter) +{ + int i; + struct rt_wlan_prot *wlan_prot; + struct rt_wlan_prot *prot; + rt_wlan_prot_event_handler handler; + rt_wlan_prot_event_t prot_event; + + LOG_D("F:%s L:%d event:%d", __FUNCTION__, __LINE__, event); + + wlan_prot = wlan->prot; + handler = RT_NULL; + prot = RT_NULL; + switch (event) + { + case RT_WLAN_DEV_EVT_INIT_DONE: + { + LOG_D("L%d event: INIT_DONE", __LINE__); + prot_event = RT_WLAN_PROT_EVT_INIT_DONE; + break; + } + case RT_WLAN_DEV_EVT_CONNECT: + { + LOG_D("L%d event: CONNECT", __LINE__); + prot_event = RT_WLAN_PROT_EVT_CONNECT; + break; + } + case RT_WLAN_DEV_EVT_DISCONNECT: + { + LOG_D("L%d event: DISCONNECT", __LINE__); + prot_event = RT_WLAN_PROT_EVT_DISCONNECT; + break; + } + case RT_WLAN_DEV_EVT_AP_START: + { + LOG_D("L%d event: AP_START", __LINE__); + prot_event = RT_WLAN_PROT_EVT_AP_START; + break; + } + case RT_WLAN_DEV_EVT_AP_STOP: + { + LOG_D("L%d event: AP_STOP", __LINE__); + prot_event = RT_WLAN_PROT_EVT_AP_STOP; + break; + } + case RT_WLAN_DEV_EVT_AP_ASSOCIATED: + { + LOG_D("L%d event: AP_ASSOCIATED", __LINE__); + prot_event = RT_WLAN_PROT_EVT_AP_ASSOCIATED; + break; + } + case RT_WLAN_DEV_EVT_AP_DISASSOCIATED: + { + LOG_D("L%d event: AP_DISASSOCIATED", __LINE__); + prot_event = RT_WLAN_PROT_EVT_AP_DISASSOCIATED; + break; + } + default: + { + return; + } + } + for (i = 0; i < RT_WLAN_PROT_MAX; i++) + { + if ((prot_event_tab[prot_event][i].handler != RT_NULL) && + (prot_event_tab[prot_event][i].prot->id == wlan_prot->id)) + { + handler = prot_event_tab[prot_event][i].handler; + prot = prot_event_tab[prot_event][i].prot; + break; + } + } + + if (handler != RT_NULL) + { + handler(prot, wlan, prot_event); + } +} + +static struct rt_wlan_device *rt_wlan_prot_find_by_name(const char *name) +{ + rt_device_t device; + + if (name == RT_NULL) + { + LOG_E("F:%s L:%d Parameter Wrongful", __FUNCTION__, __LINE__); + return RT_NULL; + } + device = rt_device_find(name); + if (device == RT_NULL) + { + LOG_E("F:%s L:%d not find wlan dev!! name:%s", __FUNCTION__, __LINE__, name); + return RT_NULL; + } + return (struct rt_wlan_device *)device; +} + +rt_err_t rt_wlan_prot_attach(const char *dev_name, const char *prot_name) +{ + struct rt_wlan_device *wlan; + + wlan = rt_wlan_prot_find_by_name(dev_name); + if (wlan == RT_NULL) + { + return -RT_ERROR; + } + return rt_wlan_prot_attach_dev(wlan, prot_name); +} + +rt_err_t rt_wlan_prot_detach(const char *name) +{ + struct rt_wlan_device *wlan; + + wlan = rt_wlan_prot_find_by_name(name); + if (wlan == RT_NULL) + { + return -RT_ERROR; + } + return rt_wlan_prot_detach_dev(wlan); +} + +rt_err_t rt_wlan_prot_attach_dev(struct rt_wlan_device *wlan, const char *prot_name) +{ + int i = 0; + struct rt_wlan_prot *prot = wlan->prot; + rt_wlan_dev_event_t event; + + if (wlan == RT_NULL) + { + LOG_E("F:%s L:%d wlan is null", __FUNCTION__, __LINE__); + return -RT_ERROR; + } + + if (prot != RT_NULL && + (rt_strcmp(prot->name, prot_name) == 0)) + { + LOG_D("prot is register"); + return RT_EOK; + } + + /* if prot not NULL */ + if (prot != RT_NULL) + rt_wlan_prot_detach_dev(wlan); + +#ifdef RT_WLAN_PROT_LWIP_PBUF_FORCE + if (rt_strcmp(RT_WLAN_PROT_LWIP, prot_name) != 0) + { + return -RT_ERROR; + } +#endif + /* find prot */ + for (i = 0; i < RT_WLAN_PROT_MAX; i++) + { + if ((_prot[i] != RT_NULL) && (rt_strcmp(_prot[i]->name, prot_name) == 0)) + { + /* attach prot */ + wlan->prot = _prot[i]->ops->dev_reg_callback(_prot[i], wlan); + break; + } + } + + if (i >= RT_WLAN_PROT_MAX) + { + LOG_E("F:%s L:%d not find wlan protocol", __FUNCTION__, __LINE__); + return -RT_ERROR; + } + + for (event = RT_WLAN_DEV_EVT_INIT_DONE; event < RT_WLAN_DEV_EVT_MAX; event ++) + { + if (rt_wlan_dev_register_event_handler(wlan, event, rt_wlan_prot_event_handle, RT_NULL) != RT_EOK) + { + LOG_E("prot register event filed:%d", event); + } + } + + return RT_EOK; +} + +rt_err_t rt_wlan_prot_detach_dev(struct rt_wlan_device *wlan) +{ + struct rt_wlan_prot *prot = wlan->prot; + rt_wlan_dev_event_t event; + + if (prot == RT_NULL) + return RT_EOK; + + for (event = RT_WLAN_DEV_EVT_INIT_DONE; event < RT_WLAN_DEV_EVT_MAX; event ++) + { + rt_wlan_dev_unregister_event_handler(wlan, event, rt_wlan_prot_event_handle); + } + + /* detach prot */ + prot->ops->dev_unreg_callback(prot, wlan); + wlan->prot = RT_NULL; + + return RT_EOK; +} + +rt_err_t rt_wlan_prot_regisetr(struct rt_wlan_prot *prot) +{ + int i; + rt_uint32_t id; + static rt_uint8_t num; + + /* Parameter checking */ + if ((prot == RT_NULL) || + (prot->ops->prot_recv == RT_NULL) || + (prot->ops->dev_reg_callback == RT_NULL)) + { + LOG_E("F:%s L:%d Parameter Wrongful", __FUNCTION__, __LINE__); + return -RT_EINVAL; + } + + /* save prot */ + for (i = 0; i < RT_WLAN_PROT_MAX; i++) + { + if (_prot[i] == RT_NULL) + { + id = (RT_LWAN_ID_PREFIX << 16) | num; + prot->id = id; + _prot[i] = prot; + num ++; + break; + } + else if (rt_strcmp(_prot[i]->name, prot->name) == 0) + { + break; + } + } + + /* is full */ + if (i >= RT_WLAN_PROT_MAX) + { + LOG_E("F:%s L:%d Space full", __FUNCTION__, __LINE__); + return -RT_ERROR; + } + + return RT_EOK; +} + +rt_err_t rt_wlan_prot_event_register(struct rt_wlan_prot *prot, rt_wlan_prot_event_t event, rt_wlan_prot_event_handler handler) +{ + int i; + + if ((prot == RT_NULL) || (handler == RT_NULL)) + { + return -RT_EINVAL; + } + + for (i = 0; i < RT_WLAN_PROT_MAX; i++) + { + if (prot_event_tab[event][i].handler == RT_NULL) + { + prot_event_tab[event][i].handler = handler; + prot_event_tab[event][i].prot = prot; + return RT_EOK; + } + } + + return -RT_ERROR; +} + +rt_err_t rt_wlan_prot_event_unregister(struct rt_wlan_prot *prot, rt_wlan_prot_event_t event) +{ + int i; + + if (prot == RT_NULL) + { + return -RT_EINVAL; + } + + for (i = 0; i < RT_WLAN_PROT_MAX; i++) + { + if ((prot_event_tab[event][i].handler != RT_NULL) && + (prot_event_tab[event][i].prot == prot)) + { + rt_memset(&prot_event_tab[event][i], 0, sizeof(struct rt_wlan_prot_event_des)); + return RT_EOK; + } + } + + return -RT_ERROR; +} + +rt_err_t rt_wlan_prot_transfer_dev(struct rt_wlan_device *wlan, void *buff, int len) +{ + if (wlan->ops->wlan_send != RT_NULL) + { + return wlan->ops->wlan_send(wlan, buff, len); + } + return -RT_ERROR; +} + +rt_err_t rt_wlan_dev_transfer_prot(struct rt_wlan_device *wlan, void *buff, int len) +{ + struct rt_wlan_prot *prot = wlan->prot; + + if (prot != RT_NULL) + { + return prot->ops->prot_recv(wlan, buff, len); + } + return -RT_ERROR; +} + +extern int rt_wlan_prot_ready_event(struct rt_wlan_device *wlan, struct rt_wlan_buff *buff); +int rt_wlan_prot_ready(struct rt_wlan_device *wlan, struct rt_wlan_buff *buff) +{ + return rt_wlan_prot_ready_event(wlan, buff); +} + +void rt_wlan_prot_dump(void) +{ + int i; + + rt_kprintf(" name id \n"); + rt_kprintf("-------- --------\n"); + for (i = 0; i < RT_WLAN_PROT_MAX; i++) + { + if (_prot[i] != RT_NULL) + { + rt_kprintf("%-8.8s ", _prot[i]->name); + rt_kprintf("%08x\n", _prot[i]->id); + } + } +} diff --git a/components/drivers/wlan/wlan_prot.h b/components/drivers/wlan/wlan_prot.h new file mode 100644 index 0000000000..34c5bae7fd --- /dev/null +++ b/components/drivers/wlan/wlan_prot.h @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-08-14 tyx the first version + */ + +#ifndef __WLAN_PROT_H__ +#define __WLAN_PROT_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef RT_WLAN_PROT_NAME_LEN +#define RT_WLAN_PROT_NAME_LEN (8) +#endif + +#ifndef RT_WLAN_PROT_MAX +#define RT_WLAN_PROT_MAX (1) +#endif + +#define RT_LWAN_ID_PREFIX (0x5054) + +#define RT_WLAN_PROT_LWIP ("lwip") + +typedef enum +{ + RT_WLAN_PROT_EVT_INIT_DONE = 0, + RT_WLAN_PROT_EVT_CONNECT, + RT_WLAN_PROT_EVT_DISCONNECT, + RT_WLAN_PROT_EVT_AP_START, + RT_WLAN_PROT_EVT_AP_STOP, + RT_WLAN_PROT_EVT_AP_ASSOCIATED, + RT_WLAN_PROT_EVT_AP_DISASSOCIATED, + RT_WLAN_PROT_EVT_MAX, +} rt_wlan_prot_event_t; + +struct rt_wlan_prot; +struct rt_wlan_prot_ops +{ + rt_err_t (*prot_recv)(struct rt_wlan_device *wlan, void *buff, int len); + struct rt_wlan_prot *(*dev_reg_callback)(struct rt_wlan_prot *prot, struct rt_wlan_device *wlan); + void (*dev_unreg_callback)(struct rt_wlan_prot *prot, struct rt_wlan_device *wlan); +}; + +struct rt_wlan_prot +{ + char name[RT_WLAN_PROT_NAME_LEN]; + rt_uint32_t id; + const struct rt_wlan_prot_ops *ops; +}; + +typedef void (*rt_wlan_prot_event_handler)(struct rt_wlan_prot *port, struct rt_wlan_device *wlan, int event); + +rt_err_t rt_wlan_prot_attach(const char *dev_name, const char *prot_name); + +rt_err_t rt_wlan_prot_attach_dev(struct rt_wlan_device *wlan, const char *prot_name); + +rt_err_t rt_wlan_prot_detach(const char *dev_name); + +rt_err_t rt_wlan_prot_detach_dev(struct rt_wlan_device *wlan); + +rt_err_t rt_wlan_prot_regisetr(struct rt_wlan_prot *prot); + +rt_err_t rt_wlan_prot_transfer_dev(struct rt_wlan_device *wlan, void *buff, int len); + +rt_err_t rt_wlan_dev_transfer_prot(struct rt_wlan_device *wlan, void *buff, int len); + +rt_err_t rt_wlan_prot_event_register(struct rt_wlan_prot *prot, rt_wlan_prot_event_t event, rt_wlan_prot_event_handler handler); + +rt_err_t rt_wlan_prot_event_unregister(struct rt_wlan_prot *prot, rt_wlan_prot_event_t event); + +int rt_wlan_prot_ready(struct rt_wlan_device *wlan, struct rt_wlan_buff *buff); + +void rt_wlan_prot_dump(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/components/drivers/wlan/wlan_workqueue.c b/components/drivers/wlan/wlan_workqueue.c new file mode 100644 index 0000000000..f65ac95d08 --- /dev/null +++ b/components/drivers/wlan/wlan_workqueue.c @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-08-19 tyx the first version + */ + +#include <rthw.h> +#include <rtthread.h> +#include <wlan_workqueue.h> +#include <ipc/workqueue.h> + +#define DBG_ENABLE +#define DBG_LEVEL DBG_INFO +#define DBG_SECTION_NAME "WLAN.work" +#define DBG_COLOR +#include <rtdbg.h> + +struct rt_wlan_work +{ + struct rt_work work; + void (*fun)(void *parameter); + void *parameter; +}; + +static struct rt_workqueue *wlan_workqueue; + +static void rt_wlan_workqueue_fun(struct rt_work *work, void *work_data) +{ + struct rt_wlan_work *wlan_work = work_data; + + wlan_work->fun(wlan_work->parameter); + rt_free(wlan_work); +} + +struct rt_workqueue *rt_wlan_get_workqueue(void) +{ + return wlan_workqueue; +} + +rt_err_t rt_wlan_workqueue_dowork(void (*func)(void *parameter), void *parameter) +{ + struct rt_wlan_work *wlan_work; + rt_err_t err = RT_EOK; + + LOG_D("F:%s is run", __FUNCTION__); + if (func == RT_NULL) + { + LOG_E("F:%s L:%d func is null", __FUNCTION__, __LINE__); + return -RT_EINVAL; + } + + if (wlan_workqueue == RT_NULL) + { + LOG_E("F:%s L:%d not init wlan work queue", __FUNCTION__, __LINE__); + return -RT_ERROR; + } + + wlan_work = rt_malloc(sizeof(struct rt_wlan_work)); + if (wlan_work == RT_NULL) + { + LOG_E("F:%s L:%d create work failed", __FUNCTION__, __LINE__); + return -RT_ENOMEM; + } + wlan_work->fun = func; + wlan_work->parameter = parameter; + rt_work_init(&wlan_work->work, rt_wlan_workqueue_fun, wlan_work); + err = rt_workqueue_dowork(wlan_workqueue, &wlan_work->work); + if (err != RT_EOK) + { + LOG_E("F:%s L:%d do work failed", __FUNCTION__, __LINE__); + rt_free(wlan_work); + return err; + } + return err; +} + +int rt_wlan_workqueue_init(void) +{ + static rt_int8_t _init_flag = 0; + + if (_init_flag == 0) + { + wlan_workqueue = rt_workqueue_create(RT_WLAN_WORKQUEUE_THREAD_NAME, RT_WLAN_WORKQUEUE_THREAD_SIZE, + RT_WLAN_WORKQUEUE_THREAD_PRIO); + if (wlan_workqueue == RT_NULL) + { + LOG_E("F:%s L:%d wlan work queue create failed", __FUNCTION__, __LINE__); + return -1; + } + _init_flag = 1; + return 0; + } + return 0; +} +INIT_PREV_EXPORT(rt_wlan_workqueue_init); diff --git a/components/drivers/wlan/wlan_workqueue.h b/components/drivers/wlan/wlan_workqueue.h new file mode 100644 index 0000000000..49bc4a5f9f --- /dev/null +++ b/components/drivers/wlan/wlan_workqueue.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-08-19 tyx the first version + */ + +#ifndef __WLAN_WORKQUEUE_H__ +#define __WLAN_WORKQUEUE_H__ + +#include <ipc/workqueue.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef RT_WLAN_WORKQUEUE_THREAD_NAME +#define RT_WLAN_WORKQUEUE_THREAD_NAME ("wlan_job") +#endif + +#ifndef RT_WLAN_WORKQUEUE_THREAD_SIZE +#define RT_WLAN_WORKQUEUE_THREAD_SIZE (2048) +#endif + +#ifndef RT_WLAN_WORKQUEUE_THREAD_PRIO +#define RT_WLAN_WORKQUEUE_THREAD_PRIO (20) +#endif + +int rt_wlan_workqueue_init(void); + +rt_err_t rt_wlan_workqueue_dowork(void (*func)(void *parameter), void *parameter); + +struct rt_workqueue *rt_wlan_get_workqueue(void); + +#ifdef __cplusplus +} +#endif + +#endif |
