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
20extern "C" {
21#endif
22
23/*
24 * Enable T=1 format to reduce to case integers.
25 * Ensure there are tests to map TEQ1_X() to the shorthand below.
26 */
27#define I(S, M) I_##S##_##M
28#define I_0_0 0
29#define I_0_1 32
30#define I_1_0 64
31#define I_1_1 96
32#define R(S, O, P) R_ ## S ##_## O ##_## P
33#define R_0_0_0 128
34#define R_0_0_1 129
35#define R_0_1_0 130
36#define R_0_1_1 131
37#define R_1_0_0 144
38#define R_1_0_1 145
39#define R_1_1_0 146
40#define R_1_1_1 147
41
42#define _S(x) x
43#define S(N, R) S_##N##_##R
44#define S_RESYNC_REQUEST 192
45#define S_IFS_REQUEST 193
46#define S_ABORT_REQUEST 194
47#define S_WTX_REQUEST 195
48#define S_RESYNC_RESPONSE 224
49#define S_IFS_RESPONSE 225
50#define S_ABORT_RESPONSE 226
51#define S_WTX_RESPONSE 227
52
53#define TEQ1_RULE(TX, RX) (((TX & 255) << 8)|(RX & 255))
54
55struct Teq1State {
56  uint8_t wait_mult;
57  uint8_t ifs;
58  uint8_t errors;
59  int retransmits;
60  const char *last_error_message;
61  struct Teq1CardState *card_state;
62  struct {
63    const struct EseSgBuffer *tx;
64    struct EseSgBuffer *rx;
65    uint32_t tx_offset;
66    uint32_t tx_count;
67    uint32_t tx_total;
68    uint32_t rx_offset;
69    uint32_t rx_count;
70    uint32_t rx_total;
71  } app_data;
72};
73
74#define TEQ1_INIT_STATE(TX_BUFS, TX_LEN, TX_TOTAL_LEN, RX_BUFS, RX_LEN, RX_TOTAL_LEN, CSTATE) \
75  { \
76    .wait_mult = 1, \
77    .ifs = IFSC, \
78    .errors = 0, \
79    .retransmits = 0, \
80    .last_error_message = NULL, \
81    .card_state = (CSTATE), \
82    .app_data = { \
83      .tx = (TX_BUFS), \
84      .rx = (RX_BUFS), \
85      .tx_offset = 0, \
86      .tx_count = (TX_LEN), \
87      .tx_total = (TX_TOTAL_LEN), \
88      .rx_offset = 0, \
89      .rx_count = (RX_LEN), \
90      .rx_total = (RX_TOTAL_LEN), \
91    }, \
92  }
93
94enum RuleResult {
95  kRuleResultComplete,
96  kRuleResultAbort,
97  kRuleResultContinue,
98  kRuleResultHardFail,
99  kRuleResultResetDevice,
100  kRuleResultResetSession,
101  kRuleResultRetransmit,
102  kRuleResultSingleShot,
103};
104
105
106const char *teq1_rule_result_to_name(enum RuleResult result);
107const char *teq1_pcb_to_name(uint8_t pcb);
108int teq1_transmit(struct EseInterface *ese,
109                  const struct Teq1ProtocolOptions *opts,
110                  struct Teq1Frame *frame);
111int teq1_receive(struct EseInterface *ese,
112                 const struct Teq1ProtocolOptions *opts,
113                 float timeout,
114                 struct Teq1Frame *frame);
115uint8_t teq1_fill_info_block(struct Teq1State *state, struct Teq1Frame *frame);
116void teq1_get_app_data(struct Teq1State *state, const struct Teq1Frame *frame);
117uint8_t teq1_frame_error_check(struct Teq1State *state,
118                               struct Teq1Frame *tx_frame,
119                               struct Teq1Frame *rx_frame);
120enum RuleResult teq1_rules(struct Teq1State *state,
121                           struct Teq1Frame *tx_frame,
122                           struct Teq1Frame *rx_frame,
123                           struct Teq1Frame *next_tx);
124
125#define teq1_dump_transmit(_B, _L) teq1_dump_buf("TX", (_B), (_L))
126#define teq1_dump_receive(_B, _L) teq1_dump_buf("RX", (_B), (_L))
127
128#ifdef __cplusplus
129}  /* extern "C" */
130#endif
131#endif  /* ESE_TEQ1_PRIVATE_H__ */
132