board_perf:add kv and vfs for risc-v

board_perf:add kv and vfs for risc-v
This commit is contained in:
Supowang
2022-06-07 17:25:22 +08:00
parent d3ab42b8b8
commit 85f804213f
18 changed files with 1199 additions and 117 deletions

View File

@@ -0,0 +1,84 @@
#include "tos_k.h"
#include "tos_hal.h"
#include "ff.h"
#include "tos_vfs.h"
#include "tos_fatfs_drv.h"
#include "tos_fatfs_vfs.h"
char buf[512];
void application_entry(void *arg)
{
int fd, ret;
vfs_err_t err;
extern vfs_blkdev_ops_t sd_dev;
extern vfs_fs_ops_t fatfs_ops;
err = tos_vfs_block_device_register("/dev/sd", &sd_dev);
if (err != VFS_ERR_NONE) {
printf("/dev/sd block device register failed!\n");
}
err = tos_vfs_fs_register("fatfs_sd", &fatfs_ops);
if (err != VFS_ERR_NONE) {
printf("fatfs_sd fs register failed!\n");
}
if (tos_vfs_fs_mkfs("/dev/sd", "fatfs_sd", FM_FAT32, 0) != 0) {
printf("mkfs failed!\n");
}
if (tos_vfs_fs_mount("/dev/sd", "/fs/fatfs_sd", "fatfs_sd") != 0) {
printf("mount failed!\n");
}
fd = tos_vfs_open("/fs/fatfs_sd/test_file.txt", VFS_OFLAG_CREATE_ALWAYS | VFS_OFLAG_WRITE);
if (fd < 0) {
printf("open failed!\n");
}
ret = tos_vfs_write(fd, "fatfs sample content", strlen("fatfs sample content"));
if (ret >= 0) {
printf("write ok\n");
printf("write data:\n%s\n", "fatfs sample content");
} else {
printf("write error: %d\n", ret);
}
ret = tos_vfs_close(fd);
if (ret < 0) {
printf("close failed!\n");
}
fd = tos_vfs_open("/fs/fatfs_sd/test_file.txt", VFS_OFLAG_EXISTING | VFS_OFLAG_READ);
if (fd < 0) {
printf("open file error!\n");
}
memset(buf, 0, sizeof(buf));
ret = tos_vfs_read(fd, buf, sizeof(buf));
if (ret >= 0) {
printf("read ok: %d\n", ret);
printf("read data:\n%s\n", buf);
} else {
printf("read error: %d\n", ret);
}
////////////////////////////////////////////////
ret = tos_vfs_lseek(fd, 2, VFS_SEEK_CUR);
if (ret < 0) {
printf("lseek error\n");
}
memset(buf, 0, sizeof(buf));
ret = tos_vfs_read(fd, buf, sizeof(buf));
if (ret >= 0) {
printf("read ok: %d\n", ret);
printf("read data:\n%s\n", buf);
} else {
printf("read error: %d\n", ret);
}
/////////////////////////////////////////////////
tos_vfs_close(fd);
}

View File

@@ -0,0 +1,84 @@
#include "tos_kv.h"
extern kv_flash_drv_t ch32v30x_spiflash_drv;
extern kv_flash_prop_t ch32v30x_spiflash_prop;
//#define KV_SAMPLE_DEBUG
void disp_value(uint8_t *value, uint32_t value_len)
{
int i = 0;
printf("value_len: %d\r\n", value_len);
printf("\"");
for (i = 0; i < value_len; ++i) {
printf("%c", value[i]);
}
printf("\"\r\n\n");
}
void kv_test(void)
{
int has_key;
kv_err_t err;
uint8_t value_buf[40];
uint32_t value_len;
err = tos_kv_init(0, 2 * 4096, &ch32v30x_spiflash_drv, &ch32v30x_spiflash_prop);
printf("kv init, rc: %d\r\n", err);
has_key = tos_kv_has_key("key00");
printf("has key[%s] ? %s\r\n\n", "key00", has_key ? "True" : "False");
err = tos_kv_set("key00", "value_00", strlen("value_00"));
printf("kv set(%s), rc: %d\r\n\n", "key00", err);
has_key = tos_kv_has_key("key00");
printf("has key[%s] ? %s\r\n\n", "key00", has_key ? "True" : "False");
if (err == KV_ERR_NONE) {
err = tos_kv_get("key00", value_buf, sizeof(value_buf), &value_len);
printf("kv get(%s), rc: %d\r\n\n", "key00", err);
if (err == KV_ERR_NONE) {
disp_value(value_buf, value_len);
}
#ifdef KV_SAMPLE_DEBUG
tos_kv_walkthru();
#endif
}
err = tos_kv_set("key00", "value_xx", strlen("value_xx"));
printf("kv set(%s), rc: %d\r\n\n", "key00", err);
if (err == KV_ERR_NONE) {
err = tos_kv_get("key00", value_buf, sizeof(value_buf), &value_len);
printf("kv get(%s), rc: %d\r\n\n", "key00", err);
if (err == KV_ERR_NONE) {
disp_value(value_buf, value_len);
}
}
#ifdef KV_SAMPLE_DEBUG
tos_kv_walkthru();
#endif
err = tos_kv_del("key00");
printf("kv del(%s), rc: %d\r\n\n", "key00", err);
has_key = tos_kv_has_key("key00");
printf("has key[%s] ? %s\r\n\n", "key00", has_key ? "True" : "False");
#ifdef KV_SAMPLE_DEBUG
tos_kv_walkthru();
#endif
}
void application_entry(void *arg)
{
kv_test();
}

View File

@@ -0,0 +1,110 @@
#include "tos_k.h"
#include "esp8266_tencent_firmware.h"
#include "tencent_firmware_module_wrapper.h"
#define PRODUCT_ID "FWR8PGACUS"
#define DEVICE_NAME "dev001"
#define DEVICE_KEY "XIBjgofTv/QEQTlRTDQnGg=="
#define REPORT_DATA_TEMPLATE "{\\\"method\\\":\\\"report\\\"\\,\\\"clientToken\\\":\\\"00000001\\\"\\,\\\"params\\\":{\\\"brightness\\\":%d\\,\\\"name\\\":\\\"bedroom\\\"}}"
void default_message_handler(mqtt_message_t* msg)
{
printf("callback:\r\n");
printf("---------------------------------------------------------\r\n");
printf("\ttopic:%s\r\n", msg->topic);
printf("\tpayload:%s\r\n", msg->payload);
printf("---------------------------------------------------------\r\n");
}
char payload[256] = {0};
static char report_topic_name[TOPIC_NAME_MAX_SIZE] = {0};
static char report_reply_topic_name[TOPIC_NAME_MAX_SIZE] = {0};
void mqtt_demo_task(void)
{
int ret = 0;
int size = 0;
int lightness = 0;
mqtt_state_t state;
char *product_id = PRODUCT_ID;
char *device_name = DEVICE_NAME;
char *key = DEVICE_KEY;
device_info_t dev_info;
memset(&dev_info, 0, sizeof(device_info_t));
/**
* Please Choose your AT Port first, default is HAL_UART_2(USART2)
*/
ret = esp8266_tencent_firmware_sal_init(HAL_UART_PORT_2);
if (ret < 0) {
printf("esp8266 tencent firmware sal init fail, ret is %d\r\n", ret);
}
esp8266_tencent_firmware_join_ap("TencentOS", "tencentos");
strncpy(dev_info.product_id, product_id, PRODUCT_ID_MAX_SIZE);
strncpy(dev_info.device_name, device_name, DEVICE_NAME_MAX_SIZE);
strncpy(dev_info.device_serc, key, DEVICE_SERC_MAX_SIZE);
tos_tf_module_info_set(&dev_info, TLS_MODE_PSK);
mqtt_param_t init_params = DEFAULT_MQTT_PARAMS;
if (tos_tf_module_mqtt_conn(init_params) != 0) {
printf("module mqtt conn fail\n");
} else {
printf("module mqtt conn success\n");
}
if (tos_tf_module_mqtt_state_get(&state) != -1) {
printf("MQTT: %s\n", state == MQTT_STATE_CONNECTED ? "CONNECTED" : "DISCONNECTED");
}
size = snprintf(report_reply_topic_name, TOPIC_NAME_MAX_SIZE, "$thing/down/property/%s/%s", product_id, device_name);
if (size < 0 || size > sizeof(report_reply_topic_name) - 1) {
printf("sub topic content length not enough! content size:%d buf size:%d", size, (int)sizeof(report_reply_topic_name));
}
if (tos_tf_module_mqtt_sub(report_reply_topic_name, QOS0, default_message_handler) != 0) {
printf("module mqtt sub fail\n");
} else {
printf("module mqtt sub success\n");
}
memset(report_topic_name, 0, sizeof(report_topic_name));
size = snprintf(report_topic_name, TOPIC_NAME_MAX_SIZE, "$thing/up/property/%s/%s", product_id, device_name);
if (size < 0 || size > sizeof(report_topic_name) - 1) {
printf("pub topic content length not enough! content size:%d buf size:%d", size, (int)sizeof(report_topic_name));
}
while (1) {
tos_sleep_ms(5000);
/* use AT+PUB AT command */
memset(payload, 0, sizeof(payload));
snprintf(payload, sizeof(payload), REPORT_DATA_TEMPLATE, lightness++);
if (lightness > 100) {
lightness = 0;
}
if (tos_tf_module_mqtt_pub(report_topic_name, QOS0, payload) != 0) {
printf("module mqtt pub fail\n");
break;
} else {
printf("module mqtt pub success\n");
}
}
}
void application_entry(void *arg)
{
mqtt_demo_task();
while (1) {
printf("This is a mqtt demo!\r\n");
tos_task_delay(1000);
}
}