Dummy program to demonstrate problems: working code
Dependencies: SLCD mbed-rtos mbed
Fork of MNG_TC by
Diff: COM_RCV_TC.h
- Revision:
- 8:cb93c1d3209a
- Parent:
- 7:e71ecfe3a340
- Child:
- 16:de2224dd9a0d
--- a/COM_RCV_TC.h Mon Jul 06 05:00:29 2015 +0000 +++ b/COM_RCV_TC.h Mon Jul 13 10:21:45 2015 +0000 @@ -1,275 +1,225 @@ -#include "stdio.h" -#include <cstdlib> -//sample data source instead of buffer//remove while interfacing with RX1M..add the logic to add data in read_byte function -//have kept this slightly higher than actual//donno why gives error - - -class RCV_TC{ - -private: - uint16_t x; - //counter for keeping count of data read - TC_list *rcv_tc_head; - unsigned char temp[135]; +namespace COM_RCV_TC{ + unsigned int bytes_read = 0; + unsigned int tc_frames = 0; + unsigned char tempString[136]; + TC_list *return_this = NULL; - TC_list *t_frame; - TC_list *prev_frame; - - bool crc_pass; - - //this just converts a binary number to store in int variable, I am using while creating sample telecommands - #define B(x) S_to_binary_(#x) - static inline unsigned long long S_to_binary_(const char *s) - { - unsigned long long i = 0; - while (*s) { - i <<= 1; - i += *s++ - '0'; + void inline attach_frame(unsigned int length){ + // allocate new node + // handle Head node + if( tc_frames == 0 ){ + VAR_SPACE::Head_node = new TC_list; + VAR_SPACE::Head_node->next_TC = NULL; + VAR_SPACE::last_node = VAR_SPACE::Head_node; } - return i; - } - - void copy_bit(unsigned char* src_byte, unsigned char* dest_byte, unsigned char* count) - { - if (*src_byte & 0x80) - { - *dest_byte <<= 1; - *dest_byte += 1; + else{ + VAR_SPACE::last_node->next_TC = new TC_list; + VAR_SPACE::last_node = VAR_SPACE::last_node->next_TC; + VAR_SPACE::last_node->next_TC = NULL; + } + + // allocate memory for string + VAR_SPACE::last_node->TC_string = new unsigned char[length]; + for(int i = 0 ; i < length ; ++i){ + VAR_SPACE::last_node->TC_string[i] = tempString[i]; } - else *dest_byte <<= 1; - *src_byte <<= 1; - (*count)++; - } - - unsigned char read_byte(unsigned char* shift_in) - { - unsigned char byte; - - // byte = VAR_SPACE::data[x++]; - ++x; - - byte = VAR_SPACE::data_node->val; - data_list *temp = VAR_SPACE::data_node->next; - delete VAR_SPACE::data_node; - VAR_SPACE::data_node = temp; - - if( x == 1 ){ - VAR_SPACE::head_data = new data_list; - VAR_SPACE::rx_new_node = VAR_SPACE::head_data; - VAR_SPACE::rx_new_node->next = NULL; + + // short or long + VAR_SPACE::last_node->short_or_long = (length == 11) ? true : false; + + // crc pass or fail + uint16_t crc_checksum = CRC::crc16_gen(tempString, length-2); + if( ( (crc_checksum & 0xFF) == tempString[length-1]) && ( ((crc_checksum >> 8) & 0xFF) == tempString[length-2] ) ){ + VAR_SPACE::last_node->crc_pass = true; + } + else{ + VAR_SPACE::last_node->crc_pass = false; } - *shift_in = 0; - - return byte; - } - - void attach_byte(unsigned char* a_byte, unsigned char* count, unsigned char* byte_no) - { - temp[(*byte_no)++] = *a_byte; - *count = 0; - } - - void attach_frame(TC_list *t_frame, unsigned char* frame_no, unsigned char* byte_no) - { - t_frame = new TC_list; - t_frame->next_TC = NULL; - if(*frame_no == 0){ - rcv_tc_head = t_frame; - prev_frame = rcv_tc_head; - } - else{ - prev_frame->next_TC = t_frame; - prev_frame = t_frame; - } - prev_frame->TC_string = new unsigned char[*byte_no]; - for(int i = 0 ; i < *byte_no ; ++i){ - prev_frame->TC_string[i] = temp[i]; - } - if(*byte_no == 11) - { - prev_frame->short_or_long = true; - } + // PSC + VAR_SPACE::last_node->packet_seq_count = VAR_SPACE::last_node->TC_string[0]; + + // apid + unsigned char tempChar = VAR_SPACE::last_node->TC_string[1]; + VAR_SPACE::last_node->apid = (tempChar >> 6) & 3; + + // abort on nack + VAR_SPACE::last_node->abort_on_nack = (((tempChar >> 3) & 1) == 1) ? true : false; + + // default values of enable and execution status + VAR_SPACE::last_node->enabled = true; + VAR_SPACE::last_node->exec_status = 0; - *byte_no = 0; - ++(*frame_no); +// printf("inside attach frame : frame num = %u\r\n", tc_frames); + +// for(int i = 0 ; i < length ; ++i){ +// std::bitset<8> b = VAR_SPACE::last_node->TC_string[i]; +// cout << b << " "; +// } +// cout << ENDL; +// for( int i = 0 ; i < length ; ++i){ +// std::bitset<8> b = tempString[i]; +// cout << b << " "; +// } +// cout << ENDL; + + ++tc_frames; } - - void scan_dstuff(unsigned char* fifo_byte, unsigned char* test_byte, unsigned char* flag, unsigned char* shift_in, unsigned char* dstuff_count) - { - if (*test_byte == 0x7E)//scan for flag - { - *flag = 2; - return; - } - if (((*test_byte) & 0xFC) == 0xF8)//destuff - { - if (*shift_in == 8) - *fifo_byte = read_byte(shift_in); - copy_bit(fifo_byte, test_byte, shift_in); - *test_byte = *test_byte | 0xF8; - *dstuff_count = 4; + + void flushData(const unsigned int& bytes, const unsigned char& outState){ + if( (bytes == 11) && (outState == 7) ){ + attach_frame(11); } - - - } - void post_flag(unsigned char* a_byte, unsigned char* b_byte, unsigned char *shift_in) - { - - for (int i = 0; i < 8; i++) - { - if (*shift_in == 8) - { - *a_byte = read_byte(shift_in); - } - copy_bit(a_byte, b_byte, shift_in); + else if( (bytes == 135) && (outState == 7) ){ + attach_frame(135); } - - } - void post_shift_out(unsigned char* fifo_byte, unsigned char* test_byte, unsigned char* shift_in) - { - if (*shift_in == 8) - *fifo_byte = read_byte(shift_in); - if (*fifo_byte & 0x80) - *test_byte |= 0x01; - (*shift_in)++; - *fifo_byte <<= 1; } - bool check_crc(unsigned char &byte_no) - { - uint16_t exp_crc = CRC::crc16_gen(temp, (byte_no - 2)); - uint16_t cal_crc = (temp[byte_no - 2] << 8) | temp[byte_no - 1]; - if (exp_crc == cal_crc) - { - return 1; - } - else - return 0; - } - void check_last_frame(unsigned char* last_frame) - { - if (temp[1] & 0x20) - { - *last_frame = 1; - }//considering 3rd bit of 2nd byte of packet as last bit flag - } - -public: - - // Constructor - RCV_TC(TC_list *HEAD){ - rcv_tc_head = HEAD; - x = 0; - } - - void RX_RCV_TC() - { - x = 0; - int count_down = 0; - unsigned char test_byte = 0; - unsigned char flag = 0; - //flag = 0 ... scanning for 0x7E/searching for start of new frame - //flag = 1 ... after detecting 0x7E, now start dstuffing , scanning for 7E, filling the fifo_byte if empty, attach the dstuff_byte - //flag = 2 ... detected 0x7E, check if the current t_frame is valid , then atttach the t_frame to p_frame - unsigned char fifo_byte = 0; - unsigned char dstuff_byte = 0; - unsigned char byte_no = 0; - unsigned char frame_no = 0; - unsigned char shift_in = 0, shift_out = 0; // flag to keep count of bytes shifted in/out of test byte - unsigned char dstuff_count = 0; - unsigned char last_frame = 0; - - //Processing starts here - while (!last_frame)//process till last frame - { -// printf("inside last frame\r\n"); - while (flag == 0) - { - printf("entered flag 0\r\n"); - if (fifo_byte != 0x7E)//check for initial 0x7E, else discard and take next byte //here I am checking byte wise instead of bitwise!!! - { - count_down++; - - fifo_byte = read_byte(&shift_in); - continue; + void rx_rcv_tc(void){ + bool frame_started = false; + bytes_read = 0; + unsigned char state7e = 0; + unsigned char outState = 0; + unsigned int outByte = 0; + bool chain_started = false; + unsigned int byteCount = 0; + + // read byte by byte + while( VAR_SPACE::data_node != NULL ){ + + unsigned char test_this = VAR_SPACE::data_node->val; + ++bytes_read; + + struct data_list *temp = VAR_SPACE::data_node->next; + delete VAR_SPACE::data_node; + VAR_SPACE::data_node = temp; + if( bytes_read == 1 ){ + VAR_SPACE::head_data = new struct data_list; + VAR_SPACE::head_data->next = NULL; + VAR_SPACE::rx_new_node = VAR_SPACE::head_data; + } + + // read bit by bit + for(int i = 7 ; i >= 0 ; --i){ + unsigned char tempBit = (test_this >> i) & 1; + bool skipIteration = false; + + if( tempBit == 1 ){ + switch( state7e ){ + case 0: + state7e = 0; + break; + case 1: + state7e = 2; + break; + case 2: + state7e = 3; + break; + case 3: + state7e = 4; + break; + case 4: + state7e = 5; + break; + case 5: + state7e = 6; + break; + case 6: + state7e = 7; + break; + case 7: +// error reset + state7e = 0; + chain_started = false; + frame_started = false; + byteCount = 0; + outByte = 0; + outState = 0; + skipIteration = true; + break; + } } - else - { - count_down = 0; - flag = 1; - - fifo_byte = read_byte(&shift_in); - post_flag(&fifo_byte, &test_byte, &shift_in); + else{ + switch( state7e ){ + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + state7e = 1; + break; + case 6: + state7e = 1; + skipIteration = true; + break; + case 7: + state7e = 0; + // detected 7e +// printf("detected 7e : chain start : %u, frame_start : %u\r\n", (chain_started ? 1 : 0), (frame_started ? 1 : 0) ); + if( !chain_started ){ + chain_started = true; + frame_started = true; + byteCount = 0; + outByte = 0; + outState = 0; + skipIteration = true; + } + else{ + flushData(byteCount, outState); + byteCount = 0; + outState = 0; + outByte = 0; + skipIteration = true; + } + break; + } + } + if( (!skipIteration) && (frame_started) ){ + // write bit to output + switch( outState ){ + case 0: + outState = 1; + tempString[outByte] = tempBit << 7; + break; + case 1: + outState = 2; + tempString[outByte] += tempBit << 6; + break; + case 2: + outState = 3; + tempString[outByte] += tempBit << 5; + break; + case 3: + outState = 4; + tempString[outByte] += tempBit << 4; + break; + case 4: + outState = 5; + tempString[outByte] += tempBit << 3; + break; + case 5: + outState = 6; + tempString[outByte] += tempBit << 2; + break; + case 6: + outState = 7; + tempString[outByte] += tempBit << 1; + break; + case 7: +// printf("wrote a byte in tempString\r\n"); + outState = 0; + tempString[outByte] += tempBit; + ++outByte; + // exceeded tc length discard + if(outByte > 135){ + outByte = 0; + } + ++byteCount; + break; + } } } - if (flag == 1) - { - for (; shift_in < 8;)//loop till all bits are written in test_byte from fifo_byte - { - if (shift_out == 8)//if the bits written to dstuff_byte =8 - { - if (byte_no < 11)//if long frame-size hasn't reached, - attach_byte(&dstuff_byte, &shift_out, &byte_no);//attach_byte to t_frame - else//else attach frame to the p_frame array - { - attach_frame(t_frame, &frame_no, &byte_no); - flag = 0; - post_flag(&fifo_byte, &test_byte, &shift_in); - dstuff_byte = 0; - break; - } - } - - if (!(dstuff_count)) - scan_dstuff(&fifo_byte, &test_byte, &flag, &shift_in, &dstuff_count); - else - dstuff_count--;//bcoz only 4th bit is being removed //don't destuff for next four loops - if (flag == 2) - break; - copy_bit(&test_byte, &dstuff_byte, &shift_out);//shift_out - post_shift_out(&fifo_byte, &test_byte, &shift_in);//updating last bit of test_byte - } - } - - if (flag == 0) - continue; - if (shift_in == 8) - fifo_byte = read_byte(&shift_in); - if (flag == 2) // detected next 0x7E - { - if (shift_out % 8 == 0)//check if the t_frame is multiple of 8 else discard - { - crc_pass = check_crc(byte_no); - if (crc_pass) - { - check_last_frame(&last_frame); //check if the current frame is last frame then set the last_frame flag -// b = byte_no; - attach_frame(t_frame, &frame_no, &byte_no); //then attach the t_frame to p_frame array - flag = 1; //start scanning new frame - prev_frame->crc_pass=1; - } - else - { - check_last_frame(&last_frame); //need to put timer instead of this to avoid problems due to last_frame bit error -// b = byte_no; - attach_frame(t_frame, &frame_no, &byte_no); - flag = 1; - prev_frame->crc_pass = 0; - shift_out = 0; - } - } - post_flag(&fifo_byte, &test_byte, &shift_in); - dstuff_byte = 0; - } - } - - //RX end - while(VAR_SPACE::data_node != NULL){ - data_list *temp = VAR_SPACE::data_node->next; - delete VAR_SPACE::data_node; - VAR_SPACE::data_node = temp; } } - -}; +} \ No newline at end of file