Berry added `tcpclient`

This commit is contained in:
Stephan Hadinger 2021-12-09 22:00:24 +01:00
parent c2d7fe27ed
commit 9faadaca67
6 changed files with 1113 additions and 906 deletions

View File

@ -10,6 +10,8 @@ All notable changes to this project will be documented in this file.
## [10.1.0] 20211208 ## [10.1.0] 20211208
- Release Noelle - Release Noelle
### Added
- Berry added ``tcpclient``
## [10.0.0.4] 20211208 ## [10.0.0.4] 20211208
### Added ### Added

View File

@ -0,0 +1,50 @@
/********************************************************************
* Webclient mapped to Arduino framework
*
* To use: `d = webclient()`
*
*******************************************************************/
#include "be_constobj.h"
#ifdef USE_WEBCLIENT
extern int wc_tcp_init(bvm *vm);
extern int wc_tcp_deinit(bvm *vm);
extern int wc_tcp_connect(bvm *vm);
extern int wc_tcp_connected(bvm *vm);
extern int wc_tcp_close(bvm *vm);
extern int wc_tcp_available(bvm *vm);
extern int wc_tcp_flush(bvm *vm);
extern int wc_tcp_write(bvm *vm);
extern int wc_tcp_read(bvm *vm);
extern int wc_tcp_readbytes(bvm *vm);
#include "../generate/be_fixed_be_class_tcpclient.h"
void be_load_webclient_lib(bvm *vm) {
be_pushntvclass(vm, &be_class_tcpclient);
be_setglobal(vm, "tcpclient");
be_pop(vm, 1);
}
/* @const_object_info_begin
class be_class_tcpclient (scope: global, name: tcpclient) {
.w, var
init, func(wc_tcp_init)
deinit, func(wc_tcp_deinit)
connect, func(wc_tcp_connect)
connected, func(wc_tcp_connected)
close, func(wc_tcp_close)
available, func(wc_tcp_available)
flush, func(wc_tcp_flush)
write, func(wc_tcp_write)
read, func(wc_tcp_read)
readbytes, func(wc_tcp_readbytes)
}
@const_object_info_end */
#endif // USE_WEBCLIENT

View File

@ -276,6 +276,8 @@ extern const bcstring be_const_str_color;
extern const bcstring be_const_str_compile; extern const bcstring be_const_str_compile;
extern const bcstring be_const_str_compress; extern const bcstring be_const_str_compress;
extern const bcstring be_const_str_concat; extern const bcstring be_const_str_concat;
extern const bcstring be_const_str_connect;
extern const bcstring be_const_str_connected;
extern const bcstring be_const_str_connection_error; extern const bcstring be_const_str_connection_error;
extern const bcstring be_const_str_constructor_cb; extern const bcstring be_const_str_constructor_cb;
extern const bcstring be_const_str_contains; extern const bcstring be_const_str_contains;
@ -549,6 +551,7 @@ extern const bcstring be_const_str_read32;
extern const bcstring be_const_str_read8; extern const bcstring be_const_str_read8;
extern const bcstring be_const_str_read_bytes; extern const bcstring be_const_str_read_bytes;
extern const bcstring be_const_str_read_sensors; extern const bcstring be_const_str_read_sensors;
extern const bcstring be_const_str_readbytes;
extern const bcstring be_const_str_readline; extern const bcstring be_const_str_readline;
extern const bcstring be_const_str_real; extern const bcstring be_const_str_real;
extern const bcstring be_const_str_reapply; extern const bcstring be_const_str_reapply;
@ -656,6 +659,7 @@ extern const bcstring be_const_str_target_search;
extern const bcstring be_const_str_tasmota; extern const bcstring be_const_str_tasmota;
extern const bcstring be_const_str_tasmota_X2Eget_light_X28_X29_X20is_X20deprecated_X2C_X20use_X20light_X2Eget_X28_X29; extern const bcstring be_const_str_tasmota_X2Eget_light_X28_X29_X20is_X20deprecated_X2C_X20use_X20light_X2Eget_X28_X29;
extern const bcstring be_const_str_tasmota_X2Eset_light_X28_X29_X20is_X20deprecated_X2C_X20use_X20light_X2Eset_X28_X29; extern const bcstring be_const_str_tasmota_X2Eset_light_X28_X29_X20is_X20deprecated_X2C_X20use_X20light_X2Eset_X28_X29;
extern const bcstring be_const_str_tcpclient;
extern const bcstring be_const_str_tele; extern const bcstring be_const_str_tele;
extern const bcstring be_const_str_the_X20second_X20argument_X20is_X20not_X20a_X20function; extern const bcstring be_const_str_the_X20second_X20argument_X20is_X20not_X20a_X20function;
extern const bcstring be_const_str_time_dump; extern const bcstring be_const_str_time_dump;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,27 @@
#include "be_constobj.h"
static be_define_const_map_slots(be_class_tcpclient_map) {
{ be_const_key(init, -1), be_const_func(wc_tcp_init) },
{ be_const_key(close, -1), be_const_func(wc_tcp_close) },
{ be_const_key(_X2Ew, 4), be_const_var(0) },
{ be_const_key(flush, -1), be_const_func(wc_tcp_flush) },
{ be_const_key(deinit, 6), be_const_func(wc_tcp_deinit) },
{ be_const_key(read, -1), be_const_func(wc_tcp_read) },
{ be_const_key(write, 8), be_const_func(wc_tcp_write) },
{ be_const_key(connected, -1), be_const_func(wc_tcp_connected) },
{ be_const_key(available, -1), be_const_func(wc_tcp_available) },
{ be_const_key(connect, 0), be_const_func(wc_tcp_connect) },
{ be_const_key(readbytes, -1), be_const_func(wc_tcp_readbytes) },
};
static be_define_const_map(
be_class_tcpclient_map,
11
);
BE_EXPORT_VARIABLE be_define_const_class(
be_class_tcpclient,
1,
NULL,
tcpclient
);

View File

@ -17,6 +17,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
// also includes tcp_client
#ifdef USE_BERRY #ifdef USE_BERRY
@ -84,7 +85,6 @@ extern "C" {
// //
int32_t wc_init(struct bvm *vm); int32_t wc_init(struct bvm *vm);
int32_t wc_init(struct bvm *vm) { int32_t wc_init(struct bvm *vm) {
// int32_t argc = be_top(vm); // Get the number of arguments
WiFiClient * wcl = new WiFiClient(); WiFiClient * wcl = new WiFiClient();
be_pushcomptr(vm, (void*) wcl); be_pushcomptr(vm, (void*) wcl);
be_setmember(vm, 1, ".w"); be_setmember(vm, 1, ".w");
@ -96,6 +96,14 @@ extern "C" {
be_return_nil(vm); be_return_nil(vm);
} }
int32_t wc_tcp_init(struct bvm *vm);
int32_t wc_tcp_init(struct bvm *vm) {
WiFiClient * wcl = new WiFiClient();
be_pushcomptr(vm, (void*) wcl);
be_setmember(vm, 1, ".w");
be_return_nil(vm);
}
HTTPClientLight * wc_getclient(struct bvm *vm) { HTTPClientLight * wc_getclient(struct bvm *vm) {
be_getmember(vm, 1, ".p"); be_getmember(vm, 1, ".p");
void *p = be_tocomptr(vm, -1); void *p = be_tocomptr(vm, -1);
@ -123,6 +131,14 @@ extern "C" {
be_return_nil(vm); be_return_nil(vm);
} }
int32_t wc_tcp_deinit(struct bvm *vm);
int32_t wc_tcp_deinit(struct bvm *vm) {
WiFiClient * wcl = wc_getwificlient(vm);
if (wcl != nullptr) { delete wcl; }
be_setmember(vm, 1, ".w");
be_return_nil(vm);
}
// wc.url_encode(string) -> string // wc.url_encode(string) -> string
int32_t wc_urlencode(struct bvm *vm); int32_t wc_urlencode(struct bvm *vm);
int32_t wc_urlencode(struct bvm *vm) { int32_t wc_urlencode(struct bvm *vm) {
@ -151,6 +167,24 @@ extern "C" {
be_return(vm); /* return self */ be_return(vm); /* return self */
} }
// tcp.connect(url:string) -> self
int32_t wc_tcp_connect(struct bvm *vm);
int32_t wc_tcp_connect(struct bvm *vm) {
int32_t argc = be_top(vm);
if (argc >= 3 && be_isstring(vm, 2) && be_isint(vm, 3)) {
WiFiClient * tcp = wc_getwificlient(vm);
const char * address = be_tostring(vm, 2);
int32_t port = be_toint(vm, 3);
// open connection
if (!tcp->connect(address, port)) {
be_raise(vm, "value_error", "unsupported protocol");
}
be_pushvalue(vm, 1);
be_return(vm); /* return self */
}
be_raise(vm, "attribute_error", NULL);
}
// wc.close(void) -> nil // wc.close(void) -> nil
int32_t wc_close(struct bvm *vm); int32_t wc_close(struct bvm *vm);
int32_t wc_close(struct bvm *vm) { int32_t wc_close(struct bvm *vm) {
@ -159,6 +193,31 @@ extern "C" {
be_return_nil(vm); be_return_nil(vm);
} }
// tcp.close(void) -> nil
int32_t wc_tcp_close(struct bvm *vm);
int32_t wc_tcp_close(struct bvm *vm) {
WiFiClient * tcp = wc_getwificlient(vm);
tcp->stop();
be_return_nil(vm);
}
// tcp.close(void) -> nil
int32_t wc_tcp_flush(struct bvm *vm);
int32_t wc_tcp_flush(struct bvm *vm) {
WiFiClient * tcp = wc_getwificlient(vm);
tcp->flush();
be_return_nil(vm);
}
// tcp.available(void) -> int
int32_t wc_tcp_available(struct bvm *vm);
int32_t wc_tcp_available(struct bvm *vm) {
WiFiClient * tcp = wc_getwificlient(vm);
int32_t available = tcp->available();
be_pushint(vm, available);
be_return(vm);
}
// wc.wc_set_timeouts([http_timeout_ms:int, tcp_timeout_ms:int]) -> self // wc.wc_set_timeouts([http_timeout_ms:int, tcp_timeout_ms:int]) -> self
int32_t wc_set_timeouts(struct bvm *vm); int32_t wc_set_timeouts(struct bvm *vm);
int32_t wc_set_timeouts(struct bvm *vm) { int32_t wc_set_timeouts(struct bvm *vm) {
@ -239,6 +298,65 @@ extern "C" {
be_return(vm); /* return code */ be_return(vm); /* return code */
} }
// tcp.connected(void) -> bool
int32_t wc_tcp_connected(struct bvm *vm);
int32_t wc_tcp_connected(struct bvm *vm) {
WiFiClient * tcp = wc_getwificlient(vm);
be_pushbool(vm, tcp->connected());
be_return(vm); /* return code */
}
// tcp.write(bytes | string) -> int
int32_t wc_tcp_write(struct bvm *vm);
int32_t wc_tcp_write(struct bvm *vm) {
int32_t argc = be_top(vm);
if (argc >= 2 && (be_isstring(vm, 2) || be_isbytes(vm, 2))) {
WiFiClient * tcp = wc_getwificlient(vm);
const char * buf = nullptr;
size_t buf_len = 0;
if (be_isstring(vm, 2)) { // string
buf = be_tostring(vm, 2);
buf_len = strlen(buf);
} else { // bytes
buf = (const char*) be_tobytes(vm, 2, &buf_len);
}
size_t bw = tcp->write(buf, buf_len);
be_pushint(vm, bw);
be_return(vm); /* return code */
}
be_raise(vm, kTypeError, nullptr);
}
// tcp.read() -> string
int32_t wc_tcp_read(struct bvm *vm);
int32_t wc_tcp_read(struct bvm *vm) {
WiFiClient * tcp = wc_getwificlient(vm);
int32_t btr = tcp->available();
if (btr <= 0) {
be_pushstring(vm, "");
} else {
char * buf = (char*) be_pushbuffer(vm, btr);
int32_t btr2 = tcp->read((uint8_t*) buf, btr);
be_pushnstring(vm, buf, btr2);
}
be_return(vm); /* return code */
}
// tcp.readbytes() -> bytes
int32_t wc_tcp_readbytes(struct bvm *vm);
int32_t wc_tcp_readbytes(struct bvm *vm) {
WiFiClient * tcp = wc_getwificlient(vm);
int32_t btr = tcp->available();
if (btr <= 0) {
be_pushbytes(vm, nullptr, 0);
} else {
uint8_t * buf = (uint8_t*) be_pushbuffer(vm, btr);
int32_t btr2 = tcp->read(buf, btr);
be_pushbytes(vm, buf, btr2);
}
be_return(vm); /* return code */
}
void wc_errorCodeMessage(int32_t httpCode, uint32_t http_connect_time) { void wc_errorCodeMessage(int32_t httpCode, uint32_t http_connect_time) {
if (httpCode < 0) { if (httpCode < 0) {
if (httpCode <= -1000) { if (httpCode <= -1000) {