Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | * Implement a simple T=1 echo endpoint. |
| 17 | */ |
| 18 | |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 23 | #include "../libese-teq1/include/ese/teq1.h" |
| 24 | #include "../libese/include/ese/ese.h" |
| 25 | #include "../libese/include/ese/log.h" |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 26 | |
| 27 | struct EchoState { |
| 28 | struct Teq1Frame frame; |
| 29 | uint8_t *rx_fill; |
| 30 | uint8_t *tx_sent; |
| 31 | int recvd; |
| 32 | }; |
| 33 | |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 34 | #define ECHO_STATE(ese) (*(struct EchoState **)(&ese->pad[1])) |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 35 | |
| 36 | static int echo_open(struct EseInterface *ese, void *hw_opts) { |
| 37 | struct EchoState *es = hw_opts; /* shorter than __attribute */ |
| 38 | struct EchoState **es_ptr; |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 39 | if (sizeof(ese->pad) < sizeof(struct EchoState *)) { |
| 40 | /* This is a compile-time correctable error only. */ |
| 41 | ALOGE("Pad size too small to use Echo HW (%zu < %zu)", sizeof(ese->pad), |
| 42 | sizeof(struct EchoState *)); |
| 43 | return -1; |
| 44 | } |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 45 | es_ptr = &ECHO_STATE(ese); |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 46 | *es_ptr = malloc(sizeof(struct EchoState)); |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 47 | if (!*es_ptr) { |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 48 | return -1; |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 49 | } |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 50 | es = ECHO_STATE(ese); |
| 51 | es->rx_fill = &es->frame.header.NAD; |
| 52 | es->tx_sent = es->rx_fill; |
| 53 | es->recvd = 0; |
| 54 | return 0; |
| 55 | } |
| 56 | |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 57 | static void echo_close(struct EseInterface *ese) { |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 58 | struct EchoState *es; |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 59 | es = ECHO_STATE(ese); |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 60 | if (!es) { |
| 61 | return; |
| 62 | } |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 63 | free(es); |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 64 | es = NULL; |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 65 | } |
| 66 | |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 67 | static uint32_t echo_receive(struct EseInterface *ese, uint8_t *buf, |
| 68 | uint32_t len, int complete) { |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 69 | struct EchoState *es = ECHO_STATE(ese); |
| 70 | ALOGV("interface attempting to read data"); |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 71 | if (!es->recvd) { |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 72 | return 0; |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 73 | } |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 74 | |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 75 | if (len > sizeof(es->frame) - (es->tx_sent - &es->frame.header.NAD)) { |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 76 | return 0; |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 77 | } |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 78 | |
| 79 | /* NAD was polled for so skip it. */ |
| 80 | memcpy(buf, es->tx_sent, len); |
| 81 | es->tx_sent += len; |
| 82 | if (complete) { |
| 83 | es->tx_sent = &es->frame.header.NAD; |
| 84 | es->recvd = 0; |
| 85 | ALOGV("card sent a frame"); |
| 86 | } |
| 87 | return sizeof(es->frame.header) + es->frame.header.LEN; |
| 88 | } |
| 89 | |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 90 | static uint32_t echo_transmit(struct EseInterface *ese, const uint8_t *buf, |
| 91 | uint32_t len, int complete) { |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 92 | struct EchoState *es = ECHO_STATE(ese); |
| 93 | ALOGV("interface transmitting data"); |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 94 | if (len > sizeof(es->frame) - (es->rx_fill - &es->frame.header.NAD)) { |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 95 | return 0; |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 96 | } |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 97 | memcpy(es->rx_fill, buf, len); |
| 98 | es->rx_fill += len; |
| 99 | es->recvd = complete; |
| 100 | if (complete) { |
| 101 | es->frame.header.NAD = 0x00; |
| 102 | if (teq1_compute_LRC(&es->frame) != es->frame.INF[es->frame.header.LEN]) { |
| 103 | ALOGV("card received frame with bad LRC"); |
| 104 | return 0; |
| 105 | } |
| 106 | ALOGV("card received valid frame"); |
| 107 | es->rx_fill = &es->frame.header.NAD; |
| 108 | } |
| 109 | return len; |
| 110 | } |
| 111 | |
| 112 | static int echo_poll(struct EseInterface *ese, uint8_t poll_for, float timeout, |
| 113 | int complete) { |
| 114 | struct EchoState *es = ECHO_STATE(ese); |
| 115 | const struct Teq1ProtocolOptions *opts = ese->ops->opts; |
| 116 | ALOGV("interface polling for start of frame/host node address: %x", poll_for); |
| 117 | /* In reality, we should be polling at intervals up to the timeout. */ |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 118 | if (timeout > 0.0) { |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 119 | usleep(timeout * 1000); |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 120 | } |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 121 | if (poll_for == opts->host_address) { |
| 122 | ALOGV("interface received NAD"); |
| 123 | if (!complete) { |
| 124 | es->tx_sent++; /* Consume the polled byte: NAD */ |
| 125 | } |
| 126 | return 1; |
| 127 | } |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | int echo_preprocess(const struct Teq1ProtocolOptions *const opts, |
| 132 | struct Teq1Frame *frame, int tx) { |
| 133 | if (tx) { |
| 134 | /* Recompute the LRC with the NAD of 0x00 */ |
| 135 | frame->header.NAD = 0x00; |
| 136 | frame->INF[frame->header.LEN] = teq1_compute_LRC(frame); |
| 137 | frame->header.NAD = opts->node_address; |
| 138 | ALOGV("interface is preprocessing outbound frame"); |
| 139 | } else { |
| 140 | /* Replace the NAD with 0x00 so the LRC check passes. */ |
| 141 | frame->header.NAD = 0x00; |
| 142 | ALOGV("interface is preprocessing inbound frame"); |
| 143 | } |
| 144 | return 0; |
| 145 | } |
| 146 | |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 147 | static const struct Teq1ProtocolOptions kTeq1Options = { |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 148 | .host_address = 0xAA, |
| 149 | .node_address = 0xBB, |
| 150 | .bwt = 3.14152f, |
| 151 | .etu = 1.0f, |
| 152 | .preprocess = &echo_preprocess, |
| 153 | }; |
| 154 | |
Will Drewry | 8f367fc | 2017-03-30 22:07:48 -0500 | [diff] [blame] | 155 | uint32_t echo_transceive(struct EseInterface *ese, |
| 156 | const struct EseSgBuffer *tx_buf, uint32_t tx_len, |
| 157 | struct EseSgBuffer *rx_buf, uint32_t rx_len) { |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 158 | return teq1_transceive(ese, &kTeq1Options, tx_buf, tx_len, rx_buf, rx_len); |
| 159 | } |
Will Drewry | d4ae528 | 2017-01-03 22:06:26 -0600 | [diff] [blame] | 160 | |
| 161 | static const char *kErrorMessages[] = { |
| 162 | "T=1 hard failure.", /* TEQ1_ERROR_HARD_FAIL */ |
| 163 | "T=1 abort.", /* TEQ1_ERROR_ABORT */ |
| 164 | "T=1 device reset failed.", /* TEQ1_ERROR_DEVICE_ABORT */ |
| 165 | }; |
Will Drewry | de2cad7 | 2017-03-10 15:53:34 -0600 | [diff] [blame] | 166 | |
| 167 | static const struct EseOperations ops = { |
| 168 | .name = "eSE Echo Hardware (fake)", |
| 169 | .open = &echo_open, |
| 170 | .hw_receive = &echo_receive, |
| 171 | .hw_transmit = &echo_transmit, |
| 172 | .transceive = &echo_transceive, |
| 173 | .poll = &echo_poll, |
| 174 | .close = &echo_close, |
| 175 | .opts = &kTeq1Options, |
| 176 | .errors = kErrorMessages, |
| 177 | .errors_count = sizeof(kErrorMessages), |
| 178 | }; |
| 179 | ESE_DEFINE_HW_OPS(ESE_HW_ECHO, ops); |