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