Dummy program to demonstrate problems: working code

Dependencies:   SLCD mbed-rtos mbed

Fork of MNG_TC by Shreesha S

Revision:
7:e71ecfe3a340
Parent:
6:6e9ae3b44e60
Child:
8:cb93c1d3209a
--- a/MNG_TC.h	Sat Jul 04 13:22:07 2015 +0000
+++ b/MNG_TC.h	Mon Jul 06 05:00:29 2015 +0000
@@ -1,6 +1,9 @@
+// 3 July
+// Dheeraj detect_nack()
+// Sukhdeep SND_TM
+
 //Jun 8
 
-
 //Jun 7 
 //PROBLEM IN DELETING TC_string pointer not solved
 
@@ -37,203 +40,234 @@
 // TMID list
 #define TMID_ACK_L1 10
 
-namespace MNG_TC
+class MNG_TC
 {
-
-    int total_valid_TC = 0;
-    TC_list *TC_list_header;
-    bool all_crc_pass = true;
-    bool no_missing_TC = true;
-    bool stop_after_current_TC = false;
-    bool execute_high_priority_TC = false;
+private:
+    int total_valid_TC;
+    bool all_crc_pass;
+    bool no_missing_TC;
+    bool stop_after_current_TC;
+    bool execute_high_priority_TC;
+    TC_list *Head_TC;
 
 
 //SECONDARY FUNCTIONS : [SHOULD NOT BE CALLED INDEPENDENTLY]
 //USED BY MEMBER FUNCTIONS FOUND BELOW
 
-    namespace MEMORY_MANAGEMENT{
-
-    //PROBLEM IN DELETING TC_string POINTER
-        void delete_TC(TC_list *tc_ptr, TC_list* previous_tc){
-            // stitch the previous and next node
-            TC_list *temp_n = tc_ptr->next_TC;
-            
-            if( (previous_tc != NULL) && (temp_n != NULL) ){
-                previous_tc->next_TC = temp_n;
-            }
-            else if( (previous_tc == NULL) && (temp_n != NULL) ){
-                // delete head node HENCE UPDATE HEADER
-                TC_list_header = temp_n;
-            }
-            else if( (previous_tc != NULL) && (temp_n == NULL) ){
-                // delete last node
-                previous_tc->next_TC = NULL;
-            }
-            else{
-                // delete the only single node present
-                // in which case head is the only node
-                TC_list_header = NULL;
-            }
+//PROBLEM IN DELETING TC_string POINTER
+    void delete_TC(TC_list *tc_ptr, TC_list* previous_tc){
+        // stitch the previous and next node
+        TC_list *temp_n = tc_ptr->next_TC;
+        
+        if( (previous_tc != NULL) && (temp_n != NULL) ){
+            previous_tc->next_TC = temp_n;
+        }
+        else if( (previous_tc == NULL) && (temp_n != NULL) ){
+            // delete head node HENCE UPDATE HEADER
+            Head_TC = temp_n;
+        }
+        else if( (previous_tc != NULL) && (temp_n == NULL) ){
+            // delete last node
+            previous_tc->next_TC = NULL;
+        }
+        else{
+            // delete the only single node present
+            // in which case head is the only node
+            Head_TC = NULL;
+        }
 
 //             delete the node
-            delete tc_ptr;
-        }        
+        delete tc_ptr;
+    }        
+    
+    void generate_L1_ack_TM(TM_list *tm_ptr){
+        tm_ptr->next_TM = NULL;
+        tm_ptr->TM_string = new unsigned char[TM_SHORT_SIZE];
+        // TMID
+        tm_ptr->tmid = 0xA;
+        tm_ptr->TM_string[0] = 0xaf;
     }
     
-    namespace ACK{
-    
-        void generate_L1_ack_TM(TM_list *tm_ptr){
-            tm_ptr->next_TM = NULL;
-            tm_ptr->TM_string = new unsigned char[TM_SHORT_SIZE];
-            // TMID
-            tm_ptr->tmid = 0xA;
-            tm_ptr->TM_string[0] = 0xaf;
-        }
+    void execode_crc(bool all_pass, TM_list *tm_ptr){
+        
+        tm_ptr->TM_string[1] = ( all_crc_pass == true ) ? 0x01 : 0x00 ;
         
-        void execode_crc(bool all_pass, TM_list *tm_ptr){
-            
-            tm_ptr->TM_string[1] = ( all_crc_pass == true ) ? 0x01 : 0x00 ;
-            
-            uint16_t crc_checksum = CRC::crc16_gen(tm_ptr->TM_string, TM_SHORT_SIZE-2);
+        uint16_t crc_checksum = CRC::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;
-        }
+        tm_ptr->TM_string[TM_SHORT_SIZE-2] = (crc_checksum >> 8) & 0xff;
+        tm_ptr->TM_string[TM_SHORT_SIZE-1] = crc_checksum & 0xff;
     }
     
-    namespace EXECUTION{
-
-        TM_list* Manage_CDMS(TC_list *ptr_tc){
-            
+    TM_list* respond_CDMS(TC_list *ptr_tc){
+        
 //            DUMMY PROGRAM TO CREATE A SAMPLE TM
 
 //            allocate memory for new tm list
-            TM_list *test_TM = new TM_list;
-            
-            test_TM->next_TM = NULL;
-            
+        TM_list *test_TM = new TM_list;
+        
+        test_TM->next_TM = NULL;
+        
 //            allocate memory for the tm string
-            unsigned char *str = new unsigned char[TM_TYPE1_SIZE];
-            
-            
+        unsigned char *str = new unsigned char[TM_TYPE1_SIZE];
+        
+        
 //            frame type-1 is 0. [ (0 << 7) = 0 ]
-            str[0] = 0;
-            
+        str[0] = 0;
+        
 //            the tmid determines type-1, or 2
 //            tmid : 0x1 belongs to type1 (dummy program)
-            test_TM->tmid = 0x1;
+        test_TM->tmid = 0x1;
+
+        // 4 bit TMID
+        str[0] += (0x1) << 3;
+        
+        // 20 bit seq. count
+        str[0] += 0x7;
+        str[1] = 0xff;
+        str[2] = 0xff;
 
-            // 4 bit TMID
-            str[0] += (0x1) << 3;
-            
-            // 20 bit seq. count
-            str[0] += 0x7;
-            str[1] = 0xff;
-            str[2] = 0xff;
+        // random data
+        for(int i = 3 ; i < (TM_TYPE1_SIZE-2) ; ++i ){
+            str[i] = 'a';
+        }
+        
+//            APPEND CRC : ALL THE PROCESSES HAVE TO GENERATE AND APPEND CRC
+        uint16_t crc_checksum = CRC::crc16_gen(str, TM_TYPE1_SIZE-2);
+        str[TM_TYPE1_SIZE-2] = (crc_checksum >> 8) & 0xff;
+        str[TM_TYPE1_SIZE-1] = crc_checksum & 0xff;
+
+        test_TM->TM_string = str;
 
-            // random data
-            for(int i = 3 ; i < (TM_TYPE1_SIZE-2) ; ++i ){
-                str[i] = 'a';
-            }
+        return test_TM;
+    }
+    
+    TM_list* RELAY(TC_list *tc_ptr){
+    
+//            FIRST send long or short tc
+        unsigned char tc_len = ( tc_ptr->short_or_long ? TC_SHORT_SIZE : TC_LONG_SIZE );
+        
+        if( tc_ptr->apid == 0x01 ){
+            // send TC to BAE microcontroler
             
-//            APPEND CRC : ALL THE PROCESSES HAVE TO GENERATE AND APPEND CRC
-            uint16_t crc_checksum = CRC::crc16_gen(str, TM_TYPE1_SIZE-2);
-            str[TM_TYPE1_SIZE-2] = (crc_checksum >> 8) & 0xff;
-            str[TM_TYPE1_SIZE-1] = crc_checksum & 0xff;
-
-            test_TM->TM_string = str;
-
-            return test_TM;
+        }
+        else{
+            // send TC to SPEED microcontroller
+            
+        }
+        
+        PC.putc( tc_len );
+//            THE TARGET SHOULD READ 'n' MORE BYTES AS FOUND IN THE FIRST BYTE
+        
+//            SEND THE TC TO THE TARGET DEVICE [ALONG WITH CRC]
+        for(unsigned int i = 0 ; i < tc_len ; ++i){
+            PC.putc( tc_ptr->TC_string[i] );
         }
         
-        TM_list* RELAY(TC_list *tc_ptr){
+//            WAIT FOR THE TM
+        TM_list* tm_head = new TM_list;
+        TM_list* tm_ptr = tm_head;
         
-//            FIRST send long or short tc
-            unsigned char tc_len = ( tc_ptr->short_or_long ? TC_SHORT_SIZE : TC_LONG_SIZE );
-            PC.putc( tc_len );
-//            THE TARGET SHOULD READ 'n' MORE BYTES AS FOUND IN THE FIRST BYTE
+//            FIRST RECEIVE NUMBER OF TM'S TO BE RECEVIVED
+         unsigned char tm_num = PC.getc();
+         for(unsigned int i = 0 ; i < tm_num ; ++i){
             
-//            SEND THE TC TO THE TARGET DEVICE [ALONG WITH CRC]
-            for(unsigned int i = 0 ; i < tc_len ; ++i){
-                PC.putc( tc_ptr->TC_string[i] );
+//                THEN FOR EACH TM FIRST SEND TYPE-1 OR TYPE-2 i.e. NUMBER OF BYTES TO READ 
+            unsigned char tm_len = PC.getc();
+            tm_ptr->TM_string = new unsigned char[tm_len];
+            for(unsigned int j = 0 ; j < tm_len ; ++j){
+                tm_ptr->TM_string[j] = PC.getc();
+            }
+            
+//                DECODE TMID FROM THE PACKET
+            if(tm_len == 134){
+                unsigned char temp = tm_ptr->TM_string[0];
+                tm_ptr->tmid = (temp >> 3) & 0xf;
+            }
+            else{
+                unsigned char temp = tm_ptr->TM_string[4];
+                tm_ptr->tmid = (temp >> 4) & 0xf;
             }
             
-//            WAIT FOR THE TM
-            TM_list* tm_head = new TM_list;
-            TM_list* tm_ptr = tm_head;
+//                ALLOCATE MEMORY FOR NEXT TM PACKET
+            if( i == tm_num-1 ){
+                tm_ptr->next_TM = NULL;
+            }
+            else{
+                tm_ptr->next_TM = new TM_list;
+                tm_ptr = tm_ptr->next_TM;
+            }
+         }
+         
+//            FILL IN THE tm_ptr AND RETURN
+        return tm_head;
+    }
+    
+    
+    /*
+    @brief:     detect ack_l234 : first tm for any tc will be the ack_l234
+    @param:     tm_ptr : pointer to the telemetry head
+    @return:    true : ack
+                false : nack
+    */
+    bool detect_ackl234(TM_list *tm_ptr){
+        char temp = tm_ptr->TM_string[0];
+        
+        // 
+        if( (temp  & 0xF0) == 0xB0 ){
+            temp = tm_ptr->TM_string[3];
             
-//            FIRST RECEIVE NUMBER OF TM'S TO BE RECEVIVED
-             unsigned char tm_num = PC.getc();
-             for(unsigned int i = 0 ; i < tm_num ; ++i){
-                
-//                THEN FOR EACH TM FIRST SEND TYPE-1 OR TYPE-2 i.e. NUMBER OF BYTES TO READ 
-                unsigned char tm_len = PC.getc();
-                tm_ptr->TM_string = new unsigned char[tm_len];
-                for(unsigned int j = 0 ; j < tm_len ; ++j){
-                    tm_ptr->TM_string[j] = PC.getc();
-                }
-                
-//                DECODE TMID FROM THE PACKET
-                if(tm_len == 134){
-                    unsigned char temp = tm_ptr->TM_string[0];
-                    tm_ptr->tmid = (temp >> 3) & 0xf;
-                }
-                else{
-                    unsigned char temp = tm_ptr->TM_string[4];
-                    tm_ptr->tmid = (temp >> 4) & 0xf;
-                }
-                
-//                ALLOCATE MEMORY FOR NEXT TM PACKET
-                if( i == tm_num-1 ){
-                    tm_ptr->next_TM = NULL;
-                }
-                else{
-                    tm_ptr->next_TM = new TM_list;
-                    tm_ptr = tm_ptr->next_TM;
-                }
-             }
-             
-//            FILL IN THE tm_ptr AND RETURN
-            return tm_head;
+            // the nack and ack code
+            if(temp != 0x00){
+                return true;
+            }
+            else{
+                return false;
+            }
         }
-        
+        else{
+            return false;
+        }
     }
-
-//    MEMBER FUNCTIONS
-
+    
+    bool sdCardOp(TC_list *tc_ptr){
+        return false;
+    }
+        
+public:
+    
+// Constructor
 /*
 @brief:     INITIALISE THE HEAD NODE AND RESET THE VARIABLES
 @param:     TC_list *head : head node pointer
 @return:    none
 */
-    void init(TC_list *head){
+    MNG_TC( TC_list *HEAD ){
 //        printf("inside init\r\n");
         total_valid_TC = 0;
-        TC_list_header = head;
         all_crc_pass = true;
         no_missing_TC = true;
         stop_after_current_TC = false;
         execute_high_priority_TC = false;
+        Head_TC = HEAD;
     }
 
-
 /*
 @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
+@return:    1 if all tc are crc pass
+            0 if atleast one crc fail
 */
     int start_with(){
 //        printf("inside start with\r\n");
-        TC_list *current_TC = TC_list_header;
-        TC_list *previous_TC = NULL;
+        TC_list *current_TC = Head_TC;
         
         total_valid_TC = 0;
         all_crc_pass = true;
 
-        TM_List *l1_ack = new TM_List;
-        TM_List *l1_ack_head = l1_ack;
-        ACK::generate_L1_ack_TM(l1_ack);
+        TM_list *l1_ack = new TM_list;
+        TM_list *l1_ack_head = l1_ack;
+        generate_L1_ack_TM(l1_ack);
         
         int TC_count = 0;
         
@@ -255,7 +289,6 @@
                 l1_ack->TM_string[2] = temp;
 
 //                 advance to the next node
-                previous_TC = current_TC;
                 current_TC = current_TC->next_TC;
             }
 //             if crc fail
@@ -265,46 +298,43 @@
                 temp &= ~( 1 << (7-TC_count) );
                 l1_ack->TM_string[2] = temp;
 
-//                 delete and advance to the next node since CRC FAIL
-                TC_list *next = current_TC->next_TC;
-                MEMORY_MANAGEMENT::delete_TC(current_TC, previous_TC);
-                current_TC = next;
+                current_TC = current_TC->next_TC;
                 all_crc_pass = false;
             }
+            
             ++TC_count;
 
 //             extend the TM linked list if TC_count > 7
             if(TC_count > 7){
                 TC_count = 0;
 
-                l1_ack->next_TM = new TM_List;
+                l1_ack->next_TM = new TM_list;
                 l1_ack = l1_ack->next_TM;
-                ACK::generate_L1_ack_TM(l1_ack);
+                generate_L1_ack_TM(l1_ack);
                 
 //                FILL TC_EXEC_CODE
 //                APPEND CRC TO THE TM
-                ACK::execode_crc( all_crc_pass, l1_ack );
+                execode_crc( all_crc_pass, l1_ack );
             }
         }
         
-//        FILL UP THE REMAINING FIELDS WITH ZEROS
+//        FILL UP THE REMAINING FIELDS WITH 0xFF since 0x00 causes problems with GS software
         while(TC_count < 8){
             l1_ack->TM_string[2] &= ~( 1 << (7-TC_count) );
-//            l1_ack->TM_string[3+TC_count] = 0;
-            l1_ack->TM_string[3+TC_count] = 0x01;
+            l1_ack->TM_string[3+TC_count] = 0xFF;
             ++TC_count;
         }
         
 //        FILL TC_EXEC_CODE
 //        APPEND CRC TO THE TM
-        ACK::execode_crc(all_crc_pass, l1_ack);        
+        execode_crc(all_crc_pass, l1_ack);
 
-        SND_TM(l1_ack);
+        SND_TM(l1_ack_head);
         
         // delete the TM
         l1_ack = l1_ack_head;
         while(l1_ack != NULL){
-            TM_List *temp = l1_ack->next_TM;
+            TM_list *temp = l1_ack->next_TM;
             delete l1_ack;
             l1_ack = temp;
         }
@@ -315,15 +345,37 @@
 
         return 1;
     }
+    
+/*
+@brief:     Run either start_with() or update_valid_tc()
+@param:     none
+@return:    int 1 for all crc pass
+            int 0 if atleast one crc fail
+*/
+    void update_valid_TC(){
+        TC_list *current_TC = Head_TC;
+        
+        total_valid_TC = 0;
+        all_crc_pass = true;
+        
+        while(current_TC != NULL){
+            if( current_TC->crc_pass ){
+                ++total_valid_TC;
+            }
+            else{
+                all_crc_pass = false;
+            }
+            current_TC = current_TC->next_TC;
+        }
+    }
 
 /*
-AT THIS STAGE ALL NODES ARE CRC PASS
 @brief:     decode the TCs and fill in the values in the tc-node. 
 @param:     none
 @return:    none
 */
     void decode_TC(){
-        TC_list *node_ptr = TC_list_header;
+        TC_list *node_ptr = Head_TC;
 
         while( node_ptr != NULL ){
 
@@ -344,12 +396,19 @@
         }
     }
 
-    int check_for_missing_TC(void){
+/*
+@brief:     check for missing tc, run start_with() or update_valid_tc() before
+@param:     none
+@return:    int 1 no missing tc
+            int 0 missing tc
+*/
+
+    int check_for_missing_TC(){
         no_missing_TC = true;
         
         for(unsigned char psc = PSC_START_VALUE ; psc < (total_valid_TC + PSC_START_VALUE) ; ++psc){
             bool flag = false;
-            TC_list *node_ptr = TC_list_header;
+            TC_list *node_ptr = Head_TC;
             
             while(node_ptr != NULL){
                 if(node_ptr->packet_seq_count == psc){
@@ -389,38 +448,98 @@
         
     //    EXECUTE ACCORDING TO THE PSC VALUE
         while( psc < (total_valid_TC+PSC_START_VALUE) ){
-            TC_list *tc_ptr = TC_list_header;
+            
+            TC_list *tc_ptr = Head_TC;
             
     //        FIND THE TC CORRESPONDING TO THE PSC VALUE
             while(tc_ptr != NULL){
-                if( tc_ptr->packet_seq_count == psc ){
+                if( (tc_ptr->packet_seq_count == psc) && (tc_ptr->crc_pass) ){
     //                THE TC WITH THE REQUIRED PSC HAS BEEN FOUND, NOW EXECUTE
-
-                    TM_List *tm_ptr;
-                    if( tc_ptr->apid == APID_CDMS ){
-    //                    IF THE TC BELONGS TO THE CDMS uc CALL THE LOCAL FUNCTION Manage_CDMS()
-                        tm_ptr = EXECUTION::Manage_CDMS(tc_ptr);
+                    
+                    if( !sdCardOp(tc_ptr) ){
+                        TM_list *tm_ptr;
+                        if( tc_ptr->apid == APID_CDMS ){
+        //                    IF THE TC BELONGS TO THE CDMS uc CALL THE LOCAL FUNCTION Manage_CDMS()
+                            tm_ptr = respond_CDMS(tc_ptr);
+                        }
+                        else{
+        //                IF THE TC BELONGS TO OTHER MODULE, RELAY() IS CALLED WHICH ACTS AS A MESSENGER
+                            tm_ptr = RELAY(tc_ptr);
+                        }
+                        
+                        // detect nack
+                        if( detect_ackl234(tm_ptr) ){
+                            tc_ptr->valid_execution = true;
+                        }
+                        else if( tc_ptr->abort_on_nack ){
+                            tc_ptr->valid_execution = false;
+                            // abort on nack is true and nack received : end thread
+                            Thread::wait(osWaitForever);
+                        }
+                        else{
+                            tc_ptr->valid_execution = false;
+                        }
+                        
+        //                SEND DATA TO GS
+                        SND_TM(tm_ptr);
+                        
+        //                DELETE THE TM AFTER USE
+                        while(tm_ptr != NULL){
+                            TM_list *temp = tm_ptr->next_TM;
+                            delete tm_ptr;
+                            tm_ptr = temp;
+                        }
+                        
                     }
-                    
                     else{
-    //                IF THE TC BELONGS TO OTHER MODULE, RELAY() IS CALLED WHICH ACTS AS A MESSENGER
-                        tm_ptr = EXECUTION::RELAY(tc_ptr);
+                        /*DO SD CARD OPERATION*/
                     }
                     
-    //                SEND DATA TO GS
-                    SND_TM(tm_ptr);
-                    
-                    tc_ptr->valid_execution = true;
-                    
-    //                DELETE THE TM AFTER USE
-                    delete tm_ptr;
-                    
     //                THE TC WITH APPROPRIATE PSC IS EXECUTED, START OVER
                     break;
                 }
                 
                 tc_ptr = tc_ptr->next_TC;
             }
-        } 
+        }
+        
+        // executed every tc : delete all
+        TC_list *tc_ptr = Head_TC;
+        while(tc_ptr != NULL){
+            TC_list *temp = tc_ptr->next_TC;
+            delete tc_ptr;
+            tc_ptr = temp;
+        }
     }
-}
+    
+    void execute_urgent(){
+        
+        TC_list *tc_ptr = Head_TC;
+        while( tc_ptr != NULL ){
+            
+        }
+        
+        // executed every tc : delete all
+        tc_ptr = Head_TC;
+        while(tc_ptr != NULL){
+            TC_list *temp = tc_ptr->next_TC;
+            delete tc_ptr;
+            tc_ptr = temp;
+        }
+    }
+};
+
+void MNG_MAIN(void const *args){
+    
+    RCV_TC RcvClass( VAR_SPACE::Head_node1 );
+    MNG_TC manager( VAR_SPACE::Head_node1 );
+    
+    if( manager.start_with() ){
+        manager.decode_TC();
+        if( manager.check_for_missing_TC() ){
+            manager.execute_TC();
+        }
+    }
+    
+    Thread::wait(osWaitForever);
+}
\ No newline at end of file