cons支持输入功能

This commit is contained in:
zhangzheng
2023-12-02 09:42:22 +08:00
parent 1d9e8cc532
commit bc9055d784
9 changed files with 155 additions and 32 deletions

View File

@@ -53,6 +53,7 @@ void fs_backend_init(void);
long be_read(long fd, char *buf, long size);
long be_write(long fd, char *buf, long size);
long be_readv(long fd, const struct iovec *iov, long iovcnt);
long be_writev(long fd, const struct iovec *iov, long iovcnt);
long be_ioctl(long fd, long req, void *args);
long be_set_tid_address(int *val);

View File

@@ -13,7 +13,7 @@
#include <assert.h>
#include <u_util.h>
#include "fd_map.h"
#include "u_sleep.h"
AUTO_CALL(101)
void fs_backend_init(void)
{
@@ -21,7 +21,42 @@ void fs_backend_init(void)
assert(fd_map_alloc(0, 1, FD_TTY) >= 0);
assert(fd_map_alloc(0, 2, FD_TTY) >= 0);
}
static int be_tty_read(char *buf, long size)
{
pid_t pid;
int len;
int r_len = 0;
if (size == 0)
{
return 0;
}
task_get_pid(TASK_THIS, (umword_t *)(&pid));
while (r_len < size)
{
if (pid == 0)
{
len = ulog_read_bytes(u_get_global_env()->log_hd, buf + r_len, size - r_len);
}
else
{
len = cons_read(buf + r_len, size - r_len);
}
if (len < 0)
{
return len;
}
else if (len == 0)
{
u_sleep_ms(10);
continue;
}
r_len += len;
break;
}
return r_len;
}
long be_read(long fd, char *buf, long size)
{
fd_map_entry_t u_fd;
@@ -35,7 +70,7 @@ long be_read(long fd, char *buf, long size)
{
case FD_TTY:
{
/*TODO:*/
return be_tty_read(buf, size);
}
break;
case FD_FS:
@@ -86,6 +121,60 @@ long be_write(long fd, char *buf, long size)
}
return -ENOSYS;
}
long be_readv(long fd, const struct iovec *iov, long iovcnt)
{
long wlen = 0;
for (int i = 0; i < iovcnt; i++)
{
fd_map_entry_t u_fd;
int ret = fd_map_get(fd, &u_fd);
if (ret < 0)
{
return -EBADF;
}
switch (u_fd.type)
{
case FD_TTY:
{
pid_t pid;
int read_cn;
task_get_pid(TASK_THIS, (umword_t *)(&pid));
if (pid == 0)
{
read_cn = ulog_read_bytes(u_get_global_env()->log_hd, iov[i].iov_base, iov[i].iov_len);
}
else
{
again_read:
read_cn = cons_read(iov[i].iov_base, iov[i].iov_len);
if (read_cn < 0)
{
return read_cn;
}
else if (read_cn == 0)
{
u_sleep_ms(10); // TODO:改成信号量
goto again_read;
}
}
wlen += read_cn;
}
break;
case FD_FS:
{
int rsize = fs_read(fd, iov[i].iov_base, iov[i].iov_len);
wlen += rsize;
}
break;
default:
return -ENOSYS;
}
}
return wlen;
}
long be_writev(long fd, const struct iovec *iov, long iovcnt)
{
long wlen = 0;

View File

@@ -1,24 +1,33 @@
#include "stdio_impl.h"
#include <sys/uio.h>
#ifndef NO_LITTLE_MODE
#include "syscall_backend.h"
#endif
size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
{
struct iovec iov[2] = {
{ .iov_base = buf, .iov_len = len - !!f->buf_size },
{ .iov_base = f->buf, .iov_len = f->buf_size }
};
{.iov_base = buf, .iov_len = len - !!f->buf_size},
{.iov_base = f->buf, .iov_len = f->buf_size}};
ssize_t cnt;
#ifdef NO_LITTLE_MODE
cnt = iov[0].iov_len ? syscall(SYS_readv, f->fd, iov, 2)
: syscall(SYS_read, f->fd, iov[1].iov_base, iov[1].iov_len);
if (cnt <= 0) {
: syscall(SYS_read, f->fd, iov[1].iov_base, iov[1].iov_len);
#else
cnt = iov[0].iov_len ? be_readv(f->fd, iov, 2)
: be_read(f->fd, iov[1].iov_base, iov[1].iov_len);
#endif
if (cnt <= 0)
{
f->flags |= cnt ? F_ERR : F_EOF;
return 0;
}
if (cnt <= iov[0].iov_len) return cnt;
if (cnt <= iov[0].iov_len)
return cnt;
cnt -= iov[0].iov_len;
f->rpos = f->buf;
f->rend = f->buf + cnt;
if (f->buf_size) buf[len-1] = *f->rpos++;
if (f->buf_size)
buf[len - 1] = *f->rpos++;
return len;
}

View File

@@ -33,7 +33,9 @@ static int checker(void *p)
#endif
return 0;
}
#ifndef NO_LITTLE_MODE
#include "syscall_backend.h"
#endif
int faccessat(int fd, const char *filename, int amode, int flag)
{
if (flag)
@@ -64,8 +66,13 @@ int faccessat(int fd, const char *filename, int amode, int flag)
pid = __clone(checker, stack + sizeof stack, 0, &c);
__syscall(SYS_close, p[1]);
#ifdef NO_LITTLE_MODE
if (pid < 0 || __syscall(SYS_read, p[0], &ret, sizeof ret) != sizeof(ret))
ret = -EBUSY;
#else
if (pid < 0 || be_read(p[0], &ret, sizeof ret) != sizeof(ret))
ret = -EBUSY;
#endif
__syscall(SYS_close, p[0]);
__syscall(SYS_wait4, pid, &status, __WCLONE, 0);

View File

@@ -1,7 +1,13 @@
#include <sys/uio.h>
#include "syscall.h"
#ifndef NO_LITTLE_MODE
#include "syscall_backend.h"
#endif
ssize_t readv(int fd, const struct iovec *iov, int count)
{
#ifdef NO_LITTLE_MODE
return syscall_cp(SYS_readv, fd, iov, count);
#else
return be_readv(fd, iov, count);
#endif
}

View File

@@ -10,8 +10,8 @@
RPC_GENERATION_CALL1(cons_t, CONS_PROT, CONS_WRITE, write,
rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, data)
RPC_GENERATION_CALL1(cons_t, CONS_PROT, CONS_WRITE, read,
rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, data)
RPC_GENERATION_CALL1(cons_t, CONS_PROT, CONS_READ, read,
rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_OUT, RPC_TYPE_DATA, data)
RPC_GENERATION_CALL1(cons_t, CONS_PROT, CONS_ACTIVE, active,
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, flags)

View File

@@ -18,8 +18,8 @@ RPC_GENERATION_OP1(cons_t, CONS_PROT, CONS_WRITE, write,
RPC_GENERATION_DISPATCH1(cons_t, CONS_PROT, CONS_WRITE, write,
rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, data)
/*read*/
RPC_GENERATION_OP1(cons_t, CONS_PROT, CONS_WRITE, read,
rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, data)
RPC_GENERATION_OP1(cons_t, CONS_PROT, CONS_READ, read,
rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_OUT, RPC_TYPE_DATA, data)
{
int16_t ret = -1;
@@ -31,17 +31,17 @@ RPC_GENERATION_OP1(cons_t, CONS_PROT, CONS_WRITE, read,
return ret;
}
RPC_GENERATION_DISPATCH1(cons_t, CONS_PROT, CONS_WRITE, read,
rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, data)
RPC_GENERATION_DISPATCH1(cons_t, CONS_PROT, CONS_READ, read,
rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_OUT, RPC_TYPE_DATA, data)
/*active*/
RPC_GENERATION_OP1(cons_t, CONS_PROT, CONS_WRITE, active,
RPC_GENERATION_OP1(cons_t, CONS_PROT, CONS_ACTIVE, active,
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, flags)
{
console_active();
return 0;
}
RPC_GENERATION_DISPATCH1(cons_t, CONS_PROT, CONS_WRITE, active,
RPC_GENERATION_DISPATCH1(cons_t, CONS_PROT, CONS_ACTIVE, active,
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, flags)
/*dispatch*/

View File

@@ -11,11 +11,12 @@
#include "test.h"
int main(int argc, char *args[])
{
uint8_t data[8];
usleep(100000);
printf("%s start running.\n", args[0]);
printf("%s: please input str.\n", args[0]);
scanf("%s", data);
printf("%s\n", data);
mr_drv_test();
pm_test();

View File

@@ -13,21 +13,29 @@
#include <rpc_prot.h>
#include "cons_svr.h"
#include <stdio.h>
#include <u_thread_util.h>
#include <u_thread.h>
static ATTR_ALIGN(8) uint8_t cons_stack[512];
static uint8_t cons_msg_buf[MSG_BUG_LEN];
static cons_t cons_obj;
static obj_handler_t cons_th;
static void console_read_func(void)
{
int r_len = ulog_read_bytes(LOG_PROT, cons_obj.r_data_buf, sizeof(cons_obj.r_data_buf));
if (r_len > 0)
while (1)
{
pthread_spin_lock(&cons_obj.r_lock);
for (int i = 0; i < r_len; i++)
int r_len = ulog_read_bytes(LOG_PROT, cons_obj.r_data_buf, sizeof(cons_obj.r_data_buf));
if (r_len > 0)
{
q_enqueue(&cons_obj.r_queue, cons_obj.r_data_buf[i]);
pthread_spin_lock(&cons_obj.r_lock);
for (int i = 0; i < r_len; i++)
{
q_enqueue(&cons_obj.r_queue, cons_obj.r_data_buf[i]);
}
pthread_spin_unlock(&cons_obj.r_lock);
}
pthread_spin_unlock(&cons_obj.r_lock);
}
handler_free_umap(cons_obj.hd_cons_read);
while (1)
@@ -40,6 +48,8 @@ void console_init(void)
{
cons_svr_obj_init(&cons_obj);
meta_reg_svr_obj(&cons_obj.svr, CONS_PROT);
u_thread_create(&cons_th, cons_stack, sizeof(cons_stack), cons_msg_buf, console_read_func);
u_thread_run(cons_th, 2);
printf("cons svr init...\n");
}
/**
@@ -82,7 +92,7 @@ int console_read(uint8_t *data, size_t len)
if (q_queue_len(&cons_obj.r_queue) == 0)
{
// 回复没有消息
return -ENODATA;
return 0;
}
else
{
@@ -91,7 +101,7 @@ int console_read(uint8_t *data, size_t len)
{
// 回复没有消息
pthread_spin_unlock(&cons_obj.r_lock);
return -ENODATA;
return 0;
}
int i;
for (i = 0; i < q_queue_len(&cons_obj.r_queue) && i < len; i++)