2007-07-01 17:35:32 +02:00
|
|
|
/****************************************************************************
|
2008-01-01 00:52:53 +01:00
|
|
|
* examples/nsh/nsh_proccmds.c
|
2007-07-01 17:35:32 +02:00
|
|
|
*
|
2009-12-15 20:00:40 +01:00
|
|
|
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
|
2007-07-01 17:35:32 +02:00
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2008-01-01 00:52:53 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-07-01 17:35:32 +02:00
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2008-08-11 00:02:19 +02:00
|
|
|
#include <unistd.h>
|
2007-07-01 17:35:32 +02:00
|
|
|
#include <sched.h>
|
|
|
|
|
|
|
|
#include "nsh.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-08-20 01:21:56 +02:00
|
|
|
/* The returned value should be zero for sucess or TRUE or non zero for
|
|
|
|
* failure or FALSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef int (*exec_t)(void);
|
2007-07-01 17:35:32 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const char *g_statenames[] =
|
|
|
|
{
|
|
|
|
"INVALID ",
|
|
|
|
"PENDING ",
|
|
|
|
"READY ",
|
|
|
|
"RUNNING ",
|
|
|
|
"INACTIVE",
|
|
|
|
"WAITSEM ",
|
|
|
|
#ifndef CONFIG_DISABLE_MQUEUE
|
|
|
|
"WAITSIG ",
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_DISABLE_MQUEUE
|
|
|
|
"MQNEMPTY",
|
|
|
|
"MQNFULL "
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ps_task
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_PS
|
2007-07-01 17:35:32 +02:00
|
|
|
static void ps_task(FAR _TCB *tcb, FAR void *arg)
|
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
struct nsh_vtbl_s *vtbl = (struct nsh_vtbl_s*)arg;
|
2007-07-01 17:35:32 +02:00
|
|
|
int i;
|
2008-01-01 00:52:53 +01:00
|
|
|
|
|
|
|
/* Show task status */
|
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, "%5d %3d %4s %7s%c%c %8s ",
|
2007-12-02 19:18:59 +01:00
|
|
|
tcb->pid, tcb->sched_priority,
|
|
|
|
tcb->flags & TCB_FLAG_ROUND_ROBIN ? "RR " : "FIFO",
|
|
|
|
tcb->flags & TCB_FLAG_PTHREAD ? "PTHREAD" : "TASK ",
|
|
|
|
tcb->flags & TCB_FLAG_NONCANCELABLE ? 'N' : ' ',
|
|
|
|
tcb->flags & TCB_FLAG_CANCEL_PENDING ? 'P' : ' ',
|
|
|
|
g_statenames[tcb->task_state]);
|
|
|
|
|
2008-01-01 00:52:53 +01:00
|
|
|
/* Show task name and arguments */
|
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, "%s(", tcb->argv[0]);
|
2008-01-01 00:52:53 +01:00
|
|
|
|
|
|
|
/* Special case 1st argument (no comma) */
|
|
|
|
|
|
|
|
if (tcb->argv[1])
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, "%p", tcb->argv[1]);
|
2008-01-01 00:52:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then any additional arguments */
|
|
|
|
|
|
|
|
#if CONFIG_MAX_TASK_ARGS > 2
|
|
|
|
for (i = 2; i <= CONFIG_MAX_TASK_ARGS && tcb->argv[i]; i++)
|
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, ", %p", tcb->argv[i]);
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
2008-01-01 00:52:53 +01:00
|
|
|
#endif
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, ")\n");
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2007-07-01 17:35:32 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_exec
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_EXEC
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_exec(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
long addr;
|
|
|
|
|
|
|
|
addr = strtol(argv[1], &endptr, 0);
|
|
|
|
if (!addr || endptr == argv[1] || *endptr != '\0')
|
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
2008-08-17 20:59:50 +02:00
|
|
|
return ERROR;
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
|
|
|
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, "Calling %p\n", (exec_t)addr);
|
2008-08-20 01:21:56 +02:00
|
|
|
return ((exec_t)addr)();
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2007-07-01 17:35:32 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_ps
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_PS
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_ps(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2007-07-01 17:35:32 +02:00
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, "PID PRI SCHD TYPE NP STATE NAME\n");
|
|
|
|
sched_foreach(ps_task, vtbl);
|
2008-08-17 20:59:50 +02:00
|
|
|
return OK;
|
2007-07-01 17:35:32 +02:00
|
|
|
}
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2008-08-11 00:02:19 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_sleep
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_SIGNALS
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_SLEEP
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_sleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2008-08-11 00:02:19 +02:00
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
long secs;
|
|
|
|
|
|
|
|
secs = strtol(argv[1], &endptr, 0);
|
|
|
|
if (!secs || endptr == argv[1] || *endptr != '\0')
|
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
2008-08-17 20:59:50 +02:00
|
|
|
return ERROR;
|
2008-08-11 00:02:19 +02:00
|
|
|
}
|
|
|
|
sleep(secs);
|
2008-08-17 20:59:50 +02:00
|
|
|
return OK;
|
2008-08-11 00:02:19 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|
2008-08-11 00:02:19 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_usleep
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_SIGNALS
|
2008-11-14 16:11:26 +01:00
|
|
|
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_USLEEP
|
2008-08-17 20:59:50 +02:00
|
|
|
int cmd_usleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
2008-08-11 00:02:19 +02:00
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
long usecs;
|
|
|
|
|
|
|
|
usecs = strtol(argv[1], &endptr, 0);
|
|
|
|
if (!usecs || endptr == argv[1] || *endptr != '\0')
|
|
|
|
{
|
2008-08-16 20:39:46 +02:00
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
2008-08-17 20:59:50 +02:00
|
|
|
return ERROR;
|
2008-08-11 00:02:19 +02:00
|
|
|
}
|
|
|
|
usleep(usecs);
|
2008-08-17 20:59:50 +02:00
|
|
|
return OK;
|
2008-08-11 00:02:19 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-11-14 16:11:26 +01:00
|
|
|
#endif
|