| 1 | package gps |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/bettercap/bettercap/session" |
| 9 | |
| 10 | "github.com/adrianmo/go-nmea" |
| 11 | "github.com/stratoberry/go-gpsd" |
| 12 | "github.com/tarm/serial" |
| 13 | |
| 14 | "github.com/evilsocket/islazy/str" |
| 15 | ) |
| 16 | |
| 17 | type GPS struct { |
| 18 | session.SessionModule |
| 19 | |
| 20 | serialPort string |
| 21 | baudRate int |
| 22 | |
| 23 | serial *serial.Port |
| 24 | gpsd *gpsd.Session |
| 25 | } |
| 26 | |
| 27 | var ModeInfo = [4]string{ |
| 28 | "NoValueSeen", |
| 29 | "NoFix", |
| 30 | "Mode2D", |
| 31 | "Mode3D", |
| 32 | } |
| 33 | |
| 34 | func NewGPS(s *session.Session) *GPS { |
| 35 | mod := &GPS{ |
| 36 | SessionModule: session.NewSessionModule("gps", s), |
| 37 | serialPort: "/dev/ttyUSB0", |
| 38 | baudRate: 4800, |
| 39 | } |
| 40 | |
| 41 | mod.AddParam(session.NewStringParameter("gps.device", |
| 42 | mod.serialPort, |
| 43 | "", |
| 44 | "Serial device of the GPS hardware or hostname:port for a GPSD instance.")) |
| 45 | |
| 46 | mod.AddParam(session.NewIntParameter("gps.baudrate", |
| 47 | fmt.Sprintf("%d", mod.baudRate), |
| 48 | "Baud rate of the GPS serial device.")) |
| 49 | |
| 50 | mod.AddHandler(session.NewModuleHandler("gps on", "", |
| 51 | "Start acquiring from the GPS hardware.", |
| 52 | func(args []string) error { |
| 53 | return mod.Start() |
| 54 | })) |
| 55 | |
| 56 | mod.AddHandler(session.NewModuleHandler("gps off", "", |
| 57 | "Stop acquiring from the GPS hardware.", |
| 58 | func(args []string) error { |
| 59 | return mod.Stop() |
| 60 | })) |
| 61 | |
| 62 | mod.AddHandler(session.NewModuleHandler("gps.show", "", |
| 63 | "Show the last coordinates returned by the GPS hardware.", |
| 64 | func(args []string) error { |
| 65 | return mod.Show() |
| 66 | })) |
| 67 | |
| 68 | return mod |
| 69 | } |
| 70 | |
| 71 | func (mod *GPS) Name() string { |
| 72 | return "gps" |
| 73 | } |
| 74 | |
| 75 | func (mod *GPS) Description() string { |
| 76 | return "A module talking with GPS hardware on a serial interface or via GPSD." |
| 77 | } |
| 78 | |
| 79 | func (mod *GPS) Author() string { |
| 80 | return "Simone Margaritelli <[email protected]>" |
| 81 | } |
| 82 | |
| 83 | func (mod *GPS) Configure() (err error) { |
| 84 | if mod.Running() { |
| 85 | return session.ErrAlreadyStarted(mod.Name()) |
| 86 | } else if err, mod.serialPort = mod.StringParam("gps.device"); err != nil { |
| 87 | return err |
| 88 | } else if err, mod.baudRate = mod.IntParam("gps.baudrate"); err != nil { |
| 89 | return err |
| 90 | } |
| 91 | |
| 92 | if mod.serialPort[0] == '/' || mod.serialPort[0] == '.' { |
| 93 | mod.Debug("connecting to serial port %s", mod.serialPort) |
| 94 | mod.serial, err = serial.OpenPort(&serial.Config{ |
| 95 | Name: mod.serialPort, |
| 96 | Baud: mod.baudRate, |
| 97 | ReadTimeout: time.Second * 1, |
| 98 | }) |
| 99 | } else { |
| 100 | mod.Debug("connecting to gpsd at %s", mod.serialPort) |
| 101 | mod.gpsd, err = gpsd.Dial(mod.serialPort) |
| 102 | } |
| 103 | |
| 104 | return |
| 105 | } |
| 106 | |
| 107 | func (mod *GPS) readLine() (line string, err error) { |
| 108 | var n int |
| 109 | |
| 110 | b := make([]byte, 1) |
| 111 | for { |
| 112 | if n, err = mod.serial.Read(b); err != nil { |
| 113 | return |
| 114 | } else if n == 1 { |
| 115 | if b[0] == '\n' { |
| 116 | return str.Trim(line), nil |
| 117 | } else { |
| 118 | line += string(b[0]) |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func (mod *GPS) Show() error { |
| 125 | mod.Printf("latitude:%f longitude:%f quality:%s satellites:%d altitude:%f\n", |
| 126 | mod.Session.GPS.Latitude, |
| 127 | mod.Session.GPS.Longitude, |
| 128 | mod.Session.GPS.FixQuality, |
| 129 | mod.Session.GPS.NumSatellites, |
| 130 | mod.Session.GPS.Altitude) |
| 131 | |
| 132 | mod.Session.Refresh() |
| 133 | |
| 134 | return nil |
| 135 | } |
| 136 | |
| 137 | func (mod *GPS) readFromSerial() { |
| 138 | if line, err := mod.readLine(); err == nil { |
| 139 | if s, err := nmea.Parse(line); err == nil { |
| 140 | // http://aprs.gids.nl/nmea/#gga |
| 141 | if m, ok := s.(nmea.GGA); ok { |
| 142 | mod.Session.GPS.Updated = time.Now() |
| 143 | mod.Session.GPS.Latitude = m.Latitude |
| 144 | mod.Session.GPS.Longitude = m.Longitude |
| 145 | mod.Session.GPS.FixQuality = m.FixQuality |
| 146 | mod.Session.GPS.NumSatellites = m.NumSatellites |
| 147 | mod.Session.GPS.HDOP = m.HDOP |
| 148 | mod.Session.GPS.Altitude = m.Altitude |
| 149 | mod.Session.GPS.Separation = m.Separation |
| 150 | |
| 151 | mod.Session.Events.Add("gps.new", mod.Session.GPS) |
| 152 | } |
| 153 | } else { |
| 154 | mod.Debug("error parsing line '%s': %s", line, err) |
| 155 | } |
| 156 | } else if err != io.EOF { |
| 157 | mod.Warning("error while reading serial port: %s", err) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func (mod *GPS) runFromGPSD() { |
| 162 | mod.gpsd.AddFilter("TPV", func(r interface{}) { |
| 163 | report := r.(*gpsd.TPVReport) |
| 164 | mod.Session.GPS.Updated = report.Time |
| 165 | mod.Session.GPS.Latitude = report.Lat |
| 166 | mod.Session.GPS.Longitude = report.Lon |
| 167 | mod.Session.GPS.FixQuality = ModeInfo[report.Mode] |
| 168 | mod.Session.GPS.Altitude = report.Alt |
| 169 | |
| 170 | mod.Session.Events.Add("gps.new", mod.Session.GPS) |
| 171 | }) |
| 172 | |
| 173 | mod.gpsd.AddFilter("SKY", func(r interface{}) { |
| 174 | report := r.(*gpsd.SKYReport) |
| 175 | mod.Session.GPS.NumSatellites = int64(len(report.Satellites)) |
| 176 | mod.Session.GPS.HDOP = report.Hdop |
| 177 | //mod.Session.GPS.Separation = 0 |
| 178 | }) |
| 179 | |
| 180 | done := mod.gpsd.Watch() |
| 181 | <-done |
| 182 | } |
| 183 | |
| 184 | func (mod *GPS) Start() error { |
| 185 | if err := mod.Configure(); err != nil { |
| 186 | return err |
| 187 | } |
| 188 | |
| 189 | return mod.SetRunning(true, func() { |
| 190 | mod.Info("started on port %s ...", mod.serialPort) |
| 191 | |
| 192 | if mod.serial != nil { |
| 193 | defer mod.serial.Close() |
| 194 | |
| 195 | for mod.Running() { |
| 196 | mod.readFromSerial() |
| 197 | } |
| 198 | } else { |
| 199 | mod.runFromGPSD() |
| 200 | } |
| 201 | }) |
| 202 | } |
| 203 | |
| 204 | func (mod *GPS) Stop() error { |
| 205 | return mod.SetRunning(false, func() { |
| 206 | if mod.serial != nil { |
| 207 | // let the read fail and exit |
| 208 | mod.serial.Close() |
| 209 | } /* |
| 210 | FIXME: no Close or Stop method in github.com/stratoberry/go-gpsd |
| 211 | else { |
| 212 | if err := mod.gpsd.Close(); err != nil { |
| 213 | mod.Error("failed closing the connection to GPSD: %s", err) |
| 214 | } |
| 215 | } */ |
| 216 | }) |
| 217 | } |