• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "nfcForum.h"
17 #include <string.h>
18 
rtdHeader(uint8_t type,NDEFRecordStr * ndefRecord,uint8_t * I2CMsg)19 static void rtdHeader(uint8_t type, NDEFRecordStr *ndefRecord, uint8_t *I2CMsg)
20 {
21 #define I2CMSG_OFFSET_HEADER        0
22 #define I2CMSG_OFFSET_TYPE_LENGTH   1
23 #define I2CMSG_OFFSET_TYPE_CODE     3
24     ndefRecord->header |= 1;
25     ndefRecord->header |= BIT_SR;
26     I2CMsg[I2CMSG_OFFSET_HEADER] = ndefRecord->header;
27 
28     ndefRecord->typeLength = 1;
29     I2CMsg[I2CMSG_OFFSET_TYPE_LENGTH] = ndefRecord->typeLength;
30 
31     ndefRecord->type.typeCode = type;
32     I2CMsg[I2CMSG_OFFSET_TYPE_CODE] = ndefRecord->type.typeCode;
33 }
34 
35 
composeRtdText(const NDEFDataStr * ndef,NDEFRecordStr * ndefRecord,uint8_t * I2CMsg)36 uint8_t composeRtdText(const NDEFDataStr *ndef, NDEFRecordStr *ndefRecord, uint8_t *I2CMsg)
37 {
38     uint8_t retLen;
39     uint32_t offset_text = 4;
40     uint8_t sizeof_header = 3;
41     uint8_t sizeof_str = 3;
42     uint8_t sizeof_type = 1;
43     uint8_t offset_i2cmsg = 2;
44 
45     rtdHeader(RTD_TEXT, ndefRecord, I2CMsg);
46 
47     uint8_t payLoadLen = addRtdText(&ndefRecord->type.typePayload.text);
48     memcpy(&I2CMsg[offset_text], &ndefRecord->type.typePayload.text, payLoadLen);
49 
50     ndefRecord->payloadLength = ndef->rtdPayloadlength + payLoadLen; // added the typePayload
51     I2CMsg[offset_i2cmsg] = ndefRecord->payloadLength;
52 
53     retLen = sizeof_header + sizeof_str + sizeof_type;
54 
55     return retLen;
56 }
57 
58 
composeRtdUri(const NDEFDataStr * ndef,NDEFRecordStr * ndefRecord,uint8_t * I2CMsg)59 uint8_t composeRtdUri(const NDEFDataStr *ndef, NDEFRecordStr *ndefRecord, uint8_t *I2CMsg)
60 {
61     uint32_t offset_text = 4;
62     uint32_t offset_length = 2;
63     uint8_t length = 5;
64 
65     rtdHeader(RTD_URI, ndefRecord, I2CMsg);
66 
67     uint8_t payLoadLen = addRtdUriRecord(ndef, &ndefRecord->type.typePayload.uri);
68     memcpy(&I2CMsg[offset_text], &ndefRecord->type.typePayload.uri, payLoadLen);
69 
70     ndefRecord->payloadLength = ndef->rtdPayloadlength + payLoadLen; // added the typePayload
71     I2CMsg[offset_length] = ndefRecord->payloadLength;
72 
73     return length;
74 }
75 
composeNDEFMBME(bool isFirstRecord,bool isLastRecord,NDEFRecordStr * ndefRecord)76 void composeNDEFMBME(bool isFirstRecord, bool isLastRecord, NDEFRecordStr *ndefRecord)
77 {
78     if (isFirstRecord) {
79         ndefRecord->header |= BIT_MB;
80     } else {
81         ndefRecord->header &= ~MASK_MB;
82     }
83 
84     if (isLastRecord) {
85         ndefRecord->header |= BIT_ME;
86     } else {
87         ndefRecord->header &= ~MASK_ME;
88     }
89 }
90