diff --git a/src/tpm.c b/src/tpm.c index 14d0472..abf8e8c 100644 --- a/src/tpm.c +++ b/src/tpm.c @@ -118,7 +118,13 @@ int tpm_pay(void* fc, tolling_info_t* info, void* security_socket, uint8_t* neig type->choice.exit->choice.request->clientId = tolling->station.obu.client_id; type->choice.exit->choice.request->infoId = info->asn->id; type->choice.exit->choice.request->transactionNonce = tolling->station.obu.nonce; - //type->choice.exit->choice.request->entryProof = calloc(1, sizeof(TollingEntryProof_t)); + uint8_t b_tep[1024]; + asn_enc_rval_t e_tep = uper_encode_to_buffer(&asn_DEF_TPM, NULL, tolling->station.obu.entry_proof, b_tep, 1024); + if (e_tep.encoded == -1) { + syslog_err("[facilities] [tolling] error encoding TPM entry proof"); + return 1; + } + uper_decode_complete(NULL, &asn_DEF_TPM, (void**) &type->choice.exit->choice.request->entryProof, b_tep, (e_tep.encoded+7)/8); break; case TollType_single: type->present = TollingType_PR_single; @@ -796,11 +802,24 @@ static void veh_handle_recv(tolling_t* tolling, TPM_t* tpm_rx, void* security_so syslog_err("[facilities] [tolling] received TPM.entry is not reply"); return; } + if (tolling->station.obu.toll_type != TollingType_PR_entry) { + syslog_err("[facilities] [tolling] received TPM (%d) is not expected toll type (%d)", + TollingType_PR_entry, tolling->station.obu.toll_type); + } client_id = type_rx->choice.entry.choice.reply.clientId; nonce = type_rx->choice.entry.choice.reply.transactionNonce; info_id = type_rx->choice.entry.choice.reply.infoId; confirmation_code = type_rx->choice.entry.choice.reply.confirmationCode; + + uint8_t b_tep[1024]; + asn_enc_rval_t e_tep = uper_encode_to_buffer(&asn_DEF_TPM, NULL, tpm_rx, b_tep, 1024); + if (e_tep.encoded == -1) { + syslog_err("[facilities] [tolling] error encoding received TPM as entry proof"); + return; + } + uper_decode_complete(NULL, &asn_DEF_TPM, (void**) &tolling->station.obu.entry_proof, b_tep, (e_tep.encoded+7)/8); break; + case TollingType_PR_exit: if (!type_rx->choice.exit || type_rx->choice.exit->present != TollingExit_PR_reply @@ -808,28 +827,37 @@ static void veh_handle_recv(tolling_t* tolling, TPM_t* tpm_rx, void* security_so syslog_err("[facilities] [tolling] received TPM.exit is not reply"); return; } + if (tolling->station.obu.toll_type != TollingType_PR_exit) { + syslog_err("[facilities] [tolling] received TPM (%d) is not expected toll type (%d)", + TollingType_PR_exit, tolling->station.obu.toll_type); + return; + } client_id = type_rx->choice.exit->choice.reply.clientId; nonce = type_rx->choice.exit->choice.reply.transactionNonce; info_id = type_rx->choice.exit->choice.reply.infoId; confirmation_code = type_rx->choice.exit->choice.reply.confirmationCode; break; + case TollingType_PR_single: if (type_rx->choice.single.present != TollingSingle_PR_reply) { syslog_err("[facilities] [tolling] received TPM.single is not reply"); return; } + if (tolling->station.obu.toll_type != TollingType_PR_single) { + syslog_err("[facilities] [tolling] received TPM (%d) is not expected toll type (%d)", + TollingType_PR_single, tolling->station.obu.toll_type); + } client_id = type_rx->choice.single.choice.reply.clientId; nonce = type_rx->choice.single.choice.reply.transactionNonce; info_id = type_rx->choice.single.choice.reply.infoId; confirmation_code = type_rx->choice.single.choice.reply.confirmationCode; break; + default: syslog_err("[facilities] [tolling] received TPM has unrecognized type"); return; } - // TODO if sent ENTRY.REQUEST expect ENTRY.REPLY, if send SINGLE.REQUEST expect ENTRY.REPLY, etc - if (client_id != tolling->station.obu.client_id) { syslog_debug("[facilities] [tolling]<- received TPM.reply clientId different from ego"); return; @@ -902,9 +930,29 @@ static void veh_handle_recv(tolling_t* tolling, TPM_t* tpm_rx, void* security_so accepted = confirmation_code == TollingConfirmationCode_accepted; } - syslog_info("[facilities] [tolling] received tolling payment reply | client:%lld nonce:%ld accepted:%s", (long long) tolling->station.obu.client_id, tolling->station.obu.nonce, accepted ? "yes" : "no"); + switch (type_rx->present) { + case TollingType_PR_entry: + syslog_info("[facilities] [tolling] received entry tolling reply | client:%lld nonce:%ld accepted:%s", (long long) tolling->station.obu.client_id, tolling->station.obu.nonce, accepted ? "yes" : "no"); + break; + + case TollingType_PR_exit: + syslog_info("[facilities] [tolling] received exit tolling reply | client:%lld nonce:%ld accepted:%s", (long long) tolling->station.obu.client_id, tolling->station.obu.nonce, accepted ? "yes" : "no"); + tolling->station.obu.toll_type = -1; + tolling->station.obu.active = false; + ASN_STRUCT_FREE(asn_DEF_TPM, tolling->station.obu.entry_proof); + tolling->station.obu.entry_proof = NULL; + break; + + case TollingType_PR_single: + syslog_info("[facilities] [tolling] received single tolling reply | client:%lld nonce:%ld accepted:%s", (long long) tolling->station.obu.client_id, tolling->station.obu.nonce, accepted ? "yes" : "no"); + tolling->station.obu.toll_type = -1; + tolling->station.obu.active = false; + break; + + default: + break; + } - tolling->station.obu.active = false; cleanup: ASN_STRUCT_FREE(asn_DEF_SecurityRequest, sreq); @@ -983,7 +1031,7 @@ int tpm_recv(void* fc, TPM_t* tpm_rx, void* security_socket, uint8_t* neighbour, break; } break; - + // Single case TollingType_PR_single: switch (tpm_rx->tpm->tollingType->choice.single.present) { case TollingSingle_PR_request: diff --git a/src/tpm.h b/src/tpm.h index 951be34..dab1841 100644 --- a/src/tpm.h +++ b/src/tpm.h @@ -35,7 +35,7 @@ typedef struct tolling { // OBU struct { bool active; - uint8_t toll_type; + int8_t toll_type; uint64_t nonce; uint64_t client_id; uint64_t tls_conn_id;