xref: /btstack/src/classic/hfp_gsm_model.c (revision a8d51f092f1b660d0f6921369ad2bc3f9368296c)
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 <string.h>
50 
51 #include "btstack_memory.h"
52 #include "classic/core.h"
53 #include "classic/hfp.h"
54 #include "classic/hfp_gsm_model.h"
55 #include "classic/sdp_server.h"
56 #include "classic/sdp_client_rfcomm.h"
57 #include "btstack_debug.h"
58 #include "hci.h"
59 #include "hci_cmd.h"
60 #include "hci_dump.h"
61 #include "l2cap.h"
62 #include "btstack_run_loop.h"
63 
64 #define HFP_GSM_MAX_NR_CALLS 3
65 #define HFP_GSM_MAX_CALL_NUMBER_SIZE 25
66 
67 static hfp_gsm_call_t gsm_calls[HFP_GSM_MAX_NR_CALLS];
68 static hfp_callsetup_status_t callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
69 
70 static uint8_t clip_type;
71 static char clip_number[HFP_GSM_MAX_CALL_NUMBER_SIZE];
72 static char last_dialed_number[HFP_GSM_MAX_CALL_NUMBER_SIZE];
73 
74 static void hfp_gsm_handler(hfp_ag_call_event_t event, uint8_t index, uint8_t type, const char * number);
75 static inline int get_number_active_calls(void);
76 
77 static void set_callsetup_status(hfp_callsetup_status_t status){
78     callsetup_status = status;
79     if (callsetup_status != HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE) return;
80 
81     int i ;
82     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
83         if (gsm_calls[i].direction == HFP_ENHANCED_CALL_DIR_OUTGOING){
84             gsm_calls[i].enhanced_status = HFP_ENHANCED_CALL_STATUS_OUTGOING_ALERTING;
85         }
86     }
87 }
88 
89 static inline void set_enhanced_call_status_active(int index_in_table){
90     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return;
91     gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_ACTIVE;
92     gsm_calls[index_in_table].used_slot = 1;
93 }
94 
95 static inline void set_enhanced_call_status_held(int index_in_table){
96     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return;
97     gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_HELD;
98     gsm_calls[index_in_table].used_slot = 1;
99 }
100 
101 static inline void set_enhanced_call_status_response_hold(int index_in_table){
102     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return;
103     gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD;
104     gsm_calls[index_in_table].used_slot = 1;
105 }
106 
107 static inline void set_enhanced_call_status_initiated(int index_in_table){
108     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return;
109     if (gsm_calls[index_in_table].direction == HFP_ENHANCED_CALL_DIR_OUTGOING){
110         gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_OUTGOING_DIALING;
111     } else {
112         if (get_number_active_calls() > 0){
113             gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_INCOMING_WAITING;
114         } else {
115             gsm_calls[index_in_table].enhanced_status = HFP_ENHANCED_CALL_STATUS_INCOMING;
116         }
117     }
118     gsm_calls[index_in_table].used_slot = 1;
119 }
120 
121 static int get_enhanced_call_status(int index_in_table){
122     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return -1;
123     if (!gsm_calls[index_in_table].used_slot) return -1;
124     return gsm_calls[index_in_table].enhanced_status;
125 }
126 
127 static inline int is_enhanced_call_status_active(int index_in_table){
128     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return 0;
129     return get_enhanced_call_status(index_in_table) == HFP_ENHANCED_CALL_STATUS_ACTIVE;
130 }
131 
132 static inline int is_enhanced_call_status_initiated(int index_in_table){
133     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return 0;
134     switch (get_enhanced_call_status(index_in_table)){
135         case HFP_ENHANCED_CALL_STATUS_OUTGOING_DIALING:
136         case HFP_ENHANCED_CALL_STATUS_OUTGOING_ALERTING:
137         case HFP_ENHANCED_CALL_STATUS_INCOMING:
138         case HFP_ENHANCED_CALL_STATUS_INCOMING_WAITING:
139             return 1;
140         default:
141             return 0;
142     }
143 }
144 
145 static void free_call_slot(int index_in_table){
146     if ((index_in_table < 0) || (index_in_table > HFP_GSM_MAX_NR_CALLS)) return;
147     gsm_calls[index_in_table].used_slot = 0;
148 }
149 
150 void hfp_gsm_init(void){
151     set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
152     clip_type = 0;
153     memset(clip_number, 0, sizeof(clip_number));
154     memset(last_dialed_number, 0, sizeof(last_dialed_number));
155     memset(gsm_calls, 0, sizeof(gsm_calls));
156     int i;
157     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
158         free_call_slot(i);
159     }
160 }
161 
162 static int get_number_calls_with_enhanced_status(hfp_enhanced_call_status_t enhanced_status){
163     int i, count = 0;
164     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
165         if (get_enhanced_call_status(i) == (int) enhanced_status) count++;
166     }
167     return count;
168 }
169 
170 static int get_call_index_with_enhanced_status(hfp_enhanced_call_status_t enhanced_status){
171     int i ;
172     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
173         if (get_enhanced_call_status(i) == (int) enhanced_status) return i;
174     }
175     return -1;
176 }
177 
178 static inline int get_initiated_call_index(void){
179     int i ;
180     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
181         if (is_enhanced_call_status_initiated(i)) return i;
182     }
183     return -1;
184 }
185 
186 static inline int get_next_free_slot(void){
187     int i ;
188     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
189         if (!gsm_calls[i].used_slot) return i;
190     }
191     return -1;
192 }
193 
194 static inline int get_active_call_index(void){
195     return get_call_index_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_ACTIVE);
196 }
197 
198 static inline int get_held_call_index(void){
199     return get_call_index_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_HELD);
200 }
201 
202 static inline int get_response_held_call_index(void){
203     return get_call_index_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD);
204 }
205 
206 static inline int get_number_none_calls(void){
207     int i, count = 0;
208     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
209         if (!gsm_calls[i].used_slot) count++;
210     }
211     return count;
212 }
213 
214 static inline int get_number_active_calls(void){
215     return get_number_calls_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_ACTIVE);
216 }
217 
218 static inline int get_number_held_calls(void){
219     return get_number_calls_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_HELD);
220 }
221 
222 static inline int get_number_response_held_calls(void){
223     return get_number_calls_with_enhanced_status(HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD);
224 }
225 
226 static int next_call_index(void){
227     return HFP_GSM_MAX_NR_CALLS + 1 - get_number_none_calls();
228 }
229 
230 static void hfp_gsm_set_clip(int index_in_table, uint8_t type, const char * number){
231     uint16_t number_str_len = (uint16_t) strlen(number);
232     if (number_str_len == 0) return;
233 
234     gsm_calls[index_in_table].clip_type = type;
235     int clip_number_size = btstack_min(number_str_len, HFP_GSM_MAX_CALL_NUMBER_SIZE - 1);
236     strncpy(gsm_calls[index_in_table].clip_number, number, clip_number_size);
237     gsm_calls[index_in_table].clip_number[clip_number_size] = '\0';
238     strncpy(last_dialed_number, number, clip_number_size);
239     last_dialed_number[clip_number_size] = '\0';
240 
241     clip_type = 0;
242     memset(clip_number, 0, sizeof(clip_number));
243 }
244 
245 static void delete_call(int delete_index_in_table){
246     int i ;
247     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
248         if (gsm_calls[i].index > gsm_calls[delete_index_in_table].index){
249             gsm_calls[i].index--;
250         }
251     }
252     free_call_slot(delete_index_in_table);
253 
254     gsm_calls[delete_index_in_table].clip_type = 0;
255     gsm_calls[delete_index_in_table].index = 0;
256     gsm_calls[delete_index_in_table].clip_number[0] = '\0';
257     gsm_calls[delete_index_in_table].mpty = HFP_ENHANCED_CALL_MPTY_NOT_A_CONFERENCE_CALL;
258 }
259 
260 
261 static void create_call(hfp_enhanced_call_dir_t direction){
262     int next_free_slot = get_next_free_slot();
263     gsm_calls[next_free_slot].direction = direction;
264     gsm_calls[next_free_slot].index = next_call_index();
265     set_enhanced_call_status_initiated(next_free_slot);
266     gsm_calls[next_free_slot].clip_type = 0;
267     gsm_calls[next_free_slot].clip_number[0] = '\0';
268     gsm_calls[next_free_slot].mpty = HFP_ENHANCED_CALL_MPTY_NOT_A_CONFERENCE_CALL;
269 
270     hfp_gsm_set_clip(next_free_slot, clip_type, clip_number);
271 }
272 
273 
274 int hfp_gsm_get_number_of_calls(void){
275     return HFP_GSM_MAX_NR_CALLS - get_number_none_calls();
276 }
277 
278 void hfp_gsm_clear_last_dialed_number(void){
279     memset(last_dialed_number, 0, sizeof(last_dialed_number));
280 }
281 
282 char * hfp_gsm_last_dialed_number(void){
283     return &last_dialed_number[0];
284 }
285 
286 hfp_gsm_call_t * hfp_gsm_call(int call_index){
287     int i;
288 
289     for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
290         hfp_gsm_call_t * call = &gsm_calls[i];
291         if (call->index != call_index) continue;
292         return call;
293     }
294     return NULL;
295 }
296 
297 uint8_t hfp_gsm_clip_type(void){
298     if (clip_type != 0) return clip_type;
299 
300     int initiated_call_index = get_initiated_call_index();
301     if (initiated_call_index != -1){
302         if (gsm_calls[initiated_call_index].clip_type != 0) {
303             return gsm_calls[initiated_call_index].clip_type;
304         }
305     }
306 
307     int active_call_index = get_active_call_index();
308     if (active_call_index != -1){
309         if (gsm_calls[active_call_index].clip_type != 0) {
310             return gsm_calls[active_call_index].clip_type;
311         }
312     }
313     return 0;
314 }
315 
316 char *  hfp_gsm_clip_number(void){
317     if (strlen(clip_number) != 0) return clip_number;
318 
319     int initiated_call_index = get_initiated_call_index();
320     if (initiated_call_index != -1){
321         if (gsm_calls[initiated_call_index].clip_type != 0) {
322             return gsm_calls[initiated_call_index].clip_number;
323         }
324     }
325 
326     int active_call_index = get_active_call_index();
327     if (active_call_index != -1){
328         if (gsm_calls[active_call_index].clip_type != 0) {
329             return gsm_calls[active_call_index].clip_number;
330         }
331     }
332     clip_number[0] = 0;
333     return clip_number;
334 }
335 
336 hfp_call_status_t hfp_gsm_call_status(void){
337     if (get_number_active_calls() + get_number_held_calls() + get_number_response_held_calls()){
338         return HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
339     }
340     return HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
341 }
342 
343 hfp_callheld_status_t hfp_gsm_callheld_status(void){
344     // @note: order is important
345     if (get_number_held_calls() == 0){
346         return HFP_CALLHELD_STATUS_NO_CALLS_HELD;
347     }
348     if (get_number_active_calls() == 0) {
349         return HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS;
350     }
351     return HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED;
352 }
353 
354 hfp_callsetup_status_t hfp_gsm_callsetup_status(void){
355     return callsetup_status;
356 }
357 
358 static int hfp_gsm_response_held_active(void){
359     return get_response_held_call_index() != -1 ;
360 }
361 
362 int hfp_gsm_call_possible(void){
363     return get_number_none_calls() > 0;
364 }
365 
366 void hfp_gsm_handle_event(hfp_ag_call_event_t event){
367     hfp_gsm_handler(event, 0, 0, NULL);
368 }
369 
370 void hfp_gsm_handle_event_with_clip(hfp_ag_call_event_t event, uint8_t type, const char * number){
371     hfp_gsm_handler(event, 0, type, number);
372 }
373 
374 void hfp_gsm_handle_event_with_call_index(hfp_ag_call_event_t event, uint8_t index){
375     hfp_gsm_handler(event, index, 0, NULL);
376 }
377 
378 void hfp_gsm_handle_event_with_call_number(hfp_ag_call_event_t event, const char * number){
379     hfp_gsm_handler(event, 0, 0, number);
380 }
381 
382 static void hfp_gsm_handler(hfp_ag_call_event_t event, uint8_t index, uint8_t type, const char * number){
383     int next_free_slot = get_next_free_slot();
384     int current_call_index = get_active_call_index();
385     int initiated_call_index = get_initiated_call_index();
386     int held_call_index = get_held_call_index();
387     int i;
388 
389     switch (event){
390         case HFP_AG_OUTGOING_CALL_INITIATED:
391         case HFP_AG_OUTGOING_REDIAL_INITIATED:
392             if (next_free_slot == -1){
393                 log_error("gsm: max call nr exceeded");
394                 return;
395             }
396             create_call(HFP_ENHANCED_CALL_DIR_OUTGOING);
397             break;
398 
399         case HFP_AG_OUTGOING_CALL_REJECTED:
400             if (current_call_index != -1){
401                 delete_call(current_call_index);
402             }
403             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
404             break;
405 
406         case HFP_AG_OUTGOING_CALL_ACCEPTED:
407             if (current_call_index != -1){
408                 set_enhanced_call_status_held(current_call_index);
409             }
410             set_callsetup_status(HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE);
411             break;
412 
413         case HFP_AG_OUTGOING_CALL_RINGING:
414             if (current_call_index == -1){
415                 log_error("gsm: no active call");
416                 return;
417             }
418             set_callsetup_status(HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE);
419             break;
420         case HFP_AG_OUTGOING_CALL_ESTABLISHED:
421             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
422             set_enhanced_call_status_active(initiated_call_index);
423             break;
424 
425         case HFP_AG_INCOMING_CALL:
426             if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS) break;
427             set_callsetup_status(HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS);
428             create_call(HFP_ENHANCED_CALL_DIR_INCOMING);
429             break;
430 
431         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
432             if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) break;
433             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
434 
435             if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
436                 set_enhanced_call_status_held(current_call_index);
437             }
438             set_enhanced_call_status_active(initiated_call_index);
439             break;
440 
441         case HFP_AG_HELD_CALL_JOINED_BY_AG:
442             if (hfp_gsm_call_status() != HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT) break;
443 
444             // TODO: is following condition correct? Can we join incoming call before it is answered?
445             if (callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
446                 set_enhanced_call_status_active(initiated_call_index);
447                 set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
448             } else if (hfp_gsm_callheld_status() == HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED) {
449                 set_enhanced_call_status_active(held_call_index);
450             }
451 
452             for (i = 0; i < HFP_GSM_MAX_NR_CALLS; i++){
453                 if (is_enhanced_call_status_active(i)){
454                     gsm_calls[i].mpty = HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL;
455                 }
456             }
457             break;
458 
459         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
460             if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) break;
461             if (hfp_gsm_call_status() != HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS) break;
462             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
463             set_enhanced_call_status_active(initiated_call_index);
464             break;
465 
466         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
467         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
468             if (hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) break;
469             if (hfp_gsm_call_status() != HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS) break;
470             set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
471             set_enhanced_call_status_response_hold(initiated_call_index);
472             break;
473 
474         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
475         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
476             if (!hfp_gsm_response_held_active()) break;
477             set_enhanced_call_status_active(get_response_held_call_index());
478             break;
479 
480         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
481         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
482             if (!hfp_gsm_response_held_active()) break;
483             delete_call(get_response_held_call_index());
484             break;
485 
486 
487         case HFP_AG_TERMINATE_CALL_BY_HF:
488             switch (hfp_gsm_call_status()){
489                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
490                     set_callsetup_status(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS);
491                     break;
492                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
493                     delete_call(current_call_index);
494                     break;
495                 default:
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