![](/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
utils.cpp@16:b45b5b7363d7, 2014-09-18 (annotated)
- Committer:
- klauss
- Date:
- Thu Sep 18 13:59:58 2014 +0000
- Revision:
- 16:b45b5b7363d7
- Parent:
- 15:9c7456c1b6f4
- Child:
- 17:67a6b557eda5
versao do firmware instalado em pista em 2014/09/17 -- registrando 4 equipamentos cbx, aceitando concluindo negociacao SIP, transferencia de audio unilateral, audio header->CBx ok, porem, audio CBx->header ausente
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 0:4d17cd9c8f9d | 1 | #include "utils.h" |
klauss | 0:4d17cd9c8f9d | 2 | |
klauss | 0:4d17cd9c8f9d | 3 | void reverse( char str[], int length ){ |
klauss | 0:4d17cd9c8f9d | 4 | int start = 0; |
klauss | 0:4d17cd9c8f9d | 5 | int end = length -1; |
klauss | 0:4d17cd9c8f9d | 6 | while (start < end){ |
klauss | 0:4d17cd9c8f9d | 7 | swap(*(str+start), *(str+end)); |
klauss | 0:4d17cd9c8f9d | 8 | start++; |
klauss | 0:4d17cd9c8f9d | 9 | end--; |
klauss | 0:4d17cd9c8f9d | 10 | } |
klauss | 0:4d17cd9c8f9d | 11 | } |
klauss | 0:4d17cd9c8f9d | 12 | char* itoa(int num, char* str, int base){ |
klauss | 0:4d17cd9c8f9d | 13 | int i = 0; |
klauss | 0:4d17cd9c8f9d | 14 | bool isNegative = false; |
klauss | 0:4d17cd9c8f9d | 15 | |
klauss | 0:4d17cd9c8f9d | 16 | if( num == 0 ){ |
klauss | 0:4d17cd9c8f9d | 17 | str[i++] = '0'; |
klauss | 0:4d17cd9c8f9d | 18 | str[i] = '\0'; |
klauss | 0:4d17cd9c8f9d | 19 | return str; |
klauss | 0:4d17cd9c8f9d | 20 | } |
klauss | 0:4d17cd9c8f9d | 21 | if( num < 0 && base == 10 ){ |
klauss | 0:4d17cd9c8f9d | 22 | isNegative = true; |
klauss | 0:4d17cd9c8f9d | 23 | num = -num; |
klauss | 0:4d17cd9c8f9d | 24 | } |
klauss | 0:4d17cd9c8f9d | 25 | while( num != 0 ){ |
klauss | 0:4d17cd9c8f9d | 26 | int rem = num % base; |
klauss | 0:4d17cd9c8f9d | 27 | str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0'; |
klauss | 0:4d17cd9c8f9d | 28 | num = num/base; |
klauss | 0:4d17cd9c8f9d | 29 | } |
klauss | 0:4d17cd9c8f9d | 30 | if( isNegative ) |
klauss | 0:4d17cd9c8f9d | 31 | str[i++] = '-'; |
klauss | 0:4d17cd9c8f9d | 32 | |
klauss | 0:4d17cd9c8f9d | 33 | str[i] = '\0'; // Append string terminator |
klauss | 0:4d17cd9c8f9d | 34 | |
klauss | 0:4d17cd9c8f9d | 35 | reverse( str, i ); |
klauss | 0:4d17cd9c8f9d | 36 | return( str ); |
klauss | 0:4d17cd9c8f9d | 37 | } |
klauss | 0:4d17cd9c8f9d | 38 | |
klauss | 0:4d17cd9c8f9d | 39 | void reset_leds( void ){ |
klauss | 0:4d17cd9c8f9d | 40 | led1 = led2 = 1; |
klauss | 0:4d17cd9c8f9d | 41 | led3 = led4 = 0; |
klauss | 0:4d17cd9c8f9d | 42 | } |
klauss | 0:4d17cd9c8f9d | 43 | |
klauss | 8:e3bfe62a477e | 44 | int __init_eth__( EthernetInterface * eth ){ |
klauss | 15:9c7456c1b6f4 | 45 | //eth->init( __MY_IP__, "255.255.255.0", "192.168.120.1" ); |
klauss | 16:b45b5b7363d7 | 46 | eth->init( "192.168.2.200", "255.255.255.0", "192.168.2.254" ); |
klauss | 15:9c7456c1b6f4 | 47 | |
klauss | 15:9c7456c1b6f4 | 48 | /* |
klauss | 15:9c7456c1b6f4 | 49 | #ifdef debug_colinas |
klauss | 15:9c7456c1b6f4 | 50 | #define __MY_IP__ "192.168.2.200" |
klauss | 15:9c7456c1b6f4 | 51 | #define __MY_GT__ "192.168.2.254" |
klauss | 15:9c7456c1b6f4 | 52 | #endif |
klauss | 15:9c7456c1b6f4 | 53 | #ifndef debug_colinas |
klauss | 15:9c7456c1b6f4 | 54 | #define __MY_IP__ "192.168.120.169" |
klauss | 15:9c7456c1b6f4 | 55 | #define __MY_GT__ "192.168.120.1" |
klauss | 15:9c7456c1b6f4 | 56 | #endif |
klauss | 15:9c7456c1b6f4 | 57 | |
klauss | 15:9c7456c1b6f4 | 58 | #define __MY_MK__ "255.255.255.0" |
klauss | 15:9c7456c1b6f4 | 59 | */ |
klauss | 8:e3bfe62a477e | 60 | return eth->connect(); |
klauss | 0:4d17cd9c8f9d | 61 | // pensar em o que acontece e como sinalizar que nao consegui conectar na web |
klauss | 0:4d17cd9c8f9d | 62 | // aparente so da time out, so que algo vai travar quando eu tentar usar a conexao |
klauss | 0:4d17cd9c8f9d | 63 | } |
klauss | 0:4d17cd9c8f9d | 64 | |
klauss | 0:4d17cd9c8f9d | 65 | Call_Box * __find_CB__( Vector * v_cb, int ext ){ |
klauss | 0:4d17cd9c8f9d | 66 | Call_Box * cb = NULL; |
klauss | 0:4d17cd9c8f9d | 67 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 68 | cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 69 | if( cb->get_ext() == ext ) return( cb ); |
klauss | 0:4d17cd9c8f9d | 70 | } |
klauss | 0:4d17cd9c8f9d | 71 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 72 | } |
klauss | 0:4d17cd9c8f9d | 73 | |
klauss | 0:4d17cd9c8f9d | 74 | VZ_call * __find_Call__( Vector * v_call, int ext ){ |
klauss | 0:4d17cd9c8f9d | 75 | VZ_call * call = NULL; |
klauss | 0:4d17cd9c8f9d | 76 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 77 | call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 78 | if( call->get_cb_ext() == ext ) return( call ); |
klauss | 0:4d17cd9c8f9d | 79 | } |
klauss | 0:4d17cd9c8f9d | 80 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 81 | } |
klauss | 0:4d17cd9c8f9d | 82 | |
klauss | 0:4d17cd9c8f9d | 83 | void registry_aging( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ |
klauss | 7:019b08223b87 | 84 | Call_Box * cb = NULL; |
klauss | 7:019b08223b87 | 85 | if( v_cb && data && write_buffer ){ |
klauss | 7:019b08223b87 | 86 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 7:019b08223b87 | 87 | cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 7:019b08223b87 | 88 | if( cb->is_timeout() ){ |
klauss | 7:019b08223b87 | 89 | if( cb->reconfigure_timeout() == 0x00 ){ |
klauss | 15:9c7456c1b6f4 | 90 | // debug_msg("( ext %d removed", cb->get_ext() ); |
klauss | 9:ffa64f38ef9c | 91 | //cb->unregistry(); |
klauss | 9:ffa64f38ef9c | 92 | //v_cb->remove_element( i ); |
klauss | 9:ffa64f38ef9c | 93 | //delete( cb ); |
klauss | 7:019b08223b87 | 94 | break; |
klauss | 7:019b08223b87 | 95 | }else{ |
klauss | 15:9c7456c1b6f4 | 96 | // debug_msg("Ping on %d", cb->get_ext() ); |
klauss | 14:22a35f575502 | 97 | |
klauss | 7:019b08223b87 | 98 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 14:22a35f575502 | 99 | |
klauss | 14:22a35f575502 | 100 | debug_msg(""); |
klauss | 14:22a35f575502 | 101 | |
klauss | 7:019b08223b87 | 102 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, |
klauss | 7:019b08223b87 | 103 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 14:22a35f575502 | 104 | |
klauss | 14:22a35f575502 | 105 | debug_msg(""); |
klauss | 14:22a35f575502 | 106 | |
klauss | 7:019b08223b87 | 107 | cb->registry(); |
klauss | 14:22a35f575502 | 108 | |
klauss | 14:22a35f575502 | 109 | debug_msg(""); |
klauss | 7:019b08223b87 | 110 | } |
klauss | 7:019b08223b87 | 111 | } |
klauss | 7:019b08223b87 | 112 | } |
klauss | 7:019b08223b87 | 113 | } |
klauss | 4:de46f0d9b14d | 114 | /* |
klauss | 0:4d17cd9c8f9d | 115 | Call_Box * cb = NULL; |
klauss | 4:de46f0d9b14d | 116 | if( v_cb && data && write_buffer ){ |
klauss | 4:de46f0d9b14d | 117 | bool remove_slave = false; |
klauss | 4:de46f0d9b14d | 118 | int slave = 0x00; |
klauss | 4:de46f0d9b14d | 119 | int tam = v_cb->size(); |
klauss | 4:de46f0d9b14d | 120 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 3:cd9148672e25 | 121 | cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 4:de46f0d9b14d | 122 | if( cb->get_ext() % 2 ) continue; |
klauss | 3:cd9148672e25 | 123 | if( cb->is_timeout() ){ |
klauss | 3:cd9148672e25 | 124 | if( cb->reconfigure_timeout() == 0x00 ){ |
klauss | 4:de46f0d9b14d | 125 | debug_msg("( ext %d removed", cb->get_ext() ); |
klauss | 4:de46f0d9b14d | 126 | cb->unregistry(); |
klauss | 4:de46f0d9b14d | 127 | slave = cb->get_ext() + 1; |
klauss | 4:de46f0d9b14d | 128 | remove_slave = true; |
klauss | 3:cd9148672e25 | 129 | v_cb->remove_element( i ); |
klauss | 3:cd9148672e25 | 130 | delete( cb ); |
klauss | 4:de46f0d9b14d | 131 | break; |
klauss | 3:cd9148672e25 | 132 | }else{ |
klauss | 3:cd9148672e25 | 133 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 3:cd9148672e25 | 134 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, |
klauss | 3:cd9148672e25 | 135 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 3:cd9148672e25 | 136 | cb->registry(); |
klauss | 3:cd9148672e25 | 137 | } |
klauss | 2:93bec7313ccc | 138 | } |
klauss | 0:4d17cd9c8f9d | 139 | } |
klauss | 4:de46f0d9b14d | 140 | if( remove_slave ){ |
klauss | 4:de46f0d9b14d | 141 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 4:de46f0d9b14d | 142 | cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 4:de46f0d9b14d | 143 | if( cb->get_ext() == slave ){ |
klauss | 4:de46f0d9b14d | 144 | debug_msg("( ext %d removed", cb->get_ext() ); |
klauss | 4:de46f0d9b14d | 145 | cb->unregistry(); |
klauss | 4:de46f0d9b14d | 146 | v_cb->remove_element( i ); |
klauss | 4:de46f0d9b14d | 147 | delete( cb ); |
klauss | 4:de46f0d9b14d | 148 | } |
klauss | 4:de46f0d9b14d | 149 | } |
klauss | 4:de46f0d9b14d | 150 | } |
klauss | 4:de46f0d9b14d | 151 | if( tam != v_cb->size() ) debug_msg("%d ---> %d", tam, v_cb->size() ); |
klauss | 0:4d17cd9c8f9d | 152 | } |
klauss | 4:de46f0d9b14d | 153 | */ |
klauss | 15:9c7456c1b6f4 | 154 | //debug_msg(""); |
klauss | 0:4d17cd9c8f9d | 155 | } |
klauss | 0:4d17cd9c8f9d | 156 | |
klauss | 0:4d17cd9c8f9d | 157 | int sip_manager( Vector * v_cb, Vector * v_call, uint8_t * write_buffer ){ |
klauss | 0:4d17cd9c8f9d | 158 | static int index = 0; |
klauss | 15:9c7456c1b6f4 | 159 | //debug_msg(""); |
klauss | 0:4d17cd9c8f9d | 160 | if( v_cb && write_buffer ){ |
klauss | 0:4d17cd9c8f9d | 161 | if( v_cb->size() > 0 ){ |
klauss | 15:9c7456c1b6f4 | 162 | //debug_msg(""); |
klauss | 0:4d17cd9c8f9d | 163 | if( index >= v_cb->size() ) index = 0; |
klauss | 0:4d17cd9c8f9d | 164 | }else return 0; |
klauss | 0:4d17cd9c8f9d | 165 | Call_Box * cb = (Call_Box * )v_cb->get_element( index ); |
klauss | 0:4d17cd9c8f9d | 166 | /* Retorna |
klauss | 0:4d17cd9c8f9d | 167 | = 0 :: ok |
klauss | 0:4d17cd9c8f9d | 168 | < 0 :: tive problemas |
klauss | 0:4d17cd9c8f9d | 169 | > 0 :: devo remover essa call do vetor de calls |
klauss | 0:4d17cd9c8f9d | 170 | */ |
klauss | 0:4d17cd9c8f9d | 171 | int returned_value = 0; |
klauss | 13:ae278302dffe | 172 | |
klauss | 14:22a35f575502 | 173 | if( cb->status == cb_on_call || cb->status == cb_idle ){ |
klauss | 15:9c7456c1b6f4 | 174 | //debug_msg(""); |
klauss | 14:22a35f575502 | 175 | returned_value = cb->listen_SIP_server(); |
klauss | 14:22a35f575502 | 176 | } |
klauss | 0:4d17cd9c8f9d | 177 | index++; |
klauss | 0:4d17cd9c8f9d | 178 | return( returned_value ); |
klauss | 0:4d17cd9c8f9d | 179 | } |
klauss | 0:4d17cd9c8f9d | 180 | led1 = !led1; |
klauss | 0:4d17cd9c8f9d | 181 | return( -1 ); |
klauss | 0:4d17cd9c8f9d | 182 | } |
klauss | 0:4d17cd9c8f9d | 183 | |
klauss | 0:4d17cd9c8f9d | 184 | /* remove calls por timeout */ |
klauss | 0:4d17cd9c8f9d | 185 | void call_manager( Vector * v_call, Vector * v_cb, uint8_t * data, uint8_t * write_buffer, Timeslice * ts ){ |
klauss | 0:4d17cd9c8f9d | 186 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 187 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 188 | if( call->is_timeout() || call->is_timetofinish() ){ |
klauss | 0:4d17cd9c8f9d | 189 | v_call->remove_element( i ); |
klauss | 0:4d17cd9c8f9d | 190 | Call_Box * cb = __find_CB__( v_cb, call->get_cb_ext() ); |
klauss | 0:4d17cd9c8f9d | 191 | if( cb ){ |
klauss | 0:4d17cd9c8f9d | 192 | cb->status = cb_idle; |
klauss | 0:4d17cd9c8f9d | 193 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 194 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 195 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 196 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 0:4d17cd9c8f9d | 197 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 198 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 199 | cb->send_bye(); |
klauss | 13:ae278302dffe | 200 | set_status( cb->sip->status, sip_idle ); |
klauss | 0:4d17cd9c8f9d | 201 | } |
klauss | 9:ffa64f38ef9c | 202 | delete( call ); |
klauss | 0:4d17cd9c8f9d | 203 | } |
klauss | 0:4d17cd9c8f9d | 204 | } |
klauss | 0:4d17cd9c8f9d | 205 | } |
klauss | 0:4d17cd9c8f9d | 206 | |
klauss | 0:4d17cd9c8f9d | 207 | void build_telemetry_report( int ext, int port, char * data ){ |
klauss | 0:4d17cd9c8f9d | 208 | // construir o pacote de telemetria aqui |
klauss | 0:4d17cd9c8f9d | 209 | // decidir se isso sera escrito em disco e de tempos em tempos enviado para o servidor ... |
klauss | 0:4d17cd9c8f9d | 210 | // ou se sera enviado toda vez que determinado cb se registrar ... |
klauss | 0:4d17cd9c8f9d | 211 | } |
klauss | 0:4d17cd9c8f9d | 212 | |
klauss | 0:4d17cd9c8f9d | 213 | void __send_to_cb__( uint8_t * buffer ){ |
klauss | 0:4d17cd9c8f9d | 214 | xmemcpy( TXBuffer, buffer, DATA_SIZE ); |
klauss | 0:4d17cd9c8f9d | 215 | send2callboxes(); |
klauss | 0:4d17cd9c8f9d | 216 | } |