![](/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
vz_protocol.cpp@92:92df17f538a8, 2015-01-10 (annotated)
- Committer:
- klauss
- Date:
- Sat Jan 10 16:35:11 2015 +0000
- Revision:
- 92:92df17f538a8
- Parent:
- 89:0fe315117b00
- Child:
- 98:43b45f26b430
with supress
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 0:4d17cd9c8f9d | 1 | #include "vz_protocol.h" |
klauss | 72:895ca792c647 | 2 | |
klauss | 78:1353744f01e1 | 3 | uint32_t pkg_ckserr = 0; |
klauss | 78:1353744f01e1 | 4 | uint32_t pkg_zero = 0; |
klauss | 78:1353744f01e1 | 5 | uint32_t pkg_cksok = 0; |
klauss | 81:3656f00ab3db | 6 | int begin = 0; |
klauss | 81:3656f00ab3db | 7 | int end = 0; |
klauss | 81:3656f00ab3db | 8 | |
klauss | 81:3656f00ab3db | 9 | void init_ranges( void ){ |
klauss | 81:3656f00ab3db | 10 | FILE * begin_ext = fopen( "/qspi/minext.txt", "r" ); |
klauss | 81:3656f00ab3db | 11 | if( begin_ext == NULL ){ |
klauss | 81:3656f00ab3db | 12 | if( debug_out_of_range ) debug_msg("Failed to open /qspi/minext.txt" ); |
klauss | 81:3656f00ab3db | 13 | begin = MIN_EXT; |
klauss | 81:3656f00ab3db | 14 | }else{ |
klauss | 81:3656f00ab3db | 15 | char inner_tmp[ 20 ]; |
klauss | 81:3656f00ab3db | 16 | if( fread( (void *)inner_tmp, 1, 20, begin_ext ) > 0 ){ |
klauss | 81:3656f00ab3db | 17 | begin = atoi( inner_tmp ); |
klauss | 81:3656f00ab3db | 18 | if( debug_out_of_range ) debug_msg("Ext Min %d", begin ); |
klauss | 81:3656f00ab3db | 19 | }else{ |
klauss | 81:3656f00ab3db | 20 | if( debug_out_of_range ) debug_msg("Failed to read /qspi/minext.txt" ); |
klauss | 81:3656f00ab3db | 21 | begin = MIN_EXT; |
klauss | 81:3656f00ab3db | 22 | } |
klauss | 81:3656f00ab3db | 23 | } |
klauss | 81:3656f00ab3db | 24 | fclose( begin_ext ); |
klauss | 81:3656f00ab3db | 25 | |
klauss | 81:3656f00ab3db | 26 | FILE * end_ext = fopen( "/qspi/maxext.txt", "r" ); |
klauss | 81:3656f00ab3db | 27 | if( end_ext == NULL ){ |
klauss | 81:3656f00ab3db | 28 | if( debug_out_of_range ) debug_msg("Failed to open /qspi/maxext.txt" ); |
klauss | 81:3656f00ab3db | 29 | end = MAX_EXT; |
klauss | 81:3656f00ab3db | 30 | }else{ |
klauss | 81:3656f00ab3db | 31 | char inner_tmp[ 20 ]; |
klauss | 81:3656f00ab3db | 32 | if( fread( (void *)inner_tmp, 1, 20, end_ext ) > 0 ){ |
klauss | 81:3656f00ab3db | 33 | end = atoi( inner_tmp ); |
klauss | 81:3656f00ab3db | 34 | if( debug_out_of_range ) debug_msg("Ext Min %d", end ); |
klauss | 81:3656f00ab3db | 35 | }else{ |
klauss | 81:3656f00ab3db | 36 | if( debug_out_of_range ) debug_msg("Failed to read /qspi/maxext.txt" ); |
klauss | 81:3656f00ab3db | 37 | begin = MAX_EXT; |
klauss | 81:3656f00ab3db | 38 | } |
klauss | 81:3656f00ab3db | 39 | } |
klauss | 89:0fe315117b00 | 40 | fclose( end_ext ); |
klauss | 81:3656f00ab3db | 41 | } |
Cola | 20:2d6ab23956e5 | 42 | |
klauss | 34:f19d9735428e | 43 | uint8_t * __parse_vz_pkg__( int * ext, int * port, volatile uint8_t * type, uint8_t * cb_buffer ){ |
klauss | 0:4d17cd9c8f9d | 44 | uint8_t p_lsb, p_msb; |
klauss | 0:4d17cd9c8f9d | 45 | uint8_t e_lsb, e_msb; |
klauss | 0:4d17cd9c8f9d | 46 | |
klauss | 81:3656f00ab3db | 47 | if( ( ext && port && type && cb_buffer ) == NULL ) return( NULL ); |
klauss | 58:af7e8788f106 | 48 | |
klauss | 63:0d95da692bb4 | 49 | if( cb_buffer[ 6 ] == __FLOOD__ ){ |
klauss | 63:0d95da692bb4 | 50 | flood_counter++; |
Cola | 21:0bd688722e81 | 51 | static uint8_t flood_cnt = 0; |
Cola | 21:0bd688722e81 | 52 | static uint8_t first_run_flag = 0; |
Cola | 21:0bd688722e81 | 53 | static Timer t; |
Cola | 21:0bd688722e81 | 54 | if (first_run_flag == 0) { |
Cola | 21:0bd688722e81 | 55 | flood_cnt = cb_buffer[0]; |
Cola | 21:0bd688722e81 | 56 | first_run_flag++; |
Cola | 21:0bd688722e81 | 57 | t.start(); |
Cola | 21:0bd688722e81 | 58 | } else { |
Cola | 21:0bd688722e81 | 59 | if (cb_buffer[0] == 0x00){ |
Cola | 21:0bd688722e81 | 60 | pc.printf("%d", t.read_us()); |
klauss | 48:195c97f12e8e | 61 | if( tcp_session ){ |
klauss | 48:195c97f12e8e | 62 | char tmp[ 16 ]; |
klauss | 48:195c97f12e8e | 63 | sprintf( tmp, "%d", t.read_us() ); |
klauss | 48:195c97f12e8e | 64 | tcp_client.send_all( tmp, strlen( tmp ) ); |
Cola | 21:0bd688722e81 | 65 | } |
klauss | 48:195c97f12e8e | 66 | } |
Cola | 21:0bd688722e81 | 67 | flood_cnt++; |
Cola | 21:0bd688722e81 | 68 | while (flood_cnt != cb_buffer[0]) { |
Cola | 21:0bd688722e81 | 69 | pc.printf("0"); |
Cola | 21:0bd688722e81 | 70 | flood_cnt++; |
klauss | 48:195c97f12e8e | 71 | if( tcp_session ){ |
klauss | 48:195c97f12e8e | 72 | tcp_client.send_all( "0", strlen( "0" ) ); |
klauss | 48:195c97f12e8e | 73 | } |
Cola | 21:0bd688722e81 | 74 | } |
Cola | 21:0bd688722e81 | 75 | int i; |
Cola | 21:0bd688722e81 | 76 | int ok = 1; |
Cola | 21:0bd688722e81 | 77 | uint8_t cmp; |
Cola | 21:0bd688722e81 | 78 | for (i=0, cmp=cb_buffer[0]; (i<300) && (ok==1); i++, cmp++) { |
Cola | 21:0bd688722e81 | 79 | if (i != 6) { |
Cola | 21:0bd688722e81 | 80 | if (cb_buffer[i] != cmp) ok = 0; |
Cola | 21:0bd688722e81 | 81 | } |
Cola | 21:0bd688722e81 | 82 | } |
klauss | 48:195c97f12e8e | 83 | if( ok ){ |
Cola | 21:0bd688722e81 | 84 | pc.printf("+"); |
klauss | 48:195c97f12e8e | 85 | if( tcp_session ){ |
klauss | 48:195c97f12e8e | 86 | tcp_client.send_all( "+", strlen( "+" ) ); |
klauss | 48:195c97f12e8e | 87 | } |
klauss | 48:195c97f12e8e | 88 | }else{ |
Cola | 21:0bd688722e81 | 89 | pc.printf("%02x",cb_buffer[0] ); |
Cola | 21:0bd688722e81 | 90 | pc.printf("#"); |
Cola | 21:0bd688722e81 | 91 | pc.printf("\n\r"); |
klauss | 48:195c97f12e8e | 92 | if( tcp_session ){ |
klauss | 48:195c97f12e8e | 93 | char tmp[ 16 ]; |
klauss | 48:195c97f12e8e | 94 | sprintf( tmp, "%02x#\n\r", cb_buffer[ 0 ] ); |
klauss | 48:195c97f12e8e | 95 | tcp_client.send_all( tmp, strlen( tmp ) ); |
klauss | 48:195c97f12e8e | 96 | } |
klauss | 48:195c97f12e8e | 97 | for( int i = 0; i < 300 ; i++ ){ |
klauss | 48:195c97f12e8e | 98 | pc.printf("%x", cb_buffer[ i ]); |
klauss | 48:195c97f12e8e | 99 | if( tcp_session ){ |
klauss | 48:195c97f12e8e | 100 | char tmp[ 16 ]; |
klauss | 48:195c97f12e8e | 101 | sprintf( tmp, "%x", cb_buffer[ i ] ); |
klauss | 48:195c97f12e8e | 102 | tcp_client.send_all( tmp, strlen( tmp ) ); |
klauss | 48:195c97f12e8e | 103 | } |
klauss | 48:195c97f12e8e | 104 | } |
Cola | 21:0bd688722e81 | 105 | } |
Cola | 21:0bd688722e81 | 106 | } |
Cola | 21:0bd688722e81 | 107 | return( NULL ); |
Cola | 21:0bd688722e81 | 108 | } |
Cola | 21:0bd688722e81 | 109 | |
klauss | 0:4d17cd9c8f9d | 110 | uint16_t cc = ( uint16_t )cb_buffer[ 4 ] << 8 | cb_buffer[ 5 ]; |
klauss | 0:4d17cd9c8f9d | 111 | |
klauss | 78:1353744f01e1 | 112 | uint16_t cc_calculated = __checksum__( cb_buffer, __CB_BUFFER_SIZE__ ); |
klauss | 78:1353744f01e1 | 113 | |
klauss | 78:1353744f01e1 | 114 | if( cc != cc_calculated ){ |
klauss | 78:1353744f01e1 | 115 | if( debug_cks_err ) send_msg( "Received PKG, but -- CKS ERROR: %d ( %d )", cc_calculated, cc ); |
klauss | 78:1353744f01e1 | 116 | |
klauss | 78:1353744f01e1 | 117 | if( cc_calculated == 30975 && cc == 0 ){ |
klauss | 78:1353744f01e1 | 118 | pkg_zero++; |
klauss | 78:1353744f01e1 | 119 | }else{ |
Cola | 20:2d6ab23956e5 | 120 | pkg_ckserr++; |
klauss | 78:1353744f01e1 | 121 | } |
klauss | 78:1353744f01e1 | 122 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 123 | }else{ |
Cola | 20:2d6ab23956e5 | 124 | pkg_cksok++; |
klauss | 0:4d17cd9c8f9d | 125 | e_msb = cb_buffer[ 0 ]; |
klauss | 0:4d17cd9c8f9d | 126 | e_lsb = cb_buffer[ 1 ]; |
klauss | 0:4d17cd9c8f9d | 127 | *ext = e_msb << 8 | e_lsb; |
klauss | 79:9bc12aa305a9 | 128 | |
klauss | 81:3656f00ab3db | 129 | //restriçao de controle para cbx malucos |
klauss | 81:3656f00ab3db | 130 | if( *ext < begin || *ext > end ){ |
klauss | 81:3656f00ab3db | 131 | if( debug_out_of_range ) send_msg("Trying from %d", *ext ); |
klauss | 79:9bc12aa305a9 | 132 | out_of_range++; |
klauss | 79:9bc12aa305a9 | 133 | return( NULL ); |
klauss | 79:9bc12aa305a9 | 134 | } |
klauss | 81:3656f00ab3db | 135 | |
klauss | 0:4d17cd9c8f9d | 136 | p_msb = cb_buffer[ 2 ]; |
klauss | 0:4d17cd9c8f9d | 137 | p_lsb = cb_buffer[ 3 ]; |
klauss | 0:4d17cd9c8f9d | 138 | *port = p_msb << 8 | p_lsb; |
klauss | 0:4d17cd9c8f9d | 139 | |
klauss | 0:4d17cd9c8f9d | 140 | *type = cb_buffer[ 6 ]; |
klauss | 0:4d17cd9c8f9d | 141 | |
klauss | 0:4d17cd9c8f9d | 142 | return( cb_buffer + __VZ_HEADER_OFFSET__ ); |
klauss | 0:4d17cd9c8f9d | 143 | } |
klauss | 0:4d17cd9c8f9d | 144 | } |
klauss | 0:4d17cd9c8f9d | 145 | uint8_t * __build_cb_package__( int ext, int port, uint8_t type, char * cb_buffer, uint8_t seq_num, int length, uint8_t * pkg ){ |
klauss | 0:4d17cd9c8f9d | 146 | pkg[ 0 ] = ( uint8_t )( ( ext & 0xFF00 ) >> 8 ); |
klauss | 0:4d17cd9c8f9d | 147 | pkg[ 1 ] = ( uint8_t )( ext & 0x00FF ); |
klauss | 0:4d17cd9c8f9d | 148 | pkg[ 2 ] = ( uint8_t )( ( port & 0xFF00 ) >> 8 ); |
klauss | 0:4d17cd9c8f9d | 149 | pkg[ 3 ] = ( uint8_t )( port & 0x00FF ); |
klauss | 0:4d17cd9c8f9d | 150 | |
klauss | 0:4d17cd9c8f9d | 151 | pkg[ 6 ] = type; |
klauss | 0:4d17cd9c8f9d | 152 | |
klauss | 0:4d17cd9c8f9d | 153 | size_t fill = 0x00; |
klauss | 0:4d17cd9c8f9d | 154 | |
klauss | 0:4d17cd9c8f9d | 155 | if( type == __AUDIO__ ){ |
klauss | 0:4d17cd9c8f9d | 156 | for( register int i = __VZ_HEADER_OFFSET__; i < length + __VZ_HEADER_OFFSET__; i++ ) |
klauss | 0:4d17cd9c8f9d | 157 | pkg[ i ] = ( uint8_t )cb_buffer[ i - __VZ_HEADER_OFFSET__ ]; |
klauss | 0:4d17cd9c8f9d | 158 | fill = length + __VZ_HEADER_OFFSET__; |
klauss | 0:4d17cd9c8f9d | 159 | }else if( type == __INVITE__ ){ |
klauss | 0:4d17cd9c8f9d | 160 | pkg[ 7 ] = seq_num; |
klauss | 92:92df17f538a8 | 161 | __print_clock__( pkg + 8 ); |
klauss | 92:92df17f538a8 | 162 | |
klauss | 0:4d17cd9c8f9d | 163 | pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ]; |
klauss | 0:4d17cd9c8f9d | 164 | fill = __TIMESLICE_PLACE__ + 1; |
klauss | 0:4d17cd9c8f9d | 165 | }else if( type == __REGISTRY__ || type == __BOOT__ ){ |
klauss | 0:4d17cd9c8f9d | 166 | pkg[ 7 ] = seq_num; |
klauss | 92:92df17f538a8 | 167 | __print_clock__( pkg + 8 ); |
klauss | 92:92df17f538a8 | 168 | |
klauss | 0:4d17cd9c8f9d | 169 | fill = __VZ_HEADER_OFFSET__ + __CLOCK_SYNC_SIZE__ + __SEQ_NUM_SIZE__; |
klauss | 70:714c33487aae | 170 | }else if( type == __CB_BYE__ ){ |
klauss | 0:4d17cd9c8f9d | 171 | pkg[ 7 ] = seq_num; |
klauss | 92:92df17f538a8 | 172 | __print_clock__( pkg + 8 ); |
klauss | 92:92df17f538a8 | 173 | |
klauss | 0:4d17cd9c8f9d | 174 | pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ]; |
klauss | 0:4d17cd9c8f9d | 175 | fill = __TIMESLICE_PLACE__ + 1; |
klauss | 67:cdedc64d9921 | 176 | }else if( type == __PROMPT__ ){ |
klauss | 92:92df17f538a8 | 177 | __print_clock__( pkg + 8 ); |
klauss | 92:92df17f538a8 | 178 | |
Cola | 21:0bd688722e81 | 179 | xmemcpy((pkg+7), (uint8_t*)cb_buffer, 293); |
klauss | 67:cdedc64d9921 | 180 | //FIXME isso forca qualquer cmd ter < 100 chars |
Cola | 21:0bd688722e81 | 181 | fill = 100; |
klauss | 67:cdedc64d9921 | 182 | }else if( type == __TELEMETRY__ ){ |
klauss | 4:de46f0d9b14d | 183 | pkg[ 7 ] = seq_num; |
klauss | 92:92df17f538a8 | 184 | __print_clock__( pkg + 8 ); |
klauss | 92:92df17f538a8 | 185 | |
klauss | 4:de46f0d9b14d | 186 | pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ]; |
klauss | 4:de46f0d9b14d | 187 | fill = __TIMESLICE_PLACE__ + 1; |
klauss | 67:cdedc64d9921 | 188 | }else if( type == __BOOTLOADER_CBX__ ){ |
klauss | 67:cdedc64d9921 | 189 | pkg[ 7 ] = seq_num; |
klauss | 92:92df17f538a8 | 190 | __print_clock__( pkg + 8 ); |
klauss | 92:92df17f538a8 | 191 | |
klauss | 67:cdedc64d9921 | 192 | fill = __VZ_HEADER_OFFSET__ + __CLOCK_SYNC_SIZE__ + __SEQ_NUM_SIZE__; |
klauss | 67:cdedc64d9921 | 193 | }else{ |
klauss | 67:cdedc64d9921 | 194 | fill = __VZ_HEADER_OFFSET__; |
klauss | 0:4d17cd9c8f9d | 195 | } |
klauss | 0:4d17cd9c8f9d | 196 | |
klauss | 67:cdedc64d9921 | 197 | for( register uint16_t i = fill; i < __CB_BUFFER_SIZE__; i++ ) pkg[ i ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 198 | |
klauss | 0:4d17cd9c8f9d | 199 | uint16_t cc = __checksum__( pkg, 300 ); |
klauss | 0:4d17cd9c8f9d | 200 | pkg[ 4 ] =( uint8_t )( ( cc & 0xFF00 ) >> 8) ; |
klauss | 0:4d17cd9c8f9d | 201 | pkg[ 5 ] =( uint8_t )( cc & 0x00FF ); |
klauss | 0:4d17cd9c8f9d | 202 | |
klauss | 0:4d17cd9c8f9d | 203 | return pkg; |
klauss | 0:4d17cd9c8f9d | 204 | } |
klauss | 0:4d17cd9c8f9d | 205 | |
klauss | 0:4d17cd9c8f9d | 206 | uint16_t __checksum__( uint8_t * buffer, size_t length ){ |
klauss | 70:714c33487aae | 207 | if( !buffer ) return( 0 ); |
klauss | 0:4d17cd9c8f9d | 208 | uint16_t cc = 0x00; |
klauss | 0:4d17cd9c8f9d | 209 | buffer[ 4 ] = buffer[ 5 ] = 0x5a; |
klauss | 0:4d17cd9c8f9d | 210 | for( register int i = 0; i < length; i++ ){ |
klauss | 0:4d17cd9c8f9d | 211 | cc += buffer[ i ]; |
klauss | 0:4d17cd9c8f9d | 212 | if( cc & BIT15 ){ |
klauss | 0:4d17cd9c8f9d | 213 | cc <<= 1; |
klauss | 0:4d17cd9c8f9d | 214 | cc |= BIT0; |
klauss | 0:4d17cd9c8f9d | 215 | }else{ cc <<= BIT0; } |
klauss | 0:4d17cd9c8f9d | 216 | } |
klauss | 0:4d17cd9c8f9d | 217 | cc ^= 0xffff; |
klauss | 0:4d17cd9c8f9d | 218 | return cc; |
klauss | 0:4d17cd9c8f9d | 219 | } |
klauss | 0:4d17cd9c8f9d | 220 | |
klauss | 0:4d17cd9c8f9d | 221 | void __print_clock__( uint8_t * buffer ){ |
klauss | 92:92df17f538a8 | 222 | //if( !buffer ) return; |
klauss | 70:714c33487aae | 223 | |
klauss | 14:22a35f575502 | 224 | //int ntp_result = ntp.setTime( "200.192.232.8", 123, 3 ); |
klauss | 70:714c33487aae | 225 | //led2 = !led2; |
klauss | 92:92df17f538a8 | 226 | //struct tm ts; |
klauss | 46:a670f187a704 | 227 | //int ntp_result = ntp.setTime( "200.192.232.8" ); |
klauss | 63:0d95da692bb4 | 228 | //int ntp_result = ntp.setTime( "200.192.232.8", 123, 3 ); |
klauss | 63:0d95da692bb4 | 229 | //if( ntp_result == 0 ){ |
klauss | 92:92df17f538a8 | 230 | // time_t seconds; |
klauss | 14:22a35f575502 | 231 | // seconds = time(NULL); |
klauss | 92:92df17f538a8 | 232 | // time( &seconds ); |
klauss | 92:92df17f538a8 | 233 | // ts = *localtime( &seconds ); |
klauss | 63:0d95da692bb4 | 234 | //} |
klauss | 0:4d17cd9c8f9d | 235 | |
klauss | 92:92df17f538a8 | 236 | //int ano = ts.tm_year + 1900; |
klauss | 92:92df17f538a8 | 237 | //int mes = ts.tm_mon + 1; |
klauss | 92:92df17f538a8 | 238 | //int dia = ts.tm_mday; |
klauss | 92:92df17f538a8 | 239 | //int hora = ts.tm_hour - 3; |
klauss | 92:92df17f538a8 | 240 | //int min = ts.tm_min; |
klauss | 92:92df17f538a8 | 241 | //int sec = ts.tm_sec; |
klauss | 0:4d17cd9c8f9d | 242 | |
klauss | 92:92df17f538a8 | 243 | //buffer[ 0 ] = ano / 1000; |
klauss | 92:92df17f538a8 | 244 | //ano -= buffer[ 0 ] * 1000; |
klauss | 92:92df17f538a8 | 245 | //buffer[ 1 ] = ano / 100; |
klauss | 92:92df17f538a8 | 246 | //ano -= buffer[ 1 ]* 100; |
klauss | 92:92df17f538a8 | 247 | //buffer[ 2 ] = ano / 10; |
klauss | 92:92df17f538a8 | 248 | //ano -= buffer[ 2 ] * 10; |
klauss | 92:92df17f538a8 | 249 | //buffer[ 3 ] = ano; |
klauss | 92:92df17f538a8 | 250 | //buffer[ 4 ] = mes / 10; |
klauss | 92:92df17f538a8 | 251 | //buffer[ 5 ] = mes % 10; |
klauss | 92:92df17f538a8 | 252 | //buffer[ 6 ] = dia / 10; |
klauss | 92:92df17f538a8 | 253 | //buffer[ 7 ] = dia % 10; |
klauss | 92:92df17f538a8 | 254 | //buffer[ 8 ] = hora / 10; |
klauss | 92:92df17f538a8 | 255 | //buffer[ 9 ] = hora % 10; |
klauss | 92:92df17f538a8 | 256 | //buffer[ 10 ] = min / 10; |
klauss | 92:92df17f538a8 | 257 | //buffer[ 11 ] = min % 10; |
klauss | 92:92df17f538a8 | 258 | //buffer[ 12 ] = sec / 10; |
klauss | 92:92df17f538a8 | 259 | //buffer[ 13 ] = sec % 10; |
klauss | 0:4d17cd9c8f9d | 260 | |
klauss | 0:4d17cd9c8f9d | 261 | /* convertendo pro ascii do nro */ |
klauss | 92:92df17f538a8 | 262 | for( register int i = 0; i < 14; i++ ) buffer[ i ] = 0xab; |
klauss | 70:714c33487aae | 263 | //led4 = !led4; |
klauss | 0:4d17cd9c8f9d | 264 | } |