/*************************************************************************
> File Name: main.c
> Author: Zhoulei
> Email: zhoulei@momenta.ai
> Created Time: Fri 16 Oct 2020 15:29:55 PM CST
> Description: a tool to sync systime to ntp server
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <math.h>
#include <errno.h>
#include "infra/utils/utils.h"
#include "status/zstat_utils.h"
#include "infra/serial/serial.h"
#include "3rdparty/cjson/cJSON.h"
#include <pthread.h>
#define RTC_SET_UART "/dev/ttyS3"
#define APP_BSP1_JSON "/data/app/config/bsp1.json"
void *Voltage_recovery_detection(void *arg);
int Voltage_recovery_flag = 0;
typedef struct bsp1_json {
char customer_name[16];
unsigned int plate;
unsigned int bitrate_front;
unsigned int bitrate_in;
unsigned int bitrate_live_front;
unsigned int bitrate_live_in;
char resolution_dvr_front[16];
char resolution_dvr_in[16];
unsigned int resolution_live_front;
unsigned int resolution_live_in;
char resolution_upload_front[16];
unsigned int quality_upload_front;
unsigned int dms_rotation;
_Bool disable_dms;
_Bool gnss_use_bd;
char ublox_agps_token[16];
double power_protect_value;
} bsp1_json;
bsp1_json *bsp1_info = NULL;
int set_mcu_cmd(float date, int cmd)
{
int date1 = date * 10;
int ones = date1 % 10;
date1 = date1 / 10;
int tens = date1 % 10;
date1 = date1 / 10;
int hundreds = date1 % 10;
serial *uart = construct_serial(RTC_SET_UART, 230400);
if(NULL == uart) {
_loge("Construct mcu uart1 failed\n");
return -1;
}
uart->sflush(uart);
char msg[11];
msg[0] = 0x55;
msg[1] = 0x04;
msg[2] = 0x06;
msg[3] = 0x15;
if(cmd == 1)
msg[4] = 0;
else if(cmd == 0)
msg[4] = 1;
else if(cmd == 2)
msg[4] = 2;
msg[5] = hundreds;
msg[6] = tens;
msg[7] = ones;
msg[8] = 0;
uint16_t sum = 0;
for (int i = 3; i < 9; i++) {
sum += msg[i];
}
msg[9] = sum;
msg[10] = (sum >> 8);
for(int i = 0; i < 5; i++) {
uart->tx(uart, (const char *)msg, 11);
usleep(10000);
}
print_data(msg, sizeof(msg));
destruct_serial(uart);
return 0;
}
bsp1_json *get_bsp1_info_from_bsp1_json(char *filepath)
{
/*****************************open file***************************/
bsp1_json* pconf = (bsp1_json*)malloc(sizeof(bsp1_json));
int f = -1;
char *cfg_stream = NULL;
cJSON *json = NULL;
if ((f = open(filepath, O_RDONLY)) < 0)
{
printf("open bsp1.json failed\n");
printf("will set power_protect_value to 9\n");
pconf->power_protect_value = 9.0;
return pconf;
}
int filelen = lseek(f, 0L, SEEK_END);
lseek(f, 0L, SEEK_SET);
cfg_stream = malloc(filelen);
if(cfg_stream == NULL)
{
printf("malloc error");
}
int ret = read(f, cfg_stream, filelen);
if(ret<=0)
{
printf("read cfg_stream failed");
}
json = cJSON_Parse(cfg_stream);
if(!json)
{
printf("cJSON_Parse wrong");
}
/*****************************open file***************************/
/*****************************get the information***************************/
cJSON *cmd;
//cJSON *cmd = cJSON_GetObjectItem(json, "customer_name");
/*
if(!cmd)
{
printf("read customer_name failed");
}
strcpy(pconf->customer_name, cmd->valuestring);
cmd = cJSON_GetObjectItem(json, "plate");
if(!cmd)
{
printf("read plate failed");
}
pconf->plate = cmd->valueint;
cmd = cJSON_GetObjectItem(json, "bitrate_front");
if(!cmd)
{
printf("read bitrate_front failed");
}
pconf->bitrate_front = cmd->valueint;
cmd = cJSON_GetObjectItem(json, "bitrate_in");
if(!cmd)
{
printf("read bitrate_in failed");
}
pconf->bitrate_in = cmd->valueint;
cmd = cJSON_GetObjectItem(json, "bitrate_live_front");
if(!cmd)
{
printf("read bitrate_live_front failed");
}
pconf->bitrate_live_front = cmd->valueint;
cmd = cJSON_GetObjectItem(json, "bitrate_live_in");
if(!cmd)
{
printf("read bitrate_live_in failed");
}
pconf->bitrate_live_in = cmd->valueint;
cmd = cJSON_GetObjectItem(json, "resolution_dvr_front");
if(!cmd)
{
printf("read resolution_dvr_front failed");
}
strcpy(pconf->resolution_dvr_front, cmd->valuestring);
cmd = cJSON_GetObjectItem(json, "resolution_dvr_in");
if(!cmd)
{
printf("read resolution_dvr_in failed");
}
strcpy(pconf->resolution_dvr_in, cmd->valuestring);
cmd = cJSON_GetObjectItem(json, "resolution_live_front");
if(!cmd)
{
printf("read resolution_live_front failed");
}
pconf->resolution_live_front = cmd->valueint;
cmd = cJSON_GetObjectItem(json, "resolution_live_in");
if(!cmd)
{
printf("read resolution_live_in failed");
}
pconf->resolution_live_in = cmd->valueint;
cmd = cJSON_GetObjectItem(json, "resolution_upload_front");
if(!cmd)
{
printf("read resolution_upload_front failed");
}
strcpy(pconf->resolution_upload_front, cmd->valuestring);
cmd = cJSON_GetObjectItem(json, "quality_upload_front");
if(!cmd)
{
printf("read quality_upload_front failed");
}
pconf->quality_upload_front = cmd->valueint;
cmd = cJSON_GetObjectItem(json, "dms_rotation");
if(!cmd)
{
printf("read dms_rotation failed");
}
pconf->dms_rotation = cmd->valueint;
cmd = cJSON_GetObjectItem(json, "disable_dms");
if(!cmd)
{
printf("read disable_dms failed");
}
pconf->disable_dms = cmd->valueint;
cmd = cJSON_GetObjectItem(json, "gnss_use_bd");
if(!cmd)
{
printf("read gnss_use_bd failed");
}
pconf->gnss_use_bd = cmd->valueint;
cmd = cJSON_GetObjectItem(json, "ublox_agps_token");
if(!cmd)
{
printf("read ublox_agps_token failed");
}
strcpy(pconf->ublox_agps_token, cmd->valuestring);
*/
cmd = cJSON_GetObjectItem(json, "power_protect_value");
if(!cmd)
{
printf("read power_protect_value failed\n");
printf("will set power_protect_value to 9\n");
pconf->power_protect_value = 9.0;
}
pconf->power_protect_value = cmd->valuedouble;
/******************************finshing work*********************/
close(f);
free(cfg_stream);
cfg_stream = NULL;
/******************************finshing work*********************/
return pconf;
}
void soc_power_management_cmd_send(const int cmd_flag, const double protect_voltage)
{
/****************************get the voltage of the acc*************************************/
power_t pwr;
memset(&pwr, 0, sizeof(power_t));
if(HAL_RET_OK == hal_status_get(ZS_ID_SYSTEM_STATUS, ZS_SYS_ID_POWER_VOLTAGE, &pwr)) {
printf("get power refresh time:%ld,in:%f,acc:%f,fcap:%f\n",
pwr.refresh_time, pwr.in, pwr.acc, pwr.fcap);
} else {
printf("get power failed\n");
}
float acc = pwr.acc / 1000;
/****************************get the voltage of the acc*************************************/
if(protect_voltage >= acc && Voltage_recovery_flag == 0)
{
Voltage_recovery_flag = 1;
int ret;
if (cmd_flag == 1)
{
_logi("[POW]SOC flag processing completed, power will shutdown after 10s\n");
ret = system("while true; fuser -k /sdcard/* ; halt; break; done &");
sleep(10);
}
else
{
if(2 >= acc)
_logi("[POW]Power will shutdown because ACC disconnect\n");
else
_logi("[POW]Power will shutdown because ACC vale is too low\n");
}
set_mcu_cmd(protect_voltage, cmd_flag);
}
else
{
static int recover_time = 480;
recover_time--;
if(recover_time <= 0)
{
int ret;
ret = system("while true; fuser -k /sdcard/* ; halt; break; done &");
ret = ret;
}
if(acc > protect_voltage)
{
int cmd_flag = 2;
set_mcu_cmd(bsp1_info->power_protect_value, cmd_flag);
_logi("[POW]Power recovery\n");
Voltage_recovery_flag = 0;
recover_time = 480;
}
}
}
int main(int argc, char *argv[])
{
if(argc != 2)
{
printf("Please enter a value( value = 0:Normal voltage monitoring\n value = 1:Notify the MCU to immediately turn off all peripheral power supplies.)\n");
return 0;
}
int cmd_flag = atoi(argv[1]);
/***********************get the information from the bsp1_json****************************/
bsp1_info = get_bsp1_info_from_bsp1_json(APP_BSP1_JSON);
printf("power_protect_value = %f \n", bsp1_info->power_protect_value);
_logi("[POW]Voltage protection value: %fv \n", bsp1_info->power_protect_value);
/***********************get the information from the bsp1_json****************************/
while(1)
{
soc_power_management_cmd_send(argv, bsp1_info->power_protect_value);
sleep(1);
}
return 0;
}
读取json文件
于 2021-09-07 15:08:17 首次发布