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