Skip to content

Commit 04c5d37

Browse files
authored
Merge pull request #41 from devicehive/develop
v0.8
2 parents fa0a124 + 167c5bd commit 04c5d37

23 files changed

+349
-198
lines changed

DeviceHiveESP8266.md

+21-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Table of contents
66
* [Overview](#overview)
77
* [Getting started](#getting-started)
8+
* [SSL support](#ssl-support)
89
* [Local services](#local-services)
910
* [mDNS](#mdns)
1011
* [RESTful API](#restful-api)
@@ -151,6 +152,18 @@ After rebooting you can send commands to DeviceHive server or local RESTful API
151152

152153
Now you can start writing your own program to create your own IoT devices with your favorite language and frameworks using DeviceHive [RESTful API](http://devicehive.com/restful) which you can transmitted with HTTP(S) or Websockets. List of accepted command for ESP8266 is listed in this document.
153154

155+
# SSL support
156+
Firmware supports encrypted WebSocket server connectity using Transport Layer Security (TLS). Server should support TLSv1.1 and TLS_RSA_WITH_AES_128_CBC_SHA or TLS_RSA_WITH_AES_256_CBC_SHA cipher.
157+
158+
Please note, chip has 2 KB buffer for secure data, so ssl handshake should not be more otherwise connection reset would occur. To check size of the handshake, run this command:
159+
```
160+
openssl s_client -connect devicehive.com:443 -tls1
161+
```
162+
and check in the output line with handshake size:
163+
```
164+
SSL handshake has read 4796 bytes and written 336 bytes
165+
```
166+
154167
# Local services
155168
Firmware sets chip hostname and announce chip with mDNS using configured DeviceId. Hostname is limited with 32 chars, further DeiviceId's chars are omitted.
156169

@@ -231,7 +244,9 @@ This is auxiliary command that is used to get a list of supported commands. This
231244
and output looks like:
232245

233246
```json
234-
[
247+
{
248+
"commands":
249+
[
235250
"gpio/write",
236251
"gpio/read",
237252
"gpio/int",
@@ -243,7 +258,8 @@ and output looks like:
243258
"uart/int",
244259
"uart/terminal",
245260
...
246-
]
261+
]
262+
}
247263
```
248264

249265
The `command/list` command is used on the `tryapi.html` page to provide command suggestion.
@@ -355,7 +371,6 @@ JSON with a set of key-value pairs, where key is pin number and value is one of
355371
*Example*:
356372
```json
357373
{
358-
"all":"read",
359374
"0":"read"
360375
}
361376
```
@@ -376,8 +391,7 @@ Json with set of key-value, where key is ADC channel and value is period in mill
376391
*Example*:
377392
```json
378393
{
379-
"0":"1000",
380-
"0":"disable"
394+
"0":"1000"
381395
}
382396
```
383397

@@ -529,7 +543,7 @@ Return "OK" in status and json like below in result on success. Or "Error" and d
529543
Write data to I2C bus.
530544

531545
*Parameters*:
532-
* "address" - I2C slave device address, decimal integer value.
546+
* "address" - I2C slave device address, hex value. Can start with "0x".
533547
* "data" - base64 encoded data that should be sent. Maximum size of data is 264 bytes.
534548
* "SDA" - GPIO port number for SDA data line. If not specified, previous pin will be used. Default is "0".
535549
* "SCL" - GPIO port number for SCL data line. If not specified, previous pin will be used. Default is "2".
@@ -539,7 +553,7 @@ Write data to I2C bus.
539553
{
540554
"SDA":"4",
541555
"SCL":"5",
542-
"address":"122",
556+
"address":"78",
543557
"data":"YWI="
544558
}
545559
```

esp-utils/Makefile

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CXX ?= g++
22
CFLAGS += -c -Wall
3-
LDFLAGS += -s -Os
3+
LDFLAGS += -Os
44
LIBS =
55
OBJDIR = build
66
SOURCES = $(wildcard common/*.cpp)
@@ -10,9 +10,12 @@ ALLOBJECTS = $(COMMONOBJECTS) $(addprefix $(OBJDIR)/, $(MAINS:%.cpp=%.o))
1010
ESPTERMINAL = $(OBJDIR)/esp-terminal
1111
ESPFLASHER = $(OBJDIR)/esp-flasher
1212
ifeq ($(OS),Windows_NT)
13-
LDFLAGS +=-static
13+
LDFLAGS += -static -s
1414
else
15-
LIBS = -pthread
15+
ifneq ($(shell uname -s),Darwin)
16+
LDFLAGS += -s
17+
LIBS = -pthread
18+
endif
1619
endif
1720

1821
all: $(SOURCES) $(ESPTERMINAL) $(ESPFLASHER)

esp-utils/common/serialport_posix.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ SerialPort *SerialPort::open(const char *port) {
7676
cfsetispeed(&tio,B115200);
7777

7878
tcflush(comp, TCIOFLUSH);
79-
80-
if( tcsetattr ( comp, TCSANOW, &tio ) != 0) {
79+
if( tcsetattr(comp, TCSANOW, &tio) != 0) {
8180
close(comp);
8281
return 0;
8382
}
83+
tcflush(comp, TCIOFLUSH);
8484

8585
SerialPort *comport = new SerialPort(comp, port);
8686
pthread_t thr;

esp-utils/esp-flasher.cpp

+22-9
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919

2020
#define ESP_BLOCK_SIZE 0x400
2121
#define ESP_SECTOR_SIZE 0x1000
22-
#define AUTODETECT_TIMEOUT 1000
22+
#define AUTODETECT_TIMEOUT 500
2323
#define FLASHING_TIMEOUT 5000
2424
#define AUTODETECT_MAX_PORT 20
2525
#define AUTODETECT_MAX_SYNC_ATTEMPS 3
26+
#define MAX_SYNC_ATTEMPS 3
2627
#define CHECK_BOOT_MODE_NOTIFY "Make sure that GPIO0 and GPIO15 are connected to low-level(ground),\r\n" \
2728
"GPIO2 and CH_PD are connected to high-level(power source)\r\n" \
2829
"And reboot device(reconnect power or connect RST pin to ground for a second).\r\n"
@@ -370,12 +371,14 @@ bool flash_sync(SerialPort *port, bool printErrors) {
370371
rh.command = 0x08; // sync
371372
rh.size = htole16(sizeof(body));
372373
rh.cs = esp_checksum((void*)body, sizeof(body));
373-
if(!flash_send(port, &rh, (void*)body, printErrors)) {
374-
if(printErrors)
375-
printf("\r\nFailed to sync device\r\n");
376-
return false;
374+
for(int i=0; i < MAX_SYNC_ATTEMPS; i++) {
375+
if(flash_send(port, &rh, (void*)body, false)) {
376+
return true;
377+
}
377378
}
378-
return true;
379+
if(printErrors)
380+
printf("\r\nFailed to sync device\r\n");
381+
return false;
379382
}
380383

381384
bool startWith(const char *str, const char *start) {
@@ -407,6 +410,7 @@ void force_flash_mode(SerialPort *port) {
407410
port->setRts(false);
408411
port->sleep(50);
409412
port->setDtr(false);
413+
port->sleep(100); //wait till chip boots
410414
}
411415

412416
void reboot(SerialPort *port) {
@@ -444,12 +448,22 @@ int main(int argc, char* argv[]) {
444448
}
445449
}
446450
if(port) {
447-
if(!flash_sync(port, true)) {
451+
TIMEOUT = AUTODETECT_TIMEOUT;
452+
bool synced = false;
453+
for (int i=0; i < AUTODETECT_MAX_SYNC_ATTEMPS; i++) {
454+
force_flash_mode(port);
455+
if(flash_sync(port, false)) {
456+
synced = true;
457+
break;
458+
}
459+
}
460+
if (!synced) {
448461
delete port;
449462
printf( "Device is not connected to UART or not in boot mode.\r\n" \
450463
CHECK_BOOT_MODE_NOTIFY"\r\n");
451464
return exit();
452465
}
466+
TIMEOUT = FLASHING_TIMEOUT;
453467
} else {
454468
TIMEOUT = AUTODETECT_TIMEOUT;
455469
printf("Detecting device...\r\n");
@@ -468,12 +482,11 @@ int main(int argc, char* argv[]) {
468482
}
469483
if (ports[i]) {
470484
mCurrentPort = ports[i];
485+
force_flash_mode(ports[i]);
471486
if (flash_sync(ports[i], false)) {
472487
printf("Device found on %s and successfully synced\r\n", ports[i]->getName());
473488
port = ports[i];
474489
}
475-
if(j == 0 && port == NULL)
476-
force_flash_mode(ports[i]);
477490
mCurrentPort = NULL;
478491
}
479492
}

firmware-src/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ TARGETAR = $(OBJDIR)/devicehive.a
1313
TARGETELF = $(OBJDIR)/devicehive.elf
1414
INCLUDEDIRS = $(addprefix -I,$(SDKPATH)/include $(CURDIR)/sources)
1515
LIBDIR = $(addprefix -L,$(SDKPATH)/lib)
16-
LIBS = $(addprefix -l,c gcc phy pp net80211 lwip wpa main json crypto)
16+
LIBS = $(addprefix -l,c gcc phy pp net80211 lwip wpa main json crypto ssl)
1717
SOURCES = $(wildcard sources/*.c) $(wildcard drivers/*.c) $(wildcard sources/devices/*.c) $(wildcard sources/DH/*.c) $(wildcard sources/commands/*.c)
1818
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:%.c=%.o))
1919
GITVER = $(shell git rev-parse HEAD 2> /dev/null || echo \"not a git repo\")
@@ -24,6 +24,7 @@ AR = $(CROSS_COMPILE)ar
2424
SIZE = $(CROSS_COMPILE)size
2525
PAGESH = pages/pages.h
2626

27+
2728
.PHONY: all flash full_flash terminal clean disassemble reboot
2829

2930
all: $(FIRMWARE)

firmware-src/pages/gen_pages.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ for file in $FILE_LIST; do
2626
filename=$(basename "$file")
2727
echo "Parsing $filename ..."
2828
name=${filename/./_}
29-
data=$(od -An -v -t x1 $file|tr -d '\n' | sed -E "s/( +)([[:xdigit:]]{2})/\\\x\2/g")
29+
data=$(gzip -c $file | od -An -v -t x1 | tr -d '\n' | sed -E "s/( +)([[:xdigit:]]{2})/\\\x\2/g")
3030
print "RO_DATA char $name[] = \"$data\";"
3131
index="$index$comma {\"$filename\", $name, sizeof($name) - 1}"
3232
comma=", "

firmware-src/sources/DH/onewire.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ unsigned int ICACHE_FLASH_ATTR dh_onewire_get_pin(void)
7575
* @brief Reset onewire bus.
7676
* @return Non-zero if device is presented. Zero otherwise.
7777
*/
78-
int ICACHE_FLASH_ATTR dh_onewire_reset(DHGpioPinMask pin_mask, int exit_on_presence)
78+
int ICACHE_FLASH_ATTR dh_onewire_reset(DHGpioPinMask pin_mask, int reset_time_us, int exit_on_presence)
7979
{
8080
system_soft_wdt_feed();
8181
const int pinstate = (gpio_input_get() & pin_mask) !=0;
@@ -91,7 +91,7 @@ int ICACHE_FLASH_ATTR dh_onewire_reset(DHGpioPinMask pin_mask, int exit_on_prese
9191

9292
// send RESET
9393
gpio_output_set(0, pin_mask, pin_mask, 0);
94-
os_delay_us(ONEWIRE_RESET_LENGHT_US);
94+
os_delay_us(reset_time_us);
9595
gpio_output_set(pin_mask, 0, pin_mask, 0);
9696

9797
// check RESPONSE
@@ -169,7 +169,7 @@ int ICACHE_FLASH_ATTR dh_onewire_write(const void *buf_, size_t len)
169169
lock_int();
170170

171171
const DHGpioPinMask pin_mask = DH_GPIO_PIN(mOneWirePin);
172-
int present = dh_onewire_reset(pin_mask, 0);
172+
int present = dh_onewire_reset(pin_mask, ONEWIRE_RESET_LENGHT_US, 0);
173173
if (!present) {
174174
unlock_int();
175175
return -1; // failed
@@ -241,7 +241,7 @@ int ICACHE_FLASH_ATTR dh_onewire_search(void *buf_, size_t *len, int command, un
241241
lock_int();
242242

243243
do {
244-
if (!dh_onewire_reset(pin_mask, 0)) {
244+
if (!dh_onewire_reset(pin_mask, ONEWIRE_RESET_LENGHT_US, 0)) {
245245
unlock_int();
246246
if (lastAmbiguity == 8*sizeof(address)) {
247247
*len = 0; // devices are not connected

firmware-src/sources/DH/onewire.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ unsigned int dh_onewire_get_pin(void);
3636
* @brief Reset onewire bus.
3737
* @param[in] pin_mask Pin mask to reset.
3838
* @param[in] exit_on_presence Flag to exit immediately if device found.
39+
* @param[in] reset_time_us reset signal length.
3940
* @return Non-zero if device is presented. Zero otherwise.
4041
*/
41-
int dh_onewire_reset(DHGpioPinMask pin_mask, int exit_on_presence);
42+
int dh_onewire_reset(DHGpioPinMask pin_mask, int reset_time_us, int exit_on_presence);
4243

4344

4445
/**

firmware-src/sources/base64.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,25 @@ static inline int ICACHE_FLASH_ATTR base64_table(uint32_t reg, int offset)
4444

4545

4646
/*
47-
* base64_encode_length() implementation.
47+
* esp_base64_encode_length() implementation.
4848
*/
49-
size_t ICACHE_FLASH_ATTR base64_encode_length(size_t data_len)
49+
size_t ICACHE_FLASH_ATTR esp_base64_encode_length(size_t data_len)
5050
{
5151
return (data_len + 2)/3 * 4; // round_up(len/3)*4
5252
}
5353

5454

5555
/*
56-
* base64_encode() implementation.
56+
* esp_base64_encode() implementation.
5757
*/
58-
int ICACHE_FLASH_ATTR base64_encode(const void *data_, size_t data_len,
59-
char *text, size_t text_len)
58+
int ICACHE_FLASH_ATTR esp_base64_encode(const void *data_, size_t data_len,
59+
char *text, size_t text_len)
6060
{
6161
if (!data_len)
6262
return 0; // nothing to encode
6363

6464
// check we have enough space for output
65-
if (base64_encode_length(data_len) > text_len)
65+
if (esp_base64_encode_length(data_len) > text_len)
6666
return 0;
6767

6868
const uint8_t *data = (const uint8_t*)data_;
@@ -97,9 +97,9 @@ int ICACHE_FLASH_ATTR base64_encode(const void *data_, size_t data_len,
9797

9898

9999
/*
100-
* base64_decode_length() implementation.
100+
* esp_base64_decode_length() implementation.
101101
*/
102-
size_t ICACHE_FLASH_ATTR base64_decode_length(const char *text, size_t text_len)
102+
size_t ICACHE_FLASH_ATTR esp_base64_decode_length(const char *text, size_t text_len)
103103
{
104104
if (!text_len)
105105
return 0; // nothing to decode
@@ -117,16 +117,16 @@ size_t ICACHE_FLASH_ATTR base64_decode_length(const char *text, size_t text_len)
117117

118118

119119
/*
120-
* base64_decode() implementation.
120+
* esp_base64_decode() implementation.
121121
*/
122-
int ICACHE_FLASH_ATTR base64_decode(const char *text, size_t text_len,
123-
void *data_base, size_t data_len)
122+
int ICACHE_FLASH_ATTR esp_base64_decode(const char *text, size_t text_len,
123+
void *data_base, size_t data_len)
124124
{
125125
if (!text_len || text_len%4)
126126
return 0; // nothing to decode
127127

128128
// check we have enough space for output
129-
if (base64_decode_length(text, text_len) > data_len)
129+
if (esp_base64_decode_length(text, text_len) > data_len)
130130
return 0;
131131

132132
uint8_t *data = (uint8_t*)data_base;

firmware-src/sources/base64.h

+10-10
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@
2222
* @param[in] text_len Output buffer length in bytes.
2323
* @return Number of bytes written to output buffer.
2424
*/
25-
int base64_encode(const void *data,
26-
size_t data_len,
27-
char *text,
28-
size_t text_len);
25+
int esp_base64_encode(const void *data,
26+
size_t data_len,
27+
char *text,
28+
size_t text_len);
2929

3030

3131
/**
3232
* @brief Estimate how many bytes will take `Base64` encoded data.
3333
* @param[in] data_len Binary data length in bytes.
3434
* @return `Base64` encoded text length in bytes.
3535
*/
36-
size_t base64_encode_length(size_t data_len);
36+
size_t esp_base64_encode_length(size_t data_len);
3737

3838

3939
/**
@@ -47,10 +47,10 @@ size_t base64_encode_length(size_t data_len);
4747
* @param[in] data_len Output buffer length in bytes.
4848
* @return Number of bytes written to output buffer.
4949
*/
50-
int base64_decode(const char *text,
51-
size_t text_len,
52-
void *data,
53-
size_t data_len);
50+
int esp_base64_decode(const char *text,
51+
size_t text_len,
52+
void *data,
53+
size_t data_len);
5454

5555

5656
/**
@@ -62,6 +62,6 @@ int base64_decode(const char *text,
6262
* @param[in] text_len Encoded text length in bytes.
6363
* @return Decoded binary data length in bytes.
6464
*/
65-
size_t base64_decode_length(const char *text, size_t text_len);
65+
size_t esp_base64_decode_length(const char *text, size_t text_len);
6666

6767
#endif /* _DATAENCODEBASE64_H_ */

0 commit comments

Comments
 (0)