Contributed by Bangbang, organized by Ai-Thinker
Adding the FlashDB Database to AI-WB2, an Upgraded Version of easyflash4
FlashDB repository <https://github.com/armink/FlashDB>
Introduction[color=var(--fgColor-accent, var(--color-accent-fg))]
[color=var(--fgColor-accent, var(--color-accent-fg))]FlashDB is an ultra-lightweight embedded database focused on providing data storage solutions for embedded products. FlashDB not only supports the traditional file-system-based database mode, but also leverages the characteristics of Flash, providing strong performance and reliability. While keeping resource usage extremely low, it extends the Flash lifespan as much as possible.
FlashDB provides two database modes:
- Key-Value Database: A non-relational database that stores data as a collection of key-value pairs, where the key serves as the unique identifier. KVDB is simple to operate and highly extensible.
- Time Series Database (TSDB): It stores data in chronological order. TSDB data carries timestamps, supports large data volumes, and offers high insertion and query performance.
The ported source code (over 3 MB, so it had to be split into two archives)
Attached files: 
FlashDB_test.zip(247.67 KB, downloads: 0)
Attached files: 
components.part01.rar(3 MB, downloads: 0)
Attached files: 
components.part02.rar(2.63 MB, downloads: 0)
The archive contains components and examples. Put the contents of the components folder into the components directory under the AI-WB2 SDK

The examples go under 
A bouffalo.mk was added to the components
Component Makefile
These include paths would be exported to project level
COMPONENT_ADD_INCLUDEDIRS += port/bl602/inc port/fal/inc inc
not be exported to project level
COMPONENT_PRIV_INCLUDEDIRS :=
This component's src
COMPONENT_SRCS := port/bl602/porting/fal_flash_bl602_port.c
port/fal/src/fal.c port/fal/src/fal_flash.c port/fal/src/fal_partition.c port/fal/src/fal_rtt.c
samples/kvdb_basic_sample.c samples/kvdb_type_blob_sample.c samples/kvdb_type_string_sample.c samples/tsdb_sample.c
src/fdb.c src/fdb_file.c src/fdb_kvdb.c src/fdb_tsdb.c src/fdb_utils.c COMPONENT_OBJS := $(patsubst %.c,%.o, $(COMPONENT_SRCS))COMPONENT_SRCDIRS := port/bl602/porting port/fal/src samples src
#CPPFLAGS +=
Create the flashdb_task in main()
xTaskCreate(flashdb_task, (char*)"flashdb_task", 512, NULL, 13, NULL);//任务优先级数值越小,优先级越低
Click to expand full code
#include <FreeRTOS.h>
#include <task.h>
#include <timers.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <vfs.h>
#include <aos/kernel.h>
#include <aos/yloop.h>
#include <event_device.h>
#include <cli.h>
#include <lwip/tcpip.h>
#include <lwip/sockets.h>
#include <lwip/netdb.h>
#include <lwip/tcp.h>
#include <lwip/err.h>
#include <http_client.h>
#include <netutils/netutils.h>
#include <bl602_glb.h>
#include <bl602_hbn.h>
#include "bl602_adc.h"
#include <bl_sys.h>
#include <bl_uart.h>
#include <bl_chip.h>
#include <bl_wifi.h>
#include <hal_wifi.h>
#include <bl_sec.h>
#include <bl_cks.h>
#include <bl_irq.h>
#include <bl_timer.h>
#include <bl_dma.h>
#include <bl_gpio_cli.h>
#include <bl_wdt_cli.h>
#include <hosal_uart.h>
#include <hal_sys.h>
#include <hal_gpio.h>
#include <hal_hbn.h>
#include <hal_boot2.h>
#include <hal_board.h>
#include <hal_button.h>
#include <looprt.h>
#include <loopset.h>
#include <sntp.h>
#include <bl_sys_time.h>
#include <bl_sys_ota.h>
#include <bl_romfs.h>
#include <fdt.h>
#include <device/vfs_uart.h>
#include <bl_gpio.h>
#include <bl60x_fw_api.h>
#include <wifi_mgmr_ext.h>
#include <utils_log.h>
#include <libfdt.h>
#include "hal_pds.h"
#include "bl_rtc.h"
#include "utils_string.h"
#include "bl_http_ota.h"
#include "utils_getopt.h"
#include "utils_time.h"
#include "event_groups.h"
#include "app_inclued.h"
#include "MultiButton_poll_Task.h"
#include "littlefs_task.h"
#include "flashdb_task.h"
#include <blog.h>
/*组件log开关 静态开关 在相应的 proj_config.mk 文件目录下,LOG_ENABLED_COMPONENTS配置上增加对应组件的名字 例如这里需要增加blog_testc组件静态开关,其他组件默认关闭 LOG_ENABLED_COMPONENTS:=blog_testc */
/* 文件log管理 //#define BLOG_HARD_DECLARE_DISABLE 1 // 关 */
/* 私有log管理 静态开关 使用就增加BLOG_DECLARE(…),不用直接不增加此行即可。 BLOG_DECLARE(blog_testc2); // 打开,其中 "blog_testc2"为用户自定义 //BLOG_DECLARE(main); */
/* 调整等级 blog_set_level_log_component(BLOG_LEVEL_ALL, "blog_demo"); */
Click to expand full code
BLOG_DECLARE(main);//必需定义????
static void func(void)
{
blog_set_level_log_component(BLOG_LEVEL_ALL, "blog_demo");
blog_print("The log level is LOG_LEVEL_ALL\r\n");//末尾需加 \r\n
blog_debug("The log level is LOG_LEVEL_DEBUG");
blog_info("The log level is LOG_LEVEL_INFO");
blog_warn("The log level is LOG_LEVEL_WARN");
blog_error("The log level is LOG_LEVEL_ERROR");
blog_assert("The log level is LOG_LEVEL_ASSERT");//不能用????
blog_debug_user(main,"The log level is LOG_LEVEL_DEBUG");
blog_info_user(main,"The log level is LOG_LEVEL_INFO");
blog_warn_user(main,"The log level is LOG_LEVEL_WARN");
blog_error_user(main,"The log level is LOG_LEVEL_ERROR");
blog_assert_user(main,"The log level is LOG_LEVEL_ASSERT");
}
#if defined(CONFIG_AUTO_PTS)
#include "bttester.h"
#include "autopts_uart.h"
#endif
#define TASK_PRIORITY_FW ( 30 )
#define mainHELLO_TASK_PRIORITY ( 20 )
#define UART_ID_2 (2)
#define WIFI_AP_PSM_INFO_SSID "conf_ap_ssid"
#define WIFI_AP_PSM_INFO_PASSWORD "conf_ap_psk"
#define WIFI_AP_PSM_INFO_PMK "conf_ap_pmk"
#define WIFI_AP_PSM_INFO_BSSID "conf_ap_bssid"
#define WIFI_AP_PSM_INFO_CHANNEL "conf_ap_channel"
#define WIFI_AP_PSM_INFO_IP "conf_ap_ip"
#define WIFI_AP_PSM_INFO_MASK "conf_ap_mask"
#define WIFI_AP_PSM_INFO_GW "conf_ap_gw"
#define WIFI_AP_PSM_INFO_DNS1 "conf_ap_dns1"
#define WIFI_AP_PSM_INFO_DNS2 "conf_ap_dns2"
#define WIFI_AP_PSM_INFO_IP_LEASE_TIME "conf_ap_ip_lease_time"
#define WIFI_AP_PSM_INFO_GW_MAC "conf_ap_gw_mac"
#define CLI_CMD_AUTOSTART1 "cmd_auto1"
#define CLI_CMD_AUTOSTART2 "cmd_auto2"
#define TIME_5MS_IN_32768CYCLE (164) // (5000/(1000000/32768))
bool pds_start = false;extern void ble_stack_start(void);
Click to expand full code
static wifi_interface_t wifi_interface;
static wifi_conf_t conf =
{.country_code = "CN", };
EventGroupHandle_t APP_event_group;
Click to expand full code
#if defined(CFG_BLE_PDS)
void vApplicationIdleHook(void)
{
if(!pds_start){__asm volatile( " wfi " );
Click to expand full code
/*empty*/
}
}
#endif
#if ( configUSE_TICKLESS_IDLE != 0 )
void vApplicationSleep( TickType_t xExpectedIdleTime_ms )
{
#if defined(CFG_BLE_PDS)
int32_t bleSleepDuration_32768cycles = 0;
int32_t expectedIdleTime_32768cycles = 0;eSleepModeStatus eSleepStatus;
Click to expand full code
bool freertos_max_idle = false;
if (pds_start == 0)
return;
if(xExpectedIdleTime_ms + xTaskGetTickCount() == portMAX_DELAY){
freertos_max_idle = true;
}else{xExpectedIdleTime_ms -= 1;
Click to expand full code
expectedIdleTime_32768cycles = 32768 * xExpectedIdleTime_ms / 1000;
}
if((!freertos_max_idle)&&(expectedIdleTime_32768cycles < TIME_5MS_IN_32768CYCLE)){
return;
}
/*Disable mtimer interrrupt*/(volatile uint8_t)configCLIC_TIMER_ENABLE_ADDRESS = 0;
Click to expand full code
eSleepStatus = eTaskConfirmSleepModeStatus();
if(eSleepStatus == eAbortSleep || ble_controller_sleep_is_ongoing())
{
/*A task has been moved out of the Blocked state since this macro wasexecuted, or a context siwth is being held pending.Restart the tick and exit the critical section. */ /Enable mtimer interrrupt/ (volatile uint8_t)configCLIC_TIMER_ENABLE_ADDRESS = 1;
Click to expand full code
//printf("%s:not do ble sleep\r\n", __func__);
return;
}
bleSleepDuration_32768cycles = ble_controller_sleep();
if(bleSleepDuration_32768cycles < TIME_5MS_IN_32768CYCLE)
{
/*BLE controller does not allow sleep. Do not enter a sleep state.Restart the tickand exit the critical section. */ /Enable mtimer interrrupt/ //printf("%s:not do pds sleep\r\n", func); (volatile uint8_t)configCLIC_TIMER_ENABLE_ADDRESS = 1;
Click to expand full code
}
else
{
printf("%s:bleSleepDuration_32768cycles=%ld\r\n", __func__, bleSleepDuration_32768cycles);
if(eSleepStatus == eStandardSleep && ((!freertos_max_idle) && (expectedIdleTime_32768cycles < bleSleepDuration_32768cycles)))
{
hal_pds_enter_with_time_compensation(1, expectedIdleTime_32768cycles - 40);//40);//20);
}
else
{
hal_pds_enter_with_time_compensation(1, bleSleepDuration_32768cycles - 40);//40);//20);
}
}
#endif
}
#endif
static void proc_hellow_entry(void *pvParameters)
{
while (1) {
printf("%s: RISC-V rv32imafc\r\n", __func__);
vTaskDelay(10000);
func();
}
vTaskDelete(NULL);
}
static void blink_test(void *param)
{
while (1)
{
bl_gpio_output_set(3, 1);
vTaskDelay(1000);
bl_gpio_output_set(3, 0);
bl_gpio_output_set(14, 1);
vTaskDelay(1000);
bl_gpio_output_set(14, 0);
bl_gpio_output_set(17, 1);
vTaskDelay(1000);
bl_gpio_output_set(17, 0);
bl_gpio_output_set(3, 1);
bl_gpio_output_set(14, 1);
vTaskDelay(1000);
bl_gpio_output_set(3, 0);
bl_gpio_output_set(14, 0);
bl_gpio_output_set(3, 1);
bl_gpio_output_set(17, 1);
vTaskDelay(1000);
bl_gpio_output_set(3, 0);
bl_gpio_output_set(17, 0);
bl_gpio_output_set(14, 1);
bl_gpio_output_set(17, 1);
vTaskDelay(1000);
bl_gpio_output_set(14, 0);
bl_gpio_output_set(17, 0);
bl_gpio_output_set(3, 1);
bl_gpio_output_set(14, 1);
bl_gpio_output_set(17, 1);
vTaskDelay(1000);
bl_gpio_output_set(3, 0);
bl_gpio_output_set(14, 0);
bl_gpio_output_set(17, 0);
}
}
static void blink_LED(char *buf, int len, int argc, char **argv)
{
xTaskCreate(blink_test, (char*)"blink_LED", 512, NULL, 13, NULL);//任务优先级数值越小,优先级越低
}
static unsigned char char_to_hex(char asccode)
{unsigned char ret;
Click to expand full code
if('0'<=asccode && asccode<='9')
ret=asccode-'0';
else if('a'<=asccode && asccode<='f')
ret=asccode-'a'+10;
else if('A'<=asccode && asccode<='F')
ret=asccode-'A'+10;
else
ret=0;
return ret;
}
static void _chan_str_to_hex(uint8_t *chan_band, uint16_t *chan_freq, char *chan)
{int i, freq_len, base=1;
Click to expand full code
uint8_t band;
uint16_t freq = 0;
char *p, *q;
/*should have the following format- 2412|0
- */
Click to expand full code
p = strchr(chan, '|') + 1;
if (NULL == p) {
return;
}
band = char_to_hex(p[0]);(*chan_band) = band;
freq_len = strlen(chan) - strlen(p) - 1; q = chan; q[freq_len] = '\0';
Click to expand full code
for (i=0; i< freq_len; i++) {
freq = freq + char_to_hex(q[freq_len-1-i]) * base;
base = base * 10;
}(*chan_freq) = freq;
Click to expand full code
}
static void bssid_str_to_mac(uint8_t *hex, char *bssid, int len)
{unsigned char l4,h4; int i,lenstr; lenstr = len;
if(lenstr%2) { lenstr -= (lenstr%2);
Click to expand full code
}
if(lenstr==0){
return;
}
for(i=0; i < lenstr; i+=2) {
h4=char_to_hex(bssid);
l4=char_to_hex(bssid[i+1]);hex[i/2]=(h4<<4)+l4;
Click to expand full code
}
}
static void _connect_wifi()
{
/*XXX caution for BIG STACK*/
char pmk[66], bssid[32], chan[10];
char ssid[33], password[66];
char val_buf[66];
// char val_len = sizeof(val_buf) - 1;uint8_t mac[6];
Click to expand full code
uint8_t band = 0;
uint16_t freq = 0;
wifi_interface = wifi_mgmr_sta_enable();
printf("[APP] [WIFI] [T] %lld\r\n""[APP] Get STA %p from Wi-Fi Mgmr, pmk ptr %p, ssid ptr %p, password %p\r\n", aos_now_ms(), wifi_interface, pmk, ssid, password );
Click to expand full code
memset(pmk, 0, sizeof(pmk));
memset(ssid, 0, sizeof(ssid));
memset(password, 0, sizeof(password));
memset(bssid, 0, sizeof(bssid));
memset(mac, 0, sizeof(mac));
memset(chan, 0, sizeof(chan));
memset(val_buf, 0, sizeof(val_buf));
// ef_get_env_blob((const char *)WIFI_AP_PSM_INFO_SSID, val_buf, val_len, NULL);
if (val_buf[0]) {
/*We believe that when ssid is set, wifi_confi is OK*/
strncpy(ssid, val_buf, sizeof(ssid) - 1);
} else {
/*Won't connect, since ssid config is empty*/
puts("[APP] Empty Config\r\n");
puts("[APP] Try to set the following ENV with psm_set command, then reboot\r\n");
puts("[APP] NOTE: " WIFI_AP_PSM_INFO_PMK " MUST be psm_unset when conf is changed\r\n");
puts("[APP] env: " WIFI_AP_PSM_INFO_SSID "\r\n");
puts("[APP] env: " WIFI_AP_PSM_INFO_PASSWORD "\r\n");
puts("[APP] env(optinal): " WIFI_AP_PSM_INFO_PMK "\r\n");
return;
}
memset(val_buf, 0, sizeof(val_buf));
// ef_get_env_blob((const char *)WIFI_AP_PSM_INFO_PASSWORD, val_buf, val_len, NULL);
if (val_buf[0]) {
strncpy(password, val_buf, sizeof(password) - 1);
}
memset(val_buf, 0, sizeof(val_buf));
// ef_get_env_blob((const char *)WIFI_AP_PSM_INFO_PMK, val_buf, val_len, NULL);
if (val_buf[0]) {
strncpy(pmk, val_buf, sizeof(pmk) - 1);
}
if (0 == pmk[0]) {
printf("[APP] [WIFI] [T] %lld\r\n",
aos_now_ms());
Click to expand full code
puts("[APP] Re-cal pmk\r\n");
/*At lease pmk is not illegal, we re-cal now*/
//XXX time consuming API, so consider lower-prirotiy for cal PSK to avoid sound glitch
wifi_mgmr_psk_cal(password, ssid, strlen(ssid), pmk );
Click to expand full code
// ef_set_env(WIFI_AP_PSM_INFO_PMK, pmk);
// ef_save_env();
}
memset(val_buf, 0, sizeof(val_buf));
// ef_get_env_blob((const char *)WIFI_AP_PSM_INFO_CHANNEL, val_buf, val_len, NULL);
if (val_buf[0]) {
strncpy(chan, val_buf, sizeof(chan) - 1);
printf("connect wifi channel = %s\r\n", chan);
_chan_str_to_hex(&band, &freq, chan);
}
memset(val_buf, 0, sizeof(val_buf));
// ef_get_env_blob((const char *)WIFI_AP_PSM_INFO_BSSID, val_buf, val_len, NULL);
if (val_buf[0]) {
strncpy(bssid, val_buf, sizeof(bssid) - 1);
printf("connect wifi bssid = %s\r\n", bssid);
bssid_str_to_mac(mac, bssid, strlen(bssid));
printf("mac = %02X:%02X:%02X:%02X:%02X:%02X\r\n",mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); } printf("[APP] [WIFI] [T] %lld\r\n" "[APP] SSID %s\r\n" "[APP] SSID len %d\r\n" "[APP] password %s\r\n" "[APP] password len %d\r\n" "[APP] pmk %s\r\n" "[APP] bssid %s\r\n" "[APP] channel band %d\r\n" "[APP] channel freq %d\r\n", aos_now_ms(), ssid, strlen(ssid), password, strlen(password), pmk, bssid, band, freq );
Click to expand full code
//wifi_mgmr_sta_connect(wifi_interface, ssid, pmk, NULL);
wifi_mgmr_sta_connect(wifi_interface, ssid, password, pmk, mac, band, freq);
}
#if defined(CONFIG_BT_MESH_SYNC)
typedef struct _wifi_item {
char ssid[32];
uint32_t ssid_len;uint8_t bssid[6];
Click to expand full code
uint8_t channel;
uint8_t auth;
int8_t rssi;} _wifi_item_t;
Click to expand full code
struct _wifi_conn {
char ssid[32];
char ssid_tail[1];
char pask[64];
};
struct _wifi_state {
char ip[16];
char gw[16];
char mask[16];
char ssid[32];
char ssid_tail[1];uint8_t bssid[6];
Click to expand full code
uint8_t state;
};
#endif /* CONFIG_BT_MESH_SYNC */
static void wifi_sta_connect(char *ssid, char *password)
{
//wifi_interface_t wifi_interface;////
wifi_interface = wifi_mgmr_sta_enable();
wifi_mgmr_sta_connect(wifi_interface, ssid, password, NULL, NULL, 0, 0);
}
#if defined(CONFIG_BT_MESH_SYNC)
static void scan_item_cb(wifi_mgmr_ap_item_t *env, uint32_t *param1, wifi_mgmr_ap_item_t *item)
{_wifi_item_t wifi_item;
Click to expand full code
void (*complete)(void *) = (void (*)(void *))param1;
wifi_item.auth = item->auth;
wifi_item.rssi = item->rssi;
wifi_item.channel = item->channel;
wifi_item.ssid_len = item->ssid_len;
memcpy(wifi_item.ssid, item->ssid, sizeof(wifi_item.ssid));
memcpy(wifi_item.bssid, item->bssid, sizeof(wifi_item.bssid));
if (complete) {
complete(&wifi_item);
}
}
static void scan_complete_cb(void *p_arg, void *param)
{
wifi_mgmr_scan_ap_all(NULL, p_arg, scan_item_cb);
}
static void wifiprov_scan(void *p_arg)
{
wifi_mgmr_scan(p_arg, scan_complete_cb);
}
static void wifiprov_wifi_state_get(void *p_arg)
{
int tmp_state;wifi_mgmr_sta_connect_ind_stat_info_t info; ip4_addr_t ip, gw, mask; struct _wifi_state state;
Click to expand full code
void (*state_get_cb)(void *) = (void (*)(void *))p_arg;
memset(&state, 0, sizeof(state));
memset(&info, 0, sizeof(info));
wifi_mgmr_state_get(&tmp_state);
wifi_mgmr_sta_ip_get(&ip.addr, &gw.addr, &mask.addr);
wifi_mgmr_sta_connect_ind_stat_get(&info);
state.state = tmp_state;
strcpy(state.ip, ip4addr_ntoa(&ip));
strcpy(state.mask, ip4addr_ntoa(&mask));
strcpy(state.gw, ip4addr_ntoa(&gw));
memcpy(state.ssid, info.ssid, sizeof(state.ssid));
memcpy(state.bssid, info.bssid, sizeof(state.bssid));state.ssid_tail[0] = 0;
Click to expand full code
printf("IP :%s \r\n", state.ip);
printf("GW :%s \r\n", state.gw);
printf("MASK:%s \r\n", state.mask);
if (state_get_cb) {
state_get_cb(&state);
}
}
#endif /* CONFIG_BT_MESH_SYNC */
uint8_t wifi_connect_count = 0;
static void event_cb_wifi_event(input_event_t *event, void *private_data)
{
static char *ssid;
static char *password;
switch (event->code) {
case CODE_WIFI_ON_INIT_DONE:
{
printf("[APP] [EVT] INIT DONE %lld\r\n", aos_now_ms());
wifi_mgmr_start_background(&conf);
}
break;
case CODE_WIFI_ON_MGMR_DONE:
{
printf("[APP] [EVT] MGMR DONE %lld\r\n", aos_now_ms());
_connect_wifi();
}
break;
case CODE_WIFI_ON_SCAN_DONE:
{
printf("[APP] [EVT] SCAN Done %lld, SCAN Result: %s\r\n",
aos_now_ms(),
WIFI_SCAN_DONE_EVENT_OK == event->value ? "OK" : "Busy now");
Click to expand full code
wifi_mgmr_cli_scanlist();
}
break;
case CODE_WIFI_ON_DISCONNECT:
{
printf("[APP] [EVT] disconnect %lld, Reason: %s\r\n",
aos_now_ms(),
wifi_mgmr_status_code_str(event->value));
Click to expand full code
xEventGroupClearBits(APP_event_group,APP_event_WIFI_STA_CONNECTED_BIT);
wifi_connect_count++;
printf("[APP] [EVT] [wifi_connect_count]:%d\r\n",wifi_connect_count);
if(wifi_connect_count >=5 ){
wifi_mgmr_sta_disconnect();
/*XXX Must make sure sta is already disconnect, otherwise sta disable won't work*/
bl_os_msleep(1000);
//wifi_mgmr_sta_disable(NULL);
wifi_mgmr_sta_connect(wifi_interface, "A512", "A51296956", NULL, NULL, 0, 0);
}
}
break;
case CODE_WIFI_ON_CONNECTING:
{
printf("[APP] [EVT] Connecting %lld\r\n", aos_now_ms());
xEventGroupClearBits(APP_event_group,APP_event_WIFI_STA_CONNECTED_BIT);
}
break;
case CODE_WIFI_CMD_RECONNECT:
{
printf("[APP] [EVT] Reconnect %lld\r\n", aos_now_ms());
xEventGroupClearBits(APP_event_group,APP_event_WIFI_STA_CONNECTED_BIT);
}
break;
case CODE_WIFI_ON_CONNECTED:
{
printf("[APP] [EVT] connected CODE_WIFI_ON_CONNECTED%lld\r\n", aos_now_ms());
//xEventGroupSetBits(APP_event_group,APP_event_WIFI_STA_CONNECTED_BIT);
}
break;
case CODE_WIFI_ON_PRE_GOT_IP:
{
printf("[APP] [EVT] connected CODE_WIFI_ON_PRE_GOT_IP%lld\r\n", aos_now_ms());
//xEventGroupSetBits(APP_event_group,APP_event_WIFI_STA_CONNECTED_BIT);
}
break;
case CODE_WIFI_ON_GOT_IP:
{
printf("[APP] [EVT] GOT IP %lld\r\n", aos_now_ms());
printf("[SYS] Memory left is %d Bytes\r\n", xPortGetFreeHeapSize());
xEventGroupSetBits(APP_event_group,APP_event_WIFI_STA_CONNECTED_BIT);
}
break;
case CODE_WIFI_ON_PROV_SSID:
{
printf("[APP] [EVT] [PROV] [SSID] %lld: %s\r\n",
aos_now_ms(),event->value ? (const char*)event->value : "UNKNOWN" );
Click to expand full code
if (ssid) {
vPortFree(ssid);
ssid = NULL;
}
ssid = (char*)event->value;
}
break;
case CODE_WIFI_ON_PROV_BSSID:
{
printf("[APP] [EVT] [PROV] [BSSID] %lld: %s\r\n",
aos_now_ms(),event->value ? (const char*)event->value : "UNKNOWN" );
Click to expand full code
if (event->value) {
vPortFree((void*)event->value);
}
}
break;
case CODE_WIFI_ON_PROV_PASSWD:
{
printf("[APP] [EVT] [PROV] [PASSWD] %lld: %s\r\n", aos_now_ms(),event->value ? (const char*)event->value : "UNKNOWN" );
Click to expand full code
if (password) {
vPortFree(password);
password = NULL;
}
password = (char*)event->value;
}
break;
case CODE_WIFI_ON_PROV_CONNECT:
{
printf("[APP] [EVT] [PROV] [CONNECT] %lld\r\n", aos_now_ms());
#if defined(CONFIG_BT_MESH_SYNC)
if(event->value){struct _wifi_conn *conn_info = (struct _wifi_conn *)event->value;
Click to expand full code
wifi_sta_connect(conn_info->ssid, conn_info->pask);
break;
}
#endif
printf("connecting to %s:%s...\r\n", ssid, password);
wifi_sta_connect(ssid, password);
}
break;
case CODE_WIFI_ON_PROV_DISCONNECT:
{
printf("[APP] [EVT] [PROV] [DISCONNECT] %lld\r\n", aos_now_ms());
#if defined(CONFIG_BT_MESH_SYNC)
wifi_mgmr_sta_disconnect();
vTaskDelay(1000);
wifi_mgmr_sta_disable(NULL);
#endif
}
break;
#if defined(CONFIG_BT_MESH_SYNC)
case CODE_WIFI_ON_PROV_SCAN_START:
{
printf("[APP] [EVT] [PROV] [SCAN] %lld\r\n", aos_now_ms());
wifiprov_scan((void *)event->value);
}
break;
case CODE_WIFI_ON_PROV_STATE_GET:
{
printf("[APP] [EVT] [PROV] [STATE] %lld\r\n", aos_now_ms());
wifiprov_wifi_state_get((void *)event->value);
}
break;
#endif /*CONFIG_BT_MESH_SYNC*/
case CODE_WIFI_ON_AP_STA_ADD:
{
printf("[APP] [EVT] CODE_WIFI_ON_AP_STA_ADD\r\n");
xEventGroupClearBits(APP_event_group,APP_event_WIFI_STA_CONNECTED_BIT);
xEventGroupSetBits(APP_event_group,APP_event_WIFI_AP_CONNECTED_BIT);
}
break;
case CODE_WIFI_ON_AP_STA_DEL:
{
printf("[APP] [EVT] CODE_WIFI_ON_AP_STA_DEL\r\n");
xEventGroupClearBits(APP_event_group,APP_event_WIFI_STA_CONNECTED_BIT | APP_event_WIFI_AP_CONNECTED_BIT);
}
break;
default:
{
printf("[APP] [EVT] Unknown code %u, %lld\r\n", event->code, aos_now_ms());
/*nothing*/
}
}
}
static void __attribute__((unused)) cmd_aws(char *buf, int len, int argc, char **argv)
{
void aws_main_entry(void *arg);
xTaskCreate(aws_main_entry, (char*)"aws_iot", 4096, NULL, 10, NULL);//任务优先级数值越小,优先级越低
}
#define MAXBUF 128
#define BUFFER_SIZE (12*1024)
#define PORT 80
static int client_demo(char *hostname)
{
int sockfd;
/* Get host address from the input name */struct hostent *hostinfo = gethostbyname(hostname);
Click to expand full code
uint8_t *recv_buffer;
if (!hostinfo) {
printf("gethostbyname Failed\r\n");
return -1;
}struct sockaddr_in dest;
Click to expand full code
char buffer[MAXBUF];
/* Create a socket */
/*---Open socket for streaming---*/
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Error in socket\r\n");
return -1;
}
/*---Initialize server address/port struct---*/
memset(&dest, 0, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(PORT);
dest.sin_addr = *((struct in_addr *) hostinfo->h_addr);
// char ip[16];
uint32_t address = dest.sin_addr.s_addr;
char *ip = inet_ntoa(address);
printf("Server ip Address : %s\r\n", ip);
/*---Connect to server---*/
if (connect(sockfd,(struct sockaddr *)&dest,
Click to expand full code
sizeof(dest)) != 0) {
printf("Error in connect\r\n");
return -1;
}
/*---Get "Hello?"---*/
memset(buffer, 0, MAXBUF);
char wbuf[]= "GET /ddm/ContentResource/music/204.mp3 HTTP/1.1\r\nHost: nf.cr.dandanman.com\r\nUser-Agent: wmsdk\r\nAccept: /\r\n\r\n";
Click to expand full code
write(sockfd, wbuf, sizeof(wbuf) - 1);
int ret = 0;
int total = 0;
int debug_counter = 0;uint32_t ticks_start, ticks_end, time_consumed;
Click to expand full code
ticks_start = xTaskGetTickCount();
recv_buffer = pvPortMalloc(BUFFER_SIZE);
if (NULL == recv_buffer) {
goto out;
}
while (1) {
ret = read(sockfd, recv_buffer, BUFFER_SIZE);
if (ret == 0) {
printf("eof\n\r");
break;
} else if (ret < 0) {
printf("ret = %d, err = %d\n\r", ret, errno);
break;
} else {total += ret;
Click to expand full code
/*use less debug*/
if (0 == ((debug_counter++) & 0xFF)) {
printf("total = %d, ret = %d\n\r", total, ret);
}
//vTaskDelay(2);
if (total > 82050000) {
ticks_end = xTaskGetTickCount();
time_consumed = ((uint32_t)(((int32_t)ticks_end) - ((int32_t)ticks_start))) / 1000;
printf("Download comlete, total time %u s, speed %u Kbps\r\n",(unsigned int)time_consumed, (unsigned int)(total / time_consumed * 8 / 1000) );
Click to expand full code
break;
}
}
}
vPortFree(recv_buffer);out:
Click to expand full code
close(sockfd);
return 0;
}
static void http_test_cmd(char *buf, int len, int argc, char **argv)
{
// <[http://nf.cr.dandanman.com/ddm/ContentResource/music/204.mp3](http://nf.cr.dandanman.com/ddm/ContentResource/music/204.mp3)>
client_demo("nf.cr.dandanman.com");
}
static void cb_httpc_result(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res, err_t err)
{
httpc_state_t **req = (httpc_state_t**)arg;
printf("[HTTPC] Transfer finished. rx_content_len is %lu\r\n", rx_content_len);*req = NULL; }
err_t cb_httpc_headers_done_fn(httpc_state_t *connection, void *arg, struct pbuf *hdr, u16_t hdr_len, u32_t content_len)
Click to expand full code
{
printf("[HTTPC] hdr_len is %u, content_len is %lu\r\n", hdr_len, content_len);
return ERR_OK;
}
static err_t cb_altcp_recv_fn(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err)
{
//printf("[HTTPC] Received %u Bytes\r\n", p->tot_len);
static int count = 0;
puts(".");
if (0 == ((count++) & 0x3F)) {
puts("\r\n");
}
altcp_recved(conn, p->tot_len);
pbuf_free(p);
return ERR_OK;
}
static void cmd_httpc_test(char *buf, int len, int argc, char **argv)
{
static httpc_connection_t settings;
static httpc_state_t *req;
if (req) {
printf("[CLI] req is on-going...\r\n");
return;
}
memset(&settings, 0, sizeof(settings));
settings.use_proxy = 0;
settings.result_fn = cb_httpc_result;
settings.headers_done_fn = cb_httpc_headers_done_fn;
httpc_get_file_dns("nf.cr.dandanman.com", 80, "/ddm/ContentResource/music/204.mp3", &settings, cb_altcp_recv_fn, &req, &req );
Click to expand full code
}
static void cmd_stack_wifi(char *buf, int len, int argc, char **argv)
{
/*wifi fw stack and thread stuff*/
static uint8_t stack_wifi_init = 0;
if (1 == stack_wifi_init) {
puts("Wi-Fi Stack Started already!!!\r\n");
return;
}
stack_wifi_init = 1;
hal_wifi_start_firmware_task();
/*Trigger to start Wi-Fi*/
aos_post_event(EV_WIFI, CODE_WIFI_ON_INIT_DONE, 0);
}
const static struct cli_command cmds_user[] STATIC_CLI_CMD_ATTRIBUTE = {{ "aws", "aws iot demo", cmd_aws}, /Stack Command/ { "stack_wifi", "Wi-Fi Stack", cmd_stack_wifi}, /TCP/IP network test/ {"http", "http client download test based on socket", http_test_cmd}, {"httpc", "http client download test based on RAW TCP", cmd_httpc_test}, {"blink_LED", "blink_LED", blink_LED},
Click to expand full code
};
static void _cli_init()
{
/*Put CLI which needs to be init here*/
int codex_debug_cli_init(void);
codex_debug_cli_init();
// easyflash_cli_init();
network_netutils_iperf_cli_register();
network_netutils_tcpclinet_cli_register();
network_netutils_tcpserver_cli_register();
network_netutils_netstat_cli_register();
network_netutils_ping_cli_register();
sntp_cli_init();
bl_sys_time_cli_init();
bl_sys_ota_cli_init();
blfdt_cli_init();
wifi_mgmr_cli_init();
bl_wdt_cli_init();
bl_gpio_cli_init();
looprt_test_cli_init();
bl_http_ota_cli_init();
}
static void event_cb_key_event(input_event_t *event, void *private_data)
{
switch (event->code) {
case KEY_1:
{
printf("[KEY_1] [EVT] INIT DONE %lld\r\n", aos_now_ms());
printf("short press \r\n");
}
break;
case KEY_2:
{
printf("[KEY_2] [EVT] INIT DONE %lld\r\n", aos_now_ms());
printf("long press \r\n");
}
break;
case KEY_3:
{
printf("[KEY_3] [EVT] INIT DONE %lld\r\n", aos_now_ms());
printf("longlong press \r\n");
}
break;
default:
{
printf("[KEY] [EVT] Unknown code %u, %lld\r\n", event->code, aos_now_ms());
/*nothing*/
}
}
}
#if defined(CONFIG_BT_TL)extern void ble_uart_init(uint8_t uartid);
Click to expand full code
#endif
static void proc_main_entry(void *pvParameters)
{
#if 0
if (0 == get_dts_addr("gpio", &fdt, &offset)) {
hal_gpio_init_from_dts(fdt, offset);
fdt_button_module_init((const void *)fdt, (int)offset);
}
#endif
_cli_init();
aos_register_event_filter(EV_WIFI, event_cb_wifi_event, NULL);
aos_register_event_filter(EV_KEY, event_cb_key_event, NULL);
#if defined(CONFIG_BT_TL)
//uart's pinmux has been configured in vfs_uart_init(load uart1's pin info from devicetree)
ble_uart_init(1);
ble_controller_init(configMAX_PRIORITIES - 1);
#endif
#if defined(CONFIG_AUTO_PTS)
pts_uart_init(1,115200,8,1,0,0);
// Initialize BLE controller
ble_controller_init(configMAX_PRIORITIES - 1);extern int hci_driver_init(void);
Click to expand full code
// Initialize BLE Host stack
hci_driver_init();
tester_send(BTP_SERVICE_ID_CORE, CORE_EV_IUT_READY, BTP_INDEX_NONE,NULL, 0);
Click to expand full code
#endif
vTaskDelete(NULL);
}
#if defined(CFG_BLE_PDS)
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize)
{
/* If the buffers to be provided to the Idle task are declared inside this
function then they must be declared static - otherwise they will be allocated onthe stack and so not exists after this function exits. */
Click to expand full code
static StaticTask_t xIdleTaskTCB;
//static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
static StackType_t uxIdleTaskStack[512];
/* Pass out a pointer to the StaticTask_t structure in which the Idle task'sstate will be stored. */ *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
/* Pass out the array that will be used as the Idle task's stack. */ *ppxIdleTaskStackBuffer = uxIdleTaskStack;
/* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer. Note that, as the array is necessarily of type StackType_t, configMINIMAL_STACK_SIZE is specified in words, not bytes. */ //*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE; *pulIdleTaskStackSize = 512;//size 512 words is For ble pds mode, otherwise stack overflow of idle task will happen.
Click to expand full code
}
#endif
static void system_thread_init()
{
/*nothing here*/
}
/*******************************************************************/
static void cmd_wifi_ap_start(int no_password,char *ssid_name, char *password,uint8_t hidden_ssid ,int channel)//no_password:0=Open network;hidden_ssid:1=hidden;channel:1~11;
{uint8_t mac[6];
Click to expand full code
//wifi_interface_t wifi_interface;
vTaskDelay(1200);
printf("%s: start Wi-Fi\r\n", __func__);
hal_wifi_start_firmware_task();
/*Trigger to start Wi-Fi*/
aos_post_event(EV_WIFI, CODE_WIFI_ON_INIT_DONE, 0);
vTaskDelay(1200);
memset(mac, 0, sizeof(mac));
bl_wifi_mac_addr_get(mac);
printf("%s: mac_addr:%x:%x:%x:%x:%x:%x\r\n", __func__,mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
wifi_interface = wifi_mgmr_ap_enable();
if(no_password == 0) {
/*no password when only one param*/
wifi_mgmr_ap_start(wifi_interface, ssid_name, hidden_ssid, NULL, channel);
} else {
/*hardcode password*/
wifi_mgmr_ap_start(wifi_interface, ssid_name, hidden_ssid, password, channel);
}
}
static void wifi_connect_station(char *ssid_name, char *password)
{
//wifi_interface_t wifi_interface;getopt_env_t getopt_env; int opt, open_bss_flag; uint16_t freq = 0; int bssid_set_flag = 0; uint8_t mac[6] = {0}; open_bss_flag = 0; unsigned char argc = 3; char *argv[3];
Click to expand full code
vTaskDelay(1200);
printf("%s: start Wi-Fi\r\n", __func__);
hal_wifi_start_firmware_task();
/*Trigger to start Wi-Fi*/
aos_post_event(EV_WIFI, CODE_WIFI_ON_INIT_DONE, 0);
vTaskDelay(1200);argv[0] = "wifi_connect_cmd"; argv[1] = ssid_name; argv[2] = password;
Click to expand full code
utils_getopt_init(&getopt_env, 0);
while ((opt = utils_getopt(&getopt_env, argc, argv, "c:b:")) != -1) {
switch (opt) {
case 'c':
freq = atoi(getopt_env.optarg);
bl_os_printf("freq: %d\r\n", freq);
break;
case 'b':
bssid_set_flag = 1;
utils_parse_number(getopt_env.optarg, ':', mac, 6, 16);
bl_os_printf("bssid: %s, mac:%02X:%02X:%02X:%02X:%02X:%02X\r\n", getopt_env.optarg,mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Click to expand full code
break;
case '?':
bl_os_printf("unknow option: %c\r\n", getopt_env.optopt);
goto _ERROUT;
}
}
if (getopt_env.optind >= argc || argc - getopt_env.optind < 1) {
bl_os_printf("Expected ssid and password\r\n");
goto _ERROUT;
}
bl_os_printf("connect wifi ssid:%s, psk:%s, bssid:%d, freq:%d\r\n", argv[getopt_env.optind], argv[getopt_env.optind+1], bssid_set_flag, freq);
if (NULL == argv[getopt_env.optind + 1]) {
open_bss_flag = 1;
}
wifi_interface = wifi_mgmr_sta_enable();
wifi_mgmr_sta_connect(wifi_interface, argv[getopt_env.optind], open_bss_flag ? NULL : argv[getopt_env.optind+1], NULL, bssid_set_flag ? mac : NULL, 0, freq);_ERROUT:
Click to expand full code
bl_os_printf("[USAGE]: %s [-c <freq>] [-b <bssid>] <ssid> [password]\r\n", argv[0]);
}
static void _startup_sntp(void *pvParameters)
{
puts("--------------------------------------- Start NTP now\r\n");
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, "0.asia.pool.ntp.org");
sntp_init();
puts("--------------------------------------- Start NTP Done\r\n");
}
static void get_sntp_date(void *pvParameters)
{
uint32_t seconds = 0, frags = 0;utils_time_date_t date;
Click to expand full code
xEventGroupWaitBits(APP_event_group,APP_event_WIFI_STA_CONNECTED_BIT,pdFALSE,pdFALSE,portMAX_DELAY);
tcpip_callback(_startup_sntp, NULL);
for(;;){
puts("SNTP GMT time is\r\n");
sntp_get_time(&seconds, &frags);
utils_time_date_from_epoch(seconds + 28800, &date);//加东八区8小时,
printf("Date & time is: %u-%02u-%02u %02u:%02u:%02u (Day %u of week, Day %u of Year)\r\n",date.ntp_year, date.ntp_month, date.ntp_date, date.ntp_hour, date.ntp_minute, date.ntp_second, date.ntp_week_day, date.day_of_year );
Click to expand full code
vTaskDelay(20000);
}
}
#include <hal_boot2.h>
static StackType_t http_server_stack[512];
static StaticTask_t http_server_handle;
static void APP_entry(void *pvParameters)
{
// Init the event group
bl_os_printf("Create the event group,Message......\r\n");
APP_event_group = xEventGroupCreate();
xTaskCreate(MultiButton_poll_Task, (char*)"MultiButton_poll_Task", 512, NULL, 14, NULL);//任务优先级数值越小,优先级越低
xTaskCreate(flashdb_task, (char*)"flashdb_task", 512, NULL, 13, NULL);//任务优先级数值越小,优先级越低
vTaskDelay(500);
vTaskDelete(NULL);
}
void main()
{
bl_sys_init();
system_thread_init();
bl_gpio_enable_output(3, 0, 0);
bl_gpio_enable_output(14, 0, 0);
bl_gpio_enable_output(17, 0, 0);
bl_gpio_output_set(3, 0);
bl_gpio_output_set(14, 0);
bl_gpio_output_set(17, 0);
blog_set_level_log_component(BLOG_LEVEL_ALL, "LivingRoomCock");
//blog_info_user(main,"[%s][%d] Starting proc_hellow_entry task...\r\n",__FILE__,__LINE__);
//xTaskCreate(proc_hellow_entry, (char*)"hellow", 512, NULL, 13, NULL);//任务优先级数值越小,优先级越低
xTaskCreate(blink_test, (char*)"blink_LED", 512, NULL, 13, NULL);//任务优先级数值越小,优先级越低
puts("[OS] Starting aos_loop_proc task...\r\n");
xTaskCreate(proc_main_entry, (char*)"main_entry", 1024, NULL, 14, NULL);//任务优先级数值越小,优先级越低
puts("[OS] Starting TCP/IP Stack...\r\n");
tcpip_init(NULL, NULL);
xTaskCreate(APP_entry, (char*)"APP_entry", 1024, NULL, 14, NULL);//任务优先级数值越小,优先级越低
#if defined(CONFIG_AUTO_PTS)
tester_init();
#endif
}Finally, the test passed. 
Thanks to Ai-Thinker

