![](/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@72:895ca792c647, 2014-11-19 (annotated)
- Committer:
- klauss
- Date:
- Wed Nov 19 13:36:33 2014 +0000
- Revision:
- 72:895ca792c647
- Parent:
- 70:714c33487aae
- Child:
- 74:81c47fff88a5
send_to_cbx | send2callbox under development -- antes de modularizar melhor o src.
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 | 63:0d95da692bb4 | 5 | uint8_t cb_tx_buffer[ __CB_BUFFER_SIZE__ ]; |
klauss | 72:895ca792c647 | 6 | |
klauss | 72:895ca792c647 | 7 | ring_buffer * rb = ring_buffer_init( NULL ); |
klauss | 72:895ca792c647 | 8 | |
klauss | 69:65665afbad5d | 9 | UDPSocket t_sock; |
klauss | 68:b54993674190 | 10 | Endpoint t_server; |
klauss | 68:b54993674190 | 11 | |
klauss | 0:4d17cd9c8f9d | 12 | void reverse( char str[], int length ){ |
klauss | 0:4d17cd9c8f9d | 13 | int start = 0; |
klauss | 0:4d17cd9c8f9d | 14 | int end = length -1; |
klauss | 0:4d17cd9c8f9d | 15 | while (start < end){ |
klauss | 0:4d17cd9c8f9d | 16 | swap(*(str+start), *(str+end)); |
klauss | 0:4d17cd9c8f9d | 17 | start++; |
klauss | 0:4d17cd9c8f9d | 18 | end--; |
klauss | 0:4d17cd9c8f9d | 19 | } |
klauss | 0:4d17cd9c8f9d | 20 | } |
klauss | 0:4d17cd9c8f9d | 21 | char* itoa(int num, char* str, int base){ |
klauss | 0:4d17cd9c8f9d | 22 | int i = 0; |
klauss | 0:4d17cd9c8f9d | 23 | bool isNegative = false; |
klauss | 0:4d17cd9c8f9d | 24 | |
klauss | 0:4d17cd9c8f9d | 25 | if( num == 0 ){ |
klauss | 0:4d17cd9c8f9d | 26 | str[i++] = '0'; |
klauss | 0:4d17cd9c8f9d | 27 | str[i] = '\0'; |
klauss | 0:4d17cd9c8f9d | 28 | return str; |
klauss | 0:4d17cd9c8f9d | 29 | } |
klauss | 0:4d17cd9c8f9d | 30 | if( num < 0 && base == 10 ){ |
klauss | 0:4d17cd9c8f9d | 31 | isNegative = true; |
klauss | 0:4d17cd9c8f9d | 32 | num = -num; |
klauss | 0:4d17cd9c8f9d | 33 | } |
klauss | 0:4d17cd9c8f9d | 34 | while( num != 0 ){ |
klauss | 0:4d17cd9c8f9d | 35 | int rem = num % base; |
klauss | 0:4d17cd9c8f9d | 36 | str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0'; |
klauss | 0:4d17cd9c8f9d | 37 | num = num/base; |
klauss | 0:4d17cd9c8f9d | 38 | } |
klauss | 0:4d17cd9c8f9d | 39 | if( isNegative ) |
klauss | 0:4d17cd9c8f9d | 40 | str[i++] = '-'; |
klauss | 0:4d17cd9c8f9d | 41 | |
klauss | 0:4d17cd9c8f9d | 42 | str[i] = '\0'; // Append string terminator |
klauss | 0:4d17cd9c8f9d | 43 | |
klauss | 0:4d17cd9c8f9d | 44 | reverse( str, i ); |
klauss | 0:4d17cd9c8f9d | 45 | return( str ); |
klauss | 0:4d17cd9c8f9d | 46 | } |
klauss | 0:4d17cd9c8f9d | 47 | |
klauss | 0:4d17cd9c8f9d | 48 | void reset_leds( void ){ |
klauss | 0:4d17cd9c8f9d | 49 | led1 = led2 = 1; |
klauss | 0:4d17cd9c8f9d | 50 | led3 = led4 = 0; |
klauss | 0:4d17cd9c8f9d | 51 | } |
klauss | 0:4d17cd9c8f9d | 52 | |
klauss | 48:195c97f12e8e | 53 | int __init_eth__(){ |
klauss | 44:cc4996469404 | 54 | static bool initialized = false; |
klauss | 50:d9b6577a70f5 | 55 | char buff_ip[ 16 ] = ""; |
klauss | 50:d9b6577a70f5 | 56 | char buff_msk[ 16 ] = ""; |
klauss | 50:d9b6577a70f5 | 57 | char buff_gtw[ 16 ] = ""; |
klauss | 48:195c97f12e8e | 58 | wdt.kick(); |
klauss | 28:94aec56c6329 | 59 | pc.printf("\n\r"); |
klauss | 27:98f824719d1c | 60 | FILE *fp = fopen( "/qspi/myip.txt", "r"); |
klauss | 27:98f824719d1c | 61 | if( fp == NULL ){ |
klauss | 31:bb5fb28a77df | 62 | if( debug_file ) debug_msg("Failed to open /qspi/myip.txt" ); |
klauss | 27:98f824719d1c | 63 | strncpy( buff_ip, __MY_IP__, 20 ); |
klauss | 27:98f824719d1c | 64 | }else{ |
klauss | 46:a670f187a704 | 65 | int read = fread( buff_ip, 1, 512, fp ); |
klauss | 46:a670f187a704 | 66 | if( read > 0 ){ |
klauss | 46:a670f187a704 | 67 | for( int i = 0; i < read; i++ ) |
klauss | 46:a670f187a704 | 68 | if( buff_ip[ i ] == '\n' || buff_ip[ i ] == '\r' ){ |
klauss | 46:a670f187a704 | 69 | buff_ip[ i ] = '\0'; |
klauss | 46:a670f187a704 | 70 | break; |
klauss | 46:a670f187a704 | 71 | } |
klauss | 31:bb5fb28a77df | 72 | if( debug_file ) debug_msg("Eth ip %s", buff_ip ); |
klauss | 27:98f824719d1c | 73 | }else{ |
klauss | 68:b54993674190 | 74 | if( debug_file ) debug_msg("Failed to read /qspi/myip.txt" ); |
klauss | 27:98f824719d1c | 75 | strncpy( buff_ip, __MY_IP__, 20 ); |
klauss | 27:98f824719d1c | 76 | } |
klauss | 27:98f824719d1c | 77 | } |
klauss | 27:98f824719d1c | 78 | fclose( fp ); |
klauss | 27:98f824719d1c | 79 | buff_ip[ 15 ] = 0; |
klauss | 44:cc4996469404 | 80 | |
klauss | 27:98f824719d1c | 81 | fp = fopen( "/qspi/mymask.txt", "r"); |
klauss | 27:98f824719d1c | 82 | if( fp == NULL ){ |
klauss | 31:bb5fb28a77df | 83 | if( debug_file ) debug_msg("Failed to open /qspi/mymask.txt" ); |
klauss | 28:94aec56c6329 | 84 | strncpy( buff_msk, __MY_MSK__, 20 ); |
klauss | 27:98f824719d1c | 85 | }else{ |
klauss | 46:a670f187a704 | 86 | int read = fread( buff_msk, 1, 512, fp ); |
klauss | 46:a670f187a704 | 87 | if( read > 0 ){ |
klauss | 46:a670f187a704 | 88 | for( int i = 0; i < read; i++ ) |
klauss | 46:a670f187a704 | 89 | if( buff_msk[ i ] == '\n' || buff_msk[ i ] == '\r' ){ |
klauss | 46:a670f187a704 | 90 | buff_msk[ i ] = '\0'; |
klauss | 46:a670f187a704 | 91 | break; |
klauss | 46:a670f187a704 | 92 | } |
klauss | 31:bb5fb28a77df | 93 | if( debug_file ) debug_msg("mascara de rede Eth %s", buff_msk ); |
klauss | 27:98f824719d1c | 94 | }else{ |
klauss | 31:bb5fb28a77df | 95 | if( debug_file ) debug_msg("Failed to read /qspi/mymask.txt" ); |
klauss | 28:94aec56c6329 | 96 | strncpy( buff_msk, __MY_MSK__, 20 ); |
klauss | 27:98f824719d1c | 97 | } |
klauss | 27:98f824719d1c | 98 | } |
klauss | 27:98f824719d1c | 99 | fclose( fp ); |
klauss | 27:98f824719d1c | 100 | buff_msk[ 15 ] = 0; |
klauss | 27:98f824719d1c | 101 | |
klauss | 27:98f824719d1c | 102 | fp = fopen( "/qspi/mygate.txt", "r"); |
klauss | 27:98f824719d1c | 103 | if( fp == NULL ){ |
klauss | 31:bb5fb28a77df | 104 | if( debug_file ) debug_msg("Failed to open /qspi/mygate.txt" ); |
klauss | 28:94aec56c6329 | 105 | strncpy( buff_gtw, __MY_GTW__, 20 ); |
klauss | 27:98f824719d1c | 106 | }else{ |
klauss | 46:a670f187a704 | 107 | int read = fread( buff_gtw, 1, 512, fp ); |
klauss | 46:a670f187a704 | 108 | if( read > 0 ){ |
klauss | 46:a670f187a704 | 109 | for( int i = 0; i < read; i++ ) |
klauss | 46:a670f187a704 | 110 | if( buff_gtw[ i ] == '\n' || buff_gtw[ i ] == '\r' ){ |
klauss | 46:a670f187a704 | 111 | buff_gtw[ i ] = '\0'; |
klauss | 46:a670f187a704 | 112 | break; |
klauss | 46:a670f187a704 | 113 | } |
klauss | 31:bb5fb28a77df | 114 | if( debug_file ) debug_msg("Ip Gateway Eth %s", buff_gtw ); |
klauss | 27:98f824719d1c | 115 | }else{ |
klauss | 31:bb5fb28a77df | 116 | if( debug_file ) debug_msg("Failed to read /qspi/mygate.txt" ); |
klauss | 28:94aec56c6329 | 117 | strncpy( buff_gtw, __MY_GTW__, 20 ); |
klauss | 27:98f824719d1c | 118 | } |
klauss | 27:98f824719d1c | 119 | } |
klauss | 27:98f824719d1c | 120 | fclose( fp ); |
klauss | 27:98f824719d1c | 121 | buff_gtw[ 15 ] = 0; |
klauss | 46:a670f187a704 | 122 | |
klauss | 44:cc4996469404 | 123 | if( !initialized ){ |
klauss | 50:d9b6577a70f5 | 124 | //eth.init( buff_ip, buff_msk, buff_gtw ); |
klauss | 50:d9b6577a70f5 | 125 | eth.init( buff_ip, buff_msk, __MY_GTW__ ); |
klauss | 44:cc4996469404 | 126 | initialized = true; |
klauss | 48:195c97f12e8e | 127 | return eth.connect(); |
klauss | 44:cc4996469404 | 128 | } |
klauss | 48:195c97f12e8e | 129 | |
klauss | 48:195c97f12e8e | 130 | if( !eth.disconnect() ){ |
klauss | 48:195c97f12e8e | 131 | return eth.connect(); |
klauss | 48:195c97f12e8e | 132 | } |
klauss | 48:195c97f12e8e | 133 | else{ |
klauss | 48:195c97f12e8e | 134 | return eth.connect(); |
klauss | 48:195c97f12e8e | 135 | } |
klauss | 0:4d17cd9c8f9d | 136 | } |
klauss | 0:4d17cd9c8f9d | 137 | |
klauss | 0:4d17cd9c8f9d | 138 | Call_Box * __find_CB__( Vector * v_cb, int ext ){ |
klauss | 0:4d17cd9c8f9d | 139 | Call_Box * cb = NULL; |
klauss | 0:4d17cd9c8f9d | 140 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 141 | cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 142 | if( cb->get_ext() == ext ) return( cb ); |
klauss | 0:4d17cd9c8f9d | 143 | } |
klauss | 0:4d17cd9c8f9d | 144 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 145 | } |
klauss | 0:4d17cd9c8f9d | 146 | |
klauss | 0:4d17cd9c8f9d | 147 | VZ_call * __find_Call__( Vector * v_call, int ext ){ |
klauss | 0:4d17cd9c8f9d | 148 | VZ_call * call = NULL; |
klauss | 0:4d17cd9c8f9d | 149 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 150 | call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 151 | if( call->get_cb_ext() == ext ) return( call ); |
klauss | 0:4d17cd9c8f9d | 152 | } |
klauss | 0:4d17cd9c8f9d | 153 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 154 | } |
klauss | 0:4d17cd9c8f9d | 155 | |
klauss | 0:4d17cd9c8f9d | 156 | void registry_aging( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ |
klauss | 7:019b08223b87 | 157 | Call_Box * cb = NULL; |
klauss | 7:019b08223b87 | 158 | if( v_cb && data && write_buffer ){ |
klauss | 7:019b08223b87 | 159 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 7:019b08223b87 | 160 | cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 7:019b08223b87 | 161 | if( cb->is_timeout() ){ |
klauss | 7:019b08223b87 | 162 | if( cb->reconfigure_timeout() == 0x00 ){ |
klauss | 30:8dfb6d8de53d | 163 | if( debug_alive ) debug_msg("( ext %d removed )", cb->get_ext() ); |
klauss | 32:43041f5e27c4 | 164 | //cb->unregistry(); |
klauss | 30:8dfb6d8de53d | 165 | v_cb->remove_element( i ); |
klauss | 30:8dfb6d8de53d | 166 | delete( cb ); |
klauss | 7:019b08223b87 | 167 | break; |
klauss | 7:019b08223b87 | 168 | }else{ |
klauss | 31:bb5fb28a77df | 169 | if( debug_alive ) debug_msg("Ping Cbx %d", cb->get_ext() ); |
klauss | 28:94aec56c6329 | 170 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 7:019b08223b87 | 171 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, |
klauss | 7:019b08223b87 | 172 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 7:019b08223b87 | 173 | cb->registry(); |
klauss | 7:019b08223b87 | 174 | } |
klauss | 7:019b08223b87 | 175 | } |
klauss | 7:019b08223b87 | 176 | } |
klauss | 7:019b08223b87 | 177 | } |
klauss | 0:4d17cd9c8f9d | 178 | } |
klauss | 0:4d17cd9c8f9d | 179 | |
klauss | 70:714c33487aae | 180 | int sip_manager( Vector * v_cb ){ |
klauss | 0:4d17cd9c8f9d | 181 | static int index = 0; |
klauss | 70:714c33487aae | 182 | if( v_cb != NULL ){ |
klauss | 0:4d17cd9c8f9d | 183 | if( v_cb->size() > 0 ){ |
klauss | 0:4d17cd9c8f9d | 184 | if( index >= v_cb->size() ) index = 0; |
klauss | 0:4d17cd9c8f9d | 185 | }else return 0; |
klauss | 0:4d17cd9c8f9d | 186 | Call_Box * cb = (Call_Box * )v_cb->get_element( index ); |
klauss | 0:4d17cd9c8f9d | 187 | /* Retorna |
klauss | 0:4d17cd9c8f9d | 188 | = 0 :: ok |
klauss | 0:4d17cd9c8f9d | 189 | < 0 :: tive problemas |
klauss | 0:4d17cd9c8f9d | 190 | > 0 :: devo remover essa call do vetor de calls |
klauss | 0:4d17cd9c8f9d | 191 | */ |
klauss | 0:4d17cd9c8f9d | 192 | int returned_value = 0; |
klauss | 13:ae278302dffe | 193 | |
klauss | 14:22a35f575502 | 194 | if( cb->status == cb_on_call || cb->status == cb_idle ){ |
klauss | 15:9c7456c1b6f4 | 195 | //debug_msg(""); |
klauss | 14:22a35f575502 | 196 | returned_value = cb->listen_SIP_server(); |
klauss | 14:22a35f575502 | 197 | } |
klauss | 0:4d17cd9c8f9d | 198 | index++; |
klauss | 0:4d17cd9c8f9d | 199 | return( returned_value ); |
klauss | 0:4d17cd9c8f9d | 200 | } |
klauss | 0:4d17cd9c8f9d | 201 | led1 = !led1; |
klauss | 0:4d17cd9c8f9d | 202 | return( -1 ); |
klauss | 0:4d17cd9c8f9d | 203 | } |
klauss | 0:4d17cd9c8f9d | 204 | |
klauss | 0:4d17cd9c8f9d | 205 | /* remove calls por timeout */ |
klauss | 0:4d17cd9c8f9d | 206 | void call_manager( Vector * v_call, Vector * v_cb, uint8_t * data, uint8_t * write_buffer, Timeslice * ts ){ |
klauss | 0:4d17cd9c8f9d | 207 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 208 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 29:7246460b73f8 | 209 | if( call->is_timetofinish() ){ |
klauss | 0:4d17cd9c8f9d | 210 | v_call->remove_element( i ); |
klauss | 0:4d17cd9c8f9d | 211 | Call_Box * cb = __find_CB__( v_cb, call->get_cb_ext() ); |
klauss | 0:4d17cd9c8f9d | 212 | if( cb ){ |
klauss | 0:4d17cd9c8f9d | 213 | cb->status = cb_idle; |
klauss | 0:4d17cd9c8f9d | 214 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 215 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 216 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 217 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 0:4d17cd9c8f9d | 218 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 219 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 220 | cb->send_bye(); |
klauss | 13:ae278302dffe | 221 | set_status( cb->sip->status, sip_idle ); |
klauss | 29:7246460b73f8 | 222 | cb->re_start_timer(); |
klauss | 0:4d17cd9c8f9d | 223 | } |
klauss | 9:ffa64f38ef9c | 224 | delete( call ); |
klauss | 0:4d17cd9c8f9d | 225 | } |
klauss | 0:4d17cd9c8f9d | 226 | } |
klauss | 0:4d17cd9c8f9d | 227 | } |
klauss | 0:4d17cd9c8f9d | 228 | |
klauss | 0:4d17cd9c8f9d | 229 | void build_telemetry_report( int ext, int port, char * data ){ |
klauss | 58:af7e8788f106 | 230 | char aux[ __CB_BUFFER_SIZE__ + 6 ]; |
klauss | 53:bb492a8f115a | 231 | char tmp[ 6 ]; |
klauss | 53:bb492a8f115a | 232 | strcpy( aux, itoa( ext, tmp, 10 ) ); |
klauss | 53:bb492a8f115a | 233 | strcat( aux, " " ); |
klauss | 55:2f5e7374af9d | 234 | |
klauss | 55:2f5e7374af9d | 235 | //strncat( aux, (char * )data, __TELEMETRY_SIZE__ ); |
klauss | 68:b54993674190 | 236 | /* |
klauss | 68:b54993674190 | 237 | pc.printf("\n\r 1. "); |
klauss | 68:b54993674190 | 238 | uint8_t count = 2; |
klauss | 68:b54993674190 | 239 | for( register int i = 0; i < __CB_BUFFER_SIZE__; i++ ){ |
klauss | 68:b54993674190 | 240 | if( i > 0 && !( i % 15 ) ) pc.printf("\n\r%2i. ", count++ ); |
klauss | 68:b54993674190 | 241 | pc.printf("%3x ", data[ i ] ); |
klauss | 68:b54993674190 | 242 | } |
klauss | 68:b54993674190 | 243 | pc.printf("\n\r"); |
klauss | 68:b54993674190 | 244 | */ |
klauss | 68:b54993674190 | 245 | //pulando o byte de id ( sequence number ) |
klauss | 68:b54993674190 | 246 | data += __SEQ_NUM_SIZE__; |
klauss | 55:2f5e7374af9d | 247 | //pulando os bytes do clock |
klauss | 55:2f5e7374af9d | 248 | data += __CLOCK_SYNC_SIZE__; |
klauss | 58:af7e8788f106 | 249 | |
klauss | 58:af7e8788f106 | 250 | uint8_t offset = strlen( aux ); |
klauss | 55:2f5e7374af9d | 251 | |
klauss | 58:af7e8788f106 | 252 | for( register uint16_t i = 0; i < __TELEMETRY_SIZE__; i++ ) aux[ i + offset ] = data[ i ]; |
klauss | 58:af7e8788f106 | 253 | |
klauss | 58:af7e8788f106 | 254 | for( register uint16_t i = __TELEMETRY_SIZE__ + offset; i < __CB_BUFFER_SIZE__; i++ ) aux[ i ] = 0; |
klauss | 53:bb492a8f115a | 255 | |
klauss | 69:65665afbad5d | 256 | int sent = t_sock.sendTo( t_server, aux, __CB_BUFFER_SIZE__ ); |
klauss | 68:b54993674190 | 257 | if( debug_telemetry ) send_msg(" Valor de retorno sent-- %d ", sent ); |
klauss | 68:b54993674190 | 258 | |
klauss | 68:b54993674190 | 259 | if( sent == -1 ){ |
klauss | 68:b54993674190 | 260 | re_start_telemetry(); |
klauss | 68:b54993674190 | 261 | sent = t_sock.sendTo( t_server, aux, __CB_BUFFER_SIZE__ ); |
klauss | 68:b54993674190 | 262 | |
klauss | 68:b54993674190 | 263 | if( debug_telemetry ) send_msg(" Valor de retorno sent-- %d ", sent ); |
klauss | 68:b54993674190 | 264 | } |
klauss | 68:b54993674190 | 265 | |
klauss | 54:448d57291be6 | 266 | if( debug_telemetry ){ |
klauss | 54:448d57291be6 | 267 | pc.printf("\n\r 1. "); |
klauss | 54:448d57291be6 | 268 | uint8_t count = 2; |
klauss | 54:448d57291be6 | 269 | for( register int i = 0; i < __CB_BUFFER_SIZE__; i++ ){ |
klauss | 58:af7e8788f106 | 270 | if( i > 0 && !( i % 15 ) ) pc.printf("\n\r%2i. ", count++ ); |
klauss | 58:af7e8788f106 | 271 | pc.printf("%3x ", aux[ i ] ); |
klauss | 54:448d57291be6 | 272 | } |
klauss | 54:448d57291be6 | 273 | pc.printf("\n\r"); |
klauss | 54:448d57291be6 | 274 | |
klauss | 68:b54993674190 | 275 | char msg_to_eth[ 1024 ]; |
klauss | 68:b54993674190 | 276 | strcpy( msg_to_eth, tmp ); |
klauss | 68:b54993674190 | 277 | strcat( msg_to_eth, " " ); |
klauss | 68:b54993674190 | 278 | for( register uint16_t i = 0; i < __TELEMETRY_SIZE__; i++ ){ |
klauss | 68:b54993674190 | 279 | strcat( msg_to_eth, itoa( data[ i ], tmp, 16 ) ); |
klauss | 68:b54993674190 | 280 | strcat( msg_to_eth, " " ); |
klauss | 68:b54993674190 | 281 | } |
klauss | 68:b54993674190 | 282 | for( register uint16_t i = __TELEMETRY_SIZE__; i < __CB_BUFFER_SIZE__ - 1; i++ ){ |
klauss | 68:b54993674190 | 283 | strcat( msg_to_eth, itoa( 0, tmp, 16 ) ); |
klauss | 68:b54993674190 | 284 | strcat( msg_to_eth, " " ); |
klauss | 68:b54993674190 | 285 | } |
klauss | 68:b54993674190 | 286 | strcat( msg_to_eth, itoa( 0, tmp, 16 ) ); |
klauss | 68:b54993674190 | 287 | |
klauss | 68:b54993674190 | 288 | if( tcp_session ){ |
klauss | 68:b54993674190 | 289 | int debug_sent = tcp_client.send_all( msg_to_eth, strlen( msg_to_eth ) ); |
klauss | 68:b54993674190 | 290 | if( debug_telemetry ) send_msg("%d - bytes enviados pro prompt-eth", debug_sent ); |
klauss | 68:b54993674190 | 291 | } |
klauss | 53:bb492a8f115a | 292 | } |
klauss | 0:4d17cd9c8f9d | 293 | } |
klauss | 0:4d17cd9c8f9d | 294 | |
klauss | 0:4d17cd9c8f9d | 295 | void __send_to_cb__( uint8_t * buffer ){ |
klauss | 72:895ca792c647 | 296 | if( tx_clear == 1 ){ |
klauss | 72:895ca792c647 | 297 | tx_clear = 0; |
klauss | 72:895ca792c647 | 298 | xmemcpy( TXBuffer, buffer, DATA_SIZE ); |
klauss | 72:895ca792c647 | 299 | parallel_write( TXBuffer[ 0 ] ); |
klauss | 72:895ca792c647 | 300 | DataReady = 1; |
klauss | 72:895ca792c647 | 301 | xmemcpy( cb_tx_buffer, buffer, __CB_BUFFER_SIZE__ ); |
klauss | 72:895ca792c647 | 302 | //send2callboxes(); |
klauss | 72:895ca792c647 | 303 | }else{ |
klauss | 72:895ca792c647 | 304 | uint8_t ret = ring_buffer_add( rb, buffer ); |
klauss | 72:895ca792c647 | 305 | |
klauss | 72:895ca792c647 | 306 | if( ret == 0x01 ) if( dparallel ) send_msg("Error: Ring buffer fully charged"); |
klauss | 72:895ca792c647 | 307 | |
klauss | 72:895ca792c647 | 308 | if( ret == 0x00 ) if( dparallel ) send_msg("Success : package queued -- on queue %u", rb->size ); |
klauss | 72:895ca792c647 | 309 | } |
klauss | 62:07e5bdc9f8f7 | 310 | } |
klauss | 72:895ca792c647 | 311 | |
klauss | 72:895ca792c647 | 312 | void tx_buffer_ring_buffer_handler( void ){ |
klauss | 72:895ca792c647 | 313 | if( rb->size != 0x00 ){ |
klauss | 72:895ca792c647 | 314 | if( tx_clear == 1 ){ |
klauss | 72:895ca792c647 | 315 | uint8_t * buffer = ring_buffer_get_next( rb ); |
klauss | 72:895ca792c647 | 316 | xmemcpy( TXBuffer, buffer, DATA_SIZE ); |
klauss | 72:895ca792c647 | 317 | xmemcpy( cb_tx_buffer, buffer, __CB_BUFFER_SIZE__ ); |
klauss | 72:895ca792c647 | 318 | if( dparallel ) send_msg("Ring Buffer less one -- remain %u", rb->size ); |
klauss | 72:895ca792c647 | 319 | send2callboxes(); |
klauss | 72:895ca792c647 | 320 | } |
klauss | 72:895ca792c647 | 321 | } |
klauss | 72:895ca792c647 | 322 | } |
klauss | 72:895ca792c647 | 323 | |
klauss | 68:b54993674190 | 324 | void init_telemetry_handler( void ){ |
klauss | 68:b54993674190 | 325 | char buff_ip[ 16 ] = ""; |
klauss | 68:b54993674190 | 326 | char buff_port[ 8 ] = ""; |
klauss | 68:b54993674190 | 327 | int telemetry_port = 0; |
klauss | 68:b54993674190 | 328 | |
klauss | 68:b54993674190 | 329 | FILE *fp = fopen( "/qspi/telemetry_ip.txt", "r"); |
klauss | 68:b54993674190 | 330 | if( fp == NULL ){ |
klauss | 68:b54993674190 | 331 | if( debug_file ) debug_msg("Failed to open /qspi/telemetry_ip.txt" ); |
klauss | 68:b54993674190 | 332 | strncpy( buff_ip, __TELEMETRY_SERVER_IP__, 20 ); |
klauss | 68:b54993674190 | 333 | }else{ |
klauss | 68:b54993674190 | 334 | int read = fread( buff_ip, 1, 512, fp ); |
klauss | 68:b54993674190 | 335 | if( read > 0 ){ |
klauss | 68:b54993674190 | 336 | for( int i = 0; i < read; i++ ) |
klauss | 68:b54993674190 | 337 | if( buff_ip[ i ] == '\n' || buff_ip[ i ] == '\r' ){ |
klauss | 68:b54993674190 | 338 | buff_ip[ i ] = '\0'; |
klauss | 68:b54993674190 | 339 | break; |
klauss | 68:b54993674190 | 340 | } |
klauss | 68:b54993674190 | 341 | if( debug_file ) debug_msg("Telemetry server ip %s", buff_ip ); |
klauss | 68:b54993674190 | 342 | }else{ |
klauss | 68:b54993674190 | 343 | if( debug_file ) debug_msg("Failed to open /qspi/telemetry_ip.txt" ); |
klauss | 68:b54993674190 | 344 | strncpy( buff_ip, __TELEMETRY_SERVER_IP__, 20 ); |
klauss | 68:b54993674190 | 345 | } |
klauss | 68:b54993674190 | 346 | } |
klauss | 68:b54993674190 | 347 | fclose( fp ); |
klauss | 68:b54993674190 | 348 | |
klauss | 68:b54993674190 | 349 | fp = fopen( "/qspi/telemetry_port.txt", "r"); |
klauss | 68:b54993674190 | 350 | if( fp == NULL ) { |
klauss | 68:b54993674190 | 351 | if( debug_sip ) debug_msg("Failed to open /qspi/telemetry_port.txt" ); |
klauss | 68:b54993674190 | 352 | telemetry_port = __TELEMETRY_SERVER_PORT__; |
klauss | 68:b54993674190 | 353 | } else { |
klauss | 68:b54993674190 | 354 | if( fread( (void *)buff_port, 1, 32, fp ) > 0 ) { |
klauss | 68:b54993674190 | 355 | telemetry_port = atoi( buff_port ); |
klauss | 68:b54993674190 | 356 | if( debug_sip ) debug_msg("Telemetry server port %d", buff_port ); |
klauss | 68:b54993674190 | 357 | } else { |
klauss | 68:b54993674190 | 358 | if( debug_sip ) debug_msg("Failed to read /qspi/telemetry_port.txt" ); |
klauss | 68:b54993674190 | 359 | telemetry_port = __TELEMETRY_SERVER_PORT__; |
klauss | 68:b54993674190 | 360 | } |
klauss | 68:b54993674190 | 361 | } |
klauss | 68:b54993674190 | 362 | fclose( fp ); |
klauss | 68:b54993674190 | 363 | |
klauss | 68:b54993674190 | 364 | //int addr = t_server.set_address( __TELEMETRY_SERVER_IP__ , __TELEMETRY_SERVER_PORT__ ); |
klauss | 68:b54993674190 | 365 | int addr = t_server.set_address( buff_ip , telemetry_port ); |
klauss | 68:b54993674190 | 366 | if( debug_telemetry ) send_msg(" Valor de retorno set_address -- %d ", addr ); |
klauss | 68:b54993674190 | 367 | t_sock.set_blocking( false, 1 ); |
klauss | 68:b54993674190 | 368 | |
klauss | 68:b54993674190 | 369 | int bind = t_sock.bind( __TELEMETRY_HEADER_PORT__ ); |
klauss | 68:b54993674190 | 370 | if( debug_telemetry ) send_msg(" Valor de retorno bind -- %d ", bind ); |
klauss | 68:b54993674190 | 371 | } |
klauss | 68:b54993674190 | 372 | |
klauss | 68:b54993674190 | 373 | void re_start_telemetry( void ){ |
klauss | 68:b54993674190 | 374 | int close = t_sock.close(); |
klauss | 68:b54993674190 | 375 | if( debug_telemetry ) send_msg(" Valor de retorno close-- %d ", close ); |
klauss | 68:b54993674190 | 376 | |
klauss | 68:b54993674190 | 377 | init_telemetry_handler(); |
klauss | 68:b54993674190 | 378 | } |
klauss | 62:07e5bdc9f8f7 | 379 | |
klauss | 62:07e5bdc9f8f7 | 380 | int ls_comp( const void * a, const void * b ){ return( *( int * )a - *( int * )b ); } |