taler-xotp_fw

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

inputInterface.c (3709B)


      1 /**
      2  * @file inputInterface.c
      3  * @author Adrian STEINER (steia19@bfh.ch)
      4  * @brief Interface to read out the keyboard from the current hardware
      5  * @version 0.1
      6  * @date 13-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 
     24 #include "inputInterface.h"
     25 
     26 #include "ringbuffer.h"
     27 #include "xtotpUtil.h"
     28 
     29 #include <stdbool.h>
     30 #include <stdlib.h>
     31 #include <string.h>
     32 
     33 #define STRING_BUFFER_MAX_SIZE (63)
     34 
     35 struct {
     36   uint8_t buffer[STRING_BUFFER_MAX_SIZE];
     37   uint8_t position;
     38 } input_stringBufferHandler;
     39 
     40 void clearStringBufferHandler(void);
     41 
     42 uint8_t input_initInterface(inputInterface *interfaceHandler,
     43                             inputPwrCB powerOnCB,
     44                             inputPwrCB powerOffCB,
     45                             inputSampleCB readInputCB)
     46 {
     47   if (NULL == interfaceHandler || NULL == powerOnCB || NULL == powerOffCB ||
     48       NULL == readInputCB) {
     49     return EXIT_FAILURE;
     50   }
     51   interfaceHandler->powerOn = powerOnCB;
     52   interfaceHandler->powerOff = powerOffCB;
     53   interfaceHandler->readInput = readInputCB;
     54 
     55   return EXIT_SUCCESS;
     56 }
     57 
     58 bool input_readNumber(uint64_t *number,
     59                       uint32_t maxLength,
     60                       const inputButtonPos inputButton)
     61 {
     62   if (NULL == number || inputButton >= INPUT_NONE) {
     63     return false;
     64   }
     65   switch (inputButton) {
     66   case INPUT_ENTER:
     67     return (0 == *number) ? false : true;
     68   case INPUT_DEL:
     69     *number /= PAYMENT_BASE;
     70     return false;
     71   default:
     72     if (*number < maxLength) {
     73       *number *= PAYMENT_BASE;
     74       *number += (uint64_t)inputButton;
     75       if (*number > maxLength) {
     76         *number = maxLength;
     77       }
     78     }
     79     return false;
     80   }
     81 }
     82 
     83 bool input_readBase8(const inputButtonPos inputButton,
     84                      uint8_t **inputString,
     85                      uint8_t *inputLength,
     86                      uint8_t stringMaxLength,
     87                      bool clearBuffer)
     88 {
     89   if (clearBuffer) {
     90     clearStringBufferHandler();
     91   }
     92   if (NULL == inputString) {
     93     return false;
     94   }
     95   *inputString = input_stringBufferHandler.buffer;
     96   switch (inputButton) {
     97   case INPUT_ENTER:
     98     if (input_stringBufferHandler.position > 0) {
     99       *inputLength = input_stringBufferHandler.position;
    100       return true;
    101     }
    102     break;
    103   case INPUT_DEL:
    104     input_stringBufferHandler.buffer[input_stringBufferHandler.position] = '\0';
    105     if (input_stringBufferHandler.position > 0) {
    106       input_stringBufferHandler.position--;
    107     }
    108     break;
    109   default:
    110     if (inputButton >= INPUT_1 && inputButton <= INPUT_8) {
    111       if (input_stringBufferHandler.position <
    112           XTOTP_MIN((STRING_BUFFER_MAX_SIZE - 1), stringMaxLength)) {
    113         input_stringBufferHandler.buffer[input_stringBufferHandler.position] =
    114             inputButton + '0';
    115         input_stringBufferHandler.position++;
    116       }
    117     }
    118     break;
    119   }
    120   input_stringBufferHandler.buffer[input_stringBufferHandler.position] = '\0';
    121   *inputLength = input_stringBufferHandler.position;
    122   return false;
    123 }
    124 
    125 void clearStringBufferHandler(void)
    126 {
    127   memset(&input_stringBufferHandler, 0, sizeof(input_stringBufferHandler));
    128 }