-1

This pseudocode is for a sink that tries to access the wireless medium in send and receive data from sensors.

  1. set pc = 0.01
  2. send a polling packet
  3. If no sensor responds to polling packet, set pc = min (pc + 0.01, 1.0)
  4. If a data packet is successfully received from one of the sensors, keep pc at current value
  5. If there is a collision between two or more sensors as indicated by a corrupted data packet, set pc = pc / 2
  6. Repeat step 2

I have read the link by How to read a FSM diagram and it really helped me for the sensor part. But I am still confused about trying to convert the above pseudocode into an FSM.

Can anyone suggest a link or ebook that gives a clear explanation about converting the pseudocode into a FSM?

Community
  • 1
  • 1
ndaisy
  • 1
  • The title sucks, but it is better than what you had originally. Please improve it; I don't know much about networking, so I will leave it up to you. – Mateen Ulhaq Dec 14 '11 at 01:16

1 Answers1

0

I'm not sure what you're really looking for here; coding this simply would be pretty straight forward, and this problem doesn't look like it deserves the full-blown FSM table-driven approach to me.

Here's some C-like pseudo-code:

double pc = 0.01;
int sensorsfd;

void loop(void) {
    for (;;) {
        fd_set readfds, writefds, exceptfds;
        FD_ZERO(&readfds);
        FD_ZERO(&writefds);
        FD_ZERO(&exceptfds);
        FD_SET(sensorsfd, &readfds);

        struct timeval tv;
        tv.tv_sec = 0;
        tv.tv_usec = 1; /* 0.001 seconds */

        int r;

        send_polling_packet();

        r = select(sensorsfd+1, &readfds, &writefds, &exceptfds, &tv);

        if (r == -1 && errno == EINTR) {
            continue;
        } else if (r == -1) {
            perror("select() failed");
            exit(1);
        } else if (r == 0) {
        /* timeout expired */
            pc = min (pc + 0.01, 1.0);
        } else if (r == 1) {
        /* sensorsfd won't block when reading it */
            packet_t p = read_packet(sensorsfd);
        /* should also handle _no packet_ if the sensors
           socket returns EOF */
            if (packet_corrupted(p)) {
                pc /= 2;
            } else {
                handle_packet(p);
            }
        } else {
            /* error in program logic */
        }
    }
}

Pseudo-code in the sense that I just wrote this and have no mechanism to test it. If your program gets much more complicated than this, you would probably want to encapsulate all the select(2) setup into a function of its own, and possibly all the details of handling the packet from the sensor's socket.

sarnold
  • 102,305
  • 22
  • 181
  • 238