areusch commented on a change in pull request #6334: URL: https://github.com/apache/incubator-tvm/pull/6334#discussion_r485020723
########## File path: src/runtime/crt/utvm_rpc_common/framing.cc ########## @@ -0,0 +1,409 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * \file framing.cc + * \brief Framing for RPC. + */ + +#include <string.h> +#include <tvm/runtime/crt/logging.h> +#include <tvm/runtime/crt/rpc_common/framing.h> + +#include "crt_config.h" + +// For debugging purposes, Framer logs can be enabled, but this should only be done when +// running from the host. This is done differently from TVMLogf() because TVMLogf() uses the +// framer in its implementation. +#ifdef TVM_CRT_FRAMER_ENABLE_LOGS +#include <cstdio> +#define TVM_FRAMER_DEBUG_LOG(msg, ...) fprintf(stderr, "utvm framer: " msg " \n", ##__VA_ARGS__) +#define TVM_UNFRAMER_DEBUG_LOG(msg, ...) fprintf(stderr, "utvm unframer: " msg " \n", ##__VA_ARGS__) +#else +#define TVM_FRAMER_DEBUG_LOG(msg, ...) +#define TVM_UNFRAMER_DEBUG_LOG(msg, ...) +#endif + +namespace tvm { +namespace runtime { + +template <typename E> +static constexpr uint8_t to_integral(E e) { + return static_cast<uint8_t>(e); +} + +void Unframer::Reset() { + state_ = State::kFindPacketStart; + saw_escape_start_ = false; + num_buffer_bytes_valid_ = 0; +} + +tvm_crt_error_t Unframer::Write(const uint8_t* data, size_t data_size_bytes, + size_t* bytes_consumed) { + tvm_crt_error_t return_code = kTvmErrorNoError; + input_ = data; + input_size_bytes_ = data_size_bytes; + + while (return_code == kTvmErrorNoError && input_size_bytes_ > 0) { + TVM_UNFRAMER_DEBUG_LOG("state: %02x size 0x%02zx", to_integral(state_), input_size_bytes_); + switch (state_) { + case State::kFindPacketStart: + return_code = FindPacketStart(); + break; + case State::kFindPacketLength: + return_code = FindPacketLength(); + break; + case State::kFindPacketCrc: + return_code = FindPacketCrc(); + break; + case State::kFindCrcEnd: + return_code = FindCrcEnd(); + break; + default: + return_code = kTvmErrorFramingInvalidState; + break; + } + } + + *bytes_consumed = data_size_bytes - input_size_bytes_; + input_ = nullptr; + input_size_bytes_ = 0; + + if (return_code != kTvmErrorNoError) { + state_ = State::kFindPacketStart; + ClearBuffer(); + } + + return return_code; +} + +tvm_crt_error_t Unframer::FindPacketStart() { + size_t i; + for (i = 0; i < input_size_bytes_; i++) { Review comment: done ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org