epm/User/agreement/agreement_master.c

352 lines
12 KiB
C

/*
* @Author: shenghao.xu
* @Date: 2023-04-06 09:33:35
* @LastEditors: shenghao.xu
* @LastEditTime: 2023-06-20 00:11:43
* @Description:
* email:545403892@qq.com
* Copyright (c) 2023 by shenghao.xu, All Rights Reserved.
*/
#include "agreement_master.h"
// 查询IP输入电流
static bool command_req_query_ip_input_current(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
return true;
}
// 调节IP输入电流
static bool command_req_adjust_ip_input_current(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
uint8_t offset = 0;
osel_memcpy(resp->pbuf->data_p + offset, (uint8_t *)&data->data.adjust_ip_input_current.value, sizeof(data->data.adjust_ip_input_current.value));
offset += sizeof(data->data.adjust_ip_input_current.value);
resp->pbuf->data_len = offset;
return true;
}
// 查询状态
static bool command_req_query_state(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
return true;
}
// 查询流程
static bool command_req_query_process(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
return true;
}
// 配置流程
static bool command_req_config_process(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(LARGE_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
uint8_t sum;
uint16_t crc;
uint16_t offset = 0;
config_convert_pbuf(*data->data.config, &resp->pbuf);
offset = resp->pbuf->data_len;
// 填充异或校验
sum = xor_compute(resp->pbuf->data_p, offset);
crc = crc16_compute(resp->pbuf->data_p, offset);
resp->pbuf->data_p[offset] = sum;
offset++;
crc = S2B_UINT16(crc);
osel_memcpy(&resp->pbuf->data_p[offset], (uint8_t *)&crc, sizeof(uint16_t));
offset += sizeof(uint16_t);
resp->pbuf->data_len = offset;
return true;
}
// 执行流程
static bool command_req_execute_process(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
uint8_t offset = 0;
resp->pbuf->data_p[offset] = data->data.execute_process.process_index;
offset++;
resp->pbuf->data_p[offset] = data->data.execute_process.plan_index;
offset++;
resp->pbuf->data_len = offset;
return true;
}
// 停止流程
static bool command_req_stop_process(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
return true;
}
// 查询数据
static bool command_req_query_data(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
uint8_t offset = 0;
uint8_t *ptr = (uint8_t *)&data->data.query_data.sensors;
resp->pbuf->data_p[offset] = data->data.query_data.count;
offset++;
for (uint8_t i = 0; i < data->data.query_data.count; i++)
{
query_data_sensor_t sensor;
osel_memcpy((uint8_t *)&sensor, ptr, sizeof(query_data_sensor_t));
ptr += sizeof(query_data_sensor_t);
osel_memcpy(&resp->pbuf->data_p[offset], (uint8_t *)&sensor, sizeof(query_data_sensor_t));
offset += sizeof(query_data_sensor_t);
}
resp->pbuf->data_len = offset;
return true;
}
// 配置地址
static bool command_req_config_address(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
uint16_t address = data->data.config_address.address;
osel_memcpy(resp->pbuf->data_p, (uint8_t *)&address, sizeof(uint16_t));
resp->pbuf->data_len = sizeof(uint16_t);
return true;
}
// 查询地址
static bool command_req_query_address(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
return true;
}
// 标定传感器
static bool command_req_calibration_sensor(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(MEDIUM_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
uint8_t offset = 0;
uint8_t *ptr = (uint8_t *)&data->data.calibration_sensor.sensor_data.sensors;
resp->pbuf->data_p[offset] = data->data.calibration_sensor.state;
offset++;
resp->pbuf->data_p[offset] = data->data.calibration_sensor.sensor_data.count;
offset++;
for (uint8_t i = 0; i < data->data.calibration_sensor.sensor_data.count; i++)
{
query_data_sensor_t sensor;
osel_memcpy((uint8_t *)&sensor, ptr, sizeof(query_data_sensor_t));
ptr += sizeof(query_data_sensor_t);
osel_memcpy(&resp->pbuf->data_p[offset], (uint8_t *)&sensor, sizeof(query_data_sensor_t));
offset += sizeof(query_data_sensor_t);
}
resp->pbuf->data_len = offset;
return true;
}
// 设定阀门
static bool command_req_set_valve(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
osel_memcpy(resp->pbuf->data_p, (uint8_t *)&data->data.set_valve, sizeof(set_valve_t));
resp->pbuf->data_len = sizeof(set_valve_t);
return true;
}
// 查询比例阀
static bool command_req_query_valve(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
return true;
}
// 设置比例阀
static bool command_req_set_proportion_valve(const command_req_t *const data, command_resp_t *resp)
{
uint8_t offset = 0;
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
float32_t valve_ratio;
valve_ratio.f = data->data.valve_ratio.value.f;
valve_ratio.c = B2S_UINT32(valve_ratio.c);
osel_memcpy(resp->pbuf->data_p, (uint8_t *)&valve_ratio.c, sizeof(int32_t));
offset += sizeof(int32_t);
resp->pbuf->data_p[offset] = data->data.valve_ratio.pid_sensor_class;
offset++;
resp->pbuf->data_p[offset] = data->data.valve_ratio.pid_sensor_no;
offset++;
resp->pbuf->data_len = offset;
return true;
}
// 设置步进电机
static bool command_req_set_stepper_motor(const command_req_t *const data, command_resp_t *resp)
{
uint8_t offset = 0;
float32_t f;
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
resp->pbuf->data_p[offset] = data->data.stepper_motor.dir;
offset++;
f.f = data->data.stepper_motor.angle;
f.c = S2B_UINT32(f.c);
osel_memcpy(&resp->pbuf->data_p[offset], (uint8_t *)&f.c, sizeof(int32_t));
offset += sizeof(int32_t);
resp->pbuf->data_len = offset;
return true;
}
// 查询I/P 输入PWM占空比
static bool command_req_query_ip_pwm_duty(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
return true;
}
// 调节I/P 输入PWM占空比
static bool command_req_set_ip_pwm_duty(const command_req_t *const data, command_resp_t *resp)
{
uint8_t offset = 0;
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
osel_memcpy(resp->pbuf->data_p, (uint8_t *)&data->data.adjust_ip_pwm_duty, sizeof(adjust_ip_pwm_duty_t));
offset += sizeof(adjust_ip_pwm_duty_t);
resp->pbuf->data_len = offset;
return true;
}
// 设置I/P 模式
static bool command_req_set_ip_mode(const command_req_t *const data, command_resp_t *resp)
{
uint8_t offset = 0;
uint8_t mode = 0;
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
mode = data->data.set_ip_mode.mode;
resp->pbuf->data_p[offset] = mode;
offset++;
resp->pbuf->data_p[offset] = data->data.set_ip_mode.data_length;
offset++;
if (data->data.set_ip_mode.data_length > 0)
{
osel_memcpy(&resp->pbuf->data_p[offset], (uint8_t *)&data->data.set_ip_mode.data, sizeof(data->data.set_ip_mode.data_length));
offset += data->data.set_ip_mode.data_length;
}
resp->pbuf->data_len = offset;
return true;
}
// 查询I/P 模式
static bool command_req_query_ip_mode(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
return true;
}
// 复位设备
static bool command_req_reset_device(const command_req_t *const data, command_resp_t *resp)
{
resp->pbuf = pbuf_allocz(SMALL_PBUF_BUFFER_SIZE __PLINE1);
if (!DBG_ASSERT(resp->pbuf != NULL __DBG_LINE))
return false;
return true;
}
// 模拟配置流程数据
bool mock_command_req_config_process(void)
{
bool ret = false;
command_req_t mock;
mock.data.config = mock_commond_req_config();
mock.src = 0xffff;
mock.dst = S2B_UINT16(0x0001);
mock.command = COMMAND_CONFIG_PROCESS;
ret = agreement_master_req(&mock);
config_free(mock.data.config);
return ret;
}
// 处理来自客户端的请求
void agreement_master_rsp(uint8_t *data, uint16_t len)
{
command_req_t req;
uint16_t offset = 0;
osel_memset((uint8_t *)&req, 0, sizeof(command_req_t));
osel_memcpy((uint8_t *)&req.dst, data, sizeof(uint16_t));
offset += sizeof(uint16_t);
osel_memcpy((uint8_t *)&req.src, data + offset, sizeof(uint16_t));
offset += sizeof(uint16_t);
req.src = S2B_UINT16(req.src);
req.dst = S2B_UINT16(req.dst);
req.command = data[offset] - 0x80;
if (!DBG_ASSERT(handle.response_call != NULL __DBG_LINE))
return;
if (!DBG_ASSERT(req.command < COMMAND_MAX __DBG_LINE))
return;
handle.response_call(data + offset, len - offset);
}
bool agreement_master_init(void)
{
command_req_ptr_arr[COMMAND_RESET_DEVICE] = command_req_reset_device;
command_req_ptr_arr[COMMAND_QUERY_IP_INPUT_CURRENT] = command_req_query_ip_input_current;
command_req_ptr_arr[COMMAND_ADJUST_IP_INPUT_CURRENT] = command_req_adjust_ip_input_current;
command_req_ptr_arr[COMMAND_QUERY_STATE] = command_req_query_state;
command_req_ptr_arr[COMMAND_QUERY_PROCESS] = command_req_query_process;
command_req_ptr_arr[COMMAND_CONFIG_PROCESS] = command_req_config_process;
command_req_ptr_arr[COMMAND_EXECUTE_PROCESS] = command_req_execute_process;
command_req_ptr_arr[COMMAND_STOP_PROCESS] = command_req_stop_process;
command_req_ptr_arr[COMMAND_QUERY_DATA] = command_req_query_data;
command_req_ptr_arr[COMMAND_CONFIG_ADDRESS] = command_req_config_address;
command_req_ptr_arr[COMMAND_QUERY_ADDRESS] = command_req_query_address;
command_req_ptr_arr[COMMAND_CALIBRATE_SENSOR] = command_req_calibration_sensor;
command_req_ptr_arr[COMMAND_SET_VALVE] = command_req_set_valve;
command_req_ptr_arr[COMMAND_QUERY_VALVE] = command_req_query_valve;
command_req_ptr_arr[COMMAND_SET_VALVE_RATIO] = command_req_set_proportion_valve;
command_req_ptr_arr[COMMAND_SET_STEPPER_MOTOR] = command_req_set_stepper_motor;
command_req_ptr_arr[COMMAND_QUERY_IP_INPUT_PWM_DUTY] = command_req_query_ip_pwm_duty;
command_req_ptr_arr[COMMAND_ADJUST_IP_INPUT_PWM_DUTY] = command_req_set_ip_pwm_duty;
command_req_ptr_arr[COMMAND_SET_IP_MODE] = command_req_set_ip_mode;
command_req_ptr_arr[COMMAND_QUERY_IP_MODE] = command_req_query_ip_mode;
return true;
}