I2C code testing

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of COM_MNG_TMTC_SIMPLE_pl123 by Siva ram

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                 FCTN_I2C_WRITE_PL((char*)tc_ptr->TC_string,TC_LONG_SIZE);
01013             else 
01014             {
01015                 FCTN_I2C_WRITE_PL((char*)tc_ptr->TC_string,TC_SHORT_SIZE);
01016             }
01017             
01018             
01019             while(1)
01020             {
01021                 gLEDG = !gLEDG;
01022                 t_pl.start();
01023                 if(rcv_isr == 1)
01024                 {
01025                     #if DEBUG
01026                     gPC.printf("read_ack = %d",read_ack);
01027                     #endif
01028                     t_pl.stop();
01029                     gPC.printf("data:\n");
01030                     //gPC.printf("i2c_t = %d",t_pl.read_ms());  
01031                         for(int i = 0;i< 134;i++)
01032                         gPC.printf("%d\n",PL_I2C_DATA[i]);
01033                         rcv_isr = 0; 
01034                         break;
01035                 }
01036             }
01037             tm_pointer->next_TM = NULL;                 return tm_ptr;
01038             break;
01039         }
01040         default: //invalid TC
01041         {
01042             //printf("INVALID TC\r\n");
01043             //tm_pointer = new Short_tm; 
01044             tm_pointer->TM_string[0] = 0xB0; 
01045             tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
01046             tm_pointer->TM_string[2] = 0x01;
01047             tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
01048             tm_pointer->TM_string[4] = 0x01;
01049             for(uint8_t i=0;i<6;i++)
01050             {
01051                 tm_pointer->TM_string[i+5] = 0;
01052             }
01053             crc16 = crc16_gen(tm_ptr->TM_string,11);
01054             tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
01055             tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
01056             tm_pointer->next_TM = NULL;                 return tm_ptr;
01057             break;
01058         }
01059      }
01060 }
01061 // CDMS TEAM CODE END
01062 // EXECUTE OBOSC
01063 #define execute_obosc_core(tc_ptr, tm_ptr, reset_flag) {\
01064     uint8_t service_subtype = (tc_ptr->TC_string[2]) & 0x0F;\
01065     uint8_t temp8 = 0x00;\
01066     Base_tc *modify_this = gHEAD_NODE_TCL;\
01067     int modify_overflow = 0x00;\
01068     switch( service_subtype ){\
01069         case OBOSC_SUB_DISABLE:\
01070             /*gPC.puts("target psc are ");*/\
01071             for(int i = 3 ; i < 9 ; ++i){\
01072                 /*gPC.printf("%u ", tc_ptr->TC_string[i]);*/\
01073             }\
01074             /*gPC.puts("\r\n");*/\
01075             /*gPC.puts("disable type obosc\r\n");*/\
01076             while( modify_this != NULL ){\
01077                 if( modify_overflow < TCL_OVERFLOW_CONSTANT ){\
01078                     uint8_t modify_psc = GETpacket_seq_count(modify_this);\
01079                     /*gPC.printf("mosify_psc = %u\r\n", modify_psc);*/\
01080                     for( int i = 3 ; i < 9 ; ++i ){\
01081                         uint8_t target_psc = tc_ptr->TC_string[i];\
01082                         if( (target_psc == modify_psc) && (target_psc != 0) ){\
01083                             uint16_t tempExec = TC_STATE_DISABLED;\
01084                             PUTexec_status( modify_this, tempExec );\
01085                             break;\
01086                         }\
01087                     }\
01088                     modify_this = modify_this->next_TC;\
01089                     ++modify_overflow;\
01090                 }\
01091                 else{\
01092                     /*PENDING: RESET CDMS*/\
01093                     break;\
01094                 }\
01095             }\
01096             break;\
01097         case OBOSC_SUB_RETRY:\
01098             /*gPC.puts("retry type obosc\r\n");*/\
01099             while( modify_this != NULL ){\
01100                 if( modify_overflow < TCL_OVERFLOW_CONSTANT ){\
01101                     uint8_t modify_psc = GETpacket_seq_count(modify_this);\
01102                     for( int i = 3 ; i < 9 ; ++i ){\
01103                         uint8_t target_psc = tc_ptr->TC_string[i];\
01104                         if( (target_psc == modify_psc) && (target_psc != 0) ){\
01105                             uint16_t tempExec = TC_STATE_MARKED_RETRY;\
01106                             PUTexec_status( modify_this, tempExec );\
01107                             /*gPC.puts("reqquired tc found: disable\r\n");*/\
01108                             break;\
01109                         }\
01110                     }\
01111                     modify_this = modify_this->next_TC;\
01112                     ++modify_overflow;\
01113                 }\
01114                 else{\
01115                     /*PENDING: RESET CDMS*/\
01116                     break;\
01117                 }\
01118             }\
01119             break;\
01120     }\
01121     Base_tm *ackl234new = new Short_tm;\
01122     ackl234new->next_TM = NULL;\
01123     /*return telemetry pointer here*/\
01124     tm_ptr = ackl234new;\
01125     ackl234new->TM_string[0] = TMID_ACK_L234 << 4;\
01126     ackl234new->TM_string[1] = GETpacket_seq_count(tc_ptr);\
01127     ackl234new->TM_string[2] = 0xA0;\
01128     for(int i = 3; i < 11; ++i){\
01129         ackl234new->TM_string[i] = 0x00;\
01130     }\
01131     uint16_t crc = crc16_gen(ackl234new->TM_string, TM_SHORT_SIZE-2);\
01132     ackl234new->TM_string[11] = (crc & 0xFF00) >> 8;\
01133     ackl234new->TM_string[12] = crc & 0x00FF;\
01134     /*ack l234 old*/\
01135     if( service_subtype == OBOSC_SUB_REP_LE ){\
01136         if( gLAST_TM_SHORT_OR_LONG == SHORT_TM_CODE ){\
01137             Base_tm *ackl234old = new Short_tm;\
01138             ackl234old->next_TM = NULL;\
01139             /*APPEND TO ACK L234 NEW HERE*/\
01140             ackl234new->next_TM = ackl234old;\
01141             for( int i = 0 ; i < TM_SHORT_SIZE ; ++i ){\
01142                 ackl234old->TM_string[i] = gLAST_TM[i];\
01143             }\
01144         }\
01145         else{\
01146             Base_tm *tempLongTM = new Long_tm;\
01147             tempLongTM->next_TM = NULL;\
01148             /*APPEND TO ACK L234 NEW HERE*/\
01149             ackl234new->next_TM = tempLongTM;\
01150             for( int i = 0 ; i < TM_LONG_SIZE ; ++i ){\
01151                 tempLongTM->TM_string[i] = gLAST_TM[i];\
01152             }\
01153         }\
01154     }\
01155     else if( service_subtype == OBOSC_SUB_REP_TCLD ){\
01156         get_tc_list(ackl234new->next_TM, GETpacket_seq_count(tc_ptr));\
01157     }\
01158     else if( service_subtype == OBOSC_SUB_RESET ){\
01159         /*reset_all;*/\
01160         reset_flag = 1;\
01161         /*PENDING: VERIFY reset_all, RESET CDMS*/\
01162     }\
01163     else if( (service_subtype != OBOSC_SUB_DISABLE) && (service_subtype != OBOSC_SUB_RETRY) ){\
01164         /*CHANGE THE ACK CODE TO INVALID TC*/\
01165         ackl234new->TM_string[2] = 0xA2;\
01166     }\
01167     /*gPC.puts("completed obosc\r\n");*/\
01168 }
01169 
01170 /*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 */
01171 #define get_call_sign(tm_ptr) {\
01172     Base_tm* call_sign_tm = new Short_tm;\
01173     call_sign_tm->next_TM = NULL;\
01174     tm_ptr = call_sign_tm;\
01175     for( int i = 0 ; i < TM_SHORT_SIZE ; ++i ){\
01176         call_sign_tm->TM_string[i] = gCALL_SIGN_STRING[i];\
01177     }\
01178 }
01179 
01180 #define EXECUTE_OBOSC_ONLY {\
01181     int reset_flag = 0;\
01182     for(uint8_t execute_psc = PSC_START_VALUE ; execute_psc < gTOTAL_VALID_TC ; ++execute_psc){\
01183         Base_tc* current_TC = gHEAD_NODE_TCL;\
01184         int overCount = 0;\
01185         while( current_TC != NULL ){\
01186             if( overCount < TCL_OVERFLOW_CONSTANT ){\
01187                 if( (GETcrc_pass(current_TC) == 1) && (GETpacket_seq_count(current_TC) == execute_psc) ){\
01188                     /*CHECK FOR OBOSC*/\
01189                     uint8_t temp82 = 0x00;\
01190                     isit_obosc(current_TC, temp82);\
01191                     if(temp82 == 0x01){\
01192                         uint16_t current_exec_status = GETexec_status(current_TC);\
01193                         if( (current_exec_status == TC_STATE_SUCCESSFULLY_EXECUTED) || (current_exec_status == TC_STATE_DISABLED) )\
01194                             break;\
01195                         else if( (current_exec_status == TC_STATE_UNEXECUTED) || (current_exec_status == TC_STATE_MARKED_RETRY) ){\
01196                             /*gPC.printf("It is obosc: %u\r\n", execute_psc);*/\
01197                             /*EXECUTION OF OBOSC TC*/\
01198                             /*WARNING: LARGE MEMORY UTILIZATION FOR TC-LIST-REPORT */\
01199                             Base_tm *obosc_tm_head = NULL;\
01200                             get_call_sign(obosc_tm_head);\
01201                             Base_tm *obosc_tm_core = NULL;\
01202                             execute_obosc_core(current_TC, obosc_tm_core, reset_flag);\
01203                             uint16_t temp16 = TC_STATE_SUCCESSFULLY_EXECUTED;\
01204                             PUTexec_status(current_TC, temp16);\
01205                             Base_tm *obosc_tm_current = obosc_tm_head;\
01206                             get_ack_l1(obosc_tm_current->next_TM);\
01207                             int overflowCountOBONLY = 0;\
01208                             while( obosc_tm_current->next_TM != NULL ){\
01209                                 if( overflowCountOBONLY < TM_OVERFLOW_CONSTANT ){\
01210                                     obosc_tm_current = obosc_tm_current->next_TM;\
01211                                     ++overflowCountOBONLY;\
01212                                 }\
01213                                 else{\
01214                                     /*PENDING: RESET CDMS*/\
01215                                     break;\
01216                                 }\
01217                             }\
01218                             obosc_tm_current->next_TM = obosc_tm_core;\
01219                             obosc_tm_current = obosc_tm_head;\
01220                             while( obosc_tm_current != NULL ){\
01221                                 int length = TM_SHORT_SIZE;\
01222                                 if( GETshort_or_long_tm(obosc_tm_current) == SHORT_TM_CODE ){\
01223                                     length = TM_SHORT_SIZE;\
01224                                 }\
01225                                 else{\
01226                                     length = TM_LONG_SIZE;\
01227                                 }\
01228                                 for(int i = 0 ; i < length ; ++i){\
01229                                     /*gPC.putc(obosc_tm_current->TM_string[i]);*/\
01230                                 }\
01231                                 obosc_tm_current = obosc_tm_current->next_TM;\
01232                             }\
01233                             /*Sending OBOSC TM to GS*/\
01234                             /*snd_tm.head_pointer(obosc_tm_head);*/\
01235                             /*transmit_adf;*/\
01236                             /*DELETE THE TM AFTER USE*/\
01237                             obosc_tm_current = obosc_tm_head;\
01238                             int overCount = 0;\
01239                             while( obosc_tm_current != NULL ){\
01240                                 if( (overCount < TM_OVERFLOW_CONSTANT) ){\
01241                                     Base_tm *temp = obosc_tm_current->next_TM;\
01242                                     delete obosc_tm_current;\
01243                                     obosc_tm_current = temp;\
01244                                     ++overCount;\
01245                                 }\
01246                                 else{\
01247                                     /*PENDING: RESET CDMS: MEMORY LEAK FOUND*/\
01248                                     break;\
01249                                 }\
01250                             }\
01251                             if ( reset_flag ==1 ){\
01252                                 reset_all;\
01253                             }\
01254                         }\
01255                     }\
01256                 }\
01257                 current_TC = current_TC->next_TC;\
01258                 ++overCount;\
01259             }\
01260             else{\
01261                 /*PENDING: REST CDMS: MEMORY LEAK FOUND*/\
01262                 break;\
01263             }\
01264         }\
01265     }\
01266 }
01267 
01268 #define EXECUTE_TC {\
01269     gMASTER_STATE = TCL_STATE_EXECUTING;\
01270     /*gPC.printf("%u\r\n", gTOTAL_VALID_TC);*/\
01271     for(uint8_t execute_psc = PSC_START_VALUE ; execute_psc < gTOTAL_VALID_TC ; ++execute_psc ){\
01272         /*gPC.printf("executing normal %u\r\n", execute_psc);*/\
01273         /*gLEDG = !gLEDG;*/\
01274         /*gLEDR = !gLEDR;*/\
01275         Base_tc* current_TC = gHEAD_NODE_TCL;\
01276         int overflowCount = 0;\
01277         while(current_TC != NULL){\
01278             if( overflowCount < TCL_OVERFLOW_CONSTANT ){\
01279                 if( (GETcrc_pass(current_TC) == 1) && (GETpacket_seq_count(current_TC) == execute_psc) ){\
01280                     uint8_t current_exec_status = GETexec_status(current_TC);\
01281                     if( (current_exec_status == TC_STATE_SUCCESSFULLY_EXECUTED) || (current_exec_status == TC_STATE_DISABLED) ){\
01282                         /*gPC.printf("disabled or completed at %u\n", execute_psc);*/\
01283                         break;\
01284                     }\
01285                     else if( (current_exec_status == TC_STATE_EXECUTION_FAILED) && (GETabort_on_nack(current_TC) == 1) ){\
01286                         /*gPC.printf("abort on nack at %u psc\r\n", execute_psc);*/\
01287                         gMASTER_STATE = TCL_STATE_ABORTED;\
01288                         Base_tm *tm_ptr_head = NULL;\
01289                         get_call_sign(tm_ptr_head);\
01290                         Base_tm *tm_ptr = tm_ptr_head;\
01291                         get_ack_l1(tm_ptr->next_TM);\
01292                         tm_ptr = tm_ptr_head;\
01293                         while( tm_ptr != NULL ){\
01294                             int length = TM_SHORT_SIZE;\
01295                             if( GETshort_or_long_tm(tm_ptr) == SHORT_TM_CODE ){\
01296                                 length = TM_SHORT_SIZE;\
01297                             }\
01298                             else{\
01299                                 length = TM_LONG_SIZE;\
01300                             }\
01301                             /*gPC.puts("Printing Call Sign, ACK_L1, TM list");*/\
01302                             for(int i = 0 ; i < length ; ++i){\
01303                                 /*gPC.putc(tm_ptr->TM_string[i]);*/\
01304                             }\
01305                             tm_ptr = tm_ptr->next_TM;\
01306                         }\ 
01307                         /*Send only call sign, ACK_L1 to GS*/\
01308                         /*snd_tm.head_pointer(tm_ptr_head);*/\
01309                         /*transmit_adf;*/\
01310                         /*DELETE THE TM AFTER USE*/\
01311                         tm_ptr = tm_ptr_head;\
01312                         int overflowCountExecute = 0;\
01313                         while(tm_ptr != NULL){\
01314                             if( overflowCountExecute < TM_OVERFLOW_CONSTANT ){\
01315                                 Base_tm *temp = tm_ptr->next_TM;\
01316                                 delete tm_ptr;\
01317                                 tm_ptr = temp;\
01318                                 ++overflowCountExecute;\
01319                             }\
01320                             else{\
01321                                 /*PENDING: RESET CDMS*/\
01322                                 break;\
01323                             }\
01324                         }\
01325                         break;\
01326                     }\
01327                     else if( (current_exec_status == TC_STATE_UNEXECUTED) || (current_exec_status == TC_STATE_MARKED_RETRY) ){\
01328                         /*EXECUTION OF TC START*/\
01329                         uint8_t temp81 = 0x00;\
01330                         isit_sdcard(current_TC, temp81);\
01331                         if( temp81 == 0x00 ){\
01332                             /*gPC.printf("non sd card at %u\r\n", execute_psc);*/\
01333                             /*EXECUTION OF NON SD-CARD (BOTH OBOSC and CDMS functions)*/\
01334                             Base_tm *tm_ptr_head = NULL;\
01335                             get_call_sign(tm_ptr_head);\
01336                             Base_tm *put_tm_here = NULL;\
01337                             uint8_t temp82 = 0x00;\
01338                             uint8_t tempPAhot = 0x00;\
01339                             isit_obosc(current_TC, temp82);\
01340                             if(temp82 == 0x01){\
01341                                 /*gPC.printf("obosc tc inside normal tc at %u\r\n", execute_psc);*/\
01342                                 /*EXECUTION OF OBOSC TC*/\
01343                                 /*SKIP EXECUTION OF OBOSC HERE*/\
01344                                 break;\
01345                             }\
01346                             else{\
01347                                 /*gPC.printf("cdms relay tmtc at %u\r\n", execute_psc);*/\
01348                                 /*call CDMS_RLY_TMTC*/\
01349                                 /*CDMS_RLY_TMTC(current_TC, put_tm_here);*/\
01350                                 put_tm_here =  FCTN_CDMS_RLY_TMTC(current_TC);\
01351                             }\
01352                             /*DETECT ACK OR NACK*/\
01353                             uint8_t temp83 = 0x00;\
01354                             uint8_t temp84 = GETpacket_seq_count(current_TC);\
01355                             detect_ack(put_tm_here, temp83, temp84);\
01356                             if( temp83 == 0x01){\
01357                                 uint16_t temp16 = TC_STATE_SUCCESSFULLY_EXECUTED;\
01358                                 PUTexec_status(current_TC, temp16);\
01359                             }\
01360                             else{\
01361                                 /*gPC.puts("TC_STATE_EXECUTION_FAILED");*/\
01362                                 uint16_t temp16 = TC_STATE_EXECUTION_FAILED;\
01363                                 PUTexec_status(current_TC, temp16);\
01364                             }\
01365                             /*ABORT ON NACK AND TC LIST COMPLETED: UPDATE IN gMASTERSTATE*/\
01366                             if( (GETexec_status(current_TC) == TC_STATE_EXECUTION_FAILED) && (GETabort_on_nack(current_TC) == 1) ){\
01367                                 gMASTER_STATE = TCL_STATE_ABORTED;\
01368                             }\
01369                             else if( execute_psc == (gTOTAL_VALID_TC-1) ){\
01370                                 /*LAST TC IS EXECUTED*/\
01371                                 gMASTER_STATE = TCL_STATE_COMPLETED;\
01372                             }\
01373                             else{\
01374                                 uint8_t check1 = 1;\
01375                                 for( uint8_t test_psc = execute_psc+1 ; test_psc < gTOTAL_VALID_TC ; ++test_psc ){\
01376                                     Base_tc* test_TC = gHEAD_NODE_TCL;\
01377                                     while(test_TC != NULL){\
01378                                         if( GETpacket_seq_count(test_TC) == test_psc ){\
01379                                             uint8_t checkval;\
01380                                             isit_obosc(test_TC, checkval);\
01381                                             if( checkval == 0 ){\
01382                                                 check1 = 0;\
01383                                             }\
01384                                         }\
01385                                         test_TC = test_TC->next_TC;\
01386                                     }\
01387                                 }\
01388                                 if( check1 == 0x01 ){\
01389                                     gMASTER_STATE = TCL_STATE_COMPLETED;\
01390                                 }\
01391                             }\
01392                             /*update last executed L1_ack*/\
01393                             if( put_tm_here != NULL ){\
01394                                 if( GETshort_or_long_tm(put_tm_here) == SHORT_TM_CODE ){\
01395                                     for(int i = 0 ; i < TM_SHORT_SIZE ; ++i){\
01396                                         gLAST_TM[i] = put_tm_here->TM_string[i];\
01397                                         gLAST_TM_SHORT_OR_LONG = SHORT_TM_CODE;\
01398                                     }\
01399                                 }\
01400                                 else{\
01401                                     for( int i = 0 ; i < TM_LONG_SIZE ; ++i ){\
01402                                         gLAST_TM[i] = put_tm_here->TM_string[i];\
01403                                         gLAST_TM_SHORT_OR_LONG = LONG_TM_CODE;\
01404                                     }\
01405                                 }\
01406                             }\
01407                             /*PENDING: APPEND ACK L1*/\
01408                             Base_tm *tm_ptr = tm_ptr_head;\
01409                             get_ack_l1(tm_ptr->next_TM);\
01410                             int overflowCountExecute = 0;\
01411                             while( tm_ptr->next_TM != NULL ){\
01412                                 if( overflowCountExecute < TM_OVERFLOW_CONSTANT ){\
01413                                     tm_ptr = tm_ptr->next_TM;\
01414                                     ++overflowCountExecute;\
01415                                 }\
01416                                 else{\
01417                                     /*PENDING: RESET CDMS*/\
01418                                     break;\
01419                                 }\
01420                             }\
01421                             tm_ptr->next_TM = put_tm_here;\
01422                             /*CHECK FOR HOT PA*/\
01423                             isPAhot(tempPAhot);\
01424                             if( tempPAhot == 0x00 ){\
01425                                 gFLAGS = gFLAGS | COM_PA_HOT_FLAG;\
01426                             }\
01427                             tm_ptr = tm_ptr_head;\
01428                             while( tm_ptr != NULL ){\
01429                                 int length = TM_SHORT_SIZE;\
01430                                 if( GETshort_or_long_tm(tm_ptr) == SHORT_TM_CODE ){\
01431                                     length = TM_SHORT_SIZE;\
01432                                 }\
01433                                 else{\
01434                                     length = TM_LONG_SIZE;\
01435                                 }\
01436                                 /*gPC.puts("Printing Call Sign, ACK_L1, TM list");*/\
01437                                 for(int i = 0 ; i < length ; ++i){\
01438                                     /*gPC.putc(tm_ptr->TM_string[i]);*/\
01439                                 }\
01440                                 tm_ptr = tm_ptr->next_TM;\
01441                             }\
01442                             /*SEND call sign, ACK_L1, NON OBSRS TM TO GS*/\
01443                             snd_tm.head_pointer(tm_ptr_head);\
01444                             transmit_adf;\
01445                             /*DELETE THE TM AFTER USE*/\
01446                             tm_ptr = tm_ptr_head;\
01447                             overflowCountExecute = 0;\
01448                             while(tm_ptr != NULL){\
01449                                 if( overflowCountExecute < TM_OVERFLOW_CONSTANT ){\
01450                                     Base_tm *temp = tm_ptr->next_TM;\
01451                                     delete tm_ptr;\
01452                                     tm_ptr = temp;\
01453                                     ++overflowCountExecute;\
01454                                 }\
01455                                 else{\
01456                                     /*PENDING: RESET CDMS*/\
01457                                     break;\
01458                                 }\ 
01459                             }\
01460                         }\
01461                         else{\
01462                             /*gPC.printf("sd card at %u\r\n", execute_psc);*/\
01463                             /*EXECUTION OF SD-CARD DATA SENDING (OBSRS)*/\
01464                             Base_tm *tm_ptr_head = NULL;\
01465                             get_call_sign(tm_ptr_head);\
01466                             Base_tm *put_tm_here = NULL;\
01467                             /*execute_obsrs(current_TC, put_tm_here)*/\
01468                             /*read_TC(current_TC);*/\
01469                             execute_OBSRS_TC(current_TC);\
01470                             transmit_adf;\
01471                             uint8_t tempExec = TC_STATE_SUCCESSFULLY_EXECUTED;\
01472                             PUTexec_status(current_TC, tempExec);\
01473                             /*PENDING: ABORT ON NACK CHECK, gMASTER_STATE VERIFICATION WITH SD CARD, session timeout, last executed ack l234*/\
01474                             if( (GETexec_status(current_TC) == TC_STATE_EXECUTION_FAILED) && (GETabort_on_nack(current_TC) == 1) ){\
01475                                 gMASTER_STATE = TCL_STATE_ABORTED;\
01476                             }\
01477                             else if( execute_psc == (gTOTAL_VALID_TC-1) ){\
01478                                 gMASTER_STATE = TCL_STATE_COMPLETED;\
01479                             }\
01480                             else{\
01481                                 uint8_t check1 = 1;\
01482                                 for( uint8_t test_psc = execute_psc+1 ; test_psc < gTOTAL_VALID_TC ; ++test_psc ){\
01483                                     Base_tc* test_TC = gHEAD_NODE_TCL;\
01484                                     while(test_TC != NULL){\
01485                                         if( GETpacket_seq_count(test_TC) == test_psc ){\
01486                                             uint8_t checkval;\
01487                                             isit_obosc(test_TC, checkval);\
01488                                             if( checkval == 0 ){\
01489                                                 check1 = 0;\
01490                                             }\
01491                                         }\
01492                                         test_TC = test_TC->next_TC;\
01493                                     }\
01494                                 }\
01495                                 if( check1 == 0x01 ){\
01496                                     gMASTER_STATE = TCL_STATE_COMPLETED;\
01497                                 }\
01498                             }\
01499                             /*update last executed L1_ack*/\
01500                             if( put_tm_here != NULL ){\
01501                                 if( GETshort_or_long_tm(put_tm_here) == SHORT_TM_CODE ){\
01502                                     for(int i = 0 ; i < TM_SHORT_SIZE ; ++i){\
01503                                         gLAST_TM[i] = put_tm_here->TM_string[i];\
01504                                         gLAST_TM_SHORT_OR_LONG = SHORT_TM_CODE;\
01505                                     }\
01506                                 }\
01507                                 else{\
01508                                     for( int i = 0 ; i < TM_LONG_SIZE ; ++i ){\
01509                                         gLAST_TM[i] = put_tm_here->TM_string[i];\
01510                                         gLAST_TM_SHORT_OR_LONG = LONG_TM_CODE;\
01511                                     }\
01512                                 }\
01513                             }\
01514                             /*PENDING: APPEND ACK L1*/\
01515                             Base_tm *tm_ptr = tm_ptr_head;\
01516                             get_ack_l1(tm_ptr->next_TM);\
01517                             int overflowCountExecute = 0;\
01518                             while( tm_ptr->next_TM != NULL ){\
01519                                 if( overflowCountExecute < TM_OVERFLOW_CONSTANT ){\
01520                                     tm_ptr = tm_ptr->next_TM;\
01521                                     ++overflowCountExecute;\
01522                                 }\
01523                                 else{\
01524                                     /*PENDING: RESET CDMS*/\
01525                                     break;\
01526                                 }\
01527                             }\
01528                             tm_ptr->next_TM = put_tm_here;\
01529                             /*CHECK FOR HOT PA*/\
01530                             uint8_t tempPAhot = 0x00;\
01531                             isPAhot(tempPAhot);\
01532                             if( tempPAhot == 0x00 ){\
01533                                 gFLAGS = gFLAGS | COM_PA_HOT_FLAG;\
01534                             }\
01535                             tm_ptr = tm_ptr_head;\
01536                             while( tm_ptr != NULL ){\
01537                                 int length = TM_SHORT_SIZE;\
01538                                 if( GETshort_or_long_tm(tm_ptr) == SHORT_TM_CODE ){\
01539                                     length = TM_SHORT_SIZE;\
01540                                 }\
01541                                 else{\
01542                                     length = TM_LONG_SIZE;\
01543                                 }\
01544                                 /*gPC.puts("Printing Call Sign, ACK_L1, TM list");*/\
01545                                 for(int i = 0 ; i < length ; ++i){\
01546                                     /*gPC.putc(tm_ptr->TM_string[i]);*/\
01547                                 }\
01548                                 tm_ptr = tm_ptr->next_TM;\
01549                             }\
01550                             /*SEND call sign, ACK_L1, OBSRS TO GS*/\
01551                             /*snd_tm.head_pointer(tm_ptr_head);*/\
01552                             /*adf_SDcard();*/\
01553                             /*DELETE THE TM AFTER USE*/\
01554                             tm_ptr = tm_ptr_head;\
01555                             overflowCountExecute = 0;\
01556                             while(tm_ptr != NULL){\
01557                                 if( overflowCountExecute < TM_OVERFLOW_CONSTANT ){\
01558                                     Base_tm *temp = tm_ptr->next_TM;\
01559                                     delete tm_ptr;\
01560                                     tm_ptr = temp;\
01561                                     ++overflowCountExecute;\
01562                                 }\
01563                                 else{\
01564                                     /*PENDING: RESET CDMS*/\
01565                                     break;\
01566                                 }\
01567                             }\
01568                         }\
01569                     }\
01570                     break;\
01571                 }\
01572                 current_TC = current_TC->next_TC;\
01573                 ++overflowCount;\
01574             }\
01575             else{\
01576                 /*PENDING: RESET CDMS*/\
01577                 break;\
01578             }\
01579         }\
01580         if( gFLAGS & COM_SESSION_TIMEOUT_FLAG ){\
01581             break;\
01582         }\
01583         else if( gMASTER_STATE == TCL_STATE_ABORTED ){\
01584             /*gPC.puts("ABORTING DUE TO ABORT ON NACK\r\n");*/\
01585             /*EXITED DUE TO ABORT ON NACK:*/\
01586             /*PENDING : POWER OFF COM TX*/\
01587             RX1M.attach(&rx_read, Serial::RxIrq);\
01588             gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);\
01589             break;\
01590         }\
01591         else if( gFLAGS & COM_PA_HOT_FLAG ){\
01592             /*PA HOT: WAIT FOR TIMEOUT*/\
01593             gCOM_PA_COOLING_TIMEOUT.attach(&after_cooling_pa, COM_PA_COOLING_TIME_LIMIT);\
01594             /*PENDING : POWER OFF COMM TX*/\
01595             RX1M.attach(&rx_read, Serial::RxIrq);\
01596             gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);\
01597             break;\
01598         }\
01599         /*gPC.printf("successflly executed %u tc\r\n", execute_psc);*/\
01600     }\
01601     if( (gMASTER_STATE == TCL_STATE_COMPLETED) || (gFLAGS & COM_SESSION_TIMEOUT_FLAG) ){\
01602         /*gPC.printf("completed or session timed out: %x\r\n", gMASTER_STATE);*/\
01603         /*COMPLETED EXECUTION OF TC*/\
01604         gMASTER_STATE = TCL_STATE_COMPLETED;\
01605         COM_POWER_OFF_TX;\
01606         reset_all;\
01607         /*PENDING : ENABLE THREADS*/\
01608         gSESSION_TIMEOUT.detach();\
01609         gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);\
01610         gFLAGS = gFLAGS & (~COM_SESSION_FLAG);\
01611     }\
01612 }