1 /*
2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <stdio.h>
16 #include <stdarg.h>
17 #include "hal_cmu.h"
18 #include "hm_sys.h"
19 #include "pmu.h"
20 #include "hal_norflash.h"
21 #include "cmsis.h"
22 #include "hal_trace.h"
23 #include "los_memory.h"
24 #include "los_task.h"
25 #include "console.h"
26 #include "hiview_util.h"
27 #include "hiview_output_log.h"
28 #include "hiview_log.h"
29 #include "devmgr_service_start.h"
30 #include "ohos_mem_pool.h"
31 //#include "threading_alt.h"
32 #include <dirent.h>
33 #include <errno.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #include "ohos_init.h"
38
OsShowInfo(void)39 void OsShowInfo(void)
40 {
41 static uint32_t last_pool_total_used_size = 0;
42 LOS_MEM_POOL_STATUS mem_pool_status = {0};
43 LOS_MemInfoGet(OS_SYS_MEM_ADDR, &mem_pool_status);
44
45 int diff = mem_pool_status.totalUsedSize - last_pool_total_used_size;
46 last_pool_total_used_size = mem_pool_status.totalUsedSize;
47
48 printf("====================================================\n");
49 printf("freeNodeNum = %d\n", mem_pool_status.freeNodeNum);
50 printf("maxFreeNodeSize = %d\n", mem_pool_status.maxFreeNodeSize);
51 printf("totalFreeSize = %d\n", mem_pool_status.totalFreeSize);
52 printf("totalUsedSize = %d\n", mem_pool_status.totalUsedSize);
53 printf("usage_mem = %d\n", diff);
54 printf("usageWaterLine = %d\n", mem_pool_status.usageWaterLine);
55 printf("usedNodeNum = %d\n", mem_pool_status.usedNodeNum);
56 printf("====================================================\n");
57 OsGetAllTskInfo();
58 }
59
doShowOsInfo(cmd_tbl_t * cmd,int argc,char * argv[])60 static int doShowOsInfo(cmd_tbl_t *cmd, int argc, char *argv[])
61 {
62 OsShowInfo();
63 return 0;
64 }
65
doFsLs(cmd_tbl_t * cmd,int argc,char * argv[])66 static int doFsLs(cmd_tbl_t *cmd, int argc, char *argv[])
67 {
68 if (argc < 2) {
69 printf("AT+LS=<path>\n");
70 return -1;
71 }
72 const char *path = argv[1];
73 DIR *dir;
74 struct dirent *dp;
75 if ((dir = opendir(path)) == NULL) {
76 printf("opendir %s failed, %s\n", path, strerror(errno));
77 return -1;
78 }
79 while ((dp = readdir(dir)) != NULL) {
80 if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) {
81 continue;
82 }
83 struct stat st_buf = {0};
84 char realpath[260];
85 snprintf(realpath, sizeof(realpath), "%s/%s", path, dp->d_name);
86 if (stat(realpath, &st_buf) != 0) {
87 printf("can not access %s\n", realpath);
88 closedir(dir);
89 return -1;
90 }
91 if ((st_buf.st_mode & S_IFMT) == S_IFDIR) {
92 printf("DIR %s\n", realpath);
93 } else {
94 printf("FILE %s, %ld bytes\n", realpath, st_buf.st_size);
95 }
96 }
97 closedir(dir);
98 printf("+ok\r\n");
99 return 0;
100 }
101
RegisterCustomATCmd()102 void RegisterCustomATCmd()
103 {
104 cmd_tbl_t cmd_list[] = {
105 {"AT+SHOWOSINFO", 1, doShowOsInfo, "AT+SHOWOSINFO - show memory and cpu usage\n"},
106 {"AT+LS", 2, doFsLs, "AT+LS - list file/dir in fs\n"},
107 };
108 for (int i = 0; i < sizeof(cmd_list) / sizeof(cmd_tbl_t); i++) {
109 console_cmd_add(&cmd_list[i]);
110 }
111 }
112 SYS_SERVICE_INIT(RegisterCustomATCmd);
113
HAL_NVIC_SystemReset(void)114 void HAL_NVIC_SystemReset(void)
115 {
116 }
117
118 /**
119 * @brief implement for hilog_lite featured/mini
120 * @notice hilog_lite mini converts '%s' to '%p'
121 */
HilogProc_Impl(const HiLogContent * hilogContent,uint32 len)122 boolean HilogProc_Impl(const HiLogContent *hilogContent, uint32 len)
123 {
124 char tempOutStr[LOG_FMT_MAX_LEN] = {0};
125 if (LogContentFmt(tempOutStr, sizeof(tempOutStr), hilogContent) > 0) {
126 printf(tempOutStr);
127 #ifdef LOG_FLUSH
128 hal_trace_flush_buffer();
129 #endif
130 }
131 return TRUE;
132 }
133
init_trace_system(void)134 int init_trace_system(void)
135 {
136 int ret = 1;
137 HiviewRegisterHilogProc(HilogProc_Impl);
138 return ret;
139 }
140
HiLogWriteInternal(const char * buffer,size_t bufLen)141 int HiLogWriteInternal(const char *buffer, size_t bufLen)
142 {
143 if (!buffer)
144 return -1;
145 // because it's called as HiLogWriteInternal(buf, strlen(buf) + 1)
146 if (bufLen < 2)
147 return 0;
148 if (buffer[bufLen - 2] != '\n') {
149 *((char *)buffer + bufLen - 1) = '\n';
150 } else {
151 bufLen--;
152 }
153 int ret = hal_trace_output((const unsigned char *)buffer, bufLen);
154 #ifdef LOG_FLUSH
155 hal_trace_flush_buffer();
156 #endif
157 return ret;
158 }
159
OhosSystemAdapterHooks(void)160 int OhosSystemAdapterHooks(void)
161 {
162 init_trace_system();
163 DeviceManagerStart();
164 return 0;
165 }
166
167 /**
168 * @brief implement for ohos_mem_pool.h
169 */
OhosMalloc(MemType type,uint32 size)170 void *OhosMalloc(MemType type, uint32 size)
171 {
172 return malloc(size);
173 }
174
OhosFree(void * ptr)175 void OhosFree(void *ptr)
176 {
177 free(ptr);
178 }
179
180 /**
181 * @brief adapter for js_ability.cpp
182 * #ifdef OHOS_ACELITE_PRODUCT_WATCH
183 */
RestoreSystemWrapper(const char * crashMessage)184 void RestoreSystemWrapper(const char *crashMessage)
185 {
186 printf("%s\n", crashMessage);
187 hal_trace_flush_buffer();
188 }
189
190
IoTWatchDogKick(void)191 void IoTWatchDogKick(void) {}
192