Dummy program to demonstrate problems: working code

Dependencies:   SLCD mbed-rtos mbed

Fork of MNG_TC by Shreesha S

Revision:
2:994e741028c7
Parent:
1:df31097c8442
Child:
3:eec1097c0dd6
--- a/MNG_TC.h	Sat Jun 06 04:27:40 2015 +0000
+++ b/MNG_TC.h	Thu Jun 11 11:20:12 2015 +0000
@@ -1,3 +1,13 @@
+//Jun 8
+
+
+//Jun 7 
+//PROBLEM IN DELETING TC_string pointer not solved
+
+// Jun 6
+// WHAT IS TC exec code in L1 ack ? 
+// Removed class and introduced namespace
+
 // Apil 15
 //added back printf statements
 //added back delete TC_string for debugging
@@ -5,7 +15,8 @@
 // add number of tm packets while calling snd 
 // function overloading z
 
-#include "SND_TM.h"
+
+Serial PC(USBTX, USBRX);
 
 // starting value of packet sequence count at each pass 
 #define PSC_START_VALUE 1
@@ -22,138 +33,216 @@
 #define HPTC2 6
 // Add more entries above
 
-// size of short TM without CRC - in bytes
-#define TM_SHORT_SIZE 14
-#define TM_LONG_SIZE 131
+// SIZE of tc in bytes
+#define TC_SHORT_SIZE 11
+#define TC_LONG_SIZE 135
+
+// size of TM - in bytes
+#define TM_SHORT_SIZE 13
+#define TM_TYPE1_SIZE 134
 
 // TMID list
 #define TMID_ACK_L1 10
 
-typedef struct TC_list{
-    // received from the RCV_TC
-    unsigned char *TC_string;
-    bool short_or_long; //'true' for short
-    bool crc_pass;
-
-    // updated info - updated in MNG_TC
-    unsigned char packet_seq_count;
-    unsigned char apid;
-    bool abort_on_nack;
-    bool enabled;
-    bool valid_execution;
-
-    struct TC_list *next_TC;
-}TC_list;
+#include "Structures.h"
+#include "SND_TM.h"
 
 //typedef struct TM_list{
 //
-//    unsigned char *TM_string;
-//    // bool short_or_long; // true for short
-//    // pass while calling the function
-//    unsigned char tmid;
+//   unsigned char *TM_string;
+////    bool short_or_long; // true for short
+////NOT REQUIRED SINCE A TMID IS EITHER SHORT OR LONG
+//   unsigned char tmid;
 //
-//    struct TM_list *next_TM;
+//   struct TM_list *next_TM;
 //}TM_List;
 
-class MNG_TC
+namespace MNG_TC
 {
-private:
 
-    // private variables and flags
-    int total_valid_TC;
+    int total_valid_TC = 0;
     TC_list *TC_list_header;
-    bool all_crc_pass;
-    bool no_missing_TC;
-    bool stop_after_current_TC;
-    bool execute_high_priority_TC;
+    bool all_crc_pass = true;
+    bool no_missing_TC = true;
+    bool stop_after_current_TC = false;
+    bool execute_high_priority_TC = false;
+
+
+//SECONDARY FUNCTIONS : [SHOULD NOT BE CALLED INDEPENDENTLY]
+//USED BY MEMBER FUNCTIONS FOUND BELOW
+
+    namespace MEMORY_MANAGEMENT{
 
-    // private functions
-    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;
+    //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;
+            }
+
+//             delete the node
+            delete tc_ptr;
+        }        
+    }
+    
+    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;
+        }
         
-        if( (previous_tc != NULL) && (temp_n != NULL) ){
-            previous_tc->next_TC = temp_n;
-        }
-        else if( (previous_tc == NULL) && (temp_n != NULL) ){
-            // delete head node
-            TC_list_header = temp_n;
-        }
-        else if( (previous_tc != NULL) && (temp_n == NULL) ){
-            // delete last node
-            previous_tc->next_TC = NULL;
+        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);
+
+            tm_ptr->TM_string[TM_SHORT_SIZE-2] = (crc_checksum >> 8) & 0xff;
+            tm_ptr->TM_string[TM_SHORT_SIZE-1] = crc_checksum & 0xff;
         }
-        else{
-            // delete the only single node present
-            // in which case head is the only node
-            TC_list_header = NULL;
+    }
+    
+    namespace EXECUTION{
+
+        TM_list* Manage_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;
+            
+//            allocate memory for the tm string
+            unsigned char *str = new unsigned char[TM_TYPE1_SIZE];
+            
+            
+//            frame type-1 is 0. [ (0 << 7) = 0 ]
+            str[0] = 0;
+            
+//            the tmid determines type-1, or 2
+//            tmid : 0x1 belongs to type1 (dummy program)
+            test_TM->tmid = 0x1;
+
+            // 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;
+
+            return test_TM;
         }
-
-        // delete the string
-        delete tc_ptr->TC_string;
-        // delete the node
-        delete tc_ptr;
+        
+        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 );
+            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] );
+            }
+            
+//            WAIT FOR THE TM
+            TM_list* tm_head = new TM_list;
+            TM_list* tm_ptr = tm_head;
+            
+//            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;
+        }
+        
     }
 
-    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->TM_string[0] = 0xaf;
-        tm_ptr->TM_string[1] = 0xff;
-        tm_ptr->TM_string[2] = 0xff;
-        tm_ptr->TM_string[3] = 0xff;
-    }
-    
-    TM_list* manage_process(TC_list *ptr_tc){
-        TM_list *test_TM = new TM_list;
-        test_TM->next_TM = NULL;
-        unsigned char *str = new unsigned char[TM_LONG_SIZE];
-        //ping the TC
-        test_TM->tmid = 0xa;
+//    MEMBER FUNCTIONS
 
-        // 4 bit TMID
-        str[0] = (0xa) << 4;
-        
-        // 20 bit seq. count
-        str[0] += 0xf;
-        str[1] = 0xff;
-        str[2] = 0xff;
-
-        // return data and append zeroes
-        for(int i = 3 ; i < 9 ; ++i ){
-            str[i] = ptr_tc->TC_string[i];
-        }
-        for(int i = 9 ; i < TM_LONG_SIZE ; ++i){
-            str[i] = 0;
-        }
-
-        test_TM->TM_string = str;
-
-        return test_TM;
-    }
-    
-    bool detect_nack(TM_list *ptr_tm){
-        bool bluff = false;
-        return bluff;
-    }
-
-public:
-    // Constructor
-    MNG_TC(TC_list *head){
+/*
+@brief:     INITIALISE THE HEAD NODE AND RESET THE VARIABLES
+@param:     TC_list *head : head node pointer
+@return:    none
+*/
+    void init(TC_list *head){
         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;
-    };
+    }
+
 
-    // delete the crc failed TC from the list to free-up memory
-    // and update the total valid TC
-    // and GENERATE L1_ACK_TM
-    void TC_list_cleanup(){
-//        printf("Inside cleanup\r\n");
+/*
+@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
+*/
+    int start_with(){
         TC_list *current_TC = TC_list_header;
         TC_list *previous_TC = NULL;
         
@@ -162,81 +251,95 @@
 
         TM_List *l1_ack = new TM_List;
         TM_List *l1_ack_head = l1_ack;
-        generate_L1_ack_TM(l1_ack);
+        ACK::generate_L1_ack_TM(l1_ack);
         
         int TC_count = 0;
         
-        l1_ack->TM_string[4] = 0x01;
-        l1_ack->TM_string[5] = 0xff;
+        while(current_TC != NULL){
+            
+            unsigned char temp = 0;
+            
+//          FILL PSC of the TC [ don't care whether crc pass or fail ]
+//          PSC starts from 4th byte
+            l1_ack->TM_string[3+TC_count] = current_TC->TC_string[0];
 
-        while(current_TC != NULL){
-//            printf("Inside null\r\n");
-            
-//            printf("TC_count = %u\r\npsc = %u\r\n", TC_count, current_TC->TC_string[0]);
-            // don't care : crc pass or fail
-            l1_ack->TM_string[6+TC_count] = current_TC->TC_string[0];
-
+//          IF CRC PASS
             if( current_TC->crc_pass ){
                 ++total_valid_TC;
 
-                // set the crc pass field
-//                l1_ack->TM_string[2] |= ( 1 << (7-TC_count) );
+//                 set the crc pass field in TC_STATUS ???
+                temp = l1_ack->TM_string[2];
+                temp |= ( 1 << (7-TC_count) );
+                l1_ack->TM_string[2] = temp;
 
-                // advance to the next node
+//                 advance to the next node
                 previous_TC = current_TC;
                 current_TC = current_TC->next_TC;
             }
-            // crc fail
+//             if crc fail
             else{
-                // unset the crc pass field
-//                l1_ack->TM_string[2] &= ~( 1 << (7-TC_count) );
+                // unset the crc pass field in TC_STATUS
+                temp = l1_ack->TM_string[2];
+                temp &= ~( 1 << (7-TC_count) );
+                l1_ack->TM_string[2] = temp;
 
-                // delete and advance to the next node
+//                 delete and advance to the next node since CRC FAIL
                 TC_list *next = current_TC->next_TC;
-//                delete_TC(current_TC, previous_TC);
+                MEMORY_MANAGEMENT::delete_TC(current_TC, previous_TC);
                 current_TC = next;
                 all_crc_pass = false;
             }
             ++TC_count;
 
-            // extend the linked list if TC_count > 7
-//            if(TC_count > 7){
-//                TC_count = 0;
-//
-//                l1_ack->next_TM = new TM_List;
-//                l1_ack = l1_ack->next_TM;
-//                generate_L1_ack_TM(l1_ack);
-//            }
+//             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 = l1_ack->next_TM;
+                ACK::generate_L1_ack_TM(l1_ack);
+                
+//                FILL TC_EXEC_CODE
+//                APPEND CRC TO THE TM
+                ACK::execode_crc( all_crc_pass, l1_ack );
+            }
         }
         
-        for(int i = 0 ; i < 7 ; ++i){
-            l1_ack->TM_string[7+i] = 0x00;
+//        FILL UP THE REMAINING FIELDS WITH ZEROS
+        while(TC_count < 8){
+            l1_ack->TM_string[2] &= ~( 1 << (7-TC_count) );
+            l1_ack->TM_string[3+TC_count] = 0;
+            ++TC_count;
+        }
+        
+//        FILL TC_EXEC_CODE
+//        APPEND CRC TO THE TM
+        ACK::execode_crc(all_crc_pass, l1_ack);        
+
+        SND_TM(l1_ack);
+        
+        // delete the TM
+        l1_ack = l1_ack_head;
+        while(l1_ack != NULL){
+            TM_List *temp = l1_ack->next_TM;
+            delete l1_ack;
+            l1_ack = temp;
         }
 
-//        printf("Sending data\r\n");
-        SND_TM(l1_ack, false, 1);
-//        printf("finished sending data\r\n");
+        if(all_crc_pass == false){
+             return 0;
+        }
 
-        // delete the TM
-//        l1_ack = l1_ack_head;
-//        while(l1_ack != NULL){
-//            TM_List *temp = l1_ack->next_TM;
-////            delete l1_ack->TM_string;
-////            delete l1_ack;
-//            l1_ack = temp;
-//        }
-
-//        if(all_crc_pass == false){
-//            // exit
-//        }
-//        printf("Completed cleanup\r\n");
-//        printf("valid TC = %d\r\n", total_valid_TC);
+        return 1;
     }
 
-    // At this stage ALL NODES ARE CRC PASS
-    // decode the TCs and fill in the values in the tc-node
+/*
+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(){
-        printf("Inside decode tc\r\n");
         TC_list *node_ptr = TC_list_header;
 
         while( node_ptr != NULL ){
@@ -247,9 +350,7 @@
             node_ptr->packet_seq_count = node_ptr->TC_string[0];
             // APID
             temp = node_ptr->TC_string[1];
-            printf("chr for apid is %d\r\n", temp);
             node_ptr->apid = (temp >> 6) & 3;
-            printf("apid = %d\r\n", node_ptr->apid);
             // Abort On Nack
             node_ptr->abort_on_nack = (temp >> 3) & 1;
             // default values of enable and execution
@@ -258,19 +359,17 @@
 
             node_ptr = node_ptr->next_TC;
         }
-        printf("completed decode\r\n");
     }
 
-    // check for missing tc
-    // run TC_list_cleanup() before running this function
-    void check_for_missing_tc(){
-        printf("inside check for missing\r\n");
+    int check_for_missing_TC(void){
         no_missing_TC = true;
-        for(unsigned char psc = PSC_START_VALUE ; psc < (total_valid_TC+PSC_START_VALUE) ; ++psc ) {
+        
+        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;
+            
             while(node_ptr != NULL){
-                if( node_ptr->packet_seq_count == psc ){
+                if(node_ptr->packet_seq_count == psc){
                     flag = true;
                     break;
                 }
@@ -278,122 +377,67 @@
                     node_ptr = node_ptr->next_TC;
                 }
             }
-            if( flag == false ){
+            
+            if(flag == false){
                 no_missing_TC = false;
-                // packet with PSC = psc is missing !!
-                // exit
-                printf("exit\r\n");
+                break;
             }
         }
-        printf("Completed check for missing\r\n");
+        
+        if(no_missing_TC){
+            return 1;
+        }
+        else{
+            return 0;
+        }
     }
 
-
-    // At this stage ALL NODE ARE CRC PASS and NO MISSING PACKETS
-    // function : send TC to the target wait and receive from target
+/*
+RUN start_with() before running execute_TC()
+V1.0
+@brief:     EXECUTE THE LIST OF TCs, WITH A SEQUENCE BASED ON PSC
+            SEND THE TC TO THE TARGET AND WAIT FOR THE TM
+            THEN FORWARD IT TO GS
+@param:     none
+@return:    none
+*/
     void execute_TC(){
-        printf("inside execute tc\r\n");
         unsigned char psc = PSC_START_VALUE;
+        
+    //    EXECUTE ACCORDING TO THE PSC VALUE
         while( psc < (total_valid_TC+PSC_START_VALUE) ){
-            printf("psc = %d\r\n", psc);
-            if(stop_after_current_TC){
-                // exit
-                printf("exit stop after current TC\r\n");
-            }
-            else if(execute_high_priority_TC){
-                printf("Inside hptc\r\n");
-                TC_list *tc_ptr = TC_list_header;
-                while(tc_ptr != NULL){
-                    // high priority TC belong APID_CDMS
+            TC_list *tc_ptr = TC_list_header;
+            
+    //        FIND THE TC CORRESPONDING TO THE PSC VALUE
+            while(tc_ptr != NULL){
+                if( tc_ptr->packet_seq_count == psc ){
+    //                THE TC WITH THE REQUIRED PSC HAS BEEN FOUND, NOW EXECUTE
+
+                    TM_List *tm_ptr;
                     if( tc_ptr->apid == APID_CDMS ){
-                        // setup the priority order in nested if else
-                        unsigned char temp = tc_ptr->TC_string[2];
-                        unsigned char service = ( temp >> 4 ) & 0xf;
-                        TM_List *tm_ptr;
-                        if(service == HPTC1){
-                            tm_ptr = manage_process(tc_ptr); 
-                        }
-                        else if(service == HPTC2){
-                            tm_ptr = manage_process(tc_ptr);
-                        }
-                        // add more entries above
-
-                        // add number of tm packets also while calling snd 
-                        // tc_ptr : SND_TM to GS
-                        // delete tm
-
-                        if( detect_nack(tm_ptr) ){
-                            if(tc_ptr->abort_on_nack){
-                                // exit
-                                printf("exit");
-                            }
-                        }
-                        else{
-                            tc_ptr->valid_execution = true;
-                            // delete the tc string to free up memory
-                            delete tc_ptr->TC_string;
-                        }
+    //                    IF THE TC BELONGS TO THE CDMS uc CALL THE LOCAL FUNCTION Manage_CDMS()
+                        tm_ptr = EXECUTION::Manage_CDMS(tc_ptr);
+                    }
+                    
+                    else{
+    //                IF THE TC BELONGS TO OTHER MODULE, RELAY() IS CALLED WHICH ACTS AS A MESSENGER
+                        tm_ptr = EXECUTION::RELAY(tc_ptr);
                     }
+                    
+    //                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;
                 }
-                execute_high_priority_TC = false;
+                
+                tc_ptr = tc_ptr->next_TC;
             }
-            else{
-                printf("executing normal TC\r\n");
-                TC_list *current_TC = TC_list_header;
-                // find the tc corresponding to the psc
-                while(current_TC != NULL){
-                    printf("while : finding tc with psc : %d\r\n", psc);
-                    if( current_TC->packet_seq_count == psc ){
-                        printf("inside IF\r\n");
-                        TM_List *tm_ptr;
-                        printf("apid = %d\r\n", current_TC->apid);
-                        if(current_TC->apid == APID_CDMS){
-                            printf("CDMS APID found!\r\n");
-                            tm_ptr = manage_process(current_TC);
-                        }
-                        else{
-                            // call RLY_TMTC
-                            // wait and receive for TM
-                            tm_ptr = tm_ptr;
-                        }
-
-                        // add number of tm packets also
-                        printf("sending data\r\n");
-                        SND_TM(tm_ptr, false, 1);
-                        printf("\r\nfinished sending data\r\n");
-
-                        if( detect_nack(tm_ptr) ){
-                            if(current_TC->abort_on_nack){
-                                // exit
-                                printf("exit");
-                            }
-                        }
-                        else{
-                            current_TC->valid_execution = true;
-                            // delete the tc string to free up memory
-                            printf("deleting TC string\r\n");
-//                            delete current_TC->TC_string;
-                            printf("finished deleting TC string\r\n");
-                        }
-
-                        // delete the TM received after sending to GS
-                        printf("deleting TM received\r\n");
-                        delete tm_ptr->TM_string;
-                        delete tm_ptr;
-
-                        // found the tc with psc hence break
-                        break;
-                    }
-
-                    current_TC = current_TC->next_TC;
-                }
-                ++psc;
-            }
-        }
-        printf("completed execute tc\r\n");
+        } 
     }
-
-//    ~MNG_TC();
- 
-    /* data */
-};
\ No newline at end of file
+}
\ No newline at end of file