Skip to content

Commit

Permalink
Merge pull request #12 from brandonros/us
Browse files Browse the repository at this point in the history
migrate milliseconds to microseconds
  • Loading branch information
SimonCahill authored Nov 4, 2022
2 parents 5dc26c0 + ae71063 commit b08cf6e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 43 deletions.
67 changes: 30 additions & 37 deletions isotp.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,26 @@
///////////////////////////////////////////////////////

/* st_min to microsecond */
static uint8_t isotp_ms_to_st_min(uint8_t ms) {
uint8_t st_min;

st_min = ms;
if (st_min > 0x7F) {
st_min = 0x7F;
static uint8_t isotp_us_to_st_min(uint32_t us) {
if (us <= 127000) {
return us / 1000;
} else if (us >= 100 && us <= 900) {
return 0xF0 + (us / 100);
}

return st_min;
return 0;
}

/* st_min to msec */
static uint8_t isotp_st_min_to_ms(uint8_t st_min) {
uint8_t ms;

if (st_min >= 0xF1 && st_min <= 0xF9) {
ms = 1;
} else if (st_min <= 0x7F) {
ms = st_min;
} else {
ms = 0;
/* st_min to usec */
static uint32_t isotp_st_min_to_us(uint8_t st_min) {
if (st_min <= 0x7F) {
return st_min * 1000;
} else if (st_min >= 0xF1 && st_min <= 0xF9) {
return (st_min - 0xF0) * 100;
}

return ms;
return 0;
}

static int isotp_send_flow_control(IsoTpLink* link, uint8_t flow_status, uint8_t block_size, uint8_t st_min_ms) {
static int isotp_send_flow_control(IsoTpLink* link, uint8_t flow_status, uint8_t block_size, uint32_t st_min_us) {

IsoTpCanMessage message;
int ret;
Expand All @@ -42,7 +35,7 @@ static int isotp_send_flow_control(IsoTpLink* link, uint8_t flow_status, uint8_t
message.as.flow_control.type = ISOTP_PCI_TYPE_FLOW_CONTROL_FRAME;
message.as.flow_control.FS = flow_status;
message.as.flow_control.BS = block_size;
message.as.flow_control.STmin = isotp_ms_to_st_min(st_min_ms);
message.as.flow_control.STmin = isotp_us_to_st_min(st_min_us);

/* send message */
#ifdef ISO_TP_FRAME_PADDING
Expand Down Expand Up @@ -273,10 +266,10 @@ int isotp_send_with_id(IsoTpLink *link, uint32_t id, const uint8_t payload[], ui
/* init multi-frame control flags */
if (ISOTP_RET_OK == ret) {
link->send_bs_remain = 0;
link->send_st_min = 0;
link->send_st_min_us = 0;
link->send_wtf_count = 0;
link->send_timer_st = isotp_user_get_ms();
link->send_timer_bs = isotp_user_get_ms() + ISO_TP_DEFAULT_RESPONSE_TIMEOUT;
link->send_timer_st = isotp_user_get_us();
link->send_timer_bs = isotp_user_get_us() + ISO_TP_DEFAULT_RESPONSE_TIMEOUT_US;
link->send_protocol_result = ISOTP_PROTOCOL_RESULT_OK;
link->send_status = ISOTP_SEND_STATUS_INPROGRESS;
}
Expand Down Expand Up @@ -342,9 +335,9 @@ void isotp_on_can_message(IsoTpLink *link, uint8_t *data, uint8_t len) {
link->receive_status = ISOTP_RECEIVE_STATUS_INPROGRESS;
/* send fc frame */
link->receive_bs_count = ISO_TP_DEFAULT_BLOCK_SIZE;
isotp_send_flow_control(link, PCI_FLOW_STATUS_CONTINUE, link->receive_bs_count, ISO_TP_DEFAULT_ST_MIN);
isotp_send_flow_control(link, PCI_FLOW_STATUS_CONTINUE, link->receive_bs_count, ISO_TP_DEFAULT_ST_MIN_US);
/* refresh timer cs */
link->receive_timer_cr = isotp_user_get_ms() + ISO_TP_DEFAULT_RESPONSE_TIMEOUT;
link->receive_timer_cr = isotp_user_get_us() + ISO_TP_DEFAULT_RESPONSE_TIMEOUT_US;
}

break;
Expand All @@ -369,7 +362,7 @@ void isotp_on_can_message(IsoTpLink *link, uint8_t *data, uint8_t len) {
/* if success */
if (ISOTP_RET_OK == ret) {
/* refresh timer cs */
link->receive_timer_cr = isotp_user_get_ms() + ISO_TP_DEFAULT_RESPONSE_TIMEOUT;
link->receive_timer_cr = isotp_user_get_us() + ISO_TP_DEFAULT_RESPONSE_TIMEOUT_US;

/* receive finished */
if (link->receive_offset >= link->receive_size) {
Expand All @@ -378,7 +371,7 @@ void isotp_on_can_message(IsoTpLink *link, uint8_t *data, uint8_t len) {
/* send fc when bs reaches limit */
if (0 == --link->receive_bs_count) {
link->receive_bs_count = ISO_TP_DEFAULT_BLOCK_SIZE;
isotp_send_flow_control(link, PCI_FLOW_STATUS_CONTINUE, link->receive_bs_count, ISO_TP_DEFAULT_ST_MIN);
isotp_send_flow_control(link, PCI_FLOW_STATUS_CONTINUE, link->receive_bs_count, ISO_TP_DEFAULT_ST_MIN_US);
}
}
}
Expand All @@ -396,7 +389,7 @@ void isotp_on_can_message(IsoTpLink *link, uint8_t *data, uint8_t len) {

if (ISOTP_RET_OK == ret) {
/* refresh bs timer */
link->send_timer_bs = isotp_user_get_ms() + ISO_TP_DEFAULT_RESPONSE_TIMEOUT;
link->send_timer_bs = isotp_user_get_us() + ISO_TP_DEFAULT_RESPONSE_TIMEOUT_US;

/* overflow */
if (PCI_FLOW_STATUS_OVERFLOW == message.as.flow_control.FS) {
Expand All @@ -421,7 +414,8 @@ void isotp_on_can_message(IsoTpLink *link, uint8_t *data, uint8_t len) {
} else {
link->send_bs_remain = message.as.flow_control.BS;
}
link->send_st_min = isotp_st_min_to_ms(message.as.flow_control.STmin);
uint32_t message_st_min_us = isotp_st_min_to_us(message.as.flow_control.STmin);
link->send_st_min_us = message_st_min_us > ISO_TP_DEFAULT_ST_MIN_US ? message_st_min_us : ISO_TP_DEFAULT_ST_MIN_US; // prefer as much st_min as possible for stability?
link->send_wtf_count = 0;
}
}
Expand Down Expand Up @@ -476,15 +470,15 @@ void isotp_poll(IsoTpLink *link) {
if (/* send data if bs_remain is invalid or bs_remain large than zero */
(ISOTP_INVALID_BS == link->send_bs_remain || link->send_bs_remain > 0) &&
/* and if st_min is zero or go beyond interval time */
(0 == link->send_st_min || IsoTpTimeAfter(isotp_user_get_ms(), link->send_timer_st))) {
(0 == link->send_st_min_us || IsoTpTimeAfter(isotp_user_get_us(), link->send_timer_st))) {

ret = isotp_send_consecutive_frame(link);
if (ISOTP_RET_OK == ret) {
if (ISOTP_INVALID_BS != link->send_bs_remain) {
link->send_bs_remain -= 1;
}
link->send_timer_bs = isotp_user_get_ms() + ISO_TP_DEFAULT_RESPONSE_TIMEOUT;
link->send_timer_st = isotp_user_get_ms() + link->send_st_min;
link->send_timer_bs = isotp_user_get_us() + ISO_TP_DEFAULT_RESPONSE_TIMEOUT_US;
link->send_timer_st = isotp_user_get_us() + link->send_st_min_us;

/* check if send finish */
if (link->send_offset >= link->send_size) {
Expand All @@ -496,7 +490,7 @@ void isotp_poll(IsoTpLink *link) {
}

/* check timeout */
if (IsoTpTimeAfter(isotp_user_get_ms(), link->send_timer_bs)) {
if (IsoTpTimeAfter(isotp_user_get_us(), link->send_timer_bs)) {
link->send_protocol_result = ISOTP_PROTOCOL_RESULT_TIMEOUT_BS;
link->send_status = ISOTP_SEND_STATUS_ERROR;
}
Expand All @@ -506,12 +500,11 @@ void isotp_poll(IsoTpLink *link) {
if (ISOTP_RECEIVE_STATUS_INPROGRESS == link->receive_status) {

/* check timeout */
if (IsoTpTimeAfter(isotp_user_get_ms(), link->receive_timer_cr)) {
if (IsoTpTimeAfter(isotp_user_get_us(), link->receive_timer_cr)) {
link->receive_protocol_result = ISOTP_PROTOCOL_RESULT_TIMEOUT_CR;
link->receive_status = ISOTP_RECEIVE_STATUS_IDLE;
}
}

return;
}

3 changes: 1 addition & 2 deletions isotp.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ typedef struct IsoTpLink {
/* multi-frame flags */
uint8_t send_sn;
uint16_t send_bs_remain; /* Remaining block size */
uint8_t send_st_min; /* Separation Time between consecutive frames, unit millis */
uint32_t send_st_min_us; /* Separation Time between consecutive frames */
uint8_t send_wtf_count; /* Maximum number of FC.Wait frame transmissions */
uint32_t send_timer_st; /* Last time send consecutive frame */
uint32_t send_timer_bs; /* Time until reception of the next FlowControl N_PDU
start at sending FF, CF, receive FC
end at receive FC */
int send_protocol_result;
uint8_t send_status;

/* receiver paramters */
uint32_t receive_arbitration_id;
/* message buffer */
Expand Down
4 changes: 2 additions & 2 deletions isotp_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/* The STmin parameter value specifies the minimum time gap allowed between
* the transmission of consecutive frame network protocol data units
*/
#define ISO_TP_DEFAULT_ST_MIN 0
#define ISO_TP_DEFAULT_ST_MIN_US 0

/* This parameter indicate how many FC N_PDU WTs can be transmitted by the
* receiver in a row.
Expand All @@ -19,7 +19,7 @@
/* Private: The default timeout to use when waiting for a response during a
* multi-frame send or receive.
*/
#define ISO_TP_DEFAULT_RESPONSE_TIMEOUT 100
#define ISO_TP_DEFAULT_RESPONSE_TIMEOUT_US 100000

/* Private: Determines if by default, padding is added to ISO-TP message frames.
*/
Expand Down
4 changes: 2 additions & 2 deletions isotp_user.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ void isotp_user_debug(const char* message, ...);
int isotp_user_send_can(const uint32_t arbitration_id,
const uint8_t* data, const uint8_t size);

/* user implemented, get millisecond */
uint32_t isotp_user_get_ms(void);
/* user implemented, get microsecond */
uint32_t isotp_user_get_us(void);

#ifdef __cplusplus
}
Expand Down

0 comments on commit b08cf6e

Please sign in to comment.