diff --git a/examples/load_keys.c b/examples/load_keys.c index 0b27f92..4c68683 100644 --- a/examples/load_keys.c +++ b/examples/load_keys.c @@ -12,12 +12,18 @@ int main() BaseResponse response = load_keys(); - // TODO(lm): Check if response is correct :/ - printf("Function: %i\n", response.function); - printf("Response Code: %i\n", response.responseCode); - printf("Commerce Code: %llu\n", response.commerceCode); - printf("Terminal ID: %i\n", response.terminalId); - puts("Keys loaded successfully.\n=================\n"); + if (response.function == 810) + { + printf("Function: %i\n", response.function); + printf("Response Code: %i\n", response.responseCode); + printf("Commerce Code: %llu\n", response.commerceCode); + printf("Terminal ID: %i\n", response.terminalId); + puts("Keys loaded successfully.\n=================\n"); + } + else + { + puts("Unable load keys on selected port.\n"); + } //Close Port retval = close_port(); diff --git a/src/hex_codes.h b/src/hex_codes.h index 620d6d4..396183a 100644 --- a/src/hex_codes.h +++ b/src/hex_codes.h @@ -1,4 +1,5 @@ -enum HexCodes{ +enum HexCodes +{ ACK = 0x06, NAK = 0x15, STX = 0x02, diff --git a/src/message.h b/src/message.h index d2eb9fd..9aa7da8 100644 --- a/src/message.h +++ b/src/message.h @@ -1,11 +1,12 @@ #ifndef MESSAGEHEADER_FILE #define MESSAGEHEADER_FILE -typedef struct { +typedef struct +{ int payloadSize; int responseSize; int retries; - char* payload; + char *payload; } Message; #endif diff --git a/src/responses.h b/src/responses.h index 33d4ac5..78a3af2 100644 --- a/src/responses.h +++ b/src/responses.h @@ -1,13 +1,14 @@ #ifndef RESPONSESHEADER_FILE #define RESPONSESHEADER_FILE -typedef struct{ +typedef struct +{ int index; int length; } ParamInfo; - -typedef struct{ +typedef struct +{ int function; int responseCode; long long commerceCode; diff --git a/src/transbank.h b/src/transbank.h index 386fc20..fc75b9e 100644 --- a/src/transbank.h +++ b/src/transbank.h @@ -10,13 +10,14 @@ #include "message.h" #include "responses.h" -enum TbkReturn{ +enum TbkReturn +{ TBK_OK = 0, TBK_NOK = -1 }; -extern enum TbkReturn open_port(char* portName, int baudrate); -extern char* sale(int amount, int ticket, bool send_messages); +extern enum TbkReturn open_port(char *portName, int baudrate); +extern char *sale(int amount, int ticket, bool send_messages); extern BaseResponse close(); extern BaseResponse load_keys(); extern enum TbkReturn poll(); diff --git a/src/transbank_serial_utils.c b/src/transbank_serial_utils.c index ca02358..4ba1756 100644 --- a/src/transbank_serial_utils.c +++ b/src/transbank_serial_utils.c @@ -3,52 +3,65 @@ static const int DEFAULT_TIMEOUT = 1500; -char* list_ports(){ +char *list_ports() +{ struct sp_port **ports; char *portList; int retval = sp_list_ports(&ports); - if (retval == SP_OK){ + if (retval == SP_OK) + { int separators = 0; int chars = 0; - for (int i=0; ports[i] != NULL; i++){ + for (int i = 0; ports[i] != NULL; i++) + { separators++; chars += strlen(sp_get_port_name(ports[i])); } - portList = malloc((separators) + chars * sizeof(char*)); + portList = malloc((separators) + chars * sizeof(char *)); - if (portList != NULL){ + if (portList != NULL) + { strcpy(portList, sp_get_port_name(ports[0])); - for (int i = 1; ports[i] != NULL; i++){ + for (int i = 1; ports[i] != NULL; i++) + { strcat(portList, "|"); strcat(portList, sp_get_port_name(ports[i])); } } - } else{ - char* error = "No serial devices detected\n"; - portList = malloc(strlen(error) * sizeof(char*)); + } + else + { + char *error = "No serial devices detected\n"; + portList = malloc(strlen(error) * sizeof(char *)); strcpy(portList, error); } sp_free_port_list(ports); return portList; } -char* get_port_name(struct sp_port *port){ - if (port != NULL){ +char *get_port_name(struct sp_port *port) +{ + if (port != NULL) + { return sp_get_port_name(port); - } else{ + } + else + { return "No port configured"; } } -int read_bytes(struct sp_port *port, char* buf, Message message){ +int read_bytes(struct sp_port *port, char *buf, Message message) +{ int retval = TBK_NOK; - if (buf != NULL && sp_input_waiting(port) > 0){ + if (buf != NULL && sp_input_waiting(port) > 0) + { int retval = sp_blocking_read(port, buf, message.responseSize, DEFAULT_TIMEOUT); - sp_flush(port, SP_BUF_INPUT); - return retval; + sp_flush(port, SP_BUF_INPUT); + return retval; } buf = NULL; @@ -56,31 +69,39 @@ int read_bytes(struct sp_port *port, char* buf, Message message){ return retval; } -int read_ack(struct sp_port *port){ +int read_ack(struct sp_port *port) +{ char buf[1]; int retval = sp_blocking_read_next(port, buf, 1, DEFAULT_TIMEOUT); - if (retval == 1 && buf[0] == ACK){ + if (retval == 1 && buf[0] == ACK) + { return TBK_OK; - } else{ + } + else + { return TBK_NOK; } } -unsigned char calculate_lrc(char* message, int length){ +unsigned char calculate_lrc(char *message, int length) +{ unsigned char result = message[1]; - for(int n=2; n < length-1; n++){ + for (int n = 2; n < length - 1; n++) + { result ^= (unsigned char)message[n]; } return result; } -int reply_ack(struct sp_port *port, char* message, int length){ +int reply_ack(struct sp_port *port, char *message, int length) +{ char buf[] = {NAK}; int retval = TBK_NOK; sp_flush(port, SP_BUF_BOTH); unsigned char lrc = calculate_lrc(message, length); - if(lrc == (unsigned char)message[length -1]){ + if (lrc == (unsigned char)message[length - 1]) + { buf[0] = ACK; retval = TBK_OK; } @@ -89,12 +110,16 @@ int reply_ack(struct sp_port *port, char* message, int length){ return retval; } -int write_message(struct sp_port *port, Message message){ +int write_message(struct sp_port *port, Message message) +{ sp_flush(port, SP_BUF_BOTH); int retval = sp_blocking_write(port, message.payload, message.payloadSize, DEFAULT_TIMEOUT); - if (retval == message.payloadSize && sp_drain(port)){ + if (retval == message.payloadSize && sp_drain(port)) + { retval = TBK_OK; - } else{ + } + else + { retval -= message.payloadSize; } sp_flush(port, SP_BUF_OUTPUT); diff --git a/src/transbank_serial_utils.h b/src/transbank_serial_utils.h index 240a1f1..8594bd2 100644 --- a/src/transbank_serial_utils.h +++ b/src/transbank_serial_utils.h @@ -3,12 +3,12 @@ #include "transbank.h" -extern char* list_ports(); -extern char* get_port_name(struct sp_port *port); -extern int read_bytes(struct sp_port *port, char* buf, Message message); +extern char *list_ports(); +extern char *get_port_name(struct sp_port *port); +extern int read_bytes(struct sp_port *port, char *buf, Message message); extern int read_ack(struct sp_port *port); -extern unsigned char calculate_lrc(char* message, int length); -extern int reply_ack(struct sp_port *port, char* message, int length); +extern unsigned char calculate_lrc(char *message, int length); +extern int reply_ack(struct sp_port *port, char *message, int length); extern int write_message(struct sp_port *port, Message message); #endif diff --git a/test/test_transbank_serial_utils.c b/test/test_transbank_serial_utils.c index bc0c98d..d66ebac 100644 --- a/test/test_transbank_serial_utils.c +++ b/test/test_transbank_serial_utils.c @@ -6,18 +6,21 @@ static char message[] = {0x02, 0x30, 0x38, 0x30, 0x30, 0x03, 0x0B}; -int __wrap_sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout_ms){ +int __wrap_sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout_ms) +{ return mock(); } -void test_calculate_lrc(void **state){ - (void) state; +void test_calculate_lrc(void **state) +{ + (void)state; int length = strlen(message); - assert_true(message[length-1] == calculate_lrc(message, length)); + assert_true(message[length - 1] == calculate_lrc(message, length)); } -void test_reply_ack_ok_lrc_ok(void **state){ - (void) state; +void test_reply_ack_ok_lrc_ok(void **state) +{ + (void)state; struct sp_port *port; will_return(__wrap_sp_blocking_write, 1); int ret = reply_ack(port, message, strlen(message)); @@ -25,19 +28,21 @@ void test_reply_ack_ok_lrc_ok(void **state){ assert_int_equal(0, ret); } -void test_reply_ack_nok_lrc_ok(void **state){ - (void) state; +void test_reply_ack_nok_lrc_ok(void **state) +{ + (void)state; struct sp_port *port; will_return(__wrap_sp_blocking_write, -1); int ret = reply_ack(port, message, strlen(message)); assert_true(ret < 0); } -void test_reply_ack_nok_lrc_nok(void **state){ - (void) state; +void test_reply_ack_nok_lrc_nok(void **state) +{ + (void)state; struct sp_port *port; will_return(__wrap_sp_blocking_write, -1); - int ret = reply_ack(port, message, strlen(message)-1); + int ret = reply_ack(port, message, strlen(message) - 1); assert_true(ret < 0); } @@ -46,8 +51,7 @@ const struct CMUnitTest transbank_serial_utils_tests[] = { cmocka_unit_test(test_calculate_lrc), cmocka_unit_test(test_reply_ack_ok_lrc_ok), cmocka_unit_test(test_reply_ack_nok_lrc_ok), - cmocka_unit_test(test_reply_ack_nok_lrc_nok) -}; + cmocka_unit_test(test_reply_ack_nok_lrc_nok)}; int main(void) {