Skip to content

Commit 32e84fd

Browse files
usart: Add client_server_without_response
It's a basic csp client & server topology. With a server as a listener only and client as a sender only. Signed-off-by: Gaetan Perrot <gaetan.perrot@spacecubics.com>
1 parent 4179b3e commit 32e84fd

File tree

4 files changed

+193
-0
lines changed

4 files changed

+193
-0
lines changed

examples/usart/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Add can examples directories
22
add_subdirectory(client_server_with_server_response)
3+
add_subdirectory(client_server_without_server_response)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# CAN node 1
2+
add_executable(csp_client_usart
3+
csp_client_usart.c
4+
)
5+
target_link_libraries(csp_client_usart csp utils Threads::Threads)
6+
7+
# CAN node 2
8+
add_executable(csp_server_usart
9+
csp_server_usart.c
10+
)
11+
target_link_libraries(csp_server_usart csp utils Threads::Threads)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <csp/csp_debug.h>
2+
#include <string.h>
3+
#include <unistd.h>
4+
#include <stdlib.h>
5+
#include <stdio.h>
6+
#include <time.h>
7+
8+
#include <csp/csp.h>
9+
#include <csp/drivers/usart.h>
10+
11+
#include "csp_posix_helper.h"
12+
13+
#define SERVER_PORT 10
14+
#define DEVICE_NAME "/tmp/pty2"
15+
#define CLIENT_ADDRESS 1
16+
#define SERVER_ADDRESS 2
17+
18+
int main(void) {
19+
int ret = EXIT_SUCCESS;
20+
struct timespec start_time, current_time;
21+
unsigned int successful_ping = 0;
22+
unsigned int run_duration_in_sec = 3;
23+
unsigned int count = 'A';
24+
25+
/* Set CSP protocol version and address */
26+
csp_conf.version = 2;
27+
28+
/* Initialize CSP */
29+
csp_init();
30+
31+
/* Start CSP router */
32+
router_start();
33+
34+
/* Add USART interface */
35+
csp_iface_t * iface = NULL;
36+
csp_usart_conf_t conf = {
37+
.device = DEVICE_NAME,
38+
.baudrate = 115200, /* supported on all platforms */
39+
.databits = 8,
40+
.stopbits = 1,
41+
.paritysetting = 0,
42+
};
43+
if (csp_usart_open_and_add_kiss_interface(&conf, CSP_IF_KISS_DEFAULT_NAME,
44+
SERVER_ADDRESS,
45+
&iface) != CSP_ERR_NONE) {
46+
csp_print("Failed to add KISS interface\n");
47+
return -1;
48+
}
49+
iface->is_default = 1;
50+
51+
/* Print interface list */
52+
csp_print("Available interfaces:\n");
53+
csp_iflist_print();
54+
55+
csp_print("Client started\n");
56+
57+
while (1) {
58+
/* Wait 200 ms between iterations */
59+
usleep(200000);
60+
61+
/* Send a CSP ping to the server */
62+
int result = csp_ping(SERVER_ADDRESS, 1000, 100, CSP_O_NONE);
63+
csp_print("Ping to address %u, result %d [ms]\n", SERVER_ADDRESS, result);
64+
if (result >= 0) {
65+
++successful_ping;
66+
}
67+
68+
/* Establish a connection to the server */
69+
csp_conn_t * conn = csp_connect(CSP_PRIO_NORM, SERVER_ADDRESS, SERVER_PORT, 1000, CSP_O_NONE);
70+
if (conn == NULL) {
71+
csp_print("Connection failed\n");
72+
ret = EXIT_FAILURE;
73+
break;
74+
}
75+
76+
/* Get a CSP packet buffer */
77+
csp_packet_t * packet = csp_buffer_get(0);
78+
if (packet == NULL) {
79+
csp_print("Failed to obtain CSP buffer\n");
80+
ret = EXIT_FAILURE;
81+
break;
82+
}
83+
84+
/* Fill the packet with data */
85+
snprintf((char *)packet->data, CSP_BUFFER_SIZE, "Hello world %c", count++);
86+
packet->length = strlen((char *)packet->data) + 1;
87+
88+
/* Send the packet and close the connection */
89+
csp_send(conn, packet);
90+
csp_close(conn);
91+
}
92+
93+
return ret;
94+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <csp/csp.h>
2+
#include <csp/csp_debug.h>
3+
#include <csp/drivers/usart.h>
4+
#include <unistd.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
#include <stdio.h>
8+
9+
#include "csp_posix_helper.h"
10+
11+
#define SERVER_ADDRESS 2
12+
#define CLIENT_ADDRESS 1
13+
#define SERVER_PORT 10
14+
#define DEVICE_NAME "/tmp/pty1"
15+
16+
void * server(void * param) {
17+
(void)param;
18+
csp_print("Server task started\n");
19+
20+
csp_socket_t sock = {0};
21+
csp_bind(&sock, CSP_ANY);
22+
csp_listen(&sock, 10);
23+
24+
while (1) {
25+
csp_conn_t * conn = csp_accept(&sock, 10000);
26+
if (!conn)
27+
continue;
28+
29+
csp_packet_t * packet;
30+
while ((packet = csp_read(conn, 50)) != NULL) {
31+
if (csp_conn_dport(conn) == SERVER_PORT) {
32+
csp_print("Received: %s\n", (char *)packet->data);
33+
csp_buffer_free(packet);
34+
} else {
35+
csp_service_handler(packet);
36+
}
37+
}
38+
39+
csp_close(conn);
40+
}
41+
42+
return NULL;
43+
}
44+
45+
int main(void) {
46+
csp_print("Initializing CSP with USART interface...\n");
47+
48+
/* Set protocol version and address */
49+
csp_conf.version = 2;
50+
51+
/* Init CSP */
52+
csp_init();
53+
54+
/* Start router */
55+
router_start();
56+
57+
/* Add USART interface */
58+
csp_iface_t * iface = NULL;
59+
csp_usart_conf_t conf = {
60+
.device = DEVICE_NAME,
61+
.baudrate = 115200, /* supported on all platforms */
62+
.databits = 8,
63+
.stopbits = 1,
64+
.paritysetting = 0,
65+
};
66+
if (csp_usart_open_and_add_kiss_interface(&conf, CSP_IF_KISS_DEFAULT_NAME,
67+
SERVER_ADDRESS,
68+
&iface) != CSP_ERR_NONE) {
69+
csp_print("Failed to add KISS interface\n");
70+
return -1;
71+
}
72+
iface->is_default = 1;
73+
74+
csp_print("Connection table\r\n");
75+
csp_conn_print_table();
76+
77+
csp_print("Interfaces\r\n");
78+
csp_iflist_print();
79+
80+
csp_pthread_create(server);
81+
82+
while (1) {
83+
sleep(1);
84+
}
85+
86+
return 0;
87+
}

0 commit comments

Comments
 (0)