Skip to content

Commit

Permalink
Apply formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonel Muñoz committed May 9, 2019
1 parent 2fd5955 commit 3cc03f0
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 101 deletions.
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
Loading

0 comments on commit 3cc03f0

Please sign in to comment.