Beispiel #1
0
/*
 *  Function: serpkt_AsyncWrite
 *   Purpose: Entry point for asynchronous writes to the serial device.
 *            See documentation for angel_DeviceWrite in devdriv.h
 */
DevError 
serpkt_AsyncWrite(DeviceID devid, p_Buffer buffer,
                  unsigned int length, DevChanID devchan)
{
    struct data_packet *const txp = SerEngine(devid)->tx_packet;
    RingBuffer *ring = SerCtrl(devid)->tx_ring;

    txp->len = length;
    txp->type = devchan;
    txp->data = buffer;

    Angel_TxEngineInit(SerEngine(devid)->config, txp,
                       SerEngine(devid)->te_state);

    /* need to protect this bit */
    Angel_EnterSVC();
    angel_DeviceStatus[devid] |= SER_TX_KICKSTART;
    serpkt_fill_tx_ring(devid, ring);
    Angel_ExitToUSR();

    LogInfo(LOG_SERPKT, ( "serpkt_AsyncWrite OK\n"));
    return DE_OKAY;
}
static int SerialWrite(DriverCall *dc) {
  int nwritten = 0;
  te_status testatus = TS_IN_PKT;

  if (dc->dc_context == NULL) {
    Angel_TxEngineInit(&config, &(dc->dc_packet), &(wstate.txstate));
    wstate.wbindex = 0;
    dc->dc_context = &wstate;
  }

  while ((testatus == TS_IN_PKT) && (wstate.wbindex < MAXWRITESIZE))
  {
    /* send the raw data through the tx engine to escape and encapsulate */
    testatus = Angel_TxEngine(&(dc->dc_packet), &(wstate.txstate),
                              &(wstate.writebuf)[wstate.wbindex]);
    if (testatus != TS_IDLE) wstate.wbindex++;
  }

  if (testatus == TS_IDLE) {
#ifdef DEBUG
    printf("SerialWrite: testatus is TS_IDLE during preprocessing\n");
#endif
  }

#ifdef DO_TRACE
  { 
    int i = 0;

    while (i<wstate.wbindex)
    {
        printf(">%02X ",wstate.writebuf[i]);

        if (!(++i % 16))
            printf("\n");
    }
    if (i % 16)
        printf("\n");
  }
#endif

#ifdef COMPILING_ON_WINDOWS
  if (WriteSerial(wstate.writebuf, wstate.wbindex) == COM_OK)
  {
    nwritten = wstate.wbindex;
    if (pfnProgressCallback != NULL)
    {
      progressInfo.nWritten += nwritten;
      (*pfnProgressCallback)(&progressInfo);
    }
  }
  else
  {
      MessageBox(GetFocus(), "Write error\n", "Angel", MB_OK | MB_ICONSTOP);
      return -1;   /* SJ - This really needs to return a value, which is picked up in */
                   /*      DevSW_Read as meaning stop debugger but don't kill. */
  }
#else
  nwritten = Unix_WriteSerial(wstate.writebuf, wstate.wbindex);

  if (nwritten < 0) {
    nwritten=0;
  }
#endif

#ifdef DEBUG
  if (nwritten > 0)
    printf("Wrote %#04x bytes\n", nwritten);
#endif

  if ((unsigned) nwritten == wstate.wbindex && 
      (testatus == TS_DONE_PKT || testatus == TS_IDLE)) {

    /* finished sending the packet */

#ifdef DEBUG
    printf("SerialWrite: calling Angel_TxEngineInit after sending packet (len=%i)\n",wstate.wbindex);
#endif
    testatus = TS_IN_PKT;
    wstate.wbindex = 0;
    return 1;
  }
  else {
#ifdef DEBUG
    printf("SerialWrite: Wrote part of packet wbindex=%i, nwritten=%i\n",
           wstate.wbindex, nwritten);
#endif
   
    /*
     *  still some data left to send shuffle whats left down and reset
     * the ptr
     */
    memmove((char *) wstate.writebuf, (char *) (wstate.writebuf+nwritten),
            wstate.wbindex-nwritten);
    wstate.wbindex -= nwritten;
    return 0;
  }
  return -1;
}
Beispiel #3
0
static int SerparWrite(DriverCall *dc)
{
    te_status status;
    int nwritten = 0;
    static TxState txstate;

    /*
     * is this a new packet?
     */
    if (dc->dc_context == NULL)
    {
        /*
         * yes - initialise TxEngine
         */
        Angel_TxEngineInit(&config, &dc->dc_packet, &txstate.state);

        txstate.index = 0;
        dc->dc_context = &txstate;
    }

    /*
     * fill the buffer using the Tx Engine
     */
    do
    {
        status = Angel_TxEngine(&dc->dc_packet, &txstate.state,
                                &txstate.writebuf[txstate.index]);
        if (status != TS_IDLE) txstate.index++;

    } while (status == TS_IN_PKT && txstate.index < MAXWRITESIZE);

#ifdef DO_TRACE
    {
        unsigned int i = 0;

        while (i < txstate.index)
        {
            printf(">%02X ", txstate.writebuf[i]);

            if (!(++i % 16))
                putc('\n', stdout);
        }

        if (i % 16)
            putc('\n', stdout);
    }
#endif

    /*
     * the data are ready, all we need now is to send them out
     * in a form that Angel can swallow.
     */
#ifdef COMPILING_ON_WINDOWS
  if (WriteParallel(txstate.writebuf, txstate.index) == COM_OK)
  {
    nwritten = txstate.index;
    if (pfnProgressCallback != NULL)
    {
      progressInfo.nWritten += nwritten;
      (*pfnProgressCallback)(&progressInfo);
    }
  }
  else
  {
      MessageBox(GetFocus(), "Write error\n", "Angel", MB_OK | MB_ICONSTOP);
      return -1;   /* SJ - This really needs to return a value, which is picked up in */
                   /*      DevSW_Read as meaning stop debugger but don't kill. */
  }
#else
    nwritten = Unix_WriteParallel(txstate.writebuf, txstate.index);
#endif

    if (nwritten < 0) nwritten = 0;

#ifdef DO_TRACE
    printf("SerparWrite: wrote %d out of %d bytes\n",
           nwritten, txstate.index);
#endif

    /*
     * has the whole packet gone?
     */
    if (nwritten == (int)txstate.index &&
        (status == TS_DONE_PKT || status == TS_IDLE))
        /*
         * yes it has
         */
        return 1;
    else
    {
        /*
         * if some data are left, shuffle them
         * to the start of the buffer
         */
        if (nwritten != (int)txstate.index && nwritten != 0)
        {
            txstate.index -= nwritten;
            (void)memmove((char *) txstate.writebuf,
                          (char *) (txstate.writebuf + nwritten),
                          txstate.index);
        }
        else if (nwritten == (int)txstate.index)
            txstate.index = 0;

        return 0;
    }
}
static int SerialOpen(const char *name, const char *arg)
{
    const char *port_name = name;

#ifdef DEBUG
    printf("SerialOpen: name %s arg %s\n", name, arg ? arg : "<NULL>");
#endif

#ifdef COMPILING_ON_WINDOWS
    if (IsOpenSerial()) return -1;
#else
    if (Unix_IsSerialInUse()) return -1;
#endif

#ifdef COMPILING_ON_WINDOWS
    if (SerialMatch(name, arg) != adp_ok)
        return adp_failed;
#else
    port_name = Unix_MatchValidSerialDevice(port_name);
# ifdef DEBUG
    printf("translated port to %s\n", port_name == 0 ? "NULL" : port_name);
# endif
    if (port_name == 0) return adp_failed;
#endif

    user_options_set = FALSE;

    /* interpret and store the arguments */
    if ( arg != NULL )
    {
        unsigned int target_baud_rate;
        target_baud_rate = (unsigned int)strtoul(arg, NULL, 10);
        if (target_baud_rate > 0)
        {
#ifdef DEBUG
            printf( "user selected baud rate %u\n", target_baud_rate );
#endif
            process_baud_rate( target_baud_rate );
        }
#ifdef DEBUG
        else
           printf( "could not understand baud rate %s\n", arg );
#endif
    }
    else if (baud_rate > 0)
    {
      /* If the user specified a baud rate on the command line "-b" or via
         the "set remotebaud" command then try to use that one */
      process_baud_rate( baud_rate );
    }

#ifdef COMPILING_ON_WINDOWS
    {
        int port = IsValidDevice(name);
        if (OpenSerial(port, FALSE) != COM_OK)
            return -1;
    }
#else
    if (Unix_OpenSerial(port_name) < 0)
      return -1;
#endif

    serial_reset();

#if defined(__unix) || defined(__CYGWIN__)
    Unix_ioctlNonBlocking();
#endif

    Angel_RxEngineInit(&config, &rxstate);
    /*
     * DANGER!: passing in NULL as the packet is ok for now as it is just
     * IGNOREd but this may well change
     */
    Angel_TxEngineInit(&config, NULL, &wstate.txstate); 
    return 0;
}