Files
kunlun/cli/host_interface/iot_cli_dbg_log.c
2024-09-28 14:24:04 +08:00

229 lines
6.6 KiB
C

/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
#include "iot_io_api.h"
#include "iot_cli_tx_rx.h"
#include "iot_cli_host_interface.h"
#include "iot_cli_dbg_log.h"
#include "iot_cli_flash_log.h"
#include "iot_cli_ckb.h"
extern module_group_config config_module[MODULE_GROUP_NUM];
extern iot_cli_host_info_t *host_info;
extern iot_cli_t cli;
CLI_REGULAR_LOG cli_regular_log_list[4] = { 0 };
void cli_config_dbglog_level(
uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac)
{
(void)src_mac;
cli_host_set_dbglog_level *level =
(cli_host_set_dbglog_level *)buffer;
cli_host_set_dbglog_level_ack ack;
if ((!level) || (bufferlen < sizeof(*level))) {
return;
}
iot_dbglog_config_level(level->general_level);
for (uint16_t i = 0; i < level->group_cnt; i++)
{
iot_dbglog_config_module_level(
level->module_level[i].module_id,
level->module_level[i].level);
}
iot_dbglog_start_stop_live_capture(1, src_mac);
ack.result = 0;
iot_cli_send_to_host(CLI_MSGID_SET_DEBUG_LOG_LEVEL_ACK,
(uint8_t*)&ack, sizeof(ack), src_mac);
}
void cli_get_dbglog_level(
uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac)
{
(void)src_mac;
(void)buffer;
(void)bufferlen;
cli_host_dbglog_level dbglog_level;
dbglog_level.level = iot_dbglog_get_level();
dbglog_level.max_level = DBGLOG_LVL_MAX;
dbglog_level.group_cnt = MODULE_GROUP_NUM;
for (uint16_t i = 0; i < MODULE_GROUP_NUM; i++)
{
os_mem_cpy(
dbglog_level.module[i].group_name,
config_module[i].group_name,
DBGLOG_MODULE_GROUP_NAME_LEN);
dbglog_level.module[i].module_id = i;
dbglog_level.module[i].level = iot_dbglog_get_module_level(i);
}
add_addr_to_mapping_table(src_mac);
iot_cli_send_to_host(CLI_MSGID_GET_DBGLOG_LEVEL_RESP,
(uint8_t*)&dbglog_level, sizeof(dbglog_level), src_mac);
}
void cli_start_stop_dbglog_live_capture(
uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac)
{
(void)src_mac;
cli_host_enable_dbglog *enable =
(cli_host_enable_dbglog *)buffer;
cli_host_enable_dbglog_ack ack;
if ((!enable) || (bufferlen < sizeof(*enable))) {
return;
}
iot_printf("start live log (%d)\n", enable->enabled);
iot_dbglog_start_stop_live_capture(enable->enabled, src_mac);
ack.result = 0;
add_addr_to_mapping_table(src_mac);
iot_cli_send_to_host(CLI_MSGID_START_STOP_LIVE_LOG_CAPTURE_ACK,
(uint8_t*)&ack, sizeof(ack), src_mac);
}
void cli_send_live_log_to_plc(
uint8_t *buffer, uint32_t bufferlen)
{
uint8_t* receiver = iot_dbglog_live_log_receiver();
if (receiver != NULL) {
iot_cli_send_to_host(
CLI_MSGID_SEND_LIVE_LOG,
buffer, bufferlen, receiver);
}
}
static void iot_cli_log_timer_func(timer_id_t timer_id, void *arg)
{
(void)arg;
(void)timer_id;
iot_task_msg_t *t_msg;
t_msg = iot_cli_create_cli_msg(IOT_CLI_LOG_TIMER, NULL);
if (t_msg) {
iot_task_queue_msg(host_info->host_task_h,
t_msg, IOT_CLI_QUEUE_TIMER);
}
}
void iot_cli_dbg_log_init()
{
#if (HW_PLATFORM != HW_PLATFORM_SIMU)
host_info->log_timer = os_create_timer(IOT_CLI_MID, 1,
iot_cli_log_timer_func, NULL);
if (host_info->log_timer) {
os_start_timer(host_info->log_timer, CLI_LOG_TIMER_PERIOD);
}
#endif
}
void iot_cli_handle_timer_log()
{
iot_printf("iot_cli_log_timer_func\n");
for (int i = 0; i < (sizeof(cli_regular_log_list) /
sizeof(cli_regular_log_list[0])); i++) {
if (cli_regular_log_list[i] != NULL) {
cli_regular_log_list[i]();
}
}
}
void register_regular_log(CLI_REGULAR_LOG cb)
{
for (int i = 0; i < (sizeof(cli_regular_log_list) /
sizeof(cli_regular_log_list[0])); i++) {
if (cli_regular_log_list[i] == cb) {
return;
} else if (cli_regular_log_list[i] == NULL) {
cli_regular_log_list[i] = cb;
return;
}
}
}
static void send_crash_dbglog(uint8_t* data, uint32_t datalen)
{
uint32_t write_offset = add_flashlog_tag_preamble(data);
cli_msg_hdr_t *msg = (cli_msg_hdr_t *)(data + write_offset);
msg->module_id = CLI_MODULEID_DEBUGLOG;
msg->msg_id = CLI_MSGID_DEBUG_LOG_REPORT;
msg->msg_len = (uint16_t)datalen;
iot_mac_addr_cpy(msg->src_mac, host_info->mac_addr);
write_offset += sizeof(cli_msg_hdr_t);
write_offset += datalen;
write_offset += add_flashlog_tag_backcode(data + write_offset);
write_log_nolock(data, write_offset);
}
void send_live_dbglog(iot_pkt_t* data)
{
uint32_t datalen = iot_pkt_data_len(data);
uint8_t* clihdr = iot_pkt_push(data, sizeof(cli_msg_hdr_t));
if (clihdr) {
cli_msg_hdr_t *msg = (cli_msg_hdr_t *)clihdr;
msg->module_id = CLI_MODULEID_DEBUGLOG;
msg->msg_id = CLI_MSGID_DEBUG_LOG_REPORT;
msg->msg_len = (uint16_t)datalen;
iot_mac_addr_cpy(msg->src_mac, host_info->mac_addr);
}
if (iot_dbglog_live_capture_enabled()) {
uint32_t len = add_flashlog_tag(data);
cli_send_live_log_to_plc(iot_pkt_data(data), len);
iot_pkt_free(data);
return;
}
#if IOT_LOG_TO_FLASH_ENABLE
if (cli.need_download_cli_to_flash) {
download_log_to_flash(data);
}
else {
send_tx_msg_to_cli_task(data);
}
#else
send_tx_msg_to_cli_task(data);
#endif
}
void cli_send_dbglog(void* buffer, uint8_t crash_log, uint32_t buffer_len)
{
if (buffer == NULL) {
return;
}
if (crash_log == 0) {
iot_pkt_t* data = (iot_pkt_t*)buffer;
send_live_dbglog(data);
}
else {
uint8_t* data = (uint8_t*)buffer;
if (buffer_len == 0) {
return;
}
send_crash_dbglog(data, buffer_len);
}
}