moved all third-party lib to separate folder
This commit is contained in:
165
thirdparty/libcsp/examples/csp_if_fifo.c
vendored
Normal file
165
thirdparty/libcsp/examples/csp_if_fifo.c
vendored
Normal file
@ -0,0 +1,165 @@
|
||||
/*
|
||||
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
|
||||
Copyright (C) 2012 GomSpace ApS (http://www.gomspace.com)
|
||||
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <csp/csp.h>
|
||||
#include <csp/csp_interface.h>
|
||||
|
||||
#define TYPE_SERVER 1
|
||||
#define TYPE_CLIENT 2
|
||||
#define PORT 10
|
||||
#define BUF_SIZE 250
|
||||
|
||||
pthread_t rx_thread;
|
||||
int rx_channel, tx_channel;
|
||||
|
||||
int csp_fifo_tx(csp_iface_t *ifc, csp_packet_t *packet, uint32_t timeout);
|
||||
|
||||
csp_iface_t csp_if_fifo = {
|
||||
.name = "fifo",
|
||||
.nexthop = csp_fifo_tx,
|
||||
.mtu = BUF_SIZE,
|
||||
};
|
||||
|
||||
int csp_fifo_tx(csp_iface_t *ifc, csp_packet_t *packet, uint32_t timeout) {
|
||||
/* Write packet to fifo */
|
||||
if (write(tx_channel, &packet->length, packet->length + sizeof(uint32_t) + sizeof(uint16_t)) < 0)
|
||||
printf("Failed to write frame\r\n");
|
||||
csp_buffer_free(packet);
|
||||
return CSP_ERR_NONE;
|
||||
}
|
||||
|
||||
void * fifo_rx(void * parameters) {
|
||||
csp_packet_t *buf = csp_buffer_get(BUF_SIZE);
|
||||
/* Wait for packet on fifo */
|
||||
while (read(rx_channel, &buf->length, BUF_SIZE) > 0) {
|
||||
csp_qfifo_write(buf, &csp_if_fifo, NULL);
|
||||
buf = csp_buffer_get(BUF_SIZE);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
int me, other, type;
|
||||
const char *message = "Testing CSP";
|
||||
const char *rx_channel_name;
|
||||
const char *tx_channel_name;
|
||||
csp_socket_t *sock;
|
||||
csp_conn_t *conn;
|
||||
csp_packet_t *packet;
|
||||
|
||||
/* Run as either server or client */
|
||||
if (argc != 2) {
|
||||
printf("usage: %s <server/client>\r\n", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Set type */
|
||||
if (strcmp(argv[1], "server") == 0) {
|
||||
me = 1;
|
||||
other = 2;
|
||||
tx_channel_name = "server_to_client";
|
||||
rx_channel_name = "client_to_server";
|
||||
type = TYPE_SERVER;
|
||||
} else if (strcmp(argv[1], "client") == 0) {
|
||||
me = 2;
|
||||
other = 1;
|
||||
tx_channel_name = "client_to_server";
|
||||
rx_channel_name = "server_to_client";
|
||||
type = TYPE_CLIENT;
|
||||
} else {
|
||||
printf("Invalid type. Must be either 'server' or 'client'\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Init CSP and CSP buffer system */
|
||||
if (csp_init(me) != CSP_ERR_NONE || csp_buffer_init(10, 300) != CSP_ERR_NONE) {
|
||||
printf("Failed to init CSP\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
tx_channel = open(tx_channel_name, O_RDWR);
|
||||
if (tx_channel < 0) {
|
||||
printf("Failed to open TX channel\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
rx_channel = open(rx_channel_name, O_RDWR);
|
||||
if (rx_channel < 0) {
|
||||
printf("Failed to open RX channel\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Start fifo RX task */
|
||||
pthread_create(&rx_thread, NULL, fifo_rx, NULL);
|
||||
|
||||
/* Set default route and start router */
|
||||
csp_route_set(CSP_DEFAULT_ROUTE, &csp_if_fifo, CSP_NODE_MAC);
|
||||
csp_route_start_task(0, 0);
|
||||
|
||||
/* Create socket and listen for incoming connections */
|
||||
if (type == TYPE_SERVER) {
|
||||
sock = csp_socket(CSP_SO_NONE);
|
||||
csp_bind(sock, PORT);
|
||||
csp_listen(sock, 5);
|
||||
}
|
||||
|
||||
/* Super loop */
|
||||
while (1) {
|
||||
if (type == TYPE_SERVER) {
|
||||
/* Process incoming packet */
|
||||
conn = csp_accept(sock, 1000);
|
||||
if (conn) {
|
||||
packet = csp_read(conn, 0);
|
||||
if (packet)
|
||||
printf("Received: %s\r\n", packet->data);
|
||||
csp_buffer_free(packet);
|
||||
csp_close(conn);
|
||||
}
|
||||
} else {
|
||||
/* Send a new packet */
|
||||
packet = csp_buffer_get(strlen(message));
|
||||
if (packet) {
|
||||
strcpy((char *) packet->data, message);
|
||||
packet->length = strlen(message);
|
||||
|
||||
conn = csp_connect(CSP_PRIO_NORM, other, PORT, 1000, CSP_O_NONE);
|
||||
printf("Sending: %s\r\n", message);
|
||||
if (!conn || !csp_send(conn, packet, 1000))
|
||||
return -1;
|
||||
csp_close(conn);
|
||||
}
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
close(rx_channel);
|
||||
close(tx_channel);
|
||||
|
||||
return 0;
|
||||
}
|
225
thirdparty/libcsp/examples/csp_if_fifo_windows.c
vendored
Normal file
225
thirdparty/libcsp/examples/csp_if_fifo_windows.c
vendored
Normal file
@ -0,0 +1,225 @@
|
||||
#include <Windows.h>
|
||||
#include <process.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <stddef.h>
|
||||
#undef interface
|
||||
|
||||
#include <csp/csp.h>
|
||||
#include <csp/csp_interface.h>
|
||||
|
||||
#define PIPE_BUFSIZE 1024
|
||||
|
||||
#define TYPE_SERVER 1
|
||||
#define TYPE_CLIENT 2
|
||||
#define PORT 10
|
||||
#define BUF_SIZE 250
|
||||
|
||||
|
||||
static LPCTSTR pipeName = TEXT("\\\\.\\pipe\\CSP_Pipe");
|
||||
|
||||
static HANDLE pipe = INVALID_HANDLE_VALUE;
|
||||
|
||||
unsigned WINAPI fifo_rx(void *);
|
||||
unsigned WINAPI pipe_listener(void *);
|
||||
|
||||
void printError(void);
|
||||
|
||||
int csp_fifo_tx(csp_packet_t *packet, uint32_t timeout);
|
||||
|
||||
csp_iface_t csp_if_fifo = {
|
||||
.name = "fifo",
|
||||
.nexthop = csp_fifo_tx,
|
||||
.mtu = BUF_SIZE,
|
||||
};
|
||||
|
||||
int csp_fifo_tx(csp_packet_t *packet, uint32_t timeout) {
|
||||
printf("csp_fifo_tx tid: %lu\n", GetCurrentThreadId());
|
||||
DWORD expectedSent = packet->length + sizeof(uint32_t) + sizeof(uint16_t);
|
||||
DWORD actualSent;
|
||||
/* Write packet to fifo */
|
||||
if( !WriteFile(pipe, &packet->length, expectedSent, &actualSent, NULL)
|
||||
|| actualSent != expectedSent ) {
|
||||
printError();
|
||||
}
|
||||
|
||||
csp_buffer_free(packet);
|
||||
return CSP_ERR_NONE;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int me, other, type;
|
||||
char *message = "Testing CSP";
|
||||
csp_socket_t *sock = NULL;
|
||||
csp_conn_t *conn = NULL;
|
||||
csp_packet_t *packet = NULL;
|
||||
|
||||
/* Run as either server or client */
|
||||
if (argc != 2) {
|
||||
printf("usage: server <server/client>\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Set type */
|
||||
if (strcmp(argv[1], "server") == 0) {
|
||||
me = 1;
|
||||
other = 2;
|
||||
type = TYPE_SERVER;
|
||||
} else if (strcmp(argv[1], "client") == 0) {
|
||||
me = 2;
|
||||
other = 1;
|
||||
type = TYPE_CLIENT;
|
||||
} else {
|
||||
printf("Invalid type. Must be either 'server' or 'client'\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Init CSP and CSP buffer system */
|
||||
if (csp_init(me) != CSP_ERR_NONE || csp_buffer_init(10, 300) != CSP_ERR_NONE) {
|
||||
printf("Failed to init CSP\r\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if( type == TYPE_SERVER ) {
|
||||
_beginthreadex(NULL, 0, pipe_listener, NULL, 0, 0);
|
||||
} else {
|
||||
pipe = CreateFile(
|
||||
pipeName,
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
0,
|
||||
NULL,
|
||||
OPEN_EXISTING,
|
||||
0,
|
||||
NULL);
|
||||
if( pipe == INVALID_HANDLE_VALUE ) {
|
||||
printError();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Set default route and start router */
|
||||
csp_route_set(CSP_DEFAULT_ROUTE, &csp_if_fifo, CSP_NODE_MAC);
|
||||
csp_route_start_task(0, 0);
|
||||
|
||||
/* Create socket and listen for incoming connections */
|
||||
if (type == TYPE_SERVER) {
|
||||
sock = csp_socket(CSP_SO_NONE);
|
||||
csp_bind(sock, PORT);
|
||||
csp_listen(sock, 5);
|
||||
}
|
||||
|
||||
/* Super loop */
|
||||
while (1) {
|
||||
if (type == TYPE_SERVER) {
|
||||
/* Process incoming packet */
|
||||
conn = csp_accept(sock, 1000);
|
||||
if (conn) {
|
||||
packet = csp_read(conn, 0);
|
||||
if (packet)
|
||||
printf("Received: %s\r\n", packet->data);
|
||||
csp_buffer_free(packet);
|
||||
csp_close(conn);
|
||||
}
|
||||
} else {
|
||||
/* Send a new packet */
|
||||
packet = csp_buffer_get(strlen(message));
|
||||
if (packet) {
|
||||
strcpy((char *) packet->data, message);
|
||||
packet->length = strlen(message);
|
||||
|
||||
conn = csp_connect(CSP_PRIO_NORM, other, PORT, 1000, CSP_O_NONE);
|
||||
printf("Sending: %s\r\n", message);
|
||||
if (!conn || !csp_send(conn, packet, 1000))
|
||||
return -1;
|
||||
csp_close(conn);
|
||||
Sleep(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void printError(void) {
|
||||
LPTSTR messageBuffer = NULL;
|
||||
DWORD errorCode = GetLastError();
|
||||
DWORD formatMessageRet;
|
||||
formatMessageRet = FormatMessage(
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
NULL,
|
||||
errorCode,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(LPTSTR)&messageBuffer,
|
||||
0,
|
||||
NULL);
|
||||
|
||||
if( !formatMessageRet ) {
|
||||
wprintf(L"FormatMessage error, code: %lu\n", GetLastError());
|
||||
return;
|
||||
}
|
||||
|
||||
printf("%s\n", messageBuffer);
|
||||
LocalFree(messageBuffer);
|
||||
}
|
||||
|
||||
unsigned WINAPI pipe_listener(void *parameters) {
|
||||
while(1) {
|
||||
HANDLE pipe = CreateNamedPipe(
|
||||
pipeName,
|
||||
PIPE_ACCESS_DUPLEX,
|
||||
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
|
||||
PIPE_UNLIMITED_INSTANCES,
|
||||
PIPE_BUFSIZE,
|
||||
PIPE_BUFSIZE,
|
||||
0,
|
||||
NULL);
|
||||
BOOL clientConnected;
|
||||
if( pipe == INVALID_HANDLE_VALUE ) {
|
||||
printf("Error creating named pipe. Code %lu\n", GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
// True if client connects *after* server called ConnectNamedPipe
|
||||
// or *between* CreateNamedPipe and ConnectNamedPipe
|
||||
clientConnected =
|
||||
ConnectNamedPipe(pipe, NULL) ? TRUE : GetLastError()==ERROR_PIPE_CONNECTED;
|
||||
printf("Client connected!\n");
|
||||
|
||||
if( !clientConnected ) {
|
||||
printf("Failure while listening for clients. Code %lu\n", GetLastError());
|
||||
CloseHandle(pipe);
|
||||
return -1;
|
||||
}
|
||||
printf("Create client thread\n");
|
||||
_beginthreadex(NULL, 0, fifo_rx, (PVOID)pipe, 0, 0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned WINAPI fifo_rx(void *handle) {
|
||||
printf("fifo_rx tid: %lu\n", GetCurrentThreadId());
|
||||
HANDLE pipe = (HANDLE) handle;
|
||||
csp_packet_t *buf = csp_buffer_get(BUF_SIZE);
|
||||
DWORD bytesRead;
|
||||
BOOL readSuccess;
|
||||
|
||||
while(1) {
|
||||
readSuccess =
|
||||
ReadFile(pipe, &buf->length, BUF_SIZE, &bytesRead, NULL);
|
||||
if( !readSuccess || bytesRead == 0 ) {
|
||||
csp_buffer_free(buf);
|
||||
printError();
|
||||
break;
|
||||
}
|
||||
csp_qfifo_write(buf, &csp_if_fifo, NULL);
|
||||
buf = csp_buffer_get(BUF_SIZE);
|
||||
}
|
||||
printf("Closing pipe to client\n");
|
||||
CloseHandle(pipe);
|
||||
|
||||
return 0;
|
||||
}
|
151
thirdparty/libcsp/examples/kiss.c
vendored
Normal file
151
thirdparty/libcsp/examples/kiss.c
vendored
Normal file
@ -0,0 +1,151 @@
|
||||
/**
|
||||
* Build this example on linux with:
|
||||
* ./waf configure --enable-examples --enable-if-kiss --with-driver-usart=linux --enable-crc32 clean build
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <csp/csp.h>
|
||||
#include <csp/interfaces/csp_if_kiss.h>
|
||||
|
||||
#include <csp/drivers/usart.h>
|
||||
#include <csp/arch/csp_thread.h>
|
||||
|
||||
#define PORT 10
|
||||
#define MY_ADDRESS 1
|
||||
|
||||
#define SERVER_TIDX 0
|
||||
#define CLIENT_TIDX 1
|
||||
#define USART_HANDLE 0
|
||||
|
||||
CSP_DEFINE_TASK(task_server) {
|
||||
int running = 1;
|
||||
csp_socket_t *socket = csp_socket(CSP_SO_NONE);
|
||||
csp_conn_t *conn;
|
||||
csp_packet_t *packet;
|
||||
csp_packet_t *response;
|
||||
|
||||
response = csp_buffer_get(sizeof(csp_packet_t) + 2);
|
||||
if( response == NULL ) {
|
||||
fprintf(stderr, "Could not allocate memory for response packet!\n");
|
||||
return CSP_TASK_RETURN;
|
||||
}
|
||||
response->data[0] = 'O';
|
||||
response->data[1] = 'K';
|
||||
response->length = 2;
|
||||
|
||||
csp_bind(socket, CSP_ANY);
|
||||
csp_listen(socket, 5);
|
||||
|
||||
printf("Server task started\r\n");
|
||||
|
||||
while(running) {
|
||||
if( (conn = csp_accept(socket, 10000)) == NULL ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
while( (packet = csp_read(conn, 100)) != NULL ) {
|
||||
switch( csp_conn_dport(conn) ) {
|
||||
case PORT:
|
||||
if( packet->data[0] == 'q' )
|
||||
running = 0;
|
||||
csp_buffer_free(packet);
|
||||
csp_send(conn, response, 1000);
|
||||
break;
|
||||
default:
|
||||
csp_service_handler(conn, packet);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
csp_close(conn);
|
||||
}
|
||||
|
||||
csp_buffer_free(response);
|
||||
|
||||
return CSP_TASK_RETURN;
|
||||
}
|
||||
|
||||
CSP_DEFINE_TASK(task_client) {
|
||||
|
||||
char outbuf = 'q';
|
||||
char inbuf[3] = {0};
|
||||
int pingResult;
|
||||
|
||||
for(int i = 50; i <= 200; i+= 50) {
|
||||
pingResult = csp_ping(MY_ADDRESS, 1000, 100, CSP_O_NONE);
|
||||
printf("Ping with payload of %d bytes, took %d ms\n", i, pingResult);
|
||||
csp_sleep_ms(1000);
|
||||
}
|
||||
csp_ps(MY_ADDRESS, 1000);
|
||||
csp_sleep_ms(1000);
|
||||
csp_memfree(MY_ADDRESS, 1000);
|
||||
csp_sleep_ms(1000);
|
||||
csp_buf_free(MY_ADDRESS, 1000);
|
||||
csp_sleep_ms(1000);
|
||||
csp_uptime(MY_ADDRESS, 1000);
|
||||
csp_sleep_ms(1000);
|
||||
|
||||
csp_transaction(0, MY_ADDRESS, PORT, 1000, &outbuf, 1, inbuf, 2);
|
||||
printf("Quit response from server: %s\n", inbuf);
|
||||
|
||||
return CSP_TASK_RETURN;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
csp_debug_toggle_level(CSP_PACKET);
|
||||
csp_debug_toggle_level(CSP_INFO);
|
||||
|
||||
csp_buffer_init(10, 300);
|
||||
csp_init(MY_ADDRESS);
|
||||
|
||||
struct usart_conf conf;
|
||||
|
||||
#if defined(CSP_WINDOWS)
|
||||
conf.device = argc != 2 ? "COM4" : argv[1];
|
||||
conf.baudrate = CBR_9600;
|
||||
conf.databits = 8;
|
||||
conf.paritysetting = NOPARITY;
|
||||
conf.stopbits = ONESTOPBIT;
|
||||
conf.checkparity = FALSE;
|
||||
#elif defined(CSP_POSIX)
|
||||
conf.device = argc != 2 ? "/dev/ttyUSB0" : argv[1];
|
||||
conf.baudrate = 500000;
|
||||
#elif defined(CSP_MACOSX)
|
||||
conf.device = argc != 2 ? "/dev/tty.usbserial-FTSM9EGE" : argv[1];
|
||||
conf.baudrate = 115200;
|
||||
#endif
|
||||
|
||||
/* Run USART init */
|
||||
usart_init(&conf);
|
||||
|
||||
/* Setup CSP interface */
|
||||
static csp_iface_t csp_if_kiss;
|
||||
static csp_kiss_handle_t csp_kiss_driver;
|
||||
csp_kiss_init(&csp_if_kiss, &csp_kiss_driver, usart_putc, usart_insert, "KISS");
|
||||
|
||||
/* Setup callback from USART RX to KISS RS */
|
||||
void my_usart_rx(uint8_t * buf, int len, void * pxTaskWoken) {
|
||||
csp_kiss_rx(&csp_if_kiss, buf, len, pxTaskWoken);
|
||||
}
|
||||
usart_set_callback(my_usart_rx);
|
||||
|
||||
csp_route_set(MY_ADDRESS, &csp_if_kiss, CSP_NODE_MAC);
|
||||
csp_route_start_task(0, 0);
|
||||
|
||||
csp_conn_print_table();
|
||||
csp_route_print_table();
|
||||
csp_route_print_interfaces();
|
||||
|
||||
csp_thread_handle_t handle_server;
|
||||
csp_thread_create(task_server, "SERVER", 1000, NULL, 0, &handle_server);
|
||||
csp_thread_handle_t handle_client;
|
||||
csp_thread_create(task_client, "CLIENT", 1000, NULL, 0, &handle_client);
|
||||
|
||||
/* Wait for program to terminate (ctrl + c) */
|
||||
while(1) {
|
||||
csp_sleep_ms(1000000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
42
thirdparty/libcsp/examples/python_bindings_example_client.py
vendored
Normal file
42
thirdparty/libcsp/examples/python_bindings_example_client.py
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
# libcsp must be build with at least these options to run this example client:
|
||||
# ./waf distclean configure build --enable-bindings --enable-crc32 --enable-rdp --enable-if-zmq --with-driver-usart=linux --enable-if-kiss --enable-xtea --enable-if-can --enable-can-socketcan --enable-hmac --enable-examples
|
||||
|
||||
# Can be run from root of libcsp like this:
|
||||
# LD_LIBRARY_PATH=build PYTHONPATH=bindings/python:build python examples/python_bindings_example_client.py
|
||||
#
|
||||
|
||||
import os
|
||||
import time
|
||||
import libcsp as csp
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
csp.buffer_init(10, 300)
|
||||
csp.init(28)
|
||||
csp.zmqhub_init(28, "localhost")
|
||||
csp.rtable_set(27, 5, "ZMQHUB")
|
||||
csp.route_start_task()
|
||||
|
||||
## allow router task startup
|
||||
time.sleep(1)
|
||||
|
||||
## cmp_ident
|
||||
(rc, host, model, rev, date, time) = csp.cmp_ident(27)
|
||||
if rc == csp.CSP_ERR_NONE:
|
||||
print (host, model, rev, date, time)
|
||||
else:
|
||||
print ("error in cmp_ident, rc=%i" % (rc))
|
||||
|
||||
## transaction
|
||||
outbuf = bytearray().fromhex('01')
|
||||
inbuf = bytearray(1)
|
||||
print ("using csp_transaction to send a single byte")
|
||||
if csp.transaction(0, 27, 10, 1000, outbuf, inbuf) < 1:
|
||||
print ("csp_transaction failed")
|
||||
else:
|
||||
print ("got reply, data=" + ''.join('{:02x}'.format(x) for x in inbuf))
|
||||
|
||||
|
30
thirdparty/libcsp/examples/python_bindings_example_client_can.py
vendored
Normal file
30
thirdparty/libcsp/examples/python_bindings_example_client_can.py
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
# libcsp must be build with at least these options to run this example client:
|
||||
# ./waf distclean configure build --enable-bindings --enable-crc32 --enable-rdp --enable-if-zmq --with-driver-usart=linux --enable-if-kiss --enable-xtea --enable-if-can --enable-can-socketcan --enable-hmac --enable-examples
|
||||
|
||||
# Can be run from root of libcsp like this:
|
||||
# LD_LIBRARY_PATH=build PYTHONPATH=bindings/python:build python examples/python_bindings_example_client.py
|
||||
#
|
||||
|
||||
import os
|
||||
import time
|
||||
import libcsp as csp
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
csp.buffer_init(10, 300)
|
||||
csp.init(28)
|
||||
csp.can_socketcan_init("can0")
|
||||
csp.rtable_set(4, 5, "CAN")
|
||||
csp.route_start_task()
|
||||
|
||||
## allow router task startup
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
node = 4
|
||||
if csp.ping(node) < 0:
|
||||
print ("Unable to ping node %d"%(node))
|
||||
|
72
thirdparty/libcsp/examples/python_bindings_example_server.py
vendored
Normal file
72
thirdparty/libcsp/examples/python_bindings_example_server.py
vendored
Normal file
@ -0,0 +1,72 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
# libcsp must be build with at least these options to run this example server:
|
||||
# ./waf distclean configure build --enable-bindings --enable-crc32 --enable-rdp --enable-if-zmq --with-driver-usart=linux --enable-if-kiss --enable-xtea --enable-if-can --enable-can-socketcan --enable-hmac --enable-examples
|
||||
|
||||
# Can be run from root of libcsp like this:
|
||||
# LD_LIBRARY_PATH=build PYTHONPATH=bindings/python:build python examples/python_bindings_example_server.py
|
||||
#
|
||||
|
||||
import os
|
||||
import time
|
||||
import sys
|
||||
import libcsp as csp
|
||||
import subprocess
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
# start a zmqproxy to transport messages to and from the client
|
||||
zmqp = subprocess.Popen('build/zmqproxy')
|
||||
|
||||
# init csp
|
||||
csp.buffer_init(10, 300)
|
||||
csp.init(27)
|
||||
csp.zmqhub_init(27, "localhost")
|
||||
csp.rtable_set(28, 5, "ZMQHUB")
|
||||
csp.route_start_task()
|
||||
|
||||
# set identity
|
||||
csp.set_hostname("test_service")
|
||||
csp.set_model("bindings")
|
||||
csp.set_revision("1.2.3")
|
||||
|
||||
# and read it back
|
||||
print (csp.get_hostname())
|
||||
print (csp.get_model())
|
||||
print (csp.get_revision())
|
||||
|
||||
# start listening for packets...
|
||||
sock = csp.socket()
|
||||
csp.bind(sock, csp.CSP_ANY)
|
||||
csp.listen(sock)
|
||||
while True:
|
||||
conn = csp.accept(sock)
|
||||
if not conn:
|
||||
continue
|
||||
|
||||
print ("connection: source=%i:%i, dest=%i:%i" % (csp.conn_src(conn),
|
||||
csp.conn_sport(conn),
|
||||
csp.conn_dst(conn),
|
||||
csp.conn_dport(conn)))
|
||||
|
||||
while True:
|
||||
packet = csp.read(conn)
|
||||
if not packet:
|
||||
break
|
||||
|
||||
if csp.conn_dport(conn) == 10:
|
||||
data = bytearray(csp.packet_get_data(packet))
|
||||
length = csp.packet_get_length(packet)
|
||||
print ("got packet, len=" + str(length) + ", data=" + ''.join('{:02x}'.format(x) for x in data))
|
||||
|
||||
data[0] = data[0] + 1
|
||||
reply_packet = csp.buffer_get(1)
|
||||
if reply_packet:
|
||||
csp.packet_set_data(reply_packet, data)
|
||||
csp.sendto_reply(packet, reply_packet, csp.CSP_O_NONE)
|
||||
|
||||
csp.buffer_free(packet)
|
||||
else:
|
||||
csp.service_handler(conn, packet)
|
||||
csp.close(conn)
|
||||
|
200
thirdparty/libcsp/examples/simple.c
vendored
Normal file
200
thirdparty/libcsp/examples/simple.c
vendored
Normal file
@ -0,0 +1,200 @@
|
||||
/*
|
||||
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
|
||||
Copyright (C) 2012 GomSpace ApS (http://www.gomspace.com)
|
||||
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <csp/csp.h>
|
||||
|
||||
/* Using un-exported header file.
|
||||
* This is allowed since we are still in libcsp */
|
||||
#include <csp/arch/csp_thread.h>
|
||||
|
||||
/** Example defines */
|
||||
#define MY_ADDRESS 1 // Address of local CSP node
|
||||
#define MY_PORT 10 // Port to send test traffic to
|
||||
|
||||
CSP_DEFINE_TASK(task_server) {
|
||||
|
||||
/* Create socket without any socket options */
|
||||
csp_socket_t *sock = csp_socket(CSP_SO_NONE);
|
||||
|
||||
/* Bind all ports to socket */
|
||||
csp_bind(sock, CSP_ANY);
|
||||
|
||||
/* Create 10 connections backlog queue */
|
||||
csp_listen(sock, 10);
|
||||
|
||||
/* Pointer to current connection and packet */
|
||||
csp_conn_t *conn;
|
||||
csp_packet_t *packet;
|
||||
|
||||
/* Process incoming connections */
|
||||
while (1) {
|
||||
|
||||
/* Wait for connection, 10000 ms timeout */
|
||||
if ((conn = csp_accept(sock, 10000)) == NULL)
|
||||
continue;
|
||||
|
||||
/* Read packets. Timout is 100 ms */
|
||||
while ((packet = csp_read(conn, 100)) != NULL) {
|
||||
switch (csp_conn_dport(conn)) {
|
||||
case MY_PORT:
|
||||
/* Process packet here */
|
||||
printf("Packet received on MY_PORT: %s\r\n", (char *) packet->data);
|
||||
csp_buffer_free(packet);
|
||||
break;
|
||||
|
||||
default:
|
||||
/* Let the service handler reply pings, buffer use, etc. */
|
||||
csp_service_handler(conn, packet);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Close current connection, and handle next */
|
||||
csp_close(conn);
|
||||
|
||||
}
|
||||
|
||||
return CSP_TASK_RETURN;
|
||||
|
||||
}
|
||||
|
||||
CSP_DEFINE_TASK(task_client) {
|
||||
|
||||
csp_packet_t * packet;
|
||||
csp_conn_t * conn;
|
||||
|
||||
while (1) {
|
||||
|
||||
/**
|
||||
* Try ping
|
||||
*/
|
||||
|
||||
csp_sleep_ms(1000);
|
||||
|
||||
int result = csp_ping(MY_ADDRESS, 100, 100, CSP_O_NONE);
|
||||
printf("Ping result %d [ms]\r\n", result);
|
||||
|
||||
csp_sleep_ms(1000);
|
||||
|
||||
/**
|
||||
* Try data packet to server
|
||||
*/
|
||||
|
||||
/* Get packet buffer for data */
|
||||
packet = csp_buffer_get(100);
|
||||
if (packet == NULL) {
|
||||
/* Could not get buffer element */
|
||||
printf("Failed to get buffer element\n");
|
||||
return CSP_TASK_RETURN;
|
||||
}
|
||||
|
||||
/* Connect to host HOST, port PORT with regular UDP-like protocol and 1000 ms timeout */
|
||||
conn = csp_connect(CSP_PRIO_NORM, MY_ADDRESS, MY_PORT, 1000, CSP_O_NONE);
|
||||
if (conn == NULL) {
|
||||
/* Connect failed */
|
||||
printf("Connection failed\n");
|
||||
/* Remember to free packet buffer */
|
||||
csp_buffer_free(packet);
|
||||
return CSP_TASK_RETURN;
|
||||
}
|
||||
|
||||
/* Copy dummy data to packet */
|
||||
const char *msg = "Hello World";
|
||||
strcpy((char *) packet->data, msg);
|
||||
|
||||
/* Set packet length */
|
||||
packet->length = strlen(msg);
|
||||
|
||||
/* Send packet */
|
||||
if (!csp_send(conn, packet, 1000)) {
|
||||
/* Send failed */
|
||||
printf("Send failed\n");
|
||||
csp_buffer_free(packet);
|
||||
}
|
||||
|
||||
/* Close connection */
|
||||
csp_close(conn);
|
||||
|
||||
}
|
||||
|
||||
return CSP_TASK_RETURN;
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
|
||||
/**
|
||||
* Initialise CSP,
|
||||
* No physical interfaces are initialised in this example,
|
||||
* so only the loopback interface is registered.
|
||||
*/
|
||||
|
||||
/* Init buffer system with 10 packets of maximum 300 bytes each */
|
||||
printf("Initialising CSP\r\n");
|
||||
csp_buffer_init(5, 300);
|
||||
|
||||
/* Init CSP with address MY_ADDRESS */
|
||||
csp_init(MY_ADDRESS);
|
||||
|
||||
/* Start router task with 500 word stack, OS task priority 1 */
|
||||
csp_route_start_task(500, 1);
|
||||
|
||||
/* Enable debug output from CSP */
|
||||
if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) {
|
||||
printf("Debug enabed\r\n");
|
||||
csp_debug_toggle_level(3);
|
||||
csp_debug_toggle_level(4);
|
||||
|
||||
printf("Conn table\r\n");
|
||||
csp_conn_print_table();
|
||||
|
||||
printf("Route table\r\n");
|
||||
csp_route_print_table();
|
||||
|
||||
printf("Interfaces\r\n");
|
||||
csp_route_print_interfaces();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise example threads, using pthreads.
|
||||
*/
|
||||
|
||||
/* Server */
|
||||
printf("Starting Server task\r\n");
|
||||
csp_thread_handle_t handle_server;
|
||||
csp_thread_create(task_server, "SERVER", 1000, NULL, 0, &handle_server);
|
||||
|
||||
/* Client */
|
||||
printf("Starting Client task\r\n");
|
||||
csp_thread_handle_t handle_client;
|
||||
csp_thread_create(task_client, "SERVER", 1000, NULL, 0, &handle_client);
|
||||
|
||||
/* Wait for execution to end (ctrl+c) */
|
||||
while(1) {
|
||||
csp_sleep_ms(100000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
82
thirdparty/libcsp/examples/zmqproxy.c
vendored
Normal file
82
thirdparty/libcsp/examples/zmqproxy.c
vendored
Normal file
@ -0,0 +1,82 @@
|
||||
#include <zmq.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include <malloc.h>
|
||||
#include <unistd.h>
|
||||
#include <csp/csp.h>
|
||||
|
||||
static void * task_capture(void *ctx) {
|
||||
|
||||
/* Subscriber (RX) */
|
||||
void *subscriber = zmq_socket(ctx, ZMQ_SUB);
|
||||
assert(zmq_connect(subscriber, "tcp://localhost:7000") == 0);
|
||||
assert(zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, "", 0) == 0);
|
||||
|
||||
while (1) {
|
||||
zmq_msg_t msg;
|
||||
zmq_msg_init_size(&msg, 1024);
|
||||
|
||||
/* Receive data */
|
||||
if (zmq_msg_recv(&msg, subscriber, 0) < 0) {
|
||||
zmq_msg_close(&msg);
|
||||
csp_log_error("ZMQ: %s\r\n", zmq_strerror(zmq_errno()));
|
||||
continue;
|
||||
}
|
||||
|
||||
int datalen = zmq_msg_size(&msg);
|
||||
if (datalen < 5) {
|
||||
csp_log_warn("ZMQ: Too short datalen: %u\r\n", datalen);
|
||||
while(zmq_msg_recv(&msg, subscriber, ZMQ_NOBLOCK) > 0)
|
||||
zmq_msg_close(&msg);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Create new csp packet */
|
||||
csp_packet_t * packet = malloc(1024);
|
||||
if (packet == NULL) {
|
||||
zmq_msg_close(&msg);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Copy the data from zmq to csp */
|
||||
char * satidptr = ((char *) &packet->id) - 1;
|
||||
memcpy(satidptr, zmq_msg_data(&msg), datalen);
|
||||
packet->length = datalen - 4 - 1;
|
||||
|
||||
printf("Input: Src %u, Dst %u, Dport %u, Sport %u, Pri %u, Flags 0x%02X, Size %"PRIu16"\r\n",
|
||||
packet->id.src, packet->id.dst, packet->id.dport,
|
||||
packet->id.sport, packet->id.pri, packet->id.flags, packet->length);
|
||||
|
||||
free(packet);
|
||||
zmq_msg_close(&msg);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
|
||||
/**
|
||||
* ZMQ PROXY
|
||||
*/
|
||||
void * ctx = zmq_ctx_new();
|
||||
assert(ctx);
|
||||
|
||||
void *frontend = zmq_socket(ctx, ZMQ_XSUB);
|
||||
assert(frontend);
|
||||
assert(zmq_bind (frontend, "tcp://*:6000") == 0);
|
||||
|
||||
void *backend = zmq_socket(ctx, ZMQ_XPUB);
|
||||
assert(backend);
|
||||
assert(zmq_bind(backend, "tcp://*:7000") == 0);
|
||||
|
||||
pthread_t capworker;
|
||||
pthread_create(&capworker, NULL, task_capture, ctx);
|
||||
|
||||
printf("Starting ZMQproxy\r\n");
|
||||
zmq_proxy(frontend, backend, NULL);
|
||||
|
||||
printf("Closing ZMQproxy\r\n");
|
||||
zmq_ctx_destroy(ctx);
|
||||
return 0;
|
||||
|
||||
}
|
Reference in New Issue
Block a user