![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
main.cpp@82:f55d13babca0, 2014-12-30 (annotated)
- Committer:
- klauss
- Date:
- Tue Dec 30 21:00:00 2014 +0000
- Revision:
- 82:f55d13babca0
- Parent:
- 81:3656f00ab3db
- Child:
- 83:b8a1d8fdeaeb
pequena correcao de bug relacionado ao tratamento de pacotes de bye do cbx.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 74:81c47fff88a5 | 1 | #include "telemetry.h" |
klauss | 74:81c47fff88a5 | 2 | #include "eth.h" |
klauss | 74:81c47fff88a5 | 3 | #include "bits.h" |
klauss | 74:81c47fff88a5 | 4 | #include "UART3Interrupt.h" // for RXBuffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 74:81c47fff88a5 | 5 | #include "utils.h" |
klauss | 74:81c47fff88a5 | 6 | #include "parallelcpld.h" |
klauss | 74:81c47fff88a5 | 7 | #include "debug.h" |
klauss | 74:81c47fff88a5 | 8 | #include "flood.h" |
klauss | 74:81c47fff88a5 | 9 | #include "bootloader_cbx.h" |
klauss | 74:81c47fff88a5 | 10 | #include "call_manager.h" |
klauss | 74:81c47fff88a5 | 11 | #include "call_box_manager.h" |
klauss | 74:81c47fff88a5 | 12 | #include "sip_manager.h" |
klauss | 74:81c47fff88a5 | 13 | #include "shared_variables.h" |
klauss | 74:81c47fff88a5 | 14 | #include "prompt.h" |
klauss | 0:4d17cd9c8f9d | 15 | #include "configs.h" |
klauss | 0:4d17cd9c8f9d | 16 | |
klauss | 0:4d17cd9c8f9d | 17 | int main(){ |
klauss | 81:3656f00ab3db | 18 | init_wdt(); |
klauss | 79:9bc12aa305a9 | 19 | |
klauss | 81:3656f00ab3db | 20 | config_lpc(); |
klauss | 79:9bc12aa305a9 | 21 | |
klauss | 0:4d17cd9c8f9d | 22 | start_cpld(); |
klauss | 79:9bc12aa305a9 | 23 | |
klauss | 43:455522f98de5 | 24 | init_fsystem(); |
klauss | 43:455522f98de5 | 25 | |
klauss | 78:1353744f01e1 | 26 | short int eth_status = __init_eth__(); |
klauss | 8:e3bfe62a477e | 27 | if( eth_status ){ |
klauss | 48:195c97f12e8e | 28 | send_msg("Cannot connect to eth\n\r"); |
klauss | 43:455522f98de5 | 29 | }else{ |
klauss | 81:3656f00ab3db | 30 | send_msg("******* Connection eth - ok *******"); |
klauss | 78:1353744f01e1 | 31 | init_prompt_eth(); |
klauss | 82:f55d13babca0 | 32 | init_external_wdt() |
klauss | 8:e3bfe62a477e | 33 | } |
klauss | 78:1353744f01e1 | 34 | |
klauss | 78:1353744f01e1 | 35 | Timer sync_timer, led_sync_timer; |
klauss | 78:1353744f01e1 | 36 | sync_timer.start(), led_sync_timer.start(); |
klauss | 0:4d17cd9c8f9d | 37 | |
klauss | 0:4d17cd9c8f9d | 38 | /* representa ramal do call box */ |
klauss | 0:4d17cd9c8f9d | 39 | int ext = 0; |
klauss | 0:4d17cd9c8f9d | 40 | |
klauss | 0:4d17cd9c8f9d | 41 | /* representa porta do call box */ |
klauss | 78:1353744f01e1 | 42 | int port = 0; |
klauss | 78:1353744f01e1 | 43 | |
klauss | 0:4d17cd9c8f9d | 44 | /* buffer para onde se copia os dados vindos do cb para tratameno interno */ |
klauss | 0:4d17cd9c8f9d | 45 | uint8_t buffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 0:4d17cd9c8f9d | 46 | |
klauss | 0:4d17cd9c8f9d | 47 | /* buffer de escrita do pacote de saida que sera enviado pro cb / servidor */ |
klauss | 0:4d17cd9c8f9d | 48 | uint8_t write_buffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 0:4d17cd9c8f9d | 49 | |
klauss | 0:4d17cd9c8f9d | 50 | /* ponteiro que aponta para os dados vindo do CPLD */ |
klauss | 0:4d17cd9c8f9d | 51 | uint8_t * buffer_from_cb_ptr = ( uint8_t * )RXBuffer; |
klauss | 0:4d17cd9c8f9d | 52 | |
klauss | 62:07e5bdc9f8f7 | 53 | /* Armazena o ultimo pacote recebido dos CBx */ |
klauss | 62:07e5bdc9f8f7 | 54 | uint8_t cb_rx_buffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 62:07e5bdc9f8f7 | 55 | |
klauss | 0:4d17cd9c8f9d | 56 | /* referencia para os dados contidos no pacote, sem o header */ |
klauss | 0:4d17cd9c8f9d | 57 | uint8_t * data = NULL; |
klauss | 0:4d17cd9c8f9d | 58 | |
klauss | 0:4d17cd9c8f9d | 59 | /* gerencia o tipo do pacote para providenciar tratamento adequado */ |
klauss | 0:4d17cd9c8f9d | 60 | volatile uint8_t type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 61 | |
klauss | 0:4d17cd9c8f9d | 62 | /* representa a lista dos Call Boxes atualmente reconhecidos pela cabeceira */ |
klauss | 0:4d17cd9c8f9d | 63 | Vector * v_cb = new Vector(); |
klauss | 0:4d17cd9c8f9d | 64 | |
klauss | 0:4d17cd9c8f9d | 65 | /* representa a lista de ligacoes ativas na cabeceira */ |
klauss | 0:4d17cd9c8f9d | 66 | Vector * v_call = new Vector(); |
klauss | 0:4d17cd9c8f9d | 67 | |
klauss | 0:4d17cd9c8f9d | 68 | /* gerencia a distribuicao de timeslice para os call boxes */ |
klauss | 0:4d17cd9c8f9d | 69 | Timeslice * ts = new Timeslice(); |
klauss | 78:1353744f01e1 | 70 | |
klauss | 81:3656f00ab3db | 71 | /* instancia o vetor de call boxes // calls */ |
klauss | 0:4d17cd9c8f9d | 72 | if( v_cb == NULL ){ |
klauss | 0:4d17cd9c8f9d | 73 | while( v_cb == NULL ){ |
klauss | 0:4d17cd9c8f9d | 74 | Vector * v_cb = new Vector(); |
klauss | 78:1353744f01e1 | 75 | if( sync_timer.read() > 5 ){ |
klauss | 48:195c97f12e8e | 76 | send_msg("Erro ao alocar o vetor de CBx"); |
klauss | 78:1353744f01e1 | 77 | sync_timer.reset(); |
klauss | 0:4d17cd9c8f9d | 78 | } |
klauss | 0:4d17cd9c8f9d | 79 | } |
klauss | 0:4d17cd9c8f9d | 80 | }else if( v_call == NULL ){ |
klauss | 0:4d17cd9c8f9d | 81 | while( v_call == NULL ){ |
klauss | 0:4d17cd9c8f9d | 82 | Vector * v_call = new Vector(); |
klauss | 78:1353744f01e1 | 83 | if( sync_timer.read() > 5 ){ |
klauss | 48:195c97f12e8e | 84 | send_msg("Erro ao alocar o vetor de Calls"); |
klauss | 78:1353744f01e1 | 85 | sync_timer.reset(); |
klauss | 0:4d17cd9c8f9d | 86 | } |
klauss | 0:4d17cd9c8f9d | 87 | } |
klauss | 0:4d17cd9c8f9d | 88 | } |
klauss | 63:0d95da692bb4 | 89 | |
klauss | 63:0d95da692bb4 | 90 | for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ){ |
klauss | 63:0d95da692bb4 | 91 | cb_rx_buffer[ i ] = 0; |
klauss | 63:0d95da692bb4 | 92 | cb_tx_buffer[ i ] = 0; |
klauss | 63:0d95da692bb4 | 93 | } |
klauss | 63:0d95da692bb4 | 94 | |
klauss | 0:4d17cd9c8f9d | 95 | reset_leds(); |
klauss | 78:1353744f01e1 | 96 | |
klauss | 33:735fd60e96d8 | 97 | udp_timer.start(); |
klauss | 68:b54993674190 | 98 | |
klauss | 68:b54993674190 | 99 | init_telemetry_handler(); |
klauss | 68:b54993674190 | 100 | |
klauss | 81:3656f00ab3db | 101 | init_ranges(); |
klauss | 81:3656f00ab3db | 102 | |
klauss | 80:61d61c9eb75c | 103 | init_refresh(); |
klauss | 80:61d61c9eb75c | 104 | |
klauss | 78:1353744f01e1 | 105 | init_aging(); |
klauss | 78:1353744f01e1 | 106 | |
klauss | 81:3656f00ab3db | 107 | //init_invite_pgk_retry_manager(); |
klauss | 81:3656f00ab3db | 108 | |
klauss | 81:3656f00ab3db | 109 | //void init_bye_pgk_retry_manager(); |
klauss | 62:07e5bdc9f8f7 | 110 | static uint8_t count = 0; |
klauss | 78:1353744f01e1 | 111 | |
klauss | 78:1353744f01e1 | 112 | short int bl_ret = init_bl_handler(); |
klauss | 67:cdedc64d9921 | 113 | if( dbl ) send_msg("Valor de retorno do bl_habdler = %d", bl_ret ); |
klauss | 67:cdedc64d9921 | 114 | |
klauss | 78:1353744f01e1 | 115 | uint8_t max_registered_cbx = 0; |
klauss | 78:1353744f01e1 | 116 | |
klauss | 81:3656f00ab3db | 117 | bool wake_all = false; |
klauss | 81:3656f00ab3db | 118 | |
klauss | 81:3656f00ab3db | 119 | uint16_t pkg_wdt = RX_CB_IDLE; |
klauss | 81:3656f00ab3db | 120 | uint16_t eth_wdt = ETH_CONNECT_TIMEOUT; |
klauss | 68:b54993674190 | 121 | |
klauss | 68:b54993674190 | 122 | send_msg("Ready"); |
klauss | 68:b54993674190 | 123 | |
klauss | 78:1353744f01e1 | 124 | /*------------------------------------------ main loop ---------------------------------------------------------------*/ |
Cola | 21:0bd688722e81 | 125 | while( true ){ |
klauss | 78:1353744f01e1 | 126 | if( v_cb->size() > max_registered_cbx ) max_registered_cbx = v_cb->size(); |
klauss | 78:1353744f01e1 | 127 | |
klauss | 78:1353744f01e1 | 128 | if( sync_timer.read() > 5 ){ |
klauss | 78:1353744f01e1 | 129 | sync_timer.reset(); |
klauss | 78:1353744f01e1 | 130 | |
klauss | 78:1353744f01e1 | 131 | if( debug_cks == true ){ |
klauss | 81:3656f00ab3db | 132 | pcks_s = true; |
klauss | 78:1353744f01e1 | 133 | } |
klauss | 78:1353744f01e1 | 134 | |
klauss | 78:1353744f01e1 | 135 | if( debug_alive == true ){ |
klauss | 81:3656f00ab3db | 136 | pshowcb = true; |
klauss | 78:1353744f01e1 | 137 | } |
klauss | 81:3656f00ab3db | 138 | |
klauss | 81:3656f00ab3db | 139 | if( !( ++count % 15 ) ){ |
klauss | 78:1353744f01e1 | 140 | if( eth_status ){ |
klauss | 78:1353744f01e1 | 141 | eth_status = __init_eth__(); |
klauss | 78:1353744f01e1 | 142 | if( eth_status ){ |
klauss | 78:1353744f01e1 | 143 | if( debug_main ) debug_msg("Cannot connect to eth"); |
klauss | 78:1353744f01e1 | 144 | }else{ |
klauss | 78:1353744f01e1 | 145 | if( debug_main ) debug_msg("Connection eth - ok"); |
klauss | 78:1353744f01e1 | 146 | init_prompt_eth(); |
klauss | 82:f55d13babca0 | 147 | init_external_wdt() |
klauss | 78:1353744f01e1 | 148 | } |
klauss | 78:1353744f01e1 | 149 | } |
klauss | 78:1353744f01e1 | 150 | } |
klauss | 81:3656f00ab3db | 151 | //35 sec. |
klauss | 81:3656f00ab3db | 152 | if( ( count > 7 ) && ( wake_all == false ) ){ |
klauss | 81:3656f00ab3db | 153 | wake_all = true; |
klauss | 78:1353744f01e1 | 154 | if( debug_wake == true ) send_msg( "Time to wake" ); |
klauss | 78:1353744f01e1 | 155 | } |
klauss | 78:1353744f01e1 | 156 | |
klauss | 81:3656f00ab3db | 157 | if( wake_all ) if( v_call->size() == 0 ) wake_all_up( v_cb, buffer, write_buffer ); |
klauss | 78:1353744f01e1 | 158 | } |
klauss | 78:1353744f01e1 | 159 | |
klauss | 68:b54993674190 | 160 | //FIXMEj colocar uma condicao aqui caso necessario pra nao comprometer ligacoes ... |
klauss | 35:96885a7931f0 | 161 | prompt_process( NULL ); |
klauss | 63:0d95da692bb4 | 162 | |
klauss | 63:0d95da692bb4 | 163 | if( r_stats ){ |
klauss | 63:0d95da692bb4 | 164 | boot_counter = 0; |
klauss | 63:0d95da692bb4 | 165 | registry_counter = 0; |
klauss | 63:0d95da692bb4 | 166 | invite_counter = 0; |
klauss | 63:0d95da692bb4 | 167 | audio_counter = 0; |
klauss | 63:0d95da692bb4 | 168 | telemetry_counter = 0; |
klauss | 63:0d95da692bb4 | 169 | cb_bye_counter = 0; |
klauss | 63:0d95da692bb4 | 170 | prompt_counter = 0; |
klauss | 63:0d95da692bb4 | 171 | flood_counter = 0; |
klauss | 67:cdedc64d9921 | 172 | bootloader_cbx_counter = 0; |
klauss | 63:0d95da692bb4 | 173 | r_stats = false; |
klauss | 63:0d95da692bb4 | 174 | stats = true; |
klauss | 63:0d95da692bb4 | 175 | } |
klauss | 81:3656f00ab3db | 176 | |
klauss | 63:0d95da692bb4 | 177 | if( stats ){ |
klauss | 63:0d95da692bb4 | 178 | char str[ 200 ]; |
klauss | 81:3656f00ab3db | 179 | snprintf( str, 200, "\n\rReceived Pkgs::\n\r Boot :: %u\n\r Registry :: %u\n\r Ivite :: %u\n\r Audio :: %u\n\r Telemetry :: %u\n\r Cb_bye :: %u\n\r Prompt :: %u\n\r Flood :: %u\n\r Bootloader_cbx :: %u\n\r", |
klauss | 67:cdedc64d9921 | 180 | boot_counter, registry_counter, invite_counter, audio_counter, telemetry_counter, cb_bye_counter, prompt_counter, flood_counter, bootloader_cbx_counter ); |
klauss | 63:0d95da692bb4 | 181 | send_msg( str ); |
klauss | 63:0d95da692bb4 | 182 | stats =false; |
klauss | 63:0d95da692bb4 | 183 | } |
klauss | 63:0d95da692bb4 | 184 | |
klauss | 62:07e5bdc9f8f7 | 185 | if( list ){ |
klauss | 78:1353744f01e1 | 186 | uint8_t missed_cb = ( ( max_ext - min_ext ) + 1 ) - v_cb->size(); |
klauss | 78:1353744f01e1 | 187 | |
klauss | 78:1353744f01e1 | 188 | if( ( max_ext % 2 ) == 0 ) missed_cb++; |
klauss | 78:1353744f01e1 | 189 | |
klauss | 78:1353744f01e1 | 190 | if( min_ext % 2 ) missed_cb++; |
klauss | 78:1353744f01e1 | 191 | |
klauss | 78:1353744f01e1 | 192 | if( min_ext == 0 && max_ext == 0 ) missed_cb = 0; |
klauss | 78:1353744f01e1 | 193 | |
klauss | 78:1353744f01e1 | 194 | send_msg("Registered %d[ %d ] CBx ( %d - %d ) - Missed %d -- Remain_timeslices :: %d :: v_call->size() :: %d", v_cb->size(), max_registered_cbx, min_ext, max_ext, missed_cb, ts->remain_timeslices(), v_call->size() ); |
klauss | 78:1353744f01e1 | 195 | if( v_cb->size() == 1 ){ |
klauss | 78:1353744f01e1 | 196 | send_msg(" %d ", ( ( Call_Box * )v_cb->get_element( 0 ) )->get_ext() ); |
klauss | 78:1353744f01e1 | 197 | }else if( v_cb->size() > 1 ){ |
klauss | 62:07e5bdc9f8f7 | 198 | char str[ 1024 ]; |
klauss | 78:1353744f01e1 | 199 | int ext_list[ __MAX_CB_IN_A_BRANCH__ ]; |
klauss | 62:07e5bdc9f8f7 | 200 | register int i = 0; |
klauss | 62:07e5bdc9f8f7 | 201 | for( ; i < v_cb->size(); i++ ){ |
klauss | 78:1353744f01e1 | 202 | ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext(); |
klauss | 62:07e5bdc9f8f7 | 203 | } |
klauss | 62:07e5bdc9f8f7 | 204 | |
klauss | 62:07e5bdc9f8f7 | 205 | qsort( ext_list, v_cb->size(), sizeof( int ), ls_comp ); |
klauss | 62:07e5bdc9f8f7 | 206 | |
klauss | 62:07e5bdc9f8f7 | 207 | char aux[ 16 ]; |
klauss | 78:1353744f01e1 | 208 | strcpy( str, "\r\n> " ); |
klauss | 62:07e5bdc9f8f7 | 209 | for( i = 0; i < v_cb->size() - 1; i++ ){ |
klauss | 62:07e5bdc9f8f7 | 210 | sprintf( aux, "%i, ", ext_list[ i ] ); |
klauss | 62:07e5bdc9f8f7 | 211 | strcat( str, aux ); |
klauss | 78:1353744f01e1 | 212 | if( ( i != 0 ) && !( ( i + 1 ) % 16 ) ) strcat( str, "\r\n> " ); |
klauss | 62:07e5bdc9f8f7 | 213 | } |
klauss | 62:07e5bdc9f8f7 | 214 | sprintf( aux, "%i ", ext_list[ i ] ); |
klauss | 62:07e5bdc9f8f7 | 215 | strcat( str, aux ); |
klauss | 62:07e5bdc9f8f7 | 216 | send_msg( "%s", str ); |
klauss | 62:07e5bdc9f8f7 | 217 | } |
klauss | 62:07e5bdc9f8f7 | 218 | list = false; |
klauss | 62:07e5bdc9f8f7 | 219 | } |
klauss | 62:07e5bdc9f8f7 | 220 | |
klauss | 81:3656f00ab3db | 221 | if( long_list ){ |
klauss | 81:3656f00ab3db | 222 | //FIXME implementar um algoritmo que me mostre o timeout de cada cbx do lado da linha, desse. |
klauss | 81:3656f00ab3db | 223 | uint8_t missed_cb = ( ( max_ext - min_ext ) + 1 ) - v_cb->size(); |
klauss | 81:3656f00ab3db | 224 | |
klauss | 81:3656f00ab3db | 225 | if( ( max_ext % 2 ) == 0 ) missed_cb++; |
klauss | 81:3656f00ab3db | 226 | |
klauss | 81:3656f00ab3db | 227 | if( min_ext % 2 ) missed_cb++; |
klauss | 81:3656f00ab3db | 228 | |
klauss | 81:3656f00ab3db | 229 | if( min_ext == 0 && max_ext == 0 ) missed_cb = 0; |
klauss | 81:3656f00ab3db | 230 | |
klauss | 81:3656f00ab3db | 231 | send_msg("Registered %d[ %d ] CBx ( %d - %d ) - Missed %d -- Remain_timeslices :: %d :: v_call->size() :: %d", v_cb->size(), max_registered_cbx, min_ext, max_ext, missed_cb, ts->remain_timeslices(), v_call->size() ); |
klauss | 81:3656f00ab3db | 232 | if( v_cb->size() == 1 ){ |
klauss | 81:3656f00ab3db | 233 | send_msg(" %d ", ( ( Call_Box * )v_cb->get_element( 0 ) )->get_ext() ); |
klauss | 81:3656f00ab3db | 234 | }else if( v_cb->size() > 1 ){ |
klauss | 81:3656f00ab3db | 235 | char str[ 1024 ]; |
klauss | 81:3656f00ab3db | 236 | int ext_list[ __MAX_CB_IN_A_BRANCH__ ]; |
klauss | 81:3656f00ab3db | 237 | register int i = 0; |
klauss | 81:3656f00ab3db | 238 | for( ; i < ( v_cb->size() * 2 ); i += 2 ){ |
klauss | 81:3656f00ab3db | 239 | ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext(); |
klauss | 81:3656f00ab3db | 240 | ext_list[ i + 1 ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_elapsed_time(); |
klauss | 81:3656f00ab3db | 241 | } |
klauss | 81:3656f00ab3db | 242 | |
klauss | 81:3656f00ab3db | 243 | qsort( ext_list, v_cb->size(), sizeof( int ) * 2, ls_comp ); |
Cola | 24:270b436a1bb0 | 244 | |
klauss | 81:3656f00ab3db | 245 | char aux[ 16 ]; |
klauss | 81:3656f00ab3db | 246 | strcpy( str, "\r\n> " ); |
klauss | 81:3656f00ab3db | 247 | for( i = 0; i < ( v_cb->size() * 2 ) - 2; i++ ){ |
klauss | 81:3656f00ab3db | 248 | sprintf( aux, "%i - %i,", ext_list[ i ], ext_list[ i + 1 ] ); |
klauss | 81:3656f00ab3db | 249 | strcat( str, aux ); |
klauss | 81:3656f00ab3db | 250 | strcat( str, "\r\n> " ); |
klauss | 81:3656f00ab3db | 251 | } |
klauss | 81:3656f00ab3db | 252 | sprintf( aux, "%i - %i,", ext_list[ i ], ext_list[ i + 1 ] ); |
klauss | 81:3656f00ab3db | 253 | strcat( str, aux ); |
klauss | 81:3656f00ab3db | 254 | send_msg( "%s", str ); |
klauss | 81:3656f00ab3db | 255 | } |
klauss | 81:3656f00ab3db | 256 | list = false; |
klauss | 52:12930cef17c4 | 257 | } |
klauss | 52:12930cef17c4 | 258 | |
klauss | 78:1353744f01e1 | 259 | if( pshowcb == true ){ |
klauss | 78:1353744f01e1 | 260 | send_msg("Registered %d ( of %d ) CBx ( %d - %d ) -- Remain_timeslices :: %d :: v_call->size() :: %d", v_cb->size(), max_registered_cbx, min_ext, max_ext, ts->remain_timeslices(), v_call->size() ); |
Cola | 24:270b436a1bb0 | 261 | for( register uint8_t i = 0; i < v_cb->size(); i++ ){ |
klauss | 30:8dfb6d8de53d | 262 | Call_Box * cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 78:1353744f01e1 | 263 | char cb_status[ 32 ]; |
klauss | 78:1353744f01e1 | 264 | char cb_sip_status[ 32 ]; |
klauss | 78:1353744f01e1 | 265 | switch( cb->status ){ |
klauss | 78:1353744f01e1 | 266 | case cb_idle : { strcpy( cb_status, "cb_idle" ); break; } |
klauss | 78:1353744f01e1 | 267 | case cb_ringing : { strcpy( cb_status, "cb_ringing" ); break; } |
klauss | 78:1353744f01e1 | 268 | case cb_trying : { strcpy( cb_status,"cb_trying" ); break; } |
klauss | 78:1353744f01e1 | 269 | case cb_on_call : { strcpy( cb_status, "cb_on_call" ); break; } |
klauss | 78:1353744f01e1 | 270 | case cb_busy : { strcpy( cb_status, "cb_busy" ); break; } |
klauss | 78:1353744f01e1 | 271 | case cb_denied : { strcpy( cb_status, "cb_denied" ); break; } |
klauss | 78:1353744f01e1 | 272 | } |
klauss | 78:1353744f01e1 | 273 | switch( cb->sip->status ){ |
klauss | 78:1353744f01e1 | 274 | case sip_idle : { strcpy( cb_sip_status, "sip_idle" ); break; } |
klauss | 78:1353744f01e1 | 275 | case sip_waiting_trying : { strcpy( cb_sip_status, "sip_waiting_trying" ); break; } |
klauss | 78:1353744f01e1 | 276 | case sip_trying : { strcpy( cb_sip_status, "sip_trying" ); break; } |
klauss | 78:1353744f01e1 | 277 | case sip_ringing : { strcpy( cb_sip_status, "sip_ringing" ); break; } |
klauss | 78:1353744f01e1 | 278 | case sip_busy : { strcpy( cb_sip_status, "sip_busy" ); break; } |
klauss | 78:1353744f01e1 | 279 | case sip_ok : { strcpy( cb_sip_status, "sip_ok" ); break; } |
klauss | 78:1353744f01e1 | 280 | case sip_on_call : { strcpy( cb_sip_status, "sip_on_call" ); break; } |
klauss | 78:1353744f01e1 | 281 | case sip_denied : { strcpy( cb_sip_status, "sip_denied" ); break; } |
klauss | 78:1353744f01e1 | 282 | } |
klauss | 78:1353744f01e1 | 283 | char cbx_to_string[ 254 ]; |
klauss | 78:1353744f01e1 | 284 | char aux[ 16 ]; |
klauss | 78:1353744f01e1 | 285 | strcpy( cbx_to_string, "Ext :: " ); |
klauss | 78:1353744f01e1 | 286 | itoa( cb->get_ext(), aux , 10 ); |
klauss | 78:1353744f01e1 | 287 | strcat( cbx_to_string, aux ); |
klauss | 78:1353744f01e1 | 288 | strcat( cbx_to_string, " :: Status -- " ); |
klauss | 78:1353744f01e1 | 289 | strcat( cbx_to_string, cb_status ); |
klauss | 78:1353744f01e1 | 290 | strcat( cbx_to_string, " - " ); |
klauss | 78:1353744f01e1 | 291 | strcat( cbx_to_string, cb_sip_status ); |
klauss | 78:1353744f01e1 | 292 | if( cb->get_timeslice() != 0 ){ |
klauss | 78:1353744f01e1 | 293 | strcat( cbx_to_string, " -- on TimeSlice :: " ); |
klauss | 78:1353744f01e1 | 294 | itoa( cb->get_timeslice(), aux , 10 ); |
klauss | 78:1353744f01e1 | 295 | strcat( cbx_to_string, aux ); |
klauss | 78:1353744f01e1 | 296 | } |
klauss | 78:1353744f01e1 | 297 | send_msg( cbx_to_string ); |
klauss | 48:195c97f12e8e | 298 | } |
klauss | 78:1353744f01e1 | 299 | pshowcb = false; |
klauss | 48:195c97f12e8e | 300 | } |
Cola | 22:d2a4b5939115 | 301 | |
klauss | 81:3656f00ab3db | 302 | if( pflood == true ) flood(); |
klauss | 81:3656f00ab3db | 303 | |
klauss | 81:3656f00ab3db | 304 | if( debug_eth ){ |
klauss | 81:3656f00ab3db | 305 | debug_eth = false; |
klauss | 81:3656f00ab3db | 306 | send_msg("Eth status %s", ( eth_status == 0 ) ? "Connected" : "Disconnected" ); |
klauss | 81:3656f00ab3db | 307 | } |
klauss | 30:8dfb6d8de53d | 308 | |
klauss | 0:4d17cd9c8f9d | 309 | if( status != __WAITING__ ){ |
klauss | 81:3656f00ab3db | 310 | pkg_wdt = RX_CB_IDLE; |
klauss | 74:81c47fff88a5 | 311 | xmemcpy( cb_rx_buffer, buffer_from_cb_ptr, __CB_BUFFER_SIZE__ ); |
klauss | 0:4d17cd9c8f9d | 312 | status = __WAITING__; |
klauss | 81:3656f00ab3db | 313 | missed_pkg--; |
klauss | 78:1353744f01e1 | 314 | xmemcpy( buffer, cb_rx_buffer, __CB_BUFFER_SIZE__ ); |
klauss | 81:3656f00ab3db | 315 | |
klauss | 81:3656f00ab3db | 316 | if( debug_cpld ){ |
klauss | 81:3656f00ab3db | 317 | rx = true; |
klauss | 81:3656f00ab3db | 318 | tx = true; |
klauss | 81:3656f00ab3db | 319 | } |
klauss | 81:3656f00ab3db | 320 | |
klauss | 81:3656f00ab3db | 321 | if( rx ){ |
klauss | 81:3656f00ab3db | 322 | char str[ 1024 ]; |
klauss | 81:3656f00ab3db | 323 | strcpy( str, "RX :: \n\r " ); |
klauss | 81:3656f00ab3db | 324 | for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ){ |
klauss | 81:3656f00ab3db | 325 | char tmp[ 16 ]; |
klauss | 81:3656f00ab3db | 326 | strcat( str, itoa( cb_rx_buffer[ i ], tmp, 16 ) ); |
klauss | 81:3656f00ab3db | 327 | if( ( i != 0 ) && !( ( i + 1 ) % 50 ) ) strcat( str, "\n\r " ); |
klauss | 81:3656f00ab3db | 328 | |
klauss | 81:3656f00ab3db | 329 | else strcat( str, " " ); |
klauss | 81:3656f00ab3db | 330 | } |
klauss | 81:3656f00ab3db | 331 | send_msg( "%s", str ); |
klauss | 81:3656f00ab3db | 332 | rx = false; |
klauss | 81:3656f00ab3db | 333 | } |
klauss | 81:3656f00ab3db | 334 | |
klauss | 34:f19d9735428e | 335 | data = __parse_vz_pkg__( &ext, &port, &type, buffer ); |
klauss | 0:4d17cd9c8f9d | 336 | |
klauss | 78:1353744f01e1 | 337 | if( data != NULL ){ |
klauss | 78:1353744f01e1 | 338 | if( min_ext == 0 ) min_ext = ext; |
klauss | 78:1353744f01e1 | 339 | |
klauss | 78:1353744f01e1 | 340 | if( ext > max_ext ) max_ext = ext; |
klauss | 78:1353744f01e1 | 341 | |
klauss | 78:1353744f01e1 | 342 | if( ext < min_ext ) min_ext = ext; |
klauss | 78:1353744f01e1 | 343 | |
klauss | 78:1353744f01e1 | 344 | if( debug_cb_rx == true ) debug_msg("Pkg from CBx :: %d -- Type :: %d", ext, type ); |
klauss | 78:1353744f01e1 | 345 | |
klauss | 7:019b08223b87 | 346 | if( type != __AUDIO__ ){ |
klauss | 0:4d17cd9c8f9d | 347 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 348 | if( cb != NULL ){ |
klauss | 78:1353744f01e1 | 349 | if( ( data[ 0 ] & BIT7 ) >= BIT7 ){ |
klauss | 78:1353744f01e1 | 350 | if( type == __BOOT__ ){ |
klauss | 78:1353744f01e1 | 351 | send2callboxes( __build_cb_package__( ext, port, __REGISTRY__, |
klauss | 78:1353744f01e1 | 352 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 81:3656f00ab3db | 353 | }else if( ( data[ 0 ] & BIT7 ) == BIT7 ){ |
klauss | 81:3656f00ab3db | 354 | if( debug_main ) debug_msg("Received ack pkg with seq_num %d", data[ 0 ] ); |
klauss | 81:3656f00ab3db | 355 | //algum motivo pra sempre receber alguma coisa com o type == 7 ??? |
klauss | 81:3656f00ab3db | 356 | if( type == 0x07 ){ |
klauss | 81:3656f00ab3db | 357 | char str[ 1024 ]; |
klauss | 81:3656f00ab3db | 358 | strcpy( str, "RX :: \n\r " ); |
klauss | 81:3656f00ab3db | 359 | for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ){ |
klauss | 81:3656f00ab3db | 360 | char tmp[ 16 ]; |
klauss | 81:3656f00ab3db | 361 | strcat( str, itoa( cb_rx_buffer[ i ], tmp, 16 ) ); |
klauss | 81:3656f00ab3db | 362 | if( ( i != 0 ) && !( ( i + 1 ) % 50 ) ) strcat( str, "\n\r " ); |
klauss | 81:3656f00ab3db | 363 | |
klauss | 81:3656f00ab3db | 364 | else strcat( str, " " ); |
klauss | 81:3656f00ab3db | 365 | } |
klauss | 81:3656f00ab3db | 366 | send_msg( "%s", str ); |
klauss | 81:3656f00ab3db | 367 | rx = false; |
klauss | 81:3656f00ab3db | 368 | } |
klauss | 81:3656f00ab3db | 369 | switch( type ){ |
klauss | 81:3656f00ab3db | 370 | case __INVITE__ : { |
klauss | 81:3656f00ab3db | 371 | if( debug_main || debug_invite ) debug_msg("Invite Ack from %d on msg_id %d", ext, cb->get_msg_id() ); |
klauss | 81:3656f00ab3db | 372 | cb->set_invite_response_ok(); |
klauss | 81:3656f00ab3db | 373 | break; |
klauss | 81:3656f00ab3db | 374 | } |
klauss | 81:3656f00ab3db | 375 | case __CB_BYE__ : { |
klauss | 81:3656f00ab3db | 376 | if( debug_main || debug_invite ) debug_msg("BYE Ack from %d on msg_id %d", ext, cb->get_msg_id() ); |
klauss | 81:3656f00ab3db | 377 | cb->set_bye_response_ok(); |
klauss | 81:3656f00ab3db | 378 | break; |
klauss | 81:3656f00ab3db | 379 | } |
klauss | 81:3656f00ab3db | 380 | case __REGISTRY__ : { |
klauss | 81:3656f00ab3db | 381 | if( debug_main || debug_aging ) debug_msg("Registry ACK from %d in pkg :: %d", ext, cb->get_msg_id() ); |
klauss | 81:3656f00ab3db | 382 | break; |
klauss | 81:3656f00ab3db | 383 | } |
klauss | 81:3656f00ab3db | 384 | default : { |
klauss | 81:3656f00ab3db | 385 | if( debug_main || debug_aging ) debug_msg("ACK from %d in pkg :: %d :: type %d", ext, cb->get_msg_id(), type ); |
klauss | 81:3656f00ab3db | 386 | } |
klauss | 81:3656f00ab3db | 387 | } |
klauss | 82:f55d13babca0 | 388 | if( type != __REGISTRY__ && type != __CB_BYE__ ) type = __DO_NOTHING__; |
klauss | 82:f55d13babca0 | 389 | //if( type != __REGISTRY__ ) type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 390 | } |
klauss | 0:4d17cd9c8f9d | 391 | } |
klauss | 0:4d17cd9c8f9d | 392 | } |
klauss | 0:4d17cd9c8f9d | 393 | } |
klauss | 0:4d17cd9c8f9d | 394 | }else type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 395 | } |
klauss | 78:1353744f01e1 | 396 | |
klauss | 81:3656f00ab3db | 397 | if( main_test == true ){ |
klauss | 82:f55d13babca0 | 398 | static int next_value = 5010; |
klauss | 82:f55d13babca0 | 399 | Call_Box * cb = new Call_Box( next_value, next_value++ ); |
klauss | 82:f55d13babca0 | 400 | v_cb->add( cb ); |
klauss | 82:f55d13babca0 | 401 | cb->cb_set_status( cb_on_call ); |
klauss | 82:f55d13babca0 | 402 | cb->set_timeslice( ts->get_timeslice() ); |
klauss | 82:f55d13babca0 | 403 | |
klauss | 81:3656f00ab3db | 404 | // cb = new Call_Box( 5011, 5011 ); |
klauss | 81:3656f00ab3db | 405 | // v_cb->add( cb ); |
klauss | 82:f55d13babca0 | 406 | |
klauss | 81:3656f00ab3db | 407 | main_test = false; |
klauss | 81:3656f00ab3db | 408 | send_msg("Missed_Pkg :: %d ::", missed_pkg ); |
klauss | 82:f55d13babca0 | 409 | |
klauss | 81:3656f00ab3db | 410 | } |
klauss | 81:3656f00ab3db | 411 | |
klauss | 81:3656f00ab3db | 412 | if( reset_cks == true ){ |
klauss | 81:3656f00ab3db | 413 | pkg_cksok = 0; |
klauss | 81:3656f00ab3db | 414 | pkg_ckserr = 0; |
klauss | 81:3656f00ab3db | 415 | reset_cks = false; |
klauss | 81:3656f00ab3db | 416 | pcks_s = true; |
klauss | 81:3656f00ab3db | 417 | } |
klauss | 81:3656f00ab3db | 418 | |
klauss | 81:3656f00ab3db | 419 | if( pcks_s == true ){ |
klauss | 81:3656f00ab3db | 420 | send_msg("PKG_CSK OK: %d :: PKG_CSK ERR: %d :: PKG_ZERO: %d :: Out_of_range: %d :: Missed_Pkg :: %d", pkg_cksok, pkg_ckserr, pkg_zero, out_of_range, missed_pkg ); |
klauss | 81:3656f00ab3db | 421 | pcks_s = false; |
klauss | 81:3656f00ab3db | 422 | } |
klauss | 81:3656f00ab3db | 423 | |
klauss | 78:1353744f01e1 | 424 | if( led_sync_timer.read() > 1 ){ |
klauss | 78:1353744f01e1 | 425 | led_sync_timer.reset(); |
klauss | 78:1353744f01e1 | 426 | led3 = !led3; |
klauss | 78:1353744f01e1 | 427 | CAB_LED = !CAB_LED; |
klauss | 78:1353744f01e1 | 428 | } |
klauss | 78:1353744f01e1 | 429 | |
klauss | 0:4d17cd9c8f9d | 430 | switch( type ){ |
klauss | 0:4d17cd9c8f9d | 431 | case __DO_NOTHING__ :{} |
klauss | 0:4d17cd9c8f9d | 432 | break; |
klauss | 0:4d17cd9c8f9d | 433 | |
klauss | 0:4d17cd9c8f9d | 434 | case __CB_BYE__ : { |
klauss | 81:3656f00ab3db | 435 | //FIXME como mandar um BYE pro servidor ? |
klauss | 63:0d95da692bb4 | 436 | cb_bye_counter++; |
klauss | 0:4d17cd9c8f9d | 437 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 438 | if( cb != NULL ){ |
klauss | 81:3656f00ab3db | 439 | if( debug_invite || debug_main ) debug_msg("Received bye pkg with msg_id %d e pkg_id %d", cb->get_msg_id(), data[ 0 ] ); |
klauss | 0:4d17cd9c8f9d | 440 | data[ 0 ] |= BIT7; |
klauss | 0:4d17cd9c8f9d | 441 | cb->set_msg_id( data[ 0 ] ); |
klauss | 81:3656f00ab3db | 442 | if( debug_main || debug_invite ) debug_msg( "Request bye from CBx " ); |
klauss | 81:3656f00ab3db | 443 | bool already_removed = true; |
klauss | 0:4d17cd9c8f9d | 444 | for( register uint8_t i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 445 | VZ_call * call = (VZ_call *)v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 446 | if( call->get_cb_ext() == ext ){ |
klauss | 81:3656f00ab3db | 447 | already_removed = false; |
klauss | 4:de46f0d9b14d | 448 | cb->send_bye(); |
klauss | 78:1353744f01e1 | 449 | |
klauss | 0:4d17cd9c8f9d | 450 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 451 | cb->set_timeslice( 0x00 ); |
klauss | 78:1353744f01e1 | 452 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 78:1353744f01e1 | 453 | |
klauss | 0:4d17cd9c8f9d | 454 | set_status( cb->status, cb_idle ); |
klauss | 78:1353744f01e1 | 455 | set_status( cb->sip->status, sip_idle ); |
klauss | 78:1353744f01e1 | 456 | |
klauss | 81:3656f00ab3db | 457 | v_call->remove_element( i ); |
klauss | 81:3656f00ab3db | 458 | |
klauss | 74:81c47fff88a5 | 459 | send2callboxes( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 460 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 82:f55d13babca0 | 461 | |
klauss | 82:f55d13babca0 | 462 | cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); |
klauss | 78:1353744f01e1 | 463 | |
klauss | 3:cd9148672e25 | 464 | delete( call ); |
klauss | 29:7246460b73f8 | 465 | cb->re_start_timer(); |
klauss | 0:4d17cd9c8f9d | 466 | } |
klauss | 0:4d17cd9c8f9d | 467 | } |
klauss | 82:f55d13babca0 | 468 | if( already_removed ) if( debug_main || debug_invite ) debug_msg( "Already removed from vector call" ); |
klauss | 82:f55d13babca0 | 469 | |
klauss | 82:f55d13babca0 | 470 | //ok, mas nem sempre o cbx "entrou em call |
klauss | 82:f55d13babca0 | 471 | for( register uint8_t i = 0; i < v_cb->size(); i++ ) { |
klauss | 82:f55d13babca0 | 472 | Call_Box * cb = (Call_Box *)v_cb->get_element( i ); |
klauss | 82:f55d13babca0 | 473 | if( cb->get_ext() == ext ) { |
klauss | 82:f55d13babca0 | 474 | already_removed = true; |
klauss | 82:f55d13babca0 | 475 | if( cb->get_status() != cb_idle ) { |
klauss | 82:f55d13babca0 | 476 | already_removed = false; |
klauss | 82:f55d13babca0 | 477 | cb->send_bye(); |
klauss | 82:f55d13babca0 | 478 | |
klauss | 82:f55d13babca0 | 479 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 82:f55d13babca0 | 480 | cb->set_timeslice( 0x00 ); |
klauss | 82:f55d13babca0 | 481 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 82:f55d13babca0 | 482 | |
klauss | 82:f55d13babca0 | 483 | set_status( cb->status, cb_idle ); |
klauss | 82:f55d13babca0 | 484 | set_status( cb->sip->status, sip_idle ); |
klauss | 82:f55d13babca0 | 485 | |
klauss | 82:f55d13babca0 | 486 | send2callboxes( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 82:f55d13babca0 | 487 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 82:f55d13babca0 | 488 | |
klauss | 82:f55d13babca0 | 489 | cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); |
klauss | 82:f55d13babca0 | 490 | |
klauss | 82:f55d13babca0 | 491 | cb->re_start_timer(); |
klauss | 82:f55d13babca0 | 492 | } |
klauss | 82:f55d13babca0 | 493 | } |
klauss | 82:f55d13babca0 | 494 | } |
klauss | 82:f55d13babca0 | 495 | |
klauss | 82:f55d13babca0 | 496 | |
klauss | 82:f55d13babca0 | 497 | if( already_removed ) if( debug_main || debug_invite ) debug_msg( "Already removed from inviting queue" ); |
klauss | 81:3656f00ab3db | 498 | |
klauss | 78:1353744f01e1 | 499 | cb->registry(); |
klauss | 81:3656f00ab3db | 500 | }else if( debug_invite || debug_main ) debug_msg("Bye from who ? %d", ext ); |
klauss | 7:019b08223b87 | 501 | }break; |
klauss | 7:019b08223b87 | 502 | |
klauss | 0:4d17cd9c8f9d | 503 | case __INVITE__ : { |
klauss | 63:0d95da692bb4 | 504 | invite_counter++; |
klauss | 78:1353744f01e1 | 505 | if( debug_invite ) debug_msg("Request Invite received from Cbx %i", ext); |
klauss | 0:4d17cd9c8f9d | 506 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 507 | if( cb == NULL ){ |
klauss | 78:1353744f01e1 | 508 | if( debug_main ) debug_msg( "Adding CBx :: %i", ext ); |
klauss | 0:4d17cd9c8f9d | 509 | cb = new Call_Box( ext, port ); |
klauss | 0:4d17cd9c8f9d | 510 | v_cb->add( cb ); |
klauss | 0:4d17cd9c8f9d | 511 | } |
klauss | 78:1353744f01e1 | 512 | cb->registry(); |
klauss | 78:1353744f01e1 | 513 | cb->set_msg_id( data[ 0 ] ); |
klauss | 78:1353744f01e1 | 514 | invite_handler( v_call, v_cb, ts, cb ); |
klauss | 0:4d17cd9c8f9d | 515 | }break; |
klauss | 4:de46f0d9b14d | 516 | case __REGISTRY__ : { |
klauss | 63:0d95da692bb4 | 517 | registry_counter++; |
klauss | 78:1353744f01e1 | 518 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 2:93bec7313ccc | 519 | |
klauss | 78:1353744f01e1 | 520 | if( cb == NULL ){ |
klauss | 78:1353744f01e1 | 521 | if( debug_main ) debug_msg("Adding Cbx :: %d", ext ); |
klauss | 4:de46f0d9b14d | 522 | cb = new Call_Box( ext, port ); |
klauss | 4:de46f0d9b14d | 523 | v_cb->add( cb ); |
klauss | 30:8dfb6d8de53d | 524 | if( debug_main ) debug_msg("Added CBx -- %d", ext ); |
klauss | 0:4d17cd9c8f9d | 525 | } |
klauss | 78:1353744f01e1 | 526 | if( debug_main ) debug_msg("Registered %d - %d", ext, port ); |
klauss | 78:1353744f01e1 | 527 | |
klauss | 4:de46f0d9b14d | 528 | cb->registry(); |
klauss | 78:1353744f01e1 | 529 | |
klauss | 4:de46f0d9b14d | 530 | }break; |
klauss | 4:de46f0d9b14d | 531 | case __BOOT__ : { |
klauss | 63:0d95da692bb4 | 532 | boot_counter++; |
klauss | 74:81c47fff88a5 | 533 | send2callboxes( __build_cb_package__( ext, port, __REGISTRY__, |
klauss | 78:1353744f01e1 | 534 | ( char * )data, data[ 0 ] | BIT7, __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 4:de46f0d9b14d | 535 | }break; |
klauss | 4:de46f0d9b14d | 536 | case __TELEMETRY__ : { |
klauss | 78:1353744f01e1 | 537 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 78:1353744f01e1 | 538 | if( cb == NULL ){ |
klauss | 78:1353744f01e1 | 539 | if( debug_main ) debug_msg("Adding Cbx :: %d", ext ); |
klauss | 78:1353744f01e1 | 540 | cb = new Call_Box( ext, port ); |
klauss | 78:1353744f01e1 | 541 | v_cb->add( cb ); |
klauss | 78:1353744f01e1 | 542 | if( debug_main ) debug_msg("Added CBx -- %d", ext ); |
klauss | 78:1353744f01e1 | 543 | } |
klauss | 78:1353744f01e1 | 544 | cb->registry(); |
klauss | 81:3656f00ab3db | 545 | //FIXME mandar um pacote do tipo 2 pro cbx precis |
klauss | 63:0d95da692bb4 | 546 | telemetry_counter++; |
klauss | 78:1353744f01e1 | 547 | if( debug_telemetry ) send_msg("::Telemetry from %d - %d::", ext, port ); |
klauss | 53:bb492a8f115a | 548 | build_telemetry_report( ext, port, ( char *)data ); |
klauss | 4:de46f0d9b14d | 549 | }break; |
klauss | 36:728498a78e1e | 550 | |
klauss | 67:cdedc64d9921 | 551 | case __BOOTLOADER_CBX__ : { |
klauss | 67:cdedc64d9921 | 552 | bootloader_cbx_counter++; |
klauss | 67:cdedc64d9921 | 553 | int ret = bl_cbx_reply_to_eth( ext, ( char * )data ); |
klauss | 67:cdedc64d9921 | 554 | }break; |
klauss | 67:cdedc64d9921 | 555 | |
klauss | 36:728498a78e1e | 556 | case __PROMPT__ : { |
klauss | 78:1353744f01e1 | 557 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 78:1353744f01e1 | 558 | if( cb == NULL ){ |
klauss | 78:1353744f01e1 | 559 | if( debug_main ) debug_msg("Adding Cbx :: %d", ext ); |
klauss | 78:1353744f01e1 | 560 | cb = new Call_Box( ext, port ); |
klauss | 78:1353744f01e1 | 561 | v_cb->add( cb ); |
klauss | 78:1353744f01e1 | 562 | if( debug_main ) debug_msg("Added CBx -- %d", ext ); |
klauss | 48:195c97f12e8e | 563 | } |
klauss | 78:1353744f01e1 | 564 | |
klauss | 78:1353744f01e1 | 565 | cb->registry(); |
klauss | 48:195c97f12e8e | 566 | |
klauss | 78:1353744f01e1 | 567 | if( xstrmatch( ( uint8_t * )data, ( uint8_t * )"ping" ) ){ |
klauss | 78:1353744f01e1 | 568 | if( debug_ping ) send_msg( "Prompt pkg from ( %i, %i ) :: Ping", ext, port ); |
klauss | 78:1353744f01e1 | 569 | }else{ |
klauss | 78:1353744f01e1 | 570 | prompt_counter++; |
klauss | 78:1353744f01e1 | 571 | //fixme isso nao poderia ser resolvido com um sendmsg ? |
klauss | 78:1353744f01e1 | 572 | send_msg( "Prompt pkg from ( %i, %i ) ::", ext, port ); |
klauss | 78:1353744f01e1 | 573 | for( register uint8_t i = 0; i < 32; i++ ){ |
klauss | 78:1353744f01e1 | 574 | pc.printf("%c", data[ i ] ); |
klauss | 78:1353744f01e1 | 575 | if( i == 15 ) pc.printf( "\r\n" ); |
klauss | 78:1353744f01e1 | 576 | } |
klauss | 78:1353744f01e1 | 577 | pc.printf("\n\r> "); |
klauss | 78:1353744f01e1 | 578 | |
klauss | 78:1353744f01e1 | 579 | if( tcp_session ){ |
klauss | 78:1353744f01e1 | 580 | char aux[ __CB_BUFFER_SIZE__ + 3 ]; |
klauss | 78:1353744f01e1 | 581 | strncpy( aux, (char * )data, __CB_BUFFER_SIZE__ ); |
klauss | 78:1353744f01e1 | 582 | strcat( aux, "\n\r\0" ); |
klauss | 78:1353744f01e1 | 583 | tcp_client.send_all( ( char *)data, strlen( (char * )data ) ); |
klauss | 78:1353744f01e1 | 584 | tcp_client.send_all( "\r\n> ", strlen( "\r\n> " ) ); |
klauss | 78:1353744f01e1 | 585 | } |
klauss | 48:195c97f12e8e | 586 | } |
klauss | 36:728498a78e1e | 587 | }break; |
klauss | 0:4d17cd9c8f9d | 588 | case __AUDIO__ : { |
klauss | 63:0d95da692bb4 | 589 | audio_counter++; |
klauss | 78:1353744f01e1 | 590 | VZ_call * call = __find_Call__( v_call, ext ); |
klauss | 0:4d17cd9c8f9d | 591 | if( call != NULL ){ |
klauss | 30:8dfb6d8de53d | 592 | char * pkg = call->build_eth_package( data + 2 ); |
klauss | 0:4d17cd9c8f9d | 593 | call->send_message( pkg ); |
klauss | 78:1353744f01e1 | 594 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 595 | if( cb != NULL ) cb->reset_elapsed_time(); |
klauss | 18:01a93677e40c | 596 | }else{ |
klauss | 78:1353744f01e1 | 597 | if( debug_main ) debug_msg("received missed package from CBx :: %i", ext ); |
klauss | 0:4d17cd9c8f9d | 598 | } |
klauss | 0:4d17cd9c8f9d | 599 | }break; |
klauss | 9:ffa64f38ef9c | 600 | }// fim switch |
klauss | 81:3656f00ab3db | 601 | |
klauss | 78:1353744f01e1 | 602 | for( register uint8_t i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 603 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 604 | int length = 0; |
klauss | 0:4d17cd9c8f9d | 605 | char * tmp = call->get_eth_message( &length ); |
klauss | 0:4d17cd9c8f9d | 606 | if( tmp != NULL ){ |
klauss | 0:4d17cd9c8f9d | 607 | int cb_port = 0xffff; |
klauss | 78:1353744f01e1 | 608 | Call_Box * cb = __find_CB__( v_cb, call->get_cb_ext() ); |
klauss | 4:de46f0d9b14d | 609 | |
klauss | 4:de46f0d9b14d | 610 | if( cb != NULL ){ |
klauss | 4:de46f0d9b14d | 611 | cb_port = cb->get_port(); |
klauss | 78:1353744f01e1 | 612 | |
klauss | 78:1353744f01e1 | 613 | uint8_t * pkg2cb = __build_cb_package__( call->get_cb_ext(), cb_port, __AUDIO__, |
klauss | 0:4d17cd9c8f9d | 614 | tmp, __AUDIO__, length, write_buffer ); |
klauss | 4:de46f0d9b14d | 615 | |
klauss | 78:1353744f01e1 | 616 | send2callboxes( pkg2cb ); |
klauss | 78:1353744f01e1 | 617 | }else if( debug_main ) debug_msg("received missed package from CBx :: %i -- Type :: %i", ext, type ); |
klauss | 0:4d17cd9c8f9d | 618 | } |
klauss | 14:22a35f575502 | 619 | } |
klauss | 81:3656f00ab3db | 620 | //if( v_call->size() == 0 ) refresh( v_cb, buffer, write_buffer, NULL ); |
klauss | 81:3656f00ab3db | 621 | refresh( v_cb, buffer, write_buffer, NULL ); |
klauss | 78:1353744f01e1 | 622 | |
klauss | 78:1353744f01e1 | 623 | //Fixme pensar melhor nessa parte durante ligacoes, pode complicar com muitos cbx ... |
klauss | 81:3656f00ab3db | 624 | //if( v_call->size() == 0 ) registry_aging( v_cb, buffer, write_buffer ); |
klauss | 4:de46f0d9b14d | 625 | |
klauss | 78:1353744f01e1 | 626 | // check sip messages only for cbx in call ? |
klauss | 78:1353744f01e1 | 627 | int ext_to__be_removed = sip_manager( v_cb ); |
klauss | 78:1353744f01e1 | 628 | if( ext_to__be_removed > 0x00 ){ |
klauss | 78:1353744f01e1 | 629 | Call_Box * cb = __find_CB__( v_cb, ext_to__be_removed ); |
klauss | 4:de46f0d9b14d | 630 | if( cb != NULL ){ |
klauss | 4:de46f0d9b14d | 631 | if( cb->status == cb_on_call ){ |
klauss | 4:de46f0d9b14d | 632 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 4:de46f0d9b14d | 633 | cb->set_timeslice( 0x00 ); |
klauss | 4:de46f0d9b14d | 634 | buffer[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 78:1353744f01e1 | 635 | |
klauss | 78:1353744f01e1 | 636 | set_status( cb->status, cb_idle ); |
klauss | 78:1353744f01e1 | 637 | set_status( cb->sip->status, sip_idle ); |
klauss | 78:1353744f01e1 | 638 | |
klauss | 4:de46f0d9b14d | 639 | for( register uint8_t i = 0; i < v_call->size(); i++ ){ |
klauss | 4:de46f0d9b14d | 640 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 78:1353744f01e1 | 641 | if( call->get_cb_ext() == ext_to__be_removed ){ |
klauss | 4:de46f0d9b14d | 642 | v_call->remove_element( i ); |
klauss | 4:de46f0d9b14d | 643 | delete( call ); |
klauss | 4:de46f0d9b14d | 644 | } |
klauss | 2:93bec7313ccc | 645 | } |
klauss | 78:1353744f01e1 | 646 | |
klauss | 82:f55d13babca0 | 647 | cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); |
klauss | 82:f55d13babca0 | 648 | //cb->set_msg_id( 80 ); |
klauss | 81:3656f00ab3db | 649 | |
klauss | 82:f55d13babca0 | 650 | //debug_msg("msg_id -- %d", cb->get_msg_id() ); |
klauss | 81:3656f00ab3db | 651 | |
klauss | 74:81c47fff88a5 | 652 | send2callboxes( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 4:de46f0d9b14d | 653 | ( char * )buffer, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 78:1353744f01e1 | 654 | |
klauss | 81:3656f00ab3db | 655 | //cb->re_start_timer(); |
klauss | 0:4d17cd9c8f9d | 656 | } |
klauss | 78:1353744f01e1 | 657 | }else if( debug_main ) debug_msg("Missed bye request from * CBx :: %d", ext ); |
klauss | 0:4d17cd9c8f9d | 658 | } |
klauss | 13:ae278302dffe | 659 | |
klauss | 81:3656f00ab3db | 660 | /* Verifica andamento de ligações para eventualmente encerra-las por timeout */ |
klauss | 10:22da1a0ac1e1 | 661 | call_manager( v_call, v_cb, buffer, write_buffer, ts ); |
klauss | 0:4d17cd9c8f9d | 662 | |
klauss | 81:3656f00ab3db | 663 | /* tratamento de pedidos de ligação */ |
klauss | 81:3656f00ab3db | 664 | invite_handler( v_call, v_cb, ts, NULL ); |
klauss | 78:1353744f01e1 | 665 | |
klauss | 81:3656f00ab3db | 666 | /* rotina de verificação de TS's perdidos */ |
klauss | 78:1353744f01e1 | 667 | if( ( v_call->size() == 0 ) && ( ts->remain_timeslices() != __MAX_TIMESLICES__ ) ){ |
klauss | 78:1353744f01e1 | 668 | bool ts_reset = true; |
klauss | 78:1353744f01e1 | 669 | for( register uint8_t i = 0; i < v_cb->size(); i++ ){ |
klauss | 82:f55d13babca0 | 670 | if ( ( ( Call_Box * )v_cb->get_element( i ) )->get_status() != cb_idle && |
klauss | 82:f55d13babca0 | 671 | ( ( Call_Box * )v_cb->get_element( i ) )->get_sip_status() != sip_idle ) |
klauss | 82:f55d13babca0 | 672 | { |
klauss | 78:1353744f01e1 | 673 | ts_reset = false; |
klauss | 78:1353744f01e1 | 674 | break; |
klauss | 78:1353744f01e1 | 675 | } |
klauss | 78:1353744f01e1 | 676 | } |
klauss | 78:1353744f01e1 | 677 | if( ts_reset ){ |
klauss | 81:3656f00ab3db | 678 | if( debug_invite ) debug_msg("Resetando TS"); |
klauss | 78:1353744f01e1 | 679 | ts->reset(); |
klauss | 78:1353744f01e1 | 680 | } |
klauss | 0:4d17cd9c8f9d | 681 | } |
klauss | 78:1353744f01e1 | 682 | |
klauss | 81:3656f00ab3db | 683 | //invite_pgk_retry_manager( v_call, v_cb, buffer, write_buffer ); |
klauss | 81:3656f00ab3db | 684 | |
klauss | 81:3656f00ab3db | 685 | /* escuta se existe algum procedimento de gravação de cbx */ |
klauss | 67:cdedc64d9921 | 686 | bl_handler(); |
klauss | 78:1353744f01e1 | 687 | |
klauss | 81:3656f00ab3db | 688 | /* rotina que esvazia possiveis pacotes que não foram transmitidos para evitar conflito */ |
klauss | 72:895ca792c647 | 689 | tx_buffer_ring_buffer_handler(); |
klauss | 33:735fd60e96d8 | 690 | |
klauss | 30:8dfb6d8de53d | 691 | type = __DO_NOTHING__; |
klauss | 81:3656f00ab3db | 692 | |
klauss | 81:3656f00ab3db | 693 | if( eth_status == 0 ) eth_wdt = ETH_CONNECT_TIMEOUT; |
klauss | 81:3656f00ab3db | 694 | |
klauss | 81:3656f00ab3db | 695 | if( wdt_timer.read() > 1 ){ |
klauss | 82:f55d13babca0 | 696 | char wake_msg[ 8 ]; |
klauss | 82:f55d13babca0 | 697 | |
klauss | 82:f55d13babca0 | 698 | int read = udp_wdt_client.receiveFrom( udp_wdt_server, wake_msg, sizeof( wake_msg ) ); |
klauss | 82:f55d13babca0 | 699 | |
klauss | 82:f55d13babca0 | 700 | if( read > 0 ){ |
klauss | 82:f55d13babca0 | 701 | if( !( strncmp( wake_msg, "alive", 5 ) ) ){ |
klauss | 82:f55d13babca0 | 702 | external_wdt = EXTERN_WDT_IDLE; |
klauss | 82:f55d13babca0 | 703 | strcpy( wake_msg, "wakeup" ); |
klauss | 82:f55d13babca0 | 704 | while( !( udp_wdt_client.sendTo( udp_wdt_server, wake_msg, strlen( wake_msg ) ) ) ); |
klauss | 82:f55d13babca0 | 705 | } |
klauss | 82:f55d13babca0 | 706 | } |
klauss | 82:f55d13babca0 | 707 | if( debug_wdt ) if( read < 0 ) debug_msg( "some's wrong ... some's wrong" ); |
klauss | 82:f55d13babca0 | 708 | |
klauss | 81:3656f00ab3db | 709 | wdt_timer.reset(); |
klauss | 81:3656f00ab3db | 710 | |
klauss | 81:3656f00ab3db | 711 | if( wdt_show ) debug_wdt = true; |
klauss | 81:3656f00ab3db | 712 | |
klauss | 81:3656f00ab3db | 713 | if( external_wdt ) external_wdt--; |
klauss | 81:3656f00ab3db | 714 | if( pkg_wdt ) pkg_wdt--; |
klauss | 81:3656f00ab3db | 715 | if( eth_wdt ) eth_wdt--; |
klauss | 81:3656f00ab3db | 716 | |
klauss | 81:3656f00ab3db | 717 | if( eth_wdt && external_wdt && pkg_wdt ){ |
klauss | 81:3656f00ab3db | 718 | wdt.kick(); |
klauss | 81:3656f00ab3db | 719 | } |
klauss | 81:3656f00ab3db | 720 | } |
klauss | 81:3656f00ab3db | 721 | |
klauss | 81:3656f00ab3db | 722 | if( debug_wdt ){ |
klauss | 81:3656f00ab3db | 723 | debug_wdt = false; |
klauss | 81:3656f00ab3db | 724 | if( eth_status == 0 ){ |
klauss | 82:f55d13babca0 | 725 | send_msg( "Wdt last reset: %s - status_eth :: Connected - Extern Wdt idle for :: %3d sec ( %3d ) - Rx from CBx idle for :: %3d sec ( %3d )", |
klauss | 81:3656f00ab3db | 726 | ( wdt.WatchdogCausedReset() ) ? "true" : "false", |
klauss | 81:3656f00ab3db | 727 | EXTERN_WDT_IDLE - external_wdt, |
klauss | 82:f55d13babca0 | 728 | EXTERN_WDT_IDLE, |
klauss | 82:f55d13babca0 | 729 | RX_CB_IDLE - pkg_wdt, |
klauss | 82:f55d13babca0 | 730 | RX_CB_IDLE |
klauss | 81:3656f00ab3db | 731 | ); |
klauss | 81:3656f00ab3db | 732 | }else{ |
klauss | 82:f55d13babca0 | 733 | send_msg( "Wdt last reset: %s - status_eth :: Disconnected :: since %3d sec - Extern Wdt idle for :: %3d sec ( %3d ) - Rx from CBx idle for :: %3d sec ( %3d )", |
klauss | 81:3656f00ab3db | 734 | ( wdt.WatchdogCausedReset() ) ? "true" : "false", |
klauss | 81:3656f00ab3db | 735 | ETH_CONNECT_TIMEOUT - eth_wdt, |
klauss | 82:f55d13babca0 | 736 | EXTERN_WDT_IDLE - external_wdt, |
klauss | 82:f55d13babca0 | 737 | EXTERN_WDT_IDLE, |
klauss | 82:f55d13babca0 | 738 | RX_CB_IDLE - pkg_wdt, |
klauss | 82:f55d13babca0 | 739 | RX_CB_IDLE |
klauss | 81:3656f00ab3db | 740 | ); |
klauss | 81:3656f00ab3db | 741 | } |
klauss | 81:3656f00ab3db | 742 | } |
klauss | 0:4d17cd9c8f9d | 743 | } |
klauss | 0:4d17cd9c8f9d | 744 | } |