xref: /btstack/src/classic/hfp_gsm_model.c (revision 9dc32eb425c45f51cd4be899bc05cae06f314fba)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "hfp_gsm_model.c"
39 
40 // *****************************************************************************
41 //
42 // GSM Model
43 //
44 // *****************************************************************************
45 
46 #include "btstack_config.h"
47 
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include "btstack_memory.h"
54 #include "classic/core.h"
55 #include "classic/hfp.h"
56 #include "classic/hfp_gsm_model.h"
57 #include "classic/sdp_server.h"
58 #include "classic/sdp_client_rfcomm.h"
59 #include "btstack_debug.h"
60 #include "hci.h"
61 #include "hci_cmd.h"
62 #include "hci_dump.h"
63 #include "l2cap.h"
64 #include "btstack_run_loop.h"
65 
66 #define HFP_GSM_MAX_NR_CALLS 3
67 #define HFP_GSM_MAX_CALL_NUMBER_SIZE 25
68 
69 static hfp_gsm_call_t gsm_calls[HFP_GSM_MAX_NR_CALLS];
70 static hfp_callsetup_status_t callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
71 
72 static uint8_t clip_type;
73 static char clip_number[HFP_GSM_MAX_CALL_NUMBER_SIZE];
74 static char last_dialed_number[HFP_GSM_MAX_CALL_NUMBER_SIZE];
75 
76 static void hfp_gsm_handler(hfp_ag_call_event_t event, uint8_t index, uint8_t type, const char * number);
77 static inline int get_number_active_calls(void);
78 
79 static void set_callsetup_status(hfp_callsetup_status_t status){
80     callsetup_status = status;
81     if (callsetup_status != HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE) return;
82 
83     int i ;
84     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
85         if (gsm_calls[i].direction == HFP_ENHANCED_CALL_DIR_OUTGOING){
86             gsm_calls[i].enhanced_status = HFP_ENHANCED_CALL_STATUS_OUTGOING_ALERTING;
87         }
88     }
89 }
90 
91 static inline void set_enhanced_call_status_active(int index_in_table){
92     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return;
93     gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_ACTIVE;
94     gsm_calls[index_in_table].used_slot = 1;
95 }
96 
97 static inline void set_enhanced_call_status_held(int index_in_table){
98     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return;
99     gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_HELD;
100     gsm_calls[index_in_table].used_slot = 1;
101 }
102 
103 static inline void set_enhanced_call_status_response_hold(int index_in_table){
104     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return;
105     gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD;
106     gsm_calls[index_in_table].used_slot = 1;
107 }
108 
109 static inline void set_enhanced_call_status_initiated(int index_in_table){
110     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return;
111     if (gsm_calls[index_in_table].direction == HFP_ENHANCED_CALL_DIR_OUTGOING){
112         gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_OUTGOING_DIALING;
113     } else {
114         if (get_number_active_calls() > 0){
115             gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_INCOMING_WAITING;
116         } else {
117             gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_INCOMING;
118         }
119     }
120     gsm_calls[index_in_table].used_slot = 1;
121 }
122 
123 static int get_enhanced_call_status(int index_in_table){
124     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return -1;
125     if (!gsm_calls[index_in_table].used_slot) return -1;
126     return gsm_calls[index_in_table].enhanced_status;
127 }
128 
129 static inline int is_enhanced_call_status_active(int index_in_table){
130     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return 0;
131     return get_enhanced_call_status(index_in_table) == HFP_ENHANCED_CALL_STATUS_ACTIVE;
132 }
133 
134 static inline int is_enhanced_call_status_initiated(int index_in_table){
135     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return 0;
136     switch (get_enhanced_call_status(index_in_table)){
137         case HFP_ENHANCED_CALL_STATUS_OUTGOING_DIALING:
138         case HFP_ENHANCED_CALL_STATUS_OUTGOING_ALERTING:
139         case HFP_ENHANCED_CALL_STATUS_INCOMING:
140         case HFP_ENHANCED_CALL_STATUS_INCOMING_WAITING:
141             return 1;
142         default:
143             return 0;
144     }
145 }
146 
147 static void free_call_slot(int index_in_table){
148     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return;
149     gsm_calls[index_in_table].used_slot = 0;
150 }
151 
152 void hfp_gsm_init(void){
153     set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
154     clip_type = 0;
155     memset(clip_number, 0, sizeof(clip_number));
156     memset(last_dialed_number, 0, sizeof(last_dialed_number));
157     memset(gsm_calls, 0, sizeof(gsm_calls));
158     int i;
159     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
160         free_call_slot(i);
161     }
162 }
163 
164 static int get_number_calls_with_enhanced_status(hfp_enhanced_call_status_t enhanced_status){
165     int i, count = 0;
166     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
167         if (get_enhanced_call_status(i) == (int) enhanced_status) count++;
168     }
169     return count;
170 }
171 
172 static int get_call_index_with_enhanced_status(hfp_enhanced_call_status_t enhanced_status){
173     int i ;
174     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
175         if (get_enhanced_call_status(i) == (int) enhanced_status) return i;
176     }
177     return -1;
178 }
179 
180 static inline int get_initiated_call_index(void){
181     int i ;
182     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
183         if (is_enhanced_call_status_initiated(i)) return i;
184     }
185     return -1;
186 }
187 
188 static inline int get_next_free_slot(void){
189     int i ;
190     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
191         if (!gsm_calls[i].used_slot) return i;
192     }
193     return -1;
194 }
195 
196 static inline int get_active_call_index(void){
197     return get_call_index_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_ACTIVE);
198 }
199 
200 static inline int get_held_call_index(void){
201     return get_call_index_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_HELD);
202 }
203 
204 static inline int get_response_held_call_index(void){
205     return get_call_index_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD);
206 }
207 
208 static inline int get_number_none_calls(void){
209     int i, count = 0;
210     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
211         if (!gsm_calls[i].used_slot) count++;
212     }
213     return count;
214 }
215 
216 static inline int get_number_active_calls(void){
217     return get_number_calls_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_ACTIVE);
218 }
219 
220 static inline int get_number_held_calls(void){
221     return get_number_calls_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_HELD);
222 }
223 
224 static inline int get_number_response_held_calls(void){
225     return get_number_calls_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD);
226 }
227 
228 static int next_call_index(void){
229     return HFP_GSM_MAX_NR_CALLS + 1 - get_number_none_calls();
230 }
231 
232 static void hfp_gsm_set_clip(int index_in_table, uint8_t type, const char * number){
233     uint16_t number_str_len = (uint16_t) strlen(number);
234     if (number_str_len == 0) return;
235 
236     gsm_calls[index_in_table].clip_type = type;
237     int clip_number_size = btstack_min(number_str_len, HFP_GSM_MAX_CALL_NUMBER_SIZE - 1);
238     strncpy(gsm_calls[index_in_table].clip_number, number, clip_number_size);
239     gsm_calls[index_in_table].clip_number[clip_number_size] = '\0';
240     strncpy(last_dialed_number, number, clip_number_size);
241     last_dialed_number[clip_number_size] = '\0';
242 
243     clip_type = 0;
244     memset(clip_number, 0, sizeof(clip_number));
245 }
246 
247 static void delete_call(int delete_index_in_table){
248     int i ;
249     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
250         if (gsm_calls[i].index > gsm_calls[delete_index_in_table].index){
251             gsm_calls[i].index--;
252         }
253     }
254     free_call_slot(delete_index_in_table);
255 
256     gsm_calls[delete_index_in_table].clip_type = 0;
257     gsm_calls[delete_index_in_table].index = 0;
258     gsm_calls[delete_index_in_table].clip_number[0] = '\0';
259     gsm_calls[delete_index_in_table].mpty = HFP_ENHANCED_CALL_MPTY_NOT_A_CONFERENCE_CALL;
260 }
261 
262 
263 static void create_call(hfp_enhanced_call_dir_t direction){
264     int next_free_slot = get_next_free_slot();
265     gsm_calls[next_free_slot].direction = direction;
266     gsm_calls[next_free_slot].index = next_call_index();
267     set_enhanced_call_status_initiated(next_free_slot);
268     gsm_calls[next_free_slot].clip_type = 0;
269     gsm_calls[next_free_slot].clip_number[0] = '\0';
270     gsm_calls[next_free_slot].mpty = HFP_ENHANCED_CALL_MPTY_NOT_A_CONFERENCE_CALL;
271 
272     hfp_gsm_set_clip(next_free_slot, clip_type, clip_number);
273 }
274 
275 
276 int hfp_gsm_get_number_of_calls(void){
277     return HFP_GSM_MAX_NR_CALLS - get_number_none_calls();
278 }
279 
280 void hfp_gsm_clear_last_dialed_number(void){
281     memset(last_dialed_number, 0, sizeof(last_dialed_number));
282 }
283 
284 char * hfp_gsm_last_dialed_number(void){
285     return &last_dialed_number[0];
286 }
287 
288 hfp_gsm_call_t * hfp_gsm_call(int call_index){
289     int i;
290 
291     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
292         hfp_gsm_call_t * call = &gsm_calls[i];
293         if (call->index != call_index) continue;
294         return call;
295     }
296     return NULL;
297 }
298 
299 uint8_t hfp_gsm_clip_type(void){
300     if (clip_type != 0) return clip_type;
301 
302     int initiated_call_index = get_initiated_call_index();
303     if (initiated_call_index != -1){
304         if (gsm_calls[initiated_call_index].clip_type != 0) {
305             return gsm_calls[initiated_call_index].clip_type;
306         }
307     }
308 
309     int active_call_index = get_active_call_index();
310     if (active_call_index != -1){
311         if (gsm_calls[active_call_index].clip_type != 0) {
312             return gsm_calls[active_call_index].clip_type;
313         }
314     }
315     return 0;
316 }
317 
318 char *  hfp_gsm_clip_number(void){
319     if (strlen(clip_number) != 0) return clip_number;
320 
321     int initiated_call_index = get_initiated_call_index();
322     if (initiated_call_index != -1){
323         if (gsm_calls[initiated_call_index].clip_type != 0) {
324             return gsm_calls[initiated_call_index].clip_number;
325         }
326     }
327 
328     int active_call_index = get_active_call_index();
329     if (active_call_index != -1){
330         if (gsm_calls[active_call_index].clip_type != 0) {
331             return gsm_calls[active_call_index].clip_number;
332         }
333     }
334     clip_number[0] = 0;
335     return clip_number;
336 }
337 
338 hfp_call_status_t hfp_gsm_call_status(void){
339     if (get_number_active_calls() + get_number_held_calls() + get_number_response_held_calls()){
340         return HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
341     }
342     return HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
343 }
344 
345 hfp_callheld_status_t hfp_gsm_callheld_status(void){
346     // @note: order is important
347     if (get_number_held_calls() == 0){
348         return HFP_CALLHELD_STATUS_NO_CALLS_HELD;
349     }
350     if (get_number_active_calls() == 0) {
351         return HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS;
352     }
353     return HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED;
354 }
355 
356 hfp_callsetup_status_t hfp_gsm_callsetup_status(void){
357     return callsetup_status;
358 }
359 
360 static int hfp_gsm_response_held_active(void){
361     return get_response_held_call_index() != -1 ;
362 }
363 
364 int hfp_gsm_call_possible(void){
365     return get_number_none_calls() > 0;
366 }
367 
368 void hfp_gsm_handle_event(hfp_ag_call_event_t event){
369     hfp_gsm_handler(event, 0, 0, NULL);
370 }
371 
372 void hfp_gsm_handle_event_with_clip(hfp_ag_call_event_t event, uint8_t type, const char * number){
373     hfp_gsm_handler(event, 0, type, number);
374 }
375 
376 void hfp_gsm_handle_event_with_call_index(hfp_ag_call_event_t event, uint8_t index){
377     hfp_gsm_handler(event, index, 0, NULL);
378 }
379 
380 void hfp_gsm_handle_event_with_call_number(hfp_ag_call_event_t event, const char * number){
381     hfp_gsm_handler(event, 0, 0, number);
382 }
383 
384 static void hfp_gsm_handler(hfp_ag_call_event_t event, uint8_t index, uint8_t type, const char * number){
385     int next_free_slot = get_next_free_slot();
386     int current_call_index = get_active_call_index();
387     int initiated_call_index = get_initiated_call_index();
388     int held_call_index = get_held_call_index();
389     int i;
390 
391     switch (event){
392         case HFP_AG_OUTGOING_CALL_INITIATED:
393         case HFP_AG_OUTGOING_REDIAL_INITIATED:
394             if (next_free_slot == -1){
395                 log_error("gsm: max call nr exceeded");
396                 return;
397             }
398             create_call(HFP_ENHANCED_CALL_DIR_OUTGOING);
399             break;
400 
401         case HFP_AG_OUTGOING_CALL_REJECTED:
402             if (current_call_index != -1){
403                 delete_call(current_call_index);
404             }
405             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
406             break;
407 
408         case HFP_AG_OUTGOING_CALL_ACCEPTED:
409             if (current_call_index != -1){
410                 set_enhanced_call_status_held(current_call_index);
411             }
412             set_callsetup_status(HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE);
413             break;
414 
415         case HFP_AG_OUTGOING_CALL_RINGING:
416             if (current_call_index == -1){
417                 log_error("gsm: no active call");
418                 return;
419             }
420             set_callsetup_status(HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE);
421             break;
422         case HFP_AG_OUTGOING_CALL_ESTABLISHED:
423             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
424             set_enhanced_call_status_active(initiated_call_index);
425             break;
426 
427         case HFP_AG_INCOMING_CALL:
428             if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS) break;
429             set_callsetup_status(HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS);
430             create_call(HFP_ENHANCED_CALL_DIR_INCOMING);
431             break;
432 
433         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
434             if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) break;
435             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
436 
437             if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
438                 set_enhanced_call_status_held(current_call_index);
439             }
440             set_enhanced_call_status_active(initiated_call_index);
441             break;
442 
443         case HFP_AG_HELD_CALL_JOINED_BY_AG:
444             if (hfp_gsm_call_status() != HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT) break;
445 
446             // TODO: is following condition correct? Can we join incoming call before it is answered?
447             if (callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
448                 set_enhanced_call_status_active(initiated_call_index);
449                 set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
450             } else if (hfp_gsm_callheld_status() == HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED) {
451                 set_enhanced_call_status_active(held_call_index);
452             }
453 
454             for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
455                 if (is_enhanced_call_status_active(i)){
456                     gsm_calls[i].mpty = HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL;
457                 }
458             }
459             break;
460 
461         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
462             if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) break;
463             if (hfp_gsm_call_status() != HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS) break;
464             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
465             set_enhanced_call_status_active(initiated_call_index);
466             break;
467 
468         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
469         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
470             if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) break;
471             if (hfp_gsm_call_status() != HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS) break;
472             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
473             set_enhanced_call_status_response_hold(initiated_call_index);
474             break;
475 
476         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
477         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
478             if (!hfp_gsm_response_held_active()) break;
479             set_enhanced_call_status_active(get_response_held_call_index());
480             break;
481 
482         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
483         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
484             if (!hfp_gsm_response_held_active()) break;
485             delete_call(get_response_held_call_index());
486             break;
487 
488 
489         case HFP_AG_TERMINATE_CALL_BY_HF:
490             switch (hfp_gsm_call_status()){
491                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
492                     set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
493                     break;
494                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
495                     delete_call(current_call_index);
496                     break;
497             }
498             break;
499 
500         case HFP_AG_TERMINATE_CALL_BY_AG:
501             switch (hfp_gsm_call_status()){
502                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
503                     if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) break;
504                     set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
505                     break;
506                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
507                     set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
508                     delete_call(current_call_index);
509                     break;
510                 default:
511                     break;
512             }
513             break;
514 
515         case HFP_AG_CALL_DROPPED:
516             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
517             if (hfp_gsm_call_status() != HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT) break;
518 
519             for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
520                 delete_call(i);
521             }
522             break;
523 
524         case HFP_AG_CALL_HOLD_USER_BUSY:
525             // Held or waiting call gets active,
526             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
527             free_call_slot(initiated_call_index);
528             set_enhanced_call_status_active(held_call_index);
529             break;
530 
531         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:
532             if ((index != 0) && (index <= HFP_GSM_MAX_NR_CALLS) ){
533                 for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
534                     if (gsm_calls[i].index == index){
535                         delete_call(i);
536                         continue;
537                     }
538                 }
539             } else {
540                 for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
541                     if (is_enhanced_call_status_active(i)){
542                         delete_call(i);
543                     }
544                 }
545             }
546 
547             if (callsetup_status != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS){
548                 set_enhanced_call_status_active(initiated_call_index);
549             } else {
550                 set_enhanced_call_status_active(held_call_index);
551             }
552 
553             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
554             break;
555 
556         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:
557             for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
558                 if (is_enhanced_call_status_active(i) && (gsm_calls[i].index != index)){
559                     set_enhanced_call_status_held(i);
560                 }
561             }
562 
563             if (callsetup_status != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS){
564                 set_enhanced_call_status_active(initiated_call_index);
565             } else {
566                 set_enhanced_call_status_active(held_call_index);
567             }
568             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
569             break;
570 
571         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
572             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
573                 for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
574                     if (gsm_calls[i].used_slot){
575                         set_enhanced_call_status_active(i);
576                         gsm_calls[i].mpty = HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL;
577                     }
578                 }
579             }
580             break;
581 
582         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
583             for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
584                 delete_call(i);
585             }
586             break;
587 
588         case HFP_AG_SET_CLIP:
589             if (initiated_call_index != -1){
590                 hfp_gsm_set_clip(initiated_call_index, type, number);
591                 break;
592             }
593 
594             clip_type = type;
595             strncpy(clip_number, number, sizeof(clip_number));
596             clip_number[sizeof(clip_number)-1] = '\0';
597 
598             break;
599         default:
600             break;
601     }
602 }
603