1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef ESE_TEQ1_PRIVATE_H__
17 #define ESE_TEQ1_PRIVATE_H__ 1
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /* Set visibility for exported functions. */
24 #ifndef API
25 #define API __attribute__ ((visibility("default")))
26 #endif  /* API */
27 
28 /* Mimic C11 _Static_assert behavior for a C99 world. */
29 #ifndef _static_assert
30 #define _static_assert(what, why) { while (!(1 / (!!(what)))); }
31 #endif
32 
33 /*
34  * Enable T=1 format to reduce to case integers.
35  * Ensure there are tests to map TEQ1_X() to the shorthand below.
36  */
37 #define I(S, M) I_##S##_##M
38 #define I_0_0 0
39 #define I_0_1 32
40 #define I_1_0 64
41 #define I_1_1 96
42 #define R(S, O, P) R_ ## S ##_## O ##_## P
43 #define R_0_0_0 128
44 #define R_0_0_1 129
45 #define R_0_1_0 130
46 #define R_0_1_1 131
47 #define R_1_0_0 144
48 #define R_1_0_1 145
49 #define R_1_1_0 146
50 #define R_1_1_1 147
51 
52 #define _S(x) x
53 #define S(N, R) S_##N##_##R
54 #define S_RESYNC_REQUEST 192
55 #define S_IFS_REQUEST 193
56 #define S_ABORT_REQUEST 194
57 #define S_WTX_REQUEST 195
58 #define S_RESYNC_RESPONSE 224
59 #define S_IFS_RESPONSE 225
60 #define S_ABORT_RESPONSE 226
61 #define S_WTX_RESPONSE 227
62 
63 #define TEQ1_RULE(TX, RX) (((TX & 255) << 8)|(RX & 255))
64 
65 struct Teq1State {
66   uint8_t wait_mult;
67   uint8_t ifs;
68   uint8_t errors;
69   int retransmits;
70   const char *last_error_message;
71   struct Teq1CardState *card_state;
72   struct {
73     uint8_t *tx_buf;
74     uint32_t tx_len;
75     uint8_t *rx_buf;
76     uint32_t rx_len;
77   } app_data;
78 };
79 
80 #define TEQ1_INIT_STATE(TX_BUF, TX_LEN, RX_BUF, RX_LEN, CSTATE) \
81   { \
82     .wait_mult = 1, \
83     .ifs = IFSC, \
84     .errors = 0, \
85     .retransmits = 0, \
86     .last_error_message = NULL, \
87     .card_state = (CSTATE), \
88     .app_data = { \
89       .tx_buf = (uint8_t *const)(TX_BUF), \
90       .tx_len = (TX_LEN), \
91       .rx_buf = (RX_BUF), \
92       .rx_len = (RX_LEN), \
93     }, \
94   }
95 
96 enum RuleResult {
97   kRuleResultComplete,
98   kRuleResultAbort,
99   kRuleResultContinue,
100   kRuleResultHardFail,
101   kRuleResultResetDevice,
102   kRuleResultResetSession,
103   kRuleResultRetransmit,
104   kRuleResultSingleShot,
105 };
106 
107 
108 const char *teq1_rule_result_to_name(enum RuleResult result);
109 const char *teq1_pcb_to_name(uint8_t pcb);
110 int teq1_transmit(struct EseInterface *ese,
111                   const struct Teq1ProtocolOptions *opts,
112                   struct Teq1Frame *frame);
113 int teq1_receive(struct EseInterface *ese,
114                  const struct Teq1ProtocolOptions *opts,
115                  float timeout,
116                  struct Teq1Frame *frame);
117 uint8_t teq1_fill_info_block(struct Teq1State *state, struct Teq1Frame *frame);
118 void teq1_get_app_data(struct Teq1State *state, struct Teq1Frame *frame);
119 uint8_t teq1_frame_error_check(struct Teq1State *state,
120                                struct Teq1Frame *tx_frame,
121                                struct Teq1Frame *rx_frame);
122 enum RuleResult teq1_rules(struct Teq1State *state,
123                            struct Teq1Frame *tx_frame,
124                            struct Teq1Frame *rx_frame,
125                            struct Teq1Frame *next_tx);
126 
127 #define teq1_dump_transmit(_B, _L) teq1_dump_buf("TX", (_B), (_L))
128 #define teq1_dump_receive(_B, _L) teq1_dump_buf("RX", (_B), (_L))
129 
130 #ifdef __cplusplus
131 }  /* extern "C" */
132 #endif
133 #endif  /* ESE_TEQ1_PRIVATE_H__ */
134