Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example/hfp_ag_demo: add an example for custom AT command #589

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions example/hfp_ag_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,126 @@ static hfp_voice_recognition_message_t msg = {
enum STATE {INIT, W4_INQUIRY_MODE_COMPLETE, ACTIVE} ;
enum STATE state = INIT;

#define IPHONEACCEV_BATTERY_LEVEL 1
#define IPHONEACCEV_BATTERY_CHARGING 2

typedef enum {
HFP_COMMAND_AT_IPHONEACCEV,
} hfp_ag_at_command_id_t;

static hfp_custom_at_command_t hfp_ag_at_table[] = {
{NULL, "AT+IPHONEACCEV=", HFP_COMMAND_AT_IPHONEACCEV}
};

static int hfp_subevent_custom_at_command_iphoneaccev_parse_key_value(int key, int val)
{
switch (key) {
case IPHONEACCEV_BATTERY_LEVEL:
printf("Received BATTERY LEVEL %d%%.\n", (val + 1) * 10);
break;

case IPHONEACCEV_BATTERY_CHARGING:
printf("Received BATTERY CHARGED %d.\n", val);
break;

default:
printf("Received unknown AT+IPHONEACCEV command %d.\n", key);
return -1;
}

return 0;
}

static int hfp_subevent_custom_at_command_iphoneaccev_handler(const char * string)
{
char *str;
char *temp, *arr;
int key = -1, val = -1;
int key_num = 0;

if (!string || strlen(string) < strlen("AT+IPHONEACCEV=")) {
return (-1);
}

str = malloc(strlen(string) + 1);
memset(str, 0 , strlen(string) + 1);
strcpy(str, string);

temp = str + sizeof("AT+IPHONEACCEV=") - 1;
arr = temp;

/* parse number of key-value pairs */
while (*temp != '\0') {
if (*temp == ',') {
*temp = '\0';
temp++;
key_num = strtol(arr, NULL, 10);
arr = temp;
break;
} else {
temp++;
}
}

if (key_num == 0) {
free(str);
return -1;
}

/* parse key-value pairs */
while (*temp != '\0') {
if (*temp == ',') {
*temp = '\0';
temp++;
if (key == -1) {
/* parse key */
key = strtol(arr, NULL, 10);
} else {
/* parse value */
val = strtol(arr, NULL, 10);
hfp_subevent_custom_at_command_iphoneaccev_parse_key_value(key, val);
key = -1; val = -1;
}
arr = temp;
} else {
temp++;
}
}

/* parse last key value */
if (key != -1) {
val = strtol(arr, NULL, 10);
hfp_subevent_custom_at_command_iphoneaccev_parse_key_value(key, val);
}

free(str);
return 0;
}

static void hfp_subevent_custom_at_command_handler(uint8_t * event)
{
uint16_t command_id;
command_id = hfp_subevent_custom_at_command_get_command_id(event);
switch (command_id) {
case HFP_COMMAND_AT_IPHONEACCEV:
hfp_subevent_custom_at_command_iphoneaccev_handler(hfp_subevent_custom_at_command_get_command_string(event));
break;

default:
break;
}
}

static void hfp_subevent_custom_command_register(void)
{
int i;
int len = sizeof(hfp_ag_at_table) / sizeof(hfp_ag_at_table[0]);

for (i = 0; i < len; i++) {
hfp_register_custom_ag_command(&hfp_ag_at_table[i]);
}
}

static void dump_supported_codecs(void){
printf("Supported codecs: CVSD");
if (hci_extended_sco_link_supported()) {
Expand Down Expand Up @@ -678,6 +798,9 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * even
break;
}
break;
case HFP_SUBEVENT_CUSTOM_AT_COMMAND:
hfp_subevent_custom_at_command_handler(event);
break;
default:
break;
}
Expand Down Expand Up @@ -767,6 +890,9 @@ int btstack_main(int argc, const char * argv[]){

// register for HFP events
hfp_ag_register_packet_handler(&packet_handler);

// register custom command
hfp_subevent_custom_command_register();

// parse human readable Bluetooth address
sscanf_bd_addr(device_addr_string, device_addr);
Expand Down
1 change: 1 addition & 0 deletions src/classic/hfp_ag.c
Original file line number Diff line number Diff line change
Expand Up @@ -2573,6 +2573,7 @@ static void hfp_ag_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t
hfp_parser_reset_line_buffer(hfp_connection);
log_info("Custom AT Command ID 0x%04x", hfp_connection->custom_at_command_id);
hfp_ag_emit_custom_command_event(hfp_connection);
hfp_ag_queue_ok(hfp_connection);
break;
case HFP_CMD_UNKNOWN:
hfp_connection->command = HFP_CMD_NONE;
Expand Down