0
uint64_t GetTimeStamp()
{
    struct timespec start;

    if ((clock_gettime( CLOCK_MONOTONIC, &start)) == -1)
    {
        perror("clock gettime\n");
    }

    return(start.tv_sec + start.tv_nsec * 1e-9);                // seconds
}

const struct sigevent *intHandler(void *arg, int id)
{
    start_clock = ClockCycles();
    // printf("start clock: %lld\n", start_clock);

    return(&event);
}

void *ConfigureISR()
{
    // Get IO privilege
    ThreadCtl( _NTO_TCTL_IO, 0 );
    // Setup COID and event
    SIGEV_INTR_INIT( &event);

    interruptID = InterruptAttach(intrNum, intHandler, NULL, 0, 0);
    if (interruptID == -1)
    {
        fprintf(stderr, "can't attach to IRQ %d\n", intrNum );
        perror(NULL);
        exit(EXIT_FAILURE);
    }

    while (loop)
    {
        InterruptWait(0, NULL);
        end_clock = ClockCycles();
        InterruptLatency = (uint32) ((end_clock - start_clock) * 1000000 / (SYSPAGE_ENTRY(qtime)->cycles_per_sec));
        printf("Current Interrupt Latency microseconds: %ld\n", InterruptLatency);
        InterruptUnmask(intrNum, interruptID);
        measurements[17] = InterruptLatency;
    }
    return(EXIT_SUCCESS);
}

int CreateSocket()
{
    // pthread_attr_t attr;
    // Socket creation for UDP

    acceptSocket = socket(AF_INET, SOCK_DGRAM, 0);

    if (acceptSocket == -1)
    {
        printf("Failure: socket creation is failed, failure code\n");
        return 1;
    }
    else
    {
        printf("Socket started!\n");
    }

    memset(&addr, 0, sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    rc = bind(acceptSocket, (struct sockaddr *)&addr, sizeof(addr));

    fcntl(acceptSocket, O_NONBLOCK);

    if (rc == -1)
    {
        printf("Oh dear, something went wrong with bind()! %s\n", strerror(errno));
        return -1;
    }
    else
    {
        printf("Socket an port %d \n", port);
    }
    return acceptSocket;
}

int main(int argc, char *argv[])
{
    pthread_t thread_id, thread_id1;

    Xcp_Initialize();

    pthread_create(&thread_id1, NULL, &ConfigureISR, NULL);

    if ((sock = CreateSocket()) < 0)
    {
        perror("Create_socket");
        exit(1);
    }

    if (pthread_create(&thread_id, NULL, &rastertask, NULL))
    {
        perror("pthread_create");
        exit(1);
    }

    do
    {
        socklen_t len;
        len = sizeof(client);
        printf("NEW DATA ARRIVED\n");
        // non blocking mode : MSG_DONTWAIT
        rc = recvfrom(sock, buf, 256, 0, (struct sockaddr *) &client, &len);
        Receive = GetTimeStamp();
        receiveTime = (uint32) (Receive / 1000000);
        printf("Receive time: %lu\n", receiveTime);
        //  printf("RECEIVE from Time in microseconds: %lu\n",  ReceiveTimestamp);
        // measurements[19] = ReceiveTimestamp;
        if (rc == 0)
        {
            printf("Server has no connection..\n");
            loop = 0;
            break;
        }
        if (rc == -1)
        {
            if (errno == SIGINT)
                continue;
            printf("Oh dear, something went wrong with read()! s\n", strerror(errno));
            loop = 0;
            break;
        }

        XcpIp_RxCallback( (uint16) rc, (uint8 *) buf, (uint16) port );
    } while (1);


    close(sock);

    return 0;
}

The above is a server code, which is receiving the data via ip address and port number.

Later sending the data back to the client. I am receiving the data but after receiving the data, taking the timestamp (you can see in the above code : Send= GetTimeStamp()).

Why is it not printing anything? I am receiving the data via the socket and I gave printf("new data arrived\n"); then also it is not printing. I am also not receiving any time over there! Could some one tell me what could be the reason?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

0

Your GetTimeStamp() function returns a uint64_t; the return statement is:

return (start.tv_sec + start.tv_nsec * 1e-9);

This creates a double value (1e-9 is a double constant), which is then truncated to an integer, just as if you'd returned:

return (start.tv_sec);

Note that this gives you the time in seconds (since the Epoch — 1970-01-01 00:00:00 +00:00).

Maybe you want:

return (start.tv_sec * 1E9 + start.tv_nsec);

This gives you the total number of nanoseconds since the Epoch. This may be tangential to the other problems you have, but not getting sensible values to send means you won't get sensible values to receive, so you should be sure about what value you expect from GetTimeStamp(). For example:

    Receive = GetTimeStamp();
    receiveTime = (uint32) (Receive / 1000000);
    printf("Receive time: %lu\n", receiveTime);

This will print a value such as 1400 (because the current time stamp is approximately 1400755363 = 2014-05-22 10:42:43), so that value divided by 1 million is 1400.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278