Last active
July 7, 2024 10:42
-
-
Save yashi/15e33944f5b1ec1a1d634b545781b68f to your computer and use it in GitHub Desktop.
This file contains 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 characters
/* | |
* 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> | |
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment