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_box.cpp@100:09a23fcd3bdf, 2015-02-12 (annotated)
- Committer:
- klauss
- Date:
- Thu Feb 12 16:42:35 2015 +0000
- Revision:
- 100:09a23fcd3bdf
- Parent:
- 99:e80850c51106
- Child:
- 106:a34fcf9f0e02
begin the job
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 | 92:92df17f538a8 | 23 | //invite_first_response = true; | 
| klauss | 81:3656f00ab3db | 24 | bye_response = true; | 
| klauss | 92:92df17f538a8 | 25 | invite_retry_count = MAX_INVITE_RETRY; | 
| klauss | 85:b6f2dc1d0f4f | 26 | cb_new_counter++; | 
| klauss | 78:1353744f01e1 | 27 | } | 
| klauss | 78:1353744f01e1 | 28 | |
| klauss | 78:1353744f01e1 | 29 | int Call_Box::get_status( void ){ return this->status; } | 
| klauss | 78:1353744f01e1 | 30 | |
| klauss | 78:1353744f01e1 | 31 | void Call_Box::cb_set_status( uint8_t status ){ this->status = status; } | 
| klauss | 78:1353744f01e1 | 32 | |
| klauss | 78:1353744f01e1 | 33 | int Call_Box::get_sip_status( void ){ | 
| klauss | 78:1353744f01e1 | 34 | if( sip == NULL ) return -1; | 
| klauss | 78:1353744f01e1 | 35 | |
| klauss | 78:1353744f01e1 | 36 | else return this->sip->get_status(); | 
| klauss | 78:1353744f01e1 | 37 | } | 
| klauss | 78:1353744f01e1 | 38 | |
| klauss | 78:1353744f01e1 | 39 | int Call_Box::set_sip_status( uint8_t status ){ | 
| klauss | 78:1353744f01e1 | 40 | if( sip == NULL ) return( -1 ); | 
| klauss | 78:1353744f01e1 | 41 | |
| klauss | 78:1353744f01e1 | 42 | sip->sip_set_status( status ); | 
| klauss | 78:1353744f01e1 | 43 | |
| klauss | 78:1353744f01e1 | 44 | return( 0 ); | 
| klauss | 0:4d17cd9c8f9d | 45 | } | 
| klauss | 0:4d17cd9c8f9d | 46 | |
| klauss | 0:4d17cd9c8f9d | 47 | Call_Box::~Call_Box( void ){ | 
| klauss | 85:b6f2dc1d0f4f | 48 | if( sip != NULL ) delete( sip ); | 
| klauss | 85:b6f2dc1d0f4f | 49 | cb_delete_counter++; | 
| klauss | 0:4d17cd9c8f9d | 50 | } | 
| klauss | 0:4d17cd9c8f9d | 51 | |
| klauss | 0:4d17cd9c8f9d | 52 | int Call_Box::get_port( void ){ return( this->port ); } | 
| klauss | 0:4d17cd9c8f9d | 53 | int Call_Box::get_ext( void ){ return( this->ext ); } | 
| klauss | 0:4d17cd9c8f9d | 54 | |
| klauss | 0:4d17cd9c8f9d | 55 | float Call_Box::get_elapsed_time( void ){ return( this->t.read_ms() ); } | 
| klauss | 0:4d17cd9c8f9d | 56 | |
| klauss | 100:09a23fcd3bdf | 57 | void Call_Box::reset_elapsed_time( void ){ | 
| klauss | 0:4d17cd9c8f9d | 58 | this->t.reset(); | 
| klauss | 78:1353744f01e1 | 59 | timeout = __TIMEOUT__; | 
| klauss | 78:1353744f01e1 | 60 | next_aging_type = __CYCLES__; | 
| klauss | 0:4d17cd9c8f9d | 61 | } | 
| klauss | 0:4d17cd9c8f9d | 62 | |
| klauss | 0:4d17cd9c8f9d | 63 | bool Call_Box::is_timeout( void ){ | 
| klauss | 78:1353744f01e1 | 64 | if( seconds.read() > __RESQUEST_TIME__ ){ | 
| klauss | 78:1353744f01e1 | 65 | seconds.reset(); | 
| klauss | 78:1353744f01e1 | 66 | return( t.read() > __TIME_TO_REGISTER__ ) ? true : false; | 
| klauss | 78:1353744f01e1 | 67 | }else return false; | 
| klauss | 0:4d17cd9c8f9d | 68 | } | 
| klauss | 0:4d17cd9c8f9d | 69 | |
| klauss | 78:1353744f01e1 | 70 | bool Call_Box::has_next_try( void ){ | 
| klauss | 78:1353744f01e1 | 71 | return( ( t.read() < __TIMEOUT__ ) ? true : false ); | 
| klauss | 0:4d17cd9c8f9d | 72 | } | 
| klauss | 0:4d17cd9c8f9d | 73 | |
| klauss | 0:4d17cd9c8f9d | 74 | void Call_Box::registry( void ){ | 
| klauss | 0:4d17cd9c8f9d | 75 | if( this->sip == NULL ){ | 
| klauss | 0:4d17cd9c8f9d | 76 | this->sip = new Sip( ext, port ); | 
| klauss | 87:679ee0d594a9 | 77 | if( sip == NULL ){ | 
| klauss | 87:679ee0d594a9 | 78 | memory_is_over = true; | 
| klauss | 89:0fe315117b00 | 79 | if( debug_memory ) debug_msg("Call_Box cb allocation fail"); | 
| klauss | 87:679ee0d594a9 | 80 | return; | 
| klauss | 87:679ee0d594a9 | 81 | } | 
| klauss | 0:4d17cd9c8f9d | 82 | |
| klauss | 78:1353744f01e1 | 83 | if( sip->registry() > 0x00 ) reset_elapsed_time(); | 
| klauss | 81:3656f00ab3db | 84 | if( debug_aging ) debug_msg( "%d Registered - ( %f )", ext, __TIMEOUT__ - t.read() ); | 
| klauss | 78:1353744f01e1 | 85 | |
| klauss | 0:4d17cd9c8f9d | 86 | delete( sip ); | 
| klauss | 0:4d17cd9c8f9d | 87 | this->sip = NULL; | 
| klauss | 0:4d17cd9c8f9d | 88 | }else{ | 
| klauss | 78:1353744f01e1 | 89 | if( sip->registry() > 0x00 ) reset_elapsed_time(); | 
| klauss | 81:3656f00ab3db | 90 | if( debug_aging ) debug_msg( "%d Registered - ( %f )", ext, __TIMEOUT__ - t.read() ); | 
| klauss | 0:4d17cd9c8f9d | 91 | } | 
| klauss | 0:4d17cd9c8f9d | 92 | } | 
| klauss | 0:4d17cd9c8f9d | 93 | |
| klauss | 0:4d17cd9c8f9d | 94 | void Call_Box::unregistry( void ){ | 
| klauss | 0:4d17cd9c8f9d | 95 | if( this->sip == NULL ){ | 
| klauss | 0:4d17cd9c8f9d | 96 | this->sip = new Sip( ext, port ); | 
| klauss | 87:679ee0d594a9 | 97 | if( sip == NULL ){ | 
| klauss | 87:679ee0d594a9 | 98 | memory_is_over = true; | 
| klauss | 89:0fe315117b00 | 99 | if( debug_memory ) debug_msg("Call_Box cb allocation fail"); | 
| klauss | 87:679ee0d594a9 | 100 | return; | 
| klauss | 87:679ee0d594a9 | 101 | } | 
| klauss | 0:4d17cd9c8f9d | 102 | sip->send_unregistry_pkg(); | 
| klauss | 0:4d17cd9c8f9d | 103 | |
| klauss | 0:4d17cd9c8f9d | 104 | delete( sip ); | 
| klauss | 0:4d17cd9c8f9d | 105 | this->sip = NULL; | 
| klauss | 0:4d17cd9c8f9d | 106 | }else{ | 
| klauss | 0:4d17cd9c8f9d | 107 | sip->send_unregistry_pkg(); | 
| klauss | 0:4d17cd9c8f9d | 108 | } | 
| klauss | 0:4d17cd9c8f9d | 109 | } | 
| klauss | 0:4d17cd9c8f9d | 110 | |
| klauss | 0:4d17cd9c8f9d | 111 | VZ_call * Call_Box::invite( void ){ | 
| klauss | 99:e80850c51106 | 112 | //t.stop(); | 
| klauss | 29:7246460b73f8 | 113 | VZ_call * call = NULL; | 
| klauss | 29:7246460b73f8 | 114 | |
| klauss | 0:4d17cd9c8f9d | 115 | if( this->sip == NULL ){ | 
| klauss | 0:4d17cd9c8f9d | 116 | this->sip = new Sip( ext, port ); | 
| klauss | 0:4d17cd9c8f9d | 117 | |
| klauss | 87:679ee0d594a9 | 118 | if( sip != NULL ){ | 
| klauss | 87:679ee0d594a9 | 119 | memory_is_over = true; | 
| klauss | 89:0fe315117b00 | 120 | if( debug_memory ) debug_msg("Call_Box cb allocation fail"); | 
| klauss | 87:679ee0d594a9 | 121 | return( NULL ); | 
| klauss | 87:679ee0d594a9 | 122 | }else{ | 
| klauss | 87:679ee0d594a9 | 123 | call = sip->invite(); | 
| klauss | 87:679ee0d594a9 | 124 | |
| klauss | 87:679ee0d594a9 | 125 | delete( sip ); | 
| klauss | 87:679ee0d594a9 | 126 | this->sip = NULL; | 
| klauss | 87:679ee0d594a9 | 127 | } | 
| klauss | 29:7246460b73f8 | 128 | }else{ | 
| klauss | 29:7246460b73f8 | 129 | call = sip->invite(); | 
| klauss | 0:4d17cd9c8f9d | 130 | } | 
| klauss | 29:7246460b73f8 | 131 | |
| klauss | 29:7246460b73f8 | 132 | if( debug_cb ) debug_msg("Call returned value :: %p ", (void * )call ); | 
| klauss | 29:7246460b73f8 | 133 | if( call == NULL ) t.start(); | 
| klauss | 29:7246460b73f8 | 134 | return( call ); | 
| klauss | 0:4d17cd9c8f9d | 135 | } | 
| klauss | 0:4d17cd9c8f9d | 136 | |
| klauss | 0:4d17cd9c8f9d | 137 | /* Retorna | 
| klauss | 0:4d17cd9c8f9d | 138 | = 0 :: ok | 
| klauss | 0:4d17cd9c8f9d | 139 | < 0 :: tive problemas | 
| klauss | 0:4d17cd9c8f9d | 140 | > 0 :: devo remover essa call do vetor de calls | 
| klauss | 0:4d17cd9c8f9d | 141 | */ | 
| klauss | 0:4d17cd9c8f9d | 142 | int Call_Box::listen_SIP_server( void ){ | 
| klauss | 0:4d17cd9c8f9d | 143 | if( this->sip == NULL ){ | 
| klauss | 0:4d17cd9c8f9d | 144 | if( status == cb_on_call || status == cb_idle ){ | 
| klauss | 0:4d17cd9c8f9d | 145 | this->sip = new Sip( ext, port ); | 
| klauss | 87:679ee0d594a9 | 146 | |
| klauss | 87:679ee0d594a9 | 147 | if( sip == NULL ){ | 
| klauss | 87:679ee0d594a9 | 148 | memory_is_over = true; | 
| klauss | 89:0fe315117b00 | 149 | if( debug_memory ) debug_msg("Call_Box cb allocation fail"); | 
| klauss | 87:679ee0d594a9 | 150 | return( -1 ); | 
| klauss | 87:679ee0d594a9 | 151 | }else{ | 
| klauss | 87:679ee0d594a9 | 152 | int out_read = sip->listen_SIP_server(); | 
| klauss | 87:679ee0d594a9 | 153 | |
| klauss | 87:679ee0d594a9 | 154 | delete( sip ); | 
| klauss | 87:679ee0d594a9 | 155 | this->sip = NULL; | 
| klauss | 87:679ee0d594a9 | 156 | return( out_read ); | 
| klauss | 87:679ee0d594a9 | 157 | } | 
| klauss | 0:4d17cd9c8f9d | 158 | }else return( 0 ); | 
| klauss | 0:4d17cd9c8f9d | 159 | }else{ | 
| klauss | 0:4d17cd9c8f9d | 160 | if( status == cb_on_call || status == cb_idle ){ | 
| klauss | 13:ae278302dffe | 161 | return ( sip->listen_SIP_server() ); | 
| klauss | 13:ae278302dffe | 162 | } else return( 0 ); | 
| klauss | 0:4d17cd9c8f9d | 163 | } | 
| klauss | 0:4d17cd9c8f9d | 164 | } | 
| klauss | 0:4d17cd9c8f9d | 165 | |
| klauss | 0:4d17cd9c8f9d | 166 | void Call_Box::set_msg_id( uint8_t msg_id ){ | 
| klauss | 78:1353744f01e1 | 167 | this->msg_id = ( msg_id > 0x10 ) ? msg_id : 0x11; | 
| klauss | 0:4d17cd9c8f9d | 168 | } | 
| klauss | 0:4d17cd9c8f9d | 169 | uint8_t Call_Box::get_msg_id( void ){ return( this->msg_id ); } | 
| klauss | 0:4d17cd9c8f9d | 170 | |
| klauss | 0:4d17cd9c8f9d | 171 | void Call_Box::set_timeslice( uint8_t timeslice ){ | 
| klauss | 0:4d17cd9c8f9d | 172 | this->timeslice = timeslice; | 
| klauss | 0:4d17cd9c8f9d | 173 | } | 
| klauss | 0:4d17cd9c8f9d | 174 | uint8_t Call_Box::get_timeslice( void ){ return( this->timeslice ); } | 
| klauss | 0:4d17cd9c8f9d | 175 | |
| klauss | 0:4d17cd9c8f9d | 176 | //void Call_Box::set_sip( Sip * sip ){ | 
| klauss | 0:4d17cd9c8f9d | 177 | // this->sip = sip; | 
| klauss | 0:4d17cd9c8f9d | 178 | //} | 
| klauss | 0:4d17cd9c8f9d | 179 | |
| klauss | 0:4d17cd9c8f9d | 180 | void Call_Box::send_bye( void ){ | 
| klauss | 0:4d17cd9c8f9d | 181 | if( this->sip == NULL ){ | 
| klauss | 0:4d17cd9c8f9d | 182 | this->sip = new Sip( ext, port ); | 
| klauss | 0:4d17cd9c8f9d | 183 | |
| klauss | 87:679ee0d594a9 | 184 | if( sip == NULL ){ | 
| klauss | 87:679ee0d594a9 | 185 | memory_is_over = true; | 
| klauss | 89:0fe315117b00 | 186 | if( debug_memory ) debug_msg("Call_Box cb allocation fail"); | 
| klauss | 87:679ee0d594a9 | 187 | return; | 
| klauss | 87:679ee0d594a9 | 188 | }else{ | 
| klauss | 13:ae278302dffe | 189 | |
| klauss | 87:679ee0d594a9 | 190 | sip->send_bye(); | 
| klauss | 87:679ee0d594a9 | 191 | |
| klauss | 87:679ee0d594a9 | 192 | delete( sip ); | 
| klauss | 87:679ee0d594a9 | 193 | sip = NULL; | 
| klauss | 87:679ee0d594a9 | 194 | } | 
| klauss | 0:4d17cd9c8f9d | 195 | }else{ | 
| klauss | 0:4d17cd9c8f9d | 196 | sip->send_bye(); | 
| klauss | 0:4d17cd9c8f9d | 197 | } | 
| klauss | 1:a1758104fa1d | 198 | } | 
| klauss | 1:a1758104fa1d | 199 | |
| klauss | 1:a1758104fa1d | 200 | void Call_Box::reset_sip( void ){ | 
| klauss | 1:a1758104fa1d | 201 | if( this->sip != NULL ) delete( this->sip ); | 
| klauss | 1:a1758104fa1d | 202 | |
| klauss | 1:a1758104fa1d | 203 | this->sip = new Sip( ext, port ); | 
| klauss | 87:679ee0d594a9 | 204 | |
| klauss | 87:679ee0d594a9 | 205 | if( sip == NULL ) memory_is_over = true; | 
| klauss | 14:22a35f575502 | 206 | } | 
| klauss | 14:22a35f575502 | 207 | |
| klauss | 14:22a35f575502 | 208 | void Call_Box::set_sip_status( int status ){ | 
| klauss | 14:22a35f575502 | 209 | if( this->sip != NULL ){ | 
| klauss | 14:22a35f575502 | 210 | this->sip->status = status; | 
| klauss | 14:22a35f575502 | 211 | }else{ | 
| klauss | 78:1353744f01e1 | 212 | if( debug_cb) debug_msg("Sip equals NULL o.O"); | 
| klauss | 14:22a35f575502 | 213 | } | 
| klauss | 29:7246460b73f8 | 214 | } | 
| klauss | 29:7246460b73f8 | 215 | |
| klauss | 29:7246460b73f8 | 216 | void Call_Box::re_start_timer( void ){ | 
| klauss | 29:7246460b73f8 | 217 | t.start(); | 
| klauss | 78:1353744f01e1 | 218 | } | 
| klauss | 78:1353744f01e1 | 219 | |
| klauss | 78:1353744f01e1 | 220 | int8_t Call_Box::get_next_aging_type( void ){ | 
| klauss | 78:1353744f01e1 | 221 | next_aging_type--; | 
| klauss | 78:1353744f01e1 | 222 | |
| klauss | 78:1353744f01e1 | 223 | if( ( next_aging_type % 2 ) == 0 || ( next_aging_type > __CYCLES__ - 15 ) ) return( REQUEST_REGISTRY ); | 
| klauss | 78:1353744f01e1 | 224 | |
| klauss | 78:1353744f01e1 | 225 | else if( ( next_aging_type % 7 ) == 0 ) return( REQUEST_PEND ); | 
| klauss | 78:1353744f01e1 | 226 | |
| klauss | 78:1353744f01e1 | 227 | else return( REQUEST_PING ); | 
| klauss | 81:3656f00ab3db | 228 | } | 
| klauss | 81:3656f00ab3db | 229 | |
| klauss | 81:3656f00ab3db | 230 | void Call_Box::set_invite_response_ok( void ){ this->invite_response = true; } | 
| klauss | 92:92df17f538a8 | 231 | |
| klauss | 92:92df17f538a8 | 232 | //void Call_Box::set_first_invite_response_ok( void ){ this->invite_first_response = true; | 
| klauss | 92:92df17f538a8 | 233 | // debug_msg(""); | 
| klauss | 92:92df17f538a8 | 234 | //} | 
| klauss | 92:92df17f538a8 | 235 | |
| klauss | 81:3656f00ab3db | 236 | void Call_Box::set_invite_response_pending( void ){ this->invite_response = false; } | 
| klauss | 81:3656f00ab3db | 237 | bool Call_Box::get_invite_response( void ){ return( this->invite_response ); } | 
| klauss | 81:3656f00ab3db | 238 | |
| klauss | 92:92df17f538a8 | 239 | //void Call_Box::set_first_invite_response_pending( void ){ this->first_invite_response = false; } | 
| klauss | 92:92df17f538a8 | 240 | //bool Call_Box::get_first_invite_response( void ){ return( this->first_invite_response ); } | 
| klauss | 92:92df17f538a8 | 241 | |
| klauss | 92:92df17f538a8 | 242 | |
| klauss | 92:92df17f538a8 | 243 | void Call_Box::invite_retry_count_reset( void ){ invite_retry_count = MAX_INVITE_RETRY; } | 
| klauss | 92:92df17f538a8 | 244 | uint8_t Call_Box::get_invite_retry_count( void ){ | 
| klauss | 92:92df17f538a8 | 245 | return ( invite_retry_count ) ? invite_retry_count-- : 0; | 
| klauss | 92:92df17f538a8 | 246 | } | 
| klauss | 92:92df17f538a8 | 247 | |
| klauss | 81:3656f00ab3db | 248 | void Call_Box::set_bye_response_ok( void ){ this->bye_response = true; } | 
| klauss | 81:3656f00ab3db | 249 | void Call_Box::set_bye_response_pending( void ){ this->bye_response = false; } | 
| klauss | 89:0fe315117b00 | 250 | bool Call_Box::get_bye_response( void ){ return( this->bye_response ); } | 
| klauss | 89:0fe315117b00 | 251 | |
| klauss | 89:0fe315117b00 | 252 | |
| klauss | 89:0fe315117b00 | 253 | int Call_Box::get_sip_socket_fd( void ){ | 
| klauss | 89:0fe315117b00 | 254 | return( sip_socket_fd ); | 
| klauss | 89:0fe315117b00 | 255 | } | 
| klauss | 89:0fe315117b00 | 256 | |
| klauss | 91:c2a86b1f8aaa | 257 | int Call_Box::sip_udp_incomming_pkg( void ){ | 
| klauss | 91:c2a86b1f8aaa | 258 | return sip->udp_incomming_pkg(); | 
| klauss | 91:c2a86b1f8aaa | 259 | } | 
| klauss | 91:c2a86b1f8aaa | 260 | |
| klauss | 91:c2a86b1f8aaa | 261 | void Call_Box::reset_cb_status( void ){ | 
| klauss | 91:c2a86b1f8aaa | 262 | if( sip != NULL ) sip->reset_call(); | 
| klauss | 97:8985817e8847 | 263 | } | 
| klauss | 97:8985817e8847 | 264 | |
| klauss | 97:8985817e8847 | 265 | int Call_Box::get_sip_ext( void ){ return this->sip->get_ext(); } | 
| klauss | 99:e80850c51106 | 266 | int Call_Box::get_sip_port( void ){ return this->sip->get_port(); } | 
| klauss | 99:e80850c51106 | 267 | |
| klauss | 99:e80850c51106 | 268 | |
| klauss | 99:e80850c51106 | 269 | int Call_Box::get_timer( void ){ return t.read(); } | 
