![](/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@38:af5ecd6997ab, 2014-10-01 (annotated)
- Committer:
- klauss
- Date:
- Wed Oct 01 19:14:38 2014 +0000
- Revision:
- 38:af5ecd6997ab
- Parent:
- 36:728498a78e1e
- Child:
- 43:455522f98de5
inicio da migracao de outputs para porta udp especifica
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 | 0:4d17cd9c8f9d | 4 | void reverse( char str[], int length ){ |
klauss | 0:4d17cd9c8f9d | 5 | int start = 0; |
klauss | 0:4d17cd9c8f9d | 6 | int end = length -1; |
klauss | 0:4d17cd9c8f9d | 7 | while (start < end){ |
klauss | 0:4d17cd9c8f9d | 8 | swap(*(str+start), *(str+end)); |
klauss | 0:4d17cd9c8f9d | 9 | start++; |
klauss | 0:4d17cd9c8f9d | 10 | end--; |
klauss | 0:4d17cd9c8f9d | 11 | } |
klauss | 0:4d17cd9c8f9d | 12 | } |
klauss | 0:4d17cd9c8f9d | 13 | char* itoa(int num, char* str, int base){ |
klauss | 0:4d17cd9c8f9d | 14 | int i = 0; |
klauss | 0:4d17cd9c8f9d | 15 | bool isNegative = false; |
klauss | 0:4d17cd9c8f9d | 16 | |
klauss | 0:4d17cd9c8f9d | 17 | if( num == 0 ){ |
klauss | 0:4d17cd9c8f9d | 18 | str[i++] = '0'; |
klauss | 0:4d17cd9c8f9d | 19 | str[i] = '\0'; |
klauss | 0:4d17cd9c8f9d | 20 | return str; |
klauss | 0:4d17cd9c8f9d | 21 | } |
klauss | 0:4d17cd9c8f9d | 22 | if( num < 0 && base == 10 ){ |
klauss | 0:4d17cd9c8f9d | 23 | isNegative = true; |
klauss | 0:4d17cd9c8f9d | 24 | num = -num; |
klauss | 0:4d17cd9c8f9d | 25 | } |
klauss | 0:4d17cd9c8f9d | 26 | while( num != 0 ){ |
klauss | 0:4d17cd9c8f9d | 27 | int rem = num % base; |
klauss | 0:4d17cd9c8f9d | 28 | str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0'; |
klauss | 0:4d17cd9c8f9d | 29 | num = num/base; |
klauss | 0:4d17cd9c8f9d | 30 | } |
klauss | 0:4d17cd9c8f9d | 31 | if( isNegative ) |
klauss | 0:4d17cd9c8f9d | 32 | str[i++] = '-'; |
klauss | 0:4d17cd9c8f9d | 33 | |
klauss | 0:4d17cd9c8f9d | 34 | str[i] = '\0'; // Append string terminator |
klauss | 0:4d17cd9c8f9d | 35 | |
klauss | 0:4d17cd9c8f9d | 36 | reverse( str, i ); |
klauss | 0:4d17cd9c8f9d | 37 | return( str ); |
klauss | 0:4d17cd9c8f9d | 38 | } |
klauss | 0:4d17cd9c8f9d | 39 | |
klauss | 0:4d17cd9c8f9d | 40 | void reset_leds( void ){ |
klauss | 0:4d17cd9c8f9d | 41 | led1 = led2 = 1; |
klauss | 0:4d17cd9c8f9d | 42 | led3 = led4 = 0; |
klauss | 0:4d17cd9c8f9d | 43 | } |
klauss | 0:4d17cd9c8f9d | 44 | |
klauss | 8:e3bfe62a477e | 45 | int __init_eth__( EthernetInterface * eth ){ |
klauss | 27:98f824719d1c | 46 | char buff_ip[ 16 ]; |
klauss | 27:98f824719d1c | 47 | char buff_msk[ 16 ]; |
klauss | 27:98f824719d1c | 48 | char buff_gtw[ 16 ]; |
klauss | 28:94aec56c6329 | 49 | pc.printf("\n\r"); |
klauss | 27:98f824719d1c | 50 | FILE *fp = fopen( "/qspi/myip.txt", "r"); |
klauss | 27:98f824719d1c | 51 | if( fp == NULL ){ |
klauss | 31:bb5fb28a77df | 52 | if( debug_file ) debug_msg("Failed to open /qspi/myip.txt" ); |
klauss | 27:98f824719d1c | 53 | strncpy( buff_ip, __MY_IP__, 20 ); |
klauss | 27:98f824719d1c | 54 | }else{ |
klauss | 27:98f824719d1c | 55 | if( fread( buff_ip, 1, 512, fp ) > 0 ){ |
klauss | 31:bb5fb28a77df | 56 | if( debug_file ) debug_msg("Eth ip %s", buff_ip ); |
klauss | 27:98f824719d1c | 57 | }else{ |
klauss | 31:bb5fb28a77df | 58 | if( debug_file ) if( debug_file ) debug_msg("Failed to read /qspi/myip.txt" ); |
klauss | 27:98f824719d1c | 59 | strncpy( buff_ip, __MY_IP__, 20 ); |
klauss | 27:98f824719d1c | 60 | } |
klauss | 27:98f824719d1c | 61 | } |
klauss | 27:98f824719d1c | 62 | fclose( fp ); |
klauss | 27:98f824719d1c | 63 | buff_ip[ 15 ] = 0; |
klauss | 27:98f824719d1c | 64 | |
klauss | 15:9c7456c1b6f4 | 65 | |
klauss | 27:98f824719d1c | 66 | fp = fopen( "/qspi/mymask.txt", "r"); |
klauss | 27:98f824719d1c | 67 | if( fp == NULL ){ |
klauss | 31:bb5fb28a77df | 68 | if( debug_file ) debug_msg("Failed to open /qspi/mymask.txt" ); |
klauss | 28:94aec56c6329 | 69 | strncpy( buff_msk, __MY_MSK__, 20 ); |
klauss | 27:98f824719d1c | 70 | }else{ |
klauss | 27:98f824719d1c | 71 | if( fread( buff_msk, 1, 512, fp ) > 0 ){ |
klauss | 31:bb5fb28a77df | 72 | if( debug_file ) debug_msg("mascara de rede Eth %s", buff_msk ); |
klauss | 27:98f824719d1c | 73 | }else{ |
klauss | 31:bb5fb28a77df | 74 | if( debug_file ) debug_msg("Failed to read /qspi/mymask.txt" ); |
klauss | 28:94aec56c6329 | 75 | strncpy( buff_msk, __MY_MSK__, 20 ); |
klauss | 27:98f824719d1c | 76 | } |
klauss | 27:98f824719d1c | 77 | } |
klauss | 27:98f824719d1c | 78 | fclose( fp ); |
klauss | 27:98f824719d1c | 79 | buff_msk[ 15 ] = 0; |
klauss | 27:98f824719d1c | 80 | |
klauss | 27:98f824719d1c | 81 | |
klauss | 27:98f824719d1c | 82 | fp = fopen( "/qspi/mygate.txt", "r"); |
klauss | 27:98f824719d1c | 83 | if( fp == NULL ){ |
klauss | 31:bb5fb28a77df | 84 | if( debug_file ) debug_msg("Failed to open /qspi/mygate.txt" ); |
klauss | 28:94aec56c6329 | 85 | strncpy( buff_gtw, __MY_GTW__, 20 ); |
klauss | 27:98f824719d1c | 86 | }else{ |
klauss | 27:98f824719d1c | 87 | if( fread( buff_gtw, 1, 512, fp ) > 0 ){ |
klauss | 31:bb5fb28a77df | 88 | if( debug_file ) debug_msg("Ip Gateway Eth %s", buff_gtw ); |
klauss | 27:98f824719d1c | 89 | }else{ |
klauss | 31:bb5fb28a77df | 90 | if( debug_file ) debug_msg("Failed to read /qspi/mygate.txt" ); |
klauss | 28:94aec56c6329 | 91 | strncpy( buff_gtw, __MY_GTW__, 20 ); |
klauss | 27:98f824719d1c | 92 | } |
klauss | 27:98f824719d1c | 93 | } |
klauss | 27:98f824719d1c | 94 | fclose( fp ); |
klauss | 27:98f824719d1c | 95 | buff_gtw[ 15 ] = 0; |
klauss | 27:98f824719d1c | 96 | |
klauss | 27:98f824719d1c | 97 | eth->init( buff_ip, buff_msk, buff_gtw ); |
klauss | 8:e3bfe62a477e | 98 | return eth->connect(); |
klauss | 0:4d17cd9c8f9d | 99 | } |
klauss | 0:4d17cd9c8f9d | 100 | |
klauss | 0:4d17cd9c8f9d | 101 | Call_Box * __find_CB__( Vector * v_cb, int ext ){ |
klauss | 0:4d17cd9c8f9d | 102 | Call_Box * cb = NULL; |
klauss | 0:4d17cd9c8f9d | 103 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 104 | cb = ( Call_Box * )v_cb->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 105 | if( cb->get_ext() == ext ) return( cb ); |
klauss | 0:4d17cd9c8f9d | 106 | } |
klauss | 0:4d17cd9c8f9d | 107 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 108 | } |
klauss | 0:4d17cd9c8f9d | 109 | |
klauss | 0:4d17cd9c8f9d | 110 | VZ_call * __find_Call__( Vector * v_call, int ext ){ |
klauss | 0:4d17cd9c8f9d | 111 | VZ_call * call = NULL; |
klauss | 0:4d17cd9c8f9d | 112 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 113 | call = ( VZ_call * )v_call->get_element( i ); |
klauss | 0:4d17cd9c8f9d | 114 | if( call->get_cb_ext() == ext ) return( call ); |
klauss | 0:4d17cd9c8f9d | 115 | } |
klauss | 0:4d17cd9c8f9d | 116 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 117 | } |
klauss | 0:4d17cd9c8f9d | 118 | |
klauss | 0:4d17cd9c8f9d | 119 | void registry_aging( Vector * v_cb, uint8_t * data, uint8_t * write_buffer ){ |
klauss | 7:019b08223b87 | 120 | Call_Box * cb = NULL; |
klauss | 7:019b08223b87 | 121 | if( v_cb && data && write_buffer ){ |
klauss | 7:019b08223b87 | 122 | for( register int i = 0; i < v_cb->size(); i++ ){ |
klauss | 7:019b08223b87 | 123 | cb = (Call_Box * )v_cb->get_element( i ); |
klauss | 7:019b08223b87 | 124 | if( cb->is_timeout() ){ |
klauss | 7:019b08223b87 | 125 | if( cb->reconfigure_timeout() == 0x00 ){ |
klauss | 30:8dfb6d8de53d | 126 | if( debug_alive ) debug_msg("( ext %d removed )", cb->get_ext() ); |
klauss | 32:43041f5e27c4 | 127 | //cb->unregistry(); |
klauss | 30:8dfb6d8de53d | 128 | v_cb->remove_element( i ); |
klauss | 30:8dfb6d8de53d | 129 | delete( cb ); |
klauss | 7:019b08223b87 | 130 | break; |
klauss | 7:019b08223b87 | 131 | }else{ |
klauss | 31:bb5fb28a77df | 132 | if( debug_alive ) debug_msg("Ping Cbx %d", cb->get_ext() ); |
klauss | 28:94aec56c6329 | 133 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 7:019b08223b87 | 134 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __REGISTRY__, |
klauss | 7:019b08223b87 | 135 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 7:019b08223b87 | 136 | cb->registry(); |
klauss | 7:019b08223b87 | 137 | } |
klauss | 7:019b08223b87 | 138 | } |
klauss | 7:019b08223b87 | 139 | } |
klauss | 7:019b08223b87 | 140 | } |
klauss | 0:4d17cd9c8f9d | 141 | } |
klauss | 0:4d17cd9c8f9d | 142 | |
klauss | 0:4d17cd9c8f9d | 143 | int sip_manager( Vector * v_cb, Vector * v_call, uint8_t * write_buffer ){ |
klauss | 0:4d17cd9c8f9d | 144 | static int index = 0; |
klauss | 0:4d17cd9c8f9d | 145 | if( v_cb && write_buffer ){ |
klauss | 0:4d17cd9c8f9d | 146 | if( v_cb->size() > 0 ){ |
klauss | 0:4d17cd9c8f9d | 147 | if( index >= v_cb->size() ) index = 0; |
klauss | 0:4d17cd9c8f9d | 148 | }else return 0; |
klauss | 0:4d17cd9c8f9d | 149 | Call_Box * cb = (Call_Box * )v_cb->get_element( index ); |
klauss | 0:4d17cd9c8f9d | 150 | /* Retorna |
klauss | 0:4d17cd9c8f9d | 151 | = 0 :: ok |
klauss | 0:4d17cd9c8f9d | 152 | < 0 :: tive problemas |
klauss | 0:4d17cd9c8f9d | 153 | > 0 :: devo remover essa call do vetor de calls |
klauss | 0:4d17cd9c8f9d | 154 | */ |
klauss | 0:4d17cd9c8f9d | 155 | int returned_value = 0; |
klauss | 13:ae278302dffe | 156 | |
klauss | 14:22a35f575502 | 157 | if( cb->status == cb_on_call || cb->status == cb_idle ){ |
klauss | 15:9c7456c1b6f4 | 158 | //debug_msg(""); |
klauss | 14:22a35f575502 | 159 | returned_value = cb->listen_SIP_server(); |
klauss | 14:22a35f575502 | 160 | } |
klauss | 0:4d17cd9c8f9d | 161 | index++; |
klauss | 0:4d17cd9c8f9d | 162 | return( returned_value ); |
klauss | 0:4d17cd9c8f9d | 163 | } |
klauss | 0:4d17cd9c8f9d | 164 | led1 = !led1; |
klauss | 0:4d17cd9c8f9d | 165 | return( -1 ); |
klauss | 0:4d17cd9c8f9d | 166 | } |
klauss | 0:4d17cd9c8f9d | 167 | |
klauss | 0:4d17cd9c8f9d | 168 | /* remove calls por timeout */ |
klauss | 0:4d17cd9c8f9d | 169 | void call_manager( Vector * v_call, Vector * v_cb, uint8_t * data, uint8_t * write_buffer, Timeslice * ts ){ |
klauss | 0:4d17cd9c8f9d | 170 | for( register int i = 0; i < v_call->size(); i++ ){ |
klauss | 0:4d17cd9c8f9d | 171 | VZ_call * call = ( VZ_call * )v_call->get_element( i ); |
klauss | 29:7246460b73f8 | 172 | if( call->is_timetofinish() ){ |
klauss | 0:4d17cd9c8f9d | 173 | v_call->remove_element( i ); |
klauss | 0:4d17cd9c8f9d | 174 | Call_Box * cb = __find_CB__( v_cb, call->get_cb_ext() ); |
klauss | 0:4d17cd9c8f9d | 175 | if( cb ){ |
klauss | 0:4d17cd9c8f9d | 176 | cb->status = cb_idle; |
klauss | 0:4d17cd9c8f9d | 177 | ts->return_timeslice( cb->get_timeslice() ); |
klauss | 0:4d17cd9c8f9d | 178 | cb->set_timeslice( 0x00 ); |
klauss | 0:4d17cd9c8f9d | 179 | data[ __TIMESLICE_PLACE__ ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 180 | cb->set_msg_id( ( ( cb->get_msg_id() ) + 1 ) & ( BIT7 ^ 0xff ) ); |
klauss | 0:4d17cd9c8f9d | 181 | __send_to_cb__( __build_cb_package__( cb->get_ext(), cb->get_port(), __CB_BYE__, |
klauss | 0:4d17cd9c8f9d | 182 | ( char * )data, cb->get_msg_id(), __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__, write_buffer ) ); |
klauss | 0:4d17cd9c8f9d | 183 | cb->send_bye(); |
klauss | 13:ae278302dffe | 184 | set_status( cb->sip->status, sip_idle ); |
klauss | 29:7246460b73f8 | 185 | cb->re_start_timer(); |
klauss | 0:4d17cd9c8f9d | 186 | } |
klauss | 9:ffa64f38ef9c | 187 | delete( call ); |
klauss | 0:4d17cd9c8f9d | 188 | } |
klauss | 0:4d17cd9c8f9d | 189 | } |
klauss | 0:4d17cd9c8f9d | 190 | } |
klauss | 0:4d17cd9c8f9d | 191 | |
klauss | 0:4d17cd9c8f9d | 192 | void build_telemetry_report( int ext, int port, char * data ){ |
klauss | 36:728498a78e1e | 193 | pc.printf("Telemetry pkg from ( %i, %i ) ::\n\r", ext, port ); |
klauss | 31:bb5fb28a77df | 194 | for( register int i = 0; i < __TELEMETRY_SIZE__; i++ ) |
klauss | 31:bb5fb28a77df | 195 | pc.printf("%3i ", data[ i ] ); |
klauss | 31:bb5fb28a77df | 196 | pc.printf("\n\r"); |
klauss | 0:4d17cd9c8f9d | 197 | } |
klauss | 0:4d17cd9c8f9d | 198 | |
klauss | 0:4d17cd9c8f9d | 199 | void __send_to_cb__( uint8_t * buffer ){ |
klauss | 0:4d17cd9c8f9d | 200 | xmemcpy( TXBuffer, buffer, DATA_SIZE ); |
klauss | 0:4d17cd9c8f9d | 201 | send2callboxes(); |
klauss | 0:4d17cd9c8f9d | 202 | } |