debugs removidos - ligacao em pares ok
Dependencies: EthernetInterface NTPClient mbed-rtos mbed
Fork of header_main by
utils.cpp@4:de46f0d9b14d, 2014-09-11 (annotated)
- Committer:
- klauss
- Date:
- Thu Sep 11 14:23:47 2014 +0000
- Revision:
- 4:de46f0d9b14d
- Parent:
- 3:cd9148672e25
- Child:
- 6:9695697dea4b
removido debugs - estavel
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 | 0:4d17cd9c8f9d | 44 | void __init_eth__( EthernetInterface * eth ){ |
klauss | 0:4d17cd9c8f9d | 45 | eth->init( __MY_IP__, "255.255.255.0", "192.168.120.1" ); |
klauss | 0:4d17cd9c8f9d | 46 | eth->connect(); |
klauss | 0:4d17cd9c8f9d | 47 | // pensar em o que acontece e como sinalizar que nao consegui conectar na web |
klauss | 0:4d17cd9c8f9d | 48 | // aparente so da time out, so que algo vai travar quando eu tentar usar a conexao |
klauss | 0:4d17cd9c8f9d | 49 | } |
klauss | 0:4d17cd9c8f9d | 50 | |
klauss | 0:4d17cd9c8f9d | 51 | Call_Box * __find_CB__( Vector * v_cb, int ext ){ |
klauss | 0:4d17cd9c8f9d | 52 | Call_Box * cb = NULL; |
klauss | 0:4d17cd9c8f9d | 53 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 54 | cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 55 | if( cb->get_ext() == ext ) return( cb ); |
klauss | 0:4d17cd9c8f9d | 56 | } |
klauss | 0:4d17cd9c8f9d | 57 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 58 | } |
klauss | 0:4d17cd9c8f9d | 59 | |
klauss | 0:4d17cd9c8f9d | 60 | VZ_call * __find_Call__( Vector * v_call, int ext ){ |
klauss | 0:4d17cd9c8f9d | 61 | VZ_call * call = NULL; |
klauss | 0:4d17cd9c8f9d | 62 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 63 | call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 64 | if( call->get_cb_ext() == ext ) return( call ); |
klauss | 0:4d17cd9c8f9d | 65 | } |
klauss | 0:4d17cd9c8f9d | 66 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 67 | } |
klauss | 0:4d17cd9c8f9d | 68 | |
klauss | 0:4d17cd9c8f9d | 69 | void registry_aging( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ |
klauss | 4:de46f0d9b14d | 70 | /* |
klauss | 0:4d17cd9c8f9d | 71 | Call_Box * cb = NULL; |
klauss | 4:de46f0d9b14d | 72 | if( v_cb && data && write_buffer ){ |
klauss | 4:de46f0d9b14d | 73 | bool remove_slave = false; |
klauss | 4:de46f0d9b14d | 74 | int slave = 0x00; |
klauss | 4:de46f0d9b14d | 75 | int tam = v_cb->size(); |
klauss | 4:de46f0d9b14d | 76 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 3:cd9148672e25 | 77 | cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 4:de46f0d9b14d | 78 | if( cb->get_ext() % 2 ) continue; |
klauss | 3:cd9148672e25 | 79 | if( cb->is_timeout() ){ |
klauss | 3:cd9148672e25 | 80 | if( cb->reconfigure_timeout() == 0x00 ){ |
klauss | 4:de46f0d9b14d | 81 | debug_msg("( ext %d removed", cb->get_ext() ); |
klauss | 4:de46f0d9b14d | 82 | cb->unregistry(); |
klauss | 4:de46f0d9b14d | 83 | slave = cb->get_ext() + 1; |
klauss | 4:de46f0d9b14d | 84 | remove_slave = true; |
klauss | 3:cd9148672e25 | 85 | v_cb->remove_element( i ); |
klauss | 3:cd9148672e25 | 86 | delete( cb ); |
klauss | 4:de46f0d9b14d | 87 | break; |
klauss | 3:cd9148672e25 | 88 | }else{ |
klauss | 3:cd9148672e25 | 89 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 3:cd9148672e25 | 90 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, |
klauss | 3:cd9148672e25 | 91 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 3:cd9148672e25 | 92 | cb->registry(); |
klauss | 3:cd9148672e25 | 93 | } |
klauss | 2:93bec7313ccc | 94 | } |
klauss | 0:4d17cd9c8f9d | 95 | } |
klauss | 4:de46f0d9b14d | 96 | if( remove_slave ){ |
klauss | 4:de46f0d9b14d | 97 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 4:de46f0d9b14d | 98 | cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 4:de46f0d9b14d | 99 | if( cb->get_ext() == slave ){ |
klauss | 4:de46f0d9b14d | 100 | debug_msg("( ext %d removed", cb->get_ext() ); |
klauss | 4:de46f0d9b14d | 101 | cb->unregistry(); |
klauss | 4:de46f0d9b14d | 102 | v_cb->remove_element( i ); |
klauss | 4:de46f0d9b14d | 103 | delete( cb ); |
klauss | 4:de46f0d9b14d | 104 | } |
klauss | 4:de46f0d9b14d | 105 | } |
klauss | 4:de46f0d9b14d | 106 | } |
klauss | 4:de46f0d9b14d | 107 | if( tam != v_cb->size() ) debug_msg("%d ---> %d", tam, v_cb->size() ); |
klauss | 0:4d17cd9c8f9d | 108 | } |
klauss | 4:de46f0d9b14d | 109 | */ |
klauss | 0:4d17cd9c8f9d | 110 | } |
klauss | 0:4d17cd9c8f9d | 111 | |
klauss | 0:4d17cd9c8f9d | 112 | int sip_manager( Vector * v_cb, Vector * v_call, uint8_t * write_buffer ){ |
klauss | 0:4d17cd9c8f9d | 113 | static int index = 0; |
klauss | 0:4d17cd9c8f9d | 114 | if( v_cb && write_buffer ){ |
klauss | 0:4d17cd9c8f9d | 115 | if( v_cb->size() > 0 ){ |
klauss | 0:4d17cd9c8f9d | 116 | if( index >= v_cb->size() ) index = 0; |
klauss | 0:4d17cd9c8f9d | 117 | }else return 0; |
klauss | 0:4d17cd9c8f9d | 118 | Call_Box * cb = (Call_Box * )v_cb->get_element( index ); |
klauss | 0:4d17cd9c8f9d | 119 | /* Retorna |
klauss | 0:4d17cd9c8f9d | 120 | = 0 :: ok |
klauss | 0:4d17cd9c8f9d | 121 | < 0 :: tive problemas |
klauss | 0:4d17cd9c8f9d | 122 | > 0 :: devo remover essa call do vetor de calls |
klauss | 0:4d17cd9c8f9d | 123 | */ |
klauss | 0:4d17cd9c8f9d | 124 | int returned_value = 0; |
klauss | 0:4d17cd9c8f9d | 125 | |
klauss | 0:4d17cd9c8f9d | 126 | if( cb->status == cb_on_call || cb->status == cb_idle ) returned_value = cb->listen_SIP_server(); |
klauss | 0:4d17cd9c8f9d | 127 | |
klauss | 0:4d17cd9c8f9d | 128 | index++; |
klauss | 0:4d17cd9c8f9d | 129 | return( returned_value ); |
klauss | 0:4d17cd9c8f9d | 130 | } |
klauss | 0:4d17cd9c8f9d | 131 | led1 = !led1; |
klauss | 0:4d17cd9c8f9d | 132 | return( -1 ); |
klauss | 0:4d17cd9c8f9d | 133 | } |
klauss | 0:4d17cd9c8f9d | 134 | |
klauss | 0:4d17cd9c8f9d | 135 | /* remove calls por timeout */ |
klauss | 0:4d17cd9c8f9d | 136 | void call_manager( Vector * v_call, Vector * v_cb, uint8_t * data, uint8_t * write_buffer, Timeslice * ts ){ |
klauss | 0:4d17cd9c8f9d | 137 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 138 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 139 | if( call->is_timeout() || call->is_timetofinish() ){ |
klauss | 0:4d17cd9c8f9d | 140 | v_call->remove_element( i ); |
klauss | 0:4d17cd9c8f9d | 141 | Call_Box * cb = __find_CB__( v_cb, call->get_cb_ext() ); |
klauss | 0:4d17cd9c8f9d | 142 | if( cb ){ |
klauss | 0:4d17cd9c8f9d | 143 | cb->status = cb_idle; |
klauss | 0:4d17cd9c8f9d | 144 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 145 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 146 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 147 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 0:4d17cd9c8f9d | 148 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 149 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 150 | cb->send_bye(); |
klauss | 0:4d17cd9c8f9d | 151 | set_status( cb->sip->status, sip_idle ); |
klauss | 0:4d17cd9c8f9d | 152 | } |
klauss | 0:4d17cd9c8f9d | 153 | delete( call ); |
klauss | 0:4d17cd9c8f9d | 154 | } |
klauss | 0:4d17cd9c8f9d | 155 | } |
klauss | 0:4d17cd9c8f9d | 156 | } |
klauss | 0:4d17cd9c8f9d | 157 | |
klauss | 0:4d17cd9c8f9d | 158 | void build_telemetry_report( int ext, int port, char * data ){ |
klauss | 0:4d17cd9c8f9d | 159 | // construir o pacote de telemetria aqui |
klauss | 0:4d17cd9c8f9d | 160 | // decidir se isso sera escrito em disco e de tempos em tempos enviado para o servidor ... |
klauss | 0:4d17cd9c8f9d | 161 | // ou se sera enviado toda vez que determinado cb se registrar ... |
klauss | 0:4d17cd9c8f9d | 162 | } |
klauss | 0:4d17cd9c8f9d | 163 | |
klauss | 0:4d17cd9c8f9d | 164 | void __send_to_cb__( uint8_t * buffer ){ |
klauss | 0:4d17cd9c8f9d | 165 | xmemcpy( TXBuffer, buffer, DATA_SIZE ); |
klauss | 0:4d17cd9c8f9d | 166 | send2callboxes(); |
klauss | 0:4d17cd9c8f9d | 167 | } |