Skip to content

Commit

Permalink
Merge pull request #4 from EmOne/develop
Browse files Browse the repository at this point in the history
Merge from official
  • Loading branch information
Apaisal authored Sep 22, 2024
2 parents ba8684d + 6ba9cd9 commit 95dce86
Show file tree
Hide file tree
Showing 21 changed files with 100 additions and 51 deletions.
14 changes: 11 additions & 3 deletions .github/workflows/build-test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Setup packages on Linux
run: |
sudo apt-get update
sudo apt-get install libzmq3-dev libsocketcan-dev
sudo apt-get install libzmq3-dev libsocketcan-dev socat
- name: Setup build system packages on Linux
run: |
Expand Down Expand Up @@ -59,6 +59,14 @@ jobs:
- name: Run ZMQ Python binding Test
run: |
build/examples/zmqproxy &
PYTHONPATH=builddir python3 examples/python_bindings_example_server.py &
PYTHONPATH=builddir python3 examples/python_bindings_example_client.py -z localhost -s 27 -a 2
PYTHONPATH=builddir python3 examples/python_bindings_example_server.py -z localhost -a 3 &
PYTHONPATH=builddir python3 examples/python_bindings_example_client.py -z localhost -s 3 -a 2
pkill zmqproxy
- name: Run KISS Python binding Test
run: |
socat -d -d -d pty,raw,echo=0,link=/tmp/pty1 pty,raw,echo=0,link=/tmp/pty2 &
sleep 1
PYTHONPATH=builddir python3 examples/python_bindings_example_server.py -k /tmp/pty2 -a 1 &
PYTHONPATH=builddir python3 examples/python_bindings_example_client.py -k /tmp/pty1 -a 2 -s 1
pkill socat
24 changes: 23 additions & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ jobs:
if: ${{ runner.os == 'Linux' }}
run: |
sudo apt-get update
sudo apt-get install libzmq3-dev libsocketcan-dev socat
sudo apt-get install libzmq3-dev libsocketcan-dev socat iproute2
sudo apt-get install linux-modules-extra-$(uname -r)
- name: Setup build system packages on Linux
if: ${{ runner.os == 'Linux' && matrix.buildsystem != 'waf' }}
Expand Down Expand Up @@ -81,3 +82,24 @@ jobs:
./build/examples/csp_client -z localhost -a 2 -C 1 -T 10 &
./build/examples/csp_server -z localhost -a 1 -t
pkill zmqproxy
- name: Setup vcan0
run: |
sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0
echo "Waiting for vcan0 to be up..."
while ! ip -br link show vcan0 | grep -q "UP"; do
sleep 0.1
done
echo "vcan0 is up"
- name: Run CAN Server Test
run: |
./build/examples/csp_server -c vcan0 -a 1 -T 10 &
./build/examples/csp_client -c vcan0 -a 2 -C 1 -t
- name: Run CAN Client Test
run: |
./build/examples/csp_client -c vcan0 -a 2 -C 1 -T 10 &
./build/examples/csp_server -c vcan0 -a 1 -t
2 changes: 1 addition & 1 deletion contrib/zephyr/samples/server-client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ int main(void) {
.stopbits = 1,
.paritysetting = 0,
};
int error = csp_usart_open_and_add_kiss_interface(&conf, CSP_IF_KISS_DEFAULT_NAME, &default_iface);
int error = csp_usart_open_and_add_kiss_interface(&conf, CSP_IF_KISS_DEFAULT_NAME, addr, &default_iface);
if (error != CSP_ERR_NONE) {
LOG_ERR("failed to add KISS interface [%s], error: %d", kiss_device, error);
exit(1);
Expand Down
3 changes: 1 addition & 2 deletions examples/csp_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,11 @@ csp_iface_t * add_interface(enum DeviceType device_type, const char * device_nam
.stopbits = 1,
.paritysetting = 0,
};
int error = csp_usart_open_and_add_kiss_interface(&conf, CSP_IF_KISS_DEFAULT_NAME, &default_iface);
int error = csp_usart_open_and_add_kiss_interface(&conf, CSP_IF_KISS_DEFAULT_NAME, client_address, &default_iface);
if (error != CSP_ERR_NONE) {
csp_print("failed to add KISS interface [%s], error: %d\n", device_name, error);
exit(1);
}
default_iface->addr = client_address;
default_iface->is_default = 1;
}

Expand Down
3 changes: 1 addition & 2 deletions examples/csp_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,11 @@ csp_iface_t * add_interface(enum DeviceType device_type, const char * device_nam
.stopbits = 1,
.paritysetting = 0,
};
int error = csp_usart_open_and_add_kiss_interface(&conf, CSP_IF_KISS_DEFAULT_NAME, &default_iface);
int error = csp_usart_open_and_add_kiss_interface(&conf, CSP_IF_KISS_DEFAULT_NAME, server_address, &default_iface);
if (error != CSP_ERR_NONE) {
csp_print("failed to add KISS interface [%s], error: %d\n", device_name, error);
exit(1);
}
default_iface->addr = server_address;
default_iface->is_default = 1;
}

Expand Down
11 changes: 8 additions & 3 deletions examples/python_bindings_example_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
import libcsp_py3 as libcsp


def getOptions():
def get_options():
parser = argparse.ArgumentParser(description="Parses command.")
parser.add_argument("-a", "--address", type=int, default=10, help="Local CSP address")
parser.add_argument("-c", "--can", help="Add CAN interface")
parser.add_argument("-k", "--kiss", help="Add KISS interface")
parser.add_argument("-z", "--zmq", help="Add ZMQ interface")
parser.add_argument("-s", "--server-address", type=int, default=27, help="Server address")
parser.add_argument("-R", "--routing-table", help="Routing table")
Expand All @@ -30,7 +31,7 @@ def getOptions():

if __name__ == "__main__":

options = getOptions()
options = get_options()

#initialize libcsp with params:
# options.address - CSP address of the system (default=1)
Expand All @@ -53,7 +54,11 @@ def getOptions():
# Format: \<address\>[/mask] \<interface\> [via][, next entry]
# Examples: "0/0 CAN, 8 KISS, 10 I2C 10", same as "0/0 CAN, 8/5 KISS, 10/5 I2C 10"
libcsp.rtable_load("0/0 ZMQHUB")


if options.kiss:
libcsp.kiss_init(options.kiss, options.address)
libcsp.rtable_load("0/0 KISS")

if options.routing_table:
# same format/use as line above
libcsp.rtable_load(options.routing_table)
Expand Down
45 changes: 28 additions & 17 deletions examples/python_bindings_example_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@
import sys
import threading

import argparse

import libcsp_py3 as libcsp

def get_options():
parser = argparse.ArgumentParser(description="Parses command.")
parser.add_argument("-a", "--address", type=int, default=10, help="Local CSP address")
parser.add_argument("-z", "--zmq", help="Add ZMQ interface")
parser.add_argument("-k", "--kiss", help="Add KISS interface")
return parser.parse_args()


def csp_server():
# parameters: {options} - bit flag corresponding to socket options (see "include\csp\csp_types.h" lines 167-180)
Expand Down Expand Up @@ -58,21 +67,21 @@ def csp_server():
# extract the data payload from the packet
# see "include\csp\csp_types.h" line 215-239 for packet structure
data = bytearray(libcsp.packet_get_data(packet))

# get length of the data (not the whole packet, just the data length)
length = libcsp.packet_get_length(packet)
print ("got packet, len=" + str(length) + ", data=" + ''.join('{:02x}'.format(x) for x in data))

# send back "input data + 1"
data[0] = data[0] + 1

# free up a buffer to hold the reply
# parameters: {buffer size (# of 4-byte doublewords)}
reply = libcsp.buffer_get(0)

# store the data into the reply buffer
libcsp.packet_set_data(reply, data)

# Send packet as a reply
# uses the info (address/port) from the original packet to reply
# parameters:
Expand All @@ -92,6 +101,7 @@ def csp_server():

if __name__ == "__main__":

options = get_options()
#initialize libcsp with params:
# 27 - CSP address of the system (default=1)
# "test_service" - Host name, returned by CSP identity requests
Expand All @@ -100,18 +110,19 @@ def csp_server():
# See "include\csp\csp.h" - lines 42-80 for more detail
# See "src\bindings\python\pycsp.c" - lines 128-156 for more detail
libcsp.init("test_service", "bindings", "1.2.3")

# init zmqhub with parameters: {address (using 255 means all addresses)} {host name/ip}
# subscribe and publish endpoints are created on the default ports using the {host}
# subscribe port = 6000, subscribe port = 7000
libcsp.zmqhub_init(27, "localhost")

# params:
# {address} - dest address/node
# {netmask} - number of bits in netmask
# {interface name} - name of interface
# optional{via} - associated with address
libcsp.rtable_set(0, 0, "ZMQHUB")

if options.zmq:
# add ZMQ interface - (address, host)
# creates publish and subrcribe endpoints from the host
libcsp.zmqhub_init(options.address, options.zmq)

# Format: \<address\>[/mask] \<interface\> [via][, next entry]
# Examples: "0/0 CAN, 8 KISS, 10 I2C 10", same as "0/0 CAN, 8/5 KISS, 10/5 I2C 10"
libcsp.rtable_load("0/0 ZMQHUB")

if options.kiss:
libcsp.kiss_init(options.kiss, options.address)
libcsp.rtable_load("0/0 KISS")

# Parameters: {priority} - 0 (critical), 1 (high), 2 (norm), 3 (low) ---- default=2
# Start the router task - creates routing thread
Expand Down
3 changes: 2 additions & 1 deletion include/csp/drivers/usart.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ void csp_usart_unlock(void * driver_data);
*
* @param[in] conf UART configuration.
* @param[in] ifname internface name (will be copied), or use NULL for default name.
* @param[in] addr CSP address of the interface.
* @param[out] return_iface the added interface.
* @return #CSP_ERR_NONE on success, otherwise an error code.
*/
int csp_usart_open_and_add_kiss_interface(const csp_usart_conf_t * conf, const char * ifname, csp_iface_t ** return_iface);
int csp_usart_open_and_add_kiss_interface(const csp_usart_conf_t * conf, const char * ifname, uint16_t addr, csp_iface_t ** return_iface);

#ifdef __cplusplus
}
Expand Down
5 changes: 3 additions & 2 deletions src/bindings/python/pycsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -875,13 +875,14 @@ static PyObject * pycsp_kiss_init(PyObject * self, PyObject * args) {
char * device;
uint32_t baudrate = 500000;
uint32_t mtu = 512;
uint16_t addr;
const char * if_name = CSP_IF_KISS_DEFAULT_NAME;
if (!PyArg_ParseTuple(args, "s|IIs", &device, &baudrate, &mtu, &if_name)) {
if (!PyArg_ParseTuple(args, "sH|IIs", &device, &addr, &baudrate, &mtu, &if_name)) {
return NULL; // TypeError is thrown
}

csp_usart_conf_t conf = {.device = device, .baudrate = baudrate};
int res = csp_usart_open_and_add_kiss_interface(&conf, if_name, NULL);
int res = csp_usart_open_and_add_kiss_interface(&conf, if_name, addr, NULL);
if (res != CSP_ERR_NONE) {
return PyErr_Error("csp_usart_open_and_add_kiss_interface()", res);
}
Expand Down
2 changes: 1 addition & 1 deletion src/csp_qfifo.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

static csp_static_queue_t qfifo_queue __noinit;
static csp_queue_handle_t qfifo_queue_handle __noinit;
char qfifo_queue_buffer[sizeof(csp_qfifo_t) * CSP_QFIFO_LEN] __noinit;
static char qfifo_queue_buffer[sizeof(csp_qfifo_t) * CSP_QFIFO_LEN] __noinit;

void csp_qfifo_init(void) {
qfifo_queue_handle = csp_queue_create_static(CSP_QFIFO_LEN, sizeof(csp_qfifo_t), qfifo_queue_buffer, &qfifo_queue);
Expand Down
2 changes: 0 additions & 2 deletions src/csp_rdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,3 @@ int csp_rdp_connect(csp_conn_t * conn);
int csp_rdp_close(csp_conn_t * conn, uint8_t closed_by);
int csp_rdp_send(csp_conn_t * conn, csp_packet_t * packet);
int csp_rdp_check_ack(csp_conn_t * conn);

void csp_rdp_conn_print(csp_conn_t * conn);
6 changes: 3 additions & 3 deletions src/csp_rdp_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

static int __csp_rdp_queue_flush(csp_queue_handle_t queue, csp_conn_t * conn) {

int ret;
int ret = CSP_ERR_NONE;
int size;

size = csp_queue_size(queue);
Expand Down Expand Up @@ -85,8 +85,8 @@ void csp_rdp_queue_flush(csp_conn_t * conn) {
csp_queue_empty(tx_queue);
csp_queue_empty(rx_queue);
} else {
(void)__csp_rdp_queue_flush(conn, tx_queue);
(void)__csp_rdp_queue_flush(conn, rx_queue);
(void)__csp_rdp_queue_flush(tx_queue, conn);
(void)__csp_rdp_queue_flush(rx_queue, conn);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/csp_yaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static void csp_yaml_end_if(struct data_s * data, unsigned int * dfl_addr) {
.stopbits = 1,
.paritysetting = 0,
};
int error = csp_usart_open_and_add_kiss_interface(&conf, data->name, &iface);
int error = csp_usart_open_and_add_kiss_interface(&conf, data->name, addr, &iface);
if (error != CSP_ERR_NONE) {
return;
}
Expand Down Expand Up @@ -206,7 +206,7 @@ void csp_yaml_init(char * filename, unsigned int * dfl_addr) {
csp_print(" Reading config from %s\n", filename);
FILE * file = fopen(filename, "rb");
if (file == NULL) {
printf(" ERROR: failed to find CSP config file\n");
csp_print(" ERROR: failed to find CSP config file\n");
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_sources(csp PRIVATE usart/usart_linux.c)
target_sources(csp PRIVATE eth/eth_linux.c)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Zephyr")
target_sources(csp PRIVATE usart/usart_zephyr.c)
endif()
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/can/can_socketcan.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static void * socketcan_rx_thread(void * arg) {
timeout.tv_sec = 10;
int n = select(ctx->socket + 1, &input, NULL, NULL, &timeout);
if (n == -1) {
printf("CAN read error\n");
csp_print("CAN read error\n");
continue;
} else if (n == 0) {
//printf("CAN idle\n");
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/eth/eth_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ int csp_eth_init(const char * device, const char * ifname, int mtu, unsigned int
/* Open RAW socket to send on */
if ((ctx->sockfd = socket(AF_PACKET, SOCK_RAW, htobe16(CSP_ETH_TYPE_CSP))) == -1) {
perror("socket");
printf("Use command 'setcap cap_net_raw+ep ./csh'\n");
csp_print("Use command 'setcap cap_net_raw+ep ./csh'\n");
return CSP_ERR_INVAL;
}

Expand Down
3 changes: 2 additions & 1 deletion src/drivers/usart/usart_kiss.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static void kiss_driver_rx(void * user_data, uint8_t * data, size_t data_size, v
csp_kiss_rx(&ctx->iface, data, data_size, pxTaskWoken);
}

int csp_usart_open_and_add_kiss_interface(const csp_usart_conf_t * conf, const char * ifname, csp_iface_t ** return_iface) {
int csp_usart_open_and_add_kiss_interface(const csp_usart_conf_t * conf, const char * ifname, uint16_t addr, csp_iface_t ** return_iface) {

if (ifname == NULL) {
ifname = CSP_IF_KISS_DEFAULT_NAME;
Expand All @@ -43,6 +43,7 @@ int csp_usart_open_and_add_kiss_interface(const csp_usart_conf_t * conf, const c

strncpy(ctx->name, ifname, sizeof(ctx->name) - 1);
ctx->iface.name = ctx->name;
ctx->iface.addr = addr;
ctx->iface.driver_data = ctx;
ctx->iface.interface_data = &ctx->ifdata;
ctx->ifdata.tx_func = kiss_driver_tx;
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ target_sources(csp PRIVATE
csp_if_tun.c
csp_if_can.c
csp_if_can_pbuf.c
csp_if_eth.c
csp_if_eth_pbuf.c
)

if(LIBZMQ_FOUND)
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/csp_if_eth_pbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ void csp_if_eth_pbuf_list_cleanup(csp_packet_t ** plist) {
void csp_if_eth_pbuf_print(const char * descr, csp_packet_t * packet) {

if (packet) {
printf("%s %p id:%u Age:%lu,%lu,%lu flen:%u\n",
csp_print("%s %p id:%u Age:%lu,%lu,%lu flen:%u\n",
descr, (void *)packet,
(unsigned)packet->cfpid,
(unsigned long)csp_get_ms(),
(unsigned long)packet->last_used,
(unsigned long)(csp_get_ms() - packet->last_used),
(unsigned)packet->frame_length);
} else {
printf("Packet is null\n");
csp_print("Packet is null\n");
}

}
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/csp_if_kiss.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ void csp_kiss_rx(csp_iface_t * iface, const uint8_t * buf, size_t len, void * px
/* If no more memory, skip frame */
if (ifdata->rx_packet == NULL) {
ifdata->rx_mode = KISS_MODE_SKIP_FRAME;
iface->drop++;
break;
}

Expand Down
Loading

0 comments on commit 95dce86

Please sign in to comment.