Compression code changed and RLY_TMTC pointers managed

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

Files at this revision

API Documentation at this revision

Comitter:
ee12b079
Date:
Tue Jul 05 11:27:29 2016 +0000
Parent:
228:335da5b0d3e4
Child:
234:2a1ea88086f3
Commit message:
Added Aniruddh's changes, to be tested, use earlier revisions for testing BAE, CDMS

Changed in this revision

COM_MNG_TMTC.h Show annotated file Show diff for this revision Revisions of this file
COM_POWER_OFF_TX.h Show annotated file Show diff for this revision Revisions of this file
COM_POWER_ON_TX.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
DefinitionsAndGlobals.h Show annotated file Show diff for this revision Revisions of this file
P_COM_INIT.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
common_functions.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
--- a/COM_MNG_TMTC.h	Mon Jul 04 23:13:24 2016 +0000
+++ b/COM_MNG_TMTC.h	Tue Jul 05 11:27:29 2016 +0000
@@ -14,7 +14,6 @@
 
 //DigitalIn tm_status_4m_slv(PIN39); //I2C interrupt to CDMS from BAE
 //DigitalIn tm_status_4m_pl(PIN61); //I2C interrupt to CDMS from PL
-#define RESET_CDMS NVIC_SystemReset()
 
 uint8_t received = 0;
 uint16_t mid1;
@@ -46,7 +45,7 @@
 
 
 
-#define detect_ack(tm_ptr, temp_ack, tc_psc) {\
+#define detect_ack(tm_ptr, temp_ack) {\
     if( tm_ptr != NULL ){\
         int length = TM_SHORT_SIZE;\
         if( GETshort_or_long_tm(tm_ptr) == SHORT_TM_CODE ){\
@@ -63,20 +62,20 @@
             if( (temp8 == 0xC0) || (temp8 == 0xA0) )\
                 temp_ack = 0x01;\
             else{\
-                temp_ack = 0x00;\
+                temp_ack = 0x02;\
                 /*CHECK FOR NACK CODE: CRC FAIL NACK*/\
                 temp8 = tm_ptr->TM_string[TM_ACK_CODE_INDEX];\
                 temp8 = temp8 & 0x0F;\
                 if( temp8 == CRC_FAIL_NACK_CODE ){\
-                    tm_ptr->TM_string[2] = tc_psc;\
+                    temp_ack = 0x04;\
                 }\
             }\
         }\
         else\
-            temp_ack = 0x00;\
+            temp_ack = 0x03;\
     }\
     else{\
-        temp_ack = 0x00;\
+        temp_ack = 0x02;\
     }\
 }
 
@@ -1048,7 +1047,7 @@
     }\
     else if( (service_subtype != OBOSC_SUB_DISABLE) && (service_subtype != OBOSC_SUB_RETRY) ){\
         /*CHANGE THE ACK CODE TO INVALID TC*/\
-        ackl234new->TM_string[2] = 0xA2;\
+        ackl234new->TM_string[2] = 0xAF;\
     }\
     if (DEBUG)\
         gPC.puts("completed obosc\r\n");\
@@ -1058,6 +1057,7 @@
 
 
 #define EXECUTE_OBOSC_ONLY {\
+    gMASTER_STATE = TCL_STATE_EXECUTING;\
     int reset_flag = 0;\
     if (DEBUG)\
         gPC.puts("iNSIDE EXECUTE_OBOSC_ONLY\r\n");\
@@ -1086,8 +1086,14 @@
                             while(obosc_tm_core != NULL){\
                                obosc_tm_core = obosc_tm_core->next_TM;\
                             }\
-                            uint16_t temp16 = TC_STATE_SUCCESSFULLY_EXECUTED;\
-                            PUTexec_status(current_TC, temp16);\
+                            if(obosc_tm_current->TM_string[2] == 0xA0){\
+                                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);\
+                            }\
                             while( obosc_tm_current != NULL ){\
                                 int length = TM_SHORT_SIZE;\
                                 if( GETshort_or_long_tm(obosc_tm_current) == SHORT_TM_CODE ){\
@@ -1101,9 +1107,6 @@
                                 }\
                                 obosc_tm_current = obosc_tm_current->next_TM;\
                             }\
-                            if ( reset_flag ==1 ){\
-                                reset_all;\
-                            }\
                         }\
                     }\
                 }\
@@ -1111,7 +1114,7 @@
                 ++overCount;\
             }\
             else{\
-                /*PENDING: REST CDMS: MEMORY LEAK FOUND*/\
+                RESET_CDMS;\
                 break;\
             }\
         }\
@@ -1151,6 +1154,12 @@
                 break;\
             }\
         }\
+    }\
+    if ( reset_flag == 1 ){\
+        reset_all;\
+        /*PENDING: Enable threads*/\
+        /*gCDMS_HK_TIMER.start(5000);*/\
+        gSESSION_TIMEOUT.detach();\
     }\    
 }
 
@@ -1248,19 +1257,33 @@
                             }\
                             /*DETECT ACK OR NACK*/\
                             uint8_t temp83 = 0x00;\
-                            uint8_t temp84 = GETpacket_seq_count(current_TC);\
-                            detect_ack(put_tm_here, temp83, temp84);\
+                            detect_ack(put_tm_here, temp83);\
                             if( temp83 == 0x01){\
                                 uint16_t temp16 = TC_STATE_SUCCESSFULLY_EXECUTED;\
                                 PUTexec_status(current_TC, temp16);\
                             }\
+                            else if( temp83 == 0x03){\
+                                /*gPC.puts("TC_STATE_EXECUTION_UNKNOWN");*/\
+                                uint16_t temp16 = TC_STATE_EXECUTION_UNKNOWN;\
+                                PUTexec_status(current_TC, temp16);\
+                            }\
+                            else if( temp83 == 0x04){\
+                                /*gPC.puts("TC_STATE_EXECUTION_UNKNOWN");*/\
+                                uint16_t temp16 = TC_STATE_EXECUTION_FAILED;\
+                                PUTexec_status(current_TC, temp16);\
+                                uint8_t temp84 = GETpacket_seq_count(current_TC);\
+                                put_tm_here->TM_string[2] = temp84;\
+                                uint16_t crc16 = crc16_gen(put_tm_here->TM_string, 11);\
+                                put_tm_here->TM_string[11]  = (crc16 & 0xFF00)>>8;\
+                                put_tm_here->TM_string[12] = (crc16 & 0x00FF);\
+                            }\
                             else{\
                                 /*gPC.puts("TC_STATE_EXECUTION_FAILED");*/\
                                 uint16_t temp16 = TC_STATE_EXECUTION_FAILED;\
                                 PUTexec_status(current_TC, temp16);\
                             }\
                             /*ABORT ON NACK AND TC LIST COMPLETED: UPDATE IN gMASTERSTATE*/\
-                            if( (GETexec_status(current_TC) == TC_STATE_EXECUTION_FAILED) && (GETabort_on_nack(current_TC) == 1) ){\
+                            if( ((GETexec_status(current_TC) == TC_STATE_EXECUTION_FAILED) || (GETexec_status(current_TC) == TC_STATE_EXECUTION_UNKNOWN)) && (GETabort_on_nack(current_TC) == 1) ){\
                                 gMASTER_STATE = TCL_STATE_ABORTED;\
                             }\
                             else if( execute_psc == (gTOTAL_VALID_TC-1) ){\
@@ -1269,20 +1292,23 @@
                             }\
                             /*update last executed L1_ack*/\
                             if( put_tm_here != NULL ){\
-                                if( GETshort_or_long_tm(put_tm_here) == SHORT_TM_CODE ){\
+                                Base_tm *ptr_tm = put_tm_here;\
+                                while(ptr_tm->next_TM != NULL)\
+                                    ptr_tm = ptr_tm->next_TM;\
+                                if( GETshort_or_long_tm(ptr_tm) == SHORT_TM_CODE ){\
                                     for(int i = 0 ; i < TM_SHORT_SIZE ; ++i){\
-                                        gLAST_TM[i] = put_tm_here->TM_string[i];\
+                                        gLAST_TM[i] = ptr_tm->TM_string[i];\
                                         gLAST_TM_SHORT_OR_LONG = SHORT_TM_CODE;\
                                     }\
                                 }\
                                 else{\
                                     for( int i = 0 ; i < TM_LONG_SIZE ; ++i ){\
-                                        gLAST_TM[i] = put_tm_here->TM_string[i];\
+                                        gLAST_TM[i] = ptr_tm->TM_string[i];\
                                         gLAST_TM_SHORT_OR_LONG = LONG_TM_CODE;\
                                     }\
                                 }\
                             }\
-                            /*PENDING: APPEND ACK L1*/\
+                            P_COM_HK;\
                             Base_tm *tm_ptr = tm_ptr_head;\
                             get_ack_l1(tm_ptr->next_TM);\
                             int overflowCountExecute = 0;\
@@ -1297,11 +1323,6 @@
                                 }\
                             }\
                             tm_ptr->next_TM = put_tm_here;\
-                            /*CHECK FOR HOT PA*/\
-                            isPAhot(tempPAhot);\
-                            if( tempPAhot == 0x00 ){\
-                                gFLAGS = gFLAGS | COM_PA_HOT_FLAG;\
-                            }\
                             tm_ptr = tm_ptr_head;\
                             while( tm_ptr != NULL ){\
                                 int length = TM_SHORT_SIZE;\
@@ -1342,112 +1363,10 @@
                             if (DEBUG)\
                                 gPC.printf("sd card at %u\r\n", execute_psc);\
                             /*EXECUTION OF SD-CARD DATA SENDING (OBSRS)*/\
-                            Base_tm *tm_ptr_head = NULL;\
-                            get_call_sign(tm_ptr_head);\
-                            Base_tm *put_tm_here = NULL;\
-                            /*execute_obsrs(current_TC, put_tm_here)*/\
-                            /*read_TC(current_TC);*/\
                             execute_OBSRS_TC(current_TC);\
                             gPC.puts("en_adf");\
                             transmit_adf;\
                             gPC.puts("ex_adf");\
-                            uint8_t tempExec = TC_STATE_SUCCESSFULLY_EXECUTED;\
-                            PUTexec_status(current_TC, tempExec);\
-                            /*PENDING: ABORT ON NACK CHECK, gMASTER_STATE VERIFICATION WITH SD CARD, session timeout, last executed ack l234*/\
-                            if( (GETexec_status(current_TC) == TC_STATE_EXECUTION_FAILED) && (GETabort_on_nack(current_TC) == 1) ){\
-                                gMASTER_STATE = TCL_STATE_ABORTED;\
-                            }\
-                            else if( execute_psc == (gTOTAL_VALID_TC-1) ){\
-                                gMASTER_STATE = TCL_STATE_COMPLETED;\
-                            }\
-                            else{\
-                                uint8_t check1 = 1;\
-                                for( uint8_t test_psc = execute_psc+1 ; test_psc < gTOTAL_VALID_TC ; ++test_psc ){\
-                                    Base_tc* test_TC = gHEAD_NODE_TCL;\
-                                    while(test_TC != NULL){\
-                                        if( GETpacket_seq_count(test_TC) == test_psc ){\
-                                            uint8_t checkval;\
-                                            isit_obosc(test_TC, checkval);\
-                                            if( checkval == 0 ){\
-                                                check1 = 0;\
-                                            }\
-                                        }\
-                                        test_TC = test_TC->next_TC;\
-                                    }\
-                                }\
-                                if( check1 == 0x01 ){\
-                                    gMASTER_STATE = TCL_STATE_COMPLETED;\
-                                }\
-                            }\
-                            /*update last executed L1_ack*/\
-                            if( put_tm_here != NULL ){\
-                                if( GETshort_or_long_tm(put_tm_here) == SHORT_TM_CODE ){\
-                                    for(int i = 0 ; i < TM_SHORT_SIZE ; ++i){\
-                                        gLAST_TM[i] = put_tm_here->TM_string[i];\
-                                        gLAST_TM_SHORT_OR_LONG = SHORT_TM_CODE;\
-                                    }\
-                                }\
-                                else{\
-                                    for( int i = 0 ; i < TM_LONG_SIZE ; ++i ){\
-                                        gLAST_TM[i] = put_tm_here->TM_string[i];\
-                                        gLAST_TM_SHORT_OR_LONG = LONG_TM_CODE;\
-                                    }\
-                                }\
-                            }\
-                            /*PENDING: APPEND ACK L1*/\
-                            Base_tm *tm_ptr = tm_ptr_head;\
-                            get_ack_l1(tm_ptr->next_TM);\
-                            int overflowCountExecute = 0;\
-                            while( tm_ptr->next_TM != NULL ){\
-                                if( overflowCountExecute < TM_OVERFLOW_CONSTANT ){\
-                                    tm_ptr = tm_ptr->next_TM;\
-                                    ++overflowCountExecute;\
-                                }\
-                                else{\
-                                    RESET_CDMS;\
-                                    break;\
-                                }\
-                            }\
-                            tm_ptr->next_TM = put_tm_here;\
-                            /*CHECK FOR HOT PA*/\
-                            uint8_t tempPAhot = 0x00;\
-                            isPAhot(tempPAhot);\
-                            if( tempPAhot == 0x00 ){\
-                                gFLAGS = gFLAGS | COM_PA_HOT_FLAG;\
-                            }\
-                            tm_ptr = tm_ptr_head;\
-                            while( tm_ptr != NULL ){\
-                                int length = TM_SHORT_SIZE;\
-                                if( GETshort_or_long_tm(tm_ptr) == SHORT_TM_CODE ){\
-                                    length = TM_SHORT_SIZE;\
-                                }\
-                                else{\
-                                    length = TM_LONG_SIZE;\
-                                }\
-                                /*gPC.puts("Printing Call Sign, ACK_L1, TM list");*/\
-                                for(int i = 0 ; i < length ; ++i){\
-                                    /*gPC.putc(tm_ptr->TM_string[i]);*/\
-                                }\
-                                tm_ptr = tm_ptr->next_TM;\
-                            }\
-                            /*SEND call sign, ACK_L1, OBSRS TO GS*/\
-                            /*snd_tm.head_pointer(tm_ptr_head);*/\
-                            /*transmit_adf;*/\
-                            /*DELETE THE TM AFTER USE*/\
-                            tm_ptr = tm_ptr_head;\
-                            overflowCountExecute = 0;\
-                            while(tm_ptr != NULL){\
-                                if( overflowCountExecute < TM_OVERFLOW_CONSTANT ){\
-                                    Base_tm *temp = tm_ptr->next_TM;\
-                                    delete tm_ptr;\
-                                    tm_ptr = temp;\
-                                    ++overflowCountExecute;\
-                                }\
-                                else{\
-                                    RESET_CDMS;\
-                                    break;\
-                                }\
-                            }\
                         }\
                     }\
                     break;\
@@ -1467,15 +1386,15 @@
             if (DEBUG)\
                 gPC.puts("ABORTING DUE TO ABORT ON NACK\r\n");\
             /*EXITED DUE TO ABORT ON NACK:*/\
-            /*COM_TX_CNTRL = 0;*/\
+            COM_TX_CNTRL = 0;\
             RX1M.attach(&rx_read, Serial::RxIrq);\
             gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);\
             break;\
         }\
-        else if( gFLAGS & COM_PA_HOT_FLAG ){\
+        else if( (gFLAGS & COM_PA_HOT_FLAG) || (gFLAGS & COM_PA_OC_FLAG) ){\
             /*PA HOT: WAIT FOR TIMEOUT*/\
             gCOM_PA_COOLING_TIMEOUT.attach(&after_cooling_pa, COM_PA_COOLING_TIME_LIMIT);\
-            /*COM_TX_CNTRL = 0;*/\
+            COM_TX_CNTRL = 0;\
             RX1M.attach(&rx_read, Serial::RxIrq);\
             gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);\
             break;\
@@ -1488,10 +1407,10 @@
             gPC.printf("completed or session timed out: %x\r\n", gMASTER_STATE);\
         /*COMPLETED EXECUTION OF TC*/\
         gMASTER_STATE = TCL_STATE_COMPLETED;\
-        /*COM_POWER_OFF_TX;*/\
+        COM_POWER_OFF_TX;\
         reset_all;\
         /*PENDING : ENABLE THREADS*/\
-        gPAY_SPI.bulkRead_start();\
+        /*gCDMS_HK_TIMER.start(5000);*/\
         gSESSION_TIMEOUT.detach();\
         gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);\
         gFLAGS = gFLAGS & (~COM_SESSION_FLAG);\
--- a/COM_POWER_OFF_TX.h	Mon Jul 04 23:13:24 2016 +0000
+++ b/COM_POWER_OFF_TX.h	Tue Jul 05 11:27:29 2016 +0000
@@ -1,15 +1,136 @@
+/*#define RESET_CDMS_TC {\
+    Base_tc *RESET_tc = new Short_tc;\
+    RESET_tc->next_TC = NULL;\
+    PUTshort_or_long(RESET_tc,SHORT_TC_CODE);\
+    PUTcrc_pass(RESET_tc,0x1);\
+    PUTexec_status(RESET_tc,0);\
+    RESET_tc->TC_string[0] = 0x01;\
+    RESET_tc->TC_string[1] = 0x81;\
+    RESET_tc->TC_string[2] = 0x81;\
+    RESET_tc->TC_string[3] = 0x38;\
+    RESET_tc->TC_string[4] = 0;\
+    RESET_tc->TC_string[5] = 0;\
+    RESET_tc->TC_string[6] = 0;\
+    RESET_tc->TC_string[7] = 0;\
+    RESET_tc->TC_string[8] = 0;\
+    uint16_t crc16 = crc16_gen(RESET_tc->TC_string, 9);\
+    RESET_tc->TC_string[9]  = (crc16 & 0xFF00)>>8;\
+    RESET_tc->TC_string[10] = (crc16 & 0x00FF);\
+    Base_tm *tm_ptr = NULL;\
+    tm_ptr =  FCTN_CDMS_RLY_TMTC(RESET_tc);\
+    delete RESET_tc;\
+    /*DELETE THE TM AFTER USE*//*
+    Base_tm *del_tm = tm_ptr;\
+    while( del_tm != NULL ){\
+        Base_tm *temp = del_tm->next_TM;\
+        delete del_tm;\
+        del_tm = temp;\
+    }\
+}*/
+
+#define power_on_bae {\
+    Base_tc *power_on_BAE = new Short_tc;\
+    power_on_BAE->next_TC = NULL;\
+    PUTshort_or_long(power_on_BAE,SHORT_TC_CODE);\
+    PUTcrc_pass(power_on_BAE,0x1);\
+    PUTexec_status(power_on_BAE,0);\
+    power_on_BAE->TC_string[0] = 0x01;\
+    power_on_BAE->TC_string[1] = BAE_APID_SOURCE;\
+    power_on_BAE->TC_string[2] = BAE_SERVICE;\
+    power_on_BAE->TC_string[3] = BAE_POWER_ON_PID;\
+    power_on_BAE->TC_string[4] = 0;\
+    power_on_BAE->TC_string[5] = 0;\
+    power_on_BAE->TC_string[6] = 0;\
+    power_on_BAE->TC_string[7] = 0;\
+    power_on_BAE->TC_string[8] = 0;\
+    uint16_t crc16 = crc16_gen(power_on_BAE->TC_string, 9);\
+    power_on_BAE->TC_string[9]  = (crc16 & 0xFF00)>>8;\
+    power_on_BAE->TC_string[10] = (crc16 & 0x00FF);\
+    Base_tm *tm_ptr = NULL;\
+    tm_ptr =  FCTN_CDMS_RLY_TMTC(power_on_BAE);\
+    delete power_on_BAE;\
+    /*DELETE THE TM AFTER USE*/\
+    Base_tm *del_tm = tm_ptr;\
+    int overCount = 0;\
+    while( del_tm != NULL ){\
+        if( (overCount < TM_OVERFLOW_CONSTANT) ){\
+            Base_tm *temp = del_tm->next_TM;\
+            delete del_tm;\
+            del_tm = temp;\
+            ++overCount;\
+        }\
+        else{\
+            RESET_CDMS;\
+            break;\
+        }\
+    }\
+}
+
+#define resume_bcn {\
+    Base_tc *beacon_tc = new Long_tc;\
+    beacon_tc->next_TC = NULL;\
+    PUTshort_or_long(beacon_tc,LONG_TC_CODE);\
+    PUTcrc_pass(beacon_tc,0x1);\
+    PUTexec_status(beacon_tc,0);\
+    beacon_tc->TC_string[0] = 0x01;\
+    beacon_tc->TC_string[1] = BCN_APID_SOURCE;\
+    beacon_tc->TC_string[2] = BCN_SERVICE;\
+    beacon_tc->TC_string[3] = 0xE2;\
+    beacon_tc->TC_string[4] = 0x00;\
+    beacon_tc->TC_string[5] = 0;\
+    beacon_tc->TC_string[6] = 0;\
+    beacon_tc->TC_string[7] = 0;\
+    beacon_tc->TC_string[8] = 0;\
+    uint16_t crc16 = crc16_gen(beacon_tc->TC_string, 9);\
+    beacon_tc->TC_string[9]  = (crc16 & 0xFF00)>>8;\
+    beacon_tc->TC_string[10] = (crc16 & 0x00FF);\
+    for(int i = 11; i < 135; i++){\
+        beacon_tc->TC_string[i] = 0;\
+    }\
+    Base_tm *tm_ptr = NULL;\
+    tm_ptr =  FCTN_CDMS_RLY_TMTC(beacon_tc);\
+    delete beacon_tc;\
+    /*DELETE THE TM AFTER USE*/\
+    Base_tm *del_tm = tm_ptr;\
+    int overCount = 0;\
+    while( del_tm != NULL ){\
+        if( (overCount < TM_OVERFLOW_CONSTANT) ){\
+            Base_tm *temp = del_tm->next_TM;\
+            delete del_tm;\
+            del_tm = temp;\
+            ++overCount;\
+        }\
+        else{\
+            RESET_CDMS;\
+            break;\
+        }\
+    }\
+}
+
 #define COM_POWER_OFF_TX {\
     /*gPC.puts("Inside COM_POWER_OFF_TX\r\n");*/\
-    /*PENDING: POWER OFF TRANSMITTER*/\
+    COM_TX_CNTRL = 0;\
     RX1M.attach(&rx_read, Serial::RxIrq);\
     gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);\
-    /*PENDING: SWITCH RF RELAY TO BEACON*/\
-    /*PENDING: RF_SW_STATUS = RF_BCN*/\
-    /*PENDING: POWER ON BAE*/\
-    /*PENDING: SET BAE SW_EN_STATUS*/\
-    /*PENDING: RESUME BEACON*/\
+    RF_SW_CNTRL_BCN = 1;\
+    Thread::wait(25);\
+    RF_SW_CNTRL_BCN = 0;\
+    gFLAGS = gFLAGS | RF_SW_STATUS_FLAG;\
+    if( gFLAGS & COM_AUTO_POWER_OFF_BAE_FLAG ){\
+        power_on_bae;\
+        gFLAGS = gFLAGS | BAE_SW_EN_FLAG;\
+        gFLAGS & (~COM_AUTO_POWER_OFF_BAE_FLAG);\
+        resume_bcn;\
+    }\
+    else{\
+        if( gFLAGS & BAE_SW_EN_FLAG ){\
+            resume_bcn;\
+        }\
+    }\
     gFLAGS = gFLAGS & (~COM_PA_HOT_FLAG);\
+    gFLAGS = gFLAGS & (~COM_PA_OC_FLAG);\
     gCOM_PA_COOLING_TIMEOUT.detach();\
     gFLAGS = gFLAGS & (~COM_TX_FLAG);\
-    /*gPC.puts("ompleted com_power_off tx\r\n");*/\
+    gFLAGS = gFLAGS & (~COM_SESSION_TIMEOUT_FLAG);\
+    /*gPC.puts("completed com_power_off tx\r\n");*/\
 }
\ No newline at end of file
--- a/COM_POWER_ON_TX.h	Mon Jul 04 23:13:24 2016 +0000
+++ b/COM_POWER_ON_TX.h	Tue Jul 05 11:27:29 2016 +0000
@@ -1,107 +1,444 @@
-#define COM_POWER_ON_TX {\
-    /*gPC.puts("Inside COM_POWER_ON_TX\r\n");*/\
-    if( !(gFLAGS & COM_TX_FLAG) ){\
-        gFLAGS = gFLAGS | COM_TX_FLAG;\
-        /*WARNING: INFINITE WHILE LOOP POSSIBLE: if standby ack received and bcn tx main status = 0*/\
-        bool retryFlag = true;\
-        while( retryFlag == true ){\
-            uint8_t standbyAck = 0xFF;\
-            /*PENDING: SET BEACON TO STANDBY*/\
-            if( standbyAck == 0xFF ){\
-                /*ACK RECCEIVED*/\
-                /*PENDING: BEACON TX MAIN STATUS*/\
-                if( true ){\
-                    retryFlag = false;\
-                    /*PENDING: SWITCH RELAY TO COM_TX*/\
-                    /*PENDING: RF_SW_STATUS = RF_COM_TX*/\
-                }\
-                else{\
-                    gCOM_MNG_TMTC_THREAD->wait(5000);\
-                }\
-            }\
-            else{\
-                /*PENDING: RESET BAE*/\
-                uint8_t standbyAck2 = 0x00;\
-                /*PENDING: SET BEACON TO STANDBY*/\
-                if( standbyAck2 == 0xFF ){\
-                    /*PENDING: BEACON TX MAIN STAUTS*/\
-                    if( true ){\
-                        retryFlag = false;\
-                        /*PENDING: SWITCH RF RELAY TO COM TX*/\
-                        /*PENDING: RF_SW_STATUS = RF_COM_TX*/\
-                    }\
-                }\
-                else{\
-                    retryFlag = false;\
-                    /*PENDING: POWER OFF BEACON*/\
-                    /*PENDING: CLEAR BAE SW_EN*/\
-                    /*PENDING: SWITCH RF RELAY TO COM TX*/\
-                    /*PENDING: RF_SW_STATUS = RF_COM_TX*/\
-                }\
-            }\
+#define STANDBY_PRCS(tm_ptr){\
+    Base_tc *stdby_tc = new Long_tc;\
+    stdby_tc->next_TC = NULL;\
+    PUTshort_or_long(stdby_tc,LONG_TC_CODE);\
+    PUTcrc_pass(stdby_tc,0x1);\
+    PUTexec_status(stdby_tc,0);\
+    stdby_tc->TC_string[0] = 0x01;\
+    stdby_tc->TC_string[1] = 0x41;\
+    stdby_tc->TC_string[2] = 0x81;\
+    stdby_tc->TC_string[3] = 0x40;\
+    stdby_tc->TC_string[4] = 0x01;\
+    stdby_tc->TC_string[5] = 0x01;\
+    stdby_tc->TC_string[6] = 0x01;\
+    stdby_tc->TC_string[7] = 0;\
+    stdby_tc->TC_string[8] = 0;\
+    uint16_t crc16 = crc16_gen(stdby_tc->TC_string, 9);\
+    stdby_tc->TC_string[9]  = (crc16 & 0xFF00)>>8;\
+    stdby_tc->TC_string[10] = (crc16 & 0x00FF);\
+    for(int i = 11; i < 135; i++){\
+        stdby_tc->TC_string[i] = 0;\
+    }\
+    tm_ptr =  FCTN_CDMS_RLY_TMTC(stdby_tc);\
+    delete stdby_tc;\
+}
+
+#define SET_BCN_STANDBY(tm_ptr){\
+    Base_tc *beacon_tc = new Long_tc;\
+    beacon_tc->next_TC = NULL;\
+    PUTshort_or_long(beacon_tc,LONG_TC_CODE);\
+    PUTcrc_pass(beacon_tc,0x1);\
+    PUTexec_status(beacon_tc,0);\
+    beacon_tc->TC_string[0] = 0x01;\
+    beacon_tc->TC_string[1] = BCN_APID_SOURCE;\
+    beacon_tc->TC_string[2] = BCN_SERVICE;\
+    beacon_tc->TC_string[3] = 0xE2;\
+    beacon_tc->TC_string[4] = 0x01;\
+    beacon_tc->TC_string[5] = 0;\
+    beacon_tc->TC_string[6] = 0;\
+    beacon_tc->TC_string[7] = 0;\
+    beacon_tc->TC_string[8] = 0;\
+    uint16_t crc16 = crc16_gen(beacon_tc->TC_string, 9);\
+    beacon_tc->TC_string[9]  = (crc16 & 0xFF00)>>8;\
+    beacon_tc->TC_string[10] = (crc16 & 0x00FF);\
+    for(int i = 11; i < 135; i++){\
+        beacon_tc->TC_string[i] = 0;\
+    }\
+    tm_ptr =  FCTN_CDMS_RLY_TMTC(beacon_tc);\
+    delete beacon_tc;\
+}
+
+#define reset_bae {\
+    Base_tc *reset_BAE = new Short_tc;\
+    reset_BAE->next_TC = NULL;\
+    PUTshort_or_long(reset_BAE,SHORT_TC_CODE);\
+    PUTcrc_pass(reset_BAE,0x1);\
+    PUTexec_status(reset_BAE,0);\
+    reset_BAE->TC_string[0] = 0x01;\
+    reset_BAE->TC_string[1] = BAE_APID_SOURCE;\
+    reset_BAE->TC_string[2] = BAE_SERVICE;\
+    reset_BAE->TC_string[3] = BAE_RESET_PID;\
+    reset_BAE->TC_string[4] = 0;\
+    reset_BAE->TC_string[5] = 0;\
+    reset_BAE->TC_string[6] = 0;\
+    reset_BAE->TC_string[7] = 0;\
+    reset_BAE->TC_string[8] = 0;\
+    uint16_t crc16 = crc16_gen(reset_BAE->TC_string, 9);\
+    reset_BAE->TC_string[9]  = (crc16 & 0xFF00)>>8;\
+    reset_BAE->TC_string[10] = (crc16 & 0x00FF);\
+    Base_tm *tm_ptr = NULL;\
+    tm_ptr =  FCTN_CDMS_RLY_TMTC(reset_BAE);\
+    delete reset_BAE;\
+    /*DELETE THE TM AFTER USE*/\
+    Base_tm *del_tm = tm_ptr;\
+    int overCount = 0;\
+    while( del_tm != NULL ){\
+        if( (overCount < TM_OVERFLOW_CONSTANT) ){\
+            Base_tm *temp = del_tm->next_TM;\
+            delete del_tm;\
+            del_tm = temp;\
+            ++overCount;\
+        }\
+        else{\
+            RESET_CDMS;\
+            break;\
         }\
     }\
-    /*PENDING: POWER ON COM_TX*/\
-    uint8_t adfConfigPass = 0xFF;\
-    for( int i = 0 ; i < COM_TX_CONFIG_LIMIT ; ++i ){\
-        /*PENDING: CONFIGURE ADF*/\
-        if( adfConfigPass == 0xFF ){\
+}
+
+#define power_off_bae {\
+    Base_tc *power_off_BAE = new Short_tc;\
+    power_off_BAE->next_TC = NULL;\
+    PUTshort_or_long(power_off_BAE,SHORT_TC_CODE);\
+    PUTcrc_pass(power_off_BAE,0x1);\
+    PUTexec_status(power_off_BAE,0);\
+    power_off_BAE->TC_string[0] = 0x01;\
+    power_off_BAE->TC_string[1] = BAE_APID_SOURCE;\
+    power_off_BAE->TC_string[2] = BAE_SERVICE;\
+    power_off_BAE->TC_string[3] = BAE_POWER_OFF_PID;\
+    power_off_BAE->TC_string[4] = 0;\
+    power_off_BAE->TC_string[5] = 0;\
+    power_off_BAE->TC_string[6] = 0;\
+    power_off_BAE->TC_string[7] = 0;\
+    power_off_BAE->TC_string[8] = 0;\
+    uint16_t crc16 = crc16_gen(power_off_BAE->TC_string, 9);\
+    power_off_BAE->TC_string[9]  = (crc16 & 0xFF00)>>8;\
+    power_off_BAE->TC_string[10] = (crc16 & 0x00FF);\
+    Base_tm *tm_ptr = NULL;\
+    tm_ptr = FCTN_CDMS_RLY_TMTC(power_off_BAE);\
+    delete power_off_BAE;\
+    /*DELETE THE TM AFTER USE*/\
+    Base_tm *del_tm = tm_ptr;\
+    int overCount = 0;\
+    while( del_tm != NULL ){\
+        if( (overCount < TM_OVERFLOW_CONSTANT) ){\
+            Base_tm *temp = del_tm->next_TM;\
+            delete del_tm;\
+            del_tm = temp;\
+            ++overCount;\
+        }\
+        else{\
+            RESET_CDMS;\
+            break;\
+        }\
+    }\
+}
+
+#define P_CDMS_HK_MAIN {\
+    Base_tc *hk_main_ptr = new Short_tc;\
+    hk_main_ptr->next_TC = NULL;\
+    PUTshort_or_long(hk_main_ptr,SHORT_TC_CODE);\
+    PUTcrc_pass(hk_main_ptr,0x1);\
+    PUTexec_status(hk_main_ptr,0);\
+    hk_main_ptr->TC_string[0] = 0x01;\
+    hk_main_ptr->TC_string[1] = 0x81;\
+    hk_main_ptr->TC_string[2] = 0x81;\
+    hk_main_ptr->TC_string[3] = 0x04;\
+    hk_main_ptr->TC_string[4] = 0;\
+    hk_main_ptr->TC_string[5] = 0;\
+    hk_main_ptr->TC_string[6] = 0;\
+    hk_main_ptr->TC_string[7] = 0;\
+    hk_main_ptr->TC_string[8] = 0;\
+    uint16_t crc16 = crc16_gen(hk_main_ptr->TC_string, 9);\
+    hk_main_ptr->TC_string[9]  = (crc16 & 0xFF00)>>8;\
+    hk_main_ptr->TC_string[10] = (crc16 & 0x00FF);\
+    Base_tm *tm_ptr = NULL;\
+    tm_ptr = FCTN_CDMS_RLY_TMTC(hk_main_ptr);\
+    delete hk_main_ptr;\
+    /*DELETE THE TM AFTER USE*/\
+    Base_tm *del_tm = tm_ptr;\
+    int overCount = 0;\
+    while( del_tm != NULL ){\
+        if( (overCount < TM_OVERFLOW_CONSTANT) ){\
+            Base_tm *temp = del_tm->next_TM;\
+            delete del_tm;\
+            del_tm = temp;\
+            ++overCount;\
+        }\
+        else{\
+            RESET_CDMS;\
             break;\
         }\
     }\
-    if( adfConfigPass == 0xFF ){\
-        /*adf successfully configured*/\
-        /*CALL SIGN*/\
-        Base_tm *power_on_tm = NULL;\
-        Base_tm *power_on_tm_head = NULL;\
-        get_call_sign( power_on_tm_head );\
-        power_on_tm = power_on_tm_head;\
-        /*ACK L1*/\
-        get_ack_l1(power_on_tm->next_TM);\
-        int overFlowCountADF = 0;\
-        while( power_on_tm->next_TM != NULL ){\
-            if( overFlowCountADF < TM_OVERFLOW_CONSTANT ){\
-                power_on_tm = power_on_tm->next_TM;\
-                ++overFlowCountADF;\
-            }\
-            else{\
-                /*PENDING: RESET CDMS*/\
-                break;\
-            }\
-        }\
-        get_tc_list(power_on_tm->next_TM, 0x00);\
-        /*PENDING: TRANSMIT ABOVE TM*/\
-        /*Call Sign, ACK_L1, TC_LIST*/\
-        /*snd_tm.head_pointer(power_on_tm_head);*/\
-        /*transmit_adf;*/\
-        /*deleting the telemetry*/\
-        power_on_tm = power_on_tm_head;\
-        overFlowCountADF = 0;\
-        while( power_on_tm != NULL ){\
-            if( overFlowCountADF < TM_OVERFLOW_CONSTANT ){\
-                Base_tm *temp = power_on_tm->next_TM;\
-                delete power_on_tm;\
-                power_on_tm = temp;\
-                ++overFlowCountADF;\
-            }\
-            else{\
-                /*PENDING: RESET CDMS*/\
-                break;\
-            }\
-        }\
+}
+
+#define P_BCN_TX_MAIN(tm_ptr) {\
+    Base_tc *bcn_tx_main_ptr = new Long_tc;\
+    bcn_tx_main_ptr->next_TC = NULL;\
+    PUTshort_or_long(bcn_tx_main_ptr,LONG_TC_CODE);\
+    PUTcrc_pass(bcn_tx_main_ptr,0x1);\
+    PUTexec_status(bcn_tx_main_ptr,0);\
+    bcn_tx_main_ptr->TC_string[0] = 0x01;\
+    bcn_tx_main_ptr->TC_string[1] = 0x41;\
+    bcn_tx_main_ptr->TC_string[2] = 0x81;\
+    bcn_tx_main_ptr->TC_string[3] = 0x07;\
+    bcn_tx_main_ptr->TC_string[4] = 0;\
+    bcn_tx_main_ptr->TC_string[5] = 0;\
+    bcn_tx_main_ptr->TC_string[6] = 0;\
+    bcn_tx_main_ptr->TC_string[7] = 0;\
+    bcn_tx_main_ptr->TC_string[8] = 0;\
+    uint16_t crc16 = crc16_gen(bcn_tx_main_ptr->TC_string, 9);\
+    bcn_tx_main_ptr->TC_string[9]  = (crc16 & 0xFF00)>>8;\
+    bcn_tx_main_ptr->TC_string[10] = (crc16 & 0x00FF);\
+    for(int i = 11; i < 135; i++){\
+        bcn_tx_main_ptr->TC_string[i] = 0;\
     }\
-    else{\
-        /*PENDING: COM_POWER OFF TX*/\
-        /*PENDING: CDMS HK MAIN*/\
-        uint8_t ackReceived = 0x00;\
-        /*PENDING: BCN TX MAIN*/\
-        if( ackReceived == 0xFF ){\
-            gCOM_MNG_TMTC_THREAD->wait(5000);\
-            /*PENDING: RESET CDMS*/\
-        }\
-        else{\
-            /*PENDING: RESET CDMS*/\
-        }\
-    }\
-    /*gPC.puts("COMPLETED COM_POWER_ON_TX\r\n");*/\
+    tm_ptr =  FCTN_CDMS_RLY_TMTC(bcn_tx_main_ptr);\
+    delete bcn_tx_main_ptr;\
+}
+
+void COM_POWER_ON_TX() {
+    if (DEBUG)
+        gPC.puts("Inside COM_POWER_ON_TX\r\n");
+    if( !(gFLAGS & COM_TX_FLAG) ){
+        gFLAGS = gFLAGS | COM_TX_FLAG;
+        if( gFLAGS & BAE_SW_EN_FLAG ){
+            /*WARNING: INFINITE WHILE LOOP POSSIBLE: if standby ack received and bcn tx main status = 0*/
+            bool retryFlag = true;
+            while( retryFlag == true ){
+                Base_tm *tm_ptr = NULL;
+                SET_BCN_STANDBY(tm_ptr);
+                uint8_t bcn_main_status = (tm_ptr->TM_string[2] & 0xEF);
+                if(( bcn_main_status == 0xC0 )||( bcn_main_status ==0xA0 )){
+                    /*ACK RECCEIVED*/
+                    if( bcn_main_status == 0xA0 ){
+                        retryFlag = false;
+                        RF_SW_CNTRL_TX = 1;
+                        Thread::wait(25);
+                        RF_SW_CNTRL_TX = 0;
+                        gFLAGS = gFLAGS & (~RF_SW_STATUS_FLAG);/*RF_SW_STATUS_FLAG set to RF_COM_TX*/
+                    }
+                    else{
+                        Thread::wait(5000);
+                    }
+                }
+                else{
+                    reset_bae;
+                    Base_tm *tm_ptr2 = NULL;
+                    SET_BCN_STANDBY(tm_ptr2);
+                    uint8_t standbyAck2 = 0;
+                    uint8_t bcn_main_status2 = (tm_ptr2->TM_string[2] & 0xEF);
+                    if( ( bcn_main_status2 == 0xC0 )||( bcn_main_status2 == 0xA0 ) ){
+                        if( bcn_main_status2 == 0xA0 ){
+                            retryFlag = false;
+                            RF_SW_CNTRL_TX = 1;
+                            Thread::wait(25);
+                            RF_SW_CNTRL_TX = 0;
+                            gFLAGS = gFLAGS & (~RF_SW_STATUS_FLAG);
+                        }
+                        else{
+                            Thread::wait(5000);
+                        }                           
+                    }
+                    else{
+                        retryFlag = false;
+                        power_off_bae;
+                        gFLAGS = gFLAGS | COM_AUTO_POWER_OFF_BAE_FLAG;
+                        gFLAGS = gFLAGS & (~BAE_SW_EN_FLAG);
+                        RF_SW_CNTRL_TX = 1;
+                        Thread::wait(25);
+                        RF_SW_CNTRL_TX = 0;
+                        gFLAGS = gFLAGS & (~RF_SW_STATUS_FLAG);/*RF_SW_STATUS_FLAG set to RF_COM_TX*/
+                    }
+                    /*DELETE THE TM AFTER USE*/
+                    Base_tm *del_tm = tm_ptr2;
+                    int overCount = 0;
+                    while( del_tm != NULL ){
+                        if( (overCount < TM_OVERFLOW_CONSTANT) ){
+                            Base_tm *temp = del_tm->next_TM;
+                            delete del_tm;
+                            del_tm = temp;
+                            ++overCount;
+                        }
+                        else{
+                            RESET_CDMS;
+                            break;
+                        }
+                    }
+                }
+                /*DELETE THE TM AFTER USE*/
+                Base_tm *del_tm = tm_ptr;
+                int overCount = 0;
+                while( del_tm != NULL ){
+                    if( (overCount < TM_OVERFLOW_CONSTANT) ){
+                        Base_tm *temp = del_tm->next_TM;
+                        delete del_tm;
+                        del_tm = temp;
+                        ++overCount;
+                    }
+                    else{
+                        RESET_CDMS;
+                        break;
+                    }
+                }
+            }
+        }
+        else{
+            RF_SW_CNTRL_TX = 1;
+            Thread::wait(25);
+            RF_SW_CNTRL_TX = 0;
+            gFLAGS = gFLAGS & (~RF_SW_STATUS_FLAG);/*RF_SW_STATUS_FLAG set to RF_COM_TX*/
+        }
+    }
+    COM_TX_CNTRL = 1;
+    uint8_t adfConfigPass = 0xFF;
+    for( int i = 0 ; i < COM_TX_CONFIG_LIMIT ; ++i ){
+        /*PENDING: CONFIGURE ADF and set adfConfigPass*/
+        if( adfConfigPass == 0xFF ){
+            break;
+        }
+    }
+    if( adfConfigPass == 0xFF ){
+        /*adf successfully configured*/
+        gFLAGS = gFLAGS | COM_TX_STATUS_FLAG;
+        /*CALL SIGN*/
+        Base_tm *power_on_tm = NULL;
+        Base_tm *power_on_tm_head = NULL;
+        get_call_sign( power_on_tm_head );
+        power_on_tm = power_on_tm_head;
+        /*ACK L1*/
+        get_ack_l1(power_on_tm->next_TM);
+        int overFlowCountADF = 0;
+        while( power_on_tm->next_TM != NULL ){
+            if( overFlowCountADF < TM_OVERFLOW_CONSTANT ){
+                power_on_tm = power_on_tm->next_TM;
+                ++overFlowCountADF;
+            }
+            else{
+                RESET_CDMS;
+                break;
+            }
+        }
+        get_tc_list(power_on_tm->next_TM, 0x00);
+        /*Call Sign, ACK_L1, TC_LIST*/
+        /*snd_tm.head_pointer(power_on_tm_head);*/
+        /*transmit_adf;*/
+        uint8_t transmissionPass = 0xFF;
+        /*PENDING: get acknowledgement of transmission*/
+        /*deleting the telemetry*/
+        power_on_tm = power_on_tm_head;
+        overFlowCountADF = 0;
+        while( power_on_tm != NULL ){
+            if( overFlowCountADF < TM_OVERFLOW_CONSTANT ){
+                Base_tm *temp = power_on_tm->next_TM;
+                delete power_on_tm;
+                power_on_tm = temp;
+                ++overFlowCountADF;
+            }
+            else{
+                RESET_CDMS;
+                break;
+            }
+        }
+        if( transmissionPass == 0 ){
+            /*NOT Transmitted succesfully*/
+            COM_POWER_OFF_TX;
+            P_CDMS_HK_MAIN;
+            Base_tm *ptr_tm = NULL;
+            STANDBY_PRCS(ptr_tm);
+            uint8_t standbyACK = 0x00;
+            uint8_t bcn_main_status = (ptr_tm->TM_string[2] & 0xEF);
+            if(( bcn_main_status == 0xC0 )||( bcn_main_status ==0xA0 )){
+                standbyACK = 1;
+            }
+            if(standbyACK == 1){
+                uint8_t ackReceived = 0x00;
+                Base_tm *tm_ptr = NULL;
+                P_BCN_TX_MAIN(tm_ptr);
+                uint8_t ackcode = tm_ptr->TM_string[2] & 0xEF;
+                if( (ackcode == 0xA0) || (ackcode == 0xC0))
+                    ackReceived = 0xFF;
+                /*DELETE THE TM AFTER USE*/
+                Base_tm *del_tm = tm_ptr;
+                int overCount = 0;
+                while( del_tm != NULL ){
+                    if( (overCount < TM_OVERFLOW_CONSTANT) ){
+                        Base_tm *temp = del_tm->next_TM;
+                        delete del_tm;
+                        del_tm = temp;
+                        ++overCount;
+                    }
+                    else{
+                        RESET_CDMS;
+                        break;
+                    }
+                }
+                if( ackReceived == 0xFF ){
+                    Thread::wait(5000);
+                }
+            }
+            /*DELETE THE TM AFTER USE*/
+            Base_tm *del_tm = ptr_tm;
+            int overCount = 0;
+            while( del_tm != NULL ){
+                if( (overCount < TM_OVERFLOW_CONSTANT) ){
+                    Base_tm *temp = del_tm->next_TM;
+                    delete del_tm;
+                    del_tm = temp;
+                    ++overCount;
+                }
+                else{
+                    RESET_CDMS;
+                    break;
+                }
+            }
+            RESET_CDMS;            
+        }
+    }
+    else{
+        /*ADF not configured*/
+        gFLAGS = gFLAGS & (~COM_TX_STATUS_FLAG);
+        COM_POWER_OFF_TX;
+        P_CDMS_HK_MAIN;
+        Base_tm *ptr_tm = NULL;
+        STANDBY_PRCS(ptr_tm);
+        uint8_t standbyACK = 0x00;
+        uint8_t bcn_main_status = (ptr_tm->TM_string[2] & 0xEF);
+        if(( bcn_main_status == 0xC0 )||( bcn_main_status ==0xA0 )){
+            standbyACK = 1;
+        }
+        if(standbyACK == 1){
+            uint8_t ackReceived = 0x00;
+            Base_tm *tm_ptr = NULL;
+            P_BCN_TX_MAIN(tm_ptr);
+            uint8_t ackcode = tm_ptr->TM_string[2] & 0xEF;
+            if( (ackcode == 0xA0) || (ackcode == 0xC0))
+                ackReceived = 0xFF;
+            /*DELETE THE TM AFTER USE*/
+            Base_tm *del_tm = tm_ptr;
+            int overCount = 0;
+            while( del_tm != NULL ){
+                if( (overCount < TM_OVERFLOW_CONSTANT) ){
+                    Base_tm *temp = del_tm->next_TM;
+                    delete del_tm;
+                    del_tm = temp;
+                    ++overCount;
+                }
+                else{
+                    RESET_CDMS;
+                    break;
+                }
+            }
+            if( ackReceived == 0xFF ){
+                Thread::wait(5000);
+            }
+        }
+        /*DELETE THE TM AFTER USE*/
+        Base_tm *del_tm = ptr_tm;
+        int overCount = 0;
+        while( del_tm != NULL ){
+            if( (overCount < TM_OVERFLOW_CONSTANT) ){
+                Base_tm *temp = del_tm->next_TM;
+                delete del_tm;
+                del_tm = temp;
+                ++overCount;
+            }
+            else{
+                RESET_CDMS;
+                break;
+            }
+        }
+        RESET_CDMS;
+    }
+    /*gPC.puts("COMPLETED COM_POWER_ON_TX\r\n");*/
 }
\ No newline at end of file
--- a/COM_RCV_TC.h	Mon Jul 04 23:13:24 2016 +0000
+++ b/COM_RCV_TC.h	Tue Jul 05 11:27:29 2016 +0000
@@ -261,5 +261,4 @@
     gRX_COUNT = 0;
     
     /*gPC.puts("Completed conversion of raw_data\r\n")*/;
-    // PENDING: SORT THE LINKED LIST ACCORDING TO PSC VALUE
 }
\ No newline at end of file
--- a/DefinitionsAndGlobals.h	Mon Jul 04 23:13:24 2016 +0000
+++ b/DefinitionsAndGlobals.h	Tue Jul 05 11:27:29 2016 +0000
@@ -36,10 +36,9 @@
 
 // TC LIST
     #define TCL_STATE_INCOMPLETE 0x00
-    #define TCL_STATE_ABORTED 0x03
-    #define TCL_STATE_EXECUTING 0x04   
-    #define TCL_STATE_COMPLETED 0x05
-    #define TCL_STATE_EXCEEDED_LIMIT 0x06
+    #define TCL_STATE_ABORTED 0x01
+    #define TCL_STATE_EXECUTING 0x02   
+    #define TCL_STATE_COMPLETED 0x03
 
 // LIST OF FLAGS
     #define UART_INT_FLAG 0x0001
@@ -50,6 +49,7 @@
     #define COM_SESSION_VALIDITY 0x0020
     #define ALL_CRC_PASS_FLAG 0x0040
     #define COM_PA_HOT_FLAG 0x0080
+    #define COM_PA_OC_FLAG 0x8000
     #define COM_TX_FLAG 0x0100
     #define COM_SESSION_TIMEOUT_FLAG 0x0200
     #define COM_AUTO_POWER_OFF_BAE_FLAG 0x0400
@@ -64,13 +64,14 @@
 
 // COM_MNG_TMTC THREAD
     #define SESSION_TIME_LIMIT 1500
+    #define COM_RX_DISABLE_TIMEOUT 10
     #define COM_MNG_TMTC_SIGNAL_UART_INT 0x01
     #define COM_MNG_TMTC_SIGNAL_ADF_NSD 0x02
     #define COM_MNG_TMTC_SIGNAL_ADF_SD 0x03
     #define SCIENCE_SIGNAL 0x04
 // COM_MNG_TMTC
     #define COM_PA_COOLING_TIME_LIMIT 20
-    #define COM_MAX_TC_LIMIT 200
+    #define COM_MAX_TC_LIMIT 256
     #define TM_ACK_CODE_INDEX 2
     #define CRC_FAIL_NACK_CODE 0x01
     
@@ -81,6 +82,7 @@
     // max value of telecommands in a tcl
     #define TCL_OVERFLOW_CONSTANT 256
     #define TM_OVERFLOW_CONSTANT 256
+    #define BYTE_OVERFLOW_CONSTANT 35000
 
     // starting value of packet sequence count at each pass 
     #define PSC_START_VALUE 1
@@ -164,6 +166,7 @@
 COM_RX_DATA_NODE *gRX_CURRENT_DATA_NODE = NULL;
 // uint8_t *gRX_CURRENT_PTR = NULL;
 uint32_t gRX_COUNT = 0;
+uint16_t gTOTAL_RAW_BYTES = 0;
 uint16_t gTOTAL_INCORRECT_SIZE_TC = 0x00;
 uint16_t gTOTAL_CRC_FAIL_TC = 0x00;
 uint16_t gTOTAL_REPEATED_TC = 0x00;
@@ -196,6 +199,7 @@
 Thread* gCOM_MNG_TMTC_THREAD = NULL;
 Timeout gRX_TIMEOUT;
 Timeout gSESSION_TIMEOUT;
+Timeout gCOM_RX_DISABLE;
 
 // COM_MNG_TMTC
 
--- a/P_COM_INIT.h	Mon Jul 04 23:13:24 2016 +0000
+++ b/P_COM_INIT.h	Tue Jul 05 11:27:29 2016 +0000
@@ -4,7 +4,7 @@
     Thread::wait(25);\
     RF_SW_CNTRL_BCN = 0;\
     gFLAGS = gFLAGS | RF_SW_STATUS_FLAG;\
-    //COM_RX_CNTRL = 0;\
+    COM_RX_CNTRL = 0;\
     Thread::wait(200);\
     COM_RX_CNTRL = 1;\
     gFLAGS = gFLAGS & (~COM_INIT_STATUS_FLAG);\
--- a/ThreadsAndFunctions.h	Mon Jul 04 23:13:24 2016 +0000
+++ b/ThreadsAndFunctions.h	Tue Jul 05 11:27:29 2016 +0000
@@ -36,6 +36,13 @@
     gCOM_MNG_TMTC_THREAD->signal_set(COM_MNG_TMTC_SIGNAL_UART_INT);
 }
 
+// COM_RX_DISABLE ISR
+void after_com_disable(){
+    gCOM_RX_DISABLE.detach();
+    RX1M.attach(&rx_read, Serial::RxIrq);
+}
+
+
 // RX_TIMEOUT ISR
 void after_receive(){
     gRX_TIMEOUT.detach();
@@ -63,15 +70,15 @@
     }\
     gHEAD_NODE_TCL = NULL;\
     gLAST_NODE_TCL = NULL;\
-    gMASTER_STATE = TCL_STATE_INCOMPLETE;\
+    gMASTER_STATE = TCL_STATE_COMPLETED;\
     gTOTAL_INCORRECT_SIZE_TC = 0;\
     gTOTAL_CRC_FAIL_TC = 0;\
     gTOTAL_REPEATED_TC = 0;\
     gTOTAL_VALID_TC = 0;\
     for(int i = 0 ; i < TM_LONG_SIZE ; ++i){\
-        gLAST_TM[i] = 0x00;\
+        /*gLAST_TM[i] = 0x00;*/\
     }\
-    gLAST_TM_SHORT_OR_LONG = SHORT_TM_CODE;\
+    /*gLAST_TM_SHORT_OR_LONG = SHORT_TM_CODE;*/\
     COM_RX_DATA_NODE *dataptr = gRX_HEAD_DATA_NODE;\
     while( dataptr != NULL ){\
         COM_RX_DATA_NODE *temp = dataptr->next_node;\
@@ -82,6 +89,7 @@
     gRX_HEAD_DATA_NODE->next_node = NULL;\
     gRX_CURRENT_DATA_NODE = gRX_HEAD_DATA_NODE;\
     gRX_COUNT = 0;\
+    gTOTAL_RAW_BYTES = 0;\
     /*PENDING : ALL GLOBAL VAARIABLES AND FLAGS*/\
     /*gPC.puts("finished reset all\r\n");*/\
 }
@@ -96,6 +104,7 @@
         gRX_CURRENT_DATA_NODE = gRX_CURRENT_DATA_NODE->next_node;\
         gRX_CURRENT_DATA_NODE->next_node = NULL;\
     }\
+    ++gTOTAL_RAW_BYTES;\
     gRX_TIMEOUT.attach(&after_receive, RX_TIMEOUT_LIMIT);\
 }
 
@@ -114,6 +123,7 @@
                 gFLAGS = gFLAGS | COM_SESSION_FLAG;
                 gSESSION_TIMEOUT.attach(&after_session, SESSION_TIME_LIMIT);
                 gFLAGS = gFLAGS | COM_RX_FLAG;
+                gTOTAL_RAW_BYTES = 0;
                 PUT_RAW_BYTE;
                 // PENDING : MEASURE RSSI
                 if( gFLAGS & COM_SESSION_FLAG ){
@@ -132,10 +142,15 @@
                 gFLAGS = gFLAGS | COM_RX_FLAG;
                 PUT_RAW_BYTE;
             }
+            if(gTOTAL_RAW_BYTES > BYTE_OVERFLOW_CONSTANT){
+                RX1M.attach(NULL);
+                gCOM_RX_DISABLE.attach(&after_com_disable, COM_RX_DISABLE_TIMEOUT);
+                after_receive();
+            }
         }
         else if( gFLAGS & NEW_TC_RECEIVED ){
             gPC.puts("NEW TC RECEIVED\r\n");
-            Thread::wait(1000);
+            Thread::wait(1000);/*For testing with transmitter. To be removed later*/
             gFLAGS = gFLAGS & (~NEW_TC_RECEIVED);
             gFLAGS = gFLAGS | COM_MNG_TMTC_RUNNING_FLAG;
 
@@ -168,25 +183,20 @@
                             /*gPC.puts("GS code match !!\r\n");*/
                             gFLAGS = gFLAGS | COM_SESSION_VALIDITY;
                             COM_POWER_ON_TX;
-                            // PENDING: PA HOT HANDLED IN EXECUTE_XXX FUNCTIONS
-                            uint8_t tempPAHot = 0x00;
-                            isPAhot(tempPAHot);
-                            if( tempPAHot == 0xFF ){
-                                gFLAGS = gFLAGS & ~(COM_PA_HOT_FLAG);
+                            P_COM_HK;
+                            if( (gFLAGS & COM_PA_HOT_FLAG) || ( gFLAGS & COM_PA_OC_FLAG ) ){
+                                COM_TX_CNTRL = 0;
+                                gCOM_PA_COOLING_TIMEOUT.attach(&after_cooling_pa, COM_PA_COOLING_TIME_LIMIT);
+                                gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);
+                                RX1M.attach(&rx_read, Serial::RxIrq);
+                            }
+                            else{
                                 gCOM_PA_COOLING_TIMEOUT.detach();
                                 gPC.puts("EXECUTING TELECOMMANDS\r\n");
-//                                DEBUF_PRINT("EXECUTING TELECOMMANDS\r\n");
                                 EXECUTE_OBOSC_ONLY;
                                 EXECUTE_TC;
                                 gPC.puts("COMPLETED EXECUTION\r\n");
                             }
-                            else{
-                                gFLAGS = gFLAGS | COM_PA_HOT_FLAG;
-                                // PENDING: power off transmitter only
-                                gCOM_PA_COOLING_TIMEOUT.attach(&after_cooling_pa, COM_PA_COOLING_TIME_LIMIT);
-                                gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);
-                                RX1M.attach(&rx_read, Serial::RxIrq);
-                            }
                         }
                         else{
                             /*gPC.puts("GS code mismatch !!\r\n");*/
@@ -198,6 +208,7 @@
                             gSESSION_TIMEOUT.detach();
                             gFLAGS = gFLAGS & (~COM_SESSION_FLAG);
                             // WARNING: clear COM_MNG_TMTC ?
+                            gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);
                         }
                     }
                     else{
@@ -229,6 +240,7 @@
                         //gPC.puts("\r\n");
                         COM_POWER_ON_TX;
                         // PENDING : POWER OFF TX
+                        COM_TX_CNTRL = 0;
                         RX1M.attach(&rx_read, Serial::RxIrq);
                         gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);
                     }
@@ -237,6 +249,9 @@
                     COM_POWER_ON_TX;
                     // POWER OFF TX transmitter only
                     // WARNING: reset_all ? clear com_session ?
+                    COM_TX_CNTRL = 0;
+                    reset_all;
+                    gFLAGS = gFLAGS & (~COM_SESSION_FLAG);
                     RX1M.attach(&rx_read, Serial::RxIrq);
                     gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);
                 }
@@ -254,29 +269,17 @@
         }
         else if( gFLAGS & COM_PA_HOT_FLAG ){
             /*gPC.puts("checking for PA hot in main\r\n");*/
-            if( gFLAGS & COM_RX_FLAG ){
-                uint8_t tempPA = 0xFF;
-                isPAhot(tempPA);
-                if( tempPA == 0x00 ){
-                    gCOM_PA_COOLING_TIMEOUT.attach(&after_cooling_pa, COM_PA_COOLING_TIME_LIMIT);
-                }
-                else{
-                    gFLAGS = gFLAGS & ~(COM_PA_HOT_FLAG);
-                    gCOM_PA_COOLING_TIMEOUT.detach();
-                }
+            P_COM_HK;
+            if((gFLAGS & COM_PA_HOT_FLAG) || (gFLAGS & COM_PA_OC_FLAG)){
+                COM_TX_CNTRL = 0;
+                gCOM_PA_COOLING_TIMEOUT.attach(&after_cooling_pa, COM_PA_COOLING_TIME_LIMIT);
             }
             else{
-                uint8_t tempPA = 0xFF;
-                isPAhot(tempPA);
-                if( tempPA == 0x00 ){
-                    gCOM_PA_COOLING_TIMEOUT.attach(&after_cooling_pa, COM_PA_COOLING_TIME_LIMIT);
-                }
-                else{
+                gCOM_PA_COOLING_TIMEOUT.detach();
+                if( !(gFLAGS & COM_RX_FLAG) ){
                     /*gPC.puts("PA IS COOLED DOWN\r\n");*/
-                    gFLAGS = gFLAGS & (~COM_PA_HOT_FLAG);
                     gFLAGS = gFLAGS | COM_MNG_TMTC_RUNNING_FLAG;
                     RX1M.attach(NULL);
-                    gCOM_PA_COOLING_TIMEOUT.detach();
                     COM_POWER_ON_TX;
                     EXECUTE_OBOSC_ONLY;
                     EXECUTE_TC;
@@ -287,7 +290,7 @@
             /*gPC.puts("session timeout: resetting in main\r\n");*/
             COM_POWER_OFF_TX;
             /*PENDING : ENABLE THREADS*/
-            
+            // gCDMS_HK_TIMER.start(5000);
             gSESSION_TIMEOUT.detach();
             gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);
             gFLAGS = gFLAGS & (~COM_SESSION_FLAG);
--- a/common_functions.h	Mon Jul 04 23:13:24 2016 +0000
+++ b/common_functions.h	Tue Jul 05 11:27:29 2016 +0000
@@ -77,7 +77,7 @@
                     }\
                 }\
                 else{\
-                    /*PENDING: reset cdms*/\
+                    RESET_CDMS;\
                     break;\
                 }\
             }\
@@ -99,18 +99,24 @@
             /*Attach this new node at the end*/\
         }\
         else{\
-            /*PENDING: reset CDMS*/\
+            RESET_CDMS;\
             break;\
         }\
     }\
 }
 
 #define put_crc_l1_ack(tm_ptr) {\
-    tm_ptr->TM_string[2] = gTOTAL_CRC_FAIL_TC & 0xFF;\
     /*TC LIST STATUS*/\
-    tm_ptr->TM_string[3] |= (gMASTER_STATE << 5) & 0xE0;\
+    tm_ptr->TM_string[3] |= (gMASTER_STATE << 6) & 0xC0;\
     /*PA HOT*/\
     if( gFLAGS & COM_PA_HOT_FLAG ){\
+        tm_ptr->TM_string[3] |= (1 << 5);\
+    }\
+    else{\
+        tm_ptr->TM_string[3] &= ~(1 << 5);\
+    }\
+    /*PA OC*/\
+    if( gFLAGS & COM_PA_OC_FLAG ){\
         tm_ptr->TM_string[3] |= (1 << 4);\
     }\
     else{\
@@ -124,16 +130,39 @@
 #define fill_l1_ack(tm_ptr) {\
     tm_ptr->next_TM = NULL;\
     tm_ptr->TM_string[0] = TMID_ACK_L1 << 4;\
-    /*PENDING: PA TEMPERATURE*/\
     tm_ptr->TM_string[1] = gTOTAL_INCORRECT_SIZE_TC & 0xFF;\
     tm_ptr->TM_string[2] = gTOTAL_CRC_FAIL_TC & 0xFF;\
 }
 
+#define P_COM_HK{\
+    uint8_t returnHere;\
+    isPAhot(returnHere);\
+    if (returnHere == 0xFF)\
+        gFLAGS = gFLAGS & ~(COM_PA_HOT_FLAG);\
+    else\
+        gFLAGS = gFLAGS | COM_PA_HOT_FLAG;\
+    if (gFLAGS & COM_PA_OC_FLAG){\
+        COM_TX_CNTRL = 1;\
+        wait_ms(5);\
+    }\
+    isPAoc(returnHere);\
+    if (returnHere == 0xFF)\
+        gFLAGS = gFLAGS & ~(COM_PA_OC_FLAG);\
+    else\
+        gFLAGS = gFLAGS | COM_PA_OC_FLAG;\
+    /*P_CDMS_HANDLE_HW_FAULTS*/\
+}
+
 #define isPAhot(returnHere){\
     /*PENDING : COMPLETE THIS FUNCTION*/\
     returnHere = 0xFF;\
 }
 
+#define isPAoc(returnHere){\
+    /*PENDING : COMPLETE THIS FUNCTION*/\
+    returnHere = 0xFF;\
+}
+
 #define get_call_sign(tm_ptr) {\
     Base_tm* call_sign_tm = new Short_tm;\
     call_sign_tm->next_TM = NULL;\
@@ -156,7 +185,6 @@
             /*IF CRC PASS*/\
             if( (GETcrc_pass(current_TC) == 1) ){\
                 if(TC_count > 4){\
-                    /*PENDING: FILL TC_EXEC_CODE, APPEND CRC TO THE TM*/\
                     put_crc_l1_ack( l1_ack );\
                     /*extend the TM linked list*/\
                     TC_count = 0;\
@@ -189,7 +217,7 @@
             ++overflowCountL1;\
         }\
         else{\
-            /*PENDING: RESET CDMS*/\
+            RESET_CDMS;\
             break;\
         }\
     }\
--- a/main.cpp	Mon Jul 04 23:13:24 2016 +0000
+++ b/main.cpp	Tue Jul 05 11:27:29 2016 +0000
@@ -3,14 +3,14 @@
 
 
 #include "mbed.h"
-
+#define RESET_CDMS NVIC_SystemReset()
 #define DEBUG 1
 
 #include "SimpleDMA.h"
 #include "dmaSPIslave.h"
 #include "rtos.h"
 #include "mbed_debug.h"
-
+void rx_read();
 #include "Structures.h"
 #include "pinconfig.h"
 #include "DefinitionsAndGlobals.h"
@@ -30,8 +30,8 @@
 #include "FMS_all.h"
 #include "CDMS_PL.h"
 #include "COM_MNG_TMTC.h"
+#include "COM_POWER_OFF_TX.h"
 #include "COM_POWER_ON_TX.h"
-#include "COM_POWER_OFF_TX.h"
 #include "Compression.h"
 #include "ThreadsAndFunctions.h"
 #include "TEST_PL.h"