diff --git a/impl/src/layer2/connection.c b/impl/src/layer2/connection.c index 87e00ab..ea25c51 100644 --- a/impl/src/layer2/connection.c +++ b/impl/src/layer2/connection.c @@ -29,6 +29,10 @@ result_t connection_init(connection_ctx_t *ctx, const ham64_t *my_addr, const ha void connection_destroy(connection_ctx_t *ctx) { + if(ctx->conn_state == CONN_STATE_UNINITIALIZED) { + return; + } + ctx->conn_state = CONN_STATE_UNINITIALIZED; packet_queue_destroy(&ctx->packet_queue); } @@ -36,6 +40,20 @@ void connection_destroy(connection_ctx_t *ctx) result_t connection_handle_packet(connection_ctx_t *ctx, const uint8_t *buf, size_t buf_len) { + // check the connection state + switch(ctx->conn_state) { + case CONN_STATE_UNINITIALIZED: + case CONN_STATE_INITIALIZED: + case CONN_STATE_CLOSED: + LOG(LVL_ERR, "Trying to pass packet to connection in state %u", ctx->conn_state); + return ERR_INVALID_STATE; + + case CONN_STATE_CONNECTING: + case CONN_STATE_ESTABLISHED: + // in these states, packets can be handled + break; + } + // check the CRC size_t packet_size = buf_len - crc_sizeof_key(PAYLOAD_CRC_SCHEME); @@ -142,6 +160,20 @@ uint8_t connection_get_last_acked_seq(const connection_ctx_t *ctx) result_t connection_enqueue_packet(connection_ctx_t *ctx, uint8_t *buf, size_t buf_len) { + // check the connection state + switch(ctx->conn_state) { + case CONN_STATE_UNINITIALIZED: + case CONN_STATE_INITIALIZED: + case CONN_STATE_CLOSED: + case CONN_STATE_CONNECTING: + LOG(LVL_ERR, "Trying to enqueue packet in inactive state %u", ctx->conn_state); + return ERR_INVALID_STATE; + + case CONN_STATE_ESTABLISHED: + // in these states, packets can be handled + break; + } + layer2_packet_header_t header; if(packet_queue_get_free_space(&ctx->packet_queue) == 0) { @@ -179,6 +211,20 @@ result_t connection_enqueue_packet(connection_ctx_t *ctx, uint8_t *buf, size_t b result_t connection_add_empty_packet(connection_ctx_t *ctx, bool tx_request) { + // check the connection state + switch(ctx->conn_state) { + case CONN_STATE_UNINITIALIZED: + case CONN_STATE_INITIALIZED: + case CONN_STATE_CLOSED: + case CONN_STATE_CONNECTING: + LOG(LVL_ERR, "Trying to add empty packet in inactive state %u", ctx->conn_state); + return ERR_INVALID_STATE; + + case CONN_STATE_ESTABLISHED: + // in these states, packets can be handled + break; + } + layer2_packet_header_t header; header.dst_addr.addr[0] = 0xFFFF; @@ -200,6 +246,20 @@ result_t connection_add_empty_packet(connection_ctx_t *ctx, bool tx_request) size_t connection_encode_next_packet(connection_ctx_t *ctx, uint8_t ack_seq_nr, uint8_t *buf, size_t buf_len) { + // check the connection state + switch(ctx->conn_state) { + case CONN_STATE_UNINITIALIZED: + case CONN_STATE_INITIALIZED: + case CONN_STATE_CLOSED: + LOG(LVL_ERR, "Trying to encode packet in inactive state %u", ctx->conn_state); + return ERR_INVALID_STATE; + + case CONN_STATE_CONNECTING: + case CONN_STATE_ESTABLISHED: + // in these states, packets may be present for transmission + break; + } + const packet_queue_entry_t *entry = packet_queue_get(&ctx->packet_queue, ctx->next_packet_index); if(!entry) { @@ -245,6 +305,8 @@ void connection_restart_tx(connection_ctx_t *ctx) void connection_tx_clean_empty_packet(connection_ctx_t *ctx) { + assert(ctx->conn_state != CONN_STATE_UNINITIALIZED); + const packet_queue_entry_t *entry = packet_queue_get(&ctx->packet_queue, 0); if(entry && entry->header.msg_type == L2_MSG_TYPE_EMPTY) { packet_queue_delete(&ctx->packet_queue, 1); @@ -258,6 +320,20 @@ void connection_tx_clean_empty_packet(connection_ctx_t *ctx) void connection_handle_ack(connection_ctx_t *ctx, uint8_t acked_seq) { + // check the connection state + switch(ctx->conn_state) { + case CONN_STATE_UNINITIALIZED: + case CONN_STATE_INITIALIZED: + case CONN_STATE_CLOSED: + case CONN_STATE_CONNECTING: + LOG(LVL_ERR, "Trying to call connection_handle_ack() in inactive state %u", ctx->conn_state); + return; + + case CONN_STATE_ESTABLISHED: + // in these states, packets may be present for transmission + break; + } + ctx->next_packet_index = 0; size_t packets_to_remove = 0; @@ -292,6 +368,8 @@ void connection_handle_ack(connection_ctx_t *ctx, uint8_t acked_seq) bool connection_can_transmit(const connection_ctx_t *ctx) { + assert(ctx->conn_state != CONN_STATE_UNINITIALIZED); + return (packet_queue_get_used_space(&ctx->packet_queue) != 0) && (packet_queue_get(&ctx->packet_queue, ctx->next_packet_index) != NULL); }