Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
call_box.cpp@91:c2a86b1f8aaa, 2015-01-08 (annotated)
- Committer:
- klauss
- Date:
- Thu Jan 08 23:44:08 2015 +0000
- Revision:
- 91:c2a86b1f8aaa
- Parent:
- 89:0fe315117b00
- Child:
- 92:92df17f538a8
- melhorias no tratamento dos pacotes udp, melhorando o tratamento da fila, evitando que ela transborde
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 0:4d17cd9c8f9d | 1 | #include "call_box.h" |
klauss | 0:4d17cd9c8f9d | 2 | |
klauss | 0:4d17cd9c8f9d | 3 | Call_Box::Call_Box( int ext, int port ){ |
klauss | 0:4d17cd9c8f9d | 4 | this->ext = ext; |
klauss | 0:4d17cd9c8f9d | 5 | this->port = port; |
klauss | 0:4d17cd9c8f9d | 6 | t.start(); |
klauss | 0:4d17cd9c8f9d | 7 | remaining_attempts = __MAX_ATTEMPTS__; |
klauss | 0:4d17cd9c8f9d | 8 | timeout = __TIMEOUT__; |
klauss | 4:de46f0d9b14d | 9 | msg_id = 0x10; |
klauss | 0:4d17cd9c8f9d | 10 | timeslice = 0x00; |
klauss | 0:4d17cd9c8f9d | 11 | status = cb_idle; |
klauss | 12:07fd716e0f35 | 12 | sip = new Sip( ext, port ); |
klauss | 89:0fe315117b00 | 13 | sip_socket_fd = sip->get_socket_fd(); |
klauss | 89:0fe315117b00 | 14 | if( sip == NULL ) |
klauss | 89:0fe315117b00 | 15 | { |
klauss | 89:0fe315117b00 | 16 | memory_is_over = true; |
klauss | 89:0fe315117b00 | 17 | if( debug_memory ) debug_msg("Sip allocation fail"); |
klauss | 89:0fe315117b00 | 18 | } |
klauss | 87:679ee0d594a9 | 19 | |
klauss | 78:1353744f01e1 | 20 | next_aging_type = __CYCLES__; |
klauss | 78:1353744f01e1 | 21 | seconds.start(); |
klauss | 81:3656f00ab3db | 22 | invite_response = true; |
klauss | 81:3656f00ab3db | 23 | bye_response = true; |
klauss | 85:b6f2dc1d0f4f | 24 | cb_new_counter++; |
klauss | 78:1353744f01e1 | 25 | } |
klauss | 78:1353744f01e1 | 26 | |
klauss | 78:1353744f01e1 | 27 | int Call_Box::get_status( void ){ return this->status; } |
klauss | 78:1353744f01e1 | 28 | |
klauss | 78:1353744f01e1 | 29 | void Call_Box::cb_set_status( uint8_t status ){ this->status = status; } |
klauss | 78:1353744f01e1 | 30 | |
klauss | 78:1353744f01e1 | 31 | int Call_Box::get_sip_status( void ){ |
klauss | 78:1353744f01e1 | 32 | if( sip == NULL ) return -1; |
klauss | 78:1353744f01e1 | 33 | |
klauss | 78:1353744f01e1 | 34 | else return this->sip->get_status(); |
klauss | 78:1353744f01e1 | 35 | } |
klauss | 78:1353744f01e1 | 36 | |
klauss | 78:1353744f01e1 | 37 | int Call_Box::set_sip_status( uint8_t status ){ |
klauss | 78:1353744f01e1 | 38 | if( sip == NULL ) return( -1 ); |
klauss | 78:1353744f01e1 | 39 | |
klauss | 78:1353744f01e1 | 40 | sip->sip_set_status( status ); |
klauss | 78:1353744f01e1 | 41 | |
klauss | 78:1353744f01e1 | 42 | return( 0 ); |
klauss | 0:4d17cd9c8f9d | 43 | } |
klauss | 0:4d17cd9c8f9d | 44 | |
klauss | 0:4d17cd9c8f9d | 45 | Call_Box::~Call_Box( void ){ |
klauss | 85:b6f2dc1d0f4f | 46 | if( sip != NULL ) delete( sip ); |
klauss | 85:b6f2dc1d0f4f | 47 | cb_delete_counter++; |
klauss | 0:4d17cd9c8f9d | 48 | } |
klauss | 0:4d17cd9c8f9d | 49 | |
klauss | 0:4d17cd9c8f9d | 50 | int Call_Box::get_port( void ){ return( this->port ); } |
klauss | 0:4d17cd9c8f9d | 51 | int Call_Box::get_ext( void ){ return( this->ext ); } |
klauss | 0:4d17cd9c8f9d | 52 | |
klauss | 0:4d17cd9c8f9d | 53 | float Call_Box::get_elapsed_time( void ){ return( this->t.read_ms() ); } |
klauss | 0:4d17cd9c8f9d | 54 | |
klauss | 0:4d17cd9c8f9d | 55 | void Call_Box::reset_elapsed_time( void ){ |
klauss | 0:4d17cd9c8f9d | 56 | this->t.reset(); |
klauss | 78:1353744f01e1 | 57 | timeout = __TIMEOUT__; |
klauss | 78:1353744f01e1 | 58 | next_aging_type = __CYCLES__; |
klauss | 0:4d17cd9c8f9d | 59 | } |
klauss | 0:4d17cd9c8f9d | 60 | |
klauss | 0:4d17cd9c8f9d | 61 | bool Call_Box::is_timeout( void ){ |
klauss | 78:1353744f01e1 | 62 | if( seconds.read() > __RESQUEST_TIME__ ){ |
klauss | 78:1353744f01e1 | 63 | seconds.reset(); |
klauss | 78:1353744f01e1 | 64 | return( t.read() > __TIME_TO_REGISTER__ ) ? true : false; |
klauss | 78:1353744f01e1 | 65 | }else return false; |
klauss | 0:4d17cd9c8f9d | 66 | } |
klauss | 0:4d17cd9c8f9d | 67 | |
klauss | 78:1353744f01e1 | 68 | bool Call_Box::has_next_try( void ){ |
klauss | 78:1353744f01e1 | 69 | return( ( t.read() < __TIMEOUT__ ) ? true : false ); |
klauss | 0:4d17cd9c8f9d | 70 | } |
klauss | 0:4d17cd9c8f9d | 71 | |
klauss | 0:4d17cd9c8f9d | 72 | void Call_Box::registry( void ){ |
klauss | 0:4d17cd9c8f9d | 73 | if( this->sip == NULL ){ |
klauss | 0:4d17cd9c8f9d | 74 | this->sip = new Sip( ext, port ); |
klauss | 87:679ee0d594a9 | 75 | if( sip == NULL ){ |
klauss | 87:679ee0d594a9 | 76 | memory_is_over = true; |
klauss | 89:0fe315117b00 | 77 | if( debug_memory ) debug_msg("Call_Box cb allocation fail"); |
klauss | 87:679ee0d594a9 | 78 | return; |
klauss | 87:679ee0d594a9 | 79 | } |
klauss | 0:4d17cd9c8f9d | 80 | |
klauss | 78:1353744f01e1 | 81 | if( sip->registry() > 0x00 ) reset_elapsed_time(); |
klauss | 81:3656f00ab3db | 82 | if( debug_aging ) debug_msg( "%d Registered - ( %f )", ext, __TIMEOUT__ - t.read() ); |
klauss | 78:1353744f01e1 | 83 | |
klauss | 0:4d17cd9c8f9d | 84 | delete( sip ); |
klauss | 0:4d17cd9c8f9d | 85 | this->sip = NULL; |
klauss | 0:4d17cd9c8f9d | 86 | }else{ |
klauss | 78:1353744f01e1 | 87 | if( sip->registry() > 0x00 ) reset_elapsed_time(); |
klauss | 81:3656f00ab3db | 88 | if( debug_aging ) debug_msg( "%d Registered - ( %f )", ext, __TIMEOUT__ - t.read() ); |
klauss | 0:4d17cd9c8f9d | 89 | } |
klauss | 0:4d17cd9c8f9d | 90 | } |
klauss | 0:4d17cd9c8f9d | 91 | |
klauss | 0:4d17cd9c8f9d | 92 | void Call_Box::unregistry( void ){ |
klauss | 0:4d17cd9c8f9d | 93 | if( this->sip == NULL ){ |
klauss | 0:4d17cd9c8f9d | 94 | this->sip = new Sip( ext, port ); |
klauss | 87:679ee0d594a9 | 95 | if( sip == NULL ){ |
klauss | 87:679ee0d594a9 | 96 | memory_is_over = true; |
klauss | 89:0fe315117b00 | 97 | if( debug_memory ) debug_msg("Call_Box cb allocation fail"); |
klauss | 87:679ee0d594a9 | 98 | return; |
klauss | 87:679ee0d594a9 | 99 | } |
klauss | 0:4d17cd9c8f9d | 100 | sip->send_unregistry_pkg(); |
klauss | 0:4d17cd9c8f9d | 101 | |
klauss | 0:4d17cd9c8f9d | 102 | delete( sip ); |
klauss | 0:4d17cd9c8f9d | 103 | this->sip = NULL; |
klauss | 0:4d17cd9c8f9d | 104 | }else{ |
klauss | 0:4d17cd9c8f9d | 105 | sip->send_unregistry_pkg(); |
klauss | 0:4d17cd9c8f9d | 106 | } |
klauss | 0:4d17cd9c8f9d | 107 | } |
klauss | 0:4d17cd9c8f9d | 108 | |
klauss | 0:4d17cd9c8f9d | 109 | VZ_call * Call_Box::invite( void ){ |
klauss | 29:7246460b73f8 | 110 | t.stop(); |
klauss | 29:7246460b73f8 | 111 | VZ_call * call = NULL; |
klauss | 29:7246460b73f8 | 112 | |
klauss | 0:4d17cd9c8f9d | 113 | if( this->sip == NULL ){ |
klauss | 0:4d17cd9c8f9d | 114 | this->sip = new Sip( ext, port ); |
klauss | 0:4d17cd9c8f9d | 115 | |
klauss | 87:679ee0d594a9 | 116 | if( sip != NULL ){ |
klauss | 87:679ee0d594a9 | 117 | memory_is_over = true; |
klauss | 89:0fe315117b00 | 118 | if( debug_memory ) debug_msg("Call_Box cb allocation fail"); |
klauss | 87:679ee0d594a9 | 119 | return( NULL ); |
klauss | 87:679ee0d594a9 | 120 | }else{ |
klauss | 87:679ee0d594a9 | 121 | call = sip->invite(); |
klauss | 87:679ee0d594a9 | 122 | |
klauss | 87:679ee0d594a9 | 123 | delete( sip ); |
klauss | 87:679ee0d594a9 | 124 | this->sip = NULL; |
klauss | 87:679ee0d594a9 | 125 | } |
klauss | 29:7246460b73f8 | 126 | }else{ |
klauss | 29:7246460b73f8 | 127 | call = sip->invite(); |
klauss | 0:4d17cd9c8f9d | 128 | } |
klauss | 29:7246460b73f8 | 129 | |
klauss | 29:7246460b73f8 | 130 | if( debug_cb ) debug_msg("Call returned value :: %p ", (void * )call ); |
klauss | 29:7246460b73f8 | 131 | if( call == NULL ) t.start(); |
klauss | 29:7246460b73f8 | 132 | return( call ); |
klauss | 0:4d17cd9c8f9d | 133 | } |
klauss | 0:4d17cd9c8f9d | 134 | |
klauss | 0:4d17cd9c8f9d | 135 | /* Retorna |
klauss | 0:4d17cd9c8f9d | 136 | = 0 :: ok |
klauss | 0:4d17cd9c8f9d | 137 | < 0 :: tive problemas |
klauss | 0:4d17cd9c8f9d | 138 | > 0 :: devo remover essa call do vetor de calls |
klauss | 0:4d17cd9c8f9d | 139 | */ |
klauss | 0:4d17cd9c8f9d | 140 | int Call_Box::listen_SIP_server( void ){ |
klauss | 0:4d17cd9c8f9d | 141 | if( this->sip == NULL ){ |
klauss | 0:4d17cd9c8f9d | 142 | if( status == cb_on_call || status == cb_idle ){ |
klauss | 0:4d17cd9c8f9d | 143 | this->sip = new Sip( ext, port ); |
klauss | 87:679ee0d594a9 | 144 | |
klauss | 87:679ee0d594a9 | 145 | if( sip == NULL ){ |
klauss | 87:679ee0d594a9 | 146 | memory_is_over = true; |
klauss | 89:0fe315117b00 | 147 | if( debug_memory ) debug_msg("Call_Box cb allocation fail"); |
klauss | 87:679ee0d594a9 | 148 | return( -1 ); |
klauss | 87:679ee0d594a9 | 149 | }else{ |
klauss | 87:679ee0d594a9 | 150 | int out_read = sip->listen_SIP_server(); |
klauss | 87:679ee0d594a9 | 151 | |
klauss | 87:679ee0d594a9 | 152 | delete( sip ); |
klauss | 87:679ee0d594a9 | 153 | this->sip = NULL; |
klauss | 87:679ee0d594a9 | 154 | return( out_read ); |
klauss | 87:679ee0d594a9 | 155 | } |
klauss | 0:4d17cd9c8f9d | 156 | }else return( 0 ); |
klauss | 0:4d17cd9c8f9d | 157 | }else{ |
klauss | 0:4d17cd9c8f9d | 158 | if( status == cb_on_call || status == cb_idle ){ |
klauss | 13:ae278302dffe | 159 | return ( sip->listen_SIP_server() ); |
klauss | 13:ae278302dffe | 160 | } else return( 0 ); |
klauss | 0:4d17cd9c8f9d | 161 | } |
klauss | 0:4d17cd9c8f9d | 162 | } |
klauss | 0:4d17cd9c8f9d | 163 | |
klauss | 0:4d17cd9c8f9d | 164 | void Call_Box::set_msg_id( uint8_t msg_id ){ |
klauss | 78:1353744f01e1 | 165 | this->msg_id = ( msg_id > 0x10 ) ? msg_id : 0x11; |
klauss | 0:4d17cd9c8f9d | 166 | } |
klauss | 0:4d17cd9c8f9d | 167 | uint8_t Call_Box::get_msg_id( void ){ return( this->msg_id ); } |
klauss | 0:4d17cd9c8f9d | 168 | |
klauss | 0:4d17cd9c8f9d | 169 | void Call_Box::set_timeslice( uint8_t timeslice ){ |
klauss | 0:4d17cd9c8f9d | 170 | this->timeslice = timeslice; |
klauss | 0:4d17cd9c8f9d | 171 | } |
klauss | 0:4d17cd9c8f9d | 172 | uint8_t Call_Box::get_timeslice( void ){ return( this->timeslice ); } |
klauss | 0:4d17cd9c8f9d | 173 | |
klauss | 0:4d17cd9c8f9d | 174 | //void Call_Box::set_sip( Sip * sip ){ |
klauss | 0:4d17cd9c8f9d | 175 | // this->sip = sip; |
klauss | 0:4d17cd9c8f9d | 176 | //} |
klauss | 0:4d17cd9c8f9d | 177 | |
klauss | 0:4d17cd9c8f9d | 178 | void Call_Box::send_bye( void ){ |
klauss | 0:4d17cd9c8f9d | 179 | if( this->sip == NULL ){ |
klauss | 0:4d17cd9c8f9d | 180 | this->sip = new Sip( ext, port ); |
klauss | 0:4d17cd9c8f9d | 181 | |
klauss | 87:679ee0d594a9 | 182 | if( sip == NULL ){ |
klauss | 87:679ee0d594a9 | 183 | memory_is_over = true; |
klauss | 89:0fe315117b00 | 184 | if( debug_memory ) debug_msg("Call_Box cb allocation fail"); |
klauss | 87:679ee0d594a9 | 185 | return; |
klauss | 87:679ee0d594a9 | 186 | }else{ |
klauss | 13:ae278302dffe | 187 | |
klauss | 87:679ee0d594a9 | 188 | sip->send_bye(); |
klauss | 87:679ee0d594a9 | 189 | |
klauss | 87:679ee0d594a9 | 190 | delete( sip ); |
klauss | 87:679ee0d594a9 | 191 | sip = NULL; |
klauss | 87:679ee0d594a9 | 192 | } |
klauss | 0:4d17cd9c8f9d | 193 | }else{ |
klauss | 0:4d17cd9c8f9d | 194 | sip->send_bye(); |
klauss | 0:4d17cd9c8f9d | 195 | } |
klauss | 1:a1758104fa1d | 196 | } |
klauss | 1:a1758104fa1d | 197 | |
klauss | 1:a1758104fa1d | 198 | void Call_Box::reset_sip( void ){ |
klauss | 1:a1758104fa1d | 199 | if( this->sip != NULL ) delete( this->sip ); |
klauss | 1:a1758104fa1d | 200 | |
klauss | 1:a1758104fa1d | 201 | this->sip = new Sip( ext, port ); |
klauss | 87:679ee0d594a9 | 202 | |
klauss | 87:679ee0d594a9 | 203 | if( sip == NULL ) memory_is_over = true; |
klauss | 14:22a35f575502 | 204 | } |
klauss | 14:22a35f575502 | 205 | |
klauss | 14:22a35f575502 | 206 | void Call_Box::set_sip_status( int status ){ |
klauss | 14:22a35f575502 | 207 | if( this->sip != NULL ){ |
klauss | 14:22a35f575502 | 208 | this->sip->status = status; |
klauss | 14:22a35f575502 | 209 | }else{ |
klauss | 78:1353744f01e1 | 210 | if( debug_cb) debug_msg("Sip equals NULL o.O"); |
klauss | 14:22a35f575502 | 211 | } |
klauss | 29:7246460b73f8 | 212 | } |
klauss | 29:7246460b73f8 | 213 | |
klauss | 29:7246460b73f8 | 214 | void Call_Box::re_start_timer( void ){ |
klauss | 29:7246460b73f8 | 215 | t.start(); |
klauss | 78:1353744f01e1 | 216 | } |
klauss | 78:1353744f01e1 | 217 | |
klauss | 78:1353744f01e1 | 218 | int8_t Call_Box::get_next_aging_type( void ){ |
klauss | 78:1353744f01e1 | 219 | next_aging_type--; |
klauss | 78:1353744f01e1 | 220 | |
klauss | 78:1353744f01e1 | 221 | if( ( next_aging_type % 2 ) == 0 || ( next_aging_type > __CYCLES__ - 15 ) ) return( REQUEST_REGISTRY ); |
klauss | 78:1353744f01e1 | 222 | |
klauss | 78:1353744f01e1 | 223 | else if( ( next_aging_type % 7 ) == 0 ) return( REQUEST_PEND ); |
klauss | 78:1353744f01e1 | 224 | |
klauss | 78:1353744f01e1 | 225 | else return( REQUEST_PING ); |
klauss | 81:3656f00ab3db | 226 | } |
klauss | 81:3656f00ab3db | 227 | |
klauss | 81:3656f00ab3db | 228 | void Call_Box::set_invite_response_ok( void ){ this->invite_response = true; } |
klauss | 81:3656f00ab3db | 229 | void Call_Box::set_invite_response_pending( void ){ this->invite_response = false; } |
klauss | 81:3656f00ab3db | 230 | bool Call_Box::get_invite_response( void ){ return( this->invite_response ); } |
klauss | 81:3656f00ab3db | 231 | |
klauss | 81:3656f00ab3db | 232 | void Call_Box::set_bye_response_ok( void ){ this->bye_response = true; } |
klauss | 81:3656f00ab3db | 233 | void Call_Box::set_bye_response_pending( void ){ this->bye_response = false; } |
klauss | 89:0fe315117b00 | 234 | bool Call_Box::get_bye_response( void ){ return( this->bye_response ); } |
klauss | 89:0fe315117b00 | 235 | |
klauss | 89:0fe315117b00 | 236 | |
klauss | 89:0fe315117b00 | 237 | int Call_Box::get_sip_socket_fd( void ){ |
klauss | 89:0fe315117b00 | 238 | return( sip_socket_fd ); |
klauss | 89:0fe315117b00 | 239 | } |
klauss | 89:0fe315117b00 | 240 | |
klauss | 91:c2a86b1f8aaa | 241 | int Call_Box::sip_udp_incomming_pkg( void ){ |
klauss | 91:c2a86b1f8aaa | 242 | return sip->udp_incomming_pkg(); |
klauss | 91:c2a86b1f8aaa | 243 | } |
klauss | 91:c2a86b1f8aaa | 244 | |
klauss | 91:c2a86b1f8aaa | 245 | void Call_Box::reset_cb_status( void ){ |
klauss | 91:c2a86b1f8aaa | 246 | if( sip != NULL ) sip->reset_call(); |
klauss | 89:0fe315117b00 | 247 | } |