AppNearMe µNFC stack for the NXP PN532 chip License: You can use the stack free of charge to prototype with mbed; if you want to use the stack with your commercial product, get in touch!
Dependents: IOT_sensor_nfc AppNearMe_MuNFC_PN532_Test p2p_nfc_test NFCMoodLamp ... more
License
You can use the stack free of charge to prototype with mbed; if you want to use the stack with your commercial product, get in touch!
PN532/TLVList.cpp@13:6f98851e025b, 2013-01-15 (annotated)
- Committer:
- AppNearMe
- Date:
- Tue Jan 15 15:34:39 2013 +0000
- Revision:
- 13:6f98851e025b
- Parent:
- 11:5be631376e5b
renamed rtos.h to platform_rtos.h to prevent insludes mess
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AppNearMe | 11:5be631376e5b | 1 | /* |
AppNearMe | 11:5be631376e5b | 2 | TLVList.cpp |
AppNearMe | 11:5be631376e5b | 3 | Copyright (c) Donatien Garnier 2012 |
AppNearMe | 11:5be631376e5b | 4 | donatien.garnier@appnearme.com |
AppNearMe | 11:5be631376e5b | 5 | http://www.appnearme.com/ |
AppNearMe | 11:5be631376e5b | 6 | */ |
AppNearMe | 11:5be631376e5b | 7 | |
AppNearMe | 11:5be631376e5b | 8 | #include "TLVList.h" |
AppNearMe | 11:5be631376e5b | 9 | |
AppNearMe | 11:5be631376e5b | 10 | #include "munfc/core/fwk.h" |
AppNearMe | 11:5be631376e5b | 11 | #include "munfc/ndef/appnearme_ndef_tlv.h" |
AppNearMe | 11:5be631376e5b | 12 | |
AppNearMe | 11:5be631376e5b | 13 | #include <cstring> //For memcpy, strlen |
AppNearMe | 11:5be631376e5b | 14 | using std::memcpy; |
AppNearMe | 11:5be631376e5b | 15 | using std::strlen; |
AppNearMe | 11:5be631376e5b | 16 | |
AppNearMe | 11:5be631376e5b | 17 | TLVList::TLVList() : m_tlvList(NULL) |
AppNearMe | 11:5be631376e5b | 18 | { |
AppNearMe | 11:5be631376e5b | 19 | |
AppNearMe | 11:5be631376e5b | 20 | } |
AppNearMe | 11:5be631376e5b | 21 | |
AppNearMe | 11:5be631376e5b | 22 | void TLVList::wrap(tlv_list* payload) |
AppNearMe | 11:5be631376e5b | 23 | { |
AppNearMe | 11:5be631376e5b | 24 | m_tlvList = payload; |
AppNearMe | 11:5be631376e5b | 25 | m_space = true; |
AppNearMe | 11:5be631376e5b | 26 | m_type = NONE; |
AppNearMe | 11:5be631376e5b | 27 | } |
AppNearMe | 11:5be631376e5b | 28 | |
AppNearMe | 11:5be631376e5b | 29 | |
AppNearMe | 11:5be631376e5b | 30 | |
AppNearMe | 11:5be631376e5b | 31 | /* Getters */ |
AppNearMe | 11:5be631376e5b | 32 | |
AppNearMe | 11:5be631376e5b | 33 | /** Iterate to next item |
AppNearMe | 11:5be631376e5b | 34 | * @return type if next item exists, NONE otherwise |
AppNearMe | 11:5be631376e5b | 35 | */ |
AppNearMe | 11:5be631376e5b | 36 | TLVList::TLVType TLVList::getNext() |
AppNearMe | 11:5be631376e5b | 37 | { |
AppNearMe | 11:5be631376e5b | 38 | if(!m_tlvList) |
AppNearMe | 11:5be631376e5b | 39 | { |
AppNearMe | 11:5be631376e5b | 40 | return NONE; |
AppNearMe | 11:5be631376e5b | 41 | } |
AppNearMe | 11:5be631376e5b | 42 | |
AppNearMe | 11:5be631376e5b | 43 | m_type = (TLVList::TLVType) appnearme_ndef_tlv_next_type(m_tlvList); //Explicit cast, this is the same enum |
AppNearMe | 11:5be631376e5b | 44 | switch(m_type) |
AppNearMe | 11:5be631376e5b | 45 | { |
AppNearMe | 11:5be631376e5b | 46 | case UINT8: |
AppNearMe | 11:5be631376e5b | 47 | m_arrayLen = 0; |
AppNearMe | 11:5be631376e5b | 48 | m_uint8 = appnearme_ndef_tlv_get_uint8(m_tlvList); |
AppNearMe | 11:5be631376e5b | 49 | break; |
AppNearMe | 11:5be631376e5b | 50 | case UINT32: |
AppNearMe | 11:5be631376e5b | 51 | m_arrayLen = 0; |
AppNearMe | 11:5be631376e5b | 52 | m_uint32 = appnearme_ndef_tlv_get_uint32(m_tlvList); |
AppNearMe | 11:5be631376e5b | 53 | break; |
AppNearMe | 11:5be631376e5b | 54 | case UINT8_ARRAY: |
AppNearMe | 11:5be631376e5b | 55 | //In this order, otherwise would break state-machine |
AppNearMe | 11:5be631376e5b | 56 | m_arrayLen = appnearme_ndef_tlv_get_array_length(m_tlvList); |
AppNearMe | 11:5be631376e5b | 57 | m_array = appnearme_ndef_tlv_get_array(m_tlvList); |
AppNearMe | 11:5be631376e5b | 58 | break; |
AppNearMe | 11:5be631376e5b | 59 | case STRING: |
AppNearMe | 11:5be631376e5b | 60 | //In this order, otherwise would break state-machine |
AppNearMe | 11:5be631376e5b | 61 | m_strLen = appnearme_ndef_tlv_get_string_length(m_tlvList); |
AppNearMe | 11:5be631376e5b | 62 | m_str = appnearme_ndef_tlv_get_string(m_tlvList); |
AppNearMe | 11:5be631376e5b | 63 | break; |
AppNearMe | 11:5be631376e5b | 64 | } |
AppNearMe | 11:5be631376e5b | 65 | return m_type; |
AppNearMe | 11:5be631376e5b | 66 | } |
AppNearMe | 11:5be631376e5b | 67 | |
AppNearMe | 11:5be631376e5b | 68 | /** Get current item's type. |
AppNearMe | 11:5be631376e5b | 69 | * @return type if item exists, NONE otherwise |
AppNearMe | 11:5be631376e5b | 70 | */ |
AppNearMe | 11:5be631376e5b | 71 | TLVList::TLVType TLVList::getType() |
AppNearMe | 11:5be631376e5b | 72 | { |
AppNearMe | 11:5be631376e5b | 73 | return m_type; |
AppNearMe | 11:5be631376e5b | 74 | } |
AppNearMe | 11:5be631376e5b | 75 | |
AppNearMe | 11:5be631376e5b | 76 | /** Get uint8_t value |
AppNearMe | 11:5be631376e5b | 77 | * If the current item's type is uint32_t, the value will be masked with 0xFF |
AppNearMe | 11:5be631376e5b | 78 | * @return uint8_t value OR 0 if the type is incompatible |
AppNearMe | 11:5be631376e5b | 79 | */ |
AppNearMe | 11:5be631376e5b | 80 | uint8_t TLVList::getUInt8() |
AppNearMe | 11:5be631376e5b | 81 | { |
AppNearMe | 11:5be631376e5b | 82 | switch(m_type) |
AppNearMe | 11:5be631376e5b | 83 | { |
AppNearMe | 11:5be631376e5b | 84 | case UINT8: |
AppNearMe | 11:5be631376e5b | 85 | return m_uint8; |
AppNearMe | 11:5be631376e5b | 86 | case UINT32: |
AppNearMe | 11:5be631376e5b | 87 | return m_uint32 & 0xFF; |
AppNearMe | 11:5be631376e5b | 88 | default: |
AppNearMe | 11:5be631376e5b | 89 | return 0; |
AppNearMe | 11:5be631376e5b | 90 | } |
AppNearMe | 11:5be631376e5b | 91 | } |
AppNearMe | 11:5be631376e5b | 92 | |
AppNearMe | 11:5be631376e5b | 93 | /** Get uint32_t value |
AppNearMe | 11:5be631376e5b | 94 | * If the current item's type is uint8_t, the value will be casted to uint32_t |
AppNearMe | 11:5be631376e5b | 95 | * @return uint32_t value OR 0 if the type is incompatible |
AppNearMe | 11:5be631376e5b | 96 | */ |
AppNearMe | 11:5be631376e5b | 97 | uint32_t TLVList::getUInt32() |
AppNearMe | 11:5be631376e5b | 98 | { |
AppNearMe | 11:5be631376e5b | 99 | switch(m_type) |
AppNearMe | 11:5be631376e5b | 100 | { |
AppNearMe | 11:5be631376e5b | 101 | case UINT32: |
AppNearMe | 11:5be631376e5b | 102 | return m_uint32; |
AppNearMe | 11:5be631376e5b | 103 | case UINT8: |
AppNearMe | 11:5be631376e5b | 104 | return m_uint8 & 0xFF; |
AppNearMe | 11:5be631376e5b | 105 | default: |
AppNearMe | 11:5be631376e5b | 106 | return 0; |
AppNearMe | 11:5be631376e5b | 107 | } |
AppNearMe | 11:5be631376e5b | 108 | } |
AppNearMe | 11:5be631376e5b | 109 | |
AppNearMe | 11:5be631376e5b | 110 | /** Get array length |
AppNearMe | 11:5be631376e5b | 111 | * @return bytes array length |
AppNearMe | 11:5be631376e5b | 112 | */ |
AppNearMe | 11:5be631376e5b | 113 | size_t TLVList::getArrayLength() |
AppNearMe | 11:5be631376e5b | 114 | { |
AppNearMe | 11:5be631376e5b | 115 | switch(m_type) |
AppNearMe | 11:5be631376e5b | 116 | { |
AppNearMe | 11:5be631376e5b | 117 | case UINT8_ARRAY: |
AppNearMe | 11:5be631376e5b | 118 | return m_arrayLen; |
AppNearMe | 11:5be631376e5b | 119 | default: |
AppNearMe | 11:5be631376e5b | 120 | return 0; |
AppNearMe | 11:5be631376e5b | 121 | } |
AppNearMe | 11:5be631376e5b | 122 | } |
AppNearMe | 11:5be631376e5b | 123 | |
AppNearMe | 11:5be631376e5b | 124 | /** Get array |
AppNearMe | 11:5be631376e5b | 125 | * @param buf pointer to buffer's start |
AppNearMe | 11:5be631376e5b | 126 | * @param maxLen maximum number of bytes to copy |
AppNearMe | 11:5be631376e5b | 127 | * @return number of copied bytes |
AppNearMe | 11:5be631376e5b | 128 | */ |
AppNearMe | 11:5be631376e5b | 129 | size_t TLVList::getArray(uint8_t* buf, size_t maxLen) |
AppNearMe | 11:5be631376e5b | 130 | { |
AppNearMe | 11:5be631376e5b | 131 | if(m_type != UINT8_ARRAY) |
AppNearMe | 11:5be631376e5b | 132 | { |
AppNearMe | 11:5be631376e5b | 133 | return 0; |
AppNearMe | 11:5be631376e5b | 134 | } |
AppNearMe | 11:5be631376e5b | 135 | size_t len = MIN(maxLen, m_arrayLen); |
AppNearMe | 11:5be631376e5b | 136 | memcpy(buf, m_array, len); |
AppNearMe | 11:5be631376e5b | 137 | return len; |
AppNearMe | 11:5be631376e5b | 138 | } |
AppNearMe | 11:5be631376e5b | 139 | |
AppNearMe | 11:5be631376e5b | 140 | /** Get string length |
AppNearMe | 11:5be631376e5b | 141 | * @return string length |
AppNearMe | 11:5be631376e5b | 142 | */ |
AppNearMe | 11:5be631376e5b | 143 | size_t TLVList::getStringLength() |
AppNearMe | 11:5be631376e5b | 144 | { |
AppNearMe | 11:5be631376e5b | 145 | switch(m_type) |
AppNearMe | 11:5be631376e5b | 146 | { |
AppNearMe | 11:5be631376e5b | 147 | case STRING: |
AppNearMe | 11:5be631376e5b | 148 | return m_strLen; |
AppNearMe | 11:5be631376e5b | 149 | default: |
AppNearMe | 11:5be631376e5b | 150 | return 0; |
AppNearMe | 11:5be631376e5b | 151 | } |
AppNearMe | 11:5be631376e5b | 152 | } |
AppNearMe | 11:5be631376e5b | 153 | |
AppNearMe | 11:5be631376e5b | 154 | |
AppNearMe | 11:5be631376e5b | 155 | /** Get string |
AppNearMe | 11:5be631376e5b | 156 | * Copy string to buffer (including null-terminating char) |
AppNearMe | 11:5be631376e5b | 157 | * @param str pointer to string's start |
AppNearMe | 11:5be631376e5b | 158 | * @param maxLen maximum number of chars to copy (not including null-terminating char) |
AppNearMe | 11:5be631376e5b | 159 | * @return number of copied chars |
AppNearMe | 11:5be631376e5b | 160 | */ |
AppNearMe | 11:5be631376e5b | 161 | size_t TLVList::getString(char* str, size_t maxLen) |
AppNearMe | 11:5be631376e5b | 162 | { |
AppNearMe | 11:5be631376e5b | 163 | if(m_type != STRING) |
AppNearMe | 11:5be631376e5b | 164 | { |
AppNearMe | 11:5be631376e5b | 165 | return 0; |
AppNearMe | 11:5be631376e5b | 166 | } |
AppNearMe | 11:5be631376e5b | 167 | size_t len = MIN(maxLen, m_strLen); |
AppNearMe | 11:5be631376e5b | 168 | memcpy(str, m_str, len); |
AppNearMe | 11:5be631376e5b | 169 | str[len] = '\0'; //Add null-terminating char |
AppNearMe | 11:5be631376e5b | 170 | return len; |
AppNearMe | 11:5be631376e5b | 171 | } |
AppNearMe | 11:5be631376e5b | 172 | |
AppNearMe | 11:5be631376e5b | 173 | /* Setters */ |
AppNearMe | 11:5be631376e5b | 174 | |
AppNearMe | 11:5be631376e5b | 175 | /** Check whether there is space left in list |
AppNearMe | 11:5be631376e5b | 176 | * @return true if there is space left, false otherwise |
AppNearMe | 11:5be631376e5b | 177 | */ |
AppNearMe | 11:5be631376e5b | 178 | bool TLVList::isSpace() |
AppNearMe | 11:5be631376e5b | 179 | { |
AppNearMe | 11:5be631376e5b | 180 | return m_space; |
AppNearMe | 11:5be631376e5b | 181 | } |
AppNearMe | 11:5be631376e5b | 182 | |
AppNearMe | 11:5be631376e5b | 183 | /** Put uint8_t value |
AppNearMe | 11:5be631376e5b | 184 | * @param value uint8_t value |
AppNearMe | 11:5be631376e5b | 185 | * @return true on success, false if there is not enough space in buffer |
AppNearMe | 11:5be631376e5b | 186 | */ |
AppNearMe | 11:5be631376e5b | 187 | bool TLVList::putUInt8(uint8_t value) |
AppNearMe | 11:5be631376e5b | 188 | { |
AppNearMe | 11:5be631376e5b | 189 | int res = appnearme_ndef_tlv_put_uint8(m_tlvList, value); |
AppNearMe | 11:5be631376e5b | 190 | if( !res ) |
AppNearMe | 11:5be631376e5b | 191 | { |
AppNearMe | 11:5be631376e5b | 192 | m_space = false; |
AppNearMe | 11:5be631376e5b | 193 | return false; |
AppNearMe | 11:5be631376e5b | 194 | } |
AppNearMe | 11:5be631376e5b | 195 | return true; |
AppNearMe | 11:5be631376e5b | 196 | } |
AppNearMe | 11:5be631376e5b | 197 | |
AppNearMe | 11:5be631376e5b | 198 | /** Put uint32_t value |
AppNearMe | 11:5be631376e5b | 199 | * @param value uint32_t value |
AppNearMe | 11:5be631376e5b | 200 | * @return true on success, false if there is not enough space in buffer |
AppNearMe | 11:5be631376e5b | 201 | */ |
AppNearMe | 11:5be631376e5b | 202 | bool TLVList::putUInt32(uint32_t value) |
AppNearMe | 11:5be631376e5b | 203 | { |
AppNearMe | 11:5be631376e5b | 204 | int res = appnearme_ndef_tlv_put_uint32(m_tlvList, value); |
AppNearMe | 11:5be631376e5b | 205 | if( !res ) |
AppNearMe | 11:5be631376e5b | 206 | { |
AppNearMe | 11:5be631376e5b | 207 | m_space = false; |
AppNearMe | 11:5be631376e5b | 208 | return false; |
AppNearMe | 11:5be631376e5b | 209 | } |
AppNearMe | 11:5be631376e5b | 210 | return true; |
AppNearMe | 11:5be631376e5b | 211 | } |
AppNearMe | 11:5be631376e5b | 212 | |
AppNearMe | 11:5be631376e5b | 213 | /** Put array |
AppNearMe | 11:5be631376e5b | 214 | * @param buf pointer to buffer's start |
AppNearMe | 11:5be631376e5b | 215 | * @param len number of bytes to copy |
AppNearMe | 11:5be631376e5b | 216 | * @return number of copied bytes |
AppNearMe | 11:5be631376e5b | 217 | */ |
AppNearMe | 11:5be631376e5b | 218 | size_t TLVList::putArray(uint8_t* buf, size_t len) |
AppNearMe | 11:5be631376e5b | 219 | { |
AppNearMe | 11:5be631376e5b | 220 | int res = appnearme_ndef_tlv_put_array(m_tlvList, buf, len); |
AppNearMe | 11:5be631376e5b | 221 | if( !res ) |
AppNearMe | 11:5be631376e5b | 222 | { |
AppNearMe | 11:5be631376e5b | 223 | m_space = false; |
AppNearMe | 11:5be631376e5b | 224 | return 0; |
AppNearMe | 11:5be631376e5b | 225 | } |
AppNearMe | 11:5be631376e5b | 226 | return len; |
AppNearMe | 11:5be631376e5b | 227 | } |
AppNearMe | 11:5be631376e5b | 228 | |
AppNearMe | 11:5be631376e5b | 229 | /** Put string |
AppNearMe | 11:5be631376e5b | 230 | * @param str pointer to null-terminated string's start |
AppNearMe | 11:5be631376e5b | 231 | * @return number of copied bytes |
AppNearMe | 11:5be631376e5b | 232 | */ |
AppNearMe | 11:5be631376e5b | 233 | size_t TLVList::putString(char* str) |
AppNearMe | 11:5be631376e5b | 234 | { |
AppNearMe | 11:5be631376e5b | 235 | int res = appnearme_ndef_tlv_put_string(m_tlvList, str); |
AppNearMe | 11:5be631376e5b | 236 | if( !res ) |
AppNearMe | 11:5be631376e5b | 237 | { |
AppNearMe | 11:5be631376e5b | 238 | m_space = false; |
AppNearMe | 11:5be631376e5b | 239 | return 0; |
AppNearMe | 11:5be631376e5b | 240 | } |
AppNearMe | 11:5be631376e5b | 241 | return strlen(str); |
AppNearMe | 11:5be631376e5b | 242 | } |
AppNearMe | 11:5be631376e5b | 243 | |
AppNearMe | 11:5be631376e5b | 244 | |
AppNearMe | 11:5be631376e5b | 245 | |
AppNearMe | 11:5be631376e5b | 246 |