Last active
July 7, 2024 10:42
Revisions
-
yashi revised this gist
Jul 7, 2024 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,7 @@ /* * You need pty setup: socat -d -d pty,raw,echo=0,link=/tmp/pty1 pty,raw,echo=0,link=/tmp/pty2 */ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> -
yashi revised this gist
Jul 7, 2024 . 1 changed file with 47 additions and 47 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,77 +14,77 @@ bool done = false; void *do_route(void *arg) { while (!done) { csp_route_work(); } } csp_usart_conf_t conf = { .device = "/tmp/pty1", .baudrate = 115200, .databits = 8, .stopbits = 1, .paritysetting = 0, }; int main(int argc, char * argv[]) { csp_iface_t * iface; csp_socket_t sock = {0}; csp_packet_t * packet; pthread_t router_thread; int ret; csp_init(); ret = csp_usart_open_and_add_kiss_interface(&conf, CSP_IF_KISS_DEFAULT_NAME, &iface); if (ret != CSP_ERR_NONE) { printf("open failed\n"); exit(1); } iface->addr = SERVER_ADDR; iface->is_default = 1; csp_conn_print_table(); csp_iflist_print(); ret = pthread_create(&router_thread, NULL, do_route, NULL); if (ret != 0) { printf("pthread failed\n"); exit(1); } csp_bind(&sock, CSP_ANY); csp_listen(&sock, SERVER_PORT); packet = csp_buffer_get(0); sprintf(packet->data, "Hi Client", 10); packet->length = 10; for (int i = 0; i < 3; i++) { csp_packet_t *req; csp_conn_t *conn; conn = csp_accept(&sock, CSP_MAX_TIMEOUT); req = csp_read(conn, CSP_MAX_TIMEOUT); switch (csp_conn_dport(conn)) { case 10: printf("Packet received on SERVER_PORT: %s\n", (char *) req->data); csp_sendto_reply(req, packet, CSP_O_SAME); break; default: printf("wrong port\n"); exit(1); break; } csp_close(conn); } return 0; } -
yashi created this gist
Jul 7, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,90 @@ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <pthread.h> #include <csp/csp.h> #include <csp/drivers/usart.h> #define SERVER_ADDR 1 #define SERVER_PORT 10 bool done = false; void *do_route(void *arg) { while (!done) { csp_route_work(); } } csp_usart_conf_t conf = { .device = "/tmp/pty1", .baudrate = 115200, .databits = 8, .stopbits = 1, .paritysetting = 0, }; int main(int argc, char * argv[]) { csp_iface_t * iface; csp_socket_t sock = {0}; csp_packet_t * packet; pthread_t router_thread; int ret; csp_init(); ret = csp_usart_open_and_add_kiss_interface(&conf, CSP_IF_KISS_DEFAULT_NAME, &iface); if (ret != CSP_ERR_NONE) { printf("open failed\n"); exit(1); } iface->addr = SERVER_ADDR; iface->is_default = 1; csp_conn_print_table(); csp_iflist_print(); ret = pthread_create(&router_thread, NULL, do_route, NULL); if (ret != 0) { printf("pthread failed\n"); exit(1); } csp_bind(&sock, CSP_ANY); csp_listen(&sock, SERVER_PORT); packet = csp_buffer_get(0); sprintf(packet->data, "Hi Client", 10); packet->length = 10; for (int i = 0; i < 3; i++) { csp_packet_t *req; csp_conn_t *conn; conn = csp_accept(&sock, CSP_MAX_TIMEOUT); req = csp_read(conn, CSP_MAX_TIMEOUT); switch (csp_conn_dport(conn)) { case 10: printf("Packet received on SERVER_PORT: %s\n", (char *) req->data); csp_sendto_reply(req, packet, CSP_O_SAME); break; default: printf("wrong port\n"); exit(1); break; } csp_close(conn); } return 0; }