优化代码结构

This commit is contained in:
zhangzheng
2023-09-24 20:05:55 +08:00
parent 0e49c75cd4
commit 20d3da94a8
10 changed files with 98 additions and 71 deletions

View File

@@ -1,5 +1,6 @@
#pragma once
#include <stddef.h>
int fs_open(const char *path, int flags, int mode);
int fs_read(int fd, void *buf, size_t len);
int fs_write(int fd, void *buf, size_t len);

View File

@@ -1,6 +1,6 @@
#define HEAP_SIZE 4
#define STACK_SIZE 1024 * 3
#define STACK_SIZE 1024 * 2
#if defined(__CC_ARM)
#define HEAP_ATTR SECTION("HEAP") __attribute__((zero_init))

View File

@@ -40,6 +40,13 @@ int main(int argc, char *args[])
int ret = rpc_creaite_bind_ipc(THREAD_MAIN, NULL, &ipc_hd);
assert(ret >= 0);
env.ns_hd = ipc_hd;
ret = app_load("fatfs", &env);
if (ret < 0)
{
printf("app load fail, 0x%x\n", ret);
}
ret = app_load("shell", &env);
if (ret < 0)
{
@@ -47,11 +54,6 @@ int main(int argc, char *args[])
}
namespace_init(ipc_hd);
ret = app_load("fatfs", &env);
if (ret < 0)
{
printf("app load fail, 0x%x\n", ret);
}
namespace_loop();
task_unmap(TASK_THIS, vpage_create_raw3(KOBJ_DELETE_RIGHT, 0, TASK_THIS)); // 删除当前task以及申请得所有对象
printf("exit init.\n");

View File

@@ -11,82 +11,20 @@
#include "u_ns.h"
#include "test.h"
#include "u_rpc.h"
#include "ns_cli.h"
#include <assert.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
void malloc_test(void)
{
void *mem = malloc(1024);
assert(mem);
void *mem1 = malloc(1024);
assert(mem1);
free(mem);
free(mem1);
mem = malloc(4 * 1024);
assert(mem);
free(mem);
mem1 = malloc(1024);
assert(mem1);
free(mem1);
}
void ns_test(void)
{
#if 0
int ret;
obj_handler_t tmp_ipc_hd;
tmp_ipc_hd = handler_alloc();
assert(tmp_ipc_hd != HANDLER_INVALID);
msg_tag_t tag = factory_create_ipc(FACTORY_PROT, vpage_create_raw3(0, 0, tmp_ipc_hd));
assert(msg_tag_get_val(tag) >= 0);
ret = cli_ns_register("shell", tmp_ipc_hd);
assert(ret >= 0);
ret = cli_ns_query("shell", &tmp_ipc_hd);
assert(ret >= 0);
ulog_write_str(u_get_global_env()->log_hd, "ns test success.\n");
#endif
obj_handler_t tmp_ipc_hd;
tmp_ipc_hd = handler_alloc();
assert(tmp_ipc_hd != HANDLER_INVALID);
msg_tag_t tag = factory_create_ipc(FACTORY_PROT, vpage_create_raw3(0, 0, tmp_ipc_hd));
assert(ns_register("shell", tmp_ipc_hd) >= 0);
obj_handler_t rcv_ipc_hd;
assert(ns_query("shell", &rcv_ipc_hd) >= 0);
handler_free_umap(rcv_ipc_hd);
}
#include "fs_cli.h"
void fs_test(void)
{
char tmp[4] = "123";
int fd = fs_open("/test", 0, 0x1 | 0x2 | 0x8);
assert(fd >= 0);
int wlen = fs_write(fd, tmp, 4);
assert(wlen == 4);
int ret = fs_lseek(fd, 0, SEEK_SET);
assert(ret >= 0);
int rlen = fs_read(fd, tmp, 4);
assert(rlen == 4);
assert(strcmp(tmp, "123") == 0);
fs_close(fd);
}
int main(int argc, char *args[])
{
printf("argc:%d args[0]:%s\n", argc, args[0]);
ulog_write_str(u_get_global_env()->log_hd, "MKRTOS:\n");
#if 0
malloc_test();
irq_test();
#endif
rpc_test();
ns_test();
#endif
fs_test();
irq_test();
task_unmap(TASK_THIS, vpage_create_raw3(KOBJ_DELETE_RIGHT, 0, TASK_THIS));
ulog_write_str(u_get_global_env()->log_hd, "Error.\n");
return 0;

View File

@@ -1,4 +1,7 @@
#pragma once
void irq_test(void);
void rpc_test(void);
void rpc_test(void);
void fs_test(void);
void ns_test(void);
void malloc_test(void);

View File

@@ -0,0 +1,21 @@
#include "fs_cli.h"
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <stddef.h>
void fs_test(void)
{
char tmp[4] = "123";
int fd = fs_open("/test", 0, 0x1 | 0x2 | 0x8);
assert(fd >= 0);
int wlen = fs_write(fd, tmp, 4);
assert(wlen == 4);
int ret = fs_lseek(fd, 0, SEEK_SET);
assert(ret >= 0);
int rlen = fs_read(fd, tmp, 4);
assert(rlen == 4);
assert(strcmp(tmp, "123") == 0);
fs_close(fd);
}

View File

@@ -56,4 +56,5 @@ void irq_test(void)
}
}
ulog_write_str(LOG_PROT, "irq test success.\n");
handler_free_umap(obj);
}

View File

@@ -0,0 +1,18 @@
#include <malloc.h>
#include <string.h>
void malloc_test(void)
{
void *mem = malloc(1024);
assert(mem);
void *mem1 = malloc(1024);
assert(mem1);
free(mem);
free(mem1);
mem = malloc(4 * 1024);
assert(mem);
free(mem);
mem1 = malloc(1024);
assert(mem1);
free(mem1);
}

View File

@@ -0,0 +1,43 @@
#include "u_log.h"
#include "u_prot.h"
#include "u_mm.h"
#include "u_factory.h"
#include "u_thread.h"
#include "u_task.h"
#include "u_ipc.h"
#include "u_env.h"
#include "u_hd_man.h"
#include "u_ns.h"
#include "ns_cli.h"
#include <assert.h>
void ns_test(void)
{
#if 0
int ret;
obj_handler_t tmp_ipc_hd;
tmp_ipc_hd = handler_alloc();
assert(tmp_ipc_hd != HANDLER_INVALID);
msg_tag_t tag = factory_create_ipc(FACTORY_PROT, vpage_create_raw3(0, 0, tmp_ipc_hd));
assert(msg_tag_get_val(tag) >= 0);
ret = cli_ns_register("shell", tmp_ipc_hd);
assert(ret >= 0);
ret = cli_ns_query("shell", &tmp_ipc_hd);
assert(ret >= 0);
ulog_write_str(u_get_global_env()->log_hd, "ns test success.\n");
#endif
obj_handler_t tmp_ipc_hd;
tmp_ipc_hd = handler_alloc();
assert(tmp_ipc_hd != HANDLER_INVALID);
msg_tag_t tag = factory_create_ipc(FACTORY_PROT, vpage_create_raw3(0, 0, tmp_ipc_hd));
assert(ns_register("shell", tmp_ipc_hd) >= 0);
obj_handler_t rcv_ipc_hd;
assert(ns_query("shell", &rcv_ipc_hd) >= 0);
handler_free_umap(rcv_ipc_hd);
}