![](/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@1:a1758104fa1d, 2014-09-10 (annotated)
- Committer:
- klauss
- Date:
- Wed Sep 10 12:50:57 2014 +0000
- Revision:
- 1:a1758104fa1d
- Parent:
- 0:4d17cd9c8f9d
- Child:
- 2:93bec7313ccc
This is a test
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 | // for aditional debugs |
klauss | 0:4d17cd9c8f9d | 11 | #define on_work |
klauss | 0:4d17cd9c8f9d | 12 | |
klauss | 0:4d17cd9c8f9d | 13 | /* |
klauss | 0:4d17cd9c8f9d | 14 | #define set_status(a,b) _set_status(a,b) |
klauss | 0:4d17cd9c8f9d | 15 | #define _set_status(a,b) { \ |
klauss | 0:4d17cd9c8f9d | 16 | a = b; \ |
klauss | 0:4d17cd9c8f9d | 17 | sprintf( debug_msg, "-- State == %d -- Line %d", a, __LINE__ ); \ |
klauss | 0:4d17cd9c8f9d | 18 | debug.sendTo( debug_server, debug_msg, strlen( debug_msg ) ); \ |
klauss | 0:4d17cd9c8f9d | 19 | } |
klauss | 0:4d17cd9c8f9d | 20 | #define whereami() _whereami() |
klauss | 0:4d17cd9c8f9d | 21 | #define _whereami() { \ |
klauss | 0:4d17cd9c8f9d | 22 | sprintf( debug_msg, "-- whereami %d", __LINE__ ); \ |
klauss | 0:4d17cd9c8f9d | 23 | debug.sendTo( debug_server, debug_msg, strlen( debug_msg ) ); \ |
klauss | 0:4d17cd9c8f9d | 24 | } |
klauss | 0:4d17cd9c8f9d | 25 | */ |
klauss | 0:4d17cd9c8f9d | 26 | volatile uint8_t status = 0x00; |
klauss | 0:4d17cd9c8f9d | 27 | |
klauss | 0:4d17cd9c8f9d | 28 | bool debug_bool = true; |
klauss | 0:4d17cd9c8f9d | 29 | |
klauss | 0:4d17cd9c8f9d | 30 | int main(){ |
klauss | 0:4d17cd9c8f9d | 31 | start_cpld(); |
klauss | 0:4d17cd9c8f9d | 32 | config_lpc(); |
klauss | 0:4d17cd9c8f9d | 33 | |
klauss | 0:4d17cd9c8f9d | 34 | /* referencia e inicio da conexao com a ethernet */ |
klauss | 0:4d17cd9c8f9d | 35 | EthernetInterface eth; |
klauss | 0:4d17cd9c8f9d | 36 | __init_eth__( ð ); |
klauss | 0:4d17cd9c8f9d | 37 | |
klauss | 0:4d17cd9c8f9d | 38 | /* debugs */ |
klauss | 0:4d17cd9c8f9d | 39 | debug_server.set_address( "192.168.120.144", 9897 ); |
klauss | 0:4d17cd9c8f9d | 40 | debug.bind( 72133 ); |
klauss | 0:4d17cd9c8f9d | 41 | debug.init(); |
klauss | 0:4d17cd9c8f9d | 42 | |
klauss | 0:4d17cd9c8f9d | 43 | Timer t; |
klauss | 0:4d17cd9c8f9d | 44 | Timer reboot; |
klauss | 0:4d17cd9c8f9d | 45 | t.start(); |
klauss | 0:4d17cd9c8f9d | 46 | |
klauss | 0:4d17cd9c8f9d | 47 | /* representa ramal do call box */ |
klauss | 0:4d17cd9c8f9d | 48 | int ext = 0; |
klauss | 0:4d17cd9c8f9d | 49 | |
klauss | 0:4d17cd9c8f9d | 50 | /* representa porta do call box */ |
klauss | 0:4d17cd9c8f9d | 51 | int port = 0; |
klauss | 0:4d17cd9c8f9d | 52 | |
klauss | 0:4d17cd9c8f9d | 53 | /* buffer para onde se copia os dados vindos do cb para tratameno interno */ |
klauss | 0:4d17cd9c8f9d | 54 | uint8_t buffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 0:4d17cd9c8f9d | 55 | |
klauss | 0:4d17cd9c8f9d | 56 | /* buffer de escrita do pacote de saida que sera enviado pro cb / servidor */ |
klauss | 0:4d17cd9c8f9d | 57 | uint8_t write_buffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 0:4d17cd9c8f9d | 58 | |
klauss | 0:4d17cd9c8f9d | 59 | /* ponteiro que aponta para os dados vindo do CPLD */ |
klauss | 0:4d17cd9c8f9d | 60 | uint8_t * buffer_from_cb_ptr = ( uint8_t * )RXBuffer; |
klauss | 0:4d17cd9c8f9d | 61 | |
klauss | 0:4d17cd9c8f9d | 62 | /* referencia para os dados contidos no pacote, sem o header */ |
klauss | 0:4d17cd9c8f9d | 63 | uint8_t * data = NULL; |
klauss | 0:4d17cd9c8f9d | 64 | |
klauss | 0:4d17cd9c8f9d | 65 | /* gerencia o tipo do pacote para providenciar tratamento adequado */ |
klauss | 0:4d17cd9c8f9d | 66 | volatile uint8_t type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 67 | |
klauss | 0:4d17cd9c8f9d | 68 | /* representa a lista dos Call Boxes atualmente reconhecidos pela cabeceira */ |
klauss | 0:4d17cd9c8f9d | 69 | Vector * v_cb = new Vector(); |
klauss | 0:4d17cd9c8f9d | 70 | |
klauss | 0:4d17cd9c8f9d | 71 | /* representa a lista de ligacoes ativas na cabeceira */ |
klauss | 0:4d17cd9c8f9d | 72 | Vector * v_call = new Vector(); |
klauss | 0:4d17cd9c8f9d | 73 | |
klauss | 0:4d17cd9c8f9d | 74 | /* gerencia a distribuicao de timeslice para os call boxes */ |
klauss | 0:4d17cd9c8f9d | 75 | Timeslice * ts = new Timeslice(); |
klauss | 0:4d17cd9c8f9d | 76 | |
klauss | 0:4d17cd9c8f9d | 77 | /* tenta instanciar o vetor de call boxes, acende o led 1 antes de encerrar o programa */ |
klauss | 0:4d17cd9c8f9d | 78 | reset_leds(); |
klauss | 0:4d17cd9c8f9d | 79 | if( v_cb == NULL ){ |
klauss | 0:4d17cd9c8f9d | 80 | led1 = 0; |
klauss | 0:4d17cd9c8f9d | 81 | while( v_cb == NULL ){ |
klauss | 0:4d17cd9c8f9d | 82 | Vector * v_cb = new Vector(); |
klauss | 0:4d17cd9c8f9d | 83 | led1 = led1; |
klauss | 0:4d17cd9c8f9d | 84 | if( t.read() > 5 ){ |
klauss | 0:4d17cd9c8f9d | 85 | debug_msg("Erro ao alocar o vetor de CBx"); |
klauss | 0:4d17cd9c8f9d | 86 | t.reset(); |
klauss | 0:4d17cd9c8f9d | 87 | } |
klauss | 0:4d17cd9c8f9d | 88 | } |
klauss | 0:4d17cd9c8f9d | 89 | }else if( v_call == NULL ){ |
klauss | 0:4d17cd9c8f9d | 90 | led2 = 0; |
klauss | 0:4d17cd9c8f9d | 91 | led1 = 1; |
klauss | 0:4d17cd9c8f9d | 92 | while( v_call == NULL ){ |
klauss | 0:4d17cd9c8f9d | 93 | Vector * v_call = new Vector(); |
klauss | 0:4d17cd9c8f9d | 94 | led1 = led1; |
klauss | 0:4d17cd9c8f9d | 95 | if( t.read() > 5 ){ |
klauss | 0:4d17cd9c8f9d | 96 | debug_msg("Erro ao alocar o vetor de Calls"); |
klauss | 0:4d17cd9c8f9d | 97 | t.reset(); |
klauss | 0:4d17cd9c8f9d | 98 | } |
klauss | 0:4d17cd9c8f9d | 99 | } |
klauss | 0:4d17cd9c8f9d | 100 | } |
klauss | 0:4d17cd9c8f9d | 101 | reset_leds(); |
klauss | 0:4d17cd9c8f9d | 102 | reboot.start(); |
klauss | 0:4d17cd9c8f9d | 103 | debug_msg( " Ready " ); |
klauss | 1:a1758104fa1d | 104 | /* |
klauss | 0:4d17cd9c8f9d | 105 | char test_buffer[400]; |
klauss | 0:4d17cd9c8f9d | 106 | UDPSocket test_sock; |
klauss | 0:4d17cd9c8f9d | 107 | Endpoint test_server; |
klauss | 0:4d17cd9c8f9d | 108 | test_server.set_address( "192.168.120.144" , 12345 ); |
klauss | 0:4d17cd9c8f9d | 109 | test_sock.set_blocking( false, 1 ); |
klauss | 0:4d17cd9c8f9d | 110 | test_sock.bind( 78965 ); |
klauss | 0:4d17cd9c8f9d | 111 | test_sock.init(); |
klauss | 0:4d17cd9c8f9d | 112 | int n = 0; |
klauss | 0:4d17cd9c8f9d | 113 | strcpy( test_buffer, "a"); |
klauss | 0:4d17cd9c8f9d | 114 | test_sock.sendTo( test_server, test_buffer, sizeof( test_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 115 | Timer reconnect; |
klauss | 0:4d17cd9c8f9d | 116 | reconnect.start(); |
klauss | 1:a1758104fa1d | 117 | */ |
klauss | 1:a1758104fa1d | 118 | while( true ){ |
klauss | 1:a1758104fa1d | 119 | /* |
klauss | 0:4d17cd9c8f9d | 120 | n = test_sock.receiveFrom( test_server, test_buffer, sizeof( test_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 121 | if( n > 0 ){ |
klauss | 0:4d17cd9c8f9d | 122 | test_sock.sendTo( test_server, test_buffer, n ); |
klauss | 0:4d17cd9c8f9d | 123 | reconnect.reset(); |
klauss | 0:4d17cd9c8f9d | 124 | }else if( reconnect.read() > 1 ){ |
klauss | 0:4d17cd9c8f9d | 125 | test_sock.close(); |
klauss | 0:4d17cd9c8f9d | 126 | test_sock.bind( 78965 ); |
klauss | 0:4d17cd9c8f9d | 127 | test_sock.init(); |
klauss | 0:4d17cd9c8f9d | 128 | reconnect.reset(); |
klauss | 0:4d17cd9c8f9d | 129 | test_sock.sendTo( test_server, test_buffer, n ); |
klauss | 0:4d17cd9c8f9d | 130 | led4 = !led4; |
klauss | 1:a1758104fa1d | 131 | }*/ |
klauss | 0:4d17cd9c8f9d | 132 | if( status != __WAITING__ ){ |
klauss | 0:4d17cd9c8f9d | 133 | __read_cb_buffer__( buffer, buffer_from_cb_ptr ); |
klauss | 0:4d17cd9c8f9d | 134 | status = __WAITING__; |
klauss | 0:4d17cd9c8f9d | 135 | |
klauss | 0:4d17cd9c8f9d | 136 | data = __parse_cb_buffer__( &ext, &port, &type, buffer ); |
klauss | 0:4d17cd9c8f9d | 137 | |
klauss | 0:4d17cd9c8f9d | 138 | if( data != NULL ){ |
klauss | 0:4d17cd9c8f9d | 139 | |
klauss | 0:4d17cd9c8f9d | 140 | if( type == __CB_BYE__ ){ |
klauss | 0:4d17cd9c8f9d | 141 | debug_msg("--> %s <--", buffer); |
klauss | 0:4d17cd9c8f9d | 142 | } |
klauss | 0:4d17cd9c8f9d | 143 | if( type != __AUDIO__ ){ |
klauss | 0:4d17cd9c8f9d | 144 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 145 | if( cb != NULL ){ |
klauss | 0:4d17cd9c8f9d | 146 | if( ( data[ 0 ] ^ cb->get_msg_id() ) == BIT7 ){ |
klauss | 0:4d17cd9c8f9d | 147 | // some pkg reply |
klauss | 0:4d17cd9c8f9d | 148 | if( ( data[ 0 ] | BIT7 ) == cb->get_msg_id() ){ |
klauss | 0:4d17cd9c8f9d | 149 | if( type == __BOOT__ ) type = __REGISTRY__; |
klauss | 0:4d17cd9c8f9d | 150 | __send_to_cb__( __build_cb_package__( ext, port, type, |
klauss | 0:4d17cd9c8f9d | 151 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 152 | type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 153 | debug_msg("-- Renviado ultimo pkg --"); |
klauss | 0:4d17cd9c8f9d | 154 | }else if( data[ 0 ] == ( cb->get_msg_id() | BIT7 ) ){ |
klauss | 0:4d17cd9c8f9d | 155 | // ack from CBx |
klauss | 0:4d17cd9c8f9d | 156 | type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 157 | debug_msg("-- ACK do meu pkg --"); |
klauss | 0:4d17cd9c8f9d | 158 | } |
klauss | 0:4d17cd9c8f9d | 159 | } |
klauss | 0:4d17cd9c8f9d | 160 | } |
klauss | 0:4d17cd9c8f9d | 161 | } |
klauss | 0:4d17cd9c8f9d | 162 | }else type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 163 | /* |
klauss | 0:4d17cd9c8f9d | 164 | if( data == NULL ) type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 165 | */ |
klauss | 0:4d17cd9c8f9d | 166 | } |
klauss | 0:4d17cd9c8f9d | 167 | //---------------------- |
klauss | 0:4d17cd9c8f9d | 168 | if( t.read() > 5 ){ |
klauss | 0:4d17cd9c8f9d | 169 | for( register uint8_t i = 0; i < v_cb->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 170 | Call_Box * cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 171 | 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 | 172 | } |
klauss | 0:4d17cd9c8f9d | 173 | t.reset(); |
klauss | 0:4d17cd9c8f9d | 174 | led3 = !led3; |
klauss | 0:4d17cd9c8f9d | 175 | } |
klauss | 0:4d17cd9c8f9d | 176 | if( reboot.read() > 180 ){ |
klauss | 0:4d17cd9c8f9d | 177 | for( register uint8_t i = 0; i < v_cb->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 178 | Call_Box * cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 179 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 180 | set_status(cb->status,cb_idle); |
klauss | 0:4d17cd9c8f9d | 181 | set_status( cb->sip->status, sip_idle ); |
klauss | 0:4d17cd9c8f9d | 182 | debug_msg( "Erased >> Type :: %d -- Ext :: %d -- TimeSlice :: %d -- remain_timeslices :: %d :: v_call->size() :: %d :: Status :: %d", type , cb->get_ext(), cb->get_timeslice(), ts->remain_timeslices(), v_call->size(), cb->status ); |
klauss | 0:4d17cd9c8f9d | 183 | } |
klauss | 0:4d17cd9c8f9d | 184 | for( register uint8_t i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 185 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 186 | v_call->remove_element( i ); |
klauss | 0:4d17cd9c8f9d | 187 | delete( call ); |
klauss | 0:4d17cd9c8f9d | 188 | } |
klauss | 0:4d17cd9c8f9d | 189 | reboot.reset(); |
klauss | 0:4d17cd9c8f9d | 190 | led4 = !led4; |
klauss | 0:4d17cd9c8f9d | 191 | } |
klauss | 0:4d17cd9c8f9d | 192 | //---------------------- |
klauss | 0:4d17cd9c8f9d | 193 | switch( type ){ |
klauss | 0:4d17cd9c8f9d | 194 | case __DO_NOTHING__ :{} |
klauss | 0:4d17cd9c8f9d | 195 | break; |
klauss | 0:4d17cd9c8f9d | 196 | |
klauss | 0:4d17cd9c8f9d | 197 | case __CB_BYE__ : { |
klauss | 0:4d17cd9c8f9d | 198 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 199 | if( cb != NULL ){ |
klauss | 0:4d17cd9c8f9d | 200 | data[ 0 ] |= BIT7; |
klauss | 0:4d17cd9c8f9d | 201 | cb->set_msg_id( data[ 0 ] ); |
klauss | 0:4d17cd9c8f9d | 202 | debug_msg( " Request bye from CBx " ); |
klauss | 0:4d17cd9c8f9d | 203 | for( register uint8_t i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 204 | VZ_call * call = (VZ_call *)v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 205 | if( call->get_cb_ext() == ext ){ |
klauss | 0:4d17cd9c8f9d | 206 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 207 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 208 | set_status( cb->status, cb_idle ); |
klauss | 0:4d17cd9c8f9d | 209 | v_call->remove_element( i ); |
klauss | 0:4d17cd9c8f9d | 210 | cb->send_bye(); |
klauss | 0:4d17cd9c8f9d | 211 | set_status( cb->sip->status, sip_idle); |
klauss | 0:4d17cd9c8f9d | 212 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 213 | __send_to_cb__( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 214 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 215 | set_status( cb->sip->status, sip_idle ); |
klauss | 0:4d17cd9c8f9d | 216 | } |
klauss | 0:4d17cd9c8f9d | 217 | } |
klauss | 0:4d17cd9c8f9d | 218 | } |
klauss | 0:4d17cd9c8f9d | 219 | }break; |
klauss | 0:4d17cd9c8f9d | 220 | |
klauss | 0:4d17cd9c8f9d | 221 | case __INVITE__ : { |
klauss | 1:a1758104fa1d | 222 | debug_msg("ok -- recebido o invite pelo menos"); |
klauss | 0:4d17cd9c8f9d | 223 | #ifndef on_work |
klauss | 0:4d17cd9c8f9d | 224 | // invite de uma ligacao em andamento ? |
klauss | 0:4d17cd9c8f9d | 225 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 226 | if( cb != NULL ){ |
klauss | 0:4d17cd9c8f9d | 227 | cb->reset_elapsed_time(); |
klauss | 0:4d17cd9c8f9d | 228 | if( cb->status == cb_on_call ){ |
klauss | 0:4d17cd9c8f9d | 229 | VZ_call * vz = __find_Call__( v_call, cb->get_ext() ); |
klauss | 0:4d17cd9c8f9d | 230 | if( vz != NULL ) break; |
klauss | 0:4d17cd9c8f9d | 231 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 232 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 233 | set_status(cb->status,cb_idle); |
klauss | 0:4d17cd9c8f9d | 234 | } |
klauss | 0:4d17cd9c8f9d | 235 | }else{ |
klauss | 0:4d17cd9c8f9d | 236 | cb = new Call_Box( ext, port ); |
klauss | 0:4d17cd9c8f9d | 237 | v_cb->add( cb ); |
klauss | 0:4d17cd9c8f9d | 238 | } |
klauss | 0:4d17cd9c8f9d | 239 | if( cb->status != cb_on_call ) set_status(cb->status,cb_ringing); |
klauss | 0:4d17cd9c8f9d | 240 | if( data[ __TIMESLICE_PLACE__ ] == __CB_BYE__ ){ |
klauss | 0:4d17cd9c8f9d | 241 | if( cb->status == cb_on_call ){ |
klauss | 0:4d17cd9c8f9d | 242 | for( register uint8_t i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 243 | if( ( (VZ_call *)v_call->get_element( i ) )->get_cb_ext() == ext ){ |
klauss | 0:4d17cd9c8f9d | 244 | v_call->remove_element( i ); |
klauss | 0:4d17cd9c8f9d | 245 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 246 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 247 | set_status(cb->status,cb_idle); |
klauss | 0:4d17cd9c8f9d | 248 | cb->send_bye(); |
klauss | 0:4d17cd9c8f9d | 249 | break; |
klauss | 0:4d17cd9c8f9d | 250 | } |
klauss | 0:4d17cd9c8f9d | 251 | } |
klauss | 0:4d17cd9c8f9d | 252 | } |
klauss | 0:4d17cd9c8f9d | 253 | } |
klauss | 0:4d17cd9c8f9d | 254 | if( cb->status == cb_ringing ){ |
klauss | 0:4d17cd9c8f9d | 255 | data[ 0 ] |= BIT7; |
klauss | 0:4d17cd9c8f9d | 256 | cb->set_msg_id( data[ 0 ] ); |
klauss | 0:4d17cd9c8f9d | 257 | cb->set_timeslice( ts->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 258 | if( cb->get_timeslice() == 0x00 ){ |
klauss | 0:4d17cd9c8f9d | 259 | set_status(cb->status,cb_idle); |
klauss | 0:4d17cd9c8f9d | 260 | data[ __CLOCK_SYNC_SIZE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 261 | __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, |
klauss | 0:4d17cd9c8f9d | 262 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 263 | break; |
klauss | 0:4d17cd9c8f9d | 264 | } |
klauss | 0:4d17cd9c8f9d | 265 | if( cb->status == cb_ringing ){ |
klauss | 0:4d17cd9c8f9d | 266 | data[ __CLOCK_SYNC_SIZE__ ] = cb->get_timeslice(); |
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 | 0:4d17cd9c8f9d | 269 | |
klauss | 0:4d17cd9c8f9d | 270 | VZ_call * call = cb->invite(); |
klauss | 0:4d17cd9c8f9d | 271 | |
klauss | 0:4d17cd9c8f9d | 272 | if( call != NULL ){ |
klauss | 0:4d17cd9c8f9d | 273 | v_call->add( call ); |
klauss | 0:4d17cd9c8f9d | 274 | set_status(cb->status,cb_on_call); |
klauss | 0:4d17cd9c8f9d | 275 | data[ __CLOCK_SYNC_SIZE__ ] = cb->get_timeslice(); |
klauss | 0:4d17cd9c8f9d | 276 | __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, |
klauss | 0:4d17cd9c8f9d | 277 | ( char * )data, cb->get_msg_id()+1, __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 278 | }else{ |
klauss | 0:4d17cd9c8f9d | 279 | set_status(cb->status,cb_idle); |
klauss | 0:4d17cd9c8f9d | 280 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 281 | data[ __CLOCK_SYNC_SIZE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 282 | __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, |
klauss | 0:4d17cd9c8f9d | 283 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 284 | } |
klauss | 0:4d17cd9c8f9d | 285 | } |
klauss | 0:4d17cd9c8f9d | 286 | } |
klauss | 0:4d17cd9c8f9d | 287 | }break; |
klauss | 0:4d17cd9c8f9d | 288 | #endif |
klauss | 0:4d17cd9c8f9d | 289 | #ifdef on_work |
klauss | 0:4d17cd9c8f9d | 290 | Call_Box * cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 291 | if( cb == NULL ){ |
klauss | 0:4d17cd9c8f9d | 292 | cb = new Call_Box( ext, port ); |
klauss | 0:4d17cd9c8f9d | 293 | v_cb->add( cb ); |
klauss | 0:4d17cd9c8f9d | 294 | set_status( cb->status, cb_idle ); |
klauss | 0:4d17cd9c8f9d | 295 | cb->registry(); |
klauss | 0:4d17cd9c8f9d | 296 | } |
klauss | 0:4d17cd9c8f9d | 297 | cb->reset_elapsed_time(); |
klauss | 0:4d17cd9c8f9d | 298 | set_status( cb->status, cb_trying ); |
klauss | 0:4d17cd9c8f9d | 299 | debug_msg(" Ok temos um cbx encontrado com status :: %d", cb->status ); |
klauss | 0:4d17cd9c8f9d | 300 | switch( cb->status ){ |
klauss | 0:4d17cd9c8f9d | 301 | case cb_on_call : { |
klauss | 0:4d17cd9c8f9d | 302 | // a priori, nao fazer nada |
klauss | 0:4d17cd9c8f9d | 303 | //data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 304 | //__send_to_cb__( __build_cb_package__( ext, port, __BYE__, |
klauss | 0:4d17cd9c8f9d | 305 | // ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 306 | }break; |
klauss | 0:4d17cd9c8f9d | 307 | |
klauss | 0:4d17cd9c8f9d | 308 | case cb_ringing : { |
klauss | 0:4d17cd9c8f9d | 309 | // tratar sip 2 |
klauss | 0:4d17cd9c8f9d | 310 | }break; |
klauss | 0:4d17cd9c8f9d | 311 | |
klauss | 0:4d17cd9c8f9d | 312 | case cb_trying : { |
klauss | 0:4d17cd9c8f9d | 313 | data[ 0 ] |= BIT7; |
klauss | 0:4d17cd9c8f9d | 314 | cb->set_msg_id( data[ 0 ] ); |
klauss | 0:4d17cd9c8f9d | 315 | cb->set_timeslice( ts->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 316 | if( cb->get_timeslice() == 0x00 ){ |
klauss | 0:4d17cd9c8f9d | 317 | //sem timeslice pra tratar com o * |
klauss | 0:4d17cd9c8f9d | 318 | set_status(cb->status,cb_idle); |
klauss | 0:4d17cd9c8f9d | 319 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 320 | __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, |
klauss | 0:4d17cd9c8f9d | 321 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 322 | set_status( cb->sip->status, sip_idle); |
klauss | 0:4d17cd9c8f9d | 323 | debug_msg( "-- Trying -- Sem TS -- %s ", write_buffer ); |
klauss | 0:4d17cd9c8f9d | 324 | }else{ |
klauss | 0:4d17cd9c8f9d | 325 | set_status(cb->status,cb_busy); |
klauss | 0:4d17cd9c8f9d | 326 | data[ __TIMESLICE_PLACE__ ] = cb->get_timeslice(); |
klauss | 0:4d17cd9c8f9d | 327 | __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, |
klauss | 0:4d17cd9c8f9d | 328 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 329 | |
klauss | 0:4d17cd9c8f9d | 330 | debug_msg(" -- %d on %d -- ", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] ); |
klauss | 0:4d17cd9c8f9d | 331 | |
klauss | 0:4d17cd9c8f9d | 332 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 0:4d17cd9c8f9d | 333 | VZ_call * call = cb->invite(); |
klauss | 0:4d17cd9c8f9d | 334 | |
klauss | 0:4d17cd9c8f9d | 335 | if( call != NULL ){ |
klauss | 0:4d17cd9c8f9d | 336 | v_call->add( call ); |
klauss | 0:4d17cd9c8f9d | 337 | set_status(cb->status,cb_on_call); |
klauss | 0:4d17cd9c8f9d | 338 | |
klauss | 0:4d17cd9c8f9d | 339 | debug_msg(" -- %d on %d -- ", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] ); |
klauss | 0:4d17cd9c8f9d | 340 | __send_to_cb__( __build_cb_package__( ext, port, __INVITE__, |
klauss | 0:4d17cd9c8f9d | 341 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 342 | debug_msg( "-- Trying -- aceitando pedido de invite -- %s ", write_buffer ); |
klauss | 0:4d17cd9c8f9d | 343 | //debug_pkg( 300, write_buffer ); |
klauss | 0:4d17cd9c8f9d | 344 | }else{ |
klauss | 0:4d17cd9c8f9d | 345 | set_status(cb->status,cb_idle); |
klauss | 0:4d17cd9c8f9d | 346 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 347 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 348 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 349 | debug_msg(" -- %d on %d -- ", cb->get_ext(), data[ __TIMESLICE_PLACE__ ] ); |
klauss | 0:4d17cd9c8f9d | 350 | __send_to_cb__( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 351 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 352 | debug_msg( "-- Trying -- Negando pedido de invite -- %s ", write_buffer ); |
klauss | 0:4d17cd9c8f9d | 353 | set_status( cb->sip->status, sip_idle); |
klauss | 0:4d17cd9c8f9d | 354 | } |
klauss | 0:4d17cd9c8f9d | 355 | } |
klauss | 0:4d17cd9c8f9d | 356 | }break; |
klauss | 0:4d17cd9c8f9d | 357 | |
klauss | 0:4d17cd9c8f9d | 358 | case cb_busy : { |
klauss | 0:4d17cd9c8f9d | 359 | // tratar sip |
klauss | 0:4d17cd9c8f9d | 360 | }break; |
klauss | 0:4d17cd9c8f9d | 361 | } |
klauss | 0:4d17cd9c8f9d | 362 | }break; |
klauss | 0:4d17cd9c8f9d | 363 | #endif |
klauss | 0:4d17cd9c8f9d | 364 | case __REGISTRY__ : |
klauss | 0:4d17cd9c8f9d | 365 | { |
klauss | 0:4d17cd9c8f9d | 366 | Call_Box * cb; |
klauss | 0:4d17cd9c8f9d | 367 | data[ 0 ] |= BIT7; |
klauss | 0:4d17cd9c8f9d | 368 | cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 369 | if( cb != NULL ){ |
klauss | 0:4d17cd9c8f9d | 370 | cb->reset_elapsed_time(); |
klauss | 0:4d17cd9c8f9d | 371 | }else{ |
klauss | 0:4d17cd9c8f9d | 372 | cb = new Call_Box( ext, port ); |
klauss | 0:4d17cd9c8f9d | 373 | v_cb->add( cb ); |
klauss | 0:4d17cd9c8f9d | 374 | } |
klauss | 0:4d17cd9c8f9d | 375 | cb->registry(); |
klauss | 0:4d17cd9c8f9d | 376 | }break; |
klauss | 0:4d17cd9c8f9d | 377 | case __BOOT__ : |
klauss | 0:4d17cd9c8f9d | 378 | __send_to_cb__( __build_cb_package__( ext, port, __REGISTRY__, |
klauss | 0:4d17cd9c8f9d | 379 | ( char * )data, data[ 0 ], __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 380 | break; |
klauss | 0:4d17cd9c8f9d | 381 | case __TELEMETRY__ : |
klauss | 0:4d17cd9c8f9d | 382 | debug_msg("Leitura da bateria :: ( Id : %x, MSB : %x, LSB : %x )", data[ 0 ], data[ 1 ], data[ 2 ] ); |
klauss | 0:4d17cd9c8f9d | 383 | break; |
klauss | 0:4d17cd9c8f9d | 384 | case __AUDIO__ : { |
klauss | 0:4d17cd9c8f9d | 385 | Call_Box * cb; |
klauss | 0:4d17cd9c8f9d | 386 | VZ_call * call = NULL; |
klauss | 0:4d17cd9c8f9d | 387 | cb = NULL; |
klauss | 0:4d17cd9c8f9d | 388 | call = __find_Call__( v_call, ext ); |
klauss | 0:4d17cd9c8f9d | 389 | if( call != NULL ){ |
klauss | 0:4d17cd9c8f9d | 390 | //debug_( 240, data ); |
klauss | 0:4d17cd9c8f9d | 391 | //debug_pkg( 242, data ); |
klauss | 0:4d17cd9c8f9d | 392 | //char * pkg = call->build_eth_package( oitocentos ); |
klauss | 0:4d17cd9c8f9d | 393 | //char * pkg = call->build_eth_package( data ); |
klauss | 0:4d17cd9c8f9d | 394 | char * pkg = call->build_eth_package( data + 2 ); |
klauss | 0:4d17cd9c8f9d | 395 | call->send_message( pkg ); |
klauss | 0:4d17cd9c8f9d | 396 | cb = __find_CB__( v_cb, ext ); |
klauss | 0:4d17cd9c8f9d | 397 | if( cb != NULL ) cb->reset_elapsed_time(); |
klauss | 0:4d17cd9c8f9d | 398 | } |
klauss | 0:4d17cd9c8f9d | 399 | }break; |
klauss | 0:4d17cd9c8f9d | 400 | }// fim switch |
klauss | 0:4d17cd9c8f9d | 401 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 402 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 403 | int length = 0; |
klauss | 0:4d17cd9c8f9d | 404 | char * tmp = call->get_eth_message( &length ); |
klauss | 0:4d17cd9c8f9d | 405 | if( tmp != NULL ){ |
klauss | 0:4d17cd9c8f9d | 406 | Call_Box * cb = NULL; |
klauss | 0:4d17cd9c8f9d | 407 | int cb_port = 0xffff; |
klauss | 0:4d17cd9c8f9d | 408 | cb = __find_CB__( v_cb, call->get_cb_ext() ); |
klauss | 0:4d17cd9c8f9d | 409 | if( cb != NULL ) cb_port = cb->get_port(); |
klauss | 0:4d17cd9c8f9d | 410 | uint8_t * pkg2cb = __build_cb_package__( call->get_cb_ext(), cb_port, __AUDIO__, |
klauss | 0:4d17cd9c8f9d | 411 | tmp, __AUDIO__, length, write_buffer ); |
klauss | 0:4d17cd9c8f9d | 412 | __send_to_cb__( pkg2cb ); |
klauss | 0:4d17cd9c8f9d | 413 | } |
klauss | 0:4d17cd9c8f9d | 414 | } |
klauss | 0:4d17cd9c8f9d | 415 | //nessa funcao eu deveria me registar a cada 2 min por exemplo |
klauss | 0:4d17cd9c8f9d | 416 | registry_aging( v_cb, data, write_buffer ); |
klauss | 0:4d17cd9c8f9d | 417 | int ret = sip_manager( v_cb, v_call, write_buffer ); |
klauss | 0:4d17cd9c8f9d | 418 | if( ret > 0x00 ){ |
klauss | 0:4d17cd9c8f9d | 419 | Call_Box * cb = __find_CB__( v_cb, ret ); |
klauss | 0:4d17cd9c8f9d | 420 | if( cb->status == cb_on_call ){ |
klauss | 0:4d17cd9c8f9d | 421 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 422 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 423 | set_status(cb->status,cb_idle); |
klauss | 0:4d17cd9c8f9d | 424 | buffer[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 425 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 0:4d17cd9c8f9d | 426 | for( register uint8_t i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 427 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 428 | v_call->remove_element( i ); |
klauss | 0:4d17cd9c8f9d | 429 | } |
klauss | 0:4d17cd9c8f9d | 430 | __send_to_cb__( __build_cb_package__( ext, port, __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 431 | ( char * )buffer, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 1:a1758104fa1d | 432 | cb->reset_sip(); |
klauss | 0:4d17cd9c8f9d | 433 | set_status( cb->sip->status, sip_idle ); |
klauss | 0:4d17cd9c8f9d | 434 | } |
klauss | 0:4d17cd9c8f9d | 435 | } |
klauss | 0:4d17cd9c8f9d | 436 | call_manager( v_call, v_cb, data, write_buffer, ts ); |
klauss | 0:4d17cd9c8f9d | 437 | |
klauss | 0:4d17cd9c8f9d | 438 | static uint8_t flag = 0; |
klauss | 0:4d17cd9c8f9d | 439 | if( v_call->size() == 0 ){ |
klauss | 0:4d17cd9c8f9d | 440 | if (flag == 0) { flag = 1; } |
klauss | 0:4d17cd9c8f9d | 441 | ts->reset(); |
klauss | 0:4d17cd9c8f9d | 442 | }else { |
klauss | 0:4d17cd9c8f9d | 443 | if (flag == 1) { flag = 0; } |
klauss | 0:4d17cd9c8f9d | 444 | } |
klauss | 0:4d17cd9c8f9d | 445 | /* eu deveria tirar ts que nao constam em chamadas aqui */ |
klauss | 0:4d17cd9c8f9d | 446 | for( register uint8_t i = 0; i < v_cb->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 447 | Call_Box * cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 448 | if( cb->get_timeslice() != 0x00 ){ |
klauss | 0:4d17cd9c8f9d | 449 | VZ_call * call = __find_Call__( v_call, cb->get_ext() ); |
klauss | 0:4d17cd9c8f9d | 450 | if( call == NULL ){ |
klauss | 0:4d17cd9c8f9d | 451 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 452 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 453 | set_status(cb->status,cb_idle); |
klauss | 0:4d17cd9c8f9d | 454 | } |
klauss | 0:4d17cd9c8f9d | 455 | } |
klauss | 0:4d17cd9c8f9d | 456 | } |
klauss | 0:4d17cd9c8f9d | 457 | type = __DO_NOTHING__; |
klauss | 0:4d17cd9c8f9d | 458 | } |
klauss | 0:4d17cd9c8f9d | 459 | } |