Skip to content

Commit f802bb6

Browse files
committed
Merge pull request #3 from esp8266/esp8266-sdk-1.0
trtyrty
2 parents bb5cda6 + 6476759 commit f802bb6

File tree

15 files changed

+398
-36
lines changed

15 files changed

+398
-36
lines changed

README.md

+21-18
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ This is mostly similar to WiFi shield library. Differences include:
102102
- ```WiFi.RSSI()``` doesn't work
103103
- ```WiFi.printDiag(Serial);``` will print out some diagnostic info
104104
- ```WiFiUDP``` class supports sending and receiving multicast packets on STA interface.
105-
When sending a multicast packet, replace ```udp.beginPacket(addr, port)``` with
105+
When sending a multicast packet, replace ```udp.beginPacket(addr, port)``` with
106106
```udp.beginPacketMulticast(addr, port, WiFi.localIP())```.
107-
When listening to multicast packets, replace ```udp.begin(port)``` with
107+
When listening to multicast packets, replace ```udp.begin(port)``` with
108108
```udp.beginMulticast(WiFi.localIP(), multicast_ip_addr, port)```.
109109
You can use ```udp.destinationIP()``` to tell whether the packet received was
110110
sent to the multicast or unicast address.
@@ -117,7 +117,7 @@ Four samples are provided for this library.
117117

118118
Library for calling functions repeatedly with a certain period. Two examples included.
119119

120-
It is currently not recommended to do blocking IO operations (network, serial, file) from Ticker
120+
It is currently not recommended to do blocking IO operations (network, serial, file) from Ticker
121121
callback functions. Instead, set a flag inside the ticker callback and check for that flag inside the loop function.
122122

123123
#### EEPROM ####
@@ -183,6 +183,11 @@ Allows the sketch to respond to multicast DNS queries for domain names like "foo
183183
Currently the library only works on STA interface, AP interface is not supported.
184184
See attached example and library README file for details.
185185

186+
#### DNS server (DNSServer library) ####
187+
188+
Implements a simple DNS server that can be used in both STA and AP modes. The DNS server currently supports only one domain (for all other domains it will reply with NXDOMAIN or custom status code). With it clients can open a web server running on ESP8266 using a domain name, not an IP address.
189+
See attached example for details.
190+
186191
#### Servo ####
187192

188193
This library exposes the ability to control RC (hobby) servo motors. It will support upto 24 servos on any available output pin. By defualt the first 12 servos will use Timer0 and currently this will not interfere with any other support. Servo counts above 12 will use Timer1 and features that use it will be effected.
@@ -211,10 +216,10 @@ You need to put ESP8266 into bootloader mode before uploading code.
211216
For stable use of the ESP8266 a power supply with 3V3 and >= 250mA is required.
212217

213218
* Note
214-
- using Power from USB to Serial is may unstable, they not deliver enough current.
215-
219+
- using Power from USB to Serial is may unstable, they not deliver enough current.
220+
216221
#### Serial Adapter ####
217-
222+
218223
There are many different USB to Serial adapters / boards.
219224

220225
* Note
@@ -223,17 +228,17 @@ There are many different USB to Serial adapters / boards.
223228
- not all board have all pins of the ICs as breakout (check before order)
224229
- CTS and DSR are not useful for upload (they are Inputs)
225230

226-
* Working ICs
231+
* Working ICs
227232
- FT232RL
228233
- CP2102
229234
- may others (drop a comment)
230-
235+
231236
#### Minimal hardware Setup for Bootloading and usage ####
232237

233238
ESPxx Hardware
234239

235240
| PIN | Resistor | Serial Adapter |
236-
| ------------- | -------- | -------------- |
241+
| ------------- | -------- | -------------- |
237242
| VCC | | VCC (3.3V) |
238243
| GND | | GND |
239244
| TX or GPIO2* | | RX |
@@ -243,19 +248,19 @@ ESPxx Hardware
243248
| GPIO15* | PullDown | |
244249
| CH_PD | PullUp | |
245250

246-
* Note
251+
* Note
247252
- GPIO15 is also named MTDO
248253
- Reset is also named RSBT or REST (adding PullUp improves the stability of the Module)
249254
- GPIO2 is alternative TX for the boot loader mode
250-
255+
251256
###### esp to Serial
252257
![ESP to Serial](https://raw.githubusercontent.com/Links2004/Arduino/esp8266/docs/ESP_to_serial.png)
253258

254259
#### Minimal hardware Setup for Bootloading only ####
255260
ESPxx Hardware
256261

257262
| PIN | Resistor | Serial Adapter |
258-
| ------------- | -------- | --------------- |
263+
| ------------- | -------- | --------------- |
259264
| VCC | | VCC (3.3V) |
260265
| GND | | GND |
261266
| TX or GPIO2 | | RX |
@@ -265,15 +270,15 @@ ESPxx Hardware
265270
| GPIO15 | PullDown | |
266271
| CH_PD | PullUp | |
267272

268-
* Note
273+
* Note
269274
- if no RTS is used a manual power toggle is needed
270-
275+
271276
#### Minimal hardware Setup for running only ####
272277

273278
ESPxx Hardware
274279

275280
| PIN | Resistor | Power supply |
276-
| ------------- | -------- | --------------- |
281+
| ------------- | -------- | --------------- |
277282
| VCC | | VCC (3.3V) |
278283
| GND | | GND |
279284
| GPIO0 | PullUp | |
@@ -284,7 +289,7 @@ ESPxx Hardware
284289
![ESP min](https://raw.githubusercontent.com/Links2004/Arduino/esp8266/docs/ESP_min.png)
285290

286291
###### improved stability
287-
![ESP improved stability](https://raw.githubusercontent.com/Links2004/Arduino/esp8266/docs/ESP_improved_stability.png)
292+
![ESP improved stability](https://raw.githubusercontent.com/Links2004/Arduino/esp8266/docs/ESP_improved_stability.png)
288293

289294
### Issues and support ###
290295

@@ -305,5 +310,3 @@ Esptool written by Christian Klippel is licensed under GPLv2, currently maintain
305310
ESP8266 core support, ESP8266WiFi, Ticker, ESP8266WebServer libraries were written by Ivan Grokhotkov, [email protected].
306311

307312
[SPI Flash File System (SPIFFS)](https://github.com/pellepl/spiffs) written by Peter Andersson is used in this project. It is distributed under MIT license.
308-
309-

hardware/esp8266com/esp8266/cores/esp8266/Arduino.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ void ets_intr_unlock();
143143
// level 15 will disable ALL interrupts,
144144
// level 0 will disable most software interrupts
145145
//
146-
#define xt_disable_interrupts(state, level) __asm__ __volatile__("rsil %0," __STRINGIFY(level) "; esync; isync; dsync" : "=a" (state))
147-
#define xt_enable_interrupts(state) __asm__ __volatile__("wsr %0,ps; esync" :: "a" (state) : "memory")
146+
#define xt_disable_interrupts(state, level) __asm__ __volatile__("rsil %0," __STRINGIFY(level) : "=a" (state))
147+
#define xt_enable_interrupts(state) __asm__ __volatile__("wsr %0,ps; isync" :: "a" (state) : "memory")
148148

149149
extern uint32_t interruptsState;
150150

151151
#define interrupts() xt_enable_interrupts(interruptsState)
152-
#define noInterrupts() __asm__ __volatile__("rsil %0,15; esync; isync; dsync" : "=a" (interruptsState))
152+
#define noInterrupts() __asm__ __volatile__("rsil %0,15" : "=a" (interruptsState))
153153

154154
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
155155
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
@@ -209,13 +209,15 @@ void loop(void);
209209
// This comes from the pins_*.c file for the active board configuration.
210210
#define digitalPinToPort(pin) (0)
211211
#define digitalPinToBitMask(pin) (1UL << (pin))
212+
#define digitalPinToTimer(pin) (0)
212213
#define portOutputRegister(port) ((volatile uint32_t*) GPO)
213214
#define portInputRegister(port) ((volatile uint32_t*) GPI)
214215
#define portModeRegister(port) ((volatile uint32_t*) GPE)
215216

216217
#define NOT_A_PIN -1
217218
#define NOT_A_PORT -1
218219
#define NOT_AN_INTERRUPT -1
220+
#define NOT_ON_TIMER 0
219221

220222
#ifdef __cplusplus
221223
} // extern "C"

hardware/esp8266com/esp8266/cores/esp8266/Esp.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class EspClass {
113113
uint32_t EspClass::getCycleCount()
114114
{
115115
uint32_t ccount;
116-
__asm__ __volatile__("rsr %0,ccount":"=a" (ccount));
116+
__asm__ __volatile__("esync; rsr %0,ccount":"=a" (ccount));
117117
return ccount;
118118
}
119119

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <ESP8266WiFi.h>
2+
#include <DNSServer.h>
3+
4+
const byte DNS_PORT = 53;
5+
IPAddress apIP(192, 168, 1, 1);
6+
DNSServer dnsServer;
7+
8+
void setup() {
9+
WiFi.mode(WIFI_AP);
10+
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
11+
WiFi.softAP("DNSServer example");
12+
13+
// modify TTL associated with the domain name (in seconds)
14+
// default is 60 seconds
15+
dnsServer.setTTL(300);
16+
// set which return code will be used for all other domains (e.g. sending
17+
// ServerFailure instead of NonExistentDomain will reduce number of queries
18+
// sent by clients)
19+
// default is DNSReplyCode::NonExistentDomain
20+
dnsServer.setErrorReplyCode(DNSReplyCode::ServerFailure);
21+
22+
//start DNS server for a specific domain name
23+
dnsServer.start(DNS_PORT, "www.example.com", apIP);
24+
}
25+
26+
void loop() {
27+
dnsServer.processNextRequest();
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=DNSServer
2+
version=1.0.0
3+
author=Kristijan Novoselić
4+
maintainer=Kristijan Novoselić, <[email protected]>
5+
sentence=A simple DNS server for ESP8266.
6+
paragraph=This library implements a simple DNS server.
7+
category=Communication
8+
url=
9+
architectures=esp8266
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include "DNSServer.h"
2+
#include <lwip/def.h>
3+
#include <Arduino.h>
4+
5+
DNSServer::DNSServer()
6+
{
7+
_ttl = htonl(60);
8+
_errorReplyCode = DNSReplyCode::NonExistentDomain;
9+
}
10+
11+
bool DNSServer::start(const uint16_t &port, const String &domainName,
12+
const IPAddress &resolvedIP)
13+
{
14+
_port = port;
15+
_domainName = domainName;
16+
_resolvedIP[0] = resolvedIP[0];
17+
_resolvedIP[1] = resolvedIP[1];
18+
_resolvedIP[2] = resolvedIP[2];
19+
_resolvedIP[3] = resolvedIP[3];
20+
downcaseAndRemoveWwwPrefix(_domainName);
21+
return _udp.begin(_port) == 1;
22+
}
23+
24+
void DNSServer::setErrorReplyCode(const DNSReplyCode &replyCode)
25+
{
26+
_errorReplyCode = replyCode;
27+
}
28+
29+
void DNSServer::setTTL(const uint32_t &ttl)
30+
{
31+
_ttl = htonl(ttl);
32+
}
33+
34+
void DNSServer::stop()
35+
{
36+
_udp.stop();
37+
}
38+
39+
void DNSServer::downcaseAndRemoveWwwPrefix(String &domainName)
40+
{
41+
domainName.toLowerCase();
42+
domainName.replace("www.", "");
43+
}
44+
45+
void DNSServer::processNextRequest()
46+
{
47+
_currentPacketSize = _udp.parsePacket();
48+
if (_currentPacketSize)
49+
{
50+
_buffer = (unsigned char*)malloc(_currentPacketSize * sizeof(char));
51+
_udp.read(_buffer, _currentPacketSize);
52+
_dnsHeader = (DNSHeader*) _buffer;
53+
54+
if (_dnsHeader->QR == DNS_QR_QUERY &&
55+
_dnsHeader->OPCode == DNS_OPCODE_QUERY &&
56+
requestIncludesOnlyOneQuestion() &&
57+
getDomainNameWithoutWwwPrefix() == _domainName)
58+
{
59+
replyWithIP();
60+
}
61+
else if (_dnsHeader->QR == DNS_QR_QUERY)
62+
{
63+
replyWithCustomCode();
64+
}
65+
66+
free(_buffer);
67+
}
68+
}
69+
70+
bool DNSServer::requestIncludesOnlyOneQuestion()
71+
{
72+
return ntohs(_dnsHeader->QDCount) == 1 &&
73+
_dnsHeader->ANCount == 0 &&
74+
_dnsHeader->NSCount == 0 &&
75+
_dnsHeader->ARCount == 0;
76+
}
77+
78+
String DNSServer::getDomainNameWithoutWwwPrefix()
79+
{
80+
String parsedDomainName = "";
81+
unsigned char *start = _buffer + 12;
82+
if (*start == 0)
83+
{
84+
return parsedDomainName;
85+
}
86+
int pos = 0;
87+
while(true)
88+
{
89+
unsigned char labelLength = *(start + pos);
90+
for(int i = 0; i < labelLength; i++)
91+
{
92+
pos++;
93+
parsedDomainName += (char)*(start + pos);
94+
}
95+
pos++;
96+
if (*(start + pos) == 0)
97+
{
98+
downcaseAndRemoveWwwPrefix(parsedDomainName);
99+
return parsedDomainName;
100+
}
101+
else
102+
{
103+
parsedDomainName += ".";
104+
}
105+
}
106+
}
107+
108+
void DNSServer::replyWithIP()
109+
{
110+
_dnsHeader->QR = DNS_QR_RESPONSE;
111+
_dnsHeader->ANCount = _dnsHeader->QDCount;
112+
_dnsHeader->QDCount = 0;
113+
114+
_udp.beginPacket(_udp.remoteIP(), _udp.remotePort());
115+
_udp.write(_buffer, _currentPacketSize);
116+
_udp.write((unsigned char*)&_ttl, 4);
117+
// Length of RData is 4 bytes (because, in this case, RData is IPv4)
118+
_udp.write((uint8_t)0);
119+
_udp.write((uint8_t)4);
120+
_udp.write(_resolvedIP, sizeof(_resolvedIP));
121+
_udp.endPacket();
122+
}
123+
124+
void DNSServer::replyWithCustomCode()
125+
{
126+
_dnsHeader->QR = DNS_QR_RESPONSE;
127+
_dnsHeader->RCode = (unsigned char)_errorReplyCode;
128+
_dnsHeader->QDCount = 0;
129+
130+
_udp.beginPacket(_udp.remoteIP(), _udp.remotePort());
131+
_udp.write(_buffer, sizeof(DNSHeader));
132+
_udp.endPacket();
133+
}

0 commit comments

Comments
 (0)