Example #1
0
int AJ_Main()
{
    AJ_Status status;

    while (1) {
        int windows = 1 << (rand() % 3); // randomize window width 1,2,4
        int blocksize = 50 + (rand() % 1000); // randomize packet size 50 - 1050
        AJ_AlwaysPrintf(("Windows:%i Blocksize:%i\n", windows, blocksize));
        txBuffer = (uint8_t*) AJ_Malloc(blocksize);
        rxBuffer = (uint8_t*) AJ_Malloc(blocksize);
        memset(txBuffer, 0x41,  blocksize);
        memset(rxBuffer, 'r', blocksize);

#ifdef READTEST
        status = AJ_SerialInit("/dev/ttyUSB0", BITRATE, windows, blocksize);
#else
        status = AJ_SerialInit("/dev/ttyUSB1", BITRATE, windows, blocksize);
#endif

        AJ_AlwaysPrintf(("serial init was %u\n", status));
        if (status != AJ_OK) {
            continue; // init failed perhaps from bad parameters, start the loop again
        }

        // Change the buffer transmission function to one that fuzzes the output.
        AJ_SetTxSerialTransmit(&FuzzBuffer);

#ifdef READTEST
        AJ_Sleep(2000); // wait for the writing side to be running, this should test the queuing of data.
        // try to read everything at once
        int i = 0;

        while (1) {
            AJ_SerialRecv(rxBuffer, blocksize, 50000, NULL);
        }


        AJ_DumpBytes("Post serial recv", rxBuffer, blocksize);
        AJ_Sleep(500);
#else
        AJ_Sleep(5000);
        int i = 0;


        while (1) {
            // change the packet to be sent every time through the loop.
            memset(txBuffer, 0x41 + (i % 26), blocksize);
            memset(rxBuffer, 0x41 + (i % 26), blocksize);
            AJ_SerialSend(txBuffer, blocksize);
            ++i;
            if (i % 20 == 0) {
                AJ_AlwaysPrintf(("Hit iteration %d\n", i));
                break;
            }
            AJ_SerialRecv(rxBuffer, blocksize, 5000, NULL);
        }

        AJ_AlwaysPrintf(("post serial send\n"));
#endif

        // clean up and start again
        AJ_SerialShutdown();
        AJ_Free(txBuffer);
        AJ_Free(rxBuffer);
    }
    return(0);
}
Example #2
0
AJ_Status AJ_SerialTX_Init()
{
    int i;
    TxPkt volatile* prev;

    if (AJ_SerialLinkParams.packetSize == 0) {
        return AJ_ERR_DRIVER;
    }

    /*
     * Initialize local static data
     */
    txQueue = NULL;
    txSent = NULL;
    txFreeList = NULL;
    txUnreliable = NULL;
    txSeqNum = 0;
    resendPrimed = FALSE;
    pendingAcks = 0;
    currentTxAck = 0;
    dataSent = 1;

    /*
     * Data packets: To maximize throughput we need as many packets as the
     * window size.
     */
    for (i = 0; i < AJ_SerialLinkParams.maxWindowSize; ++i) {
        void* payload;
        prev = txFreeList;
        txFreeList = AJ_Malloc(sizeof(TxPkt));
        payload = AJ_Malloc(AJ_SerialLinkParams.packetSize);
        if (!txFreeList || !payload) {
            return AJ_ERR_RESOURCES;
        }
        txFreeList->payload = payload;
        txFreeList->next = prev;
    }

    AJ_SlippedBuffer volatile* prevBuf = NULL;
    bufferTxFreeList = NULL;
    for (i = 0; i < AJ_SerialLinkParams.maxWindowSize; i++) {
        void* buf;
        prevBuf = bufferTxFreeList;
        bufferTxFreeList = AJ_Malloc(sizeof(AJ_SlippedBuffer));
        buf = AJ_Malloc(SLIPPED_LEN(AJ_SerialLinkParams.packetSize)); //TODO: calculate slipped length based on packet size
        if (!bufferTxFreeList || !buf) {
            return AJ_ERR_RESOURCES;
        }
        bufferTxFreeList->buffer = buf;
        bufferTxFreeList->actualLen = 0;
        bufferTxFreeList->allocatedLen = SLIPPED_LEN(AJ_SerialLinkParams.packetSize);
        bufferTxFreeList->next = prevBuf;
    }

    prevBuf = bufferTxFreeList;

    /*
     * Buffer for unreliable packets
     */
    txUnreliable = (TxPkt*)AJ_Malloc(sizeof(TxPkt));
    memset((void*)txUnreliable, 0, sizeof(TxPkt));
    txUnreliable->payload = (uint8_t*)AJ_Malloc(AJ_LINK_PACKET_PAYLOAD);

    AJ_InitTimer(&resendTime);
    AJ_TimeAddOffset(&resendTime, AJ_TIMER_FOREVER);
    resendPrimed = FALSE;

    AJ_InitTimer(&ackTime);
    AJ_TimeAddOffset(&ackTime, AJ_TIMER_FOREVER);

    AJ_SetTxSerialTransmit(&__AJ_TX);
    AJ_SetTxCB(&AJ_TransmitCallback);
    return AJ_OK;
}