1 // Copyright 2021 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 15 // Configuration macros for the transfer module. 16 #pragma once 17 18 #include <chrono> 19 #include <cinttypes> 20 #include <limits> 21 22 #include "pw_chrono/system_clock.h" 23 24 // The default maximum number of times a transfer should retry sending a chunk 25 // when no response is received. This can later be configured per-transfer. 26 #ifndef PW_TRANSFER_DEFAULT_MAX_RETRIES 27 #define PW_TRANSFER_DEFAULT_MAX_RETRIES 3 28 #endif // PW_TRANSFER_DEFAULT_MAX_RETRIES 29 30 static_assert(PW_TRANSFER_DEFAULT_MAX_RETRIES > 0 && 31 PW_TRANSFER_DEFAULT_MAX_RETRIES <= 32 std::numeric_limits<uint8_t>::max()); 33 34 // The default maximum number of times a transfer should retry sending a chunk 35 // over the course of its entire lifetime. 36 // This number should be high, particularly if long-running transfers are 37 // expected. Its purpose is to prevent transfers from getting stuck in an 38 // infinite loop. 39 #ifndef PW_TRANSFER_DEFAULT_MAX_LIFETIME_RETRIES 40 #define PW_TRANSFER_DEFAULT_MAX_LIFETIME_RETRIES \ 41 (static_cast<uint32_t>(PW_TRANSFER_DEFAULT_MAX_RETRIES) * 1000u) 42 #endif // PW_TRANSFER_DEFAULT_MAX_LIFETIME_RETRIES 43 44 static_assert(PW_TRANSFER_DEFAULT_MAX_LIFETIME_RETRIES > 45 PW_TRANSFER_DEFAULT_MAX_RETRIES && 46 PW_TRANSFER_DEFAULT_MAX_LIFETIME_RETRIES <= 47 std::numeric_limits<uint32_t>::max()); 48 49 // The default amount of time, in milliseconds, to wait for a chunk to arrive 50 // before retrying. This can later be configured per-transfer. 51 #ifndef PW_TRANSFER_DEFAULT_TIMEOUT_MS 52 #define PW_TRANSFER_DEFAULT_TIMEOUT_MS 2000 53 #endif // PW_TRANSFER_DEFAULT_TIMEOUT_MS 54 55 static_assert(PW_TRANSFER_DEFAULT_TIMEOUT_MS > 0); 56 57 // The fractional position within a window at which a receive transfer should 58 // extend its window size to minimize the amount of time the transmitter 59 // spends blocked. 60 // 61 // For example, a divisor of 2 will extend the window when half of the 62 // requested data has been received, a divisor of three will extend at a third 63 // of the window, and so on. 64 #ifndef PW_TRANSFER_DEFAULT_EXTEND_WINDOW_DIVISOR 65 #define PW_TRANSFER_DEFAULT_EXTEND_WINDOW_DIVISOR 2 66 #endif // PW_TRANSFER_DEFAULT_EXTEND_WINDOW_DIVISOR 67 68 static_assert(PW_TRANSFER_DEFAULT_EXTEND_WINDOW_DIVISOR > 1); 69 70 namespace pw::transfer::cfg { 71 72 inline constexpr uint8_t kDefaultMaxRetries = PW_TRANSFER_DEFAULT_MAX_RETRIES; 73 inline constexpr uint16_t kDefaultMaxLifetimeRetries = 74 PW_TRANSFER_DEFAULT_MAX_LIFETIME_RETRIES; 75 76 inline constexpr chrono::SystemClock::duration kDefaultChunkTimeout = 77 std::chrono::milliseconds(PW_TRANSFER_DEFAULT_TIMEOUT_MS); 78 79 inline constexpr uint32_t kDefaultExtendWindowDivisor = 80 PW_TRANSFER_DEFAULT_EXTEND_WINDOW_DIVISOR; 81 82 } // namespace pw::transfer::cfg 83