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