Skip to content

Commit 50129cb

Browse files
can: 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 3f9273d commit 50129cb

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed
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_can
3+
csp_client_can.c
4+
)
5+
target_link_libraries(csp_client_can csp utils Threads::Threads)
6+
7+
# CAN node 2
8+
add_executable(csp_server_can
9+
csp_server_can.c
10+
)
11+
target_link_libraries(csp_server_can csp utils Threads::Threads)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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/can_socketcan.h>
10+
11+
#include "csp_posix_helper.h"
12+
13+
#define SERVER_PORT 10
14+
#define VCAN_DEVICE "vcan0"
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 CAN interface */
35+
csp_iface_t * iface = NULL;
36+
int error = csp_can_socketcan_open_and_add_interface(VCAN_DEVICE, CSP_IF_CAN_DEFAULT_NAME, CLIENT_ADDRESS, 1000000, true, &iface);
37+
if (error != CSP_ERR_NONE) {
38+
csp_print("Error adding CAN interface [%s], error: %d\n", VCAN_DEVICE, error);
39+
return EXIT_FAILURE;
40+
}
41+
42+
/* Set this interface as default */
43+
iface->is_default = 1;
44+
45+
/* Print interface list */
46+
csp_print("Available interfaces:\n");
47+
csp_iflist_print();
48+
49+
csp_print("Client started\n");
50+
51+
while (1) {
52+
/* Wait 200 ms between iterations */
53+
usleep(200000);
54+
55+
/* Send a CSP ping to the server */
56+
int result = csp_ping(SERVER_ADDRESS, 1000, 100, CSP_O_NONE);
57+
csp_print("Ping to address %u, result %d [ms]\n", SERVER_ADDRESS, result);
58+
if (result >= 0) {
59+
++successful_ping;
60+
}
61+
62+
/* Establish a connection to the server */
63+
csp_conn_t * conn = csp_connect(CSP_PRIO_NORM, SERVER_ADDRESS, SERVER_PORT, 1000, CSP_O_NONE);
64+
if (conn == NULL) {
65+
csp_print("Connection failed\n");
66+
ret = EXIT_FAILURE;
67+
break;
68+
}
69+
70+
/* Get a CSP packet buffer */
71+
csp_packet_t * packet = csp_buffer_get(0);
72+
if (packet == NULL) {
73+
csp_print("Failed to obtain CSP buffer\n");
74+
ret = EXIT_FAILURE;
75+
break;
76+
}
77+
78+
/* Fill the packet with data */
79+
snprintf((char *)packet->data, CSP_BUFFER_SIZE, "Hello world %c", count++);
80+
packet->length = strlen((char *)packet->data) + 1;
81+
82+
/* Send the packet and close the connection */
83+
csp_send(conn, packet);
84+
csp_close(conn);
85+
}
86+
87+
return ret;
88+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <csp/csp.h>
2+
#include <csp/csp_debug.h>
3+
#include <csp/drivers/can_socketcan.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 VCAN_DEVICE "vcan0"
15+
16+
static unsigned int server_received = 0;
17+
18+
/* Server thread */
19+
void * server(void * param) {
20+
(void)param;
21+
22+
csp_print("Server thread started\n");
23+
24+
csp_socket_t sock = {0};
25+
csp_bind(&sock, CSP_ANY);
26+
csp_listen(&sock, 5);
27+
28+
while (1) {
29+
csp_conn_t * conn = csp_accept(&sock, 10000);
30+
if (!conn)
31+
continue;
32+
33+
csp_packet_t * packet;
34+
while ((packet = csp_read(conn, 100)) != NULL) {
35+
if (csp_conn_dport(conn) == SERVER_PORT) {
36+
csp_print("Received: %s\n", (char *)packet->data);
37+
server_received++;
38+
csp_buffer_free(packet);
39+
} else {
40+
csp_service_handler(packet);
41+
}
42+
}
43+
44+
csp_close(conn);
45+
}
46+
47+
return NULL;
48+
}
49+
50+
int main(void) {
51+
csp_print("Initializing CSP with CAN interface...\n");
52+
53+
/* Set protocol version and address */
54+
csp_conf.version = 2;
55+
56+
/* Init CSP */
57+
csp_init();
58+
59+
/* Start router */
60+
router_start();
61+
62+
/* Add CAN interface */
63+
csp_iface_t * iface = NULL;
64+
if (csp_can_socketcan_open_and_add_interface(
65+
VCAN_DEVICE, CSP_IF_CAN_DEFAULT_NAME, SERVER_ADDRESS, 1000000, true,
66+
&iface) != CSP_ERR_NONE) {
67+
csp_print("Failed to add CAN interface\n");
68+
return 1;
69+
}
70+
iface->is_default = 1;
71+
72+
csp_print("Interfaces:\n");
73+
csp_iflist_print();
74+
75+
csp_print("Starting server...\n");
76+
csp_pthread_create(server);
77+
78+
while (1) {
79+
sleep(5);
80+
csp_print("Received %u packets so far\n", server_received);
81+
}
82+
83+
return 0;
84+
}

0 commit comments

Comments
 (0)