NTP Client Library. Set sync provider pointing to public class function. Possible? I am trying to develop a NTP client library for ESP8266/Arduino to make adding NTP sync an easier task.

Basically, I've thought about a constructor as NTPClient(String host, int interval); and a NTPClient.begin() function to register sync function.

My problem is with that registering.

I have a public function: time_t ntpClient::getNtpTime() that connects to NTP server, decodes the response and returns a time_t variable.

Then, inside boolean ntpClient::begin() function I try to run setSyncProvider(ntpClient::getNtpTime). I've also tried to use setSyncProvider(this-&gt;getNtpTime).

I get a compile error here:

ntpClient.cpp:In member function 'boolean ntpClient::begin()
ntpClient.cpp:79:39: error: cannot convert 'ntpClient::getNtpTime' from type 'time_t (ntpClient::)() {aka long int (ntpClient::)()}' to type 'getExternalTime {aka long int (*)()}
  setSyncProvider(ntpClient*:getNtpTime)
Error compiling project sources


What could I do to allow registering this function as sync provider to hide NTP internals to my sketches?
What could I do to allow registering this function as sync provider to hide NTP internals to my sketches?


The function should be a static member function. A "normal" member function does not have the right prototype as there is a hidden parameter ("this"). 

static time_t getNtpTime();


To solve this you need a single-ton (an instance of the client class) that the static function can use. 

class NTPClient {
public:
   NTPClient() ...
   {
     ...
     s_client = this;
   }
   time_t getNtpTime();
   ...
   static time_t getNtpTime() ( return (s_client-&gt;getNtpTime()); }
   ...
protected:
   static NTPClient* s_client;
   ...
}; 
...
NTPClient::s_client = NULL;
...
NTPClient ntpClient;
...
setSyncProvider(NTPClient::getNtpTime)


Alternatively a global function and a known instance:

class NTPClient { ... };
extern NTPClient ntpClient;
...
time_t getNtpTime() { return (ntpClient.getNtpTime()); } 
...
setSyncProvider(getNtpTime) 


Cheers!