Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into new_client_api
Browse files Browse the repository at this point in the history
  • Loading branch information
driftregion committed Jan 24, 2025
2 parents caf3795 + 251b16f commit 7f02bea
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
27 changes: 21 additions & 6 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -830,10 +830,8 @@ static uint8_t evaluateServiceResponse(UDSServer_t *srv, UDSReq_t *r) {
uint8_t sid = r->recv_buf[0];
UDSService service = getServiceForSID(sid);

if (NULL == service || NULL == srv->fn) {
if (NULL == srv->fn)
return NegativeResponse(r, UDS_NRC_ServiceNotSupported);
}
UDS_ASSERT(service);
UDS_ASSERT(srv->fn); // service handler functions will call srv->fn. it must be valid

switch (sid) {
Expand All @@ -846,6 +844,7 @@ static uint8_t evaluateServiceResponse(UDSServer_t *srv, UDSReq_t *r) {
case kSID_ROUTINE_CONTROL:
case kSID_TESTER_PRESENT:
case kSID_CONTROL_DTC_SETTING: {
assert(service);
response = service(srv, r);

bool suppressPosRspMsgIndicationBit = r->recv_buf[1] & 0x80;
Expand All @@ -872,12 +871,12 @@ static uint8_t evaluateServiceResponse(UDSServer_t *srv, UDSReq_t *r) {
case kSID_TRANSFER_DATA:
case kSID_REQUEST_FILE_TRANSFER:
case kSID_REQUEST_TRANSFER_EXIT: {
assert(service);
response = service(srv, r);
break;
}

/* CASE Service_not_implemented */
/* shouldn't get this far as getServiceForSID(sid) will return NULL*/
/* CASE Service_optional */
case kSID_CLEAR_DIAGNOSTIC_INFORMATION:
case kSID_READ_DTC_INFORMATION:
case kSID_READ_SCALING_DATA_BY_IDENTIFIER:
Expand All @@ -889,7 +888,23 @@ static uint8_t evaluateServiceResponse(UDSServer_t *srv, UDSReq_t *r) {
case kSID_SECURED_DATA_TRANSMISSION:
case kSID_RESPONSE_ON_EVENT:
default: {
response = UDS_NRC_ServiceNotSupported;
if (service) {
response = service(srv, r);
} else { /* getServiceForSID(sid) returned NULL*/
UDSCustomArgs_t args = {
.sid = sid,
.optionRecord = &r->recv_buf[1],
.len = r->recv_len - 1,
.copyResponse = safe_copy,
};

r->send_buf[0] = UDS_RESPONSE_SID_OF(sid);
r->send_len = 1;

response = EmitEvent(srv, UDS_EVT_CUSTOM, &args);
if (UDS_PositiveResponse != response)
return NegativeResponse(r, response);
}
break;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,13 @@ typedef struct {
send in each `TransferData` request */
} UDSRequestFileTransferArgs_t;

typedef struct {
const uint16_t sid; /*! serviceIdentifier */
const uint8_t *optionRecord; /*! optional data */
const uint16_t len; /*! length of optional data */
uint8_t (*copyResponse)(UDSServer_t *srv, const void *src,
uint16_t len); /*! function for copying response data (optional) */
} UDSCustomArgs_t;

UDSErr_t UDSServerInit(UDSServer_t *srv);
void UDSServerPoll(UDSServer_t *srv);
2 changes: 2 additions & 0 deletions src/uds.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ typedef enum UDSEvent {
UDS_EVT_SessionTimeout, // NULL
UDS_EVT_DoScheduledReset, // enum UDSEcuResetType *
UDS_EVT_RequestFileTransfer, // UDSRequestFileTransferArgs_t *
UDS_EVT_CUSTOM, // UDSCustomArgs_t *

// Client Event
UDS_EVT_Poll, // NULL
UDS_EVT_SendComplete, //
UDS_EVT_ResponseReceived, //
UDS_EVT_Idle, // NULL

UDS_EVT_MAX, // unused
} UDSEvent_t;

typedef enum {
Expand Down
20 changes: 10 additions & 10 deletions test/test_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void test_0x11_good_response(void **state) {
.resp_len = 2,
.delay_ms = 0, // immediate response
});
int call_count[20] = {0};
int call_count[UDS_EVT_MAX] = {0};
e->client->fn = fn_log_call_count;
e->client->fn_data = call_count;
UDSSendECUReset(e->client, kHardReset);
Expand All @@ -105,7 +105,7 @@ void test_0x11_timeout(void **state) {
.resp_len = 2,
.delay_ms = e->client->p2_ms + 10, // timeout
});
int call_count[20] = {0};
int call_count[UDS_EVT_MAX] = {0};
e->client->fn = fn_log_call_count;
e->client->fn_data = call_count;
UDSSendECUReset(e->client, kHardReset);
Expand All @@ -122,7 +122,7 @@ void test_0x11_sid_mismatch(void **state) {
.resp_len = 2,
.delay_ms = 0, // immediate response
});
int call_count[20] = {0};
int call_count[UDS_EVT_MAX] = {0};
e->client->fn = fn_log_call_count;
e->client->fn_data = call_count;
UDSSendECUReset(e->client, kHardReset);
Expand All @@ -139,7 +139,7 @@ void test_0x11_short_response(void **state) {
.resp_len = 1,
.delay_ms = 0, // immediate response
});
int call_count[20] = {0};
int call_count[UDS_EVT_MAX] = {0};
e->client->fn = fn_log_call_count;
e->client->fn_data = call_count;
UDSSendECUReset(e->client, kHardReset);
Expand All @@ -156,7 +156,7 @@ void test_0x11_inconsistent_subfunc(void **state) {
.resp_len = 2,
.delay_ms = 0, // immediate response
});
int call_count[20] = {0};
int call_count[UDS_EVT_MAX] = {0};
e->client->fn = fn_log_call_count;
e->client->fn_data = call_count;
UDSSendECUReset(e->client, kHardReset);
Expand All @@ -173,7 +173,7 @@ void test_0x11_neg_resp(void **state) {
.resp_len = 3,
.delay_ms = 0, // immediate response
});
int call_count[20] = {0};
int call_count[UDS_EVT_MAX] = {0};
e->client->fn = fn_log_call_count;
e->client->fn_data = call_count;
UDSSendECUReset(e->client, kHardReset);
Expand All @@ -190,7 +190,7 @@ void test_0x11_rcrrp_timeout(void **state) {
.resp_len = 3,
.delay_ms = 0, // immediate response
});
int call_count[20] = {0};
int call_count[UDS_EVT_MAX] = {0};
e->client->fn = fn_log_call_count;
e->client->fn_data = call_count;
UDSSendECUReset(e->client, kHardReset);
Expand All @@ -210,7 +210,7 @@ void test_0x11_rcrrp_ok(void **state) {
.resp_len = 3,
.delay_ms = 0, // immediate response
});
int call_count[20] = {0};
int call_count[UDS_EVT_MAX] = {0};
e->client->fn = fn_log_call_count;
e->client->fn_data = call_count;
UDSSendECUReset(e->client, kHardReset);
Expand All @@ -226,7 +226,7 @@ void test_0x11_rcrrp_ok(void **state) {

void test_0x11_suppress_pos_resp(void **state) {
Env_t *e = *state;
int call_count[20] = {0};
int call_count[UDS_EVT_MAX] = {0};
e->client->fn = fn_log_call_count;
e->client->fn_data = call_count;

Expand All @@ -243,7 +243,7 @@ void test_0x11_suppress_pos_resp(void **state) {

void test_0x22_unpack_rdbi_response(void **state) {
Env_t *e = *state;
int call_count[20] = {0};
int call_count[UDS_EVT_MAX] = {0};
e->client->fn = fn_log_call_count;
e->client->fn_data = call_count;

Expand Down

0 comments on commit 7f02bea

Please sign in to comment.