Skip to content

Commit

Permalink
Merge branch 'master' into chore/add-missing-test
Browse files Browse the repository at this point in the history
  • Loading branch information
afiebig committed May 9, 2019
2 parents da46f39 + 7ad4e71 commit c3b6c13
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 59 deletions.
18 changes: 12 additions & 6 deletions examples/load_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/hex_codes.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
enum HexCodes{
enum HexCodes
{
ACK = 0x06,
NAK = 0x15,
STX = 0x02,
Expand Down
5 changes: 3 additions & 2 deletions src/message.h
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions src/responses.h
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
7 changes: 4 additions & 3 deletions src/transbank.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
77 changes: 51 additions & 26 deletions src/transbank_serial_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,105 @@

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;
sp_flush(port, SP_BUF_INPUT);
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;
}
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/transbank_serial_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 17 additions & 13 deletions test/test_transbank_serial_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,43 @@

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));

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);
}
Expand All @@ -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)
{
Expand Down

0 comments on commit c3b6c13

Please sign in to comment.