Skip to content

Commit cf1e308

Browse files
committed
First steps towards adding sockets to python
1 parent 71eaa8e commit cf1e308

3 files changed

Lines changed: 78 additions & 1 deletion

File tree

main/python.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "freertos/task.h"
77
#include "pocketpy.h"
88
#include "portmacro.h"
9+
#include "python_socket.h"
910
#include "sdkconfig.h"
1011

1112
#ifdef CONFIG_IDF_TARGET_ESP32P4
@@ -16,7 +17,7 @@
1617
static const char* TAG = "python";
1718

1819
#define REPL_BUFFER_SIZE (1024)
19-
#define PYTHON_TASK_STACK_SIZE (4096)
20+
#define PYTHON_TASK_STACK_SIZE (16384)
2021

2122
char input_buffer[64] = {0};
2223
uint32_t input_offset = 0;
@@ -130,6 +131,8 @@ static void python_task(void* pvParameters) {
130131
// Initialize the Python runtime
131132
py_initialize();
132133

134+
pk__add_module_socket();
135+
133136
while (true) {
134137
int size = python_repl(line_buffer, sizeof(line_buffer));
135138
assert(size < sizeof(line_buffer));

main/python_socket.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "python_socket.h"
2+
#include <string.h>
3+
#include "esp_log.h"
4+
#include "freertos/FreeRTOS.h"
5+
#include "freertos/task.h"
6+
#include "pocketpy/pocketpy.h"
7+
#include "sys/socket.h"
8+
9+
static bool example_func(int argc, py_Ref argv) {
10+
PY_CHECK_ARGC(1);
11+
py_f64 secs;
12+
if (!py_castfloat(argv, &secs)) return false;
13+
14+
printf("EXAMPLE FUNC\r\n");
15+
16+
py_newnone(py_retval());
17+
18+
return true;
19+
}
20+
21+
static bool getaddrinfo(int argc, py_Ref argv) {
22+
PY_CHECK_ARGC(2);
23+
PY_CHECK_ARG_TYPE(0, tp_str); // Hostname
24+
PY_CHECK_ARG_TYPE(1, tp_int); // Port
25+
26+
const char* hostname = py_tostr(py_arg(0));
27+
int port = py_toint(py_arg(1));
28+
29+
// struct addrinfo hints = {.ai_socktype = SOCK_STREAM};
30+
struct addrinfo* address_info = NULL;
31+
32+
/*int res = getaddrinfo(hostname, port, &hints, &address_info);
33+
if (res != 0 || address_info == NULL) {
34+
return OSError("Getaddrinfo failed %d", res);
35+
}*/
36+
37+
py_newnone(py_retval());
38+
return true;
39+
}
40+
41+
void pk__add_module_socket(void) {
42+
py_Ref mod = py_newmodule("socket");
43+
py_bindfunc(mod, "socket", example_func);
44+
py_bindfunc(mod, "getaddrinfo", getaddrinfo);
45+
}
46+
47+
/*
48+
py_bindfunc(mod, "bind", example_func);
49+
py_bindfunc(mod, "listen", example_func);
50+
py_bindfunc(mod, "accept", example_func);
51+
py_bindfunc(mod, "connect", example_func);
52+
py_bindfunc(mod, "send", example_func);
53+
py_bindfunc(mod, "sendall", example_func);
54+
py_bindfunc(mod, "recv", example_func);
55+
py_bindfunc(mod, "sendto", example_func);
56+
py_bindfunc(mod, "recvfrom", example_func);
57+
py_bindfunc(mod, "setsockopt", example_func);
58+
py_bindfunc(mod, "makefile", example_func);
59+
py_bindfunc(mod, "settimeout", example_func);
60+
py_bindfunc(mod, "setblocking", example_func);
61+
*/

main/python_socket.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#define POCKETPY_SOCKET_AF_INET (2)
4+
#define POCKETPY_SOCKET_AF_INET6 (10)
5+
6+
#define POCKETPY_SOCKET_SOCK_STREAM (1)
7+
#define POCKETPY_SOCKET_SOCK_DGRAM (2)
8+
#define POCKETPY_SOCKET_SOCK_RAW (3)
9+
10+
#define POCKETPY_SOCKET_STA_IF (0)
11+
#define POCKETPY_SOCKET_AP_IF (1)
12+
13+
void pk__add_module_socket(void);

0 commit comments

Comments
 (0)