C语言实现设计模式-策略模式+命令模式组合使用

本文介绍在主从设备串口通讯中,通过策略模式和命令模式优化控制访问。策略模式用于智能选择应答策略,命令模式则区分不同命令动作,实现灵活的响应处理。

使用环境介绍

当主设备和从设备使用串口信息交互通讯,根据串口协议,实现对从设备的控制访问。
使用情况1:当主设备发送板卡信息获取、读取数据命令时,希望获取从设备的数据信息,从设备应答操作。
使用情况2:当主设备发送采集命令时,从设备收到但不进行回复,不应答操作。
在这里插入图片描述

使用策略模式-区分使用哪种方法

  1. 首先识别命令,对需要进行应答的命令进行判断,区分命令,以下命令都是从设备进行应答的命令。
const rz_sensor_cmd_type_t support_send_cmd_reply[] = {
	CMD_READ_DATA,
	CMD_READ_INFO,
	CMD_READ_PARAMETERS,
	CMD_PREPARE_UPDATE,
	CMD_SEGMENT_UPDATE,
};
  1. 再动作函数区分:
  • rz_subassembly_uart_send_without_reply (发送数据包,从机不进行响应应答)
  • rz_subassembly_uart_send (发送数据包,从机进行响应应答)
/*
 * Strategy mode
 * The strategy mode is to use a unified method interface to access different types of data separately.
 */
uint32_t rz_sensor_uart_send(SubAssembly_Data_Type *t,uint8_t sensorType,uint8_t addr,uint8_t cmd,uint16_t len,uint8_t *buf)
{
	uint8_t  cmd_type = sizeof(support_send_cmd_reply)/sizeof(rz_sensor_cmd_type_t);
	uint32_t (*pfunc)(SubAssembly_Data_Type *t,uint8_t sensorType,uint8_t addr,uint8_t cmd,uint16_t len,uint8_t *buf);

	for (uint8_t i=0; i < cmd_type; i++) {
		/* If the interface function cannot be found, the default interface function is used */
		pfunc = rz_subassembly_uart_send_without_reply;

		if (cmd == support_send_cmd_reply[i]) {
			/* if matched, call the reload interface */
			pfunc = rz_subassembly_uart_send;
            break;
		}
	}

	/* Call interface function for processing */
	if (1 != pfunc(t,sensorType,addr,cmd,len,buf)) {
		rt_kprintf("rz_sensor_uart_send failed, sensorType=0x%02x, addr=0x%02x, cmd=0x%02x\r\n",sensorType, addr,cmd);
		return 0;
	}

	return 1;

}

使用命令模式-区分哪种命令动作

  1. 串口接收数据,可根据不同的功能码进行不同的响应执行,这样整体的解析函数不需要改动。
    使用参数结构体进行统一的参数管理,命令和函数动作区分
typedef struct sensor_cmd_param_def {
	SubAssembly_Data_Type *pAssembly;
	uint8_t sendCmd;
	uint8_t cmd;
	uint16_t dataLength;
	uint8_t *dataBuf;
} sensor_cmd_param_t;

typedef struct sensor_cmd_msg_def {
	uint8_t  type;
	uint32_t ( *do_process_cmd ) ( sensor_cmd_param_t *param );
} sensor_cmd_msg_t;
  1. 统一的命令接口解析,后期即使业务需求改动,只需要改动子模块即可。
static const sensor_cmd_msg_t bolt_cmd_process_table[] = {
	{READ_DATA, bolt_cmd_process_read_data},
	{READ_INFO, bolt_cmd_process_read_info},
	{READ_PARAMETERS, bolt_cmd_process_read_param},
	{PREPARE_UPDATE,bolt_cmd_process_read_update_info},
	{SEGMENT_UPDATE,bolt_cmd_process_send_data_packet},
};
uint32_t bolt_cmd_process (SubAssembly_Data_Type *pAssembly, uint8_t sendCmd,uint8_t cmd, uint16_t dataLength, uint8_t *dataBuf)
{
	uint8_t num_items = sizeof (bolt_cmd_process_table) /sizeof(bolt_cmd_process_table[0]);
	uint8_t ret = 0;

	sensor_cmd_param_t *param = (sensor_cmd_param_t *)calloc(1,sizeof ( sensor_cmd_param_t ));
	if (CHECK_PTR_OBJECT(param)) {
		rt_kprintf ( "%s(),line %d, malloc failed\r\n", __FUNCTION__, __LINE__ );
		return 0;
	}

	param->pAssembly = pAssembly;
	param->sendCmd = sendCmd;
	param->cmd = cmd;
	param->dataLength = dataLength;
	param->dataBuf = dataBuf;

	for (uint8_t target = 0; target < num_items; target++) {
		if (bolt_cmd_process_table[target].type == cmd) {
			ret = bolt_cmd_process_table[target].do_process_cmd ( param );
			break;
		}
	}

	if (param != NULL) {
		free (param);
		param = NULL;
	}

	return ret;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值