Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Committer:
klauss
Date:
Mon Apr 20 14:40:09 2015 +0000
Revision:
114:472502b31a12
Parent:
113:db67ae00550e
Child:
115:a1e1e2e60a2c
antes de implementar os retrys da header -> cbx

Who changed what in which revision?

UserRevisionLine numberNew contents of line
klauss 74:81c47fff88a5 1 #include "call_manager.h"
klauss 74:81c47fff88a5 2
klauss 81:3656f00ab3db 3 Timer invite_pkg_retry_timer;
klauss 81:3656f00ab3db 4 Timer bye_pkg_retry_timer;
klauss 81:3656f00ab3db 5
klauss 81:3656f00ab3db 6 void init_invite_pgk_retry_manager( void ){
klauss 81:3656f00ab3db 7 invite_pkg_retry_timer.start();
klauss 81:3656f00ab3db 8 }
klauss 81:3656f00ab3db 9
klauss 81:3656f00ab3db 10 void init_bye_pgk_retry_manager( void ){
klauss 81:3656f00ab3db 11 bye_pkg_retry_timer.start();
klauss 81:3656f00ab3db 12 }
klauss 81:3656f00ab3db 13
klauss 81:3656f00ab3db 14 void invite_pgk_retry_manager( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){
klauss 81:3656f00ab3db 15 for( register uint8_t i = 0; i < v_cb->size(); i++ ){
klauss 81:3656f00ab3db 16 Call_Box * cb = ( Call_Box * )v_cb->get_element( i );
klauss 81:3656f00ab3db 17 if( cb->get_status() == cb_on_call ){
klauss 81:3656f00ab3db 18 if( cb->get_invite_response() == false ){
klauss 81:3656f00ab3db 19 invite_pkg_retry_timer.reset();
klauss 81:3656f00ab3db 20
klauss 114:472502b31a12 21 //cb->set_msg_id( cb->get_msg_id() & ~BIT7 );
klauss 92:92df17f538a8 22 cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
klauss 81:3656f00ab3db 23
klauss 114:472502b31a12 24 send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __INVITE__, ( char * )data,
klauss 114:472502b31a12 25 cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
klauss 81:3656f00ab3db 26
klauss 81:3656f00ab3db 27 return;
klauss 81:3656f00ab3db 28 }
klauss 81:3656f00ab3db 29 }
klauss 81:3656f00ab3db 30 }
klauss 81:3656f00ab3db 31 }
klauss 81:3656f00ab3db 32
klauss 81:3656f00ab3db 33 void bye_pgk_retry_manager( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){
klauss 81:3656f00ab3db 34 for( register uint8_t i = 0; i < v_cb->size(); i++ ){
klauss 81:3656f00ab3db 35 Call_Box * cb = ( Call_Box * )v_cb->get_element( i );
klauss 81:3656f00ab3db 36 if( cb->get_status() == cb_idle ){
klauss 81:3656f00ab3db 37 if( cb->get_bye_response() == false ){
klauss 81:3656f00ab3db 38 bye_pkg_retry_timer.reset();
klauss 81:3656f00ab3db 39
klauss 114:472502b31a12 40 //cb->set_msg_id( cb->get_msg_id() & ~BIT7 );
klauss 92:92df17f538a8 41 cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
klauss 81:3656f00ab3db 42
klauss 114:472502b31a12 43 send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __CB_BYE__, ( char * )data,
klauss 114:472502b31a12 44 cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
klauss 81:3656f00ab3db 45
klauss 81:3656f00ab3db 46 return;
klauss 81:3656f00ab3db 47 }
klauss 81:3656f00ab3db 48 }
klauss 81:3656f00ab3db 49 }
klauss 81:3656f00ab3db 50 }
klauss 78:1353744f01e1 51
klauss 74:81c47fff88a5 52 VZ_call * __find_Call__( Vector * v_call, int ext ){
klauss 74:81c47fff88a5 53 VZ_call * call = NULL;
klauss 74:81c47fff88a5 54 for( register int i = 0; i < v_call->size(); i++ ){
klauss 74:81c47fff88a5 55 call = ( VZ_call * )v_call->get_element( i );
klauss 74:81c47fff88a5 56 if( call->get_cb_ext() == ext ) return( call );
klauss 74:81c47fff88a5 57 }
klauss 74:81c47fff88a5 58 return( NULL );
klauss 74:81c47fff88a5 59 }
klauss 74:81c47fff88a5 60
klauss 74:81c47fff88a5 61 /* remove calls por timeout */
klauss 114:472502b31a12 62 void call_manager( Vector * v_call, Vector * v_cb, uint8_t * data, uint8_t * write_buffer, Timeslice * ts ){
klauss 81:3656f00ab3db 63 for( register uint8_t i = 0; i < v_call->size(); i++ ){
klauss 74:81c47fff88a5 64 VZ_call * call = ( VZ_call * )v_call->get_element( i );
klauss 92:92df17f538a8 65 if( call->is_timetofinish() || call->cbx_pkg_is_idle() ){
klauss 92:92df17f538a8 66
klauss 92:92df17f538a8 67 if( debug_invite ){
klauss 92:92df17f538a8 68 if( call->cbx_pkg_is_idle() ){
klauss 97:8985817e8847 69 if( debug_invite ) debug_msg("Call timeout: no pkg");
klauss 92:92df17f538a8 70 } else {
klauss 97:8985817e8847 71 if( debug_invite ) debug_msg("Call timeout");
klauss 92:92df17f538a8 72 }
klauss 92:92df17f538a8 73 }
klauss 74:81c47fff88a5 74 v_call->remove_element( i );
klauss 74:81c47fff88a5 75 Call_Box * cb = __find_CB__( v_cb, call->get_cb_ext() );
klauss 81:3656f00ab3db 76 if( cb != NULL ){
klauss 74:81c47fff88a5 77 cb->status = cb_idle;
klauss 74:81c47fff88a5 78 ts->return_timeslice( cb->get_timeslice() );
klauss 74:81c47fff88a5 79 cb->set_timeslice( 0x00 );
klauss 74:81c47fff88a5 80 data[ __TIMESLICE_PLACE__ ] = 0x00;
klauss 114:472502b31a12 81 // cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 );
klauss 92:92df17f538a8 82 cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
klauss 114:472502b31a12 83 send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __CB_BYE__,
klauss 114:472502b31a12 84 ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
klauss 74:81c47fff88a5 85 cb->send_bye();
klauss 74:81c47fff88a5 86 set_status( cb->sip->status, sip_idle );
klauss 114:472502b31a12 87 //cb->re_start_timer();
klauss 114:472502b31a12 88 cb -> reset_cb_status();
klauss 114:472502b31a12 89 //cb -> set_rtp_port ( 0 );
klauss 114:472502b31a12 90 //cb -> reset_rtp_timer ();
klauss 74:81c47fff88a5 91 }
klauss 74:81c47fff88a5 92 delete( call );
klauss 74:81c47fff88a5 93 }
klauss 74:81c47fff88a5 94 }
klauss 78:1353744f01e1 95 }
klauss 78:1353744f01e1 96
klauss 78:1353744f01e1 97 void invite_handler( Vector * v_call, Vector * v_cb, Timeslice * ts, Call_Box * inviting_cb ){
klauss 114:472502b31a12 98 uint8_t data[ __CB_BUFFER_SIZE__ ];
klauss 114:472502b31a12 99 uint8_t write_buffer[ __CB_BUFFER_SIZE__ ];
klauss 78:1353744f01e1 100
klauss 114:472502b31a12 101 if( inviting_cb != NULL ){
klauss 114:472502b31a12 102 //if( inviting_cb->status != cb_on_call ){
klauss 114:472502b31a12 103
klauss 114:472502b31a12 104 //}
klauss 114:472502b31a12 105 if( inviting_cb->status == cb_idle ){
klauss 114:472502b31a12 106 inviting_cb->cb_set_status( cb_trying );
klauss 114:472502b31a12 107 if( debug_invite ) debug_msg("Request invite cbx status :: cb_trying" );
klauss 113:db67ae00550e 108 }
klauss 114:472502b31a12 109 }
klauss 114:472502b31a12 110
klauss 114:472502b31a12 111 for( register uint8_t i = 0; i < v_cb->size(); i++ ){
klauss 114:472502b31a12 112 Call_Box * cb = ( Call_Box * )v_cb->get_element( i );
klauss 114:472502b31a12 113 if( cb->get_status() == cb_idle || cb->get_status() == cb_on_call ) continue;
klauss 114:472502b31a12 114
klauss 114:472502b31a12 115 int ext = cb->get_ext();
klauss 114:472502b31a12 116 int port = cb->get_port();
klauss 113:db67ae00550e 117
klauss 114:472502b31a12 118 VZ_call * call = NULL;
klauss 114:472502b31a12 119
klauss 114:472502b31a12 120 switch( cb->status ){
klauss 114:472502b31a12 121 case cb_trying : {
klauss 114:472502b31a12 122 uint8_t temp = cb->get_msg_id();
klauss 114:472502b31a12 123 cb->set_msg_id( temp |= BIT7 );
klauss 114:472502b31a12 124 cb->set_timeslice( ts->get_timeslice() );
klauss 114:472502b31a12 125 if( cb->get_timeslice() == 0x00 ){
klauss 114:472502b31a12 126
klauss 114:472502b31a12 127 cb->cb_set_status( cb_idle );
klauss 114:472502b31a12 128
klauss 114:472502b31a12 129 data[ __TIMESLICE_PLACE__ ] = 0x00;
klauss 114:472502b31a12 130
klauss 114:472502b31a12 131 send2callboxes( __build_cb_package__( ext, port, __INVITE__,
klauss 114:472502b31a12 132 ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
klauss 114:472502b31a12 133
klauss 114:472502b31a12 134 set_status( cb->sip->status, sip_idle );
klauss 114:472502b31a12 135
klauss 114:472502b31a12 136 //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 );
klauss 114:472502b31a12 137 cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
klauss 114:472502b31a12 138
klauss 114:472502b31a12 139 if( debug_invite ) debug_msg( "-- Trying -- without TS --");
klauss 114:472502b31a12 140
klauss 114:472502b31a12 141 return;
klauss 114:472502b31a12 142 }else{
klauss 114:472502b31a12 143 data[ __TIMESLICE_PLACE__ ] = cb->get_timeslice();
klauss 114:472502b31a12 144
klauss 114:472502b31a12 145 cb->cb_set_status( cb_ringing );
klauss 114:472502b31a12 146
klauss 114:472502b31a12 147 cb->set_invite_response_pending();
klauss 114:472502b31a12 148
klauss 114:472502b31a12 149 //cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ( ~BIT7 ) );
klauss 114:472502b31a12 150 cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
klauss 113:db67ae00550e 151
klauss 114:472502b31a12 152 send2callboxes( __build_cb_package__( ext, port, __INVITE__,
klauss 114:472502b31a12 153 ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
klauss 109:a5b8264ffbbc 154
klauss 114:472502b31a12 155 if( debug_invite ) debug_msg("-- %d on TS::%d --", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] );
klauss 114:472502b31a12 156 }
klauss 114:472502b31a12 157 }break;
klauss 114:472502b31a12 158 case cb_ringing : {
klauss 114:472502b31a12 159 call = cb->invite();
klauss 114:472502b31a12 160
klauss 114:472502b31a12 161 if( cb->get_sip_status() == sip_busy ){
klauss 114:472502b31a12 162 cb->cb_set_status( cb_busy );
klauss 114:472502b31a12 163 }else if( call != NULL ){
klauss 114:472502b31a12 164 if( __find_Call__( v_call, call->get_cb_ext() ) == NULL ) v_call -> add( call );
klauss 114:472502b31a12 165
klauss 114:472502b31a12 166 call->init_cbx_pkg_idle_timer();
klauss 114:472502b31a12 167
klauss 114:472502b31a12 168 if( cb->get_sip_status() == sip_on_call ){
klauss 108:18a3702650f3 169
klauss 114:472502b31a12 170 set_status( cb->status, cb_on_call );
klauss 113:db67ae00550e 171
klauss 114:472502b31a12 172 data[ __TIMESLICE_PLACE__ ] = cb->get_timeslice();
klauss 114:472502b31a12 173
klauss 114:472502b31a12 174 //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 );
klauss 114:472502b31a12 175 cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
klauss 114:472502b31a12 176
klauss 114:472502b31a12 177 send2callboxes( __build_cb_package__( ext, port, __INVITE__, ( char * )data,
klauss 114:472502b31a12 178 cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
klauss 114:472502b31a12 179 //debug_msg("send ok liga o mic");
klauss 114:472502b31a12 180
klauss 114:472502b31a12 181 cb->set_invite_response_pending();
klauss 114:472502b31a12 182
klauss 114:472502b31a12 183 if( debug_invite ) debug_msg( "-- ringing -- accepting call request --" );
klauss 109:a5b8264ffbbc 184 }
klauss 114:472502b31a12 185 }else{
klauss 114:472502b31a12 186 if( cb->get_sip_status() == sip_denied ) cb->cb_set_status( cb_denied );
klauss 114:472502b31a12 187 }
klauss 114:472502b31a12 188 }break;
klauss 114:472502b31a12 189 case cb_busy : {
klauss 114:472502b31a12 190 set_status( cb->status, cb_idle );
klauss 114:472502b31a12 191 ts->return_timeslice( cb->get_timeslice() );
klauss 114:472502b31a12 192 cb->set_timeslice( 0x00 );
klauss 114:472502b31a12 193 data[ __TIMESLICE_PLACE__ ] = 0x00;
klauss 114:472502b31a12 194 if( debug_invite ) debug_msg("-- %d on %d --", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] );
klauss 114:472502b31a12 195
klauss 114:472502b31a12 196 //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 );
klauss 114:472502b31a12 197 cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
klauss 114:472502b31a12 198
klauss 114:472502b31a12 199 send2callboxes( __build_cb_package__( ext, port, __CB_BYE__,
klauss 114:472502b31a12 200 ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
klauss 114:472502b31a12 201
klauss 114:472502b31a12 202 if( debug_invite ) debug_msg( "-- Trying -- denying call request << Busy Here >> --" );
klauss 114:472502b31a12 203
klauss 114:472502b31a12 204 for( register uint8_t i = 0; i < v_call->size(); i++ ){
klauss 114:472502b31a12 205 VZ_call * call = ( VZ_call * )v_call->get_element( i );
klauss 114:472502b31a12 206 if( call->get_cb_ext() == cb->get_ext() ){
klauss 114:472502b31a12 207 v_call->remove_element( i );
klauss 114:472502b31a12 208 if( call != NULL ) delete( call );
klauss 114:472502b31a12 209 break;
klauss 114:472502b31a12 210 }
klauss 114:472502b31a12 211 }
klauss 114:472502b31a12 212
klauss 114:472502b31a12 213 cb->send_bye();
klauss 114:472502b31a12 214 set_status( cb->sip->status, sip_idle );
klauss 114:472502b31a12 215 //cb->re_start_timer();
klauss 114:472502b31a12 216 }break;
klauss 114:472502b31a12 217 case cb_denied : {
klauss 114:472502b31a12 218 ts->return_timeslice( cb->get_timeslice() );
klauss 114:472502b31a12 219 cb->set_timeslice( 0x00 );
klauss 114:472502b31a12 220 data[ __TIMESLICE_PLACE__ ] = 0x00;
klauss 114:472502b31a12 221 if( debug_invite ) debug_msg("-- %d on %d --", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] );
klauss 114:472502b31a12 222
klauss 114:472502b31a12 223 //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 );
klauss 114:472502b31a12 224 cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 );
klauss 114:472502b31a12 225
klauss 114:472502b31a12 226 send2callboxes( __build_cb_package__( ext, port, __CB_BYE__,
klauss 114:472502b31a12 227 ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) );
klauss 113:db67ae00550e 228
klauss 114:472502b31a12 229 for( register uint8_t i = 0; i < v_call->size(); i++ ){
klauss 114:472502b31a12 230 VZ_call * call = ( VZ_call * )v_call->get_element( i );
klauss 114:472502b31a12 231 if( call->get_cb_ext() == cb->get_ext() ){
klauss 114:472502b31a12 232 v_call->remove_element( i );
klauss 114:472502b31a12 233 if( call != NULL ) delete( call );
klauss 114:472502b31a12 234 break;
klauss 78:1353744f01e1 235 }
klauss 114:472502b31a12 236 }
klauss 114:472502b31a12 237 //cb->re_start_timer();
klauss 114:472502b31a12 238 if( debug_invite ) debug_msg( "-- Trying -- denying call request --" );
klauss 114:472502b31a12 239 set_status( cb->status, cb_idle );
klauss 114:472502b31a12 240 set_status( cb->sip->status, sip_idle );
klauss 114:472502b31a12 241 }break;
klauss 78:1353744f01e1 242 }
klauss 78:1353744f01e1 243 }
klauss 74:81c47fff88a5 244 }