/* ----------------------------------------------------------------------------
 * Copyright (c) Huawei Technologies Co., Ltd. 2013-2019. All rights reserved.
 * Description: ShellCmd SysInfo
 * Author: Huawei LiteOS Team
 * Create: 2013-01-01
 * 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.
 * 3. Neither the name of the copyright holder nor the names of its contributors may be 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 HOLDER 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.
 * --------------------------------------------------------------------------- */
/*
这段文件是针对LiteOS操作系统中的Shell命令编写的，
主要实现了获取系统信息的功能，获取的信息包括进程数量、信号量数量、互斥锁数量、消息队列数量和软件定时器数量。
*/
#include "los_config.h"
#include "los_swtmr.h"
#include "los_sem_pri.h"
#include "los_mux_pri.h"
#include "los_queue_pri.h"
#include "los_swtmr_pri.h"

#ifdef LOSCFG_SHELL
#include "shcmd.h"
#include "shell.h"
#endif

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */
//这个函数用于获取当系统的进程数量
UINT32 OsShellCmdTaskCntGet(VOID)
{
    UINT32 loop;
    UINT32 taskCnt = 0;
    UINT32 intSave;
    LosTaskCB *taskCB = NULL;
    //加上锁，用于防止中断
    intSave = LOS_IntLock();
    //遍历所有进程，并统计进程数量
    for (loop = 0; loop < g_taskMaxNum; loop++) {
        taskCB = (LosTaskCB *)g_taskCBArray + loop;
        //若该进程未使用，则直接跳过
        if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) {
            continue;
        }
        taskCnt++;
    }
    //为进程解锁
    LOS_IntRestore(intSave);
    //返回系统的进程数量
    return taskCnt;
}

#ifdef LOSCFG_BASE_IPC_SEM
//这个函数用于获取当前系统信号量的数量
UINT32 OsShellCmdSemCntGet(VOID)
{
    UINT32 loop;
    UINT32 semCnt = 0;
    UINT32 intSave;
    LosSemCB *semNode = NULL;
    //加上锁，用于防止中断
    intSave = LOS_IntLock();
    //遍历所有信号量
    for (loop = 0; loop < LOSCFG_BASE_IPC_SEM_LIMIT; loop++) {
        semNode = GET_SEM(loop);
        //如果该信号量被使用了，数量加1
        if (semNode->semStat == LOS_USED) {
            semCnt++;
        }
    }
    //为进程解锁
    LOS_IntRestore(intSave);
    //返回系统信号量的数量
    return semCnt;
}
#endif

#ifdef LOSCFG_BASE_IPC_MUX
//这个函数用于获取当前系统互斥锁的数量
UINT32 OsShellCmdMuxCntGet(VOID)
{
    UINT32 loop;
    UINT32 muxCnt = 0;
    UINT32 intSave;
    LosMuxCB *muxNode = NULL;
    //加上锁，用于防止中断
    intSave = LOS_IntLock();
    //遍历所有互斥锁
    for (loop = 0; loop < LOSCFG_BASE_IPC_MUX_LIMIT; loop++) {
        muxNode = GET_MUX(loop);
        //如果互斥锁的状态被使用了，数量加1
        if (muxNode->muxStat == LOS_USED) {
            muxCnt++;
        }
    }
    //为进程解锁
    LOS_IntRestore(intSave);
    //返回系统互斥锁的数量
    return muxCnt;
}
#endif

#ifdef LOSCFG_BASE_IPC_QUEUE
//这个函数用于获取系统消息队列的数量
UINT32 OsShellCmdQueueCntGet(VOID)
{
    UINT32 loop;
    UINT32 queueCnt = 0;
    UINT32 intSave;
    LosQueueCB *queueCB = NULL;
    //加上锁，用于防止中断
    intSave = LOS_IntLock();
    queueCB = g_allQueue;
    //遍历所有消息队列
    for (loop = 0; loop < LOSCFG_BASE_IPC_QUEUE_LIMIT; loop++, queueCB++) {
        //如果消息队列的状态为使用，数量加1
        if (queueCB->queueState == LOS_USED) {
            queueCnt++;
        }
    }
    //为进程解锁
    LOS_IntRestore(intSave);
    //返回系统获取消息队列的数量
    return queueCnt;
}
#endif

#ifdef LOSCFG_BASE_CORE_SWTMR
//这个函数用于获取系统软件定时器的数量
UINT32 OsShellCmdSwtmrCntGet(VOID)
{
    UINT32 loop;
    UINT32 swtmrCnt = 0;
    UINT32 intSave;
    LosSwtmrCB *swtmrCB = NULL;
    //加上锁，用于防止中断
    intSave = LOS_IntLock();
    swtmrCB = g_swtmrCBArray;
    //遍历所有软件定时器
    for (loop = 0; loop < LOSCFG_BASE_CORE_SWTMR_LIMIT; loop++, swtmrCB++) {
        //如果软件定时器的状态不是没有使用，数量加1
        if (swtmrCB->state != OS_SWTMR_STATUS_UNUSED) {
            swtmrCnt++;
        }
    }
    //为进程解锁
    LOS_IntRestore(intSave);
    //返回系统软件定时器的数量
    return swtmrCnt;
}
#endif
//这个函数用于打印系统信息，包括进程数量、信号量数量、互斥锁数量、消息队列数量和软件定时器数量。
LITE_OS_SEC_TEXT_MINOR VOID OsShellCmdSystemInfoGet(VOID)
{
    //打印表头
    PRINTK("\n   Module    Used      Total\n");
    PRINTK("--------------------------------\n");
    //打印进程数量
    PRINTK("   Task      %-10u%-10d\n",
           OsShellCmdTaskCntGet(),
           LOSCFG_BASE_CORE_TSK_LIMIT);
#ifdef LOSCFG_BASE_IPC_SEM
    //打印信号量数量
    PRINTK("   Sem       %-10u%-10d\n",
           OsShellCmdSemCntGet(),
           LOSCFG_BASE_IPC_SEM_LIMIT);
#endif
#ifdef LOSCFG_BASE_IPC_MUX
    //打印互斥锁数量
    PRINTK("   Mutex     %-10u%-10d\n",
           OsShellCmdMuxCntGet(),
           LOSCFG_BASE_IPC_MUX_LIMIT);
#endif
#ifdef LOSCFG_BASE_IPC_QUEUE
    //打印消息队列数量
    PRINTK("   Queue     %-10u%-10d\n",
           OsShellCmdQueueCntGet(),
           LOSCFG_BASE_IPC_QUEUE_LIMIT);
#endif
#ifdef LOSCFG_BASE_CORE_SWTMR
    //打印软件定时器数量
    PRINTK("   SwTmr     %-10u%-10d\n",
           OsShellCmdSwtmrCntGet(),
           LOSCFG_BASE_CORE_SWTMR_LIMIT);
#endif
}
//这个函数用于处理shell的systeminfo命令，根据相应的参数，做出相应的处理
INT32 OsShellCmdSystemInfo(INT32 argc, const CHAR **argv)
{
    //如果参数数量为0，调用OsShellCmdSystemInfoGet函数，打印所有的系统信息。
    if (argc == 0) {
        OsShellCmdSystemInfoGet();
        return 0;
    }
    //参数数量不正确，打印错误信息
    PRINTK("systeminfo: invalid option %s\n"
           "Systeminfo has NO ARGS.\n",
           argv[0]);
    return -1;
}

#ifdef LOSCFG_SHELL
//注册Shell命令systeminfo，调用OsShellCmdSystemInfo，根据参数输出当前系统的进程数量、信号量数量、互斥锁数量、消息队列数量和软件定时器数量。。
SHELLCMD_ENTRY(systeminfo_shellcmd, CMD_TYPE_EX, "systeminfo", 1, (CmdCallBackFunc)OsShellCmdSystemInfo);
#endif /* LOSCFG_SHELL */

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
