summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Guiter <olivier.guiter@linux.intel.com>2012-06-20 13:11:39 +0200
committerSamuel Ortiz <sameo@linux.intel.com>2012-06-20 13:11:39 +0200
commite0ea4d9eb4d0dde405ef346d354725e855782ada (patch)
tree809413a68397fbd91567b38818a0ca0212dd27ee
parente2b7f49f59a8d05ace128ee40624c0ab0f309960 (diff)
downloadneard-e0ea4d9eb4d0dde405ef346d354725e855782ada.tar.gz
neard-e0ea4d9eb4d0dde405ef346d354725e855782ada.tar.bz2
neard-e0ea4d9eb4d0dde405ef346d354725e855782ada.zip
ndef: Add NDEF record couting routine
For a defined record type, this routine will count how many records are found in an NDEF.
-rw-r--r--include/ndef.h3
-rw-r--r--src/ndef.c61
2 files changed, 64 insertions, 0 deletions
diff --git a/include/ndef.h b/include/ndef.h
index 0d23a85..4bc5aa9 100644
--- a/include/ndef.h
+++ b/include/ndef.h
@@ -32,6 +32,9 @@ struct near_ndef_message {
uint8_t *data;
};
+int near_ndef_count_records(uint8_t *ndef_in, size_t ndef_in_length,
+ uint8_t record_type);
+
int near_ndef_record_length(uint8_t *ndef_in, size_t ndef_in_length);
GList *near_ndef_parse(uint8_t *ndef_data, size_t ndef_length);
diff --git a/src/ndef.c b/src/ndef.c
index ca47ad6..5a7440e 100644
--- a/src/ndef.c
+++ b/src/ndef.c
@@ -1921,6 +1921,67 @@ int near_ndef_record_length(uint8_t *ndef_in, size_t ndef_in_length)
return ndef_size;
}
+int near_ndef_count_records(uint8_t *ndef_in, size_t ndef_in_length,
+ uint8_t record_type)
+{
+ uint8_t p_mb = 0, p_me = 0, err;
+ size_t offset;
+ struct near_ndef_record *record = NULL;
+ int counted_records = 0 ;
+
+ DBG("");
+
+ offset = 0;
+
+ if (ndef_in == NULL || ndef_in_length < NDEF_MSG_MIN_LENGTH) {
+ err = -EINVAL;
+ goto fail;
+ }
+
+ while (offset < ndef_in_length) {
+ record = g_try_malloc0(sizeof(struct near_ndef_record));
+ if (record == NULL) {
+ err = -ENOMEM;
+ goto fail;
+ }
+
+ /* Create a record */
+ record->header = parse_record_header(ndef_in, offset,
+ ndef_in_length);
+ if (record->header == NULL) {
+ err = -EINVAL;
+ goto fail;
+ }
+
+ /* Validate MB ME */
+ if (validate_record_begin_and_end_bits(&p_mb, &p_me,
+ record->header->mb,
+ record->header->me) != 0) {
+ DBG("validate mb me failed");
+ err = -EINVAL;
+ goto fail;
+ }
+
+ /* Is this what we want ? */
+ if (record->header->rec_type == record_type)
+ counted_records++;
+
+ /* Jump to the next record */
+ offset = record->header->offset + record->header->payload_len;
+
+ free_ndef_record(record);
+ }
+
+ DBG("Type %d Records found: %d", record_type, counted_records);
+
+ return counted_records;
+
+fail:
+ near_error("ndef counting failed");
+ free_ndef_record(record);
+ return err;
+}
+
/**
* @brief Prepare Text ndef record
*