174386ee0SMatthias Ringwald /* 274386ee0SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 374386ee0SMatthias Ringwald * 474386ee0SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 574386ee0SMatthias Ringwald * modification, are permitted provided that the following conditions 674386ee0SMatthias Ringwald * are met: 774386ee0SMatthias Ringwald * 874386ee0SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 974386ee0SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 1074386ee0SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 1174386ee0SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 1274386ee0SMatthias Ringwald * documentation and/or other materials provided with the distribution. 1374386ee0SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 1474386ee0SMatthias Ringwald * contributors may be used to endorse or promote products derived 1574386ee0SMatthias Ringwald * from this software without specific prior written permission. 1674386ee0SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 1774386ee0SMatthias Ringwald * personal benefit and not for any commercial purpose or for 1874386ee0SMatthias Ringwald * monetary gain. 1974386ee0SMatthias Ringwald * 2074386ee0SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 2174386ee0SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2274386ee0SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 2374386ee0SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 2474386ee0SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2574386ee0SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2674386ee0SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 2774386ee0SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2874386ee0SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2974386ee0SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 3074386ee0SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3174386ee0SMatthias Ringwald * SUCH DAMAGE. 3274386ee0SMatthias Ringwald * 3374386ee0SMatthias Ringwald * Please inquire about commercial licensing options at 3474386ee0SMatthias Ringwald * [email protected] 3574386ee0SMatthias Ringwald * 3674386ee0SMatthias Ringwald */ 3774386ee0SMatthias Ringwald 3874386ee0SMatthias Ringwald // ***************************************************************************** 3974386ee0SMatthias Ringwald // 409cae807eSMatthias Ringwald // GSM Model 4174386ee0SMatthias Ringwald // 4274386ee0SMatthias Ringwald // ***************************************************************************** 4374386ee0SMatthias Ringwald 447907f069SMatthias Ringwald #include "btstack_config.h" 4574386ee0SMatthias Ringwald 4674386ee0SMatthias Ringwald #include <stdint.h> 4774386ee0SMatthias Ringwald #include <stdio.h> 4874386ee0SMatthias Ringwald #include <stdlib.h> 4974386ee0SMatthias Ringwald #include <string.h> 5074386ee0SMatthias Ringwald 5174386ee0SMatthias Ringwald #include "btstack_memory.h" 5259c6af15SMatthias Ringwald #include "classic/core.h" 5374386ee0SMatthias Ringwald #include "classic/hfp.h" 5474386ee0SMatthias Ringwald #include "classic/hfp_gsm_model.h" 55746ccb7eSMatthias Ringwald #include "classic/sdp_server.h" 56efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 5716ece135SMatthias Ringwald #include "btstack_debug.h" 5874386ee0SMatthias Ringwald #include "hci.h" 5956042629SMatthias Ringwald #include "hci_cmd.h" 6074386ee0SMatthias Ringwald #include "hci_dump.h" 6174386ee0SMatthias Ringwald #include "l2cap.h" 6282636622SMatthias Ringwald #include "btstack_run_loop.h" 6374386ee0SMatthias Ringwald 6474386ee0SMatthias Ringwald #define HFP_GSM_MAX_NR_CALLS 3 659cae807eSMatthias Ringwald #define HFP_GSM_MAX_CALL_NUMBER_SIZE 25 6674386ee0SMatthias Ringwald 6774386ee0SMatthias Ringwald static hfp_gsm_call_t gsm_calls[HFP_GSM_MAX_NR_CALLS]; 6874386ee0SMatthias Ringwald static hfp_callsetup_status_t callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 6974386ee0SMatthias Ringwald 70d0c20769SMatthias Ringwald static uint8_t clip_type; 719cae807eSMatthias Ringwald static char clip_number[HFP_GSM_MAX_CALL_NUMBER_SIZE]; 729cae807eSMatthias Ringwald static char last_dialed_number[HFP_GSM_MAX_CALL_NUMBER_SIZE]; 73d0c20769SMatthias Ringwald 74d0c20769SMatthias Ringwald static void hfp_gsm_handler(hfp_ag_call_event_t event, uint8_t index, uint8_t type, const char * number); 7566a048abSMatthias Ringwald static inline int get_number_active_calls(void); 7666a048abSMatthias Ringwald 7766a048abSMatthias Ringwald static void set_callsetup_status(hfp_callsetup_status_t status){ 7866a048abSMatthias Ringwald callsetup_status = status; 7966a048abSMatthias Ringwald if (callsetup_status != HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE) return; 8066a048abSMatthias Ringwald 8166a048abSMatthias Ringwald int i ; 8266a048abSMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 8366a048abSMatthias Ringwald if (gsm_calls[i].direction == HFP_ENHANCED_CALL_DIR_OUTGOING){ 8466a048abSMatthias Ringwald gsm_calls[i].enhanced_status = HFP_ENHANCED_CALL_STATUS_OUTGOING_ALERTING; 8566a048abSMatthias Ringwald } 8666a048abSMatthias Ringwald } 8766a048abSMatthias Ringwald } 8866a048abSMatthias Ringwald 8966a048abSMatthias Ringwald static inline void set_enhanced_call_status_active(int index_in_table){ 9066a048abSMatthias Ringwald gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_ACTIVE; 9166a048abSMatthias Ringwald gsm_calls[index_in_table].used_slot = 1; 9266a048abSMatthias Ringwald } 9366a048abSMatthias Ringwald 9466a048abSMatthias Ringwald static inline void set_enhanced_call_status_held(int index_in_table){ 9566a048abSMatthias Ringwald gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_HELD; 9666a048abSMatthias Ringwald gsm_calls[index_in_table].used_slot = 1; 9766a048abSMatthias Ringwald } 9866a048abSMatthias Ringwald 9966a048abSMatthias Ringwald static inline void set_enhanced_call_status_response_hold(int index_in_table){ 10066a048abSMatthias Ringwald gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD; 10166a048abSMatthias Ringwald gsm_calls[index_in_table].used_slot = 1; 10266a048abSMatthias Ringwald } 10366a048abSMatthias Ringwald 10466a048abSMatthias Ringwald static inline void set_enhanced_call_status_initiated(int index_in_table){ 10566a048abSMatthias Ringwald if (gsm_calls[index_in_table].direction == HFP_ENHANCED_CALL_DIR_OUTGOING){ 10666a048abSMatthias Ringwald gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_OUTGOING_DIALING; 10766a048abSMatthias Ringwald } else { 10866a048abSMatthias Ringwald if (get_number_active_calls() > 0){ 10966a048abSMatthias Ringwald gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_INCOMING_WAITING; 11066a048abSMatthias Ringwald } else { 11166a048abSMatthias Ringwald gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_INCOMING; 11266a048abSMatthias Ringwald } 11366a048abSMatthias Ringwald } 11466a048abSMatthias Ringwald gsm_calls[index_in_table].used_slot = 1; 11566a048abSMatthias Ringwald } 11666a048abSMatthias Ringwald 11766a048abSMatthias Ringwald static int get_enhanced_call_status(int index_in_table){ 11866a048abSMatthias Ringwald if (!gsm_calls[index_in_table].used_slot) return -1; 11966a048abSMatthias Ringwald return gsm_calls[index_in_table].enhanced_status; 12066a048abSMatthias Ringwald } 12166a048abSMatthias Ringwald 12266a048abSMatthias Ringwald static inline int is_enhanced_call_status_active(int index_in_table){ 12366a048abSMatthias Ringwald return get_enhanced_call_status(index_in_table) == HFP_ENHANCED_CALL_STATUS_ACTIVE; 12466a048abSMatthias Ringwald } 12566a048abSMatthias Ringwald 12666a048abSMatthias Ringwald static inline int is_enhanced_call_status_initiated(int index_in_table){ 12766a048abSMatthias Ringwald switch (get_enhanced_call_status(index_in_table)){ 12866a048abSMatthias Ringwald case HFP_ENHANCED_CALL_STATUS_OUTGOING_DIALING: 12966a048abSMatthias Ringwald case HFP_ENHANCED_CALL_STATUS_OUTGOING_ALERTING: 13066a048abSMatthias Ringwald case HFP_ENHANCED_CALL_STATUS_INCOMING: 13166a048abSMatthias Ringwald case HFP_ENHANCED_CALL_STATUS_INCOMING_WAITING: 13266a048abSMatthias Ringwald return 1; 13366a048abSMatthias Ringwald default: 13466a048abSMatthias Ringwald return 0; 13566a048abSMatthias Ringwald } 13666a048abSMatthias Ringwald } 13766a048abSMatthias Ringwald 13866a048abSMatthias Ringwald static void free_call_slot(int index_in_table){ 13966a048abSMatthias Ringwald gsm_calls[index_in_table].used_slot = 0; 14066a048abSMatthias Ringwald } 141d0c20769SMatthias Ringwald 142d210d9c4SMatthias Ringwald void hfp_gsm_init(void){ 14366a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 144d0c20769SMatthias Ringwald clip_type = 0; 145d0c20769SMatthias Ringwald memset(clip_number, 0, sizeof(clip_number)); 1469cae807eSMatthias Ringwald memset(last_dialed_number, 0, sizeof(last_dialed_number)); 147d210d9c4SMatthias Ringwald memset(gsm_calls, 0, sizeof(gsm_calls)); 148d210d9c4SMatthias Ringwald int i; 149d210d9c4SMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 15066a048abSMatthias Ringwald free_call_slot(i); 151d210d9c4SMatthias Ringwald } 152d210d9c4SMatthias Ringwald } 15374386ee0SMatthias Ringwald 15466a048abSMatthias Ringwald static int get_number_calls_with_enhanced_status(hfp_enhanced_call_status_t enhanced_status){ 15574386ee0SMatthias Ringwald int i, count = 0; 15674386ee0SMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 15766a048abSMatthias Ringwald if (get_enhanced_call_status(i) == enhanced_status) count++; 15874386ee0SMatthias Ringwald } 15974386ee0SMatthias Ringwald return count; 16074386ee0SMatthias Ringwald } 16174386ee0SMatthias Ringwald 16266a048abSMatthias Ringwald static int get_call_index_with_enhanced_status(hfp_enhanced_call_status_t enhanced_status){ 16374386ee0SMatthias Ringwald int i ; 16474386ee0SMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 16566a048abSMatthias Ringwald if (get_enhanced_call_status(i) == enhanced_status) return i; 16666a048abSMatthias Ringwald } 16766a048abSMatthias Ringwald return -1; 16866a048abSMatthias Ringwald } 16966a048abSMatthias Ringwald 17066a048abSMatthias Ringwald static inline int get_initiated_call_index(void){ 17166a048abSMatthias Ringwald int i ; 17266a048abSMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 17366a048abSMatthias Ringwald if (is_enhanced_call_status_initiated(i)) return i; 17474386ee0SMatthias Ringwald } 17574386ee0SMatthias Ringwald return -1; 17674386ee0SMatthias Ringwald } 17774386ee0SMatthias Ringwald 1789cae807eSMatthias Ringwald static inline int get_next_free_slot(void){ 17966a048abSMatthias Ringwald int i ; 18066a048abSMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 18166a048abSMatthias Ringwald if (!gsm_calls[i].used_slot) return i; 18266a048abSMatthias Ringwald } 18366a048abSMatthias Ringwald return -1; 18474386ee0SMatthias Ringwald } 18574386ee0SMatthias Ringwald 1869cae807eSMatthias Ringwald static inline int get_active_call_index(void){ 18766a048abSMatthias Ringwald return get_call_index_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_ACTIVE); 188d210d9c4SMatthias Ringwald } 189d210d9c4SMatthias Ringwald 1909cae807eSMatthias Ringwald static inline int get_held_call_index(void){ 19166a048abSMatthias Ringwald return get_call_index_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_HELD); 192d210d9c4SMatthias Ringwald } 193d210d9c4SMatthias Ringwald 1949cae807eSMatthias Ringwald static inline int get_response_held_call_index(void){ 19566a048abSMatthias Ringwald return get_call_index_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD); 196d210d9c4SMatthias Ringwald } 197d210d9c4SMatthias Ringwald 1989cae807eSMatthias Ringwald static inline int get_number_none_calls(void){ 19966a048abSMatthias Ringwald int i, count = 0; 20066a048abSMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 20166a048abSMatthias Ringwald if (!gsm_calls[i].used_slot) count++; 20266a048abSMatthias Ringwald } 20366a048abSMatthias Ringwald return count; 204d210d9c4SMatthias Ringwald } 20574386ee0SMatthias Ringwald 2069cae807eSMatthias Ringwald static inline int get_number_active_calls(void){ 20766a048abSMatthias Ringwald return get_number_calls_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_ACTIVE); 20874386ee0SMatthias Ringwald } 20974386ee0SMatthias Ringwald 2109cae807eSMatthias Ringwald static inline int get_number_held_calls(void){ 21166a048abSMatthias Ringwald return get_number_calls_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_HELD); 21274386ee0SMatthias Ringwald } 21374386ee0SMatthias Ringwald 2149cae807eSMatthias Ringwald static inline int get_number_response_held_calls(void){ 21566a048abSMatthias Ringwald return get_number_calls_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD); 216d210d9c4SMatthias Ringwald } 217d210d9c4SMatthias Ringwald 2189cae807eSMatthias Ringwald static int next_call_index(void){ 219d0c20769SMatthias Ringwald return HFP_GSM_MAX_NR_CALLS + 1 - get_number_none_calls(); 220d0c20769SMatthias Ringwald } 221d0c20769SMatthias Ringwald 222d0c20769SMatthias Ringwald static void hfp_gsm_set_clip(int index_in_table, uint8_t type, const char * number){ 2239cae807eSMatthias Ringwald if (strlen(number) == 0) return; 2249cae807eSMatthias Ringwald 225d0c20769SMatthias Ringwald gsm_calls[index_in_table].clip_type = type; 226d0c20769SMatthias Ringwald 227*7c959318SMatthias Ringwald int clip_number_size = strlen(number) < HFP_GSM_MAX_CALL_NUMBER_SIZE ? (int) strlen(number) : HFP_GSM_MAX_CALL_NUMBER_SIZE-1; 228d0c20769SMatthias Ringwald strncpy(gsm_calls[index_in_table].clip_number, number, clip_number_size); 2299cae807eSMatthias Ringwald gsm_calls[index_in_table].clip_number[clip_number_size] = '\0'; 2309cae807eSMatthias Ringwald strncpy(last_dialed_number, number, clip_number_size); 2319cae807eSMatthias Ringwald last_dialed_number[clip_number_size] = '\0'; 2329cae807eSMatthias Ringwald 2339cae807eSMatthias Ringwald clip_type = 0; 2349cae807eSMatthias Ringwald memset(clip_number, 0, sizeof(clip_number)); 235d0c20769SMatthias Ringwald } 236d0c20769SMatthias Ringwald 237d0c20769SMatthias Ringwald static void delete_call(int delete_index_in_table){ 238d0c20769SMatthias Ringwald int i ; 239d0c20769SMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 240d0c20769SMatthias Ringwald if (gsm_calls[i].index > gsm_calls[delete_index_in_table].index){ 241d0c20769SMatthias Ringwald gsm_calls[i].index--; 242d0c20769SMatthias Ringwald } 243d0c20769SMatthias Ringwald } 24466a048abSMatthias Ringwald free_call_slot(delete_index_in_table); 245d0c20769SMatthias Ringwald 246d0c20769SMatthias Ringwald gsm_calls[delete_index_in_table].clip_type = 0; 247d0c20769SMatthias Ringwald gsm_calls[delete_index_in_table].index = 0; 248d0c20769SMatthias Ringwald gsm_calls[delete_index_in_table].clip_number[0] = '\0'; 2499cae807eSMatthias Ringwald gsm_calls[delete_index_in_table].mpty = HFP_ENHANCED_CALL_MPTY_NOT_A_CONFERENCE_CALL; 250d0c20769SMatthias Ringwald } 251d0c20769SMatthias Ringwald 2529cae807eSMatthias Ringwald 2539cae807eSMatthias Ringwald static void create_call(hfp_enhanced_call_dir_t direction){ 254d0c20769SMatthias Ringwald int next_free_slot = get_next_free_slot(); 2559cae807eSMatthias Ringwald gsm_calls[next_free_slot].direction = direction; 256d0c20769SMatthias Ringwald gsm_calls[next_free_slot].index = next_call_index(); 25766a048abSMatthias Ringwald set_enhanced_call_status_initiated(next_free_slot); 258d0c20769SMatthias Ringwald gsm_calls[next_free_slot].clip_type = 0; 259d0c20769SMatthias Ringwald gsm_calls[next_free_slot].clip_number[0] = '\0'; 2609cae807eSMatthias Ringwald gsm_calls[next_free_slot].mpty = HFP_ENHANCED_CALL_MPTY_NOT_A_CONFERENCE_CALL; 261d0c20769SMatthias Ringwald 262d0c20769SMatthias Ringwald hfp_gsm_set_clip(next_free_slot, clip_type, clip_number); 263d0c20769SMatthias Ringwald } 264d0c20769SMatthias Ringwald 2659cae807eSMatthias Ringwald 2669cae807eSMatthias Ringwald int hfp_gsm_get_number_of_calls(void){ 2679cae807eSMatthias Ringwald return HFP_GSM_MAX_NR_CALLS - get_number_none_calls(); 2689cae807eSMatthias Ringwald } 2699cae807eSMatthias Ringwald 2709cae807eSMatthias Ringwald void hfp_gsm_clear_last_dialed_number(void){ 2719cae807eSMatthias Ringwald memset(last_dialed_number, 0, sizeof(last_dialed_number)); 2729cae807eSMatthias Ringwald } 2739cae807eSMatthias Ringwald 2749cae807eSMatthias Ringwald char * hfp_gsm_last_dialed_number(void){ 2759cae807eSMatthias Ringwald return &last_dialed_number[0]; 2769cae807eSMatthias Ringwald } 2779cae807eSMatthias Ringwald 2789cae807eSMatthias Ringwald hfp_gsm_call_t * hfp_gsm_call(int call_index){ 2799cae807eSMatthias Ringwald int i; 2809cae807eSMatthias Ringwald 2819cae807eSMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 2829cae807eSMatthias Ringwald hfp_gsm_call_t * call = &gsm_calls[i]; 2839cae807eSMatthias Ringwald if (call->index != call_index) continue; 2849cae807eSMatthias Ringwald return call; 2859cae807eSMatthias Ringwald } 2869cae807eSMatthias Ringwald return NULL; 2879cae807eSMatthias Ringwald } 2889cae807eSMatthias Ringwald 2899cae807eSMatthias Ringwald uint8_t hfp_gsm_clip_type(void){ 290d0c20769SMatthias Ringwald if (clip_type != 0) return clip_type; 291d0c20769SMatthias Ringwald 292d0c20769SMatthias Ringwald int initiated_call_index = get_initiated_call_index(); 293d0c20769SMatthias Ringwald if (initiated_call_index != -1){ 294d0c20769SMatthias Ringwald if (gsm_calls[initiated_call_index].clip_type != 0) { 295d0c20769SMatthias Ringwald return gsm_calls[initiated_call_index].clip_type; 296d0c20769SMatthias Ringwald } 297d0c20769SMatthias Ringwald } 298d0c20769SMatthias Ringwald 299d0c20769SMatthias Ringwald int active_call_index = get_active_call_index(); 300d0c20769SMatthias Ringwald if (active_call_index != -1){ 301d0c20769SMatthias Ringwald if (gsm_calls[active_call_index].clip_type != 0) { 302d0c20769SMatthias Ringwald return gsm_calls[active_call_index].clip_type; 303d0c20769SMatthias Ringwald } 304d0c20769SMatthias Ringwald } 305d0c20769SMatthias Ringwald return 0; 306d0c20769SMatthias Ringwald } 307d0c20769SMatthias Ringwald 3089cae807eSMatthias Ringwald char * hfp_gsm_clip_number(void){ 3099cae807eSMatthias Ringwald if (strlen(clip_number) != 0) return clip_number; 310d0c20769SMatthias Ringwald 311d0c20769SMatthias Ringwald int initiated_call_index = get_initiated_call_index(); 312d0c20769SMatthias Ringwald if (initiated_call_index != -1){ 313d0c20769SMatthias Ringwald if (gsm_calls[initiated_call_index].clip_type != 0) { 314d0c20769SMatthias Ringwald return gsm_calls[initiated_call_index].clip_number; 315d0c20769SMatthias Ringwald } 316d0c20769SMatthias Ringwald } 317d0c20769SMatthias Ringwald 318d0c20769SMatthias Ringwald int active_call_index = get_active_call_index(); 319d0c20769SMatthias Ringwald if (active_call_index != -1){ 320d0c20769SMatthias Ringwald if (gsm_calls[active_call_index].clip_type != 0) { 321d0c20769SMatthias Ringwald return gsm_calls[active_call_index].clip_number; 322d0c20769SMatthias Ringwald } 323d0c20769SMatthias Ringwald } 324d0c20769SMatthias Ringwald clip_number[0] = 0; 325d0c20769SMatthias Ringwald return clip_number; 326d0c20769SMatthias Ringwald } 327d0c20769SMatthias Ringwald 3289cae807eSMatthias Ringwald hfp_call_status_t hfp_gsm_call_status(void){ 329d210d9c4SMatthias Ringwald if (get_number_active_calls() + get_number_held_calls() + get_number_response_held_calls()){ 33074386ee0SMatthias Ringwald return HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT; 33174386ee0SMatthias Ringwald } 33274386ee0SMatthias Ringwald return HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS; 33374386ee0SMatthias Ringwald } 33474386ee0SMatthias Ringwald 3359cae807eSMatthias Ringwald hfp_callheld_status_t hfp_gsm_callheld_status(void){ 33674386ee0SMatthias Ringwald // @note: order is important 33774386ee0SMatthias Ringwald if (get_number_held_calls() == 0){ 33874386ee0SMatthias Ringwald return HFP_CALLHELD_STATUS_NO_CALLS_HELD; 33974386ee0SMatthias Ringwald } 34074386ee0SMatthias Ringwald if (get_number_active_calls() == 0) { 34174386ee0SMatthias Ringwald return HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS; 34274386ee0SMatthias Ringwald } 34374386ee0SMatthias Ringwald return HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED; 34474386ee0SMatthias Ringwald } 34574386ee0SMatthias Ringwald 3469cae807eSMatthias Ringwald hfp_callsetup_status_t hfp_gsm_callsetup_status(void){ 34774386ee0SMatthias Ringwald return callsetup_status; 34874386ee0SMatthias Ringwald } 34974386ee0SMatthias Ringwald 3509cae807eSMatthias Ringwald static int hfp_gsm_response_held_active(void){ 351d210d9c4SMatthias Ringwald return get_response_held_call_index() != -1 ; 352d210d9c4SMatthias Ringwald } 353d210d9c4SMatthias Ringwald 354d210d9c4SMatthias Ringwald int hfp_gsm_call_possible(void){ 355d210d9c4SMatthias Ringwald return get_number_none_calls() > 0; 356d210d9c4SMatthias Ringwald } 357d210d9c4SMatthias Ringwald 35874386ee0SMatthias Ringwald void hfp_gsm_handle_event(hfp_ag_call_event_t event){ 359d0c20769SMatthias Ringwald hfp_gsm_handler(event, 0, 0, NULL); 360d0c20769SMatthias Ringwald } 361d0c20769SMatthias Ringwald 362d0c20769SMatthias Ringwald void hfp_gsm_handle_event_with_clip(hfp_ag_call_event_t event, uint8_t type, const char * number){ 363d0c20769SMatthias Ringwald hfp_gsm_handler(event, 0, type, number); 364d0c20769SMatthias Ringwald } 365d0c20769SMatthias Ringwald 366d0c20769SMatthias Ringwald void hfp_gsm_handle_event_with_call_index(hfp_ag_call_event_t event, uint8_t index){ 367d0c20769SMatthias Ringwald hfp_gsm_handler(event, index, 0, NULL); 368d0c20769SMatthias Ringwald } 369d0c20769SMatthias Ringwald 3709cae807eSMatthias Ringwald void hfp_gsm_handle_event_with_call_number(hfp_ag_call_event_t event, const char * number){ 3719cae807eSMatthias Ringwald hfp_gsm_handler(event, 0, 0, number); 3729cae807eSMatthias Ringwald } 3739cae807eSMatthias Ringwald 374d0c20769SMatthias Ringwald static void hfp_gsm_handler(hfp_ag_call_event_t event, uint8_t index, uint8_t type, const char * number){ 375d0c20769SMatthias Ringwald int next_free_slot = get_next_free_slot(); 37674386ee0SMatthias Ringwald int current_call_index = get_active_call_index(); 377d210d9c4SMatthias Ringwald int initiated_call_index = get_initiated_call_index(); 378d210d9c4SMatthias Ringwald int held_call_index = get_held_call_index(); 379d0c20769SMatthias Ringwald int i; 380d0c20769SMatthias Ringwald 38174386ee0SMatthias Ringwald switch (event){ 38274386ee0SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED: 38374386ee0SMatthias Ringwald case HFP_AG_OUTGOING_REDIAL_INITIATED: 38474386ee0SMatthias Ringwald if (next_free_slot == -1){ 385d210d9c4SMatthias Ringwald log_error("gsm: max call nr exceeded"); 38674386ee0SMatthias Ringwald return; 38774386ee0SMatthias Ringwald } 3889cae807eSMatthias Ringwald create_call(HFP_ENHANCED_CALL_DIR_OUTGOING); 389d210d9c4SMatthias Ringwald break; 39074386ee0SMatthias Ringwald 391d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_REJECTED: 392d210d9c4SMatthias Ringwald if (current_call_index != -1){ 393d0c20769SMatthias Ringwald delete_call(current_call_index); 394d210d9c4SMatthias Ringwald } 39566a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 396d210d9c4SMatthias Ringwald break; 397d210d9c4SMatthias Ringwald 398d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ACCEPTED: 39974386ee0SMatthias Ringwald if (current_call_index != -1){ 40066a048abSMatthias Ringwald set_enhanced_call_status_held(current_call_index); 40174386ee0SMatthias Ringwald } 40266a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE); 40374386ee0SMatthias Ringwald break; 404d210d9c4SMatthias Ringwald 40574386ee0SMatthias Ringwald case HFP_AG_OUTGOING_CALL_RINGING: 406d210d9c4SMatthias Ringwald if (current_call_index == -1){ 407d210d9c4SMatthias Ringwald log_error("gsm: no active call"); 408d210d9c4SMatthias Ringwald return; 409d210d9c4SMatthias Ringwald } 41066a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE); 41174386ee0SMatthias Ringwald break; 41274386ee0SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ESTABLISHED: 41366a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 41466a048abSMatthias Ringwald set_enhanced_call_status_active(initiated_call_index); 41574386ee0SMatthias Ringwald break; 416d210d9c4SMatthias Ringwald 417d210d9c4SMatthias Ringwald case HFP_AG_INCOMING_CALL: 418d210d9c4SMatthias Ringwald if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS) break; 41966a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS); 4209cae807eSMatthias Ringwald create_call(HFP_ENHANCED_CALL_DIR_INCOMING); 421d210d9c4SMatthias Ringwald break; 422d210d9c4SMatthias Ringwald 423d210d9c4SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG: 424d210d9c4SMatthias Ringwald if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) break; 42566a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 426d210d9c4SMatthias Ringwald 427d210d9c4SMatthias Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 42866a048abSMatthias Ringwald set_enhanced_call_status_held(current_call_index); 429d210d9c4SMatthias Ringwald } 43066a048abSMatthias Ringwald set_enhanced_call_status_active(initiated_call_index); 431d210d9c4SMatthias Ringwald break; 432d210d9c4SMatthias Ringwald 433d210d9c4SMatthias Ringwald case HFP_AG_HELD_CALL_JOINED_BY_AG: 434d210d9c4SMatthias Ringwald if (hfp_gsm_call_status() != HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT) break; 435d210d9c4SMatthias Ringwald 436d210d9c4SMatthias Ringwald // TODO: is following condition correct? Can we join incoming call before it is answered? 437d210d9c4SMatthias Ringwald if (callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 43866a048abSMatthias Ringwald set_enhanced_call_status_active(initiated_call_index); 43966a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 4409cae807eSMatthias Ringwald } else if (hfp_gsm_callheld_status() == HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED) { 44166a048abSMatthias Ringwald set_enhanced_call_status_active(held_call_index); 442d210d9c4SMatthias Ringwald } 443d210d9c4SMatthias Ringwald 4449cae807eSMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 44566a048abSMatthias Ringwald if (is_enhanced_call_status_active(i)){ 4469cae807eSMatthias Ringwald gsm_calls[i].mpty = HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL; 4479cae807eSMatthias Ringwald } 448d210d9c4SMatthias Ringwald } 449d210d9c4SMatthias Ringwald break; 450d210d9c4SMatthias Ringwald 451d210d9c4SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 452d210d9c4SMatthias Ringwald if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) break; 453d210d9c4SMatthias Ringwald if (hfp_gsm_call_status() != HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS) break; 45466a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 45566a048abSMatthias Ringwald set_enhanced_call_status_active(initiated_call_index); 456d210d9c4SMatthias Ringwald break; 457d210d9c4SMatthias Ringwald 458d210d9c4SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 459d210d9c4SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 460d210d9c4SMatthias Ringwald if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) break; 461d210d9c4SMatthias Ringwald if (hfp_gsm_call_status() != HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS) break; 46266a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 46366a048abSMatthias Ringwald set_enhanced_call_status_response_hold(initiated_call_index); 464d210d9c4SMatthias Ringwald break; 465d210d9c4SMatthias Ringwald 466d210d9c4SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 467d210d9c4SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 468d210d9c4SMatthias Ringwald if (!hfp_gsm_response_held_active()) break; 46966a048abSMatthias Ringwald set_enhanced_call_status_active(get_response_held_call_index()); 470d210d9c4SMatthias Ringwald break; 471d210d9c4SMatthias Ringwald 472d210d9c4SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 473d210d9c4SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 474d210d9c4SMatthias Ringwald if (!hfp_gsm_response_held_active()) break; 475d0c20769SMatthias Ringwald delete_call(get_response_held_call_index()); 476d210d9c4SMatthias Ringwald break; 477d210d9c4SMatthias Ringwald 478d210d9c4SMatthias Ringwald 479d210d9c4SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_HF: 480d210d9c4SMatthias Ringwald switch (hfp_gsm_call_status()){ 481d210d9c4SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 48266a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 483d210d9c4SMatthias Ringwald break; 484d210d9c4SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 485d0c20769SMatthias Ringwald delete_call(current_call_index); 486d210d9c4SMatthias Ringwald break; 487d210d9c4SMatthias Ringwald } 488d210d9c4SMatthias Ringwald break; 489d210d9c4SMatthias Ringwald 490d210d9c4SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_AG: 491d210d9c4SMatthias Ringwald switch (hfp_gsm_call_status()){ 492d210d9c4SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 493d210d9c4SMatthias Ringwald if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) break; 49466a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 495d210d9c4SMatthias Ringwald break; 496d210d9c4SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 49766a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 498d0c20769SMatthias Ringwald delete_call(current_call_index); 499d210d9c4SMatthias Ringwald break; 500d210d9c4SMatthias Ringwald default: 501d210d9c4SMatthias Ringwald break; 502d210d9c4SMatthias Ringwald } 503d210d9c4SMatthias Ringwald break; 504d210d9c4SMatthias Ringwald 505d0c20769SMatthias Ringwald case HFP_AG_CALL_DROPPED: 50666a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 507d210d9c4SMatthias Ringwald if (hfp_gsm_call_status() != HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT) break; 508d210d9c4SMatthias Ringwald 509d210d9c4SMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 510d0c20769SMatthias Ringwald delete_call(i); 511d210d9c4SMatthias Ringwald } 512d210d9c4SMatthias Ringwald break; 513d0c20769SMatthias Ringwald 514d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_USER_BUSY: 515d210d9c4SMatthias Ringwald // Held or waiting call gets active, 51666a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 51766a048abSMatthias Ringwald free_call_slot(initiated_call_index); 51866a048abSMatthias Ringwald set_enhanced_call_status_active(held_call_index); 519d210d9c4SMatthias Ringwald break; 520d210d9c4SMatthias Ringwald 521d0c20769SMatthias Ringwald case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL: 522d0c20769SMatthias Ringwald if (index != 0 && index <= HFP_GSM_MAX_NR_CALLS ){ 523d0c20769SMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 524d0c20769SMatthias Ringwald if (gsm_calls[i].index == index){ 525d0c20769SMatthias Ringwald delete_call(i); 526d0c20769SMatthias Ringwald continue; 527d0c20769SMatthias Ringwald } 528d0c20769SMatthias Ringwald } 529d0c20769SMatthias Ringwald } else { 530d210d9c4SMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 53166a048abSMatthias Ringwald if (is_enhanced_call_status_active(i)){ 532d0c20769SMatthias Ringwald delete_call(i); 533d0c20769SMatthias Ringwald } 534d210d9c4SMatthias Ringwald } 535d210d9c4SMatthias Ringwald } 536d210d9c4SMatthias Ringwald 537d210d9c4SMatthias Ringwald if (callsetup_status != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS){ 53866a048abSMatthias Ringwald set_enhanced_call_status_active(initiated_call_index); 539d210d9c4SMatthias Ringwald } else { 54066a048abSMatthias Ringwald set_enhanced_call_status_active(held_call_index); 541d210d9c4SMatthias Ringwald } 542d0c20769SMatthias Ringwald 54366a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 544d210d9c4SMatthias Ringwald break; 545d0c20769SMatthias Ringwald 546d0c20769SMatthias Ringwald case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL: 547d210d9c4SMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 54866a048abSMatthias Ringwald if (is_enhanced_call_status_active(i) && gsm_calls[i].index != index){ 54966a048abSMatthias Ringwald set_enhanced_call_status_held(i); 550d210d9c4SMatthias Ringwald } 551d210d9c4SMatthias Ringwald } 552d210d9c4SMatthias Ringwald 553d210d9c4SMatthias Ringwald if (callsetup_status != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS){ 55466a048abSMatthias Ringwald set_enhanced_call_status_active(initiated_call_index); 555d210d9c4SMatthias Ringwald } else { 55666a048abSMatthias Ringwald set_enhanced_call_status_active(held_call_index); 557d210d9c4SMatthias Ringwald } 55866a048abSMatthias Ringwald set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 559d210d9c4SMatthias Ringwald break; 560d0c20769SMatthias Ringwald 561d0c20769SMatthias Ringwald case HFP_AG_CALL_HOLD_ADD_HELD_CALL: 562d210d9c4SMatthias Ringwald if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 563d210d9c4SMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 56466a048abSMatthias Ringwald if (gsm_calls[i].used_slot){ 56566a048abSMatthias Ringwald set_enhanced_call_status_active(i); 5669cae807eSMatthias Ringwald gsm_calls[i].mpty = HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL; 567d210d9c4SMatthias Ringwald } 568d210d9c4SMatthias Ringwald } 569d210d9c4SMatthias Ringwald } 570d210d9c4SMatthias Ringwald break; 571d0c20769SMatthias Ringwald 572d0c20769SMatthias Ringwald case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS: 573d210d9c4SMatthias Ringwald for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){ 574d0c20769SMatthias Ringwald delete_call(i); 575d210d9c4SMatthias Ringwald } 576d0c20769SMatthias Ringwald break; 577d210d9c4SMatthias Ringwald 578d0c20769SMatthias Ringwald case HFP_AG_SET_CLIP: 579d0c20769SMatthias Ringwald if (initiated_call_index != -1){ 580d0c20769SMatthias Ringwald hfp_gsm_set_clip(initiated_call_index, type, number); 581d210d9c4SMatthias Ringwald break; 582d210d9c4SMatthias Ringwald } 5839cae807eSMatthias Ringwald 584d0c20769SMatthias Ringwald clip_type = type; 585d0c20769SMatthias Ringwald strncpy(clip_number, number, sizeof(clip_number)); 586d0c20769SMatthias Ringwald clip_number[sizeof(clip_number)-1] = '\0'; 587d0c20769SMatthias Ringwald 588d0c20769SMatthias Ringwald break; 58974386ee0SMatthias Ringwald default: 59074386ee0SMatthias Ringwald break; 59174386ee0SMatthias Ringwald } 59274386ee0SMatthias Ringwald }