| 127 | } |
| 128 | |
| 129 | void flasherDataHandler(uint8_t* data, size_t len, uint8_t transportType) { |
| 130 | static struct flasherCommand* cmd = nullptr; |
| 131 | static uint8_t flasherSerialState = FLASHER_WAIT_A; |
| 132 | static uint32_t flasherCmdDataIndex = 0; |
| 133 | static uint16_t flasherCRC = 0xAB34; |
| 134 | static uint32_t flasherLastCmd = 0; |
| 135 | static uint8_t curLenIndex = 0; |
| 136 | |
| 137 | if ((flasherSerialState != FLASHER_WAIT_A) && (millis() - flasherLastCmd >= 3000)) { |
| 138 | flasherSerialState = FLASHER_RESET; |
| 139 | } |
| 140 | |
| 141 | while (len--) { |
| 142 | uint8_t usbbyte = *(data++); |
| 143 | switch (flasherSerialState) { |
| 144 | case FLASHER_RESET: |
| 145 | if (transportType == TRANSPORT_TCP && cmd != nullptr) { |
| 146 | if (cmd->data != nullptr) { |
| 147 | free(cmd->data); |
| 148 | cmd->data = nullptr; |
| 149 | } |
| 150 | delete cmd; |
| 151 | cmd = nullptr; |
| 152 | } |
| 153 | flasherSerialState = FLASHER_WAIT_A; |
| 154 | case FLASHER_WAIT_A: |
| 155 | if (usbbyte == 'A') { |
| 156 | flasherSerialState = FLASHER_WAIT_T; |
| 157 | flasherLastCmd = millis(); |
| 158 | } else { |
| 159 | // enterConsoleMode(); |
| 160 | } |
| 161 | break; |
| 162 | case FLASHER_WAIT_T: |
| 163 | if (usbbyte == 'T') { |
| 164 | flasherSerialState = FLASHER_WAIT_CMD; |
| 165 | cmd = new flasherCommand; |
| 166 | flasherCRC = 0xAB34; |
| 167 | flasherCmdDataIndex = 0; |
| 168 | } else { |
| 169 | flasherSerialState = FLASHER_RESET; |
| 170 | } |
| 171 | break; |
| 172 | case FLASHER_WAIT_CMD: |
| 173 | cmd->command = usbbyte; |
| 174 | flasherCRC += usbbyte; |
| 175 | flasherSerialState = FLASHER_WAIT_LEN; |
| 176 | curLenIndex = 0; |
| 177 | break; |
| 178 | case FLASHER_WAIT_LEN: |
| 179 | flasherCRC += usbbyte; |
| 180 | cmd->len |= ((uint32_t)usbbyte) << (24 - (8 * curLenIndex)); |
| 181 | curLenIndex++; |
| 182 | if (curLenIndex == sizeof(cmd->len)) { |
| 183 | if (cmd->len) { |
| 184 | // not 0 |
| 185 | cmd->data = (uint8_t*)calloc(cmd->len, 1); |
| 186 | if (cmd->data == nullptr) { |
no test coverage detected