Skip to content

Instantly share code, notes, and snippets.

@yashi
Last active July 7, 2024 10:42

Revisions

  1. yashi revised this gist Jul 7, 2024. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions port10-server.c
    Original 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>
  2. yashi revised this gist Jul 7, 2024. 1 changed file with 47 additions and 47 deletions.
    94 changes: 47 additions & 47 deletions port10-server.c
    Original 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();
    }
    while (!done) {
    csp_route_work();
    }
    }

    csp_usart_conf_t conf = {
    .device = "/tmp/pty1",
    .baudrate = 115200,
    .databits = 8,
    .stopbits = 1,
    .paritysetting = 0,
    .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_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;
    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);
    }
    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);
    csp_bind(&sock, CSP_ANY);
    csp_listen(&sock, SERVER_PORT);

    packet = csp_buffer_get(0);
    sprintf(packet->data, "Hi Client", 10);
    packet->length = 10;
    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;
    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);
    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);
    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;
    csp_sendto_reply(req, packet, CSP_O_SAME);
    break;

    default:
    printf("wrong port\n");
    exit(1);
    break;
    }
    default:
    printf("wrong port\n");
    exit(1);
    break;
    }

    csp_close(conn);
    }
    csp_close(conn);
    }

    return 0;
    }
  3. yashi created this gist Jul 7, 2024.
    90 changes: 90 additions & 0 deletions port10-server.c
    Original 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;
    }