From b60a2e308ff79626840e2bd98480d0fdd3800eef Mon Sep 17 00:00:00 2001
From: Milanka Ringwald <mila@ringwald.ch>
Date: Wed, 17 Feb 2016 14:34:58 +0100
Subject: [PATCH] hfp hf docu

---
 src/hfp.c                     |   1 +
 src/hfp.h                     |   6 +-
 src/hfp_hf.c                  |  32 +++++---
 src/hfp_hf.h                  | 139 ++++++++++++++++++++++++----------
 test/hfp/hfp_hf_client_test.c |  14 +++-
 test/pts/hfp_hf_test.c        |   7 +-
 6 files changed, 141 insertions(+), 58 deletions(-)

diff --git a/src/hfp.c b/src/hfp.c
index 825c8663a..a8537d462 100644
--- a/src/hfp.c
+++ b/src/hfp.c
@@ -1310,6 +1310,7 @@ void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_
 
 void hfp_release_service_level_connection(hfp_connection_t * context){
     if (!context) return;
+    hfp_release_audio_connection(context);
     
     if (context->state < HFP_W4_RFCOMM_CONNECTED){
         context->state = HFP_IDLE;
diff --git a/src/hfp.h b/src/hfp.h
index 94c13f2a8..f257fab8e 100644
--- a/src/hfp.h
+++ b/src/hfp.h
@@ -37,13 +37,13 @@
 
 // *****************************************************************************
 //
-//  HFP Hands-Free (HF) unit and Audio-Gateway Commons
+//  HFP Hands-Free (HF) unit and Audio Gateway Commons
 //
 // *****************************************************************************
 
 
-#ifndef btstack_hfp_h
-#define btstack_hfp_h
+#ifndef __BTSTACK_HFP_H
+#define __BTSTACK_HFP_H
 
 #include "hci.h"
 #include "sdp_query_rfcomm.h"
diff --git a/src/hfp_hf.c b/src/hfp_hf.c
index b6a2c32f3..114e29562 100644
--- a/src/hfp_hf.c
+++ b/src/hfp_hf.c
@@ -120,7 +120,7 @@ static int has_hf_indicators_feature(hfp_connection_t * connection){
 
 static void packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
 
-void hfp_hf_create_sdp_record(uint8_t * service, int rfcomm_channel_nr, const char * name, uint16_t supported_features){
+void hfp_hf_create_sdp_record(uint8_t * service, int rfcomm_channel_nr, const char * name, uint32_t supported_features){
     if (!name){
         name = default_hfp_hf_service_name;
     }
@@ -1022,7 +1022,20 @@ static void packet_handler(void * connection, uint8_t packet_type, uint16_t chan
     hfp_run();
 }
 
-void hfp_hf_set_codecs(uint8_t * codecs, int codecs_nr){
+void hfp_hf_init(uint16_t rfcomm_channel_nr){
+    l2cap_init();
+    l2cap_register_packet_handler(packet_handler);
+    rfcomm_register_packet_handler(packet_handler);
+    hfp_init(rfcomm_channel_nr);
+
+    hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
+    hfp_codecs_nr = 0;
+    hfp_indicators_nr = 0;
+    hfp_hf_speaker_gain = 9;
+    hfp_hf_microphone_gain = 9;
+}
+
+void hfp_hf_set_codecs(int codecs_nr, uint8_t * codecs){
     if (codecs_nr > HFP_MAX_NUM_CODECS){
         log_error("hfp_hf_set_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
         return;
@@ -1047,26 +1060,23 @@ void hfp_hf_set_codecs(uint8_t * codecs, int codecs_nr){
     }
 }
 
-void hfp_hf_init(uint16_t rfcomm_channel_nr, uint32_t supported_features, uint16_t * indicators, int indicators_nr, uint32_t indicators_status){
-    l2cap_init();
-    l2cap_register_packet_handler(packet_handler);
-    rfcomm_register_packet_handler(packet_handler);
-    hfp_init(rfcomm_channel_nr);
-    
+void hfp_hf_set_supported_features(uint32_t supported_features){
     hfp_supported_features = supported_features;
+}
 
+void hfp_hf_set_indicators(int indicators_nr, uint16_t * indicators){
     hfp_indicators_nr = indicators_nr;
-    hfp_indicators_status = indicators_status;
     int i;
     for (i=0; i<indicators_nr; i++){
         hfp_indicators[i] = indicators[i];
     }
 }
 
-void hfp_hf_set_supported_features(uint32_t supported_features){
-    hfp_supported_features = supported_features;
+void hfp_hf_set_indicators_status(uint32_t indicators_status){
+    hfp_indicators_status = indicators_status;
 }
 
+
 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
     hfp_establish_service_level_connection(bd_addr, SDP_HandsfreeAudioGateway);
 }
diff --git a/src/hfp_hf.h b/src/hfp_hf.h
index a9393c06d..c285282fe 100644
--- a/src/hfp_hf.h
+++ b/src/hfp_hf.h
@@ -57,182 +57,230 @@ extern "C" {
 
 /**
  * @brief Create HFP Hands-Free (HF) SDP service record. 
+ * @param service
+ * @param rfcomm_channel_nr
+ * @param name
+ * @param suported_features 32-bit bitmap, see HFP_HFSF_* values in hfp.h
  */
-void hfp_hf_create_sdp_record(uint8_t * service, int rfcomm_channel_nr, const char * name, uint16_t supported_features);
+void hfp_hf_create_sdp_record(uint8_t * service, int rfcomm_channel_nr, const char * name, uint32_t supported_features);
 
 /**
- * @brief Intialize HFP Hands-Free (HF) device. 
- * TODO:  move optional params into setters
+ * @brief Set up HFP Hands-Free (HF) device. 
+ * 
+ * @param rfcomm_channel_nr
  */
-void hfp_hf_init(uint16_t rfcomm_channel_nr, uint32_t supported_features, uint16_t * indicators, int indicators_nr, uint32_t indicators_status);
+void hfp_hf_init(uint16_t rfcomm_channel_nr);
 
-void hfp_hf_set_codecs(uint8_t * codecs, int codecs_nr);
+/**
+ * @brief Set codecs. 
+ * 
+ * @param codecs_nr
+ * @param codecs
+ */
+void hfp_hf_set_codecs(int codecs_nr, uint8_t * codecs);
 
+/**
+ * @brief Set supported feature bitmap. 
+ * 
+ * @param supported_features 32-bit bitmap, see HFP_HFSF_* values in hfp.h
+ */
 void hfp_hf_set_supported_features(uint32_t supported_features);
 
+/**
+ * @brief Set HF indicators. 
+ * 
+ * @param indicators_nr
+ * @param indicators
+ */
+void hfp_hf_set_indicators(int indicators_nr, uint16_t * indicators);
+
+/**
+ * @brief Set HF indicators status bitmap. 
+ * 
+ * @param indicators_status 32-bit bitmap, 0 - indicator is disabled, 1 - indicator is enabled
+ */
+void hfp_hf_set_indicators_status(uint32_t indicators_status);
+
 /**
  * @brief Register callback for the HFP Hands-Free (HF) client. 
  */
 void hfp_hf_register_packet_handler(hfp_callback_t callback);
 
 /**
- * @brief Establish RFCOMM connection, and perform service level connection agreement:
- * - exchange of supported features
+ * @brief Establish RFCOMM connection with the AG with given Bluetooth address, and perform service level connection agreement:
+ * - exchange supported features
  * - retrieve Audio Gateway (AG) indicators and their status 
  * - enable indicator status update in the AG
  * - notify the AG about its own available codecs, if possible
  * - retrieve the AG information describing the call hold and multiparty services, if possible
  * - retrieve which HF indicators are enabled on the AG, if possible
+ *
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr);
 
 /**
  * @brief Release the RFCOMM channel and the audio connection between the HF and the AG. 
- * TODO: trigger release of the audio connection
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_release_service_level_connection(bd_addr_t bd_addr);
 
 /**
- * @brief Deactivate/reactivate status update for all indicators in the AG.
+ * @brief Enable status update for all indicators in the AG.
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_enable_status_update_for_all_ag_indicators(bd_addr_t bd_addr);
 
+/**
+ * @brief Disable status update for all indicators in the AG.
+ * @param bd_addr Bluetooth address of the AG
+ */
 void hfp_hf_disable_status_update_for_all_ag_indicators(bd_addr_t bd_addr);
 
 /**
- * @brief Deactivate/reactivate status update for the individual indicators in the AG using bitmap.
+ * @brief Enable or disable status update for the individual indicators in the AG using bitmap.
+ *
+ * @param bd_addr Bluetooth address of the AG
+ * @param indicators_status_bitmap 32-bit bitmap, 0 - indicator is disabled, 1 - indicator is enabled
  */
 void hfp_hf_set_status_update_for_individual_ag_indicators(bd_addr_t bd_addr, uint32_t indicators_status_bitmap);
 
 
 /**
  * @brief Find out the name of the currently selected Network operator by AG. 
- * The name is restricted to max 16 characters.
+ * 
+ * The name is restricted to max 16 characters. If the operator is selected, a 
+ * HCI_EVENT_HFP_META event with HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED subtype 
+ * containing network operator mode, format and name, is sent.
+ * If no operator is selected, format and operator are omitted.
  *
- * TODO: what is the result of this?
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_query_operator_selection(bd_addr_t bd_addr);
 
 /**
- * @brief Enable/disable Extended Audio Gateway Error result codes in the AG.
+ * @brief Enable Extended Audio Gateway Error result codes in the AG.
  * Whenever there is an error relating to the functionality of the AG as a 
- * result of AT command, the AG shall send +CME ERROR:
- * - +CME ERROR: 0  - AG failure
- * - +CME ERROR: 1  - no connection to phone 
- * - +CME ERROR: 3  - operation not allowed 
- * - +CME ERROR: 4  - operation not supported 
- * - +CME ERROR: 5  - PH-SIM PIN required 
- * - +CME ERROR: 10 - SIM not inserted 
- * - +CME ERROR: 11 - SIM PIN required 
- * - +CME ERROR: 12 - SIM PUK required 
- * - +CME ERROR: 13 - SIM failure
- * - +CME ERROR: 14 - SIM busy
- * - +CME ERROR: 16 - incorrect password 
- * - +CME ERROR: 17 - SIM PIN2 required 
- * - +CME ERROR: 18 - SIM PUK2 required 
- * - +CME ERROR: 20 - memory full
- * - +CME ERROR: 21 - invalid index
- * - +CME ERROR: 23 - memory failure
- * - +CME ERROR: 24 - text string too long
- * - +CME ERROR: 25 - invalid characters in text string
- * - +CME ERROR: 26 - dial string too long
- * - +CME ERROR: 27 - invalid characters in dial string
- * - +CME ERROR: 30 - no network service
- * - +CME ERROR: 31 - network Timeout.
- * - +CME ERROR: 32 - network not allowed – Emergency calls only
+ * result of AT command, the AG shall send +CME ERROR, see hfp_cme_error_t in hfp.h
+ *
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_enable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr);
-void hfp_hf_disable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr);
+
+/**
+ * @brief Disable Extended Audio Gateway Error result codes in the AG.
+ *
+ * @param bd_addr Bluetooth address of the AG
+ */
+ void hfp_hf_disable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr);
 
 /**
  * @brief 
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_establish_audio_connection(bd_addr_t bd_addr);
 
 /**
  * @brief 
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_release_audio_connection(bd_addr_t bd_addr);
 
 /**
  * @brief 
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_answer_incoming_call(bd_addr_t bd_addr);
 
 /**
  * @brief 
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_reject_call(bd_addr_t bd_addr);
 
 /**
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_user_busy(bd_addr_t addr);
 
 /**
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_end_active_and_accept_other(bd_addr_t addr);
 
 /**
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_swap_calls(bd_addr_t addr);
 
 /**
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_join_held_call(bd_addr_t addr);
 
 /**
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_connect_calls(bd_addr_t addr);
 
 /**
  * @brief 
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_terminate_call(bd_addr_t bd_addr);
 
 /**
  * @brief 
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_dial_number(bd_addr_t bd_addr, char * number);
 
 /**
  * @brief
  * TODO: use int for number instead of string? 
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_dial_memory(bd_addr_t bd_addr, char * number);
 
 /**
  * @brief 
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_redial_last_number(bd_addr_t bd_addr);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_activate_call_waiting_notification(bd_addr_t bd_addr);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_deactivate_call_waiting_notification(bd_addr_t bd_addr);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_activate_calling_line_notification(bd_addr_t bd_addr);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_deactivate_calling_line_notification(bd_addr_t bd_addr);
 
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_activate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr);
 
@@ -243,76 +291,91 @@ void hfp_hf_deactivate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_activate_voice_recognition_notification(bd_addr_t bd_addr);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_deactivate_voice_recognition_notification(bd_addr_t bd_addr);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_set_microphone_gain(bd_addr_t bd_addr, int gain);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_set_speaker_gain(bd_addr_t bd_addr, int gain);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_send_dtmf_code(bd_addr_t bd_addr, char code);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_request_phone_number_for_voice_tag(bd_addr_t addr);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_query_current_call_status(bd_addr_t addr);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_release_call_with_index(bd_addr_t addr, int index);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_private_consultation_with_call(bd_addr_t addr, int index);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_rrh_query_status(bd_addr_t addr);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_rrh_hold_call(bd_addr_t addr);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_rrh_accept_held_call(bd_addr_t addr);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_rrh_reject_held_call(bd_addr_t addr);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_query_subscriber_number(bd_addr_t addr);
 
 /*
  * @brief
+ * @param bd_addr Bluetooth address of the AG
  */
 void hfp_hf_set_hf_indicator(bd_addr_t addr, int assigned_number, int value);
 
diff --git a/test/hfp/hfp_hf_client_test.c b/test/hfp/hfp_hf_client_test.c
index d541f0b01..56557f2b9 100644
--- a/test/hfp/hfp_hf_client_test.c
+++ b/test/hfp/hfp_hf_client_test.c
@@ -338,7 +338,7 @@ void simulate_test_sequence(hfp_test_item_t * test_item){
             sscanf(&expected_cmd[7],"%d,%d", &parsed_codecs[0], &parsed_codecs[1]);
             new_codecs[0] = parsed_codecs[0];
             new_codecs[1] = parsed_codecs[1];
-            hfp_hf_set_codecs((uint8_t*)new_codecs, 2);
+            hfp_hf_set_codecs(2, (uint8_t*)new_codecs);
             while (has_more_hfp_hf_commands()){
                 // empty rfcomm payload buffer
                 get_next_hfp_hf_command();
@@ -349,7 +349,9 @@ void simulate_test_sequence(hfp_test_item_t * test_item){
             sscanf(&expected_cmd[8],"%d", &supported_features);
             printf("Call hfp_hf_init with SF %d\n", supported_features);
             hfp_hf_release_service_level_connection(device_addr);
-            hfp_hf_init(rfcomm_channel_nr, supported_features, indicators, sizeof(indicators)/sizeof(uint16_t), 1);
+            
+            hfp_hf_set_supported_features(supported_features);
+            
             user_command('a');
             while (has_more_hfp_hf_commands()){
                 // empty rfcomm payload buffer
@@ -479,8 +481,12 @@ TEST_GROUP(HFPClient){
         stop_ringing = 0;
         call_termiated = 0;
 
-        hfp_hf_init(rfcomm_channel_nr, supported_features_with_codec_negotiation, indicators, sizeof(indicators)/sizeof(uint16_t), 1);
-        hfp_hf_set_codecs(codecs, sizeof(codecs));
+        hfp_hf_init(rfcomm_channel_nr);
+        hfp_hf_set_supported_features(supported_features_with_codec_negotiation);
+        hfp_hf_set_indicators(sizeof(indicators)/sizeof(uint16_t), indicators);
+        hfp_hf_set_indicators_status(1);
+
+        hfp_hf_set_codecs(sizeof(codecs), codecs);
     }
 
     void teardown(void){
diff --git a/test/pts/hfp_hf_test.c b/test/pts/hfp_hf_test.c
index c8cde4d85..1adbaf2e9 100644
--- a/test/pts/hfp_hf_test.c
+++ b/test/pts/hfp_hf_test.c
@@ -534,8 +534,11 @@ int btstack_main(int argc, const char * argv[]){
     rfcomm_init();
     
     // hfp_hf_init(rfcomm_channel_nr, HFP_DEFAULT_HF_SUPPORTED_FEATURES, codecs, sizeof(codecs), indicators, sizeof(indicators)/sizeof(uint16_t), 1);
-    hfp_hf_init(rfcomm_channel_nr, 438 | (1<<HFP_HFSF_ESCO_S4) | (1<<HFP_HFSF_EC_NR_FUNCTION), indicators, sizeof(indicators)/sizeof(uint16_t), 1);
-    hfp_hf_set_codecs(codecs, sizeof(codecs));
+    hfp_hf_init(rfcomm_channel_nr);
+    hfp_hf_set_supported_features(438 | (1<<HFP_HFSF_ESCO_S4) | (1<<HFP_HFSF_EC_NR_FUNCTION)); 
+    hfp_hf_set_indicators(sizeof(indicators)/sizeof(uint16_t), indicators);
+    hfp_hf_set_codecs(sizeof(codecs), codecs);
+    hfp_hf_set_indicators_status(1);
     
     hfp_hf_register_packet_handler(packet_handler);