TR-064 SOAP Library
tr064.h
Go to the documentation of this file.
1 
22 #ifndef tr064_h
23 #define tr064_h
24 
25 #include "Arduino.h"
26 #include <MD5Builder.h>
27 #if defined(ESP8266)
28  //if(Serial) Serial.println(F("Version compiled for ESP8266."));
29  #include <ESP8266WiFi.h>
30  #include <ESP8266HTTPClient.h>
31 #elif defined(ESP32)
32  //if(Serial) Serial.println(F("Version compiled for ESP32."));
33  #include <WiFi.h>
34  #include <HTTPClient.h>
35 
36 #else
37  //INCOMPATIBLE!
38 #endif
39 
41 typedef enum {
42 
43  //TR064_CODE_UNAUTHORIZED = 401, //If a user is not authenticated, 401 (“Unauthorized”) will be returned.
44  TR064_CODE_UNKNOWNACTION = 401,
45  TR064_CODE_FALSEARGUMENTS = 402, //If a user is not authenticated, 401 (“Unauthorized”) will be returned.
46  TR064_CODE_ACTIONNOTAUTHORIZED = 606, //If a user is authenticated but has not the needed rights, 606 (“Action not authorized”) will be returned
47  TR064_CODE_SECONDFACTORAUTHREQUIRED = 866, //If an action needs 2FA, the status code 866 (“second factor authentication required”)
48  TR064_CODE_SECONDFACTORAUTHBLOCKED = 867, // (“second factor authentication blocked”) or
49  TR064_CODE_SECONDFACTORAUTHBUSY = 868, // (“second factorauthentication busy”) will be returned and the 2FA procedure
51 
52 
53 
54 #define arr_len( x ) ( sizeof( x ) / sizeof( *x ) )
55 
56 // Possible values for client.state()
57 #define TR064_NO_SERVICES -1
58 #define TR064_SERVICES_LOADED 0
59 
60 // Possible values for client.state()
61 #define TR064_NO_SERVICES -1
62 #define TR064_SERVICES_LOADED 0
63 
64 // Possible values for client.state()
65 #define TR064_NO_SERVICES -1
66 #define TR064_SERVICES_LOADED 0
67 
68 /**************************************************************************/
74 /**************************************************************************/
75 
76 class TR064 {
77  public:
85  enum LoggingLevels {DEBUG_NONE, DEBUG_ERROR, DEBUG_WARNING, DEBUG_INFO, DEBUG_VERBOSE};
86 
89 
90  //TR064_CODE_UNAUTHORIZED = 401, //If a user is not authenticated, 401 (“Unauthorized”) will be returned.
91  TR064_CODE_UNKNOWNACTION = 401,
92  TR064_CODE_FALSEARGUMENTS = 402, //If a user is not authenticated, 401 (“Unauthorized”) will be returned.
93  TR064_CODE_AUTHFAILED = 503, //If a user is not authenticated, 401 (“Unauthorized”) will be returned.
94  TR064_CODE_ARGUMENTVALUEINVALIDE = 600, // Argument Value Invalid
95  TR064_CODE_ACTIONNOTAUTHORIZED = 606, //If a user is authenticated but has not the needed rights, 606 (“Action not authorized”) will be returned
96  TR064_CODE_ARRAYINDEXINVALID = 713, //SpecifiedArrayIndexInvalid
97  TR064_CODE_NOSUCHENTRY = 714, //No such array entry in array
98  TR064_CODE_INTERNALERROR = 820, // Internal Error
99  TR064_CODE_SECONDFACTORAUTHREQUIRED = 866, //If an action needs 2FA, the status code 866 (“second factor authentication required”)
100  TR064_CODE_SECONDFACTORAUTHBLOCKED = 867, // (“second factor authentication blocked”) or
101  TR064_CODE_SECONDFACTORAUTHBUSY = 868, // (“second factorauthentication busy”) will be returned and the 2FA procedure
102  } ;
103 
104  TR064();
105  TR064(uint16_t port, const String& ip, const String& user, const String& pass);
106  ~TR064() {}
107  TR064& setServer(uint16_t port, const String& ip, const String& user, const String& pass);
108  void init();
109  int state();
110 
111  bool action(const String& service, const String& act, String params[][2] = {}, int nParam = 0,const String& url = "");
112  //bool action(const String& service, const String& act, String params[][2], int nParam, const String& url = "");
113  bool action(const String& service, const String& act, String params[][2], int nParam, String (*req)[2], int nReq, const String& url = "");
114 
115  String md5String(const String& s);
116  String byte2hex(byte number);
118 
119  private:
120  WiFiClient tr064client;
121  HTTPClient http;
122 
123  //TODO: More consistent naming
124 
125  void initServiceURLs();
126  void deb_print(const String& message, int level);
127  void deb_println(const String& message, int level);
128  bool action_raw(const String& service,const String& act, String params[][2], int nParam, const String& url = "");
129  bool httpRequest(const String& url, const String& xml, const String& action, bool retry);
130  String generateAuthToken();
131  String generateAuthXML();
132  String findServiceURL(const String& service);
133  String cleanOldServiceName(const String& service);
134  bool xmlTakeParam(String (*params)[2], int nParam);
135  bool xmlTakeParam(String& value, const String& needParam);
136  static String errorToString(int error);
137 
138  int _state;
139  String _ip;
140  uint16_t _port;
141  String _user;
142  String _pass;
143  String _realm; // To be requested from the router
144  String _secretH; // To be generated
145  String _nonce = "";
146  String _status;
147 
148  const char* const _requestStart = "<?xml version=\"1.0\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">";
149  const char* const _detectPage = "/tr64desc.xml";
150  const char* const _servicePrefix = "urn:dslforum-org:service:";
151  unsigned long lastOutActivity;
152  unsigned long lastInActivity;
153  /*
154  * TODO: We should give access to this data for users to inspect the
155  * possibilities of their device(s) - see #9 on Github.
156  * TODO: Remove 100 services limits here
157  */
158  String _services[100][2];
159 };
160 
161 #endif
Class to easily make TR-064 calls. This is the main class of this library. An object of this class ke...
Definition: tr064.h:76
String md5String(const String &s)
Translates a string into its MD5 hash.
Definition: tr064.cpp:534
bool action(const String &service, const String &act, String params[][2]={}, int nParam=0, const String &url="")
This function will call an action on the service of the device with certain parameters and return val...
Definition: tr064.cpp:205
TR064()
Library/Class to easily make TR-064 calls. Do not construct this unless you have a working connection...
Definition: tr064.cpp:66
tr064_codes
HTTP codes see RFC7231.
Definition: tr064.h:88
TR064 & setServer(uint16_t port, const String &ip, const String &user, const String &pass)
Set the server parameters, needed if the empty constructor was used.
Definition: tr064.cpp:100
t_tr064_codes
HTTP codes see RFC7231.
Definition: tr064.h:41
LoggingLevels
Definition: tr064.h:85
int debug_level
Available levels are DEBUG_NONE, DEBUG_ERROR, DEBUG_WARNING, DEBUG_INFO, and DEBUG_VERBOSE.
Definition: tr064.h:117
String byte2hex(byte number)
Translates a byte number into a hex number.
Definition: tr064.cpp:554
void init()
Initializes the library. Needs to be explicitly called. There should already be a working connection ...
Definition: tr064.cpp:78
int state()
Returns the State of Service Load.
Definition: tr064.cpp:350