AiCPlayer
Interface of aic vm - for rendering aspect, sensors, video records
mockVMLibNci.h
Go to the documentation of this file.
1 
2 #include <stdio.h>
3 #include <signal.h>
4 #include <stdlib.h>
5 #include <stdint.h>
6 #include "sensors.h"
7 #include "player_nfc.h"
8 #include "nfc.pb-c.h"
9 #include "buffer_sizes.h"
10 #include "socket.h"
11 
12 #include "amqp_listen.h"
13 #include "config_env.h"
14 
15 #include "logger.h"
16 #include <pthread.h>
17 
18 //_____________________
19 
20 #define NCI_BRCM_CO_ID 0x2E
21 
22 /* Define the message header size for all NCI Commands and Notifications.
23 */
24 #define NCI_MSG_HDR_SIZE 3 /* per NCI spec */
25 #define NCI_DATA_HDR_SIZE 3 /* per NCI spec */
26 #define NCI_MAX_PAYLOAD_SIZE 0xFE
27 #define NCI_MAX_CTRL_SIZE 0xFF /* max control message size */
28 #define NCI_CTRL_INIT_SIZE 32 /* initial NFCC control payload size */
29 #define NCI_MAX_VSC_SIZE 0xFF
30 #define NCI_VSC_MSG_HDR_SIZE \
31  12 /* NCI header (3) + callback function pointer(8; use 8 to be safe) + HCIT (1 byte) */
32 #define NCI_TL_SIZE 2
33 
34 #define NCI_ISO_DEP_MAX_INFO \
35  253 /* Max frame size (256) - Prologue (1) - Epilogue (2) in ISO-DEP, CID and NAD are not \
36  used*/
37 #define NCI_NFC_DEP_MAX_DATA \
38  251 /* Max payload (254) - Protocol Header (3) in NFC-DEP, DID and NAD are not used */
39 
40 /* NCI Command and Notification Format:
41  * 3 byte message header:
42  * byte 0: MT PBF GID
43  * byte 1: OID
44  * byte 2: Message Length */
45 /* MT: Message Type (byte 0) */
46 #define NCI_MT_MASK 0xE0
47 #define NCI_MT_SHIFT 5
48 #define NCI_MT_DATA 0x00
49 #define NCI_MT_CMD 1 /* (NCI_MT_CMD << NCI_MT_SHIFT) = 0x20 */
50 #define NCI_MT_RSP 2 /* (NCI_MT_RSP << NCI_MT_SHIFT) = 0x40 */
51 #define NCI_MT_NTF 3 /* (NCI_MT_NTF << NCI_MT_SHIFT) = 0x60 */
52 #define NCI_MT_CFG 4 /* (NCI_MT_CFG << NCI_MT_SHIFT) = 0x80 */
53 
54 #define NCI_MTS_CMD 0x20
55 #define NCI_MTS_RSP 0x40
56 #define NCI_MTS_NTF 0x60
57 #define NCI_MTS_CFG 0x80
58 
59 #define NCI_NTF_BIT 0x80 /* the tNFC_VS_EVT is a notification */
60 #define NCI_RSP_BIT 0x40 /* the tNFC_VS_EVT is a response */
61 
62 /* for internal use only; not from specification */
63 /* the following 2 flags are used in layer_specific for fragmentation/reassembly of data packets */
64 #define NCI_LS_DATA 0x00
65 #define NCI_LS_DATA_PBF 0x01
66 
67 /* PBF: Packet Boundary Flag (byte 0) */
68 #define NCI_PBF_MASK 0x10
69 #define NCI_PBF_SHIFT 4
70 #define NCI_PBF_NO_OR_LAST 0x00 /* not fragmented or last fragment */
71 #define NCI_PBF_ST_CONT 0x10 /* start or continuing fragment */
72 
73 /* GID: Group Identifier (byte 0) */
74 #define NCI_GID_MASK 0x0F
75 #define NCI_GID_SHIFT 0
76 #define NCI_GID_CORE 0x00 /* 0000b NCI Core group */
77 #define NCI_GID_RF_MANAGE 0x01 /* 0001b RF Management group */
78 #define NCI_GID_EE_MANAGE 0x02 /* 0010b NFCEE Management group */
79 #define NCI_GID_PROP 0x0F /* 1111b Proprietary */
80 /* 0111b - 1110b RFU */
81 
82 /* OID: Opcode Identifier (byte 1) */
83 #define NCI_OID_MASK 0x3F
84 #define NCI_OID_SHIFT 0
85 
86 /* For routing */
87 #define NCI_DH_ID 0 /* for DH */
88 /* To identify the loopback test */
89 #define NCI_TEST_ID 0xFE /* for loopback test */
90 
91 /* Destination Type */
92 #define NCI_DEST_TYPE_NFCC 1 /* NFCC - loopback */
93 #define NCI_DEST_TYPE_REMOTE 2 /* Remote NFC Endpoint */
94 #define NCI_DEST_TYPE_NFCEE 3 /* NFCEE */
95 
96 /* builds byte0 of NCI Command and Notification packet */
97 #define NCI_MSG_BLD_HDR0(p, mt, gid) *(p)++ = (uint8_t)(((mt) << NCI_MT_SHIFT) | (gid));
98 
99 #define NCI_MSG_PBLD_HDR0(p, mt, pbf, gid) \
100  *(p)++ = (uint8_t)(((mt) << NCI_MT_SHIFT) | ((pbf) << NCI_PBF_SHIFT) | (gid));
101 
102 /* builds byte1 of NCI Command and Notification packet */
103 #define NCI_MSG_BLD_HDR1(p, oid) *(p)++ = (uint8_t)(((oid) << NCI_OID_SHIFT));
104 
105 /* parse byte0 of NCI packet */
106 #define NCI_MSG_PRS_HDR0(p, mt, pbf, gid) \
107  mt = (*(p) &NCI_MT_MASK) >> NCI_MT_SHIFT; \
108  pbf = (*(p) &NCI_PBF_MASK) >> NCI_PBF_SHIFT; \
109  gid = *(p)++ & NCI_GID_MASK;
110 
111 /* parse MT and PBF bits of NCI packet */
112 #define NCI_MSG_PRS_MT_PBF(p, mt, pbf) \
113  mt = (*(p) &NCI_MT_MASK) >> NCI_MT_SHIFT; \
114  pbf = (*(p) &NCI_PBF_MASK) >> NCI_PBF_SHIFT;
115 
116 /* parse byte1 of NCI Cmd/Ntf */
117 #define NCI_MSG_PRS_HDR1(p, oid) \
118  oid = (*(p) &NCI_OID_MASK); \
119  (p)++;
120 
121 /* NCI Data Format:
122  * byte 0: MT(0) PBF CID
123  * byte 1: RFU
124  * byte 2: Data Length */
125 /* CID: Connection Identifier (byte 0) 1-0xF Dynamically assigned (by NFCC), 0 is predefined */
126 #define NCI_CID_MASK 0x0F
127 
128 /* builds 3-byte message header of NCI Data packet */
129 #define NCI_DATA_BLD_HDR(p, cid, len) \
130  *(p)++ = (uint8_t)(cid); \
131  *(p)++ = 0; \
132  *(p)++ = (uint8_t)(len);
133 
134 #define NCI_DATA_PBLD_HDR(p, pbf, cid, len) \
135  *(p)++ = (uint8_t)(((pbf) << NCI_PBF_SHIFT) | (cid)); \
136  *(p)++ = 0; \
137  *(p)++ = (len);
138 
139 #define NCI_DATA_PRS_HDR(p, pbf, cid, len) \
140  (pbf) = (*(p) &NCI_PBF_MASK) >> NCI_PBF_SHIFT; \
141  (cid) = (*(p) &NCI_CID_MASK); \
142  p++; \
143  p++; \
144  (len) = *(p)++;
145 
146 /* Logical target ID 0x01-0xFE */
147 
148 #define BE_STREAM_TO_uint32_t(u32, p) \
149  { \
150  u32 = ((uint32_t)(*((p) + 3)) + ((uint32_t)(*((p) + 2)) << 8) + \
151  ((uint32_t)(*((p) + 1)) << 16) + ((uint32_t)(*(p)) << 24)); \
152  (p) += 4; \
153  }
154 
155 typedef struct
156 {
157  uint16_t event;
158  uint16_t len;
159  uint16_t offset;
160  uint16_t layer_specific;
161 } BT_HDR;
162 
163 /* Define the status code returned from the Validate, Parse or Build functions
164 */
165 enum
166 {
167  NDEF_OK, /* 0 - OK */
168 
169  NDEF_REC_NOT_FOUND, /* 1 - No record matching the find criteria */
170  NDEF_MSG_TOO_SHORT, /* 2 - Message was too short (< 3 bytes) */
171  NDEF_MSG_NO_MSG_BEGIN, /* 3 - No 'begin' flag at start of message */
172  NDEF_MSG_NO_MSG_END, /* 4 - No 'end' flag at end of message */
173  NDEF_MSG_EXTRA_MSG_BEGIN, /* 5 - 'begin' flag after start of message */
174  NDEF_MSG_UNEXPECTED_CHUNK, /* 6 - Unexpected chunk found */
175  NDEF_MSG_INVALID_EMPTY_REC, /* 7 - Empty record with non-zero contents */
176  NDEF_MSG_INVALID_CHUNK, /* 8 - Invalid chunk found */
177  NDEF_MSG_LENGTH_MISMATCH, /* 9 - Overall message length doesn't match */
178  NDEF_MSG_INSUFFICIENT_MEM /* 10 - Insuffiecient memory to add record */
179 };
180 typedef uint8_t tNDEF_STATUS;
181 
182 /* Definitions for tNFA_TNF (NDEF type name format ID) */
183 #define NFA_TNF_EMPTY NDEF_TNF_EMPTY /* Empty or no type specified */
184 #define NFA_TNF_WKT NDEF_TNF_WKT /* NFC Forum well-known type [NFC RTD] */
185 #define NFA_TNF_RFC2046_MEDIA NDEF_TNF_MEDIA /* Media-type as defined in RFC 2046 [RFC 2046] */
186 #define NFA_TNF_RFC3986_URI NDEF_TNF_URI /* Absolute URI as defined in RFC 3986 [RFC 3986] */
187 #define NFA_TNF_EXTERNAL NDEF_TNF_EXT /* NFC Forum external type [NFC RTD] */
188 #define NFA_TNF_UNKNOWN case NDEF_TNF_UNKNOWN /* Unknown */
189 #define NFA_TNF_UNCHANGED NDEF_TNF_UNCHANGED /* Unchanged */
190 #define NFA_TNF_RESERVED NDEF_TNF_RESERVED /* Reserved */
191 #define NFA_TNF_DEFAULT case 0xFF /* Used to register default NDEF type handler */
192 typedef uint8_t tNFA_TNF;
193 
194 #define NDEF_MB_MASK 0x80 /* Message Begin */
195 #define NDEF_ME_MASK 0x40 /* Message End */
196 #define NDEF_CF_MASK 0x20 /* Chunk Flag */
197 #define NDEF_SR_MASK 0x10 /* Short Record */
198 #define NDEF_IL_MASK 0x08 /* ID Length */
199 #define NDEF_TNF_MASK 0x07 /* Type Name Format */
200 
201 /* NDEF Type Name Format */
202 #define NDEF_TNF_EMPTY 0 /* Empty (type/id/payload len =0) */
203 #define NDEF_TNF_WKT 1 /* NFC Forum well-known type/RTD */
204 #define NDEF_TNF_MEDIA 2 /* Media-type as defined in RFC 2046 */
205 #define NDEF_TNF_URI 3 /* Absolute URI as defined in RFC 3986 */
206 #define NDEF_TNF_EXT 4 /* NFC Forum external type/RTD */
207 #define NDEF_TNF_UNKNOWN 5 /* Unknown (type len =0) */
208 #define NDEF_TNF_UNCHANGED 6 /* Unchanged (type len =0) */
209 #define NDEF_TNF_RESERVED 7 /* Reserved */
210 
211 void createBufNdef_TypeURI(uint8_t* strIN, int sizLen, uint8_t* strOUT);
212 void createBufNdef_TypeText(uint8_t* strIN, int sizLen, uint8_t* strOUT);
213 void createBufNdef_TypeSmartPoster(uint8_t* strIN, uint8_t* strIN2, int sizLen, uint8_t* strOUT);
214 
215 void vshort_actidata(uint8_t* strIN, int sizLen, uint8_t* strOUT);
216 void vshort_sendata(uint8_t* strIN, int sizLen, uint8_t* strOUT);
217 
218 int codeNFC(NfcPayload* nfcData, uint8_t* msg);
219 
220 /*******************************************************************************
221 **
222 ** Function NDEF_RecGetType
223 **
224 ** Description This function gets a pointer to the record type for the given NDEF record.
225 **
226 ** Returns Pointer to Type (NULL if none). TNF and len are filled in.
227 **
228 *******************************************************************************/
229 uint8_t* NDEF_RecGetType(uint8_t* p_rec, uint8_t* p_tnf, uint8_t* p_type_len);
230 
231 /*******************************************************************************
232 **
233 ** Function NDEF_RecGetId
234 **
235 ** Description This function gets a pointer to the record id for the given NDEF record.
236 **
237 ** Returns Pointer to Id (NULL if none). ID Len is filled in.
238 **
239 *******************************************************************************/
240 uint8_t* NDEF_RecGetId(uint8_t* p_rec, uint8_t* p_id_len);
241 
242 /*******************************************************************************
243 **
244 ** Function NDEF_RecGetPayload
245 **
246 ** Description This function gets a pointer to the payload for the given NDEF record.
247 **
248 ** Returns a pointer to the payload (or NULL none). Payload len filled in.
249 **
250 *******************************************************************************/
251 uint8_t* NDEF_RecGetPayload(uint8_t* p_rec, uint32_t* p_payload_len);
252 
253 /*******************************************************************************
254 **
255 ** Function NDEF_MsgValidate
256 **
257 ** Description This function validates an NDEF message.
258 **
259 ** Returns TRUE if all OK, or FALSE if the message is invalid.
260 **
261 *******************************************************************************/
262 tNDEF_STATUS NDEF_MsgValidate(uint8_t* p_msg, uint32_t msg_len, int b_allow_chunks);
Utilities to get config values from the environment.
Utilities for consuming RabbitMQ messages.
tNDEF_STATUS NDEF_MsgValidate(uint8_t *p_msg, uint32_t msg_len, int b_allow_chunks)
Definition: mockVMLibNci.c:333
uint8_t * NDEF_RecGetId(uint8_t *p_rec, uint8_t *p_id_len)
void createBufNdef_TypeURI(uint8_t *strIN, int sizLen, uint8_t *strOUT)
uint8_t tNFA_TNF
Definition: mockVMLibNci.h:192
uint8_t tNDEF_STATUS
Nfc player.
void vshort_actidata(uint8_t *strIN, int sizLen, uint8_t *strOUT)
int codeNFC(NfcPayload *nfcData, uint8_t *msg)
Definition: mockVMLibNci.c:145
Defines ports and structures for sensor threads.
uint8_t tNDEF_STATUS
Definition: mockVMLibNci.h:180
void createBufNdef_TypeSmartPoster(uint8_t *strIN, uint8_t *strIN2, int sizLen, uint8_t *strOUT)
Definition: mockVMLibNci.c:70
uint8_t * NDEF_RecGetPayload(uint8_t *p_rec, uint32_t *p_payload_len)
uint8_t * NDEF_RecGetType(uint8_t *p_rec, uint8_t *p_tnf, uint8_t *p_type_len)
Logging macros.
void createBufNdef_TypeText(uint8_t *strIN, int sizLen, uint8_t *strOUT)
Define common buffer sizes.
Define socket utilities to simplify networking.
void vshort_sendata(uint8_t *strIN, int sizLen, uint8_t *strOUT)