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