Skip to content

Commit 772241b

Browse files
committed
WIP Fix picoruby-net for both MicroRuby and PicoRuby
1 parent 60a04b6 commit 772241b

File tree

10 files changed

+360
-128
lines changed

10 files changed

+360
-128
lines changed

mrbgems/picoruby-mbedtls/include/mbedtls_config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
*/
55
#define MBEDTLS_PLATFORM_C
66

7+
#define MBEDTLS_TIMING_C
8+
#define MBEDTLS_TIMING_ALT
9+
710
/*
811
* To debug TSL connection, you can use the following code:
912
* ```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef TIMING_ALT_DEFINED_H_
2+
#define TIMING_ALT_DEFINED_H_
3+
4+
void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms);
5+
int mbedtls_timing_get_delay(void *data);
6+
7+
#endif /* TIMING_ALT_DEFINED_H_ */
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "pico/time.h"
2+
#include "mbedtls/timing.h"
3+
4+
typedef struct {
5+
absolute_time_t end_time;
6+
int int_ms;
7+
int fin_ms;
8+
int active;
9+
} timing_delay_context;
10+
11+
void
12+
mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
13+
{
14+
timing_delay_context *ctx = (timing_delay_context *)data;
15+
16+
ctx->int_ms = (int)int_ms;
17+
ctx->fin_ms = (int)fin_ms;
18+
ctx->active = 1;
19+
20+
if (fin_ms == 0) {
21+
// Cancel the timer
22+
ctx->active = 0;
23+
return;
24+
}
25+
26+
// Set end time to now + fin_ms
27+
ctx->end_time = make_timeout_time_ms(fin_ms);
28+
}
29+
30+
int
31+
mbedtls_timing_get_delay(void *data)
32+
{
33+
timing_delay_context *ctx = (timing_delay_context *)data;
34+
35+
if (ctx->fin_ms == 0 || !ctx->active)
36+
return -1;
37+
38+
absolute_time_t now = get_absolute_time();
39+
int64_t elapsed_ms = absolute_time_diff_us(now, ctx->end_time) / 1000;
40+
41+
if (elapsed_ms <= -ctx->fin_ms) {
42+
return 2; // Final delay passed
43+
} else if (elapsed_ms <= -ctx->int_ms) {
44+
return 1; // Intermediate delay passed
45+
} else {
46+
return 0; // No delay passed yet
47+
}
48+
}
49+
50+
// Stub function for mbedtls_timing_hardclock
51+
unsigned long
52+
mbedtls_timing_hardclock(void)
53+
{
54+
static unsigned long counter = 0;
55+
return ++counter;
56+
}

mrbgems/picoruby-net/include/net.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,31 @@
1616
extern "C" {
1717
#endif
1818

19-
void lwip_begin(void);
20-
void lwip_end(void);
21-
void Net_sleep_ms(int);
22-
err_t Net_get_ip(const char *name, ip_addr_t *ip);
23-
2419
#if defined(PICORB_VM_MRUBYC)
2520
#define mrb_state mrbc_vm
2621
#endif
2722

28-
#define OUTBUF_SIZE 16
23+
typedef struct {
24+
const char *host;
25+
int port;
26+
const char *send_data;
27+
size_t send_data_len;
28+
bool is_tls;
29+
} net_request_t;
30+
31+
typedef struct {
32+
char *recv_data;
33+
size_t recv_data_len;
34+
} net_response_t;
2935

3036
void DNS_resolve(const char *name, bool is_tcp, char *outbuf, size_t outlen);
31-
mrb_value TCPClient_send(const char *host, int port, mrb_state *mrb, mrb_value send_data, bool is_tls);
32-
mrb_value UDPClient_send(const char *host, int port, mrb_state *mrb, mrb_value send_data, bool is_dtls);
37+
void TCPClient_send(mrb_state *mrb, const net_request_t *req, net_response_t *res);
38+
void UDPClient_send(mrb_state *mrb, const net_request_t *req, net_response_t *res);
39+
40+
void lwip_begin(void);
41+
void lwip_end(void);
42+
void Net_sleep_ms(int);
43+
err_t Net_get_ip(const char *name, ip_addr_t *ip);
3344

3445
#ifdef __cplusplus
3546
}
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#include "../include/net.h"
2-
#include "lwipopts.h"
32
#include "lwip/pbuf.h"
43

5-
#include "mruby.h"
6-
74
void
85
DNS_resolve(const char *name, bool is_tcp, char *outbuf, size_t outlen)
96
{

mrbgems/picoruby-net/src/mruby/net.c

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
#include "../include/net.h"
12
#include "mruby.h"
23
#include "mruby/presym.h"
3-
#include "mruby.h"
4-
#include "../include/net.h"
4+
#include "mruby/string.h"
5+
6+
#define DNS_OUTBUF_SIZE 16
57

68
static mrb_value
79
mrb_net_dns_s_resolve(mrb_state *mrb, mrb_value self)
810
{
911
const char *host;
1012
mrb_bool is_tls;
1113
mrb_get_args(mrb, "zb", &host, &is_tls);
12-
char outbuf[OUTBUF_SIZE];
13-
DNS_resolve(host, is_tls, outbuf, sizeof(outbuf));
14+
char outbuf[DNS_OUTBUF_SIZE];
15+
DNS_resolve(host, is_tls, outbuf, DNS_OUTBUF_SIZE);
1416
if (outbuf[0] == '\0') {
1517
return mrb_nil_value();
1618
} else {
@@ -25,8 +27,27 @@ mrb_net_tcpclient_s__request_impl(mrb_state *mrb, mrb_value self)
2527
mrb_int port;
2628
mrb_value data;
2729
mrb_bool use_dtls;
30+
mrb_value ret;
2831
mrb_get_args(mrb, "ziob", &host, &port, &data, &use_dtls);
29-
return TCPClient_send(host, port, mrb, data, use_dtls);
32+
net_request_t req = {
33+
.host = host,
34+
.port = port,
35+
.send_data = RSTRING_PTR(data),
36+
.send_data_len = RSTRING_LEN(data),
37+
.is_tls = use_dtls
38+
};
39+
net_response_t res = {
40+
.recv_data = NULL,
41+
.recv_data_len = 0
42+
};
43+
TCPClient_send(mrb, &req, &res);
44+
if (res.recv_data == NULL) {
45+
ret = mrb_nil_value();
46+
} else {
47+
ret = mrb_str_new(mrb, (const char*)res.recv_data, res.recv_data_len);
48+
picorb_free(mrb, res.recv_data);
49+
}
50+
return ret;
3051
}
3152

3253
static mrb_value
@@ -36,8 +57,27 @@ mrb_net_udpclient_s__send_impl(mrb_state *mrb, mrb_value self)
3657
mrb_int port;
3758
mrb_value data;
3859
mrb_bool use_dtls;
60+
mrb_value ret;
3961
mrb_get_args(mrb, "ziob", &host, &port, &data, &use_dtls);
40-
return UDPClient_send(host, port, mrb, data, use_dtls);
62+
net_request_t req = {
63+
.host = host,
64+
.port = port,
65+
.send_data = RSTRING_PTR(data),
66+
.send_data_len = RSTRING_LEN(data),
67+
.is_tls = use_dtls
68+
};
69+
net_response_t res = {
70+
.recv_data = NULL,
71+
.recv_data_len = 0
72+
};
73+
UDPClient_send(mrb, &req, &res);
74+
if (res.recv_data == NULL) {
75+
ret = mrb_nil_value();
76+
} else {
77+
ret = mrb_str_new(mrb, (const char*)res.recv_data, res.recv_data_len);
78+
picorb_free(mrb, res.recv_data);
79+
}
80+
return ret;
4181
}
4282

4383
void
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include "mrubyc.h"
2+
3+
static void
4+
c_net_dns_resolve(mrbc_vm *vm, mrbc_value *v, int argc)
5+
{
6+
if (argc != 1) {
7+
mrbc_raise(vm, MRBC_CLASS(ArgumentError), "wrong number of arguments");
8+
return;
9+
}
10+
mrbc_value input = GET_ARG(1);
11+
if (input.tt != MRBC_TT_STRING) {
12+
mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument");
13+
return;
14+
}
15+
16+
mrbc_value ret = DNS_resolve(vm, (const char *)GET_STRING_ARG(1), (GET_ARG(2).tt == MRBC_TT_TRUE));
17+
SET_RETURN(ret);
18+
}
19+
20+
static void
21+
c_net_tcpclient__request_impl(mrbc_vm *vm, mrbc_value *v, int argc)
22+
{
23+
if (argc != 4) {
24+
mrbc_raise(vm, MRBC_CLASS(ArgumentError), "wrong number of arguments");
25+
return;
26+
}
27+
mrbc_value host = GET_ARG(1);
28+
if (host.tt != MRBC_TT_STRING) {
29+
mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument");
30+
return;
31+
}
32+
mrbc_value port = GET_ARG(2);
33+
if (port.tt != MRBC_TT_INTEGER) {
34+
mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument");
35+
return;
36+
}
37+
mrbc_value input = GET_ARG(3);
38+
if (input.tt != MRBC_TT_STRING) {
39+
mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument");
40+
return;
41+
}
42+
mrbc_value is_tls = GET_ARG(4);
43+
if (!((is_tls.tt == MRBC_TT_TRUE) || (is_tls.tt == MRBC_TT_FALSE))) {
44+
mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument");
45+
return;
46+
}
47+
48+
mrbc_value ret = TCPClient_send((const char *)GET_STRING_ARG(1), port.i, vm, &input, (is_tls.tt == MRBC_TT_TRUE));
49+
SET_RETURN(ret);
50+
}
51+
52+
static void
53+
c_net_udpclient__send_impl(mrbc_vm *vm, mrbc_value *v, int argc)
54+
{
55+
if (argc != 4) {
56+
mrbc_raise(vm, MRBC_CLASS(ArgumentError), "wrong number of arguments");
57+
return;
58+
}
59+
mrbc_value host = GET_ARG(1);
60+
if (host.tt != MRBC_TT_STRING) {
61+
mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument for host");
62+
return;
63+
}
64+
mrbc_value port = GET_ARG(2);
65+
if (port.tt != MRBC_TT_INTEGER) {
66+
mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument for port");
67+
return;
68+
}
69+
mrbc_value data = GET_ARG(3);
70+
if (data.tt != MRBC_TT_STRING) {
71+
mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument for data");
72+
return;
73+
}
74+
mrbc_value use_dtls = GET_ARG(4);
75+
if (!((use_dtls.tt == MRBC_TT_TRUE) || (use_dtls.tt == MRBC_TT_FALSE))) {
76+
mrbc_raise(vm, MRBC_CLASS(TypeError), "wrong type of argument for use_dtls");
77+
return;
78+
}
79+
80+
mrbc_value ret = UDPClient_send((const char *)GET_STRING_ARG(1), port.i, vm, &data, (use_dtls.tt == MRBC_TT_TRUE));
81+
SET_RETURN(ret);
82+
}
83+
84+
void
85+
mrbc_net_init(mrbc_vm *vm)
86+
{
87+
mrbc_class *class_Net = mrbc_define_class(vm, "Net", mrbc_class_object);
88+
89+
mrbc_class *class_Net_DNS = mrbc_define_class_under(vm, class_Net, "DNS", mrbc_class_object);
90+
mrbc_define_method(vm, class_Net_DNS, "resolve", c_net_dns_resolve);
91+
92+
mrbc_class *class_Net_TCPClient = mrbc_define_class_under(vm, class_Net, "TCPClient", mrbc_class_object);
93+
mrbc_define_method(vm, class_Net_TCPClient, "_request_impl", c_net_tcpclient__request_impl);
94+
95+
mrbc_class *class_Net_UDPClient = mrbc_define_class_under(vm, class_Net, "UDPClient", mrbc_class_object);
96+
mrbc_define_method(vm, class_Net_UDPClient, "_send_impl", c_net_udpclient__send_impl);
97+
}

mrbgems/picoruby-net/src/net.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ Net_get_ip(const char *name, ip_addr_t *ip)
3535

3636
#if defined(PICORB_VM_MRUBY)
3737

38-
#include "mruby/dns.c"
3938
#include "mruby/net.c"
40-
#include "mruby/tcp.c"
41-
#include "mruby/udp.c"
4239

4340
#elif defined(PICORB_VM_MRUBYC)
4441

0 commit comments

Comments
 (0)