![](/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@31:bb5fb28a77df, 2014-09-25 (annotated)
- Committer:
- klauss
- Date:
- Thu Sep 25 19:34:46 2014 +0000
- Revision:
- 31:bb5fb28a77df
- Parent:
- 23:4a6d631a6298
- Child:
- 34:f19d9735428e
- mais mudancas no prompt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 0:4d17cd9c8f9d | 1 | #include "vz_protocol.h" |
Cola | 23:4a6d631a6298 | 2 | #include "prompt.h" |
klauss | 0:4d17cd9c8f9d | 3 | |
klauss | 0:4d17cd9c8f9d | 4 | extern DigitalOut led2; |
klauss | 0:4d17cd9c8f9d | 5 | extern DigitalOut led3; |
Cola | 20:2d6ab23956e5 | 6 | |
Cola | 20:2d6ab23956e5 | 7 | uint16_t pkg_ckserr = 0; |
Cola | 20:2d6ab23956e5 | 8 | uint16_t pkg_cksok = 0; |
klauss | 0:4d17cd9c8f9d | 9 | |
klauss | 0:4d17cd9c8f9d | 10 | uint8_t * __parse_cb_buffer__( int * ext, int * port, volatile uint8_t * type, uint8_t * cb_buffer ){ |
klauss | 0:4d17cd9c8f9d | 11 | /** |
klauss | 0:4d17cd9c8f9d | 12 | * parse and split vz package, |
klauss | 0:4d17cd9c8f9d | 13 | * | E | E | P | P | C | C | T | [ Seq_num | Audio ] | 14[ Clock | Audio ] | [ TS | Audio ] | ... | |
klauss | 0:4d17cd9c8f9d | 14 | * E = Ext = Ramal |
klauss | 0:4d17cd9c8f9d | 15 | * P = Port = Porta |
klauss | 0:4d17cd9c8f9d | 16 | * C = Checksum |
klauss | 0:4d17cd9c8f9d | 17 | * T = Type = Tipo |
klauss | 0:4d17cd9c8f9d | 18 | * Seq_num = Sequence Number = Numero de sequencia |
klauss | 0:4d17cd9c8f9d | 19 | * Clock = 14 bytes to sync |
klauss | 0:4d17cd9c8f9d | 20 | * ... = demais __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__ bytes |
klauss | 0:4d17cd9c8f9d | 21 | */ |
klauss | 0:4d17cd9c8f9d | 22 | uint8_t p_lsb, p_msb; |
klauss | 0:4d17cd9c8f9d | 23 | uint8_t e_lsb, e_msb; |
klauss | 0:4d17cd9c8f9d | 24 | |
klauss | 0:4d17cd9c8f9d | 25 | if( cb_buffer == NULL ) return( NULL ); |
klauss | 0:4d17cd9c8f9d | 26 | |
Cola | 21:0bd688722e81 | 27 | if (cb_buffer[6] == 64) { |
Cola | 21:0bd688722e81 | 28 | static uint8_t flood_cnt = 0; |
Cola | 21:0bd688722e81 | 29 | static uint8_t first_run_flag = 0; |
Cola | 21:0bd688722e81 | 30 | static Timer t; |
Cola | 21:0bd688722e81 | 31 | if (first_run_flag == 0) { |
Cola | 21:0bd688722e81 | 32 | flood_cnt = cb_buffer[0]; |
Cola | 21:0bd688722e81 | 33 | first_run_flag++; |
Cola | 21:0bd688722e81 | 34 | t.start(); |
Cola | 21:0bd688722e81 | 35 | } else { |
Cola | 21:0bd688722e81 | 36 | if (cb_buffer[0] == 0x00){ |
Cola | 21:0bd688722e81 | 37 | pc.printf("%d", t.read_us()); |
Cola | 21:0bd688722e81 | 38 | } |
Cola | 21:0bd688722e81 | 39 | flood_cnt++; |
Cola | 21:0bd688722e81 | 40 | while (flood_cnt != cb_buffer[0]) { |
Cola | 21:0bd688722e81 | 41 | pc.printf("0"); |
Cola | 21:0bd688722e81 | 42 | flood_cnt++; |
Cola | 21:0bd688722e81 | 43 | } |
Cola | 21:0bd688722e81 | 44 | int i; |
Cola | 21:0bd688722e81 | 45 | int ok = 1; |
Cola | 21:0bd688722e81 | 46 | uint8_t cmp; |
Cola | 21:0bd688722e81 | 47 | for (i=0, cmp=cb_buffer[0]; (i<300) && (ok==1); i++, cmp++) { |
Cola | 21:0bd688722e81 | 48 | if (i != 6) { |
Cola | 21:0bd688722e81 | 49 | if (cb_buffer[i] != cmp) ok = 0; |
Cola | 21:0bd688722e81 | 50 | } |
Cola | 21:0bd688722e81 | 51 | } |
Cola | 21:0bd688722e81 | 52 | if (ok) { |
Cola | 21:0bd688722e81 | 53 | pc.printf("+"); |
Cola | 21:0bd688722e81 | 54 | } else { |
Cola | 21:0bd688722e81 | 55 | pc.printf("%02x",cb_buffer[0] ); |
Cola | 21:0bd688722e81 | 56 | pc.printf("#"); |
Cola | 21:0bd688722e81 | 57 | pc.printf("\n\r"); |
Cola | 21:0bd688722e81 | 58 | for (int i = 0; i< 300 ; i++) {pc.printf("%x", cb_buffer[i]);} |
Cola | 21:0bd688722e81 | 59 | |
Cola | 21:0bd688722e81 | 60 | } |
Cola | 21:0bd688722e81 | 61 | } |
Cola | 21:0bd688722e81 | 62 | return( NULL ); |
Cola | 21:0bd688722e81 | 63 | } |
Cola | 21:0bd688722e81 | 64 | |
klauss | 0:4d17cd9c8f9d | 65 | uint16_t cc = ( uint16_t )cb_buffer[ 4 ] << 8 | cb_buffer[ 5 ]; |
klauss | 0:4d17cd9c8f9d | 66 | |
klauss | 18:01a93677e40c | 67 | if( cc != __checksum__( cb_buffer, __CB_BUFFER_SIZE__ ) ){ |
Cola | 20:2d6ab23956e5 | 68 | /*pc.printf("\n\r CKS ERROR: "); |
Cola | 20:2d6ab23956e5 | 69 | for (int i=0; i< 300; i++ ){ pc.printf("%x ", cb_buffer[i]); } |
Cola | 20:2d6ab23956e5 | 70 | pc.printf("\n\r");*/ |
Cola | 20:2d6ab23956e5 | 71 | pkg_ckserr++; |
klauss | 18:01a93677e40c | 72 | return( NULL ); |
Cola | 20:2d6ab23956e5 | 73 | |
klauss | 0:4d17cd9c8f9d | 74 | }else{ |
Cola | 20:2d6ab23956e5 | 75 | pkg_cksok++; |
klauss | 0:4d17cd9c8f9d | 76 | e_msb = cb_buffer[ 0 ]; |
klauss | 0:4d17cd9c8f9d | 77 | e_lsb = cb_buffer[ 1 ]; |
klauss | 0:4d17cd9c8f9d | 78 | *ext = e_msb << 8 | e_lsb; |
klauss | 0:4d17cd9c8f9d | 79 | |
klauss | 0:4d17cd9c8f9d | 80 | p_msb = cb_buffer[ 2 ]; |
klauss | 0:4d17cd9c8f9d | 81 | p_lsb = cb_buffer[ 3 ]; |
klauss | 0:4d17cd9c8f9d | 82 | *port = p_msb << 8 | p_lsb; |
klauss | 0:4d17cd9c8f9d | 83 | |
klauss | 0:4d17cd9c8f9d | 84 | *type = cb_buffer[ 6 ]; |
klauss | 0:4d17cd9c8f9d | 85 | |
klauss | 0:4d17cd9c8f9d | 86 | return( cb_buffer + __VZ_HEADER_OFFSET__ ); |
klauss | 0:4d17cd9c8f9d | 87 | } |
Cola | 20:2d6ab23956e5 | 88 | |
klauss | 0:4d17cd9c8f9d | 89 | } |
klauss | 0:4d17cd9c8f9d | 90 | 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 | 91 | pkg[ 0 ] = ( uint8_t )( ( ext & 0xFF00 ) >> 8 ); |
klauss | 0:4d17cd9c8f9d | 92 | pkg[ 1 ] = ( uint8_t )( ext & 0x00FF ); |
klauss | 0:4d17cd9c8f9d | 93 | pkg[ 2 ] = ( uint8_t )( ( port & 0xFF00 ) >> 8 ); |
klauss | 0:4d17cd9c8f9d | 94 | pkg[ 3 ] = ( uint8_t )( port & 0x00FF ); |
klauss | 0:4d17cd9c8f9d | 95 | |
klauss | 0:4d17cd9c8f9d | 96 | pkg[ 6 ] = type; |
klauss | 0:4d17cd9c8f9d | 97 | |
klauss | 0:4d17cd9c8f9d | 98 | size_t fill = 0x00; |
klauss | 0:4d17cd9c8f9d | 99 | |
klauss | 0:4d17cd9c8f9d | 100 | if( type == __AUDIO__ ){ |
klauss | 0:4d17cd9c8f9d | 101 | for( register int i = __VZ_HEADER_OFFSET__; i < length + __VZ_HEADER_OFFSET__; i++ ) |
klauss | 0:4d17cd9c8f9d | 102 | pkg[ i ] = ( uint8_t )cb_buffer[ i - __VZ_HEADER_OFFSET__ ]; |
klauss | 0:4d17cd9c8f9d | 103 | fill = length + __VZ_HEADER_OFFSET__; |
klauss | 0:4d17cd9c8f9d | 104 | }else if( type == __INVITE__ ){ |
klauss | 0:4d17cd9c8f9d | 105 | pkg[ 7 ] = seq_num; |
klauss | 0:4d17cd9c8f9d | 106 | //__print_clock__( pkg + 8 ); |
klauss | 0:4d17cd9c8f9d | 107 | pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ]; |
klauss | 0:4d17cd9c8f9d | 108 | fill = __TIMESLICE_PLACE__ + 1; |
klauss | 0:4d17cd9c8f9d | 109 | }else if( type == __REGISTRY__ || type == __BOOT__ ){ |
klauss | 0:4d17cd9c8f9d | 110 | pkg[ 7 ] = seq_num; |
klauss | 14:22a35f575502 | 111 | //__print_clock__( pkg + 8 ); |
klauss | 0:4d17cd9c8f9d | 112 | fill = __VZ_HEADER_OFFSET__ + __CLOCK_SYNC_SIZE__ + __SEQ_NUM_SIZE__; |
klauss | 0:4d17cd9c8f9d | 113 | }if( type == __CB_BYE__ ){ |
klauss | 0:4d17cd9c8f9d | 114 | pkg[ 7 ] = seq_num; |
klauss | 0:4d17cd9c8f9d | 115 | //__print_clock__( pkg + 8 ); |
klauss | 0:4d17cd9c8f9d | 116 | pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ]; |
klauss | 0:4d17cd9c8f9d | 117 | fill = __TIMESLICE_PLACE__ + 1; |
Cola | 21:0bd688722e81 | 118 | } |
Cola | 21:0bd688722e81 | 119 | |
Cola | 21:0bd688722e81 | 120 | if( type == __PROMPT__ ){ |
Cola | 21:0bd688722e81 | 121 | //__print_clock__( pkg + 8 ); |
Cola | 21:0bd688722e81 | 122 | xmemcpy((pkg+7), (uint8_t*)cb_buffer, 293); |
Cola | 21:0bd688722e81 | 123 | fill = 100; |
Cola | 21:0bd688722e81 | 124 | } |
Cola | 21:0bd688722e81 | 125 | |
Cola | 21:0bd688722e81 | 126 | else if( type == __TELEMETRY__ ){ |
klauss | 4:de46f0d9b14d | 127 | pkg[ 7 ] = seq_num; |
klauss | 14:22a35f575502 | 128 | //__print_clock__( pkg + 8 ); |
klauss | 4:de46f0d9b14d | 129 | pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ]; |
klauss | 4:de46f0d9b14d | 130 | fill = __TIMESLICE_PLACE__ + 1; |
klauss | 0:4d17cd9c8f9d | 131 | } |
klauss | 0:4d17cd9c8f9d | 132 | |
klauss | 0:4d17cd9c8f9d | 133 | for( register int i = fill; i < __CB_BUFFER_SIZE__; i++ ) pkg[ i ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 134 | led2 = !led2; |
klauss | 0:4d17cd9c8f9d | 135 | |
klauss | 0:4d17cd9c8f9d | 136 | uint16_t cc = __checksum__( pkg, 300 ); |
klauss | 0:4d17cd9c8f9d | 137 | pkg[ 4 ] =( uint8_t )( ( cc & 0xFF00 ) >> 8) ; |
klauss | 0:4d17cd9c8f9d | 138 | pkg[ 5 ] =( uint8_t )( cc & 0x00FF ); |
klauss | 0:4d17cd9c8f9d | 139 | |
klauss | 0:4d17cd9c8f9d | 140 | return pkg; |
klauss | 0:4d17cd9c8f9d | 141 | } |
klauss | 0:4d17cd9c8f9d | 142 | |
klauss | 0:4d17cd9c8f9d | 143 | char * __build_eth__package__( void ){ |
klauss | 0:4d17cd9c8f9d | 144 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 145 | } |
klauss | 0:4d17cd9c8f9d | 146 | |
klauss | 0:4d17cd9c8f9d | 147 | uint8_t * __read_cb_buffer__( uint8_t * dest, uint8_t * src ){ |
klauss | 0:4d17cd9c8f9d | 148 | for( register int i = 0; i < __CB_BUFFER_SIZE__; i++ ) *dest++ = *src++; |
klauss | 0:4d17cd9c8f9d | 149 | return( dest ); |
klauss | 0:4d17cd9c8f9d | 150 | } |
klauss | 0:4d17cd9c8f9d | 151 | |
klauss | 0:4d17cd9c8f9d | 152 | uint8_t * __read_eth_buffer__( uint8_t * dest, uint8_t * src ){ |
klauss | 0:4d17cd9c8f9d | 153 | |
klauss | 0:4d17cd9c8f9d | 154 | for( register int i = 0; i < __ETH_BUFFER_SIZE__; i++ ) *dest++ = *src++; |
klauss | 0:4d17cd9c8f9d | 155 | |
klauss | 0:4d17cd9c8f9d | 156 | return( dest ); |
klauss | 0:4d17cd9c8f9d | 157 | } |
klauss | 0:4d17cd9c8f9d | 158 | |
klauss | 0:4d17cd9c8f9d | 159 | uint8_t * __write_cb_buffer__( uint8_t * dest, uint8_t * src ){ |
klauss | 0:4d17cd9c8f9d | 160 | for( register int i = 0; i < __CB_BUFFER_SIZE__; i++ ) *dest++ = *src++; |
klauss | 0:4d17cd9c8f9d | 161 | return( dest ); |
klauss | 0:4d17cd9c8f9d | 162 | } |
klauss | 0:4d17cd9c8f9d | 163 | |
klauss | 0:4d17cd9c8f9d | 164 | uint8_t * __write_eth_buffer__( uint8_t * dest, uint8_t * src ){ |
klauss | 0:4d17cd9c8f9d | 165 | for( register int i = 0; i < __ETH_BUFFER_SIZE__; i++ ) *dest++ = *src++; |
klauss | 0:4d17cd9c8f9d | 166 | |
klauss | 0:4d17cd9c8f9d | 167 | return( dest ); |
klauss | 0:4d17cd9c8f9d | 168 | } |
klauss | 0:4d17cd9c8f9d | 169 | |
klauss | 0:4d17cd9c8f9d | 170 | uint16_t __checksum__( uint8_t * buffer, size_t length ){ |
klauss | 0:4d17cd9c8f9d | 171 | uint16_t cc = 0x00; |
klauss | 0:4d17cd9c8f9d | 172 | buffer[ 4 ] = buffer[ 5 ] = 0x5a; |
klauss | 0:4d17cd9c8f9d | 173 | for( register int i = 0; i < length; i++ ){ |
klauss | 0:4d17cd9c8f9d | 174 | cc += buffer[ i ]; |
klauss | 0:4d17cd9c8f9d | 175 | if( cc & BIT15 ){ |
klauss | 0:4d17cd9c8f9d | 176 | cc <<= 1; |
klauss | 0:4d17cd9c8f9d | 177 | cc |= BIT0; |
klauss | 0:4d17cd9c8f9d | 178 | }else{ cc <<= BIT0; } |
klauss | 0:4d17cd9c8f9d | 179 | } |
klauss | 0:4d17cd9c8f9d | 180 | cc ^= 0xffff; |
klauss | 0:4d17cd9c8f9d | 181 | return cc; |
klauss | 0:4d17cd9c8f9d | 182 | } |
klauss | 0:4d17cd9c8f9d | 183 | |
klauss | 0:4d17cd9c8f9d | 184 | void __print_clock__( uint8_t * buffer ){ |
klauss | 0:4d17cd9c8f9d | 185 | NTPClient ntp; |
klauss | 14:22a35f575502 | 186 | //int ntp_result = ntp.setTime( "200.192.232.8", 123, 3 ); |
klauss | 0:4d17cd9c8f9d | 187 | struct tm ts; |
klauss | 14:22a35f575502 | 188 | int ntp_result = ntp.setTime( "200.192.232.8" ); |
klauss | 14:22a35f575502 | 189 | if( ntp_result == 0 ){ |
klauss | 14:22a35f575502 | 190 | time_t seconds; |
klauss | 14:22a35f575502 | 191 | // seconds = time(NULL); |
klauss | 14:22a35f575502 | 192 | time( &seconds ); |
klauss | 14:22a35f575502 | 193 | ts = *localtime( &seconds ); |
klauss | 14:22a35f575502 | 194 | } |
klauss | 0:4d17cd9c8f9d | 195 | |
klauss | 0:4d17cd9c8f9d | 196 | int ano = ts.tm_year + 1900; |
klauss | 0:4d17cd9c8f9d | 197 | int mes = ts.tm_mon + 1; |
klauss | 0:4d17cd9c8f9d | 198 | int dia = ts.tm_mday; |
klauss | 0:4d17cd9c8f9d | 199 | int hora = ts.tm_hour - 3; |
klauss | 0:4d17cd9c8f9d | 200 | int min = ts.tm_min; |
klauss | 0:4d17cd9c8f9d | 201 | int sec = ts.tm_sec; |
klauss | 0:4d17cd9c8f9d | 202 | |
klauss | 0:4d17cd9c8f9d | 203 | buffer[ 0 ] = ano / 1000; |
klauss | 0:4d17cd9c8f9d | 204 | ano -= buffer[ 0 ] * 1000; |
klauss | 0:4d17cd9c8f9d | 205 | buffer[ 1 ] = ano / 100; |
klauss | 0:4d17cd9c8f9d | 206 | ano -= buffer[ 1 ]* 100; |
klauss | 0:4d17cd9c8f9d | 207 | buffer[ 2 ] = ano / 10; |
klauss | 0:4d17cd9c8f9d | 208 | ano -= buffer[ 2 ] * 10; |
klauss | 0:4d17cd9c8f9d | 209 | buffer[ 3 ] = ano; |
klauss | 0:4d17cd9c8f9d | 210 | buffer[ 4 ] = mes / 10; |
klauss | 0:4d17cd9c8f9d | 211 | buffer[ 5 ] = mes % 10; |
klauss | 0:4d17cd9c8f9d | 212 | buffer[ 6 ] = dia / 10; |
klauss | 0:4d17cd9c8f9d | 213 | buffer[ 7 ] = dia % 10; |
klauss | 0:4d17cd9c8f9d | 214 | buffer[ 8 ] = hora / 10; |
klauss | 0:4d17cd9c8f9d | 215 | buffer[ 9 ] = hora % 10; |
klauss | 0:4d17cd9c8f9d | 216 | buffer[ 10 ] = min / 10; |
klauss | 0:4d17cd9c8f9d | 217 | buffer[ 11 ] = min % 10; |
klauss | 0:4d17cd9c8f9d | 218 | buffer[ 12 ] = sec / 10; |
klauss | 0:4d17cd9c8f9d | 219 | buffer[ 13 ] = sec % 10; |
klauss | 0:4d17cd9c8f9d | 220 | |
klauss | 0:4d17cd9c8f9d | 221 | /* convertendo pro ascii do nro */ |
klauss | 0:4d17cd9c8f9d | 222 | for( register int i = 0; i < 14; i++ ) buffer[ i ] += 0x30; |
klauss | 0:4d17cd9c8f9d | 223 | } |