![](/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@17:67a6b557eda5, 2014-09-19 (annotated)
- Committer:
- klauss
- Date:
- Fri Sep 19 16:40:17 2014 +0000
- Revision:
- 17:67a6b557eda5
- Parent:
- 14:22a35f575502
- Child:
- 18:01a93677e40c
versao em teste de wdt, filesystem, callme
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 0:4d17cd9c8f9d | 1 | #include "vz_protocol.h" |
klauss | 0:4d17cd9c8f9d | 2 | |
klauss | 0:4d17cd9c8f9d | 3 | extern DigitalOut led2; |
klauss | 0:4d17cd9c8f9d | 4 | extern DigitalOut led3; |
klauss | 0:4d17cd9c8f9d | 5 | |
klauss | 0:4d17cd9c8f9d | 6 | uint8_t * __parse_cb_buffer__( int * ext, int * port, volatile uint8_t * type, uint8_t * cb_buffer ){ |
klauss | 0:4d17cd9c8f9d | 7 | /** |
klauss | 0:4d17cd9c8f9d | 8 | * parse and split vz package, |
klauss | 0:4d17cd9c8f9d | 9 | * | E | E | P | P | C | C | T | [ Seq_num | Audio ] | 14[ Clock | Audio ] | [ TS | Audio ] | ... | |
klauss | 0:4d17cd9c8f9d | 10 | * E = Ext = Ramal |
klauss | 0:4d17cd9c8f9d | 11 | * P = Port = Porta |
klauss | 0:4d17cd9c8f9d | 12 | * C = Checksum |
klauss | 0:4d17cd9c8f9d | 13 | * T = Type = Tipo |
klauss | 0:4d17cd9c8f9d | 14 | * Seq_num = Sequence Number = Numero de sequencia |
klauss | 0:4d17cd9c8f9d | 15 | * Clock = 14 bytes to sync |
klauss | 0:4d17cd9c8f9d | 16 | * ... = demais __CB_BUFFER_SIZE__ - __VZ_HEADER_OFFSET__ bytes |
klauss | 0:4d17cd9c8f9d | 17 | */ |
klauss | 0:4d17cd9c8f9d | 18 | uint8_t p_lsb, p_msb; |
klauss | 0:4d17cd9c8f9d | 19 | uint8_t e_lsb, e_msb; |
klauss | 0:4d17cd9c8f9d | 20 | |
klauss | 0:4d17cd9c8f9d | 21 | if( cb_buffer == NULL ) return( NULL ); |
klauss | 0:4d17cd9c8f9d | 22 | |
klauss | 0:4d17cd9c8f9d | 23 | uint16_t cc = ( uint16_t )cb_buffer[ 4 ] << 8 | cb_buffer[ 5 ]; |
klauss | 0:4d17cd9c8f9d | 24 | |
klauss | 17:67a6b557eda5 | 25 | uint16_t calc_cc = __checksum__( cb_buffer, __CB_BUFFER_SIZE__ ); |
klauss | 0:4d17cd9c8f9d | 26 | |
klauss | 17:67a6b557eda5 | 27 | if( cc != calc_cc ){ |
klauss | 17:67a6b557eda5 | 28 | debug_msg("wrong check sum -- calc :: %d ( %d ) -- diff %d", calc_cc, cc, ( cc > calc_cc ) ? cc - calc_cc : calc_cc - cc ); |
klauss | 17:67a6b557eda5 | 29 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 30 | }else{ |
klauss | 0:4d17cd9c8f9d | 31 | e_msb = cb_buffer[ 0 ]; |
klauss | 0:4d17cd9c8f9d | 32 | e_lsb = cb_buffer[ 1 ]; |
klauss | 0:4d17cd9c8f9d | 33 | *ext = e_msb << 8 | e_lsb; |
klauss | 0:4d17cd9c8f9d | 34 | |
klauss | 0:4d17cd9c8f9d | 35 | p_msb = cb_buffer[ 2 ]; |
klauss | 0:4d17cd9c8f9d | 36 | p_lsb = cb_buffer[ 3 ]; |
klauss | 0:4d17cd9c8f9d | 37 | *port = p_msb << 8 | p_lsb; |
klauss | 0:4d17cd9c8f9d | 38 | |
klauss | 0:4d17cd9c8f9d | 39 | *type = cb_buffer[ 6 ]; |
klauss | 0:4d17cd9c8f9d | 40 | |
klauss | 0:4d17cd9c8f9d | 41 | return( cb_buffer + __VZ_HEADER_OFFSET__ ); |
klauss | 0:4d17cd9c8f9d | 42 | } |
klauss | 0:4d17cd9c8f9d | 43 | } |
klauss | 0:4d17cd9c8f9d | 44 | 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 | 17:67a6b557eda5 | 45 | //debug_msg(""); |
klauss | 0:4d17cd9c8f9d | 46 | pkg[ 0 ] = ( uint8_t )( ( ext & 0xFF00 ) >> 8 ); |
klauss | 0:4d17cd9c8f9d | 47 | pkg[ 1 ] = ( uint8_t )( ext & 0x00FF ); |
klauss | 0:4d17cd9c8f9d | 48 | pkg[ 2 ] = ( uint8_t )( ( port & 0xFF00 ) >> 8 ); |
klauss | 0:4d17cd9c8f9d | 49 | pkg[ 3 ] = ( uint8_t )( port & 0x00FF ); |
klauss | 0:4d17cd9c8f9d | 50 | |
klauss | 0:4d17cd9c8f9d | 51 | pkg[ 6 ] = type; |
klauss | 0:4d17cd9c8f9d | 52 | |
klauss | 0:4d17cd9c8f9d | 53 | size_t fill = 0x00; |
klauss | 0:4d17cd9c8f9d | 54 | |
klauss | 0:4d17cd9c8f9d | 55 | if( type == __AUDIO__ ){ |
klauss | 0:4d17cd9c8f9d | 56 | for( register int i = __VZ_HEADER_OFFSET__; i < length + __VZ_HEADER_OFFSET__; i++ ) |
klauss | 0:4d17cd9c8f9d | 57 | pkg[ i ] = ( uint8_t )cb_buffer[ i - __VZ_HEADER_OFFSET__ ]; |
klauss | 0:4d17cd9c8f9d | 58 | fill = length + __VZ_HEADER_OFFSET__; |
klauss | 0:4d17cd9c8f9d | 59 | }else if( type == __INVITE__ ){ |
klauss | 0:4d17cd9c8f9d | 60 | pkg[ 7 ] = seq_num; |
klauss | 0:4d17cd9c8f9d | 61 | //__print_clock__( pkg + 8 ); |
klauss | 0:4d17cd9c8f9d | 62 | pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ]; |
klauss | 0:4d17cd9c8f9d | 63 | fill = __TIMESLICE_PLACE__ + 1; |
klauss | 0:4d17cd9c8f9d | 64 | }else if( type == __REGISTRY__ || type == __BOOT__ ){ |
klauss | 0:4d17cd9c8f9d | 65 | pkg[ 7 ] = seq_num; |
klauss | 14:22a35f575502 | 66 | //__print_clock__( pkg + 8 ); |
klauss | 0:4d17cd9c8f9d | 67 | fill = __VZ_HEADER_OFFSET__ + __CLOCK_SYNC_SIZE__ + __SEQ_NUM_SIZE__; |
klauss | 0:4d17cd9c8f9d | 68 | }if( type == __CB_BYE__ ){ |
klauss | 0:4d17cd9c8f9d | 69 | pkg[ 7 ] = seq_num; |
klauss | 0:4d17cd9c8f9d | 70 | //__print_clock__( pkg + 8 ); |
klauss | 0:4d17cd9c8f9d | 71 | pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ]; |
klauss | 0:4d17cd9c8f9d | 72 | fill = __TIMESLICE_PLACE__ + 1; |
klauss | 4:de46f0d9b14d | 73 | }else if( type == __TELEMETRY__ ){ |
klauss | 4:de46f0d9b14d | 74 | pkg[ 7 ] = seq_num; |
klauss | 14:22a35f575502 | 75 | //__print_clock__( pkg + 8 ); |
klauss | 4:de46f0d9b14d | 76 | pkg[ __TIMESLICE_PLACE__ ] = cb_buffer[ __TIMESLICE_PLACE__ ]; |
klauss | 4:de46f0d9b14d | 77 | fill = __TIMESLICE_PLACE__ + 1; |
klauss | 0:4d17cd9c8f9d | 78 | } |
klauss | 0:4d17cd9c8f9d | 79 | |
klauss | 0:4d17cd9c8f9d | 80 | for( register int i = fill; i < __CB_BUFFER_SIZE__; i++ ) pkg[ i ] = 0x00; |
klauss | 0:4d17cd9c8f9d | 81 | led2 = !led2; |
klauss | 0:4d17cd9c8f9d | 82 | |
klauss | 0:4d17cd9c8f9d | 83 | uint16_t cc = __checksum__( pkg, 300 ); |
klauss | 0:4d17cd9c8f9d | 84 | pkg[ 4 ] =( uint8_t )( ( cc & 0xFF00 ) >> 8) ; |
klauss | 0:4d17cd9c8f9d | 85 | pkg[ 5 ] =( uint8_t )( cc & 0x00FF ); |
klauss | 0:4d17cd9c8f9d | 86 | |
klauss | 17:67a6b557eda5 | 87 | //debug_msg(""); |
klauss | 0:4d17cd9c8f9d | 88 | return pkg; |
klauss | 0:4d17cd9c8f9d | 89 | } |
klauss | 0:4d17cd9c8f9d | 90 | |
klauss | 0:4d17cd9c8f9d | 91 | char * __build_eth__package__( void ){ |
klauss | 0:4d17cd9c8f9d | 92 | return( NULL ); |
klauss | 0:4d17cd9c8f9d | 93 | } |
klauss | 0:4d17cd9c8f9d | 94 | |
klauss | 0:4d17cd9c8f9d | 95 | uint8_t * __read_cb_buffer__( uint8_t * dest, uint8_t * src ){ |
klauss | 0:4d17cd9c8f9d | 96 | for( register int i = 0; i < __CB_BUFFER_SIZE__; i++ ) *dest++ = *src++; |
klauss | 0:4d17cd9c8f9d | 97 | return( dest ); |
klauss | 0:4d17cd9c8f9d | 98 | } |
klauss | 0:4d17cd9c8f9d | 99 | |
klauss | 0:4d17cd9c8f9d | 100 | uint8_t * __read_eth_buffer__( uint8_t * dest, uint8_t * src ){ |
klauss | 0:4d17cd9c8f9d | 101 | |
klauss | 0:4d17cd9c8f9d | 102 | for( register int i = 0; i < __ETH_BUFFER_SIZE__; i++ ) *dest++ = *src++; |
klauss | 0:4d17cd9c8f9d | 103 | |
klauss | 0:4d17cd9c8f9d | 104 | return( dest ); |
klauss | 0:4d17cd9c8f9d | 105 | } |
klauss | 0:4d17cd9c8f9d | 106 | |
klauss | 0:4d17cd9c8f9d | 107 | uint8_t * __write_cb_buffer__( uint8_t * dest, uint8_t * src ){ |
klauss | 0:4d17cd9c8f9d | 108 | for( register int i = 0; i < __CB_BUFFER_SIZE__; i++ ) *dest++ = *src++; |
klauss | 0:4d17cd9c8f9d | 109 | return( dest ); |
klauss | 0:4d17cd9c8f9d | 110 | } |
klauss | 0:4d17cd9c8f9d | 111 | |
klauss | 0:4d17cd9c8f9d | 112 | uint8_t * __write_eth_buffer__( uint8_t * dest, uint8_t * src ){ |
klauss | 0:4d17cd9c8f9d | 113 | for( register int i = 0; i < __ETH_BUFFER_SIZE__; i++ ) *dest++ = *src++; |
klauss | 0:4d17cd9c8f9d | 114 | |
klauss | 0:4d17cd9c8f9d | 115 | return( dest ); |
klauss | 0:4d17cd9c8f9d | 116 | } |
klauss | 0:4d17cd9c8f9d | 117 | |
klauss | 0:4d17cd9c8f9d | 118 | uint16_t __checksum__( uint8_t * buffer, size_t length ){ |
klauss | 0:4d17cd9c8f9d | 119 | uint16_t cc = 0x00; |
klauss | 0:4d17cd9c8f9d | 120 | buffer[ 4 ] = buffer[ 5 ] = 0x5a; |
klauss | 0:4d17cd9c8f9d | 121 | for( register int i = 0; i < length; i++ ){ |
klauss | 0:4d17cd9c8f9d | 122 | cc += buffer[ i ]; |
klauss | 0:4d17cd9c8f9d | 123 | if( cc & BIT15 ){ |
klauss | 0:4d17cd9c8f9d | 124 | cc <<= 1; |
klauss | 0:4d17cd9c8f9d | 125 | cc |= BIT0; |
klauss | 0:4d17cd9c8f9d | 126 | }else{ cc <<= BIT0; } |
klauss | 0:4d17cd9c8f9d | 127 | } |
klauss | 0:4d17cd9c8f9d | 128 | cc ^= 0xffff; |
klauss | 0:4d17cd9c8f9d | 129 | return cc; |
klauss | 0:4d17cd9c8f9d | 130 | } |
klauss | 0:4d17cd9c8f9d | 131 | |
klauss | 0:4d17cd9c8f9d | 132 | void __print_clock__( uint8_t * buffer ){ |
klauss | 14:22a35f575502 | 133 | debug_msg(""); |
klauss | 0:4d17cd9c8f9d | 134 | NTPClient ntp; |
klauss | 14:22a35f575502 | 135 | debug_msg(""); |
klauss | 14:22a35f575502 | 136 | //int ntp_result = ntp.setTime( "200.192.232.8", 123, 3 ); |
klauss | 0:4d17cd9c8f9d | 137 | struct tm ts; |
klauss | 14:22a35f575502 | 138 | int ntp_result = ntp.setTime( "200.192.232.8" ); |
klauss | 14:22a35f575502 | 139 | if( ntp_result == 0 ){ |
klauss | 14:22a35f575502 | 140 | debug_msg(""); |
klauss | 14:22a35f575502 | 141 | time_t seconds; |
klauss | 14:22a35f575502 | 142 | debug_msg(""); |
klauss | 14:22a35f575502 | 143 | // seconds = time(NULL); |
klauss | 14:22a35f575502 | 144 | time( &seconds ); |
klauss | 14:22a35f575502 | 145 | debug_msg(""); |
klauss | 14:22a35f575502 | 146 | ts = *localtime( &seconds ); |
klauss | 14:22a35f575502 | 147 | } |
klauss | 0:4d17cd9c8f9d | 148 | |
klauss | 14:22a35f575502 | 149 | debug_msg(""); |
klauss | 0:4d17cd9c8f9d | 150 | int ano = ts.tm_year + 1900; |
klauss | 0:4d17cd9c8f9d | 151 | int mes = ts.tm_mon + 1; |
klauss | 0:4d17cd9c8f9d | 152 | int dia = ts.tm_mday; |
klauss | 0:4d17cd9c8f9d | 153 | int hora = ts.tm_hour - 3; |
klauss | 0:4d17cd9c8f9d | 154 | int min = ts.tm_min; |
klauss | 0:4d17cd9c8f9d | 155 | int sec = ts.tm_sec; |
klauss | 0:4d17cd9c8f9d | 156 | |
klauss | 0:4d17cd9c8f9d | 157 | buffer[ 0 ] = ano / 1000; |
klauss | 0:4d17cd9c8f9d | 158 | ano -= buffer[ 0 ] * 1000; |
klauss | 0:4d17cd9c8f9d | 159 | buffer[ 1 ] = ano / 100; |
klauss | 0:4d17cd9c8f9d | 160 | ano -= buffer[ 1 ]* 100; |
klauss | 0:4d17cd9c8f9d | 161 | buffer[ 2 ] = ano / 10; |
klauss | 0:4d17cd9c8f9d | 162 | ano -= buffer[ 2 ] * 10; |
klauss | 0:4d17cd9c8f9d | 163 | buffer[ 3 ] = ano; |
klauss | 0:4d17cd9c8f9d | 164 | buffer[ 4 ] = mes / 10; |
klauss | 0:4d17cd9c8f9d | 165 | buffer[ 5 ] = mes % 10; |
klauss | 0:4d17cd9c8f9d | 166 | buffer[ 6 ] = dia / 10; |
klauss | 0:4d17cd9c8f9d | 167 | buffer[ 7 ] = dia % 10; |
klauss | 0:4d17cd9c8f9d | 168 | buffer[ 8 ] = hora / 10; |
klauss | 0:4d17cd9c8f9d | 169 | buffer[ 9 ] = hora % 10; |
klauss | 0:4d17cd9c8f9d | 170 | buffer[ 10 ] = min / 10; |
klauss | 0:4d17cd9c8f9d | 171 | buffer[ 11 ] = min % 10; |
klauss | 0:4d17cd9c8f9d | 172 | buffer[ 12 ] = sec / 10; |
klauss | 0:4d17cd9c8f9d | 173 | buffer[ 13 ] = sec % 10; |
klauss | 0:4d17cd9c8f9d | 174 | |
klauss | 0:4d17cd9c8f9d | 175 | /* convertendo pro ascii do nro */ |
klauss | 0:4d17cd9c8f9d | 176 | for( register int i = 0; i < 14; i++ ) buffer[ i ] += 0x30; |
klauss | 14:22a35f575502 | 177 | debug_msg(""); |
klauss | 0:4d17cd9c8f9d | 178 | } |