Skip to content

Commit 43a90bc

Browse files
committed
Merge pull request esp8266#8 from slaff/feature/lwipr-compat
Restructured the lwip raw compatability code.
2 parents 66e1a5f + 7c38865 commit 43a90bc

File tree

5 files changed

+237
-9
lines changed

5 files changed

+237
-9
lines changed

util/README.md renamed to compat/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ If you are using [LWIP raw tcp mode](http://lwip.wikia.com/wiki/Raw/TCP) and wan
33
First you have to include the `lwipr_compat.h` header.
44

55
```C
6-
#include "util/lwipr_compat.h"
6+
#include "compat/lwipr_compat.h"
77
```
88

99
Then in the code block where you initialize the tcp raw connection you should call `axl_init`.

util/lwipr_compat.c renamed to compat/lwipr_compat.c

+43-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
#include "lwipr_compat.h"
88

9+
AxlTcpDataArray axlFdArray;
10+
911
#include <stdlib.h>
1012

1113
/* High Level "public" functions */
@@ -24,6 +26,34 @@ int axl_append(struct tcp_pcb *tcp) {
2426
return ax_fd_append(&axlFdArray, tcp);
2527
}
2628

29+
/**
30+
* Frees the internal mapping from this tcp. Returns the number of occurrences of the tcp
31+
*/
32+
int axl_free(struct tcp_pcb *tcp) {
33+
int i;
34+
int occurances = 0;
35+
36+
if(tcp == NULL) {
37+
return 0;
38+
}
39+
40+
AxlTcpDataArray *vector = &axlFdArray;
41+
AXL_DEBUG("AXL: Freeing %d tcp item", vector->size);
42+
for (i = 0; i < vector->size; i++) {
43+
if (vector->data[i].tcp == tcp) {
44+
if(vector->data[i].tcp_pbuf != NULL) {
45+
pbuf_free(vector->data[i].tcp_pbuf);
46+
vector->data[i].tcp_pbuf = NULL;
47+
}
48+
vector->data[i].tcp = NULL;
49+
vector->data[i].pbuf_offset = 0;
50+
occurances++;
51+
}
52+
}
53+
54+
return occurances;
55+
}
56+
2757
/**
2858
* Reads data from the SSL over TCP stream. Returns decrypted data.
2959
* @param SSL *sslObj
@@ -63,9 +93,13 @@ int axl_ssl_read(SSL *ssl, uint8_t **in_data, struct tcp_pcb *tcp, struct pbuf *
6393
data->pbuf_offset = 0;
6494
}
6595

96+
AXL_DEBUG("READY TO READ SOME DATA\n");
97+
6698
tcp_recved(tcp, p->tot_len);
6799
do {
100+
WATCHDOG_FEED();
68101
read_bytes = ssl_read(ssl, in_data);
102+
AXL_DEBUG("axl_ssl_read: Read bytes: %d\n", read_bytes);
69103
if(read_bytes < SSL_OK) {
70104
/* An error has occurred. Give it back for further processing */
71105
total_bytes = read_bytes;
@@ -97,6 +131,7 @@ int ax_port_write(int clientfd, uint8_t *buf, uint16_t bytes_needed) {
97131
}
98132

99133
if (data == NULL || data->tcp == NULL || buf == NULL || bytes_needed == 0) {
134+
AXL_DEBUG("Return Zero.\n");
100135
return 0;
101136
}
102137

@@ -116,16 +151,17 @@ int ax_port_write(int clientfd, uint8_t *buf, uint16_t bytes_needed) {
116151
}
117152

118153
do {
119-
err = tcp_write(data->tcp, buf, tcp_len, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
154+
err = tcp_write(data->tcp, buf, tcp_len, TCP_WRITE_FLAG_COPY);
120155
if(err < SSL_OK) {
121156
AXL_DEBUG("Got error: %d\n", err);
122157
}
123158

124159
if (err == ERR_MEM) {
160+
AXL_DEBUG("Not enough memory to write data with length: %d (%d)\n", tcp_len, bytes_needed);
125161
tcp_len /= 2;
126162
}
127163
} while (err == ERR_MEM && tcp_len > 1);
128-
AXL_DEBUG("send_raw_packet length %d\n", tcp_len);
164+
AXL_DEBUG("send_raw_packet length %d(%d)\n", tcp_len, bytes_needed);
129165
if (err == ERR_OK) {
130166
err = tcp_output(data->tcp);
131167
if(err != ERR_OK) {
@@ -176,6 +212,11 @@ int ax_port_read(int clientfd, uint8_t *buf, int bytes_needed) {
176212
return recv_len;
177213
}
178214

215+
int ax_get_file(const char *filename, uint8_t **buf) {
216+
*buf = 0;
217+
return 0;
218+
}
219+
179220
/*
180221
* Utility functions
181222
*/

util/lwipr_compat.h renamed to compat/lwipr_compat.h

+19-6
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
#ifndef LWIPR_COMPAT_H
99
#define LWIPR_COMPAT_H
1010

11-
#include <stdint.h>
12-
1311
/*
1412
* All those functions will run only if LWIP tcp raw mode is used
1513
*/
1614
#if LWIP_RAW==1
1715

18-
#include "lwip/tcp.h"
16+
#ifdef __cplusplus
17+
extern "C" {
18+
#endif
19+
20+
#include "lwipr_platform.h"
1921
#include "ssl/ssl.h"
2022
#include "ssl/tls1.h"
2123

@@ -31,7 +33,14 @@
3133
* Define the AXL_DEBUG function to add debug functionality
3234
*/
3335
#ifndef AXL_DEBUG
34-
#define AXL_DEBUG printf
36+
#define AXL_DEBUG(...)
37+
#endif
38+
39+
/**
40+
* Define watchdog function to be called during CPU intensive operations.
41+
*/
42+
#ifndef WATCHDOG_FEED
43+
#define WATCHDOG_FEED()
3544
#endif
3645

3746
typedef struct {
@@ -47,14 +56,13 @@ typedef struct {
4756
AxlTcpData *data; /* array of TcpData objects */
4857
} AxlTcpDataArray;
4958

50-
AxlTcpDataArray axlFdArray;
51-
5259
/*
5360
* High Level Functions - these are the ones that should be used directly
5461
*/
5562

5663
void axl_init(int capacity);
5764
int axl_append(struct tcp_pcb *tcp);
65+
int axl_free(struct tcp_pcb *tcp);
5866

5967
#define axl_ssl_write(A, B, C) ssl_write(A, B, C)
6068
int axl_ssl_read(SSL *sslObj, uint8_t **in_data, struct tcp_pcb *tcp, struct pbuf *p);
@@ -77,6 +85,11 @@ void ax_fd_set(AxlTcpDataArray *vector, int index, struct tcp_pcb *tcp);
7785
void ax_fd_double_capacity_if_full(AxlTcpDataArray *vector);
7886
void ax_fd_free(AxlTcpDataArray *vector);
7987

88+
89+
#ifdef __cplusplus
90+
}
8091
#endif
8192

93+
#endif /* LWIP_RAW==1 */
94+
8295
#endif /* LWIPR_COMPAT_H */

compat/lwipr_platform.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* lwipr_platform.h
3+
*
4+
* Created on: Feb 8, 2016
5+
* Author: slavey
6+
*
7+
*/
8+
9+
#ifndef AXTLS_8266_COMPAT_LWIPR_PLATFORM_H_
10+
#define AXTLS_8266_COMPAT_LWIPR_PLATFORM_H_
11+
12+
/* Add here all platform specific things */
13+
14+
15+
// Some calls require the watchdog to be reset
16+
#ifndef WATCHDOG_FEED
17+
#define WATCHDOG_FEED()
18+
#endif
19+
20+
21+
/* SSL_DEBUG is for more information */
22+
#ifndef SSL_DEBUG
23+
#define AXL_DEBUG(...)
24+
#endif
25+
26+
27+
#endif /* AXTLS_8266_COMPAT_LWIPR_PLATFORM_H_ */

replacements/time.c

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
extern uint32_t system_get_time(void);
23+
extern uint64_t system_mktime(uint32_t year, uint32_t mon, uint32_t day, uint32_t hour, uint32_t min, uint32_t sec);
24+
25+
static int errno_var = 0;
26+
27+
int* __errno(void) {
28+
// DEBUGV("__errno is called last error: %d (not current)\n", errno_var);
29+
return &errno_var;
30+
}
31+
32+
unsigned long millis(void)
33+
{
34+
return system_get_time() / 1000UL;
35+
}
36+
37+
unsigned long micros(void)
38+
{
39+
return system_get_time();
40+
}
41+
42+
#ifndef _TIMEVAL_DEFINED
43+
#define _TIMEVAL_DEFINED
44+
struct timeval {
45+
time_t tv_sec;
46+
suseconds_t tv_usec;
47+
};
48+
#endif
49+
50+
extern char* sntp_asctime(const struct tm *t);
51+
extern struct tm* sntp_localtime(const time_t *clock);
52+
53+
// time gap in seconds from 01.01.1900 (NTP time) to 01.01.1970 (UNIX time)
54+
#define DIFF1900TO1970 2208988800UL
55+
56+
static int s_daylightOffset_sec = 0;
57+
static long s_timezone_sec = 0;
58+
static time_t s_bootTime = 0;
59+
60+
// calculate offset used in gettimeofday
61+
static void ensureBootTimeIsSet()
62+
{
63+
if (!s_bootTime)
64+
{
65+
time_t now = sntp_get_current_timestamp();
66+
if (now)
67+
{
68+
s_bootTime = now - millis() / 1000;
69+
}
70+
}
71+
}
72+
73+
static void setServer(int id, const char* name_or_ip)
74+
{
75+
if (name_or_ip)
76+
{
77+
//TODO: check whether server is given by name or IP
78+
sntp_setservername(id, (char*) name_or_ip);
79+
}
80+
}
81+
82+
void configTime(int timezone, int daylightOffset_sec, const char* server1, const char* server2, const char* server3)
83+
{
84+
sntp_stop();
85+
86+
setServer(0, server1);
87+
setServer(1, server2);
88+
setServer(2, server3);
89+
90+
s_timezone_sec = timezone;
91+
s_daylightOffset_sec = daylightOffset_sec;
92+
sntp_set_timezone(timezone/3600);
93+
sntp_init();
94+
}
95+
96+
int clock_gettime(clockid_t unused, struct timespec *tp)
97+
{
98+
tp->tv_sec = millis() / 1000;
99+
tp->tv_nsec = micros() * 1000;
100+
return 0;
101+
}
102+
103+
// seconds since 1970
104+
time_t mktime(struct tm *t)
105+
{
106+
// system_mktime expects month in range 1..12
107+
#define START_MONTH 1
108+
return DIFF1900TO1970 + system_mktime(t->tm_year, t->tm_mon + START_MONTH, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
109+
}
110+
111+
time_t time(time_t * t)
112+
{
113+
time_t seconds = sntp_get_current_timestamp();
114+
if (t)
115+
{
116+
*t = seconds;
117+
}
118+
return seconds;
119+
}
120+
121+
char* asctime(const struct tm *t)
122+
{
123+
return sntp_asctime(t);
124+
}
125+
126+
struct tm* localtime(const time_t *clock)
127+
{
128+
return sntp_localtime(clock);
129+
}
130+
131+
char* ctime(const time_t *t)
132+
{
133+
struct tm* p_tm = localtime(t);
134+
char* result = asctime(p_tm);
135+
return result;
136+
}
137+
138+
int gettimeofday(struct timeval *tp, void *tzp)
139+
{
140+
if (tp)
141+
{
142+
ensureBootTimeIsSet();
143+
tp->tv_sec = (s_bootTime + millis()) / 1000;
144+
tp->tv_usec = micros() * 1000;
145+
}
146+
return 0;
147+
}

0 commit comments

Comments
 (0)