Commit 6a746dc0 authored by Dinesh Kumar K's avatar Dinesh Kumar K Committed by Anas Nashif
Browse files

drivers: sensor: grow_r502a: add RX packet validation

Add RX packet validator where header, address, pid
and checksum bytes are verified and validated on
every functions.

Signed-off-by: default avatarDinesh Kumar K <dinesh@linumiz.com>
parent e08ce0ed
Loading
Loading
Loading
Loading
+102 −53
Changes for drivers/sensor/grow_r502a/grow_r502a.c: 102 added lines, 53 removed lines.
Original line number Diff line number Diff line
@@ -67,6 +67,61 @@ static int transceive_packet(const struct device *dev, union r502a_packet *tx_pa
	return 0;
}

static int r502a_validate_rx_packet(union r502a_packet *rx_packet)
{
	uint16_t recv_cks = 0, calc_cks = 0;
	uint8_t cks_start_idx;

	if (sys_be16_to_cpu(rx_packet->start) == R502A_STARTCODE) {
		LOG_DBG("startcode matched 0x%X", sys_be16_to_cpu(rx_packet->start));
	} else {
		LOG_ERR("startcode didn't match 0x%X", sys_be16_to_cpu(rx_packet->start));
		return -EINVAL;
	}

	if (sys_be32_to_cpu(rx_packet->addr) == R502A_DEFAULT_ADDRESS) {
		LOG_DBG("Address matched 0x%X", sys_be32_to_cpu(rx_packet->addr));
	} else {
		LOG_ERR("Address didn't match 0x%X", sys_be32_to_cpu(rx_packet->addr));
		return -EINVAL;
	}

	switch (rx_packet->pid) {
	case R502A_DATA_PACKET:
		LOG_DBG("Data Packet Received 0x%X", rx_packet->pid);
		break;
	case R502A_END_DATA_PACKET:
		LOG_DBG("End of Data Packet Received 0x%X", rx_packet->pid);
		break;
	case R502A_ACK_PACKET:
		LOG_DBG("Acknowledgment Packet Received 0x%X", rx_packet->pid);
		break;
	default:
		LOG_ERR("Error Package ID 0x%X", rx_packet->pid);
		return -EINVAL;
	}

	cks_start_idx = sys_be16_to_cpu(rx_packet->len) - R502A_CHECKSUM_LEN;

	recv_cks = sys_get_be16(&rx_packet->data[cks_start_idx]);

	calc_cks += rx_packet->pid + (sys_be16_to_cpu(rx_packet->len) >> 8) +
					(sys_be16_to_cpu(rx_packet->len) & 0xFF);

	for (int i = 0; i < cks_start_idx; i++) {
		calc_cks += rx_packet->data[i];
	}

	if (recv_cks == calc_cks) {
		LOG_DBG("Checksum matched calculated 0x%x received 0x%x", calc_cks, recv_cks);
	} else {
		LOG_ERR("Checksum mismatch calculated 0x%x received 0x%x", calc_cks, recv_cks);
		return -EINVAL;
	}

	return 0;
}

static void uart_cb_tx_handler(const struct device *dev)
{
	const struct grow_r502a_config *config = dev->config;
@@ -145,9 +200,9 @@ static int fps_set_sys_param(const struct device *dev, const struct sensor_value
		return ret;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		return -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		return ret;
	}

	if (rx_packet.buf[R502A_CC_IDX] == R502A_OK) {
@@ -180,9 +235,8 @@ int r502a_read_sys_param(const struct device *dev, struct r502a_sys_param *val)
		goto unlock;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		ret = -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		goto unlock;
	}

@@ -239,9 +293,9 @@ static int fps_led_control(const struct device *dev, struct r502a_led_params *le
		return ret;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		return -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		return ret;
	}

	if (rx_packet.buf[R502A_CC_IDX] == R502A_OK) {
@@ -273,15 +327,15 @@ static int fps_verify_password(const struct device *dev)
		return ret;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		return -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		return ret;
	}

	if (rx_packet.buf[R502A_CC_IDX] == R502A_OK) {
		LOG_DBG("Correct password, R502A verified");
	} else {
		LOG_ERR("Package receive error 0x%X", rx_packet.buf[R502A_CC_IDX]);
		LOG_ERR("Password verification error 0x%X", rx_packet.buf[R502A_CC_IDX]);
		return -EIO;
	}

@@ -305,9 +359,9 @@ static int fps_get_template_count(const struct device *dev)
		return ret;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		return -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		return ret;
	}

	if (rx_packet.buf[R502A_CC_IDX] == R502A_OK) {
@@ -341,11 +395,9 @@ static int fps_read_template_table(const struct device *dev, uint32_t *free_idx)
		goto unlock;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		ret = -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		goto unlock;

	}

	if (rx_packet.buf[R502A_CC_IDX] == R502A_OK) {
@@ -396,9 +448,9 @@ static int fps_get_image(const struct device *dev)
		return ret;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		return -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		return ret;
	}

	if (rx_packet.buf[R502A_CC_IDX] == R502A_OK) {
@@ -431,9 +483,9 @@ static int fps_image_to_char(const struct device *dev, uint8_t char_buf_idx)
		return ret;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		return -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		return ret;
	}

	if (rx_packet.buf[R502A_CC_IDX] == R502A_OK) {
@@ -462,9 +514,9 @@ static int fps_create_model(const struct device *dev)
		return ret;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		return -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		return ret;
	}

	if (rx_packet.buf[R502A_CC_IDX] == R502A_OK) {
@@ -504,9 +556,8 @@ static int fps_store_model(const struct device *dev, uint16_t id)
		goto unlock;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		ret = -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		goto unlock;
	}

@@ -546,9 +597,8 @@ static int fps_delete_model(const struct device *dev, uint16_t id, uint16_t coun
		goto unlock;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		ret = -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		goto unlock;
	}

@@ -582,9 +632,8 @@ static int fps_empty_db(const struct device *dev)
		goto unlock;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		ret = -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		goto unlock;
	}

@@ -629,9 +678,8 @@ static int fps_search(const struct device *dev, struct sensor_value *val)
		goto unlock;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		ret = -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		goto unlock;
	}

@@ -682,9 +730,8 @@ static int fps_load_template(const struct device *dev, uint16_t id)
		goto unlock;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		ret = -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		goto unlock;
	}

@@ -727,9 +774,8 @@ static int fps_match_templates(const struct device *dev, struct sensor_value *va
		goto unlock;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		ret = -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		goto unlock;
	}

@@ -814,9 +860,8 @@ int fps_upload_char_buf(const struct device *dev, struct r502a_template *temp)
		goto unlock;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		ret = -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		goto unlock;
	}

@@ -835,6 +880,11 @@ int fps_upload_char_buf(const struct device *dev, struct r502a_template *temp)
			goto unlock;
		}

		ret = r502a_validate_rx_packet(&rx_packet);
		if (ret != 0) {
			goto unlock;
		}

		memcpy(&temp->data[idx], &rx_packet.data,
				sys_be16_to_cpu(rx_packet.len) - R502A_CHECKSUM_LEN);
		idx += sys_be16_to_cpu(rx_packet.len) - R502A_CHECKSUM_LEN;
@@ -877,9 +927,8 @@ int fps_download_char_buf(const struct device *dev, uint8_t char_buf_id,
		goto unlock;
	}

	if (rx_packet.pid != R502A_ACK_PACKET) {
		LOG_ERR("Error receiving ack packet 0x%X", rx_packet.pid);
		ret = -EIO;
	ret = r502a_validate_rx_packet(&rx_packet);
	if (ret != 0) {
		goto unlock;
	}