Repository for CDMS code

Dependencies:   SimpleDMA mbed-rtos mbed eeprom

Fork of COM_MNG_TMTC_SIMPLE by Shreesha S

Files at this revision

API Documentation at this revision

Comitter:
shreeshas95
Date:
Tue Dec 01 10:56:10 2015 +0000
Child:
1:a0055b3280c8
Commit message:
Working without COM_SND_TM

Changed in this revision

COM_MNG_TMTC.h Show annotated file Show diff for this revision Revisions of this file
COM_RCV_TC.h Show annotated file Show diff for this revision Revisions of this file
COM_SND_TM_functions.h Show annotated file Show diff for this revision Revisions of this file
DefinitionsAndGlobals.h Show annotated file Show diff for this revision Revisions of this file
Structures.h Show annotated file Show diff for this revision Revisions of this file
ThreadsAndFunctions.h Show annotated file Show diff for this revision Revisions of this file
crc.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/COM_MNG_TMTC.h	Tue Dec 01 10:56:10 2015 +0000
@@ -0,0 +1,384 @@
+// 8 Jul
+// did flowchart of states
+
+// handle sd card with cdms team
+
+// Jun 6
+// WHAT IS TC exec code in L1 ack ? 
+
+#define delete_TC(tc_ptr) {\
+    if(tc_ptr == gHEAD_NODE_TCL){\
+        gHEAD_NODE_TCL = tc_ptr->next_TC;\
+    }\
+    delete tc_ptr;\
+}
+
+// typeof tm_ptr: Base_tm
+// typeof tc_ptr: Base_tc
+// typeof temp_xxxx: uint8_t
+#define fill_l1_ack(tm_ptr) {\
+    uint8_t temp8;\
+    tm_ptr->next_TM = NULL;\
+    temp8 = 0xA;\
+    PUTtmid(tm_ptr->fields, temp8);\
+    temp8 = 0x00;\
+    PUTshort_or_long_tm(tm_ptr->fields, temp8);\
+    tm_ptr->TM_string[0] = TMID_ACK_L1 << 4;\
+    tm_ptr->TM_string[1] = gTOTAL_INCORRECT_SIZE_TC & 0xFF;\
+    tm_ptr->TM_string[2] = gTOTAL_CRC_FAIL_TC & 0xFF;\
+    tm_ptr->TM_string[3] = (gMASTER_STATE << 4) & 0xF0;\
+}
+
+#define put_crc_l1_ack(tm_ptr, num_crc) {\
+    tm_ptr->TM_string[2] = num_crc & 0xFF;\
+    crc_checksum = crc16_gen(tm_ptr->TM_string, TM_SHORT_SIZE-2);\
+    tm_ptr->TM_string[TM_SHORT_SIZE-2] = (crc_checksum >> 8) & 0xFF;\
+    tm_ptr->TM_string[TM_SHORT_SIZE-1] = crc_checksum & 0xFF;\
+}
+
+#define detect_ack(tm_ptr, temp_ack) {\
+    uint8_t temp8;\
+    if( tm_ptr != NULL ){\
+        temp8 = tm_ptr->TM_string[3];\
+        if((temp8 == 0xE0) || (temp8 == 0xA0) || (temp8 == 0xC0))\
+            temp_ack = 0x01;\
+        else\
+            temp_ack = 0x00;\
+    }\
+    else\
+        temp_ack = 0x00;\
+}
+
+#define isit_obosc(tc_ptr, temp_obosc) {\
+    temp_obosc = 0x00;\
+    if( GETapid(tc_ptr) == 2 ){\
+        if( ((tc_ptr->TC_string[2]) >> 4) == 0xB ){\
+            switch( (tc_ptr->TC_string[2]) & 0xf ){\
+                case 1:\
+                case 2:\
+                case 5:\
+                case 6:\
+                case 15:\
+                    temp_obosc = 0x01;\
+            }\
+        }\
+    }\
+}
+
+#define isit_sdcard(tc_ptr, temp_sdcard) {\
+    temp_sdcard = 0x00;\
+    if( GETapid(tc_ptr) == 2 ){\
+        if( ( (tc_ptr->TC_string[2]) >> 4) == 0xF ){\
+            switch( (tc_ptr->TC_string[2]) & 0xf ){\
+                case 0:\
+                case 1:\
+                case 2:\
+                case 3:\
+                case 4:\
+                    temp_sdcard = 0x01;\
+            }\
+        }\
+    }\
+}
+
+/*
+@brief:     check for missing tc, also check crc, i.e. 
+            if true execution can be started else have to wait
+            decide the next state
+@param:     none
+@return:    bool indicating whether there are missing tc
+*/
+// PENDING: LAST FRAME BIT
+/*
+for loop: check for missing tc
+if: check for incorrect sized tc
+if: check for last frame bit
+*/
+#define continueToExecute(returnHere) {\
+    uint8_t tempReturn = 0x00;\
+    for(uint8_t p = PSC_START_VALUE ; p < (gTOTAL_VALID_TC + PSC_START_VALUE) ; ++p){\
+        bool flag = false;\
+        Base_tc *node_ptr = gHEAD_NODE_TCL;\
+        while(node_ptr != NULL){\
+            if( (GETpacket_seq_count(node_ptr) == p) && (GETcrc_pass(node_ptr) == 1) ){\
+                flag = true;\
+                break;\
+            }\
+            else{\
+                node_ptr = node_ptr->next_TC;\
+            }\
+        }\
+        if(flag == false){\
+            tempReturn = 0x02;\
+            break;\
+        }\
+    }\
+    Base_tc *tcp = gHEAD_NODE_TCL;\
+    while(tcp != NULL){\
+        if(GETpacket_seq_count(tcp) == (gTOTAL_VALID_TC + PSC_START_VALUE - 1)){\
+            if( ( (tcp->TC_string[1]) & 0x20 ) == 0x00 ){\
+                tempReturn = tempReturn + 0x01;\
+            }\
+            break;\
+        }\
+        tcp = tcp->next_TC;\
+    }\
+    returnHere = tempReturn;\
+}
+
+/*
+@brief:     DELETE THE CRC FAILED TC FROM THE LIST TO FREE-UP MEMORY AND UPDATE 
+            THE TOTAL VALID TC AND GENERATE L1_ACK_TM
+@param:     none
+@return:    none
+*/
+#define send_l1_ack {\
+    Base_tc *current_TC = gHEAD_NODE_TCL;\
+    Base_tm *l1_ack = new Short_tm;\
+    Base_tm *l1_ack_head = l1_ack;\
+    fill_l1_ack(l1_ack);\
+    int TC_count = 0;\
+    uint16_t crc_checksum = 0;\
+    while(current_TC != NULL){\
+        /*IF CRC PASS*/\
+        if( (GETcrc_pass(current_TC) == 1) ){\
+            if(TC_count > 4){\
+                /*extend the TM linked list*/\
+                TC_count = 0;\
+                l1_ack->next_TM = new Short_tm;\
+                l1_ack = l1_ack->next_TM;\
+                fill_l1_ack(l1_ack);\
+                /*PENDING: FILL TC_EXEC_CODE, APPEND CRC TO THE TM*/\
+                put_crc_l1_ack( l1_ack, gTOTAL_CRC_FAIL_TC );\
+            }\
+            /*PSC starts from 7th byte*/\
+            l1_ack->TM_string[6+TC_count] = current_TC->TC_string[0];\
+            /*TC exec status*/\
+            switch(TC_count){\
+                case 0:\
+                    l1_ack->TM_string[3] |= (GETexec_status(current_TC)) & 0x0F;\
+                    break;\
+                case 1:\
+                    l1_ack->TM_string[4] = (GETexec_status(current_TC) << 4) & 0xF0;\
+                    break;\
+                case 2:\
+                    l1_ack->TM_string[4] |= (GETexec_status(current_TC)) & 0x0F;\
+                    break;\
+                case 3:\
+                    l1_ack->TM_string[5] = (GETexec_status(current_TC) << 4) & 0xF0;\
+                    break;\
+                case 4:\
+                    l1_ack->TM_string[5] |= (GETexec_status(current_TC)) & 0x0F;\
+            }\
+            ++TC_count;\
+        }\
+        current_TC = current_TC->next_TC;\
+    }\
+    /*FILL UP THE REMAINING FIELDS WITH 0x01: 0x00 => problem with GS*/\
+    if(TC_count < 5){\
+        while(TC_count < 5){\
+            l1_ack->TM_string[6+TC_count] = current_TC->TC_string[0];\
+            switch(TC_count){\
+                case 0:\
+                    l1_ack->TM_string[3] |= (GETexec_status(current_TC)) & 0x0F;\
+                    break;\
+                case 1:\
+                    l1_ack->TM_string[4] = (GETexec_status(current_TC) << 4) & 0xF0;\
+                    break;\
+                case 2:\
+                    l1_ack->TM_string[4] |= (GETexec_status(current_TC)) & 0x0F;\
+                    break;\
+                case 3:\
+                    l1_ack->TM_string[5] = (GETexec_status(current_TC) << 4) & 0xF0;\
+                    break;\
+                case 4:\
+                    l1_ack->TM_string[5] |= (GETexec_status(current_TC)) & 0x0F;\
+            }\
+            ++TC_count;\
+        }\
+        put_crc_l1_ack(l1_ack, gTOTAL_CRC_FAIL_TC);\
+    }\
+    /*snd_tm.head_pointer(l1_ack_head);*/\
+    /*adf_not_SDcard();*/\
+    /*delete the TM*/\
+    l1_ack = l1_ack_head;\
+    while(l1_ack != NULL){\
+        Base_tm *temp = l1_ack->next_TM;\
+        delete l1_ack;\
+        l1_ack = temp;\
+    }\
+}
+
+// CDMS TEAM CODE START
+#define CDMS_RLY_TMTC(tc_ptr, tm_ptr){\
+    tm_ptr = NULL;\
+}\
+// CDMS TEAM CODE END
+
+// EXECUTE OBOSC
+#define execute_obosc_core(tc_ptr, tm_ptr) {\
+    uint8_t service_subtype = (tc_ptr->TC_string[2]) & 0x0F;\
+    /*including both lower and upper limits*/\
+    uint8_t targetL = tc_ptr->TC_string[3];\
+    uint8_t targetU = targetL + tc_ptr->TC_string[4] - 1;\
+    if( (targetL == 0x00) && (tc_ptr->TC_string[4] == 0) ){\
+        /*PENDING: ALL REPORTS BELOW*/\
+        switch(service_subtype){\
+            case OBOSC_SUB_REP_TCL_D:\
+                break;\
+            case OBOSC_SUB_REP_LE:\
+                break;\
+            case OBOSC_SUB_RESET:\
+                break;\
+            case OBOSC_SUB_REP_TCL:\
+                break;\
+        }\
+    }\
+    else{\
+        uint16_t temp16 = 2;\
+        switch( service_subtype ){\
+            case OBOSC_SUB_DISABLE:\
+                temp16 = 2;\
+                break;\
+            case OBOSC_SUB_RETRY:\
+                temp16 = 3;\
+                break;\
+        }\
+        Base_tc *tcp = gHEAD_NODE_TCL;\
+        while( tcp != NULL ){\
+            uint16_t tcPSC = GETpacket_seq_count(tcp);\
+            if( (tcPSC >= targetL) && (tcPSC <= targetU) ){\
+                PUTexec_status(tcp, temp16);\
+            }\
+            tcp = tcp->next_TC;\
+        }\
+    }\
+    /*PENDING: generate L234 ACK*/\
+    tm_ptr = NULL;\
+}
+
+#define EXECUTE_OBOSC_ONLY {\
+    for(uint8_t execute_psc = gOBOSC_PSC ; execute_psc < (gTOTAL_VALID_TC+PSC_START_VALUE) ; ++execute_psc){\
+        Base_tc* current_TC = gOBOSC_HEAD;\
+        while( current_TC != NULL ){\
+            if( (GETcrc_pass(current_TC) == 1) && (GETpacket_seq_count(current_TC) == execute_psc) ){\
+                uint8_t current_exec_status = GETexec_status(current_TC);\
+                if( (current_exec_status == TC_STATE_SUCCESSFULLY_EXECUTED) || (current_exec_status == TC_STATE_DISABLED) )\
+                    break;\
+                else if( (current_exec_status == TC_STATE_EXECUTION_FAILED) && (GETabort_on_nack(current_TC) == 1) ){\
+                    gMASTER_STATE = TCL_STATE_ABORTED;\
+                    break;\
+                }\
+                else if( (current_exec_status == TC_STATE_UNEXECUTED) || (current_exec_status == TC_STATE_MARKED_RETRY) ){\
+                    Base_tm* tm_ptr = NULL;\
+                    uint8_t temp82 = 0x00;\
+                    isit_obosc(current_TC, temp82);\
+                    if(temp82 == 0x01){\
+                        /*EXECUTION OF OBOSC TC*/\
+                        execute_obosc_core(current_TC, tm_ptr);\
+                    }\
+                    /*snd_tm.head_pointer(tm_ptr);*/\
+                    /*adf_not_SDcard();*/\
+                    uint8_t temp83 = 0x00;\
+                    detect_ack(tm_ptr, temp83);\
+                    if( temp83 == 0x01){\
+                        uint16_t temp16 = TC_STATE_SUCCESSFULLY_EXECUTED;\
+                        PUTexec_status(current_TC, temp16);\
+                    }\
+                    else{\
+                        uint16_t temp16 = TC_STATE_EXECUTION_FAILED;\
+                        PUTexec_status(current_TC, temp16);\
+                    }\
+                    /*update last executed L1_ack*/\
+                    if( tm_ptr != NULL ){\
+                        for(int i = 0 ; i < TM_SHORT_SIZE ; ++i){\
+                            gLAST_L1_ACK[i] = gLAST_L1_ACK_BUFFER[i];\
+                            gLAST_L1_ACK_BUFFER[i] = tm_ptr->TM_string[i];\
+                        }\
+                    }\
+                    /*DELETE THE TM AFTER USE*/\
+                    while(tm_ptr != NULL){\
+                        Base_tm *temp = tm_ptr->next_TM;\
+                        delete tm_ptr;\
+                        tm_ptr = temp;\
+                    }\
+                }\
+            }\
+            current_TC = current_TC->next_TC;\
+        }\
+    }\
+}
+
+#define EXECUTE_TC {\
+    for(uint8_t execute_psc = PSC_START_VALUE ; execute_psc < (PSC_START_VALUE+gTOTAL_VALID_TC) ; ++execute_psc ){\
+        gLEDG = !gLEDG;\
+        gLEDR = !gLEDR;\
+        Base_tc* current_TC = gHEAD_NODE_TCL;\
+        while(current_TC != NULL){\
+            if( (GETcrc_pass(current_TC) == 1) && (GETpacket_seq_count(current_TC) == execute_psc) ){\
+                uint8_t current_exec_status = GETexec_status(current_TC);\
+                if( (current_exec_status == TC_STATE_SUCCESSFULLY_EXECUTED) || (current_exec_status == TC_STATE_DISABLED) )\
+                    break;\
+                else if( (current_exec_status == TC_STATE_EXECUTION_FAILED) && (GETabort_on_nack(current_TC) == 1) ){\
+                    gMASTER_STATE = TCL_STATE_ABORTED;\
+                    break;\
+                }\
+                else if( (current_exec_status == TC_STATE_UNEXECUTED) || (current_exec_status == TC_STATE_MARKED_RETRY) ){\
+                    /*EXECUTION OF TC START*/\
+                    uint8_t temp81 = 0x00;\
+                    isit_sdcard(current_TC, temp81);\
+                    if( temp81 == 0x00 ){\
+                        /*EXECUTION OF NON SD-CARD (BOTH OBOSC and CDMS functions)*/\
+                        Base_tm *tm_ptr;\
+                        uint8_t temp82 = 0x00;\
+                        isit_obosc(current_TC, temp82);\
+                        if(temp82 == 0x01){\
+                            /*EXECUTION OF OBOSC TC*/\
+                            execute_obosc_core(current_TC, tm_ptr);\
+                        }\
+                        else{\
+                            /*call CDMS_RLY_TMTC*/\
+                            CDMS_RLY_TMTC(current_TC, tm_ptr);\
+                        }\
+                        /*snd_tm.head_pointer(tm_ptr);*/\
+                        /*adf_not_SDcard();*/\
+                        uint8_t temp83 = 0x00;\
+                        detect_ack(tm_ptr, temp83);\
+                        if( temp83 == 0x01){\
+                            uint16_t temp16 = TC_STATE_SUCCESSFULLY_EXECUTED;\
+                            PUTexec_status(current_TC, temp16);\
+                        }\
+                        else{\
+                            uint16_t temp16 = TC_STATE_EXECUTION_FAILED;\
+                            PUTexec_status(current_TC, temp16);\
+                        }\
+                        /*update last executed L1_ack*/\
+                        if( tm_ptr != NULL ){\
+                            for(int i = 0 ; i < TM_SHORT_SIZE ; ++i){\
+                                gLAST_L1_ACK[i] = gLAST_L1_ACK_BUFFER[i];\
+                                gLAST_L1_ACK_BUFFER[i] = tm_ptr->TM_string[i];\
+                            }\
+                        }\
+                        /*DELETE THE TM AFTER USE*/\
+                        while(tm_ptr != NULL){\
+                            Base_tm *temp = tm_ptr->next_TM;\
+                            delete tm_ptr;\
+                            tm_ptr = temp;\
+                        }\
+                    }\
+                    else{\
+                        /*EXECUTION OF SD-CARD DATA SENDING (OBSRS)*/\
+                        /*read_TC(tc_ptr);*/\
+                    }\
+                    /*ABORT ON NACK*/\
+                    if( (GETexec_status(current_TC) == TC_STATE_EXECUTION_FAILED) && (GETabort_on_nack(current_TC) == 1) ){\
+                        gMASTER_STATE = TCL_STATE_ABORTED;\
+                    }\
+                }\
+            }\
+            current_TC = current_TC->next_TC;\
+        }\
+        if( gMASTER_STATE == TCL_STATE_ABORTED )\
+            break;\
+    }\
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/COM_RCV_TC.h	Tue Dec 01 10:56:10 2015 +0000
@@ -0,0 +1,263 @@
+#define flushData_appendTC(bytes, outState) {\
+    uint16_t tc_type = 2;\
+    if( (bytes == TC_SHORT_SIZE) && (outState == 7) ){\
+        tc_type = 0;\
+    }\
+    else if( (bytes == TC_LONG_SIZE) && (outState == 7) ){\
+        tc_type = 1;\
+    }\
+    else{\
+        ++rx_incorrectSizedTCcount;\
+    }\
+    if( tc_type == 0 ){\
+        uint16_t crc_checksum = crc16_gen(rx_tempString, TC_SHORT_SIZE-2);\
+        if( ( (crc_checksum & 0xFF) == rx_tempString[TC_SHORT_SIZE-1]) && ( ((crc_checksum >> 8) & 0xFF) == rx_tempString[TC_SHORT_SIZE-2] ) ){\
+            uint16_t temp16;\
+            Base_tc *temp_ptr = gHEAD_NODE_TCL;\
+            bool repeat_flag = false;\
+            uint8_t testPSC = rx_tempString[0];\
+            while(temp_ptr != NULL){\
+                if( GETpacket_seq_count(temp_ptr) == testPSC ){\
+                    repeat_flag = true;\
+                    break;\
+                }\
+                temp_ptr = temp_ptr->next_TC;\
+            }\
+            if( repeat_flag == false ){\
+                if( rx_tc_frames == 0 ){\
+                    gHEAD_NODE_TCL = new Short_tc;\
+                    gHEAD_NODE_TCL->next_TC = NULL;\
+                    gLAST_NODE_TCL = gHEAD_NODE_TCL;\
+                }\
+                else{\
+                    gLAST_NODE_TCL->next_TC = new Short_tc;\
+                    gLAST_NODE_TCL = gLAST_NODE_TCL;\
+                    gLAST_NODE_TCL->next_TC = NULL;\
+                }\
+                for(int i = 0 ; i < TC_SHORT_SIZE ; ++i){\
+                    gLAST_NODE_TCL->TC_string[i] = rx_tempString[i];\
+                }\
+                PUTshort_or_long(gLAST_NODE_TCL, tc_type);\
+                temp16 = 1;\
+                PUTcrc_pass(gLAST_NODE_TCL, temp16);\
+                temp16 = 0;\
+                PUTexec_status(gLAST_NODE_TCL, temp16);\
+                ++gTOTAL_VALID_TC;\
+            }\
+        }\
+        else{\
+            ++gTOTAL_CRC_FAIL_TC;\
+        }\
+    }\
+    else if( tc_type == 1 ){\
+        uint16_t crc_checksum = crc16_gen(rx_tempString, TC_LONG_SIZE-2);\
+        if( ( (crc_checksum & 0xFF) == rx_tempString[TC_LONG_SIZE-1]) && ( ((crc_checksum >> 8) & 0xFF) == rx_tempString[TC_LONG_SIZE-2] ) ){\
+            uint16_t temp16;\
+            Base_tc *temp_ptr = gHEAD_NODE_TCL;\
+            bool repeat_flag = false;\
+            uint8_t testPSC = rx_tempString[0];\
+            while(temp_ptr != NULL){\
+                if( GETpacket_seq_count(temp_ptr) == testPSC ){\
+                    repeat_flag = true;\
+                    break;\
+                }\
+                temp_ptr = temp_ptr->next_TC;\
+            }\
+            if( repeat_flag == false ){\
+                if( rx_tc_frames == 0 ){\
+                    gHEAD_NODE_TCL = new Long_tc;\
+                    gHEAD_NODE_TCL->next_TC = NULL;\
+                    gLAST_NODE_TCL = gHEAD_NODE_TCL;\
+                }\
+                else{\
+                    gLAST_NODE_TCL->next_TC = new Long_tc;\
+                    gLAST_NODE_TCL = gLAST_NODE_TCL->next_TC;\
+                    gLAST_NODE_TCL->next_TC = NULL;\
+                }\
+                for(int i = 0 ; i < TC_LONG_SIZE ; ++i){\
+                    gLAST_NODE_TCL->TC_string[i] = rx_tempString[i];\
+                }\
+                PUTshort_or_long(gLAST_NODE_TCL, tc_type);\
+                temp16 = 1;\
+                PUTcrc_pass(gLAST_NODE_TCL, temp16);\
+                temp16 = 0;\
+                PUTexec_status(gLAST_NODE_TCL, temp16);\
+                ++gTOTAL_VALID_TC;\
+            }\
+        }\
+        else{\
+            ++gTOTAL_CRC_FAIL_TC;\
+        }\
+    }\
+    ++rx_tc_frames;\
+}
+
+unsigned int rx_tc_frames = 0;
+unsigned char rx_tempString[TC_LONG_SIZE+1];
+uint32_t rx_incorrectSizedTCcount = 0;
+
+void raw_data_to_tc(void){
+    bool frame_started = false;
+    bool chain_started = false;
+    bool flush_called = false;
+
+    uint32_t bytes_read = 0;
+    uint8_t state7e = 0;
+    uint8_t outState = 0;
+    uint32_t outByte = 0;
+    uint32_t byteCount = 0;
+    rx_incorrectSizedTCcount = 0;
+    
+    gOBOSC_PSC = PSC_START_VALUE + gTOTAL_VALID_TC - 1;
+    gOBOSC_HEAD = gLAST_NODE_TCL;
+    
+    COM_RX_DATA_NODE *data_node = gRX_HEAD_DATA_NODE;
+    // read byte by byte
+    while( data_node != NULL ){
+        for(int iBuf = 0 ; iBuf < RX_BUFFER_LENGTH ; ++iBuf ){
+            uint8_t test_this = data_node->values[iBuf];
+            ++bytes_read;
+    
+            // 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
+                            if( !chain_started ){
+                                chain_started = true;
+                                frame_started = true;
+                                byteCount = 0;
+                                outByte = 0;
+                                outState = 0;
+                                skipIteration = true;
+                            }
+                            else{
+                                flush_called = true;
+                                flushData_appendTC(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;
+                            rx_tempString[outByte] = tempBit << 7;
+                            break;
+                        case 1:
+                            outState = 2;
+                            rx_tempString[outByte] += tempBit << 6;
+                            break;
+                        case 2:
+                            outState = 3;
+                            rx_tempString[outByte] += tempBit << 5;
+                            break;
+                        case 3:
+                            outState = 4;
+                            rx_tempString[outByte] += tempBit << 4;
+                            break;
+                        case 4:
+                            outState = 5;
+                            rx_tempString[outByte] += tempBit << 3;
+                            break;
+                        case 5:
+                            outState = 6;
+                            rx_tempString[outByte] += tempBit << 2;
+                            break;
+                        case 6:
+                            outState = 7;
+                            rx_tempString[outByte] += tempBit << 1;
+                            break;
+                        case 7:
+                            outState = 0;
+                            rx_tempString[outByte] += tempBit;
+                            ++outByte;
+                            // exceeded tc length discard
+                            if(outByte > 135){
+                                ++rx_incorrectSizedTCcount;
+                                outByte = 0;
+                            }
+                            ++byteCount;
+                            break;
+                    }
+                }
+            }
+        }
+        COM_RX_DATA_NODE *temp = data_node->next_node;
+        delete data_node;
+        data_node = temp;
+    }
+    if( (!flush_called) && (gTOTAL_INCORRECT_SIZE_TC == 0) ){
+        ++gTOTAL_INCORRECT_SIZE_TC;
+    }
+    gRX_HEAD_DATA_NODE = new COM_RX_DATA_NODE;
+    gRX_HEAD_DATA_NODE->next_node = NULL;
+    gRX_CURRENT_DATA_NODE = gRX_HEAD_DATA_NODE;
+    gRX_CURRENT_PTR = gRX_CURRENT_DATA_NODE->values;
+    
+    if( gOBOSC_HEAD != NULL ){
+        gOBOSC_HEAD = gOBOSC_HEAD->next_TC;
+        if( gOBOSC_HEAD != NULL ){
+            ++gOBOSC_PSC;
+        }
+    }
+    
+    // PENDING: SORT THE LINKED LIST ACCORDING TO PSC VALUE
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/COM_SND_TM_functions.h	Tue Dec 01 10:56:10 2015 +0000
@@ -0,0 +1,1186 @@
+// ********************************** COMPRESSION **********************************
+int disk_write(const uint8_t *, uint64_t);
+uint64_t  RTC_TIME;                                    //need to  be changed to uint_64
+unsigned char SDcard_lastWritten[512]  = {0};
+ 
+namespace Science_TMframe {
+    
+    #define OUTLENGTH 360                                           //length of the output frame after convolution
+    #define SDcard_block 512                                        //block size of the sd card
+    
+    Convolution ConvObj;                                            //object which stores the frame after convolution
+    bool fresh[3] = {true,true,true};                               // True only for the first time                                                                 
+    unsigned char frames[3][134] = {0};                             // "frame" stores the address of the current frame...."first_frame_address" stores the address of the first node made.
+    unsigned int FCN[4] = {0};                                      //frame count number
+    unsigned int data_starting_point[3] = {8,5,10};             
+    unsigned int max_data[3] = {124,127,122};                       //number of bytes in each frame excluding TMID,FCN,first_header_point,crc
+    unsigned char TM_convoluted_data[270] = {0};                    //270 bytes is the size after convolution of 1072 bits
+    unsigned char complete_frame[SDcard_block] = {0};
+    uint64_t  SDC_address = 200;
+    bool SCH_FCCH_FLAG = true;
+    
+    
+    void add_SCH_FCCH(){
+        int i = 0;
+        complete_frame[0] = 0x0a;
+        complete_frame[1] = 0x3f;;
+        complete_frame[2] = 0x46;
+        complete_frame[3] = 0xb4;
+        complete_frame[4] = 0x00;
+        
+        for(i = 149 ; i < 159 ; i ++){
+            complete_frame[i] = 0 ; 
+        }
+        
+        complete_frame[159] = 0x0a;
+        complete_frame[160] = 0x3f;;
+        complete_frame[161] = 0x46;
+        complete_frame[162] = 0xb4;
+        complete_frame[163] = 0x00;
+        
+        for(i = 308 ; i < 318 ; i ++){
+            complete_frame[i] = 0 ; 
+        }
+        
+    }
+    
+    void making_frameHeader(unsigned char  TMID){
+    
+        unsigned char frame_type_identifier = 0;                                                        // not conform about the values , yet to be done    
+        frames[TMID][0] = (frame_type_identifier<<7) + ( (TMID + 1)<<3 ) + ( (FCN[TMID]>>24) & 0x7 );   //frame number should be less than 2^23 since 23 bits are assigned for that
+        frames[TMID][1] = ((FCN[TMID]>>16) & 0xff );
+        frames[TMID][2] = ( (FCN[TMID]>>8 )& 0xff );
+        frames[TMID][3] = ( FCN[TMID] & 0xff      );                                                        // first bit for (frame identifier), next 4 for (TMID) and next 27 for FCN
+        
+        if(TMID == 0){
+            frames[TMID][5] =( (RTC_TIME>>29) & 0xff );
+            frames[TMID][6] =( (RTC_TIME>>21) & 0xff ); 
+            frames[TMID][7] =( (RTC_TIME>>13) & 0xff );
+            
+        }else if(TMID == 2){
+            frames[TMID][5] =( (RTC_TIME>>32) & 0xff );
+            frames[TMID][6] =( (RTC_TIME>>24) & 0xff ); 
+            frames[TMID][7] =( (RTC_TIME>>16) & 0xff );
+            frames[TMID][8] =( (RTC_TIME>>8 ) & 0xff );
+            frames[TMID][9] =( (RTC_TIME    ) & 0xff );
+        }
+        
+    }
+ 
+    void convolution (unsigned char * ptr){
+    
+        ConvObj.convolutionEncode(ptr , TM_convoluted_data);
+        ConvObj.convolutionEncode(ptr + 67, TM_convoluted_data + 135);
+ 
+    }
+ 
+    /*
+        @brief : take the address of array of LCR or HCR and stores it into a frame
+        @parameters: type->L or H , deprnding on wheather it is LCR or HCR respectively
+        @return: nothing
+    */
+ 
+//  type 2 yet to be done
+    void making_frame(unsigned char TMID ,unsigned char type, unsigned char* pointer){
+        
+        TMID--;                                          //TMID goes from 1 to 3 , convinient to ue from 0 to 2
+        static int frame_space_number[3] = {0};          //this variable represents the register number of the frame in which LCR or HCR data to be written not including header
+        int  packet_len = 0;
+        int copy_count = 0 ;
+        
+        switch(int(TMID)){
+            case 0:                                     //SCP
+                if(type == 'L'){                        //below threshold
+                    packet_len = 22;   
+                }
+                else if(type == 'H'){                   //above threshold
+                    packet_len = 26;
+                }
+                break;
+            
+            case 1:                                     //SFP above threshold
+                packet_len = 35;
+                break;
+        
+            case 2:                                     //SFP  below threshold
+                packet_len = 23;
+                break;  
+        }
+        
+        if(SCH_FCCH_FLAG){
+            add_SCH_FCCH();
+            SCH_FCCH_FLAG = false;
+        }
+ 
+        if(fresh[TMID]){
+            //welcome to first frame
+            making_frameHeader(TMID);
+            frames[TMID][4] = 0;
+            fresh[TMID] = false;
+        }
+    
+    
+        while(copy_count < packet_len){                                                                  // 22 bytes is the size of the LCR
+            frames[TMID][ frame_space_number[TMID] + data_starting_point[TMID] ]= *(pointer + copy_count);
+            frame_space_number[TMID]++;                                          
+            copy_count++;
+            if( frame_space_number[TMID] == max_data[TMID] ){                                //frame space number can go from 0 to 126 as data is written from 0+5 to 126+5
+                FCN[TMID]++;
+                // convolution and save frame in the sd card
+                
+                // copying crc in 132 and 133
+                int temp_crc;
+                temp_crc = CRC::crc16_gen(frames[TMID],132);
+                frames[TMID][132] = temp_crc>>8;
+                frames[TMID][133] = temp_crc & 0xff;                
+                // xor 
+                for(int j = 0 ; j < 134 ; j++){
+ //                   frames[TMID][j] = frames[TMID][j]^exorThisWithTMFrame[j];
+                }
+                //convolution and interleaving  
+                convolution(frames[TMID]);
+                interleave(TM_convoluted_data , complete_frame + 5);
+                interleave(TM_convoluted_data+ 135,complete_frame + 164);
+                
+                // writing the SDC_address in a buffer , to store it in SDcard at address 5
+                SDcard_lastWritten[0] = SDC_address>>56;
+                SDcard_lastWritten[1] = (SDC_address>>48)&0xFF;
+                SDcard_lastWritten[2] = (SDC_address>>40)&0xFF;
+                SDcard_lastWritten[3] = (SDC_address>>32)&0xFF;
+                SDcard_lastWritten[4] = (SDC_address>>24)&0xFF;
+                SDcard_lastWritten[5] = (SDC_address>>16)&0xFF;
+                SDcard_lastWritten[6] = (SDC_address>>8)&0xFF;
+                SDcard_lastWritten[7] = (SDC_address)&0xFF;
+                
+                SPI_mutex.lock();
+                disk_write(complete_frame , SDC_address);
+                SPI_mutex.unlock();
+                SDC_address++;
+               
+            
+                
+                //now save to the sd card TM_convoluted_data
+//              std::bitset<8> b;
+//              printf("\nthis is frame %d\n",TMID);                                        //for printing frame 
+//                  for(int j =0; j<134;j++){
+//                  printf(" %d",frames[TMID][j]);
+////                    b =  frames[TMID][j];
+////                    cout<<b;
+//                  }
+                
+                frame_space_number[TMID] = 0;
+                making_frameHeader(TMID);
+                frames[TMID][4]=packet_len - copy_count;
+                //write time  here also
+                continue;
+            }       
+        }
+    
+//      printf("\nthis is frame %d\n",TMID);                                              //for printing frame 
+//      for(int j =0; j<134;j++){
+//          printf(" %d",frames[TMID][j]);
+//      }
+ 
+    }   
+ 
+ 
+}
+ 
+ 
+ 
+// *************************************************** COMPRESSION *************************************************** 
+ 
+namespace Science_Data_Compression{
+ 
+    # define PACKET_SEQUENCE_COUNT 1                         //1 byte
+    # define NUM_PROTON_BIN 17                               //2 byte each
+    # define NUM_ELECTRON_BIN 14                             //2 byte each 
+    # define VETO 1                                          //2 byte
+    # define FASTCHAIN 2                                     //4 byte each
+    #define RAW_PACKET_LENGTH 73                             //73 bytes
+ 
+//  #define PACKET_SEQ_COUNT 1
+//  #define PROTON_BIN_SIZE 2
+//  #define ELECTRON_BIN_SIZE 2
+//  #define VETO 2
+//  #define FAST_CHAIN 4
+    
+    
+    /*
+    @brief:     read one uint16_t equivalent of first two chars from the stream. short int because 16 bits
+    @param:     pointer to the start of the short int
+    @return:    uint16_t 
+    */
+    
+    
+    unsigned int read_2byte(unsigned char* ptr){
+        unsigned int output = (unsigned int) *(ptr+1);
+        output += ( (unsigned int)(*ptr) ) << 8;
+        return output;
+    }
+    
+    /*
+    @brief:     read one int equivalent of first four chars from the stream.  int because 32 bits
+    @param:     pointer to the start of the short int
+    @return:    unsigned int  
+    */
+ 
+    unsigned int read_4byte(unsigned char* ptr){
+        unsigned int output = (unsigned int) *(ptr+3);
+        output += (unsigned int)*(ptr+2)<<8;
+        output += (unsigned int)*(ptr+1)<<16;
+        output += (unsigned int)*(ptr)<<24;
+        return output;
+    }
+ 
+    unsigned int SFP_thresholds[35]={0  ,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100};//threashold values 
+    unsigned int SCP_thresholds[35]={0  ,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,0  ,0  ,0  };
+    unsigned int SFP_bin[35];
+    unsigned int SCP_bin[35]={0};
+    unsigned char SFP_outputBT[23];                         //BT = below threshold
+    unsigned char SFP_outputAT[35];
+    unsigned char SCP_outputLCR[22];
+    unsigned char SCP_outputHCR[26];
+ 
+    
+//**********************************************************************************************************************************************************************
+    //lots of compression functions are listed below
+ 
+    
+    unsigned char SFP_compress4_BT(unsigned int input){                                 //for veto
+        int de_4 = 0;
+        unsigned char output;
+        
+        if(input<= 3){
+        //  DE = 0;
+            output = 0x0;
+            de_4 = 0;
+        }
+        else if(input <= 12){
+        //  DE = 01;
+            output = 0x1;
+            de_4 = 2;   
+        }
+        else if(input <= 48){
+        //  DE = 10
+            output = 0x2;
+            de_4 = 4;   
+        }
+        else {
+        //  DE = 11
+            output = 0x3;
+            de_4 = 6;
+        }
+        
+        unsigned short int temp = input >> de_4;
+        output += (temp ) << 2;
+        return output;
+    }
+    
+    
+    unsigned char SFP_compress5_BT(unsigned int input){
+        int de_4 = 0;                                   //number by which bin value need to be shifted
+        unsigned char output;
+        
+        if(input <= 15){
+//            D = 0 [0]
+            output = 0x0;
+            de_4 = 0;
+        }
+        else if(input <= 60){
+//            D = 1
+            output = 0x1;
+            de_4 = 2;
+        }
+        
+        unsigned short int temp = input >> de_4;
+        output += (temp ) << 1;
+        
+        return output;      
+    };
+    
+    
+    unsigned char SFP_compress6_BT(unsigned int input){
+        int de_4 = 0;;
+        unsigned char output;
+        
+        if(input <= 31){
+//          E = 0 [0]
+            output = 0x0;
+            de_4 = 0;
+        }
+        else if(input <= 124){
+//            E = 1
+            output = 0x1;
+            de_4 = 2;
+        }
+         
+        
+        unsigned short int temp = input >> de_4;
+        output += (temp ) << 1;
+        
+        return output;
+    };
+    
+    unsigned char SFP_compress7_AT(unsigned int input){
+        int de_4  = 0;
+        unsigned char output;
+        
+        if(input <= 31){
+//            DE = 00 [0]
+            output = 0x0;
+            de_4 = 0;
+        }
+        else if(input <= 124){
+//            DE = 01 [1]
+            output = 0x1;
+            de_4 = 2;
+        }
+        
+        else if(input <= 496){
+//            DE = 10 [2]
+            output = 0x2;
+            de_4 = 4;
+        }
+        
+        else if(input <= 1984){
+//           DE = 11 [3]
+            output = 0x3;
+            de_4 = 6;
+        }
+        
+        unsigned short int temp = input >> de_4;
+        output += (temp ) << 2;
+        
+        return output;
+        
+    };
+    
+    
+    unsigned char SFP_compress8_AT(unsigned int input){
+        
+        int de_4 = 0;;
+        unsigned char output;
+        
+        if(input <= 63){
+//            DE = 00 [0]
+            output = 0x0;
+            de_4 = 0;
+        }
+        else if(input <= 252){
+//            DE = 01 [1]
+            output = 0x1;
+            de_4 = 2;
+        }
+        
+        else if(input <= 1008){
+//            DE = 10 [2]
+            output = 0x2;
+            de_4 = 4;
+        }
+        
+        else {
+//           DE = 11 [3]
+            output = 0x3;
+            de_4 = 6;
+        }
+        
+        unsigned short int temp = input >> de_4;
+        output += (temp ) << 2;
+        
+        return output;
+    };
+    
+    unsigned char SFP_compress5_AT(unsigned int input){
+        int de_4 = 0;;
+        unsigned char output;
+        
+        if(input <= 3){
+//            DE = 000 [0]
+            output = 0x0;
+            de_4 = 0;
+        }
+        else if(input <= 12){
+//            DE = 001 [1]
+            output = 0x1;
+            de_4 = 2;
+        }
+        
+        else if(input <= 48){
+//            DE = 010 [2]
+            output = 0x2;
+            de_4 = 4;
+        }
+        
+        else if(input <= 192) {
+//           DE = 011 [3]
+            output = 0x3;
+            de_4 = 6;
+        }
+        
+        else if(input <= 768) {
+//           DE = 100 [4]
+            output = 0x4;
+            de_4 = 8;
+        }
+        
+        else if(input <= 3072) {
+//           DE = 101 [5]
+            output = 0x5;
+            de_4 = 10;
+        }
+        
+        else if(input <= 12288) {
+//           DE = 110 [6]
+            output = 0x6;
+            de_4 = 12;
+        }
+        
+        else  {
+//          DE = 111 [7]
+            output = 0x7;
+            de_4 = 14;
+        }
+           
+        unsigned short int temp = input >> de_4;
+        output += (temp ) << 3;
+        
+        return output;
+    }
+    
+    unsigned char SFP_compress7FC_AT(unsigned int input){                   // for fast chain above threshold
+        int de_4 = 0;;
+        unsigned char output;
+        
+        if(input <= 15){
+//            DE = 000 [0]
+            output = 0x0;
+            de_4 = 0;
+        }
+        else if(input <= 60){
+//            DE = 001 [1]
+            output = 0x1;
+            de_4 = 2;
+        }
+        
+        else if(input <= 240){
+//            DE = 010 [2]
+            output = 0x2;
+            de_4 = 4;
+        }
+        
+        else if(input <= 960) {
+//           DE = 011 [3]
+            output = 0x3;
+            de_4 = 6;
+        }
+        
+        else if(input <= 3840) {
+//           DE = 100 [4]
+            output = 0x4;
+            de_4 = 8;
+        }
+        
+        else if(input <= 15360) {
+//           DE = 101 [5]
+            output = 0x5;
+            de_4 = 10;
+        }
+        
+        else if(input <= 61440) {
+//           DE = 110 [6]
+            output = 0x6;
+            de_4 = 12;
+        }
+        
+        else  {
+//          DE = 111 [7]
+            output = 0x7;
+            de_4 = 14;
+        }
+           
+        unsigned short int temp = input >> de_4;
+        output += (temp ) << 3;
+        
+        return output;
+    }
+    
+    
+    
+    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+    unsigned char SCP_compress6(unsigned int input){
+        int ef_4;
+        unsigned char output;
+        
+        if(input <= 15){
+//      EF = 00
+        output = 0x0;
+        ef_4 = 0;
+        }
+        
+        else if(input <= 60 ){
+//          EF = 01 [1]
+            output = 0x01;
+            ef_4 = 2;
+        }
+        else if(input <= 240){
+//          EF = 10 [2]
+            output = 0x02;
+            ef_4 = 4;
+        }
+        else{
+//          EF = 11 [3]
+            output = 0x03;
+            ef_4 = 6;
+        }
+            
+            unsigned short int temp = input >> ef_4;
+            output += (temp & 0xf) << 2;
+            
+            return output;
+    }
+    
+    unsigned char SCP_compress5(unsigned int input){
+        
+        int de_4 = 0;;
+        unsigned char output;
+        
+        if(input <= 7){
+//            DE = 00 [0]
+            output = 0x0;
+            de_4 = 0;
+        }
+        
+        else if(input <= 28){
+//            DE = 01 [1]
+            output = 0x01;
+            de_4 = 2;
+        }
+        else if(input <= 112){
+//            DE = 10 [2]
+            output = 0x02;
+            de_4 = 4;
+        }
+        else{
+//        DE = 11 [3]
+            output = 0x03;
+            de_4 = 6;
+        }
+        
+        unsigned short int temp = input >> de_4;
+        output += (temp & 0x7) << 2;
+        
+        return output;
+    }
+    
+        unsigned char SCP_compress6h(unsigned int input) {
+    
+        int ef_4;
+        unsigned char output;
+        
+        if(input <=7){
+//          EF = 000 [0]
+            output = 0x00;
+            ef_4 = 0;
+        }
+        
+        else if(input <=28){
+//          EF = 001 [1]
+            output = 0x01;
+            ef_4 = 2;
+        }
+        else if(input <= 112){
+        
+//          EF = 010 [2]
+            output = 0x02;
+            ef_4 = 4;
+        }
+        else if(input <= 448){
+//          EF = 011 [3]
+            output = 0x03;
+            ef_4 = 6;
+        }
+        else if(input <= 1792){
+//          EF = 100 [4]
+            output = 0x04;
+            ef_4 = 8;
+            
+        }
+        else if(input <= 7168){
+//          EF = 101 [5]
+            output = 0x05;
+            ef_4 = 10;
+            
+        }
+        else if(input <= 28672){
+//          EF = 110 [6]
+            output = 0x06;
+            ef_4 = 12;
+        }
+        else{
+//          EF = 111 [7]
+            output = 0x07;
+            ef_4 =14;
+        }
+        
+        unsigned short int temp = input >> ef_4;
+        output += (temp & 0x7) << 3;
+        
+        return output;
+        
+    }
+    
+    
+    unsigned char SCP_compress7h(unsigned int input) {
+    
+        int fg_4;
+        unsigned char output;
+        
+        if(input <= 15){
+//          EF = 000 [0]
+            output = 0x0;
+            fg_4 = 0;
+        }
+        
+        else if(input <= 60){
+//          EF = 001 [1]
+            output = 0x01;
+            fg_4 = 2;
+        }
+        else if(input <= 240){
+        
+//          EF = 010 [2]                    
+            output = 0x02;
+            fg_4 = 4;
+        }
+        else if(input <= 960){
+//          EF = 011 [3]
+            output = 0x03;
+            fg_4 = 6;
+        }
+        else if(input <= 3840){
+//          EF = 100 [4]
+            output = 0x04;
+            fg_4 = 8;
+            
+        }
+        else if(input <= 15360){
+//          EF = 101 [5]
+            output = 0x05;
+            fg_4 = 10;
+            
+        }
+        else if(input <= 61440){
+//          EF = 110 [6]
+            output = 0x06;
+            fg_4 = 12;
+        }
+        else{
+//          EF = 111 [7]
+            output = 0x07;
+            fg_4 =14;
+        }
+        
+        unsigned short int temp = input >> fg_4;
+        output += (temp & 0xf) << 3;
+        
+        return output;
+        
+    }
+    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+    void SCP_compress_data();                   
+    void SFP_compress_data(unsigned char* input){
+ 
+            bool LCR = true;
+            int i = 0;
+            static int packet_no = 0;                       //takes value from 0 to 29
+            //TRAVERSE THE LIST TO DETERMINE LCR OR HCR and stroing the values in proton_bin and electron bin
+            SFP_bin[0] = *input;
+            for(i=1 ; i<= NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO ; ++i){          //storing the bin values into an array name bin
+                SFP_bin[i]=read_2byte(input+1+((i-1)<<1));                             //proton bin and elecron bin are 2 byte, hence read_2byte and 
+                SCP_bin[i]+=SFP_bin[i];
+                if(SFP_bin[i] > SFP_thresholds[i]){                                         //fast cahin is 4 byte hence read_4byte
+                    LCR = false;                                                    // if a single value is above threshold then lcr becomes false
+                    i++;
+                    break;
+                }
+            }
+            
+            for( ; i<= NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO ; ++i){
+                SCP_bin[i]+=SFP_bin[i];
+                SFP_bin[i] = read_2byte(input + 1 + ( (i-1)<<1) );
+            }
+            
+            SFP_bin[i] = read_4byte(input+1+ ((i-1)<<1))    ;
+            SCP_bin[i]+=SFP_bin[i];
+            
+            if(SFP_bin[i]>SFP_thresholds[i])
+                LCR = false;                                                    //since veto starts from location (input + 65) and (input + 69)
+                    
+            SFP_bin[i+1] = read_4byte(input+69);
+            SCP_bin[i]+=SFP_bin[i];
+            
+            if(SFP_bin[i]>SFP_thresholds[i])
+                LCR = false;
+            
+            
+//          printf("\n");                       //for printing the sfp bin
+//          for (i=0;i<35;i++){
+//              printf("sfp[%d] = %d",i,SFP_bin[i]);
+//          }
+//          printf("\n");
+ 
+            if(LCR){   
+                
+                 SFP_outputBT[0]  =  (packet_no<<3) + ( SFP_compress5_BT(SFP_bin[1])>>2 );
+                 SFP_outputBT[1]  =  ( SFP_compress5_BT(SFP_bin[1])<<6 ) + ( SFP_compress5_BT(SFP_bin[2])<<1 ) +   ( SFP_compress5_BT(SFP_bin[3])>>4 );
+                 SFP_outputBT[2]  =  ( SFP_compress5_BT(SFP_bin[3])<<4 ) + ( SFP_compress5_BT(SFP_bin[4])>>1 );
+                 SFP_outputBT[3]  =  ( SFP_compress5_BT(SFP_bin[4])<<7 ) + ( SFP_compress5_BT(SFP_bin[5])<<2 ) + ( SFP_compress5_BT(SFP_bin[6])>>3 );            
+                 SFP_outputBT[4]  =  ( SFP_compress5_BT(SFP_bin[6])<<5 ) + ( SFP_compress5_BT(SFP_bin[7]) );
+                 SFP_outputBT[5]  =  ( SFP_compress5_BT(SFP_bin[8])<<3 ) + ( SFP_compress5_BT(SFP_bin[9])>>2 );
+                 SFP_outputBT[6]  =  ( SFP_compress5_BT(SFP_bin[9])<<6 ) + ( SFP_compress5_BT(SFP_bin[10])<<1 ) +  ( SFP_compress5_BT(SFP_bin[11])>>4 );
+                 SFP_outputBT[7]  =  ( SFP_compress5_BT(SFP_bin[11])<<4 ) + ( SFP_compress5_BT(SFP_bin[12])>>1 );            
+                 SFP_outputBT[8]  =  ( SFP_compress5_BT(SFP_bin[12])<<7 ) + ( SFP_compress5_BT(SFP_bin[13])<<2 ) + ( SFP_compress5_BT(SFP_bin[14])>>3 );
+                 SFP_outputBT[9]  =  ( SFP_compress5_BT(SFP_bin[14])<<5 ) + ( SFP_compress5_BT(SFP_bin[15]) );
+                 SFP_outputBT[10] =  ( SFP_compress5_BT(SFP_bin[16])<<3 ) + ( SFP_compress5_BT(SFP_bin[17])>>2 );
+                 SFP_outputBT[11] =  ( SFP_compress5_BT(SFP_bin[17])<<6 ) + ( SFP_compress6_BT(SFP_bin[18]) );
+                 SFP_outputBT[12] =  ( SFP_compress6_BT(SFP_bin[19])<<2 ) + ( SFP_compress6_BT(SFP_bin[20])>>4 );
+                 SFP_outputBT[13] =  ( SFP_compress6_BT(SFP_bin[20])<<4 ) + ( SFP_compress5_BT(SFP_bin[21])>>1 );        
+                 SFP_outputBT[14] =  ( SFP_compress5_BT(SFP_bin[21])<<7 ) + ( SFP_compress5_BT(SFP_bin[22])<<2 )  + ( SFP_compress5_BT(SFP_bin[23])>>3 );
+                 SFP_outputBT[15] =  ( SFP_compress5_BT(SFP_bin[23])<<5 ) + ( SFP_compress5_BT(SFP_bin[24]) );
+                 SFP_outputBT[16] =  ( SFP_compress5_BT(SFP_bin[25])<<3 ) + ( SFP_compress5_BT(SFP_bin[26])>>2 );
+                 SFP_outputBT[17] =  ( SFP_compress5_BT(SFP_bin[26])<<6 ) + ( SFP_compress5_BT(SFP_bin[27])<<1 ) + ( SFP_compress5_BT(SFP_bin[28])>>4);
+                 SFP_outputBT[18] =  ( SFP_compress5_BT(SFP_bin[28])<<4 ) + ( SFP_compress5_BT(SFP_bin[29])>>1) ;
+                 SFP_outputBT[19] =  ( SFP_compress5_BT(SFP_bin[29])<<7 ) + ( SFP_compress5_BT(SFP_bin[30])<<2 ) + ( SFP_compress5_BT(SFP_bin[31])>>3)  ; 
+                 SFP_outputBT[20] =  ( SFP_compress5_BT(SFP_bin[31])<<5 ) + ( SFP_compress4_BT(SFP_bin[32])<<1)  + ( SCP_compress5(SFP_bin[33])>>4 ) ; //
+                 SFP_outputBT[21] =  ( SCP_compress5(SFP_bin[33])<<4 ) + ( SCP_compress5(SFP_bin[34])>>1 );        //here intentionally SCP_compress is used instead of SCP_compress5 is different than SFP_compress5
+                 SFP_outputBT[22] =  ( SCP_compress5(SFP_bin[34])<<7 );                                             //7 bits are spare
+                 
+                 Science_TMframe::making_frame(3,'L',SFP_outputBT);
+                 if(++packet_no == 30){
+                    packet_no=0;
+                    SCP_compress_data();
+                    for(i=0;i<PACKET_SEQUENCE_COUNT + NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO + FASTCHAIN;i++){
+                        if(packet_no==0){
+                            SCP_bin[i]=0;
+                        }
+                    }
+                 }
+                 
+                 
+            }
+ 
+            else {
+                
+                
+                SFP_outputAT[0]  = (RTC_TIME>>27)&(0xff);
+                SFP_outputAT[1]  = (RTC_TIME>>19)&(0xff);
+                SFP_outputAT[2]  = (RTC_TIME>>11)&(0xff);
+                SFP_outputAT[3]  = (RTC_TIME>>3 )&(0xff);
+                SFP_outputAT[4]  = (RTC_TIME<<5 )&(0xff) + (packet_no);
+                SFP_outputAT[5]  = ( SFP_compress7_AT(SFP_bin[1])<<1 ) + ( SFP_compress7_AT(SFP_bin[2])>>6 );
+                SFP_outputAT[6]  = ( SFP_compress7_AT(SFP_bin[2])<<2 ) + ( SFP_compress7_AT(SFP_bin[3])>>5 );
+                SFP_outputAT[7]  = ( SFP_compress7_AT(SFP_bin[3])<<3 ) + ( SFP_compress7_AT(SFP_bin[4])>>4 );
+                SFP_outputAT[8]  = ( SFP_compress7_AT(SFP_bin[4])<<4 ) + ( SFP_compress7_AT(SFP_bin[5])>>3 );
+                SFP_outputAT[9]  = ( SFP_compress7_AT(SFP_bin[5])<<5 ) + ( SFP_compress7_AT(SFP_bin[6])>>2 );
+                SFP_outputAT[10] = ( SFP_compress7_AT(SFP_bin[6])<<6 ) + ( SFP_compress7_AT(SFP_bin[7])>>1 );
+                SFP_outputAT[11] = ( SFP_compress7_AT(SFP_bin[7])<<7 ) + ( SFP_compress7_AT(SFP_bin[8]) );
+                SFP_outputAT[12] = ( SFP_compress7_AT(SFP_bin[9])<<1 ) + ( SFP_compress7_AT(SFP_bin[10])>>6);
+                SFP_outputAT[13] = ( SFP_compress7_AT(SFP_bin[10])<<2 ) + ( SFP_compress7_AT(SFP_bin[11])>>5);
+                SFP_outputAT[14] = ( SFP_compress7_AT(SFP_bin[11])<<3 ) + ( SFP_compress7_AT(SFP_bin[12])>>4);
+                SFP_outputAT[15] = ( SFP_compress7_AT(SFP_bin[12])<<4 ) + ( SFP_compress7_AT(SFP_bin[13])>>3);
+                SFP_outputAT[16] = ( SFP_compress7_AT(SFP_bin[13])<<5 ) + ( SFP_compress7_AT(SFP_bin[14])>>2);
+                SFP_outputAT[17] = ( SFP_compress7_AT(SFP_bin[14])<<6 ) + ( SFP_compress7_AT(SFP_bin[15])>>1);
+                SFP_outputAT[18] = ( SFP_compress7_AT(SFP_bin[15])<<7 ) + ( SFP_compress7_AT(SFP_bin[16]));
+                SFP_outputAT[19] = ( SFP_compress7_AT(SFP_bin[17])<<1 ) + ( SFP_compress8_AT(SFP_bin[18])>>7 );
+                SFP_outputAT[20] = ( SFP_compress8_AT(SFP_bin[18])<<1 ) + ( SFP_compress8_AT(SFP_bin[19])>>7 );
+                SFP_outputAT[21] = ( SFP_compress8_AT(SFP_bin[19])<<1 ) + ( SFP_compress8_AT(SFP_bin[20])>>7 );
+                SFP_outputAT[22] = ( SFP_compress8_AT(SFP_bin[20])<<1 ) + ( SFP_compress7_AT(SFP_bin[21])>>6 );
+                SFP_outputAT[23] = ( SFP_compress7_AT(SFP_bin[21])<<2 ) + ( SFP_compress7_AT(SFP_bin[22])>>5 );
+                SFP_outputAT[24] = ( SFP_compress7_AT(SFP_bin[22])<<3 ) + ( SFP_compress7_AT(SFP_bin[23])>>4 );
+                SFP_outputAT[25] = ( SFP_compress7_AT(SFP_bin[23])<<4 ) + ( SFP_compress7_AT(SFP_bin[24])>>3 );
+                SFP_outputAT[26] = ( SFP_compress7_AT(SFP_bin[24])<<5 ) + ( SFP_compress7_AT(SFP_bin[25])>>2 );
+                SFP_outputAT[27] = ( SFP_compress7_AT(SFP_bin[25])<<6 ) + ( SFP_compress7_AT(SFP_bin[26])>>1 );
+                SFP_outputAT[28] = ( SFP_compress7_AT(SFP_bin[26])<<7 ) + ( SFP_compress7_AT(SFP_bin[27]) );
+                SFP_outputAT[29] = ( SFP_compress7_AT(SFP_bin[28])<<1 ) + ( SFP_compress7_AT(SFP_bin[29])>>6);
+                SFP_outputAT[30] = ( SFP_compress7_AT(SFP_bin[29])<<2 ) + ( SFP_compress7_AT(SFP_bin[30])>>5);
+                SFP_outputAT[31] = ( SFP_compress7_AT(SFP_bin[30])<<3 ) +( SFP_compress7_AT(SFP_bin[31])>>4);  
+                SFP_outputAT[32] = ( SFP_compress7_AT(SFP_bin[31])<<4 ) +( SFP_compress5_AT(SFP_bin[32])>>1); 
+                SFP_outputAT[33] = ( SFP_compress5_AT(SFP_bin[32])<<7 ) +( SFP_compress7FC_AT(SFP_bin[33])); 
+                SFP_outputAT[34] =  ( SFP_compress7FC_AT(SFP_bin[34])<<1 );                                             // 1 bit is spare          
+                
+                Science_TMframe::making_frame(2,'H',SFP_outputAT);
+                if(++packet_no == 30){
+                    packet_no=0;
+                    SCP_compress_data();
+                    for(i=0;i<PACKET_SEQUENCE_COUNT + NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO + FASTCHAIN;i++){
+                        if(packet_no==0){
+                            SCP_bin[i]=0;
+                        }
+                    }
+                }
+                            
+            }
+ 
+    }
+ 
+ 
+/*
+    brief: takes the  pointer of the raw data string and return the address of the array which stores the address of 30 packets.
+    input: pointer to the raw data.
+    output : void
+*/
+ 
+    void complete_compression(unsigned char *SRP,uint64_t x){
+        RTC_TIME = x;
+        int i;                  //30 times because 3 second data
+    
+        for(i=0;i<30;i++){
+         SFP_compress_data(SRP + 73*i); 
+        }
+ 
+    }
+ 
+ 
+ 
+/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+ 
+    
+    
+    /*
+    @brief:     compresses the given input stream and return output packet
+    @param:     pointer to input stream. Input stream always has the fixed size of RAW_PACKET_LENGTH
+    @return:    pointer to output stream. Output stream has the size of 22 or 26 bytes
+    */
+    void SCP_compress_data(){
+        
+        bool LCR = true;
+        int i = 0;
+        
+        for(i=1;i<=PACKET_SEQUENCE_COUNT + NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO + FASTCHAIN ;i++){
+            if(SCP_bin[i]>SCP_thresholds[i]){
+            LCR = false;
+            break;
+            }
+        }
+//      printf("\n");                    //for printing the scp bin
+//          for (i=0;i<35;i++){
+//              printf(" scp[%d] = %d    ",i,SCP_bin[i]);
+//          }
+//          printf("\n");         
+        // compressing the data
+        if(LCR){   
+             SCP_outputLCR[0] = (RTC_TIME>>5)&(0xff);                //first 13 bits for time tagging
+             SCP_outputLCR[1] = (RTC_TIME<<3)&(0xff);                //then 4 bits for attitude tag
+             SCP_outputLCR[2] = 0x00;                                //only attitude tag is left
+             SCP_outputLCR[2] += ( SCP_compress5(SCP_bin[0])<<1 ) + ( SCP_compress5(SCP_bin[1])>>4 );
+             SCP_outputLCR[3] = ( SCP_compress5(SCP_bin[1])<<4 ) + ( SCP_compress5(SCP_bin[2])>>1 );
+             SCP_outputLCR[4] = ( SCP_compress5(SCP_bin[2])<<7 ) + ( SCP_compress5(SCP_bin[3])<<2 ) + ( SCP_compress5(SCP_bin[4])>>3 );
+             SCP_outputLCR[5] = ( SCP_compress5(SCP_bin[4])<<5 ) + ( SCP_compress5(SCP_bin[5]) );            
+             SCP_outputLCR[6] = ( SCP_compress5(SCP_bin[6])<<3 ) + ( SCP_compress5(SCP_bin[7])>>2 );
+             SCP_outputLCR[7] = ( SCP_compress5(SCP_bin[7])<<6 ) + ( SCP_compress5(SCP_bin[8])<<1 ) +  ( SCP_compress5(SCP_bin[9])>>4 );
+             SCP_outputLCR[8] = ( SCP_compress5(SCP_bin[9])<<4 ) + ( SCP_compress5(SCP_bin[10])>>1 );
+             SCP_outputLCR[9] = ( SCP_compress5(SCP_bin[10])<<7 ) + ( SCP_compress5(SCP_bin[11])<<2) + ( SCP_compress5(SCP_bin[12])>>3 );            
+             SCP_outputLCR[10] = ( SCP_compress5(SCP_bin[12])<<5 ) + ( SCP_compress5(SCP_bin[13]) );
+             SCP_outputLCR[11] = ( SCP_compress5(SCP_bin[14])<<3 ) + ( SCP_compress5(SCP_bin[15])>>2 );
+             SCP_outputLCR[12] = ( SCP_compress5(SCP_bin[15])<<6 ) + ( SCP_compress5(SCP_bin[16])<<1) +  ( SCP_compress6(SCP_bin[17])>>5 );
+             SCP_outputLCR[13] = ( SCP_compress6(SCP_bin[17])<<3 ) + ( SCP_compress6(SCP_bin[18])>>3 );
+             SCP_outputLCR[14] = ( SCP_compress5(SCP_bin[18])<<5 ) + ( SCP_compress6(SCP_bin[19])>>1 );
+             SCP_outputLCR[15] = ( SCP_compress6(SCP_bin[19])<<7 ) + ( SCP_compress5(SCP_bin[20])<<2 ) + ( SCP_compress5(SCP_bin[21])>>3 );          
+             SCP_outputLCR[16] = ( SCP_compress5(SCP_bin[21])<<5 ) + ( SCP_compress5(SCP_bin[22]) );
+             SCP_outputLCR[17] = ( SCP_compress5(SCP_bin[23])<<3 ) + ( SCP_compress5(SCP_bin[24])>>2 );
+             SCP_outputLCR[18] = ( SCP_compress5(SCP_bin[24])<<6 ) + ( SCP_compress5(SCP_bin[25])<<1) +  ( SCP_compress5(SCP_bin[26])>>4 );
+             SCP_outputLCR[19] = ( SCP_compress5(SCP_bin[26])<<4 ) + ( SCP_compress5(SCP_bin[27])>>1 );
+             SCP_outputLCR[20] = ( SCP_compress5(SCP_bin[27])<<7 ) + ( SCP_compress5(SCP_bin[28])<<2 ) + ( SCP_compress5(SCP_bin[29])>>3 ); 
+             SCP_outputLCR[21] = ( SCP_compress5(SCP_bin[29])<<5 ) + ( SCP_compress5(SCP_bin[30]) );
+         
+             Science_TMframe::making_frame(1,'L',SCP_outputLCR);
+        }
+        else{
+            
+            SCP_outputLCR[0] = (RTC_TIME>>5)&(0xff);                //first 13 bits for time tagging
+            SCP_outputLCR[1] = (RTC_TIME<<3)&(0xff);
+            SCP_outputHCR[2] = 0x40;
+            SCP_outputHCR[2] += ( SCP_compress6h(SCP_bin[0])<<6 ) ;
+            SCP_outputHCR[3] = ( SCP_compress6h(SCP_bin[1])<<2 ) + ( SCP_compress6h(SCP_bin[2])>>4) ;
+            SCP_outputHCR[4] = ( SCP_compress6h(SCP_bin[2])<<4 ) + ( SCP_compress6h(SCP_bin[3])>>2) ;
+            SCP_outputHCR[5] = ( SCP_compress6h(SCP_bin[3])<<6 ) + ( SCP_compress6h(SCP_bin[4])) ;
+            SCP_outputHCR[6] = ( SCP_compress6h(SCP_bin[5])<<2 ) + ( SCP_compress6h(SCP_bin[6])>>4) ;
+            SCP_outputHCR[7] = ( SCP_compress6h(SCP_bin[6])<<4 ) + ( SCP_compress6h(SCP_bin[7])>>2) ;
+            SCP_outputHCR[8] = ( SCP_compress6h(SCP_bin[7])<<6 ) + ( SCP_compress6h(SCP_bin[8])) ;
+            SCP_outputHCR[9] = ( SCP_compress6h(SCP_bin[9])<<2 ) + ( SCP_compress6h(SCP_bin[10])>>4) ;
+            SCP_outputHCR[10] = ( SCP_compress6h(SCP_bin[10])<<4 ) + ( SCP_compress6h(SCP_bin[11])>>2);
+            SCP_outputHCR[11] = ( SCP_compress6h(SCP_bin[11])<<6 ) + ( SCP_compress6h(SCP_bin[12])) ;
+            SCP_outputHCR[12] = ( SCP_compress6h(SCP_bin[13])<<2 ) + ( SCP_compress6h(SCP_bin[14])>>4)  ;
+            SCP_outputHCR[13] = ( SCP_compress6h(SCP_bin[14])<<4 ) + ( SCP_compress6h(SCP_bin[15])>>2)  ;
+            SCP_outputHCR[14] = ( SCP_compress6h(SCP_bin[15])<<6 ) + ( SCP_compress6h(SCP_bin[16]))     ;
+            SCP_outputHCR[15] = ( SCP_compress7h(SCP_bin[17])<<1 ) + ( SCP_compress7h(SCP_bin[18])>>6)  ;
+            SCP_outputHCR[16] = ( SCP_compress7h(SCP_bin[18])<<2 ) + ( SCP_compress7h(SCP_bin[19])>>5)  ;
+            SCP_outputHCR[17] = ( SCP_compress7h(SCP_bin[19])<<3 ) + ( SCP_compress6h(SCP_bin[20])>>3)  ;
+            SCP_outputHCR[18] = ( SCP_compress6h(SCP_bin[20])<<5 ) + ( SCP_compress6h(SCP_bin[21])>>1)  ;
+            SCP_outputHCR[19] = ( SCP_compress6h(SCP_bin[21])<<7 ) + ( SCP_compress6h(SCP_bin[22])<<1) +  ( SCP_compress6h(SCP_bin[23])>>5) ;
+            SCP_outputHCR[20] = ( SCP_compress6h(SCP_bin[23])<<3 ) + ( SCP_compress6h(SCP_bin[24])>>3)  ;
+            SCP_outputHCR[21] = ( SCP_compress6h(SCP_bin[24])<<5 ) + ( SCP_compress6h(SCP_bin[25])>>1)  ;
+            SCP_outputHCR[22] = ( SCP_compress6h(SCP_bin[25])<<7 ) + ( SCP_compress6h(SCP_bin[26])<<1) +  ( SCP_compress6h(SCP_bin[27])>>5) ;
+            SCP_outputHCR[23] = ( SCP_compress6h(SCP_bin[27])<<3 ) + ( SCP_compress6h(SCP_bin[28])>>3)  ;
+            SCP_outputHCR[24] = ( SCP_compress6h(SCP_bin[28])<<5 ) + ( SCP_compress6h(SCP_bin[29])>>1)  ;
+            SCP_outputHCR[25] = ( SCP_compress6h(SCP_bin[29])<<7 ) + ( SCP_compress6h(SCP_bin[30])<<1)  ; //last bit is empty
+        
+        /*  for (i=0;i<26;i++){
+                printf("\nscp[%d] = %d",i,SCP_outputHCR[i]);
+            }*/
+            Science_TMframe::making_frame(1,'H',SCP_outputHCR);
+        }
+    }
+    
+}
+
+
+// ******************************************** INTERLEAVE **************************************
+    void interleave( unsigned char *input, unsigned char *output ){
+    
+    unsigned int outState = 0;
+    unsigned int outByte = 0;
+    
+    for( unsigned int i = 0 ; i < 36 ; ++i ){
+        for(unsigned int j = 0 ; j < 30 ; ++j){
+            unsigned int x = j*36+i;
+            unsigned char tempBit = ((input[x >> 3]) >> (7-(x % 8))) & 1;
+            switch(outState){
+                case 0:
+                    outState = 1;
+                    output[outByte] = tempBit << 7;
+                    break;
+                case 1:
+                    outState = 2;
+                    output[outByte] += tempBit << 6;
+                    break;
+                case 2:
+                    outState = 3;
+                    output[outByte] += tempBit << 5;
+                    break;
+                case 3:
+                    outState = 4;
+                    output[outByte] += tempBit << 4;
+                    break;
+                case 4:
+                    outState = 5;
+                    output[outByte] += tempBit << 3;
+                    break;
+                case 5:
+                    outState = 6;
+                    output[outByte] += tempBit << 2;
+                    break;
+                case 6:
+                    outState = 7;
+                    output[outByte] += tempBit << 1;
+                    break;
+                case 7:
+                    outState = 0;
+                    output[outByte] += tempBit;
+                    ++outByte;
+                    break;
+            }
+        }
+        for(unsigned int j = 0 ; j < 2 ; ++j){
+            switch(outState){
+                case 0:
+                    output[outByte] = 0;
+                    outState = 1;
+                    break;
+                case 1:
+                    outState = 2;
+                    break;
+                case 2:
+                    outState = 3;
+                    break;
+                case 3:
+                    outState = 4;
+                    break;
+                case 4:
+                    outState = 5;
+                    break;
+                case 5:
+                    outState = 6;
+                    break;
+                case 6:
+                    outState = 7;
+                    break;
+                case 7:
+                    outState = 0;
+                    ++outByte;
+                    break;
+            }
+        }
+    }
+}
+
+// CONVOLUTION
+class Convolution{
+    
+private:
+    typedef struct ConvNode{
+        //next state
+        //format : 8 bits mapped as [1]xxxx [0]xxxx
+        unsigned char nextState;
+        
+        //output
+        // format : 8 bits mapped as 
+        // **** xxxx{2} [1]-[0]   {1} [1]-[0]      i.e.   ****xxxx
+        // x = data, * = junk
+        unsigned char output;
+    }ConvNode;
+
+    ConvNode convStateList[16]; 
+    
+public:
+    /*
+    @brief:     Constructor : Initialise all the variables
+    @param:     none
+    @return:    none
+    */
+    Convolution(){
+        
+        convStateList[0].nextState = 0x10;
+        convStateList[1].nextState = 0x32;
+        convStateList[2].nextState = 0x54;
+        convStateList[3].nextState = 0x76;
+        convStateList[4].nextState = 0x98;
+        convStateList[5].nextState = 0xBA;
+        convStateList[6].nextState = 0xDC;
+        convStateList[7].nextState = 0xFE;
+        convStateList[8].nextState = 0x10;
+        convStateList[9].nextState = 0x32;
+        convStateList[10].nextState = 0x54;
+        convStateList[11].nextState = 0x76;
+        convStateList[12].nextState = 0x98;
+        convStateList[13].nextState = 0xBA;
+        convStateList[14].nextState = 0xDC;
+        convStateList[15].nextState = 0xFE;
+        
+        convStateList[0].output = 0x0A;
+        convStateList[1].output = 0x06;
+        convStateList[2].output = 0x06;
+        convStateList[3].output = 0x0A;
+        convStateList[4].output = 0x09;
+        convStateList[5].output = 0x05;
+        convStateList[6].output = 0x05;
+        convStateList[7].output = 0x09;
+        convStateList[8].output = 0x05;
+        convStateList[9].output = 0x09;
+        convStateList[10].output = 0x09;
+        convStateList[11].output = 0x05;
+        convStateList[12].output = 0x06;
+        convStateList[13].output = 0x0A;
+        convStateList[14].output = 0x0A;
+        convStateList[15].output = 0x06;
+    }
+    
+        
+    /*
+    @brief:     convolution encode the input bytes : in sync with =>   (1-TM-Frame -> 2-T-Frame)
+    @param:     inPtr : pointer to input unsigned char array
+                inLength : length in bytes of input array
+                outPtr : pointer to the output bytes, define it outside the function
+                outLength : length of output array in bytes [call by reference, edited inside the function]
+    @return:    none
+    */
+    void convolutionEncode(unsigned char *input, unsigned char *output){
+        
+//      cout << "inside convolution" << endl;
+        
+        unsigned int convState = 0;
+        
+        int inBit = 7;
+        unsigned int inByte = 0;
+        
+        unsigned int outState = 0;
+        unsigned int outByte = 0;
+        
+        for(unsigned int j = 0 ; j < 536 ; ++j){
+//          printf("j = %u, inByte = %u\n", j, inByte);
+            // read a new bit from input stream
+//          cout << "inByte " <<inByte;
+            unsigned char tempBit = (input[inByte] >> inBit) & 1;
+            --inBit;
+            
+            // convolute and write output
+            switch(outState){
+                case 0:
+                    outState = 2;
+                    output[outByte] = ( (convStateList[convState].output >> tempBit) & 1 ) << 7;
+                    output[outByte] += ( (convStateList[convState].output >> (tempBit + 2)) & 1 ) << 6;
+                    break;
+                case 2:
+                    outState = 4;
+                    output[outByte] += ( (convStateList[convState].output >> tempBit) & 1 ) << 5;
+                    output[outByte] += ( (convStateList[convState].output >> (tempBit + 2)) & 1 ) << 4;
+                    break;
+                case 4:
+                    outState = 6;
+                    output[outByte] += ( (convStateList[convState].output >> tempBit) & 1 ) << 3;
+                    output[outByte] += ( (convStateList[convState].output >> (tempBit + 2)) & 1 ) << 2;
+                    break;
+                case 6:
+                    outState = 0;
+                    output[outByte] += ( (convStateList[convState].output >> tempBit) & 1 ) << 1;
+                    output[outByte] += ( (convStateList[convState].output >> (tempBit + 2)) & 1 );
+                    ++outByte;
+                    output[outByte] = 0x00;
+                    break;
+            }
+            
+            // next state transition
+            switch(tempBit){
+                case 0:
+                    convState = (convStateList[convState].nextState) & 0xF;
+                    break;
+                case 1:
+                    convState = (convStateList[convState].nextState >> 4) & 0xF;
+                    break;
+            }
+            
+            if(inBit == -1){
+//              printf("in byte = %u\n", inByte);
+                ++inByte;
+                inBit = 7;
+            }
+            
+        }
+        
+//      printf("normal iteration complete, outByte = %u\n", outByte);
+
+        for(unsigned int j = 0 ; j < 4 ; ++j){
+//          printf("j = %u\n, outByte = %u\n", j, outByte);
+            // append zero at the end
+            switch(outState){
+                case 0:
+                    outState = 2;
+                    output[outByte] = ( (convStateList[convState].output) & 1 ) << 7;
+                    output[outByte] += ( (convStateList[convState].output >> 2) & 1 ) << 6;
+                    break;
+                case 2:
+                    outState = 4;
+                    output[outByte] += ( (convStateList[convState].output) & 1 ) << 5;
+                    output[outByte] += ( (convStateList[convState].output >> 2) & 1 ) << 4;
+                    break;
+                case 4:
+                    outState = 6;
+                    output[outByte] += ( (convStateList[convState].output ) & 1 ) << 3;
+                    output[outByte] += ( (convStateList[convState].output >> 2) & 1 ) << 2;
+                    break;
+                case 6:
+                    outState = 0;
+                    output[outByte] += ( (convStateList[convState].output ) & 1 ) << 1;
+                    output[outByte] += ( (convStateList[convState].output >> 2) & 1 );
+                    ++outByte;
+//                  printf("outByte @ end of switch = %u\n", outByte);
+                    break;
+            }
+            // next state transition
+            convState = (convStateList[convState].nextState) & 0xF;
+        }
+//      printf("complete\n");
+    }
+    
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DefinitionsAndGlobals.h	Tue Dec 01 10:56:10 2015 +0000
@@ -0,0 +1,108 @@
+// **************DEFINITIONS*********************
+// COM_RX
+    #define RX_TIMEOUT_LIMIT 2.0
+    #define COM_RX_UART_TX PTE22
+    #define COM_RX_UART_RX PTE23
+
+// COMMON SPI
+    #define SPI_MOSI PTE1
+    #define SPI_MISO PTE3
+    #define SPI_CLK PTE2
+
+// TC LIST
+    #define TCL_STATE_IDLE 0x00
+    #define TCL_STATE_EXECUTING 0x01
+    #define TCL_STATE_COMPLETED 0x03
+    #define TCL_STATE_ABORTED 0x02
+    /*
+    0: IDLE OR WAITING FOR TCL COMPLETION
+    1: EXECUTING AFTER COMPLETION OF TCL
+    2: COMPLETED EXECUTION OF TCL
+    3: IDLE DUE TO ABORT ON NACK
+    */
+
+// LIST OF FLAGS
+    #define UART_INT_FLAG 0x01
+    #define NEW_TC_RECEIVED 0x02
+    #define START_SESSION 0x03
+    #define END_SESSION 0x04
+    #define ALL_CRC_PASS_FLAG 0x05
+    #define EXECUTE_OBOSC_FLAG 0x06
+
+// COM_MNG_TMTC THREAD
+    #define SESSION_TIME_LIMIT 1200
+    #define COM_MNG_TMTC_SIGNAL_UART_INT 0x01
+    #define COM_MNG_TMTC_SIGNAL_ADF_NSD 0x02
+    #define COM_MNG_TMTC_SIGNAL_ADF_SD 0x03
+
+// COM_MNG_TMTC
+    // starting value of packet sequence count at each pass 
+    #define PSC_START_VALUE 1
+    
+    // APID list
+    #define APID_CALLSIGN 0
+    #define APID_BAE 1
+    #define APID_CDMS 2
+    #define APID_SPEED 3
+    
+    // HIGH PRIORITY TC - priority list
+    // not correct values here
+    #define HPTC1 5
+    #define HPTC2 6
+    // Add more entries above
+    
+    // SIZE of tc in bytes
+    #define TC_SHORT_SIZE 11
+    #define TC_LONG_SIZE 135
+    
+    // TMID list
+    #define TMID_ACK_L1 0xA
+
+// OBOSC SERVICE SUBTYPE
+    #define OBOSC_SUB_DISABLE 0x01
+    #define OBOSC_SUB_RETRY 0x05
+    #define OBOSC_SUB_REP_TCL_D 0x06
+    #define OBOSC_SUB_REP_TCL 0x08
+    #define OBOSC_SUB_REP_LE 0x0F
+    #define OBOSC_SUB_RESET 0x07
+    
+    
+    
+// ****************GLOBAL VARIABLES******************
+// DEBUG
+Serial gPC( USBTX, USBRX );
+DigitalOut gLEDR(LED_RED);
+DigitalOut gLEDG(LED_GREEN);
+
+// COM_RX
+RawSerial RX1M( COM_RX_UART_TX, COM_RX_UART_RX );
+COM_RX_DATA_NODE *gRX_HEAD_DATA_NODE = NULL;
+COM_RX_DATA_NODE *gRX_CURRENT_DATA_NODE = NULL;
+uint8_t *gRX_CURRENT_PTR = NULL;
+uint32_t gRX_COUNT = 0;
+uint16_t gTOTAL_INCORRECT_SIZE_TC = 0x00;
+uint16_t gTOTAL_CRC_FAIL_TC = 0x00;
+
+
+// COMMON SPI
+SPI spi( SPI_MOSI, SPI_MISO, SPI_CLK );
+Mutex SPI_mutex;
+
+// TC LIST
+Base_tc* gHEAD_NODE_TCL = NULL;
+Base_tc* gLAST_NODE_TCL = NULL;
+uint8_t gMASTER_STATE = TCL_STATE_IDLE;
+uint8_t gFLAGS = 0x00;
+
+// COM_MNG_TMTC THREAD
+Thread* gCOM_MNG_TMTC_THREAD = NULL;
+Timeout gRX_TIMEOUT;
+Timeout gSESSION_TIMEOUT;
+
+// COM_MNG_TMTC
+uint8_t gTOTAL_VALID_TC = 0x00;
+// USE LAST_L1_ACK FOR GENERATING REPORT
+uint8_t gLAST_L1_ACK[TM_SHORT_SIZE];
+uint8_t gLAST_L1_ACK_BUFFER[TM_SHORT_SIZE];
+uint8_t gOBOSC_PSC = PSC_START_VALUE;
+Base_tc* gOBOSC_HEAD = NULL;
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Structures.h	Tue Dec 01 10:56:10 2015 +0000
@@ -0,0 +1,142 @@
+// TC TM
+    #define TC_SHORT_SIZE 11
+    #define TC_LONG_SIZE 135
+    #define TM_LONG_SIZE 134
+    #define TM_SHORT_SIZE 13
+
+// COM_RX
+    #define RX_BUFFER_LENGTH 60
+    // 60+4 = 64
+
+struct data_node{
+   uint8_t values[RX_BUFFER_LENGTH];
+   struct data_node* next_node;
+};
+typedef struct data_node COM_RX_DATA_NODE;
+
+//TELECOMMAND:
+
+/*
+exec_status:
+0: unexecuted
+1: successfully executed
+2: Execution Failed
+3: Disabled
+4: Marked For retry
+*/
+#define TC_STATE_UNEXECUTED 0x00
+#define TC_STATE_SUCCESSFULLY_EXECUTED 0x01
+#define TC_STATE_EXECUTION_FAILED 0x02
+#define TC_STATE_DISABLED 0x03
+#define TC_STATE_MARKED_RETRY 0x04
+
+//MASKS
+#define SHORT_LONG_TC_MASK 0x10
+#define CRC_MASK 0x08
+#define EXEC_STATUS_MASK 0x07
+
+//USE ONLY THE BELOW MACROS TO MODIFY 'flags' VARIABLE
+//x should be a Base_tc pointer
+#define GETshort_or_long_tc(x) ( ( (x->flags) & SHORT_LONG_TC_MASK ) >> 4 )
+#define GETcrc_pass(x) ( ( (x->flags) & CRC_MASK ) >> 3 )
+#define GETabort_on_nack(x) ( ( (x->TC_string[1]) & 0x08 ) >> 3 )
+#define GETapid(x) ( ( (x->TC_string[1]) & 0xA0 ) >> 6 )
+#define GETexec_status(x) ( (x->flags) & EXEC_STATUS_MASK )
+#define GETpacket_seq_count(x) (x->TC_string[0])
+
+//x should be a Base_tc pointer
+//y should be a 16-bit number with relevant data in LSB
+//use in a seperate line with ; at the end: similar to a function
+#define PUTshort_or_long(x,y) x->flags = ( (x->flags) & ~(SHORT_LONG_TC_MASK)) | ( (y << 4) & SHORT_LONG_TC_MASK )
+#define PUTcrc_pass(x,y) x->flags = ( (x->flags) & ~(CRC_MASK)) | ( (y << 3) & CRC_MASK)
+#define PUTexec_status(x,y) x->flags = ( (x->flags) & ~(EXEC_STATUS_MASK)) | ( y & EXEC_STATUS_MASK)
+
+//PARENT TELECOMMAND CLASS 
+class Base_tc{
+public:
+    uint8_t flags;
+    uint8_t *TC_string;
+    Base_tc *next_TC;
+    
+    virtual ~Base_tc(){}
+};
+
+//DERIVED CLASS - SHORT TC
+class Short_tc : public Base_tc{
+private:
+    uint8_t fix_str[TC_SHORT_SIZE];
+public:
+    Short_tc(){
+        TC_string = fix_str;
+        flags = 0;
+    }
+    
+    ~Short_tc(){}
+};
+
+//DERIVED CLASS - LONG TC
+class Long_tc : public Base_tc{
+private:
+    uint8_t fix_str[TC_LONG_SIZE];
+public:
+    Long_tc(){
+        TC_string = fix_str;
+        flags = 0;
+    }
+    
+    ~Long_tc(){}
+};
+
+// TELEMETRY:
+// MASKS
+#define SHORT_LONG_TM_MASK 0x10
+#define TMID_MASK 0x0F
+
+//x should be 'fields' variable defined in the Base_tm
+#define GETshort_or_long_tm(x) ((x & SHORT_LONG_TM_MASK) >> 4)
+#define GETtmid(x) (x & TMID_MASK)
+
+//x should be 'fields' variable defines in the Base_tm
+//y should be an 8-bit number with relevent data at LSB
+#define PUTshort_or_long_tm(x,y) x = (x & ~(SHORT_LONG_TM_MASK)) | ((y<<4) & SHORT_LONG_TM_MASK)
+#define PUTtmid(x,y) x = (x & ~(TMID_MASK)) | (y & TMID_MASK)
+
+// PARENT TELEMETRY CLASS
+class Base_tm{
+public:
+    uint8_t fields;
+    uint8_t *TM_string;
+    Base_tm *next_TM;
+    
+    virtual ~Base_tm(){}
+};
+
+// DERIVED CLASS : Long tm [type 0]
+// type 0
+class Long_tm : public Base_tm{
+private:
+    uint8_t fix_str[TM_LONG_SIZE];
+public:
+    Long_tm(){
+        TM_string = fix_str;
+        // type 0
+        fields = 0;
+    }
+    
+    ~Long_tm(){}
+};
+
+// DERIVED CLASS : Short tm [type 1]
+// type 1
+class Short_tm : public Base_tm{
+private:
+    uint8_t fix_str[TM_SHORT_SIZE];
+public:
+    Short_tm(){
+        TM_string = fix_str;
+        // type 1
+        fields = 0x10;
+    }
+    
+    ~Short_tm(){}
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ThreadsAndFunctions.h	Tue Dec 01 10:56:10 2015 +0000
@@ -0,0 +1,108 @@
+// UART ISR
+void rx_read(){
+    *gRX_CURRENT_PTR = RX1M.getc();
+    gFLAGS = gFLAGS | (0xFF & UART_INT_FLAG);
+    gCOM_MNG_TMTC_THREAD->signal_set(COM_MNG_TMTC_SIGNAL_UART_INT);
+}
+
+// RX_TIMEOUT ISR
+void after_receive(){
+    gRX_TIMEOUT.detach();
+    gFLAGS = gFLAGS | (0xFF & NEW_TC_RECEIVED);
+    gCOM_MNG_TMTC_THREAD->signal_set(COM_MNG_TMTC_SIGNAL_UART_INT);
+}
+
+// SESSION_TIMOUT ISR
+void after_session(){
+    gSESSION_TIMEOUT.detach();
+    gFLAGS = gFLAGS | (0xFF & END_SESSION);
+}
+
+#define reset_all {\
+    Base_tc *tcp = gHEAD_NODE_TCL;\
+    while(tcp != NULL){\
+        Base_tc *temp = tcp->next_TC;\
+        delete tcp;\
+        tcp = temp;\
+    }\
+    rx_tc_frames = 0;\
+    rx_tc_frames = 0;\
+    gTOTAL_INCORRECT_SIZE_TC = 0;\
+    gTOTAL_CRC_FAIL_TC = 0;\
+    COM_RX_DATA_NODE *dataptr = gRX_HEAD_DATA_NODE;\
+    while( dataptr != NULL ){\
+        COM_RX_DATA_NODE *temp = dataptr->next_node;\
+        delete dataptr;\
+        dataptr = temp;\
+    }\
+    gRX_HEAD_DATA_NODE = new COM_RX_DATA_NODE;\
+    gRX_HEAD_DATA_NODE->next_node = NULL;\
+    gRX_CURRENT_DATA_NODE = gRX_HEAD_DATA_NODE;\
+    gRX_CURRENT_PTR = gRX_HEAD_DATA_NODE->values;\
+    gRX_COUNT = 0;\
+}
+
+void COM_MNG_TMTC_FUN(void const *args){
+    while(true){
+        Thread::signal_wait( COM_MNG_TMTC_SIGNAL_UART_INT );
+        if( gFLAGS & UART_INT_FLAG ){
+            gFLAGS = gFLAGS & (~UART_INT_FLAG);
+            if( gRX_COUNT < (RX_BUFFER_LENGTH-1) ){
+                ++gRX_CURRENT_PTR;
+                ++gRX_COUNT;
+            }
+            else{
+                gLEDG = !gLEDG;
+                gRX_COUNT = 0;
+                gRX_CURRENT_DATA_NODE->next_node = new COM_RX_DATA_NODE;
+                gRX_CURRENT_DATA_NODE = gRX_CURRENT_DATA_NODE->next_node;
+                gRX_CURRENT_DATA_NODE->next_node = NULL;
+                gRX_CURRENT_PTR = gRX_CURRENT_DATA_NODE->values;
+            }
+            gRX_TIMEOUT.attach(&after_receive, RX_TIMEOUT_LIMIT);
+        }
+        else if( gFLAGS & NEW_TC_RECEIVED ){
+            gFLAGS = gFLAGS & (~NEW_TC_RECEIVED);
+            
+            // DISABLE THE RX1M INTERRUPT
+            RX1M.attach(NULL);
+            
+            // PENDING: BEACON
+            while(gRX_COUNT < (RX_BUFFER_LENGTH-1)){
+                *gRX_CURRENT_PTR = 0x00;
+                ++gRX_CURRENT_PTR;
+                ++gRX_COUNT;
+            }
+            gRX_COUNT = 0;
+            
+            if( (gMASTER_STATE == TCL_STATE_IDLE) || (gMASTER_STATE == TCL_STATE_ABORTED) || (gMASTER_STATE == TCL_STATE_COMPLETED)){
+                raw_data_to_tc();
+                if( (gTOTAL_VALID_TC > 0) && (gFLAGS & START_SESSION) ){
+                    gFLAGS = gFLAGS & (~START_SESSION);
+                    gSESSION_TIMEOUT.attach(&after_session, SESSION_TIME_LIMIT);
+                }
+                uint8_t tempContinue = 0xFF;
+                continueToExecute(tempContinue);
+                if( tempContinue == 0x00 ){
+                    if(gMASTER_STATE == TCL_STATE_ABORTED){
+                        gMASTER_STATE = TCL_STATE_EXECUTING;
+                        send_l1_ack;
+                        EXECUTE_OBOSC_ONLY;
+                    }
+                    EXECUTE_TC;
+                }
+                else{
+                    send_l1_ack;
+                }
+                gPC.puts("Completed Execution\r\n");
+            }
+            else{
+                // invalid state found - reset
+                reset_all;
+            }
+            
+            // PENDING: BEACON
+            RX1M.attach(&rx_read, Serial::RxIrq);
+        }
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/crc.h	Tue Dec 01 10:56:10 2015 +0000
@@ -0,0 +1,77 @@
+//EDITS
+//changed the initial remainder from 0x0000 to 0xffff according to the standards
+//made two seperate functions crc16_gen and crc8_gen
+
+#define TOPBIT16 (1 << 15)
+#define TOPBIT8 (1 << 7)
+#define POLYNOMIAL16 0x1021
+#define POLYNOMIAL8 0xEA
+
+typedef uint16_t crctype16; 
+crctype16 crc16_gen(const unsigned char message[], unsigned int nBytes){
+    crctype16 remainder = 0xffff;
+    int byte;
+    char bit;
+    
+    for( byte = 0 ; byte < nBytes ; byte++ ){
+        /*
+        Bring the data byte by byte
+        each time only one byte is brought
+        0 xor x = x
+        */
+        remainder = remainder ^ ( message[byte] << 8 );
+        
+        for( bit = 8 ; bit > 0 ; bit--){
+            /*
+            for each bit, xor the remainder with polynomial
+            if the MSB is 1
+            */
+            if(remainder & TOPBIT16){
+                remainder = (remainder << 1) ^ POLYNOMIAL16;
+                /*
+                each time the remainder is xor-ed with polynomial, the MSB is made zero
+                hence the first digit of the remainder is ignored in the loop
+                */
+            }
+            else{
+                remainder = (remainder << 1);
+            }
+        }
+    }
+    
+    return remainder;
+}
+
+typedef uint8_t crctype8;
+crctype8 crc8_gen(const unsigned char message[], unsigned int nBytes){
+    
+    crctype8 remainder = 0xff;
+    
+    for(int byte = 0 ; byte < nBytes ; byte++ ){
+        /*
+        Bring the data byte by byte
+        each time only one byte is brought
+        0 xor x = x
+        */
+        remainder = remainder ^ ( message[byte] );
+        
+        for(int bit = 8 ; bit > 0 ; bit--){
+            /*
+            for each bit, xor the remainder with polynomial
+            if the MSB is 1
+            */
+            if(remainder & TOPBIT8){
+                remainder = (remainder << 1) ^ POLYNOMIAL8;
+                /*
+                each time the remainder is xor-ed with polynomial, the MSB is made zero
+                hence the first digit of the remainder is ignored in the loop
+                */
+            }
+            else{
+                remainder = (remainder << 1);
+            }
+        }
+    }
+    
+    return remainder;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Dec 01 10:56:10 2015 +0000
@@ -0,0 +1,65 @@
+#include "mbed.h"
+#include "rtos.h"
+
+#include "Structures.h"
+#include "DefinitionsAndGlobals.h"
+#include "crc.h"
+#include "COM_RCV_TC.h"
+#include "COM_MNG_TMTC.h"
+#include "ThreadsAndFunctions.h"
+
+DigitalOut led(LED_RED);
+
+int main()
+{
+    // ******************INITIALISATIONS START******************
+    // COM RX
+    RX1M.baud(1200);
+    gRX_HEAD_DATA_NODE = new COM_RX_DATA_NODE;
+    gRX_HEAD_DATA_NODE->next_node = NULL;
+    gRX_CURRENT_DATA_NODE = gRX_HEAD_DATA_NODE;
+    gRX_CURRENT_PTR = gRX_CURRENT_DATA_NODE->values;
+    RX1M.attach(&rx_read, Serial::RxIrq);
+    
+    // DEBUG
+    gPC.baud(9600);
+    gPC.puts("welcome to mng_tm_tc\r\n");
+    
+    // COMMON SPI
+    spi.format(8,0);
+    spi.frequency(1000000);
+
+    // SD CARD
+    // cs_SDCard=1;
+    // initialise_card();
+    // int result= initialise_card();
+    // disk_initialize();    
+    
+    // COM_TX
+    // *********************THE TEST
+    // cs_adf=1;
+    // *********************THE TEST
+
+    // COM_MNG_TMTC THREAD
+    gCOM_MNG_TMTC_THREAD = new Thread(COM_MNG_TMTC_FUN);
+    gCOM_MNG_TMTC_THREAD->set_priority(osPriorityAboveNormal);
+    // *******************INITIALISATIONS END********************
+    
+    int theAcount = 0;
+    while(true){
+        if(theAcount >= 100000){
+            theAcount = 0;
+            gLEDR = !gLEDR;
+        }
+        else{
+            theAcount++;
+        }
+        if(gFLAGS & END_SESSION){
+            gFLAGS = gFLAGS & (~END_SESSION);
+            gFLAGS = gFLAGS | (0xFF & START_SESSION);
+            // pass got over reset all
+            reset_all;
+            //PENDING: also consider frame_no
+        }
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Tue Dec 01 10:56:10 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#6c35e082773a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Dec 01 10:56:10 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/165afa46840b
\ No newline at end of file