Dummy program to demonstrate problems: working code

Dependencies:   SLCD mbed-rtos mbed

Fork of MNG_TC by Shreesha S

COM_RCV_TC.h

Committer:
shreeshas95
Date:
2015-09-17
Revision:
17:2b04e53f3b1d
Parent:
8:cb93c1d3209a
Child:
16:de2224dd9a0d

File content as of revision 17:2b04e53f3b1d:

namespace COM_RCV_TC{
    unsigned int bytes_read = 0;
    unsigned int tc_frames = 0;
    unsigned char tempString[136];
    TC_list *return_this = NULL;
    
    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;
        }
        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];
        }

        // 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;
        }

        // 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;
        
//        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 flushData(const unsigned int& bytes, const unsigned char& outState){
        if( (bytes == 11) && (outState == 7) ){
            attach_frame(11);
        }
        else if( (bytes == 135) && (outState == 7) ){
            attach_frame(135);
        }
    }

    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{
                    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;
                    }
                }
            }
        }
    }
}