![](/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@10:22da1a0ac1e1, 2014-09-15 (annotated)
- Committer:
- klauss
- Date:
- Mon Sep 15 20:55:48 2014 +0000
- Revision:
- 10:22da1a0ac1e1
- Parent:
- 9:ffa64f38ef9c
- Child:
- 12:07fd716e0f35
voltando a versao que abre e fecha n sockets, travando em momentos random
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 | 8:e3bfe62a477e | 45 | eth->init( __MY_IP__, "255.255.255.0", "192.168.120.1" ); |
klauss | 8:e3bfe62a477e | 46 | return 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 | 10:22da1a0ac1e1 | 70 | //debug_msg(""); |
klauss | 7:019b08223b87 | 71 | Call_Box * cb = NULL; |
klauss | 7:019b08223b87 | 72 | if( v_cb && data && write_buffer ){ |
klauss | 8:e3bfe62a477e | 73 | int tam = v_cb->size(); |
klauss | 7:019b08223b87 | 74 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 7:019b08223b87 | 75 | cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 7:019b08223b87 | 76 | if( cb->is_timeout() ){ |
klauss | 7:019b08223b87 | 77 | if( cb->reconfigure_timeout() == 0x00 ){ |
klauss | 8:e3bfe62a477e | 78 | debug_msg("( ext %d removed", cb->get_ext() ); |
klauss | 9:ffa64f38ef9c | 79 | //cb->unregistry(); |
klauss | 9:ffa64f38ef9c | 80 | //v_cb->remove_element( i ); |
klauss | 9:ffa64f38ef9c | 81 | //delete( cb ); |
klauss | 7:019b08223b87 | 82 | break; |
klauss | 7:019b08223b87 | 83 | }else{ |
klauss | 7:019b08223b87 | 84 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 7:019b08223b87 | 85 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, |
klauss | 7:019b08223b87 | 86 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 7:019b08223b87 | 87 | cb->registry(); |
klauss | 7:019b08223b87 | 88 | } |
klauss | 7:019b08223b87 | 89 | } |
klauss | 7:019b08223b87 | 90 | } |
klauss | 7:019b08223b87 | 91 | if( tam != v_cb->size() ) debug_msg("%d ---> %d", tam, v_cb->size() ); |
klauss | 7:019b08223b87 | 92 | } |
klauss | 9:ffa64f38ef9c | 93 | |
klauss | 9:ffa64f38ef9c | 94 | |
klauss | 4:de46f0d9b14d | 95 | /* |
klauss | 0:4d17cd9c8f9d | 96 | Call_Box * cb = NULL; |
klauss | 4:de46f0d9b14d | 97 | if( v_cb && data && write_buffer ){ |
klauss | 4:de46f0d9b14d | 98 | bool remove_slave = false; |
klauss | 4:de46f0d9b14d | 99 | int slave = 0x00; |
klauss | 4:de46f0d9b14d | 100 | int tam = v_cb->size(); |
klauss | 4:de46f0d9b14d | 101 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 3:cd9148672e25 | 102 | cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 4:de46f0d9b14d | 103 | if( cb->get_ext() % 2 ) continue; |
klauss | 3:cd9148672e25 | 104 | if( cb->is_timeout() ){ |
klauss | 3:cd9148672e25 | 105 | if( cb->reconfigure_timeout() == 0x00 ){ |
klauss | 4:de46f0d9b14d | 106 | debug_msg("( ext %d removed", cb->get_ext() ); |
klauss | 4:de46f0d9b14d | 107 | cb->unregistry(); |
klauss | 4:de46f0d9b14d | 108 | slave = cb->get_ext() + 1; |
klauss | 4:de46f0d9b14d | 109 | remove_slave = true; |
klauss | 3:cd9148672e25 | 110 | v_cb->remove_element( i ); |
klauss | 3:cd9148672e25 | 111 | delete( cb ); |
klauss | 4:de46f0d9b14d | 112 | break; |
klauss | 3:cd9148672e25 | 113 | }else{ |
klauss | 3:cd9148672e25 | 114 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 3:cd9148672e25 | 115 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, |
klauss | 3:cd9148672e25 | 116 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 3:cd9148672e25 | 117 | cb->registry(); |
klauss | 3:cd9148672e25 | 118 | } |
klauss | 2:93bec7313ccc | 119 | } |
klauss | 0:4d17cd9c8f9d | 120 | } |
klauss | 4:de46f0d9b14d | 121 | if( remove_slave ){ |
klauss | 4:de46f0d9b14d | 122 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 4:de46f0d9b14d | 123 | cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 4:de46f0d9b14d | 124 | if( cb->get_ext() == slave ){ |
klauss | 4:de46f0d9b14d | 125 | debug_msg("( ext %d removed", cb->get_ext() ); |
klauss | 4:de46f0d9b14d | 126 | cb->unregistry(); |
klauss | 4:de46f0d9b14d | 127 | v_cb->remove_element( i ); |
klauss | 4:de46f0d9b14d | 128 | delete( cb ); |
klauss | 4:de46f0d9b14d | 129 | } |
klauss | 4:de46f0d9b14d | 130 | } |
klauss | 4:de46f0d9b14d | 131 | } |
klauss | 4:de46f0d9b14d | 132 | if( tam != v_cb->size() ) debug_msg("%d ---> %d", tam, v_cb->size() ); |
klauss | 0:4d17cd9c8f9d | 133 | } |
klauss | 4:de46f0d9b14d | 134 | */ |
klauss | 0:4d17cd9c8f9d | 135 | } |
klauss | 0:4d17cd9c8f9d | 136 | |
klauss | 0:4d17cd9c8f9d | 137 | int sip_manager( Vector * v_cb, Vector * v_call, uint8_t * write_buffer ){ |
klauss | 10:22da1a0ac1e1 | 138 | //debug_msg(""); |
klauss | 0:4d17cd9c8f9d | 139 | static int index = 0; |
klauss | 0:4d17cd9c8f9d | 140 | if( v_cb && write_buffer ){ |
klauss | 0:4d17cd9c8f9d | 141 | if( v_cb->size() > 0 ){ |
klauss | 0:4d17cd9c8f9d | 142 | if( index >= v_cb->size() ) index = 0; |
klauss | 0:4d17cd9c8f9d | 143 | }else return 0; |
klauss | 0:4d17cd9c8f9d | 144 | Call_Box * cb = (Call_Box * )v_cb->get_element( index ); |
klauss | 0:4d17cd9c8f9d | 145 | /* Retorna |
klauss | 0:4d17cd9c8f9d | 146 | = 0 :: ok |
klauss | 0:4d17cd9c8f9d | 147 | < 0 :: tive problemas |
klauss | 0:4d17cd9c8f9d | 148 | > 0 :: devo remover essa call do vetor de calls |
klauss | 0:4d17cd9c8f9d | 149 | */ |
klauss | 0:4d17cd9c8f9d | 150 | int returned_value = 0; |
klauss | 0:4d17cd9c8f9d | 151 | |
klauss | 10:22da1a0ac1e1 | 152 | //if( cb->status == cb_on_call || cb->status == cb_idle ) returned_value = cb->listen_SIP_server(); |
klauss | 10:22da1a0ac1e1 | 153 | if( cb->status == cb_on_call ) returned_value = cb->listen_SIP_server(); |
klauss | 0:4d17cd9c8f9d | 154 | |
klauss | 0:4d17cd9c8f9d | 155 | index++; |
klauss | 0:4d17cd9c8f9d | 156 | return( returned_value ); |
klauss | 0:4d17cd9c8f9d | 157 | } |
klauss | 0:4d17cd9c8f9d | 158 | led1 = !led1; |
klauss | 0:4d17cd9c8f9d | 159 | return( -1 ); |
klauss | 0:4d17cd9c8f9d | 160 | } |
klauss | 0:4d17cd9c8f9d | 161 | |
klauss | 0:4d17cd9c8f9d | 162 | /* remove calls por timeout */ |
klauss | 0:4d17cd9c8f9d | 163 | void call_manager( Vector * v_call, Vector * v_cb, uint8_t * data, uint8_t * write_buffer, Timeslice * ts ){ |
klauss | 10:22da1a0ac1e1 | 164 | //debug_msg(""); |
klauss | 0:4d17cd9c8f9d | 165 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 166 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 167 | if( call->is_timeout() || call->is_timetofinish() ){ |
klauss | 0:4d17cd9c8f9d | 168 | v_call->remove_element( i ); |
klauss | 0:4d17cd9c8f9d | 169 | Call_Box * cb = __find_CB__( v_cb, call->get_cb_ext() ); |
klauss | 0:4d17cd9c8f9d | 170 | if( cb ){ |
klauss | 0:4d17cd9c8f9d | 171 | cb->status = cb_idle; |
klauss | 0:4d17cd9c8f9d | 172 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 173 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 174 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 175 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 0:4d17cd9c8f9d | 176 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 177 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 178 | cb->send_bye(); |
klauss | 10:22da1a0ac1e1 | 179 | if( cb->sip ) set_status( cb->sip->status, sip_idle ); |
klauss | 0:4d17cd9c8f9d | 180 | } |
klauss | 9:ffa64f38ef9c | 181 | delete( call ); |
klauss | 0:4d17cd9c8f9d | 182 | } |
klauss | 0:4d17cd9c8f9d | 183 | } |
klauss | 0:4d17cd9c8f9d | 184 | } |
klauss | 0:4d17cd9c8f9d | 185 | |
klauss | 0:4d17cd9c8f9d | 186 | void build_telemetry_report( int ext, int port, char * data ){ |
klauss | 0:4d17cd9c8f9d | 187 | // construir o pacote de telemetria aqui |
klauss | 0:4d17cd9c8f9d | 188 | // decidir se isso sera escrito em disco e de tempos em tempos enviado para o servidor ... |
klauss | 0:4d17cd9c8f9d | 189 | // ou se sera enviado toda vez que determinado cb se registrar ... |
klauss | 0:4d17cd9c8f9d | 190 | } |
klauss | 0:4d17cd9c8f9d | 191 | |
klauss | 0:4d17cd9c8f9d | 192 | void __send_to_cb__( uint8_t * buffer ){ |
klauss | 0:4d17cd9c8f9d | 193 | xmemcpy( TXBuffer, buffer, DATA_SIZE ); |
klauss | 0:4d17cd9c8f9d | 194 | send2callboxes(); |
klauss | 0:4d17cd9c8f9d | 195 | } |