(buffer: Buffer)
| 299 | } |
| 300 | |
| 301 | protected processData(buffer: Buffer): void { |
| 302 | let offset = 0; |
| 303 | // PeMicro streams data in packets. Each packet has a 32 byte header, followed by the data |
| 304 | // It only sends one packet per TCP packet, but this interface concatenates TCP packets |
| 305 | // So we may need to process multiple in one callback |
| 306 | while ((buffer.length - offset) >= PeMicroHeader.headerLength) { |
| 307 | try { |
| 308 | const header = PeMicroHeader.fromBuffer(buffer.subarray(offset, Math.min(offset + PeMicroHeader.headerLength, buffer.length))); |
| 309 | // skip over header |
| 310 | offset = offset + PeMicroHeader.headerLength; |
| 311 | switch (this.state) { |
| 312 | case PeState.CREATE_PIPE: { |
| 313 | if (header.type === PeHeaderType.RX_COMMAND) { |
| 314 | const response = JSON.parse(buffer.subarray(offset, Math.min(offset + header.dataLength, buffer.length)).toString()); |
| 315 | if (response.control['00000001'].result === 0) { |
| 316 | this.configureSWO(); |
| 317 | this.state = PeState.CONFIGURE_SWO; |
| 318 | } |
| 319 | } |
| 320 | break; |
| 321 | } |
| 322 | case PeState.CONFIGURE_SWO: { |
| 323 | if (header.type === PeHeaderType.RX_COMMAND) { |
| 324 | const response = JSON.parse(buffer.subarray(offset, Math.min(offset + header.dataLength, buffer.length)).toString()); |
| 325 | if (response.control['00000001'].result === 0) { |
| 326 | this.resumePipe(); |
| 327 | this.state = PeState.RESUME_PIPE; |
| 328 | } |
| 329 | } |
| 330 | break; |
| 331 | } |
| 332 | case PeState.RESUME_PIPE: { |
| 333 | if (header.type === PeHeaderType.RX_COMMAND) { |
| 334 | const response = JSON.parse(buffer.subarray(offset, Math.min(offset + header.dataLength, buffer.length)).toString()); |
| 335 | if (response.control['00000001'].result === 0) { |
| 336 | this.state = PeState.RECEIVING; |
| 337 | } |
| 338 | } |
| 339 | break; |
| 340 | } |
| 341 | case PeState.RECEIVING: { |
| 342 | if (header.type === PeHeaderType.RX_STREAM) { |
| 343 | this.emit('data', buffer.subarray(offset, Math.min(offset + header.dataLength, buffer.length))); |
| 344 | } |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | offset = offset + header.dataLength; |
| 349 | } catch (err) { |
| 350 | console.log(err.message); |
| 351 | // If we couldn't decode the header, just discard the data. |
| 352 | // Its probably garbage or out of sync, so upstream would be confused anyway |
| 353 | } |
| 354 | |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | public write(data) { |
nothing calls this directly
no test coverage detected