⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ext_svr.c

📁 RT9S12 target document
💻 C
📖 第 1 页 / 共 4 页
字号:
    * fields:
    *
    * CS1 - checksum 1 (uint32_T)
    * CS2 - checksum 2 (uint32_T)
    * CS3 - checksum 3 (uint32_T)
    * CS4 - checksum 4 (uint32_T)
    *
    * intCodeOnly   - flag indicating if target is integer only (uint32_T)
    * 
    * targetStatus  - the status of the target (uint32_T)
    *
    * nDataTypes    - # of data types        (uint32_T)
    * dataTypeSizes - 1 per nDataTypes       (uint32_T[])
    */
   {
      int   nPktEls=4+                        /* checkSums       */
      1+                        /* intCodeOnly     */
      1+                        /* targetStatus    */
      1+                        /* nDataTypes      */
      dtGetNumDataTypes(dtInfo); /* data type sizes */

      tmpBufSize=nPktEls*sizeof(uint32_T);

      PRINT_DEBUG_MSG_LVL3("Allocating memory for temporary buffer for 2nd EXT_CONNECT_RESPONSE (");
      PRINT_DEBUG_MSG_LVL3_UDec((uint16_T)tmpBufSize);
      PRINT_DEBUG_MSG_LVL3_Raw(" bytes)\n\r");
      tmpBuf=(uint32_T*)malloc(tmpBufSize);

      if(tmpBuf==NULL) {

         abort_LED(51);

         error=EXT_ERROR;
         goto EXIT_POINT;

      }
   }

   /* Send packet header. */
   pktHdr.type=EXT_CONNECT_RESPONSE;
   pktHdr.size=tmpBufSize;

   error=ExtSetHostPkt(extUD, sizeof(pktHdr), (char_T*)&pktHdr, &nSet);
   if(error||(nSet!=sizeof(pktHdr))) {

      abort_LED(52);

      //fprintf(stderr,
      //    "ExtSetHostPkt() failed for 2nd EXT_CONNECT_RESPONSE.\n");
      goto EXIT_POINT;

   }

   /* Checksums, target status & SL_DOUBLESize. */
   tmpBuf[0]=rteiGetChecksum0(ei);
   tmpBuf[1]=rteiGetChecksum1(ei);
   tmpBuf[2]=rteiGetChecksum2(ei);
   tmpBuf[3]=rteiGetChecksum3(ei);

   #if INTEGER_CODE == 0
   tmpBuf[4]=(uint32_T)0;
   #else
   tmpBuf[4]=(uint32_T)1;
   #endif

   tmpBuf[5]=(uint32_T)modelStatus;

   /* nDataTypes and dataTypeSizes */
   tmpBuf[6]=(uint32_T)nDataTypes;
   // the following line implies that the data types are stored as uint32_T
   // (not the case on 16-bit targets such as the 9s12...   fw/06/07
   //
   // (void)memcpy(&tmpBuf[7], dtSizes, sizeof(uint32_T)*nDataTypes);
   {
      int_T i;

      for(i=0; i<nDataTypes; i++) {

         tmpBuf[7+i]=(uint32_T)dtSizes[i];

      }
   }

   /* Send the packet. */
   error=ExtSetHostPkt(extUD, tmpBufSize, (char_T*)tmpBuf, &nSet);
   if(error||(nSet!=tmpBufSize)) {

      abort_LED(53);

      //fprintf(stderr,
      //    "ExtSetHostPkt() failed.\n");
      goto EXIT_POINT;

   }

   commInitialized=TRUE;
   //PRINT_DEBUG_MSG_LVL1("commInitialized = TRUE\n\r");


   EXIT_POINT:PRINT_DEBUG_MSG_LVL3("Freeing memory of temporary buffer (2nd EXT_CONNECT_RESPONSE)\n\r");
   free(tmpBuf);

   PRINT_DEBUG_MSG_LVL3("OUT, error status: ");
   PRINT_DEBUG_MSG_LVL3_UDec(error);
   PRINT_DEBUG_MSG_NL3;

   return error;

} /* end ProcessConnectPkt */


/* Function: SendPktHdrToHost ==================================================
 * Abstract:
 *  Send a packet header to the host.
 */
PRIVATE boolean_T SendPktHdrToHost(const ExtModeAction action, const int           size)  /* # of bytes to follow pkt header */ {

   int_T       nSet;
   PktHeader   pktHdr;
   boolean_T   error=EXT_NO_ERROR;

   pktHdr.type=(uint32_T)action;
   pktHdr.size=size;

   error=ExtSetHostPkt(extUD, sizeof(pktHdr), (char_T*)&pktHdr, &nSet);
   if(error||(nSet!=sizeof(pktHdr))) {

      abort_LED(54);    /* indicates that 'nSet != sizeof(pktHdr)'  --  fw-07-07 */

      //fprintf(stderr,"ExtSetHostPkt() failed.\n");
      error=EXT_ERROR;
      goto EXIT_POINT;

   }

   EXIT_POINT:return(error);

} /* end SendPktHdrToHost */


/* Function: SendPktDataToHost =================================================
 * Abstract:
 *  Send packet data to host. You are responsible for sending a header
 *  prior to sending the header.
 */
PRIVATE boolean_T SendPktDataToHost(const char *data, const int size) {

   int_T       nSet;
   boolean_T   error=EXT_NO_ERROR;

   error=ExtSetHostPkt(extUD, size, data, &nSet);
   if(error||(nSet!=size)) {

      abort_LED(55);

      //fprintf(stderr,"ExtSetHostPkt() failed.\n");
      error=EXT_ERROR;
      goto EXIT_POINT;

   }

   EXIT_POINT:return(error);

} /* end SendPktDataToHost */


/* Function: SendPktToHost =====================================================
 * Abstract:
 *  Send a packet to the host.  Packets can be of two forms:
 *      o packet header only
 *          the type is used as a flag to notify Simulink of an event
 *          that has taken place on the target (event == action == type)
 *      o pkt header, followed by data
 */
PUBLIC boolean_T SendPktToHost(const ExtModeAction action, const int size, /* # of bytes to follow pkt header */
    const char *data) {

   boolean_T   error=EXT_NO_ERROR;

   // local function name... debugging only
   #if DEBUG_MSG_LVL > 0
   const char     *funct_name="SendPktToHost";
   #endif

   PRINT_DEBUG_MSG_LVL2("Sending action ");
   PRINT_DEBUG_MSG_LVL2_Raw(debugActionStrings[(uint_T)action]);
   PRINT_DEBUG_MSG_LVL2_Raw(" (");
   PRINT_DEBUG_MSG_LVL2_UDec((uint16_T)action);
   PRINT_DEBUG_MSG_LVL2_Raw(") with size ");
   PRINT_DEBUG_MSG_LVL2_UDec((uint16_T)size);
   PRINT_DEBUG_MSG_NL2;

   error=SendPktHdrToHost(action, size);
   if(error!=EXT_NO_ERROR) {

      abort_LED(56);

      goto EXIT_POINT;

   }
   PRINT_DEBUG_MSG_LVL2("... action and size sent.\n\r");

   if(data!=NULL) {

     PRINT_DEBUG_MSG_LVL2("Sending data...\n\r");

      error=SendPktDataToHost(data, size);
      if(error!=EXT_NO_ERROR) {

         abort_LED(57);

         goto EXIT_POINT;

      }

     PRINT_DEBUG_MSG_LVL2("... data sent.\n\r");
   }
   else {

      assert(size==0);

   }

   EXIT_POINT:return(error);

} /* end SendPktToHost */


/* Function:  SendResponseStatus ===============================================
 *  
 */
PRIVATE boolean_T SendResponseStatus(const ExtModeAction  response, const ResponseStatus status, int32_T upInfoIdx) {

   int32_T     msg[2];
   boolean_T   error=EXT_NO_ERROR;

   // local function name... debugging only
   #if DEBUG_MSG_LVL > 0
   const char  *funct_name="SendResponseStatus";
   #endif

   PRINT_DEBUG_MSG_LVL2("IN\n\r");

   msg[0]=(int32_T)status;
   msg[1]=upInfoIdx;

   PRINT_DEBUG_MSG_LVL2("Response status: ");
   PRINT_DEBUG_MSG_LVL2_UDec((uint16_T)status);
   PRINT_DEBUG_MSG_LVL2_Raw(" (upInfoIdx = ");
   PRINT_DEBUG_MSG_LVL2_UDec((uint16_T)upInfoIdx);
   PRINT_DEBUG_MSG_LVL2_Raw(")\n\r");
   PRINT_DEBUG_MSG_NL2;

   error=SendPktToHost(response, 2*sizeof(int32_T), (char_T*)&msg);

   PRINT_DEBUG_MSG_LVL2("OUT, error status: ");
   PRINT_DEBUG_MSG_LVL2_UDec(error);
   PRINT_DEBUG_MSG_NL2;

   return(error);

} /* end SendResponseStatus */


/* Function: ProcessSetParamPkt ================================================
 * Receive and process the EXT_SETPARAM packet.
 */
PRIVATE boolean_T ProcessSetParamPkt(RTWExtModeInfo *ei, const int pktSize) {

   int32_T     msg;
   const char  *pkt;
   boolean_T   error=EXT_NO_ERROR;

   // local function name... debugging only
   #if DEBUG_MSG_LVL > 0
   const char  *funct_name="ProcessSetParamPkt";
   #endif

   //PRINT_DEBUG_MSG_LVL1("IN\n\r");

   /*
    * Receive packet and set parameters.
    */
   pkt=GetPkt(pktSize);
   if(pkt==NULL) {

      msg=(int32_T)NOT_ENOUGH_MEMORY;
      SendPktToHost(EXT_SETPARAM_RESPONSE, sizeof(int32_T), (char_T*)&msg);

      abort_LED(58);

      error=EXT_ERROR;
      goto EXIT_POINT;

   }

   //PRINT_DEBUG_MSG_LVL1("after GetPkt\n\r");

   SetParam(ei, pkt);

   msg=(int32_T)STATUS_OK;
   error=SendPktToHost(EXT_SETPARAM_RESPONSE, sizeof(int32_T), (char_T*)&msg);
   if(error!=EXT_NO_ERROR) {

      abort_LED(59);

      goto EXIT_POINT;

   }

EXIT_POINT:

   //PRINT_DEBUG_MSG_LVL1("OUT, error status: ");
   //PRINT_DEBUG_MSG_LVL1_UDec(error);
   //PRINT_DEBUG_MSG_NL1;

   return(error);

} /* end ProcessSetParamPkt */


/* Function: ProcessGetParamsPkt ===============================================
 *  Respond to the hosts request for the parameters by gathering up all the
 *  params and sending them to the host.
 */
PRIVATE boolean_T ProcessGetParamsPkt(RTWExtModeInfo *ei) {

   int_T                         i;
   int_T                         nBytesTotal;
   boolean_T                     error=EXT_NO_ERROR;
   const DataTypeTransInfo       *dtInfo=rteiGetModelMappingInfo(ei);
   const DataTypeTransitionTable *dtTable=dtGetParamDataTypeTrans(dtInfo);

   // local function name... debugging only
   #if DEBUG_MSG_LVL > 0
   const char                    *funct_name="ProcessGetParamsPkt";
   #endif

   PRINT_DEBUG_MSG_LVL3("IN\n\r");

   if(dtTable!=NULL) {

      /*
       * We've got some params in the model.  Send their values to the
       * host.
       */
      int_T       nTrans=dtGetNumTransitions(dtTable);
      const uint_T *dtSizes=dtGetDataTypeSizes(dtInfo);

      /*
       * Take pass 1 through the transitions to figure out how many
       * bytes we're going to send.
       */
      nBytesTotal=0;
      for(i=0; i<nTrans; i++) {

         int_T tranIsComplex=dtTransGetComplexFlag(dtTable, i);
         int_T dt=dtTransGetDataType(dtTable, i);
         int_T dtSize=dtSizes[dt];
         int_T elSize=dtSize * (tranIsComplex?2:1);
         int_T nEls=dtTransNEls(dtTable, i);
         int_T nBytes=elSize *nEls;

         nBytesTotal+=nBytes;

      }

      /*
       * Send the packet header.
       */
      error=SendPktHdrToHost(EXT_GETPARAMS_RESPONSE, nBytesTotal);
      if(error!=EXT_NO_ERROR) {

         abort_LED(60);

         goto EXIT_POINT;

      }

      /*
       * Take pass 2 through the transitions and send the parameters.
       */
      for(i=0; i<nTrans; i++) {

         char_T *tranAddress=dtTransGetAddress(dtTable, i);
         int_T tranIsComplex=dtTransGetComplexFlag(dtTable, i);
         int_T dt=dtTransGetDataType(dtTable, i);
         int_T dtSize=dtSizes[dt];
         int_T elSize=dtSize * (tranIsComplex?2:1);
         int_T nEls=dtTransNEls(dtTable, i);
         int_T nBytes=elSize *nEls;

         error=SendPktDataToHost(tranAddress, nBytes);
         if(error!=EXT_NO_ERROR) {

            abort_LED(61);

            goto EXIT_POINT;

         }

      }

   }
   else {

      /*
       * We've got no params in the model.
       */
      error=SendPktHdrToHost(EXT_GETPARAMS_RESPONSE, 0);
      if(error!=EXT_NO_ERROR) {

         abort_LED(62);

         goto EXIT_POINT;

      }

   }

   EXIT_POINT:PRINT_DEBUG_MSG_LVL3("OUT, error status: ");
   PRINT_DEBUG_MSG_LVL3_UDec(error);
   PRINT_DEBUG_MSG_NL3;

   return(error);

} /* end ProcessGetParamsPkt */


/* Function: ProcessArmTriggerPkt ==============================================
 * Receive and process the EXT_ARM_TRIGGER packet.
 */
PRIVATE boolean_T ProcessArmTriggerPkt(const int pktSize, int_T numSampTimes) {

   const char  *pkt;
   int32_T     upInfoIdx;
   boolean_T   error=EXT_NO_ERROR;

   // local function name... debugging only
   #if DEBUG_MSG_LVL > 0
   const char  *funct_name="ProcessArmTriggerPkt";
   #endif

   pkt=GetPkt(pktSize);
   if(pkt==NULL) {

      SendResponseStatus(EXT_ARM_TRIGGER_RESPONSE, NOT_ENOUGH_MEMORY, -1);

      abort_LED(63);

      error=EXT_ERROR;
      goto EXIT_POINT;

   }

   /* Extract upInfoIdx */
   (void)memcpy(&upInfoIdx, pkt, sizeof(int32_T));

   PRINT_DEBUG_MSG_LVL3("Got EXT_ARM_TRIGGER packet for upInfoIdx ");
   PRINT_DEBUG_MSG_LVL3_UDec((uint16_T)upInfoIdx);
   PRINT_DEBUG_MSG_NL3;

   UploadArmTrigger(upInfoIdx, numSampTimes);

   error=SendResponseStatus(EXT_ARM_TRIGGER_RESPONSE, STATUS_OK, upInfoIdx);
   if(error!=EXT_NO_ERROR) {

      abort_LED(64);

      goto EXIT_POINT;

   }

   EXIT_POINT:return(error);

} /* end ProcessArmTriggerPkt */


/* Function: ProcessSelectSignalsPkt ===========================================
 * Receive and process the EXT_SELECT_SIGNALS packet.
 */
PRIVATE boolean_T ProcessSelectSignalsPkt(RTWExtModeInfo *ei, int_T          numSampTimes, const int      pktSize) {

   const char  *pkt;
   int32_T     upInfoIdx;
   boolean_T   error=EXT_NO_ERROR;

   // local function name... debugging only
   #if DEBUG_MSG_LVL > 0
   const char  *funct_name="ProcessSelectSignalsPkt";
   #endif


   PRINT_DEBUG_MSG_LVL2("IN\n\r");

   pkt=GetPkt(pktSize);
   if(pkt==NULL) {

      SendResponseStatus(EXT_SELECT_SIGNALS_RESPONSE, NOT_ENOUGH_MEMORY, -1);

      abort_LED(65);

      error=EXT_ERROR;
      goto EXIT_POINT;

   }

   /* Extract upInfoIdx */
   (void)memcpy(&upInfoIdx, pkt, sizeof(int32_T));

   PRINT_DEBUG_MSG_LVL3("Got EXT_SELECT_SIGNALS packet for upInfoIdx ");
   PRINT_DEBUG_MSG_LVL3_UDec((uint16_T)upInfoIdx);
   PRINT_DEBUG_MSG_NL3;

   error=UploadLogInfoInit(ei, numSampTimes, pkt+sizeof(int32_T), upInfoIdx);
   if(error!=NO_ERR) {

      SendResponseStatus(EXT_SELECT_SIGNALS_RESPONSE, NOT_ENOUGH_MEMORY, upInfoIdx);

      abort_LED(66);

      //printf(
      //    "\nError in UploadLogInfoInit(). Most likely a memory\n"
      //    "allocation error or an attempt to re-initialize the\n"
      //    "signal selection during the data logging process\n"
      //    "(i.e., multiple EXT_SELECT_SIGNAL packets were received\n"
      //    "before the logging session terminated or an\n"
      //    "EXT_CANCEL_LOGGING packet was received)\n");
      goto EXIT_POINT;

   }

   error=SendResponseStatus(EXT_SELECT_SIGNALS_RESPONSE, STATUS_OK, upInfoIdx);
   if(error!=EXT_NO_ERROR) {

      abort_LED(67);

      goto EXIT_POINT;

   }

   EXIT_POINT:PRINT_DEBUG_MSG_LVL2("OUT, error status: ");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -