pl ack in tmtc

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of COM_MNG_TMTC_SIMPLE_pl123 by shubham c

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers COM_MNG_TMTC.h Source File

COM_MNG_TMTC.h

00001 // 8 Jul
00002 // did flowchart of states
00003 
00004 // handle sd card with cdms team
00005 
00006 // Jun 6
00007 // WHAT IS TC exec code in L1 ack ? 
00008 
00009 //Jan 7 
00010 //added RLY_TMTC function
00011 //added included related files
00012 
00013 //#include "Flash.h"
00014 #include "cdms_rtc.h"
00015 Timer t_pl;
00016 //defined in definations and globals, need to mapped with the final ones.
00017 //DigitalIn tm_status_4m_slv(PIN39); //I2C interrupt to CDMS from BAE
00018 //DigitalIn tm_status_4m_pl(PIN61); //I2C interrupt to CDMS from PL 
00019 
00020 uint8_t received = 0;
00021 uint16_t mid1;
00022 uint16_t mid2;
00023 uint16_t Nbytes;
00024 uint32_t Data[2];
00025 uint16_t nbytes;
00026 uint8_t flash_counter = 0;
00027 uint16_t crc16;
00028 uint16_t mid;
00029 uint32_t block;
00030 
00031 #define delete_TC(tc_ptr) {\
00032     if(tc_ptr == gHEAD_NODE_TCL){\
00033         gHEAD_NODE_TCL = tc_ptr->next_TC;\
00034     }\
00035     delete tc_ptr;\
00036 }
00037 
00038 // typeof tm_ptr: Base_tm
00039 // typeof tc_ptr: Base_tc
00040 // typeof temp_xxxx: uint8_t
00041 #define fill_l1_ack(tm_ptr) {\
00042     tm_ptr->next_TM = NULL;\
00043     tm_ptr->TM_string[0] = TMID_ACK_L1 << 4;\
00044     /*PENDING: PA TEMPERATURE*/\
00045     tm_ptr->TM_string[1] = gTOTAL_INCORRECT_SIZE_TC & 0xFF;\
00046     tm_ptr->TM_string[2] = gTOTAL_CRC_FAIL_TC & 0xFF;\
00047 }
00048 
00049 #define put_crc_l1_ack(tm_ptr) {\
00050     tm_ptr->TM_string[2] = gTOTAL_CRC_FAIL_TC & 0xFF;\
00051     /*TC LIST STATUS*/\
00052     tm_ptr->TM_string[3] |= (gMASTER_STATE << 5) & 0xE0;\
00053     /*PA HOT*/\
00054     if( gFLAGS & COM_PA_HOT_FLAG ){\
00055         tm_ptr->TM_string[3] |= (1 << 4);\
00056     }\
00057     else{\
00058         tm_ptr->TM_string[3] &= ~(1 << 4);\
00059     }\
00060     uint16_t crc_checksum = crc16_gen(tm_ptr->TM_string, TM_SHORT_SIZE-2);\
00061     tm_ptr->TM_string[TM_SHORT_SIZE-2] = (crc_checksum >> 8) & 0xFF;\
00062     tm_ptr->TM_string[TM_SHORT_SIZE-1] = crc_checksum & 0xFF;\
00063 }
00064 
00065 #define detect_ack(tm_ptr, temp_ack, tc_psc) {\
00066     if( tm_ptr != NULL ){\
00067         int length = TM_SHORT_SIZE;\
00068         if( GETshort_or_long_tm(tm_ptr) == SHORT_TM_CODE ){\
00069             length = TM_SHORT_SIZE;\
00070         }\
00071         else{\
00072             length = TM_LONG_SIZE;\
00073         }\
00074         uint16_t crc = crc16_gen(tm_ptr->TM_string, length-2);\
00075         if( (((crc & 0xFF00) >> 8) == tm_ptr->TM_string[length-2]) && ( (crc & 0x00FF) == tm_ptr->TM_string[length-1] ) ){\
00076             uint8_t temp8;\
00077             temp8 = tm_ptr->TM_string[TM_ACK_CODE_INDEX];\
00078             temp8 = temp8 & 0xE0;\
00079             if( (temp8 == 0xC0) || (temp8 == 0xA0) )\
00080                 temp_ack = 0x01;\
00081             else{\
00082                 temp_ack = 0x00;\
00083                 /*CHECK FOR NACK CODE: CRC FAIL NACK*/\
00084                 temp8 = tm_ptr->TM_string[TM_ACK_CODE_INDEX];\
00085                 temp8 = temp8 & 0x0F;\
00086                 if( temp8 == CRC_FAIL_NACK_CODE ){\
00087                     tm_ptr->TM_string[2] = tc_psc;\
00088                 }\
00089             }\
00090         }\
00091         else\
00092             temp_ack = 0x00;\
00093     }\
00094     else{\
00095         temp_ack = 0x00;\
00096     }\
00097 }
00098 
00099 #define isit_obosc(tc_ptr, temp_obosc) {\
00100     temp_obosc = 0x00;\
00101     if( GETapid(tc_ptr) == APID_COM ){\
00102         if( ((tc_ptr->TC_string[2]) >> 4) == SERVICE_OBOSC ){\
00103             temp_obosc = 0x01;\
00104         }\
00105     }\
00106 }
00107 
00108 #define isit_sdcard(tc_ptr, temp_sdcard) {\
00109     temp_sdcard = 0x00;\
00110     if( GETapid(tc_ptr) == APID_COM ){\
00111         if( ( (tc_ptr->TC_string[2]) >> 4) == SERVICE_OBSRS ){\
00112             temp_sdcard = 0x01;\
00113         }\
00114     }\
00115 }
00116 
00117 #define isPAhot(returnHere){\
00118     /*PENDING : COMPLETE THIS FUNCTION*/\
00119     returnHere = 0xFF;\
00120 }
00121 
00122 void after_cooling_pa(){
00123     gCOM_MNG_TMTC_THREAD->signal_set(COM_MNG_TMTC_SIGNAL_UART_INT);
00124 }
00125 
00126 /*
00127 @brief:     check for missing tc, also check crc, i.e. 
00128             if true execution can be started else have to wait
00129             decide the next state
00130 @param:     none
00131 @return:    bool indicating whether there are missing tc
00132 */
00133 // PENDING: LAST FRAME BIT
00134 /*
00135 for loop: check for missing tc
00136 if: check for incorrect sized tc
00137 if: check for last frame bit
00138 */
00139 #define continueToExecute(returnHere) {\
00140     uint8_t tempReturn = 0x00;\
00141     /*search for missing psc*/\
00142     for(uint8_t p = 0x00 ; p < (gTOTAL_VALID_TC) ; ++p){\
00143         bool flag = false;\
00144         Base_tc *node_ptr = gHEAD_NODE_TCL;\
00145         while(node_ptr != NULL){\
00146             if( (GETpacket_seq_count(node_ptr) == p) && (GETcrc_pass(node_ptr) == 1) ){\
00147                 flag = true;\
00148                 break;\
00149             }\
00150             else{\
00151                 node_ptr = node_ptr->next_TC;\
00152             }\
00153         }\
00154         if(flag == false){\
00155             tempReturn = 0x02;\
00156             break;\
00157         }\
00158     }\
00159     /*search for last packet bit*/\
00160     tempReturn = tempReturn + 0x01;\
00161     Base_tc *tcp = gHEAD_NODE_TCL;\
00162     while(tcp != NULL){\
00163         if(GETpacket_seq_count(tcp) == (gTOTAL_VALID_TC - 1)){\
00164             if( ( (tcp->TC_string[1]) & 0x20 ) == 0x20 ){\
00165                 tempReturn = tempReturn - 0x01;\
00166             }\
00167             break;\
00168         }\
00169         tcp = tcp->next_TC;\
00170     }\
00171     returnHere = tempReturn;\
00172     /*UPDATE gMASTER STATE*/\
00173     if( (returnHere == 0x00) ){\
00174         if( (gMASTER_STATE == TCL_STATE_INCOMPLETE) || (gMASTER_STATE == TCL_STATE_COMPLETED) || (gMASTER_STATE == TCL_STATE_ABORTED) ){\
00175             gMASTER_STATE = TCL_STATE_EXECUTING;\
00176         }\
00177     }\
00178     else{\
00179         if( (gMASTER_STATE == TCL_STATE_INCOMPLETE) || (gMASTER_STATE == TCL_STATE_COMPLETED) ){\
00180             gMASTER_STATE = TCL_STATE_INCOMPLETE;\
00181         }\
00182     }\
00183 }
00184 
00185 /*
00186 return 1 if code match
00187 return 0 if code mismatch
00188 */
00189 #define GScodeVerification(returnHere){\
00190     Base_tc *testTC = gHEAD_NODE_TCL;\
00191     uint16_t overflowCount = 0;\
00192     returnHere = 0xFF;\
00193     while( (overflowCount < TCL_OVERFLOW_CONSTANT) && (testTC != NULL) ){\
00194         if( (GETpacket_seq_count(testTC) == PSC_CALLSIGN) && (GETapid(testTC) == APID_CALLSIGN) ){\
00195             /*uint8_t temp8 = testTC->TC_string[1];*/\
00196             if( true ){\
00197                 for( int i = 2 ; i <= 8 ; ++i ){\
00198                     if( testTC->TC_string[i] != gGSCODE[i-2] ){\
00199                         returnHere = 0;\
00200                         break;\
00201                     }\
00202                 }\
00203             }\
00204             break;\
00205         }\
00206         testTC = testTC->next_TC;\
00207         ++overflowCount;\
00208     }\
00209 }
00210 
00211 /*
00212 @brief:     DELETE THE CRC FAILED TC FROM THE LIST TO FREE-UP MEMORY AND UPDATE 
00213             THE TOTAL VALID TC AND GENERATE L1_ACK_TM
00214 @param:     none
00215 @return:    none
00216 */
00217 /*tm_ptr is the next_TM of a linked list, and should have the value NULL, i.e. tm_ptr should be the next_TM pointer of thte last node */
00218 #define get_ack_l1( tm_ptr ){\
00219     Base_tc *current_TC = gHEAD_NODE_TCL;\
00220     Base_tm *l1_ack = new Short_tm;\
00221     /*APPEND TO THE tm_ptr HERE*/\
00222     tm_ptr = l1_ack;\
00223     fill_l1_ack(l1_ack);\
00224     int TC_count = 0;\
00225     int overflowCountL1 = 0;\
00226     while(current_TC != NULL){\
00227         if( overflowCountL1 < TCL_OVERFLOW_CONSTANT ){\
00228             /*IF CRC PASS*/\
00229             if( (GETcrc_pass(current_TC) == 1) ){\
00230                 if(TC_count > 4){\
00231                     /*PENDING: FILL TC_EXEC_CODE, APPEND CRC TO THE TM*/\
00232                     put_crc_l1_ack( l1_ack );\
00233                     /*extend the TM linked list*/\
00234                     TC_count = 0;\
00235                     l1_ack->next_TM = new Short_tm;\
00236                     l1_ack = l1_ack->next_TM;\
00237                     fill_l1_ack(l1_ack);\
00238                 }\
00239                 /*PSC starts from 7th byte*/\
00240                 l1_ack->TM_string[6+TC_count] = GETpacket_seq_count(current_TC);\
00241                 /*TC exec status*/\
00242                 switch(TC_count){\
00243                     case 0:\
00244                         l1_ack->TM_string[3] = (GETexec_status(current_TC)) & 0x0F;\
00245                         break;\
00246                     case 1:\
00247                         l1_ack->TM_string[4] = (GETexec_status(current_TC) << 4) & 0xF0;\
00248                         break;\
00249                     case 2:\
00250                         l1_ack->TM_string[4] |= (GETexec_status(current_TC)) & 0x0F;\
00251                         break;\
00252                     case 3:\
00253                         l1_ack->TM_string[5] = (GETexec_status(current_TC) << 4) & 0xF0;\
00254                         break;\
00255                     case 4:\
00256                         l1_ack->TM_string[5] |= (GETexec_status(current_TC)) & 0x0F;\
00257                 }\
00258                 ++TC_count;\
00259             }\
00260             current_TC = current_TC->next_TC;\
00261             ++overflowCountL1;\
00262         }\
00263         else{\
00264             /*PENDING: RESET CDMS*/\
00265             break;\
00266         }\
00267     }\
00268     /*FILL UP THE REMAINING FIELDS WITH 0x00 as PSC, AND 0xF as tc exec status*/\
00269     if( (TC_count < 5) && (TC_count != 0) ){\
00270         while(TC_count < 5){\
00271             l1_ack->TM_string[6+TC_count] = 0x00;\
00272             switch(TC_count){\
00273                 case 1:\
00274                     l1_ack->TM_string[4] = 0xF0;\
00275                     break;\
00276                 case 2:\
00277                     l1_ack->TM_string[4] |= 0x0F;\
00278                     break;\
00279                 case 3:\
00280                     l1_ack->TM_string[5] = 0xF0;\
00281                     break;\
00282                 case 4:\
00283                     l1_ack->TM_string[5] |= 0x0F;\
00284             }\
00285             ++TC_count;\
00286         }\
00287     }\
00288     put_crc_l1_ack(l1_ack);\
00289 }
00290 
00291 /*tm_ptr is the next_TM of a linked list, and should have the value NULL, i.e. tm_ptr should be the next_TM pointer of thte last node */
00292 #define get_tc_list(tm_ptr, thePSC){\
00293     uint8_t TM_PSC = 0xFF;\
00294     uint8_t TC_PSC = thePSC;\
00295     int overflowCount = 0;\
00296     uint8_t TC_LIST_TAG = 0x00;\
00297     Base_tc *temp_tc = gHEAD_NODE_TCL;\
00298     bool first_time = true;\
00299     Base_tm *current_tm = NULL;\
00300     while( temp_tc != NULL ){\
00301         if( overflowCount < TCL_OVERFLOW_CONSTANT ){\
00302             TM_PSC = TM_PSC + 1;\
00303             /*Increment TM_PSC for every new TM pointer created*/\
00304             Base_tm *temp_tm = new Long_tm;\
00305             temp_tm->next_TM = NULL;\
00306             temp_tm->TM_string[0] = 0x38;\
00307             /*Frame type(1) = 0; TMID = 0111; Spare = 000*/\
00308             temp_tm->TM_string[1] = TC_PSC;\
00309             temp_tm->TM_string[3] = TM_PSC;\
00310             int indexCustom = 4, num_of_short_tc = 0;\
00311             while(temp_tc != NULL){\
00312                 if( overflowCount < TCL_OVERFLOW_CONSTANT ){\
00313                     if( GETshort_or_long_tc(temp_tc) == SHORT_TC_CODE ){\
00314                         for( int i = 0 ; i < TC_SHORT_SIZE ; ++i ){\
00315                             temp_tm->TM_string[indexCustom + i] = temp_tc->TC_string[i];\
00316                         }\
00317                         indexCustom = indexCustom + TC_SHORT_SIZE;\
00318                         ++overflowCount;\
00319                         /*INCREMENT STUFF*/\
00320                         temp_tc = temp_tc->next_TC;\
00321                         num_of_short_tc = num_of_short_tc + 1;\
00322                         if (temp_tc == NULL){\
00323                             for (int i = indexCustom ; i < TM_LONG_SIZE-2 ; ++i){\
00324                                 temp_tm->TM_string[i] = 0;\
00325                                 /*Fill remaining values with 0*/\
00326                             }\
00327                             TC_LIST_TAG = num_of_short_tc;\
00328                             break;\
00329                         }\
00330                         if( (GETshort_or_long_tc(temp_tc) == LONG_TC_CODE) || (num_of_short_tc == OBOSC_TCL_MAX_SHORT_SIZE) ){\
00331                             /*Next TC is long or memory is filled*/\
00332                             for(int i = indexCustom; i < TM_LONG_SIZE-2 ; ++i){\
00333                                 temp_tm->TM_string[i] = 0;\
00334                                 /*Fill remaining values with 0*/\
00335                             }\
00336                             TC_LIST_TAG = num_of_short_tc;\
00337                             break;\
00338                         }\
00339                     }\
00340                     else if (TC_LIST_TAG == OBOSC_TCL_TAG_LONG_FIRST_HALF){\
00341                         /*Long TC, 2nd half*/\
00342                         for (int i = 0; i < OBOSC_LONG_TC_SECOND_HALF_SIZE ; ++i){\
00343                             temp_tm->TM_string[indexCustom + i] = temp_tc->TC_string[OBOSC_LONG_TC_FIRST_HALF_SIZE + i];\
00344                             /*Fill the TM with TC_values*/\
00345                         }\
00346                         for (int i = OBOSC_LONG_TC_SECOND_HALF_SIZE + indexCustom ; i < TM_LONG_SIZE-2 ; ++i ){\
00347                             temp_tm->TM_string[i] = 0;\
00348                             /*Fill remaining values with 0*/\
00349                         }\
00350                         temp_tc = temp_tc->next_TC;\
00351                         TC_LIST_TAG = OBOSC_TCL_TAG_LONG_SECOND_HALF;\
00352                         ++overflowCount;\
00353                         break;\
00354                     }\
00355                     else{\
00356                         /*Long TC, 1st half */\
00357                         for (int i = 0; i < OBOSC_LONG_TC_FIRST_HALF_SIZE; ++i){\
00358                             /*gPC.printf("indexCustom = %d\r\n", indexCustom);*/\
00359                             temp_tm->TM_string[indexCustom + i] = temp_tc->TC_string[i];\
00360                             /*Fill the TM with TC_values*/\
00361                         }\
00362                         for (int i = OBOSC_LONG_TC_FIRST_HALF_SIZE + indexCustom ; i < TM_LONG_SIZE-2 ; ++i ){\
00363                             temp_tm->TM_string[i] = 0;\
00364                             /*Fill remaining values with 0*/\
00365                         }\
00366                         TC_LIST_TAG = OBOSC_TCL_TAG_LONG_FIRST_HALF;\
00367                         break;\
00368                     }\
00369                 }\
00370                 else{\
00371                     /*PENDING: reset cdms*/\
00372                     break;\
00373                 }\
00374             }\
00375             temp_tm->TM_string[2] = TC_LIST_TAG;\
00376             /*TC_LIST_TAG as is updated in the inner while loop*/\
00377             uint16_t crc16 = crc16_gen(temp_tm->TM_string, TM_LONG_SIZE-2);\
00378             temp_tm->TM_string[TM_LONG_SIZE-2] = (crc16 & 0xFF00) >> 8 ;\
00379             temp_tm->TM_string[TM_LONG_SIZE-1] = crc16 & 0x00FF;\
00380             /*Attach this new node to the TM linked list*/\
00381             if( first_time ){\
00382                 first_time = false;\
00383                 tm_ptr = temp_tm;\
00384                 current_tm = temp_tm;\
00385             }\
00386             else{\
00387                 current_tm->next_TM = temp_tm;\
00388                 current_tm = current_tm->next_TM;\
00389             }\
00390             /*Attach this new node at the end*/\
00391         }\
00392         else{\
00393             /*PENDING: reset CDMS*/\
00394             break;\
00395         }\
00396     }\
00397 }
00398 
00399 // CDMS TEAM CODE START
00400 inline Base_tm* FCTN_CDMS_RLY_TMTC(Base_tc *tc_ptr){
00401     uint8_t ACKCODE = 0xB0;
00402     //printf("\rTC execution in progress\r\n");
00403     Base_tm *tm_pointer = new Long_tm;
00404     Base_tm *tm_ptr = tm_pointer;
00405     received = 0;
00406     switch(GETapid(tc_ptr))
00407     {
00408         case 1: //apid=01 implies it corresponds to bae
00409         {
00410             //printf("Telecommand is for BAE\r\n");
00411             //printf("Sending TC to BAE...\r\n"); //interrupt to be sent to the bae
00412             //FCTN_I2C_WRITE((char*)tc_ptr->TC_string);
00413 //            while(1)
00414 //            {
00415 //                wait(1); //TimeOut instead of wait
00416 //                if(tm_status_4m_slv == 1)
00417 //                {
00418 //                    //printf("receiving...\r\n");
00419 //                    FCTN_I2C_READ((char*)tm_pointer->TM_string,134);
00420 //                    //printf("%s", tm_pointer->TM_string);
00421 //                    received+=1;
00422 //                    tm_pointer = tm_pointer->next_TM;
00423 //                    break; //only for testing purpose
00424 //                }
00425 //                else
00426 //                {
00427 //                    break;
00428 //                }
00429 //            }
00430 //            if(received >= 1 )
00431 //            {
00432 //                //printf("Telemetry is received from BAE\r\n");
00433 //                Base_tm *tm_print = tm_ptr;
00434 //                for(uint8_t i=0;i<received;i++)
00435 //                {  
00436 //                    //printf("%s", tm_print->TM_string);
00437 //                    tm_print = tm_print->next_TM; //for check
00438 //                }
00439 //                received  = 0;
00440 //                tm_pointer->next_TM = NULL;                 return tm_ptr;
00441 //            }
00442 //            else
00443 //            {
00444 //                //printf("Telemetry is not received from BAE\r\n");
00445 //                //tm_pointer = new Short_tm;
00446 //                tm_pointer->TM_string[0] = 0xB0;
00447 //                tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);
00448 //                tm_pointer->TM_string[2] = 0x01;
00449 //                tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
00450 //                tm_pointer->TM_string[4] = 0x01;
00451 //                for(uint8_t i=0;i<6;i++)
00452 //                {
00453 //                    tm_pointer->TM_string[i+5] = 0;
00454 //                }
00455 //                crc16 = crc16_gen(tm_ptr->TM_string,11);
00456 //                tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00457 //                tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00458                 tm_pointer->next_TM = NULL;                 return tm_ptr;
00459 //            }
00460             break;
00461         }
00462         case 2:
00463         {
00464             //gPC.printf("Telecommand is for CDMS\r\n"); //apid = 10 corresponds to cdms
00465             switch(GETservice_type(tc_ptr))
00466             {
00467                 case 0x60:
00468                 {
00469                         //gPC.printf("service:MMS\r\n");
00470                         switch(GETservice_subtype(tc_ptr))
00471                         {
00472                             case 0x1:
00473                             {
00474                             //printf("sub_service:Read from RAM_Memory\r\n");
00475                             mid=(uint16_t)(tc_ptr->TC_string[3]<<4)+(uint16_t)(tc_ptr->TC_string[4]);                                                       
00476                                                              
00477                                     //Data[0] = FCTN_CDMS_RD_FLASH(0);
00478                                     //Data[1] = FCTN_CDMS_RD_FLASH(1);
00479                                     //tm_pointer = new Long_tm;
00480                                     tm_pointer->TM_string[0] = 0x30;
00481                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);
00482                                     tm_pointer->TM_string[2] = (uint8_t)(flash_counter);
00483                                     for(uint8_t i=0;i<4;i++)
00484                                     {
00485                                         tm_pointer->TM_string[3+i] = (uint8_t)((Data[0]>>(8*i))&0x00FF);
00486                                     }
00487                                     for(uint8_t i=4;i<8;i++)
00488                                     {
00489                                         tm_pointer->TM_string[3+i] = (uint8_t)((Data[1]>>(8*i))&0x00FF);
00490                                     }
00491                                     crc16 = crc16_gen(tm_ptr->TM_string,9);
00492                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00493                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00494                                 tm_pointer->next_TM = NULL;                 return tm_ptr;
00495                             }                            
00496                             case 0x2:
00497                             {
00498                             printf("sub_service:Read from Flash_Memory\r\n");
00499                             mid=(uint16_t)(tc_ptr->TC_string[3]<<4)+(uint16_t)(tc_ptr->TC_string[4]);                           
00500                                                                                              
00501                                     //Data[0] = FCTN_CDMS_RD_FLASH(0);
00502                                     //Data[1] = FCTN_CDMS_RD_FLASH(1);                                                                                             
00503                                     //tm_pointer = new Long_tm;
00504                                     tm_pointer->TM_string[0] = 0xB0;
00505                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);
00506                                     tm_pointer->TM_string[2] = ACKCODE;
00507                                     
00508                                     for(uint8_t i=0;i<4;i++)
00509                                     {
00510                                         tm_pointer->TM_string[3+i] = (uint8_t)((Data[0]>>(8*i))&0x00FF);
00511                                     }
00512                                     for(uint8_t i=4;i<8;i++)
00513                                     {
00514                                         tm_pointer->TM_string[3+i] = (uint8_t)((Data[1]>>(8*i))&0x00FF);
00515                                     }
00516                                     crc16 = crc16_gen(tm_ptr->TM_string,9);
00517                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00518                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00519                                     //for(int i = )
00520                                 tm_pointer->next_TM = NULL;                 return tm_ptr;
00521                             }                           
00522                             case 0x5:
00523                             {
00524                             //printf("WRITE ON FLASH_MEMORY\r\n");                          
00525                                     mid=(uint16_t)(tc_ptr->TC_string[3]<<4)+(uint16_t)(tc_ptr->TC_string[4]);                                   
00526                                     block = (((uint32_t)(tc_ptr->TC_string[5])<<24)|((uint32_t)(tc_ptr->TC_string[5])<<16)|((uint32_t)(tc_ptr->TC_string[5])<<8)|((uint32_t)(tc_ptr->TC_string[5])));
00527                                     //FCTN_CDMS_WR_FLASH(mid,block);
00528                                     //tm_pointer = new Short_tm;
00529                                     tm_pointer->TM_string[0] = 0xB0;
00530                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);
00531                                     tm_pointer->TM_string[2] = 0x01;
00532                                     tm_pointer->TM_string[3] = ACKCODE;  //ackcode to be decided
00533                                     tm_pointer->TM_string[4] = 0x01;
00534                                     for(uint8_t i=0;i<6;i++)
00535                                     {
00536                                         tm_pointer->TM_string[i+5] = 0;
00537                                     }
00538                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00539                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00540                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00541                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00542                                     break;
00543                             }                                                    
00544                             default:
00545                             {
00546                                 //printf("INVALID TC\r\n"); //Send Invalid TC Telemetry
00547                                 //tm_pointer = new Short_tm;
00548                                 tm_pointer->TM_string[0] = 0xB0;
00549                                 tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);
00550                                 tm_pointer->TM_string[2] = 0x01;
00551                                 tm_pointer->TM_string[3] = ACKCODE;  //ackcode to be decided
00552                                 tm_pointer->TM_string[4] = 0x01;
00553                                 for(uint8_t i=0;i<6;i++)
00554                                 {
00555                                     tm_pointer->TM_string[i+5] = 0;
00556                                 }
00557                                 crc16 = crc16_gen(tm_ptr->TM_string,11);
00558                                 tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00559                                 tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00560                                 tm_pointer->next_TM = NULL;                 return tm_ptr;                               
00561                             }
00562                             break;
00563                         }
00564                     }                                                                   
00565                     case 0x8:
00566                     {
00567                         //printf("service:FUNCTION MANAGEMENT SERVICE\r\n"); 
00568                         if(GETservice_subtype(tc_ptr)==0x1)
00569                             {                               
00570                               if(GETpid(tc_ptr)==0x01)
00571                                 {
00572                                     //printf("TC_PL_INIT\r\n"); // call PWR_SWCH_ON function
00573                                     //tm_pointer = new Short_tm; 
00574                                     tm_pointer->TM_string[0] = 0xB0; 
00575                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00576                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00577                                     for(uint8_t i=3;i<11;i++)
00578                                     {
00579                                         tm_pointer->TM_string[i] = 0;
00580                                     }
00581                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00582                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00583                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00584                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00585                                     break;
00586                                 }                                
00587                                 else if(GETpid(tc_ptr)==0x02)
00588                                 {
00589                                     //printf("TC_PL_MAIN\r\n"); // call PWR_SWCH_ON function
00590                                     //tm_pointer = new Short_tm; 
00591                                     tm_pointer->TM_string[0] = 0xB0; 
00592                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00593                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00594                                     for(uint8_t i=3;i<11;i++)
00595                                     {
00596                                         tm_pointer->TM_string[i] = 0;
00597                                     }
00598                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00599                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00600                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00601                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00602                                     break;
00603                                 }
00604                                else if(GETpid(tc_ptr)==0x03)
00605                                 {
00606                                     //printf("TC_COM_INIT\r\n"); // call PWR_SWCH_ON function
00607                                     //tm_pointer = new Short_tm; 
00608                                     tm_pointer->TM_string[0] = 0xB0; 
00609                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00610                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00611                                     for(uint8_t i=3;i<11;i++)
00612                                     {
00613                                         tm_pointer->TM_string[i] = 0;
00614                                     }
00615                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00616                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00617                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00618                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00619                                     break;
00620                                 }
00621                                 else if(GETpid(tc_ptr)==0x04)
00622                                 {
00623                                     //printf("TC_CDMS_HK_MAIN\r\n"); // call PWR_SWCH_ON function
00624                                     //tm_pointer = new Short_tm; 
00625                                     tm_pointer->TM_string[0] = 0xB0; 
00626                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00627                                     tm_pointer->TM_string[2] = ACKCODE;                                    
00628                                     for(uint8_t i=3;i<11;i++)
00629                                     {
00630                                         tm_pointer->TM_string[i] = 0;
00631                                     }
00632                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00633                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00634                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00635                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00636                                     break;                                   
00637                                 }
00638                                 else if(GETpid(tc_ptr)==0x11)
00639                                 {
00640                                     //printf("TC_SW_ON_SD\r\n"); // call PWR_SWCH_ON function
00641                                     //tm_pointer = new Short_tm; 
00642                                     tm_pointer->TM_string[0] = 0xB0; 
00643                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00644                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00645                                     for(uint8_t i=3;i<11;i++)
00646                                     {
00647                                         tm_pointer->TM_string[i] = 0;
00648                                     }
00649                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00650                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00651                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00652                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00653                                     break;
00654                                 }
00655                                 else if(GETpid(tc_ptr)==0x12)
00656                                 {
00657                                     //printf("TC_SW_ON_RTC\r\n"); // call PWR_SWCH_ON function
00658                                     //tm_pointer = new Short_tm; 
00659                                     tm_pointer->TM_string[0] = 0xB0; 
00660                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00661                                     tm_pointer->TM_string[2] = ACKCODE;                                    
00662                                     for(uint8_t i=3;i<11;i++)
00663                                     {
00664                                         tm_pointer->TM_string[i] = 0;
00665                                     }
00666                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00667                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00668                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00669                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00670                                     break;
00671                                 }
00672                                 else if(GETpid(tc_ptr)==0x13)
00673                                 {
00674                                     //printf("TC_SW_ON_BAE\r\n"); // call PWR_SWCH_ON function
00675                                     //tm_pointer = new Short_tm; 
00676                                     tm_pointer->TM_string[0] = 0xB0; 
00677                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00678                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00679                                     for(uint8_t i=3;i<11;i++)
00680                                     {
00681                                         tm_pointer->TM_string[i] = 0;
00682                                     }
00683                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00684                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00685                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00686                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00687                                     break;
00688                                 }
00689                                 else if(GETpid(tc_ptr)==0x14)
00690                                 {
00691                                     //printf("TC_SW_ON_PL_DL\r\n"); // call PWR_SWCH_ON function
00692                                     //tm_pointer = new Short_tm; 
00693                                     tm_pointer->TM_string[0] = 0xB0; 
00694                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00695                                     tm_pointer->TM_string[2] = ACKCODE;                                    
00696                                     for(uint8_t i=3;i<11;i++)
00697                                     {
00698                                         tm_pointer->TM_string[i] = 0;
00699                                     }
00700                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00701                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00702                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00703                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00704                                     break;
00705                                 }
00706                                  else if(GETpid(tc_ptr)==0x15)
00707                                 {
00708                                     //printf("TC_SW_ON_PL_AG_HV\r\n"); // call PWR_SWCH_ON function
00709                                     //tm_pointer = new Short_tm; 
00710                                     tm_pointer->TM_string[0] = 0xB0; 
00711                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00712                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00713                                     for(uint8_t i=3;i<11;i++)
00714                                     {
00715                                         tm_pointer->TM_string[i] = 0;
00716                                     }
00717                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00718                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00719                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00720                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00721                                     break;
00722                                 }
00723                                  else if(GETpid(tc_ptr)==0x16)
00724                                 {
00725                                     //printf("TC_SW_ON_V_A_EN\r\n"); // call PWR_SWCH_ON function
00726                                     //tm_pointer = new Short_tm; 
00727                                     tm_pointer->TM_string[0] = 0xB0; 
00728                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00729                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00730                                     for(uint8_t i=3;i<11;i++)
00731                                     {
00732                                         tm_pointer->TM_string[i] = 0;
00733                                     }
00734                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00735                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00736                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00737                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00738                                     break;                                   
00739                                 }
00740                                 else if(GETpid(tc_ptr)==0x21)
00741                                 {
00742                                     //printf("TC_SW_OFF_SD\r\n"); // call PWR_SWCH_ON function
00743                                     //tm_pointer = new Short_tm; 
00744                                     tm_pointer->TM_string[0] = 0xB0; 
00745                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00746                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00747                                     for(uint8_t i=3;i<11;i++)
00748                                     {
00749                                         tm_pointer->TM_string[i] = 0;
00750                                     }
00751                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00752                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00753                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00754                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00755                                     break;
00756                                 }
00757                                 else if(GETpid(tc_ptr)==0x22)
00758                                 {
00759                                     //printf("TC_SW_OFF_RTC\r\n"); // call PWR_SWCH_ON function
00760                                     //tm_pointer = new Short_tm; 
00761                                     tm_pointer->TM_string[0] = 0xB0; 
00762                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00763                                     tm_pointer->TM_string[2] = ACKCODE;                                    
00764                                     for(uint8_t i=3;i<11;i++)
00765                                     {
00766                                         tm_pointer->TM_string[i] = 0;
00767                                     }
00768                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00769                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00770                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00771                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00772                                     break;
00773                                 }
00774                                 else if(GETpid(tc_ptr)==0x23)
00775                                 {
00776                                     //printf("TC_SW_OFF_BAE\r\n"); // call PWR_SWCH_ON function
00777                                     //tm_pointer = new Short_tm; 
00778                                     tm_pointer->TM_string[0] = 0xB0; 
00779                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00780                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00781                                     for(uint8_t i=3;i<11;i++)
00782                                     {
00783                                         tm_pointer->TM_string[i] = 0;
00784                                     }
00785                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00786                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00787                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00788                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00789                                     break;
00790                                 }
00791                                 else if(GETpid(tc_ptr)==0x24)
00792                                 {
00793                                     //printf("TC_SW_OFF_PL_DL\r\n"); // call PWR_SWCH_ON function
00794                                     //tm_pointer = new Short_tm; 
00795                                     tm_pointer->TM_string[0] = 0xB0; 
00796                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00797                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00798                                     for(uint8_t i=3;i<11;i++)
00799                                     {
00800                                         tm_pointer->TM_string[i] = 0;
00801                                     }
00802                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00803                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00804                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00805                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00806                                     break;
00807                                 }
00808                                  else if(GETpid(tc_ptr)==0x25)
00809                                 {
00810                                     //printf("TC_SW_OFF_PL_AG_HV\r\n"); // call PWR_SWCH_ON function
00811                                     //tm_pointer = new Short_tm; 
00812                                     tm_pointer->TM_string[0] = 0xB0; 
00813                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00814                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00815                                     for(uint8_t i=3;i<11;i++)
00816                                     {
00817                                         tm_pointer->TM_string[i] = 0;
00818                                     }
00819                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00820                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00821                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00822                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00823                                     break;
00824                                 }
00825                                  else if(GETpid(tc_ptr)==0x26)
00826                                 {
00827                                     //printf("TC_SW_OFF_V_A_EN\r\n"); // call PWR_SWCH_ON function
00828                                     //tm_pointer = new Short_tm; 
00829                                     tm_pointer->TM_string[0] = 0xB0; 
00830                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00831                                     tm_pointer->TM_string[2] = ACKCODE;                                    
00832                                     for(uint8_t i=3;i<11;i++)
00833                                     {
00834                                         tm_pointer->TM_string[i] = 0;
00835                                     }
00836                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00837                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00838                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00839                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00840                                     break;
00841                                 }
00842                                 else if(GETpid(tc_ptr)==0x31)
00843                                 {
00844                                     //printf("TC_RST_SD\r\n"); // call PWR_SWCH_ON function
00845                                     //tm_pointer = new Short_tm; 
00846                                     tm_pointer->TM_string[0] = 0xB0; 
00847                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00848                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00849                                     for(uint8_t i=3;i<11;i++)
00850                                     {
00851                                         tm_pointer->TM_string[i] = 0;
00852                                     }
00853                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00854                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00855                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00856                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00857                                     break;
00858                                 }
00859                                 else if(GETpid(tc_ptr)==0x32)
00860                                 {
00861                                     //printf("TC_RST_RTC\r\n"); // call PWR_SWCH_ON function
00862                                     //tm_pointer = new Short_tm; 
00863                                     tm_pointer->TM_string[0] = 0xB0; 
00864                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00865                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00866                                     for(uint8_t i=3;i<11;i++)
00867                                     {
00868                                         tm_pointer->TM_string[i] = 0;
00869                                     }
00870                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00871                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00872                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00873                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00874                                     break;
00875                                 }
00876                                 else if(GETpid(tc_ptr)==0x33)
00877                                 {
00878                                     //printf("TC_RST_BAE\r\n"); // call PWR_SWCH_ON function
00879                                     //tm_pointer = new Short_tm; 
00880                                     tm_pointer->TM_string[0] = 0xB0; 
00881                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00882                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00883                                     for(uint8_t i=3;i<11;i++)
00884                                     {
00885                                         tm_pointer->TM_string[i] = 0;
00886                                     }
00887                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00888                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00889                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00890                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00891                                     break;
00892                                 }
00893                                 else if(GETpid(tc_ptr)==0x34)
00894                                 {
00895                                     //printf("TC_RST_PL_DL\r\n"); // call PWR_SWCH_ON function
00896                                     //tm_pointer = new Short_tm; 
00897                                     tm_pointer->TM_string[0] = 0xB0; 
00898                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00899                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00900                                     for(uint8_t i=3;i<11;i++)
00901                                     {
00902                                         tm_pointer->TM_string[i] = 0;
00903                                     }
00904                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00905                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00906                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00907                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00908                                     break;
00909                                     break;
00910                                 }
00911                                 else if(GETpid(tc_ptr)==0xC1)
00912                                 {
00913                                     //printf("RESET_HK_COUNTER\r\n"); // call PWR_SWCH_ON function
00914                                     //tm_pointer = new Short_tm; 
00915                                     tm_pointer->TM_string[0] = 0xB0; 
00916                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00917                                     tm_pointer->TM_string[2] = ACKCODE;                                   
00918                                     for(uint8_t i=3;i<11;i++)
00919                                     {
00920                                         tm_pointer->TM_string[i] = 0;
00921                                     }
00922                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00923                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00924                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00925                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00926                                     break;
00927                                 }
00928                                 else if(GETpid(tc_ptr)==0xF1)
00929                                 {
00930                                     //printf("RD_RTC\r\n"); //call RD_RTC
00931                                     //tm_pointer = new Short_tm;
00932                                     tm_pointer->TM_string[0] = 0xD0;
00933                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);
00934                                     tm_pointer->TM_string[2] = 0x01;
00935                                     uint64_t time = FCTN_CDMS_RD_RTC();
00936                                     tm_pointer->TM_string[3] = (uint8_t)((time&0xFF00000000000000)>>56);
00937                                     tm_pointer->TM_string[4] = (uint8_t)((time&0x00FF000000000000)>>48);
00938                                     tm_pointer->TM_string[5] = (uint8_t)((time&0x0000FF0000000000)>>40);
00939                                     tm_pointer->TM_string[6] = (uint8_t)((time&0x000000FF00000000)>>32);
00940                                     tm_pointer->TM_string[7] = (uint8_t)((time&0x00000000FF000000)>>24);
00941                                     tm_pointer->TM_string[8] = (uint8_t)((time&0x0000000000FF0000)>>16);
00942                                     tm_pointer->TM_string[9] = (uint8_t)((time&0x000000000000FF00)>>8);
00943                                     tm_pointer->TM_string[10] = (uint8_t)(time&0x00000000000000FF);
00944                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00945                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00946                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00947                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00948                                 }                               
00949                                 else 
00950                                 {
00951                                     //printf("INVALID TC\r\n");
00952                                     //tm_pointer = new Short_tm; 
00953                                     tm_pointer->TM_string[0] = 0xB0; 
00954                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00955                                     tm_pointer->TM_string[2] = 0x01;
00956                                     tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
00957                                     tm_pointer->TM_string[4] = 0x01;
00958                                     for(uint8_t i=0;i<6;i++)
00959                                     {
00960                                         tm_pointer->TM_string[i+5] = 0;
00961                                     }
00962                                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00963                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00964                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00965                                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00966                                 }
00967                             }                          
00968                                               
00969                                      
00970                 }
00971                 default:
00972                 {
00973                     //printf("INVALID TC"); //send invalid TC TM
00974                     //tm_pointer = new Short_tm; 
00975                     tm_pointer->TM_string[0] = 0xB0; 
00976                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
00977                     tm_pointer->TM_string[2] = 0x01;
00978                     tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
00979                     tm_pointer->TM_string[4] = 0x01;
00980                     for(uint8_t i=0;i<6;i++)
00981                     {
00982                         tm_pointer->TM_string[i+5] = 0;
00983                     }
00984                     crc16 = crc16_gen(tm_ptr->TM_string,11);
00985                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
00986                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
00987                     tm_pointer->next_TM = NULL;                 return tm_ptr;
00988                     break;
00989                 }
00990             }
00991         }
00992         case 3:
00993         {
00994             
00995             
00996             
00997             #if DEBUG
00998             printf("Telecommand is for PL\r\n");
00999             #endif
01000 
01001 
01002             if(GETservice_subtype(tc_ptr) == 0xD0 || GETservice_subtype(tc_ptr) == 0xD2 || GETservice_subtype(tc_ptr) == 0xD3)
01003             {
01004                 PL_TM_SIZE = TM_LONG_SIZE;
01005             }
01006             else
01007             {
01008                 PL_TM_SIZE = TM_SHORT_SIZE;
01009             }   
01010                 
01011             if(GETshort_or_long_tc(tc_ptr) == LONG_TC_CODE)
01012                 {
01013                     int y=FCTN_I2C_WRITE_PL((char*)tc_ptr->TC_string,TC_LONG_SIZE);
01014                     if(y==1)
01015                     {
01016                         tm_pointer->TM_string[0] = 0xB0; 
01017                         tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
01018                         tm_pointer->TM_string[2] = 0x33; //ack_code
01019                         tm_pointer->TM_string[3] = 0x00; 
01020                         tm_pointer->TM_string[4] = 0x00;
01021                         for(uint8_t i=0;i<6;i++)
01022                         {
01023                             tm_pointer->TM_string[i+5] = 0;
01024                         }
01025                         crc16 = crc16_gen(tm_ptr->TM_string,11);
01026                         tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
01027                         tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
01028                         tm_pointer->next_TM = NULL;                 
01029                         return tm_ptr;
01030                         break;
01031                     }
01032             }
01033             else 
01034             {
01035                 int y=FCTN_I2C_WRITE_PL((char*)tc_ptr->TC_string,TC_SHORT_SIZE);
01036                 if(y==1)
01037                     {
01038                         tm_pointer->TM_string[0] = 0xB0; 
01039                         tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
01040                         tm_pointer->TM_string[2] = 0x33; //ack_code
01041                         tm_pointer->TM_string[3] = 0x00; 
01042                         tm_pointer->TM_string[4] = 0x00;
01043                         for(uint8_t i=0;i<6;i++)
01044                         {
01045                             tm_pointer->TM_string[i+5] = 0;
01046                         }
01047                         crc16 = crc16_gen(tm_ptr->TM_string,11);
01048                         tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
01049                         tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
01050                         tm_pointer->next_TM = NULL;                 
01051                         return tm_ptr;
01052                         break;
01053                     }
01054             }
01055             
01056             
01057             while(1)
01058             {
01059                 gLEDG = !gLEDG;
01060                 t_pl.start();
01061                 if(rcv_isr == 1)
01062                 {
01063                     #if DEBUG
01064                     gPC.printf("read_ack = %d",read_ack);
01065                     #endif
01066                     t_pl.stop();
01067                     gPC.printf("data:\n");
01068                     //gPC.printf("i2c_t = %d",t_pl.read_ms());  
01069                         for(int i = 0;i< 134;i++)
01070                         gPC.printf("%d\n",PL_I2C_DATA[i]);
01071                         rcv_isr = 0; 
01072                         break;
01073                 }
01074             }
01075             tm_pointer->next_TM = NULL;                 return tm_ptr;
01076             break;
01077         }
01078         default: //invalid TC
01079         {
01080             //printf("INVALID TC\r\n");
01081             //tm_pointer = new Short_tm; 
01082             tm_pointer->TM_string[0] = 0xB0; 
01083             tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
01084             tm_pointer->TM_string[2] = 0x01;
01085             tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
01086             tm_pointer->TM_string[4] = 0x01;
01087             for(uint8_t i=0;i<6;i++)
01088             {
01089                 tm_pointer->TM_string[i+5] = 0;
01090             }
01091             crc16 = crc16_gen(tm_ptr->TM_string,11);
01092             tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
01093             tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
01094             tm_pointer->next_TM = NULL;                 return tm_ptr;
01095             break;
01096         }
01097      }
01098 }
01099 // CDMS TEAM CODE END
01100 // EXECUTE OBOSC
01101 #define execute_obosc_core(tc_ptr, tm_ptr, reset_flag) {\
01102     uint8_t service_subtype = (tc_ptr->TC_string[2]) & 0x0F;\
01103     uint8_t temp8 = 0x00;\
01104     Base_tc *modify_this = gHEAD_NODE_TCL;\
01105     int modify_overflow = 0x00;\
01106     switch( service_subtype ){\
01107         case OBOSC_SUB_DISABLE:\
01108             /*gPC.puts("target psc are ");*/\
01109             for(int i = 3 ; i < 9 ; ++i){\
01110                 /*gPC.printf("%u ", tc_ptr->TC_string[i]);*/\
01111             }\
01112             /*gPC.puts("\r\n");*/\
01113             /*gPC.puts("disable type obosc\r\n");*/\
01114             while( modify_this != NULL ){\
01115                 if( modify_overflow < TCL_OVERFLOW_CONSTANT ){\
01116                     uint8_t modify_psc = GETpacket_seq_count(modify_this);\
01117                     /*gPC.printf("mosify_psc = %u\r\n", modify_psc);*/\
01118                     for( int i = 3 ; i < 9 ; ++i ){\
01119                         uint8_t target_psc = tc_ptr->TC_string[i];\
01120                         if( (target_psc == modify_psc) && (target_psc != 0) ){\
01121                             uint16_t tempExec = TC_STATE_DISABLED;\
01122                             PUTexec_status( modify_this, tempExec );\
01123                             break;\
01124                         }\
01125                     }\
01126                     modify_this = modify_this->next_TC;\
01127                     ++modify_overflow;\
01128                 }\
01129                 else{\
01130                     /*PENDING: RESET CDMS*/\
01131                     break;\
01132                 }\
01133             }\
01134             break;\
01135         case OBOSC_SUB_RETRY:\
01136             /*gPC.puts("retry type obosc\r\n");*/\
01137             while( modify_this != NULL ){\
01138                 if( modify_overflow < TCL_OVERFLOW_CONSTANT ){\
01139                     uint8_t modify_psc = GETpacket_seq_count(modify_this);\
01140                     for( int i = 3 ; i < 9 ; ++i ){\
01141                         uint8_t target_psc = tc_ptr->TC_string[i];\
01142                         if( (target_psc == modify_psc) && (target_psc != 0) ){\
01143                             uint16_t tempExec = TC_STATE_MARKED_RETRY;\
01144                             PUTexec_status( modify_this, tempExec );\
01145                             /*gPC.puts("reqquired tc found: disable\r\n");*/\
01146                             break;\
01147                         }\
01148                     }\
01149                     modify_this = modify_this->next_TC;\
01150                     ++modify_overflow;\
01151                 }\
01152                 else{\
01153                     /*PENDING: RESET CDMS*/\
01154                     break;\
01155                 }\
01156             }\
01157             break;\
01158     }\
01159     Base_tm *ackl234new = new Short_tm;\
01160     ackl234new->next_TM = NULL;\
01161     /*return telemetry pointer here*/\
01162     tm_ptr = ackl234new;\
01163     ackl234new->TM_string[0] = TMID_ACK_L234 << 4;\
01164     ackl234new->TM_string[1] = GETpacket_seq_count(tc_ptr);\
01165     ackl234new->TM_string[2] = 0xA0;\
01166     for(int i = 3; i < 11; ++i){\
01167         ackl234new->TM_string[i] = 0x00;\
01168     }\
01169     uint16_t crc = crc16_gen(ackl234new->TM_string, TM_SHORT_SIZE-2);\
01170     ackl234new->TM_string[11] = (crc & 0xFF00) >> 8;\
01171     ackl234new->TM_string[12] = crc & 0x00FF;\
01172     /*ack l234 old*/\
01173     if( service_subtype == OBOSC_SUB_REP_LE ){\
01174         if( gLAST_TM_SHORT_OR_LONG == SHORT_TM_CODE ){\
01175             Base_tm *ackl234old = new Short_tm;\
01176             ackl234old->next_TM = NULL;\
01177             /*APPEND TO ACK L234 NEW HERE*/\
01178             ackl234new->next_TM = ackl234old;\
01179             for( int i = 0 ; i < TM_SHORT_SIZE ; ++i ){\
01180                 ackl234old->TM_string[i] = gLAST_TM[i];\
01181             }\
01182         }\
01183         else{\
01184             Base_tm *tempLongTM = new Long_tm;\
01185             tempLongTM->next_TM = NULL;\
01186             /*APPEND TO ACK L234 NEW HERE*/\
01187             ackl234new->next_TM = tempLongTM;\
01188             for( int i = 0 ; i < TM_LONG_SIZE ; ++i ){\
01189                 tempLongTM->TM_string[i] = gLAST_TM[i];\
01190             }\
01191         }\
01192     }\
01193     else if( service_subtype == OBOSC_SUB_REP_TCLD ){\
01194         get_tc_list(ackl234new->next_TM, GETpacket_seq_count(tc_ptr));\
01195     }\
01196     else if( service_subtype == OBOSC_SUB_RESET ){\
01197         /*reset_all;*/\
01198         reset_flag = 1;\
01199         /*PENDING: VERIFY reset_all, RESET CDMS*/\
01200     }\
01201     else if( (service_subtype != OBOSC_SUB_DISABLE) && (service_subtype != OBOSC_SUB_RETRY) ){\
01202         /*CHANGE THE ACK CODE TO INVALID TC*/\
01203         ackl234new->TM_string[2] = 0xA2;\
01204     }\
01205     /*gPC.puts("completed obosc\r\n");*/\
01206 }
01207 
01208 /*tm_ptr is the next_TM of a linked list, and should have the value NULL, i.e. tm_ptr should be the next_TM pointer of thte last node */
01209 #define get_call_sign(tm_ptr) {\
01210     Base_tm* call_sign_tm = new Short_tm;\
01211     call_sign_tm->next_TM = NULL;\
01212     tm_ptr = call_sign_tm;\
01213     for( int i = 0 ; i < TM_SHORT_SIZE ; ++i ){\
01214         call_sign_tm->TM_string[i] = gCALL_SIGN_STRING[i];\
01215     }\
01216 }
01217 
01218 #define EXECUTE_OBOSC_ONLY {\
01219     int reset_flag = 0;\
01220     for(uint8_t execute_psc = PSC_START_VALUE ; execute_psc < gTOTAL_VALID_TC ; ++execute_psc){\
01221         Base_tc* current_TC = gHEAD_NODE_TCL;\
01222         int overCount = 0;\
01223         while( current_TC != NULL ){\
01224             if( overCount < TCL_OVERFLOW_CONSTANT ){\
01225                 if( (GETcrc_pass(current_TC) == 1) && (GETpacket_seq_count(current_TC) == execute_psc) ){\
01226                     /*CHECK FOR OBOSC*/\
01227                     uint8_t temp82 = 0x00;\
01228                     isit_obosc(current_TC, temp82);\
01229                     if(temp82 == 0x01){\
01230                         uint16_t current_exec_status = GETexec_status(current_TC);\
01231                         if( (current_exec_status == TC_STATE_SUCCESSFULLY_EXECUTED) || (current_exec_status == TC_STATE_DISABLED) )\
01232                             break;\
01233                         else if( (current_exec_status == TC_STATE_UNEXECUTED) || (current_exec_status == TC_STATE_MARKED_RETRY) ){\
01234                             /*gPC.printf("It is obosc: %u\r\n", execute_psc);*/\
01235                             /*EXECUTION OF OBOSC TC*/\
01236                             /*WARNING: LARGE MEMORY UTILIZATION FOR TC-LIST-REPORT */\
01237                             Base_tm *obosc_tm_head = NULL;\
01238                             get_call_sign(obosc_tm_head);\
01239                             Base_tm *obosc_tm_core = NULL;\
01240                             execute_obosc_core(current_TC, obosc_tm_core, reset_flag);\
01241                             uint16_t temp16 = TC_STATE_SUCCESSFULLY_EXECUTED;\
01242                             PUTexec_status(current_TC, temp16);\
01243                             Base_tm *obosc_tm_current = obosc_tm_head;\
01244                             get_ack_l1(obosc_tm_current->next_TM);\
01245                             int overflowCountOBONLY = 0;\
01246                             while( obosc_tm_current->next_TM != NULL ){\
01247                                 if( overflowCountOBONLY < TM_OVERFLOW_CONSTANT ){\
01248                                     obosc_tm_current = obosc_tm_current->next_TM;\
01249                                     ++overflowCountOBONLY;\
01250                                 }\
01251                                 else{\
01252                                     /*PENDING: RESET CDMS*/\
01253                                     break;\
01254                                 }\
01255                             }\
01256                             obosc_tm_current->next_TM = obosc_tm_core;\
01257                             obosc_tm_current = obosc_tm_head;\
01258                             while( obosc_tm_current != NULL ){\
01259                                 int length = TM_SHORT_SIZE;\
01260                                 if( GETshort_or_long_tm(obosc_tm_current) == SHORT_TM_CODE ){\
01261                                     length = TM_SHORT_SIZE;\
01262                                 }\
01263                                 else{\
01264                                     length = TM_LONG_SIZE;\
01265                                 }\
01266                                 for(int i = 0 ; i < length ; ++i){\
01267                                     /*gPC.putc(obosc_tm_current->TM_string[i]);*/\
01268                                 }\
01269                                 obosc_tm_current = obosc_tm_current->next_TM;\
01270                             }\
01271                             /*Sending OBOSC TM to GS*/\
01272                             /*snd_tm.head_pointer(obosc_tm_head);*/\
01273                             /*transmit_adf;*/\
01274                             /*DELETE THE TM AFTER USE*/\
01275                             obosc_tm_current = obosc_tm_head;\
01276                             int overCount = 0;\
01277                             while( obosc_tm_current != NULL ){\
01278                                 if( (overCount < TM_OVERFLOW_CONSTANT) ){\
01279                                     Base_tm *temp = obosc_tm_current->next_TM;\
01280                                     delete obosc_tm_current;\
01281                                     obosc_tm_current = temp;\
01282                                     ++overCount;\
01283                                 }\
01284                                 else{\
01285                                     /*PENDING: RESET CDMS: MEMORY LEAK FOUND*/\
01286                                     break;\
01287                                 }\
01288                             }\
01289                             if ( reset_flag ==1 ){\
01290                                 reset_all;\
01291                             }\
01292                         }\
01293                     }\
01294                 }\
01295                 current_TC = current_TC->next_TC;\
01296                 ++overCount;\
01297             }\
01298             else{\
01299                 /*PENDING: REST CDMS: MEMORY LEAK FOUND*/\
01300                 break;\
01301             }\
01302         }\
01303     }\
01304 }
01305 
01306 #define EXECUTE_TC {\
01307     gMASTER_STATE = TCL_STATE_EXECUTING;\
01308     /*gPC.printf("%u\r\n", gTOTAL_VALID_TC);*/\
01309     for(uint8_t execute_psc = PSC_START_VALUE ; execute_psc < gTOTAL_VALID_TC ; ++execute_psc ){\
01310         /*gPC.printf("executing normal %u\r\n", execute_psc);*/\
01311         /*gLEDG = !gLEDG;*/\
01312         /*gLEDR = !gLEDR;*/\
01313         Base_tc* current_TC = gHEAD_NODE_TCL;\
01314         int overflowCount = 0;\
01315         while(current_TC != NULL){\
01316             if( overflowCount < TCL_OVERFLOW_CONSTANT ){\
01317                 if( (GETcrc_pass(current_TC) == 1) && (GETpacket_seq_count(current_TC) == execute_psc) ){\
01318                     uint8_t current_exec_status = GETexec_status(current_TC);\
01319                     if( (current_exec_status == TC_STATE_SUCCESSFULLY_EXECUTED) || (current_exec_status == TC_STATE_DISABLED) ){\
01320                         /*gPC.printf("disabled or completed at %u\n", execute_psc);*/\
01321                         break;\
01322                     }\
01323                     else if( (current_exec_status == TC_STATE_EXECUTION_FAILED) && (GETabort_on_nack(current_TC) == 1) ){\
01324                         /*gPC.printf("abort on nack at %u psc\r\n", execute_psc);*/\
01325                         gMASTER_STATE = TCL_STATE_ABORTED;\
01326                         Base_tm *tm_ptr_head = NULL;\
01327                         get_call_sign(tm_ptr_head);\
01328                         Base_tm *tm_ptr = tm_ptr_head;\
01329                         get_ack_l1(tm_ptr->next_TM);\
01330                         tm_ptr = tm_ptr_head;\
01331                         while( tm_ptr != NULL ){\
01332                             int length = TM_SHORT_SIZE;\
01333                             if( GETshort_or_long_tm(tm_ptr) == SHORT_TM_CODE ){\
01334                                 length = TM_SHORT_SIZE;\
01335                             }\
01336                             else{\
01337                                 length = TM_LONG_SIZE;\
01338                             }\
01339                             /*gPC.puts("Printing Call Sign, ACK_L1, TM list");*/\
01340                             for(int i = 0 ; i < length ; ++i){\
01341                                 /*gPC.putc(tm_ptr->TM_string[i]);*/\
01342                             }\
01343                             tm_ptr = tm_ptr->next_TM;\
01344                         }\ 
01345                         /*Send only call sign, ACK_L1 to GS*/\
01346                         /*snd_tm.head_pointer(tm_ptr_head);*/\
01347                         /*transmit_adf;*/\
01348                         /*DELETE THE TM AFTER USE*/\
01349                         tm_ptr = tm_ptr_head;\
01350                         int overflowCountExecute = 0;\
01351                         while(tm_ptr != NULL){\
01352                             if( overflowCountExecute < TM_OVERFLOW_CONSTANT ){\
01353                                 Base_tm *temp = tm_ptr->next_TM;\
01354                                 delete tm_ptr;\
01355                                 tm_ptr = temp;\
01356                                 ++overflowCountExecute;\
01357                             }\
01358                             else{\
01359                                 /*PENDING: RESET CDMS*/\
01360                                 break;\
01361                             }\
01362                         }\
01363                         break;\
01364                     }\
01365                     else if( (current_exec_status == TC_STATE_UNEXECUTED) || (current_exec_status == TC_STATE_MARKED_RETRY) ){\
01366                         /*EXECUTION OF TC START*/\
01367                         uint8_t temp81 = 0x00;\
01368                         isit_sdcard(current_TC, temp81);\
01369                         if( temp81 == 0x00 ){\
01370                             /*gPC.printf("non sd card at %u\r\n", execute_psc);*/\
01371                             /*EXECUTION OF NON SD-CARD (BOTH OBOSC and CDMS functions)*/\
01372                             Base_tm *tm_ptr_head = NULL;\
01373                             get_call_sign(tm_ptr_head);\
01374                             Base_tm *put_tm_here = NULL;\
01375                             uint8_t temp82 = 0x00;\
01376                             uint8_t tempPAhot = 0x00;\
01377                             isit_obosc(current_TC, temp82);\
01378                             if(temp82 == 0x01){\
01379                                 /*gPC.printf("obosc tc inside normal tc at %u\r\n", execute_psc);*/\
01380                                 /*EXECUTION OF OBOSC TC*/\
01381                                 /*SKIP EXECUTION OF OBOSC HERE*/\
01382                                 break;\
01383                             }\
01384                             else{\
01385                                 /*gPC.printf("cdms relay tmtc at %u\r\n", execute_psc);*/\
01386                                 /*call CDMS_RLY_TMTC*/\
01387                                 /*CDMS_RLY_TMTC(current_TC, put_tm_here);*/\
01388                                 put_tm_here =  FCTN_CDMS_RLY_TMTC(current_TC);\
01389                             }\
01390                             /*DETECT ACK OR NACK*/\
01391                             uint8_t temp83 = 0x00;\
01392                             uint8_t temp84 = GETpacket_seq_count(current_TC);\
01393                             detect_ack(put_tm_here, temp83, temp84);\
01394                             if( temp83 == 0x01){\
01395                                 uint16_t temp16 = TC_STATE_SUCCESSFULLY_EXECUTED;\
01396                                 PUTexec_status(current_TC, temp16);\
01397                             }\
01398                             else{\
01399                                 /*gPC.puts("TC_STATE_EXECUTION_FAILED");*/\
01400                                 uint16_t temp16 = TC_STATE_EXECUTION_FAILED;\
01401                                 PUTexec_status(current_TC, temp16);\
01402                             }\
01403                             /*ABORT ON NACK AND TC LIST COMPLETED: UPDATE IN gMASTERSTATE*/\
01404                             if( (GETexec_status(current_TC) == TC_STATE_EXECUTION_FAILED) && (GETabort_on_nack(current_TC) == 1) ){\
01405                                 gMASTER_STATE = TCL_STATE_ABORTED;\
01406                             }\
01407                             else if( execute_psc == (gTOTAL_VALID_TC-1) ){\
01408                                 /*LAST TC IS EXECUTED*/\
01409                                 gMASTER_STATE = TCL_STATE_COMPLETED;\
01410                             }\
01411                             else{\
01412                                 uint8_t check1 = 1;\
01413                                 for( uint8_t test_psc = execute_psc+1 ; test_psc < gTOTAL_VALID_TC ; ++test_psc ){\
01414                                     Base_tc* test_TC = gHEAD_NODE_TCL;\
01415                                     while(test_TC != NULL){\
01416                                         if( GETpacket_seq_count(test_TC) == test_psc ){\
01417                                             uint8_t checkval;\
01418                                             isit_obosc(test_TC, checkval);\
01419                                             if( checkval == 0 ){\
01420                                                 check1 = 0;\
01421                                             }\
01422                                         }\
01423                                         test_TC = test_TC->next_TC;\
01424                                     }\
01425                                 }\
01426                                 if( check1 == 0x01 ){\
01427                                     gMASTER_STATE = TCL_STATE_COMPLETED;\
01428                                 }\
01429                             }\
01430                             /*update last executed L1_ack*/\
01431                             if( put_tm_here != NULL ){\
01432                                 if( GETshort_or_long_tm(put_tm_here) == SHORT_TM_CODE ){\
01433                                     for(int i = 0 ; i < TM_SHORT_SIZE ; ++i){\
01434                                         gLAST_TM[i] = put_tm_here->TM_string[i];\
01435                                         gLAST_TM_SHORT_OR_LONG = SHORT_TM_CODE;\
01436                                     }\
01437                                 }\
01438                                 else{\
01439                                     for( int i = 0 ; i < TM_LONG_SIZE ; ++i ){\
01440                                         gLAST_TM[i] = put_tm_here->TM_string[i];\
01441                                         gLAST_TM_SHORT_OR_LONG = LONG_TM_CODE;\
01442                                     }\
01443                                 }\
01444                             }\
01445                             /*PENDING: APPEND ACK L1*/\
01446                             Base_tm *tm_ptr = tm_ptr_head;\
01447                             get_ack_l1(tm_ptr->next_TM);\
01448                             int overflowCountExecute = 0;\
01449                             while( tm_ptr->next_TM != NULL ){\
01450                                 if( overflowCountExecute < TM_OVERFLOW_CONSTANT ){\
01451                                     tm_ptr = tm_ptr->next_TM;\
01452                                     ++overflowCountExecute;\
01453                                 }\
01454                                 else{\
01455                                     /*PENDING: RESET CDMS*/\
01456                                     break;\
01457                                 }\
01458                             }\
01459                             tm_ptr->next_TM = put_tm_here;\
01460                             /*CHECK FOR HOT PA*/\
01461                             isPAhot(tempPAhot);\
01462                             if( tempPAhot == 0x00 ){\
01463                                 gFLAGS = gFLAGS | COM_PA_HOT_FLAG;\
01464                             }\
01465                             tm_ptr = tm_ptr_head;\
01466                             while( tm_ptr != NULL ){\
01467                                 int length = TM_SHORT_SIZE;\
01468                                 if( GETshort_or_long_tm(tm_ptr) == SHORT_TM_CODE ){\
01469                                     length = TM_SHORT_SIZE;\
01470                                 }\
01471                                 else{\
01472                                     length = TM_LONG_SIZE;\
01473                                 }\
01474                                 /*gPC.puts("Printing Call Sign, ACK_L1, TM list");*/\
01475                                 for(int i = 0 ; i < length ; ++i){\
01476                                     /*gPC.putc(tm_ptr->TM_string[i]);*/\
01477                                 }\
01478                                 tm_ptr = tm_ptr->next_TM;\
01479                             }\
01480                             /*SEND call sign, ACK_L1, NON OBSRS TM TO GS*/\
01481                             snd_tm.head_pointer(tm_ptr_head);\
01482                             transmit_adf;\
01483                             /*DELETE THE TM AFTER USE*/\
01484                             tm_ptr = tm_ptr_head;\
01485                             overflowCountExecute = 0;\
01486                             while(tm_ptr != NULL){\
01487                                 if( overflowCountExecute < TM_OVERFLOW_CONSTANT ){\
01488                                     Base_tm *temp = tm_ptr->next_TM;\
01489                                     delete tm_ptr;\
01490                                     tm_ptr = temp;\
01491                                     ++overflowCountExecute;\
01492                                 }\
01493                                 else{\
01494                                     /*PENDING: RESET CDMS*/\
01495                                     break;\
01496                                 }\ 
01497                             }\
01498                         }\
01499                         else{\
01500                             /*gPC.printf("sd card at %u\r\n", execute_psc);*/\
01501                             /*EXECUTION OF SD-CARD DATA SENDING (OBSRS)*/\
01502                             Base_tm *tm_ptr_head = NULL;\
01503                             get_call_sign(tm_ptr_head);\
01504                             Base_tm *put_tm_here = NULL;\
01505                             /*execute_obsrs(current_TC, put_tm_here)*/\
01506                             /*read_TC(current_TC);*/\
01507                             execute_OBSRS_TC(current_TC);\
01508                             transmit_adf;\
01509                             uint8_t tempExec = TC_STATE_SUCCESSFULLY_EXECUTED;\
01510                             PUTexec_status(current_TC, tempExec);\
01511                             /*PENDING: ABORT ON NACK CHECK, gMASTER_STATE VERIFICATION WITH SD CARD, session timeout, last executed ack l234*/\
01512                             if( (GETexec_status(current_TC) == TC_STATE_EXECUTION_FAILED) && (GETabort_on_nack(current_TC) == 1) ){\
01513                                 gMASTER_STATE = TCL_STATE_ABORTED;\
01514                             }\
01515                             else if( execute_psc == (gTOTAL_VALID_TC-1) ){\
01516                                 gMASTER_STATE = TCL_STATE_COMPLETED;\
01517                             }\
01518                             else{\
01519                                 uint8_t check1 = 1;\
01520                                 for( uint8_t test_psc = execute_psc+1 ; test_psc < gTOTAL_VALID_TC ; ++test_psc ){\
01521                                     Base_tc* test_TC = gHEAD_NODE_TCL;\
01522                                     while(test_TC != NULL){\
01523                                         if( GETpacket_seq_count(test_TC) == test_psc ){\
01524                                             uint8_t checkval;\
01525                                             isit_obosc(test_TC, checkval);\
01526                                             if( checkval == 0 ){\
01527                                                 check1 = 0;\
01528                                             }\
01529                                         }\
01530                                         test_TC = test_TC->next_TC;\
01531                                     }\
01532                                 }\
01533                                 if( check1 == 0x01 ){\
01534                                     gMASTER_STATE = TCL_STATE_COMPLETED;\
01535                                 }\
01536                             }\
01537                             /*update last executed L1_ack*/\
01538                             if( put_tm_here != NULL ){\
01539                                 if( GETshort_or_long_tm(put_tm_here) == SHORT_TM_CODE ){\
01540                                     for(int i = 0 ; i < TM_SHORT_SIZE ; ++i){\
01541                                         gLAST_TM[i] = put_tm_here->TM_string[i];\
01542                                         gLAST_TM_SHORT_OR_LONG = SHORT_TM_CODE;\
01543                                     }\
01544                                 }\
01545                                 else{\
01546                                     for( int i = 0 ; i < TM_LONG_SIZE ; ++i ){\
01547                                         gLAST_TM[i] = put_tm_here->TM_string[i];\
01548                                         gLAST_TM_SHORT_OR_LONG = LONG_TM_CODE;\
01549                                     }\
01550                                 }\
01551                             }\
01552                             /*PENDING: APPEND ACK L1*/\
01553                             Base_tm *tm_ptr = tm_ptr_head;\
01554                             get_ack_l1(tm_ptr->next_TM);\
01555                             int overflowCountExecute = 0;\
01556                             while( tm_ptr->next_TM != NULL ){\
01557                                 if( overflowCountExecute < TM_OVERFLOW_CONSTANT ){\
01558                                     tm_ptr = tm_ptr->next_TM;\
01559                                     ++overflowCountExecute;\
01560                                 }\
01561                                 else{\
01562                                     /*PENDING: RESET CDMS*/\
01563                                     break;\
01564                                 }\
01565                             }\
01566                             tm_ptr->next_TM = put_tm_here;\
01567                             /*CHECK FOR HOT PA*/\
01568                             uint8_t tempPAhot = 0x00;\
01569                             isPAhot(tempPAhot);\
01570                             if( tempPAhot == 0x00 ){\
01571                                 gFLAGS = gFLAGS | COM_PA_HOT_FLAG;\
01572                             }\
01573                             tm_ptr = tm_ptr_head;\
01574                             while( tm_ptr != NULL ){\
01575                                 int length = TM_SHORT_SIZE;\
01576                                 if( GETshort_or_long_tm(tm_ptr) == SHORT_TM_CODE ){\
01577                                     length = TM_SHORT_SIZE;\
01578                                 }\
01579                                 else{\
01580                                     length = TM_LONG_SIZE;\
01581                                 }\
01582                                 /*gPC.puts("Printing Call Sign, ACK_L1, TM list");*/\
01583                                 for(int i = 0 ; i < length ; ++i){\
01584                                     /*gPC.putc(tm_ptr->TM_string[i]);*/\
01585                                 }\
01586                                 tm_ptr = tm_ptr->next_TM;\
01587                             }\
01588                             /*SEND call sign, ACK_L1, OBSRS TO GS*/\
01589                             /*snd_tm.head_pointer(tm_ptr_head);*/\
01590                             /*adf_SDcard();*/\
01591                             /*DELETE THE TM AFTER USE*/\
01592                             tm_ptr = tm_ptr_head;\
01593                             overflowCountExecute = 0;\
01594                             while(tm_ptr != NULL){\
01595                                 if( overflowCountExecute < TM_OVERFLOW_CONSTANT ){\
01596                                     Base_tm *temp = tm_ptr->next_TM;\
01597                                     delete tm_ptr;\
01598                                     tm_ptr = temp;\
01599                                     ++overflowCountExecute;\
01600                                 }\
01601                                 else{\
01602                                     /*PENDING: RESET CDMS*/\
01603                                     break;\
01604                                 }\
01605                             }\
01606                         }\
01607                     }\
01608                     break;\
01609                 }\
01610                 current_TC = current_TC->next_TC;\
01611                 ++overflowCount;\
01612             }\
01613             else{\
01614                 /*PENDING: RESET CDMS*/\
01615                 break;\
01616             }\
01617         }\
01618         if( gFLAGS & COM_SESSION_TIMEOUT_FLAG ){\
01619             break;\
01620         }\
01621         else if( gMASTER_STATE == TCL_STATE_ABORTED ){\
01622             /*gPC.puts("ABORTING DUE TO ABORT ON NACK\r\n");*/\
01623             /*EXITED DUE TO ABORT ON NACK:*/\
01624             /*PENDING : POWER OFF COM TX*/\
01625             RX1M.attach(&rx_read, Serial::RxIrq);\
01626             gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);\
01627             break;\
01628         }\
01629         else if( gFLAGS & COM_PA_HOT_FLAG ){\
01630             /*PA HOT: WAIT FOR TIMEOUT*/\
01631             gCOM_PA_COOLING_TIMEOUT.attach(&after_cooling_pa, COM_PA_COOLING_TIME_LIMIT);\
01632             /*PENDING : POWER OFF COMM TX*/\
01633             RX1M.attach(&rx_read, Serial::RxIrq);\
01634             gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);\
01635             break;\
01636         }\
01637         /*gPC.printf("successflly executed %u tc\r\n", execute_psc);*/\
01638     }\
01639     if( (gMASTER_STATE == TCL_STATE_COMPLETED) || (gFLAGS & COM_SESSION_TIMEOUT_FLAG) ){\
01640         /*gPC.printf("completed or session timed out: %x\r\n", gMASTER_STATE);*/\
01641         /*COMPLETED EXECUTION OF TC*/\
01642         gMASTER_STATE = TCL_STATE_COMPLETED;\
01643         COM_POWER_OFF_TX;\
01644         reset_all;\
01645         /*PENDING : ENABLE THREADS*/\
01646         gSESSION_TIMEOUT.detach();\
01647         gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);\
01648         gFLAGS = gFLAGS & (~COM_SESSION_FLAG);\
01649     }\
01650 }