/
sink_callmanager.c
599 lines (511 loc) · 17.3 KB
/
sink_callmanager.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/****************************************************************************
Copyright (C) Cambridge Silicon Radio Ltd. 2005 - 2015.
FILE NAME
sink_callmanager.h
DESCRIPTION
This is the call manager for sink device
NOTES
*/
#include "sink_callmanager.h"
#include "sink_statemanager.h"
#include "sink_tones.h"
#include "sink_audio.h"
#include "sink_private.h"
#include "sink_configmanager.h"
#include "sink_slc.h"
#include "sink_audio_prompts.h"
#include "sink_multipoint.h"
#include "sink_speech_recognition.h"
#include "sink_config.h"
#include <ps.h>
#include <connection.h>
#include <hfp.h>
#include <stdlib.h>
#include <audio.h>
#include <string.h>
#include <sink.h>
#ifdef DEBUG_CALL_MAN
#define CM_DEBUG(x) DEBUG(x)
#else
#define CM_DEBUG(x)
#endif
#define SIZE_CONFIG_PHONE_NUMBER 20
#define SIZE_PHONE_NUMBER 21
/****************************************************************************
NAME
sinkHandleRingInd
DESCRIPTION
Received a RING indication from the AG.
RETURNS
void
*/
void sinkHandleRingInd( const HFP_RING_IND_T * pInd )
{
/* check whether the ring ind needs to be shown as a call waiting beep
for multipoint operation, if not multipoint function returns false */
if(!MPCheckRingInd(pInd))
{
/* Determine which AG has the outband ring (if applicable) and play
appropriate tone. */
#ifdef ENABLE_SPEECH_RECOGNITION
if((!pInd->in_band)&&(!speechRecognitionIsEnabled()))
#else
if(!pInd->in_band)
#endif
{
/* determine whether this is AG1 or AG2 and play appropriate tone */
if(pInd->priority == hfp_primary_link)
{
CM_DEBUG(("CM: OutBandRing - AG1 play ring 1\n")) ;
TonesPlayEvent(EventSysRingtone1);
}
/* this is AG2 so play ring tone for AG2 */
else if(pInd->priority == hfp_secondary_link)
{
CM_DEBUG(("CM: OutBandRing - AG2 play ring 2\n")) ;
TonesPlayEvent(EventSysRingtone2);
}
}
else
CM_DEBUG(("CM: inBandRing or speech rec enabled - no tone played\n")) ;
}
}
/****************************************************************************
NAME
sinkAnswerOrRejectCall
DESCRIPTION
Answer an incoming call from the device
RETURNS
void
*/
void sinkAnswerOrRejectCall( bool Action )
{
hfp_link_priority priority;
/* if the SR plugin is running, disconnect it */
MessageSend ( &theSink.task , EventSysSpeechRecognitionStop , 0 ) ;
/* get profile if AG with incoming call */
priority = HfpLinkPriorityFromCallState(hfp_call_state_incoming);
CM_DEBUG(("CM: Answer Call on AG%x\n",priority)) ;
/* if match found */
if(priority)
{
/* answer call */
HfpCallAnswerRequest(priority, Action);
theSink.profile_data[PROFILE_INDEX(priority)].status.local_call_action = TRUE;
}
/*terminate the ring tone*/
ToneTerminate();
}
/****************************************************************************
NAME
sinkHangUpCall
DESCRIPTION
Hang up the call from the device.
RETURNS
void
*/
void sinkHangUpCall( void )
{
/* Determine which is the current active call */
hfp_link_priority priority = hfp_invalid_link;
hfp_call_state call_state;
/* Get profile if AG with audio, also ensure AG is actually in a call, otherwise try other methods */
if((theSink.routed_audio)&&(HfpLinkGetCallState(HfpLinkPriorityFromAudioSink(theSink.routed_audio), &call_state)&& (call_state)))
priority = HfpLinkPriorityFromAudioSink(theSink.routed_audio);
/* No audio so use AG state */
if(!priority)
priority = HfpLinkPriorityFromCallState(hfp_call_state_active);
/* No active call, check for an outgoing call to terminate */
if(!priority)
priority = HfpLinkPriorityFromCallState(hfp_call_state_outgoing);
/* No active/outgoing calls, check for TWC state */
if(!priority)
priority = HfpLinkPriorityWithActiveCall(FALSE);
/* no active calls but still a held call on the phone */
if(!priority)
priority = HfpLinkPriorityFromCallState(hfp_call_state_held_remaining);
/* If match found */
if(priority && HfpLinkGetCallState(priority, &call_state))
{
switch(call_state)
{
case hfp_call_state_twc_incoming:
case hfp_call_state_twc_outgoing:
case hfp_call_state_held_active:
case hfp_call_state_multiparty:
CM_DEBUG(("CM: HangUp TWC active Call on AG%x\n",priority)) ;
HfpCallHoldActionRequest(priority, hfp_chld_release_active_accept_other, 0);
break;
case hfp_call_state_held_remaining:
CM_DEBUG(("CM: HangUp TWC held Call on AG%x\n",priority)) ;
HfpCallHoldActionRequest(priority, hfp_chld_release_held_reject_waiting, 0);
break;
default:
/* Terminate call using AT+CHUP */
CM_DEBUG(("CM: HangUp Call on AG%x\n",priority)) ;
HfpCallTerminateRequest(priority);
break;
}
}
}
/****************************************************************************
NAME
sinkInitiateLNR
DESCRIPTION
If HFP and connected - issues command
If HFP and not connected - connects and issues if not in call
If HSP sends button press
RETURNS
void
*/
void sinkInitiateLNR ( hfp_link_priority priority )
{
CM_DEBUG(("CM: LNR\n")) ;
/* if device not connected to any AG initiate a connection */
if (!stateManagerIsConnected() )
{
#ifdef ENABLE_AVRCP
sinkAvrcpCheckManualConnectReset(NULL);
#endif
MessageSend ( &theSink.task , EventUsrEstablishSLC , 0 ) ;
sinkQueueEvent(EventUsrLastNumberRedial) ;
}
/* have at least one connection */
else
{
uint8 Option = hfp_primary_link;
CM_DEBUG(("CM: LNR Connected\n")) ;
/* for multipoint use there are two options */
if(theSink.MultipointEnable)
{
/* these being to use separate LNR buttons, one for AG1 and one for AG2
or use one LNR event and dial using the AG that last made an outgoing
call */
if(theSink.features.SeparateLNRButtons)
{
CM_DEBUG(("CM: LNR use separate LNR buttons\n")) ;
Option = priority;
}
/* dial using AG that made last outgoing call */
else
{
CM_DEBUG(("CM: LNR use last outgoing AG = %d\n",theSink.last_outgoing_ag)) ;
Option = theSink.last_outgoing_ag;
}
}
CM_DEBUG(("CM: LNR on AG %d\n",Option)) ;
/* perform last number redial */
HfpDialLastNumberRequest(Option);
}
}
/****************************************************************************
NAME
sinkInitiateVoiceDial
DESCRIPTION
If HFP and connected - issues command
If HFP and not connected - connects and issues if not in call
If HSP sends button press
RETURNS
void
*/
void sinkInitiateVoiceDial ( hfp_link_priority priority )
{
CM_DEBUG(("CM: VD\n")) ;
if (!stateManagerIsConnected() )
{
#ifdef ENABLE_AVRCP
sinkAvrcpCheckManualConnectReset(NULL);
#endif
MessageSend ( &theSink.task , EventUsrEstablishSLC , 0 ) ;
sinkQueueEvent( EventUsrInitateVoiceDial ) ;
theSink.VoiceRecognitionIsActive = hfp_invalid_link ;
}
else
{
uint8 Option = hfp_primary_link;
CM_DEBUG(("CM: VD Connected\n")) ;
/* for multipoint use there are two options */
if(theSink.MultipointEnable)
{
/* these being to use separate VD buttons, one for AG1 and one for AG2
or use one VD event and dial using the AG that last made an outgoing
call */
if(theSink.features.SeparateVDButtons)
{
Option = priority;
}
/* voice dial using AG that made last outgoing call */
else
{
Option = theSink.last_outgoing_ag;
}
}
CM_DEBUG(("CM: VoiceDial on %d\n",Option)) ;
HfpVoiceRecognitionEnableRequest(Option, TRUE);
theSink.VoiceRecognitionIsActive = Option;
}
}
/****************************************************************************
NAME
sinkCancelVoiceDial
DESCRIPTION
cancels a voice dial request
RETURNS
void
*/
void sinkCancelVoiceDial ( hfp_link_priority priority )
{
uint8 Option = hfp_primary_link;
CM_DEBUG(("CM: VD Cancelled")) ;
/* for multipoint use there are two options */
if(theSink.MultipointEnable)
{
/* these being to use separate VD buttons, one for AG1 and one for AG2
or use one VD event and dial using the AG that last made an outgoing
call */
if(theSink.features.SeparateVDButtons)
{
Option = priority;
}
/* voice dial using AG that made last outgoing call */
else
{
Option = theSink.last_outgoing_ag;
}
}
/*if we believe voice dial is currently active*/
if ( theSink.VoiceRecognitionIsActive)
{
HfpVoiceRecognitionEnableRequest(Option, FALSE);
theSink.VoiceRecognitionIsActive = hfp_invalid_link;
}
}
/****************************************************************************
NAME
sinkQueueEvent
DESCRIPTION
Queues an event to be sent once the device is connected
RETURNS
void
*/
void sinkQueueEvent ( sinkEvents_t pEvent )
{
CM_DEBUG(("CM: QQ Ev[%x]\n", pEvent)) ;
theSink.gEventQueuedOnConnection = (pEvent - EVENTS_MESSAGE_BASE);
}
/****************************************************************************
NAME
sinkRecallQueuedEvent
DESCRIPTION
Checks to see if an event was Queued and issues it
RETURNS
void
*/
void sinkRecallQueuedEvent ( void )
{
/*this is currently only applicable to LNR and voice Dial but does not care */
if ((theSink.gEventQueuedOnConnection != (EventInvalid - EVENTS_MESSAGE_BASE)))
{
switch (stateManagerGetState() )
{
case deviceIncomingCallEstablish:
case deviceOutgoingCallEstablish:
case deviceActiveCallSCO:
case deviceActiveCallNoSCO:
case deviceThreeWayCallWaiting:
case deviceThreeWayCallOnHold:
case deviceThreeWayMulticall:
/* Do Nothing Message Gets ignored*/
break ;
default:
/* delay sending of queued message by 1 second as some phones are ignoring the AT command when
using native firmware as the connection time is much quicker using that firmware */
MessageSendLater ( &theSink.task , (theSink.gEventQueuedOnConnection + EVENTS_MESSAGE_BASE), 0 , 1500) ;
break;
}
}
/*reset the queued event*/
sinkClearQueueudEvent();
}
/****************************************************************************
NAME
sinkClearQueueudEvent
DESCRIPTION
Clears the QUEUE - used on failure to connect / power on / off etc
RETURNS
void
*/
void sinkClearQueueudEvent ( void )
{
/*this resets the queue - on a conenction fail / power off etc*/
sinkQueueEvent(EventInvalid) ;
}
/****************************************************************************
NAME
sinkUpdateStoredNumber
DESCRIPTION
Request a number to store from the primary AG
RETURNS
void
*/
void sinkUpdateStoredNumber (void)
{
Sink sink;
/* validate HFP available for use, i.e. AG connected */
if((HfpLinkGetSlcSink(hfp_primary_link, &sink))&&SinkIsValid(sink))
{
/* Request user select a number on the AG */
HfpVoiceTagNumberRequest(hfp_primary_link);
}
else
{
/* Connect and queue request */
#ifdef ENABLE_AVRCP
sinkAvrcpCheckManualConnectReset(NULL);
#endif
MessageSend ( &theSink.task , EventUsrEstablishSLC , 0 ) ;
sinkQueueEvent( EventUsrUpdateStoredNumber ) ;
}
}
/****************************************************************************
NAME
sinkWriteStoredNumber
DESCRIPTION
Store number obtained via HfpRequestNumberForVoiceTag in
CONFIG_PHONE_NUMBER
RETURNS
void
*/
void sinkWriteStoredNumber ( HFP_VOICE_TAG_NUMBER_IND_T* ind )
{
/* validate length of number returned */
if((ind->size_phone_number)&&(ind->size_phone_number<SIZE_CONFIG_PHONE_NUMBER))
{
uint16 phone_number_key[SIZE_CONFIG_PHONE_NUMBER];
CM_DEBUG(("Store Number "));
/* Make sure the phone number key is all zero to start */
memset(phone_number_key, 0, SIZE_CONFIG_PHONE_NUMBER*sizeof(uint16));
/* Write phone number into key array */
memmove(phone_number_key,ind->phone_number,ind->size_phone_number);
/* Write to PS */
ConfigStore(CONFIG_PHONE_NUMBER, &phone_number_key, ind->size_phone_number);
}
}
/****************************************************************************
NAME
sinkDialStoredNumber
DESCRIPTION
Dials a number stored in CONFIG_PHONE_NUMBER
If HFP and connected - issues command
If HFP and not connected - connects and issues if not in call
If HSP sends button press
RETURNS
void
*/
void sinkDialStoredNumber ( void )
{
uint16 ret_len;
Sink sink;
uint16 phone_number_key[SIZE_CONFIG_PHONE_NUMBER];
CM_DEBUG(("sinkDialStoredNumber\n")) ;
if ((ret_len = ConfigRetrieve(CONFIG_PHONE_NUMBER, phone_number_key, SIZE_CONFIG_PHONE_NUMBER )))
{
if((HfpLinkGetSlcSink(hfp_primary_link, &sink)) && SinkIsValid(sink))
{
/* Send the dial request now */
CM_DEBUG(("CM:Dial Stored Number (Connected) len=%x\n",ret_len)) ;
HfpDialNumberRequest(hfp_primary_link, ret_len, (uint8 *)&phone_number_key[0]);
}
else
{
/* Not connected, connect and queue the dial request */
#ifdef ENABLE_AVRCP
sinkAvrcpCheckManualConnectReset(NULL);
#endif
MessageSend ( &theSink.task , EventUsrEstablishSLC , 0 ) ;
sinkQueueEvent( EventUsrDialStoredNumber ) ;
}
}
else
{
/*The PSKEY could not be read*/
MessageSend(&theSink.task, EventUsrUpdateStoredNumber, 0);
}
}
/****************************************************************************
NAME
sinkPlaceIncomingCallOnHold
DESCRIPTION
looks for an incoming call and performs the twc hold incoming call function
RETURNS
void
*/
void sinkPlaceIncomingCallOnHold(void)
{
hfp_call_state CallState;
/* find incoming call to hold */
if((HfpLinkGetCallState(hfp_primary_link, &CallState))&&
(CallState == hfp_call_state_incoming))
{
CM_DEBUG(("MAIN: Hold incoming Call on AG1\n")) ;
HfpResponseHoldActionRequest(hfp_primary_link, hfp_hold_incoming_call);
}
else if((HfpLinkGetCallState(hfp_secondary_link, &CallState))&&
(CallState == hfp_call_state_incoming))
{
CM_DEBUG(("MAIN: Hold incoming Call on AG2\n")) ;
HfpResponseHoldActionRequest(hfp_secondary_link, hfp_hold_incoming_call);
}
}
/****************************************************************************
NAME
sinkAcceptHeldIncomingCall
DESCRIPTION
looks for a held incoming call and performs the twc accept held incoming
call function
RETURNS
void
*/
void sinkAcceptHeldIncomingCall(void)
{
hfp_call_state CallState;
/* find incoming held call */
if((HfpLinkGetCallState(hfp_primary_link, &CallState))&&
(CallState == hfp_call_state_incoming_held))
{
CM_DEBUG(("MAIN: Hold incoming Call on AG1\n")) ;
HfpResponseHoldActionRequest(hfp_primary_link, hfp_accept_held_incoming_call);
}
else if((HfpLinkGetCallState(hfp_secondary_link, &CallState))&&
(CallState == hfp_call_state_incoming_held))
{
CM_DEBUG(("MAIN: Hold incoming Call on AG2\n")) ;
HfpResponseHoldActionRequest(hfp_secondary_link, hfp_accept_held_incoming_call);
}
}
/****************************************************************************
NAME
sinkRejectHeldIncomingCall
DESCRIPTION
looks for a held incoming call and performs the twc reject held incoming
call function
RETURNS
void
*/
void sinkRejectHeldIncomingCall(void)
{
hfp_call_state CallState;
/* find incoming held call */
if((HfpLinkGetCallState(hfp_primary_link, &CallState))&&
(CallState == hfp_call_state_incoming_held))
{
CM_DEBUG(("MAIN: Hold incoming Call on AG1\n")) ;
HfpResponseHoldActionRequest(hfp_primary_link, hfp_reject_held_incoming_call);
}
else if((HfpLinkGetCallState(hfp_secondary_link, &CallState))&&
(CallState == hfp_call_state_incoming_held))
{
CM_DEBUG(("MAIN: Hold incoming Call on AG2\n")) ;
HfpResponseHoldActionRequest(hfp_secondary_link, hfp_reject_held_incoming_call);
}
}