xref: /kernel/liteos_a/shell/full/src/base/show.c (revision 0d163575)
10d163575Sopenharmony_ci/*
20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
40d163575Sopenharmony_ci *
50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
60d163575Sopenharmony_ci * are permitted provided that the following conditions are met:
70d163575Sopenharmony_ci *
80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
90d163575Sopenharmony_ci *    conditions and the following disclaimer.
100d163575Sopenharmony_ci *
110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
120d163575Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
130d163575Sopenharmony_ci *    provided with the distribution.
140d163575Sopenharmony_ci *
150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
160d163575Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
170d163575Sopenharmony_ci *    permission.
180d163575Sopenharmony_ci *
190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
300d163575Sopenharmony_ci */
310d163575Sopenharmony_ci
320d163575Sopenharmony_ci#include "show.h"
330d163575Sopenharmony_ci#include "shmsg.h"
340d163575Sopenharmony_ci#include "shcmd.h"
350d163575Sopenharmony_ci#include "console.h"
360d163575Sopenharmony_ci
370d163575Sopenharmony_ci
380d163575Sopenharmony_ciSTATIC BOOL g_shellSourceFlag = FALSE;
390d163575Sopenharmony_ci
400d163575Sopenharmony_ciSTATIC UINT32 OsShellCmdInit(VOID)
410d163575Sopenharmony_ci{
420d163575Sopenharmony_ci    UINT32 ret = OsCmdInit();
430d163575Sopenharmony_ci    if (ret != LOS_OK) {
440d163575Sopenharmony_ci        return ret;
450d163575Sopenharmony_ci    }
460d163575Sopenharmony_ci
470d163575Sopenharmony_ci    return OsShellSysCmdRegister();
480d163575Sopenharmony_ci}
490d163575Sopenharmony_ci
500d163575Sopenharmony_ciSTATIC UINT32 OsShellCreateTask(ShellCB *shellCB)
510d163575Sopenharmony_ci{
520d163575Sopenharmony_ci    UINT32 ret = ShellTaskInit(shellCB);
530d163575Sopenharmony_ci    if (ret != LOS_OK) {
540d163575Sopenharmony_ci        return ret;
550d163575Sopenharmony_ci    }
560d163575Sopenharmony_ci
570d163575Sopenharmony_ci    return ShellEntryInit(shellCB);
580d163575Sopenharmony_ci}
590d163575Sopenharmony_ci
600d163575Sopenharmony_ciSTATIC UINT32 OsShellSourceInit(INT32 consoleId)
610d163575Sopenharmony_ci{
620d163575Sopenharmony_ci    UINT32 ret = LOS_NOK;
630d163575Sopenharmony_ci    CONSOLE_CB *consoleCB = OsGetConsoleByID(consoleId);
640d163575Sopenharmony_ci    if ((consoleCB == NULL) || (consoleCB->shellHandle != NULL)) {
650d163575Sopenharmony_ci        return LOS_NOK;
660d163575Sopenharmony_ci    }
670d163575Sopenharmony_ci    consoleCB->shellHandle = LOS_MemAlloc((VOID *)m_aucSysMem0, sizeof(ShellCB));
680d163575Sopenharmony_ci    if (consoleCB->shellHandle == NULL) {
690d163575Sopenharmony_ci        return LOS_NOK;
700d163575Sopenharmony_ci    }
710d163575Sopenharmony_ci    ShellCB *shellCB = (ShellCB *)consoleCB->shellHandle;
720d163575Sopenharmony_ci    if (memset_s(shellCB, sizeof(ShellCB), 0, sizeof(ShellCB)) != EOK) {
730d163575Sopenharmony_ci        goto ERR_OUT1;
740d163575Sopenharmony_ci    }
750d163575Sopenharmony_ci
760d163575Sopenharmony_ci    shellCB->consoleID = (UINT32)consoleId;
770d163575Sopenharmony_ci    ret = (UINT32)pthread_mutex_init(&shellCB->keyMutex, NULL);
780d163575Sopenharmony_ci    if (ret != LOS_OK) {
790d163575Sopenharmony_ci        goto ERR_OUT1;
800d163575Sopenharmony_ci    }
810d163575Sopenharmony_ci    ret = (UINT32)pthread_mutex_init(&shellCB->historyMutex, NULL);
820d163575Sopenharmony_ci    if (ret != LOS_OK) {
830d163575Sopenharmony_ci        goto ERR_OUT2;
840d163575Sopenharmony_ci    }
850d163575Sopenharmony_ci
860d163575Sopenharmony_ci    ret = OsShellKeyInit(shellCB);
870d163575Sopenharmony_ci    if (ret != LOS_OK) {
880d163575Sopenharmony_ci        goto ERR_OUT3;
890d163575Sopenharmony_ci    }
900d163575Sopenharmony_ci    if (strncpy_s(shellCB->shellWorkingDirectory, PATH_MAX, "/", 2) != EOK) { /* 2:space for "/" */
910d163575Sopenharmony_ci        ret = LOS_NOK;
920d163575Sopenharmony_ci        goto ERR_OUT4;
930d163575Sopenharmony_ci    }
940d163575Sopenharmony_ci#if !defined(LOSCFG_PLATFORM_ROOTFS)
950d163575Sopenharmony_ci    /*
960d163575Sopenharmony_ci     * In case of ROOTFS disabled but
970d163575Sopenharmony_ci     * serial console enabled, it is required
980d163575Sopenharmony_ci     * to create Shell task in kernel for it.
990d163575Sopenharmony_ci     */
1000d163575Sopenharmony_ci    if (consoleId == CONSOLE_TELNET || consoleId == CONSOLE_SERIAL) {
1010d163575Sopenharmony_ci#else
1020d163575Sopenharmony_ci    if (consoleId == CONSOLE_TELNET) {
1030d163575Sopenharmony_ci#endif
1040d163575Sopenharmony_ci        ret = OsShellCreateTask(shellCB);
1050d163575Sopenharmony_ci        if (ret != LOS_OK) {
1060d163575Sopenharmony_ci            goto ERR_OUT4;
1070d163575Sopenharmony_ci        }
1080d163575Sopenharmony_ci    }
1090d163575Sopenharmony_ci
1100d163575Sopenharmony_ci    return LOS_OK;
1110d163575Sopenharmony_ciERR_OUT4:
1120d163575Sopenharmony_ci    (VOID)LOS_MemFree((VOID *)m_aucSysMem0, shellCB->cmdKeyLink);
1130d163575Sopenharmony_ci    (VOID)LOS_MemFree((VOID *)m_aucSysMem0, shellCB->cmdHistoryKeyLink);
1140d163575Sopenharmony_ciERR_OUT3:
1150d163575Sopenharmony_ci    (VOID)pthread_mutex_destroy(&shellCB->historyMutex);
1160d163575Sopenharmony_ciERR_OUT2:
1170d163575Sopenharmony_ci    (VOID)pthread_mutex_destroy(&shellCB->keyMutex);
1180d163575Sopenharmony_ciERR_OUT1:
1190d163575Sopenharmony_ci    (VOID)LOS_MemFree((VOID *)m_aucSysMem0, shellCB);
1200d163575Sopenharmony_ci    consoleCB->shellHandle = NULL;
1210d163575Sopenharmony_ci    return ret;
1220d163575Sopenharmony_ci}
1230d163575Sopenharmony_ci
1240d163575Sopenharmony_ciUINT32 OsShellInit(INT32 consoleId)
1250d163575Sopenharmony_ci{
1260d163575Sopenharmony_ci    if (g_shellSourceFlag == FALSE) {
1270d163575Sopenharmony_ci        UINT32 ret = OsShellCmdInit();
1280d163575Sopenharmony_ci        if (ret == LOS_OK) {
1290d163575Sopenharmony_ci            g_shellSourceFlag = TRUE;
1300d163575Sopenharmony_ci        } else {
1310d163575Sopenharmony_ci            return ret;
1320d163575Sopenharmony_ci        }
1330d163575Sopenharmony_ci    }
1340d163575Sopenharmony_ci    return OsShellSourceInit(consoleId);
1350d163575Sopenharmony_ci}
1360d163575Sopenharmony_ci
1370d163575Sopenharmony_ciINT32 OsShellDeinit(INT32 consoleId)
1380d163575Sopenharmony_ci{
1390d163575Sopenharmony_ci    CONSOLE_CB *consoleCB = NULL;
1400d163575Sopenharmony_ci    ShellCB *shellCB = NULL;
1410d163575Sopenharmony_ci
1420d163575Sopenharmony_ci    consoleCB = OsGetConsoleByID(consoleId);
1430d163575Sopenharmony_ci    if (consoleCB == NULL) {
1440d163575Sopenharmony_ci        PRINT_ERR("shell deinit error.\n");
1450d163575Sopenharmony_ci        return -1;
1460d163575Sopenharmony_ci    }
1470d163575Sopenharmony_ci
1480d163575Sopenharmony_ci    shellCB = (ShellCB *)consoleCB->shellHandle;
1490d163575Sopenharmony_ci    consoleCB->shellHandle = NULL;
1500d163575Sopenharmony_ci    if (shellCB == NULL) {
1510d163575Sopenharmony_ci        PRINT_ERR("shell deinit error.\n");
1520d163575Sopenharmony_ci        return -1;
1530d163575Sopenharmony_ci    }
1540d163575Sopenharmony_ci
1550d163575Sopenharmony_ci    (VOID)LOS_TaskDelete(shellCB->shellEntryHandle);
1560d163575Sopenharmony_ci    (VOID)LOS_EventWrite(&shellCB->shellEvent, CONSOLE_SHELL_KEY_EVENT);
1570d163575Sopenharmony_ci
1580d163575Sopenharmony_ci    return 0;
1590d163575Sopenharmony_ci}
1600d163575Sopenharmony_ci
1610d163575Sopenharmony_ciCHAR *OsShellGetWorkingDirectory(VOID)
1620d163575Sopenharmony_ci{
1630d163575Sopenharmony_ci    CONSOLE_CB *consoleCB = OsGetConsoleByTaskID(OsCurrTaskGet()->taskID);
1640d163575Sopenharmony_ci    ShellCB *shellCB = NULL;
1650d163575Sopenharmony_ci
1660d163575Sopenharmony_ci    if (consoleCB == NULL) {
1670d163575Sopenharmony_ci        return NULL;
1680d163575Sopenharmony_ci    }
1690d163575Sopenharmony_ci    shellCB = (ShellCB *)consoleCB->shellHandle;
1700d163575Sopenharmony_ci    if (shellCB == NULL) {
1710d163575Sopenharmony_ci        return NULL;
1720d163575Sopenharmony_ci    }
1730d163575Sopenharmony_ci    return shellCB->shellWorkingDirectory;
1740d163575Sopenharmony_ci}
1750d163575Sopenharmony_ci
176