}
通过result = desc->fn();进入,使用的newlib/stdio.c
[indent]int libc_system_init(void)[/indent][indent]{[/indent][indent]#if defined(RT_USING_DFS) & defined(RT_USING_DFS_DEVFS)[/indent][indent] rt_device_t dev_console;
dev_console = rt_console_get_device();[/indent][indent] if (dev_console)[/indent][indent] {[/indent][indent] #if defined(RT_USING_POSIX)[/indent][indent] libc_stdio_set_console(dev_console->parent.name, O_RDWR);[/indent][indent] #else[/indent][indent] libc_stdio_set_console(dev_console->parent.name, O_WRONLY);[/indent][indent] #endif[/indent][indent] }[/indent][indent]#endif[/indent]进入libc_stdio_set_console(dev_console->parent.name, O_RDWR);
[indent]int libc_stdio_set_console(const char* device_name, int mode)[/indent][indent]{[/indent][indent] FILE *fp;[/indent][indent] char name[STDIO_DEVICE_NAME_MAX];[/indent][indent] char *file_mode;
snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);[/indent][indent] name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
if (mode == O_RDWR) file_mode = "r+";[/indent][indent] else if (mode == O_WRONLY) file_mode = "wb";[/indent][indent] else if (mode == O_RDONLY) file_mode = "rb";
fp = fopen(name, file_mode);[/indent][indent] if (fp)[/indent][indent] {[/indent][indent] setvbuf(fp, NULL, _IONBF, 0);[/indent]进入 fopen,然后进入,_fopen_r,这个函数没有c代码,然后进入_open_r, 使用的是newlib/syscall.c
int
[indent]_open_r(struct _reent *ptr, const char *file, int flags, int mode)[/indent][indent]{[/indent][indent]#ifndef RT_USING_DFS[/indent][indent] return 0;[/indent][indent]#else[/indent][indent] int rc;
[/indent][indent] rc = open(file, flags, mode);[/indent][indent] return rc;[/indent][indent]#endif[/indent][indent]}[/indent]进入 open,使用的是dfs_posix.c
[indent]int open(const char *file, int flags, ...)[/indent][indent]{[/indent][indent] int fd, result;[/indent][indent] struct dfs_fd *d;[/indent][indent] /* allocate a fd */[/indent][indent] fd = fd_new();[/indent][indent] if (fd < 0)[/indent][indent] {[/indent][indent] rt_set_errno(-ENOMEM);
return -1;[/indent][indent] }[/indent][indent] d = fd_get(fd);
result = dfs_file_open(d, file, flags);[/indent][indent] if (result < 0)[/indent][indent] {[/indent][indent] /* release the ref-count of fd */[/indent][indent] fd_put(d);[/indent][indent] fd_put(d);
rt_set_errno(result);
return -1;[/indent][indent] }
/* release the ref-count of fd */[/indent][indent] fd_put(d);
return fd;[/indent][indent]}[/indent]进入
int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
[indent]{[/indent][indent] struct dfs_filesystem *fs;[/indent][indent] char *fullpath;[/indent][indent] int result;[/indent][indent] /* parameter check */[/indent][indent] if (fd == NULL)[/indent][indent] return -EINVAL;
/* make sure we have an absolute path */[/indent][indent] fullpath = dfs_normalize_path(NULL, path);[/indent][indent] if (fullpath == NULL)[/indent][indent] {[/indent][indent] return -ENOMEM;[/indent][indent] }
dbg_log(DBG_LOG, "open file:%s
", fullpath);
/* Check whether file is already open */[/indent][indent] if (fd_is_open(fullpath) == 0)[/indent][indent] {[/indent][indent] rt_free(fullpath); /* release path */
return -EBUSY;[/indent][indent] }
/* find filesystem */[/indent][indent] fs = dfs_filesystem_lookup(fullpath);[/indent][indent] if (fs == NULL)[/indent][indent] {[/indent][indent] rt_free(fullpath); /* release path */
return -ENOENT;[/indent][indent] }
dbg_log(DBG_LOG, "open in filesystem:%s
", fs->ops->name);[/indent][indent] fd->fops = fs->ops->fops; /* set file ops */
/* initialize the fd item */[/indent][indent] fd->type = FT_REGULAR;[/indent][indent] fd->flags = flags;[/indent][indent] fd->size = 0;[/indent][indent] fd->pos = 0;[/indent][indent] fd->data = fs;[/indent][indent] if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))[/indent][indent] {[/indent][indent] if (dfs_subdir(fs->path, fullpath) == NULL)[/indent][indent] fd->path = rt_strdup("/");[/indent][indent] else[/indent][indent] fd->path = rt_strdup(dfs_subdir(fs->path, fullpath));[/indent][indent] rt_free(fullpath);[/indent][indent] dbg_log(DBG_LOG, "Actual file path: %s
", fd->path);[/indent][indent] }[/indent][indent] else[/indent][indent] {[/indent][indent] fd->path = fullpath;
}
/* specific file system open routine */[/indent][indent] if (fd->fops->open == NULL)[/indent][indent] {[/indent][indent] /* clear fd */[/indent][indent] rt_free(fd->path);[/indent][indent] fd->path = NULL;
return -ENOSYS;[/indent][indent] }[/indent][indent] if ((result = fd->fops->open(fd)) < 0)[/indent][indent] {[/indent][indent] /* clear fd */[/indent][indent] rt_free(fd->path);[/indent]进入
[indent]static int serial_fops_open(struct dfs_fd *fd)[/indent][indent]{[/indent][indent] rt_err_t ret = 0;[/indent][indent] rt_uint16_t flags = 0;[/indent][indent] rt_device_t device;
device = (rt_device_t)fd->data;[/indent][indent] RT_ASSERT(device != RT_NULL);
switch (fd->flags & O_ACCMODE)[/indent][indent] {[/indent][indent] case O_RDONLY:[/indent][indent] dbg_log(DBG_LOG, "fops open: O_RDONLY!
");[/indent][indent] flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDONLY;[/indent][indent] break;[/indent][indent] case O_WRONLY:[/indent][indent] dbg_log(DBG_LOG, "fops open: O_WRONLY!
");[/indent][indent] flags = RT_DEVICE_FLAG_WRONLY;[/indent][indent] break;[/indent][indent] case O_RDWR:[/indent][indent] dbg_log(DBG_LOG, "fops open: O_RDWR!
");[/indent][indent] flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDWR;[/indent][indent] break;[/indent][indent] default:[/indent][indent] dbg_log(DBG_ERROR, "fops open: unknown mode - %d!
", fd->flags & O_ACCMODE);[/indent][indent] break;[/indent][indent] }
if ((fd->flags & O_ACCMODE) != O_WRONLY)[/indent][indent] rt_device_set_rx_indicate(device, serial_fops_rx_ind);[/indent][indent] ret = rt_device_open(device, flags);[/indent][indent] if (ret == RT_EOK) return 0;[/indent][indent] return ret;[/indent][indent]}[/indent]进入
[indent]rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)[/indent][indent]{[/indent][indent] rt_err_t result = RT_EOK;[/indent][indent] RT_ASSERT(dev != RT_NULL);[/indent][indent] /* if device is not initialized, initialize it. */[/indent][indent] if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))[/indent][indent] {[/indent][indent] if (dev->init != RT_NULL)[/indent][indent] {[/indent][indent] result = dev->init(dev);[/indent][indent] if (result != RT_EOK)[/indent][indent] {[/indent][indent] rt_kprintf("To initialize device:%s failed. The error code is %d
",[/indent][indent] dev->parent.name, result);[/indent][indent] return result;[/indent][indent] }[/indent][indent] }[/indent][indent]
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;[/indent][indent] }[/indent][indent]
/* device is a stand alone device and opened */[/indent][indent] if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&[/indent][indent] (dev->open_flag & RT_DEVICE_OFLAG_OPEN))[/indent][indent] {[/indent][indent] return -RT_EBUSY;[/indent][indent] }[/indent][indent] /* call device open interface */[/indent][indent] if (dev->open != RT_NULL)[/indent][indent] {[/indent][indent] result = dev->open(dev, oflag);[/indent]进入
[indent]static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)[/indent][indent] rt_uint16_t stream_flag = 0;[/indent][indent] struct rt_serial_device *serial;[/indent][indent]
RT_ASSERT(dev != RT_NULL);[/indent][indent] serial = (struct rt_serial_device *)dev;[/indent][indent]
dbg_log(DBG_LOG, "open serial device: 0x%08x with open flag: 0x%04x
",[/indent][indent] dev, oflag);[/indent][indent] /* check device flag with the open flag */[/indent][indent] if ((oflag & RT_DEVICE_FLAG_DMA_RX) && !(dev->flag & RT_DEVICE_FLAG_DMA_RX))[/indent][indent] return -RT_EIO;[/indent][indent] if ((oflag & RT_DEVICE_FLAG_DMA_TX) && !(dev->flag & RT_DEVICE_FLAG_DMA_TX))[/indent][indent] return -RT_EIO;[/indent][indent] if ((oflag & RT_DEVICE_FLAG_INT_RX) && !(dev->flag & RT_DEVICE_FLAG_INT_RX))[/indent][indent] return -RT_EIO;[/indent][indent] if ((oflag & RT_DEVICE_FLAG_INT_TX) && !(dev->flag & RT_DEVICE_FLAG_INT_TX))[/indent][indent] return -RT_EIO;
/* keep steam flag */[/indent][indent] if ((oflag & RT_DEVICE_FLAG_STREAM) || (dev->open_flag & RT_DEVICE_FLAG_STREAM))[/indent][indent] stream_flag = RT_DEVICE_FLAG_STREAM;[/indent]在此处返回 -RT_EIO, oflag的值为 0x103, dev->flag的值为 0xa13,无论怎么修改libc_stdio_set_console(dev_console->parent.name, O_RDWR);的参数,这一句都执行不过去。感觉是不是个bug啊。
查看更多