Home | History | Annotate | Download | only in include
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2010-2014 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 /******************************************************************************
     20  *
     21  *  This file contains definitions for some utility functions to help parse
     22  *  and build NFC Data Exchange Format (NDEF) messages
     23  *
     24  ******************************************************************************/
     25 
     26 #ifndef NDEF_UTILS_H
     27 #define NDEF_UTILS_H
     28 
     29 #include "bt_types.h"
     30 
     31 #define NDEF_MB_MASK 0x80  /* Message Begin */
     32 #define NDEF_ME_MASK 0x40  /* Message End */
     33 #define NDEF_CF_MASK 0x20  /* Chunk Flag */
     34 #define NDEF_SR_MASK 0x10  /* Short Record */
     35 #define NDEF_IL_MASK 0x08  /* ID Length */
     36 #define NDEF_TNF_MASK 0x07 /* Type Name Format */
     37 /* First valid ASCII as per RTD specification */
     38 #define NDEF_RTD_VALID_START 0x20
     39 /* Last valid ASCII as per RTD specification */
     40 #define NDEF_RTD_VALID_END 0x7E
     41 
     42 /* NDEF Type Name Format */
     43 #define NDEF_TNF_EMPTY 0     /* Empty (type/id/payload len =0) */
     44 #define NDEF_TNF_WKT 1       /* NFC Forum well-known type/RTD */
     45 #define NDEF_TNF_MEDIA 2     /* Media-type as defined in RFC 2046 */
     46 #define NDEF_TNF_URI 3       /* Absolute URI as defined in RFC 3986 */
     47 #define NDEF_TNF_EXT 4       /* NFC Forum external type/RTD */
     48 #define NDEF_TNF_UNKNOWN 5   /* Unknown (type len =0) */
     49 #define NDEF_TNF_UNCHANGED 6 /* Unchanged (type len =0) */
     50 #define NDEF_TNF_RESERVED 7  /* Reserved */
     51 
     52 /* Define the status code returned from the Validate, Parse or Build functions
     53 */
     54 enum {
     55   NDEF_OK, /* 0 - OK                                   */
     56 
     57   NDEF_REC_NOT_FOUND,         /* 1 - No record matching the find criteria */
     58   NDEF_MSG_TOO_SHORT,         /* 2 - Message was too short (< 3 bytes)    */
     59   NDEF_MSG_NO_MSG_BEGIN,      /* 3 - No 'begin' flag at start of message  */
     60   NDEF_MSG_NO_MSG_END,        /* 4 - No 'end' flag at end of message      */
     61   NDEF_MSG_EXTRA_MSG_BEGIN,   /* 5 - 'begin' flag after start of message  */
     62   NDEF_MSG_UNEXPECTED_CHUNK,  /* 6 - Unexpected chunk found               */
     63   NDEF_MSG_INVALID_EMPTY_REC, /* 7 - Empty record with non-zero contents  */
     64   NDEF_MSG_INVALID_CHUNK,     /* 8 - Invalid chunk found                  */
     65   NDEF_MSG_LENGTH_MISMATCH,   /* 9 - Overall message length doesn't match */
     66   NDEF_MSG_INSUFFICIENT_MEM,  /* 10 - Insuffiecient memory to add record  */
     67   NDEF_MSG_INVALID_TYPE       /* 11 - TYPE field contains invalid characters  */
     68 };
     69 typedef uint8_t tNDEF_STATUS;
     70 
     71 #define HR_REC_TYPE_LEN 2        /* Handover Request Record Type     */
     72 #define HS_REC_TYPE_LEN 2        /* Handover Select Record Type      */
     73 #define HC_REC_TYPE_LEN 2        /* Handover Carrier recrod Type     */
     74 #define CR_REC_TYPE_LEN 2        /* Collision Resolution Record Type */
     75 #define AC_REC_TYPE_LEN 2        /* Alternative Carrier Record Type  */
     76 #define ERR_REC_TYPE_LEN 3       /* Error Record Type                */
     77 #define BT_OOB_REC_TYPE_LEN 32   /* Bluetooth OOB Data Type          */
     78 #define WIFI_WSC_REC_TYPE_LEN 23 /* Wifi WSC Data Type               */
     79 
     80 #ifdef __cplusplus
     81 extern "C" {
     82 #endif
     83 
     84 /* Functions to parse a received NDEF Message
     85 */
     86 /*******************************************************************************
     87 **
     88 ** Function         NDEF_MsgValidate
     89 **
     90 ** Description      This function validates an NDEF message.
     91 **
     92 ** Returns          TRUE if all OK, or FALSE if the message is invalid.
     93 **
     94 *******************************************************************************/
     95 extern tNDEF_STATUS NDEF_MsgValidate(uint8_t* p_msg, uint32_t msg_len,
     96                                      bool b_allow_chunks);
     97 
     98 /*******************************************************************************
     99 **
    100 ** Function         NDEF_MsgGetNumRecs
    101 **
    102 ** Description      This function gets the number of records in the given NDEF
    103 **                  message.
    104 **
    105 ** Returns          The record count, or 0 if the message is invalid.
    106 **
    107 *******************************************************************************/
    108 extern int32_t NDEF_MsgGetNumRecs(uint8_t* p_msg);
    109 
    110 /*******************************************************************************
    111 **
    112 ** Function         NDEF_MsgGetRecLength
    113 **
    114 ** Description      This function returns length of the current record in the
    115 **                  given NDEF message.
    116 **
    117 ** Returns          Length of record
    118 **
    119 *******************************************************************************/
    120 extern uint32_t NDEF_MsgGetRecLength(uint8_t* p_cur_rec);
    121 
    122 /*******************************************************************************
    123 **
    124 ** Function         NDEF_MsgGetNextRec
    125 **
    126 ** Description      This function gets a pointer to the next record after the
    127 **                  current one.
    128 **
    129 ** Returns          Pointer to the start of the record, or NULL if no more
    130 **
    131 *******************************************************************************/
    132 extern uint8_t* NDEF_MsgGetNextRec(uint8_t* p_cur_rec);
    133 
    134 /*******************************************************************************
    135 **
    136 ** Function         NDEF_MsgGetRecByIndex
    137 **
    138 ** Description      This function gets a pointer to the record with the given
    139 **                  index (0-based index) in the given NDEF message.
    140 **
    141 ** Returns          Pointer to the start of the record, or NULL
    142 **
    143 *******************************************************************************/
    144 extern uint8_t* NDEF_MsgGetRecByIndex(uint8_t* p_msg, int32_t index);
    145 
    146 /*******************************************************************************
    147 **
    148 ** Function         NDEF_MsgGetLastRecInMsg
    149 **
    150 ** Description      This function gets a pointer to the last record in the
    151 **                  given NDEF message.
    152 **
    153 ** Returns          Pointer to the start of the last record, or NULL if some
    154 **                  problem
    155 **
    156 *******************************************************************************/
    157 extern uint8_t* NDEF_MsgGetLastRecInMsg(uint8_t* p_msg);
    158 
    159 /*******************************************************************************
    160 **
    161 ** Function         NDEF_MsgGetFirstRecByType
    162 **
    163 ** Description      This function gets a pointer to the first record with the
    164 **                  given record type in the given NDEF message.
    165 **
    166 ** Returns          Pointer to the start of the record, or NULL
    167 **
    168 *******************************************************************************/
    169 extern uint8_t* NDEF_MsgGetFirstRecByType(uint8_t* p_msg, uint8_t tnf,
    170                                           uint8_t* p_type, uint8_t tlen);
    171 
    172 /*******************************************************************************
    173 **
    174 ** Function         NDEF_MsgGetNextRecByType
    175 **
    176 ** Description      This function gets a pointer to the next record with the
    177 **                  given record type in the given NDEF message.
    178 **
    179 ** Returns          Pointer to the start of the record, or NULL
    180 **
    181 *******************************************************************************/
    182 extern uint8_t* NDEF_MsgGetNextRecByType(uint8_t* p_cur_rec, uint8_t tnf,
    183                                          uint8_t* p_type, uint8_t tlen);
    184 
    185 /*******************************************************************************
    186 **
    187 ** Function         NDEF_MsgGetFirstRecById
    188 **
    189 ** Description      This function gets a pointer to the first record with the
    190 **                  given record id in the given NDEF message.
    191 **
    192 ** Returns          Pointer to the start of the record, or NULL
    193 **
    194 *******************************************************************************/
    195 extern uint8_t* NDEF_MsgGetFirstRecById(uint8_t* p_msg, uint8_t* p_id,
    196                                         uint8_t ilen);
    197 
    198 /*******************************************************************************
    199 **
    200 ** Function         NDEF_MsgGetNextRecById
    201 **
    202 ** Description      This function gets a pointer to the next record with the
    203 **                  given record id in the given NDEF message.
    204 **
    205 ** Returns          Pointer to the start of the record, or NULL
    206 **
    207 *******************************************************************************/
    208 extern uint8_t* NDEF_MsgGetNextRecById(uint8_t* p_cur_rec, uint8_t* p_id,
    209                                        uint8_t ilen);
    210 
    211 /*******************************************************************************
    212 **
    213 ** Function         NDEF_RecGetType
    214 **
    215 ** Description      This function gets a pointer to the record type for the
    216 **                  given NDEF record.
    217 **
    218 ** Returns          Pointer to Type (NULL if none). TNF and len are filled in.
    219 **
    220 *******************************************************************************/
    221 extern uint8_t* NDEF_RecGetType(uint8_t* p_rec, uint8_t* p_tnf,
    222                                 uint8_t* p_type_len);
    223 
    224 /*******************************************************************************
    225 **
    226 ** Function         NDEF_RecGetId
    227 **
    228 ** Description      This function gets a pointer to the record id for the given
    229 **                  NDEF record.
    230 **
    231 ** Returns          Pointer to Id (NULL if none). ID Len is filled in.
    232 **
    233 *******************************************************************************/
    234 extern uint8_t* NDEF_RecGetId(uint8_t* p_rec, uint8_t* p_id_len);
    235 
    236 /*******************************************************************************
    237 **
    238 ** Function         NDEF_RecGetPayload
    239 **
    240 ** Description      This function gets a pointer to the payload for the given
    241 **                  NDEF record.
    242 **
    243 ** Returns          a pointer to the payload (NULL if none). Payload len filled
    244 **                  in.
    245 **
    246 *******************************************************************************/
    247 extern uint8_t* NDEF_RecGetPayload(uint8_t* p_rec, uint32_t* p_payload_len);
    248 
    249 /* Functions to build an NDEF Message
    250 */
    251 /*******************************************************************************
    252 **
    253 ** Function         NDEF_MsgInit
    254 **
    255 ** Description      This function initializes an NDEF message.
    256 **
    257 ** Returns          void
    258 **                  *p_cur_size is initialized to 0
    259 **
    260 *******************************************************************************/
    261 extern void NDEF_MsgInit(uint8_t* p_msg, uint32_t max_size,
    262                          uint32_t* p_cur_size);
    263 
    264 /*******************************************************************************
    265 **
    266 ** Function         NDEF_MsgAddRec
    267 **
    268 ** Description      This function adds an NDEF record to the end of an NDEF
    269 **                  message.
    270 **
    271 ** Returns          OK, or error if the record did not fit
    272 **                  *p_cur_size is updated
    273 **
    274 *******************************************************************************/
    275 extern tNDEF_STATUS NDEF_MsgAddRec(uint8_t* p_msg, uint32_t max_size,
    276                                    uint32_t* p_cur_size, uint8_t tnf,
    277                                    uint8_t* p_type, uint8_t type_len,
    278                                    uint8_t* p_id, uint8_t id_len,
    279                                    uint8_t* p_payload, uint32_t payload_len);
    280 
    281 /*******************************************************************************
    282 **
    283 ** Function         NDEF_MsgInsertRec
    284 **
    285 ** Description      This function inserts a record at a specific index into the
    286 **                  given NDEF message
    287 **
    288 ** Returns          OK, or error if the record did not fit
    289 **                  *p_cur_size is updated
    290 **
    291 *******************************************************************************/
    292 extern tNDEF_STATUS NDEF_MsgInsertRec(uint8_t* p_msg, uint32_t max_size,
    293                                       uint32_t* p_cur_size, int32_t index,
    294                                       uint8_t tnf, uint8_t* p_type,
    295                                       uint8_t type_len, uint8_t* p_id,
    296                                       uint8_t id_len, uint8_t* p_payload,
    297                                       uint32_t payload_len);
    298 
    299 /*******************************************************************************
    300 **
    301 ** Function         NDEF_MsgAppendRec
    302 **
    303 ** Description      This function adds NDEF records to the end of an NDEF
    304 **                  message.
    305 **
    306 ** Returns          OK, or error if the record did not fit
    307 **                  *p_cur_size is updated
    308 **
    309 *******************************************************************************/
    310 extern tNDEF_STATUS NDEF_MsgAppendRec(uint8_t* p_msg, uint32_t max_size,
    311                                       uint32_t* p_cur_size, uint8_t* p_new_rec,
    312                                       uint32_t new_rec_len);
    313 
    314 /*******************************************************************************
    315 **
    316 ** Function         NDEF_MsgAppendPayload
    317 **
    318 ** Description      This function appends extra payload to a specific record in
    319 **                  the given NDEF message
    320 **
    321 ** Returns          OK, or error if the extra payload did not fit
    322 **                  *p_cur_size is updated
    323 **
    324 *******************************************************************************/
    325 extern tNDEF_STATUS NDEF_MsgAppendPayload(uint8_t* p_msg, uint32_t max_size,
    326                                           uint32_t* p_cur_size, uint8_t* p_rec,
    327                                           uint8_t* p_add_pl,
    328                                           uint32_t add_pl_len);
    329 
    330 /*******************************************************************************
    331 **
    332 ** Function         NDEF_MsgReplacePayload
    333 **
    334 ** Description      This function replaces the payload of a specific record in
    335 **                  the given NDEF message
    336 **
    337 ** Returns          OK, or error if the new payload did not fit
    338 **                  *p_cur_size is updated
    339 **
    340 *******************************************************************************/
    341 extern tNDEF_STATUS NDEF_MsgReplacePayload(uint8_t* p_msg, uint32_t max_size,
    342                                            uint32_t* p_cur_size, uint8_t* p_rec,
    343                                            uint8_t* p_new_pl,
    344                                            uint32_t new_pl_len);
    345 
    346 /*******************************************************************************
    347 **
    348 ** Function         NDEF_MsgReplaceType
    349 **
    350 ** Description      This function replaces the type field of a specific record
    351 **                  in the given NDEF message
    352 **
    353 ** Returns          OK, or error if the new type field did not fit
    354 **                  *p_cur_size is updated
    355 **
    356 *******************************************************************************/
    357 extern tNDEF_STATUS NDEF_MsgReplaceType(uint8_t* p_msg, uint32_t max_size,
    358                                         uint32_t* p_cur_size, uint8_t* p_rec,
    359                                         uint8_t* p_new_type,
    360                                         uint8_t new_type_len);
    361 
    362 /*******************************************************************************
    363 **
    364 ** Function         NDEF_MsgReplaceId
    365 **
    366 ** Description      This function replaces the ID field of a specific record in
    367 **                  the given NDEF message
    368 **
    369 ** Returns          OK, or error if the new ID field did not fit
    370 **                  *p_cur_size is updated
    371 **
    372 *******************************************************************************/
    373 extern tNDEF_STATUS NDEF_MsgReplaceId(uint8_t* p_msg, uint32_t max_size,
    374                                       uint32_t* p_cur_size, uint8_t* p_rec,
    375                                       uint8_t* p_new_id, uint8_t new_id_len);
    376 
    377 /*******************************************************************************
    378 **
    379 ** Function         NDEF_MsgRemoveRec
    380 **
    381 ** Description      This function removes the record at the given
    382 **                  index in the given NDEF message.
    383 **
    384 ** Returns          OK, or error if the index was invalid
    385 **                  *p_cur_size is updated
    386 **
    387 *******************************************************************************/
    388 extern tNDEF_STATUS NDEF_MsgRemoveRec(uint8_t* p_msg, uint32_t* p_cur_size,
    389                                       int32_t index);
    390 
    391 /*******************************************************************************
    392 **
    393 ** Function         NDEF_MsgCopyAndDechunk
    394 **
    395 ** Description      This function copies and de-chunks an NDEF message.
    396 **                  It is assumed that the destination is at least as large
    397 **                  as the source, since the source may not actually contain
    398 **                  any chunks.
    399 **
    400 ** Returns          The output byte count
    401 **
    402 *******************************************************************************/
    403 extern tNDEF_STATUS NDEF_MsgCopyAndDechunk(uint8_t* p_src, uint32_t src_len,
    404                                            uint8_t* p_dest,
    405                                            uint32_t* p_out_len);
    406 
    407 #ifdef __cplusplus
    408 }
    409 #endif
    410 
    411 #endif /* NDEF_UTILS_H */
    412