diff --git a/org.fortiss.af3.platform.raspberry/trunk/lib/inc/CanConnector.h b/org.fortiss.af3.platform.raspberry/trunk/lib/inc/CanConnector.h index c2042c622be595d5b610edbc283a836785d3185b..a75fa33e0743b97dc0581d91843497031ab8ef63 100644 --- a/org.fortiss.af3.platform.raspberry/trunk/lib/inc/CanConnector.h +++ b/org.fortiss.af3.platform.raspberry/trunk/lib/inc/CanConnector.h @@ -3,10 +3,21 @@ #include <stdio.h> +#include "inc/libcansocket.h" #include "inc-gen/data_dictionary.h" // types -typedef const char* CAN_PORT_T; +typedef const char* CAN_MSG_T; +typedef unsigned long CAN_CLK_T; + +// constants +extern const char* CAN_DEVICE_NAME; + +// send and receive +void can_send(); +void can_send_clock(CAN_CLK_T counter); +void can_receive(); +CAN_CLK_T can_receive_clock(); // init, terminate, prepare void can_init(); @@ -19,17 +30,17 @@ void prepare_output_CanConnector(); void finish_output_CanConnector(); // noval -GEN_TYPE_boolean can_is_noval(CAN_PORT_T port); -GEN_TYPE_boolean can_set_noval(CAN_PORT_T port); +GEN_TYPE_boolean can_is_noval(CAN_MSG_T msg); +GEN_TYPE_boolean can_set_noval(CAN_MSG_T msg); // readers -GEN_TYPE_boolean can_read_bool(CAN_PORT_T port); -GEN_TYPE_double can_read_double(CAN_PORT_T port); -GEN_TYPE_int can_read_int(CAN_PORT_T port); +GEN_TYPE_boolean can_read_bool(CAN_MSG_T msg); +GEN_TYPE_double can_read_double(CAN_MSG_T msg); +GEN_TYPE_int can_read_int(CAN_MSG_T pomsgrt); // writers -void can_write_bool(CAN_PORT_T port, GEN_TYPE_boolean value); -void can_write_double(CAN_PORT_T port, GEN_TYPE_double value); -void can_write_int(CAN_PORT_T port, GEN_TYPE_int value); +void can_write_bool(CAN_MSG_T msg, GEN_TYPE_boolean value); +void can_write_double(CAN_MSG_T msg, GEN_TYPE_double value); +void can_write_int(CAN_MSG_T msg, GEN_TYPE_int value); #endif // __CANCONNECTOR_H diff --git a/org.fortiss.af3.platform.raspberry/trunk/lib/inc/libcansocket.h b/org.fortiss.af3.platform.raspberry/trunk/lib/inc/libcansocket.h new file mode 100644 index 0000000000000000000000000000000000000000..a791bad054014c1e9057e1589a2d1bd093dbf3b2 --- /dev/null +++ b/org.fortiss.af3.platform.raspberry/trunk/lib/inc/libcansocket.h @@ -0,0 +1,43 @@ +#ifndef __LIBCANSOCKET_H +#define __LIBCANSOCKET_H + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <net/if.h> +#include <sys/ioctl.h> + +#include <linux/can.h> +#include <linux/can/raw.h> + +#define SR_ERR -1 + +struct can_socket { + int s; + struct sockaddr_can addr; + struct ifreq ifr; +}; + +/** + * Opens a can socket on the given interface + */ +struct can_socket* can_socket_open(const char *if_name); + +/** + * Closes a given socket + */ +void can_socket_close(struct can_socket* sock); + +/** + * Sends a can frame on the specified socket + */ +ssize_t can_socket_send(struct can_socket* sock, struct can_frame* frame); + +/** + * + */ +ssize_t can_socket_receive(struct can_socket* sock, struct can_frame* frame); + +#endif // __LIBCANSOCKET_H diff --git a/org.fortiss.af3.platform.raspberry/trunk/lib/src/CanConnector.c b/org.fortiss.af3.platform.raspberry/trunk/lib/src/CanConnector.c index 42ec3c814dd62f76fd6878ea574d89afaccff451..196072d4b2921e8b197e0b7c6560d100041c4439 100644 --- a/org.fortiss.af3.platform.raspberry/trunk/lib/src/CanConnector.c +++ b/org.fortiss.af3.platform.raspberry/trunk/lib/src/CanConnector.c @@ -1,63 +1,84 @@ #include "inc/CanConnector.h" -// init, terminate -void can_init() { - printf("initializing CAN\n"); +// constants +const char* CAN_DEVICE_NAME = "can0"; + +// variables +struct can_socket* can_sock; + +// send and receive +void can_send() { + // TODO send and empty buffer } -void can_term() { - printf("terminating CAN\n"); +void can_send_clock(CAN_CLK_T counter) { + // TODO send clock message with clock counter } -void prepare_input_CanConnector() { +void can_receive() { + // TODO fill buffer +} +CAN_CLK_T can_receive_clock() { + // TODO receive clock message and return clock counter } -void finish_input_CanConnector() { +// init, terminate +void can_init() { + printf("initializing CAN\n"); + + can_sock = can_socket_open(CAN_DEVICE_NAME); + // TODO init buffers } -void prepare_output_CanConnector() { +void can_term() { + printf("terminating CAN\n"); + can_socket_close(can_sock); } -void finish_output_CanConnector() { +void prepare_input_CanConnector() {} -} +void finish_input_CanConnector() {} + +void prepare_output_CanConnector() {} + +void finish_output_CanConnector() {} // noval -GEN_TYPE_boolean can_is_noval(CAN_PORT_T port) { - return true; +GEN_TYPE_boolean can_is_noval(CAN_MSG_T msg) { + return false; } -GEN_TYPE_boolean can_set_noval(CAN_PORT_T port) { - return true; +GEN_TYPE_boolean can_set_noval(CAN_MSG_T msg) { + return false; } // readers -GEN_TYPE_boolean can_read_bool(CAN_PORT_T port) { - return false; +GEN_TYPE_boolean can_read_bool(CAN_MSG_T msg) { + return false; // TODO } -GEN_TYPE_double can_read_double(CAN_PORT_T port) { - return 0.0; +GEN_TYPE_double can_read_double(CAN_MSG_T msg) { + return 0.0; // TODO } -GEN_TYPE_int can_read_int(CAN_PORT_T port) { - return 0; +GEN_TYPE_int can_read_int(CAN_MSG_T msg) { + return 0; // TODO } // writers -void can_write_bool(CAN_PORT_T port, GEN_TYPE_boolean value) { - +void can_write_bool(CAN_MSG_T msg, GEN_TYPE_boolean value) { + // TODO } -void can_write_double(CAN_PORT_T port, GEN_TYPE_double value) { - +void can_write_double(CAN_MSG_T msg, GEN_TYPE_double value) { + // TODO } -void can_write_int(CAN_PORT_T port, GEN_TYPE_int value) { - +void can_write_int(CAN_MSG_T msg, GEN_TYPE_int value) { + // TODO } diff --git a/org.fortiss.af3.platform.raspberry/trunk/lib/src/Gamepad.c b/org.fortiss.af3.platform.raspberry/trunk/lib/src/Gamepad.c index 39222859efc6af810509d7edae8d231a8e0b971b..c3134d4fd907563640d0671ff7f02575cf86db72 100644 --- a/org.fortiss.af3.platform.raspberry/trunk/lib/src/Gamepad.c +++ b/org.fortiss.af3.platform.raspberry/trunk/lib/src/Gamepad.c @@ -15,7 +15,7 @@ void* gp_worker(void* pt_args) { fd = open(GP_DEVICE_NAME, O_RDONLY); if(fd <= 0) { - perror("Error while connecting to gamepad device"); + perror("Error while connecting to gamepad device. Aborting"); return NULL; } diff --git a/org.fortiss.af3.platform.raspberry/trunk/lib/src/libcansocket.c b/org.fortiss.af3.platform.raspberry/trunk/lib/src/libcansocket.c new file mode 100644 index 0000000000000000000000000000000000000000..b57cc6e8b4e33be281d3beec9e2ce293f79c1f30 --- /dev/null +++ b/org.fortiss.af3.platform.raspberry/trunk/lib/src/libcansocket.c @@ -0,0 +1,56 @@ +#include "inc/libcansocket.h" + +struct can_socket* can_socket_open(const char *if_name) { + struct can_socket* sock = malloc(sizeof(struct can_socket)); + + /* open socket */ + if ((sock->s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { + perror("socket"); + return NULL; + } + + sock->addr.can_family = AF_CAN; + + /* map if name to interface index -> will fail if interface does not exist */ + strcpy(sock->ifr.ifr_name, if_name); + if (ioctl(sock->s, SIOCGIFINDEX, &sock->ifr) < 0) { + perror("SIOCGIFINDEX"); + return NULL; + } + sock->addr.can_ifindex = sock->ifr.ifr_ifindex; + + /* bind socket to interface -> will fail if interface is not up */ + if (bind(sock->s, (struct sockaddr *)&sock->addr, sizeof(sock->addr)) < 0) { + perror("bind"); + return NULL; + } + + return sock; +} + +void can_socket_close(struct can_socket* sock) { + close(sock->s); +} + +ssize_t can_socket_send(struct can_socket* sock, struct can_frame* frame) { + ssize_t nbytes; + + /* send frame */ + if ((nbytes = write(sock->s, &frame, sizeof(*frame))) != sizeof(*frame)) { + perror("write"); + return SR_ERR; + } + + return nbytes; +} + +ssize_t can_socket_receive(struct can_socket* sock, struct can_frame* frame) { + ssize_t nbytes; + + if ((nbytes = read(sock->s, frame, sizeof(struct can_frame))) != sizeof(*frame)) { + perror("read"); + return SR_ERR; + } + + return nbytes; +} diff --git a/org.fortiss.af3.platform.raspberry/trunk/lib/src/main.c b/org.fortiss.af3.platform.raspberry/trunk/lib/src/main.c index f39feb8986786440fb76b996c8ae3071bcd72555..0f7a5177407818f83526b06bdd798ca8c7ad931c 100644 --- a/org.fortiss.af3.platform.raspberry/trunk/lib/src/main.c +++ b/org.fortiss.af3.platform.raspberry/trunk/lib/src/main.c @@ -3,13 +3,19 @@ */ #include <stdio.h> +#include <string.h> #include <unistd.h> #include <pthread.h> #include "inc-gen/system.h" +#include "inc/CanConnector.h" #include "inc/Gamepad.h" #include "inc/PWM.h" +#define PARAM_MAXCMP 5 +#define PARAM_MASTER "master" +#define PARAM_SLAVE "slave" + int main(int argc, char* argv[]) { // init the system initialize_system(); @@ -22,9 +28,24 @@ int main(int argc, char* argv[]) { pthread_t pt_pwm; pthread_create(&pt_pwm, NULL, pwm_worker, NULL); - // run endlessly - while(1) { - run_system(); - usleep(50000); + // MASTER MODE (master as parameter) + if(argc >= 2 && strncmp(argv[1], PARAM_MASTER, PARAM_MAXCMP) == 0) { + // TODO do master stuff + } + + // SLAVE MODE (slave as parameter) + else if(argc >= 2 && strncmp(argv[1], PARAM_SLAVE, PARAM_MAXCMP) == 0) { + // TODO do slave stuff } + + // DEFAULT MODE (no parameter) + else { + // run endlessly + while(1) { + run_system(); + usleep(50000); + } + } + + } diff --git a/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/RaspberryPIExecutable.java b/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/RaspberryPIExecutable.java index caeeca62854cde045d801306eb77d99286e0a4fa..920e0922fd8cf136dea062f526d510c6555eb433 100644 --- a/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/RaspberryPIExecutable.java +++ b/org.fortiss.af3.platform.raspberry/trunk/src/org/fortiss/af3/platform/raspberry/generator/executable/RaspberryPIExecutable.java @@ -108,6 +108,8 @@ public class RaspberryPIExecutable extends GenericExecutionUnitExecutable { addFile(pkg, "src/libuart.c"); addFile(pkg, "inc/libmaestro.h"); addFile(pkg, "src/libmaestro.c"); + addFile(pkg, "inc/libcansocket.h"); + addFile(pkg, "src/libcansocket.c"); /** FIXES AND PATCHES **/