Skip to content

Commit

Permalink
Merge pull request #43 from samsondw/customServiceHandler
Browse files Browse the repository at this point in the history
Add support for handling non-implemented services via custom service event.
  • Loading branch information
driftregion authored Nov 3, 2024
2 parents 4623fc1 + 5f9e104 commit 251b16f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,7 @@ 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) {
return NegativeResponse(r, kServiceNotSupported);
}
assert(service);
if (NULL == srv->fn) return NegativeResponse(r, kServiceNotSupported);
assert(srv->fn); // service handler functions will call srv->fn. it must be valid

switch (sid) {
Expand All @@ -840,6 +837,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 @@ -866,12 +864,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 @@ -883,7 +881,22 @@ static uint8_t evaluateServiceResponse(UDSServer_t *srv, UDSReq_t *r) {
case kSID_SECURED_DATA_TRANSMISSION:
case kSID_RESPONSE_ON_EVENT:
default: {
response = kServiceNotSupported;
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_SRV_EVT_CUSTOM, &args);
if (kPositiveResponse != response) return NegativeResponse(r, response);
}
break;
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,12 @@ 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);
1 change: 1 addition & 0 deletions src/uds.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum UDSServerEvent {
UDS_SRV_EVT_RequestFileTransfer, // UDSRequestFileTransferArgs_t *
UDS_SRV_EVT_SessionTimeout, // NULL
UDS_SRV_EVT_DoScheduledReset, // enum UDSEcuResetType *
UDS_SRV_EVT_CUSTOM, // UDSCustomArgs_t *
UDS_SRV_EVT_Err, // UDSErr_t *
UDS_EVT_IDLE,
UDS_EVT_RESP_RECV,
Expand Down

0 comments on commit 251b16f

Please sign in to comment.