voltando a versao de n aberturas e fechamentos de sockets
Dependencies: EthernetInterface NTPClient mbed-rtos_old mbed
Fork of header_main_public by
main.cpp@3:cd9148672e25, 2014-09-10 (annotated)
- Committer:
- klauss
- Date:
- Wed Sep 10 18:21:48 2014 +0000
- Revision:
- 3:cd9148672e25
- Parent:
- 2:93bec7313ccc
- Child:
- 4:de46f0d9b14d
versao estavel; - Realiza ligacao de 1 cb, master/slave, como musica de conforto e tratamento sip/rtp, desalocacao de recursos.
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" |
klauss | 0:4d17cd9c8f9d | 6 | |
klauss | 0:4d17cd9c8f9d | 7 | #include "debug.h" |
klauss | 0:4d17cd9c8f9d | 8 | #include "utils.h" |
klauss | 0:4d17cd9c8f9d | 9 | |
klauss | 0:4d17cd9c8f9d | 10 | /* |
klauss | 0:4d17cd9c8f9d | 11 | #define set_status(a,b) _set_status(a,b) |
klauss | 0:4d17cd9c8f9d | 12 | #define _set_status(a,b) { \ |
klauss | 0:4d17cd9c8f9d | 13 | a = b; \ |
klauss | 0:4d17cd9c8f9d | 14 | sprintf( debug_msg, "-- State == %d -- Line %d", a, __LINE__ ); \ |
klauss | 0:4d17cd9c8f9d | 15 | debug.sendTo( debug_server, debug_msg, strlen( debug_msg ) ); \ |
klauss | 0:4d17cd9c8f9d | 16 | } |
klauss | 0:4d17cd9c8f9d | 17 | #define whereami() _whereami() |
klauss | 0:4d17cd9c8f9d | 18 | #define _whereami() { \ |
klauss | 0:4d17cd9c8f9d | 19 | sprintf( debug_msg, "-- whereami %d", __LINE__ ); \ |
klauss | 0:4d17cd9c8f9d | 20 | debug.sendTo( debug_server, debug_msg, strlen( debug_msg ) ); \ |
klauss | 0:4d17cd9c8f9d | 21 | } |
klauss | 0:4d17cd9c8f9d | 22 | */ |
klauss | 0:4d17cd9c8f9d | 23 | volatile uint8_t status = 0x00; |
klauss | 0:4d17cd9c8f9d | 24 | |
klauss | 0:4d17cd9c8f9d | 25 | bool debug_bool = true; |
klauss | 0:4d17cd9c8f9d | 26 | |
klauss | 0:4d17cd9c8f9d | 27 | int main(){ |
klauss | 0:4d17cd9c8f9d | 28 | start_cpld(); |
klauss | 0:4d17cd9c8f9d | 29 | config_lpc(); |
klauss | 0:4d17cd9c8f9d | 30 | |
klauss | 0:4d17cd9c8f9d | 31 | /* referencia e inicio da conexao com a ethernet */ |
klauss | 0:4d17cd9c8f9d | 32 | EthernetInterface eth; |
klauss | 0:4d17cd9c8f9d | 33 | __init_eth__( ð ); |
klauss | 0:4d17cd9c8f9d | 34 | |
klauss | 0:4d17cd9c8f9d | 35 | /* debugs */ |
klauss | 2:93bec7313ccc | 36 | // debug_server.set_address( "192.168.120.144", 9897 ); |
klauss | 2:93bec7313ccc | 37 | // debug.bind( 72133 ); |
klauss | 2:93bec7313ccc | 38 | // debug.init(); |
klauss | 0:4d17cd9c8f9d | 39 | |
klauss | 0:4d17cd9c8f9d | 40 | Timer t; |
klauss | 0:4d17cd9c8f9d | 41 | t.start(); |
klauss | 0:4d17cd9c8f9d | 42 | |
klauss | 0:4d17cd9c8f9d | 43 | /* representa ramal do call box */ |
klauss | 0:4d17cd9c8f9d | 44 | int ext = 0; |
klauss | 0:4d17cd9c8f9d | 45 | |
klauss | 0:4d17cd9c8f9d | 46 | /* representa porta do call box */ |
klauss | 0:4d17cd9c8f9d | 47 | int port = 0; |
klauss | 0:4d17cd9c8f9d | 48 | |
klauss | 0:4d17cd9c8f9d | 49 | /* buffer para onde se copia os dados vindos do cb para tratameno interno */ |
klauss | 0:4d17cd9c8f9d | 50 | uint8_t buffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 0:4d17cd9c8f9d | 51 | |
klauss | 0:4d17cd9c8f9d | 52 | /* buffer de escrita do pacote de saida que sera enviado pro cb / servidor */ |
klauss | 0:4d17cd9c8f9d | 53 | uint8_t write_buffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 0:4d17cd9c8f9d | 54 | |
klauss | 0:4d17cd9c8f9d | 55 | /* ponteiro que aponta para os dados vindo do CPLD */ |
klauss | 0:4d17cd9c8f9d | 56 | uint8_t * buffer_from_cb_ptr = ( uint8_t * )RXBuffer; |
klauss | 0:4d17cd9c8f9d | 57 | |
klauss | 0:4d17cd9c8f9d | 58 | /* referencia para os dados contidos no pacote, sem o header */ |
klauss | 0:4d17cd9c8f9d | 59 | uint8_t * data = NULL; |
klauss | 0:4d17cd9c8f9d | 60 | |
klauss | 0:4d17cd9c8f9d | 61 | /* gerencia o tipo do pacote para providenciar tratamento adequado */ |
klauss | 0:4d17cd9c8f9d | 62 | volatile uint8_t type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 63 | |
klauss | 0:4d17cd9c8f9d | 64 | /* representa a lista dos Call Boxes atualmente reconhecidos pela cabeceira */ |
klauss | 0:4d17cd9c8f9d | 65 | Vector * v_cb = new Vector(); |
klauss | 0:4d17cd9c8f9d | 66 | |
klauss | 0:4d17cd9c8f9d | 67 | /* representa a lista de ligacoes ativas na cabeceira */ |
klauss | 0:4d17cd9c8f9d | 68 | Vector * v_call = new Vector(); |
klauss | 0:4d17cd9c8f9d | 69 | |
klauss | 0:4d17cd9c8f9d | 70 | /* gerencia a distribuicao de timeslice para os call boxes */ |
klauss | 0:4d17cd9c8f9d | 71 | Timeslice * ts = new Timeslice(); |
klauss | 0:4d17cd9c8f9d | 72 | |
klauss | 0:4d17cd9c8f9d | 73 | /* tenta instanciar o vetor de call boxes, acende o led 1 antes de encerrar o programa */ |
klauss | 0:4d17cd9c8f9d | 74 | reset_leds(); |
klauss | 0:4d17cd9c8f9d | 75 | if( v_cb == NULL ){ |
klauss | 0:4d17cd9c8f9d | 76 | led1 = 0; |
klauss | 0:4d17cd9c8f9d | 77 | while( v_cb == NULL ){ |
klauss | 0:4d17cd9c8f9d | 78 | Vector * v_cb = new Vector(); |
klauss | 0:4d17cd9c8f9d | 79 | led1 = led1; |
klauss | 0:4d17cd9c8f9d | 80 | if( t.read() > 5 ){ |
klauss | 0:4d17cd9c8f9d | 81 | debug_msg("Erro ao alocar o vetor de CBx"); |
klauss | 0:4d17cd9c8f9d | 82 | t.reset(); |
klauss | 0:4d17cd9c8f9d | 83 | } |
klauss | 0:4d17cd9c8f9d | 84 | } |
klauss | 0:4d17cd9c8f9d | 85 | }else if( v_call == NULL ){ |
klauss | 0:4d17cd9c8f9d | 86 | led2 = 0; |
klauss | 0:4d17cd9c8f9d | 87 | led1 = 1; |
klauss | 0:4d17cd9c8f9d | 88 | while( v_call == NULL ){ |
klauss | 0:4d17cd9c8f9d | 89 | Vector * v_call = new Vector(); |
klauss | 0:4d17cd9c8f9d | 90 | led1 = led1; |
klauss | 0:4d17cd9c8f9d | 91 | if( t.read() > 5 ){ |
klauss | 0:4d17cd9c8f9d | 92 | debug_msg("Erro ao alocar o vetor de Calls"); |
klauss | 0:4d17cd9c8f9d | 93 | t.reset(); |
klauss | 0:4d17cd9c8f9d | 94 | } |
klauss | 0:4d17cd9c8f9d | 95 | } |
klauss | 0:4d17cd9c8f9d | 96 | } |
klauss | 0:4d17cd9c8f9d | 97 | reset_leds(); |
klauss | 0:4d17cd9c8f9d | 98 | debug_msg( " Ready " ); |
klauss | 1:a1758104fa1d | 99 | while( true ){ |
klauss | 0:4d17cd9c8f9d | 100 | if( status != __WAITING__ ){ |
klauss | 0:4d17cd9c8f9d | 101 | __read_cb_buffer__( buffer, buffer_from_cb_ptr ); |
klauss | 0:4d17cd9c8f9d | 102 | status = __WAITING__; |
klauss | 0:4d17cd9c8f9d | 103 | |
klauss | 0:4d17cd9c8f9d | 104 | data = __parse_cb_buffer__( &ext, &port, &type, buffer ); |
klauss | 0:4d17cd9c8f9d | 105 | |
klauss | 3:cd9148672e25 | 106 | if( data != NULL ){ |
klauss | 0:4d17cd9c8f9d | 107 | if( type == __CB_BYE__ ){ |
klauss | 0:4d17cd9c8f9d | 108 | debug_msg("--> %s <--", buffer); |
klauss | 0:4d17cd9c8f9d | 109 | } |
klauss | 0:4d17cd9c8f9d | 110 | if( type != __AUDIO__ ){ |
klauss | 0:4d17cd9c8f9d | 111 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 112 | if( cb != NULL ){ |
klauss | 3:cd9148672e25 | 113 | cb->reset_elapsed_time(); |
klauss | 0:4d17cd9c8f9d | 114 | if( ( data[ 0 ] ^ cb->get_msg_id() ) == BIT7 ){ |
klauss | 0:4d17cd9c8f9d | 115 | // some pkg reply |
klauss | 0:4d17cd9c8f9d | 116 | if( ( data[ 0 ] | BIT7 ) == cb->get_msg_id() ){ |
klauss | 0:4d17cd9c8f9d | 117 | if( type == __BOOT__ ) type = __REGISTRY__; |
klauss | 0:4d17cd9c8f9d | 118 | __send_to_cb__( __build_cb_package__( ext, port, type, |
klauss | 0:4d17cd9c8f9d | 119 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 120 | type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 121 | debug_msg("-- Renviado ultimo pkg --"); |
klauss | 0:4d17cd9c8f9d | 122 | }else if( data[ 0 ] == ( cb->get_msg_id() | BIT7 ) ){ |
klauss | 0:4d17cd9c8f9d | 123 | // ack from CBx |
klauss | 3:cd9148672e25 | 124 | debug_msg("-- ACK do meu pkg --"); |
klauss | 0:4d17cd9c8f9d | 125 | type = __DO_NOTHING__; |
klauss | 3:cd9148672e25 | 126 | debug_msg("morri aqui"); |
klauss | 0:4d17cd9c8f9d | 127 | } |
klauss | 0:4d17cd9c8f9d | 128 | } |
klauss | 0:4d17cd9c8f9d | 129 | } |
klauss | 0:4d17cd9c8f9d | 130 | } |
klauss | 0:4d17cd9c8f9d | 131 | }else type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 132 | } |
klauss | 2:93bec7313ccc | 133 | //---------------------- |
klauss | 0:4d17cd9c8f9d | 134 | if( t.read() > 5 ){ |
klauss | 0:4d17cd9c8f9d | 135 | for( register uint8_t i = 0; i < v_cb->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 136 | Call_Box * cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 137 | 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 | 0:4d17cd9c8f9d | 138 | } |
klauss | 0:4d17cd9c8f9d | 139 | t.reset(); |
klauss | 0:4d17cd9c8f9d | 140 | led3 = !led3; |
klauss | 0:4d17cd9c8f9d | 141 | } |
klauss | 0:4d17cd9c8f9d | 142 | //---------------------- |
klauss | 0:4d17cd9c8f9d | 143 | switch( type ){ |
klauss | 0:4d17cd9c8f9d | 144 | case __DO_NOTHING__ :{} |
klauss | 0:4d17cd9c8f9d | 145 | break; |
klauss | 0:4d17cd9c8f9d | 146 | |
klauss | 0:4d17cd9c8f9d | 147 | case __CB_BYE__ : { |
klauss | 0:4d17cd9c8f9d | 148 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 149 | if( cb != NULL ){ |
klauss | 0:4d17cd9c8f9d | 150 | data[ 0 ] |= BIT7; |
klauss | 0:4d17cd9c8f9d | 151 | cb->set_msg_id( data[ 0 ] ); |
klauss | 0:4d17cd9c8f9d | 152 | debug_msg( " Request bye from CBx " ); |
klauss | 0:4d17cd9c8f9d | 153 | for( register uint8_t i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 154 | VZ_call * call = (VZ_call *)v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 155 | if( call->get_cb_ext() == ext ){ |
klauss | 0:4d17cd9c8f9d | 156 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 157 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 158 | set_status( cb->status, cb_idle ); |
klauss | 0:4d17cd9c8f9d | 159 | v_call->remove_element( i ); |
klauss | 0:4d17cd9c8f9d | 160 | cb->send_bye(); |
klauss | 0:4d17cd9c8f9d | 161 | set_status( cb->sip->status, sip_idle); |
klauss | 0:4d17cd9c8f9d | 162 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 163 | __send_to_cb__( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 164 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 165 | set_status( cb->sip->status, sip_idle ); |
klauss | 3:cd9148672e25 | 166 | delete( call ); |
klauss | 0:4d17cd9c8f9d | 167 | } |
klauss | 0:4d17cd9c8f9d | 168 | } |
klauss | 0:4d17cd9c8f9d | 169 | } |
klauss | 0:4d17cd9c8f9d | 170 | }break; |
klauss | 0:4d17cd9c8f9d | 171 | |
klauss | 0:4d17cd9c8f9d | 172 | case __INVITE__ : { |
klauss | 1:a1758104fa1d | 173 | debug_msg("ok -- recebido o invite pelo menos"); |
klauss | 0:4d17cd9c8f9d | 174 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 175 | if( cb == NULL ){ |
klauss | 0:4d17cd9c8f9d | 176 | cb = new Call_Box( ext, port ); |
klauss | 0:4d17cd9c8f9d | 177 | v_cb->add( cb ); |
klauss | 0:4d17cd9c8f9d | 178 | set_status( cb->status, cb_idle ); |
klauss | 0:4d17cd9c8f9d | 179 | cb->registry(); |
klauss | 0:4d17cd9c8f9d | 180 | } |
klauss | 0:4d17cd9c8f9d | 181 | cb->reset_elapsed_time(); |
klauss | 0:4d17cd9c8f9d | 182 | set_status( cb->status, cb_trying ); |
klauss | 0:4d17cd9c8f9d | 183 | debug_msg(" Ok temos um cbx encontrado com status :: %d", cb->status ); |
klauss | 0:4d17cd9c8f9d | 184 | switch( cb->status ){ |
klauss | 0:4d17cd9c8f9d | 185 | case cb_on_call : { |
klauss | 0:4d17cd9c8f9d | 186 | // a priori, nao fazer nada |
klauss | 0:4d17cd9c8f9d | 187 | //data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 188 | //__send_to_cb__( __build_cb_package__( ext, port, __BYE__, |
klauss | 0:4d17cd9c8f9d | 189 | // ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 190 | }break; |
klauss | 0:4d17cd9c8f9d | 191 | |
klauss | 0:4d17cd9c8f9d | 192 | case cb_ringing : { |
klauss | 0:4d17cd9c8f9d | 193 | // tratar sip 2 |
klauss | 0:4d17cd9c8f9d | 194 | }break; |
klauss | 0:4d17cd9c8f9d | 195 | |
klauss | 0:4d17cd9c8f9d | 196 | case cb_trying : { |
klauss | 0:4d17cd9c8f9d | 197 | data[ 0 ] |= BIT7; |
klauss | 0:4d17cd9c8f9d | 198 | cb->set_msg_id( data[ 0 ] ); |
klauss | 0:4d17cd9c8f9d | 199 | cb->set_timeslice( ts->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 200 | if( cb->get_timeslice() == 0x00 ){ |
klauss | 0:4d17cd9c8f9d | 201 | //sem timeslice pra tratar com o * |
klauss | 0:4d17cd9c8f9d | 202 | set_status(cb->status,cb_idle); |
klauss | 0:4d17cd9c8f9d | 203 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 204 | __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, |
klauss | 0:4d17cd9c8f9d | 205 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 206 | set_status( cb->sip->status, sip_idle); |
klauss | 0:4d17cd9c8f9d | 207 | debug_msg( "-- Trying -- Sem TS -- %s ", write_buffer ); |
klauss | 0:4d17cd9c8f9d | 208 | }else{ |
klauss | 0:4d17cd9c8f9d | 209 | set_status(cb->status,cb_busy); |
klauss | 0:4d17cd9c8f9d | 210 | data[ __TIMESLICE_PLACE__ ] = cb->get_timeslice(); |
klauss | 0:4d17cd9c8f9d | 211 | __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, |
klauss | 0:4d17cd9c8f9d | 212 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 213 | |
klauss | 0:4d17cd9c8f9d | 214 | debug_msg(" -- %d on %d -- ", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] ); |
klauss | 0:4d17cd9c8f9d | 215 | |
klauss | 3:cd9148672e25 | 216 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 0:4d17cd9c8f9d | 217 | VZ_call * call = cb->invite(); |
klauss | 0:4d17cd9c8f9d | 218 | |
klauss | 0:4d17cd9c8f9d | 219 | if( call != NULL ){ |
klauss | 0:4d17cd9c8f9d | 220 | v_call->add( call ); |
klauss | 0:4d17cd9c8f9d | 221 | set_status(cb->status,cb_on_call); |
klauss | 0:4d17cd9c8f9d | 222 | |
klauss | 0:4d17cd9c8f9d | 223 | debug_msg(" -- %d on %d -- ", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] ); |
klauss | 0:4d17cd9c8f9d | 224 | __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, |
klauss | 0:4d17cd9c8f9d | 225 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 226 | debug_msg( "-- Trying -- aceitando pedido de invite -- %s ", write_buffer ); |
klauss | 0:4d17cd9c8f9d | 227 | //debug_pkg( 300, write_buffer ); |
klauss | 0:4d17cd9c8f9d | 228 | }else{ |
klauss | 0:4d17cd9c8f9d | 229 | set_status(cb->status,cb_idle); |
klauss | 0:4d17cd9c8f9d | 230 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 231 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 232 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 233 | debug_msg(" -- %d on %d -- ", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] ); |
klauss | 0:4d17cd9c8f9d | 234 | __send_to_cb__( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 235 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 236 | debug_msg( "-- Trying -- Negando pedido de invite -- %s ", write_buffer ); |
klauss | 0:4d17cd9c8f9d | 237 | set_status( cb->sip->status, sip_idle); |
klauss | 0:4d17cd9c8f9d | 238 | } |
klauss | 0:4d17cd9c8f9d | 239 | } |
klauss | 0:4d17cd9c8f9d | 240 | }break; |
klauss | 0:4d17cd9c8f9d | 241 | |
klauss | 0:4d17cd9c8f9d | 242 | case cb_busy : { |
klauss | 0:4d17cd9c8f9d | 243 | // tratar sip |
klauss | 0:4d17cd9c8f9d | 244 | }break; |
klauss | 0:4d17cd9c8f9d | 245 | } |
klauss | 0:4d17cd9c8f9d | 246 | }break; |
klauss | 0:4d17cd9c8f9d | 247 | case __REGISTRY__ : |
klauss | 0:4d17cd9c8f9d | 248 | { |
klauss | 3:cd9148672e25 | 249 | Call_Box * cb_master = NULL; |
klauss | 3:cd9148672e25 | 250 | Call_Box * cb_slave = NULL; |
klauss | 2:93bec7313ccc | 251 | |
klauss | 0:4d17cd9c8f9d | 252 | data[ 0 ] |= BIT7; |
klauss | 2:93bec7313ccc | 253 | cb_master = __find_CB__( v_cb, ext ); |
klauss | 3:cd9148672e25 | 254 | cb_slave = __find_CB__( v_cb, ext + 1 ); |
klauss | 2:93bec7313ccc | 255 | if( cb_master != NULL ){ |
klauss | 2:93bec7313ccc | 256 | cb_master->reset_elapsed_time(); |
klauss | 3:cd9148672e25 | 257 | }else if( cb_slave != NULL ){ |
klauss | 2:93bec7313ccc | 258 | cb_slave->reset_elapsed_time(); |
klauss | 0:4d17cd9c8f9d | 259 | }else{ |
klauss | 2:93bec7313ccc | 260 | cb_master = new Call_Box( ext, port ); |
klauss | 2:93bec7313ccc | 261 | cb_slave = new Call_Box( ext + 1, port + 1 ); |
klauss | 3:cd9148672e25 | 262 | |
klauss | 2:93bec7313ccc | 263 | v_cb->add( cb_master ); |
klauss | 2:93bec7313ccc | 264 | v_cb->add( cb_slave ); |
klauss | 3:cd9148672e25 | 265 | debug_msg("Registrados"); |
klauss | 0:4d17cd9c8f9d | 266 | } |
klauss | 2:93bec7313ccc | 267 | cb_master->registry(); |
klauss | 2:93bec7313ccc | 268 | cb_slave->registry(); |
klauss | 0:4d17cd9c8f9d | 269 | }break; |
klauss | 0:4d17cd9c8f9d | 270 | case __BOOT__ : |
klauss | 0:4d17cd9c8f9d | 271 | __send_to_cb__( __build_cb_package__( ext, port, __REGISTRY__, |
klauss | 0:4d17cd9c8f9d | 272 | ( char * )data, data[ 0 ], __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 273 | break; |
klauss | 0:4d17cd9c8f9d | 274 | case __TELEMETRY__ : |
klauss | 0:4d17cd9c8f9d | 275 | debug_msg("Leitura da bateria :: ( Id : %x, MSB : %x, LSB : %x )", data[ 0 ], data[ 1 ], data[ 2 ] ); |
klauss | 0:4d17cd9c8f9d | 276 | break; |
klauss | 0:4d17cd9c8f9d | 277 | case __AUDIO__ : { |
klauss | 0:4d17cd9c8f9d | 278 | Call_Box * cb; |
klauss | 0:4d17cd9c8f9d | 279 | VZ_call * call = NULL; |
klauss | 0:4d17cd9c8f9d | 280 | cb = NULL; |
klauss | 0:4d17cd9c8f9d | 281 | call = __find_Call__( v_call, ext ); |
klauss | 0:4d17cd9c8f9d | 282 | if( call != NULL ){ |
klauss | 0:4d17cd9c8f9d | 283 | //debug_( 240, data ); |
klauss | 0:4d17cd9c8f9d | 284 | //debug_pkg( 242, data ); |
klauss | 0:4d17cd9c8f9d | 285 | //char * pkg = call->build_eth_package( oitocentos ); |
klauss | 0:4d17cd9c8f9d | 286 | //char * pkg = call->build_eth_package( data ); |
klauss | 0:4d17cd9c8f9d | 287 | char * pkg = call->build_eth_package( data + 2 ); |
klauss | 0:4d17cd9c8f9d | 288 | call->send_message( pkg ); |
klauss | 0:4d17cd9c8f9d | 289 | cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 290 | if( cb != NULL ) cb->reset_elapsed_time(); |
klauss | 0:4d17cd9c8f9d | 291 | } |
klauss | 0:4d17cd9c8f9d | 292 | }break; |
klauss | 0:4d17cd9c8f9d | 293 | }// fim switch |
klauss | 0:4d17cd9c8f9d | 294 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 295 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 296 | int length = 0; |
klauss | 0:4d17cd9c8f9d | 297 | char * tmp = call->get_eth_message( &length ); |
klauss | 0:4d17cd9c8f9d | 298 | if( tmp != NULL ){ |
klauss | 0:4d17cd9c8f9d | 299 | Call_Box * cb = NULL; |
klauss | 0:4d17cd9c8f9d | 300 | int cb_port = 0xffff; |
klauss | 0:4d17cd9c8f9d | 301 | cb = __find_CB__( v_cb, call->get_cb_ext() ); |
klauss | 0:4d17cd9c8f9d | 302 | if( cb != NULL ) cb_port = cb->get_port(); |
klauss | 0:4d17cd9c8f9d | 303 | uint8_t * pkg2cb = __build_cb_package__( call->get_cb_ext(), cb_port, __AUDIO__, |
klauss | 0:4d17cd9c8f9d | 304 | tmp, __AUDIO__, length, write_buffer ); |
klauss | 0:4d17cd9c8f9d | 305 | __send_to_cb__( pkg2cb ); |
klauss | 0:4d17cd9c8f9d | 306 | } |
klauss | 0:4d17cd9c8f9d | 307 | } |
klauss | 0:4d17cd9c8f9d | 308 | //nessa funcao eu deveria me registar a cada 2 min por exemplo |
klauss | 0:4d17cd9c8f9d | 309 | registry_aging( v_cb, data, write_buffer ); |
klauss | 0:4d17cd9c8f9d | 310 | int ret = sip_manager( v_cb, v_call, write_buffer ); |
klauss | 0:4d17cd9c8f9d | 311 | if( ret > 0x00 ){ |
klauss | 0:4d17cd9c8f9d | 312 | Call_Box * cb = __find_CB__( v_cb, ret ); |
klauss | 0:4d17cd9c8f9d | 313 | if( cb->status == cb_on_call ){ |
klauss | 0:4d17cd9c8f9d | 314 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 315 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 316 | set_status(cb->status,cb_idle); |
klauss | 0:4d17cd9c8f9d | 317 | buffer[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 318 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 0:4d17cd9c8f9d | 319 | for( register uint8_t i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 320 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 2:93bec7313ccc | 321 | if( call->get_cb_ext() == ret ){ |
klauss | 2:93bec7313ccc | 322 | v_call->remove_element( i ); |
klauss | 2:93bec7313ccc | 323 | delete( call ); |
klauss | 2:93bec7313ccc | 324 | } |
klauss | 0:4d17cd9c8f9d | 325 | } |
klauss | 0:4d17cd9c8f9d | 326 | __send_to_cb__( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 327 | ( char * )buffer, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 1:a1758104fa1d | 328 | cb->reset_sip(); |
klauss | 0:4d17cd9c8f9d | 329 | set_status( cb->sip->status, sip_idle ); |
klauss | 0:4d17cd9c8f9d | 330 | } |
klauss | 0:4d17cd9c8f9d | 331 | } |
klauss | 0:4d17cd9c8f9d | 332 | call_manager( v_call, v_cb, data, write_buffer, ts ); |
klauss | 0:4d17cd9c8f9d | 333 | |
klauss | 0:4d17cd9c8f9d | 334 | static uint8_t flag = 0; |
klauss | 0:4d17cd9c8f9d | 335 | if( v_call->size() == 0 ){ |
klauss | 0:4d17cd9c8f9d | 336 | if (flag == 0) { flag = 1; } |
klauss | 0:4d17cd9c8f9d | 337 | ts->reset(); |
klauss | 0:4d17cd9c8f9d | 338 | }else { |
klauss | 0:4d17cd9c8f9d | 339 | if (flag == 1) { flag = 0; } |
klauss | 0:4d17cd9c8f9d | 340 | } |
klauss | 0:4d17cd9c8f9d | 341 | /* eu deveria tirar ts que nao constam em chamadas aqui */ |
klauss | 0:4d17cd9c8f9d | 342 | for( register uint8_t i = 0; i < v_cb->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 343 | Call_Box * cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 344 | if( cb->get_timeslice() != 0x00 ){ |
klauss | 0:4d17cd9c8f9d | 345 | VZ_call * call = __find_Call__( v_call, cb->get_ext() ); |
klauss | 0:4d17cd9c8f9d | 346 | if( call == NULL ){ |
klauss | 0:4d17cd9c8f9d | 347 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 348 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 349 | set_status(cb->status,cb_idle); |
klauss | 0:4d17cd9c8f9d | 350 | } |
klauss | 0:4d17cd9c8f9d | 351 | } |
klauss | 0:4d17cd9c8f9d | 352 | } |
klauss | 0:4d17cd9c8f9d | 353 | type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 354 | } |
klauss | 0:4d17cd9c8f9d | 355 | } |