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 "shcmd.h" 330d163575Sopenharmony_ci#include "shmsg.h" 340d163575Sopenharmony_ci#include "unistd.h" 350d163575Sopenharmony_ci#include "stdio.h" 360d163575Sopenharmony_ci#include "time.h" 370d163575Sopenharmony_ci#include "los_event.h" 380d163575Sopenharmony_ci#include "los_tick.h" 390d163575Sopenharmony_ci 400d163575Sopenharmony_ci#include "securec.h" 410d163575Sopenharmony_ci 420d163575Sopenharmony_ci#define timeval64 timeval 430d163575Sopenharmony_ci#define gettimeofday64 gettimeofday 440d163575Sopenharmony_ci#define ctime64 ctime 450d163575Sopenharmony_ci 460d163575Sopenharmony_ci#ifdef LOSCFG_SHELL_CMD_DEBUG 470d163575Sopenharmony_citypedef struct { 480d163575Sopenharmony_ci BOOL title; /* whether to hide the timestamps */ 490d163575Sopenharmony_ci UINT32 count; /* the total number of command executions */ 500d163575Sopenharmony_ci UINT32 interval; /* running cycle of the command */ 510d163575Sopenharmony_ci EVENT_CB_S watchEvent; /* event handle of the watch structure */ 520d163575Sopenharmony_ci CHAR cmdbuf[CMD_MAX_LEN]; /* the command to watch */ 530d163575Sopenharmony_ci} WatchCB; 540d163575Sopenharmony_ci 550d163575Sopenharmony_ciSTATIC WatchCB *g_watchCmd; 560d163575Sopenharmony_ci 570d163575Sopenharmony_ci#define WATCH_COUNT_MAX 0xFFFFFF 580d163575Sopenharmony_ci#define WATCH_INTERTVAL_MAX 0xFFFFFF 590d163575Sopenharmony_ci 600d163575Sopenharmony_ciSTATIC VOID PrintTime(VOID) 610d163575Sopenharmony_ci{ 620d163575Sopenharmony_ci struct timeval64 stNowTime = {0}; 630d163575Sopenharmony_ci 640d163575Sopenharmony_ci if (gettimeofday64(&stNowTime, NULL) == 0) { 650d163575Sopenharmony_ci PRINTK("%s", ctime64(&(stNowTime.tv_sec))); 660d163575Sopenharmony_ci } 670d163575Sopenharmony_ci} 680d163575Sopenharmony_ci 690d163575Sopenharmony_ciSTATIC VOID OsShellCmdDoWatch(UINTPTR arg1) 700d163575Sopenharmony_ci{ 710d163575Sopenharmony_ci WatchCB *watchItem = (WatchCB *)arg1; 720d163575Sopenharmony_ci UINT32 ret; 730d163575Sopenharmony_ci g_watchCmd = watchItem; 740d163575Sopenharmony_ci 750d163575Sopenharmony_ci while (watchItem->count--) { 760d163575Sopenharmony_ci printf("\033[2J\n"); 770d163575Sopenharmony_ci if (watchItem->title) { 780d163575Sopenharmony_ci PrintTime(); 790d163575Sopenharmony_ci } 800d163575Sopenharmony_ci (VOID)ShellMsgParse(watchItem->cmdbuf); 810d163575Sopenharmony_ci ret = LOS_EventRead(&watchItem->watchEvent, 0x01, LOS_WAITMODE_OR | LOS_WAITMODE_CLR, watchItem->interval); 820d163575Sopenharmony_ci if (ret == 0x01) { 830d163575Sopenharmony_ci break; 840d163575Sopenharmony_ci } 850d163575Sopenharmony_ci } 860d163575Sopenharmony_ci 870d163575Sopenharmony_ci (VOID)LOS_EventDestroy(&watchItem->watchEvent); 880d163575Sopenharmony_ci free(g_watchCmd); 890d163575Sopenharmony_ci g_watchCmd = NULL; 900d163575Sopenharmony_ci} 910d163575Sopenharmony_ci 920d163575Sopenharmony_ciSTATIC INLINE VOID OsWatchCmdUsage(VOID) 930d163575Sopenharmony_ci{ 940d163575Sopenharmony_ci PRINTK("\nUsage: watch\n"); 950d163575Sopenharmony_ci PRINTK("watch [options] command\n"); 960d163575Sopenharmony_ci} 970d163575Sopenharmony_ci 980d163575Sopenharmony_ciSTATIC UINT32 OsWatchOverFunc(VOID) 990d163575Sopenharmony_ci{ 1000d163575Sopenharmony_ci UINT32 ret; 1010d163575Sopenharmony_ci if (g_watchCmd != NULL) { 1020d163575Sopenharmony_ci ret = LOS_EventWrite(&g_watchCmd->watchEvent, 0x01); 1030d163575Sopenharmony_ci if (ret != LOS_OK) { 1040d163575Sopenharmony_ci PRINT_ERR("Write event failed in %s,%d\n", __FUNCTION__, __LINE__); 1050d163575Sopenharmony_ci return OS_ERROR; 1060d163575Sopenharmony_ci } 1070d163575Sopenharmony_ci return LOS_OK; 1080d163575Sopenharmony_ci } else { 1090d163575Sopenharmony_ci PRINTK("No watch task to turn off.\n"); 1100d163575Sopenharmony_ci return OS_ERROR; 1110d163575Sopenharmony_ci } 1120d163575Sopenharmony_ci} 1130d163575Sopenharmony_ci 1140d163575Sopenharmony_ciINT32 OsWatchOptionParsed(UINT32 argc, UINT32 *argoff, const CHAR **argv, WatchCB *watchItem) 1150d163575Sopenharmony_ci{ 1160d163575Sopenharmony_ci long tmpVal; 1170d163575Sopenharmony_ci CHAR *strPtr = NULL; 1180d163575Sopenharmony_ci UINT32 argcount = argc; 1190d163575Sopenharmony_ci 1200d163575Sopenharmony_ci while (argv[*argoff][0] == '-') { 1210d163575Sopenharmony_ci if (argcount <= 1) { 1220d163575Sopenharmony_ci OsWatchCmdUsage(); 1230d163575Sopenharmony_ci return -1; 1240d163575Sopenharmony_ci } 1250d163575Sopenharmony_ci 1260d163575Sopenharmony_ci if ((strcmp(argv[*argoff], "-n") == 0) || (strcmp(argv[*argoff], "--interval") == 0)) { 1270d163575Sopenharmony_ci if (argcount <= 2) { /* 2:count of parameter */ 1280d163575Sopenharmony_ci OsWatchCmdUsage(); 1290d163575Sopenharmony_ci return -1; 1300d163575Sopenharmony_ci } 1310d163575Sopenharmony_ci tmpVal = (long)strtoul(argv[*argoff + 1], &strPtr, 0); 1320d163575Sopenharmony_ci if ((*strPtr != 0) || (tmpVal <= 0) || (tmpVal > WATCH_INTERTVAL_MAX)) { 1330d163575Sopenharmony_ci PRINTK("\ninterval time is invalid\n"); 1340d163575Sopenharmony_ci OsWatchCmdUsage(); 1350d163575Sopenharmony_ci return -1; 1360d163575Sopenharmony_ci } 1370d163575Sopenharmony_ci watchItem->interval = g_tickPerSecond * (UINT32)tmpVal; 1380d163575Sopenharmony_ci argcount -= 2; /* 2:offset of argv */ 1390d163575Sopenharmony_ci (*argoff) += 2; /* 2:offset of argv */ 1400d163575Sopenharmony_ci } else if ((strcmp(argv[*argoff], "-t") == 0) || (strcmp(argv[*argoff], "-no-title") == 0)) { 1410d163575Sopenharmony_ci watchItem->title = FALSE; 1420d163575Sopenharmony_ci argcount--; 1430d163575Sopenharmony_ci (*argoff)++; 1440d163575Sopenharmony_ci } else if ((strcmp(argv[*argoff], "-c") == 0) || (strcmp(argv[*argoff], "--count") == 0)) { 1450d163575Sopenharmony_ci if (argcount <= 2) { /* 2:count of parameter */ 1460d163575Sopenharmony_ci OsWatchCmdUsage(); 1470d163575Sopenharmony_ci return -1; 1480d163575Sopenharmony_ci } 1490d163575Sopenharmony_ci tmpVal = (long)strtoul(argv[*argoff + 1], &strPtr, 0); 1500d163575Sopenharmony_ci if ((*strPtr != 0) || (tmpVal <= 0) || (tmpVal > WATCH_COUNT_MAX)) { 1510d163575Sopenharmony_ci PRINTK("\ncount is invalid\n"); 1520d163575Sopenharmony_ci OsWatchCmdUsage(); 1530d163575Sopenharmony_ci return -1; 1540d163575Sopenharmony_ci } 1550d163575Sopenharmony_ci watchItem->count = (UINT32)tmpVal; 1560d163575Sopenharmony_ci argcount -= 2; /* 2:offset of argv */ 1570d163575Sopenharmony_ci (*argoff) += 2; /* 2:offset of argv */ 1580d163575Sopenharmony_ci } else { 1590d163575Sopenharmony_ci PRINTK("Unknown option.\n"); 1600d163575Sopenharmony_ci return -1; 1610d163575Sopenharmony_ci } 1620d163575Sopenharmony_ci } 1630d163575Sopenharmony_ci return 0; 1640d163575Sopenharmony_ci} 1650d163575Sopenharmony_ci 1660d163575Sopenharmony_ciINT32 OsWatchCmdSplice(UINT32 argc, UINT32 argoff, const CHAR **argv, WatchCB *watchItem) 1670d163575Sopenharmony_ci{ 1680d163575Sopenharmony_ci INT32 err = 0; 1690d163575Sopenharmony_ci if ((argc - argoff) == 0) { 1700d163575Sopenharmony_ci PRINT_ERR("no watch command!\n"); 1710d163575Sopenharmony_ci return -1; 1720d163575Sopenharmony_ci } 1730d163575Sopenharmony_ci while (argc - argoff) { 1740d163575Sopenharmony_ci err = strcat_s(watchItem->cmdbuf, sizeof(watchItem->cmdbuf), argv[argoff]); 1750d163575Sopenharmony_ci if (err != EOK) { 1760d163575Sopenharmony_ci PRINT_ERR("%s, %d strcat_s failed!\n", __FUNCTION__, __LINE__); 1770d163575Sopenharmony_ci return -1; 1780d163575Sopenharmony_ci } 1790d163575Sopenharmony_ci err = strcat_s(watchItem->cmdbuf, sizeof(watchItem->cmdbuf), " "); 1800d163575Sopenharmony_ci if (err != EOK) { 1810d163575Sopenharmony_ci PRINT_ERR("%s, %d strcat_s failed!\n", __FUNCTION__, __LINE__); 1820d163575Sopenharmony_ci return -1; 1830d163575Sopenharmony_ci } 1840d163575Sopenharmony_ci argoff++; 1850d163575Sopenharmony_ci } 1860d163575Sopenharmony_ci return err; 1870d163575Sopenharmony_ci} 1880d163575Sopenharmony_ci 1890d163575Sopenharmony_ciUINT32 OsWatchTaskCreate(WatchCB *watchItem) 1900d163575Sopenharmony_ci{ 1910d163575Sopenharmony_ci TSK_INIT_PARAM_S initParam = {0}; 1920d163575Sopenharmony_ci UINT32 watchTaskId = 0; 1930d163575Sopenharmony_ci UINT32 ret; 1940d163575Sopenharmony_ci 1950d163575Sopenharmony_ci ret = LOS_EventInit(&watchItem->watchEvent); 1960d163575Sopenharmony_ci if (ret != 0) { 1970d163575Sopenharmony_ci PRINT_ERR("Watch event init failed in %s, %d\n", __FUNCTION__, __LINE__); 1980d163575Sopenharmony_ci return ret; 1990d163575Sopenharmony_ci } 2000d163575Sopenharmony_ci 2010d163575Sopenharmony_ci initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)OsShellCmdDoWatch; 2020d163575Sopenharmony_ci initParam.usTaskPrio = 10; /* 10:shellcmd_watch task priority */ 2030d163575Sopenharmony_ci initParam.auwArgs[0] = (UINTPTR)watchItem; 2040d163575Sopenharmony_ci initParam.uwStackSize = 0x3000; /* 0x3000:stack size of shellcmd_watch task */ 2050d163575Sopenharmony_ci initParam.pcName = "shellcmd_watch"; 2060d163575Sopenharmony_ci initParam.uwResved = LOS_TASK_STATUS_DETACHED; 2070d163575Sopenharmony_ci 2080d163575Sopenharmony_ci ret = LOS_TaskCreate(&watchTaskId, &initParam); 2090d163575Sopenharmony_ci if (ret != 0) { 2100d163575Sopenharmony_ci PRINT_ERR("Watch task init failed in %s, %d\n", __FUNCTION__, __LINE__); 2110d163575Sopenharmony_ci return ret; 2120d163575Sopenharmony_ci } 2130d163575Sopenharmony_ci return ret; 2140d163575Sopenharmony_ci} 2150d163575Sopenharmony_ci 2160d163575Sopenharmony_ciUINT32 OsShellCmdWatch(UINT32 argc, const CHAR **argv) 2170d163575Sopenharmony_ci{ 2180d163575Sopenharmony_ci WatchCB *watchItem = NULL; 2190d163575Sopenharmony_ci UINT32 argoff = 0; 2200d163575Sopenharmony_ci UINT32 ret; 2210d163575Sopenharmony_ci INT32 err; 2220d163575Sopenharmony_ci 2230d163575Sopenharmony_ci if (argc == 0) { 2240d163575Sopenharmony_ci OsWatchCmdUsage(); 2250d163575Sopenharmony_ci return OS_ERROR; 2260d163575Sopenharmony_ci } 2270d163575Sopenharmony_ci 2280d163575Sopenharmony_ci if (argv == NULL) { 2290d163575Sopenharmony_ci OsWatchCmdUsage(); 2300d163575Sopenharmony_ci return OS_ERROR; 2310d163575Sopenharmony_ci } 2320d163575Sopenharmony_ci 2330d163575Sopenharmony_ci if ((argc == 1) && (strcmp(argv[0], "--over") == 0)) { 2340d163575Sopenharmony_ci ret = OsWatchOverFunc(); 2350d163575Sopenharmony_ci return ret; 2360d163575Sopenharmony_ci } 2370d163575Sopenharmony_ci 2380d163575Sopenharmony_ci if (g_watchCmd != NULL) { 2390d163575Sopenharmony_ci PRINTK("Please turn off previous watch before to start a new watch.\n"); 2400d163575Sopenharmony_ci return OS_ERROR; 2410d163575Sopenharmony_ci } 2420d163575Sopenharmony_ci 2430d163575Sopenharmony_ci watchItem = (WatchCB *)malloc(sizeof(WatchCB)); 2440d163575Sopenharmony_ci if (watchItem == NULL) { 2450d163575Sopenharmony_ci PRINTK("Malloc error!\n"); 2460d163575Sopenharmony_ci return OS_ERROR; 2470d163575Sopenharmony_ci } 2480d163575Sopenharmony_ci (VOID)memset_s(watchItem, sizeof(WatchCB), 0, sizeof(WatchCB)); 2490d163575Sopenharmony_ci watchItem->title = TRUE; 2500d163575Sopenharmony_ci watchItem->count = WATCH_COUNT_MAX; 2510d163575Sopenharmony_ci watchItem->interval = g_tickPerSecond; 2520d163575Sopenharmony_ci 2530d163575Sopenharmony_ci err = OsWatchOptionParsed(argc, &argoff, argv, watchItem); 2540d163575Sopenharmony_ci if (err != 0) { 2550d163575Sopenharmony_ci goto WATCH_ERROR; 2560d163575Sopenharmony_ci } 2570d163575Sopenharmony_ci 2580d163575Sopenharmony_ci err = OsWatchCmdSplice(argc, argoff, argv, watchItem); 2590d163575Sopenharmony_ci if (err != 0) { 2600d163575Sopenharmony_ci goto WATCH_ERROR; 2610d163575Sopenharmony_ci } 2620d163575Sopenharmony_ci 2630d163575Sopenharmony_ci ret = OsWatchTaskCreate(watchItem); 2640d163575Sopenharmony_ci if (ret != 0) { 2650d163575Sopenharmony_ci goto WATCH_ERROR; 2660d163575Sopenharmony_ci } 2670d163575Sopenharmony_ci 2680d163575Sopenharmony_ci return LOS_OK; 2690d163575Sopenharmony_ci 2700d163575Sopenharmony_ciWATCH_ERROR: 2710d163575Sopenharmony_ci free(watchItem); 2720d163575Sopenharmony_ci return OS_ERROR; 2730d163575Sopenharmony_ci} 2740d163575Sopenharmony_ci 2750d163575Sopenharmony_ciSHELLCMD_ENTRY(watch_shellcmd, CMD_TYPE_EX, "watch", XARGS, (CmdCallBackFunc)OsShellCmdWatch); 2760d163575Sopenharmony_ci#endif 277