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