/* * @Author: shenghao.xu * @Date: 2023-04-10 13:08:00 * @LastEditors: shenghao.xu * @LastEditTime: 2023-04-25 23:05:46 * @Description: * email:545403892@qq.com * Copyright (c) 2023 by shenghao.xu, All Rights Reserved. */ #include "agreement_frame.h" config_t *g_config = NULL; calibration_sensor_t *g_calibration_sensor = NULL; query_data_t *g_query_data = NULL; execute_process_t g_execute_process; // 执行流程数据域 void config_print(config_t *config) { if (config != NULL) { if (config->processes != NULL) { for (uint8_t i = 0; i < config->process_count; i++) { if (config->processes[i].plans != NULL) { for (uint8_t j = 0; j < config->processes[i].plan_count; j++) { if (config->processes[i].plans[j].actions != NULL) { for (uint8_t k = 0; k < config->processes[i].plans[j].action_count; k++) { #ifdef STM32 #else LOG_PRINT("process[%d] plan[%d] action[%d] type:%02x\r", i, j, k, config->processes[i].plans[j].actions[k].type); uint8_t *ptr = (uint8_t *)&config->processes[i].plans[j].actions[k].data; LOG_HEX(ptr, 10); #endif } } } } } } } } /** * @description: 配置参数释放 * @param {config} *config * @return {*} */ void config_free(config_t *config) { if (config != NULL) { if (config->processes != NULL) { for (uint8_t i = 0; i < config->process_count; i++) { if (config->processes[i].plans != NULL) { for (uint8_t j = 0; j < config->processes[i].plan_count; j++) { if (config->processes[i].plans[j].actions != NULL) { osel_mem_free(config->processes[i].plans[j].actions); } } osel_mem_free(config->processes[i].plans); } } osel_mem_free(config->processes); } osel_mem_free(config); } } /** * @description: 配置参数数据结构初始化 * @param {uint8_t} process_count * @param {uint8_t} plan_count * @param {uint8_t} action_count * @return {*} */ config_t *config_alloc(uint8_t process_count, uint8_t plan_count, uint8_t action_count) { config_t *config = osel_mem_alloc(sizeof(config_t)); if (config != NULL) { config->process_count = process_count; config->processes = osel_mem_alloc(sizeof(process_t) * process_count); if (config->processes != NULL) { for (uint8_t i = 0; i < process_count; i++) { config->processes[i].plan_count = plan_count; config->processes[i].plans = osel_mem_alloc(sizeof(plan_t) * plan_count); if (config->processes[i].plans != NULL) { for (uint8_t j = 0; j < plan_count; j++) { config->processes[i].plans[j].action_count = action_count; config->processes[i].plans[j].actions = osel_mem_alloc(sizeof(action_t) * action_count); if (config->processes[i].plans[j].actions == NULL) { config_free(config); return NULL; } } } else { config_free(config); return NULL; } } } else { config_free(config); return NULL; } } return config; } /** * @description: 二进制数据转换为配置参数 * @param {uint8_t} *data * @param {uint16_t} len * @return {*} */ config_t *data_convert_config(uint8_t *data, uint16_t len) { uint16_t length = 0; uint16_t offset = 0; uint8_t process_count = 0, plan_count = 0, action_count = 0; if (g_config != NULL) { config_free(g_config); g_config = NULL; } g_config = osel_mem_alloc(sizeof(config_t)); osel_memset((uint8_t *)g_config, 0, sizeof(config_t)); length = length; length = BUILD_UINT16(data[1], data[0]); offset += 2; process_count = data[offset]; offset++; g_config->processes = (process_t *)osel_mem_alloc(process_count * sizeof(process_t)); g_config->process_count = process_count; for (uint8_t i = 0; i < process_count; i++) { length = BUILD_UINT16(data[offset + 1], data[offset]); offset += 2; plan_count = data[offset]; offset++; g_config->processes[i].plans = (plan_t *)osel_mem_alloc(plan_count * sizeof(plan_t)); g_config->processes[i].plan_count = plan_count; for (uint8_t j = 0; j < plan_count; j++) { length = BUILD_UINT16(data[offset + 1], data[offset]); offset += 2; action_count = data[offset]; offset++; g_config->processes[i].plans[j].actions = (action_t *)osel_mem_alloc(action_count * sizeof(action_t)); g_config->processes[i].plans[j].action_count = action_count; for (uint8_t k = 0; k < action_count; k++) { osel_memcpy((uint8_t *)&g_config->processes[i].plans[j].actions[k], &data[offset], sizeof(action_t)); offset += sizeof(action_t); } } } return g_config; } /** * @description: 配置请求结构体转换为pbuf * @param {config_t} config * @param {pbuf_t} * * @return {*} */ void config_convert_pbuf(config_t config, pbuf_t **const pbuf) { uint8_t *process_ptr, *plan_ptr, *action_ptr; uint16_t length = 0; uint16_t offset = 0; uint8_t process_count = 0, plan_count = 0, action_count = 0; process_t *processes_ptr; plan_t *plans_ptr; action_t *actions_ptr; if (config.processes == NULL) { return; } process_count = config.process_count; offset = (*pbuf)->data_len; // data_len初始不是为0,在app层已经赋值用来存储长度 // 填写流程的长度 process_ptr = &(*pbuf)->data_p[offset]; offset += 2; // 填写流程数量 (*pbuf)->data_p[offset] = process_count; offset++; processes_ptr = config.processes; for (uint8_t i = 0; i < process_count; i++) { processes_ptr += i * sizeof(process_t); plans_ptr = processes_ptr->plans; plan_count = processes_ptr->plan_count; // 填写流程1的长度 plan_ptr = &(*pbuf)->data_p[offset]; offset += 2; // 填写流程1方案数量 (*pbuf)->data_p[offset] = plan_count; offset++; for (uint8_t j = 0; j < plan_count; j++) { plans_ptr += j * sizeof(plan_t); actions_ptr = plans_ptr->actions; action_count = plans_ptr->action_count; // 填写方案1的长度 action_ptr = &(*pbuf)->data_p[offset]; offset += 2; // 填写方案1动作数量 (*pbuf)->data_p[offset] = action_count; offset++; for (uint8_t k = 0; k < action_count; k++) { osel_memcpy(&(*pbuf)->data_p[offset], (uint8_t *)(&actions_ptr[k]), sizeof(action_t)); offset += sizeof(action_t); } length = (&(*pbuf)->data_p[offset] - action_ptr); length = S2B_UINT16(length - 2); osel_memcpy(action_ptr, (uint8_t *)&length, 2); } length = (&(*pbuf)->data_p[offset] - plan_ptr); length = S2B_UINT16(length - 2); osel_memcpy(plan_ptr, (uint8_t *)&length, 2); } length = (&(*pbuf)->data_p[offset] - process_ptr); length = S2B_UINT16(length - 2); osel_memcpy(process_ptr, (uint8_t *)&length, 2); (*pbuf)->data_len = offset; } config_t *mock_commond_req_config(void) { config_t *config; uint8_t action_count = 6, plan_count = 1, process_count = 1; action_t action; valve_t valve; uint8_t action_offset = 0; config = config_alloc(process_count, plan_count, action_count); // 打开两通阀6 osel_memset((uint8_t *)&action, 0, sizeof(action_t)); osel_memset((uint8_t *)&valve, 0, sizeof(valve_t)); valve.valve_type = UNIT_TWO_WAY_VALVE; valve.no = 4; valve.data.open = true; action.type = ACTION_VALVE; osel_memcpy((uint8_t *)&action.data.valve, (uint8_t *)&valve, sizeof(valve_t)); osel_memcpy((uint8_t *)(&config->processes->plans->actions[action_offset]), (uint8_t *)&action, sizeof(action_t)); action_offset++; // 等待1秒 osel_memset((uint8_t *)&action, 0, sizeof(action_t)); osel_memset((uint8_t *)&valve, 0, sizeof(valve_t)); action.type = ACTION_WAIT; action.data.sleep = S2B_UINT16(1000); osel_memcpy((uint8_t *)(&config->processes->plans->actions[action_offset]), (uint8_t *)&action, sizeof(action_t)); action_offset++; // 关闭两通阀6 osel_memset((uint8_t *)&action, 0, sizeof(action_t)); osel_memset((uint8_t *)&valve, 0, sizeof(valve_t)); valve.valve_type = UNIT_TWO_WAY_VALVE; valve.no = 4; valve.data.open = false; action.type = ACTION_VALVE; osel_memcpy((uint8_t *)&action.data.valve, (uint8_t *)&valve, sizeof(valve_t)); osel_memcpy((uint8_t *)(&config->processes->plans->actions[action_offset]), (uint8_t *)&action, sizeof(action_t)); action_offset++; // 打开两通阀2 osel_memset((uint8_t *)&action, 0, sizeof(action_t)); osel_memset((uint8_t *)&valve, 0, sizeof(valve_t)); valve.valve_type = UNIT_TWO_WAY_VALVE; valve.no = 2; valve.data.open = true; action.type = ACTION_VALVE; osel_memcpy((uint8_t *)&action.data.valve, (uint8_t *)&valve, sizeof(valve_t)); osel_memcpy((uint8_t *)(&config->processes->plans->actions[action_offset]), (uint8_t *)&action, sizeof(action_t)); action_offset++; // 等待2秒 osel_memset((uint8_t *)&action, 0, sizeof(action_t)); osel_memset((uint8_t *)&valve, 0, sizeof(valve_t)); action.type = ACTION_WAIT; action.data.sleep = S2B_UINT16(2000); osel_memcpy((uint8_t *)(&config->processes->plans->actions[action_offset]), (uint8_t *)&action, sizeof(action_t)); action_offset++; // 开始测试 osel_memset((uint8_t *)&action, 0, sizeof(action_t)); osel_memset((uint8_t *)&valve, 0, sizeof(valve_t)); action.type = ACTION_WORK; osel_memcpy((uint8_t *)(&config->processes->plans->actions[action_offset]), (uint8_t *)&action, sizeof(action_t)); action_offset++; return config; } void calibration_sensor_free(calibration_sensor_t *data) { if (data != NULL) { if (data->sensor_data.sensors != NULL) { osel_mem_free(data->sensor_data.sensors); } osel_mem_free(data); } } /** * @description:二进制数据转换为标定传感器结构体 * @param {uint8_t} *data * @param {uint16_t} len * @return {*} */ calibration_sensor_t *data_convert_calibration_sensor(uint8_t *data, uint16_t len) { uint16_t offset = 0; if (g_calibration_sensor != NULL) { calibration_sensor_free(g_calibration_sensor); g_calibration_sensor = NULL; } g_calibration_sensor = osel_mem_alloc(sizeof(calibration_sensor_t)); g_calibration_sensor->state = data[offset]; offset += 1; g_calibration_sensor->sensor_data.count = data[offset]; offset += 1; if (g_calibration_sensor->sensor_data.count > 0) { g_calibration_sensor->sensor_data.sensors = osel_mem_alloc(sizeof(query_data_sensor_t) * g_calibration_sensor->sensor_data.count); } for (uint8_t i = 0; i < g_calibration_sensor->sensor_data.count; i++) { osel_memcpy((uint8_t *)&g_calibration_sensor->sensor_data.sensors[i], &data[offset], sizeof(query_data_sensor_t)); offset += sizeof(query_data_sensor_t); } return g_calibration_sensor; } void calibration_sensor_convert_pbuf(calibration_sensor_t calibration_sensor, pbuf_t **const pbuf) { uint16_t offset = 0; (*pbuf)->data_p[offset] = calibration_sensor.state; offset++; (*pbuf)->data_p[offset] = calibration_sensor.sensor_data.count; offset++; for (uint8_t i = 0; i < calibration_sensor.sensor_data.count; i++) { osel_memcpy((uint8_t *)&(*pbuf)->data_p[offset], (uint8_t *)&calibration_sensor.sensor_data.sensors[i], sizeof(query_data_sensor_t)); offset += sizeof(query_data_sensor_t); } (*pbuf)->data_len = offset; } static void query_data_free(query_data_t *data) { if (data != NULL) { if (data->sensors != NULL) { osel_mem_free(data->sensors); } osel_mem_free(data); } } /** * @description:二进制数据转换为查询数据结构体 * @param {uint8_t} *data * @param {uint16_t} len * @return {*} */ query_data_t *data_convert_query_data(uint8_t *data, uint16_t len) { uint16_t offset = 0; if (g_query_data != NULL) { query_data_free(g_query_data); g_query_data = NULL; } g_query_data = osel_mem_alloc(sizeof(query_data_t)); g_query_data->count = data[offset]; offset += 1; if (g_query_data->count > 0) { g_query_data->sensors = osel_mem_alloc(sizeof(query_data_sensor_t) * g_query_data->count); } for (uint8_t i = 0; i < g_query_data->count; i++) { osel_memcpy((uint8_t *)&g_query_data->sensors[i], &data[offset], sizeof(query_data_sensor_t)); offset += sizeof(query_data_sensor_t); } return g_query_data; } void query_data_convert_pbuf(query_data_rsp_t *query_data, uint8_t count, pbuf_t **const pbuf) { uint16_t offset = 0; uint8_t len = 0; uint8_t *len_ptr; query_data_rsp_t *ptr = query_data; (*pbuf)->data_p[offset] = count; offset++; for (uint8_t i = 0; i < count; i++) { (*pbuf)->data_p[offset] = ptr->sensor_class; offset++; (*pbuf)->data_p[offset] = ptr->sensor.data; offset++; len_ptr = &(*pbuf)->data_p[offset]; offset++; for (uint8_t j = 0; j < ptr->count; j++) { ptr->data[j].c = B2S_UINT32(ptr->data[j].c); osel_memcpy((uint8_t *)&(*pbuf)->data_p[offset], (uint8_t *)&ptr->data[j].c, sizeof(uint32_t)); offset += sizeof(uint32_t); len += sizeof(uint32_t); } *len_ptr = len; ptr++; len = 0; } (*pbuf)->data_len = offset; osel_mem_free(query_data); // 释放内存 }