From 3cc03f084f0a776a00a793657c98c6dfa0086ca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonel=20Mu=C3=B1oz?= Date: Thu, 9 May 2019 10:02:48 -0400 Subject: [PATCH] Apply formatting --- src/hex_codes.h | 3 +- src/message.h | 5 +- src/responses.h | 7 +- src/transbank.h | 7 +- src/transbank_serial_utils.c | 77 ++++++++++++------- src/transbank_serial_utils.h | 10 +-- test/test_transbank.c | 114 +++++++++++++++++------------ test/test_transbank_serial_utils.c | 30 ++++---- 8 files changed, 152 insertions(+), 101 deletions(-) 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.c b/test/test_transbank.c index e5d1974..d66312b 100644 --- a/test/test_transbank.c +++ b/test/test_transbank.c @@ -4,55 +4,63 @@ #include "transbank.h" #include -int __wrap_write_message(struct sp_port *port, Message message){ +int __wrap_write_message(struct sp_port *port, Message message) +{ return mock(); } -int __wrap_read_ack(struct sp_port *port){ +int __wrap_read_ack(struct sp_port *port) +{ return mock(); } -int __wrap_read_bytes(struct sp_port *port, char* buf, Message message){ - char response[] = { 0x02, - 0x30, 0x38, 0x31, 0x30, 0x07, - 0x30, 0x30, 0x07, - 0x35, 0x39, 0x37, 0x30, 0x32, 0x39, 0x34, 0x31, 0x34, 0x33, 0x30, 0x30, 0x07, - 0x37, 0x35, 0x30, 0x30, 0x31, 0x30, 0x38, 0x37, 0x03, 0x30, '\0'}; +int __wrap_read_bytes(struct sp_port *port, char *buf, Message message) +{ + char response[] = {0x02, + 0x30, 0x38, 0x31, 0x30, 0x07, + 0x30, 0x30, 0x07, + 0x35, 0x39, 0x37, 0x30, 0x32, 0x39, 0x34, 0x31, 0x34, 0x33, 0x30, 0x30, 0x07, + 0x37, 0x35, 0x30, 0x30, 0x31, 0x30, 0x38, 0x37, 0x03, 0x30, '\0'}; strcpy(buf, response); return mock(); } -int __wrap_sp_input_waiting(struct sp_port *port){ +int __wrap_sp_input_waiting(struct sp_port *port) +{ return mock(); } -int __wrap_reply_ack(struct sp_port *port, char* message, int length){ +int __wrap_reply_ack(struct sp_port *port, char *message, int length) +{ return mock(); } - -void test_poll_ok(void **state){ - (void) state; /* unused */ +void test_poll_ok(void **state) +{ + (void)state; /* unused */ will_return(__wrap_write_message, TBK_OK); will_return(__wrap_read_ack, TBK_OK); assert_int_equal((int)TBK_OK, poll()); } -void test_poll_write_nok(void **state){ - (void) state; /* unused */ +void test_poll_write_nok(void **state) +{ + (void)state; /* unused */ will_return_count(__wrap_write_message, TBK_NOK, 3); assert_int_equal((int)TBK_NOK, poll()); } -void test_poll_ack_nok(void **state){ - (void) state; /* unused */ +void test_poll_ack_nok(void **state) +{ + (void)state; /* unused */ will_return_count(__wrap_write_message, TBK_OK, 3); will_return_count(__wrap_read_ack, TBK_NOK, 3); assert_int_equal((int)TBK_NOK, poll()); } -void test_poll_ok_on_second_try(void **state){ - (void) state; /* unused */ +void test_poll_ok_on_second_try(void **state) +{ + (void)state; /* unused */ will_return(__wrap_write_message, TBK_NOK); will_return(__wrap_write_message, TBK_OK); will_return_count(__wrap_read_ack, TBK_OK, 1); @@ -60,37 +68,42 @@ void test_poll_ok_on_second_try(void **state){ assert_int_equal((int)TBK_OK, poll()); } -void test_poll_ok_on_third_try(void **state){ - (void) state; /* unused */ - will_return_count(__wrap_write_message, TBK_NOK,2); +void test_poll_ok_on_third_try(void **state) +{ + (void)state; /* unused */ + will_return_count(__wrap_write_message, TBK_NOK, 2); will_return(__wrap_write_message, TBK_OK); will_return_count(__wrap_read_ack, TBK_OK, 1); assert_int_equal((int)TBK_OK, poll()); } -void test_set_normal_mode_ok(void **state){ - (void) state; /* unused */ +void test_set_normal_mode_ok(void **state) +{ + (void)state; /* unused */ will_return(__wrap_write_message, TBK_OK); will_return(__wrap_read_ack, TBK_OK); assert_int_equal((int)TBK_OK, set_normal_mode()); } -void test_set_normal_mode_write_nok(void **state){ - (void) state; /* unused */ +void test_set_normal_mode_write_nok(void **state) +{ + (void)state; /* unused */ will_return_count(__wrap_write_message, TBK_NOK, 3); assert_int_equal((int)TBK_NOK, set_normal_mode()); } -void test_set_normal_mode_ack_nok(void **state){ - (void) state; /* unused */ +void test_set_normal_mode_ack_nok(void **state) +{ + (void)state; /* unused */ will_return_count(__wrap_write_message, TBK_OK, 3); will_return_count(__wrap_read_ack, TBK_NOK, 3); assert_int_equal((int)TBK_NOK, set_normal_mode()); } -void test_set_normal_mode_ok_on_second_try(void **state){ - (void) state; /* unused */ +void test_set_normal_mode_ok_on_second_try(void **state) +{ + (void)state; /* unused */ will_return(__wrap_write_message, TBK_NOK); will_return(__wrap_write_message, TBK_OK); will_return_count(__wrap_read_ack, TBK_OK, 1); @@ -98,17 +111,19 @@ void test_set_normal_mode_ok_on_second_try(void **state){ assert_int_equal((int)TBK_OK, set_normal_mode()); } -void test_set_normal_mode_ok_on_third_try(void **state){ - (void) state; /* unused */ - will_return_count(__wrap_write_message, TBK_NOK,2); +void test_set_normal_mode_ok_on_third_try(void **state) +{ + (void)state; /* unused */ + will_return_count(__wrap_write_message, TBK_NOK, 2); will_return(__wrap_write_message, TBK_OK); will_return_count(__wrap_read_ack, TBK_OK, 1); assert_int_equal((int)TBK_OK, set_normal_mode()); } -void test_load_keys_ok(void **state){ - (void) state; +void test_load_keys_ok(void **state) +{ + (void)state; will_return(__wrap_write_message, TBK_OK); will_return(__wrap_read_ack, TBK_OK); will_return(__wrap_read_bytes, 33); @@ -119,30 +134,33 @@ void test_load_keys_ok(void **state){ assert_int_equal(810, response.function); assert_int_equal(0, response.responseCode); - assert_int_equal(597029414300,response.commerceCode); + assert_int_equal(597029414300, response.commerceCode); assert_int_equal(75001087, response.terminalId); assert_int_equal(TBK_OK, response.initilized); } -void test_load_keys_nok(void **state){ - (void) state; +void test_load_keys_nok(void **state) +{ + (void)state; will_return_count(__wrap_write_message, TBK_NOK, 3); BaseResponse response = load_keys(); assert_int_equal(NULL, response.initilized); } -void test_load_keys_ack_nok(void **state){ - (void) state; +void test_load_keys_ack_nok(void **state) +{ + (void)state; will_return_count(__wrap_write_message, TBK_OK, 3); - will_return_count(__wrap_read_ack, TBK_NOK,3); + will_return_count(__wrap_read_ack, TBK_NOK, 3); BaseResponse response = load_keys(); assert_int_equal(NULL, response.initilized); } -void test_load_keys_read_bytes_nok(void **state){ - (void) state; +void test_load_keys_read_bytes_nok(void **state) +{ + (void)state; will_return(__wrap_write_message, TBK_OK); will_return(__wrap_read_ack, TBK_OK); will_return_count(__wrap_read_bytes, -1, 3); @@ -152,8 +170,9 @@ void test_load_keys_read_bytes_nok(void **state){ assert_int_equal(NULL, response.initilized); } -void test_load_keys_reply_ack_nok(void **state){ - (void) state; +void test_load_keys_reply_ack_nok(void **state) +{ + (void)state; will_return(__wrap_write_message, TBK_OK); will_return(__wrap_read_ack, TBK_OK); will_return_count(__wrap_read_bytes, 33, 3); @@ -169,7 +188,7 @@ const struct CMUnitTest transbank_tests[] = { cmocka_unit_test(test_poll_write_nok), cmocka_unit_test(test_poll_ack_nok), cmocka_unit_test(test_poll_ok_on_second_try), - cmocka_unit_test( test_poll_ok_on_third_try), + cmocka_unit_test(test_poll_ok_on_third_try), cmocka_unit_test(test_set_normal_mode_ok), cmocka_unit_test(test_set_normal_mode_write_nok), cmocka_unit_test(test_set_normal_mode_ack_nok), @@ -179,8 +198,7 @@ const struct CMUnitTest transbank_tests[] = { cmocka_unit_test(test_load_keys_nok), cmocka_unit_test(test_load_keys_ack_nok), cmocka_unit_test(test_load_keys_read_bytes_nok), - cmocka_unit_test(test_load_keys_reply_ack_nok) -}; + cmocka_unit_test(test_load_keys_reply_ack_nok)}; int main(void) { 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) {