hfp: replace strncpy with btstack_strcpy

This commit is contained in:
Matthias Ringwald 2022-01-17 16:44:47 +01:00
parent de22414a4c
commit c10fde09c0
3 changed files with 76 additions and 96 deletions

View File

@ -472,14 +472,14 @@ void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subt
#else
uint8_t event[40];
#endif
uint16_t string_len = btstack_min(strlen(value), sizeof(event) - 6);
event[0] = HCI_EVENT_HFP_META;
event[1] = sizeof(event) - 2;
event[1] = 4 + string_len;
event[2] = event_subtype;
little_endian_store_16(event, 3, hfp_connection->acl_handle);
uint16_t size = btstack_min(strlen(value), sizeof(event) - 6);
strncpy((char*)&event[5], value, size);
event[5 + size] = 0;
hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
memcpy((char*)&event[5], value, string_len);
event[5 + string_len] = 0;
hfp_emit_event_for_context(hfp_connection, event, 6 + string_len);
}
btstack_linked_list_t * hfp_get_connections(void){
@ -1403,15 +1403,12 @@ static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int
switch (hfp_connection->command){
case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
strncpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
hfp_connection->network_operator.name[HFP_MAX_NETWORK_OPERATOR_NAME_SIZE - 1] = 0;
log_info("name %s\n", hfp_connection->line_buffer);
btstack_strcpy(hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE, (char *)hfp_connection->line_buffer);
break;
case HFP_CMD_RETRIEVE_AG_INDICATORS:
hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer);
hfp_next_indicators_index(hfp_connection);
hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index;
log_info("%s)\n", hfp_connection->line_buffer);
break;
default:
break;
@ -1478,8 +1475,7 @@ static void parse_sequence(hfp_connection_t * hfp_connection){
break;
case 1:
// <number>: Quoted string containing the phone number in the format specified by <type>.
strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer);
break;
case 2:
/*
@ -1525,8 +1521,7 @@ static void parse_sequence(hfp_connection_t * hfp_connection){
hfp_connection->clcc_mpty = value;
break;
case 5:
strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer);
break;
case 6:
value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
@ -1577,8 +1572,7 @@ static void parse_sequence(hfp_connection_t * hfp_connection){
hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
break;
case HFP_CMD_RETRIEVE_AG_INDICATORS:
strncpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, (char *)hfp_connection->line_buffer, HFP_MAX_INDICATOR_DESC_SIZE);
hfp_connection->ag_indicators[hfp_connection->parser_item_index].name[HFP_MAX_INDICATOR_DESC_SIZE-1] = 0;
btstack_strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, HFP_MAX_INDICATOR_DESC_SIZE, (char *)hfp_connection->line_buffer);
hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
break;
@ -1666,8 +1660,7 @@ static void parse_sequence(hfp_connection_t * hfp_connection){
case HFP_CMD_AG_SENT_PHONE_NUMBER:
case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
case HFP_CMD_AG_SENT_CLIP_INFORMATION:
strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
btstack_strcpy((char *)hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer);
break;
case HFP_CMD_CALL_HOLD:
hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0';

View File

@ -243,12 +243,8 @@ static void hfp_gsm_set_clip(int index_in_table, uint8_t type, const char * numb
if (number_str_len == 0) return;
hfp_gsm_model_calls[index_in_table].clip_type = type;
int clip_number_size = btstack_min(number_str_len, HFP_GSM_MAX_CALL_NUMBER_SIZE - 1);
strncpy(hfp_gsm_model_calls[index_in_table].clip_number, number, clip_number_size);
hfp_gsm_model_calls[index_in_table].clip_number[clip_number_size] = '\0';
strncpy(hfp_gsm_model_last_dialed_number, number, clip_number_size);
hfp_gsm_model_last_dialed_number[clip_number_size] = '\0';
btstack_strcpy(hfp_gsm_model_calls[index_in_table].clip_number, HFP_GSM_MAX_CALL_NUMBER_SIZE, number);
btstack_strcpy(hfp_gsm_model_last_dialed_number, HFP_GSM_MAX_CALL_NUMBER_SIZE, number);
hfp_gsm_model_clip_type = 0;
memset(hfp_gsm_model_clip_number, 0, sizeof(hfp_gsm_model_clip_number));
}
@ -295,8 +291,7 @@ char * hfp_gsm_last_dialed_number(void){
}
void hfp_gsm_set_last_dialed_number(const char* number){
strncpy(hfp_gsm_model_last_dialed_number, number, sizeof(hfp_gsm_model_last_dialed_number));
hfp_gsm_model_last_dialed_number[sizeof(hfp_gsm_model_last_dialed_number) - 1] = 0;
btstack_strcpy(hfp_gsm_model_last_dialed_number, sizeof(hfp_gsm_model_last_dialed_number), number);
}
hfp_gsm_call_t * hfp_gsm_call(int call_index){
@ -594,9 +589,7 @@ void hfp_gsm_handler(hfp_ag_call_event_t event, uint8_t index, uint8_t type, con
}
hfp_gsm_model_clip_type = type;
strncpy(hfp_gsm_model_clip_number, number, sizeof(hfp_gsm_model_clip_number));
hfp_gsm_model_clip_number[sizeof(hfp_gsm_model_clip_number) - 1] = '\0';
btstack_strcpy(hfp_gsm_model_clip_number, sizeof(hfp_gsm_model_clip_number), number);
break;
default:
break;

View File

@ -136,111 +136,105 @@ static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t
return NULL;
}
/* emit functinos */
/* emit functions */
static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){
if (hfp_hf_callback == NULL) return;
uint8_t event[33];
uint16_t bnip_number_len = btstack_min(strlen(hfp_connection->bnip_number), sizeof(hfp_connection->bnip_number)-1);
uint8_t event[7 + sizeof(hfp_connection->bnip_number)];
event[0] = HCI_EVENT_HFP_META;
event[1] = sizeof(event) - 2;
event[1] = 6 + bnip_number_len;
event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION;
little_endian_store_16(event, 3, hfp_connection->acl_handle);
event[5] = status;
event[6] = hfp_connection->bnip_type;
uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 8);
strncpy((char*)&event[7], hfp_connection->bnip_number, size);
event[7 + size] = 0;
(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
memcpy(&event[7], hfp_connection->bnip_number, bnip_number_len);
event[7 + bnip_number_len] = 0;
(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 8 + bnip_number_len);
}
static void hfp_hf_emit_type_and_number(const hfp_connection_t * hfp_connection, uint8_t event_subtype){
if (hfp_hf_callback == NULL) return;
uint8_t event[32];
uint16_t bnip_number_len = btstack_min(strlen(hfp_connection->bnip_number), sizeof(hfp_connection->bnip_number)-1);
uint8_t event[6 + sizeof(hfp_connection->bnip_number)];
event[0] = HCI_EVENT_HFP_META;
event[1] = sizeof(event) - 2;
event[1] = 5 + bnip_number_len;
event[2] = event_subtype;
little_endian_store_16(event, 3, hfp_connection->acl_handle);
event[5] = hfp_connection->bnip_type;
uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 7);
strncpy((char*)&event[6], hfp_connection->bnip_number, size);
event[6 + size] = 0;
(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
memcpy(&event[6], hfp_connection->bnip_number, bnip_number_len);
event[6 + bnip_number_len] = 0;
(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 7 + bnip_number_len);
}
static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){
if (hfp_hf_callback == NULL) return;
uint8_t event[38];
int pos = 0;
event[pos++] = HCI_EVENT_HFP_META;
event[pos++] = sizeof(event) - 2;
event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
little_endian_store_16(event, pos, hfp_connection->acl_handle);
pos += 2;
event[pos++] = hfp_connection->clcc_idx;
event[pos++] = hfp_connection->clcc_dir;
event[pos++] = hfp_connection->clcc_status;
event[pos++] = hfp_connection->clcc_mode;
event[pos++] = hfp_connection->clcc_mpty;
event[pos++] = hfp_connection->bnip_type;
uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - pos - 1);
strncpy((char*)&event[pos], hfp_connection->bnip_number, size);
pos += size;
event[pos++] = 0;
(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
uint16_t bnip_number_len = btstack_min(strlen(hfp_connection->bnip_number), sizeof(hfp_connection->bnip_number)-1);
uint8_t event[11 + sizeof(hfp_connection->bnip_number)];
event[0] = HCI_EVENT_HFP_META;
event[1] = 10 + bnip_number_len;
event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
little_endian_store_16(event, 3, hfp_connection->acl_handle);
event[4] = hfp_connection->clcc_idx;
event[5] = hfp_connection->clcc_dir;
event[6] = hfp_connection->clcc_status;
event[7] = hfp_connection->clcc_mode;
event[8] = hfp_connection->clcc_mpty;
event[9] = hfp_connection->bnip_type;
memcpy(&event[10], hfp_connection->bnip_number, bnip_number_len);
event[11+bnip_number_len] = 0;
(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 12+bnip_number_len);
}
static void hfp_emit_ag_indicator_mapping_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
if (hfp_hf_callback == NULL) return;
uint8_t event[8+HFP_MAX_INDICATOR_DESC_SIZE+1];
int pos = 0;
event[pos++] = HCI_EVENT_HFP_META;
event[pos++] = sizeof(event) - 2;
event[pos++] = HFP_SUBEVENT_AG_INDICATOR_MAPPING;
little_endian_store_16(event, pos, hfp_connection->acl_handle);
pos += 2;
event[pos++] = indicator->index;
event[pos++] = indicator->min_range;
event[pos++] = indicator->max_range;
strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE);
pos += HFP_MAX_INDICATOR_DESC_SIZE;
event[pos] = 0;
(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
uint8_t event[8 + HFP_MAX_INDICATOR_DESC_SIZE];
uint16_t indicator_len = btstack_min(strlen(indicator->name), HFP_MAX_INDICATOR_DESC_SIZE-1);
event[0] = HCI_EVENT_HFP_META;
event[1] = 7 + indicator_len;
event[2] = HFP_SUBEVENT_AG_INDICATOR_MAPPING;
little_endian_store_16(event, 3, hfp_connection->acl_handle);
event[5] = indicator->index;
event[6] = indicator->min_range;
event[7] = indicator->max_range;
memcpy(&event[8], indicator->name, indicator_len);
event[8+indicator_len] = 0;
(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 9 + indicator_len);
}
static void hfp_emit_ag_indicator_status_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
if (hfp_hf_callback == NULL) return;
uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE+1];
int pos = 0;
event[pos++] = HCI_EVENT_HFP_META;
event[pos++] = sizeof(event) - 2;
event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
little_endian_store_16(event, pos, hfp_connection->acl_handle);
pos += 2;
event[pos++] = indicator->index;
event[pos++] = indicator->status;
event[pos++] = indicator->min_range;
event[pos++] = indicator->max_range;
event[pos++] = indicator->mandatory;
event[pos++] = indicator->enabled;
event[pos++] = indicator->status_changed;
strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE);
pos += HFP_MAX_INDICATOR_DESC_SIZE;
event[pos] = 0;
(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE];
uint16_t indicator_len = btstack_min(strlen(indicator->name), HFP_MAX_INDICATOR_DESC_SIZE-1);
event[0] = HCI_EVENT_HFP_META;
event[1] = 11 + indicator_len;
event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
little_endian_store_16(event, 3, hfp_connection->acl_handle);
event[5] = indicator->index;
event[6] = indicator->status;
event[7] = indicator->min_range;
event[8] = indicator->max_range;
event[9] = indicator->mandatory;
event[10] = indicator->enabled;
event[11] = indicator->status_changed;
memcpy(&event[12], indicator->name, indicator_len);
event[12+indicator_len] = 0;
(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 13 + indicator_len);
}
static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){
if (hfp_hf_callback == NULL) return;
uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
uint16_t operator_len = btstack_min(strlen(hfp_connection->network_operator.name), HFP_MAX_NETWORK_OPERATOR_NAME_SIZE-1);
uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE];
event[0] = HCI_EVENT_HFP_META;
event[1] = sizeof(event) - 2;
event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
event[2] = 6 + operator_len;
little_endian_store_16(event, 3, hfp_connection->acl_handle);
event[5] = hfp_connection->network_operator.mode;
event[6] = hfp_connection->network_operator.format;
strncpy((char*)&event[7], hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
memcpy(&event[7], hfp_connection->network_operator.name, operator_len);
event[7+operator_len] = 0;
(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 8 + operator_len);
}