Last active
July 7, 2024 10:42
-
-
Save yashi/bd1479a1489d8160a5ee05a9cb174566 to your computer and use it in GitHub Desktop.
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 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 | |
#define CLIENT_ADDR 2 | |
bool done = false; | |
void *do_route(void *arg) | |
{ | |
while (!done) { | |
csp_route_work(); | |
} | |
} | |
csp_usart_conf_t conf = { | |
.device = "/tmp/pty2", | |
.baudrate = 115200, | |
.databits = 8, | |
.stopbits = 1, | |
.paritysetting = 0, | |
}; | |
int main(int argc, char * argv[]) | |
{ | |
csp_iface_t * iface; | |
csp_conn_t * conn; | |
csp_packet_t * packet; | |
csp_packet_t * reply; | |
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) { | |
csp_print("open failed\n"); | |
exit(1); | |
} | |
iface->addr = CLIENT_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); | |
} | |
packet = csp_buffer_get(0); | |
sprintf(packet->data, "Hi Server", 10); | |
packet->length = 10; | |
conn = csp_connect(CSP_PRIO_NORM, SERVER_ADDR, SERVER_PORT, 0, CSP_O_NONE); | |
printf("Packet port: %d\n", csp_conn_dport(conn)); | |
csp_send(conn, packet); | |
reply = csp_read(conn, CSP_MAX_TIMEOUT); | |
printf("Packet received on CLIENT_PORT: %s\n", (char *)reply->data); | |
csp_close(conn); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment