void handleTestCommand(ts_channel_s *tsChannel) { /** * this is NOT a standard TunerStudio command, this is my own * extension of the protocol to simplify troubleshooting */ tunerStudioDebug("got T (Test)"); tunerStudioWriteData(tsChannel, (const uint8_t *) VCS_VERSION, sizeof(VCS_VERSION)); /** * Please note that this response is a magic constant used by dev console for protocol detection * @see EngineState#TS_PROTOCOL_TAG */ tunerStudioWriteData(tsChannel, (const uint8_t *) " ts_p_alive\r\n", 8); }
static bool handlePlainCommand(uint8_t command) { if (command == TS_HELLO_COMMAND) { scheduleMsg(&logger, "Got naked Query command"); handleQueryCommand(TS_PLAIN); return true; } else if (command == 't' || command == 'T') { handleTestCommand(); return true; } else if (command == TS_PAGE_COMMAND) { int recieved = chSequentialStreamRead(getTsSerialDevice(), (uint8_t *)&pageIn, sizeof(pageIn)); // todo: validate 'recieved' value handlePageSelectCommand(TS_PLAIN, pageIn); return true; } else if (command == TS_READ_COMMAND) { //scheduleMsg(&logger, "Got naked READ PAGE???"); int recieved = chSequentialStreamRead(getTsSerialDevice(), (uint8_t *)&readRequest, sizeof(readRequest)); if (recieved != sizeof(readRequest)) { // todo: handler error return true; } handlePageReadCommand(TS_PLAIN, readRequest.page, readRequest.offset, readRequest.count); return true; } else if (command == TS_OUTPUT_COMMAND) { //scheduleMsg(&logger, "Got naked Channels???"); handleOutputChannelsCommand(TS_PLAIN); return true; } else if (command == 'F') { tunerStudioDebug("not ignoring F"); tunerStudioWriteData((const uint8_t *) PROTOCOL, strlen(PROTOCOL)); return true; } else { return false; } }
void tsSendResponse(ts_channel_s *tsChannel, ts_response_format_e mode, const uint8_t * buffer, int size) { if (mode == TS_CRC) { tunerStudioWriteCrcPacket(tsChannel, TS_RESPONSE_OK, buffer, size); } else { if (size > 0) tunerStudioWriteData(tsChannel, buffer, size); } }
void handleTestCommand(void) { /** * this is NOT a standard TunerStudio command, this is my own * extension of the protocol to simplify troubleshooting */ tunerStudioDebug("got T (Test)\r\n"); tunerStudioWriteData((const uint8_t *) "alive\r\n", 7); }
/** * Adds size to the beginning of a packet and a crc32 at the end. Then send the packet. */ void tunerStudioWriteCrcPacket(ts_channel_s *tsChannel, const uint8_t responseCode, const void *buf, const uint16_t size) { uint8_t *writeBuffer = tsChannel->writeBuffer; *(uint16_t *) writeBuffer = SWAP_UINT16(size + 1); // packet size including command *(uint8_t *) (writeBuffer + 2) = responseCode; tunerStudioWriteData(tsChannel, writeBuffer, 3); // header // CRC on whole packet uint32_t crc = crc32((void *) (writeBuffer + 2), 1); // command part of CRC crc = crc32inc((void *) buf, crc, (uint32_t) (size)); // combined with packet CRC *(uint32_t *) (writeBuffer) = SWAP_UINT32(crc); if (size > 0) { tunerStudioWriteData(tsChannel, (const uint8_t*)buf, size); // body } tunerStudioWriteData(tsChannel, writeBuffer, 4); // CRC footer }
/** * Adds size to the beginning of a packet and a crc32 at the end. Then send the packet. */ void tunerStudioWriteCrcPacket(const uint8_t command, const void *buf, const uint16_t size) { // todo: max size validation *(uint16_t *) crcIoBuffer = SWAP_UINT16(size + 1); // packet size including command *(uint8_t *) (crcIoBuffer + 2) = command; if (size != 0) memcpy(crcIoBuffer + 3, buf, size); // CRC on whole packet uint32_t crc = crc32((void *) (crcIoBuffer + 2), (uint32_t) (size + 1)); *(uint32_t *) (crcIoBuffer + 2 + 1 + size) = SWAP_UINT32(crc); // scheduleMsg(&logger, "TunerStudio: CRC command %x size %d", command, size); tunerStudioWriteData(crcIoBuffer, size + 2 + 1 + 4); // with size, command and CRC }
/** * @return true if legacy command was processed, false otherwise */ bool handlePlainCommand(ts_channel_s *tsChannel, uint8_t command) { if (command == TS_HELLO_COMMAND || command == TS_HELLO_COMMAND_DEPRECATED) { scheduleMsg(&tsLogger, "Got naked Query command"); handleQueryCommand(tsChannel, TS_PLAIN); return true; } else if (command == 't' || command == 'T') { handleTestCommand(tsChannel); return true; } else if (command == TS_PAGE_COMMAND) { int recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&pageIn, sizeof(pageIn), TS_READ_TIMEOUT); if (recieved != sizeof(pageIn)) { tunerStudioError("ERROR: not enough for PAGE"); return true; } handlePageSelectCommand(tsChannel, TS_PLAIN, pageIn); return true; } else if (command == TS_BURN_COMMAND) { scheduleMsg(&tsLogger, "Got naked BURN"); uint16_t page; int recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&page, sizeof(page), TS_READ_TIMEOUT); if (recieved != sizeof(page)) { tunerStudioError("ERROR: Not enough for plain burn"); return true; } handleBurnCommand(tsChannel, TS_PLAIN, page); return true; } else if (command == TS_CHUNK_WRITE_COMMAND) { scheduleMsg(&tsLogger, "Got naked CHUNK_WRITE"); int recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&writeChunkRequest, sizeof(writeChunkRequest), TS_READ_TIMEOUT); if (recieved != sizeof(writeChunkRequest)) { scheduleMsg(&tsLogger, "ERROR: Not enough for plain chunk write header: %d", recieved); tsState.errorCounter++; return true; } recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&tsChannel->crcReadBuffer, writeChunkRequest.count, TS_READ_TIMEOUT); if (recieved != writeChunkRequest.count) { scheduleMsg(&tsLogger, "ERROR: Not enough for plain chunk write content: %d while expecting %d", recieved, writeChunkRequest.count); tsState.errorCounter++; return true; } currentPageId = writeChunkRequest.page; handleWriteChunkCommand(tsChannel, TS_PLAIN, writeChunkRequest.offset, writeChunkRequest.count, (uint8_t *) &tsChannel->crcReadBuffer); return true; } else if (command == TS_READ_COMMAND) { //scheduleMsg(logger, "Got naked READ PAGE???"); int recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&readRequest, sizeof(readRequest), TS_READ_TIMEOUT); if (recieved != sizeof(readRequest)) { tunerStudioError("Not enough for plain read header"); return true; } handlePageReadCommand(tsChannel, TS_PLAIN, readRequest.page, readRequest.offset, readRequest.count); return true; } else if (command == TS_OUTPUT_COMMAND) { //scheduleMsg(logger, "Got naked Channels???"); handleOutputChannelsCommand(tsChannel, TS_PLAIN); return true; } else if (command == TS_LEGACY_HELLO_COMMAND) { tunerStudioDebug("ignoring LEGACY_HELLO_COMMAND"); return true; } else if (command == TS_COMMAND_F) { tunerStudioDebug("not ignoring F"); tunerStudioWriteData(tsChannel, (const uint8_t *) PROTOCOL, strlen(PROTOCOL)); return true; } else { return false; } }
void runBinaryProtocolLoop(ts_channel_s *tsChannel, bool_t isConsoleRedirect) { int wasReady = false; while (true) { int isReady = ts_serial_ready(isConsoleRedirect); if (!isReady) { chThdSleepMilliseconds(10); wasReady = false; continue; } if (!wasReady) { wasReady = true; // scheduleSimpleMsg(&logger, "ts channel is now ready ", hTimeNow()); } tsState.tsCounter++; int recieved = chnReadTimeout(tsChannel->channel, &firstByte, 1, TS_READ_TIMEOUT); if (recieved != 1) { // tunerStudioError("ERROR: no command"); continue; } // scheduleMsg(logger, "Got first=%x=[%c]", firstByte, firstByte); if (handlePlainCommand(tsChannel, firstByte)) continue; recieved = chnReadTimeout(tsChannel->channel, &secondByte, 1, TS_READ_TIMEOUT); if (recieved != 1) { tunerStudioError("ERROR: no second"); continue; } // scheduleMsg(logger, "Got secondByte=%x=[%c]", secondByte, secondByte); uint32_t incomingPacketSize = firstByte * 256 + secondByte; if (incomingPacketSize == BINARY_SWITCH_TAG) { // we are here if we get a binary switch request while already in binary mode. We will just ignore it. tunerStudioWriteData(tsChannel, (const uint8_t *) &BINARY_RESPONSE, 2); continue; } if (incomingPacketSize == 0 || incomingPacketSize > (sizeof(tsChannel->crcReadBuffer) - CRC_WRAPPING_SIZE)) { scheduleMsg(&tsLogger, "TunerStudio: invalid size: %d", incomingPacketSize); tunerStudioError("ERROR: CRC header size"); sendErrorCode(tsChannel); continue; } recieved = chnReadTimeout(tsChannel->channel, (uint8_t* )tsChannel->crcReadBuffer, 1, TS_READ_TIMEOUT); if (recieved != 1) { tunerStudioError("ERROR: did not receive command"); continue; } char command = tsChannel->crcReadBuffer[0]; if (!isKnownCommand(command)) { scheduleMsg(&tsLogger, "unexpected command %x", command); sendErrorCode(tsChannel); continue; } // scheduleMsg(logger, "TunerStudio: reading %d+4 bytes(s)", incomingPacketSize); recieved = chnReadTimeout(tsChannel->channel, (uint8_t * ) (tsChannel->crcReadBuffer + 1), incomingPacketSize + CRC_VALUE_SIZE - 1, TS_READ_TIMEOUT); int expectedSize = incomingPacketSize + CRC_VALUE_SIZE - 1; if (recieved != expectedSize) { scheduleMsg(&tsLogger, "got ONLY %d for packet size %d/%d for command %c", recieved, incomingPacketSize, expectedSize, command); tunerStudioError("ERROR: not enough"); continue; } uint32_t expectedCrc = *(uint32_t*) (tsChannel->crcReadBuffer + incomingPacketSize); expectedCrc = SWAP_UINT32(expectedCrc); uint32_t actualCrc = crc32(tsChannel->crcReadBuffer, incomingPacketSize); if (actualCrc != expectedCrc) { scheduleMsg(&tsLogger, "TunerStudio: CRC %x %x %x %x", tsChannel->crcReadBuffer[incomingPacketSize + 0], tsChannel->crcReadBuffer[incomingPacketSize + 1], tsChannel->crcReadBuffer[incomingPacketSize + 2], tsChannel->crcReadBuffer[incomingPacketSize + 3]); scheduleMsg(&tsLogger, "TunerStudio: command %c actual CRC %x/expected %x", tsChannel->crcReadBuffer[0], actualCrc, expectedCrc); tunerStudioError("ERROR: CRC issue"); continue; } // scheduleMsg(logger, "TunerStudio: P00-07 %x %x %x %x %x %x %x %x", crcIoBuffer[0], crcIoBuffer[1], // crcIoBuffer[2], crcIoBuffer[3], crcIoBuffer[4], crcIoBuffer[5], crcIoBuffer[6], crcIoBuffer[7]); int success = tunerStudioHandleCrcCommand(tsChannel, tsChannel->crcReadBuffer, incomingPacketSize); if (!success) print("got unexpected TunerStudio command %x:%c\r\n", command, command); } }
/** * @brief 'Output' command sends out a snapshot of current values */ void handleOutputChannelsCommand(void) { tsState.outputChannelsCommandCounter++; // this method is invoked too often to print any debug information tunerStudioWriteData((const uint8_t *) &tsOutputChannels, sizeof(TunerStudioOutputChannels)); }
void handleQueryCommand(void) { tsState.queryCommandCounter++; tunerStudioDebug("got H (queryCommand)"); tunerStudioWriteData((const uint8_t *) TS_SIGNATURE, strlen(TS_SIGNATURE) + 1); }