taler-xotp_fw

xOTP generator firmware
Log | Files | Refs | Submodules | README

xtotpUtil.h (2988B)


      1 /**
      2  * @file xtotpUtil.h
      3  * @author Adrian STEINER (steia19@bfh.ch)
      4  * @brief Includes some help functions and important global defines
      5  * @version 0.1
      6  * @date 27-02-2025
      7  *
      8  * @copyright (C) 2025 Adrian STEINER
      9  * This program is free software: you can redistribute it and/or modify
     10  * it under the terms of the GNU General Public License as published by
     11  * the Free Software Foundation, either version 3 of the License, or
     12  * (at your option) any later version.
     13  *
     14  * This program is distributed in the hope that it will be useful,
     15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17  * GNU General Public License for more details.
     18  *
     19  * You should have received a copy of the GNU General Public License
     20  * along with this program.  If not, see <https: //www.gnu.org/licenses/>.
     21  *
     22  */
     23 #ifndef XTOTP_UTIL_H
     24 #define XTOTP_UTIL_H
     25 
     26 #ifdef __cplusplus
     27 extern "C" {
     28 #endif
     29 
     30 #include <stdint.h>
     31 
     32 #define MAX_AMOUNT_DIGITS (8)       ///< Maximum 8 digits for digits
     33 #define MAX_INPUT_LENGTH (10000000) ///< Maximum number for the input (8 DIGITS)
     34 #define MAX_PASSCODE_LENGTH_DIGITS (8)         ///< Maximum passcode length
     35 #define MAX_PASSCODE_LENGTH_NUMBER (100000000) ///< Maximum passcode length
     36 
     37 #define PAYMENT_BASE (10) ///< Payment is based on 10
     38 
     39 #define APP_SEC_BASE (1000) ///< Second in the base (ms)
     40 #define MS_TO_SECOND(s)                                                        \
     41   ((s) / APP_SEC_BASE) ///< Get the second value of a millisecond
     42 #define SECOND_TO_MS(s) ((s) * APP_SEC_BASE) ///< Get the ms value of a second
     43 
     44 /**
     45  * @brief Get the minium of a and b
     46  *
     47  */
     48 #define XTOTP_MIN(A, B) ((A) < (B) ? (A) : (B))
     49 
     50 /**
     51  * @brief Get the maximum of a and b
     52  *
     53  */
     54 #define XTOTP_MAX(A, B) ((A) > (B) ? (A) : (B))
     55 
     56 /**
     57  * @brief Check if the data is in range
     58  *
     59  */
     60 #define XTOTP_IS_IN_RANGE(X, MIN, MAX)                                         \
     61   ((((X) >= (MIN)) && ((X) <= (MAX))) ? (true) : (false))
     62 
     63 /**
     64  * @brief Get the array size
     65  *
     66  */
     67 #define XTOTP_ARRAY_SIZE(ARRAY) (sizeof((ARRAY)) / sizeof((ARRAY[0])))
     68 
     69 /**
     70  * @brief Calculates the power of the base 10 with the desired exponent
     71  *
     72  * @param exponent A positive integer as exponent. Accepted values are 0-9
     73  * (to hide an overflow)
     74  * @return uint32_t The result of the calculation
     75  */
     76 uint32_t power10(const uint8_t exponent);
     77 
     78 /**
     79  * @brief Returns the base-10 exponent of a positive integer.
     80  *
     81  * Calculates how many digits the number has.
     82  *
     83  * @param number A positive integer.
     84  * @return uint8_t The number of digits in base 10.
     85  */
     86 
     87 uint8_t base10Exponent(uint32_t number);
     88 
     89 /**
     90  * @brief Calculates the tick difference including an overflow of the new tick
     91  *
     92  * @param newTick The current tick
     93  * @param oldTick The old tick for the difference
     94  * @return uint32_t the difference including overflow error
     95  */
     96 uint32_t getTickDiff(uint32_t newTick, uint32_t oldTick);
     97 
     98 #ifdef __cplusplus
     99 }
    100 #endif
    101 
    102 #endif /* XTOTP_UTIL_H*/