tiagoshibata / Android-GPSd-Forwarder

Android service to forward NMEA messages to a GPSd server
MIT License
46 stars 21 forks source link

mDNS support (Initial proof of concept implementation provided in my fork) #16

Open ForestJohnson opened 2 years ago

ForestJohnson commented 2 years ago

Hello I have forked your app to add mdns support because i needed it for my use case. This means you don't have to know the IP address of the gpsd UDP server as long as that UDP server is advertising a mDNS service discovery record with the service type _gpsd_.udp. and the service name equal to its hostname.

image

For example this is the avahi-daemon service unit XML file that I created on my server to advertise the GPSd service so the android app can discover it:

/etc/avahi/services/gpsd.service

<?xml version="1.0" standalone='no'?><!--*-nxml-*-->
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">

<!-- See avahi.service(5) for more information about this configuration file -->

<service-group>

  <!-- its important that we only include the %h here (hostname) because 
       the android app checks for this to match the hostname -->
  <name replace-wildcards="yes">%h</name>

  <service protocol="any">
    <type>_gpsd._udp</type>
    <port>4200</port>
  </service>

</service-group>

ℹ️ INFO

I designed it like this (even tho it seemed wrong to me) because I wasn't able to figure out how to grab the machines hostname from the android NsdServiceManager. It looks like it only gives the service info, not the actual xyz.local domain names. So my hack was to make the service name equal to the domain name.

⚠️ WARNING

I encountered GPSd issues when I was testing this, the problem is when you ask GPSd to listen on udp://*:4200 it interprets that * as 0.0.0.0 not :: in other words it only listens on IPv4, however sometimes the mDNS discovery on android would return the ipv6 address and it didn't appear to work properly when I configured the avahi-daemon to only respond with ipv4. . My fix was to introduce a simple UDP forwarder app written in a language i know so I can control how it listens on the UDP port and get it to listen properly on both ipv4 and ipv6. So my forwarder listens on port 4200 and then forwards to 127.0.0.1:4201 which is the exact address gpsd is listening on. I wasn't able to get it to work reliably any other way :weary:

If you are interested in merging this contribution let me know and I might be willing to help you clean it up. Otherwise feel free to pull from my fork:

https://github.com/ForestJohnson/Android-GPSd-Forwarder/commit/fdc1598209c1718843646464d4752c3e568082e6

add initial mdns support for _gpsd_.udp. service

tiagoshibata commented 2 years ago

Sorry for the slow response, I've been busy and didn't check GitHub notifications.

Thanks, mDNS is a very nice to have feature. Unfortunately I don''t use it myself and wouldn't be interested in polishing up and testing the code to have it merged.

If you do clean it up and feel like it's good to be merged let me know; I'm happy to review a PR.