From d8d66203208bff4134f33cd3df2ff8c4bee60205 Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Thu, 11 Jul 2013 21:11:06 +0200 Subject: [PATCH] Add debuging version of ext_svr.c --- ert_linux/ert_linux.tlc | 1 + ert_linux/ert_linux_genfiles.tlc | 6 + ert_linux/ext_svr.c | 1411 ++++++++++++++++++++++++++++++ 3 files changed, 1418 insertions(+) create mode 100644 ert_linux/ert_linux_genfiles.tlc create mode 100644 ert_linux/ext_svr.c diff --git a/ert_linux/ert_linux.tlc b/ert_linux/ert_linux.tlc index 67353b9..3a0d73f 100644 --- a/ert_linux/ert_linux.tlc +++ b/ert_linux/ert_linux.tlc @@ -32,6 +32,7 @@ %include "commontargetlib.tlc" %include "codegenentry.tlc" +%include "ert_linux_genfiles.tlc" /% BEGIN_RTW_OPTIONS diff --git a/ert_linux/ert_linux_genfiles.tlc b/ert_linux/ert_linux_genfiles.tlc new file mode 100644 index 0000000..6fe15cb --- /dev/null +++ b/ert_linux/ert_linux_genfiles.tlc @@ -0,0 +1,6 @@ +/% Copy my custom version of ext_svr (for debugging) %/ +%openfile ext_svr="ext_svr.c" +%selectfile ext_svr +/* Ahoj */ +%include "ext_svr.c" +%closefile ext_svr diff --git a/ert_linux/ext_svr.c b/ert_linux/ext_svr.c new file mode 100644 index 0000000..30a7384 --- /dev/null +++ b/ert_linux/ext_svr.c @@ -0,0 +1,1411 @@ +/* + * Copyright 1994-2012 The MathWorks, Inc. + * + * File: ext_svr.c + * + * Abstract: + * External mode server interface (TCPIP example). Provides functions + * that get called by main routine (model-name.c): + * o ExtParseArgsAndInitUD: parse args and create UserData + * o ExtWaitForStartPkt: return true if waiting for host to start + * o rt_ExtModeInit: external mode initialization + * o rt_ExtModeSleep: pause the process + * o rt_PktServerWork: server for setting/getting packets from host + * o rt_PktServer: server dispatcher - for multi-tasking targets + * o rt_UploadServerWork: server for setting data upload packets on host + * o rt_UploadServer: server dispatcher - for multi-tasking targets + * o rt_ExtModeShutdown: external mode termination + * + * Parameter downloading and data uploading supported for single and + * multi-tasking targets. + */ + +/***************** + * Include files * + *****************/ + +/*ANSI C headers*/ +#ifndef EXTMODE_DISABLEPRINTF +#include +#endif + +#include +#include + +#if defined(VXWORKS) + /*VxWorks headers*/ +# include +# include +# include +# include +# include +#endif + +/*Real Time Workshop headers*/ +#include "rtwtypes.h" +#include "rtw_extmode.h" + +#include "ext_types.h" +#include "ext_share.h" +#include "ext_test.h" +#include "ext_svr_transport.h" +#include "updown.h" +#include "updown_util.h" +#include "dt_info.h" + +/*Uncomment to test 4 byte reals*/ +/*#define real_T float*/ + +/********************** + * External Variables * + **********************/ +extern int_T volatile startModel; +extern TargetSimStatus volatile modelStatus; +#ifdef VXWORKS +extern SEM_ID uploadSem; +extern SEM_ID pktSem; +#endif +extern boolean_T host_upstatus_is_uploading; + +/******************** + * Global Variables * + ********************/ + +/* + * Flags. + */ +PRIVATE boolean_T connected = FALSE; +PRIVATE boolean_T commInitialized = FALSE; + +/* + * Pointer to opaque user data (defined by ext_svr_transport.c). + */ +PRIVATE ExtUserData *extUD = NULL; + +/* + * Buffer used to receive packets. + */ +PRIVATE int_T pktBufSize = 0; +PRIVATE char *pktBuf = NULL; + + +#ifndef EXTMODE_DISABLESIGNALMONITORING +#ifndef EXTMODE_DISABLEPRINTF +PRIVATE char ERRMSG_PROCESSSELECTSIGNAL[] = + "\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"; + +PRIVATE char ERRMSG_PROCESSSELECTTRIGGER[] = + "\nError in UploadInitTrigger(). Most likely a memory\n" + "allocation error or an attempt to re-initialize the\n" + "trigger selection during the data logging process\n" + "(i.e., multiple EXT_SELECT_TRIGGER packets were received\n" + "before the logging session terminated or an\n" + "EXT_CANCEL_LOGGING packet was received)\n"; +#else +PRIVATE char ERRMSG_PROCESSSELECTSIGNAL[] = ""; +PRIVATE char ERRMSG_PROCESSSELECTTRIGGER[] = ""; +#endif +#endif + +/******************* + * Local Functions * + *******************/ + +/* Function: GrowRecvBufIfNeeded =============================================== + * Abstract: + * Allocate or increase the size of buffer for receiving packets from target. + */ +PRIVATE boolean_T GrowRecvBufIfNeeded(const int pktSize) +{ + if (pktSize > pktBufSize) { + if (pktBuf != NULL) { + free(pktBuf); + pktBufSize = 0; + } + + pktBuf = (char *)malloc(pktSize); + if (pktBuf == NULL) return(EXT_ERROR); + + pktBufSize = pktSize; + } + return(EXT_NO_ERROR); +} /* end GrowRecvBufIfNeeded */ + + +/* Function: GetPktHdr ========================================================= + * Abstract: + * Attempts to retrieve a packet header from the host. If a header is in + * fact retrieved, the reference arg, 'hdrAvail' will be returned as true. + * + * EXT_NO_ERROR is returned on success, EXT_ERROR is returned on failure. + * + * NOTES: + * o It is not necessarily an error for 'hdrAvail' to be returned as false. + * It typically means that we were polling for packets and none were + * available. + */ +PRIVATE boolean_T GetPktHdr(PktHeader *pktHdr, boolean_T *hdrAvail) +{ + int_T nGot = 0; /* assume */ + int_T nGotTotal = 0; + int_T pktSize = sizeof(PktHeader); + boolean_T error = EXT_NO_ERROR; + + /* Get the header. */ + while(nGotTotal < pktSize) { + error = ExtGetHostPkt(extUD, + pktSize - nGotTotal, &nGot, (char_T *)((char_T *)pktHdr + nGotTotal)); + if (error) goto EXIT_POINT; + + nGotTotal += nGot; + + if (nGotTotal == 0) break; + } + assert((nGot == 0) || (nGotTotal == pktSize)); + +EXIT_POINT: + *hdrAvail = (boolean_T)(nGot > 0); + return(error); +} /* end GetPktHdr */ + + +/* Function: ClearPkt ========================================================== + * Abstract: + * Remove the data from the communication line one byte at a time. This + * function is called when there was not enough memory to receive an entire + * packet. Since the data was never received, it must be discarded so that + * other packets can be sent. + */ +PRIVATE void ClearPkt(const int pktSize) +{ + int_T nGot; + boolean_T error = EXT_NO_ERROR; + int_T nGotTotal = 0; + static char buffer; + + /* Get and discard the data one char at a time. */ + while(nGotTotal < pktSize) { + error = ExtGetHostPkt(extUD, 1, &nGot, (char_T *)&buffer); + if (error) { +#ifndef EXTMODE_DISABLEPRINTF + fprintf(stderr,"ExtGetHostPkt() failed.\n"); +#endif + goto EXIT_POINT; + } + + nGotTotal += nGot; + } + +EXIT_POINT: + return; + +} /* end ClearPkt */ + + +/* Function: GetPkt ============================================================ + * Abstract: + * Receive nBytes from the host. Return a buffer containing the bytes or + * NULL if an error occurs. Note that the pointer returned is that of the + * global pktBuf. If the buf needs to be grown to accommodate the package, + * it is realloc'd. This function will try to get the requested number + * of bytes indefinitely - it is assumed that the data is either already there, + * or will show up in a "reasonable" amount of time. + */ +PRIVATE const char *GetPkt(const int pktSize) +{ + int_T nGot; + boolean_T error = EXT_NO_ERROR; + int_T nGotTotal = 0; + + error = GrowRecvBufIfNeeded(pktSize); + if (error != EXT_NO_ERROR) { +#ifndef EXTMODE_DISABLEPRINTF + fprintf(stderr,"Previous pkt from host thrown away due to lack of memory.\n"); +#endif + ClearPkt(pktSize); + goto EXIT_POINT; + } + + /* Get the data. */ + while(nGotTotal < pktSize) { + error = ExtGetHostPkt(extUD, + pktSize - nGotTotal, &nGot, (char_T *)(pktBuf + nGotTotal)); + if (error) { +#ifndef EXTMODE_DISABLEPRINTF + fprintf(stderr,"ExtGetHostPkt() failed.\n"); +#endif + goto EXIT_POINT; + } + + nGotTotal += nGot; + } + +EXIT_POINT: + return((error == EXT_NO_ERROR) ? pktBuf : NULL); +} /* end GetPkt */ + + +#ifndef EXTMODE_DISABLESIGNALMONITORING +/* Forward declaration */ +void UploadServerWork(int32_T, int_T numSampTimes); +#endif + +/* Function: DisconnectFromHost ================================================ + * Abstract: + * Disconnect from the host. + */ +PRIVATE void DisconnectFromHost(int_T numSampTimes) +{ + int i; + + for (i=0; i 0) { + for (i=0; isection1, + bufMem->nBytes1); + if (error != EXT_NO_ERROR) { +#ifndef EXTMODE_DISABLEPRINTF + fprintf(stderr,"SendPktDataToHost() failed on data upload.\n"); +#endif + goto EXIT_POINT; + } + + if (bufMem->nBytes2 > 0) { + + error = SendPktDataToHost( + bufMem->section2, + bufMem->nBytes2); + if (error != EXT_NO_ERROR) { +#ifndef EXTMODE_DISABLEPRINTF + fprintf(stderr,"SendPktDataToHost() failed on data upload.\n"); +#endif + goto EXIT_POINT; + } + } + /* confirm that the data was sent */ + UploadBufDataSent(upList.tids[i], upInfoIdx); + } + UploadBufGetData(&upList, upInfoIdx, numSampTimes); + } + +EXIT_POINT: + if (error != EXT_NO_ERROR) { + /* An error in this function is caused by a physical failure in the + * external mode connection. We assume this failure caused the host + * to disconnect. The target must be disconnected and returned to a + * state where it is running and can be re-connected to by the host. + */ + ForceDisconnectFromHost(numSampTimes); + } +} +/* end UploadServerWork */ +#endif /* ifndef EXTMODE_DISABLESIGNALMONITORING */ + +#ifndef EXTMODE_DISABLESIGNALMONITORING +/* Function: rt_UploadServerWork =============================================== + * Abstract: + * Wrapper function that calls UploadServerWork once for each upInfo + */ +PUBLIC void rt_UploadServerWork(int_T numSampTimes) +{ + int i; + + for (i=0; i