hfp: rewrite parse indicator

This commit is contained in:
Milanka Ringwald 2015-08-14 14:48:02 +02:00
parent b5b7d6dda3
commit 99b9166836

View File

@ -497,11 +497,14 @@ uint32_t fromBinary(char *s) {
return (uint32_t) strtol(s, NULL, 2); return (uint32_t) strtol(s, NULL, 2);
} }
static void hfp_parser_write_buffer(hfp_connection_t * context, uint8_t byte){ static void hfp_parser_store_byte(hfp_connection_t * context, uint8_t byte){
// TODO: add limit // TODO: add limit
context->line_buffer[context->line_size++] = byte; context->line_buffer[context->line_size++] = byte;
context->line_buffer[context->line_size] = 0; context->line_buffer[context->line_size] = 0;
} }
static int hfp_parser_buffer_empty(hfp_connection_t * context){
return context->line_size == 0;
}
static int hfp_parser_reached_end_of_line(uint8_t byte){ static int hfp_parser_reached_end_of_line(uint8_t byte){
return byte == '\n' || byte == '\r'; return byte == '\n' || byte == '\r';
@ -511,8 +514,11 @@ static int hfp_parser_reached_sequence_separator(uint8_t byte){
return byte == ','; return byte == ',';
} }
static int hfp_parser_item_done(uint8_t byte){ static int hfp_parser_found_separator(hfp_connection_t * context, uint8_t byte){
return byte == ',' || byte == '\n' || byte == '\r'; int found_separator = byte == ',' || byte == '\n' || byte == '\r' ||
byte == ')' || byte == '(' || byte == ':' ||
byte == '?' || byte == '-' || byte == '"';
return found_separator;
} }
static void hfp_parser_next_state(hfp_connection_t * context, uint8_t byte){ static void hfp_parser_next_state(hfp_connection_t * context, uint8_t byte){
@ -529,15 +535,18 @@ static void hfp_parser_next_state(hfp_connection_t * context, uint8_t byte){
case HFP_PARSER_SECOND_ITEM: case HFP_PARSER_SECOND_ITEM:
context->parser_state = (hfp_parser_state_t)((int)context->parser_state + 1); context->parser_state = (hfp_parser_state_t)((int)context->parser_state + 1);
break; break;
case HFP_PARSER_THIRD_ITEM:
context->parser_state = HFP_PARSER_CMD_HEADER;
break;
case HFP_PARSER_CMD_INDICATOR_MAX_RANGE: case HFP_PARSER_CMD_INDICATOR_MAX_RANGE:
case HFP_PARSER_CMD_INDICATOR_STATUS:
context->parser_state = HFP_PARSER_CMD_SEQUENCE; context->parser_state = HFP_PARSER_CMD_SEQUENCE;
break; break;
case HFP_PARSER_CMD_SEQUENCE: case HFP_PARSER_CMD_SEQUENCE:
break; break;
case HFP_PARSER_THIRD_ITEM: case HFP_PARSER_CMD_INDICATOR_STATUS:
context->parser_state = HFP_PARSER_CMD_HEADER; context->parser_state = HFP_PARSER_CMD_HEADER;
break;
} }
} }
@ -723,10 +732,12 @@ void hfp_parse(hfp_connection_t * context, uint8_t byte){
break; break;
case HFP_PARSER_SECOND_ITEM: case HFP_PARSER_SECOND_ITEM:
if (!hfp_parser_item_done(byte)){ if (!hfp_parser_found_separator(context, byte)){
hfp_parser_write_buffer(context, byte); hfp_parser_store_byte(context, byte);
break; break;
} }
if (hfp_parser_buffer_empty(context)) break;
switch (context->command){ switch (context->command){
case HFP_CMD_QUERY_OPERATOR_SELECTION: case HFP_CMD_QUERY_OPERATOR_SELECTION:
if (context->operator_name_format == 1) { if (context->operator_name_format == 1) {
@ -746,10 +757,12 @@ void hfp_parse(hfp_connection_t * context, uint8_t byte){
break; break;
case HFP_PARSER_THIRD_ITEM: case HFP_PARSER_THIRD_ITEM:
if (!hfp_parser_item_done(byte)){ if (!hfp_parser_found_separator(context, byte)){
hfp_parser_write_buffer(context, byte); hfp_parser_store_byte(context, byte);
break; break;
} }
if (hfp_parser_buffer_empty(context)) break;
switch (context->command){ switch (context->command){
case HFP_CMD_QUERY_OPERATOR_SELECTION: case HFP_CMD_QUERY_OPERATOR_SELECTION:
if (context->operator_name == 1){ if (context->operator_name == 1){
@ -789,48 +802,43 @@ void hfp_parse(hfp_connection_t * context, uint8_t byte){
break; break;
case HFP_PARSER_CMD_INDICATOR_NAME: // parse indicator name case HFP_PARSER_CMD_INDICATOR_NAME: // parse indicator name
//if (byte == '\n' || byte == '\r') return; if (!hfp_parser_found_separator(context, byte)){
if (byte == '"'){ hfp_parser_store_byte(context, byte);
context->line_buffer[context->line_size] = 0;
context->line_size = 0;
printf("Indicator %d: %s (", context->ag_indicators_nr+1, context->line_buffer);
strcpy((char *)context->ag_indicators[context->parser_item_index].name, (char *)context->line_buffer);
context->ag_indicators[context->parser_item_index].index = context->parser_item_index+1;
break; break;
} }
if (byte == '('){ // parse indicator range if (hfp_parser_buffer_empty(context)) break;
context->parser_state = HFP_PARSER_CMD_INDICATOR_MIN_RANGE;
break; strcpy((char *)context->ag_indicators[context->parser_item_index].name, (char *)context->line_buffer);
} context->ag_indicators[context->parser_item_index].index = context->parser_item_index+1;
context->line_buffer[context->line_size++] = byte; printf("Indicator %d: %s (", context->ag_indicators_nr+1, context->line_buffer);
hfp_parser_next_state(context, byte);
break; break;
case HFP_PARSER_CMD_INDICATOR_MIN_RANGE: case HFP_PARSER_CMD_INDICATOR_MIN_RANGE:
//if (byte == '\n' || byte == '\r') return; if (!hfp_parser_found_separator(context, byte)){
if (byte == ',' || byte == '-'){ // end min_range hfp_parser_store_byte(context, byte);
context->parser_state = HFP_PARSER_CMD_INDICATOR_MAX_RANGE;
context->line_buffer[context->line_size] = 0;
printf("%d, ", atoi((char *)&context->line_buffer[0]));
context->ag_indicators[context->parser_item_index].min_range = atoi((char *)context->line_buffer);
context->line_size = 0;
break; break;
} }
// min. range if (hfp_parser_buffer_empty(context)) break;
context->line_buffer[context->line_size++] = byte;
context->ag_indicators[context->parser_item_index].min_range = atoi((char *)context->line_buffer);
printf("%s, ", context->line_buffer);
hfp_parser_next_state(context, byte);
break; break;
case HFP_PARSER_CMD_INDICATOR_MAX_RANGE: case HFP_PARSER_CMD_INDICATOR_MAX_RANGE:
//if (byte == '\n' || byte == '\r') return; if (!hfp_parser_found_separator(context, byte)){
if (byte == ')'){ // end max_range hfp_parser_store_byte(context, byte);
context->parser_state = HFP_PARSER_CMD_SEQUENCE;
context->line_buffer[context->line_size] = 0;
printf("%d)\n", atoi((char *)&context->line_buffer[0]));
context->ag_indicators[context->parser_item_index].max_range = atoi((char *)context->line_buffer);
context->line_size = 0;
context->parser_item_index++;
context->ag_indicators_nr = context->parser_item_index;
break; break;
} }
if (hfp_parser_buffer_empty(context)) break;
context->line_buffer[context->line_size++] = byte;
context->ag_indicators[context->parser_item_index].max_range = atoi((char *)context->line_buffer);
context->parser_item_index++;
context->ag_indicators_nr = context->parser_item_index;
printf("%s)\n", context->line_buffer);
hfp_parser_next_state(context, byte);
break; break;
} }