pdr: Add pldm_pdr_delete_by_sensor_id() API

Adds a new libpldm API to delete the PDR record from the PDR repo
based on the sensor ID of the PDR. This API is used when a state
sensor PDR record needs to be deleted based on the sensor ID
and return the corresponding record handle deleted.

Change-Id: Ia78c24467f6460fdcf456a31ac851fa7b0c0b385
Signed-off-by: Pavithra Barithaya <pavithrabarithaya07@gmail.com>
diff --git a/src/dsp/pdr.c b/src/dsp/pdr.c
index e549fbb..69229d5 100644
--- a/src/dsp/pdr.c
+++ b/src/dsp/pdr.c
@@ -403,6 +403,80 @@
 	       record_handle <= last_record_handle;
 }
 
+LIBPLDM_CC_NONNULL
+static int decode_pldm_state_sensor_pdr(uint8_t *data, uint32_t size,
+					struct pldm_state_sensor_pdr *pdr)
+{
+	PLDM_MSGBUF_DEFINE_P(buf);
+	int rc = 0;
+	rc = pldm_msgbuf_init_errno(buf, sizeof(struct pldm_state_sensor_pdr),
+				    (uint8_t *)data, size);
+	if (rc) {
+		return rc;
+	}
+
+	pldm_msgbuf_extract(buf, pdr->hdr.record_handle);
+	pldm_msgbuf_extract(buf, pdr->hdr.version);
+	pldm_msgbuf_extract(buf, pdr->hdr.type);
+	pldm_msgbuf_extract(buf, pdr->hdr.record_change_num);
+	pldm_msgbuf_extract(buf, pdr->hdr.length);
+	pldm_msgbuf_extract(buf, pdr->terminus_handle);
+	pldm_msgbuf_extract(buf, pdr->sensor_id);
+	pldm_msgbuf_extract(buf, pdr->entity_type);
+	pldm_msgbuf_extract(buf, pdr->entity_instance);
+	pldm_msgbuf_extract(buf, pdr->container_id);
+	pldm_msgbuf_extract(buf, pdr->sensor_init);
+	pldm_msgbuf_extract(buf, pdr->sensor_auxiliary_names_pdr);
+	pldm_msgbuf_extract(buf, pdr->composite_sensor_count);
+
+	return pldm_msgbuf_complete(buf);
+}
+
+LIBPLDM_ABI_TESTING
+int pldm_pdr_delete_by_sensor_id(pldm_pdr *repo, uint16_t sensor_id,
+				 bool is_remote, uint32_t *record_handle)
+{
+	pldm_pdr_record *record;
+	pldm_pdr_record *prev = NULL;
+	int rc = 0;
+	int found;
+	struct pldm_state_sensor_pdr pdr;
+
+	if (!repo) {
+		return -EINVAL;
+	}
+
+	record = repo->first;
+
+	while (record != NULL) {
+		if (!record->data) {
+			continue;
+		}
+
+		rc = decode_pldm_state_sensor_pdr(record->data, record->size,
+						  &pdr);
+		if (rc) {
+			return rc;
+		}
+
+		if (record->is_remote != is_remote ||
+		    pdr.hdr.type != PLDM_STATE_SENSOR_PDR) {
+			record = record->next;
+			continue;
+		}
+		found = pdr.sensor_id == sensor_id;
+		if (found) {
+			if (record_handle) {
+				*record_handle = record->record_handle;
+			}
+			prev = pldm_pdr_get_prev_record(repo, record);
+			return pldm_pdr_remove_record(repo, record, prev);
+		}
+		record = record->next;
+	}
+	return -ENOENT;
+}
+
 LIBPLDM_ABI_TESTING
 int pldm_pdr_find_child_container_id_index_range_exclude(
 	const pldm_pdr *repo, uint16_t entity_type, uint16_t entity_instance,