first submission
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* File : pin.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2015, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2015-01-20 Bernard the first version
|
||||
* 2017-10-20 ZYH add mode open drain and input pull down
|
||||
*/
|
||||
|
||||
#ifndef PIN_H__
|
||||
#define PIN_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* pin device and operations for RT-Thread */
|
||||
struct rt_device_pin
|
||||
{
|
||||
struct rt_device parent;
|
||||
const struct rt_pin_ops *ops;
|
||||
};
|
||||
|
||||
#define PIN_LOW 0x00
|
||||
#define PIN_HIGH 0x01
|
||||
|
||||
#define PIN_MODE_OUTPUT 0x00
|
||||
#define PIN_MODE_INPUT 0x01
|
||||
#define PIN_MODE_INPUT_PULLUP 0x02
|
||||
#define PIN_MODE_INPUT_PULLDOWN 0x03
|
||||
#define PIN_MODE_OUTPUT_OD 0x04
|
||||
|
||||
#define PIN_MODE_INPUT_AIN 0x05
|
||||
#define PIN_MODE_OUTPUT_AF_OD 0x06
|
||||
#define PIN_MODE_OUTPUT_AF_PP 0x07
|
||||
|
||||
|
||||
|
||||
#define PIN_IRQ_MODE_RISING 0x00
|
||||
#define PIN_IRQ_MODE_FALLING 0x01
|
||||
#define PIN_IRQ_MODE_RISING_FALLING 0x02
|
||||
#define PIN_IRQ_MODE_HIGH_LEVEL 0x03
|
||||
#define PIN_IRQ_MODE_LOW_LEVEL 0x04
|
||||
|
||||
#define PIN_IRQ_DISABLE 0x00
|
||||
#define PIN_IRQ_ENABLE 0x01
|
||||
|
||||
#define PIN_IRQ_PIN_NONE -1
|
||||
|
||||
struct rt_device_pin_mode
|
||||
{
|
||||
rt_uint16_t pin;
|
||||
rt_uint16_t mode;
|
||||
};
|
||||
struct rt_device_pin_status
|
||||
{
|
||||
rt_uint16_t pin;
|
||||
rt_uint16_t status;
|
||||
};
|
||||
struct rt_pin_irq_hdr
|
||||
{
|
||||
rt_int16_t pin;
|
||||
rt_uint16_t mode;
|
||||
void (*hdr)(void *args);
|
||||
void *args;
|
||||
};
|
||||
struct rt_pin_ops
|
||||
{
|
||||
void (*pin_mode)(struct rt_device *device, rt_base_t pin, rt_base_t mode);
|
||||
void (*pin_write)(struct rt_device *device, rt_base_t pin, rt_base_t value);
|
||||
int (*pin_read)(struct rt_device *device, rt_base_t pin);
|
||||
|
||||
/* TODO: add GPIO interrupt */
|
||||
rt_err_t (*pin_attach_irq)(struct rt_device *device, rt_int32_t pin,
|
||||
rt_uint32_t mode, void (*hdr)(void *args), void *args);
|
||||
rt_err_t (*pin_dettach_irq)(struct rt_device *device, rt_int32_t pin);
|
||||
rt_err_t (*pin_irq_enable)(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled);
|
||||
};
|
||||
|
||||
int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data);
|
||||
|
||||
void rt_pin_mode(rt_base_t pin, rt_base_t mode);
|
||||
void rt_pin_write(rt_base_t pin, rt_base_t value);
|
||||
int rt_pin_read(rt_base_t pin);
|
||||
rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode,
|
||||
void (*hdr)(void *args), void *args);
|
||||
rt_err_t rt_pin_dettach_irq(rt_int32_t pin);
|
||||
rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled);
|
||||
|
||||
int rt_device_pin_irq_register(const char *name, const struct rt_pin_ops *ops,
|
||||
void *user_data);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
* File : serial.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2012-05-15 lgnq first version.
|
||||
* 2012-05-28 bernard change interfaces
|
||||
* 2013-02-20 bernard use RT_SERIAL_RB_BUFSZ to define
|
||||
* the size of ring buffer.
|
||||
*/
|
||||
|
||||
#ifndef __SERIAL_H__
|
||||
#define __SERIAL_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#define BAUD_RATE_2400 2400
|
||||
#define BAUD_RATE_4800 4800
|
||||
#define BAUD_RATE_9600 9600
|
||||
#define BAUD_RATE_19200 19200
|
||||
#define BAUD_RATE_38400 38400
|
||||
#define BAUD_RATE_57600 57600
|
||||
#define BAUD_RATE_115200 115200
|
||||
#define BAUD_RATE_230400 230400
|
||||
#define BAUD_RATE_460800 460800
|
||||
#define BAUD_RATE_921600 921600
|
||||
#define BAUD_RATE_2000000 2000000
|
||||
#define BAUD_RATE_3000000 3000000
|
||||
|
||||
#define DATA_BITS_5 5
|
||||
#define DATA_BITS_6 6
|
||||
#define DATA_BITS_7 7
|
||||
#define DATA_BITS_8 8
|
||||
#define DATA_BITS_9 9
|
||||
|
||||
#define STOP_BITS_1 0
|
||||
#define STOP_BITS_2 1
|
||||
#define STOP_BITS_3 2
|
||||
#define STOP_BITS_4 3
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#define PARITY_NONE 0
|
||||
#define PARITY_ODD 1
|
||||
#define PARITY_EVEN 2
|
||||
#endif
|
||||
|
||||
#define BIT_ORDER_LSB 0
|
||||
#define BIT_ORDER_MSB 1
|
||||
|
||||
#define NRZ_NORMAL 0 /* Non Return to Zero : normal mode */
|
||||
#define NRZ_INVERTED 1 /* Non Return to Zero : inverted mode */
|
||||
|
||||
#ifndef RT_SERIAL_RB_BUFSZ
|
||||
#define RT_SERIAL_RB_BUFSZ 64
|
||||
#endif
|
||||
|
||||
#define RT_SERIAL_EVENT_RX_IND 0x01 /* Rx indication */
|
||||
#define RT_SERIAL_EVENT_TX_DONE 0x02 /* Tx complete */
|
||||
#define RT_SERIAL_EVENT_RX_DMADONE 0x03 /* Rx DMA transfer done */
|
||||
#define RT_SERIAL_EVENT_TX_DMADONE 0x04 /* Tx DMA transfer done */
|
||||
#define RT_SERIAL_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
|
||||
|
||||
#define RT_SERIAL_DMA_RX 0x01
|
||||
#define RT_SERIAL_DMA_TX 0x02
|
||||
|
||||
#define RT_SERIAL_RX_INT 0x01
|
||||
#define RT_SERIAL_TX_INT 0x02
|
||||
|
||||
#define RT_SERIAL_ERR_OVERRUN 0x01
|
||||
#define RT_SERIAL_ERR_FRAMING 0x02
|
||||
#define RT_SERIAL_ERR_PARITY 0x03
|
||||
|
||||
#define RT_SERIAL_TX_DATAQUEUE_SIZE 2048
|
||||
#define RT_SERIAL_TX_DATAQUEUE_LWM 30
|
||||
|
||||
/* Default config for serial_configure structure */
|
||||
#define RT_SERIAL_CONFIG_DEFAULT \
|
||||
{ \
|
||||
BAUD_RATE_115200, /* 115200 bits/s */ \
|
||||
DATA_BITS_8, /* 8 databits */ \
|
||||
STOP_BITS_1, /* 1 stopbit */ \
|
||||
PARITY_NONE, /* No parity */ \
|
||||
BIT_ORDER_LSB, /* LSB first sent */ \
|
||||
NRZ_NORMAL, /* Normal mode */ \
|
||||
RT_SERIAL_RB_BUFSZ, /* Buffer size */ \
|
||||
0 \
|
||||
}
|
||||
|
||||
struct serial_configure
|
||||
{
|
||||
rt_uint32_t baud_rate;
|
||||
|
||||
rt_uint32_t data_bits :4;
|
||||
rt_uint32_t stop_bits :2;
|
||||
rt_uint32_t parity :2;
|
||||
rt_uint32_t bit_order :1;
|
||||
rt_uint32_t invert :1;
|
||||
rt_uint32_t bufsz :16;
|
||||
rt_uint32_t reserved :4;
|
||||
};
|
||||
|
||||
/*
|
||||
* Serial FIFO mode
|
||||
*/
|
||||
struct rt_serial_rx_fifo
|
||||
{
|
||||
/* software fifo */
|
||||
rt_uint8_t *buffer;
|
||||
|
||||
rt_uint16_t put_index, get_index;
|
||||
|
||||
rt_bool_t is_full;
|
||||
};
|
||||
|
||||
struct rt_serial_tx_fifo
|
||||
{
|
||||
struct rt_completion completion;
|
||||
};
|
||||
|
||||
/*
|
||||
* Serial DMA mode
|
||||
*/
|
||||
struct rt_serial_rx_dma
|
||||
{
|
||||
rt_bool_t activated;
|
||||
};
|
||||
|
||||
struct rt_serial_tx_dma
|
||||
{
|
||||
rt_bool_t activated;
|
||||
struct rt_data_queue data_queue;
|
||||
};
|
||||
|
||||
|
||||
struct rt_serial_device
|
||||
{
|
||||
struct rt_device parent;
|
||||
|
||||
const struct rt_uart_ops *ops;
|
||||
struct serial_configure config;
|
||||
|
||||
void *serial_rx;
|
||||
void *serial_tx;
|
||||
};
|
||||
typedef struct rt_serial_device rt_serial_t;
|
||||
|
||||
|
||||
/**
|
||||
* uart operators
|
||||
*/
|
||||
struct rt_uart_ops
|
||||
{
|
||||
rt_err_t (*configure)(struct rt_serial_device *serial, struct serial_configure *cfg);
|
||||
rt_err_t (*control)(struct rt_serial_device *serial, int cmd, void *arg);
|
||||
|
||||
int (*putc)(struct rt_serial_device *serial, char c);
|
||||
int (*getc)(struct rt_serial_device *serial);
|
||||
|
||||
rt_size_t (*dma_transmit)(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction);
|
||||
};
|
||||
|
||||
|
||||
void rt_hw_serial_isr(struct rt_serial_device *serial, int event);
|
||||
|
||||
rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
|
||||
const char *name,
|
||||
rt_uint32_t flag,
|
||||
void *data);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef COMPLETION_H_
|
||||
#define COMPLETION_H_
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
/**
|
||||
* Completion
|
||||
*/
|
||||
|
||||
struct rt_completion
|
||||
{
|
||||
rt_uint32_t flag;
|
||||
|
||||
/* suspended list */
|
||||
rt_list_t suspended_list;
|
||||
};
|
||||
|
||||
void rt_completion_init(struct rt_completion *completion);
|
||||
rt_err_t rt_completion_wait(struct rt_completion *completion,
|
||||
rt_int32_t timeout);
|
||||
void rt_completion_done(struct rt_completion *completion);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef DATAQUEUE_H__
|
||||
#define DATAQUEUE_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#define RT_DATAQUEUE_EVENT_UNKNOWN 0x00
|
||||
#define RT_DATAQUEUE_EVENT_POP 0x01
|
||||
#define RT_DATAQUEUE_EVENT_PUSH 0x02
|
||||
#define RT_DATAQUEUE_EVENT_LWM 0x03
|
||||
|
||||
struct rt_data_item;
|
||||
#define RT_DATAQUEUE_SIZE(dq) ((dq)->put_index - (dq)->get_index)
|
||||
#define RT_DATAQUEUE_EMPTY(dq) ((dq)->size - RT_DATAQUEUE_SIZE(dq))
|
||||
/* data queue implementation */
|
||||
struct rt_data_queue
|
||||
{
|
||||
rt_uint16_t size;
|
||||
rt_uint16_t lwm;
|
||||
rt_bool_t waiting_lwm;
|
||||
|
||||
rt_uint16_t get_index;
|
||||
rt_uint16_t put_index;
|
||||
|
||||
struct rt_data_item *queue;
|
||||
|
||||
rt_list_t suspended_push_list;
|
||||
rt_list_t suspended_pop_list;
|
||||
|
||||
/* event notify */
|
||||
void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event);
|
||||
};
|
||||
|
||||
/**
|
||||
* DataQueue for DeviceDriver
|
||||
*/
|
||||
rt_err_t rt_data_queue_init(struct rt_data_queue *queue,
|
||||
rt_uint16_t size,
|
||||
rt_uint16_t lwm,
|
||||
void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event));
|
||||
rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
|
||||
const void *data_ptr,
|
||||
rt_size_t data_size,
|
||||
rt_int32_t timeout);
|
||||
rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
|
||||
const void **data_ptr,
|
||||
rt_size_t *size,
|
||||
rt_int32_t timeout);
|
||||
rt_err_t rt_data_queue_peak(struct rt_data_queue *queue,
|
||||
const void **data_ptr,
|
||||
rt_size_t *size);
|
||||
void rt_data_queue_reset(struct rt_data_queue *queue);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef PIPE_H__
|
||||
#define PIPE_H__
|
||||
|
||||
/**
|
||||
* Pipe Device
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#ifndef RT_PIPE_BUFSZ
|
||||
#define PIPE_BUFSZ 512
|
||||
#else
|
||||
#define PIPE_BUFSZ RT_PIPE_BUFSZ
|
||||
#endif
|
||||
|
||||
struct rt_pipe_device
|
||||
{
|
||||
struct rt_device parent;
|
||||
|
||||
/* ring buffer in pipe device */
|
||||
struct rt_ringbuffer *fifo;
|
||||
rt_uint16_t bufsz;
|
||||
|
||||
rt_uint8_t readers;
|
||||
rt_uint8_t writers;
|
||||
|
||||
rt_wqueue_t reader_queue;
|
||||
rt_wqueue_t writer_queue;
|
||||
|
||||
struct rt_mutex lock;
|
||||
};
|
||||
typedef struct rt_pipe_device rt_pipe_t;
|
||||
|
||||
rt_pipe_t *rt_pipe_create(const char *name, int bufsz);
|
||||
int rt_pipe_delete(const char *name);
|
||||
#endif /* PIPE_H__ */
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* File : poll.h
|
||||
* This file is part of Device File System in RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006-2017, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2016-09-19 Heyuanjie The first version.
|
||||
* 2016-12-26 Bernard Update poll interface
|
||||
*/
|
||||
#ifndef IPC_POLL_H__
|
||||
#define IPC_POLL_H__
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct rt_pollreq;
|
||||
typedef void (*poll_queue_proc)(rt_wqueue_t *, struct rt_pollreq *);
|
||||
|
||||
typedef struct rt_pollreq
|
||||
{
|
||||
poll_queue_proc _proc;
|
||||
short _key;
|
||||
} rt_pollreq_t;
|
||||
|
||||
rt_inline void rt_poll_add(rt_wqueue_t *wq, rt_pollreq_t *req)
|
||||
{
|
||||
if (req && req->_proc && wq)
|
||||
{
|
||||
req->_proc(wq, req);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,88 @@
|
||||
#ifndef RINGBUFFER_H__
|
||||
#define RINGBUFFER_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
/* ring buffer */
|
||||
struct rt_ringbuffer
|
||||
{
|
||||
rt_uint8_t *buffer_ptr;
|
||||
/* use the msb of the {read,write}_index as mirror bit. You can see this as
|
||||
* if the buffer adds a virtual mirror and the pointers point either to the
|
||||
* normal or to the mirrored buffer. If the write_index has the same value
|
||||
* with the read_index, but in a different mirror, the buffer is full.
|
||||
* While if the write_index and the read_index are the same and within the
|
||||
* same mirror, the buffer is empty. The ASCII art of the ringbuffer is:
|
||||
*
|
||||
* mirror = 0 mirror = 1
|
||||
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
||||
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Full
|
||||
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
||||
* read_idx-^ write_idx-^
|
||||
*
|
||||
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
||||
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Empty
|
||||
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
||||
* read_idx-^ ^-write_idx
|
||||
*
|
||||
* The tradeoff is we could only use 32KiB of buffer for 16 bit of index.
|
||||
* But it should be enough for most of the cases.
|
||||
*
|
||||
* Ref: http://en.wikipedia.org/wiki/Circular_buffer#Mirroring */
|
||||
rt_uint16_t read_mirror : 1;
|
||||
rt_uint16_t read_index : 15;
|
||||
rt_uint16_t write_mirror : 1;
|
||||
rt_uint16_t write_index : 15;
|
||||
/* as we use msb of index as mirror bit, the size should be signed and
|
||||
* could only be positive. */
|
||||
rt_int16_t buffer_size;
|
||||
};
|
||||
|
||||
enum rt_ringbuffer_state
|
||||
{
|
||||
RT_RINGBUFFER_EMPTY,
|
||||
RT_RINGBUFFER_FULL,
|
||||
/* half full is neither full nor empty */
|
||||
RT_RINGBUFFER_HALFFULL,
|
||||
};
|
||||
|
||||
/**
|
||||
* RingBuffer for DeviceDriver
|
||||
*
|
||||
* Please note that the ring buffer implementation of RT-Thread
|
||||
* has no thread wait or resume feature.
|
||||
*/
|
||||
void rt_ringbuffer_init(struct rt_ringbuffer *rb, rt_uint8_t *pool, rt_int16_t size);
|
||||
void rt_ringbuffer_reset(struct rt_ringbuffer *rb);
|
||||
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length);
|
||||
rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length);
|
||||
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch);
|
||||
rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch);
|
||||
rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, rt_uint8_t *ptr, rt_uint16_t length);
|
||||
rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch);
|
||||
rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb);
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
struct rt_ringbuffer* rt_ringbuffer_create(rt_uint16_t length);
|
||||
void rt_ringbuffer_destroy(struct rt_ringbuffer *rb);
|
||||
#endif
|
||||
|
||||
rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
|
||||
{
|
||||
RT_ASSERT(rb != RT_NULL);
|
||||
return rb->buffer_size;
|
||||
}
|
||||
|
||||
/** return the size of empty space in rb */
|
||||
#define rt_ringbuffer_space_len(rb) ((rb)->buffer_size - rt_ringbuffer_data_len(rb))
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* File : waitqueue.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018/06/26 Bernard Fix the wait queue issue when wakeup a soon
|
||||
* to blocked thread.
|
||||
*/
|
||||
|
||||
#ifndef WAITQUEUE_H__
|
||||
#define WAITQUEUE_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#define RT_WQ_FLAG_CLEAN 0x00
|
||||
#define RT_WQ_FLAG_WAKEUP 0x01
|
||||
|
||||
struct rt_wqueue_node;
|
||||
typedef int (*rt_wqueue_func_t)(struct rt_wqueue_node *wait, void *key);
|
||||
|
||||
struct rt_wqueue_node
|
||||
{
|
||||
rt_thread_t polling_thread;
|
||||
rt_list_t list;
|
||||
|
||||
rt_wqueue_func_t wakeup;
|
||||
rt_uint32_t key;
|
||||
};
|
||||
typedef struct rt_wqueue_node rt_wqueue_node_t;
|
||||
|
||||
int __wqueue_default_wake(struct rt_wqueue_node *wait, void *key);
|
||||
|
||||
rt_inline void rt_wqueue_init(rt_wqueue_t *queue)
|
||||
{
|
||||
RT_ASSERT(queue != RT_NULL);
|
||||
|
||||
queue->flag = RT_WQ_FLAG_CLEAN;
|
||||
rt_list_init(&(queue->waiting_list));
|
||||
}
|
||||
|
||||
void rt_wqueue_add(rt_wqueue_t *queue, struct rt_wqueue_node *node);
|
||||
void rt_wqueue_remove(struct rt_wqueue_node *node);
|
||||
int rt_wqueue_wait(rt_wqueue_t *queue, int condition, int timeout);
|
||||
void rt_wqueue_wakeup(rt_wqueue_t *queue, void *key);
|
||||
|
||||
#define DEFINE_WAIT_FUNC(name, function) \
|
||||
struct rt_wqueue_node name = { \
|
||||
rt_current_thread, \
|
||||
RT_LIST_OBJECT_INIT(((name).list)), \
|
||||
\
|
||||
function, \
|
||||
0 \
|
||||
}
|
||||
|
||||
#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, __wqueue_default_wake)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,44 @@
|
||||
#ifndef WORKQUEUE_H__
|
||||
#define WORKQUEUE_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
/* workqueue implementation */
|
||||
struct rt_workqueue
|
||||
{
|
||||
rt_list_t work_list;
|
||||
struct rt_work *work_current; /* current work */
|
||||
|
||||
struct rt_semaphore sem;
|
||||
rt_thread_t work_thread;
|
||||
};
|
||||
|
||||
struct rt_work
|
||||
{
|
||||
rt_list_t list;
|
||||
|
||||
void (*work_func)(struct rt_work* work, void* work_data);
|
||||
void *work_data;
|
||||
};
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
/**
|
||||
* WorkQueue for DeviceDriver
|
||||
*/
|
||||
struct rt_workqueue *rt_workqueue_create(const char* name, rt_uint16_t stack_size, rt_uint8_t priority);
|
||||
rt_err_t rt_workqueue_destroy(struct rt_workqueue* queue);
|
||||
rt_err_t rt_workqueue_dowork(struct rt_workqueue* queue, struct rt_work* work);
|
||||
rt_err_t rt_workqueue_cancel_work(struct rt_workqueue* queue, struct rt_work* work);
|
||||
rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue* queue, struct rt_work* work);
|
||||
|
||||
rt_inline void rt_work_init(struct rt_work* work, void (*work_func)(struct rt_work* work, void* work_data),
|
||||
void* work_data)
|
||||
{
|
||||
rt_list_init(&(work->list));
|
||||
work->work_func = work_func;
|
||||
work->work_data = work_data;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* File : rtdevice.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2012-01-08 bernard first version.
|
||||
* 2014-07-12 bernard Add workqueue implementation.
|
||||
*/
|
||||
|
||||
#ifndef __RT_DEVICE_H__
|
||||
#define __RT_DEVICE_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#include "ipc/ringbuffer.h"
|
||||
#include "ipc/completion.h"
|
||||
#include "ipc/dataqueue.h"
|
||||
#include "ipc/workqueue.h"
|
||||
#include "ipc/waitqueue.h"
|
||||
#include "ipc/pipe.h"
|
||||
#include "ipc/poll.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define RT_DEVICE(device) ((rt_device_t)device)
|
||||
|
||||
#ifdef RT_USING_RTC
|
||||
#include "drivers/rtc.h"
|
||||
#ifdef RT_USING_ALARM
|
||||
#include "drivers/alarm.h"
|
||||
#endif
|
||||
#endif /* RT_USING_RTC */
|
||||
|
||||
#ifdef RT_USING_SPI
|
||||
#include "drivers/spi.h"
|
||||
#endif /* RT_USING_SPI */
|
||||
|
||||
#ifdef RT_USING_MTD_NOR
|
||||
#include "drivers/mtd_nor.h"
|
||||
#endif /* RT_USING_MTD_NOR */
|
||||
|
||||
#ifdef RT_USING_MTD_NAND
|
||||
#include "drivers/mtd_nand.h"
|
||||
#endif /* RT_USING_MTD_NAND */
|
||||
|
||||
#ifdef RT_USING_USB_DEVICE
|
||||
#include "drivers/usb_device.h"
|
||||
#endif /* RT_USING_USB_DEVICE */
|
||||
|
||||
#ifdef RT_USING_USB_HOST
|
||||
#include "drivers/usb_host.h"
|
||||
#endif /* RT_USING_USB_HOST */
|
||||
|
||||
#ifdef RT_USING_SERIAL
|
||||
#include "drivers/serial.h"
|
||||
#endif /* RT_USING_SERIAL */
|
||||
|
||||
#ifdef RT_USING_I2C
|
||||
#include "drivers/i2c.h"
|
||||
#include "drivers/i2c_dev.h"
|
||||
|
||||
#ifdef RT_USING_I2C_BITOPS
|
||||
#include "drivers/i2c-bit-ops.h"
|
||||
#endif /* RT_USING_I2C_BITOPS */
|
||||
#endif /* RT_USING_I2C */
|
||||
|
||||
#ifdef RT_USING_SDIO
|
||||
#include "drivers/mmcsd_core.h"
|
||||
#include "drivers/sd.h"
|
||||
#include "drivers/sdio.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_WDT
|
||||
#include "drivers/watchdog.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_PIN
|
||||
#include "drivers/pin.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_CAN
|
||||
#include "drivers/can.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_HWTIMER
|
||||
#include "drivers/hwtimer.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_AUDIO
|
||||
#include "drivers/audio.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_CPUTIME
|
||||
#include "drivers/cputime.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_PWM
|
||||
#include "drivers/rt_drv_pwm.h"
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __RT_DEVICE_H__ */
|
||||
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* File : pin.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2015, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2015-01-20 Bernard the first version
|
||||
*/
|
||||
|
||||
#include <drivers/pin.h>
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
#endif
|
||||
|
||||
static struct rt_device_pin _hw_pin;
|
||||
static rt_size_t _pin_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
|
||||
{
|
||||
struct rt_device_pin_status *status;
|
||||
struct rt_device_pin *pin = (struct rt_device_pin *)dev;
|
||||
|
||||
/* check parameters */
|
||||
RT_ASSERT(pin != RT_NULL);
|
||||
|
||||
status = (struct rt_device_pin_status *) buffer;
|
||||
if (status == RT_NULL || size != sizeof(*status)) return 0;
|
||||
|
||||
status->status = pin->ops->pin_read(dev, status->pin);
|
||||
return size;
|
||||
}
|
||||
|
||||
static rt_size_t _pin_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
|
||||
{
|
||||
struct rt_device_pin_status *status;
|
||||
struct rt_device_pin *pin = (struct rt_device_pin *)dev;
|
||||
|
||||
/* check parameters */
|
||||
RT_ASSERT(pin != RT_NULL);
|
||||
|
||||
status = (struct rt_device_pin_status *) buffer;
|
||||
if (status == RT_NULL || size != sizeof(*status)) return 0;
|
||||
|
||||
pin->ops->pin_write(dev, (rt_base_t)status->pin, (rt_base_t)status->status);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
static rt_err_t _pin_control(rt_device_t dev, int cmd, void *args)
|
||||
{
|
||||
struct rt_device_pin_mode *mode;
|
||||
struct rt_device_pin *pin = (struct rt_device_pin *)dev;
|
||||
|
||||
/* check parameters */
|
||||
RT_ASSERT(pin != RT_NULL);
|
||||
|
||||
mode = (struct rt_device_pin_mode *) args;
|
||||
if (mode == RT_NULL) return -RT_ERROR;
|
||||
|
||||
pin->ops->pin_mode(dev, (rt_base_t)mode->pin, (rt_base_t)mode->mode);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data)
|
||||
{
|
||||
_hw_pin.parent.type = RT_Device_Class_Miscellaneous;
|
||||
_hw_pin.parent.rx_indicate = RT_NULL;
|
||||
_hw_pin.parent.tx_complete = RT_NULL;
|
||||
|
||||
_hw_pin.parent.init = RT_NULL;
|
||||
_hw_pin.parent.open = RT_NULL;
|
||||
_hw_pin.parent.close = RT_NULL;
|
||||
_hw_pin.parent.read = _pin_read;
|
||||
_hw_pin.parent.write = _pin_write;
|
||||
_hw_pin.parent.control = _pin_control;
|
||||
|
||||
_hw_pin.ops = ops;
|
||||
_hw_pin.parent.user_data = user_data;
|
||||
|
||||
/* register a character device */
|
||||
rt_device_register(&_hw_pin.parent, name, RT_DEVICE_FLAG_RDWR);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode,
|
||||
void (*hdr)(void *args), void *args)
|
||||
{
|
||||
RT_ASSERT(_hw_pin.ops != RT_NULL);
|
||||
if(_hw_pin.ops->pin_attach_irq)
|
||||
{
|
||||
return _hw_pin.ops->pin_attach_irq(&_hw_pin.parent, pin, mode, hdr, args);
|
||||
}
|
||||
return RT_ENOSYS;
|
||||
}
|
||||
rt_err_t rt_pin_dettach_irq(rt_int32_t pin)
|
||||
{
|
||||
RT_ASSERT(_hw_pin.ops != RT_NULL);
|
||||
if(_hw_pin.ops->pin_dettach_irq)
|
||||
{
|
||||
return _hw_pin.ops->pin_dettach_irq(&_hw_pin.parent, pin);
|
||||
}
|
||||
return RT_ENOSYS;
|
||||
}
|
||||
rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled)
|
||||
{
|
||||
RT_ASSERT(_hw_pin.ops != RT_NULL);
|
||||
if(_hw_pin.ops->pin_irq_enable)
|
||||
{
|
||||
return _hw_pin.ops->pin_irq_enable(&_hw_pin.parent, pin, enabled);
|
||||
}
|
||||
return RT_ENOSYS;
|
||||
}
|
||||
/* RT-Thread Hardware PIN APIs */
|
||||
void rt_pin_mode(rt_base_t pin, rt_base_t mode)
|
||||
{
|
||||
RT_ASSERT(_hw_pin.ops != RT_NULL);
|
||||
_hw_pin.ops->pin_mode(&_hw_pin.parent, pin, mode);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_mode, pinMode, set hardware pin mode);
|
||||
|
||||
void rt_pin_write(rt_base_t pin, rt_base_t value)
|
||||
{
|
||||
RT_ASSERT(_hw_pin.ops != RT_NULL);
|
||||
_hw_pin.ops->pin_write(&_hw_pin.parent, pin, value);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_write, pinWrite, write value to hardware pin);
|
||||
|
||||
int rt_pin_read(rt_base_t pin)
|
||||
{
|
||||
RT_ASSERT(_hw_pin.ops != RT_NULL);
|
||||
return _hw_pin.ops->pin_read(&_hw_pin.parent, pin);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_read, pinRead, read status from hardware pin);
|
||||
@@ -0,0 +1,444 @@
|
||||
/*
|
||||
* File : serial.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2006-03-13 bernard first version
|
||||
* 2012-05-15 lgnq modified according bernard's implementation.
|
||||
* 2012-05-28 bernard code cleanup
|
||||
* 2012-11-23 bernard fix compiler warning.
|
||||
* 2013-02-20 bernard use RT_SERIAL_RB_BUFSZ to define
|
||||
* the size of ring buffer.
|
||||
* 2014-07-10 bernard rewrite serial framework
|
||||
* 2014-12-31 bernard use open_flag for poll_tx stream mode.
|
||||
* 2015-05-19 Quintin fix DMA tx mod tx_dma->activated flag !=RT_FALSE BUG
|
||||
* in open function.
|
||||
* 2015-11-10 bernard fix the poll rx issue when there is no data.
|
||||
* 2016-05-10 armink add fifo mode to DMA rx when serial->config.bufsz != 0.
|
||||
* 2017-01-19 aubr.cool prevent change serial rx bufsz when serial is opened.
|
||||
* 2017-11-07 JasonJia fix data bits error issue when using tcsetattr.
|
||||
* 2017-11-15 JasonJia fix poll rx issue when data is full.
|
||||
* add TCFLSH and FIONREAD support.
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
// #define DEBUG_ENABLE
|
||||
#define DEBUG_LEVEL DBG_LOG
|
||||
#define DBG_SECTION_NAME "[UART]"
|
||||
#define DEBUG_COLOR
|
||||
#include <rtdbg.h>
|
||||
|
||||
/*
|
||||
* Serial poll routines
|
||||
*/
|
||||
rt_inline int _serial_poll_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
|
||||
{
|
||||
int ch;
|
||||
int size;
|
||||
|
||||
RT_ASSERT(serial != RT_NULL);
|
||||
size = length;
|
||||
|
||||
while (length)
|
||||
{
|
||||
ch = serial->ops->getc(serial);
|
||||
if (ch == -1) break;
|
||||
|
||||
*data = ch;
|
||||
data ++; length --;
|
||||
|
||||
if (ch == '\n') break;
|
||||
}
|
||||
|
||||
return size - length;
|
||||
}
|
||||
|
||||
rt_inline int _serial_poll_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
|
||||
{
|
||||
int size;
|
||||
RT_ASSERT(serial != RT_NULL);
|
||||
|
||||
size = length;
|
||||
while (length)
|
||||
{
|
||||
/*
|
||||
* to be polite with serial console add a line feed
|
||||
* to the carriage return character
|
||||
*/
|
||||
if (*data == '\n' && (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
|
||||
{
|
||||
serial->ops->putc(serial, '\r');
|
||||
}
|
||||
|
||||
serial->ops->putc(serial, *data);
|
||||
|
||||
++ data;
|
||||
-- length;
|
||||
}
|
||||
|
||||
return size - length;
|
||||
}
|
||||
|
||||
/*
|
||||
* Serial interrupt routines
|
||||
*/
|
||||
rt_inline int _serial_int_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
|
||||
{
|
||||
int size;
|
||||
struct rt_serial_rx_fifo* rx_fifo;
|
||||
|
||||
RT_ASSERT(serial != RT_NULL);
|
||||
size = length;
|
||||
|
||||
rx_fifo = (struct rt_serial_rx_fifo*) serial->serial_rx;
|
||||
RT_ASSERT(rx_fifo != RT_NULL);
|
||||
|
||||
/* read from software FIFO */
|
||||
while (length)
|
||||
{
|
||||
int ch;
|
||||
rt_base_t level;
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
/* there's no data: */
|
||||
if ((rx_fifo->get_index == rx_fifo->put_index) && (rx_fifo->is_full == RT_FALSE))
|
||||
{
|
||||
/* no data, enable interrupt and break out */
|
||||
rt_hw_interrupt_enable(level);
|
||||
break;
|
||||
}
|
||||
|
||||
/* otherwise there's the data: */
|
||||
ch = rx_fifo->buffer[rx_fifo->get_index];
|
||||
rx_fifo->get_index += 1;
|
||||
if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
|
||||
|
||||
if (rx_fifo->is_full == RT_TRUE)
|
||||
{
|
||||
rx_fifo->is_full = RT_FALSE;
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
*data = ch & 0xff;
|
||||
data ++; length --;
|
||||
}
|
||||
|
||||
return size - length;
|
||||
}
|
||||
|
||||
/* RT-Thread Device Interface */
|
||||
/*
|
||||
* This function initializes serial device.
|
||||
*/
|
||||
static rt_err_t rt_serial_init(struct rt_device *dev)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
struct rt_serial_device *serial;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
serial = (struct rt_serial_device *)dev;
|
||||
|
||||
/* initialize rx/tx */
|
||||
serial->serial_rx = RT_NULL;
|
||||
serial->serial_tx = RT_NULL;
|
||||
|
||||
/* apply configuration */
|
||||
if (serial->ops->configure)
|
||||
result = serial->ops->configure(serial, &serial->config);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
|
||||
{
|
||||
rt_uint16_t stream_flag = 0;
|
||||
struct rt_serial_device *serial;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
serial = (struct rt_serial_device *)dev;
|
||||
|
||||
dbg_log(DBG_LOG, "open serial device: 0x%08x with open flag: 0x%04x\n",
|
||||
dev, oflag);
|
||||
/* check device flag with the open flag */
|
||||
if ((oflag & RT_DEVICE_FLAG_INT_RX) && !(dev->flag & RT_DEVICE_FLAG_INT_RX))
|
||||
return -RT_EIO;
|
||||
if ((oflag & RT_DEVICE_FLAG_INT_TX) && !(dev->flag & RT_DEVICE_FLAG_INT_TX))
|
||||
return -RT_EIO;
|
||||
|
||||
/* keep steam flag */
|
||||
if ((oflag & RT_DEVICE_FLAG_STREAM) || (dev->open_flag & RT_DEVICE_FLAG_STREAM))
|
||||
stream_flag = RT_DEVICE_FLAG_STREAM;
|
||||
|
||||
/* get open flags */
|
||||
dev->open_flag = oflag & 0xff;
|
||||
|
||||
/* initialize the Rx/Tx structure according to open flag */
|
||||
if (serial->serial_rx == RT_NULL)
|
||||
{
|
||||
if (oflag & RT_DEVICE_FLAG_INT_RX)
|
||||
{
|
||||
struct rt_serial_rx_fifo* rx_fifo;
|
||||
|
||||
rx_fifo = (struct rt_serial_rx_fifo*) rt_malloc (sizeof(struct rt_serial_rx_fifo) +
|
||||
serial->config.bufsz);
|
||||
RT_ASSERT(rx_fifo != RT_NULL);
|
||||
rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1);
|
||||
rt_memset(rx_fifo->buffer, 0, serial->config.bufsz);
|
||||
rx_fifo->put_index = 0;
|
||||
rx_fifo->get_index = 0;
|
||||
rx_fifo->is_full = RT_FALSE;
|
||||
|
||||
serial->serial_rx = rx_fifo;
|
||||
dev->open_flag |= RT_DEVICE_FLAG_INT_RX;
|
||||
/* configure low level device */
|
||||
serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
|
||||
}
|
||||
else
|
||||
{
|
||||
serial->serial_rx = RT_NULL;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (oflag & RT_DEVICE_FLAG_INT_RX)
|
||||
dev->open_flag |= RT_DEVICE_FLAG_INT_RX;
|
||||
}
|
||||
|
||||
if (serial->serial_tx == RT_NULL)
|
||||
{
|
||||
|
||||
serial->serial_tx = RT_NULL;
|
||||
}
|
||||
|
||||
/* set stream flag */
|
||||
dev->open_flag |= stream_flag;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t rt_serial_close(struct rt_device *dev)
|
||||
{
|
||||
struct rt_serial_device *serial;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
serial = (struct rt_serial_device *)dev;
|
||||
|
||||
/* this device has more reference count */
|
||||
if (dev->ref_count > 1) return RT_EOK;
|
||||
|
||||
if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
|
||||
{
|
||||
struct rt_serial_rx_fifo* rx_fifo;
|
||||
|
||||
rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
|
||||
RT_ASSERT(rx_fifo != RT_NULL);
|
||||
|
||||
rt_free(rx_fifo);
|
||||
serial->serial_rx = RT_NULL;
|
||||
dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX;
|
||||
/* configure low level device */
|
||||
serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)RT_DEVICE_FLAG_INT_RX);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_size_t rt_serial_read(struct rt_device *dev,
|
||||
rt_off_t pos,
|
||||
void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
struct rt_serial_device *serial;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
if (size == 0) return 0;
|
||||
|
||||
serial = (struct rt_serial_device *)dev;
|
||||
|
||||
if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
|
||||
{
|
||||
return _serial_int_rx(serial, buffer, size);
|
||||
}
|
||||
|
||||
return _serial_poll_rx(serial, buffer, size);
|
||||
}
|
||||
|
||||
static rt_size_t rt_serial_write(struct rt_device *dev,
|
||||
rt_off_t pos,
|
||||
const void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
struct rt_serial_device *serial;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
if (size == 0) return 0;
|
||||
|
||||
serial = (struct rt_serial_device *)dev;
|
||||
|
||||
return _serial_poll_tx(serial, buffer, size);
|
||||
}
|
||||
|
||||
static rt_err_t rt_serial_control(struct rt_device *dev,
|
||||
int cmd,
|
||||
void *args)
|
||||
{
|
||||
rt_err_t ret = RT_EOK;
|
||||
struct rt_serial_device *serial;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
serial = (struct rt_serial_device *)dev;
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case RT_DEVICE_CTRL_SUSPEND:
|
||||
/* suspend device */
|
||||
dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
|
||||
break;
|
||||
|
||||
case RT_DEVICE_CTRL_RESUME:
|
||||
/* resume device */
|
||||
dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
|
||||
break;
|
||||
|
||||
case RT_DEVICE_CTRL_CONFIG:
|
||||
if (args)
|
||||
{
|
||||
struct serial_configure *pconfig = (struct serial_configure *) args;
|
||||
if (pconfig->bufsz != serial->config.bufsz && serial->parent.ref_count)
|
||||
{
|
||||
/*can not change buffer size*/
|
||||
return RT_EBUSY;
|
||||
}
|
||||
/* set serial configure */
|
||||
serial->config = *pconfig;
|
||||
if (serial->parent.ref_count)
|
||||
{
|
||||
/* serial device has been opened, to configure it */
|
||||
serial->ops->configure(serial, (struct serial_configure *) args);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default :
|
||||
/* control device */
|
||||
ret = serial->ops->control(serial, cmd, args);
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* serial register
|
||||
*/
|
||||
rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
|
||||
const char *name,
|
||||
rt_uint32_t flag,
|
||||
void *data)
|
||||
{
|
||||
rt_err_t ret;
|
||||
struct rt_device *device;
|
||||
RT_ASSERT(serial != RT_NULL);
|
||||
|
||||
device = &(serial->parent);
|
||||
|
||||
device->type = RT_Device_Class_Char;
|
||||
device->rx_indicate = RT_NULL;
|
||||
device->tx_complete = RT_NULL;
|
||||
|
||||
device->init = rt_serial_init;
|
||||
device->open = rt_serial_open;
|
||||
device->close = rt_serial_close;
|
||||
device->read = rt_serial_read;
|
||||
device->write = rt_serial_write;
|
||||
device->control = rt_serial_control;
|
||||
device->user_data = data;
|
||||
|
||||
/* register a character device */
|
||||
ret = rt_device_register(device, name, flag);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* ISR for serial interrupt */
|
||||
void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
|
||||
{
|
||||
switch (event & 0xff)
|
||||
{
|
||||
case RT_SERIAL_EVENT_RX_IND:
|
||||
{
|
||||
int ch = -1;
|
||||
rt_base_t level;
|
||||
struct rt_serial_rx_fifo* rx_fifo;
|
||||
|
||||
/* interrupt mode receive */
|
||||
rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
|
||||
RT_ASSERT(rx_fifo != RT_NULL);
|
||||
|
||||
while (1)
|
||||
{
|
||||
ch = serial->ops->getc(serial);
|
||||
if (ch == -1) break;
|
||||
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
rx_fifo->buffer[rx_fifo->put_index] = ch;
|
||||
rx_fifo->put_index += 1;
|
||||
if (rx_fifo->put_index >= serial->config.bufsz) rx_fifo->put_index = 0;
|
||||
|
||||
/* if the next position is read index, discard this 'read char' */
|
||||
if (rx_fifo->put_index == rx_fifo->get_index)
|
||||
{
|
||||
rx_fifo->get_index += 1;
|
||||
rx_fifo->is_full = RT_TRUE;
|
||||
if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
}
|
||||
|
||||
/* invoke callback */
|
||||
if (serial->parent.rx_indicate != RT_NULL)
|
||||
{
|
||||
rt_size_t rx_length;
|
||||
|
||||
/* get rx length */
|
||||
level = rt_hw_interrupt_disable();
|
||||
rx_length = (rx_fifo->put_index >= rx_fifo->get_index)? (rx_fifo->put_index - rx_fifo->get_index):
|
||||
(serial->config.bufsz - (rx_fifo->get_index - rx_fifo->put_index));
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
serial->parent.rx_indicate(&serial->parent, rx_length);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,262 @@
|
||||
/*
|
||||
* File : finsh.h
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#ifndef __FINSH_H__
|
||||
#define __FINSH_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "finsh_api.h"
|
||||
|
||||
/* -- the beginning of option -- */
|
||||
#define FINSH_NAME_MAX 16 /* max length of identifier */
|
||||
#define FINSH_NODE_MAX 16 /* max number of node */
|
||||
|
||||
#define FINSH_HEAP_MAX 128 /* max length of heap */
|
||||
#define FINSH_STRING_MAX 128 /* max length of string */
|
||||
#define FINSH_VARIABLE_MAX 8 /* max number of variable */
|
||||
|
||||
#define FINSH_STACK_MAX 64 /* max stack size */
|
||||
#define FINSH_TEXT_MAX 128 /* max text segment size */
|
||||
|
||||
#define HEAP_ALIGNMENT 4 /* heap alignment */
|
||||
|
||||
#define FINSH_GET16(x) (*(x)) | (*((x)+1) << 8)
|
||||
#define FINSH_GET32(x) (rt_uint32_t)(*(x)) | ((rt_uint32_t)*((x)+1) << 8) | \
|
||||
((rt_uint32_t)*((x)+2) << 16) | ((rt_uint32_t)*((x)+3) << 24)
|
||||
|
||||
#define FINSH_SET16(x, v) \
|
||||
do \
|
||||
{ \
|
||||
*(x) = (v) & 0x00ff; \
|
||||
(*((x)+1)) = (v) >> 8; \
|
||||
} while ( 0 )
|
||||
|
||||
#define FINSH_SET32(x, v) \
|
||||
do \
|
||||
{ \
|
||||
*(x) = (rt_uint32_t)(v) & 0x000000ff; \
|
||||
(*((x)+1)) = ((rt_uint32_t)(v) >> 8) & 0x000000ff; \
|
||||
(*((x)+2)) = ((rt_uint32_t)(v) >> 16) & 0x000000ff; \
|
||||
(*((x)+3)) = ((rt_uint32_t)(v) >> 24); \
|
||||
} while ( 0 )
|
||||
|
||||
/* -- the end of option -- */
|
||||
|
||||
/* std header file */
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#define FINSH_VERSION_MAJOR 1
|
||||
#define FINSH_VERSION_MINOR 0
|
||||
|
||||
/**
|
||||
* @addtogroup finsh
|
||||
*/
|
||||
/*@{*/
|
||||
#define FINSH_ERROR_OK 0 /**< No error */
|
||||
#define FINSH_ERROR_INVALID_TOKEN 1 /**< Invalid token */
|
||||
#define FINSH_ERROR_EXPECT_TYPE 2 /**< Expect a type */
|
||||
#define FINSH_ERROR_UNKNOWN_TYPE 3 /**< Unknown type */
|
||||
#define FINSH_ERROR_VARIABLE_EXIST 4 /**< Variable exist */
|
||||
#define FINSH_ERROR_EXPECT_OPERATOR 5 /**< Expect a operator */
|
||||
#define FINSH_ERROR_MEMORY_FULL 6 /**< Memory full */
|
||||
#define FINSH_ERROR_UNKNOWN_OP 7 /**< Unknown operator */
|
||||
#define FINSH_ERROR_UNKNOWN_NODE 8 /**< Unknown node */
|
||||
#define FINSH_ERROR_EXPECT_CHAR 9 /**< Expect a character */
|
||||
#define FINSH_ERROR_UNEXPECT_END 10 /**< Unexpect end */
|
||||
#define FINSH_ERROR_UNKNOWN_TOKEN 11 /**< Unknown token */
|
||||
#define FINSH_ERROR_NO_FLOAT 12 /**< Float not supported */
|
||||
#define FINSH_ERROR_UNKNOWN_SYMBOL 13 /**< Unknown symbol */
|
||||
#define FINSH_ERROR_NULL_NODE 14 /**< Null node */
|
||||
/*@}*/
|
||||
|
||||
/* system call item */
|
||||
struct finsh_syscall_item
|
||||
{
|
||||
struct finsh_syscall_item* next; /* next item */
|
||||
struct finsh_syscall syscall; /* syscall */
|
||||
};
|
||||
extern struct finsh_syscall_item *global_syscall_list;
|
||||
|
||||
/* system variable table */
|
||||
struct finsh_sysvar
|
||||
{
|
||||
const char* name; /* the name of variable */
|
||||
#if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
|
||||
const char* desc; /* description of system variable */
|
||||
#endif
|
||||
uint8_t type; /* the type of variable */
|
||||
void* var ; /* the address of variable */
|
||||
};
|
||||
|
||||
#if defined(_MSC_VER) || (defined(__GNUC__) && defined(__x86_64__))
|
||||
struct finsh_syscall* finsh_syscall_next(struct finsh_syscall* call);
|
||||
struct finsh_sysvar* finsh_sysvar_next(struct finsh_sysvar* call);
|
||||
#define FINSH_NEXT_SYSCALL(index) index=finsh_syscall_next(index)
|
||||
#define FINSH_NEXT_SYSVAR(index) index=finsh_sysvar_next(index)
|
||||
#else
|
||||
#define FINSH_NEXT_SYSCALL(index) index++
|
||||
#define FINSH_NEXT_SYSVAR(index) index++
|
||||
#endif
|
||||
|
||||
/* system variable item */
|
||||
struct finsh_sysvar_item
|
||||
{
|
||||
struct finsh_sysvar_item *next; /* next item */
|
||||
struct finsh_sysvar sysvar; /* system variable */
|
||||
};
|
||||
extern struct finsh_sysvar *_sysvar_table_begin, *_sysvar_table_end;
|
||||
extern struct finsh_sysvar_item* global_sysvar_list;
|
||||
|
||||
/* find out system variable, which should be implemented in user program */
|
||||
struct finsh_sysvar* finsh_sysvar_lookup(const char* name);
|
||||
|
||||
|
||||
struct finsh_token
|
||||
{
|
||||
char eof;
|
||||
char replay;
|
||||
|
||||
int position;
|
||||
uint8_t current_token;
|
||||
|
||||
union {
|
||||
char char_value;
|
||||
int int_value;
|
||||
long long_value;
|
||||
} value;
|
||||
uint8_t string[FINSH_STRING_MAX];
|
||||
|
||||
uint8_t* line;
|
||||
};
|
||||
|
||||
#define FINSH_IDTYPE_VAR 0x01
|
||||
#define FINSH_IDTYPE_SYSVAR 0x02
|
||||
#define FINSH_IDTYPE_SYSCALL 0x04
|
||||
#define FINSH_IDTYPE_ADDRESS 0x08
|
||||
struct finsh_node
|
||||
{
|
||||
uint8_t node_type; /* node node_type */
|
||||
uint8_t data_type; /* node data node_type */
|
||||
uint8_t idtype; /* id node information */
|
||||
|
||||
union { /* value node */
|
||||
char char_value;
|
||||
short short_value;
|
||||
int int_value;
|
||||
long long_value;
|
||||
void* ptr;
|
||||
} value;
|
||||
union
|
||||
{
|
||||
/* point to variable identifier or function identifier */
|
||||
struct finsh_var *var;
|
||||
struct finsh_sysvar *sysvar;
|
||||
struct finsh_syscall*syscall;
|
||||
}id;
|
||||
|
||||
/* sibling and child node */
|
||||
struct finsh_node *sibling, *child;
|
||||
};
|
||||
|
||||
struct finsh_parser
|
||||
{
|
||||
uint8_t* parser_string;
|
||||
|
||||
struct finsh_token token;
|
||||
struct finsh_node* root;
|
||||
};
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* The basic data type in finsh shell
|
||||
*/
|
||||
enum finsh_type {
|
||||
finsh_type_unknown = 0, /**< unknown data type */
|
||||
finsh_type_void, /**< void */
|
||||
finsh_type_voidp, /**< void pointer */
|
||||
finsh_type_char, /**< char */
|
||||
finsh_type_uchar, /**< unsigned char */
|
||||
finsh_type_charp, /**< char pointer */
|
||||
finsh_type_short, /**< short */
|
||||
finsh_type_ushort, /**< unsigned short */
|
||||
finsh_type_shortp, /**< short pointer */
|
||||
finsh_type_int, /**< int */
|
||||
finsh_type_uint, /**< unsigned int */
|
||||
finsh_type_intp, /**< int pointer */
|
||||
finsh_type_long, /**< long */
|
||||
finsh_type_ulong, /**< unsigned long */
|
||||
finsh_type_longp /**< long pointer */
|
||||
};
|
||||
|
||||
/* init finsh environment */
|
||||
int finsh_init(struct finsh_parser* parser);
|
||||
/* flush finsh node, text segment */
|
||||
int finsh_flush(struct finsh_parser* parser);
|
||||
/* reset all of finsh */
|
||||
int finsh_reset(struct finsh_parser* parser);
|
||||
#ifdef RT_USING_DEVICE
|
||||
void finsh_set_device(const char* device_name);
|
||||
#endif
|
||||
|
||||
/* run finsh parser to generate abstract synatx tree */
|
||||
void finsh_parser_run (struct finsh_parser* parser, const unsigned char* string);
|
||||
/* run compiler to compile abstract syntax tree */
|
||||
int finsh_compiler_run(struct finsh_node* node);
|
||||
/* run finsh virtual machine */
|
||||
void finsh_vm_run(void);
|
||||
|
||||
/* get variable value */
|
||||
struct finsh_var* finsh_var_lookup(const char* name);
|
||||
/* get bottom value of stack */
|
||||
long finsh_stack_bottom(void);
|
||||
|
||||
/* get error number of finsh */
|
||||
uint8_t finsh_errno(void);
|
||||
/* get error string */
|
||||
const char* finsh_error_string(uint8_t type);
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This function appends a system call to finsh runtime environment
|
||||
* @param name the name of system call
|
||||
* @param func the function pointer of system call
|
||||
*/
|
||||
void finsh_syscall_append(const char* name, syscall_func func);
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This function appends a system variable to finsh runtime environment
|
||||
* @param name the name of system variable
|
||||
* @param type the data type of system variable
|
||||
* @param addr the address of system variable
|
||||
*/
|
||||
void finsh_sysvar_append(const char* name, uint8_t type, void* addr);
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,231 @@
|
||||
/*
|
||||
* File : finsh_api.h
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#ifndef FINSH_API_H__
|
||||
#define FINSH_API_H__
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma section("FSymTab$f",read)
|
||||
#pragma section("VSymTab",read)
|
||||
#endif
|
||||
|
||||
typedef long (*syscall_func)(void);
|
||||
|
||||
/* system call table */
|
||||
struct finsh_syscall
|
||||
{
|
||||
const char* name; /* the name of system call */
|
||||
#if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
|
||||
const char* desc; /* description of system call */
|
||||
#endif
|
||||
syscall_func func; /* the function address of system call */
|
||||
};
|
||||
extern struct finsh_syscall *_syscall_table_begin, *_syscall_table_end;
|
||||
|
||||
/* find out system call, which should be implemented in user program */
|
||||
struct finsh_syscall* finsh_syscall_lookup(const char* name);
|
||||
|
||||
#ifdef FINSH_USING_SYMTAB
|
||||
|
||||
#ifdef __TI_COMPILER_VERSION__
|
||||
#define __TI_FINSH_EXPORT_FUNCTION(f) PRAGMA(DATA_SECTION(f,"FSymTab"))
|
||||
#define __TI_FINSH_EXPORT_VAR(v) PRAGMA(DATA_SECTION(v,"VSymTab"))
|
||||
#endif
|
||||
|
||||
#ifdef FINSH_USING_DESCRIPTION
|
||||
#ifdef _MSC_VER
|
||||
#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
|
||||
const char __fsym_##cmd##_name[] = #cmd; \
|
||||
const char __fsym_##cmd##_desc[] = #desc; \
|
||||
__declspec(allocate("FSymTab$f")) \
|
||||
const struct finsh_syscall __fsym_##cmd = \
|
||||
{ \
|
||||
__fsym_##cmd##_name, \
|
||||
__fsym_##cmd##_desc, \
|
||||
(syscall_func)&name \
|
||||
};
|
||||
#pragma comment(linker, "/merge:FSymTab=mytext")
|
||||
|
||||
#define FINSH_VAR_EXPORT(name, type, desc) \
|
||||
const char __vsym_##name##_name[] = #name; \
|
||||
const char __vsym_##name##_desc[] = #desc; \
|
||||
__declspec(allocate("VSymTab")) \
|
||||
const struct finsh_sysvar __vsym_##name = \
|
||||
{ \
|
||||
__vsym_##name##_name, \
|
||||
__vsym_##name##_desc, \
|
||||
type, \
|
||||
(void*)&name \
|
||||
};
|
||||
|
||||
#elif defined(__TI_COMPILER_VERSION__)
|
||||
#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
|
||||
__TI_FINSH_EXPORT_FUNCTION(__fsym_##cmd); \
|
||||
const char __fsym_##cmd##_name[] = #cmd; \
|
||||
const char __fsym_##cmd##_desc[] = #desc; \
|
||||
const struct finsh_syscall __fsym_##cmd = \
|
||||
{ \
|
||||
__fsym_##cmd##_name, \
|
||||
__fsym_##cmd##_desc, \
|
||||
(syscall_func)&name \
|
||||
};
|
||||
|
||||
#define FINSH_VAR_EXPORT(name, type, desc) \
|
||||
__TI_FINSH_EXPORT_VAR(__vsym_##name); \
|
||||
const char __vsym_##name##_name[] = #name; \
|
||||
const char __vsym_##name##_desc[] = #desc; \
|
||||
const struct finsh_sysvar __vsym_##name = \
|
||||
{ \
|
||||
__vsym_##name##_name, \
|
||||
__vsym_##name##_desc, \
|
||||
type, \
|
||||
(void*)&name \
|
||||
};
|
||||
|
||||
#else
|
||||
#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
|
||||
const char __fsym_##cmd##_name[] SECTION(".rodata.name") = #cmd; \
|
||||
const char __fsym_##cmd##_desc[] SECTION(".rodata.name") = #desc; \
|
||||
const struct finsh_syscall __fsym_##cmd SECTION("FSymTab")= \
|
||||
{ \
|
||||
__fsym_##cmd##_name, \
|
||||
__fsym_##cmd##_desc, \
|
||||
(syscall_func)&name \
|
||||
};
|
||||
|
||||
#define FINSH_VAR_EXPORT(name, type, desc) \
|
||||
const char __vsym_##name##_name[] SECTION(".rodata.name") = #name; \
|
||||
const char __vsym_##name##_desc[] SECTION(".rodata.name") = #desc; \
|
||||
const struct finsh_sysvar __vsym_##name SECTION("VSymTab")= \
|
||||
{ \
|
||||
__vsym_##name##_name, \
|
||||
__vsym_##name##_desc, \
|
||||
type, \
|
||||
(void*)&name \
|
||||
};
|
||||
|
||||
#endif
|
||||
#else
|
||||
#ifdef _MSC_VER
|
||||
#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
|
||||
const char __fsym_##cmd##_name[] = #cmd; \
|
||||
__declspec(allocate("FSymTab$f")) \
|
||||
const struct finsh_syscall __fsym_##cmd = \
|
||||
{ \
|
||||
__fsym_##cmd##_name, \
|
||||
(syscall_func)&name \
|
||||
};
|
||||
#pragma comment(linker, "/merge:FSymTab=mytext")
|
||||
|
||||
#define FINSH_VAR_EXPORT(name, type, desc) \
|
||||
const char __vsym_##name##_name[] = #name; \
|
||||
__declspec(allocate("VSymTab")) const struct finsh_sysvar __vsym_##name = \
|
||||
{ \
|
||||
__vsym_##name##_name, \
|
||||
type, \
|
||||
(void*)&name \
|
||||
};
|
||||
|
||||
#elif defined(__TI_COMPILER_VERSION__)
|
||||
#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
|
||||
__TI_FINSH_EXPORT_FUNCTION(__fsym_##cmd); \
|
||||
const char __fsym_##cmd##_name[] = #cmd; \
|
||||
const struct finsh_syscall __fsym_##cmd = \
|
||||
{ \
|
||||
__fsym_##cmd##_name, \
|
||||
(syscall_func)&name \
|
||||
};
|
||||
|
||||
#define FINSH_VAR_EXPORT(name, type, desc) \
|
||||
__TI_FINSH_EXPORT_VAR(__vsym_##name); \
|
||||
const char __vsym_##name##_name[] = #name; \
|
||||
const struct finsh_sysvar __vsym_##name = \
|
||||
{ \
|
||||
__vsym_##name##_name, \
|
||||
type, \
|
||||
(void*)&name \
|
||||
};
|
||||
|
||||
#else
|
||||
#define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
|
||||
const char __fsym_##cmd##_name[] = #cmd; \
|
||||
const struct finsh_syscall __fsym_##cmd SECTION("FSymTab")= \
|
||||
{ \
|
||||
__fsym_##cmd##_name, \
|
||||
(syscall_func)&name \
|
||||
};
|
||||
|
||||
#define FINSH_VAR_EXPORT(name, type, desc) \
|
||||
const char __vsym_##name##_name[] = #name; \
|
||||
const struct finsh_sysvar __vsym_##name SECTION("VSymTab")= \
|
||||
{ \
|
||||
__vsym_##name##_name, \
|
||||
type, \
|
||||
(void*)&name \
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif /* end of FINSH_USING_DESCRIPTION */
|
||||
#endif /* end of FINSH_USING_SYMTAB */
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This macro exports a system function to finsh shell.
|
||||
*
|
||||
* @param name the name of function.
|
||||
* @param desc the description of function, which will show in help.
|
||||
*/
|
||||
#define FINSH_FUNCTION_EXPORT(name, desc) \
|
||||
FINSH_FUNCTION_EXPORT_CMD(name, name, desc)
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This macro exports a system function with an alias name to finsh shell.
|
||||
*
|
||||
* @param name the name of function.
|
||||
* @param alias the alias name of function.
|
||||
* @param desc the description of function, which will show in help.
|
||||
*/
|
||||
#define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc) \
|
||||
FINSH_FUNCTION_EXPORT_CMD(name, alias, desc)
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This macro exports a command to module shell.
|
||||
*
|
||||
* @param command the name of command.
|
||||
* @param desc the description of command, which will show in help.
|
||||
*/
|
||||
#ifdef FINSH_USING_MSH
|
||||
#define MSH_CMD_EXPORT(command, desc) \
|
||||
FINSH_FUNCTION_EXPORT_CMD(command, __cmd_##command, desc)
|
||||
#define MSH_CMD_EXPORT_ALIAS(command, alias, desc) \
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(command, __cmd_##alias, desc)
|
||||
#else
|
||||
#define MSH_CMD_EXPORT(command, desc)
|
||||
#define MSH_CMD_EXPORT_ALIAS(command, alias, desc)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,585 @@
|
||||
/*
|
||||
* RT-Thread module shell implementation.
|
||||
*
|
||||
* COPYRIGHT (C) 2013, Shanghai Real-Thread Technology Co., Ltd
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2013-03-30 Bernard the first verion for finsh
|
||||
* 2014-01-03 Bernard msh can execute module.
|
||||
* 2017-07-19 Aubr.Cool limit argc to RT_FINSH_ARG_MAX
|
||||
*/
|
||||
|
||||
#include "msh.h"
|
||||
#include <finsh.h>
|
||||
#include <shell.h>
|
||||
|
||||
#ifdef RT_USING_DFS
|
||||
#include <dfs_posix.h>
|
||||
#endif
|
||||
|
||||
#define RT_FINSH_ARG_MAX 10
|
||||
typedef int (*cmd_function_t)(int argc, char **argv);
|
||||
|
||||
#ifdef FINSH_USING_MSH
|
||||
#ifdef FINSH_USING_MSH_ONLY
|
||||
rt_bool_t msh_is_used(void)
|
||||
{
|
||||
return RT_TRUE;
|
||||
}
|
||||
#else
|
||||
#ifdef FINSH_USING_MSH_DEFAULT
|
||||
static rt_bool_t __msh_state = RT_TRUE;
|
||||
#else
|
||||
static rt_bool_t __msh_state = RT_FALSE;
|
||||
#endif
|
||||
rt_bool_t msh_is_used(void)
|
||||
{
|
||||
return __msh_state;
|
||||
}
|
||||
|
||||
static int msh_exit(int argc, char **argv)
|
||||
{
|
||||
/* return to finsh shell mode */
|
||||
__msh_state = RT_FALSE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(msh_exit, __cmd_exit, return to RT-Thread shell mode.);
|
||||
|
||||
static int msh_enter(void)
|
||||
{
|
||||
/* enter module shell mode */
|
||||
__msh_state = RT_TRUE;
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(msh_enter, msh, use module shell);
|
||||
#endif
|
||||
|
||||
int msh_help(int argc, char **argv)
|
||||
{
|
||||
rt_kprintf("RT-Thread shell commands:\n");
|
||||
{
|
||||
struct finsh_syscall *index;
|
||||
|
||||
for (index = _syscall_table_begin;
|
||||
index < _syscall_table_end;
|
||||
FINSH_NEXT_SYSCALL(index))
|
||||
{
|
||||
if (strncmp(index->name, "__cmd_", 6) != 0) continue;
|
||||
#if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
|
||||
rt_kprintf("%-16s - %s\n", &index->name[6], index->desc);
|
||||
#else
|
||||
rt_kprintf("%s ", &index->name[6]);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
rt_kprintf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(msh_help, __cmd_help, RT-Thread shell help);
|
||||
|
||||
static int msh_split(char *cmd, rt_size_t length, char *argv[RT_FINSH_ARG_MAX])
|
||||
{
|
||||
char *ptr;
|
||||
rt_size_t position;
|
||||
rt_size_t argc;
|
||||
rt_size_t i;
|
||||
|
||||
ptr = cmd;
|
||||
position = 0; argc = 0;
|
||||
|
||||
while (position < length)
|
||||
{
|
||||
/* strip bank and tab */
|
||||
while ((*ptr == ' ' || *ptr == '\t') && position < length)
|
||||
{
|
||||
*ptr = '\0';
|
||||
ptr ++; position ++;
|
||||
}
|
||||
|
||||
if(argc >= RT_FINSH_ARG_MAX)
|
||||
{
|
||||
rt_kprintf("Too many args ! We only Use:\n");
|
||||
for(i = 0; i < argc; i++)
|
||||
{
|
||||
rt_kprintf("%s ", argv[i]);
|
||||
}
|
||||
rt_kprintf("\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if (position >= length) break;
|
||||
|
||||
/* handle string */
|
||||
if (*ptr == '"')
|
||||
{
|
||||
ptr ++; position ++;
|
||||
argv[argc] = ptr; argc ++;
|
||||
|
||||
/* skip this string */
|
||||
while (*ptr != '"' && position < length)
|
||||
{
|
||||
if (*ptr == '\\')
|
||||
{
|
||||
if (*(ptr + 1) == '"')
|
||||
{
|
||||
ptr ++; position ++;
|
||||
}
|
||||
}
|
||||
ptr ++; position ++;
|
||||
}
|
||||
if (position >= length) break;
|
||||
|
||||
/* skip '"' */
|
||||
*ptr = '\0'; ptr ++; position ++;
|
||||
}
|
||||
else
|
||||
{
|
||||
argv[argc] = ptr;
|
||||
argc ++;
|
||||
while ((*ptr != ' ' && *ptr != '\t') && position < length)
|
||||
{
|
||||
ptr ++; position ++;
|
||||
}
|
||||
if (position >= length) break;
|
||||
}
|
||||
}
|
||||
|
||||
return argc;
|
||||
}
|
||||
|
||||
static cmd_function_t msh_get_cmd(char *cmd, int size)
|
||||
{
|
||||
struct finsh_syscall *index;
|
||||
cmd_function_t cmd_func = RT_NULL;
|
||||
|
||||
for (index = _syscall_table_begin;
|
||||
index < _syscall_table_end;
|
||||
FINSH_NEXT_SYSCALL(index))
|
||||
{
|
||||
if (strncmp(index->name, "__cmd_", 6) != 0) continue;
|
||||
|
||||
if (strncmp(&index->name[6], cmd, size) == 0 &&
|
||||
index->name[6 + size] == '\0')
|
||||
{
|
||||
cmd_func = (cmd_function_t)index->func;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return cmd_func;
|
||||
}
|
||||
|
||||
#if defined(RT_USING_MODULE) && defined(RT_USING_DFS)
|
||||
/* Return 0 on module executed. Other value indicate error.
|
||||
*/
|
||||
int msh_exec_module(const char *cmd_line, int size)
|
||||
{
|
||||
int ret;
|
||||
int fd = -1;
|
||||
char *pg_name;
|
||||
int length, cmd_length = 0;
|
||||
|
||||
if (size == 0)
|
||||
return -RT_ERROR;
|
||||
/* get the length of command0 */
|
||||
while ((cmd_line[cmd_length] != ' ' && cmd_line[cmd_length] != '\t') && cmd_length < size)
|
||||
cmd_length ++;
|
||||
|
||||
/* get name length */
|
||||
length = cmd_length + 32;
|
||||
|
||||
/* allocate program name memory */
|
||||
pg_name = (char *) rt_malloc(length);
|
||||
if (pg_name == RT_NULL)
|
||||
return -RT_ENOMEM;
|
||||
|
||||
/* copy command0 */
|
||||
memcpy(pg_name, cmd_line, cmd_length);
|
||||
pg_name[cmd_length] = '\0';
|
||||
|
||||
if (strstr(pg_name, ".mo") != RT_NULL || strstr(pg_name, ".MO") != RT_NULL)
|
||||
{
|
||||
/* try to open program */
|
||||
fd = open(pg_name, O_RDONLY, 0);
|
||||
|
||||
/* search in /bin path */
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_snprintf(pg_name, length - 1, "/bin/%.*s", cmd_length, cmd_line);
|
||||
fd = open(pg_name, O_RDONLY, 0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* add .mo and open program */
|
||||
|
||||
/* try to open program */
|
||||
strcat(pg_name, ".mo");
|
||||
fd = open(pg_name, O_RDONLY, 0);
|
||||
|
||||
/* search in /bin path */
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_snprintf(pg_name, length - 1, "/bin/%.*s.mo", cmd_length, cmd_line);
|
||||
fd = open(pg_name, O_RDONLY, 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (fd >= 0)
|
||||
{
|
||||
/* found program */
|
||||
close(fd);
|
||||
rt_module_exec_cmd(pg_name, cmd_line, size);
|
||||
ret = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
rt_free(pg_name);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int system(const char *command)
|
||||
{
|
||||
int ret = -RT_ENOMEM;
|
||||
char *cmd = rt_strdup(command);
|
||||
|
||||
if (cmd)
|
||||
{
|
||||
ret = msh_exec(cmd, rt_strlen(cmd));
|
||||
rt_free(cmd);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
RTM_EXPORT(system);
|
||||
#endif
|
||||
|
||||
static int _msh_exec_cmd(char *cmd, rt_size_t length, int *retp)
|
||||
{
|
||||
int argc;
|
||||
rt_size_t cmd0_size = 0;
|
||||
cmd_function_t cmd_func;
|
||||
char *argv[RT_FINSH_ARG_MAX];
|
||||
|
||||
RT_ASSERT(cmd);
|
||||
RT_ASSERT(retp);
|
||||
|
||||
/* find the size of first command */
|
||||
while ((cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t') && cmd0_size < length)
|
||||
cmd0_size ++;
|
||||
if (cmd0_size == 0)
|
||||
return -RT_ERROR;
|
||||
|
||||
cmd_func = msh_get_cmd(cmd, cmd0_size);
|
||||
if (cmd_func == RT_NULL)
|
||||
return -RT_ERROR;
|
||||
|
||||
/* split arguments */
|
||||
memset(argv, 0x00, sizeof(argv));
|
||||
argc = msh_split(cmd, length, argv);
|
||||
if (argc == 0)
|
||||
return -RT_ERROR;
|
||||
|
||||
/* exec this command */
|
||||
*retp = cmd_func(argc, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int msh_exec(char *cmd, rt_size_t length)
|
||||
{
|
||||
int cmd_ret;
|
||||
|
||||
/* strim the beginning of command */
|
||||
while (*cmd == ' ' || *cmd == '\t')
|
||||
{
|
||||
cmd++;
|
||||
length--;
|
||||
}
|
||||
|
||||
if (length == 0)
|
||||
return 0;
|
||||
|
||||
/* Exec sequence:
|
||||
* 1. built-in command
|
||||
* 2. module(if enabled)
|
||||
* 3. chdir to the directry(if possible)
|
||||
*/
|
||||
if (_msh_exec_cmd(cmd, length, &cmd_ret) == 0)
|
||||
{
|
||||
return cmd_ret;
|
||||
}
|
||||
#ifdef RT_USING_MODULE
|
||||
if (msh_exec_module(cmd, length) == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR)
|
||||
if (msh_exec_script(cmd, length) == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* change to this directory */
|
||||
if (chdir(cmd) == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* truncate the cmd at the first space. */
|
||||
{
|
||||
char *tcmd;
|
||||
tcmd = cmd;
|
||||
while (*tcmd != ' ' && *tcmd != '\0')
|
||||
{
|
||||
tcmd++;
|
||||
}
|
||||
*tcmd = '\0';
|
||||
}
|
||||
rt_kprintf("%s: command not found.\n", cmd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int str_common(const char *str1, const char *str2)
|
||||
{
|
||||
const char *str = str1;
|
||||
|
||||
while ((*str != 0) && (*str2 != 0) && (*str == *str2))
|
||||
{
|
||||
str ++;
|
||||
str2 ++;
|
||||
}
|
||||
|
||||
return (str - str1);
|
||||
}
|
||||
|
||||
#ifdef RT_USING_DFS
|
||||
void msh_auto_complete_path(char *path)
|
||||
{
|
||||
DIR *dir = RT_NULL;
|
||||
struct dirent *dirent = RT_NULL;
|
||||
char *full_path, *ptr, *index;
|
||||
|
||||
if (!path)
|
||||
return;
|
||||
|
||||
full_path = (char *)rt_malloc(256);
|
||||
if (full_path == RT_NULL) return; /* out of memory */
|
||||
|
||||
if (*path != '/')
|
||||
{
|
||||
getcwd(full_path, 256);
|
||||
if (full_path[rt_strlen(full_path) - 1] != '/')
|
||||
strcat(full_path, "/");
|
||||
}
|
||||
else *full_path = '\0';
|
||||
|
||||
index = RT_NULL;
|
||||
ptr = path;
|
||||
for (;;)
|
||||
{
|
||||
if (*ptr == '/') index = ptr + 1;
|
||||
if (!*ptr) break;
|
||||
|
||||
ptr ++;
|
||||
}
|
||||
if (index == RT_NULL) index = path;
|
||||
|
||||
if (index != RT_NULL)
|
||||
{
|
||||
char *dest = index;
|
||||
|
||||
/* fill the parent path */
|
||||
ptr = full_path;
|
||||
while (*ptr) ptr ++;
|
||||
|
||||
for (index = path; index != dest;)
|
||||
*ptr++ = *index++;
|
||||
*ptr = '\0';
|
||||
|
||||
dir = opendir(full_path);
|
||||
if (dir == RT_NULL) /* open directory failed! */
|
||||
{
|
||||
rt_free(full_path);
|
||||
return;
|
||||
}
|
||||
|
||||
/* restore the index position */
|
||||
index = dest;
|
||||
}
|
||||
|
||||
/* auto complete the file or directory name */
|
||||
if (*index == '\0') /* display all of files and directories */
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
dirent = readdir(dir);
|
||||
if (dirent == RT_NULL) break;
|
||||
|
||||
rt_kprintf("%s\n", dirent->d_name);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_size_t length, min_length;
|
||||
|
||||
min_length = 0;
|
||||
for (;;)
|
||||
{
|
||||
dirent = readdir(dir);
|
||||
if (dirent == RT_NULL) break;
|
||||
|
||||
/* matched the prefix string */
|
||||
if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
|
||||
{
|
||||
if (min_length == 0)
|
||||
{
|
||||
min_length = rt_strlen(dirent->d_name);
|
||||
/* save dirent name */
|
||||
strcpy(full_path, dirent->d_name);
|
||||
}
|
||||
|
||||
length = str_common(dirent->d_name, full_path);
|
||||
|
||||
if (length < min_length)
|
||||
{
|
||||
min_length = length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (min_length)
|
||||
{
|
||||
if (min_length < rt_strlen(full_path))
|
||||
{
|
||||
/* list the candidate */
|
||||
rewinddir(dir);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
dirent = readdir(dir);
|
||||
if (dirent == RT_NULL) break;
|
||||
|
||||
if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
|
||||
rt_kprintf("%s\n", dirent->d_name);
|
||||
}
|
||||
}
|
||||
|
||||
length = index - path;
|
||||
memcpy(index, full_path, min_length);
|
||||
path[length + min_length] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
rt_free(full_path);
|
||||
}
|
||||
#endif
|
||||
|
||||
void msh_auto_complete(char *prefix)
|
||||
{
|
||||
int length, min_length;
|
||||
const char *name_ptr, *cmd_name;
|
||||
struct finsh_syscall *index;
|
||||
|
||||
min_length = 0;
|
||||
name_ptr = RT_NULL;
|
||||
|
||||
if (*prefix == '\0')
|
||||
{
|
||||
msh_help(0, RT_NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_DFS
|
||||
/* check whether a spare in the command */
|
||||
{
|
||||
char *ptr;
|
||||
|
||||
ptr = prefix + rt_strlen(prefix);
|
||||
while (ptr != prefix)
|
||||
{
|
||||
if (*ptr == ' ')
|
||||
{
|
||||
msh_auto_complete_path(ptr + 1);
|
||||
break;
|
||||
}
|
||||
|
||||
ptr --;
|
||||
}
|
||||
#ifdef RT_USING_MODULE
|
||||
/* There is a chance that the user want to run the module directly. So
|
||||
* try to complete the file names. If the completed path is not a
|
||||
* module, the system won't crash anyway. */
|
||||
if (ptr == prefix)
|
||||
{
|
||||
msh_auto_complete_path(ptr);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
/* checks in internal command */
|
||||
{
|
||||
for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
|
||||
{
|
||||
/* skip finsh shell function */
|
||||
if (strncmp(index->name, "__cmd_", 6) != 0) continue;
|
||||
|
||||
cmd_name = (const char *) &index->name[6];
|
||||
if (strncmp(prefix, cmd_name, strlen(prefix)) == 0)
|
||||
{
|
||||
if (min_length == 0)
|
||||
{
|
||||
/* set name_ptr */
|
||||
name_ptr = cmd_name;
|
||||
/* set initial length */
|
||||
min_length = strlen(name_ptr);
|
||||
}
|
||||
|
||||
length = str_common(name_ptr, cmd_name);
|
||||
if (length < min_length)
|
||||
min_length = length;
|
||||
|
||||
rt_kprintf("%s\n", cmd_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* auto complete string */
|
||||
if (name_ptr != NULL)
|
||||
{
|
||||
rt_strncpy(prefix, name_ptr, min_length);
|
||||
}
|
||||
|
||||
return ;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* RT-Thread module shell implementation.
|
||||
*
|
||||
* COPYRIGHT (C) 2013, Shanghai Real-Thread Technology Co., Ltd
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2013-03-30 Bernard the first verion for FinSH
|
||||
*/
|
||||
|
||||
#ifndef __M_SHELL__
|
||||
#define __M_SHELL__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
rt_bool_t msh_is_used(void);
|
||||
int msh_exec(char *cmd, rt_size_t length);
|
||||
void msh_auto_complete(char *prefix);
|
||||
|
||||
int msh_exec_module(const char *cmd_line, int size);
|
||||
int msh_exec_script(const char *cmd_line, int size);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,434 @@
|
||||
/*
|
||||
* internal commands for RT-Thread module shell
|
||||
*
|
||||
* COPYRIGHT (C) 2013-2015, Shanghai Real-Thread Technology Co., Ltd
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2013-03-30 Bernard the first verion for FinSH
|
||||
* 2015-08-28 Bernard Add mkfs command.
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <finsh.h>
|
||||
|
||||
#include "msh.h"
|
||||
|
||||
#ifdef FINSH_USING_MSH
|
||||
#ifdef RT_USING_DFS
|
||||
#include <dfs_posix.h>
|
||||
|
||||
#ifdef DFS_USING_WORKDIR
|
||||
extern char working_directory[];
|
||||
#endif
|
||||
|
||||
int cmd_ls(int argc, char **argv)
|
||||
{
|
||||
extern void ls(const char *pathname);
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
#ifdef DFS_USING_WORKDIR
|
||||
ls(working_directory);
|
||||
#else
|
||||
ls("/");
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
ls(argv[1]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ls, __cmd_ls, List information about the FILEs.);
|
||||
|
||||
int cmd_cp(int argc, char **argv)
|
||||
{
|
||||
void copy(const char *src, const char *dst);
|
||||
|
||||
if (argc != 3)
|
||||
{
|
||||
rt_kprintf("Usage: cp SOURCE DEST\n");
|
||||
rt_kprintf("Copy SOURCE to DEST.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
copy(argv[1], argv[2]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cp, __cmd_cp, Copy SOURCE to DEST.);
|
||||
|
||||
int cmd_mv(int argc, char **argv)
|
||||
{
|
||||
if (argc != 3)
|
||||
{
|
||||
rt_kprintf("Usage: mv SOURCE DEST\n");
|
||||
rt_kprintf("Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
int fd;
|
||||
char *dest = RT_NULL;
|
||||
|
||||
rt_kprintf("%s => %s\n", argv[1], argv[2]);
|
||||
|
||||
fd = open(argv[2], O_DIRECTORY, 0);
|
||||
if (fd >= 0)
|
||||
{
|
||||
char *src;
|
||||
|
||||
close(fd);
|
||||
|
||||
/* it's a directory */
|
||||
dest = (char *)rt_malloc(DFS_PATH_MAX);
|
||||
if (dest == RT_NULL)
|
||||
{
|
||||
rt_kprintf("out of memory\n");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
src = argv[1] + rt_strlen(argv[1]);
|
||||
while (src != argv[1])
|
||||
{
|
||||
if (*src == '/') break;
|
||||
src --;
|
||||
}
|
||||
|
||||
rt_snprintf(dest, DFS_PATH_MAX - 1, "%s/%s", argv[2], src);
|
||||
}
|
||||
else
|
||||
{
|
||||
fd = open(argv[2], O_RDONLY, 0);
|
||||
if (fd >= 0)
|
||||
{
|
||||
close(fd);
|
||||
|
||||
unlink(argv[2]);
|
||||
}
|
||||
|
||||
dest = argv[2];
|
||||
}
|
||||
|
||||
rename(argv[1], dest);
|
||||
if (dest != RT_NULL && dest != argv[2]) rt_free(dest);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mv, __cmd_mv, Rename SOURCE to DEST.);
|
||||
|
||||
int cmd_cat(int argc, char **argv)
|
||||
{
|
||||
int index;
|
||||
extern void cat(const char *filename);
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
rt_kprintf("Usage: cat [FILE]...\n");
|
||||
rt_kprintf("Concatenate FILE(s)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (index = 1; index < argc; index ++)
|
||||
{
|
||||
cat(argv[index]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cat, __cmd_cat, Concatenate FILE(s));
|
||||
|
||||
int cmd_rm(int argc, char **argv)
|
||||
{
|
||||
int index;
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
rt_kprintf("Usage: rm FILE...\n");
|
||||
rt_kprintf("Remove (unlink) the FILE(s).\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (index = 1; index < argc; index ++)
|
||||
{
|
||||
unlink(argv[index]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_rm, __cmd_rm, Remove(unlink) the FILE(s).);
|
||||
|
||||
#ifdef DFS_USING_WORKDIR
|
||||
int cmd_cd(int argc, char **argv)
|
||||
{
|
||||
if (argc == 1)
|
||||
{
|
||||
rt_kprintf("%s\n", working_directory);
|
||||
}
|
||||
else if (argc == 2)
|
||||
{
|
||||
if (chdir(argv[1]) != 0)
|
||||
{
|
||||
rt_kprintf("No such directory: %s\n", argv[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_cd, __cmd_cd, Change the shell working directory.);
|
||||
|
||||
int cmd_pwd(int argc, char **argv)
|
||||
{
|
||||
rt_kprintf("%s\n", working_directory);
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_pwd, __cmd_pwd, Print the name of the current working directory.);
|
||||
#endif
|
||||
|
||||
int cmd_mkdir(int argc, char **argv)
|
||||
{
|
||||
if (argc == 1)
|
||||
{
|
||||
rt_kprintf("Usage: mkdir [OPTION] DIRECTORY\n");
|
||||
rt_kprintf("Create the DIRECTORY, if they do not already exist.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
mkdir(argv[1], 0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mkdir, __cmd_mkdir, Create the DIRECTORY.);
|
||||
|
||||
int cmd_mkfs(int argc, char **argv)
|
||||
{
|
||||
int result = 0;
|
||||
char *type = "elm"; /* use the default file system type as 'fatfs' */
|
||||
|
||||
if (argc == 2)
|
||||
{
|
||||
result = dfs_mkfs(type, argv[1]);
|
||||
}
|
||||
else if (argc == 4)
|
||||
{
|
||||
if (strcmp(argv[1], "-t") == 0)
|
||||
{
|
||||
type = argv[2];
|
||||
result = dfs_mkfs(type, argv[3]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Usage: mkfs [-t type] device\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
rt_kprintf("mkfs failed, result=%d\n", result);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_mkfs, __cmd_mkfs, format disk with file system);
|
||||
|
||||
extern int df(const char *path);
|
||||
int cmd_df(int argc, char** argv)
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
df("/");
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((strcmp(argv[1], "--help") == 0) || (strcmp(argv[1], "-h") == 0))
|
||||
{
|
||||
rt_kprintf("df [path]\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
df(argv[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_df, __cmd_df, disk free);
|
||||
|
||||
int cmd_echo(int argc, char** argv)
|
||||
{
|
||||
if (argc == 2)
|
||||
{
|
||||
rt_kprintf("%s\n", argv[1]);
|
||||
}
|
||||
else if (argc == 3)
|
||||
{
|
||||
int fd;
|
||||
|
||||
fd = open(argv[2], O_RDWR | O_APPEND | O_CREAT, 0);
|
||||
if (fd >= 0)
|
||||
{
|
||||
write (fd, argv[1], strlen(argv[1]));
|
||||
close(fd);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("open file:%s failed!\n", argv[2]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("Usage: echo \"string\" [filename]\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_echo, __cmd_echo, echo string to file);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_LWIP
|
||||
int cmd_ifconfig(int argc, char **argv)
|
||||
{
|
||||
extern void list_if(void);
|
||||
extern void set_if(char *netif_name, char *ip_addr, char *gw_addr, char *nm_addr);
|
||||
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
list_if();
|
||||
}
|
||||
else if (argc == 5)
|
||||
{
|
||||
rt_kprintf("config : %s\n", argv[1]);
|
||||
rt_kprintf("IP addr: %s\n", argv[2]);
|
||||
rt_kprintf("Gateway: %s\n", argv[3]);
|
||||
rt_kprintf("netmask: %s\n", argv[4]);
|
||||
set_if(argv[1], argv[2], argv[3], argv[4]);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("bad parameter! e.g: ifconfig e0 192.168.1.30 192.168.1.1 255.255.255.0\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ifconfig, __cmd_ifconfig, list the information of network interfaces);
|
||||
|
||||
#ifdef RT_LWIP_DNS
|
||||
#include <lwip/api.h>
|
||||
#include <lwip/dns.h>
|
||||
#include <lwip/ip_addr.h>
|
||||
#include <lwip/init.h>
|
||||
|
||||
int cmd_dns(int argc, char **argv)
|
||||
{
|
||||
extern void set_dns(char* dns_server);
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
int index;
|
||||
|
||||
#if (LWIP_VERSION) < 0x02000000U
|
||||
ip_addr_t ip_addr;
|
||||
for(index=0; index<DNS_MAX_SERVERS; index++)
|
||||
{
|
||||
ip_addr = dns_getserver(index);
|
||||
rt_kprintf("dns server #%d: %s\n", index, ipaddr_ntoa(&ip_addr));
|
||||
}
|
||||
#else
|
||||
const ip_addr_t *ip_addr;
|
||||
for(index=0; index<DNS_MAX_SERVERS; index++)
|
||||
{
|
||||
ip_addr = dns_getserver(index);
|
||||
rt_kprintf("dns server #%d: %s\n", index, ipaddr_ntoa(ip_addr));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
else if (argc == 2)
|
||||
{
|
||||
rt_kprintf("dns : %s\n", argv[1]);
|
||||
set_dns(argv[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("bad parameter! e.g: dns 114.114.114.114\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_dns, __cmd_dns, list the information of dns);
|
||||
#endif
|
||||
|
||||
#ifdef RT_LWIP_TCP
|
||||
int cmd_netstat(int argc, char **argv)
|
||||
{
|
||||
extern void list_tcps(void);
|
||||
|
||||
list_tcps();
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_netstat, __cmd_netstat, list the information of TCP / IP);
|
||||
#endif
|
||||
#endif /* RT_USING_LWIP */
|
||||
|
||||
int cmd_ps(int argc, char **argv)
|
||||
{
|
||||
extern long list_thread(void);
|
||||
extern int list_module(void);
|
||||
|
||||
#ifdef RT_USING_MODULE
|
||||
if ((argc == 2) && (strcmp(argv[1], "-m") == 0))
|
||||
list_module();
|
||||
else
|
||||
#endif
|
||||
list_thread();
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_ps, __cmd_ps, List threads in the system);
|
||||
|
||||
int cmd_time(int argc, char **argv)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
//FINSH_FUNCTION_EXPORT_ALIAS(cmd_time, __cmd_time, Execute command with time);
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
int cmd_free(int argc, char **argv)
|
||||
{
|
||||
extern void list_mem(void);
|
||||
extern void list_memheap(void);
|
||||
|
||||
#ifdef RT_USING_MEMHEAP_AS_HEAP
|
||||
list_memheap();
|
||||
#else
|
||||
list_mem();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT_ALIAS(cmd_free, __cmd_free, Show the memory usage in the system);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* script for RT-Thread module shell
|
||||
*
|
||||
* COPYRIGHT (C) 2013-2015, Shanghai Real-Thread Technology Co., Ltd
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2015-09-25 Bernard the first verion for FinSH
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <finsh.h>
|
||||
|
||||
#include "msh.h"
|
||||
|
||||
#if defined(FINSH_USING_MSH) && defined(RT_USING_DFS)
|
||||
#include <dfs_posix.h>
|
||||
|
||||
static int msh_readline(int fd, char *line_buf, int size)
|
||||
{
|
||||
char ch;
|
||||
int index = 0;
|
||||
|
||||
do
|
||||
{
|
||||
if (read(fd, &ch, 1) != 1)
|
||||
{
|
||||
/* nothing in this file */
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
while (ch == '\n' || ch == '\r');
|
||||
|
||||
/* set the first character */
|
||||
line_buf[index ++] = ch;
|
||||
|
||||
while (index < size)
|
||||
{
|
||||
if (read(fd, &ch, 1) == 1)
|
||||
{
|
||||
if (ch == '\n' || ch == '\r')
|
||||
{
|
||||
line_buf[index] = '\0';
|
||||
break;
|
||||
}
|
||||
|
||||
line_buf[index++] = ch;
|
||||
}
|
||||
else
|
||||
{
|
||||
line_buf[index] = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
int msh_exec_script(const char *cmd_line, int size)
|
||||
{
|
||||
int ret;
|
||||
int fd = -1;
|
||||
char *pg_name;
|
||||
int length, cmd_length = 0;
|
||||
|
||||
if (size == 0) return -RT_ERROR;
|
||||
|
||||
/* get the length of command0 */
|
||||
while ((cmd_line[cmd_length] != ' ' && cmd_line[cmd_length] != '\t') && cmd_length < size)
|
||||
cmd_length ++;
|
||||
|
||||
/* get name length */
|
||||
length = cmd_length + 32;
|
||||
|
||||
/* allocate program name memory */
|
||||
pg_name = (char *) rt_malloc(length);
|
||||
if (pg_name == RT_NULL) return -RT_ENOMEM;
|
||||
|
||||
/* copy command0 */
|
||||
memcpy(pg_name, cmd_line, cmd_length);
|
||||
pg_name[cmd_length] = '\0';
|
||||
|
||||
if (strstr(pg_name, ".sh") != RT_NULL || strstr(pg_name, ".SH") != RT_NULL)
|
||||
{
|
||||
/* try to open program */
|
||||
fd = open(pg_name, O_RDONLY, 0);
|
||||
|
||||
/* search in /bin path */
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_snprintf(pg_name, length - 1, "/bin/%.*s", cmd_length, cmd_line);
|
||||
fd = open(pg_name, O_RDONLY, 0);
|
||||
}
|
||||
}
|
||||
|
||||
rt_free(pg_name);
|
||||
if (fd >= 0)
|
||||
{
|
||||
/* found script */
|
||||
char *line_buf;
|
||||
int length;
|
||||
|
||||
line_buf = (char *) rt_malloc(RT_CONSOLEBUF_SIZE);
|
||||
|
||||
/* read line by line and then exec it */
|
||||
do
|
||||
{
|
||||
length = msh_readline(fd, line_buf, RT_CONSOLEBUF_SIZE);
|
||||
if (length > 0)
|
||||
{
|
||||
char ch = '\0';
|
||||
int index;
|
||||
|
||||
for (index = 0; index < length; index ++)
|
||||
{
|
||||
ch = line_buf[index];
|
||||
if (ch == ' ' || ch == '\t') continue;
|
||||
else break;
|
||||
}
|
||||
|
||||
if (ch != '#') /* not a comment */
|
||||
msh_exec(line_buf, length);
|
||||
}
|
||||
}
|
||||
while (length > 0);
|
||||
|
||||
close(fd);
|
||||
rt_free(line_buf);
|
||||
|
||||
ret = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,798 @@
|
||||
/*
|
||||
* shell implementation for finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2006-04-30 Bernard the first version for FinSH
|
||||
* 2006-05-08 Bernard change finsh thread stack to 2048
|
||||
* 2006-06-03 Bernard add support for skyeye
|
||||
* 2006-09-24 Bernard remove the code related with hardware
|
||||
* 2010-01-18 Bernard fix down then up key bug.
|
||||
* 2010-03-19 Bernard fix backspace issue and fix device read in shell.
|
||||
* 2010-04-01 Bernard add prompt output when start and remove the empty history
|
||||
* 2011-02-23 Bernard fix variable section end issue of finsh shell
|
||||
* initialization when use GNU GCC compiler.
|
||||
* 2016-11-26 armink add password authentication
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
|
||||
#include "finsh.h"
|
||||
#include "shell.h"
|
||||
|
||||
#ifdef FINSH_USING_MSH
|
||||
#include "msh.h"
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <stdio.h> /* for putchar */
|
||||
#endif
|
||||
|
||||
/* finsh thread */
|
||||
#ifndef RT_USING_HEAP
|
||||
static struct rt_thread finsh_thread;
|
||||
ALIGN(RT_ALIGN_SIZE)
|
||||
static char finsh_thread_stack[FINSH_THREAD_STACK_SIZE];
|
||||
#endif
|
||||
struct finsh_shell *shell;
|
||||
|
||||
#if defined(FINSH_USING_MSH) || (defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR))
|
||||
#if defined(RT_USING_DFS)
|
||||
#include <dfs_posix.h>
|
||||
#endif
|
||||
const char *finsh_get_prompt()
|
||||
{
|
||||
#define _MSH_PROMPT "msh "
|
||||
#define _PROMPT "finsh "
|
||||
static char finsh_prompt[RT_CONSOLEBUF_SIZE + 1] = {0};
|
||||
|
||||
#ifdef FINSH_USING_MSH
|
||||
if (msh_is_used()) strcpy(finsh_prompt, _MSH_PROMPT);
|
||||
else
|
||||
#endif
|
||||
strcpy(finsh_prompt, _PROMPT);
|
||||
|
||||
#if defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR)
|
||||
/* get current working directory */
|
||||
getcwd(&finsh_prompt[rt_strlen(finsh_prompt)], RT_CONSOLEBUF_SIZE - rt_strlen(finsh_prompt));
|
||||
#endif
|
||||
|
||||
strcat(finsh_prompt, ">");
|
||||
|
||||
return finsh_prompt;
|
||||
}
|
||||
#endif
|
||||
|
||||
static char finsh_getchar(void)
|
||||
{
|
||||
#ifdef RT_USING_POSIX
|
||||
return getchar();
|
||||
#else
|
||||
char ch;
|
||||
|
||||
RT_ASSERT(shell != RT_NULL);
|
||||
while (rt_device_read(shell->device, -1, &ch, 1) != 1)
|
||||
rt_sem_take(&shell->rx_sem, RT_WAITING_FOREVER);
|
||||
|
||||
return ch;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef RT_USING_POSIX
|
||||
static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(shell != RT_NULL);
|
||||
|
||||
/* release semaphore to let finsh thread rx data */
|
||||
rt_sem_release(&shell->rx_sem);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This function sets the input device of finsh shell.
|
||||
*
|
||||
* @param device_name the name of new input device.
|
||||
*/
|
||||
void finsh_set_device(const char *device_name)
|
||||
{
|
||||
rt_device_t dev = RT_NULL;
|
||||
|
||||
RT_ASSERT(shell != RT_NULL);
|
||||
dev = rt_device_find(device_name);
|
||||
if (dev == RT_NULL)
|
||||
{
|
||||
rt_kprintf("finsh: can not find device: %s\n", device_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* check whether it's a same device */
|
||||
if (dev == shell->device) return;
|
||||
/* open this device and set the new device in finsh shell */
|
||||
if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | \
|
||||
RT_DEVICE_FLAG_STREAM) == RT_EOK)
|
||||
{
|
||||
if (shell->device != RT_NULL)
|
||||
{
|
||||
/* close old finsh device */
|
||||
rt_device_close(shell->device);
|
||||
rt_device_set_rx_indicate(shell->device, RT_NULL);
|
||||
}
|
||||
|
||||
/* clear line buffer before switch to new device */
|
||||
memset(shell->line, 0, sizeof(shell->line));
|
||||
shell->line_curpos = shell->line_position = 0;
|
||||
|
||||
shell->device = dev;
|
||||
rt_device_set_rx_indicate(dev, finsh_rx_ind);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This function returns current finsh shell input device.
|
||||
*
|
||||
* @return the finsh shell input device name is returned.
|
||||
*/
|
||||
const char *finsh_get_device()
|
||||
{
|
||||
RT_ASSERT(shell != RT_NULL);
|
||||
return shell->device->parent.name;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This function set the echo mode of finsh shell.
|
||||
*
|
||||
* FINSH_OPTION_ECHO=0x01 is echo mode, other values are none-echo mode.
|
||||
*
|
||||
* @param echo the echo mode
|
||||
*/
|
||||
void finsh_set_echo(rt_uint32_t echo)
|
||||
{
|
||||
RT_ASSERT(shell != RT_NULL);
|
||||
shell->echo_mode = (rt_uint8_t)echo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This function gets the echo mode of finsh shell.
|
||||
*
|
||||
* @return the echo mode
|
||||
*/
|
||||
rt_uint32_t finsh_get_echo()
|
||||
{
|
||||
RT_ASSERT(shell != RT_NULL);
|
||||
|
||||
return shell->echo_mode;
|
||||
}
|
||||
|
||||
#ifdef FINSH_USING_AUTH
|
||||
/**
|
||||
* set a new password for finsh
|
||||
*
|
||||
* @param password new password
|
||||
*
|
||||
* @return result, RT_EOK on OK, -RT_ERROR on the new password length is less than
|
||||
* FINSH_PASSWORD_MIN or greater than FINSH_PASSWORD_MAX
|
||||
*/
|
||||
rt_err_t finsh_set_password(const char *password) {
|
||||
rt_ubase_t level;
|
||||
rt_size_t pw_len = rt_strlen(password);
|
||||
|
||||
if (pw_len < FINSH_PASSWORD_MIN || pw_len > FINSH_PASSWORD_MAX)
|
||||
return -RT_ERROR;
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
rt_strncpy(shell->password, password, FINSH_PASSWORD_MAX);
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the finsh password
|
||||
*
|
||||
* @return password
|
||||
*/
|
||||
const char *finsh_get_password(void)
|
||||
{
|
||||
return shell->password;
|
||||
}
|
||||
|
||||
static void finsh_wait_auth(void)
|
||||
{
|
||||
char ch;
|
||||
rt_bool_t input_finish = RT_FALSE;
|
||||
char password[FINSH_PASSWORD_MAX] = { 0 };
|
||||
rt_size_t cur_pos = 0;
|
||||
/* password not set */
|
||||
if (rt_strlen(finsh_get_password()) == 0) return;
|
||||
|
||||
while (1)
|
||||
{
|
||||
rt_kprintf("Password for login: ");
|
||||
while (!input_finish)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
/* read one character from device */
|
||||
ch = finsh_getchar();
|
||||
|
||||
if (ch >= ' ' && ch <= '~' && cur_pos < FINSH_PASSWORD_MAX)
|
||||
{
|
||||
/* change the printable characters to '*' */
|
||||
rt_kprintf("*");
|
||||
password[cur_pos++] = ch;
|
||||
}
|
||||
else if (ch == '\b' && cur_pos > 0)
|
||||
{
|
||||
/* backspace */
|
||||
password[cur_pos] = '\0';
|
||||
cur_pos--;
|
||||
rt_kprintf("\b \b");
|
||||
}
|
||||
else if (ch == '\r' || ch == '\n')
|
||||
{
|
||||
rt_kprintf("\n");
|
||||
input_finish = RT_TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!rt_strncmp(shell->password, password, FINSH_PASSWORD_MAX)) return;
|
||||
else
|
||||
{
|
||||
/* authentication failed, delay 2S for retry */
|
||||
rt_thread_delay(2 * RT_TICK_PER_SECOND);
|
||||
rt_kprintf("Sorry, try again.\n");
|
||||
cur_pos = 0;
|
||||
input_finish = RT_FALSE;
|
||||
rt_memset(password, '\0', FINSH_PASSWORD_MAX);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* FINSH_USING_AUTH */
|
||||
|
||||
static void shell_auto_complete(char *prefix)
|
||||
{
|
||||
|
||||
rt_kprintf("\n");
|
||||
#ifdef FINSH_USING_MSH
|
||||
if (msh_is_used() == RT_TRUE)
|
||||
{
|
||||
msh_auto_complete(prefix);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
#ifndef FINSH_USING_MSH_ONLY
|
||||
extern void list_prefix(char * prefix);
|
||||
list_prefix(prefix);
|
||||
#endif
|
||||
}
|
||||
|
||||
rt_kprintf("%s%s", FINSH_PROMPT, prefix);
|
||||
}
|
||||
|
||||
#ifndef FINSH_USING_MSH_ONLY
|
||||
void finsh_run_line(struct finsh_parser *parser, const char *line)
|
||||
{
|
||||
const char *err_str;
|
||||
|
||||
if(shell->echo_mode)
|
||||
rt_kprintf("\n");
|
||||
finsh_parser_run(parser, (unsigned char *)line);
|
||||
|
||||
/* compile node root */
|
||||
if (finsh_errno() == 0)
|
||||
{
|
||||
finsh_compiler_run(parser->root);
|
||||
}
|
||||
else
|
||||
{
|
||||
err_str = finsh_error_string(finsh_errno());
|
||||
rt_kprintf("%s\n", err_str);
|
||||
}
|
||||
|
||||
/* run virtual machine */
|
||||
if (finsh_errno() == 0)
|
||||
{
|
||||
char ch;
|
||||
finsh_vm_run();
|
||||
|
||||
ch = (unsigned char)finsh_stack_bottom();
|
||||
if (ch > 0x20 && ch < 0x7e)
|
||||
{
|
||||
rt_kprintf("\t'%c', %d, 0x%08x\n",
|
||||
(unsigned char)finsh_stack_bottom(),
|
||||
(unsigned int)finsh_stack_bottom(),
|
||||
(unsigned int)finsh_stack_bottom());
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("\t%d, 0x%08x\n",
|
||||
(unsigned int)finsh_stack_bottom(),
|
||||
(unsigned int)finsh_stack_bottom());
|
||||
}
|
||||
}
|
||||
|
||||
finsh_flush(parser);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef FINSH_USING_HISTORY
|
||||
static rt_bool_t shell_handle_history(struct finsh_shell *shell)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
int i;
|
||||
rt_kprintf("\r");
|
||||
|
||||
for (i = 0; i <= 60; i++)
|
||||
putchar(' ');
|
||||
rt_kprintf("\r");
|
||||
|
||||
#else
|
||||
rt_kprintf("\033[2K\r");
|
||||
#endif
|
||||
rt_kprintf("%s%s", FINSH_PROMPT, shell->line);
|
||||
return RT_FALSE;
|
||||
}
|
||||
|
||||
static void shell_push_history(struct finsh_shell *shell)
|
||||
{
|
||||
if (shell->line_position != 0)
|
||||
{
|
||||
/* push history */
|
||||
if (shell->history_count >= FINSH_HISTORY_LINES)
|
||||
{
|
||||
/* if current cmd is same as last cmd, don't push */
|
||||
if (memcmp(&shell->cmd_history[FINSH_HISTORY_LINES - 1], shell->line, FINSH_CMD_SIZE))
|
||||
{
|
||||
/* move history */
|
||||
int index;
|
||||
for (index = 0; index < FINSH_HISTORY_LINES - 1; index ++)
|
||||
{
|
||||
memcpy(&shell->cmd_history[index][0],
|
||||
&shell->cmd_history[index + 1][0], FINSH_CMD_SIZE);
|
||||
}
|
||||
memset(&shell->cmd_history[index][0], 0, FINSH_CMD_SIZE);
|
||||
memcpy(&shell->cmd_history[index][0], shell->line, shell->line_position);
|
||||
|
||||
/* it's the maximum history */
|
||||
shell->history_count = FINSH_HISTORY_LINES;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* if current cmd is same as last cmd, don't push */
|
||||
if (shell->history_count == 0 || memcmp(&shell->cmd_history[shell->history_count - 1], shell->line, FINSH_CMD_SIZE))
|
||||
{
|
||||
shell->current_history = shell->history_count;
|
||||
memset(&shell->cmd_history[shell->history_count][0], 0, FINSH_CMD_SIZE);
|
||||
memcpy(&shell->cmd_history[shell->history_count][0], shell->line, shell->line_position);
|
||||
|
||||
/* increase count and set current history position */
|
||||
shell->history_count ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
shell->current_history = shell->history_count;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef RT_USING_HEAP
|
||||
struct finsh_shell _shell;
|
||||
#endif
|
||||
void finsh_thread_entry(void *parameter)
|
||||
{
|
||||
char ch;
|
||||
|
||||
/* normal is echo mode */
|
||||
#ifndef FINSH_ECHO_DISABLE_DEFAULT
|
||||
shell->echo_mode = 1;
|
||||
#else
|
||||
shell->echo_mode = 0;
|
||||
#endif
|
||||
|
||||
#ifndef FINSH_USING_MSH_ONLY
|
||||
finsh_init(&shell->parser);
|
||||
#endif
|
||||
|
||||
#ifndef RT_USING_POSIX
|
||||
/* set console device as shell device */
|
||||
if (shell->device == RT_NULL)
|
||||
{
|
||||
rt_device_t console = rt_console_get_device();
|
||||
if (console)
|
||||
{
|
||||
finsh_set_device(console->parent.name);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef FINSH_USING_AUTH
|
||||
/* set the default password when the password isn't setting */
|
||||
if (rt_strlen(finsh_get_password()) == 0)
|
||||
{
|
||||
if (finsh_set_password(FINSH_DEFAULT_PASSWORD) != RT_EOK)
|
||||
{
|
||||
rt_kprintf("Finsh password set failed.\n");
|
||||
}
|
||||
}
|
||||
/* waiting authenticate success */
|
||||
finsh_wait_auth();
|
||||
#endif
|
||||
|
||||
rt_kprintf(FINSH_PROMPT);
|
||||
|
||||
while (1)
|
||||
{
|
||||
ch = finsh_getchar();
|
||||
|
||||
/*
|
||||
* handle control key
|
||||
* up key : 0x1b 0x5b 0x41
|
||||
* down key: 0x1b 0x5b 0x42
|
||||
* right key:0x1b 0x5b 0x43
|
||||
* left key: 0x1b 0x5b 0x44
|
||||
*/
|
||||
if (ch == 0x1b)
|
||||
{
|
||||
shell->stat = WAIT_SPEC_KEY;
|
||||
continue;
|
||||
}
|
||||
else if (shell->stat == WAIT_SPEC_KEY)
|
||||
{
|
||||
if (ch == 0x5b)
|
||||
{
|
||||
shell->stat = WAIT_FUNC_KEY;
|
||||
continue;
|
||||
}
|
||||
|
||||
shell->stat = WAIT_NORMAL;
|
||||
}
|
||||
else if (shell->stat == WAIT_FUNC_KEY)
|
||||
{
|
||||
shell->stat = WAIT_NORMAL;
|
||||
|
||||
if (ch == 0x41) /* up key */
|
||||
{
|
||||
#ifdef FINSH_USING_HISTORY
|
||||
/* prev history */
|
||||
if (shell->current_history > 0)
|
||||
shell->current_history --;
|
||||
else
|
||||
{
|
||||
shell->current_history = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* copy the history command */
|
||||
memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
|
||||
FINSH_CMD_SIZE);
|
||||
shell->line_curpos = shell->line_position = strlen(shell->line);
|
||||
shell_handle_history(shell);
|
||||
#endif
|
||||
continue;
|
||||
}
|
||||
else if (ch == 0x42) /* down key */
|
||||
{
|
||||
#ifdef FINSH_USING_HISTORY
|
||||
/* next history */
|
||||
if (shell->current_history < shell->history_count - 1)
|
||||
shell->current_history ++;
|
||||
else
|
||||
{
|
||||
/* set to the end of history */
|
||||
if (shell->history_count != 0)
|
||||
shell->current_history = shell->history_count - 1;
|
||||
else
|
||||
continue;
|
||||
}
|
||||
|
||||
memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
|
||||
FINSH_CMD_SIZE);
|
||||
shell->line_curpos = shell->line_position = strlen(shell->line);
|
||||
shell_handle_history(shell);
|
||||
#endif
|
||||
continue;
|
||||
}
|
||||
else if (ch == 0x44) /* left key */
|
||||
{
|
||||
if (shell->line_curpos)
|
||||
{
|
||||
rt_kprintf("\b");
|
||||
shell->line_curpos --;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
else if (ch == 0x43) /* right key */
|
||||
{
|
||||
if (shell->line_curpos < shell->line_position)
|
||||
{
|
||||
rt_kprintf("%c", shell->line[shell->line_curpos]);
|
||||
shell->line_curpos ++;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
/* received null or error */
|
||||
if (ch == '\0' || ch == 0xFF) continue;
|
||||
/* handle tab key */
|
||||
else if (ch == '\t')
|
||||
{
|
||||
int i;
|
||||
/* move the cursor to the beginning of line */
|
||||
for (i = 0; i < shell->line_curpos; i++)
|
||||
rt_kprintf("\b");
|
||||
|
||||
/* auto complete */
|
||||
shell_auto_complete(&shell->line[0]);
|
||||
/* re-calculate position */
|
||||
shell->line_curpos = shell->line_position = strlen(shell->line);
|
||||
|
||||
continue;
|
||||
}
|
||||
/* handle backspace key */
|
||||
else if (ch == 0x7f || ch == 0x08)
|
||||
{
|
||||
/* note that shell->line_curpos >= 0 */
|
||||
if (shell->line_curpos == 0)
|
||||
continue;
|
||||
|
||||
shell->line_position--;
|
||||
shell->line_curpos--;
|
||||
|
||||
if (shell->line_position > shell->line_curpos)
|
||||
{
|
||||
int i;
|
||||
|
||||
rt_memmove(&shell->line[shell->line_curpos],
|
||||
&shell->line[shell->line_curpos + 1],
|
||||
shell->line_position - shell->line_curpos);
|
||||
shell->line[shell->line_position] = 0;
|
||||
|
||||
rt_kprintf("\b%s \b", &shell->line[shell->line_curpos]);
|
||||
|
||||
/* move the cursor to the origin position */
|
||||
for (i = shell->line_curpos; i <= shell->line_position; i++)
|
||||
rt_kprintf("\b");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("\b \b");
|
||||
shell->line[shell->line_position] = 0;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
/* handle end of line, break */
|
||||
if (ch == '\r' || ch == '\n')
|
||||
{
|
||||
#ifdef FINSH_USING_HISTORY
|
||||
shell_push_history(shell);
|
||||
#endif
|
||||
|
||||
#ifdef FINSH_USING_MSH
|
||||
if (msh_is_used() == RT_TRUE)
|
||||
{
|
||||
if (shell->echo_mode)
|
||||
rt_kprintf("\n");
|
||||
msh_exec(shell->line, shell->line_position);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
#ifndef FINSH_USING_MSH_ONLY
|
||||
/* add ';' and run the command line */
|
||||
shell->line[shell->line_position] = ';';
|
||||
|
||||
if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line);
|
||||
else
|
||||
if (shell->echo_mode) rt_kprintf("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
rt_kprintf(FINSH_PROMPT);
|
||||
memset(shell->line, 0, sizeof(shell->line));
|
||||
shell->line_curpos = shell->line_position = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* it's a large line, discard it */
|
||||
if (shell->line_position >= FINSH_CMD_SIZE)
|
||||
shell->line_position = 0;
|
||||
|
||||
/* normal character */
|
||||
if (shell->line_curpos < shell->line_position)
|
||||
{
|
||||
int i;
|
||||
|
||||
rt_memmove(&shell->line[shell->line_curpos + 1],
|
||||
&shell->line[shell->line_curpos],
|
||||
shell->line_position - shell->line_curpos);
|
||||
shell->line[shell->line_curpos] = ch;
|
||||
if (shell->echo_mode)
|
||||
rt_kprintf("%s", &shell->line[shell->line_curpos]);
|
||||
|
||||
/* move the cursor to new position */
|
||||
for (i = shell->line_curpos; i < shell->line_position; i++)
|
||||
rt_kprintf("\b");
|
||||
}
|
||||
else
|
||||
{
|
||||
shell->line[shell->line_position] = ch;
|
||||
if (shell->echo_mode)
|
||||
rt_kprintf("%c", ch);
|
||||
}
|
||||
|
||||
ch = 0;
|
||||
shell->line_position ++;
|
||||
shell->line_curpos++;
|
||||
if (shell->line_position >= FINSH_CMD_SIZE)
|
||||
{
|
||||
/* clear command line */
|
||||
shell->line_position = 0;
|
||||
shell->line_curpos = 0;
|
||||
}
|
||||
} /* end of device read */
|
||||
}
|
||||
|
||||
void finsh_system_function_init(const void *begin, const void *end)
|
||||
{
|
||||
_syscall_table_begin = (struct finsh_syscall *) begin;
|
||||
_syscall_table_end = (struct finsh_syscall *) end;
|
||||
}
|
||||
|
||||
void finsh_system_var_init(const void *begin, const void *end)
|
||||
{
|
||||
_sysvar_table_begin = (struct finsh_sysvar *) begin;
|
||||
_sysvar_table_end = (struct finsh_sysvar *) end;
|
||||
}
|
||||
|
||||
#if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */
|
||||
#ifdef FINSH_USING_SYMTAB
|
||||
#pragma section="FSymTab"
|
||||
#pragma section="VSymTab"
|
||||
#endif
|
||||
#elif defined(__ADSPBLACKFIN__) /* for VisaulDSP++ Compiler*/
|
||||
#ifdef FINSH_USING_SYMTAB
|
||||
extern "asm" int __fsymtab_start;
|
||||
extern "asm" int __fsymtab_end;
|
||||
extern "asm" int __vsymtab_start;
|
||||
extern "asm" int __vsymtab_end;
|
||||
#endif
|
||||
#elif defined(_MSC_VER)
|
||||
#pragma section("FSymTab$a", read)
|
||||
const char __fsym_begin_name[] = "__start";
|
||||
const char __fsym_begin_desc[] = "begin of finsh";
|
||||
__declspec(allocate("FSymTab$a")) const struct finsh_syscall __fsym_begin =
|
||||
{
|
||||
__fsym_begin_name,
|
||||
__fsym_begin_desc,
|
||||
NULL
|
||||
};
|
||||
|
||||
#pragma section("FSymTab$z", read)
|
||||
const char __fsym_end_name[] = "__end";
|
||||
const char __fsym_end_desc[] = "end of finsh";
|
||||
__declspec(allocate("FSymTab$z")) const struct finsh_syscall __fsym_end =
|
||||
{
|
||||
__fsym_end_name,
|
||||
__fsym_end_desc,
|
||||
NULL
|
||||
};
|
||||
#endif
|
||||
|
||||
/*
|
||||
* @ingroup finsh
|
||||
*
|
||||
* This function will initialize finsh shell
|
||||
*/
|
||||
int finsh_system_init(void)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
rt_thread_t tid;
|
||||
|
||||
#ifdef FINSH_USING_SYMTAB
|
||||
#ifdef __CC_ARM /* ARM C Compiler */
|
||||
extern const int FSymTab$$Base;
|
||||
extern const int FSymTab$$Limit;
|
||||
extern const int VSymTab$$Base;
|
||||
extern const int VSymTab$$Limit;
|
||||
finsh_system_function_init(&FSymTab$$Base, &FSymTab$$Limit);
|
||||
#ifndef FINSH_USING_MSH_ONLY
|
||||
finsh_system_var_init(&VSymTab$$Base, &VSymTab$$Limit);
|
||||
#endif
|
||||
#elif defined (__ICCARM__) || defined(__ICCRX__) /* for IAR Compiler */
|
||||
finsh_system_function_init(__section_begin("FSymTab"),
|
||||
__section_end("FSymTab"));
|
||||
finsh_system_var_init(__section_begin("VSymTab"),
|
||||
__section_end("VSymTab"));
|
||||
#elif defined (__GNUC__) || defined(__TI_COMPILER_VERSION__)
|
||||
/* GNU GCC Compiler and TI CCS */
|
||||
extern const int __fsymtab_start;
|
||||
extern const int __fsymtab_end;
|
||||
extern const int __vsymtab_start;
|
||||
extern const int __vsymtab_end;
|
||||
finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
|
||||
finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
|
||||
#elif defined(__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
|
||||
finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
|
||||
finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
|
||||
#elif defined(_MSC_VER)
|
||||
unsigned int *ptr_begin, *ptr_end;
|
||||
|
||||
ptr_begin = (unsigned int *)&__fsym_begin;
|
||||
ptr_begin += (sizeof(struct finsh_syscall) / sizeof(unsigned int));
|
||||
while (*ptr_begin == 0) ptr_begin ++;
|
||||
|
||||
ptr_end = (unsigned int *) &__fsym_end;
|
||||
ptr_end --;
|
||||
while (*ptr_end == 0) ptr_end --;
|
||||
|
||||
finsh_system_function_init(ptr_begin, ptr_end);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_HEAP
|
||||
/* create or set shell structure */
|
||||
shell = (struct finsh_shell *)rt_calloc(1, sizeof(struct finsh_shell));
|
||||
if (shell == RT_NULL)
|
||||
{
|
||||
rt_kprintf("no memory for shell\n");
|
||||
return -1;
|
||||
}
|
||||
tid = rt_thread_create(FINSH_THREAD_NAME,
|
||||
finsh_thread_entry, RT_NULL,
|
||||
FINSH_THREAD_STACK_SIZE, FINSH_THREAD_PRIORITY, 10);
|
||||
#else
|
||||
shell = &_shell;
|
||||
tid = &finsh_thread;
|
||||
result = rt_thread_init(&finsh_thread,
|
||||
FINSH_THREAD_NAME,
|
||||
finsh_thread_entry, RT_NULL,
|
||||
&finsh_thread_stack[0], sizeof(finsh_thread_stack),
|
||||
FINSH_THREAD_PRIORITY, 10);
|
||||
#endif /* RT_USING_HEAP */
|
||||
|
||||
rt_sem_init(&(shell->rx_sem), "shrx", 0, 0);
|
||||
|
||||
if (tid != NULL && result == RT_EOK)
|
||||
rt_thread_startup(tid);
|
||||
return 0;
|
||||
}
|
||||
INIT_APP_EXPORT(finsh_system_init);
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* shell implementation for finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-06-02 Bernard Add finsh_get_prompt function declaration
|
||||
*/
|
||||
|
||||
#ifndef __SHELL_H__
|
||||
#define __SHELL_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "finsh.h"
|
||||
|
||||
#ifndef FINSH_THREAD_PRIORITY
|
||||
#define FINSH_THREAD_PRIORITY 20
|
||||
#endif
|
||||
#ifndef FINSH_THREAD_STACK_SIZE
|
||||
#define FINSH_THREAD_STACK_SIZE 2048
|
||||
#endif
|
||||
#ifndef FINSH_CMD_SIZE
|
||||
#define FINSH_CMD_SIZE 80
|
||||
#endif
|
||||
|
||||
#define FINSH_OPTION_ECHO 0x01
|
||||
#if defined(FINSH_USING_MSH) || (defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR))
|
||||
#define FINSH_PROMPT finsh_get_prompt()
|
||||
const char* finsh_get_prompt(void);
|
||||
#else
|
||||
#define FINSH_PROMPT "finsh>>"
|
||||
#endif
|
||||
|
||||
#ifdef FINSH_USING_HISTORY
|
||||
#ifndef FINSH_HISTORY_LINES
|
||||
#define FINSH_HISTORY_LINES 5
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef FINSH_USING_AUTH
|
||||
#ifndef FINSH_PASSWORD_MAX
|
||||
#define FINSH_PASSWORD_MAX RT_NAME_MAX
|
||||
#endif
|
||||
#ifndef FINSH_PASSWORD_MIN
|
||||
#define FINSH_PASSWORD_MIN 6
|
||||
#endif
|
||||
#ifndef FINSH_DEFAULT_PASSWORD
|
||||
#define FINSH_DEFAULT_PASSWORD "rtthread"
|
||||
#endif
|
||||
#endif /* FINSH_USING_AUTH */
|
||||
|
||||
#ifndef FINSH_THREAD_NAME
|
||||
#define FINSH_THREAD_NAME "tshell"
|
||||
#endif
|
||||
|
||||
enum input_stat
|
||||
{
|
||||
WAIT_NORMAL,
|
||||
WAIT_SPEC_KEY,
|
||||
WAIT_FUNC_KEY,
|
||||
};
|
||||
struct finsh_shell
|
||||
{
|
||||
struct rt_semaphore rx_sem;
|
||||
|
||||
enum input_stat stat;
|
||||
|
||||
rt_uint8_t echo_mode:1;
|
||||
|
||||
#ifdef FINSH_USING_HISTORY
|
||||
rt_uint16_t current_history;
|
||||
rt_uint16_t history_count;
|
||||
|
||||
char cmd_history[FINSH_HISTORY_LINES][FINSH_CMD_SIZE];
|
||||
#endif
|
||||
|
||||
#ifndef FINSH_USING_MSH_ONLY
|
||||
struct finsh_parser parser;
|
||||
#endif
|
||||
|
||||
char line[FINSH_CMD_SIZE];
|
||||
rt_uint8_t line_position;
|
||||
rt_uint8_t line_curpos;
|
||||
|
||||
#ifndef RT_USING_POSIX
|
||||
rt_device_t device;
|
||||
#endif
|
||||
|
||||
#ifdef FINSH_USING_AUTH
|
||||
char password[FINSH_PASSWORD_MAX];
|
||||
#endif
|
||||
};
|
||||
|
||||
void finsh_set_echo(rt_uint32_t echo);
|
||||
rt_uint32_t finsh_get_echo(void);
|
||||
|
||||
int finsh_system_init(void);
|
||||
void finsh_set_device(const char* device_name);
|
||||
const char* finsh_get_device(void);
|
||||
|
||||
#ifdef FINSH_USING_AUTH
|
||||
rt_err_t finsh_set_password(const char *password);
|
||||
const char *finsh_get_password(void);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* symbols in finsh shell.
|
||||
*
|
||||
* COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
|
||||
*
|
||||
* This file is part of RT-Thread (http://www.rt-thread.org)
|
||||
* Maintainer: bernard.xiong <bernard.xiong at gmail.com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2010-03-22 Bernard first version
|
||||
*/
|
||||
#include "finsh.h"
|
||||
|
||||
long hello(void);
|
||||
long version(void);
|
||||
long list(void);
|
||||
long list_thread(void);
|
||||
long list_sem(void);
|
||||
long list_mutex(void);
|
||||
long list_fevent(void);
|
||||
long list_event(void);
|
||||
long list_mailbox(void);
|
||||
long list_msgqueue(void);
|
||||
long list_mempool(void);
|
||||
long list_timer(void);
|
||||
|
||||
#ifdef FINSH_USING_SYMTAB
|
||||
struct finsh_syscall *_syscall_table_begin = NULL;
|
||||
struct finsh_syscall *_syscall_table_end = NULL;
|
||||
struct finsh_sysvar *_sysvar_table_begin = NULL;
|
||||
struct finsh_sysvar *_sysvar_table_end = NULL;
|
||||
#else
|
||||
struct finsh_syscall _syscall_table[] =
|
||||
{
|
||||
{"hello", hello},
|
||||
{"version", version},
|
||||
{"list", list},
|
||||
{"list_thread", list_thread},
|
||||
#ifdef RT_USING_SEMAPHORE
|
||||
{"list_sem", list_sem},
|
||||
#endif
|
||||
#ifdef RT_USING_MUTEX
|
||||
{"list_mutex", list_mutex},
|
||||
#endif
|
||||
#ifdef RT_USING_FEVENT
|
||||
{"list_fevent", list_fevent},
|
||||
#endif
|
||||
#ifdef RT_USING_EVENT
|
||||
{"list_event", list_event},
|
||||
#endif
|
||||
#ifdef RT_USING_MAILBOX
|
||||
{"list_mb", list_mailbox},
|
||||
#endif
|
||||
#ifdef RT_USING_MESSAGEQUEUE
|
||||
{"list_mq", list_msgqueue},
|
||||
#endif
|
||||
#ifdef RT_USING_MEMPOOL
|
||||
{"list_memp", list_mempool},
|
||||
#endif
|
||||
{"list_timer", list_timer},
|
||||
};
|
||||
struct finsh_syscall *_syscall_table_begin = &_syscall_table[0];
|
||||
struct finsh_syscall *_syscall_table_end = &_syscall_table[sizeof(_syscall_table) / sizeof(struct finsh_syscall)];
|
||||
|
||||
struct finsh_sysvar *_sysvar_table_begin = NULL;
|
||||
struct finsh_sysvar *_sysvar_table_end = NULL;
|
||||
#endif
|
||||
Reference in New Issue
Block a user