int l_ping (GPPort *p, GPContext *c) { return (l_ping_rec (p, 0)); }
static int l_ping_rec (GPPort *p, unsigned int level) { unsigned char c; /* Write ENQ and read the response. */ c = ENQ; CHECK (gp_port_write (p, &c, 1)); CHECK (gp_port_read (p, &c, 1)); switch (c) { case ACK: return (GP_OK); case NACK: /* * Uh, lets try again a couple of times, but * make sure we don't recurse forever. */ if (level < 30) return (l_ping_rec (p, level + 1)); else return (GP_ERROR_CORRUPTED_DATA); case ENQ: /* * ENQ received. It seems that the camera would like * to send us data, but we do not want it and * therefore simply reject it. The camera will try * two more times with ENQ to get us to receive data * before finally giving up and sending us ACK. */ /* Write NACK. */ c = NACK; CHECK (gp_port_write (p, &c, 1)); for (;;) { CHECK (gp_port_read (p, &c, 1)); switch (c) { case ENQ: /* The camera has not yet given up. */ continue; case ACK: /* ACK received. We can proceed. */ return (GP_OK); default: /* This should not happen. */ return (GP_ERROR_CORRUPTED_DATA); } break; } break; default: /* * The camera seems to send us data. We'll * simply ignore it and try again (but make * sure that we don't loop forever). */ CHECK (gp_port_flush (p, 0)); CHECK (gp_port_flush (p, 1)); if (level > 50) return (GP_ERROR_CORRUPTED_DATA); else return (l_ping_rec (p, level + 1)); } return (GP_OK); }
int l_ping (GPPort *p, GPFsErr *c) { return (l_ping_rec (p, 0)); }