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