Skip to content

Commit

Permalink
Merge pull request esp8266#2 from esp8266/master
Browse files Browse the repository at this point in the history
pull master
  • Loading branch information
me-no-dev committed Nov 8, 2015
2 parents a8976a0 + 810ab68 commit 14bb946
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 60 deletions.
4 changes: 3 additions & 1 deletion cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void ets_intr_unlock();
// level (0-15), interrupts of the given level and above will be active
// level 15 will disable ALL interrupts,
// level 0 will enable ALL interrupts,
//
//
#define xt_rsil(level) (__extension__({uint32_t state; __asm__ __volatile__("rsil %0," __STRINGIFY(level) : "=a" (state)); state;}))
#define xt_wsr_ps(state) __asm__ __volatile__("wsr %0,ps; isync" :: "a" (state) : "memory")

Expand Down Expand Up @@ -271,6 +271,8 @@ long random(long, long);
void randomSeed(unsigned long);
long map(long, long, long, long, long);

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

#endif

Expand Down
17 changes: 13 additions & 4 deletions cores/esp8266/core_esp8266_si2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,15 @@ static unsigned char twi_read_byte(bool nack) {
unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop){
unsigned int i;
if(!twi_write_start()) return 4;//line busy
if(!twi_write_byte(((address << 1) | 0) & 0xFF)) return 2;//received NACK on transmit of address
for(i=0; i<len; i++){
if(!twi_write_byte(buf[i])) return 3;//received NACK on transmit of data
if(!twi_write_byte(((address << 1) | 0) & 0xFF)) {
if (sendStop) twi_write_stop();
return 2; //received NACK on transmit of address
}
for(i=0; i<len; i++) {
if(!twi_write_byte(buf[i])) {
if (sendStop) twi_write_stop();
return 3;//received NACK on transmit of data
}
}
if(sendStop) twi_write_stop();
i = 0;
Expand All @@ -168,7 +174,10 @@ unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned i
unsigned char twi_readFrom(unsigned char address, unsigned char* buf, unsigned int len, unsigned char sendStop){
unsigned int i;
if(!twi_write_start()) return 4;//line busy
if(!twi_write_byte(((address << 1) | 1) & 0xFF)) return 2;//received NACK on transmit of address
if(!twi_write_byte(((address << 1) | 1) & 0xFF)) {
if (sendStop) twi_write_stop();
return 2;//received NACK on transmit of address
}
for(i=0; i<(len-1); i++) buf[i] = twi_read_byte(false);
buf[len-1] = twi_read_byte(true);
if(sendStop) twi_write_stop();
Expand Down
18 changes: 0 additions & 18 deletions cores/esp8266/libc_replacements.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,24 +393,6 @@ int* __errno(void) {
return &errno_var;
}


char * ctime(const time_t *clock) {
return 0;
}

time_t time(time_t * t) {
return 0;
}

int gettimeofday(void *tp, void *tzp) {
return 0;
}

time_t mktime(struct tm *timp) {
return 0;
}


/*
* begin newlib/string/strlcpy.c
*
Expand Down
128 changes: 128 additions & 0 deletions cores/esp8266/time.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* time.c - ESP8266-specific functions for SNTP
* Copyright (c) 2015 Peter Dobler. All rights reserved.
* This file is part of the esp8266 core for Arduino environment.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

#include <time.h>
#include "sntp.h"


#ifndef _TIMEVAL_DEFINED
#define _TIMEVAL_DEFINED
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
#endif

extern char* sntp_asctime(const struct tm *t);
extern struct tm* sntp_localtime(const time_t *clock);

// time gap in seconds from 01.01.1900 (NTP time) to 01.01.1970 (UNIX time)
#define DIFF1900TO1970 2208988800UL

static int s_daylightOffset_sec = 0;
static int s_timezone_sec = 0;
static time_t s_bootTime = 0;

// calculate offset used in gettimeofday
static void ensureBootTimeIsSet()
{
if (!s_bootTime)
{
time_t now = sntp_get_current_timestamp();
if (now)
{
s_bootTime = - millis() / 1000;
}
}
}

static void setServer(int id, const char* name_or_ip)
{
if (name_or_ip)
{
//TODO: check whether server is given by name or IP
sntp_setservername(0, (char*) name_or_ip);
}
}

void configTime(int timezone, int daylightOffset_sec, const char* server1, const char* server2, const char* server3)
{
sntp_stop();

setServer(0, server1);
setServer(1, server2);
setServer(2, server3);

s_timezone_sec = timezone;
s_daylightOffset_sec = daylightOffset_sec;
sntp_set_timezone(timezone/3600);
sntp_init();
}

int clock_gettime(clockid_t unused, struct timespec *tp)
{
tp->tv_sec = millis() / 1000;
tp->tv_nsec = micros() * 1000;
return 0;
}

// seconds since 1970
time_t mktime(struct tm *t)
{
// system_mktime expects month in range 1..12
#define START_MONTH 1
return DIFF1900TO1970 + system_mktime(t->tm_year, t->tm_mon + START_MONTH, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
}

time_t time(time_t * t)
{
time_t seconds = sntp_get_current_timestamp();
ensureBootTimeIsSet();
if (t)
{
*t = seconds;
}
return seconds;
}

char* asctime(const struct tm *t)
{
return sntp_asctime(t);
}

struct tm* localtime(const time_t *clock)
{
return sntp_localtime(clock);
}

char* ctime(const time_t *t)
{
struct tm* p_tm = localtime(t);
char* result = asctime(p_tm);
return result;
}

int gettimeofday(struct timeval *tp, void *tzp)
{
if (tp)
{
ensureBootTimeIsSet();
tp->tv_sec = (s_bootTime + millis()) / 1000;
tp->tv_usec = micros() * 1000;
}
}
65 changes: 29 additions & 36 deletions doc/ota_updates.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,45 @@ title: OTA Update
* [Arduino IDE](#arduino-ide)
* [HTTP Server](#http-server)
* [Stream Interface](#stream-interface)

## Basic Requirements

- Flash chip size is 2x the size of the sketch
- Flash chip size is 2x the size of the sketch.

## Arduino IDE

TODO describe Arduino IDE OTA process

#### Requirements
- The ESP and the Computer must be connected to the Same network.

- The ESP and the computer must be connected to the same network.

## HTTP Server

the ```ESPhttpUpdate``` class can check for updates and download a binary file form a HTTP web server.
It is possible to download updates from every IP or domain address on the Network or Internet.

```ESPhttpUpdate``` class can check for updates and download a binary file from HTTP web server.
It is possible to download updates from every IP or domain address on the network or Internet.

#### Requirements
- web server


#### Arduino code

##### simple updater
##### Simple updater

the Simple Updater downloads the File every time the function is called.
Simple updater downloads the file every time the function is called.

```cpp
ESPhttpUpdate.update("192.168.0.2", 80, "/arduino.bin");
```

##### advanced updater
##### Advanced updater

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

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

```cpp
t_httpUpdate_return ret = ESPhttpUpdate.update("192.168.0.2", 80, "/esp/update/arduino.php", "optional current version string here");
Expand All @@ -59,23 +57,23 @@ switch(ret) {
Serial.println("[update] Update no Update.");
break;
case HTTP_UPDATE_OK:
Serial.println("[update] Update ok."); // may not called we reboot the ESP
Serial.println("[update] Update ok."); // may not called we reboot the ESP
break;
}
```

#### Server request handling

##### simple updater
##### Simple updater

for the simple Updater the Server only needs to deliver the binary file for update.
For the simple updater the server only needs to deliver the binary file for update.

##### advanced updater
##### Advanced updater

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

example Header data:
Example header data:
```
[HTTP_USER_AGENT] => ESP8266-http-Update
[HTTP_X_ESP8266_STA_MAC] => 18:FE:AA:AA:AA:AA
Expand All @@ -87,10 +85,9 @@ example Header data:
[HTTP_X_ESP8266_VERSION] => DOOR-7-g14f53a19
```

with this information the script now can check if a update is needed.
It is also possible to deliver different binary´s based on the MAC address for example.
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.

script example:
Script example:
```php
<?PHP

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

if(
!check_header('HTTP_X_ESP8266_STA_MAC') ||
!check_header('HTTP_X_ESP8266_STA_MAC') ||
!check_header('HTTP_X_ESP8266_AP_MAC') ||
!check_header('HTTP_X_ESP8266_FREE_SPACE') ||
!check_header('HTTP_X_ESP8266_SKETCH_SIZE') ||
Expand All @@ -145,18 +142,14 @@ if(isset($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']])) {
} else {
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
}
exit();
}

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

```


## Stream Interface

TODO describe Stream Interface update proccess
## Updater class

```cpp
ESP.updateSketch(client, length);
```

TODO describe Updater class
1 change: 0 additions & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,6 @@ void ESP8266WiFiClass::_eventCallback(void* arg)

if (event->event == EVENT_STAMODE_DISCONNECTED) {
WiFiClient::stopAll();
WiFiUDP::stopAll();
}
}

Expand Down
3 changes: 3 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiClientSecure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ int WiFiClientSecure::available() {
}

uint8_t WiFiClientSecure::connected() {
if (!_client)
return 0;

if (_client->state() == ESTABLISHED)
return 1;

Expand Down
35 changes: 35 additions & 0 deletions tests/Time/Time.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <ESP8266WiFi.h>
#include <time.h>

const char* ssid = "..........";
const char* password = "..........";

int timezone = 3;
int dst = 0;

void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("\nConnecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}

configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
Serial.println("\nWaiting for time");
while (!time(nullptr)) {
Serial.print(".");
delay(1000);
}
Serial.println("");
}

void loop() {
time_t now = time(nullptr);
Serial.println(ctime(&now));
delay(1000);
}

0 comments on commit 14bb946

Please sign in to comment.