![](/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@98:43b45f26b430, 2015-01-11 (annotated)
- Committer:
- klauss
- Date:
- Sun Jan 11 03:30:47 2015 +0000
- Revision:
- 98:43b45f26b430
- Parent:
- 97:8985817e8847
- Child:
- 99:e80850c51106
telemetry cbx bug fix ( melhor tratamento do recebimento de pacotes enviados pelos cbx )
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 91:c2a86b1f8aaa | 1 | #include "telemetry.h" |
klauss | 91:c2a86b1f8aaa | 2 | #include "eth.h" |
klauss | 91:c2a86b1f8aaa | 3 | #include "bits.h" |
klauss | 74:81c47fff88a5 | 4 | #include "UART3Interrupt.h" // for RXBuffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 91:c2a86b1f8aaa | 5 | #include "utils.h" |
klauss | 91:c2a86b1f8aaa | 6 | #include "parallelcpld.h" |
klauss | 91:c2a86b1f8aaa | 7 | #include "debug.h" |
klauss | 91:c2a86b1f8aaa | 8 | #include "flood.h" |
klauss | 91:c2a86b1f8aaa | 9 | #include "bootloader_cbx.h" |
klauss | 91:c2a86b1f8aaa | 10 | #include "call_manager.h" |
klauss | 91:c2a86b1f8aaa | 11 | #include "call_box_manager.h" |
klauss | 91:c2a86b1f8aaa | 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 | 87:679ee0d594a9 | 16 | #include "sdram.h" |
klauss | 0:4d17cd9c8f9d | 17 | |
klauss | 87:679ee0d594a9 | 18 | volatile u16_t lpc_low_level_input_counter = 0; |
klauss | 85:b6f2dc1d0f4f | 19 | |
klauss | 91:c2a86b1f8aaa | 20 | int main() |
klauss | 91:c2a86b1f8aaa | 21 | { |
klauss | 81:3656f00ab3db | 22 | init_wdt(); |
klauss | 91:c2a86b1f8aaa | 23 | |
klauss | 81:3656f00ab3db | 24 | config_lpc(); |
klauss | 91:c2a86b1f8aaa | 25 | |
klauss | 0:4d17cd9c8f9d | 26 | start_cpld(); |
klauss | 91:c2a86b1f8aaa | 27 | |
klauss | 91:c2a86b1f8aaa | 28 | if( sdram_init() == 1 ) { |
klauss | 87:679ee0d594a9 | 29 | send_msg("******* Failed to initialize SDRAM *******"); |
klauss | 87:679ee0d594a9 | 30 | return 1; |
klauss | 89:0fe315117b00 | 31 | } else { |
klauss | 87:679ee0d594a9 | 32 | send_msg("******* Success to initialize SDRAM *******"); |
klauss | 87:679ee0d594a9 | 33 | } |
klauss | 91:c2a86b1f8aaa | 34 | |
klauss | 43:455522f98de5 | 35 | init_fsystem(); |
klauss | 91:c2a86b1f8aaa | 36 | |
klauss | 89:0fe315117b00 | 37 | { |
klauss | 89:0fe315117b00 | 38 | char s[ 32 ]; |
klauss | 89:0fe315117b00 | 39 | mbed_mac_address( s ); |
klauss | 89:0fe315117b00 | 40 | send_msg( "::Mac::%02x:%02x:%02x:%02x:%02x:%02x:: ", s[0],s[1],s[2],s[3],s[4],s[5] ); |
klauss | 89:0fe315117b00 | 41 | } |
klauss | 91:c2a86b1f8aaa | 42 | |
klauss | 78:1353744f01e1 | 43 | short int eth_status = __init_eth__(); |
klauss | 91:c2a86b1f8aaa | 44 | if( eth_status ) { |
klauss | 87:679ee0d594a9 | 45 | send_msg("******* Cannot connect to eth *******"); |
klauss | 91:c2a86b1f8aaa | 46 | } else { |
klauss | 81:3656f00ab3db | 47 | send_msg("******* Connection eth - ok *******"); |
klauss | 78:1353744f01e1 | 48 | init_prompt_eth(); |
klauss | 82:f55d13babca0 | 49 | init_external_wdt() |
klauss | 8:e3bfe62a477e | 50 | } |
klauss | 91:c2a86b1f8aaa | 51 | |
klauss | 78:1353744f01e1 | 52 | Timer sync_timer, led_sync_timer; |
klauss | 78:1353744f01e1 | 53 | sync_timer.start(), led_sync_timer.start(); |
klauss | 91:c2a86b1f8aaa | 54 | |
klauss | 0:4d17cd9c8f9d | 55 | /* representa ramal do call box */ |
klauss | 0:4d17cd9c8f9d | 56 | int ext = 0; |
klauss | 91:c2a86b1f8aaa | 57 | |
klauss | 0:4d17cd9c8f9d | 58 | /* representa porta do call box */ |
klauss | 78:1353744f01e1 | 59 | int port = 0; |
klauss | 91:c2a86b1f8aaa | 60 | |
klauss | 0:4d17cd9c8f9d | 61 | /* buffer para onde se copia os dados vindos do cb para tratameno interno */ |
klauss | 91:c2a86b1f8aaa | 62 | uint8_t buffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 91:c2a86b1f8aaa | 63 | |
klauss | 0:4d17cd9c8f9d | 64 | /* buffer de escrita do pacote de saida que sera enviado pro cb / servidor */ |
klauss | 0:4d17cd9c8f9d | 65 | uint8_t write_buffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 91:c2a86b1f8aaa | 66 | |
klauss | 0:4d17cd9c8f9d | 67 | /* ponteiro que aponta para os dados vindo do CPLD */ |
klauss | 0:4d17cd9c8f9d | 68 | uint8_t * buffer_from_cb_ptr = ( uint8_t * )RXBuffer; |
klauss | 91:c2a86b1f8aaa | 69 | |
klauss | 62:07e5bdc9f8f7 | 70 | /* Armazena o ultimo pacote recebido dos CBx */ |
klauss | 62:07e5bdc9f8f7 | 71 | uint8_t cb_rx_buffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 91:c2a86b1f8aaa | 72 | |
klauss | 0:4d17cd9c8f9d | 73 | /* referencia para os dados contidos no pacote, sem o header */ |
klauss | 0:4d17cd9c8f9d | 74 | uint8_t * data = NULL; |
klauss | 91:c2a86b1f8aaa | 75 | |
klauss | 0:4d17cd9c8f9d | 76 | /* gerencia o tipo do pacote para providenciar tratamento adequado */ |
klauss | 0:4d17cd9c8f9d | 77 | volatile uint8_t type = __DO_NOTHING__; |
klauss | 91:c2a86b1f8aaa | 78 | |
klauss | 87:679ee0d594a9 | 79 | /* representa a lista dos Call Boxes atualmente recfonhecidos pela cabeceira */ |
klauss | 0:4d17cd9c8f9d | 80 | Vector * v_cb = new Vector(); |
klauss | 91:c2a86b1f8aaa | 81 | |
klauss | 0:4d17cd9c8f9d | 82 | /* representa a lista de ligacoes ativas na cabeceira */ |
klauss | 0:4d17cd9c8f9d | 83 | Vector * v_call = new Vector(); |
klauss | 91:c2a86b1f8aaa | 84 | |
klauss | 0:4d17cd9c8f9d | 85 | /* gerencia a distribuicao de timeslice para os call boxes */ |
klauss | 0:4d17cd9c8f9d | 86 | Timeslice * ts = new Timeslice(); |
klauss | 91:c2a86b1f8aaa | 87 | if( ts == NULL ) { |
klauss | 89:0fe315117b00 | 88 | memory_is_over = true; |
klauss | 89:0fe315117b00 | 89 | if( debug_memory ) debug_msg("TS allocation fail"); |
klauss | 89:0fe315117b00 | 90 | } |
klauss | 91:c2a86b1f8aaa | 91 | |
klauss | 81:3656f00ab3db | 92 | /* instancia o vetor de call boxes // calls */ |
klauss | 91:c2a86b1f8aaa | 93 | if( v_cb == NULL ) { |
klauss | 91:c2a86b1f8aaa | 94 | while( v_cb == NULL ) { |
klauss | 0:4d17cd9c8f9d | 95 | Vector * v_cb = new Vector(); |
klauss | 91:c2a86b1f8aaa | 96 | if( sync_timer.read() > 5 ) { |
klauss | 48:195c97f12e8e | 97 | send_msg("Erro ao alocar o vetor de CBx"); |
klauss | 78:1353744f01e1 | 98 | sync_timer.reset(); |
klauss | 0:4d17cd9c8f9d | 99 | } |
klauss | 0:4d17cd9c8f9d | 100 | } |
klauss | 91:c2a86b1f8aaa | 101 | } else if( v_call == NULL ) { |
klauss | 91:c2a86b1f8aaa | 102 | while( v_call == NULL ) { |
klauss | 0:4d17cd9c8f9d | 103 | Vector * v_call = new Vector(); |
klauss | 91:c2a86b1f8aaa | 104 | if( sync_timer.read() > 5 ) { |
klauss | 48:195c97f12e8e | 105 | send_msg("Erro ao alocar o vetor de Calls"); |
klauss | 78:1353744f01e1 | 106 | sync_timer.reset(); |
klauss | 0:4d17cd9c8f9d | 107 | } |
klauss | 0:4d17cd9c8f9d | 108 | } |
klauss | 0:4d17cd9c8f9d | 109 | } |
klauss | 91:c2a86b1f8aaa | 110 | |
klauss | 91:c2a86b1f8aaa | 111 | if( v_cb == NULL ) { |
klauss | 89:0fe315117b00 | 112 | memory_is_over = true; |
klauss | 89:0fe315117b00 | 113 | if( debug_memory ) debug_msg("Call_Box vector allocation fail"); |
klauss | 89:0fe315117b00 | 114 | } |
klauss | 91:c2a86b1f8aaa | 115 | |
klauss | 91:c2a86b1f8aaa | 116 | if( v_call == NULL ) { |
klauss | 89:0fe315117b00 | 117 | memory_is_over = true; |
klauss | 89:0fe315117b00 | 118 | if( debug_memory ) debug_msg("Call vector allocation fail"); |
klauss | 89:0fe315117b00 | 119 | } |
klauss | 91:c2a86b1f8aaa | 120 | |
klauss | 91:c2a86b1f8aaa | 121 | for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) { |
klauss | 63:0d95da692bb4 | 122 | cb_rx_buffer[ i ] = 0; |
klauss | 63:0d95da692bb4 | 123 | cb_tx_buffer[ i ] = 0; |
klauss | 63:0d95da692bb4 | 124 | } |
klauss | 91:c2a86b1f8aaa | 125 | |
klauss | 0:4d17cd9c8f9d | 126 | reset_leds(); |
klauss | 91:c2a86b1f8aaa | 127 | |
klauss | 33:735fd60e96d8 | 128 | udp_timer.start(); |
klauss | 91:c2a86b1f8aaa | 129 | |
klauss | 68:b54993674190 | 130 | init_telemetry_handler(); |
klauss | 91:c2a86b1f8aaa | 131 | |
klauss | 81:3656f00ab3db | 132 | init_ranges(); |
klauss | 91:c2a86b1f8aaa | 133 | |
klauss | 80:61d61c9eb75c | 134 | init_refresh(); |
klauss | 91:c2a86b1f8aaa | 135 | |
klauss | 78:1353744f01e1 | 136 | init_aging(); |
klauss | 91:c2a86b1f8aaa | 137 | |
klauss | 89:0fe315117b00 | 138 | init_sync_refresh(); |
klauss | 91:c2a86b1f8aaa | 139 | |
klauss | 81:3656f00ab3db | 140 | //init_invite_pgk_retry_manager(); |
klauss | 91:c2a86b1f8aaa | 141 | |
klauss | 81:3656f00ab3db | 142 | //void init_bye_pgk_retry_manager(); |
klauss | 91:c2a86b1f8aaa | 143 | |
klauss | 62:07e5bdc9f8f7 | 144 | static uint8_t count = 0; |
klauss | 91:c2a86b1f8aaa | 145 | |
klauss | 89:0fe315117b00 | 146 | //short int bl_ret = init_bl_handler(); |
klauss | 89:0fe315117b00 | 147 | //if( dbl ) send_msg("Valor de retorno do bl_habdler = %d", bl_ret ); |
klauss | 91:c2a86b1f8aaa | 148 | |
klauss | 78:1353744f01e1 | 149 | uint8_t max_registered_cbx = 0; |
klauss | 91:c2a86b1f8aaa | 150 | |
klauss | 81:3656f00ab3db | 151 | bool wake_all = false; |
klauss | 91:c2a86b1f8aaa | 152 | |
klauss | 81:3656f00ab3db | 153 | uint16_t pkg_wdt = RX_CB_IDLE; |
klauss | 81:3656f00ab3db | 154 | uint16_t eth_wdt = ETH_CONNECT_TIMEOUT; |
klauss | 83:b8a1d8fdeaeb | 155 | uint32_t uptime = 0; |
klauss | 91:c2a86b1f8aaa | 156 | |
klauss | 68:b54993674190 | 157 | send_msg("Ready"); |
klauss | 91:c2a86b1f8aaa | 158 | |
klauss | 92:92df17f538a8 | 159 | //bool registry_test = false; |
klauss | 92:92df17f538a8 | 160 | //static int next_value = 5002; |
klauss | 92:92df17f538a8 | 161 | //static int nex_test_registry = next_value; |
klauss | 92:92df17f538a8 | 162 | |
klauss | 92:92df17f538a8 | 163 | bool invite_retry_time = false; |
klauss | 91:c2a86b1f8aaa | 164 | |
klauss | 78:1353744f01e1 | 165 | /*------------------------------------------ main loop ---------------------------------------------------------------*/ |
klauss | 91:c2a86b1f8aaa | 166 | while( true ) { |
klauss | 78:1353744f01e1 | 167 | if( v_cb->size() > max_registered_cbx ) max_registered_cbx = v_cb->size(); |
klauss | 91:c2a86b1f8aaa | 168 | |
klauss | 91:c2a86b1f8aaa | 169 | if( sync_timer.read() > 5 ) { |
klauss | 78:1353744f01e1 | 170 | sync_timer.reset(); |
klauss | 91:c2a86b1f8aaa | 171 | |
klauss | 91:c2a86b1f8aaa | 172 | if( debug_cks == true ) { |
klauss | 81:3656f00ab3db | 173 | pcks_s = true; |
klauss | 78:1353744f01e1 | 174 | } |
klauss | 91:c2a86b1f8aaa | 175 | |
klauss | 91:c2a86b1f8aaa | 176 | if( debug_alive == true ) { |
klauss | 81:3656f00ab3db | 177 | pshowcb = true; |
klauss | 78:1353744f01e1 | 178 | } |
klauss | 91:c2a86b1f8aaa | 179 | |
klauss | 91:c2a86b1f8aaa | 180 | if( !( ++count % 15 ) ) { |
klauss | 91:c2a86b1f8aaa | 181 | if( eth_status ) { |
klauss | 78:1353744f01e1 | 182 | eth_status = __init_eth__(); |
klauss | 91:c2a86b1f8aaa | 183 | if( eth_status ) { |
klauss | 78:1353744f01e1 | 184 | if( debug_main ) debug_msg("Cannot connect to eth"); |
klauss | 91:c2a86b1f8aaa | 185 | } else { |
klauss | 78:1353744f01e1 | 186 | if( debug_main ) debug_msg("Connection eth - ok"); |
klauss | 78:1353744f01e1 | 187 | init_prompt_eth(); |
klauss | 82:f55d13babca0 | 188 | init_external_wdt() |
klauss | 78:1353744f01e1 | 189 | } |
klauss | 78:1353744f01e1 | 190 | } |
klauss | 78:1353744f01e1 | 191 | } |
klauss | 81:3656f00ab3db | 192 | //35 sec. |
klauss | 91:c2a86b1f8aaa | 193 | if( ( count > 7 ) && ( wake_all == false ) ) { |
klauss | 81:3656f00ab3db | 194 | wake_all = true; |
klauss | 78:1353744f01e1 | 195 | if( debug_wake == true ) send_msg( "Time to wake" ); |
klauss | 78:1353744f01e1 | 196 | } |
klauss | 78:1353744f01e1 | 197 | } |
klauss | 91:c2a86b1f8aaa | 198 | |
klauss | 68:b54993674190 | 199 | //FIXMEj colocar uma condicao aqui caso necessario pra nao comprometer ligacoes ... |
klauss | 35:96885a7931f0 | 200 | prompt_process( NULL ); |
klauss | 91:c2a86b1f8aaa | 201 | |
klauss | 91:c2a86b1f8aaa | 202 | if( r_stats ) { |
klauss | 91:c2a86b1f8aaa | 203 | boot_counter = 0; |
klauss | 91:c2a86b1f8aaa | 204 | registry_counter = 0; |
klauss | 91:c2a86b1f8aaa | 205 | invite_counter = 0; |
klauss | 91:c2a86b1f8aaa | 206 | audio_counter = 0; |
klauss | 91:c2a86b1f8aaa | 207 | telemetry_counter = 0; |
klauss | 91:c2a86b1f8aaa | 208 | cb_bye_counter = 0; |
klauss | 91:c2a86b1f8aaa | 209 | prompt_counter = 0; |
klauss | 91:c2a86b1f8aaa | 210 | flood_counter = 0; |
klauss | 67:cdedc64d9921 | 211 | bootloader_cbx_counter = 0; |
klauss | 63:0d95da692bb4 | 212 | r_stats = false; |
klauss | 63:0d95da692bb4 | 213 | stats = true; |
klauss | 63:0d95da692bb4 | 214 | } |
klauss | 91:c2a86b1f8aaa | 215 | |
klauss | 91:c2a86b1f8aaa | 216 | if( stats ) { |
klauss | 63:0d95da692bb4 | 217 | char str[ 200 ]; |
klauss | 98:43b45f26b430 | 218 | snprintf( str, 200, "\n\rReceived Pkgs::\n\r Boot :: %u\n\r Registry :: %u\n\r Invite :: %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 | 91:c2a86b1f8aaa | 219 | boot_counter, registry_counter, invite_counter, audio_counter, telemetry_counter, cb_bye_counter, prompt_counter, flood_counter, bootloader_cbx_counter ); |
klauss | 63:0d95da692bb4 | 220 | send_msg( str ); |
klauss | 63:0d95da692bb4 | 221 | stats =false; |
klauss | 63:0d95da692bb4 | 222 | } |
klauss | 91:c2a86b1f8aaa | 223 | |
klauss | 91:c2a86b1f8aaa | 224 | if( list ) { |
klauss | 78:1353744f01e1 | 225 | uint8_t missed_cb = ( ( max_ext - min_ext ) + 1 ) - v_cb->size(); |
klauss | 91:c2a86b1f8aaa | 226 | |
klauss | 78:1353744f01e1 | 227 | if( ( max_ext % 2 ) == 0 ) missed_cb++; |
klauss | 91:c2a86b1f8aaa | 228 | |
klauss | 78:1353744f01e1 | 229 | if( min_ext % 2 ) missed_cb++; |
klauss | 91:c2a86b1f8aaa | 230 | |
klauss | 78:1353744f01e1 | 231 | if( min_ext == 0 && max_ext == 0 ) missed_cb = 0; |
klauss | 91:c2a86b1f8aaa | 232 | |
klauss | 78:1353744f01e1 | 233 | 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 | 91:c2a86b1f8aaa | 234 | if( v_cb->size() == 1 ) { |
klauss | 78:1353744f01e1 | 235 | send_msg(" %d ", ( ( Call_Box * )v_cb->get_element( 0 ) )->get_ext() ); |
klauss | 91:c2a86b1f8aaa | 236 | } else if( v_cb->size() > 1 ) { |
klauss | 62:07e5bdc9f8f7 | 237 | char str[ 1024 ]; |
klauss | 78:1353744f01e1 | 238 | int ext_list[ __MAX_CB_IN_A_BRANCH__ ]; |
klauss | 62:07e5bdc9f8f7 | 239 | register int i = 0; |
klauss | 91:c2a86b1f8aaa | 240 | for( ; i < v_cb->size(); i++ ) { |
klauss | 78:1353744f01e1 | 241 | ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext(); |
klauss | 62:07e5bdc9f8f7 | 242 | } |
klauss | 91:c2a86b1f8aaa | 243 | |
klauss | 62:07e5bdc9f8f7 | 244 | qsort( ext_list, v_cb->size(), sizeof( int ), ls_comp ); |
klauss | 91:c2a86b1f8aaa | 245 | |
klauss | 62:07e5bdc9f8f7 | 246 | char aux[ 16 ]; |
klauss | 78:1353744f01e1 | 247 | strcpy( str, "\r\n> " ); |
klauss | 91:c2a86b1f8aaa | 248 | for( i = 0; i < v_cb->size() - 1; i++ ) { |
klauss | 62:07e5bdc9f8f7 | 249 | sprintf( aux, "%i, ", ext_list[ i ] ); |
klauss | 62:07e5bdc9f8f7 | 250 | strcat( str, aux ); |
klauss | 78:1353744f01e1 | 251 | if( ( i != 0 ) && !( ( i + 1 ) % 16 ) ) strcat( str, "\r\n> " ); |
klauss | 62:07e5bdc9f8f7 | 252 | } |
klauss | 62:07e5bdc9f8f7 | 253 | sprintf( aux, "%i ", ext_list[ i ] ); |
klauss | 62:07e5bdc9f8f7 | 254 | strcat( str, aux ); |
klauss | 62:07e5bdc9f8f7 | 255 | send_msg( "%s", str ); |
klauss | 62:07e5bdc9f8f7 | 256 | } |
klauss | 91:c2a86b1f8aaa | 257 | list = false; |
klauss | 62:07e5bdc9f8f7 | 258 | } |
klauss | 91:c2a86b1f8aaa | 259 | |
klauss | 91:c2a86b1f8aaa | 260 | if( long_list ) { |
klauss | 81:3656f00ab3db | 261 | //FIXME implementar um algoritmo que me mostre o timeout de cada cbx do lado da linha, desse. |
klauss | 85:b6f2dc1d0f4f | 262 | /* |
klauss | 81:3656f00ab3db | 263 | uint8_t missed_cb = ( ( max_ext - min_ext ) + 1 ) - v_cb->size(); |
klauss | 91:c2a86b1f8aaa | 264 | |
klauss | 81:3656f00ab3db | 265 | if( ( max_ext % 2 ) == 0 ) missed_cb++; |
klauss | 91:c2a86b1f8aaa | 266 | |
klauss | 81:3656f00ab3db | 267 | if( min_ext % 2 ) missed_cb++; |
klauss | 91:c2a86b1f8aaa | 268 | |
klauss | 81:3656f00ab3db | 269 | if( min_ext == 0 && max_ext == 0 ) missed_cb = 0; |
klauss | 91:c2a86b1f8aaa | 270 | |
klauss | 81:3656f00ab3db | 271 | 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 | 272 | if( v_cb->size() == 1 ){ |
klauss | 81:3656f00ab3db | 273 | send_msg(" %d ", ( ( Call_Box * )v_cb->get_element( 0 ) )->get_ext() ); |
klauss | 81:3656f00ab3db | 274 | }else if( v_cb->size() > 1 ){ |
klauss | 81:3656f00ab3db | 275 | char str[ 1024 ]; |
klauss | 81:3656f00ab3db | 276 | int ext_list[ __MAX_CB_IN_A_BRANCH__ ]; |
klauss | 81:3656f00ab3db | 277 | register int i = 0; |
klauss | 81:3656f00ab3db | 278 | for( ; i < ( v_cb->size() * 2 ); i += 2 ){ |
klauss | 81:3656f00ab3db | 279 | ext_list[ i ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_ext(); |
klauss | 81:3656f00ab3db | 280 | ext_list[ i + 1 ] = ( ( Call_Box * )v_cb->get_element( i ) )->get_elapsed_time(); |
klauss | 81:3656f00ab3db | 281 | } |
klauss | 91:c2a86b1f8aaa | 282 | |
klauss | 81:3656f00ab3db | 283 | qsort( ext_list, v_cb->size(), sizeof( int ) * 2, ls_comp ); |
klauss | 91:c2a86b1f8aaa | 284 | |
klauss | 81:3656f00ab3db | 285 | char aux[ 16 ]; |
klauss | 81:3656f00ab3db | 286 | strcpy( str, "\r\n> " ); |
klauss | 81:3656f00ab3db | 287 | for( i = 0; i < ( v_cb->size() * 2 ) - 2; i++ ){ |
klauss | 81:3656f00ab3db | 288 | sprintf( aux, "%i - %i,", ext_list[ i ], ext_list[ i + 1 ] ); |
klauss | 81:3656f00ab3db | 289 | strcat( str, aux ); |
klauss | 81:3656f00ab3db | 290 | strcat( str, "\r\n> " ); |
klauss | 81:3656f00ab3db | 291 | } |
klauss | 81:3656f00ab3db | 292 | sprintf( aux, "%i - %i,", ext_list[ i ], ext_list[ i + 1 ] ); |
klauss | 81:3656f00ab3db | 293 | strcat( str, aux ); |
klauss | 81:3656f00ab3db | 294 | send_msg( "%s", str ); |
klauss | 81:3656f00ab3db | 295 | } |
klauss | 85:b6f2dc1d0f4f | 296 | */ |
klauss | 91:c2a86b1f8aaa | 297 | long_list = false; |
klauss | 52:12930cef17c4 | 298 | } |
klauss | 91:c2a86b1f8aaa | 299 | |
klauss | 91:c2a86b1f8aaa | 300 | if( pshowcb == true ) { |
klauss | 78:1353744f01e1 | 301 | 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() ); |
klauss | 91:c2a86b1f8aaa | 302 | for( register uint8_t i = 0; i < v_cb->size(); i++ ) { |
klauss | 30:8dfb6d8de53d | 303 | Call_Box * cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 78:1353744f01e1 | 304 | char cb_status[ 32 ]; |
klauss | 78:1353744f01e1 | 305 | char cb_sip_status[ 32 ]; |
klauss | 91:c2a86b1f8aaa | 306 | switch( cb->status ) { |
klauss | 91:c2a86b1f8aaa | 307 | case cb_idle : { |
klauss | 91:c2a86b1f8aaa | 308 | strcpy( cb_status, "cb_idle" ); |
klauss | 91:c2a86b1f8aaa | 309 | break; |
klauss | 91:c2a86b1f8aaa | 310 | } |
klauss | 91:c2a86b1f8aaa | 311 | case cb_ringing : { |
klauss | 91:c2a86b1f8aaa | 312 | strcpy( cb_status, "cb_ringing" ); |
klauss | 91:c2a86b1f8aaa | 313 | break; |
klauss | 91:c2a86b1f8aaa | 314 | } |
klauss | 91:c2a86b1f8aaa | 315 | case cb_trying : { |
klauss | 91:c2a86b1f8aaa | 316 | strcpy( cb_status,"cb_trying" ); |
klauss | 91:c2a86b1f8aaa | 317 | break; |
klauss | 91:c2a86b1f8aaa | 318 | } |
klauss | 91:c2a86b1f8aaa | 319 | case cb_on_call : { |
klauss | 91:c2a86b1f8aaa | 320 | strcpy( cb_status, "cb_on_call" ); |
klauss | 91:c2a86b1f8aaa | 321 | break; |
klauss | 91:c2a86b1f8aaa | 322 | } |
klauss | 91:c2a86b1f8aaa | 323 | case cb_busy : { |
klauss | 91:c2a86b1f8aaa | 324 | strcpy( cb_status, "cb_busy" ); |
klauss | 91:c2a86b1f8aaa | 325 | break; |
klauss | 91:c2a86b1f8aaa | 326 | } |
klauss | 91:c2a86b1f8aaa | 327 | case cb_denied : { |
klauss | 91:c2a86b1f8aaa | 328 | strcpy( cb_status, "cb_denied" ); |
klauss | 91:c2a86b1f8aaa | 329 | break; |
klauss | 91:c2a86b1f8aaa | 330 | } |
klauss | 78:1353744f01e1 | 331 | } |
klauss | 91:c2a86b1f8aaa | 332 | switch( cb->sip->status ) { |
klauss | 91:c2a86b1f8aaa | 333 | case sip_idle : { |
klauss | 91:c2a86b1f8aaa | 334 | strcpy( cb_sip_status, "sip_idle" ); |
klauss | 91:c2a86b1f8aaa | 335 | break; |
klauss | 91:c2a86b1f8aaa | 336 | } |
klauss | 91:c2a86b1f8aaa | 337 | case sip_waiting_trying : { |
klauss | 91:c2a86b1f8aaa | 338 | strcpy( cb_sip_status, "sip_waiting_trying" ); |
klauss | 91:c2a86b1f8aaa | 339 | break; |
klauss | 91:c2a86b1f8aaa | 340 | } |
klauss | 91:c2a86b1f8aaa | 341 | case sip_trying : { |
klauss | 91:c2a86b1f8aaa | 342 | strcpy( cb_sip_status, "sip_trying" ); |
klauss | 91:c2a86b1f8aaa | 343 | break; |
klauss | 91:c2a86b1f8aaa | 344 | } |
klauss | 91:c2a86b1f8aaa | 345 | case sip_ringing : { |
klauss | 91:c2a86b1f8aaa | 346 | strcpy( cb_sip_status, "sip_ringing" ); |
klauss | 91:c2a86b1f8aaa | 347 | break; |
klauss | 91:c2a86b1f8aaa | 348 | } |
klauss | 91:c2a86b1f8aaa | 349 | case sip_busy : { |
klauss | 91:c2a86b1f8aaa | 350 | strcpy( cb_sip_status, "sip_busy" ); |
klauss | 91:c2a86b1f8aaa | 351 | break; |
klauss | 91:c2a86b1f8aaa | 352 | } |
klauss | 91:c2a86b1f8aaa | 353 | case sip_ok : { |
klauss | 91:c2a86b1f8aaa | 354 | strcpy( cb_sip_status, "sip_ok" ); |
klauss | 91:c2a86b1f8aaa | 355 | break; |
klauss | 91:c2a86b1f8aaa | 356 | } |
klauss | 91:c2a86b1f8aaa | 357 | case sip_on_call : { |
klauss | 91:c2a86b1f8aaa | 358 | strcpy( cb_sip_status, "sip_on_call" ); |
klauss | 91:c2a86b1f8aaa | 359 | break; |
klauss | 91:c2a86b1f8aaa | 360 | } |
klauss | 91:c2a86b1f8aaa | 361 | case sip_denied : { |
klauss | 91:c2a86b1f8aaa | 362 | strcpy( cb_sip_status, "sip_denied" ); |
klauss | 91:c2a86b1f8aaa | 363 | break; |
klauss | 91:c2a86b1f8aaa | 364 | } |
klauss | 78:1353744f01e1 | 365 | } |
klauss | 78:1353744f01e1 | 366 | char cbx_to_string[ 254 ]; |
klauss | 78:1353744f01e1 | 367 | char aux[ 16 ]; |
klauss | 78:1353744f01e1 | 368 | strcpy( cbx_to_string, "Ext :: " ); |
klauss | 78:1353744f01e1 | 369 | itoa( cb->get_ext(), aux , 10 ); |
klauss | 98:43b45f26b430 | 370 | strcat( cbx_to_string, aux ); |
klauss | 97:8985817e8847 | 371 | strcat( cbx_to_string, " :: Port :: " ); |
klauss | 97:8985817e8847 | 372 | itoa( cb->get_port(), aux , 10 ); |
klauss | 78:1353744f01e1 | 373 | strcat( cbx_to_string, aux ); |
klauss | 78:1353744f01e1 | 374 | strcat( cbx_to_string, " :: Status -- " ); |
klauss | 78:1353744f01e1 | 375 | strcat( cbx_to_string, cb_status ); |
klauss | 78:1353744f01e1 | 376 | strcat( cbx_to_string, " - " ); |
klauss | 78:1353744f01e1 | 377 | strcat( cbx_to_string, cb_sip_status ); |
klauss | 91:c2a86b1f8aaa | 378 | if( cb->get_timeslice() != 0 ) { |
klauss | 78:1353744f01e1 | 379 | strcat( cbx_to_string, " -- on TimeSlice :: " ); |
klauss | 78:1353744f01e1 | 380 | itoa( cb->get_timeslice(), aux , 10 ); |
klauss | 78:1353744f01e1 | 381 | strcat( cbx_to_string, aux ); |
klauss | 78:1353744f01e1 | 382 | } |
klauss | 78:1353744f01e1 | 383 | send_msg( cbx_to_string ); |
klauss | 48:195c97f12e8e | 384 | } |
klauss | 78:1353744f01e1 | 385 | pshowcb = false; |
klauss | 48:195c97f12e8e | 386 | } |
klauss | 97:8985817e8847 | 387 | |
klauss | 97:8985817e8847 | 388 | if( show_sip == true ){ |
klauss | 97:8985817e8847 | 389 | show_sip = false; |
klauss | 97:8985817e8847 | 390 | debug_msg(":: Sip :: "); |
klauss | 97:8985817e8847 | 391 | for( register uint8_t i = 0; i < v_cb->size(); i++ ) { |
klauss | 97:8985817e8847 | 392 | Call_Box * cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 97:8985817e8847 | 393 | debug_msg("sip ext :: %d -- sip port :: %d", cb->get_sip_ext(), cb->get_sip_port() ); |
klauss | 97:8985817e8847 | 394 | } |
klauss | 97:8985817e8847 | 395 | } |
klauss | 91:c2a86b1f8aaa | 396 | |
klauss | 81:3656f00ab3db | 397 | if( pflood == true ) flood(); |
klauss | 91:c2a86b1f8aaa | 398 | |
klauss | 91:c2a86b1f8aaa | 399 | if( debug_eth ) { |
klauss | 81:3656f00ab3db | 400 | debug_eth = false; |
klauss | 81:3656f00ab3db | 401 | send_msg("Eth status %s", ( eth_status == 0 ) ? "Connected" : "Disconnected" ); |
klauss | 81:3656f00ab3db | 402 | } |
klauss | 91:c2a86b1f8aaa | 403 | |
klauss | 91:c2a86b1f8aaa | 404 | if( status != __WAITING__ ) { |
klauss | 81:3656f00ab3db | 405 | pkg_wdt = RX_CB_IDLE; |
klauss | 86:bf7b0d4c3232 | 406 | xmemcpy( cb_rx_buffer, buffer_from_cb_ptr, __CB_BUFFER_SIZE__ ); |
klauss | 0:4d17cd9c8f9d | 407 | status = __WAITING__; |
klauss | 81:3656f00ab3db | 408 | missed_pkg--; |
klauss | 91:c2a86b1f8aaa | 409 | xmemcpy( buffer, cb_rx_buffer, __CB_BUFFER_SIZE__ ); |
klauss | 98:43b45f26b430 | 410 | |
klauss | 98:43b45f26b430 | 411 | /* |
klauss | 98:43b45f26b430 | 412 | { |
klauss | 98:43b45f26b430 | 413 | uint16_t e, p; |
klauss | 98:43b45f26b430 | 414 | e = buffer[0]; |
klauss | 98:43b45f26b430 | 415 | e <<= 8; |
klauss | 98:43b45f26b430 | 416 | e |= buffer[1]; |
klauss | 98:43b45f26b430 | 417 | p = buffer[2]; |
klauss | 98:43b45f26b430 | 418 | p <<= 8; |
klauss | 98:43b45f26b430 | 419 | p |= buffer[3]; |
klauss | 98:43b45f26b430 | 420 | //if (e != (p+100)) { |
klauss | 98:43b45f26b430 | 421 | if( ( buffer[0] == 0x14 ) && ( buffer[1] == 0x0a ) ) { |
klauss | 98:43b45f26b430 | 422 | int i; |
klauss | 98:43b45f26b430 | 423 | char s[400], ss[4];; |
klauss | 98:43b45f26b430 | 424 | strcpy(s,"OPS-PKG:"); |
klauss | 98:43b45f26b430 | 425 | for (i=0; i<40; i++) { |
klauss | 98:43b45f26b430 | 426 | sprintf(ss," %02x",buffer[i]); |
klauss | 98:43b45f26b430 | 427 | strcat(s,ss); |
klauss | 98:43b45f26b430 | 428 | } |
klauss | 98:43b45f26b430 | 429 | debug_msg( s ); |
klauss | 98:43b45f26b430 | 430 | } |
klauss | 98:43b45f26b430 | 431 | } |
klauss | 98:43b45f26b430 | 432 | */ |
klauss | 98:43b45f26b430 | 433 | |
klauss | 91:c2a86b1f8aaa | 434 | if( debug_cpld ) { |
klauss | 81:3656f00ab3db | 435 | rx = true; |
klauss | 81:3656f00ab3db | 436 | tx = true; |
klauss | 81:3656f00ab3db | 437 | } |
klauss | 91:c2a86b1f8aaa | 438 | |
klauss | 91:c2a86b1f8aaa | 439 | if( rx ) { |
klauss | 81:3656f00ab3db | 440 | char str[ 1024 ]; |
klauss | 81:3656f00ab3db | 441 | strcpy( str, "RX :: \n\r " ); |
klauss | 91:c2a86b1f8aaa | 442 | for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ) { |
klauss | 81:3656f00ab3db | 443 | char tmp[ 16 ]; |
klauss | 81:3656f00ab3db | 444 | strcat( str, itoa( cb_rx_buffer[ i ], tmp, 16 ) ); |
klauss | 81:3656f00ab3db | 445 | if( ( i != 0 ) && !( ( i + 1 ) % 50 ) ) strcat( str, "\n\r " ); |
klauss | 91:c2a86b1f8aaa | 446 | |
klauss | 81:3656f00ab3db | 447 | else strcat( str, " " ); |
klauss | 81:3656f00ab3db | 448 | } |
klauss | 81:3656f00ab3db | 449 | send_msg( "%s", str ); |
klauss | 91:c2a86b1f8aaa | 450 | rx = false; |
klauss | 81:3656f00ab3db | 451 | } |
klauss | 91:c2a86b1f8aaa | 452 | |
klauss | 34:f19d9735428e | 453 | data = __parse_vz_pkg__( &ext, &port, &type, buffer ); |
klauss | 91:c2a86b1f8aaa | 454 | |
klauss | 91:c2a86b1f8aaa | 455 | if( data != NULL ) { |
klauss | 78:1353744f01e1 | 456 | if( min_ext == 0 ) min_ext = ext; |
klauss | 91:c2a86b1f8aaa | 457 | |
klauss | 78:1353744f01e1 | 458 | if( ext > max_ext ) max_ext = ext; |
klauss | 91:c2a86b1f8aaa | 459 | |
klauss | 78:1353744f01e1 | 460 | if( ext < min_ext ) min_ext = ext; |
klauss | 91:c2a86b1f8aaa | 461 | |
klauss | 78:1353744f01e1 | 462 | if( debug_cb_rx == true ) debug_msg("Pkg from CBx :: %d -- Type :: %d", ext, type ); |
klauss | 91:c2a86b1f8aaa | 463 | |
klauss | 91:c2a86b1f8aaa | 464 | if( type != __AUDIO__ ) { |
klauss | 0:4d17cd9c8f9d | 465 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 91:c2a86b1f8aaa | 466 | if( cb != NULL ) { |
klauss | 92:92df17f538a8 | 467 | if( data[ 0 ] & BIT7 ) { |
klauss | 91:c2a86b1f8aaa | 468 | if( type == __BOOT__ ) { |
klauss | 91:c2a86b1f8aaa | 469 | send2callboxes( __build_cb_package__( ext, port, __REGISTRY__, |
klauss | 92:92df17f538a8 | 470 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 92:92df17f538a8 | 471 | } else { |
klauss | 81:3656f00ab3db | 472 | if( debug_main ) debug_msg("Received ack pkg with seq_num %d", data[ 0 ] ); |
klauss | 91:c2a86b1f8aaa | 473 | |
klauss | 91:c2a86b1f8aaa | 474 | switch( type ) { |
klauss | 81:3656f00ab3db | 475 | case __INVITE__ : { |
klauss | 81:3656f00ab3db | 476 | if( debug_main || debug_invite ) debug_msg("Invite Ack from %d on msg_id %d", ext, cb->get_msg_id() ); |
klauss | 92:92df17f538a8 | 477 | //cb->first_invite_response_ok(); |
klauss | 91:c2a86b1f8aaa | 478 | break; |
klauss | 81:3656f00ab3db | 479 | } |
klauss | 81:3656f00ab3db | 480 | case __CB_BYE__ : { |
klauss | 81:3656f00ab3db | 481 | if( debug_main || debug_invite ) debug_msg("BYE Ack from %d on msg_id %d", ext, cb->get_msg_id() ); |
klauss | 81:3656f00ab3db | 482 | cb->set_bye_response_ok(); |
klauss | 91:c2a86b1f8aaa | 483 | break; |
klauss | 81:3656f00ab3db | 484 | } |
klauss | 81:3656f00ab3db | 485 | case __REGISTRY__ : { |
klauss | 81:3656f00ab3db | 486 | if( debug_main || debug_aging ) debug_msg("Registry ACK from %d in pkg :: %d", ext, cb->get_msg_id() ); |
klauss | 81:3656f00ab3db | 487 | break; |
klauss | 81:3656f00ab3db | 488 | } |
klauss | 81:3656f00ab3db | 489 | default : { |
klauss | 91:c2a86b1f8aaa | 490 | if( debug_main || debug_aging ) debug_msg("ACK from %d in pkg :: %d :: type %d", ext, cb->get_msg_id(), type ); |
klauss | 91:c2a86b1f8aaa | 491 | } |
klauss | 81:3656f00ab3db | 492 | } |
klauss | 92:92df17f538a8 | 493 | if( type != __REGISTRY__ && type != __CB_BYE__ ) type = __DO_NOTHING__; |
klauss | 92:92df17f538a8 | 494 | if( type == __CB_BYE__ ){ |
klauss | 92:92df17f538a8 | 495 | VZ_call * call = __find_Call__( v_call, ext ); |
klauss | 92:92df17f538a8 | 496 | if( call != NULL ){ |
klauss | 92:92df17f538a8 | 497 | if( call->get_elapsed_time() < 120000 ){ |
klauss | 92:92df17f538a8 | 498 | if( debug_invite ) debug_msg("%d ack bye ignored", ext ); |
klauss | 92:92df17f538a8 | 499 | type = __DO_NOTHING__; |
klauss | 92:92df17f538a8 | 500 | } |
klauss | 92:92df17f538a8 | 501 | } |
klauss | 92:92df17f538a8 | 502 | } |
klauss | 0:4d17cd9c8f9d | 503 | } |
klauss | 0:4d17cd9c8f9d | 504 | } |
klauss | 0:4d17cd9c8f9d | 505 | } |
klauss | 0:4d17cd9c8f9d | 506 | } |
klauss | 91:c2a86b1f8aaa | 507 | } else type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 508 | } |
klauss | 91:c2a86b1f8aaa | 509 | |
klauss | 91:c2a86b1f8aaa | 510 | if( main_test == true ) { |
klauss | 87:679ee0d594a9 | 511 | main_test = false; |
klauss | 85:b6f2dc1d0f4f | 512 | send_msg("CB_New (%u) -- CB_Delete (%u)", cb_new_counter, cb_delete_counter ); |
klauss | 85:b6f2dc1d0f4f | 513 | send_msg("SIP_New (%u) -- SIP_Delete (%u)", sip_new_counter, sip_delete_counter ); |
klauss | 87:679ee0d594a9 | 514 | send_msg("RTP_header_New (%u) -- RTP_header_Delete (%u)", rtp_header_new_counter, rtp_header_delete_counter ); |
klauss | 87:679ee0d594a9 | 515 | send_msg("RTP_body_New (%u) -- RTP_body_Delete (%u)", rtp_body_new_counter, rtp_body_delete_counter ); |
klauss | 91:c2a86b1f8aaa | 516 | send_msg("Call_New (%u) -- Call_Delete (%u)", call_new_counter, call_delete_counter ); |
klauss | 87:679ee0d594a9 | 517 | send_msg("lpc_low_level_input_counter :: %d", lpc_low_level_input_counter ); |
klauss | 87:679ee0d594a9 | 518 | send_msg("Memory is %s", ( memory_is_over ) ? "Over" : "Ok" ); |
klauss | 81:3656f00ab3db | 519 | send_msg("Missed_Pkg :: %d ::", missed_pkg ); |
klauss | 87:679ee0d594a9 | 520 | send_msg("Sizeof Sip :: %u", sizeof( Sip ) ); |
klauss | 87:679ee0d594a9 | 521 | send_msg("Sizeof Call_Box :: %u", sizeof( Call_Box ) ); |
klauss | 87:679ee0d594a9 | 522 | send_msg("Sizeof VZ_call :: %u", sizeof( VZ_call ) ); |
klauss | 87:679ee0d594a9 | 523 | send_msg("Sizeof RTP :: %u", sizeof( RTP ) ); |
klauss | 87:679ee0d594a9 | 524 | send_msg("Sizeof RTP_Header :: %u", sizeof( RTP_Header ) ); |
klauss | 87:679ee0d594a9 | 525 | send_msg("Sizeof RTP_Body :: %u", sizeof( RTP_Body ) ); |
klauss | 87:679ee0d594a9 | 526 | send_msg("Sizeof Vector :: %u", sizeof( Vector ) ); |
klauss | 87:679ee0d594a9 | 527 | send_msg("Sizeof Timeslice :: %u", sizeof( Timeslice ) ); |
klauss | 87:679ee0d594a9 | 528 | send_msg("Sizeof Watchdog :: %u", sizeof( Watchdog ) ); |
klauss | 81:3656f00ab3db | 529 | } |
klauss | 91:c2a86b1f8aaa | 530 | |
klauss | 92:92df17f538a8 | 531 | //static int wdt_count = 0; |
klauss | 92:92df17f538a8 | 532 | /* |
klauss | 91:c2a86b1f8aaa | 533 | if( registry_test == true ) { |
klauss | 91:c2a86b1f8aaa | 534 | registry_test = false; |
klauss | 91:c2a86b1f8aaa | 535 | |
klauss | 91:c2a86b1f8aaa | 536 | if( wdt_count++ > 5 || v_cb->size() < 66 ) { |
klauss | 89:0fe315117b00 | 537 | wdt_count = 0; |
klauss | 91:c2a86b1f8aaa | 538 | |
klauss | 91:c2a86b1f8aaa | 539 | if( type == __DO_NOTHING__ ) { |
klauss | 91:c2a86b1f8aaa | 540 | if( next_value < ( 5002 + __MAX_CB_IN_A_BRANCH__ - 2 ) ) { |
klauss | 91:c2a86b1f8aaa | 541 | //type = __REGISTRY__; |
klauss | 89:0fe315117b00 | 542 | data = buffer; |
klauss | 89:0fe315117b00 | 543 | ext = next_value; |
klauss | 89:0fe315117b00 | 544 | port = next_value++; |
klauss | 91:c2a86b1f8aaa | 545 | } else { |
klauss | 91:c2a86b1f8aaa | 546 | if( nex_test_registry > ( 5002 + __MAX_CB_IN_A_BRANCH__ - 1 - 2 ) ) nex_test_registry = 5002; |
klauss | 91:c2a86b1f8aaa | 547 | //type = __REGISTRY__; |
klauss | 89:0fe315117b00 | 548 | data = buffer; |
klauss | 89:0fe315117b00 | 549 | ext = nex_test_registry; |
klauss | 89:0fe315117b00 | 550 | port = nex_test_registry++; |
klauss | 89:0fe315117b00 | 551 | } |
klauss | 86:bf7b0d4c3232 | 552 | } |
klauss | 86:bf7b0d4c3232 | 553 | } |
klauss | 86:bf7b0d4c3232 | 554 | } |
klauss | 92:92df17f538a8 | 555 | */ |
klauss | 92:92df17f538a8 | 556 | |
klauss | 91:c2a86b1f8aaa | 557 | { |
klauss | 91:c2a86b1f8aaa | 558 | fd_set fdSet; |
klauss | 91:c2a86b1f8aaa | 559 | FD_ZERO(&fdSet); |
klauss | 91:c2a86b1f8aaa | 560 | |
klauss | 91:c2a86b1f8aaa | 561 | for( register uint8_t i = 0; i < v_cb->size(); i++ ) { |
klauss | 91:c2a86b1f8aaa | 562 | Call_Box * cb = (Call_Box *)v_cb->get_element( i ); |
klauss | 91:c2a86b1f8aaa | 563 | FD_SET( cb->get_sip_socket_fd(), &fdSet); |
klauss | 91:c2a86b1f8aaa | 564 | } |
klauss | 91:c2a86b1f8aaa | 565 | |
klauss | 91:c2a86b1f8aaa | 566 | FD_SET( udp_wdt_client.get_fd(), &fdSet); |
klauss | 91:c2a86b1f8aaa | 567 | |
klauss | 91:c2a86b1f8aaa | 568 | struct timeval t; |
klauss | 91:c2a86b1f8aaa | 569 | t.tv_sec = 0; |
klauss | 91:c2a86b1f8aaa | 570 | t.tv_usec = 0; |
klauss | 91:c2a86b1f8aaa | 571 | int ret = lwip_select( FD_SETSIZE, &fdSet, NULL, NULL, &t ); |
klauss | 91:c2a86b1f8aaa | 572 | |
klauss | 91:c2a86b1f8aaa | 573 | if(ret > 0 ) { |
klauss | 91:c2a86b1f8aaa | 574 | for( register uint8_t i = 0; i < v_cb->size(); i++ ) { |
klauss | 91:c2a86b1f8aaa | 575 | Call_Box * cb = (Call_Box *)v_cb->get_element( i ); |
klauss | 91:c2a86b1f8aaa | 576 | int fd = cb->get_sip_socket_fd(); |
klauss | 91:c2a86b1f8aaa | 577 | if( FD_ISSET( fd, &fdSet ) ) { |
klauss | 91:c2a86b1f8aaa | 578 | int rcv = cb->sip_udp_incomming_pkg(); |
klauss | 91:c2a86b1f8aaa | 579 | } |
klauss | 91:c2a86b1f8aaa | 580 | } |
klauss | 91:c2a86b1f8aaa | 581 | if( FD_ISSET( udp_wdt_client.get_fd(), &fdSet ) ) { |
klauss | 91:c2a86b1f8aaa | 582 | char wake_msg[ 768 ]; |
klauss | 91:c2a86b1f8aaa | 583 | Endpoint udp_wdt_server; |
klauss | 91:c2a86b1f8aaa | 584 | |
klauss | 91:c2a86b1f8aaa | 585 | if( udp_wdt_client.receiveFrom( udp_wdt_server, wake_msg, sizeof( wake_msg ) ) > 0 ){ |
klauss | 91:c2a86b1f8aaa | 586 | if( !( strncmp( wake_msg, "alive", 5 ) ) ) { |
klauss | 91:c2a86b1f8aaa | 587 | // Just ckeck but not set 'alive?' |
klauss | 91:c2a86b1f8aaa | 588 | // 'alive*' - force wdt tick right now |
klauss | 91:c2a86b1f8aaa | 589 | // Ckecking and set 'alive' |
klauss | 91:c2a86b1f8aaa | 590 | bool question_alive = ( wake_msg[ 5 ] == '?' ); |
klauss | 91:c2a86b1f8aaa | 591 | if( wake_msg[ 5 ] == '*' ) wdt.kick(); |
klauss | 89:0fe315117b00 | 592 | |
klauss | 98:43b45f26b430 | 593 | snprintf( wake_msg, 48,"wdt:%u,%u,%u,%c,%u,%u,%u,%u,%u:", uptime, invite_counter, external_wdt, ( wdt.WatchdogCausedReset() ) ? '1' : '0', cb_new_counter, v_cb->size(), ts->remain_timeslices(), sip_socket_send_failure, v_call->size() ); |
klauss | 92:92df17f538a8 | 594 | wake_msg[ 768 - 1 ] = 0; |
klauss | 91:c2a86b1f8aaa | 595 | udp_wdt_client.sendTo( udp_wdt_server, wake_msg, strlen( wake_msg ) ); |
klauss | 91:c2a86b1f8aaa | 596 | |
klauss | 91:c2a86b1f8aaa | 597 | if( ( (!question_alive) && ( cb_new_counter <= __MAX_CB_IN_A_BRANCH__ ) && ( cb_new_counter >= 2 ) ) ) { |
klauss | 91:c2a86b1f8aaa | 598 | external_wdt = EXTERN_WDT_IDLE; |
klauss | 91:c2a86b1f8aaa | 599 | } |
klauss | 91:c2a86b1f8aaa | 600 | } else if( !( strncmp( wake_msg, "reset", 5 ) ) ) { |
klauss | 91:c2a86b1f8aaa | 601 | external_wdt = 0; |
klauss | 91:c2a86b1f8aaa | 602 | |
klauss | 91:c2a86b1f8aaa | 603 | sprintf( wake_msg, "rst:%u:", uptime ); |
klauss | 91:c2a86b1f8aaa | 604 | udp_wdt_client.sendTo( udp_wdt_server, wake_msg, strlen( wake_msg ) ); |
klauss | 91:c2a86b1f8aaa | 605 | } |
klauss | 91:c2a86b1f8aaa | 606 | } |
klauss | 91:c2a86b1f8aaa | 607 | } |
klauss | 91:c2a86b1f8aaa | 608 | } |
klauss | 91:c2a86b1f8aaa | 609 | } |
klauss | 91:c2a86b1f8aaa | 610 | |
klauss | 91:c2a86b1f8aaa | 611 | if( reset_cks == true ) { |
klauss | 81:3656f00ab3db | 612 | pkg_cksok = 0; |
klauss | 81:3656f00ab3db | 613 | pkg_ckserr = 0; |
klauss | 81:3656f00ab3db | 614 | reset_cks = false; |
klauss | 81:3656f00ab3db | 615 | pcks_s = true; |
klauss | 81:3656f00ab3db | 616 | } |
klauss | 91:c2a86b1f8aaa | 617 | |
klauss | 91:c2a86b1f8aaa | 618 | if( pcks_s == true ) { |
klauss | 86:bf7b0d4c3232 | 619 | send_msg("PKG_CSK OK: %d :: PKG_CSK ERR: %d :: PKG_ZERO: %d :: Out_of_range: %d :: Missed_Pkg :: %d :: CPLD_RCV :: %d", pkg_cksok, pkg_ckserr, pkg_zero, out_of_range, missed_pkg, received_cpld_pkg ); |
klauss | 81:3656f00ab3db | 620 | pcks_s = false; |
klauss | 81:3656f00ab3db | 621 | } |
klauss | 91:c2a86b1f8aaa | 622 | |
klauss | 91:c2a86b1f8aaa | 623 | if( led_sync_timer.read() > 1 ) { |
klauss | 78:1353744f01e1 | 624 | led_sync_timer.reset(); |
klauss | 78:1353744f01e1 | 625 | led3 = !led3; |
klauss | 78:1353744f01e1 | 626 | CAB_LED = !CAB_LED; |
klauss | 78:1353744f01e1 | 627 | } |
klauss | 91:c2a86b1f8aaa | 628 | |
klauss | 91:c2a86b1f8aaa | 629 | switch( type ) { |
klauss | 91:c2a86b1f8aaa | 630 | case __DO_NOTHING__ : |
klauss | 91:c2a86b1f8aaa | 631 | {} |
klauss | 91:c2a86b1f8aaa | 632 | break; |
klauss | 91:c2a86b1f8aaa | 633 | |
klauss | 0:4d17cd9c8f9d | 634 | case __CB_BYE__ : { |
klauss | 63:0d95da692bb4 | 635 | cb_bye_counter++; |
klauss | 0:4d17cd9c8f9d | 636 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 91:c2a86b1f8aaa | 637 | if( cb != NULL ) { |
klauss | 81:3656f00ab3db | 638 | 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 | 639 | data[ 0 ] |= BIT7; |
klauss | 0:4d17cd9c8f9d | 640 | cb->set_msg_id( data[ 0 ] ); |
klauss | 81:3656f00ab3db | 641 | if( debug_main || debug_invite ) debug_msg( "Request bye from CBx " ); |
klauss | 92:92df17f538a8 | 642 | |
klauss | 96:b4660d559ac3 | 643 | //debug_msg("----------"); |
klauss | 92:92df17f538a8 | 644 | for( register uint8_t i = 0; i < v_call->size(); i++ ) { |
klauss | 92:92df17f538a8 | 645 | VZ_call * call = (VZ_call *)v_call->get_element( i ); |
klauss | 96:b4660d559ac3 | 646 | //debug_msg("--%d--", call->get_cb_ext() ); |
klauss | 92:92df17f538a8 | 647 | } |
klauss | 96:b4660d559ac3 | 648 | //debug_msg("----------"); |
klauss | 92:92df17f538a8 | 649 | |
klauss | 81:3656f00ab3db | 650 | bool already_removed = true; |
klauss | 91:c2a86b1f8aaa | 651 | for( register uint8_t i = 0; i < v_call->size(); i++ ) { |
klauss | 0:4d17cd9c8f9d | 652 | VZ_call * call = (VZ_call *)v_call->get_element( i ); |
klauss | 91:c2a86b1f8aaa | 653 | if( call->get_cb_ext() == ext ) { |
klauss | 81:3656f00ab3db | 654 | already_removed = false; |
klauss | 4:de46f0d9b14d | 655 | cb->send_bye(); |
klauss | 91:c2a86b1f8aaa | 656 | |
klauss | 0:4d17cd9c8f9d | 657 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 658 | cb->set_timeslice( 0x00 ); |
klauss | 78:1353744f01e1 | 659 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 91:c2a86b1f8aaa | 660 | |
klauss | 0:4d17cd9c8f9d | 661 | set_status( cb->status, cb_idle ); |
klauss | 78:1353744f01e1 | 662 | set_status( cb->sip->status, sip_idle ); |
klauss | 91:c2a86b1f8aaa | 663 | |
klauss | 81:3656f00ab3db | 664 | v_call->remove_element( i ); |
klauss | 92:92df17f538a8 | 665 | |
klauss | 92:92df17f538a8 | 666 | //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); |
klauss | 92:92df17f538a8 | 667 | //cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); |
klauss | 91:c2a86b1f8aaa | 668 | |
klauss | 74:81c47fff88a5 | 669 | send2callboxes( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 670 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 92:92df17f538a8 | 671 | |
klauss | 92:92df17f538a8 | 672 | // envia o ack bye depois atualiza o msg_id |
klauss | 92:92df17f538a8 | 673 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); |
klauss | 91:c2a86b1f8aaa | 674 | |
klauss | 3:cd9148672e25 | 675 | delete( call ); |
klauss | 29:7246460b73f8 | 676 | cb->re_start_timer(); |
klauss | 0:4d17cd9c8f9d | 677 | } |
klauss | 0:4d17cd9c8f9d | 678 | } |
klauss | 82:f55d13babca0 | 679 | if( already_removed ) if( debug_main || debug_invite ) debug_msg( "Already removed from vector call" ); |
klauss | 91:c2a86b1f8aaa | 680 | |
klauss | 82:f55d13babca0 | 681 | //ok, mas nem sempre o cbx "entrou em call |
klauss | 82:f55d13babca0 | 682 | for( register uint8_t i = 0; i < v_cb->size(); i++ ) { |
klauss | 82:f55d13babca0 | 683 | Call_Box * cb = (Call_Box *)v_cb->get_element( i ); |
klauss | 82:f55d13babca0 | 684 | if( cb->get_ext() == ext ) { |
klauss | 82:f55d13babca0 | 685 | already_removed = true; |
klauss | 82:f55d13babca0 | 686 | if( cb->get_status() != cb_idle ) { |
klauss | 82:f55d13babca0 | 687 | already_removed = false; |
klauss | 82:f55d13babca0 | 688 | cb->send_bye(); |
klauss | 82:f55d13babca0 | 689 | |
klauss | 82:f55d13babca0 | 690 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 82:f55d13babca0 | 691 | cb->set_timeslice( 0x00 ); |
klauss | 82:f55d13babca0 | 692 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 82:f55d13babca0 | 693 | |
klauss | 82:f55d13babca0 | 694 | set_status( cb->status, cb_idle ); |
klauss | 82:f55d13babca0 | 695 | set_status( cb->sip->status, sip_idle ); |
klauss | 82:f55d13babca0 | 696 | |
klauss | 82:f55d13babca0 | 697 | send2callboxes( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 92:92df17f538a8 | 698 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 82:f55d13babca0 | 699 | |
klauss | 92:92df17f538a8 | 700 | //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); |
klauss | 92:92df17f538a8 | 701 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); |
klauss | 82:f55d13babca0 | 702 | |
klauss | 82:f55d13babca0 | 703 | cb->re_start_timer(); |
klauss | 82:f55d13babca0 | 704 | } |
klauss | 82:f55d13babca0 | 705 | } |
klauss | 82:f55d13babca0 | 706 | } |
klauss | 91:c2a86b1f8aaa | 707 | |
klauss | 82:f55d13babca0 | 708 | if( already_removed ) if( debug_main || debug_invite ) debug_msg( "Already removed from inviting queue" ); |
klauss | 91:c2a86b1f8aaa | 709 | |
klauss | 78:1353744f01e1 | 710 | cb->registry(); |
klauss | 91:c2a86b1f8aaa | 711 | } else if( debug_invite || debug_main ) debug_msg("Bye from who ? %d", ext ); |
klauss | 91:c2a86b1f8aaa | 712 | } |
klauss | 91:c2a86b1f8aaa | 713 | break; |
klauss | 91:c2a86b1f8aaa | 714 | |
klauss | 0:4d17cd9c8f9d | 715 | case __INVITE__ : { |
klauss | 63:0d95da692bb4 | 716 | invite_counter++; |
klauss | 78:1353744f01e1 | 717 | if( debug_invite ) debug_msg("Request Invite received from Cbx %i", ext); |
klauss | 0:4d17cd9c8f9d | 718 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 91:c2a86b1f8aaa | 719 | if( cb == NULL ) { |
klauss | 91:c2a86b1f8aaa | 720 | if( v_cb->size() < __MAX_CB_IN_A_BRANCH__ ) { |
klauss | 91:c2a86b1f8aaa | 721 | if( debug_main ) debug_msg( "Adding CBx :: %i", ext ); |
klauss | 91:c2a86b1f8aaa | 722 | cb = new Call_Box( ext, port ); |
klauss | 91:c2a86b1f8aaa | 723 | |
klauss | 91:c2a86b1f8aaa | 724 | if( cb == NULL ) { |
klauss | 91:c2a86b1f8aaa | 725 | memory_is_over = true; |
klauss | 91:c2a86b1f8aaa | 726 | if( debug_memory ) debug_msg("Invite allocation cb fail"); |
klauss | 91:c2a86b1f8aaa | 727 | } else { |
klauss | 91:c2a86b1f8aaa | 728 | v_cb->add( cb ); |
klauss | 91:c2a86b1f8aaa | 729 | } |
klauss | 89:0fe315117b00 | 730 | } |
klauss | 0:4d17cd9c8f9d | 731 | } |
klauss | 91:c2a86b1f8aaa | 732 | if( cb != NULL ) { |
klauss | 91:c2a86b1f8aaa | 733 | cb->set_msg_id( data[ 0 ] ); |
klauss | 92:92df17f538a8 | 734 | if( cb->status == cb_idle ){ |
klauss | 92:92df17f538a8 | 735 | cb->set_invite_response_ok(); |
klauss | 92:92df17f538a8 | 736 | cb->invite_retry_count_reset(); |
klauss | 92:92df17f538a8 | 737 | } |
klauss | 87:679ee0d594a9 | 738 | invite_handler( v_call, v_cb, ts, cb ); |
klauss | 87:679ee0d594a9 | 739 | } |
klauss | 91:c2a86b1f8aaa | 740 | } |
klauss | 91:c2a86b1f8aaa | 741 | break; |
klauss | 4:de46f0d9b14d | 742 | case __REGISTRY__ : { |
klauss | 63:0d95da692bb4 | 743 | registry_counter++; |
klauss | 78:1353744f01e1 | 744 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 91:c2a86b1f8aaa | 745 | if( v_cb->size() < __MAX_CB_IN_A_BRANCH__ ) { |
klauss | 91:c2a86b1f8aaa | 746 | if( cb == NULL ) { |
klauss | 91:c2a86b1f8aaa | 747 | if( debug_main ) debug_msg("Adding Cbx :: %d", ext ); |
klauss | 91:c2a86b1f8aaa | 748 | cb = new Call_Box( ext, port ); |
klauss | 91:c2a86b1f8aaa | 749 | |
klauss | 91:c2a86b1f8aaa | 750 | if( cb == NULL ) { |
klauss | 91:c2a86b1f8aaa | 751 | memory_is_over = true; |
klauss | 91:c2a86b1f8aaa | 752 | if( debug_memory ) debug_msg("Registry cb allocation fail"); |
klauss | 91:c2a86b1f8aaa | 753 | } else { |
klauss | 91:c2a86b1f8aaa | 754 | v_cb->add( cb ); |
klauss | 91:c2a86b1f8aaa | 755 | if( debug_main ) debug_msg("Added CBx -- %d", ext ); |
klauss | 91:c2a86b1f8aaa | 756 | } |
klauss | 87:679ee0d594a9 | 757 | } |
klauss | 0:4d17cd9c8f9d | 758 | } |
klauss | 78:1353744f01e1 | 759 | if( debug_main ) debug_msg("Registered %d - %d", ext, port ); |
klauss | 91:c2a86b1f8aaa | 760 | |
klauss | 87:679ee0d594a9 | 761 | if( cb != NULL ) cb->registry(); |
klauss | 91:c2a86b1f8aaa | 762 | |
klauss | 91:c2a86b1f8aaa | 763 | } |
klauss | 91:c2a86b1f8aaa | 764 | break; |
klauss | 4:de46f0d9b14d | 765 | case __BOOT__ : { |
klauss | 63:0d95da692bb4 | 766 | boot_counter++; |
klauss | 91:c2a86b1f8aaa | 767 | send2callboxes( __build_cb_package__( ext, port, __REGISTRY__, |
klauss | 92:92df17f538a8 | 768 | ( char * )data, data[ 0 ] | BIT7, __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 91:c2a86b1f8aaa | 769 | } |
klauss | 91:c2a86b1f8aaa | 770 | break; |
klauss | 4:de46f0d9b14d | 771 | case __TELEMETRY__ : { |
klauss | 98:43b45f26b430 | 772 | /* |
klauss | 78:1353744f01e1 | 773 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 91:c2a86b1f8aaa | 774 | |
klauss | 91:c2a86b1f8aaa | 775 | if( cb == NULL ) { |
klauss | 92:92df17f538a8 | 776 | // send2callboxes( __build_cb_package__( ext, port, __REGISTRY__, |
klauss | 92:92df17f538a8 | 777 | // ( char * )data, ( ( data[ 0 ] & ~BIT7 ) + 1 ), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 87:679ee0d594a9 | 778 | |
klauss | 78:1353744f01e1 | 779 | if( debug_main ) debug_msg("Adding Cbx :: %d", ext ); |
klauss | 78:1353744f01e1 | 780 | cb = new Call_Box( ext, port ); |
klauss | 91:c2a86b1f8aaa | 781 | |
klauss | 87:679ee0d594a9 | 782 | if( cb == NULL ){ |
klauss | 87:679ee0d594a9 | 783 | memory_is_over = true; |
klauss | 87:679ee0d594a9 | 784 | }else{ |
klauss | 87:679ee0d594a9 | 785 | v_cb->add( cb ); |
klauss | 87:679ee0d594a9 | 786 | if( debug_main ) debug_msg("Added CBx -- %d", ext ); |
klauss | 87:679ee0d594a9 | 787 | } |
klauss | 78:1353744f01e1 | 788 | } |
klauss | 98:43b45f26b430 | 789 | |
klauss | 87:679ee0d594a9 | 790 | if( cb != NULL ){ |
klauss | 87:679ee0d594a9 | 791 | cb->registry(); |
klauss | 92:92df17f538a8 | 792 | } |
klauss | 98:43b45f26b430 | 793 | */ |
klauss | 63:0d95da692bb4 | 794 | telemetry_counter++; |
klauss | 78:1353744f01e1 | 795 | if( debug_telemetry ) send_msg("::Telemetry from %d - %d::", ext, port ); |
klauss | 91:c2a86b1f8aaa | 796 | build_telemetry_report( ext, port, ( char *)data ); |
klauss | 91:c2a86b1f8aaa | 797 | } |
klauss | 91:c2a86b1f8aaa | 798 | break; |
klauss | 91:c2a86b1f8aaa | 799 | |
klauss | 67:cdedc64d9921 | 800 | case __BOOTLOADER_CBX__ : { |
klauss | 67:cdedc64d9921 | 801 | bootloader_cbx_counter++; |
klauss | 91:c2a86b1f8aaa | 802 | //int ret = bl_cbx_reply_to_eth( ext, ( char * )data ); |
klauss | 91:c2a86b1f8aaa | 803 | } |
klauss | 91:c2a86b1f8aaa | 804 | break; |
klauss | 91:c2a86b1f8aaa | 805 | |
klauss | 36:728498a78e1e | 806 | case __PROMPT__ : { |
klauss | 78:1353744f01e1 | 807 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 91:c2a86b1f8aaa | 808 | |
klauss | 91:c2a86b1f8aaa | 809 | if( cb == NULL ) { |
klauss | 92:92df17f538a8 | 810 | // send2callboxes( __build_cb_package__( ext, port, __REGISTRY__, |
klauss | 92:92df17f538a8 | 811 | // ( char * )data, ( ( data[ 0 ] & ~BIT7 ) + 1 ), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 92:92df17f538a8 | 812 | |
klauss | 78:1353744f01e1 | 813 | if( debug_main ) debug_msg("Adding Cbx :: %d", ext ); |
klauss | 78:1353744f01e1 | 814 | cb = new Call_Box( ext, port ); |
klauss | 87:679ee0d594a9 | 815 | if( cb == NULL ){ |
klauss | 87:679ee0d594a9 | 816 | memory_is_over = true; |
klauss | 87:679ee0d594a9 | 817 | }else{ |
klauss | 87:679ee0d594a9 | 818 | v_cb->add( cb ); |
klauss | 87:679ee0d594a9 | 819 | if( debug_main ) debug_msg("Added CBx -- %d", ext ); |
klauss | 87:679ee0d594a9 | 820 | } |
klauss | 48:195c97f12e8e | 821 | } |
klauss | 91:c2a86b1f8aaa | 822 | |
klauss | 92:92df17f538a8 | 823 | if( cb!= NULL ) cb->registry(); |
klauss | 91:c2a86b1f8aaa | 824 | |
klauss | 91:c2a86b1f8aaa | 825 | if( xstrmatch( ( uint8_t * )data, ( uint8_t * )"ping" ) ) { |
klauss | 78:1353744f01e1 | 826 | if( debug_ping ) send_msg( "Prompt pkg from ( %i, %i ) :: Ping", ext, port ); |
klauss | 91:c2a86b1f8aaa | 827 | } else { |
klauss | 78:1353744f01e1 | 828 | prompt_counter++; |
klauss | 78:1353744f01e1 | 829 | //fixme isso nao poderia ser resolvido com um sendmsg ? |
klauss | 78:1353744f01e1 | 830 | send_msg( "Prompt pkg from ( %i, %i ) ::", ext, port ); |
klauss | 91:c2a86b1f8aaa | 831 | for( register uint8_t i = 0; i < 32; i++ ) { |
klauss | 78:1353744f01e1 | 832 | pc.printf("%c", data[ i ] ); |
klauss | 78:1353744f01e1 | 833 | if( i == 15 ) pc.printf( "\r\n" ); |
klauss | 78:1353744f01e1 | 834 | } |
klauss | 78:1353744f01e1 | 835 | pc.printf("\n\r> "); |
klauss | 91:c2a86b1f8aaa | 836 | |
klauss | 91:c2a86b1f8aaa | 837 | if( tcp_session ) { |
klauss | 78:1353744f01e1 | 838 | char aux[ __CB_BUFFER_SIZE__ + 3 ]; |
klauss | 78:1353744f01e1 | 839 | strncpy( aux, (char * )data, __CB_BUFFER_SIZE__ ); |
klauss | 78:1353744f01e1 | 840 | strcat( aux, "\n\r\0" ); |
klauss | 78:1353744f01e1 | 841 | tcp_client.send_all( ( char *)data, strlen( (char * )data ) ); |
klauss | 78:1353744f01e1 | 842 | tcp_client.send_all( "\r\n> ", strlen( "\r\n> " ) ); |
klauss | 78:1353744f01e1 | 843 | } |
klauss | 48:195c97f12e8e | 844 | } |
klauss | 91:c2a86b1f8aaa | 845 | } |
klauss | 91:c2a86b1f8aaa | 846 | break; |
klauss | 0:4d17cd9c8f9d | 847 | case __AUDIO__ : { |
klauss | 63:0d95da692bb4 | 848 | audio_counter++; |
klauss | 78:1353744f01e1 | 849 | VZ_call * call = __find_Call__( v_call, ext ); |
klauss | 91:c2a86b1f8aaa | 850 | if( call != NULL ) { |
klauss | 91:c2a86b1f8aaa | 851 | char * pkg = call->build_eth_package( data + 2 ); |
klauss | 0:4d17cd9c8f9d | 852 | call->send_message( pkg ); |
klauss | 92:92df17f538a8 | 853 | call->cbx_pkg_idle_timer_reset(); |
klauss | 78:1353744f01e1 | 854 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 92:92df17f538a8 | 855 | if( cb != NULL ){ |
klauss | 92:92df17f538a8 | 856 | cb->reset_elapsed_time(); |
klauss | 92:92df17f538a8 | 857 | cb->set_invite_response_ok(); |
klauss | 92:92df17f538a8 | 858 | cb->invite_retry_count_reset(); |
klauss | 92:92df17f538a8 | 859 | } |
klauss | 91:c2a86b1f8aaa | 860 | } else { |
klauss | 78:1353744f01e1 | 861 | if( debug_main ) debug_msg("received missed package from CBx :: %i", ext ); |
klauss | 0:4d17cd9c8f9d | 862 | } |
klauss | 91:c2a86b1f8aaa | 863 | } |
klauss | 91:c2a86b1f8aaa | 864 | break; |
klauss | 9:ffa64f38ef9c | 865 | }// fim switch |
klauss | 92:92df17f538a8 | 866 | |
klauss | 92:92df17f538a8 | 867 | { |
klauss | 92:92df17f538a8 | 868 | if( invite_retry_time == true ){ |
klauss | 92:92df17f538a8 | 869 | invite_retry_time = false; |
klauss | 92:92df17f538a8 | 870 | static int retry_invite_pkg = 0; |
klauss | 92:92df17f538a8 | 871 | if( retry_invite_pkg >= v_cb->size() ) retry_invite_pkg = 0; |
klauss | 92:92df17f538a8 | 872 | |
klauss | 92:92df17f538a8 | 873 | Call_Box * cb = (Call_Box *)v_cb->get_element( retry_invite_pkg++ ); |
klauss | 92:92df17f538a8 | 874 | |
klauss | 92:92df17f538a8 | 875 | if( ( cb->status == cb_ringing ) || ( cb->status == cb_trying ) || ( cb->status == cb_on_call ) ){ |
klauss | 92:92df17f538a8 | 876 | if( cb->get_invite_response() == false ){ |
klauss | 92:92df17f538a8 | 877 | //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); |
klauss | 92:92df17f538a8 | 878 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); |
klauss | 92:92df17f538a8 | 879 | |
klauss | 92:92df17f538a8 | 880 | buffer[ __TIMESLICE_PLACE__ ] = cb->get_timeslice(); |
klauss | 92:92df17f538a8 | 881 | |
klauss | 92:92df17f538a8 | 882 | send2callboxes( __build_cb_package__( cb->get_ext(), cb->get_port(), __INVITE__, ( char * )buffer, |
klauss | 92:92df17f538a8 | 883 | cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 92:92df17f538a8 | 884 | if( debug_invite ) debug_msg("resend invite OK to Cbx : %d", cb->get_ext() ); |
klauss | 92:92df17f538a8 | 885 | } |
klauss | 92:92df17f538a8 | 886 | if( cb->get_invite_retry_count() == 0 ){ |
klauss | 92:92df17f538a8 | 887 | cb->send_bye(); |
klauss | 92:92df17f538a8 | 888 | |
klauss | 92:92df17f538a8 | 889 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 92:92df17f538a8 | 890 | cb->set_timeslice( 0x00 ); |
klauss | 92:92df17f538a8 | 891 | |
klauss | 92:92df17f538a8 | 892 | //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); |
klauss | 92:92df17f538a8 | 893 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); |
klauss | 92:92df17f538a8 | 894 | |
klauss | 92:92df17f538a8 | 895 | for( register uint8_t i = 0; i < v_call->size(); i++ ){ |
klauss | 92:92df17f538a8 | 896 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 92:92df17f538a8 | 897 | if( call->get_cb_ext() == cb->get_ext() ){ |
klauss | 92:92df17f538a8 | 898 | v_call->remove_element( i ); |
klauss | 92:92df17f538a8 | 899 | if( call != NULL ) delete( call ); |
klauss | 92:92df17f538a8 | 900 | break; |
klauss | 92:92df17f538a8 | 901 | } |
klauss | 92:92df17f538a8 | 902 | } |
klauss | 92:92df17f538a8 | 903 | |
klauss | 92:92df17f538a8 | 904 | if( debug_invite ) debug_msg( "-- No audio pkgs --" ); |
klauss | 92:92df17f538a8 | 905 | set_status( cb->status, cb_idle ); |
klauss | 92:92df17f538a8 | 906 | set_status( cb->sip->status, sip_idle ); |
klauss | 92:92df17f538a8 | 907 | } |
klauss | 92:92df17f538a8 | 908 | } |
klauss | 92:92df17f538a8 | 909 | } |
klauss | 92:92df17f538a8 | 910 | } |
klauss | 91:c2a86b1f8aaa | 911 | |
klauss | 91:c2a86b1f8aaa | 912 | for( register uint8_t i = 0; i < v_call->size(); i++ ) { |
klauss | 0:4d17cd9c8f9d | 913 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 914 | int length = 0; |
klauss | 0:4d17cd9c8f9d | 915 | char * tmp = call->get_eth_message( &length ); |
klauss | 91:c2a86b1f8aaa | 916 | if( tmp != NULL ) { |
klauss | 0:4d17cd9c8f9d | 917 | int cb_port = 0xffff; |
klauss | 78:1353744f01e1 | 918 | Call_Box * cb = __find_CB__( v_cb, call->get_cb_ext() ); |
klauss | 91:c2a86b1f8aaa | 919 | |
klauss | 91:c2a86b1f8aaa | 920 | if( cb != NULL ) { |
klauss | 4:de46f0d9b14d | 921 | cb_port = cb->get_port(); |
klauss | 91:c2a86b1f8aaa | 922 | |
klauss | 91:c2a86b1f8aaa | 923 | uint8_t * pkg2cb = __build_cb_package__( call->get_cb_ext(), cb_port, __AUDIO__, |
klauss | 91:c2a86b1f8aaa | 924 | tmp, __AUDIO__, length, write_buffer ); |
klauss | 91:c2a86b1f8aaa | 925 | |
klauss | 78:1353744f01e1 | 926 | send2callboxes( pkg2cb ); |
klauss | 91:c2a86b1f8aaa | 927 | } else if( debug_main ) debug_msg("received missed package from CBx :: %i -- Type :: %i", ext, type ); |
klauss | 0:4d17cd9c8f9d | 928 | } |
klauss | 14:22a35f575502 | 929 | } |
klauss | 92:92df17f538a8 | 930 | |
klauss | 81:3656f00ab3db | 931 | //if( v_call->size() == 0 ) refresh( v_cb, buffer, write_buffer, NULL ); |
klauss | 91:c2a86b1f8aaa | 932 | |
klauss | 91:c2a86b1f8aaa | 933 | if( timer_sync_refresh.read_ms() > 500 ) { |
klauss | 89:0fe315117b00 | 934 | timer_sync_refresh.reset(); |
klauss | 91:c2a86b1f8aaa | 935 | |
klauss | 89:0fe315117b00 | 936 | static uint8_t mode = TIME_TO_REFRESH; |
klauss | 91:c2a86b1f8aaa | 937 | |
klauss | 91:c2a86b1f8aaa | 938 | if( mode == TIME_TO_REFRESH ) { |
klauss | 89:0fe315117b00 | 939 | mode = TIME_TO_WAKE_UP; |
klauss | 89:0fe315117b00 | 940 | refresh( v_cb, buffer, write_buffer, NULL ); |
klauss | 91:c2a86b1f8aaa | 941 | } else { |
klauss | 89:0fe315117b00 | 942 | mode = TIME_TO_REFRESH; |
klauss | 91:c2a86b1f8aaa | 943 | if( wake_all ) if( v_call->size() == 0 ) wake_all_up( v_cb, buffer, write_buffer ); |
klauss | 89:0fe315117b00 | 944 | } |
klauss | 89:0fe315117b00 | 945 | } |
klauss | 91:c2a86b1f8aaa | 946 | |
klauss | 91:c2a86b1f8aaa | 947 | |
klauss | 89:0fe315117b00 | 948 | //refresh( v_cb, buffer, write_buffer, NULL ); |
klauss | 91:c2a86b1f8aaa | 949 | |
klauss | 78:1353744f01e1 | 950 | //Fixme pensar melhor nessa parte durante ligacoes, pode complicar com muitos cbx ... |
klauss | 81:3656f00ab3db | 951 | //if( v_call->size() == 0 ) registry_aging( v_cb, buffer, write_buffer ); |
klauss | 91:c2a86b1f8aaa | 952 | |
klauss | 78:1353744f01e1 | 953 | // check sip messages only for cbx in call ? |
klauss | 78:1353744f01e1 | 954 | int ext_to__be_removed = sip_manager( v_cb ); |
klauss | 91:c2a86b1f8aaa | 955 | if( ext_to__be_removed > 0x00 ) { |
klauss | 78:1353744f01e1 | 956 | Call_Box * cb = __find_CB__( v_cb, ext_to__be_removed ); |
klauss | 91:c2a86b1f8aaa | 957 | if( cb != NULL ) { |
klauss | 91:c2a86b1f8aaa | 958 | if( cb->status == cb_on_call ) { |
klauss | 4:de46f0d9b14d | 959 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 4:de46f0d9b14d | 960 | cb->set_timeslice( 0x00 ); |
klauss | 4:de46f0d9b14d | 961 | buffer[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 91:c2a86b1f8aaa | 962 | |
klauss | 78:1353744f01e1 | 963 | set_status( cb->status, cb_idle ); |
klauss | 78:1353744f01e1 | 964 | set_status( cb->sip->status, sip_idle ); |
klauss | 91:c2a86b1f8aaa | 965 | |
klauss | 91:c2a86b1f8aaa | 966 | for( register uint8_t i = 0; i < v_call->size(); i++ ) { |
klauss | 4:de46f0d9b14d | 967 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 91:c2a86b1f8aaa | 968 | if( call->get_cb_ext() == ext_to__be_removed ) { |
klauss | 4:de46f0d9b14d | 969 | v_call->remove_element( i ); |
klauss | 4:de46f0d9b14d | 970 | delete( call ); |
klauss | 4:de46f0d9b14d | 971 | } |
klauss | 2:93bec7313ccc | 972 | } |
klauss | 91:c2a86b1f8aaa | 973 | |
klauss | 92:92df17f538a8 | 974 | //cb->set_msg_id( ( cb->get_msg_id() & ~BIT7 ) + 1 ); |
klauss | 92:92df17f538a8 | 975 | cb->set_msg_id( ( cb->get_msg_id() + 1 ) & ~BIT7 ); |
klauss | 91:c2a86b1f8aaa | 976 | |
klauss | 82:f55d13babca0 | 977 | //debug_msg("msg_id -- %d", cb->get_msg_id() ); |
klauss | 91:c2a86b1f8aaa | 978 | |
klauss | 91:c2a86b1f8aaa | 979 | send2callboxes( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 4:de46f0d9b14d | 980 | ( char * )buffer, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 91:c2a86b1f8aaa | 981 | |
klauss | 91:c2a86b1f8aaa | 982 | if( debug_invite ) debug_msg("Received Bye from *"); |
klauss | 91:c2a86b1f8aaa | 983 | |
klauss | 0:4d17cd9c8f9d | 984 | } |
klauss | 91:c2a86b1f8aaa | 985 | } else if( debug_main ) debug_msg("Missed bye request from * CBx :: %d", ext ); |
klauss | 0:4d17cd9c8f9d | 986 | } |
klauss | 91:c2a86b1f8aaa | 987 | |
klauss | 81:3656f00ab3db | 988 | /* Verifica andamento de ligações para eventualmente encerra-las por timeout */ |
klauss | 10:22da1a0ac1e1 | 989 | call_manager( v_call, v_cb, buffer, write_buffer, ts ); |
klauss | 91:c2a86b1f8aaa | 990 | |
klauss | 81:3656f00ab3db | 991 | /* tratamento de pedidos de ligação */ |
klauss | 81:3656f00ab3db | 992 | invite_handler( v_call, v_cb, ts, NULL ); |
klauss | 91:c2a86b1f8aaa | 993 | |
klauss | 81:3656f00ab3db | 994 | /* rotina de verificação de TS's perdidos */ |
klauss | 91:c2a86b1f8aaa | 995 | if( ( v_call->size() == 0 ) && ( ts->remain_timeslices() != __MAX_TIMESLICES__ ) ) { |
klauss | 91:c2a86b1f8aaa | 996 | bool ts_reset = true; |
klauss | 91:c2a86b1f8aaa | 997 | for( register uint8_t i = 0; i < v_cb->size(); i++ ) { |
klauss | 82:f55d13babca0 | 998 | if ( ( ( Call_Box * )v_cb->get_element( i ) )->get_status() != cb_idle && |
klauss | 91:c2a86b1f8aaa | 999 | ( ( Call_Box * )v_cb->get_element( i ) )->get_sip_status() != sip_idle ) { |
klauss | 78:1353744f01e1 | 1000 | ts_reset = false; |
klauss | 78:1353744f01e1 | 1001 | break; |
klauss | 78:1353744f01e1 | 1002 | } |
klauss | 91:c2a86b1f8aaa | 1003 | } |
klauss | 91:c2a86b1f8aaa | 1004 | if( ts_reset ) { |
klauss | 81:3656f00ab3db | 1005 | if( debug_invite ) debug_msg("Resetando TS"); |
klauss | 78:1353744f01e1 | 1006 | ts->reset(); |
klauss | 78:1353744f01e1 | 1007 | } |
klauss | 0:4d17cd9c8f9d | 1008 | } |
klauss | 91:c2a86b1f8aaa | 1009 | |
klauss | 81:3656f00ab3db | 1010 | //invite_pgk_retry_manager( v_call, v_cb, buffer, write_buffer ); |
klauss | 91:c2a86b1f8aaa | 1011 | |
klauss | 81:3656f00ab3db | 1012 | /* escuta se existe algum procedimento de gravação de cbx */ |
klauss | 91:c2a86b1f8aaa | 1013 | //bl_handler(); |
klauss | 91:c2a86b1f8aaa | 1014 | |
klauss | 81:3656f00ab3db | 1015 | /* rotina que esvazia possiveis pacotes que não foram transmitidos para evitar conflito */ |
klauss | 72:895ca792c647 | 1016 | tx_buffer_ring_buffer_handler(); |
klauss | 91:c2a86b1f8aaa | 1017 | |
klauss | 30:8dfb6d8de53d | 1018 | type = __DO_NOTHING__; |
klauss | 91:c2a86b1f8aaa | 1019 | |
klauss | 81:3656f00ab3db | 1020 | if( eth_status == 0 ) eth_wdt = ETH_CONNECT_TIMEOUT; |
klauss | 91:c2a86b1f8aaa | 1021 | |
klauss | 91:c2a86b1f8aaa | 1022 | if( wdt_timer.read() >= 1 ) { |
klauss | 91:c2a86b1f8aaa | 1023 | //FIXME remove myself |
klauss | 92:92df17f538a8 | 1024 | //registry_test = true; |
klauss | 92:92df17f538a8 | 1025 | invite_retry_time = true; |
klauss | 91:c2a86b1f8aaa | 1026 | |
klauss | 83:b8a1d8fdeaeb | 1027 | uptime++; |
klauss | 91:c2a86b1f8aaa | 1028 | |
klauss | 81:3656f00ab3db | 1029 | wdt_timer.reset(); |
klauss | 91:c2a86b1f8aaa | 1030 | |
klauss | 81:3656f00ab3db | 1031 | if( wdt_show ) debug_wdt = true; |
klauss | 91:c2a86b1f8aaa | 1032 | |
klauss | 81:3656f00ab3db | 1033 | if( external_wdt ) external_wdt--; |
klauss | 81:3656f00ab3db | 1034 | if( pkg_wdt ) pkg_wdt--; |
klauss | 81:3656f00ab3db | 1035 | if( eth_wdt ) eth_wdt--; |
klauss | 91:c2a86b1f8aaa | 1036 | |
klauss | 91:c2a86b1f8aaa | 1037 | if( eth_wdt && external_wdt && pkg_wdt ){ |
klauss | 81:3656f00ab3db | 1038 | wdt.kick(); |
klauss | 81:3656f00ab3db | 1039 | } |
klauss | 81:3656f00ab3db | 1040 | } |
klauss | 91:c2a86b1f8aaa | 1041 | |
klauss | 91:c2a86b1f8aaa | 1042 | if( debug_wdt ) { |
klauss | 87:679ee0d594a9 | 1043 | send_msg("lpc_low_level_input_counter :: %d", lpc_low_level_input_counter ); |
klauss | 87:679ee0d594a9 | 1044 | lpc_low_level_input_counter = 0; |
klauss | 91:c2a86b1f8aaa | 1045 | |
klauss | 81:3656f00ab3db | 1046 | debug_wdt = false; |
klauss | 91:c2a86b1f8aaa | 1047 | if( eth_status == 0 ) { |
klauss | 91:c2a86b1f8aaa | 1048 | 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 | 91:c2a86b1f8aaa | 1049 | ( wdt.WatchdogCausedReset() ) ? "true" : "false", |
klauss | 91:c2a86b1f8aaa | 1050 | EXTERN_WDT_IDLE - external_wdt, |
klauss | 91:c2a86b1f8aaa | 1051 | EXTERN_WDT_IDLE, |
klauss | 91:c2a86b1f8aaa | 1052 | RX_CB_IDLE - pkg_wdt, |
klauss | 91:c2a86b1f8aaa | 1053 | RX_CB_IDLE |
klauss | 91:c2a86b1f8aaa | 1054 | ); |
klauss | 91:c2a86b1f8aaa | 1055 | } else { |
klauss | 91:c2a86b1f8aaa | 1056 | 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 | 91:c2a86b1f8aaa | 1057 | ( wdt.WatchdogCausedReset() ) ? "true" : "false", |
klauss | 91:c2a86b1f8aaa | 1058 | ETH_CONNECT_TIMEOUT - eth_wdt, |
klauss | 91:c2a86b1f8aaa | 1059 | EXTERN_WDT_IDLE - external_wdt, |
klauss | 91:c2a86b1f8aaa | 1060 | EXTERN_WDT_IDLE, |
klauss | 91:c2a86b1f8aaa | 1061 | RX_CB_IDLE - pkg_wdt, |
klauss | 91:c2a86b1f8aaa | 1062 | RX_CB_IDLE |
klauss | 91:c2a86b1f8aaa | 1063 | ); |
klauss | 81:3656f00ab3db | 1064 | } |
klauss | 81:3656f00ab3db | 1065 | } |
klauss | 0:4d17cd9c8f9d | 1066 | } |
klauss | 0:4d17cd9c8f9d | 1067 | } |