Skip to content

Commit

Permalink
stop overflowing serial output.
Browse files Browse the repository at this point in the history
Information from setup() method disappeared, like Reading Services for example
  • Loading branch information
saak2820 committed Jun 1, 2021
1 parent a674a63 commit c81ab19
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 90 deletions.
11 changes: 11 additions & 0 deletions DEBUG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
there is probably an error on loading the initial XML. as you can see the URL String has no Service "https://fritz.box:49443"
This should be : http://fritz.box:49000/upnp/control/x_voip

you can further Debug:

If you use wireshark you will probably see the correct httpResponse with the XML
https://www.it-techblog.de/fritzbox-und-wireshark-wlan-router-von-avm-monitoren-teil-1/11/2017/

you can debug HTTPClient as well. use HTTPClient in step 2

https://github.com/esp8266/Arduino/blob/master/doc/Troubleshooting/debugging.rst
6 changes: 1 addition & 5 deletions examples/caller/caller.ino
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ void loop() {
// char* status=getStatus();
delay(20000);
} else {
if (Serial) {
Serial.println();
Serial.printf("Button not pressed");
}
delay(50);

}
}

Expand Down
Binary file added src/HTTP_Client_Debug.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
151 changes: 74 additions & 77 deletions src/tr064.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ TR064::TR064(int port, String ip, String user, String pass) {
_user = user;
_pass = pass;
debug_level = DEBUG_ERROR;
this->_state = TR064_NO_SERVICES;
}

/**************************************************************************/
Expand All @@ -76,45 +77,33 @@ void TR064::initServiceURLs() {
/* TODO: We should give access to this data for users to inspect the
* possibilities of their device(s) - see #9 on Github.
*/
String inStr = httpRequest(_detectPage, "", "");
int CountChar = 7; //length of word "service"
int i = 0;
deb_println("Detected Services:", DEBUG_INFO);
while (inStr.indexOf("<service>") > 0 || inStr.indexOf("</service>") > 0) {
int indexStart=inStr.indexOf("<service>");
int indexStop= inStr.indexOf("</service>");
String serviceXML = inStr.substring(indexStart+CountChar+2, indexStop);
String servicename = xmlTakeParam(serviceXML, "serviceType");
String controlurl = xmlTakeParam(serviceXML, "controlURL");
_services[i][0] = servicename;
_services[i][1] = controlurl;
++i;

deb_print(" " + String(i) + "\t", DEBUG_INFO);
if (Serial) Serial.flush();
deb_println(servicename + " @ " + controlurl, DEBUG_INFO);
if (Serial) Serial.flush();
inStr = inStr.substring(indexStop+CountChar+3);
if(httpRequest(_detectPage, "", "")){
int CountChar = 7; //length of word "service"
int i = 0;
deb_println("Detected Services:", DEBUG_INFO);
while (_payload.indexOf("<service>") > 0 || _payload.indexOf("</service>") > 0) {
int indexStart=_payload.indexOf("<service>");
int indexStop= _payload.indexOf("</service>");
String serviceXML = _payload.substring(indexStart+CountChar+2, indexStop);
String servicename = xmlTakeParam(serviceXML, "serviceType");
String controlurl = xmlTakeParam(serviceXML, "controlURL");
_services[i][0] = servicename;
_services[i][1] = controlurl;
++i;

deb_print(" " + String(i) + "\t", DEBUG_INFO);
if (Serial) Serial.flush();
deb_println(servicename + " @ " + controlurl, DEBUG_INFO);
if (Serial) Serial.flush();
_payload = _payload.substring(indexStop+CountChar+3);
}
_state = TR064_SERVICES_LOADED;
}else{
deb_println("[TR064]<Error> initServiceURLs Failed", DEBUG_ERROR);
}
clear();
}

/**************************************************************************/
/*!
@brief <b>Deprecated, not required anymore.</b>
Only here for backwards-compatibility.
Fetches the initial nonce and the realm for internal use.
*/
/**************************************************************************/
void TR064::initNonce() {
deb_println("<warning> initNonce is deprecated and not required anymore. Might be removed in future versions.", DEBUG_WARNING);
deb_println("Geting the initial nonce and realm", DEBUG_INFO);
// TODO: Is this request supported by all devices or should we use a different one here?
String a[][2] = {{"NewAssociatedDeviceIndex", "1"}};
String xmlR = action_raw("urn:dslforum-org:service:WLANConfiguration:1", "GetGenericAssociatedDeviceInfo", a, 1);
takeNonce(xmlR);
}


/**************************************************************************/
/*!
@brief Generates and returns the XML-header for authentification.
Expand Down Expand Up @@ -159,7 +148,7 @@ String TR064::generateAuthToken() {
@return The response from the device.
*/
/**************************************************************************/
String TR064::action(String service, String act) {
bool TR064::action(String service, String act) {
deb_println("[action] simple", DEBUG_VERBOSE);
String p[][2] = {{}};
return action(service, act, p, 0);
Expand Down Expand Up @@ -190,13 +179,16 @@ String TR064::action(String service, String act) {
`req[][2] = {{ "resp1", "value1" }, { "resp2", "value2" }}`
@param nReq
The number of response parameters you passed.
@return The response from the device.
@return success bool
*/
/**************************************************************************/
String TR064::action(String service, String act, String params[][2], int nParam, String (*req)[2], int nReq) {
bool TR064::action(String service, String act, String params[][2], int nParam, String (*req)[2], int nReq) {
deb_println("[action] with extraction", DEBUG_VERBOSE);
String xmlR = action(service, act, params, nParam);
String body = xmlTakeParam(xmlR, "s:Body");

if(!action(service, act, params, nParam)){
return false;
}
String body = xmlTakeParam(_payload, "s:Body");

if (nReq > 0) {
for (int i=0; i<nReq; ++i) {
Expand All @@ -205,7 +197,7 @@ String TR064::action(String service, String act, String params[][2], int nParam,
}
}
}
return xmlR;
return true;
}

/**************************************************************************/
Expand All @@ -223,23 +215,22 @@ String TR064::action(String service, String act, String params[][2], int nParam,
`params[][2] = {{ "arg1", "value1" }, { "arg2", "value2" }}`.
@param nParam
The number of input parameters you passed.
@return The response from the device.
@return success bool
*/
/**************************************************************************/
String TR064::action(String service, String act, String params[][2], int nParam) {
deb_println("[action] with parameters", DEBUG_VERBOSE);

bool TR064::action(String service, String act, String params[][2], int nParam) {
deb_println("[action] with parameters", DEBUG_VERBOSE);
String status = "unauthenticated";
String xmlR = "";

int tries = 0; // Keep track on the number of times we tried to request.
while (status == "unauthenticated" && tries < 3) {
++tries;
while ((_nonce == "" || _realm == "") && tries <= 3) {
deb_println("[action] no nonce/realm found. requesting...", DEBUG_INFO);
// TODO: Is this request supported by all devices or should we use a different one here?
String a[][2] = {{"NewAssociatedDeviceIndex", "1"}};
xmlR = action_raw("urn:dslforum-org:service:WLANConfiguration:1", "GetGenericAssociatedDeviceInfo", a, 1);
takeNonce(xmlR);
action_raw("urn:dslforum-org:service:WLANConfiguration:1", "GetGenericAssociatedDeviceInfo", a, 1);
takeNonce(_payload);
if (_nonce == "" || _realm == "") {
++tries;
deb_println("[action]<error> nonce/realm request not successful!", DEBUG_ERROR);
Expand All @@ -248,26 +239,26 @@ String TR064::action(String service, String act, String params[][2], int nParam)
}
}

xmlR = action_raw(service, act, params, nParam);
status = xmlTakeParam(xmlR, "Status");
action_raw(service, act, params, nParam);
status = xmlTakeParam(_payload, "Status");
deb_println("[action] Response status: "+status, DEBUG_INFO);
status.toLowerCase();
// If we already have a nonce, but the request comes back unauthenticated.
if (status == "unauthenticated" && tries < 3) {
deb_println("[action]<error> got an unauthenticated error. Using the new nonce and trying again in 3s.", DEBUG_ERROR);
takeNonce(xmlR);
takeNonce(_payload);
delay(3000);
}
}

if (tries >= 3) {
deb_println("[action]<error> Giving up the request ", DEBUG_ERROR);
} else {
deb_println("[action] Done.", DEBUG_INFO);
takeNonce(xmlR);
return false;
} else {
takeNonce(_payload);
}

return xmlR;
deb_println("[action] Done.", DEBUG_INFO);
return true;
}

/**************************************************************************/
Expand All @@ -284,10 +275,10 @@ String TR064::action(String service, String act, String params[][2], int nParam)
`params[][2] = {{ "arg1", "value1" }, { "arg2", "value2" }}`.
@param nParam
The number of input parameters you passed (in `params`).
@return The response from the device.
@return success bool.
*/
/**************************************************************************/
String TR064::action_raw(String service, String act, String params[][2], int nParam) {
bool TR064::action_raw(String service, String act, String params[][2], int nParam) {
// Generate the XML-envelop
String xml = _requestStart + generateAuthXML() + "<s:Body><u:"+act+" xmlns:u='" + service + "'>";
// Add request-parameters to XML
Expand Down Expand Up @@ -368,10 +359,10 @@ String TR064::findServiceURL(String service) {
The requested action
@param xml
The request XML
@return The response from the device.
@return success bool.
*/
/**************************************************************************/
String TR064::httpRequest(String url, String xml, String soapaction) {
bool TR064::httpRequest(String url, String xml, String soapaction) {
return httpRequest(url, xml, soapaction, true);
}

Expand All @@ -388,14 +379,16 @@ String TR064::httpRequest(String url, String xml, String soapaction) {
The request XML
@param retry
Should the request be repeated with a new nonce, if it fails?
@return The response from the device.
@return success bool.
*/
/**************************************************************************/
String TR064::httpRequest(String url, String xml, String soapaction, bool retry) {
bool TR064::httpRequest(String url, String xml, String soapaction, bool retry) {
deb_println("[HTTP] prepare request to URL: http://" + _ip + ":" + _port + url, DEBUG_INFO);

HTTPClient http;
http.begin(_ip, _port, url);
if(!http.begin(_ip, _port, url)){
deb_println("[HTTP]<Error> , protocol check failed: check Debug from [HTTP-Client]", DEBUG_ERROR);
return false;
}
if (soapaction != "") {
http.addHeader("CONTENT-TYPE", "text/xml"); //; charset=\"utf-8\"
http.addHeader("SOAPACTION", soapaction);
Expand All @@ -416,17 +409,19 @@ String TR064::httpRequest(String url, String xml, String soapaction, bool retry)
deb_println("[HTTP] GET...", DEBUG_INFO);
}


String payload = "";
// httpCode will be negative on error
// HTTP header has been send and Server response header has been handled
deb_println("[HTTP] request code: " + String(httpCode), DEBUG_INFO);
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
deb_println("[HTTP] request code: " + String(httpCode), DEBUG_INFO);

// file found at server
if (httpCode == HTTP_CODE_OK) {
payload = http.getString();
_payload = http.getString();
}
deb_println("[HTTP] Received back", DEBUG_VERBOSE);
deb_println("---------------------------------", DEBUG_VERBOSE);
deb_println(_payload, DEBUG_VERBOSE);
deb_println("---------------------------------\n", DEBUG_VERBOSE);
http.end();
} else {
// Error
Expand All @@ -442,15 +437,10 @@ String TR064::httpRequest(String url, String xml, String soapaction, bool retry)
return httpRequest(url, xml, soapaction, false);
} else {
deb_println("[HTTP]<Error> Giving up.", DEBUG_ERROR);
return "";
_payload="";
return false;
}
}

deb_println("[HTTP] Received back", DEBUG_VERBOSE);
deb_println("---------------------------------", DEBUG_VERBOSE);
deb_println(payload, DEBUG_VERBOSE);
deb_println("---------------------------------\n", DEBUG_VERBOSE);
return payload;
}


Expand Down Expand Up @@ -607,4 +597,11 @@ void TR064::deb_println(String message, int level) {
}
}

int TR064::state() {
return this->_state;
}

void TR064::clear()
{
_payload="";
}
28 changes: 20 additions & 8 deletions src/tr064.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@
#define arr_len( x ) ( sizeof( x ) / sizeof( *x ) ) ///< Gives the length of an array

// Different debug level
#define DEBUG_NONE 0 ///< Print no debug messages whatsoever
/* #define DEBUG_NONE 0 ///< Print no debug messages whatsoever
#define DEBUG_ERROR 1 ///< Only print error messages
#define DEBUG_WARNING 2 ///< Only print error and warning messages
#define DEBUG_INFO 3 ///< Print error, warning and info messages
#define DEBUG_VERBOSE 4 ///< Print all messages
#define DEBUG_VERBOSE 4 ///< Print all messages */

// Possible values for client.state()
#define TR064_NO_SERVICES -1
#define TR064_SERVICES_LOADED 0

/**************************************************************************/
/*!
Expand All @@ -55,31 +58,39 @@

class TR064 {
public:
enum LoggingLevels {DEBUG_NONE, DEBUG_ERROR, DEBUG_WARNING, DEBUG_INFO, DEBUG_VERBOSE};
TR064(int port, String ip, String user, String pass);
void init();
void initNonce();
String action(String service, String act);
String action(String service, String act, String params[][2], int nParam);
String action(String service, String act, String params[][2], int nParam, String (*req)[2], int nReq);
bool action(String service, String act);
bool action(String service, String act, String params[][2], int nParam);
bool action(String service, String act, String params[][2], int nParam, String (*req)[2], int nReq);
String xmlTakeParam(String inStr, String needParam);
String xmlTakeInsensitiveParam(String inStr, String needParam);
String xmlTakeSensitiveParam(String inStr, String needParam);
String md5String(String s);
String byte2hex(byte number);
int debug_level; ///< Available levels are `DEBUG_NONE`, `DEBUG_ERROR`, `DEBUG_WARNING`, `DEBUG_INFO`, and `DEBUG_VERBOSE`.
int state();
private:
//TODO: More consistent naming.

WiFiClient tr064client;
HTTPClient http;

void initServiceURLs();
void deb_print(String message, int level);
void deb_println(String message, int level);
String action_raw(String service, String act, String params[][2], int nParam);
bool action_raw(String service, String act, String params[][2], int nParam);
void takeNonce(String xml);
String httpRequest(String url, String xml, String action);
String httpRequest(String url, String xml, String action, bool retry);
bool httpRequest(String url, String xml, String action);
bool httpRequest(String url, String xml, String action, bool retry);
String generateAuthToken();
String generateAuthXML();
String findServiceURL(String service);
String _xmlTakeParam(String inStr, String needParam);
void clear();
int _state;
String _ip;
int _port;
String _user;
Expand All @@ -95,6 +106,7 @@ class TR064 {
TODO: Remove 100 services limits here
*/
String _services[100][2];
String _payload;
};

#endif

0 comments on commit c81ab19

Please sign in to comment.