|
| 1 | +# P.I.N.T |
| 2 | + |
| 3 | +### Python Implementation of Network Transports. |
| 4 | + |
| 5 | +Want to glue your esoteric network device into MicroPython without delving |
| 6 | +into C or trying to figure out lwIP? PINT is just the refreshment you need. |
| 7 | + |
| 8 | +### Reference Implementation |
| 9 | + |
| 10 | +PINT uses a Python class to implement a socket-based networking driver. |
| 11 | + |
| 12 | +How you implement these functions is down to you- but you must expect/return |
| 13 | +the correct data. Here's the basic skeleton: |
| 14 | + |
| 15 | +```python |
| 16 | +class PINT_Socket(): |
| 17 | + """An arbitrary structure for storing data about your sockets. |
| 18 | +
|
| 19 | + Does not have to be a class, you could just return an int with the |
| 20 | + socket ID of your target device. |
| 21 | + """ |
| 22 | + def __init__(self): |
| 23 | + pass |
| 24 | + |
| 25 | + |
| 26 | +class PINT_NIC: |
| 27 | + """The actual NIC implementation. |
| 28 | +
|
| 29 | + Most of the heavy lifting is sockets based, you might want to implement |
| 30 | + the socket_ methods on your socket class and simply delegate to them. |
| 31 | + """ |
| 32 | + def __init__(self) -> None: |
| 33 | + pass |
| 34 | + |
| 35 | + def __del__(self) -> None: |
| 36 | + pass |
| 37 | + |
| 38 | + def gethostbyname(self, name: str) -> tuple[int, int, int, int]: |
| 39 | + return (127, 0, 0, 1) |
| 40 | + |
| 41 | + def socket_socket(self) -> PINT_Socket: |
| 42 | + return PINT_Socket() |
| 43 | + |
| 44 | + def socket_close(self, socket: PINT_Socket) -> None: |
| 45 | + pass |
| 46 | + |
| 47 | + def socket_bind(self, socket: PINT_Socket, ip: tuple[int, int, int, int], port: int) -> bool: |
| 48 | + return True |
| 49 | + |
| 50 | + def socket_listen(self, socket: PINT_Socket, backlog: int) -> bool: |
| 51 | + return True |
| 52 | + |
| 53 | + def socket_accept(self, socket: PINT_Socket, socket2: PINT_Socket, ip: tuple[int, int, int, int], port: int) -> bool: |
| 54 | + return True |
| 55 | + |
| 56 | + def socket_connect(self, socket: PINT_Socket, ip: tuple[int, int, int, int], port) -> bool: |
| 57 | + return True |
| 58 | + |
| 59 | + def socket_send(self, socket: PINT_Socket, buf: bytearray) -> int: |
| 60 | + return 0 |
| 61 | + |
| 62 | + def socket_recv(self, socket: PINT_Socket, buf: bytearray) -> int: |
| 63 | + """Buf is provided as a mutable bytearray, you must write into it.""" |
| 64 | + return 0 |
| 65 | + |
| 66 | + def socket_sendto(self, socket: PINT_Socket, buf: bytearray, ip, port) -> int: |
| 67 | + return 0 |
| 68 | + |
| 69 | + def socket_recvfrom(self, socket: PINT_Socket, buf: bytearray, ip, port) -> int: |
| 70 | + """Buf is provided as a mutable bytearray, you must write into it.""" |
| 71 | + return 0 |
| 72 | + |
| 73 | + def socket_setsockopt(self, socket: PINT_Socket, level: int, opt: int, val: bytearray) -> bool: |
| 74 | + return True |
| 75 | + |
| 76 | + def socket_settimeout(self, socket: PINT_Socket, timeout_ms: int) -> bool: |
| 77 | + return True |
| 78 | + |
| 79 | + def socket_ioctl(self, socket: PINT_Socket, request: int, arg: int) -> bool: |
| 80 | + return True |
| 81 | + |
| 82 | +``` |
0 commit comments