/* SITL handling This simulates a GPS on a serial port Andrew Tridgell November 2011 */ #include #include #include #include #include #include #include #include #include #include "desktop.h" #include "util.h" // state of GPS emulation static struct { /* pipe emulating UBLOX GPS serial stream */ int gps_fd; uint32_t last_update; // milliseconds } gps_state; /* hook for reading from the GPS pipe */ ssize_t sitl_gps_read(int fd, void *buf, size_t count) { return read(fd, buf, count); } /* setup GPS input pipe */ int sitl_gps_pipe(void) { int fd[2]; pipe(fd); gps_state.gps_fd = fd[1]; gps_state.last_update = millis(); set_nonblocking(gps_state.gps_fd); set_nonblocking(fd[0]); return fd[0]; } /* send a UBLOX GPS message */ static void gps_send(uint8_t msgid, uint8_t *buf, uint16_t size) { const uint8_t PREAMBLE1 = 0xb5; const uint8_t PREAMBLE2 = 0x62; const uint8_t CLASS_NAV = 0x1; uint8_t hdr[6], chk[2]; hdr[0] = PREAMBLE1; hdr[1] = PREAMBLE2; hdr[2] = CLASS_NAV; hdr[3] = msgid; hdr[4] = size & 0xFF; hdr[5] = size >> 8; chk[0] = chk[1] = hdr[2]; chk[1] += (chk[0] += hdr[3]); chk[1] += (chk[0] += hdr[4]); chk[1] += (chk[0] += hdr[5]); for (uint8_t i=0; i