mesh: simplify mesh_access_parser

This commit is contained in:
Matthias Ringwald 2019-04-22 12:46:50 +02:00
parent 24c4300fb0
commit c5fa658b92

View File

@ -813,123 +813,73 @@ static uint16_t mesh_pdu_len(mesh_pdu_t * pdu){
} }
typedef struct { typedef struct {
mesh_pdu_t * pdu; uint32_t opcode;
uint16_t pos; uint8_t * data;
uint16_t len;
} mesh_access_parser_state_t; } mesh_access_parser_state_t;
static void mesh_access_parser_skip(mesh_access_parser_state_t * state, uint16_t bytes_to_skip){
state->data += bytes_to_skip;
state->len -= bytes_to_skip;
}
static int mesh_access_parser_init(mesh_access_parser_state_t * state, mesh_pdu_t * pdu){ static int mesh_access_parser_init(mesh_access_parser_state_t * state, mesh_pdu_t * pdu){
state->pdu = pdu; switch (pdu->pdu_type){
uint32_t opcode = 0; case MESH_PDU_TYPE_TRANSPORT:
int ok = mesh_access_pdu_get_opcode(pdu, &opcode, &state->pos); state->data = ((mesh_transport_pdu_t*) pdu)->data;
if (pdu->pdu_type == MESH_PDU_TYPE_NETWORK){ state->len = ((mesh_transport_pdu_t*) pdu)->len;
state->pos += 10; break;
case MESH_PDU_TYPE_NETWORK:
state->data = &((mesh_transport_pdu_t*) pdu)->data[10];
state->len = ((mesh_network_pdu_t *) pdu)->len - 10;
break;
default:
return 0;
}
uint16_t opcode_size = 0;
int ok = mesh_access_get_opcode(state->data, state->len, &state->opcode, &opcode_size);
if (ok){
mesh_access_parser_skip(state, opcode_size);
} }
return ok; return ok;
} }
static void mesh_access_parser_skip(mesh_access_parser_state_t * state, uint16_t bytes_to_skip){
state->pos += bytes_to_skip;
}
static uint16_t mesh_access_parser_available(mesh_access_parser_state_t * state){ static uint16_t mesh_access_parser_available(mesh_access_parser_state_t * state){
switch (state->pdu->pdu_type){ return state->len;
case MESH_PDU_TYPE_TRANSPORT:
return ((mesh_transport_pdu_t *) state->pdu)->len - state->pos;
case MESH_PDU_TYPE_NETWORK:
return ((mesh_network_pdu_t *) state->pdu)->len - state->pos;
default:
return 0;
}
} }
static uint8_t mesh_access_parser_get_u8(mesh_access_parser_state_t * state){ static uint8_t mesh_access_parser_get_u8(mesh_access_parser_state_t * state){
switch (state->pdu->pdu_type){ uint8_t value = *state->data;
case MESH_PDU_TYPE_TRANSPORT: mesh_access_parser_skip(state, 1);
return ((mesh_transport_pdu_t *) state->pdu)->data[state->pos++]; return value;
case MESH_PDU_TYPE_NETWORK:
return ((mesh_network_pdu_t *) state->pdu)->data[state->pos++];
default:
return 0;
}
} }
static uint16_t mesh_access_parser_get_u16(mesh_access_parser_state_t * state){ static uint16_t mesh_access_parser_get_u16(mesh_access_parser_state_t * state){
uint16_t value; uint16_t value = little_endian_read_16(state->data, 0);
switch (state->pdu->pdu_type){ mesh_access_parser_skip(state, 2);
case MESH_PDU_TYPE_TRANSPORT:
value = little_endian_read_16( ((mesh_transport_pdu_t *) state->pdu)->data, state->pos);
break;
case MESH_PDU_TYPE_NETWORK:
value = little_endian_read_16( ((mesh_network_pdu_t *) state->pdu)->data, state->pos);
break;
default:
value = 0;
break;
}
state->pos += 2;
return value; return value;
} }
static uint32_t mesh_access_parser_get_u24(mesh_access_parser_state_t * state){ static uint32_t mesh_access_parser_get_u24(mesh_access_parser_state_t * state){
uint32_t value; uint32_t value = little_endian_read_24(state->data, 0);
switch (state->pdu->pdu_type){ mesh_access_parser_skip(state, 3);
case MESH_PDU_TYPE_TRANSPORT:
value = little_endian_read_24( ((mesh_transport_pdu_t *) state->pdu)->data, state->pos);
break;
case MESH_PDU_TYPE_NETWORK:
value = little_endian_read_24( ((mesh_network_pdu_t *) state->pdu)->data, state->pos);
break;
default:
value = 0;
break;
}
state->pos += 3;
return value; return value;
} }
static uint32_t mesh_access_parser_get_u32(mesh_access_parser_state_t * state){ static uint32_t mesh_access_parser_get_u32(mesh_access_parser_state_t * state){
uint32_t value; uint32_t value = little_endian_read_24(state->data, 0);
switch (state->pdu->pdu_type){ mesh_access_parser_skip(state, 4);
case MESH_PDU_TYPE_TRANSPORT:
value = little_endian_read_32( ((mesh_transport_pdu_t *) state->pdu)->data, state->pos);
break;
case MESH_PDU_TYPE_NETWORK:
value = little_endian_read_32( ((mesh_network_pdu_t *) state->pdu)->data, state->pos);
break;
default:
value = 0;
break;
}
state->pos += 4;
return value; return value;
} }
static void mesh_access_parser_get_u128(mesh_access_parser_state_t * state, uint8_t * dest){ static void mesh_access_parser_get_u128(mesh_access_parser_state_t * state, uint8_t * dest){
switch (state->pdu->pdu_type){ reverse_128( state->data, dest);
case MESH_PDU_TYPE_TRANSPORT: mesh_access_parser_skip(state, 16);
reverse_128( ((mesh_transport_pdu_t *) state->pdu)->data, dest);
break;
case MESH_PDU_TYPE_NETWORK:
reverse_128( ((mesh_network_pdu_t *) state->pdu)->data, dest);
break;
default:
break;
}
state->pos += 16;
} }
static void mesh_access_parser_get_label_uuid(mesh_access_parser_state_t * state, uint8_t * dest){ static void mesh_access_parser_get_label_uuid(mesh_access_parser_state_t * state, uint8_t * dest){
switch (state->pdu->pdu_type){ memcpy( dest, state->data, 16);
case MESH_PDU_TYPE_TRANSPORT: mesh_access_parser_skip(state, 16);
memcpy( dest, ((mesh_transport_pdu_t *) state->pdu)->data, 16);
break;
case MESH_PDU_TYPE_NETWORK:
memcpy( dest, ((mesh_network_pdu_t *) state->pdu)->data, 16);
break;
default:
break;
}
state->pos += 16;
} }
// message builder // message builder