xref: /btstack/src/classic/hfp_gsm_model.c (revision c1ab6cc1beb14b16b46e74a3723644016d8c3cc7)
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  */
37ab2c6ae4SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp_gsm_model.c"
3974386ee0SMatthias Ringwald 
4074386ee0SMatthias Ringwald // *****************************************************************************
4174386ee0SMatthias Ringwald //
429cae807eSMatthias Ringwald // GSM Model
4374386ee0SMatthias Ringwald //
4474386ee0SMatthias Ringwald // *****************************************************************************
4574386ee0SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
4774386ee0SMatthias Ringwald 
4874386ee0SMatthias Ringwald #include <stdint.h>
4974386ee0SMatthias Ringwald #include <stdio.h>
5074386ee0SMatthias Ringwald #include <stdlib.h>
5174386ee0SMatthias Ringwald #include <string.h>
5274386ee0SMatthias Ringwald 
5374386ee0SMatthias Ringwald #include "btstack_memory.h"
5459c6af15SMatthias Ringwald #include "classic/core.h"
5574386ee0SMatthias Ringwald #include "classic/hfp.h"
5674386ee0SMatthias Ringwald #include "classic/hfp_gsm_model.h"
57746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
58efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
5916ece135SMatthias Ringwald #include "btstack_debug.h"
6074386ee0SMatthias Ringwald #include "hci.h"
6156042629SMatthias Ringwald #include "hci_cmd.h"
6274386ee0SMatthias Ringwald #include "hci_dump.h"
6374386ee0SMatthias Ringwald #include "l2cap.h"
6482636622SMatthias Ringwald #include "btstack_run_loop.h"
6574386ee0SMatthias Ringwald 
6674386ee0SMatthias Ringwald #define HFP_GSM_MAX_NR_CALLS 3
679cae807eSMatthias Ringwald #define HFP_GSM_MAX_CALL_NUMBER_SIZE 25
6874386ee0SMatthias Ringwald 
6974386ee0SMatthias Ringwald static hfp_gsm_call_t gsm_calls[HFP_GSM_MAX_NR_CALLS];
7074386ee0SMatthias Ringwald static hfp_callsetup_status_t callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
7174386ee0SMatthias Ringwald 
72d0c20769SMatthias Ringwald static uint8_t clip_type;
739cae807eSMatthias Ringwald static char clip_number[HFP_GSM_MAX_CALL_NUMBER_SIZE];
749cae807eSMatthias Ringwald static char last_dialed_number[HFP_GSM_MAX_CALL_NUMBER_SIZE];
75d0c20769SMatthias Ringwald 
76d0c20769SMatthias Ringwald static void hfp_gsm_handler(hfp_ag_call_event_t event, uint8_t index, uint8_t type, const char * number);
7766a048abSMatthias Ringwald static inline int get_number_active_calls(void);
7866a048abSMatthias Ringwald 
7966a048abSMatthias Ringwald static void set_callsetup_status(hfp_callsetup_status_t status){
8066a048abSMatthias Ringwald     callsetup_status = status;
8166a048abSMatthias Ringwald     if (callsetup_status != HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE) return;
8266a048abSMatthias Ringwald 
8366a048abSMatthias Ringwald     int i ;
8466a048abSMatthias Ringwald     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
8566a048abSMatthias Ringwald         if (gsm_calls[i].direction == HFP_ENHANCED_CALL_DIR_OUTGOING){
8666a048abSMatthias Ringwald             gsm_calls[i].enhanced_status = HFP_ENHANCED_CALL_STATUS_OUTGOING_ALERTING;
8766a048abSMatthias Ringwald         }
8866a048abSMatthias Ringwald     }
8966a048abSMatthias Ringwald }
9066a048abSMatthias Ringwald 
9166a048abSMatthias Ringwald static inline void set_enhanced_call_status_active(int index_in_table){
9266a048abSMatthias Ringwald     gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_ACTIVE;
9366a048abSMatthias Ringwald     gsm_calls[index_in_table].used_slot = 1;
9466a048abSMatthias Ringwald }
9566a048abSMatthias Ringwald 
9666a048abSMatthias Ringwald static inline void set_enhanced_call_status_held(int index_in_table){
9766a048abSMatthias Ringwald     gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_HELD;
9866a048abSMatthias Ringwald     gsm_calls[index_in_table].used_slot = 1;
9966a048abSMatthias Ringwald }
10066a048abSMatthias Ringwald 
10166a048abSMatthias Ringwald static inline void set_enhanced_call_status_response_hold(int index_in_table){
10266a048abSMatthias Ringwald     gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD;
10366a048abSMatthias Ringwald     gsm_calls[index_in_table].used_slot = 1;
10466a048abSMatthias Ringwald }
10566a048abSMatthias Ringwald 
10666a048abSMatthias Ringwald static inline void set_enhanced_call_status_initiated(int index_in_table){
10766a048abSMatthias Ringwald     if (gsm_calls[index_in_table].direction == HFP_ENHANCED_CALL_DIR_OUTGOING){
10866a048abSMatthias Ringwald         gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_OUTGOING_DIALING;
10966a048abSMatthias Ringwald     } else {
11066a048abSMatthias Ringwald         if (get_number_active_calls() > 0){
11166a048abSMatthias Ringwald             gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_INCOMING_WAITING;
11266a048abSMatthias Ringwald         } else {
11366a048abSMatthias Ringwald             gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_INCOMING;
11466a048abSMatthias Ringwald         }
11566a048abSMatthias Ringwald     }
11666a048abSMatthias Ringwald     gsm_calls[index_in_table].used_slot = 1;
11766a048abSMatthias Ringwald }
11866a048abSMatthias Ringwald 
11966a048abSMatthias Ringwald static int get_enhanced_call_status(int index_in_table){
12066a048abSMatthias Ringwald     if (!gsm_calls[index_in_table].used_slot) return -1;
12166a048abSMatthias Ringwald     return gsm_calls[index_in_table].enhanced_status;
12266a048abSMatthias Ringwald }
12366a048abSMatthias Ringwald 
12466a048abSMatthias Ringwald static inline int is_enhanced_call_status_active(int index_in_table){
12566a048abSMatthias Ringwald     return get_enhanced_call_status(index_in_table) == HFP_ENHANCED_CALL_STATUS_ACTIVE;
12666a048abSMatthias Ringwald }
12766a048abSMatthias Ringwald 
12866a048abSMatthias Ringwald static inline int is_enhanced_call_status_initiated(int index_in_table){
12966a048abSMatthias Ringwald     switch (get_enhanced_call_status(index_in_table)){
13066a048abSMatthias Ringwald         case HFP_ENHANCED_CALL_STATUS_OUTGOING_DIALING:
13166a048abSMatthias Ringwald         case HFP_ENHANCED_CALL_STATUS_OUTGOING_ALERTING:
13266a048abSMatthias Ringwald         case HFP_ENHANCED_CALL_STATUS_INCOMING:
13366a048abSMatthias Ringwald         case HFP_ENHANCED_CALL_STATUS_INCOMING_WAITING:
13466a048abSMatthias Ringwald             return 1;
13566a048abSMatthias Ringwald         default:
13666a048abSMatthias Ringwald             return 0;
13766a048abSMatthias Ringwald     }
13866a048abSMatthias Ringwald }
13966a048abSMatthias Ringwald 
14066a048abSMatthias Ringwald static void free_call_slot(int index_in_table){
14166a048abSMatthias Ringwald     gsm_calls[index_in_table].used_slot = 0;
14266a048abSMatthias Ringwald }
143d0c20769SMatthias Ringwald 
144d210d9c4SMatthias Ringwald void hfp_gsm_init(void){
14566a048abSMatthias Ringwald     set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
146d0c20769SMatthias Ringwald     clip_type = 0;
147d0c20769SMatthias Ringwald     memset(clip_number, 0, sizeof(clip_number));
1489cae807eSMatthias Ringwald     memset(last_dialed_number, 0, sizeof(last_dialed_number));
149d210d9c4SMatthias Ringwald     memset(gsm_calls, 0, sizeof(gsm_calls));
150d210d9c4SMatthias Ringwald     int i;
151d210d9c4SMatthias Ringwald     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
15266a048abSMatthias Ringwald         free_call_slot(i);
153d210d9c4SMatthias Ringwald     }
154d210d9c4SMatthias Ringwald }
15574386ee0SMatthias Ringwald 
15666a048abSMatthias Ringwald static int get_number_calls_with_enhanced_status(hfp_enhanced_call_status_t enhanced_status){
15774386ee0SMatthias Ringwald     int i, count = 0;
15874386ee0SMatthias Ringwald     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
159fdfcab43SMatthias Ringwald         if (get_enhanced_call_status(i) == (int) enhanced_status) count++;
16074386ee0SMatthias Ringwald     }
16174386ee0SMatthias Ringwald     return count;
16274386ee0SMatthias Ringwald }
16374386ee0SMatthias Ringwald 
16466a048abSMatthias Ringwald static int get_call_index_with_enhanced_status(hfp_enhanced_call_status_t enhanced_status){
16574386ee0SMatthias Ringwald     int i ;
16674386ee0SMatthias Ringwald     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
167fdfcab43SMatthias Ringwald         if (get_enhanced_call_status(i) == (int) enhanced_status) return i;
16866a048abSMatthias Ringwald     }
16966a048abSMatthias Ringwald     return -1;
17066a048abSMatthias Ringwald }
17166a048abSMatthias Ringwald 
17266a048abSMatthias Ringwald static inline int get_initiated_call_index(void){
17366a048abSMatthias Ringwald     int i ;
17466a048abSMatthias Ringwald     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
17566a048abSMatthias Ringwald         if (is_enhanced_call_status_initiated(i)) return i;
17674386ee0SMatthias Ringwald     }
17774386ee0SMatthias Ringwald     return -1;
17874386ee0SMatthias Ringwald }
17974386ee0SMatthias Ringwald 
1809cae807eSMatthias Ringwald static inline int get_next_free_slot(void){
18166a048abSMatthias Ringwald     int i ;
18266a048abSMatthias Ringwald     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
18366a048abSMatthias Ringwald         if (!gsm_calls[i].used_slot) return i;
18466a048abSMatthias Ringwald     }
18566a048abSMatthias Ringwald     return -1;
18674386ee0SMatthias Ringwald }
18774386ee0SMatthias Ringwald 
1889cae807eSMatthias Ringwald static inline int get_active_call_index(void){
18966a048abSMatthias Ringwald     return get_call_index_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_ACTIVE);
190d210d9c4SMatthias Ringwald }
191d210d9c4SMatthias Ringwald 
1929cae807eSMatthias Ringwald static inline int get_held_call_index(void){
19366a048abSMatthias Ringwald     return get_call_index_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_HELD);
194d210d9c4SMatthias Ringwald }
195d210d9c4SMatthias Ringwald 
1969cae807eSMatthias Ringwald static inline int get_response_held_call_index(void){
19766a048abSMatthias Ringwald     return get_call_index_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD);
198d210d9c4SMatthias Ringwald }
199d210d9c4SMatthias Ringwald 
2009cae807eSMatthias Ringwald static inline int get_number_none_calls(void){
20166a048abSMatthias Ringwald     int i, count = 0;
20266a048abSMatthias Ringwald     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
20366a048abSMatthias Ringwald         if (!gsm_calls[i].used_slot) count++;
20466a048abSMatthias Ringwald     }
20566a048abSMatthias Ringwald     return count;
206d210d9c4SMatthias Ringwald }
20774386ee0SMatthias Ringwald 
2089cae807eSMatthias Ringwald static inline int get_number_active_calls(void){
20966a048abSMatthias Ringwald     return get_number_calls_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_ACTIVE);
21074386ee0SMatthias Ringwald }
21174386ee0SMatthias Ringwald 
2129cae807eSMatthias Ringwald static inline int get_number_held_calls(void){
21366a048abSMatthias Ringwald     return get_number_calls_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_HELD);
21474386ee0SMatthias Ringwald }
21574386ee0SMatthias Ringwald 
2169cae807eSMatthias Ringwald static inline int get_number_response_held_calls(void){
21766a048abSMatthias Ringwald     return get_number_calls_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD);
218d210d9c4SMatthias Ringwald }
219d210d9c4SMatthias Ringwald 
2209cae807eSMatthias Ringwald static int next_call_index(void){
221d0c20769SMatthias Ringwald     return HFP_GSM_MAX_NR_CALLS + 1 - get_number_none_calls();
222d0c20769SMatthias Ringwald }
223d0c20769SMatthias Ringwald 
224d0c20769SMatthias Ringwald static void hfp_gsm_set_clip(int index_in_table, uint8_t type, const char * number){
2259cae807eSMatthias Ringwald     if (strlen(number) == 0) return;
2269cae807eSMatthias Ringwald 
227d0c20769SMatthias Ringwald     gsm_calls[index_in_table].clip_type = type;
228d0c20769SMatthias Ringwald 
229*c1ab6cc1SMatthias Ringwald     int clip_number_size = (strlen(number) < HFP_GSM_MAX_CALL_NUMBER_SIZE) ? (int) strlen(number) : (HFP_GSM_MAX_CALL_NUMBER_SIZE-1);
230d0c20769SMatthias Ringwald     strncpy(gsm_calls[index_in_table].clip_number, number, clip_number_size);
2319cae807eSMatthias Ringwald     gsm_calls[index_in_table].clip_number[clip_number_size] = '\0';
2329cae807eSMatthias Ringwald     strncpy(last_dialed_number, number, clip_number_size);
2339cae807eSMatthias Ringwald     last_dialed_number[clip_number_size] = '\0';
2349cae807eSMatthias Ringwald 
2359cae807eSMatthias Ringwald     clip_type = 0;
2369cae807eSMatthias Ringwald     memset(clip_number, 0, sizeof(clip_number));
237d0c20769SMatthias Ringwald }
238d0c20769SMatthias Ringwald 
239d0c20769SMatthias Ringwald static void delete_call(int delete_index_in_table){
240d0c20769SMatthias Ringwald     int i ;
241d0c20769SMatthias Ringwald     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
242d0c20769SMatthias Ringwald         if (gsm_calls[i].index > gsm_calls[delete_index_in_table].index){
243d0c20769SMatthias Ringwald             gsm_calls[i].index--;
244d0c20769SMatthias Ringwald         }
245d0c20769SMatthias Ringwald     }
24666a048abSMatthias Ringwald     free_call_slot(delete_index_in_table);
247d0c20769SMatthias Ringwald 
248d0c20769SMatthias Ringwald     gsm_calls[delete_index_in_table].clip_type = 0;
249d0c20769SMatthias Ringwald     gsm_calls[delete_index_in_table].index = 0;
250d0c20769SMatthias Ringwald     gsm_calls[delete_index_in_table].clip_number[0] = '\0';
2519cae807eSMatthias Ringwald     gsm_calls[delete_index_in_table].mpty = HFP_ENHANCED_CALL_MPTY_NOT_A_CONFERENCE_CALL;
252d0c20769SMatthias Ringwald }
253d0c20769SMatthias Ringwald 
2549cae807eSMatthias Ringwald 
2559cae807eSMatthias Ringwald static void create_call(hfp_enhanced_call_dir_t direction){
256d0c20769SMatthias Ringwald     int next_free_slot = get_next_free_slot();
2579cae807eSMatthias Ringwald     gsm_calls[next_free_slot].direction = direction;
258d0c20769SMatthias Ringwald     gsm_calls[next_free_slot].index = next_call_index();
25966a048abSMatthias Ringwald     set_enhanced_call_status_initiated(next_free_slot);
260d0c20769SMatthias Ringwald     gsm_calls[next_free_slot].clip_type = 0;
261d0c20769SMatthias Ringwald     gsm_calls[next_free_slot].clip_number[0] = '\0';
2629cae807eSMatthias Ringwald     gsm_calls[next_free_slot].mpty = HFP_ENHANCED_CALL_MPTY_NOT_A_CONFERENCE_CALL;
263d0c20769SMatthias Ringwald 
264d0c20769SMatthias Ringwald     hfp_gsm_set_clip(next_free_slot, clip_type, clip_number);
265d0c20769SMatthias Ringwald }
266d0c20769SMatthias Ringwald 
2679cae807eSMatthias Ringwald 
2689cae807eSMatthias Ringwald int hfp_gsm_get_number_of_calls(void){
2699cae807eSMatthias Ringwald     return HFP_GSM_MAX_NR_CALLS - get_number_none_calls();
2709cae807eSMatthias Ringwald }
2719cae807eSMatthias Ringwald 
2729cae807eSMatthias Ringwald void hfp_gsm_clear_last_dialed_number(void){
2739cae807eSMatthias Ringwald     memset(last_dialed_number, 0, sizeof(last_dialed_number));
2749cae807eSMatthias Ringwald }
2759cae807eSMatthias Ringwald 
2769cae807eSMatthias Ringwald char * hfp_gsm_last_dialed_number(void){
2779cae807eSMatthias Ringwald     return &last_dialed_number[0];
2789cae807eSMatthias Ringwald }
2799cae807eSMatthias Ringwald 
2809cae807eSMatthias Ringwald hfp_gsm_call_t * hfp_gsm_call(int call_index){
2819cae807eSMatthias Ringwald     int i;
2829cae807eSMatthias Ringwald 
2839cae807eSMatthias Ringwald     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
2849cae807eSMatthias Ringwald         hfp_gsm_call_t * call = &gsm_calls[i];
2859cae807eSMatthias Ringwald         if (call->index != call_index) continue;
2869cae807eSMatthias Ringwald         return call;
2879cae807eSMatthias Ringwald     }
2889cae807eSMatthias Ringwald     return NULL;
2899cae807eSMatthias Ringwald }
2909cae807eSMatthias Ringwald 
2919cae807eSMatthias Ringwald uint8_t hfp_gsm_clip_type(void){
292d0c20769SMatthias Ringwald     if (clip_type != 0) return clip_type;
293d0c20769SMatthias Ringwald 
294d0c20769SMatthias Ringwald     int initiated_call_index = get_initiated_call_index();
295d0c20769SMatthias Ringwald     if (initiated_call_index != -1){
296d0c20769SMatthias Ringwald         if (gsm_calls[initiated_call_index].clip_type != 0) {
297d0c20769SMatthias Ringwald             return gsm_calls[initiated_call_index].clip_type;
298d0c20769SMatthias Ringwald         }
299d0c20769SMatthias Ringwald     }
300d0c20769SMatthias Ringwald 
301d0c20769SMatthias Ringwald     int active_call_index = get_active_call_index();
302d0c20769SMatthias Ringwald     if (active_call_index != -1){
303d0c20769SMatthias Ringwald         if (gsm_calls[active_call_index].clip_type != 0) {
304d0c20769SMatthias Ringwald             return gsm_calls[active_call_index].clip_type;
305d0c20769SMatthias Ringwald         }
306d0c20769SMatthias Ringwald     }
307d0c20769SMatthias Ringwald     return 0;
308d0c20769SMatthias Ringwald }
309d0c20769SMatthias Ringwald 
3109cae807eSMatthias Ringwald char *  hfp_gsm_clip_number(void){
3119cae807eSMatthias Ringwald     if (strlen(clip_number) != 0) return clip_number;
312d0c20769SMatthias Ringwald 
313d0c20769SMatthias Ringwald     int initiated_call_index = get_initiated_call_index();
314d0c20769SMatthias Ringwald     if (initiated_call_index != -1){
315d0c20769SMatthias Ringwald         if (gsm_calls[initiated_call_index].clip_type != 0) {
316d0c20769SMatthias Ringwald             return gsm_calls[initiated_call_index].clip_number;
317d0c20769SMatthias Ringwald         }
318d0c20769SMatthias Ringwald     }
319d0c20769SMatthias Ringwald 
320d0c20769SMatthias Ringwald     int active_call_index = get_active_call_index();
321d0c20769SMatthias Ringwald     if (active_call_index != -1){
322d0c20769SMatthias Ringwald         if (gsm_calls[active_call_index].clip_type != 0) {
323d0c20769SMatthias Ringwald             return gsm_calls[active_call_index].clip_number;
324d0c20769SMatthias Ringwald         }
325d0c20769SMatthias Ringwald     }
326d0c20769SMatthias Ringwald     clip_number[0] = 0;
327d0c20769SMatthias Ringwald     return clip_number;
328d0c20769SMatthias Ringwald }
329d0c20769SMatthias Ringwald 
3309cae807eSMatthias Ringwald hfp_call_status_t hfp_gsm_call_status(void){
331d210d9c4SMatthias Ringwald     if (get_number_active_calls() + get_number_held_calls() + get_number_response_held_calls()){
33274386ee0SMatthias Ringwald         return HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
33374386ee0SMatthias Ringwald     }
33474386ee0SMatthias Ringwald     return HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
33574386ee0SMatthias Ringwald }
33674386ee0SMatthias Ringwald 
3379cae807eSMatthias Ringwald hfp_callheld_status_t hfp_gsm_callheld_status(void){
33874386ee0SMatthias Ringwald     // @note: order is important
33974386ee0SMatthias Ringwald     if (get_number_held_calls() == 0){
34074386ee0SMatthias Ringwald         return HFP_CALLHELD_STATUS_NO_CALLS_HELD;
34174386ee0SMatthias Ringwald     }
34274386ee0SMatthias Ringwald     if (get_number_active_calls() == 0) {
34374386ee0SMatthias Ringwald         return HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS;
34474386ee0SMatthias Ringwald     }
34574386ee0SMatthias Ringwald     return HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED;
34674386ee0SMatthias Ringwald }
34774386ee0SMatthias Ringwald 
3489cae807eSMatthias Ringwald hfp_callsetup_status_t hfp_gsm_callsetup_status(void){
34974386ee0SMatthias Ringwald     return callsetup_status;
35074386ee0SMatthias Ringwald }
35174386ee0SMatthias Ringwald 
3529cae807eSMatthias Ringwald static int hfp_gsm_response_held_active(void){
353d210d9c4SMatthias Ringwald     return get_response_held_call_index() != -1 ;
354d210d9c4SMatthias Ringwald }
355d210d9c4SMatthias Ringwald 
356d210d9c4SMatthias Ringwald int hfp_gsm_call_possible(void){
357d210d9c4SMatthias Ringwald     return get_number_none_calls() > 0;
358d210d9c4SMatthias Ringwald }
359d210d9c4SMatthias Ringwald 
36074386ee0SMatthias Ringwald void hfp_gsm_handle_event(hfp_ag_call_event_t event){
361d0c20769SMatthias Ringwald     hfp_gsm_handler(event, 0, 0, NULL);
362d0c20769SMatthias Ringwald }
363d0c20769SMatthias Ringwald 
364d0c20769SMatthias Ringwald void hfp_gsm_handle_event_with_clip(hfp_ag_call_event_t event, uint8_t type, const char * number){
365d0c20769SMatthias Ringwald     hfp_gsm_handler(event, 0, type, number);
366d0c20769SMatthias Ringwald }
367d0c20769SMatthias Ringwald 
368d0c20769SMatthias Ringwald void hfp_gsm_handle_event_with_call_index(hfp_ag_call_event_t event, uint8_t index){
369d0c20769SMatthias Ringwald     hfp_gsm_handler(event, index, 0, NULL);
370d0c20769SMatthias Ringwald }
371d0c20769SMatthias Ringwald 
3729cae807eSMatthias Ringwald void hfp_gsm_handle_event_with_call_number(hfp_ag_call_event_t event, const char * number){
3739cae807eSMatthias Ringwald     hfp_gsm_handler(event, 0, 0, number);
3749cae807eSMatthias Ringwald }
3759cae807eSMatthias Ringwald 
376d0c20769SMatthias Ringwald static void hfp_gsm_handler(hfp_ag_call_event_t event, uint8_t index, uint8_t type, const char * number){
377d0c20769SMatthias Ringwald     int next_free_slot = get_next_free_slot();
37874386ee0SMatthias Ringwald     int current_call_index = get_active_call_index();
379d210d9c4SMatthias Ringwald     int initiated_call_index = get_initiated_call_index();
380d210d9c4SMatthias Ringwald     int held_call_index = get_held_call_index();
381d0c20769SMatthias Ringwald     int i;
382d0c20769SMatthias Ringwald 
38374386ee0SMatthias Ringwald     switch (event){
38474386ee0SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED:
38574386ee0SMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:
38674386ee0SMatthias Ringwald             if (next_free_slot == -1){
387d210d9c4SMatthias Ringwald                 log_error("gsm: max call nr exceeded");
38874386ee0SMatthias Ringwald                 return;
38974386ee0SMatthias Ringwald             }
3909cae807eSMatthias Ringwald             create_call(HFP_ENHANCED_CALL_DIR_OUTGOING);
391d210d9c4SMatthias Ringwald             break;
39274386ee0SMatthias Ringwald 
393d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
394d210d9c4SMatthias Ringwald             if (current_call_index != -1){
395d0c20769SMatthias Ringwald                 delete_call(current_call_index);
396d210d9c4SMatthias Ringwald             }
39766a048abSMatthias Ringwald             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
398d210d9c4SMatthias Ringwald             break;
399d210d9c4SMatthias Ringwald 
400d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:
40174386ee0SMatthias Ringwald             if (current_call_index != -1){
40266a048abSMatthias Ringwald                 set_enhanced_call_status_held(current_call_index);
40374386ee0SMatthias Ringwald             }
40466a048abSMatthias Ringwald             set_callsetup_status(HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE);
40574386ee0SMatthias Ringwald             break;
406d210d9c4SMatthias Ringwald 
40774386ee0SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
408d210d9c4SMatthias Ringwald             if (current_call_index == -1){
409d210d9c4SMatthias Ringwald                 log_error("gsm: no active call");
410d210d9c4SMatthias Ringwald                 return;
411d210d9c4SMatthias Ringwald             }
41266a048abSMatthias Ringwald             set_callsetup_status(HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE);
41374386ee0SMatthias Ringwald             break;
41474386ee0SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:
41566a048abSMatthias Ringwald             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
41666a048abSMatthias Ringwald             set_enhanced_call_status_active(initiated_call_index);
41774386ee0SMatthias Ringwald             break;
418d210d9c4SMatthias Ringwald 
419d210d9c4SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
420d210d9c4SMatthias Ringwald             if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS) break;
42166a048abSMatthias Ringwald             set_callsetup_status(HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS);
4229cae807eSMatthias Ringwald             create_call(HFP_ENHANCED_CALL_DIR_INCOMING);
423d210d9c4SMatthias Ringwald             break;
424d210d9c4SMatthias Ringwald 
425d210d9c4SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
426d210d9c4SMatthias Ringwald             if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) break;
42766a048abSMatthias Ringwald             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
428d210d9c4SMatthias Ringwald 
429d210d9c4SMatthias Ringwald             if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
43066a048abSMatthias Ringwald                 set_enhanced_call_status_held(current_call_index);
431d210d9c4SMatthias Ringwald             }
43266a048abSMatthias Ringwald             set_enhanced_call_status_active(initiated_call_index);
433d210d9c4SMatthias Ringwald             break;
434d210d9c4SMatthias Ringwald 
435d210d9c4SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
436d210d9c4SMatthias Ringwald             if (hfp_gsm_call_status() != HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT) break;
437d210d9c4SMatthias Ringwald 
438d210d9c4SMatthias Ringwald             // TODO: is following condition correct? Can we join incoming call before it is answered?
439d210d9c4SMatthias Ringwald             if (callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
44066a048abSMatthias Ringwald                 set_enhanced_call_status_active(initiated_call_index);
44166a048abSMatthias Ringwald                 set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
4429cae807eSMatthias Ringwald             } else if (hfp_gsm_callheld_status() == HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED) {
44366a048abSMatthias Ringwald                 set_enhanced_call_status_active(held_call_index);
444d210d9c4SMatthias Ringwald             }
445d210d9c4SMatthias Ringwald 
4469cae807eSMatthias Ringwald             for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
44766a048abSMatthias Ringwald                 if (is_enhanced_call_status_active(i)){
4489cae807eSMatthias Ringwald                     gsm_calls[i].mpty = HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL;
4499cae807eSMatthias Ringwald                 }
450d210d9c4SMatthias Ringwald             }
451d210d9c4SMatthias Ringwald             break;
452d210d9c4SMatthias Ringwald 
453d210d9c4SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
454d210d9c4SMatthias Ringwald             if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) break;
455d210d9c4SMatthias Ringwald             if (hfp_gsm_call_status() != HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS) break;
45666a048abSMatthias Ringwald             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
45766a048abSMatthias Ringwald             set_enhanced_call_status_active(initiated_call_index);
458d210d9c4SMatthias Ringwald             break;
459d210d9c4SMatthias Ringwald 
460d210d9c4SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
461d210d9c4SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
462d210d9c4SMatthias Ringwald             if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) break;
463d210d9c4SMatthias Ringwald             if (hfp_gsm_call_status() != HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS) break;
46466a048abSMatthias Ringwald             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
46566a048abSMatthias Ringwald             set_enhanced_call_status_response_hold(initiated_call_index);
466d210d9c4SMatthias Ringwald             break;
467d210d9c4SMatthias Ringwald 
468d210d9c4SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
469d210d9c4SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
470d210d9c4SMatthias Ringwald             if (!hfp_gsm_response_held_active()) break;
47166a048abSMatthias Ringwald             set_enhanced_call_status_active(get_response_held_call_index());
472d210d9c4SMatthias Ringwald             break;
473d210d9c4SMatthias Ringwald 
474d210d9c4SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
475d210d9c4SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
476d210d9c4SMatthias Ringwald             if (!hfp_gsm_response_held_active()) break;
477d0c20769SMatthias Ringwald             delete_call(get_response_held_call_index());
478d210d9c4SMatthias Ringwald             break;
479d210d9c4SMatthias Ringwald 
480d210d9c4SMatthias Ringwald 
481d210d9c4SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
482d210d9c4SMatthias Ringwald             switch (hfp_gsm_call_status()){
483d210d9c4SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
48466a048abSMatthias Ringwald                     set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
485d210d9c4SMatthias Ringwald                     break;
486d210d9c4SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
487d0c20769SMatthias Ringwald                     delete_call(current_call_index);
488d210d9c4SMatthias Ringwald                     break;
489d210d9c4SMatthias Ringwald             }
490d210d9c4SMatthias Ringwald             break;
491d210d9c4SMatthias Ringwald 
492d210d9c4SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
493d210d9c4SMatthias Ringwald             switch (hfp_gsm_call_status()){
494d210d9c4SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
495d210d9c4SMatthias Ringwald                     if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) break;
49666a048abSMatthias Ringwald                     set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
497d210d9c4SMatthias Ringwald                     break;
498d210d9c4SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
49966a048abSMatthias Ringwald                     set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
500d0c20769SMatthias Ringwald                     delete_call(current_call_index);
501d210d9c4SMatthias Ringwald                     break;
502d210d9c4SMatthias Ringwald                 default:
503d210d9c4SMatthias Ringwald                     break;
504d210d9c4SMatthias Ringwald             }
505d210d9c4SMatthias Ringwald             break;
506d210d9c4SMatthias Ringwald 
507d0c20769SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
50866a048abSMatthias Ringwald             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
509d210d9c4SMatthias Ringwald             if (hfp_gsm_call_status() != HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT) break;
510d210d9c4SMatthias Ringwald 
511d210d9c4SMatthias Ringwald             for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
512d0c20769SMatthias Ringwald                 delete_call(i);
513d210d9c4SMatthias Ringwald             }
514d210d9c4SMatthias Ringwald             break;
515d0c20769SMatthias Ringwald 
516d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
517d210d9c4SMatthias Ringwald             // Held or waiting call gets active,
51866a048abSMatthias Ringwald             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
51966a048abSMatthias Ringwald             free_call_slot(initiated_call_index);
52066a048abSMatthias Ringwald             set_enhanced_call_status_active(held_call_index);
521d210d9c4SMatthias Ringwald             break;
522d210d9c4SMatthias Ringwald 
523d0c20769SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:
524*c1ab6cc1SMatthias Ringwald             if ((index != 0) && (index <= HFP_GSM_MAX_NR_CALLS) ){
525d0c20769SMatthias Ringwald                 for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
526d0c20769SMatthias Ringwald                     if (gsm_calls[i].index == index){
527d0c20769SMatthias Ringwald                         delete_call(i);
528d0c20769SMatthias Ringwald                         continue;
529d0c20769SMatthias Ringwald                     }
530d0c20769SMatthias Ringwald                 }
531d0c20769SMatthias Ringwald             } else {
532d210d9c4SMatthias Ringwald                 for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
53366a048abSMatthias Ringwald                     if (is_enhanced_call_status_active(i)){
534d0c20769SMatthias Ringwald                         delete_call(i);
535d0c20769SMatthias Ringwald                     }
536d210d9c4SMatthias Ringwald                 }
537d210d9c4SMatthias Ringwald             }
538d210d9c4SMatthias Ringwald 
539d210d9c4SMatthias Ringwald             if (callsetup_status != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS){
54066a048abSMatthias Ringwald                 set_enhanced_call_status_active(initiated_call_index);
541d210d9c4SMatthias Ringwald             } else {
54266a048abSMatthias Ringwald                 set_enhanced_call_status_active(held_call_index);
543d210d9c4SMatthias Ringwald             }
544d0c20769SMatthias Ringwald 
54566a048abSMatthias Ringwald             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
546d210d9c4SMatthias Ringwald             break;
547d0c20769SMatthias Ringwald 
548d0c20769SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:
549d210d9c4SMatthias Ringwald             for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
550*c1ab6cc1SMatthias Ringwald                 if (is_enhanced_call_status_active(i) && (gsm_calls[i].index != index)){
55166a048abSMatthias Ringwald                     set_enhanced_call_status_held(i);
552d210d9c4SMatthias Ringwald                 }
553d210d9c4SMatthias Ringwald             }
554d210d9c4SMatthias Ringwald 
555d210d9c4SMatthias Ringwald             if (callsetup_status != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS){
55666a048abSMatthias Ringwald                 set_enhanced_call_status_active(initiated_call_index);
557d210d9c4SMatthias Ringwald             } else {
55866a048abSMatthias Ringwald                 set_enhanced_call_status_active(held_call_index);
559d210d9c4SMatthias Ringwald             }
56066a048abSMatthias Ringwald             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
561d210d9c4SMatthias Ringwald             break;
562d0c20769SMatthias Ringwald 
563d0c20769SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
564d210d9c4SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
565d210d9c4SMatthias Ringwald                 for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
56666a048abSMatthias Ringwald                     if (gsm_calls[i].used_slot){
56766a048abSMatthias Ringwald                         set_enhanced_call_status_active(i);
5689cae807eSMatthias Ringwald                         gsm_calls[i].mpty = HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL;
569d210d9c4SMatthias Ringwald                     }
570d210d9c4SMatthias Ringwald                 }
571d210d9c4SMatthias Ringwald             }
572d210d9c4SMatthias Ringwald             break;
573d0c20769SMatthias Ringwald 
574d0c20769SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
575d210d9c4SMatthias Ringwald             for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
576d0c20769SMatthias Ringwald                 delete_call(i);
577d210d9c4SMatthias Ringwald             }
578d0c20769SMatthias Ringwald             break;
579d210d9c4SMatthias Ringwald 
580d0c20769SMatthias Ringwald         case HFP_AG_SET_CLIP:
581d0c20769SMatthias Ringwald             if (initiated_call_index != -1){
582d0c20769SMatthias Ringwald                 hfp_gsm_set_clip(initiated_call_index, type, number);
583d210d9c4SMatthias Ringwald                 break;
584d210d9c4SMatthias Ringwald             }
5859cae807eSMatthias Ringwald 
586d0c20769SMatthias Ringwald             clip_type = type;
587d0c20769SMatthias Ringwald             strncpy(clip_number, number, sizeof(clip_number));
588d0c20769SMatthias Ringwald             clip_number[sizeof(clip_number)-1] = '\0';
589d0c20769SMatthias Ringwald 
590d0c20769SMatthias Ringwald             break;
59174386ee0SMatthias Ringwald         default:
59274386ee0SMatthias Ringwald             break;
59374386ee0SMatthias Ringwald     }
59474386ee0SMatthias Ringwald }
595