![](/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@76:9f5f99dd895a, 2014-11-25 (annotated)
- Committer:
- klauss
- Date:
- Tue Nov 25 17:37:56 2014 +0000
- Revision:
- 76:9f5f99dd895a
- Parent:
- 75:bd3c647b860b
- Child:
- 77:e8c0253b57bc
desenvolvendo invite assincrono | validando
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 | volatile uint8_t status = 0x00; |
klauss | 0:4d17cd9c8f9d | 18 | |
klauss | 0:4d17cd9c8f9d | 19 | int main(){ |
klauss | 48:195c97f12e8e | 20 | wdt.Configure( 40.0 ); |
klauss | 0:4d17cd9c8f9d | 21 | start_cpld(); |
klauss | 0:4d17cd9c8f9d | 22 | config_lpc(); |
klauss | 0:4d17cd9c8f9d | 23 | |
klauss | 43:455522f98de5 | 24 | init_fsystem(); |
klauss | 43:455522f98de5 | 25 | |
klauss | 68:b54993674190 | 26 | //FIXMEj pensar melhor em como saber se estou conectado na eth |
klauss | 48:195c97f12e8e | 27 | //int eth_status = __init_eth__( ð ); |
klauss | 48:195c97f12e8e | 28 | int eth_status = __init_eth__(); |
klauss | 30:8dfb6d8de53d | 29 | |
klauss | 8:e3bfe62a477e | 30 | if( eth_status ){ |
klauss | 48:195c97f12e8e | 31 | send_msg("Cannot connect to eth\n\r"); |
klauss | 43:455522f98de5 | 32 | }else{ |
klauss | 48:195c97f12e8e | 33 | send_msg("Connection eth - ok"); |
klauss | 43:455522f98de5 | 34 | init_prompt(); |
klauss | 8:e3bfe62a477e | 35 | } |
klauss | 0:4d17cd9c8f9d | 36 | Timer t; |
klauss | 0:4d17cd9c8f9d | 37 | t.start(); |
klauss | 67:cdedc64d9921 | 38 | //Timer udp_timer; |
klauss | 0:4d17cd9c8f9d | 39 | |
klauss | 0:4d17cd9c8f9d | 40 | /* representa ramal do call box */ |
klauss | 0:4d17cd9c8f9d | 41 | int ext = 0; |
klauss | 0:4d17cd9c8f9d | 42 | |
klauss | 0:4d17cd9c8f9d | 43 | /* representa porta do call box */ |
klauss | 0:4d17cd9c8f9d | 44 | int port = 0; |
klauss | 0:4d17cd9c8f9d | 45 | /* buffer para onde se copia os dados vindos do cb para tratameno interno */ |
klauss | 0:4d17cd9c8f9d | 46 | uint8_t buffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 0:4d17cd9c8f9d | 47 | |
klauss | 0:4d17cd9c8f9d | 48 | /* buffer de escrita do pacote de saida que sera enviado pro cb / servidor */ |
klauss | 0:4d17cd9c8f9d | 49 | uint8_t write_buffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 0:4d17cd9c8f9d | 50 | |
klauss | 0:4d17cd9c8f9d | 51 | /* ponteiro que aponta para os dados vindo do CPLD */ |
klauss | 0:4d17cd9c8f9d | 52 | uint8_t * buffer_from_cb_ptr = ( uint8_t * )RXBuffer; |
klauss | 0:4d17cd9c8f9d | 53 | |
klauss | 62:07e5bdc9f8f7 | 54 | /* Armazena o ultimo pacote recebido dos CBx */ |
klauss | 62:07e5bdc9f8f7 | 55 | uint8_t cb_rx_buffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 62:07e5bdc9f8f7 | 56 | |
klauss | 0:4d17cd9c8f9d | 57 | /* referencia para os dados contidos no pacote, sem o header */ |
klauss | 0:4d17cd9c8f9d | 58 | uint8_t * data = NULL; |
klauss | 0:4d17cd9c8f9d | 59 | |
klauss | 0:4d17cd9c8f9d | 60 | /* gerencia o tipo do pacote para providenciar tratamento adequado */ |
klauss | 0:4d17cd9c8f9d | 61 | volatile uint8_t type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 62 | |
klauss | 0:4d17cd9c8f9d | 63 | /* representa a lista dos Call Boxes atualmente reconhecidos pela cabeceira */ |
klauss | 0:4d17cd9c8f9d | 64 | Vector * v_cb = new Vector(); |
klauss | 0:4d17cd9c8f9d | 65 | |
klauss | 0:4d17cd9c8f9d | 66 | /* representa a lista de ligacoes ativas na cabeceira */ |
klauss | 0:4d17cd9c8f9d | 67 | Vector * v_call = new Vector(); |
klauss | 0:4d17cd9c8f9d | 68 | |
klauss | 0:4d17cd9c8f9d | 69 | /* gerencia a distribuicao de timeslice para os call boxes */ |
klauss | 0:4d17cd9c8f9d | 70 | Timeslice * ts = new Timeslice(); |
klauss | 0:4d17cd9c8f9d | 71 | /* tenta instanciar o vetor de call boxes, acende o led 1 antes de encerrar o programa */ |
klauss | 0:4d17cd9c8f9d | 72 | reset_leds(); |
klauss | 0:4d17cd9c8f9d | 73 | if( v_cb == NULL ){ |
klauss | 0:4d17cd9c8f9d | 74 | led1 = 0; |
klauss | 0:4d17cd9c8f9d | 75 | while( v_cb == NULL ){ |
klauss | 0:4d17cd9c8f9d | 76 | Vector * v_cb = new Vector(); |
klauss | 0:4d17cd9c8f9d | 77 | led1 = led1; |
klauss | 0:4d17cd9c8f9d | 78 | if( t.read() > 5 ){ |
klauss | 48:195c97f12e8e | 79 | send_msg("Erro ao alocar o vetor de CBx"); |
klauss | 0:4d17cd9c8f9d | 80 | t.reset(); |
klauss | 0:4d17cd9c8f9d | 81 | } |
klauss | 0:4d17cd9c8f9d | 82 | } |
klauss | 0:4d17cd9c8f9d | 83 | }else if( v_call == NULL ){ |
klauss | 0:4d17cd9c8f9d | 84 | led2 = 0; |
klauss | 0:4d17cd9c8f9d | 85 | led1 = 1; |
klauss | 0:4d17cd9c8f9d | 86 | while( v_call == NULL ){ |
klauss | 0:4d17cd9c8f9d | 87 | Vector * v_call = new Vector(); |
klauss | 0:4d17cd9c8f9d | 88 | if( t.read() > 5 ){ |
klauss | 48:195c97f12e8e | 89 | send_msg("Erro ao alocar o vetor de Calls"); |
klauss | 0:4d17cd9c8f9d | 90 | t.reset(); |
klauss | 0:4d17cd9c8f9d | 91 | } |
klauss | 0:4d17cd9c8f9d | 92 | } |
klauss | 0:4d17cd9c8f9d | 93 | } |
klauss | 63:0d95da692bb4 | 94 | |
klauss | 63:0d95da692bb4 | 95 | for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ){ |
klauss | 63:0d95da692bb4 | 96 | cb_rx_buffer[ i ] = 0; |
klauss | 63:0d95da692bb4 | 97 | cb_tx_buffer[ i ] = 0; |
klauss | 63:0d95da692bb4 | 98 | } |
klauss | 63:0d95da692bb4 | 99 | |
klauss | 0:4d17cd9c8f9d | 100 | reset_leds(); |
klauss | 33:735fd60e96d8 | 101 | udp_timer.start(); |
klauss | 68:b54993674190 | 102 | |
klauss | 68:b54993674190 | 103 | init_telemetry_handler(); |
klauss | 68:b54993674190 | 104 | |
klauss | 62:07e5bdc9f8f7 | 105 | static uint8_t count = 0; |
klauss | 67:cdedc64d9921 | 106 | int bl_ret = init_bl_handler(); |
klauss | 67:cdedc64d9921 | 107 | if( dbl ) send_msg("Valor de retorno do bl_habdler = %d", bl_ret ); |
klauss | 67:cdedc64d9921 | 108 | |
klauss | 68:b54993674190 | 109 | uint8_t before = t.read(); |
klauss | 68:b54993674190 | 110 | |
klauss | 68:b54993674190 | 111 | send_msg("Ready"); |
klauss | 68:b54993674190 | 112 | |
klauss | 67:cdedc64d9921 | 113 | /*------------------------------------------------ main loop -------------------------------------------------------------------------*/ |
Cola | 21:0bd688722e81 | 114 | while( true ){ |
klauss | 68:b54993674190 | 115 | //FIXMEj colocar uma condicao aqui caso necessario pra nao comprometer ligacoes ... |
klauss | 35:96885a7931f0 | 116 | prompt_process( NULL ); |
klauss | 33:735fd60e96d8 | 117 | wdt.kick(); |
klauss | 62:07e5bdc9f8f7 | 118 | |
klauss | 62:07e5bdc9f8f7 | 119 | if( rx ){ |
klauss | 62:07e5bdc9f8f7 | 120 | char str[ 1024 ]; |
klauss | 63:0d95da692bb4 | 121 | strcpy( str, "RX :: \n\r " ); |
klauss | 62:07e5bdc9f8f7 | 122 | for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ){ |
klauss | 62:07e5bdc9f8f7 | 123 | char tmp[ 16 ]; |
klauss | 62:07e5bdc9f8f7 | 124 | strcat( str, itoa( cb_rx_buffer[ i ], tmp, 16 ) ); |
klauss | 63:0d95da692bb4 | 125 | if( ( i != 0 ) && !( ( i + 1 ) % 20 ) ) strcat( str, "\n\r " ); |
klauss | 62:07e5bdc9f8f7 | 126 | |
klauss | 62:07e5bdc9f8f7 | 127 | else strcat( str, " " ); |
klauss | 62:07e5bdc9f8f7 | 128 | } |
klauss | 62:07e5bdc9f8f7 | 129 | send_msg( "%s", str ); |
klauss | 62:07e5bdc9f8f7 | 130 | rx = false; |
klauss | 62:07e5bdc9f8f7 | 131 | } |
klauss | 62:07e5bdc9f8f7 | 132 | |
klauss | 63:0d95da692bb4 | 133 | if( tx ){ |
klauss | 63:0d95da692bb4 | 134 | char str[ 1024 ]; |
klauss | 63:0d95da692bb4 | 135 | strcpy( str, "TX :: \n\r " ); |
klauss | 63:0d95da692bb4 | 136 | for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ){ |
klauss | 63:0d95da692bb4 | 137 | char tmp[ 16 ]; |
klauss | 63:0d95da692bb4 | 138 | strcat( str, itoa( cb_tx_buffer[ i ], tmp, 16 ) ); |
klauss | 63:0d95da692bb4 | 139 | if( ( i != 0 ) && !( ( i + 1 ) % 20 ) ) strcat( str, "\n\r " ); |
klauss | 63:0d95da692bb4 | 140 | |
klauss | 63:0d95da692bb4 | 141 | else strcat( str, " " ); |
klauss | 63:0d95da692bb4 | 142 | } |
klauss | 63:0d95da692bb4 | 143 | send_msg( "%s", str ); |
klauss | 63:0d95da692bb4 | 144 | tx = false; |
klauss | 63:0d95da692bb4 | 145 | } |
klauss | 63:0d95da692bb4 | 146 | |
klauss | 63:0d95da692bb4 | 147 | if( r_stats ){ |
klauss | 63:0d95da692bb4 | 148 | boot_counter = 0; |
klauss | 63:0d95da692bb4 | 149 | registry_counter = 0; |
klauss | 63:0d95da692bb4 | 150 | invite_counter = 0; |
klauss | 63:0d95da692bb4 | 151 | audio_counter = 0; |
klauss | 63:0d95da692bb4 | 152 | telemetry_counter = 0; |
klauss | 63:0d95da692bb4 | 153 | cb_bye_counter = 0; |
klauss | 63:0d95da692bb4 | 154 | prompt_counter = 0; |
klauss | 63:0d95da692bb4 | 155 | flood_counter = 0; |
klauss | 67:cdedc64d9921 | 156 | bootloader_cbx_counter = 0; |
klauss | 63:0d95da692bb4 | 157 | r_stats = false; |
klauss | 63:0d95da692bb4 | 158 | stats = true; |
klauss | 63:0d95da692bb4 | 159 | } |
klauss | 63:0d95da692bb4 | 160 | if( stats ){ |
klauss | 63:0d95da692bb4 | 161 | char str[ 200 ]; |
klauss | 67:cdedc64d9921 | 162 | 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_by :: %u\n\r Prompt :: %u\n\r Flood :: %u\n\r Bootloader_cbx :: %u\n\r", |
klauss | 67:cdedc64d9921 | 163 | boot_counter, registry_counter, invite_counter, audio_counter, telemetry_counter, cb_bye_counter, prompt_counter, flood_counter, bootloader_cbx_counter ); |
klauss | 63:0d95da692bb4 | 164 | send_msg( str ); |
klauss | 63:0d95da692bb4 | 165 | stats =false; |
klauss | 63:0d95da692bb4 | 166 | } |
klauss | 63:0d95da692bb4 | 167 | |
klauss | 62:07e5bdc9f8f7 | 168 | if( list ){ |
klauss | 62:07e5bdc9f8f7 | 169 | send_msg("Registred %d CBx", v_cb->size() ); |
klauss | 62:07e5bdc9f8f7 | 170 | if( v_cb->size() == 0 ){ |
klauss | 62:07e5bdc9f8f7 | 171 | send_msg("{-/-}"); |
klauss | 62:07e5bdc9f8f7 | 172 | }else if( v_cb->size() == 1 ){ |
klauss | 62:07e5bdc9f8f7 | 173 | Call_Box * cb = ( Call_Box * )v_cb->get_element( 0 ); |
klauss | 62:07e5bdc9f8f7 | 174 | send_msg(" %d ", cb->get_ext() ); |
klauss | 62:07e5bdc9f8f7 | 175 | }else{ |
klauss | 62:07e5bdc9f8f7 | 176 | char str[ 1024 ]; |
klauss | 62:07e5bdc9f8f7 | 177 | int ext_list[ 128 ]; |
klauss | 62:07e5bdc9f8f7 | 178 | Call_Box * cb = NULL; |
klauss | 62:07e5bdc9f8f7 | 179 | register int i = 0; |
klauss | 62:07e5bdc9f8f7 | 180 | for( ; i < v_cb->size(); i++ ){ |
klauss | 62:07e5bdc9f8f7 | 181 | cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 62:07e5bdc9f8f7 | 182 | ext_list[ i ] = cb->get_ext(); |
klauss | 62:07e5bdc9f8f7 | 183 | } |
klauss | 62:07e5bdc9f8f7 | 184 | |
klauss | 62:07e5bdc9f8f7 | 185 | qsort( ext_list, v_cb->size(), sizeof( int ), ls_comp ); |
klauss | 62:07e5bdc9f8f7 | 186 | |
klauss | 62:07e5bdc9f8f7 | 187 | char aux[ 16 ]; |
klauss | 62:07e5bdc9f8f7 | 188 | strcpy( str, "\r\n " ); |
klauss | 62:07e5bdc9f8f7 | 189 | for( i = 0; i < v_cb->size() - 1; i++ ){ |
klauss | 62:07e5bdc9f8f7 | 190 | sprintf( aux, "%i, ", ext_list[ i ] ); |
klauss | 62:07e5bdc9f8f7 | 191 | strcat( str, aux ); |
klauss | 62:07e5bdc9f8f7 | 192 | if( ( i != 0 ) && !( ( i + 1 ) % 8 ) ) strcat( str, "\r\n " ); |
klauss | 62:07e5bdc9f8f7 | 193 | } |
klauss | 62:07e5bdc9f8f7 | 194 | sprintf( aux, "%i ", ext_list[ i ] ); |
klauss | 62:07e5bdc9f8f7 | 195 | strcat( str, aux ); |
klauss | 62:07e5bdc9f8f7 | 196 | send_msg( "%s", str ); |
klauss | 62:07e5bdc9f8f7 | 197 | } |
klauss | 62:07e5bdc9f8f7 | 198 | list = false; |
klauss | 62:07e5bdc9f8f7 | 199 | } |
klauss | 62:07e5bdc9f8f7 | 200 | |
Cola | 22:d2a4b5939115 | 201 | if (pcks_s == 1){ |
klauss | 48:195c97f12e8e | 202 | send_msg("PKG_CSK OK: %d :: PKG_CSK ERR: %d", pkg_cksok, pkg_ckserr ); |
Cola | 24:270b436a1bb0 | 203 | pcks_s =0; |
Cola | 24:270b436a1bb0 | 204 | } |
Cola | 24:270b436a1bb0 | 205 | |
klauss | 52:12930cef17c4 | 206 | if( reset_cks ){ |
klauss | 52:12930cef17c4 | 207 | pkg_cksok = 0; |
klauss | 52:12930cef17c4 | 208 | pkg_ckserr = 0; |
klauss | 52:12930cef17c4 | 209 | send_msg("PKG_CSK OK: %d :: PKG_CSK ERR: %d", pkg_cksok, pkg_ckserr ); |
klauss | 52:12930cef17c4 | 210 | reset_cks = false; |
klauss | 52:12930cef17c4 | 211 | } |
klauss | 52:12930cef17c4 | 212 | |
Cola | 24:270b436a1bb0 | 213 | if (pshowcb == 1){ |
klauss | 48:195c97f12e8e | 214 | send_msg("Registred %d CBx", v_cb->size() ); |
Cola | 24:270b436a1bb0 | 215 | for( register uint8_t i = 0; i < v_cb->size(); i++ ){ |
klauss | 30:8dfb6d8de53d | 216 | Call_Box * cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 48:195c97f12e8e | 217 | send_msg("Type :: %d -- Ext :: %d -- TimeSlice :: %d -- remain_timeslices :: %d :: v_call->size() :: %d :: Status :: %d :: SIP Status %d", type , cb->get_ext(), cb->get_timeslice(), ts->remain_timeslices(), v_call->size(), cb->status, cb->sip->status ); |
klauss | 48:195c97f12e8e | 218 | } |
klauss | 48:195c97f12e8e | 219 | pshowcb = 0; |
klauss | 48:195c97f12e8e | 220 | } |
Cola | 22:d2a4b5939115 | 221 | |
klauss | 30:8dfb6d8de53d | 222 | if( pflood == 1 ) flood(); |
klauss | 30:8dfb6d8de53d | 223 | |
klauss | 0:4d17cd9c8f9d | 224 | if( status != __WAITING__ ){ |
klauss | 74:81c47fff88a5 | 225 | xmemcpy( cb_rx_buffer, buffer_from_cb_ptr, __CB_BUFFER_SIZE__ ); |
klauss | 74:81c47fff88a5 | 226 | //__read_cb_buffer__( buffer, buffer_from_cb_ptr ); |
klauss | 0:4d17cd9c8f9d | 227 | status = __WAITING__; |
klauss | 74:81c47fff88a5 | 228 | __read_cb_buffer__( buffer, cb_rx_buffer ); |
klauss | 0:4d17cd9c8f9d | 229 | |
klauss | 34:f19d9735428e | 230 | data = __parse_vz_pkg__( &ext, &port, &type, buffer ); |
klauss | 0:4d17cd9c8f9d | 231 | |
klauss | 3:cd9148672e25 | 232 | if( data != NULL ){ |
klauss | 31:bb5fb28a77df | 233 | if( debug_alive == 1 && debug_cb_rx ) debug_msg("Pkg from CBx :: %d -- Type :: %d", ext, type ); |
klauss | 7:019b08223b87 | 234 | if( type != __AUDIO__ ){ |
klauss | 0:4d17cd9c8f9d | 235 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 236 | if( cb != NULL ){ |
klauss | 3:cd9148672e25 | 237 | cb->reset_elapsed_time(); |
klauss | 0:4d17cd9c8f9d | 238 | if( ( data[ 0 ] ^ cb->get_msg_id() ) == BIT7 ){ |
klauss | 0:4d17cd9c8f9d | 239 | // some pkg reply |
klauss | 0:4d17cd9c8f9d | 240 | if( ( data[ 0 ] | BIT7 ) == cb->get_msg_id() ){ |
klauss | 7:019b08223b87 | 241 | if( type == __BOOT__ ) type = __REGISTRY__; |
klauss | 74:81c47fff88a5 | 242 | send2callboxes( __build_cb_package__( ext, port, type, |
klauss | 0:4d17cd9c8f9d | 243 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 244 | type = __DO_NOTHING__; |
klauss | 30:8dfb6d8de53d | 245 | if( debug_main ) debug_msg("-- Renviado ultimo pkg --"); |
klauss | 0:4d17cd9c8f9d | 246 | }else if( data[ 0 ] == ( cb->get_msg_id() | BIT7 ) ){ |
klauss | 0:4d17cd9c8f9d | 247 | // ack from CBx |
klauss | 30:8dfb6d8de53d | 248 | if( debug_main ) debug_msg("-- ACK do meu pkg --"); |
klauss | 0:4d17cd9c8f9d | 249 | type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 250 | } |
klauss | 0:4d17cd9c8f9d | 251 | } |
klauss | 0:4d17cd9c8f9d | 252 | } |
klauss | 0:4d17cd9c8f9d | 253 | } |
klauss | 0:4d17cd9c8f9d | 254 | }else type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 255 | } |
klauss | 2:93bec7313ccc | 256 | //---------------------- |
klauss | 57:78f7191a8b69 | 257 | uint8_t now = t.read(); |
klauss | 57:78f7191a8b69 | 258 | if( before != now ){ |
klauss | 57:78f7191a8b69 | 259 | led3 = !led3; |
klauss | 57:78f7191a8b69 | 260 | before = now; |
klauss | 57:78f7191a8b69 | 261 | } |
klauss | 57:78f7191a8b69 | 262 | |
klauss | 27:98f824719d1c | 263 | if( t.read() > 5 ){ |
klauss | 71:757fa1ef37c6 | 264 | static int test_ext = 5010; |
klauss | 71:757fa1ef37c6 | 265 | static int test_port = 5010; |
klauss | 48:195c97f12e8e | 266 | //send_msg("from_eth = %s - tcp_session = %s", ( from_eth ) ? "true" : "false", ( tcp_session ) ? "true" : "false" ); |
klauss | 48:195c97f12e8e | 267 | if( debug_alive ){ |
klauss | 48:195c97f12e8e | 268 | send_msg("Registred %d CBx", v_cb->size() ); |
klauss | 48:195c97f12e8e | 269 | for( register uint8_t i = 0; i < v_cb->size(); i++ ){ |
klauss | 48:195c97f12e8e | 270 | Call_Box * cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 48:195c97f12e8e | 271 | send_msg("Type :: %d -- Ext :: %d -- TimeSlice :: %d -- remain_timeslices :: %d :: v_call->size() :: %d :: Status :: %d :: SIP Status %d", type , cb->get_ext(), cb->get_timeslice(), ts->remain_timeslices(), v_call->size(), cb->status, cb->sip->status ); |
klauss | 30:8dfb6d8de53d | 272 | } |
klauss | 33:735fd60e96d8 | 273 | } |
klauss | 48:195c97f12e8e | 274 | /* |
klauss | 30:8dfb6d8de53d | 275 | if( debug_alive == 1 ){ |
klauss | 48:195c97f12e8e | 276 | if( debug_main ) send_msg("Status - alive "); |
klauss | 30:8dfb6d8de53d | 277 | //uint32_t errper = 100*(pkg_ckserr/(pkg_ckserr + pkg_cksok)); |
klauss | 30:8dfb6d8de53d | 278 | //pc.printf("\n\r Error = %d", errper); |
Cola | 21:0bd688722e81 | 279 | } |
klauss | 48:195c97f12e8e | 280 | */ |
klauss | 0:4d17cd9c8f9d | 281 | t.reset(); |
Cola | 22:d2a4b5939115 | 282 | CAB_LED = !CAB_LED; |
klauss | 27:98f824719d1c | 283 | |
klauss | 48:195c97f12e8e | 284 | if( debug_cks ) send_msg("PKG_CSK OK: %d :: PKG_CSK ERR: %d", pkg_cksok, pkg_ckserr ); |
klauss | 40:990683572031 | 285 | |
klauss | 40:990683572031 | 286 | //<><><><><><><><><><> |
klauss | 43:455522f98de5 | 287 | count++; |
klauss | 49:4837ed2aad6b | 288 | if( !( count % 15 ) ){ |
klauss | 44:cc4996469404 | 289 | if( eth_status ){ |
klauss | 44:cc4996469404 | 290 | wdt.kick(); |
klauss | 48:195c97f12e8e | 291 | //eth_status = __init_eth__( ð ); |
klauss | 48:195c97f12e8e | 292 | eth_status = __init_eth__(); |
klauss | 44:cc4996469404 | 293 | if( eth_status ){ |
klauss | 44:cc4996469404 | 294 | if( debug_main ) debug_msg("Cannot connect to eth"); |
klauss | 44:cc4996469404 | 295 | }else{ |
klauss | 44:cc4996469404 | 296 | if( debug_main ) debug_msg("Connection eth - ok"); |
klauss | 44:cc4996469404 | 297 | init_prompt(); |
klauss | 44:cc4996469404 | 298 | } |
klauss | 44:cc4996469404 | 299 | } |
klauss | 44:cc4996469404 | 300 | } |
klauss | 53:bb492a8f115a | 301 | |
klauss | 54:448d57291be6 | 302 | if( main_test ){ |
klauss | 62:07e5bdc9f8f7 | 303 | data = buffer; |
klauss | 71:757fa1ef37c6 | 304 | ext = test_ext++; |
klauss | 71:757fa1ef37c6 | 305 | port = test_port++; |
klauss | 68:b54993674190 | 306 | //type = __REGISTRY__; |
klauss | 71:757fa1ef37c6 | 307 | /*type = __TELEMETRY__; |
klauss | 68:b54993674190 | 308 | ext = 5108; |
klauss | 68:b54993674190 | 309 | port = 5008; |
klauss | 68:b54993674190 | 310 | uint8_t var = 0xAA; |
klauss | 68:b54993674190 | 311 | for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__; i++ ){ |
klauss | 68:b54993674190 | 312 | data[ i ] = var++; |
klauss | 68:b54993674190 | 313 | if( var == 0xFF ) var = 0xAA; |
klauss | 68:b54993674190 | 314 | } |
klauss | 68:b54993674190 | 315 | for( register uint16_t i = __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__; i < __CB_BUFFER_SIZE__; i++ ){ |
klauss | 68:b54993674190 | 316 | data[ i ] = 0x00; |
klauss | 68:b54993674190 | 317 | } |
klauss | 71:757fa1ef37c6 | 318 | main_test = false;*/ |
klauss | 71:757fa1ef37c6 | 319 | Call_Box * cb = new Call_Box( ext, port ); |
klauss | 71:757fa1ef37c6 | 320 | if( cb != NULL ){ |
klauss | 71:757fa1ef37c6 | 321 | v_cb->add( cb ); |
klauss | 71:757fa1ef37c6 | 322 | cb->set_timeslice( ts->get_timeslice() ); |
klauss | 71:757fa1ef37c6 | 323 | VZ_call * call = new VZ_call( ext, port, 913, 10112 ); |
klauss | 71:757fa1ef37c6 | 324 | v_call->add( call ); |
klauss | 71:757fa1ef37c6 | 325 | } |
klauss | 46:a670f187a704 | 326 | } |
klauss | 53:bb492a8f115a | 327 | |
klauss | 48:195c97f12e8e | 328 | /* |
klauss | 46:a670f187a704 | 329 | if( count == 5 * 2 ){ |
klauss | 46:a670f187a704 | 330 | //debug_msg("Colocar simulacoes de cbx aqui"); |
klauss | 46:a670f187a704 | 331 | data = buffer; |
klauss | 46:a670f187a704 | 332 | ext = 891; |
klauss | 46:a670f187a704 | 333 | port = 5001; |
klauss | 46:a670f187a704 | 334 | type = __INVITE__; |
klauss | 46:a670f187a704 | 335 | debug_msg("registro enviado"); |
klauss | 45:c80574f24dc3 | 336 | }*/ |
klauss | 0:4d17cd9c8f9d | 337 | } |
klauss | 0:4d17cd9c8f9d | 338 | //---------------------- |
klauss | 0:4d17cd9c8f9d | 339 | switch( type ){ |
klauss | 0:4d17cd9c8f9d | 340 | case __DO_NOTHING__ :{} |
klauss | 0:4d17cd9c8f9d | 341 | break; |
klauss | 0:4d17cd9c8f9d | 342 | |
klauss | 0:4d17cd9c8f9d | 343 | case __CB_BYE__ : { |
klauss | 63:0d95da692bb4 | 344 | cb_bye_counter++; |
klauss | 0:4d17cd9c8f9d | 345 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 346 | if( cb != NULL ){ |
klauss | 0:4d17cd9c8f9d | 347 | data[ 0 ] |= BIT7; |
klauss | 0:4d17cd9c8f9d | 348 | cb->set_msg_id( data[ 0 ] ); |
klauss | 30:8dfb6d8de53d | 349 | if( debug_main ) debug_msg( " Request bye from CBx " ); |
klauss | 0:4d17cd9c8f9d | 350 | for( register uint8_t i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 351 | VZ_call * call = (VZ_call *)v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 352 | if( call->get_cb_ext() == ext ){ |
klauss | 4:de46f0d9b14d | 353 | cb->send_bye(); |
klauss | 0:4d17cd9c8f9d | 354 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 355 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 356 | set_status( cb->status, cb_idle ); |
klauss | 13:ae278302dffe | 357 | v_call->remove_element( i ); |
klauss | 13:ae278302dffe | 358 | set_status( cb->sip->status, sip_idle); |
klauss | 0:4d17cd9c8f9d | 359 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 74:81c47fff88a5 | 360 | send2callboxes( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 361 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 13:ae278302dffe | 362 | set_status( cb->sip->status, sip_idle ); |
klauss | 3:cd9148672e25 | 363 | delete( call ); |
klauss | 29:7246460b73f8 | 364 | cb->re_start_timer(); |
klauss | 0:4d17cd9c8f9d | 365 | } |
klauss | 0:4d17cd9c8f9d | 366 | } |
klauss | 0:4d17cd9c8f9d | 367 | } |
klauss | 7:019b08223b87 | 368 | }break; |
klauss | 7:019b08223b87 | 369 | |
klauss | 0:4d17cd9c8f9d | 370 | case __INVITE__ : { |
klauss | 63:0d95da692bb4 | 371 | invite_counter++; |
klauss | 76:9f5f99dd895a | 372 | if( debug_invite ) debug_msg("Request Invite received from Cbx %i", ext); |
klauss | 71:757fa1ef37c6 | 373 | /*procura pelo cbx usando como chave o ramal( ext ) - caso nao encontre, cria o objeto e coloca na logica*/ |
klauss | 0:4d17cd9c8f9d | 374 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 375 | if( cb == NULL ){ |
klauss | 31:bb5fb28a77df | 376 | if( debug_main ) debug_msg("Adding CBx :: %i", ext ); |
klauss | 0:4d17cd9c8f9d | 377 | cb = new Call_Box( ext, port ); |
klauss | 0:4d17cd9c8f9d | 378 | v_cb->add( cb ); |
klauss | 0:4d17cd9c8f9d | 379 | set_status( cb->status, cb_idle ); |
klauss | 0:4d17cd9c8f9d | 380 | cb->registry(); |
klauss | 0:4d17cd9c8f9d | 381 | } |
klauss | 13:ae278302dffe | 382 | cb->reset_elapsed_time(); |
klauss | 76:9f5f99dd895a | 383 | |
klauss | 76:9f5f99dd895a | 384 | invite_handler( v_call, v_cb, ts, cb ); |
klauss | 0:4d17cd9c8f9d | 385 | }break; |
klauss | 4:de46f0d9b14d | 386 | case __REGISTRY__ : { |
klauss | 63:0d95da692bb4 | 387 | registry_counter++; |
klauss | 4:de46f0d9b14d | 388 | Call_Box * cb = NULL; |
klauss | 2:93bec7313ccc | 389 | |
klauss | 4:de46f0d9b14d | 390 | data[ 0 ] |= BIT7 ; |
klauss | 4:de46f0d9b14d | 391 | cb = __find_CB__( v_cb, ext ); |
klauss | 4:de46f0d9b14d | 392 | if( cb != NULL ){ |
klauss | 4:de46f0d9b14d | 393 | cb->reset_elapsed_time(); |
klauss | 30:8dfb6d8de53d | 394 | if( debug_main ) debug_msg(" reset_elapsed_time CBx -- %d", ext ); |
klauss | 0:4d17cd9c8f9d | 395 | }else{ |
klauss | 30:8dfb6d8de53d | 396 | if( debug_main ) debug_msg("Adding Cbx :: %d -- Type :: %d", ext, type ); |
klauss | 4:de46f0d9b14d | 397 | cb = new Call_Box( ext, port ); |
klauss | 4:de46f0d9b14d | 398 | v_cb->add( cb ); |
klauss | 30:8dfb6d8de53d | 399 | if( debug_main ) debug_msg("Added CBx -- %d", ext ); |
klauss | 0:4d17cd9c8f9d | 400 | } |
klauss | 30:8dfb6d8de53d | 401 | if( debug_main ) debug_msg("::registered %d - %d::", ext, port ); |
klauss | 4:de46f0d9b14d | 402 | cb->registry(); |
klauss | 4:de46f0d9b14d | 403 | }break; |
klauss | 4:de46f0d9b14d | 404 | case __BOOT__ : { |
klauss | 63:0d95da692bb4 | 405 | boot_counter++; |
klauss | 74:81c47fff88a5 | 406 | send2callboxes( __build_cb_package__( ext, port, __REGISTRY__, |
klauss | 4:de46f0d9b14d | 407 | ( char * )data, data[ 0 ], __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 4:de46f0d9b14d | 408 | }break; |
klauss | 4:de46f0d9b14d | 409 | case __TELEMETRY__ : { |
klauss | 63:0d95da692bb4 | 410 | telemetry_counter++; |
klauss | 53:bb492a8f115a | 411 | if( debug_telemetry ) send_msg("::telemetry from %d - %d::", ext, port ); |
klauss | 53:bb492a8f115a | 412 | build_telemetry_report( ext, port, ( char *)data ); |
klauss | 4:de46f0d9b14d | 413 | }break; |
klauss | 36:728498a78e1e | 414 | |
klauss | 67:cdedc64d9921 | 415 | case __BOOTLOADER_CBX__ : { |
klauss | 67:cdedc64d9921 | 416 | bootloader_cbx_counter++; |
klauss | 67:cdedc64d9921 | 417 | int ret = bl_cbx_reply_to_eth( ext, ( char * )data ); |
klauss | 67:cdedc64d9921 | 418 | }break; |
klauss | 67:cdedc64d9921 | 419 | |
klauss | 36:728498a78e1e | 420 | case __PROMPT__ : { |
klauss | 63:0d95da692bb4 | 421 | prompt_counter++; |
klauss | 48:195c97f12e8e | 422 | send_msg( "Prompt pkg from ( %i, %i ) ::", ext, port ); |
klauss | 48:195c97f12e8e | 423 | /* |
klauss | 48:195c97f12e8e | 424 | if( tcp_session ){ // udp ? |
klauss | 48:195c97f12e8e | 425 | char tmp[ 255 ]; |
klauss | 48:195c97f12e8e | 426 | sprintf( tmp, "Prompt pkg from ( %i, %i ) :: ", ext, port ); |
klauss | 48:195c97f12e8e | 427 | tcp_client.send_all( tmp, strlen( tmp ) ); |
klauss | 48:195c97f12e8e | 428 | } |
klauss | 48:195c97f12e8e | 429 | */ |
klauss | 62:07e5bdc9f8f7 | 430 | for( register int i = 0; i < 32; i++ ){ |
klauss | 36:728498a78e1e | 431 | pc.printf("%3i ", data[ i ] ); |
klauss | 62:07e5bdc9f8f7 | 432 | if( i == 15 ) pc.printf( "\r\n" ); |
klauss | 48:195c97f12e8e | 433 | } |
klauss | 62:07e5bdc9f8f7 | 434 | pc.printf("\n\r> "); |
klauss | 48:195c97f12e8e | 435 | |
klauss | 48:195c97f12e8e | 436 | if( tcp_session ){ |
klauss | 48:195c97f12e8e | 437 | char aux[ __CB_BUFFER_SIZE__ + 3 ]; |
klauss | 48:195c97f12e8e | 438 | strncpy( aux, (char * )data, __CB_BUFFER_SIZE__ ); |
klauss | 48:195c97f12e8e | 439 | strcat( aux, "\n\r\0" ); |
klauss | 48:195c97f12e8e | 440 | tcp_client.send_all( ( char *)data, strlen( (char * )data ) ); |
klauss | 62:07e5bdc9f8f7 | 441 | tcp_client.send_all( "\r\n> ", strlen( "\r\n> " ) ); |
klauss | 48:195c97f12e8e | 442 | } |
klauss | 36:728498a78e1e | 443 | }break; |
klauss | 0:4d17cd9c8f9d | 444 | case __AUDIO__ : { |
klauss | 63:0d95da692bb4 | 445 | audio_counter++; |
klauss | 0:4d17cd9c8f9d | 446 | Call_Box * cb; |
klauss | 0:4d17cd9c8f9d | 447 | VZ_call * call = NULL; |
klauss | 0:4d17cd9c8f9d | 448 | cb = NULL; |
klauss | 0:4d17cd9c8f9d | 449 | call = __find_Call__( v_call, ext ); |
klauss | 0:4d17cd9c8f9d | 450 | if( call != NULL ){ |
klauss | 30:8dfb6d8de53d | 451 | char * pkg = call->build_eth_package( data + 2 ); |
klauss | 0:4d17cd9c8f9d | 452 | call->send_message( pkg ); |
klauss | 0:4d17cd9c8f9d | 453 | cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 454 | if( cb != NULL ) cb->reset_elapsed_time(); |
klauss | 18:01a93677e40c | 455 | }else{ |
klauss | 30:8dfb6d8de53d | 456 | if( debug_main ) debug_msg("received missed package from CBx :: %i -- Type :: %i", ext, type ); |
klauss | 0:4d17cd9c8f9d | 457 | } |
klauss | 0:4d17cd9c8f9d | 458 | }break; |
klauss | 9:ffa64f38ef9c | 459 | }// fim switch |
klauss | 0:4d17cd9c8f9d | 460 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 461 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 462 | int length = 0; |
klauss | 0:4d17cd9c8f9d | 463 | char * tmp = call->get_eth_message( &length ); |
klauss | 0:4d17cd9c8f9d | 464 | if( tmp != NULL ){ |
klauss | 0:4d17cd9c8f9d | 465 | Call_Box * cb = NULL; |
klauss | 0:4d17cd9c8f9d | 466 | int cb_port = 0xffff; |
klauss | 0:4d17cd9c8f9d | 467 | cb = __find_CB__( v_cb, call->get_cb_ext() ); |
klauss | 4:de46f0d9b14d | 468 | |
klauss | 4:de46f0d9b14d | 469 | if( cb != NULL ){ |
klauss | 4:de46f0d9b14d | 470 | cb_port = cb->get_port(); |
klauss | 30:8dfb6d8de53d | 471 | }else if( debug_main ) debug_msg("received missed package from CBx :: %i -- Type :: %i", ext, type ); |
klauss | 4:de46f0d9b14d | 472 | |
klauss | 0:4d17cd9c8f9d | 473 | uint8_t * pkg2cb = __build_cb_package__( call->get_cb_ext(), cb_port, __AUDIO__, |
klauss | 0:4d17cd9c8f9d | 474 | tmp, __AUDIO__, length, write_buffer ); |
klauss | 4:de46f0d9b14d | 475 | |
klauss | 74:81c47fff88a5 | 476 | send2callboxes( pkg2cb ); |
klauss | 0:4d17cd9c8f9d | 477 | } |
klauss | 14:22a35f575502 | 478 | } |
klauss | 28:94aec56c6329 | 479 | if( v_call->size() == 0 ) registry_aging( v_cb, buffer, write_buffer ); |
klauss | 4:de46f0d9b14d | 480 | |
klauss | 70:714c33487aae | 481 | int ret = sip_manager( v_cb ); |
klauss | 0:4d17cd9c8f9d | 482 | if( ret > 0x00 ){ |
klauss | 0:4d17cd9c8f9d | 483 | Call_Box * cb = __find_CB__( v_cb, ret ); |
klauss | 4:de46f0d9b14d | 484 | if( cb != NULL ){ |
klauss | 4:de46f0d9b14d | 485 | if( cb->status == cb_on_call ){ |
klauss | 4:de46f0d9b14d | 486 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 4:de46f0d9b14d | 487 | cb->set_timeslice( 0x00 ); |
klauss | 4:de46f0d9b14d | 488 | set_status(cb->status,cb_idle); |
klauss | 4:de46f0d9b14d | 489 | buffer[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 4:de46f0d9b14d | 490 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 4:de46f0d9b14d | 491 | for( register uint8_t i = 0; i < v_call->size(); i++ ){ |
klauss | 4:de46f0d9b14d | 492 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 4:de46f0d9b14d | 493 | if( call->get_cb_ext() == ret ){ |
klauss | 4:de46f0d9b14d | 494 | v_call->remove_element( i ); |
klauss | 4:de46f0d9b14d | 495 | delete( call ); |
klauss | 4:de46f0d9b14d | 496 | } |
klauss | 2:93bec7313ccc | 497 | } |
klauss | 74:81c47fff88a5 | 498 | send2callboxes( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 4:de46f0d9b14d | 499 | ( char * )buffer, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 68:b54993674190 | 500 | //FIXMEj talvez eu nao precise dessa linha mudar o set_status( cb->sip->status, sip_idle ); pra ver o que acontece |
klauss | 38:af5ecd6997ab | 501 | //cb->reset_sip(); |
klauss | 13:ae278302dffe | 502 | set_status( cb->sip->status, sip_idle ); |
klauss | 29:7246460b73f8 | 503 | cb->re_start_timer(); |
klauss | 0:4d17cd9c8f9d | 504 | } |
klauss | 30:8dfb6d8de53d | 505 | }else if( debug_main ) debug_msg("Missed bye request from * CBx :: %d -- Type :: %d", ext, type ); |
klauss | 0:4d17cd9c8f9d | 506 | } |
klauss | 13:ae278302dffe | 507 | |
klauss | 10:22da1a0ac1e1 | 508 | call_manager( v_call, v_cb, buffer, write_buffer, ts ); |
klauss | 0:4d17cd9c8f9d | 509 | |
klauss | 76:9f5f99dd895a | 510 | invite_handler( v_call, v_cb, ts, NULL ); |
klauss | 76:9f5f99dd895a | 511 | |
klauss | 71:757fa1ef37c6 | 512 | /* |
klauss | 0:4d17cd9c8f9d | 513 | static uint8_t flag = 0; |
klauss | 0:4d17cd9c8f9d | 514 | if( v_call->size() == 0 ){ |
klauss | 0:4d17cd9c8f9d | 515 | if (flag == 0) { flag = 1; } |
klauss | 0:4d17cd9c8f9d | 516 | ts->reset(); |
klauss | 49:4837ed2aad6b | 517 | }else{ |
klauss | 0:4d17cd9c8f9d | 518 | if (flag == 1) { flag = 0; } |
klauss | 0:4d17cd9c8f9d | 519 | } |
klauss | 71:757fa1ef37c6 | 520 | */ |
klauss | 67:cdedc64d9921 | 521 | |
klauss | 67:cdedc64d9921 | 522 | bl_handler(); |
klauss | 72:895ca792c647 | 523 | tx_buffer_ring_buffer_handler(); |
klauss | 33:735fd60e96d8 | 524 | |
klauss | 30:8dfb6d8de53d | 525 | type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 526 | } |
klauss | 0:4d17cd9c8f9d | 527 | } |