taler-mdb

GNU Taler Extensions and Integrations
Log | Files | Refs | Submodules | README | LICENSE

commit 2c75a0a240f06d96d6f43ace7917e2ebfc0c4e1c
parent b1fd9e82edbd8b1a04ea25388a6ce28f00742445
Author: Boss Marco <bossm8@students.bfh.ch>
Date:   Fri, 29 Nov 2019 16:08:18 +0100

integration mdb first try

Diffstat:
Msrc/Makefile.am | 3++-
Msrc/main.c | 231+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Msrc/mdb/mdb_uart.c | 527+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Msrc/mdb/mdb_uart.h | 37+++++++++++++++++++------------------
4 files changed, 519 insertions(+), 279 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am @@ -7,7 +7,8 @@ if USE_COVERAGE endif taler_mdb_SOURCES = \ - main.c + main.c \ + mdb/mdb_uart.c taler_mdb_LDADD = \ -ltalermerchant \ -ltalerjson \ diff --git a/src/main.c b/src/main.c @@ -53,6 +53,8 @@ along with #include <linux/fb.h> #endif +#include "mdb/mdb_uart.h" + /** * Disable i18n support. */ @@ -90,12 +92,41 @@ along with */ #define UID_LEN_LOWER 4 +/* Commands for communication via MDB/ICP */ +/* VMC commands */ +#define VMC_VEND 0x13 +#define VMC_VEND_REQUEST 0x00 +#define VMC_VEND_CANCEL 0x01 +#define VMC_VEND_SUCCESS 0x02 +#define VMC_VEND_FAILURE 0x03 +#define VMC_VEND_SESSION_COMPLETE 0x04 + +/* Reader commands */ +#define READER_CONFIG "01" +#define READER_DISPLAY_REQUEST "02" +#define READER_BEGIN_SESSION "03" +#define READER_SESSION_CANCEL_REQUEST "04" +#define READER_VEND_APPROVE "05" +#define READER_VEND_DENIED "06" +#define READER_END_SESSION "07" +#define READER_SESSION_CANCELLED "08" +#define READER_REVALUE_APPROVED "0D" +#define READER_REVALUE_DENIED "0E" + +/* Constants */ +#define MAX_SIZE_RX_BUFFER 256 + +/* Macro for char to hex conversion */ +#define convertChar2Hex(C) (((C) >= 'A' && (C) <= 'F') ? ((C) -55) : ((C) \ + - '0')) /** * @brief FRAMEBUFFER_DEVICE framebuffer device to diplay qr code */ const char *FRAMEBUFFER_DEVICE = "/dev/fb1"; +const char *UART_DEVICE = "/dev/ttyAMA0"; + /** * Taler wallet application identifier */ @@ -194,6 +225,14 @@ struct PaymentActivity int wallet_has_uri; }; +struct mdbHandle +{ + + int uartfd; + struct mdbCmd *cmd; + int session_running; +}; + /** * Handle for the Framebuffer device */ @@ -245,6 +284,7 @@ static int global_ret; */ static struct GNUNET_SCHEDULER_Task *keyboard_task; +static struct GNUNET_SCHEDULER_Task *mdb_task; /** * Curl context for communication with taler backend */ @@ -295,6 +335,14 @@ static struct Product *products; */ static unsigned int products_length; +static struct mdbHandle mdb; + +struct mdbCmd *beginSession; +struct mdbCmd *approveVend; +struct mdbCmd *endSession; +struct mdbCmd *denyVend; +struct mdbCmd *readerConfigData; + /** * Handle for the framebuffer device */ @@ -413,6 +461,11 @@ cleanup_payment (struct PaymentActivity *pa) GNUNET_SCHEDULER_cancel (pa->task); if (NULL != pa->delay_task) GNUNET_SCHEDULER_cancel (pa->delay_task); + if (NULL != mdb.cmd) + { + mdb.cmd = NULL; + mdb.session_running = GNUNET_NO; + } if (NULL != pa->taler_pay_uri) { #if HAVE_QRENCODE_H @@ -457,6 +510,11 @@ shutdown_task (void *cls) GNUNET_SCHEDULER_cancel (keyboard_task); keyboard_task = NULL; } + if (NULL != mdb_task) + { + GNUNET_SCHEDULER_cancel (mdb_task); + mdb_task = NULL; + } if (NULL != ctx) { GNUNET_CURL_fini (ctx); @@ -749,6 +807,7 @@ check_payment_cb (void *cls, GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Backend request to /check-payment failed: %u", http_status); + sendMdbCmd (denyVend, mdb.uartfd); cleanup_payment (pa); GNUNET_assert (payment_activity == pa); payment_activity = NULL; @@ -757,10 +816,7 @@ check_payment_cb (void *cls, if (paid) { - fprintf (stderr, - "FIXME: yield product here!\n"); - /* FIXME: later: continue to offer Internet UNTIL - NFC device disconnects (if NFC connected) */ + sendMdbCmd (approveVend, mdb.uartfd); cleanup_payment (pa); GNUNET_assert (payment_activity == pa); payment_activity = NULL; @@ -996,6 +1052,101 @@ start_read_keyboard () NULL); } +static void +start_read_mdb (void); + +static void +read_mdb_command (void *cls) +{ + (void) cls; + char*input; + + mdb_task = NULL; + input = receiveMdbCmd (mdb.uartfd); + + /* FIXME SHUTDOWN ? */ + + if (NULL != input) + { + if (0 == strcmp ("Config Data", input)) + { + mdb.cmd = readerConfigData; + } + else if (0 == strcmp ("Reader Enable", input)) + { + mdb.session_running = GNUNET_YES; + mdb.cmd = beginSession; + } + else if (0 == strcmp ("Command out of Sequence", input)) + { + mdb.session_running = GNUNET_YES; + mdb.cmd = beginSession; + } + else if (0 == strcmp ("Resend previous data", input)) + { + sendMdbCmd (mdb.cmd, mdb.uartfd); + } + else if (0 == strncmp ("Vend Request", input, strlen ("Vend Request"))) + { + for (unsigned int i = 0; i < products_length; i++) + if (0 == strcmp (&input[strlen ("Vend Request for : ")], + products[i].number)) + { + payment_activity = launch_payment (&products[i]); + start_read_mdb (); + return; + } + } + else if (0 == strcmp ("Vend Success", input)) + { + } + else if (0 == strcmp ("Vend Failure", input)) + { + mdb.cmd = endSession; + if (NULL != payment_activity) + { + cleanup_payment (payment_activity); + payment_activity = NULL; + } + } + else if (0 == strcmp ("Session Complete", input)) + { + sendMdbCmd (endSession, mdb.uartfd); + cleanup_payment (payment_activity); + payment_activity = NULL; + } + else + { + mdb.cmd = NULL; + } + + free (input); + input = NULL; + } + start_read_mdb (); +} + +static void +start_read_mdb () +{ + struct GNUNET_DISK_FileHandle fh = { mdb.uartfd }; + if (NULL == mdb.cmd) + { + mdb.session_running = GNUNET_YES; + mdb.cmd = beginSession; + sendMdbCmd (mdb.cmd, mdb.uartfd); + } + else if (GNUNET_NO == mdb.session_running) + { + sendMdbCmd (mdb.cmd, mdb.uartfd); + } + GNUNET_assert (NULL == mdb_task); + mdb_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, + &fh, + &read_mdb_command, + NULL); +} + /** * Read the products from the configuration file * @param cls closure @@ -1187,6 +1338,7 @@ run (void *cls, GNUNET_assert (GNUNET_OK == GNUNET_CURL_append_header (ctx, authorization)); + #if HAVE_QRENCODE_H /* open the framebuffer device */ qrDisplay.devicefd = open (FRAMEBUFFER_DEVICE, @@ -1267,7 +1419,14 @@ run (void *cls, } #endif - start_read_keyboard (); + + readerConfigData = newMdbCmd ("Reader Config", "0101", "19780A02FF0C"); + beginSession = newMdbCmd ("Begin Session", "03", "FFFF"); + denyVend = newMdbCmd ("Deny Vend", "06", NULL); + approveVend = newMdbCmd ("Approve Vend", "05", "0001"); + endSession = newMdbCmd ("End Session", "07", NULL); + start_read_mdb (); + // start_read_keyboard (); } int @@ -1294,6 +1453,58 @@ main (int argc, fprintf (stderr, "Failed to set terminal discipline\n"); } + + /* open uart connection */ + struct termios uart_opts_backup, uart_opts_raw; + if (0 > (mdb.uartfd = open (UART_DEVICE, + O_RDWR | O_NOCTTY | O_NDELAY))) + { + fprintf (stderr, + "Failed to open uart device\n"); + return EXIT_FAILURE; + } + + if (0 != tcgetattr (mdb.uartfd, &uart_opts_backup)) + { + fprintf (stderr, + "Failed to get uart discipline\n"); + return EXIT_FAILURE; + } + + uart_opts_raw = uart_opts_backup; + + /* reset current uart discipline */ + memset (&uart_opts_raw, + 0, + sizeof(uart_opts_raw)); + + /* set baudrate */ + cfsetispeed (&uart_opts_raw, B9600); + cfsetospeed (&uart_opts_raw, B9600); + + /* set options */ + uart_opts_raw.c_cflag &= ~PARENB; + uart_opts_raw.c_cflag &= ~CSTOPB; + uart_opts_raw.c_cflag &= ~CSIZE; + uart_opts_raw.c_cflag |= CS8; + + /* 19200 bps, 8 databits, ignore cd-signal, allow reading */ + uart_opts_raw.c_cflag |= (CLOCAL | CREAD); + + uart_opts_raw.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); + uart_opts_raw.c_iflag = IGNPAR; + uart_opts_raw.c_oflag &= ~OPOST; + uart_opts_raw.c_cc[VMIN] = 14; + uart_opts_raw.c_cc[VTIME] = 10; + tcflush (mdb.uartfd,TCIOFLUSH); + if (0 != tcsetattr (mdb.uartfd, + TCSAFLUSH, + &uart_opts_raw)) + { + printf ("Failed to set uart discipline\n"); + return EXIT_FAILURE; + } + ret = GNUNET_PROGRAM_run (argc, argv, "taler-mdb", @@ -1308,6 +1519,16 @@ main (int argc, fprintf (stderr, "Failed to restore terminal discipline\n"); } + + if (0 != tcsetattr (mdb.uartfd, + TCSAFLUSH, + &uart_opts_backup)) + { + printf ("Failed to restore uart discipline\n"); + global_ret = EXIT_FAILURE; + } + close (mdb.uartfd); + if (GNUNET_OK != ret) return 1; return global_ret; diff --git a/src/mdb/mdb_uart.c b/src/mdb/mdb_uart.c @@ -26,319 +26,336 @@ along with #include "mdb_uart.h" int -mdbUart_init(void) +mdbUart_init (void) { - int uart0_filestream = -1; - struct termios options; - - //Open Uart - uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY); - printf("File Descriptor: %d\n", uart0_filestream); - - if(uart0_filestream < 0){ - printf("Error - cannot open UART Device\n"); + int uart0_filestream = -1; + struct termios options; + + // Open Uart + uart0_filestream = open ("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY); + printf ("File Descriptor: %d\n", uart0_filestream); + + if (uart0_filestream < 0) + { + printf ("Error - cannot open UART Device\n"); + } + else{ + fcntl (uart0_filestream, F_SETFL, 0); + if (tcgetattr (uart0_filestream, &options) != 0) + { + printf ("Error - Cannot get current UART configuration\n"); + return -1; } - else{ - fcntl(uart0_filestream, F_SETFL, 0); - if(tcgetattr(uart0_filestream, &options) != 0){ - printf("Error - Cannot get current UART configuration\n"); - return -1; - } - - memset(&options, 0, sizeof(options)); /* Structur loeschen, ggf. vorher sichern + + memset (&options, 0, sizeof(options)); /* Structur loeschen, ggf. vorher sichern und bei Programmende wieder restaurieren */ - /* Baudrate setzen */ - cfsetispeed(&options, B9600); - cfsetospeed(&options, B9600); - - /* setze Optionen */ - options.c_cflag &= ~PARENB; /* kein Paritybit */ - options.c_cflag &= ~CSTOPB; /* 1 Stoppbit */ - options.c_cflag &= ~CSIZE; /* 8 Datenbits */ - options.c_cflag |= CS8; - - /* 19200 bps, 8 Datenbits, CD-Signal ignorieren, Lesen erlauben */ - options.c_cflag |= (CLOCAL | CREAD); - - /* Kein Echo, keine Steuerzeichen, keine Interrupts */ - options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); - options.c_iflag = IGNPAR; /* Parity-Fehler ignorieren */ - options.c_oflag &= ~OPOST; /* setze "raw" Input */ - options.c_cc[VMIN] = 14; /* warten auf min. 0 Zeichen */ - options.c_cc[VTIME] = 10; /* Timeout 1 Sekunde */ - tcflush(uart0_filestream,TCIOFLUSH); /* Puffer leeren */ - if(tcsetattr(uart0_filestream, TCSAFLUSH, &options) != 0){ - printf("Error - Cannot configurate UART device\n"); - return(-1); - } + /* Baudrate setzen */ + cfsetispeed (&options, B9600); + cfsetospeed (&options, B9600); + + /* setze Optionen */ + options.c_cflag &= ~PARENB; /* kein Paritybit */ + options.c_cflag &= ~CSTOPB; /* 1 Stoppbit */ + options.c_cflag &= ~CSIZE; /* 8 Datenbits */ + options.c_cflag |= CS8; + + /* 19200 bps, 8 Datenbits, CD-Signal ignorieren, Lesen erlauben */ + options.c_cflag |= (CLOCAL | CREAD); + + /* Kein Echo, keine Steuerzeichen, keine Interrupts */ + options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); + options.c_iflag = IGNPAR; /* Parity-Fehler ignorieren */ + options.c_oflag &= ~OPOST; /* setze "raw" Input */ + options.c_cc[VMIN] = 14; /* warten auf min. 0 Zeichen */ + options.c_cc[VTIME] = 10; /* Timeout 1 Sekunde */ + tcflush (uart0_filestream,TCIOFLUSH); /* Puffer leeren */ + if (tcsetattr (uart0_filestream, TCSAFLUSH, &options) != 0) + { + printf ("Error - Cannot configurate UART device\n"); + return(-1); } + } - return uart0_filestream; + return uart0_filestream; } int -uart_close(int filestream) +uart_close (int filestream) { - return close(filestream); + return close (filestream); } + int -mdb_init(int filestream) -{ - mdbCmd *readerConfigData; - - - readerConfigData = newMdbCmd("Reader Config", "0101", "19780A02FF0C"); - - while(1){ - sendMdbCmd(readerConfigData, filestream); - receivedMdbCmd = receiveMdbCmd(filestream); - if(receivedMdbCmd){ - if(strcmp("Reader Enable", receivedMdbCmd) == 0){ - break; - } - else{ - free(receivedMdbCmd); - } - } - } - - free(receivedMdbCmd); - - deleteMdbCmd(readerConfigData); - - return EXIT_SUCCESS; -} -int -uartSendBytes(int uart0_filestream, uint8_t* txBuffer, int nrOfBytes) +uartSendBytes (int uart0_filestream, uint8_t*txBuffer, int nrOfBytes) { - int nrOfSentBytes = 0; + int nrOfSentBytes = 0; - nrOfSentBytes = write(uart0_filestream, txBuffer, nrOfBytes); + nrOfSentBytes = write (uart0_filestream, txBuffer, nrOfBytes); - printf("Number of Bytes sent: %d\n", nrOfSentBytes); + printf ("Number of Bytes sent: %d\n", nrOfSentBytes); - return nrOfSentBytes; + return nrOfSentBytes; } int -uartReceiveBytes(int uart0_filestream, uint8_t *rxBuffer, int nrOfBytes) +uartReceiveBytes (int uart0_filestream, uint8_t *rxBuffer, int nrOfBytes) { - int Bytes = 0; + int nrOfReceivedBytes = 0; - nrOfReceivedBytes = read(uart0_filestream, rxBuffer, nrOfBytes); + nrOfReceivedBytes = read (uart0_filestream, rxBuffer, nrOfBytes); - // printf("Number of Bytes received: %d\n", nrOfReceivedBytes); + // printf("Number of Bytes received: %d\n", nrOfReceivedBytes); - return nrOfReceivedBytes; + return nrOfReceivedBytes; } char * -receiveMdbCmd(int filestream) +receiveMdbCmd (int filestream) { - uint8_t rxBuffer[MAX_SIZE_RX_BUFFER]; - uint8_t cmd = 0xFF; + uint8_t rxBuffer[MAX_SIZE_RX_BUFFER]; + uint8_t cmd = 0xFF; - size_t cmdStartIdx = 0; - int nrOfReceivedBytes = 0; + size_t cmdStartIdx = 0; + int nrOfReceivedBytes = 0; - nrOfReceivedBytes = read(filestream, rxBuffer, MAX_SIZE_RX_BUFFER); + nrOfReceivedBytes = read (filestream, rxBuffer, MAX_SIZE_RX_BUFFER); - if(nrOfReceivedBytes < 4){ - return NULL; - } + if (nrOfReceivedBytes < 4) + { + return NULL; + } - while(rxBuffer[cmdStartIdx] != 0x02){ - cmdStartIdx++; - if(cmdStartIdx == nrOfReceivedBytes){ - return NULL; - } + while (rxBuffer[cmdStartIdx] != 0x02) + { + cmdStartIdx++; + if (cmdStartIdx == nrOfReceivedBytes) + { + return NULL; } - //printf("Received Bytes: %d\n", nrOfReceivedBytes); - char *retVal = malloc(30*sizeof(char) + sizeof(uint16_t)); - - if(!retVal){ - puts("Error: Cannot allocate memory"); + } + // printf("Received Bytes: %d\n", nrOfReceivedBytes); + char *retVal = malloc (30 * sizeof(char) + sizeof(uint16_t)); + + if (! retVal) + { + puts ("Error: Cannot allocate memory"); + } + + cmd = ((convertChar2Hex (rxBuffer[cmdStartIdx + 1]) << 4) | (convertChar2Hex ( + rxBuffer[ + cmdStartIdx + + 2]))); + printf ("Received Command: %X\n", cmd); + + switch (cmd) + { + case 0x13: + if (((convertChar2Hex (rxBuffer[cmdStartIdx + 3]) << 4) | (convertChar2Hex ( + rxBuffer[ + cmdStartIdx + + 4]))) == + 0x00) + { + uint16_t productNmbr = 0x0000; + size_t idx; + size_t shiftIdx; + for (idx = cmdStartIdx + 9, shiftIdx = 12; idx <= cmdStartIdx + 12; idx++, + shiftIdx -= 4) { + productNmbr |= ((uint16_t) ((convertChar2Hex (rxBuffer[idx])) + << shiftIdx)); + } + puts (""); + sprintf (retVal, "Vend Request for: %d", productNmbr); } - - cmd = ((convertChar2Hex(rxBuffer[cmdStartIdx+1]) << 4) | (convertChar2Hex(rxBuffer[cmdStartIdx+2]))); - printf("Received Command: %X\n", cmd); - - switch (cmd) { - case 0x13: - if(((convertChar2Hex(rxBuffer[cmdStartIdx+3]) << 4) | (convertChar2Hex(rxBuffer[cmdStartIdx+4]))) == 0x00){ - uint16_t productNmbr = 0x0000; - size_t idx; - size_t shiftIdx; - for (idx = cmdStartIdx + 9, shiftIdx = 12; idx <= cmdStartIdx + 12; idx++, shiftIdx -= 4) { - productNmbr |= ((uint16_t)((convertChar2Hex(rxBuffer[idx])) << shiftIdx)); - } - puts(""); - sprintf(retVal, "Vend Request for: %d", productNmbr); - } - else if (((convertChar2Hex(rxBuffer[cmdStartIdx+3]) << 4) | (convertChar2Hex(rxBuffer[cmdStartIdx+4]))) == 0x02) { - sprintf(retVal, "Vend Success"); - } - else if (((convertChar2Hex(rxBuffer[cmdStartIdx+3]) << 4) | (convertChar2Hex(rxBuffer[cmdStartIdx+4]))) == 0x03) { - sprintf(retVal, "Vend Failure"); - } - else if (((convertChar2Hex(rxBuffer[cmdStartIdx+3]) << 4) | (convertChar2Hex(rxBuffer[cmdStartIdx+4]))) == 0x04) { - sprintf(retVal, "Session Complete"); - } - else { - free(retVal); - retVal = NULL; - } - break; - case 0x11: - sprintf(retVal, "Config Data"); - break; - case 0x14: - if(((convertChar2Hex(rxBuffer[cmdStartIdx+3]) << 4) | (convertChar2Hex(rxBuffer[cmdStartIdx+4]))) == 0x01){ - sprintf(retVal, "Reader Enable"); - } - else { - free(retVal); - retVal = NULL; - } - break; - case 0x00: - sprintf(retVal, "Acknowledge"); - break; - case 0xD0: - sprintf(retVal, "Command out of Sequence"); - break; - case 0xAA: - sprintf(retVal, "Resend previous data"); - break; - default: - free(retVal); - retVal = NULL; - break; + else if (((convertChar2Hex (rxBuffer[cmdStartIdx + 3]) << 4) + | (convertChar2Hex (rxBuffer[cmdStartIdx + 4]))) == 0x02) + { + sprintf (retVal, "Vend Success"); } - return retVal; - -} -mdbCmd * -newMdbCmd(char *name, char *cmd, char *data) -{ - - mdbCmd* cmdNew = malloc(sizeof(mdbCmd)); - size_t idx = 0; - size_t strIdx = 0; - - if(name == NULL){ - cmdNew->name = malloc(sizeof(char) * strlen("No Cmd Name Set") + 1); - strncpy(cmdNew->name, "No Cmd Name Set", strlen("No Cmd Name Set") + 1); + else if (((convertChar2Hex (rxBuffer[cmdStartIdx + 3]) << 4) + | (convertChar2Hex (rxBuffer[cmdStartIdx + 4]))) == 0x03) + { + sprintf (retVal, "Vend Failure"); } - else{ - cmdNew->name = malloc(sizeof(char) * strlen(name) + 1); - strncpy(cmdNew->name, name, strlen(name) + 1); + else if (((convertChar2Hex (rxBuffer[cmdStartIdx + 3]) << 4) + | (convertChar2Hex (rxBuffer[cmdStartIdx + 4]))) == 0x04) + { + sprintf (retVal, "Session Complete"); } - - if(cmd == NULL){ - cmdNew->cmdLength = 0; - cmdNew->cmd = NULL; + else { + free (retVal); + retVal = NULL; } - else{ - cmdNew->cmdLength = strlen(cmd) / 2; - cmdNew->cmd = malloc(sizeof(uint8_t) * cmdNew->cmdLength); - printf("New Command: "); - for(idx = 0, strIdx = 0; idx < cmdNew->cmdLength; idx++, strIdx += 2){ - cmdNew->cmd[idx] = (convertChar2Hex(cmd[strIdx]) << 4 | convertChar2Hex(cmd[strIdx + 1])); - printf("%X", cmdNew->cmd[idx]); - } - puts(""); + break; + case 0x11: + sprintf (retVal, "Config Data"); + break; + case 0x14: + if (((convertChar2Hex (rxBuffer[cmdStartIdx + 3]) << 4) | (convertChar2Hex ( + rxBuffer[ + cmdStartIdx + + 4]))) == + 0x01) + { + sprintf (retVal, "Reader Enable"); } - - if(data == NULL){ - cmdNew->dataLength = 0; - cmdNew->data = NULL; + else { + free (retVal); + retVal = NULL; } - else{ - cmdNew->dataLength = strlen(data) / 2; - cmdNew->data = malloc(sizeof(uint8_t) * cmdNew->dataLength); + break; + case 0x00: + sprintf (retVal, "Acknowledge"); + break; + case 0xD0: + sprintf (retVal, "Command out of Sequence"); + break; + case 0xAA: + sprintf (retVal, "Resend previous data"); + break; + default: + free (retVal); + retVal = NULL; + break; + } + return retVal; - for(idx = 0, strIdx = 0; idx < cmdNew->dataLength; idx++, strIdx += 2){ - cmdNew->data[idx] = ((convertChar2Hex(data[strIdx])) << 4 | convertChar2Hex(data[strIdx + 1])); - } +} +struct mdbCmd * +newMdbCmd (char *name, char *cmd, char *data) +{ + + struct mdbCmd*cmdNew = malloc (sizeof(struct mdbCmd)); + size_t idx = 0; + size_t strIdx = 0; + + if (name == NULL) + { + cmdNew->name = malloc (sizeof(char) * strlen ("No Cmd Name Set") + 1); + strncpy (cmdNew->name, "No Cmd Name Set", strlen ("No Cmd Name Set") + 1); + } + else{ + cmdNew->name = malloc (sizeof(char) * strlen (name) + 1); + strncpy (cmdNew->name, name, strlen (name) + 1); + } + + if (cmd == NULL) + { + cmdNew->cmdLength = 0; + cmdNew->cmd = NULL; + } + else{ + cmdNew->cmdLength = strlen (cmd) / 2; + cmdNew->cmd = malloc (sizeof(uint8_t) * cmdNew->cmdLength); + printf ("New Command: "); + for (idx = 0, strIdx = 0; idx < cmdNew->cmdLength; idx++, strIdx += 2) { + cmdNew->cmd[idx] = (convertChar2Hex (cmd[strIdx]) << 4 | convertChar2Hex ( + cmd[strIdx + 1])); + printf ("%X", cmdNew->cmd[idx]); } + puts (""); + } + + if (data == NULL) + { + cmdNew->dataLength = 0; + cmdNew->data = NULL; + } + else{ + cmdNew->dataLength = strlen (data) / 2; + cmdNew->data = malloc (sizeof(uint8_t) * cmdNew->dataLength); + + for (idx = 0, strIdx = 0; idx < cmdNew->dataLength; idx++, strIdx += 2) { + cmdNew->data[idx] = ((convertChar2Hex (data[strIdx])) << 4 + | convertChar2Hex (data[strIdx + 1])); + } + } - return cmdNew; + return cmdNew; } void -deleteMdbCmd(mdbCmd *mdbCmdToDelete) +deleteMdbCmd (struct mdbCmd *mdbCmdToDelete) { - free(mdbCmdToDelete->name); - free(mdbCmdToDelete->cmd); - free(mdbCmdToDelete->data); - free(mdbCmdToDelete); + free (mdbCmdToDelete->name); + free (mdbCmdToDelete->cmd); + free (mdbCmdToDelete->data); + free (mdbCmdToDelete); } void -setMdbCmdData(mdbCmd *mdbCmdToSet, char *data) +setMdbCmdData (struct mdbCmd *mdbCmdToSet, char *data) { - mdbCmdToSet->dataLength = strlen(data); - - if(mdbCmdToSet->dataLength == 0){ - free(mdbCmdToSet->data); - mdbCmdToSet->data = NULL; - } - else{ - mdbCmdToSet = realloc(mdbCmdToSet->data, sizeof(uint8_t) * mdbCmdToSet->dataLength); - size_t strIdx = 0; - for(size_t idx = 0; idx < mdbCmdToSet->dataLength; idx++){ - mdbCmdToSet->data[idx] = (convertChar2Hex(data[strIdx]) << 4) | (convertChar2Hex(data[strIdx + 1])); - strIdx = strIdx + 2; - } + mdbCmdToSet->dataLength = strlen (data); + + if (mdbCmdToSet->dataLength == 0) + { + free (mdbCmdToSet->data); + mdbCmdToSet->data = NULL; + } + else{ + mdbCmdToSet = realloc (mdbCmdToSet->data, sizeof(uint8_t) + * mdbCmdToSet->dataLength); + size_t strIdx = 0; + for (size_t idx = 0; idx < mdbCmdToSet->dataLength; idx++) { + mdbCmdToSet->data[idx] = (convertChar2Hex (data[strIdx]) << 4) + | (convertChar2Hex (data[strIdx + 1])); + strIdx = strIdx + 2; } + } } int -sendMdbCmd(mdbCmd* cmdToSend, int filestream) +sendMdbCmd (struct mdbCmd*cmdToSend, int filestream) { - uint8_t* txBuffer; - uint32_t chkSum = 0x00000000; - int nrOfSentBytes = 0; - char *receivedMdbCmd = NULL; - - txBuffer = malloc(sizeof(uint8_t) * (cmdToSend->cmdLength + cmdToSend->dataLength + 1)); - - for(size_t idx = 0; idx < cmdToSend->cmdLength; idx++){ - txBuffer[idx] = cmdToSend->cmd[idx]; - //printf("%X", cmdToSend->cmd[idx]); - chkSum += cmdToSend->cmd[idx]; - } - - for(size_t idx = 0; idx < cmdToSend->dataLength; idx++){ - txBuffer[idx + cmdToSend->cmdLength] = cmdToSend->data[idx]; - chkSum += cmdToSend->data[idx]; - } - - txBuffer[cmdToSend->cmdLength + cmdToSend->dataLength] = (uint8_t)(chkSum & 0xFF); - - printf("Send command: %s\n", cmdToSend->name); - - - while(!receivedMdbCmd){ - nrOfSentBytes = uartSendBytes(filestream, txBuffer, cmdToSend->cmdLength + cmdToSend->dataLength + 1); - receivedMdbCmd = receiveMdbCmd(filestream); - - if(receivedMdbCmd){ - if(strcmp("Acknowledge", receivedMdbCmd) == 0){ - break; - } - else{ - free(receivedMdbCmd); - receivedMdbCmd = NULL; - } - } + uint8_t*txBuffer; + uint32_t chkSum = 0x00000000; + int nrOfSentBytes = 0; + char *receivedMdbCmd = NULL; + + txBuffer = malloc (sizeof(uint8_t) * (cmdToSend->cmdLength + + cmdToSend->dataLength + 1)); + + for (size_t idx = 0; idx < cmdToSend->cmdLength; idx++) { + txBuffer[idx] = cmdToSend->cmd[idx]; + // printf("%X", cmdToSend->cmd[idx]); + chkSum += cmdToSend->cmd[idx]; + } + + for (size_t idx = 0; idx < cmdToSend->dataLength; idx++) { + txBuffer[idx + cmdToSend->cmdLength] = cmdToSend->data[idx]; + chkSum += cmdToSend->data[idx]; + } + + txBuffer[cmdToSend->cmdLength + cmdToSend->dataLength] = (uint8_t) (chkSum + & 0xFF); + + printf ("Send command: %s\n", cmdToSend->name); + + + while (! receivedMdbCmd) + { + nrOfSentBytes = uartSendBytes (filestream, txBuffer, cmdToSend->cmdLength + + cmdToSend->dataLength + 1); + receivedMdbCmd = receiveMdbCmd (filestream); + + if (receivedMdbCmd) + { + if (strcmp ("Acknowledge", receivedMdbCmd) == 0) + { + break; + } + else{ + free (receivedMdbCmd); + receivedMdbCmd = NULL; + } } - free(receivedMdbCmd); + } + free (receivedMdbCmd); - return nrOfSentBytes; + return nrOfSentBytes; } diff --git a/src/mdb/mdb_uart.h b/src/mdb/mdb_uart.h @@ -57,26 +57,27 @@ along with #define MAX_SIZE_RX_BUFFER 256 /* Macro for char to hex conversion */ -#define convertChar2Hex(C) (((C) >= 'A' && (C) <= 'F') ? ((C) - 55) : ((C) - '0')) +#define convertChar2Hex(C) (((C) >= 'A' && (C) <= 'F') ? ((C) -55) : ((C) \ + - '0')) /* Datatype for mdb command */ -typedef struct mdbCmd_ +struct mdbCmd { - char* name; - uint8_t* cmd; - size_t cmdLength; - uint8_t* data; - size_t dataLength; -}mdbCmd; + char*name; + uint8_t*cmd; + size_t cmdLength; + uint8_t*data; + size_t dataLength; +}; /* Function Prototypes */ -int uart_init(void); -int uart_close(int filestream); -int mdb_init(int filestream); -int uartSendBytes(int uart0_filestream, uint8_t* txBuffer, int nrOfBytes); -int uartReceiveBytes(int uart0_filestream, uint8_t* rxBuffer, int nrOfBytes); -mdbCmd* newMdbCmd(char* name, char *cmd, char *data); -void deleteMdbCmd(mdbCmd *mdbCmdToDelete); -void setMdbCmdData(mdbCmd *mdbCmdToSet, char *data); -int sendMdbCmd(mdbCmd* cmdToSend, int filestream); -char *receiveMdbCmd(int filestream); +int uart_init (void); +int uart_close (int filestream); +int mdb_init (int filestream); +int uartSendBytes (int uart0_filestream, uint8_t*txBuffer, int nrOfBytes); +int uartReceiveBytes (int uart0_filestream, uint8_t*rxBuffer, int nrOfBytes); +struct mdbCmd *newMdbCmd (char*name, char *cmd, char *data); +void deleteMdbCmd (struct mdbCmd *mdbCmdToDelete); +void setMdbCmdData (struct mdbCmd *mdbCmdToSet, char *data); +int sendMdbCmd (struct mdbCmd*cmdToSend, int filestream); +char *receiveMdbCmd (int filestream);