![](/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@54:448d57291be6, 2014-10-22 (annotated)
- Committer:
- klauss
- Date:
- Wed Oct 22 13:53:49 2014 +0000
- Revision:
- 54:448d57291be6
- Parent:
- 53:bb492a8f115a
- Child:
- 55:2f5e7374af9d
set_ip -- ok
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 0:4d17cd9c8f9d | 1 | #include "utils.h" |
klauss | 30:8dfb6d8de53d | 2 | #include "prompt.h" |
klauss | 0:4d17cd9c8f9d | 3 | |
klauss | 48:195c97f12e8e | 4 | EthernetInterface eth; |
klauss | 48:195c97f12e8e | 5 | |
klauss | 0:4d17cd9c8f9d | 6 | void reverse( char str[], int length ){ |
klauss | 0:4d17cd9c8f9d | 7 | int start = 0; |
klauss | 0:4d17cd9c8f9d | 8 | int end = length -1; |
klauss | 0:4d17cd9c8f9d | 9 | while (start < end){ |
klauss | 0:4d17cd9c8f9d | 10 | swap(*(str+start), *(str+end)); |
klauss | 0:4d17cd9c8f9d | 11 | start++; |
klauss | 0:4d17cd9c8f9d | 12 | end--; |
klauss | 0:4d17cd9c8f9d | 13 | } |
klauss | 0:4d17cd9c8f9d | 14 | } |
klauss | 0:4d17cd9c8f9d | 15 | char* itoa(int num, char* str, int base){ |
klauss | 0:4d17cd9c8f9d | 16 | int i = 0; |
klauss | 0:4d17cd9c8f9d | 17 | bool isNegative = false; |
klauss | 0:4d17cd9c8f9d | 18 | |
klauss | 0:4d17cd9c8f9d | 19 | if( num == 0 ){ |
klauss | 0:4d17cd9c8f9d | 20 | str[i++] = '0'; |
klauss | 0:4d17cd9c8f9d | 21 | str[i] = '\0'; |
klauss | 0:4d17cd9c8f9d | 22 | return str; |
klauss | 0:4d17cd9c8f9d | 23 | } |
klauss | 0:4d17cd9c8f9d | 24 | if( num < 0 && base == 10 ){ |
klauss | 0:4d17cd9c8f9d | 25 | isNegative = true; |
klauss | 0:4d17cd9c8f9d | 26 | num = -num; |
klauss | 0:4d17cd9c8f9d | 27 | } |
klauss | 0:4d17cd9c8f9d | 28 | while( num != 0 ){ |
klauss | 0:4d17cd9c8f9d | 29 | int rem = num % base; |
klauss | 0:4d17cd9c8f9d | 30 | str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0'; |
klauss | 0:4d17cd9c8f9d | 31 | num = num/base; |
klauss | 0:4d17cd9c8f9d | 32 | } |
klauss | 0:4d17cd9c8f9d | 33 | if( isNegative ) |
klauss | 0:4d17cd9c8f9d | 34 | str[i++] = '-'; |
klauss | 0:4d17cd9c8f9d | 35 | |
klauss | 0:4d17cd9c8f9d | 36 | str[i] = '\0'; // Append string terminator |
klauss | 0:4d17cd9c8f9d | 37 | |
klauss | 0:4d17cd9c8f9d | 38 | reverse( str, i ); |
klauss | 0:4d17cd9c8f9d | 39 | return( str ); |
klauss | 0:4d17cd9c8f9d | 40 | } |
klauss | 0:4d17cd9c8f9d | 41 | |
klauss | 0:4d17cd9c8f9d | 42 | void reset_leds( void ){ |
klauss | 0:4d17cd9c8f9d | 43 | led1 = led2 = 1; |
klauss | 0:4d17cd9c8f9d | 44 | led3 = led4 = 0; |
klauss | 0:4d17cd9c8f9d | 45 | } |
klauss | 0:4d17cd9c8f9d | 46 | |
klauss | 48:195c97f12e8e | 47 | int __init_eth__(){ |
klauss | 44:cc4996469404 | 48 | static bool initialized = false; |
klauss | 50:d9b6577a70f5 | 49 | char buff_ip[ 16 ] = ""; |
klauss | 50:d9b6577a70f5 | 50 | char buff_msk[ 16 ] = ""; |
klauss | 50:d9b6577a70f5 | 51 | char buff_gtw[ 16 ] = ""; |
klauss | 48:195c97f12e8e | 52 | wdt.kick(); |
klauss | 28:94aec56c6329 | 53 | pc.printf("\n\r"); |
klauss | 27:98f824719d1c | 54 | FILE *fp = fopen( "/qspi/myip.txt", "r"); |
klauss | 27:98f824719d1c | 55 | if( fp == NULL ){ |
klauss | 31:bb5fb28a77df | 56 | if( debug_file ) debug_msg("Failed to open /qspi/myip.txt" ); |
klauss | 27:98f824719d1c | 57 | strncpy( buff_ip, __MY_IP__, 20 ); |
klauss | 27:98f824719d1c | 58 | }else{ |
klauss | 46:a670f187a704 | 59 | int read = fread( buff_ip, 1, 512, fp ); |
klauss | 46:a670f187a704 | 60 | if( read > 0 ){ |
klauss | 46:a670f187a704 | 61 | for( int i = 0; i < read; i++ ) |
klauss | 46:a670f187a704 | 62 | if( buff_ip[ i ] == '\n' || buff_ip[ i ] == '\r' ){ |
klauss | 46:a670f187a704 | 63 | buff_ip[ i ] = '\0'; |
klauss | 46:a670f187a704 | 64 | break; |
klauss | 46:a670f187a704 | 65 | } |
klauss | 31:bb5fb28a77df | 66 | if( debug_file ) debug_msg("Eth ip %s", buff_ip ); |
klauss | 27:98f824719d1c | 67 | }else{ |
klauss | 31:bb5fb28a77df | 68 | if( debug_file ) if( debug_file ) debug_msg("Failed to read /qspi/myip.txt" ); |
klauss | 27:98f824719d1c | 69 | strncpy( buff_ip, __MY_IP__, 20 ); |
klauss | 27:98f824719d1c | 70 | } |
klauss | 27:98f824719d1c | 71 | } |
klauss | 27:98f824719d1c | 72 | fclose( fp ); |
klauss | 27:98f824719d1c | 73 | buff_ip[ 15 ] = 0; |
klauss | 44:cc4996469404 | 74 | |
klauss | 27:98f824719d1c | 75 | fp = fopen( "/qspi/mymask.txt", "r"); |
klauss | 27:98f824719d1c | 76 | if( fp == NULL ){ |
klauss | 31:bb5fb28a77df | 77 | if( debug_file ) debug_msg("Failed to open /qspi/mymask.txt" ); |
klauss | 28:94aec56c6329 | 78 | strncpy( buff_msk, __MY_MSK__, 20 ); |
klauss | 27:98f824719d1c | 79 | }else{ |
klauss | 46:a670f187a704 | 80 | int read = fread( buff_msk, 1, 512, fp ); |
klauss | 46:a670f187a704 | 81 | if( read > 0 ){ |
klauss | 46:a670f187a704 | 82 | for( int i = 0; i < read; i++ ) |
klauss | 46:a670f187a704 | 83 | if( buff_msk[ i ] == '\n' || buff_msk[ i ] == '\r' ){ |
klauss | 46:a670f187a704 | 84 | buff_msk[ i ] = '\0'; |
klauss | 46:a670f187a704 | 85 | break; |
klauss | 46:a670f187a704 | 86 | } |
klauss | 31:bb5fb28a77df | 87 | if( debug_file ) debug_msg("mascara de rede Eth %s", buff_msk ); |
klauss | 27:98f824719d1c | 88 | }else{ |
klauss | 31:bb5fb28a77df | 89 | if( debug_file ) debug_msg("Failed to read /qspi/mymask.txt" ); |
klauss | 28:94aec56c6329 | 90 | strncpy( buff_msk, __MY_MSK__, 20 ); |
klauss | 27:98f824719d1c | 91 | } |
klauss | 27:98f824719d1c | 92 | } |
klauss | 27:98f824719d1c | 93 | fclose( fp ); |
klauss | 27:98f824719d1c | 94 | buff_msk[ 15 ] = 0; |
klauss | 27:98f824719d1c | 95 | |
klauss | 27:98f824719d1c | 96 | fp = fopen( "/qspi/mygate.txt", "r"); |
klauss | 27:98f824719d1c | 97 | if( fp == NULL ){ |
klauss | 31:bb5fb28a77df | 98 | if( debug_file ) debug_msg("Failed to open /qspi/mygate.txt" ); |
klauss | 28:94aec56c6329 | 99 | strncpy( buff_gtw, __MY_GTW__, 20 ); |
klauss | 27:98f824719d1c | 100 | }else{ |
klauss | 46:a670f187a704 | 101 | int read = fread( buff_gtw, 1, 512, fp ); |
klauss | 46:a670f187a704 | 102 | if( read > 0 ){ |
klauss | 46:a670f187a704 | 103 | for( int i = 0; i < read; i++ ) |
klauss | 46:a670f187a704 | 104 | if( buff_gtw[ i ] == '\n' || buff_gtw[ i ] == '\r' ){ |
klauss | 46:a670f187a704 | 105 | buff_gtw[ i ] = '\0'; |
klauss | 46:a670f187a704 | 106 | break; |
klauss | 46:a670f187a704 | 107 | } |
klauss | 31:bb5fb28a77df | 108 | if( debug_file ) debug_msg("Ip Gateway Eth %s", buff_gtw ); |
klauss | 27:98f824719d1c | 109 | }else{ |
klauss | 31:bb5fb28a77df | 110 | if( debug_file ) debug_msg("Failed to read /qspi/mygate.txt" ); |
klauss | 28:94aec56c6329 | 111 | strncpy( buff_gtw, __MY_GTW__, 20 ); |
klauss | 27:98f824719d1c | 112 | } |
klauss | 27:98f824719d1c | 113 | } |
klauss | 27:98f824719d1c | 114 | fclose( fp ); |
klauss | 27:98f824719d1c | 115 | buff_gtw[ 15 ] = 0; |
klauss | 46:a670f187a704 | 116 | |
klauss | 44:cc4996469404 | 117 | if( !initialized ){ |
klauss | 50:d9b6577a70f5 | 118 | //eth.init( buff_ip, buff_msk, buff_gtw ); |
klauss | 50:d9b6577a70f5 | 119 | eth.init( buff_ip, buff_msk, __MY_GTW__ ); |
klauss | 44:cc4996469404 | 120 | initialized = true; |
klauss | 48:195c97f12e8e | 121 | return eth.connect(); |
klauss | 44:cc4996469404 | 122 | } |
klauss | 48:195c97f12e8e | 123 | |
klauss | 48:195c97f12e8e | 124 | if( !eth.disconnect() ){ |
klauss | 48:195c97f12e8e | 125 | return eth.connect(); |
klauss | 48:195c97f12e8e | 126 | } |
klauss | 48:195c97f12e8e | 127 | else{ |
klauss | 48:195c97f12e8e | 128 | return eth.connect(); |
klauss | 48:195c97f12e8e | 129 | } |
klauss | 0:4d17cd9c8f9d | 130 | } |
klauss | 0:4d17cd9c8f9d | 131 | |
klauss | 0:4d17cd9c8f9d | 132 | Call_Box * __find_CB__( Vector * v_cb, int ext ){ |
klauss | 0:4d17cd9c8f9d | 133 | Call_Box * cb = NULL; |
klauss | 0:4d17cd9c8f9d | 134 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 135 | cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 136 | if( cb->get_ext() == ext ) return( cb ); |
klauss | 0:4d17cd9c8f9d | 137 | } |
klauss | 0:4d17cd9c8f9d | 138 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 139 | } |
klauss | 0:4d17cd9c8f9d | 140 | |
klauss | 0:4d17cd9c8f9d | 141 | VZ_call * __find_Call__( Vector * v_call, int ext ){ |
klauss | 0:4d17cd9c8f9d | 142 | VZ_call * call = NULL; |
klauss | 0:4d17cd9c8f9d | 143 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 144 | call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 145 | if( call->get_cb_ext() == ext ) return( call ); |
klauss | 0:4d17cd9c8f9d | 146 | } |
klauss | 0:4d17cd9c8f9d | 147 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 148 | } |
klauss | 0:4d17cd9c8f9d | 149 | |
klauss | 0:4d17cd9c8f9d | 150 | void registry_aging( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ |
klauss | 7:019b08223b87 | 151 | Call_Box * cb = NULL; |
klauss | 7:019b08223b87 | 152 | if( v_cb && data && write_buffer ){ |
klauss | 7:019b08223b87 | 153 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 7:019b08223b87 | 154 | cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 7:019b08223b87 | 155 | if( cb->is_timeout() ){ |
klauss | 7:019b08223b87 | 156 | if( cb->reconfigure_timeout() == 0x00 ){ |
klauss | 30:8dfb6d8de53d | 157 | if( debug_alive ) debug_msg("( ext %d removed )", cb->get_ext() ); |
klauss | 32:43041f5e27c4 | 158 | //cb->unregistry(); |
klauss | 30:8dfb6d8de53d | 159 | v_cb->remove_element( i ); |
klauss | 30:8dfb6d8de53d | 160 | delete( cb ); |
klauss | 7:019b08223b87 | 161 | break; |
klauss | 7:019b08223b87 | 162 | }else{ |
klauss | 31:bb5fb28a77df | 163 | if( debug_alive ) debug_msg("Ping Cbx %d", cb->get_ext() ); |
klauss | 28:94aec56c6329 | 164 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 7:019b08223b87 | 165 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, |
klauss | 7:019b08223b87 | 166 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 7:019b08223b87 | 167 | cb->registry(); |
klauss | 7:019b08223b87 | 168 | } |
klauss | 7:019b08223b87 | 169 | } |
klauss | 7:019b08223b87 | 170 | } |
klauss | 7:019b08223b87 | 171 | } |
klauss | 0:4d17cd9c8f9d | 172 | } |
klauss | 0:4d17cd9c8f9d | 173 | |
klauss | 0:4d17cd9c8f9d | 174 | int sip_manager( Vector * v_cb, Vector * v_call, uint8_t * write_buffer ){ |
klauss | 0:4d17cd9c8f9d | 175 | static int index = 0; |
klauss | 0:4d17cd9c8f9d | 176 | if( v_cb && write_buffer ){ |
klauss | 0:4d17cd9c8f9d | 177 | if( v_cb->size() > 0 ){ |
klauss | 0:4d17cd9c8f9d | 178 | if( index >= v_cb->size() ) index = 0; |
klauss | 0:4d17cd9c8f9d | 179 | }else return 0; |
klauss | 0:4d17cd9c8f9d | 180 | Call_Box * cb = (Call_Box * )v_cb->get_element( index ); |
klauss | 0:4d17cd9c8f9d | 181 | /* Retorna |
klauss | 0:4d17cd9c8f9d | 182 | = 0 :: ok |
klauss | 0:4d17cd9c8f9d | 183 | < 0 :: tive problemas |
klauss | 0:4d17cd9c8f9d | 184 | > 0 :: devo remover essa call do vetor de calls |
klauss | 0:4d17cd9c8f9d | 185 | */ |
klauss | 0:4d17cd9c8f9d | 186 | int returned_value = 0; |
klauss | 13:ae278302dffe | 187 | |
klauss | 14:22a35f575502 | 188 | if( cb->status == cb_on_call || cb->status == cb_idle ){ |
klauss | 15:9c7456c1b6f4 | 189 | //debug_msg(""); |
klauss | 14:22a35f575502 | 190 | returned_value = cb->listen_SIP_server(); |
klauss | 14:22a35f575502 | 191 | } |
klauss | 0:4d17cd9c8f9d | 192 | index++; |
klauss | 0:4d17cd9c8f9d | 193 | return( returned_value ); |
klauss | 0:4d17cd9c8f9d | 194 | } |
klauss | 0:4d17cd9c8f9d | 195 | led1 = !led1; |
klauss | 0:4d17cd9c8f9d | 196 | return( -1 ); |
klauss | 0:4d17cd9c8f9d | 197 | } |
klauss | 0:4d17cd9c8f9d | 198 | |
klauss | 0:4d17cd9c8f9d | 199 | /* remove calls por timeout */ |
klauss | 0:4d17cd9c8f9d | 200 | void call_manager( Vector * v_call, Vector * v_cb, uint8_t * data, uint8_t * write_buffer, Timeslice * ts ){ |
klauss | 0:4d17cd9c8f9d | 201 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 202 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 29:7246460b73f8 | 203 | if( call->is_timetofinish() ){ |
klauss | 0:4d17cd9c8f9d | 204 | v_call->remove_element( i ); |
klauss | 0:4d17cd9c8f9d | 205 | Call_Box * cb = __find_CB__( v_cb, call->get_cb_ext() ); |
klauss | 0:4d17cd9c8f9d | 206 | if( cb ){ |
klauss | 0:4d17cd9c8f9d | 207 | cb->status = cb_idle; |
klauss | 0:4d17cd9c8f9d | 208 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 209 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 210 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 211 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 0:4d17cd9c8f9d | 212 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 213 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 214 | cb->send_bye(); |
klauss | 13:ae278302dffe | 215 | set_status( cb->sip->status, sip_idle ); |
klauss | 29:7246460b73f8 | 216 | cb->re_start_timer(); |
klauss | 0:4d17cd9c8f9d | 217 | } |
klauss | 9:ffa64f38ef9c | 218 | delete( call ); |
klauss | 0:4d17cd9c8f9d | 219 | } |
klauss | 0:4d17cd9c8f9d | 220 | } |
klauss | 0:4d17cd9c8f9d | 221 | } |
klauss | 0:4d17cd9c8f9d | 222 | |
klauss | 0:4d17cd9c8f9d | 223 | void build_telemetry_report( int ext, int port, char * data ){ |
klauss | 53:bb492a8f115a | 224 | char aux[ __CB_BUFFER_SIZE__ ]; |
klauss | 53:bb492a8f115a | 225 | char tmp[ 6 ]; |
klauss | 53:bb492a8f115a | 226 | strcpy( aux, itoa( ext, tmp, 10 ) ); |
klauss | 53:bb492a8f115a | 227 | strcat( aux, " " ); |
klauss | 53:bb492a8f115a | 228 | strncat( aux, (char * )data, __TELEMETRY_SIZE__ ); |
klauss | 53:bb492a8f115a | 229 | |
klauss | 54:448d57291be6 | 230 | if( debug_telemetry ){ |
klauss | 54:448d57291be6 | 231 | pc.printf("\n\r 1. "); |
klauss | 54:448d57291be6 | 232 | uint8_t count = 2; |
klauss | 54:448d57291be6 | 233 | for( register int i = 0; i < __CB_BUFFER_SIZE__; i++ ){ |
klauss | 54:448d57291be6 | 234 | if( i > 0 && !( i % 15 ) ) pc.printf("\n\r%2u. ", count++ ); |
klauss | 54:448d57291be6 | 235 | pc.printf("%3i ", aux[ i ] ); |
klauss | 54:448d57291be6 | 236 | } |
klauss | 54:448d57291be6 | 237 | pc.printf("\n\r"); |
klauss | 54:448d57291be6 | 238 | |
klauss | 54:448d57291be6 | 239 | if( tcp_session ) tcp_client.send_all( ( char *)aux, strlen( (char * )aux ) ); |
klauss | 53:bb492a8f115a | 240 | } |
klauss | 53:bb492a8f115a | 241 | |
klauss | 53:bb492a8f115a | 242 | UDPSocket t_sock; |
klauss | 53:bb492a8f115a | 243 | Endpoint t_server; |
klauss | 53:bb492a8f115a | 244 | |
klauss | 53:bb492a8f115a | 245 | int adr = t_server.set_address( __TELEMETRY_SERVER_IP__ , __TELEMETRY_SERVER_PORT__ ); |
klauss | 53:bb492a8f115a | 246 | if( debug_telemetry ) send_msg("Valor de retorno adr -- %d", adr ); |
klauss | 53:bb492a8f115a | 247 | t_sock.set_blocking( false, 1 ); |
klauss | 53:bb492a8f115a | 248 | |
klauss | 53:bb492a8f115a | 249 | int bind = t_sock.bind( __TELEMETRY_HEADER_PORT__ ); |
klauss | 53:bb492a8f115a | 250 | if( debug_telemetry ) send_msg("Valor de retorno bind -- %d", bind ); |
klauss | 53:bb492a8f115a | 251 | |
klauss | 53:bb492a8f115a | 252 | //int init = t_sock.init(); |
klauss | 53:bb492a8f115a | 253 | //if( debug_telemetry ) send_msg("Valor de retorno init -- %d", init ); |
klauss | 53:bb492a8f115a | 254 | |
klauss | 53:bb492a8f115a | 255 | int ret = t_sock.sendTo( t_server, aux, strlen( aux ) ); |
klauss | 53:bb492a8f115a | 256 | if( debug_telemetry ) send_msg("Valor de retorno sent-- %d", ret ); |
klauss | 53:bb492a8f115a | 257 | |
klauss | 53:bb492a8f115a | 258 | int close = t_sock.close(); |
klauss | 53:bb492a8f115a | 259 | if( debug_telemetry ) send_msg("Valor de retorno close-- %d", close ); |
klauss | 0:4d17cd9c8f9d | 260 | } |
klauss | 0:4d17cd9c8f9d | 261 | |
klauss | 0:4d17cd9c8f9d | 262 | void __send_to_cb__( uint8_t * buffer ){ |
klauss | 0:4d17cd9c8f9d | 263 | xmemcpy( TXBuffer, buffer, DATA_SIZE ); |
klauss | 0:4d17cd9c8f9d | 264 | send2callboxes(); |
klauss | 0:4d17cd9c8f9d | 265 | } |