Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query IP address info from the application #25

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion ELClient/ELClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,26 @@ ELClientPacket* ELClient::protoCompletedCb(void) {
_debug->print("RESP_V: ");
_debug->println(packet->value);
}
_longPacket = 0;
return packet;
case CMD_RESP_CB_CONTINUE:
// response callback, but one of the parts of a long packet --> just prepare assembly
// Currently just a copy (without notes) of the code below...

{
// Serial.print("CMD_RESP_CB_CONTINUE\n");
_longPacket = 1;
FP<void, void*> *fp;
fp = (FP<void, void*>*)packet->value;
if (fp->attached()) {
ELClientResponse resp(packet);
(*fp)(&resp);
}
return NULL;
}

case CMD_RESP_CB: // response callback: perform the callback!
{
FP<void, void*> *fp;
// callback reponse
if (_debugEn) {
Expand All @@ -75,12 +93,15 @@ ELClientPacket* ELClient::protoCompletedCb(void) {
_debug->print(" ");
_debug->println(packet->argc);
}

_longPacket = 0;
fp = (FP<void, void*>*)packet->value;
if (fp->attached()) {
ELClientResponse resp(packet);
(*fp)(&resp);
}
return NULL;
}
case CMD_SYNC: // esp-link is not in sync, it may have reset, signal up the stack
_debug->println("NEED_SYNC!");
if (resetCb != NULL) (*resetCb)();
Expand Down Expand Up @@ -403,7 +424,7 @@ _serial(serial) {
*/
ELClient::ELClient(Stream* serial, Stream* debug) :
_debug(debug), _serial(serial) {
_debugEn = true;
// _debugEn = true; // Danny
init();
}

Expand Down
20 changes: 20 additions & 0 deletions ELClient/ELClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ typedef enum {
CMD_CB_ADD, /**< Add a custom callback */
CMD_CB_EVENTS, /**< ??? */
CMD_GET_TIME, /**< Get current time in seconds since the unix epoch */
CMD_GET_WIFI_INFO, /**< Get several bits of IP address info */
CMD_SET_WIFI_INFO, /**< Set several bits of IP address info */
//CMD_GET_INFO,

CMD_MQTT_SETUP = 10, /**< Register callback functions */
CMD_MQTT_PUBLISH, /**< Publish MQTT topic */
CMD_MQTT_SUBSCRIBE, /**< Subscribe to MQTT topic */
CMD_MQTT_LWT, /**< Define MQTT last will */
CMD_MQTT_GET_CLIENTID,

CMD_REST_SETUP = 20, /**< Setup REST connection */
CMD_REST_REQUEST, /**< Make request to REST server */
Expand All @@ -40,6 +43,22 @@ typedef enum {

CMD_SOCKET_SETUP = 40, /**< Setup socket connection */
CMD_SOCKET_SEND, /**< Send socket packet */

CMD_WIFI_GET_APCOUNT = 50, // Query number of access pointer
CMD_WIFI_GET_APNAME, // Get the name for an access point
CMD_WIFI_SELECT_SSID, // Connect to this network
CMD_WIFI_SIGNAL_STRENGTH, // Query RSSI
CMD_WIFI_GET_SSID, // Query SSID currently connected to
CMD_WIFI_START_SCAN, // Trigger a scan (takes a long time)

CMD_UPNP_SCAN = 60,
CMD_UPNP_ADD_PORT,
CMD_UPNP_REMOVE_PORT,
CMD_UPNP_BEGIN,
CMD_UPNP_QUERY_EXTERNAL_ADDRESS,

CMD_RESP_CB_CONTINUE = 70, // RESP_CB for a long packet

} CmdName; /**< Enumeration of commands supported by esp-link, this needs to match the definition in esp-link! */

enum WIFI_STATUS {
Expand Down Expand Up @@ -126,6 +145,7 @@ class ELClient {
uint16_t crc; /**< CRC checksum */
ELClientProtocol _proto; /**< Protocol structure */
uint8_t _protoBuf[128]; /**< Protocol buffer */
uint16_t _longPacket; /**< Packet length in case of long packet */

void init();
void DBG(const char* info);
Expand Down
205 changes: 205 additions & 0 deletions ELClient/ELClientCmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,208 @@ uint32_t ELClientCmd::GetTime() {
return pkt ? pkt->value : 0;
}

/*! GetWifiInfo()
@brief Get IP address info from ESP
@details ip address, network mask, gateway ip
@return Three parameters allow returning the values looked up, specify pointer to <code>uint32_t</code> in them.
@par Example
@code
uint32_t ip, nm, gw;
cmd.GetWifiInfo(&ip, &nm, &gw);
@endcode
*/
void ELClientCmd::GetWifiInfo(uint32_t *ptr_ip, uint32_t *ptr_netmask, uint32_t *ptr_gateway) {
clientCmdCb.attach(this, &ELClientCmd::wifiInfoCmdCallback);
_elc->Request(CMD_GET_WIFI_INFO, (uint32_t)&clientCmdCb, 0);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
if (_elc->_debugEn) {
_elc->_debug->println("Returning ...");
}
if (ptr_ip)
*ptr_ip = ip;
if (ptr_netmask)
*ptr_netmask = netmask;
if (ptr_gateway)
*ptr_gateway = gateway;
}

/*! SetWifiInfo()
@brief Set IP address
@details ip address, network mask, gateway ip
@return
@par Example
@code
uint32_t ip, nm, gw;
cmd.SetWifiInfo(ip, nm, gw);
@endcode
*/
void ELClientCmd::SetWifiInfo(uint32_t ptr_ip, uint32_t ptr_netmask, uint32_t ptr_gateway) {
_elc->Request(CMD_SET_WIFI_INFO, 0, 0);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
// return pkt ? pkt->value : 0;
}


/*! wifiInfoCmdCallback()
@brief Helper function to decode the three bits of information from the packet
@details See GetWifiInfo()
@return none
*/
void ELClientCmd::wifiInfoCmdCallback(void *res) {
ELClientResponse *resp = (ELClientResponse *)res;

resp->popArg(&ip, sizeof(ip));
if (_elc->_debugEn) {
_elc->_debug->print("IP ");
_elc->_debug->println(ip);
}

resp->popArg(&netmask, sizeof(netmask));
if (_elc->_debugEn) {
_elc->_debug->print("NM ");
_elc->_debug->println(netmask);
}

resp->popArg(&gateway, sizeof(gateway));
if (_elc->_debugEn) {
_elc->_debug->print("GW ");
_elc->_debug->println(gateway);
}

resp->popArg(&mac, sizeof(mac));
}

/*
* FIXME this depends on having called getWifiInfo
*/
char *ELClientCmd::getMac() {
return (char *)mac;
}

/*
* Query the number of Access Points scanned.
* FIXME this relies on having triggered such a scan
*/
uint32_t ELClientCmd::GetWifiApCount() {
_elc->Request(CMD_WIFI_GET_APCOUNT, 0, 0);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
return pkt ? pkt->value : -1;
}

/*
* Query the SSID of a network. Range and FIXME as with the ApCount.
*/
char * ELClientCmd::GetWifiApName(int i) {
uint16_t ix = i;

clientCmdCb.attach(this, &ELClientCmd::wifiGetApNameCallback);
_elc->Request(CMD_WIFI_GET_APNAME, (uint32_t)&clientCmdCb, 1);
_elc->Request(&ix, 2);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
if (_elc->_debugEn) {
_elc->_debug->println("Returning ...");
}

return ssid;
}

void ELClientCmd::wifiGetApNameCallback(void *res) {
ELClientResponse *resp = (ELClientResponse *)res;

if (ssid == 0) ssid = (char *)malloc(33);
resp->popArg(ssid, 33);
ssid[32] = '\0';
}

/*
* Query the MQTT clientid
*/
void ELClientCmd:: mqttGetClientIdCallback(void *res) {
ELClientResponse *resp = (ELClientResponse *)res;

if (mqtt_clientid == 0) mqtt_clientid = (char *)malloc(33);
resp->popArg(mqtt_clientid, 32);
mqtt_clientid[32] = '\0';
}

char *ELClientCmd::mqttGetClientId() {
mqttCmdCb.attach(this, &ELClientCmd::mqttGetClientIdCallback);
_elc->Request(CMD_MQTT_GET_CLIENTID, (uint32_t)&mqttCmdCb, 0);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
if (_elc->_debugEn) {
_elc->_debug->println("Returning ...");
}

return mqtt_clientid;
}

/*
* Query RSSI (signal strength)
*/
int ELClientCmd::GetRSSI(int i) {
char x = i;
_elc->Request(CMD_WIFI_SIGNAL_STRENGTH, 0, 1);
_elc->Request(&x, 1);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
return pkt ? pkt->value : 0;
}

void ELClientCmd::SelectSSID(char *ssid, char *pass) {
_elc->Request(CMD_WIFI_SELECT_SSID, 0, 2);
_elc->Request(ssid, strlen(ssid));
_elc->Request(pass, strlen(pass));
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
if (_elc->_debugEn) {
_elc->_debug->println("Returning ...");
}
}

void ELClientCmd::SelectSSID(int xssid, char *pass) {
unsigned char x = xssid;
_elc->Request(CMD_WIFI_SELECT_SSID, 0, 2);
_elc->Request(&x, 1);
_elc->Request(pass, strlen(pass));
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
if (_elc->_debugEn) {
_elc->_debug->println("Returning ...");
}
}

char *ELClientCmd::GetSSID() {
clientCmdCb.attach(this, &ELClientCmd::wifiGetApNameCallback);
_elc->Request(CMD_WIFI_GET_SSID, (uint32_t)&clientCmdCb, 0);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
if (_elc->_debugEn) {
_elc->_debug->println("Returning ...");
}

return ssid;
}

void ELClientCmd::StartScan() {
_elc->Request(CMD_WIFI_START_SCAN, 0, 0);
_elc->Request();

ELClientPacket *pkt = _elc->WaitReturn();
if (_elc->_debugEn) {
_elc->_debug->println("Returning ...");
}
}
22 changes: 22 additions & 0 deletions ELClient/ELClientCmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,30 @@ class ELClientCmd {
ELClientCmd(ELClient* elc);
// Get the current time in seconds since the epoch, 0 if the time is unknown
uint32_t GetTime();
void GetWifiInfo(uint32_t *, uint32_t *, uint32_t *);
void SetWifiInfo(uint32_t, uint32_t, uint32_t);
uint32_t GetWifiApCount();
char * GetWifiApName(int);
char *getMac();
char *mqttGetClientId();
int GetRSSI(int); // Current signal strength if <0, or selected network's rssi
void SelectSSID(char *, char *);
void SelectSSID(int, char *);
char *GetSSID();
void StartScan();

private:
ELClient* _elc; /**< ELClient instance */
FP<void, void*> clientCmdCb; /**< Pointer to external callback function */
void wifiInfoCmdCallback(void *resp);
uint32_t ip, netmask, gateway;
uint8_t mac[6];

char *ssid;
void wifiGetApNameCallback(void *);

FP<void, void*> mqttCmdCb; /**< Pointer to external callback function */
void mqttGetClientIdCallback(void *);
char *mqtt_clientid;
};
#endif
2 changes: 1 addition & 1 deletion ELClient/ELClientMqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ELClientMqtt::ELClientMqtt(ELClient* elc) :_elc(elc) {}
@endcode
*/
void ELClientMqtt::setup(void) {
Serial.print(F("ConnectedCB is 0x")); Serial.println((uint32_t)&connectedCb, 16);
// Serial.print(F("ConnectedCB is 0x")); Serial.println((uint32_t)&connectedCb, 16);
_elc->Request(CMD_MQTT_SETUP, 0, 4);
uint32_t cb = (uint32_t)&connectedCb;
_elc->Request(&cb, 4);
Expand Down
Loading