Skip to content

Commit 14bb946

Browse files
committed
Merge pull request #2 from esp8266/master
pull master
2 parents a8976a0 + 810ab68 commit 14bb946

File tree

8 files changed

+211
-60
lines changed

8 files changed

+211
-60
lines changed

Diff for: cores/esp8266/Arduino.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void ets_intr_unlock();
153153
// level (0-15), interrupts of the given level and above will be active
154154
// level 15 will disable ALL interrupts,
155155
// level 0 will enable ALL interrupts,
156-
//
156+
//
157157
#define xt_rsil(level) (__extension__({uint32_t state; __asm__ __volatile__("rsil %0," __STRINGIFY(level) : "=a" (state)); state;}))
158158
#define xt_wsr_ps(state) __asm__ __volatile__("wsr %0,ps; isync" :: "a" (state) : "memory")
159159

@@ -271,6 +271,8 @@ long random(long, long);
271271
void randomSeed(unsigned long);
272272
long map(long, long, long, long, long);
273273

274+
extern "C" void configTime(int timezone, int daylightOffset_sec,
275+
const char* server1, const char* server2 = nullptr, const char* server3 = nullptr);
274276

275277
#endif
276278

Diff for: cores/esp8266/core_esp8266_si2c.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,15 @@ static unsigned char twi_read_byte(bool nack) {
150150
unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop){
151151
unsigned int i;
152152
if(!twi_write_start()) return 4;//line busy
153-
if(!twi_write_byte(((address << 1) | 0) & 0xFF)) return 2;//received NACK on transmit of address
154-
for(i=0; i<len; i++){
155-
if(!twi_write_byte(buf[i])) return 3;//received NACK on transmit of data
153+
if(!twi_write_byte(((address << 1) | 0) & 0xFF)) {
154+
if (sendStop) twi_write_stop();
155+
return 2; //received NACK on transmit of address
156+
}
157+
for(i=0; i<len; i++) {
158+
if(!twi_write_byte(buf[i])) {
159+
if (sendStop) twi_write_stop();
160+
return 3;//received NACK on transmit of data
161+
}
156162
}
157163
if(sendStop) twi_write_stop();
158164
i = 0;
@@ -168,7 +174,10 @@ unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned i
168174
unsigned char twi_readFrom(unsigned char address, unsigned char* buf, unsigned int len, unsigned char sendStop){
169175
unsigned int i;
170176
if(!twi_write_start()) return 4;//line busy
171-
if(!twi_write_byte(((address << 1) | 1) & 0xFF)) return 2;//received NACK on transmit of address
177+
if(!twi_write_byte(((address << 1) | 1) & 0xFF)) {
178+
if (sendStop) twi_write_stop();
179+
return 2;//received NACK on transmit of address
180+
}
172181
for(i=0; i<(len-1); i++) buf[i] = twi_read_byte(false);
173182
buf[len-1] = twi_read_byte(true);
174183
if(sendStop) twi_write_stop();

Diff for: cores/esp8266/libc_replacements.c

-18
Original file line numberDiff line numberDiff line change
@@ -393,24 +393,6 @@ int* __errno(void) {
393393
return &errno_var;
394394
}
395395

396-
397-
char * ctime(const time_t *clock) {
398-
return 0;
399-
}
400-
401-
time_t time(time_t * t) {
402-
return 0;
403-
}
404-
405-
int gettimeofday(void *tp, void *tzp) {
406-
return 0;
407-
}
408-
409-
time_t mktime(struct tm *timp) {
410-
return 0;
411-
}
412-
413-
414396
/*
415397
* begin newlib/string/strlcpy.c
416398
*

Diff for: cores/esp8266/time.c

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* time.c - ESP8266-specific functions for SNTP
3+
* Copyright (c) 2015 Peter Dobler. All rights reserved.
4+
* This file is part of the esp8266 core for Arduino environment.
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the GNU Lesser General Public License for more details.
13+
* You should have received a copy of the GNU Lesser General Public
14+
* License along with this library; if not, write to the Free Software
15+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16+
*
17+
*/
18+
19+
#include <time.h>
20+
#include "sntp.h"
21+
22+
23+
#ifndef _TIMEVAL_DEFINED
24+
#define _TIMEVAL_DEFINED
25+
struct timeval {
26+
time_t tv_sec;
27+
suseconds_t tv_usec;
28+
};
29+
#endif
30+
31+
extern char* sntp_asctime(const struct tm *t);
32+
extern struct tm* sntp_localtime(const time_t *clock);
33+
34+
// time gap in seconds from 01.01.1900 (NTP time) to 01.01.1970 (UNIX time)
35+
#define DIFF1900TO1970 2208988800UL
36+
37+
static int s_daylightOffset_sec = 0;
38+
static int s_timezone_sec = 0;
39+
static time_t s_bootTime = 0;
40+
41+
// calculate offset used in gettimeofday
42+
static void ensureBootTimeIsSet()
43+
{
44+
if (!s_bootTime)
45+
{
46+
time_t now = sntp_get_current_timestamp();
47+
if (now)
48+
{
49+
s_bootTime = - millis() / 1000;
50+
}
51+
}
52+
}
53+
54+
static void setServer(int id, const char* name_or_ip)
55+
{
56+
if (name_or_ip)
57+
{
58+
//TODO: check whether server is given by name or IP
59+
sntp_setservername(0, (char*) name_or_ip);
60+
}
61+
}
62+
63+
void configTime(int timezone, int daylightOffset_sec, const char* server1, const char* server2, const char* server3)
64+
{
65+
sntp_stop();
66+
67+
setServer(0, server1);
68+
setServer(1, server2);
69+
setServer(2, server3);
70+
71+
s_timezone_sec = timezone;
72+
s_daylightOffset_sec = daylightOffset_sec;
73+
sntp_set_timezone(timezone/3600);
74+
sntp_init();
75+
}
76+
77+
int clock_gettime(clockid_t unused, struct timespec *tp)
78+
{
79+
tp->tv_sec = millis() / 1000;
80+
tp->tv_nsec = micros() * 1000;
81+
return 0;
82+
}
83+
84+
// seconds since 1970
85+
time_t mktime(struct tm *t)
86+
{
87+
// system_mktime expects month in range 1..12
88+
#define START_MONTH 1
89+
return DIFF1900TO1970 + system_mktime(t->tm_year, t->tm_mon + START_MONTH, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
90+
}
91+
92+
time_t time(time_t * t)
93+
{
94+
time_t seconds = sntp_get_current_timestamp();
95+
ensureBootTimeIsSet();
96+
if (t)
97+
{
98+
*t = seconds;
99+
}
100+
return seconds;
101+
}
102+
103+
char* asctime(const struct tm *t)
104+
{
105+
return sntp_asctime(t);
106+
}
107+
108+
struct tm* localtime(const time_t *clock)
109+
{
110+
return sntp_localtime(clock);
111+
}
112+
113+
char* ctime(const time_t *t)
114+
{
115+
struct tm* p_tm = localtime(t);
116+
char* result = asctime(p_tm);
117+
return result;
118+
}
119+
120+
int gettimeofday(struct timeval *tp, void *tzp)
121+
{
122+
if (tp)
123+
{
124+
ensureBootTimeIsSet();
125+
tp->tv_sec = (s_bootTime + millis()) / 1000;
126+
tp->tv_usec = micros() * 1000;
127+
}
128+
}

Diff for: doc/ota_updates.md

+29-36
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,45 @@ title: OTA Update
77
* [Arduino IDE](#arduino-ide)
88
* [HTTP Server](#http-server)
99
* [Stream Interface](#stream-interface)
10-
10+
1111
## Basic Requirements
1212

13-
- Flash chip size is 2x the size of the sketch
14-
13+
- Flash chip size is 2x the size of the sketch.
14+
1515
## Arduino IDE
1616

1717
TODO describe Arduino IDE OTA process
1818

1919
#### Requirements
20-
- The ESP and the Computer must be connected to the Same network.
21-
20+
- The ESP and the computer must be connected to the same network.
2221

2322
## HTTP Server
2423

25-
the ```ESPhttpUpdate``` class can check for updates and download a binary file form a HTTP web server.
26-
It is possible to download updates from every IP or domain address on the Network or Internet.
27-
24+
```ESPhttpUpdate``` class can check for updates and download a binary file from HTTP web server.
25+
It is possible to download updates from every IP or domain address on the network or Internet.
2826

2927
#### Requirements
3028
- web server
3129

32-
3330
#### Arduino code
3431

35-
##### simple updater
32+
##### Simple updater
3633

37-
the Simple Updater downloads the File every time the function is called.
34+
Simple updater downloads the file every time the function is called.
3835

3936
```cpp
4037
ESPhttpUpdate.update("192.168.0.2", 80, "/arduino.bin");
4138
```
4239

43-
##### advanced updater
40+
##### Advanced updater
4441

45-
Its possible to point to a script at the server.
46-
If a version String is delivered to the Function this String will be send to the server.
47-
A Server side Update check is now possible.
42+
Its possible to point update function to a script at the server.
43+
If version string argument is given, it will be sent to the server.
44+
Server side script can use this to check if update should be performed.
4845

49-
the Server can return a binary file for update (Header 200)
50-
or it return header 304 to notify the ESP that no Update is needed.
46+
Server side script can respond as follows:
47+
- response code 200, and send the firmware image,
48+
- or response code 304 to notify ESP that no update is required.
5149

5250
```cpp
5351
t_httpUpdate_return ret = ESPhttpUpdate.update("192.168.0.2", 80, "/esp/update/arduino.php", "optional current version string here");
@@ -59,23 +57,23 @@ switch(ret) {
5957
Serial.println("[update] Update no Update.");
6058
break;
6159
case HTTP_UPDATE_OK:
62-
Serial.println("[update] Update ok."); // may not called we reboot the ESP
60+
Serial.println("[update] Update ok."); // may not called we reboot the ESP
6361
break;
6462
}
6563
```
6664

6765
#### Server request handling
6866

69-
##### simple updater
67+
##### Simple updater
7068

71-
for the simple Updater the Server only needs to deliver the binary file for update.
69+
For the simple updater the server only needs to deliver the binary file for update.
7270

73-
##### advanced updater
71+
##### Advanced updater
7472

75-
for advanced update management a Script needs to run at the Server side, for example a PHP script.
76-
at every Update request the the ESP sends some informations in the Header to the Server
73+
For advanced update management a script needs to run at the server side, for example a PHP script.
74+
At every update request the the ESP sends some information in HTTP headers to the server.
7775

78-
example Header data:
76+
Example header data:
7977
```
8078
[HTTP_USER_AGENT] => ESP8266-http-Update
8179
[HTTP_X_ESP8266_STA_MAC] => 18:FE:AA:AA:AA:AA
@@ -87,10 +85,9 @@ example Header data:
8785
[HTTP_X_ESP8266_VERSION] => DOOR-7-g14f53a19
8886
```
8987

90-
with this information the script now can check if a update is needed.
91-
It is also possible to deliver different binary´s based on the MAC address for example.
88+
With this information the script now can check if a update is needed. It is also possible to deliver different binaries based on the MAC address for example.
9289

93-
script example:
90+
Script example:
9491
```php
9592
<?PHP
9693

@@ -118,10 +115,10 @@ if(!check_header('HTTP_USER_AGENT', 'ESP8266-http-Update')) {
118115
header($_SERVER["SERVER_PROTOCOL"].' 403 Forbidden', true, 403);
119116
echo "only for ESP8266 updater!\n";
120117
exit();
121-
}
118+
}
122119

123120
if(
124-
!check_header('HTTP_X_ESP8266_STA_MAC') ||
121+
!check_header('HTTP_X_ESP8266_STA_MAC') ||
125122
!check_header('HTTP_X_ESP8266_AP_MAC') ||
126123
!check_header('HTTP_X_ESP8266_FREE_SPACE') ||
127124
!check_header('HTTP_X_ESP8266_SKETCH_SIZE') ||
@@ -145,18 +142,14 @@ if(isset($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']])) {
145142
} else {
146143
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
147144
}
145+
exit();
148146
}
149147

150148
header($_SERVER["SERVER_PROTOCOL"].' 500 no version for ESP MAC', true, 500);
151149

152150
```
153151

154152

155-
## Stream Interface
156-
157-
TODO describe Stream Interface update proccess
153+
## Updater class
158154

159-
```cpp
160-
ESP.updateSketch(client, length);
161-
```
162-
155+
TODO describe Updater class

Diff for: libraries/ESP8266WiFi/src/ESP8266WiFi.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,6 @@ void ESP8266WiFiClass::_eventCallback(void* arg)
831831

832832
if (event->event == EVENT_STAMODE_DISCONNECTED) {
833833
WiFiClient::stopAll();
834-
WiFiUDP::stopAll();
835834
}
836835
}
837836

Diff for: libraries/ESP8266WiFi/src/WiFiClientSecure.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ int WiFiClientSecure::available() {
267267
}
268268

269269
uint8_t WiFiClientSecure::connected() {
270+
if (!_client)
271+
return 0;
272+
270273
if (_client->state() == ESTABLISHED)
271274
return 1;
272275

Diff for: tests/Time/Time.ino

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <ESP8266WiFi.h>
2+
#include <time.h>
3+
4+
const char* ssid = "..........";
5+
const char* password = "..........";
6+
7+
int timezone = 3;
8+
int dst = 0;
9+
10+
void setup() {
11+
Serial.begin(115200);
12+
Serial.setDebugOutput(true);
13+
14+
WiFi.mode(WIFI_STA);
15+
WiFi.begin(ssid, password);
16+
Serial.println("\nConnecting to WiFi");
17+
while (WiFi.status() != WL_CONNECTED) {
18+
Serial.print(".");
19+
delay(1000);
20+
}
21+
22+
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
23+
Serial.println("\nWaiting for time");
24+
while (!time(nullptr)) {
25+
Serial.print(".");
26+
delay(1000);
27+
}
28+
Serial.println("");
29+
}
30+
31+
void loop() {
32+
time_t now = time(nullptr);
33+
Serial.println(ctime(&now));
34+
delay(1000);
35+
}

0 commit comments

Comments
 (0)