GadgetSeed  0.9.6
shell_task.c
[詳解]
1 /** @file
2  @brief コマンドシェルタスク
3 
4  @date 2015.09.06
5  @author Takashi SHUDO
6 
7  @page command_shell コマンドシェル
8 
9  GadgetSeedは主にシステム及びアプリケーションのデバッグを目的としたコマンドシェルがあります。
10 
11  コマンドシェルを使用するには、以下のコンフィグ項目を有効にして下さい。
12 
13  * COMP_ENABLE_SHELL
14 
15 
16  デフォルトで使用できるコマンドは以下があります。
17 
18  | コマンド名 | 機能 | 詳細 |
19  |:------------------|:------------------------------|:----------------------|
20  | help | @copybrief com_help | @ref help_command 参照|
21  | sys | @copybrief com_sys | @ref sys_command 参照 |
22  | mem | @copybrief com_mem | @ref mem_command 参照 |
23  | dev | @copybrief com_dev | @ref dev_command 参照 |
24  | task | @copybrief com_task | @ref task_command 参照|
25  | graph | @copybrief com_graphics | @ref graph_command 参照|
26  | i2c | @copybrief com_i2c | @ref i2c_command 参照 |
27  | file | @copybrief com_file | @ref file_command 参照|
28  | net | @copybrief com_net | @ref net_command 参照 |
29 
30  ユーザ独自のコマンドを追加することができます。
31  詳細は @ref add_shell_command を参照して下さい。
32 */
33 
34 #include "sysconfig.h"
35 #include "task/syscall.h"
36 #include "shell.h"
37 #include "tprintf.h"
38 #include "tkprintf.h"
39 #include "str.h"
40 
41 #define SIZEOFSHELLST (1024*8)
42 
43 extern const struct st_shell_command com_help;
44 extern const struct st_shell_command com_sys;
45 extern const struct st_shell_command com_mem;
46 extern const struct st_shell_command com_dev;
47 extern const struct st_shell_command com_task;
48 #ifdef GSC_COMP_ENABLE_GRAPHICS
49 extern const struct st_shell_command com_graphics;
50 #endif
51 #ifdef GSC_DEV_ENABLE_I2C
52 extern const struct st_shell_command com_i2c;
53 #endif
54 #ifdef GSC_COMP_ENABLE_FATFS
55 extern const struct st_shell_command com_file;
56 #endif
57 #ifdef GSC_COMP_ENABLE_TCPIP
58 extern const struct st_shell_command com_net;
59 #endif
60 
61 struct st_shell gs_shell;
62 static const uchar prompt[] = ": ";
64 
65 #ifdef GSC_SHELL_USER_COMMAND_NUM ///< $gsc shellユーザコマンド登録可能数
66 static const struct st_shell_command * const icom_list[] = {
67 #else
68 const struct st_shell_command * const com_list[] = {
69 #endif
70  &com_help,
71  &com_sys,
72  &com_mem,
73  &com_dev,
74  &com_task,
75 #ifdef GSC_COMP_ENABLE_GRAPHICS
76  &com_graphics,
77 #endif
78 #ifdef GSC_DEV_ENABLE_I2C
79  &com_i2c,
80 #endif
81 #ifdef GSC_COMP_ENABLE_FATFS
82  &com_file,
83 #endif
84 #ifdef GSC_COMP_ENABLE_TCPIP
85  &com_net,
86 #endif
87  0
88 };
89 
90 #ifdef GSC_SHELL_USER_COMMAND_NUM
91 #define MAX_COMMAND_NUM ((sizeof(icom_list)/sizeof(struct st_shell_command *))+GSC_SHELL_USER_COMMAND_NUM)
92 struct st_shell_command *com_list[MAX_COMMAND_NUM];
93 
94 /**
95  @brief shell にユーザコマンドを追加する
96 
97  @param[in] command ユーザコマンド
98 
99  @return 0:成功、!=0:エラー
100 
101  @info 本関数を使用するにはマクロ GSC_SHELL_USER_COMMAND_NUM に、追加したいコマンド数を定義する必要があります
102 */
103 int add_shell_command(struct st_shell_command *command)
104 {
105  int i = 0;
106 
107  for(i=0; i<MAX_COMMAND_NUM; i++) {
108  if(com_list[i] == 0) {
109  com_list[i] = (struct st_shell_command *)command;
110  tprintf("Add user shell command \"%s\"\n", command->name);
111  return 0;
112  }
113  }
114 
115  SYSERR_PRINT("Cannot add user shell command \"%s\"\n", command->name);
116 
117  return -1;
118 }
119 #else
120 #define MAX_COMMAND_NUM ((sizeof(com_list)/sizeof(struct st_shell_command *))-1)
121 #endif
122 
123 static int shell_task(char *arg)
124 {
125 #ifdef GSC_SHELL_USER_COMMAND_NUM
126  int i = 0;
127 
128  while(icom_list[i] != 0) {
129  com_list[i] = (struct st_shell_command *)icom_list[i];
130  i ++;
131  }
132  shell_com_count = i;
133 #else
134  shell_com_count = MAX_COMMAND_NUM;
135 #endif
136  init_shell(&gs_shell, (struct st_shell_command * const *)com_list, prompt);
137 
138  print_prompt(&gs_shell);
139 
140  while(1) {
141  unsigned char rd;
142 
143  if(cgetc(&rd)) {
144  task_shell(&gs_shell, rd);
145  }
146  }
147 
148  return 0;
149 }
150 
151 static struct st_tcb shell_tcb;
152 static unsigned int shell_stack[SIZEOFSHELLST/sizeof(unsigned int)];
153 
154 void startup_shell(void)
155 {
156 #ifdef STARTUP_APP
157  task_add(shell_task, "shell", 0, &shell_tcb,
158  shell_stack, SIZEOFSHELLST, 0);
159 #else
160  task_exec(shell_task, "shell", 0, &shell_tcb,
161  shell_stack, SIZEOFSHELLST, 0);
162 #endif
163 }
164 
165 int exec_command(uchar *str)
166 {
168 
169  (void)strncopy(cmd, str, GSC_SHELL_MAX_LINE_COLUMS);
170 
171  return exec_shell_command(&gs_shell, cmd);
172 }
unsigned char uchar
GadgetSeedの文字(列)は unsigned char 型となる
Definition: str.h:13
シェルコマンド構造体
Definition: shell.h:33
コマンドシェル
const struct st_shell_command com_i2c
I2Cデバイス情報取得、制御
Definition: com_i2c.c:299
シェルデータ構造体
Definition: shell.h:43
const struct st_shell_command com_sys
システム状態取得、設定
Definition: com_system.c:1169
const uchar * prompt
コマンドプロンプト文字列
Definition: shell.h:47
const struct st_shell_command com_mem
メモリ表示、編集
Definition: com_mem.c:669
文字列処理
int cgetc(unsigned char *rd)
標準入力より1文字を取得する
Definition: console.c:197
int task_add(task_func func, char *name, int priority, struct st_tcb *tcb, unsigned int *stack, int stack_size, char *arg)
タスクを追加する
Definition: syscall_api.c:188
const struct st_shell_command com_graphics
グラフィックス描画
void init_shell(struct st_shell *shell, struct st_shell_command *const *coms, const uchar *prompt)
シェルを初期化する
Definition: shell.c:39
カーネル用機能限定printf
int exec_shell_command(struct st_shell *shell, uchar *str)
str文字列のコマンドを実行する
Definition: shell.c:218
uchar name[12]
コマンド名文字列
Definition: shell.h:34
int tprintf(const char *fmt,...)
簡易printf
Definition: tprintf.c:85
システムコール
const struct st_shell_command com_dev
デバイスドライバ操作
Definition: com_device.c:727
const struct st_shell_command com_task
タスク状態取得
Definition: com_task.c:226
int shell_com_count
$gsc shellユーザコマンド登録可能数
Definition: shell_task.c:63
int task_shell(struct st_shell *shell, uchar ch)
文字列編集タスク
Definition: shell.c:457
const struct st_shell_command com_file
ファイル操作
Definition: com_file.c:895
uchar * strncopy(uchar *dest, const uchar *src, unsigned int n)
文字列コピー
Definition: str.c:632
const struct st_shell_command com_help
ヘルプメッセージ表示
Definition: com_system.c:1107
void print_prompt(struct st_shell *shell)
プロンプトを表示する
Definition: shell.c:57
int task_exec(task_func func, char *name, int priority, struct st_tcb *tcb, unsigned int *stack, int stack_size, char *arg)
タスクを追加し起動する
Definition: syscall_api.c:231
#define GSC_SHELL_MAX_LINE_COLUMS
$gsc shellコマンドラインの最大文字数
Definition: lineedit.h:16
タスクコンテキスト
Definition: tcb.h:32
機能限定printf
const struct st_shell_command com_net
ネットワーク情報取得
Definition: com_net.c:475