Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
call_manager.cpp@76:9f5f99dd895a, 2014-11-25 (annotated)
- Committer:
- klauss
- Date:
- Tue Nov 25 17:37:56 2014 +0000
- Revision:
- 76:9f5f99dd895a
- Parent:
- 74:81c47fff88a5
- Child:
- 77:e8c0253b57bc
desenvolvendo invite assincrono | validando
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 74:81c47fff88a5 | 1 | #include "call_manager.h" |
klauss | 74:81c47fff88a5 | 2 | |
klauss | 74:81c47fff88a5 | 3 | VZ_call * __find_Call__( Vector * v_call, int ext ){ |
klauss | 74:81c47fff88a5 | 4 | VZ_call * call = NULL; |
klauss | 74:81c47fff88a5 | 5 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 74:81c47fff88a5 | 6 | call = ( VZ_call * )v_call->get_element( i ); |
klauss | 74:81c47fff88a5 | 7 | if( call->get_cb_ext() == ext ) return( call ); |
klauss | 74:81c47fff88a5 | 8 | } |
klauss | 74:81c47fff88a5 | 9 | return( NULL ); |
klauss | 74:81c47fff88a5 | 10 | } |
klauss | 74:81c47fff88a5 | 11 | |
klauss | 74:81c47fff88a5 | 12 | /* remove calls por timeout */ |
klauss | 74:81c47fff88a5 | 13 | void call_manager( Vector * v_call, Vector * v_cb, uint8_t * data, uint8_t * write_buffer, Timeslice * ts ){ |
klauss | 74:81c47fff88a5 | 14 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 74:81c47fff88a5 | 15 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 74:81c47fff88a5 | 16 | if( call->is_timetofinish() ){ |
klauss | 74:81c47fff88a5 | 17 | v_call->remove_element( i ); |
klauss | 74:81c47fff88a5 | 18 | Call_Box * cb = __find_CB__( v_cb, call->get_cb_ext() ); |
klauss | 74:81c47fff88a5 | 19 | if( cb ){ |
klauss | 74:81c47fff88a5 | 20 | cb->status = cb_idle; |
klauss | 74:81c47fff88a5 | 21 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 74:81c47fff88a5 | 22 | cb->set_timeslice( 0x00 ); |
klauss | 74:81c47fff88a5 | 23 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 74:81c47fff88a5 | 24 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 74:81c47fff88a5 | 25 | send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __CB_BYE__, |
klauss | 74:81c47fff88a5 | 26 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 74:81c47fff88a5 | 27 | cb->send_bye(); |
klauss | 74:81c47fff88a5 | 28 | set_status( cb->sip->status, sip_idle ); |
klauss | 74:81c47fff88a5 | 29 | cb->re_start_timer(); |
klauss | 74:81c47fff88a5 | 30 | } |
klauss | 74:81c47fff88a5 | 31 | delete( call ); |
klauss | 74:81c47fff88a5 | 32 | } |
klauss | 74:81c47fff88a5 | 33 | } |
klauss | 76:9f5f99dd895a | 34 | } |
klauss | 76:9f5f99dd895a | 35 | |
klauss | 76:9f5f99dd895a | 36 | void invite_handler( Vector * v_call, Vector * v_cb, Timeslice * ts, Call_Box * cb ){ |
klauss | 76:9f5f99dd895a | 37 | uint8_t data[ __CB_BUFFER_SIZE__ ]; |
klauss | 76:9f5f99dd895a | 38 | uint8_t write_buffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 76:9f5f99dd895a | 39 | |
klauss | 76:9f5f99dd895a | 40 | int ext = 0x00; |
klauss | 76:9f5f99dd895a | 41 | int port = 0x00; |
klauss | 76:9f5f99dd895a | 42 | |
klauss | 76:9f5f99dd895a | 43 | bool should_return = true; |
klauss | 76:9f5f99dd895a | 44 | |
klauss | 76:9f5f99dd895a | 45 | if( cb != NULL ){ |
klauss | 76:9f5f99dd895a | 46 | ext = cb->get_ext(); |
klauss | 76:9f5f99dd895a | 47 | port = cb->get_port(); |
klauss | 76:9f5f99dd895a | 48 | set_status( cb->status, cb_trying ); |
klauss | 76:9f5f99dd895a | 49 | if( debug_invite ) debug_msg("Request invite cbx status :: %d", cb->status ); |
klauss | 76:9f5f99dd895a | 50 | }else{ |
klauss | 76:9f5f99dd895a | 51 | should_return = true; |
klauss | 76:9f5f99dd895a | 52 | for( register uint8_t i = 0; i < v_cb->size(); i++ ){ |
klauss | 76:9f5f99dd895a | 53 | Call_Box * inner_cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 76:9f5f99dd895a | 54 | if( inner_cb -> get_status() != cb_idle ){ |
klauss | 76:9f5f99dd895a | 55 | should_return = false; |
klauss | 76:9f5f99dd895a | 56 | cb = inner_cb; |
klauss | 76:9f5f99dd895a | 57 | break; |
klauss | 76:9f5f99dd895a | 58 | } |
klauss | 76:9f5f99dd895a | 59 | } |
klauss | 76:9f5f99dd895a | 60 | if( should_return ) return; |
klauss | 76:9f5f99dd895a | 61 | } |
klauss | 76:9f5f99dd895a | 62 | |
klauss | 76:9f5f99dd895a | 63 | for( register uint8_t i = 0; i < v_cb->size(); i++ ){ |
klauss | 76:9f5f99dd895a | 64 | cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 76:9f5f99dd895a | 65 | |
klauss | 76:9f5f99dd895a | 66 | if( cb -> get_status() == cb_idle ) continue; |
klauss | 76:9f5f99dd895a | 67 | |
klauss | 76:9f5f99dd895a | 68 | VZ_call * call = NULL; |
klauss | 76:9f5f99dd895a | 69 | |
klauss | 76:9f5f99dd895a | 70 | switch( cb->status ){ |
klauss | 76:9f5f99dd895a | 71 | case cb_ringing : { |
klauss | 76:9f5f99dd895a | 72 | call = cb->invite(); |
klauss | 76:9f5f99dd895a | 73 | |
klauss | 76:9f5f99dd895a | 74 | if( call != NULL ){ |
klauss | 76:9f5f99dd895a | 75 | if( __find_Call__( v_call, call->get_cb_ext() ) == NULL ) v_call -> add( call ); |
klauss | 76:9f5f99dd895a | 76 | |
klauss | 76:9f5f99dd895a | 77 | //if( ( cb->get_sip_status() == sip_trying ) || ( cb->get_sip_status() == sip_ringing ) ); |
klauss | 76:9f5f99dd895a | 78 | |
klauss | 76:9f5f99dd895a | 79 | if( cb->get_sip_status() == sip_on_call ){ |
klauss | 76:9f5f99dd895a | 80 | //if( debug_main ) debug_msg(" -- %d on %d -- ", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] ); |
klauss | 76:9f5f99dd895a | 81 | |
klauss | 76:9f5f99dd895a | 82 | /* isso quer dizer que o invite foi aceito, tenho que aceitar ele mais pra baixp*/ |
klauss | 76:9f5f99dd895a | 83 | send2callboxes( __build_cb_package__( ext, port, __INVITE__, ( char * )data, |
klauss | 76:9f5f99dd895a | 84 | cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 76:9f5f99dd895a | 85 | |
klauss | 76:9f5f99dd895a | 86 | if( debug_invite ) debug_msg( "-- ringing -- accepting call request --" ); |
klauss | 76:9f5f99dd895a | 87 | } |
klauss | 76:9f5f99dd895a | 88 | }else{ |
klauss | 76:9f5f99dd895a | 89 | if( cb->get_sip_status() == sip_denied ) set_status( cb->status , cb_denied ); |
klauss | 76:9f5f99dd895a | 90 | } |
klauss | 76:9f5f99dd895a | 91 | }break; |
klauss | 76:9f5f99dd895a | 92 | case cb_trying : { |
klauss | 76:9f5f99dd895a | 93 | data[ 0 ] |= BIT7; |
klauss | 76:9f5f99dd895a | 94 | cb->set_msg_id( data[ 0 ] ); |
klauss | 76:9f5f99dd895a | 95 | cb->set_timeslice( ts->get_timeslice() ); |
klauss | 76:9f5f99dd895a | 96 | // No protocolo VZ, timeslice == 0x00 significa que nao temos mais ts |
klauss | 76:9f5f99dd895a | 97 | if( cb->get_timeslice() == 0x00 ){ |
klauss | 76:9f5f99dd895a | 98 | set_status(cb->status,cb_idle); |
klauss | 76:9f5f99dd895a | 99 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 76:9f5f99dd895a | 100 | send2callboxes( __build_cb_package__( ext, port, __INVITE__, |
klauss | 76:9f5f99dd895a | 101 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 76:9f5f99dd895a | 102 | set_status( cb->sip->status, sip_idle ); |
klauss | 76:9f5f99dd895a | 103 | if( debug_invite ) debug_msg( "-- Trying -- without TS -- %s ", write_buffer ); |
klauss | 76:9f5f99dd895a | 104 | }else{ |
klauss | 76:9f5f99dd895a | 105 | data[ __TIMESLICE_PLACE__ ] = cb->get_timeslice(); |
klauss | 76:9f5f99dd895a | 106 | |
klauss | 76:9f5f99dd895a | 107 | set_status( cb->status, cb_ringing ); |
klauss | 76:9f5f99dd895a | 108 | |
klauss | 76:9f5f99dd895a | 109 | /* Isso diz pro CBx o Timeslice que ele vai usar e deixa o Cbx na espera da resposta do Asterisk */ |
klauss | 76:9f5f99dd895a | 110 | send2callboxes( __build_cb_package__( ext, port, __INVITE__, |
klauss | 76:9f5f99dd895a | 111 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 76:9f5f99dd895a | 112 | |
klauss | 76:9f5f99dd895a | 113 | if( debug_invite ) debug_msg(" -- %d on TS::%d -- ", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] ); |
klauss | 76:9f5f99dd895a | 114 | |
klauss | 76:9f5f99dd895a | 115 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 76:9f5f99dd895a | 116 | } |
klauss | 76:9f5f99dd895a | 117 | }break; |
klauss | 76:9f5f99dd895a | 118 | case cb_busy : { |
klauss | 76:9f5f99dd895a | 119 | set_status( cb->status, cb_idle ); |
klauss | 76:9f5f99dd895a | 120 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 76:9f5f99dd895a | 121 | cb->set_timeslice( 0x00 ); |
klauss | 76:9f5f99dd895a | 122 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 76:9f5f99dd895a | 123 | if( debug_invite ) debug_msg(" -- %d on %d -- ", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] ); |
klauss | 76:9f5f99dd895a | 124 | |
klauss | 76:9f5f99dd895a | 125 | send2callboxes( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 76:9f5f99dd895a | 126 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 76:9f5f99dd895a | 127 | |
klauss | 76:9f5f99dd895a | 128 | if( debug_invite ) debug_msg( "-- Trying -- denying call request << Busy Here >> --" ); |
klauss | 76:9f5f99dd895a | 129 | |
klauss | 76:9f5f99dd895a | 130 | for( register uint8_t i = 0; i < v_call->size(); i++ ){ |
klauss | 76:9f5f99dd895a | 131 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 76:9f5f99dd895a | 132 | if( call->get_cb_ext() == cb->get_ext() ){ |
klauss | 76:9f5f99dd895a | 133 | v_call->remove_element( i ); |
klauss | 76:9f5f99dd895a | 134 | delete( call ); |
klauss | 76:9f5f99dd895a | 135 | break; |
klauss | 76:9f5f99dd895a | 136 | } |
klauss | 76:9f5f99dd895a | 137 | } |
klauss | 76:9f5f99dd895a | 138 | |
klauss | 76:9f5f99dd895a | 139 | cb->send_bye(); |
klauss | 76:9f5f99dd895a | 140 | set_status( cb->sip->status, sip_idle ); |
klauss | 76:9f5f99dd895a | 141 | cb->re_start_timer(); |
klauss | 76:9f5f99dd895a | 142 | }break; |
klauss | 76:9f5f99dd895a | 143 | case cb_denied : { |
klauss | 76:9f5f99dd895a | 144 | set_status( cb->status, cb_idle ); |
klauss | 76:9f5f99dd895a | 145 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 76:9f5f99dd895a | 146 | cb->set_timeslice( 0x00 ); |
klauss | 76:9f5f99dd895a | 147 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 76:9f5f99dd895a | 148 | if( debug_invite ) debug_msg(" -- %d on %d -- ", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] ); |
klauss | 76:9f5f99dd895a | 149 | |
klauss | 76:9f5f99dd895a | 150 | send2callboxes( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 76:9f5f99dd895a | 151 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 76:9f5f99dd895a | 152 | |
klauss | 76:9f5f99dd895a | 153 | for( register uint8_t i = 0; i < v_call->size(); i++ ){ |
klauss | 76:9f5f99dd895a | 154 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 76:9f5f99dd895a | 155 | if( call->get_cb_ext() == cb->get_ext() ){ |
klauss | 76:9f5f99dd895a | 156 | v_call->remove_element( i ); |
klauss | 76:9f5f99dd895a | 157 | delete( call ); |
klauss | 76:9f5f99dd895a | 158 | break; |
klauss | 76:9f5f99dd895a | 159 | } |
klauss | 76:9f5f99dd895a | 160 | } |
klauss | 76:9f5f99dd895a | 161 | |
klauss | 76:9f5f99dd895a | 162 | if( debug_invite ) debug_msg( "-- Trying -- denying call request --" ); |
klauss | 76:9f5f99dd895a | 163 | set_status( cb->sip->status, sip_idle ); |
klauss | 76:9f5f99dd895a | 164 | }break; |
klauss | 76:9f5f99dd895a | 165 | } |
klauss | 76:9f5f99dd895a | 166 | } |
klauss | 74:81c47fff88a5 | 167 | } |