Skip to content

Instantly share code, notes, and snippets.

@cpq
Last active June 4, 2021 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpq/8df18ed692f7a4d7a461a662ba87d380 to your computer and use it in GitHub Desktop.
Save cpq/8df18ed692f7a4d7a461a662ba87d380 to your computer and use it in GitHub Desktop.
CNIP API suggestion
// The stack has two API: low level and high level
// Low level: used to hook it up to a hardware, to send/receive MAC frames
// High level: used to implement network apps, like HTTP/MQTT client/servers.
// Low level
// A device code does something like this:
//
// void cnip_mac_out(char *buf, size_t len) {
// bitbang_to_ethernet(buf, len); // Push outgoing frame to the network
// }
//
// for (;;) {
// char frame[1500];
// size_t n = receive_frame(frame, sizeof(frame));
// cnip_mac_in(frame, n); // Process incoming frame
// cnip_poll(current_time); // Process timers, and call user high level handlers
// }
void cnip_mac_in(char *buf, size_t len); // Implemented by CNIP
void cnip_mac_out(char *buf, size_t le); // Must be implemented by user
void cnip_poll(time_t now); // Must be called periodically or on event
// High level API - for creating network applications/services
// Example usage:
// void my_fn(struct cn_connection *c, int ev, void *ev_data) {
// switch (ev) {
// case EV_READ:
// serve_http(c, c->recv_buffer);
// break;
// }
// }
//
// cn_tcp_listen(0, 80, my_fn, &minecraft_server_data);
struct cn_connection {
... stuff like local/remote addresses, incoming and outgoing IO buffers
};
// User-defined callback function, called on various events
enum { EV_ERROR, EV_CONNECT, EV_ACCEPT, EV_READ, EV_WRITE, EV_CLOSE };
typedef void (*cnip_fn_t)(struct cn_connection *, int ev, void *ev_data);
struct cn_connection *cnip_tcp_listen(uint32_t ip, uint16_t port, cnip_fn_t fn, void *fn_data);
struct cn_connection *cnip_tcp_connect(uint32_t ip, uint16_t port, cnip_fn_t fn, void *fn_data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment