From ddc40c70cfce8c93b228bc28a3144da45acd6708 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Mon, 16 Sep 2013 21:01:56 +0200 Subject: [PATCH 1/1] Changed all (I think) files using CRLF to use LF. --- rtems/gw/cangw/Makefile.omk | 15 +- rtems/gw/cangw/gw.c | 524 ++++++++++++++-------------- rtems/gw/cangw/gw.h | 16 +- rtems/gw/cangw/helpers.c | 240 ++++++------- rtems/gw/cangw/helpers.h | 158 ++++----- rtems/gw/cangw/init.c | 86 ++--- rtems/gw/cangw/system.h | 4 +- rtems/gw/libs/load.c | 244 ++++++------- rtems/gw/libs/load.h | 70 ++-- rtems/gw/system_opt/Makefile.omk | 2 - rtems/gw/system_opt/networkconfig.h | 58 +-- 11 files changed, 695 insertions(+), 722 deletions(-) diff --git a/rtems/gw/cangw/Makefile.omk b/rtems/gw/cangw/Makefile.omk index eeef8e4..e0b0378 100644 --- a/rtems/gw/cangw/Makefile.omk +++ b/rtems/gw/cangw/Makefile.omk @@ -3,14 +3,11 @@ default_CONFIG += CONFIG_OC_GDBSTUB=n bin_PROGRAMS = cangw - -#I am not entirely sure these two are needed, but according to documentation, they are. -CFLAGS_LD += HeapSize=0x80000 -MANAGERS = io event semaphore + +#I am not entirely sure these two are needed, but according to documentation, they are. +CFLAGS_LD += HeapSize=0x80000 +MANAGERS = io event semaphore cangw_SOURCES += init.c gw.c helpers.c - -lib_LOADLIBES += load - -#lib_LIBRARIES -#include_HEADERS + +lib_LOADLIBES += load diff --git a/rtems/gw/cangw/gw.c b/rtems/gw/cangw/gw.c index 2a08a0c..1b8a80d 100644 --- a/rtems/gw/cangw/gw.c +++ b/rtems/gw/cangw/gw.c @@ -1,263 +1,263 @@ -#include -#include "app_def.h" -#include "system.h" -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "gw.h" - -/* Local defines. */ -#define CAN_GW_A_TO_B_MODE 1 -#define CAN_GW_B_TO_A_MODE 2 -#define CAN_GW_BAUDRATE 1000000 -#define CAN_GW_TASK_HIGH_PRIO 250 /* POSIX API actually has priorities the "right way around" that is, higher is indeed more important */ - -/* counters for informational purposes */ -unsigned long int total_1 = 0, total_2 = 0; -unsigned long int succ_1 = 0, succ_2 = 0; -unsigned long int err_1 = 0, err_2 = 0; - -/* Pthread handles for the GW */ -static pthread_t CAN_A_to_B_thread, CAN_B_to_A_thread; -/* Dummy FDs for setting baudrate */ -static int fd1, fd2; - -/* Prototypes for local functions. */ -static void* CAN_GW_thread(void* arg); -static void fd_closer(void* arg); -static int start_tasks(); -static int end_tasks(); -static int set_baudrate(int baudrate, int* fd1, int* fd2); - -/* -* Simple cleanup handler for GW threads. Closes opened FD. -*/ -static void fd_closer(void* arg){ - close( (*(int*)arg) ); -} - - - -/* -* This function implements the main thread loop and enough decision logic so that it knows which device to take messages from and which device to send the messages to. -* -*/ -static void* CAN_GW_thread(void* arg){ - int fd_in, fd_out; - int res; -#ifndef BENCH_BUILD - unsigned long int *total = NULL, *succ = NULL, *err = NULL; /* Makes compiler shut up about warnings. */ -#endif - - struct can_message canmsg; - struct mscan_rx_parms rxparms; - struct mscan_tx_parms txparms; - memset(&canmsg, 0, sizeof(canmsg)); - memset(&rxparms, 0, sizeof(rxparms)); - memset(&txparms, 0, sizeof(txparms)); - - /* These remain constant during the loop. */ - rxparms.rx_mess = &canmsg; - rxparms.rx_timeout = rtems_clock_get_ticks_per_second(); /* 1s timeout */ - txparms.tx_mess = &canmsg; - - - /* Decide in which direction this task should work. */ - if (((int)arg) == CAN_GW_A_TO_B_MODE){ - fd_in = open(MSCAN_A_DEV_NAME, O_RDWR); - fd_out = open(MSCAN_B_DEV_NAME, O_RDWR); - } else if (((int)arg) == CAN_GW_B_TO_A_MODE){ - fd_in = open(MSCAN_B_DEV_NAME, O_RDWR); - fd_out = open(MSCAN_A_DEV_NAME, O_RDWR); - } else { - /* Invalid argument, terminate self. */ - return NULL; - } - -#ifndef BENCH_BUILD /* Overhead decrease for benching builds.*/ - if (((int)arg) == CAN_GW_A_TO_B_MODE){ - total = &total_1; - succ = &succ_1; - err = &err_1; - } else { - total = &total_2; - succ = &succ_2; - err = &err_2; - } -#endif - - /* CANs are set to proper baudrate outside of these task due to potential race condition. */ - - - /* Prepare cleanup for both opened FDs. */ - pthread_cleanup_push(fd_closer, (void*)&fd_in); - pthread_cleanup_push(fd_closer, (void*)&fd_out); - - while (true){ - /* RTEMS doesn't implement cancellation point inside read(), violating the API's contract. Luckily, pthread_testcancel() works. */ - pthread_testcancel(); - res = read(fd_in, &rxparms, sizeof(rxparms)); - if (res < 0){ - /* Read error, doesn't really do anything. (Currently) */ - } else { - /* Message was read, now we should resend it. */ - txparms.tx_idx = canmsg.mess_id; - res = write(fd_out, &txparms, sizeof(txparms)); - if (res < 0) { -#ifndef BENCH_BUILD /* This is the simplest way to remove all the counting */ - /* Retry? Decide later. */ - (*err)++; - } else { - (*succ)++; - } - (*total)++; -#else - } /* Turns it into empty loop. */ -#endif - - } - } - return NULL; -} - - -static int start_tasks(){ - int res; - - - pthread_attr_t attributes; - struct sched_param parm; - parm.sched_priority = CAN_GW_TASK_HIGH_PRIO; - pthread_attr_init(&attributes); -#ifdef HIGH_PRIO /* Without setting PTHREAD_EXPLICIT_SCHED, thread is created with parameters inherited from this thread. */ - res = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED); -#endif - pthread_attr_setschedparam(&attributes, &parm); - - res = pthread_create(&CAN_A_to_B_thread, &attributes, CAN_GW_thread, (void*)CAN_GW_A_TO_B_MODE); - if (res != 0){ - /* No cleanup is needed here. */ - return 1; - } - - res = pthread_create(&CAN_B_to_A_thread, &attributes, CAN_GW_thread, (void*)CAN_GW_B_TO_A_MODE); - if (res != 0){ - /* First thread needs to be aborted before return. */ - pthread_cancel(CAN_A_to_B_thread); - return 1; - } - - pthread_attr_destroy(&attributes); - - /* detach is needed so that the threads call clean up handlers automatically. */ - pthread_detach(CAN_B_to_A_thread); - pthread_detach(CAN_A_to_B_thread); - - /* Threads are started and running at this point. */ - return 0; -} - -/* -* This function starts the GW. -* -* It opens two dummy file descriptors to set CAN baudrate (avoiding need of synchronized thread start), starts the forwarding threads. -* -* Currently has only simple error handling. -*/ -int start_GW(){ - /* Baudrate is set for dummy FD, it should be remembered by the device. */ - int res; - res = set_baudrate(CAN_GW_BAUDRATE, &fd1, &fd2); - printf("Baudrate set.\n"); - if (res == 0){ - res = start_tasks(); - printf("tasks started\n"); - } - return res; -} - - -/* -* This function stops threads implementing GW's forwarding. It tries to stop the threads "politely" via pthread_cancel. -* -* Error handling is not yet implemented, so it always returns 0 (success). -*/ -static int end_tasks(){ - int res; - printf("Attempting to stop thread 1\n"); - res = pthread_cancel(CAN_A_to_B_thread); - if (res != 0){ - printf("Failed.\n"); - /* Not sure what to do with error here, will have to figure out later. */ - } - printf("Attempting to stop thread 2\n"); - res = pthread_cancel(CAN_B_to_A_thread); - if (res != 0){ - printf("Failed.\n"); - /* Not sure what to do with error here, will have to figure out later. */ - } - sleep(1); - printf("Both threads should now be stopped.\n"); - return 0; -} - - -/* -* Sets baudrate to the devices via dummy fd. -*/ -static int set_baudrate(int baudrate, int* fd1, int* fd2){ - int fd, res; - struct mscan_ctrl_parms ctrl_parms; - memset(&ctrl_parms, 0, sizeof(ctrl_parms)); - ctrl_parms.ctrl_can_bitrate = baudrate; - - printf("Attempting to set bitrate %"PRIu32" for fd.\n", ctrl_parms.ctrl_can_bitrate); - - fd = open(MSCAN_A_DEV_NAME, O_RDWR); - if (fd < 0){ - return 1; - } - res = ioctl(fd, MSCAN_SET_BAUDRATE, &ctrl_parms); - if (res < 0) { - printf("fd - MSCAN_SET_BAUDRATE error (%x:%x) %s\n", res, errno, strerror(errno)); - return 1; - } - (*fd1) = fd; - - fd = open(MSCAN_B_DEV_NAME, O_RDWR); - if (fd < 0){ - /* Needs to cleanup by closing first fd. */ - close( (*fd1) ); - return 1; - } - res = ioctl(fd, MSCAN_SET_BAUDRATE, &ctrl_parms); - if (res < 0) { - printf("fd - MSCAN_SET_BAUDRATE error (%x:%x) %s\n", res, errno, strerror(errno)); - return 1; - } - - (*fd2) = fd; - - return 0; -} - -/* -* Wrapper function and entry point for stopping the GW. -*/ -int end_GW(){ - return end_tasks(); - /* Clean up of dummy FDs. */ - close(fd1); - close(fd2); - +#include +#include "app_def.h" +#include "system.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "gw.h" + +/* Local defines. */ +#define CAN_GW_A_TO_B_MODE 1 +#define CAN_GW_B_TO_A_MODE 2 +#define CAN_GW_BAUDRATE 1000000 +#define CAN_GW_TASK_HIGH_PRIO 250 /* POSIX API actually has priorities the "right way around" that is, higher is indeed more important */ + +/* counters for informational purposes */ +unsigned long int total_1 = 0, total_2 = 0; +unsigned long int succ_1 = 0, succ_2 = 0; +unsigned long int err_1 = 0, err_2 = 0; + +/* Pthread handles for the GW */ +static pthread_t CAN_A_to_B_thread, CAN_B_to_A_thread; +/* Dummy FDs for setting baudrate */ +static int fd1, fd2; + +/* Prototypes for local functions. */ +static void* CAN_GW_thread(void* arg); +static void fd_closer(void* arg); +static int start_tasks(); +static int end_tasks(); +static int set_baudrate(int baudrate, int* fd1, int* fd2); + +/* +* Simple cleanup handler for GW threads. Closes opened FD. +*/ +static void fd_closer(void* arg){ + close( (*(int*)arg) ); +} + + + +/* +* This function implements the main thread loop and enough decision logic so that it knows which device to take messages from and which device to send the messages to. +* +*/ +static void* CAN_GW_thread(void* arg){ + int fd_in, fd_out; + int res; +#ifndef BENCH_BUILD + unsigned long int *total = NULL, *succ = NULL, *err = NULL; /* Makes compiler shut up about warnings. */ +#endif + + struct can_message canmsg; + struct mscan_rx_parms rxparms; + struct mscan_tx_parms txparms; + memset(&canmsg, 0, sizeof(canmsg)); + memset(&rxparms, 0, sizeof(rxparms)); + memset(&txparms, 0, sizeof(txparms)); + + /* These remain constant during the loop. */ + rxparms.rx_mess = &canmsg; + rxparms.rx_timeout = rtems_clock_get_ticks_per_second(); /* 1s timeout */ + txparms.tx_mess = &canmsg; + + + /* Decide in which direction this task should work. */ + if (((int)arg) == CAN_GW_A_TO_B_MODE){ + fd_in = open(MSCAN_A_DEV_NAME, O_RDWR); + fd_out = open(MSCAN_B_DEV_NAME, O_RDWR); + } else if (((int)arg) == CAN_GW_B_TO_A_MODE){ + fd_in = open(MSCAN_B_DEV_NAME, O_RDWR); + fd_out = open(MSCAN_A_DEV_NAME, O_RDWR); + } else { + /* Invalid argument, terminate self. */ + return NULL; + } + +#ifndef BENCH_BUILD /* Overhead decrease for benching builds.*/ + if (((int)arg) == CAN_GW_A_TO_B_MODE){ + total = &total_1; + succ = &succ_1; + err = &err_1; + } else { + total = &total_2; + succ = &succ_2; + err = &err_2; + } +#endif + + /* CANs are set to proper baudrate outside of these task due to potential race condition. */ + + + /* Prepare cleanup for both opened FDs. */ + pthread_cleanup_push(fd_closer, (void*)&fd_in); + pthread_cleanup_push(fd_closer, (void*)&fd_out); + + while (true){ + /* RTEMS doesn't implement cancellation point inside read(), violating the API's contract. Luckily, pthread_testcancel() works. */ + pthread_testcancel(); + res = read(fd_in, &rxparms, sizeof(rxparms)); + if (res < 0){ + /* Read error, doesn't really do anything. (Currently) */ + } else { + /* Message was read, now we should resend it. */ + txparms.tx_idx = canmsg.mess_id; + res = write(fd_out, &txparms, sizeof(txparms)); + if (res < 0) { +#ifndef BENCH_BUILD /* This is the simplest way to remove all the counting */ + /* Retry? Decide later. */ + (*err)++; + } else { + (*succ)++; + } + (*total)++; +#else + } /* Turns it into empty loop. */ +#endif + + } + } + return NULL; +} + + +static int start_tasks(){ + int res; + + + pthread_attr_t attributes; + struct sched_param parm; + parm.sched_priority = CAN_GW_TASK_HIGH_PRIO; + pthread_attr_init(&attributes); +#ifdef HIGH_PRIO /* Without setting PTHREAD_EXPLICIT_SCHED, thread is created with parameters inherited from this thread. */ + res = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED); +#endif + pthread_attr_setschedparam(&attributes, &parm); + + res = pthread_create(&CAN_A_to_B_thread, &attributes, CAN_GW_thread, (void*)CAN_GW_A_TO_B_MODE); + if (res != 0){ + /* No cleanup is needed here. */ + return 1; + } + + res = pthread_create(&CAN_B_to_A_thread, &attributes, CAN_GW_thread, (void*)CAN_GW_B_TO_A_MODE); + if (res != 0){ + /* First thread needs to be aborted before return. */ + pthread_cancel(CAN_A_to_B_thread); + return 1; + } + + pthread_attr_destroy(&attributes); + + /* detach is needed so that the threads call clean up handlers automatically. */ + pthread_detach(CAN_B_to_A_thread); + pthread_detach(CAN_A_to_B_thread); + + /* Threads are started and running at this point. */ + return 0; +} + +/* +* This function starts the GW. +* +* It opens two dummy file descriptors to set CAN baudrate (avoiding need of synchronized thread start), starts the forwarding threads. +* +* Currently has only simple error handling. +*/ +int start_GW(){ + /* Baudrate is set for dummy FD, it should be remembered by the device. */ + int res; + res = set_baudrate(CAN_GW_BAUDRATE, &fd1, &fd2); + printf("Baudrate set.\n"); + if (res == 0){ + res = start_tasks(); + printf("tasks started\n"); + } + return res; +} + + +/* +* This function stops threads implementing GW's forwarding. It tries to stop the threads "politely" via pthread_cancel. +* +* Error handling is not yet implemented, so it always returns 0 (success). +*/ +static int end_tasks(){ + int res; + printf("Attempting to stop thread 1\n"); + res = pthread_cancel(CAN_A_to_B_thread); + if (res != 0){ + printf("Failed.\n"); + /* Not sure what to do with error here, will have to figure out later. */ + } + printf("Attempting to stop thread 2\n"); + res = pthread_cancel(CAN_B_to_A_thread); + if (res != 0){ + printf("Failed.\n"); + /* Not sure what to do with error here, will have to figure out later. */ + } + sleep(1); + printf("Both threads should now be stopped.\n"); + return 0; +} + + +/* +* Sets baudrate to the devices via dummy fd. +*/ +static int set_baudrate(int baudrate, int* fd1, int* fd2){ + int fd, res; + struct mscan_ctrl_parms ctrl_parms; + memset(&ctrl_parms, 0, sizeof(ctrl_parms)); + ctrl_parms.ctrl_can_bitrate = baudrate; + + printf("Attempting to set bitrate %"PRIu32" for fd.\n", ctrl_parms.ctrl_can_bitrate); + + fd = open(MSCAN_A_DEV_NAME, O_RDWR); + if (fd < 0){ + return 1; + } + res = ioctl(fd, MSCAN_SET_BAUDRATE, &ctrl_parms); + if (res < 0) { + printf("fd - MSCAN_SET_BAUDRATE error (%x:%x) %s\n", res, errno, strerror(errno)); + return 1; + } + (*fd1) = fd; + + fd = open(MSCAN_B_DEV_NAME, O_RDWR); + if (fd < 0){ + /* Needs to cleanup by closing first fd. */ + close( (*fd1) ); + return 1; + } + res = ioctl(fd, MSCAN_SET_BAUDRATE, &ctrl_parms); + if (res < 0) { + printf("fd - MSCAN_SET_BAUDRATE error (%x:%x) %s\n", res, errno, strerror(errno)); + return 1; + } + + (*fd2) = fd; + + return 0; +} + +/* +* Wrapper function and entry point for stopping the GW. +*/ +int end_GW(){ + return end_tasks(); + /* Clean up of dummy FDs. */ + close(fd1); + close(fd2); + } \ No newline at end of file diff --git a/rtems/gw/cangw/gw.h b/rtems/gw/cangw/gw.h index 742ab1a..abb7a55 100644 --- a/rtems/gw/cangw/gw.h +++ b/rtems/gw/cangw/gw.h @@ -1,9 +1,9 @@ -#ifndef __GW_H_ -#define __GW_H_ - -extern unsigned long int total_1, total_2, succ_1, succ_2, err_1, err_2; - -int start_GW(); -int end_GW(); - +#ifndef __GW_H_ +#define __GW_H_ + +extern unsigned long int total_1, total_2, succ_1, succ_2, err_1, err_2; + +int start_GW(); +int end_GW(); + #endif \ No newline at end of file diff --git a/rtems/gw/cangw/helpers.c b/rtems/gw/cangw/helpers.c index d994a55..f128951 100644 --- a/rtems/gw/cangw/helpers.c +++ b/rtems/gw/cangw/helpers.c @@ -1,120 +1,120 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "helpers.h" -#include "gw.h" -#include "load.h" - -#include -#include "networkconfig.h" - - -static rtems_device_major_number mscan_major; -static rtems_driver_address_table mscan_driver_table=MSCAN_DRIVER_TABLE_ENTRY; - -/* -* Prints can stats. (Used in debugging) -*/ -int print_can_totals(int argc, char** argv){ - printf("Total 1: %"PRIu32", Total 2: %"PRIu32"\n", total_1, total_2); - printf("Success 1: %"PRIu32", Success 2: %"PRIu32"\n", succ_1, succ_2); - printf("Errors 1: %"PRIu32", Errors 2: %"PRIu32"\n", err_1, err_2); - return 0; -} - -/* -* Prints clocks as given by the uboot. (Used in debugging) -*/ -int print_clocks(int argc, char** argv){ - printf("IPB_CLOCK: %lu\n", bsp_uboot_board_info.bi_ipbfreq); - printf("XLB_CLOCK: %lu\n", bsp_uboot_board_info.bi_busfreq); - printf("G2_CLOCK: %lu\n", bsp_uboot_board_info.bi_intfreq); - printf("BAUD: %lu\n", bsp_uboot_board_info.bi_baudrate); - return 0; -} - -/* -* Single function to prepare CAN devices for read/write operation. -* -* Sets up baud rate at 1M, opens fda and fdb and checks for errors. -*/ -static int init_CAN(){ - rtems_status_code status; - - printf("Initializing CAN bus.\n"); - - printf("Changing gpiopcr setting...\n"); - //Clear PCR_CHIP_SELECT bits and sets them to ALT_CAN. - mpc5200.gpiopcr = (mpc5200.gpiopcr & (~GPIO_PCR_CHIP_SELECT_1)) | GPIO_PCR_CHIP_ALTS_CAN; - - printf("Registering CAN drivers...\n"); - status = rtems_io_register_driver(0, &mscan_driver_table, &mscan_major); - if (status != RTEMS_SUCCESSFUL){ - printf("caninit: rtems_io_register_driver %s\n",rtems_status_text(status)); - return 1; - } - - return 0; -} - -int start_can(int argc, char** argv){ - int res; - static char inited = 0; - if (!inited){ - res = init_CAN(); - inited = 1; - if (res != 0){ - //TODO - printf("Error while initializing CAN\n"); - return res; - } - printf("CAN inited\n"); - } - res = start_GW(); - return 0; -} - -int end_can(int argc, char** argv){ - return end_GW(); -} - -int show_net(int argc, char** argv){ - rtems_bsdnet_show_if_stats(); - rtems_bsdnet_show_ip_stats(); - rtems_bsdnet_show_icmp_stats(); - rtems_bsdnet_show_mbuf_stats(); - rtems_bsdnet_show_inet_routes(); - return 0; -} - -int start_net(int argc, char** argv){ - int res; - printf("Initializing Network\n"); - res = rtems_bsdnet_initialize_network (); - if (res < 0){ - printf("Error while initializing network: %d %s\n", errno, strerror(errno)); - return 1; - } - printf("Success\n"); - printf("Found routes.\n"); - rtems_bsdnet_show_inet_routes (); - return 0; -} - -int start_load(int argc, char** argv){ - return start_thread_load(); -} - -int stop_load(int argc, char** argv){ - return end_thread_load(); -} +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "helpers.h" +#include "gw.h" +#include "load.h" + +#include +#include "networkconfig.h" + + +static rtems_device_major_number mscan_major; +static rtems_driver_address_table mscan_driver_table=MSCAN_DRIVER_TABLE_ENTRY; + +/* +* Prints can stats. (Used in debugging) +*/ +int print_can_totals(int argc, char** argv){ + printf("Total 1: %"PRIu32", Total 2: %"PRIu32"\n", total_1, total_2); + printf("Success 1: %"PRIu32", Success 2: %"PRIu32"\n", succ_1, succ_2); + printf("Errors 1: %"PRIu32", Errors 2: %"PRIu32"\n", err_1, err_2); + return 0; +} + +/* +* Prints clocks as given by the uboot. (Used in debugging) +*/ +int print_clocks(int argc, char** argv){ + printf("IPB_CLOCK: %lu\n", bsp_uboot_board_info.bi_ipbfreq); + printf("XLB_CLOCK: %lu\n", bsp_uboot_board_info.bi_busfreq); + printf("G2_CLOCK: %lu\n", bsp_uboot_board_info.bi_intfreq); + printf("BAUD: %lu\n", bsp_uboot_board_info.bi_baudrate); + return 0; +} + +/* +* Single function to prepare CAN devices for read/write operation. +* +* Sets up baud rate at 1M, opens fda and fdb and checks for errors. +*/ +static int init_CAN(){ + rtems_status_code status; + + printf("Initializing CAN bus.\n"); + + printf("Changing gpiopcr setting...\n"); + //Clear PCR_CHIP_SELECT bits and sets them to ALT_CAN. + mpc5200.gpiopcr = (mpc5200.gpiopcr & (~GPIO_PCR_CHIP_SELECT_1)) | GPIO_PCR_CHIP_ALTS_CAN; + + printf("Registering CAN drivers...\n"); + status = rtems_io_register_driver(0, &mscan_driver_table, &mscan_major); + if (status != RTEMS_SUCCESSFUL){ + printf("caninit: rtems_io_register_driver %s\n",rtems_status_text(status)); + return 1; + } + + return 0; +} + +int start_can(int argc, char** argv){ + int res; + static char inited = 0; + if (!inited){ + res = init_CAN(); + inited = 1; + if (res != 0){ + //TODO + printf("Error while initializing CAN\n"); + return res; + } + printf("CAN inited\n"); + } + res = start_GW(); + return 0; +} + +int end_can(int argc, char** argv){ + return end_GW(); +} + +int show_net(int argc, char** argv){ + rtems_bsdnet_show_if_stats(); + rtems_bsdnet_show_ip_stats(); + rtems_bsdnet_show_icmp_stats(); + rtems_bsdnet_show_mbuf_stats(); + rtems_bsdnet_show_inet_routes(); + return 0; +} + +int start_net(int argc, char** argv){ + int res; + printf("Initializing Network\n"); + res = rtems_bsdnet_initialize_network (); + if (res < 0){ + printf("Error while initializing network: %d %s\n", errno, strerror(errno)); + return 1; + } + printf("Success\n"); + printf("Found routes.\n"); + rtems_bsdnet_show_inet_routes (); + return 0; +} + +int start_load(int argc, char** argv){ + return start_thread_load(); +} + +int stop_load(int argc, char** argv){ + return end_thread_load(); +} diff --git a/rtems/gw/cangw/helpers.h b/rtems/gw/cangw/helpers.h index 95dcf39..daed658 100644 --- a/rtems/gw/cangw/helpers.h +++ b/rtems/gw/cangw/helpers.h @@ -1,80 +1,80 @@ -#ifndef __HELPERS_H_ -#define __HELPERS_H_ - -int start_can(int argc, char** argv); -int end_can(int argc, char** argv); -int print_clocks(int argc, char** argv); -int print_can_totals(int argc, char** argv); -int start_net(int argc, char** argv); -int show_net(int argc, char** argv); -int start_load(int argc, char** argv); -int stop_load(int argc, char** argv); - -/* chain of shell command descriptors */ -static rtems_shell_cmd_t shell_command_stop_load = { - "stop_load", /* name */ - "stops cpu loading threads", /* usage */ - "user", /* topic */ - stop_load, /* command */ - NULL, /* alias */ - NULL /* next */ -}; -static rtems_shell_cmd_t shell_command_start_load = { - "start_load", /* name */ - "starts cpu loading threads", /* usage */ - "user", /* topic */ - start_load, /* command */ - NULL, /* alias */ - &shell_command_stop_load /* next */ -}; -static rtems_shell_cmd_t shell_command_start_net = { - "startNET", /* name */ - "starts ethernet driver", /* usage */ - "user", /* topic */ - start_net, /* command */ - NULL, /* alias */ - &shell_command_start_load /* next */ -}; -static rtems_shell_cmd_t shell_command_stop_gw = { - "stopGW", /* name */ - "stops CAN gateway", /* usage */ - "user", /* topic */ - end_can, /* command */ - NULL, /* alias */ - &shell_command_start_net /* next */ -}; -static rtems_shell_cmd_t shell_command_start_gw = { - "startGW", /* name */ - "starts CAN gateway", /* usage */ - "user", /* topic */ - start_can, /* command */ - NULL, /* alias */ - &shell_command_stop_gw /* next */ -}; -static rtems_shell_cmd_t shell_command_print_net = { - "show_net", /* name */ - "shows some debug information about bsdnet", /* usage */ - "user", /* topic */ - show_net, /* command */ - NULL, /* alias */ - &shell_command_start_gw /* next */ -}; -static rtems_shell_cmd_t shell_command_print_clocks = { - "print_clocks", /* name */ - "prints clock as taken from uboot", /* usage */ - "user", /* topic */ - print_clocks, /* command */ - NULL, /* alias */ - &shell_command_print_net /* next */ -}; -static rtems_shell_cmd_t shell_command_print_can_totals = { - "printcan", /* name */ - "prints can stats", /* usage */ - "user", /* topic */ - print_can_totals, /* command */ - NULL, /* alias */ - &shell_command_print_clocks /* next */ -}; - - +#ifndef __HELPERS_H_ +#define __HELPERS_H_ + +int start_can(int argc, char** argv); +int end_can(int argc, char** argv); +int print_clocks(int argc, char** argv); +int print_can_totals(int argc, char** argv); +int start_net(int argc, char** argv); +int show_net(int argc, char** argv); +int start_load(int argc, char** argv); +int stop_load(int argc, char** argv); + +/* chain of shell command descriptors */ +static rtems_shell_cmd_t shell_command_stop_load = { + "stop_load", /* name */ + "stops cpu loading threads", /* usage */ + "user", /* topic */ + stop_load, /* command */ + NULL, /* alias */ + NULL /* next */ +}; +static rtems_shell_cmd_t shell_command_start_load = { + "start_load", /* name */ + "starts cpu loading threads", /* usage */ + "user", /* topic */ + start_load, /* command */ + NULL, /* alias */ + &shell_command_stop_load /* next */ +}; +static rtems_shell_cmd_t shell_command_start_net = { + "startNET", /* name */ + "starts ethernet driver", /* usage */ + "user", /* topic */ + start_net, /* command */ + NULL, /* alias */ + &shell_command_start_load /* next */ +}; +static rtems_shell_cmd_t shell_command_stop_gw = { + "stopGW", /* name */ + "stops CAN gateway", /* usage */ + "user", /* topic */ + end_can, /* command */ + NULL, /* alias */ + &shell_command_start_net /* next */ +}; +static rtems_shell_cmd_t shell_command_start_gw = { + "startGW", /* name */ + "starts CAN gateway", /* usage */ + "user", /* topic */ + start_can, /* command */ + NULL, /* alias */ + &shell_command_stop_gw /* next */ +}; +static rtems_shell_cmd_t shell_command_print_net = { + "show_net", /* name */ + "shows some debug information about bsdnet", /* usage */ + "user", /* topic */ + show_net, /* command */ + NULL, /* alias */ + &shell_command_start_gw /* next */ +}; +static rtems_shell_cmd_t shell_command_print_clocks = { + "print_clocks", /* name */ + "prints clock as taken from uboot", /* usage */ + "user", /* topic */ + print_clocks, /* command */ + NULL, /* alias */ + &shell_command_print_net /* next */ +}; +static rtems_shell_cmd_t shell_command_print_can_totals = { + "printcan", /* name */ + "prints can stats", /* usage */ + "user", /* topic */ + print_can_totals, /* command */ + NULL, /* alias */ + &shell_command_print_clocks /* next */ +}; + + #endif \ No newline at end of file diff --git a/rtems/gw/cangw/init.c b/rtems/gw/cangw/init.c index c8057b2..5147673 100644 --- a/rtems/gw/cangw/init.c +++ b/rtems/gw/cangw/init.c @@ -1,29 +1,7 @@ -/* Init - * - * This routine is the initialization task for this test program. - * It is called from init_exec and has the responsibility for creating - * and starting the tasks that make up the test. If the time of day - * clock is required for the test, it should also be set to a known - * value by this function. - * - * Input parameters: NONE - * - * Output parameters: NONE - * - * COPYRIGHT (c) 1989-1999. - * On-Line Applications Research Corporation (OAR). - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rtems.com/license/LICENSE. - * - * $Id: init.c,v 1.12.4.1 2003/09/04 18:46:30 joel Exp $ - */ - #define CONFIGURE_INIT #include #include "system.h" -#include "app_def.h" +#include "app_def.h" #include #include #include @@ -42,15 +20,15 @@ __XSTRING(major) "." __XSTRING(minor) "." __XSTRING(patch) #define RTEMS_VER_CODE VER_CODE(__RTEMS_MAJOR__ ,__RTEMS_MINOR__ ,__RTEMS_REVISION__) - - -#define CONFIGURE_SHELL_USER_COMMANDS &shell_command_print_can_totals -#define CONFIGURE_SHELL_COMMANDS_INIT -#define CONFIGURE_SHELL_COMMANDS_ALL - -#include -#include - + + +#define CONFIGURE_SHELL_USER_COMMANDS &shell_command_print_can_totals +#define CONFIGURE_SHELL_COMMANDS_INIT +#define CONFIGURE_SHELL_COMMANDS_ALL + +#include +#include + rtems_task Init(rtems_task_argument ignored){ rtems_status_code status; @@ -64,28 +42,28 @@ rtems_task Init(rtems_task_argument ignored){ printf( "Starting application " SW_VER_ID " v " BUILD_VERSION_STRING(SW_VER_MAJOR,SW_VER_MINOR,SW_VER_PATCH) "\n" ); - - /* Inits */ -#ifdef BENCH_BUILD - start_can(0, NULL); - start_net(0, NULL); -#ifdef LOAD_BUILD - start_load(0, NULL); -#endif -#endif - -#ifndef BENCH_BUILD - rtems_shell_init( - "SHLL", /* task name */ - RTEMS_MINIMUM_STACK_SIZE * 4, /* task stack size */ - 100, /* task priority */ - "/dev/console", /* device name */ - true, /* run forever */ - false, /* wait for shell to terminate */ - NULL /* login check function, use NULL to disable a login check */ - ); -#endif - + + /* Inits */ +#ifdef BENCH_BUILD + start_can(0, NULL); + start_net(0, NULL); +#ifdef LOAD_BUILD + start_load(0, NULL); +#endif +#endif + +#ifndef BENCH_BUILD + rtems_shell_init( + "SHLL", /* task name */ + RTEMS_MINIMUM_STACK_SIZE * 4, /* task stack size */ + 100, /* task priority */ + "/dev/console", /* device name */ + true, /* run forever */ + false, /* wait for shell to terminate */ + NULL /* login check function, use NULL to disable a login check */ + ); +#endif + status = rtems_task_delete( RTEMS_SELF ); exit( 0 ); diff --git a/rtems/gw/cangw/system.h b/rtems/gw/cangw/system.h index 0673310..3910271 100644 --- a/rtems/gw/cangw/system.h +++ b/rtems/gw/cangw/system.h @@ -23,7 +23,7 @@ rtems_task Init( /* configuration information */ -#include /* for device driver prototypes */ +#include /* for device driver prototypes */ #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER @@ -39,7 +39,7 @@ rtems_task Init( #define CONFIGURE_MAXIMUM_USER_EXTENSIONS 2 #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 32 #define CONFIGURE_MAXIMUM_DRIVERS (CONFIGURE_NUMBER_OF_DRIVERS+10) - + #ifdef RTEMS_POSIX_API #define CONFIGURE_MAXIMUM_POSIX_THREADS 32 diff --git a/rtems/gw/libs/load.c b/rtems/gw/libs/load.c index 6afb7a9..d895491 100644 --- a/rtems/gw/libs/load.c +++ b/rtems/gw/libs/load.c @@ -1,123 +1,123 @@ -#include - -#include -#include - -#include "load.h" - -/* Load function for threads. */ -static void* produce(void* arg); -static void* consume(void* arg); - -/* semaphores for consumer/producer pair */ -static sem_t produced, consumed; -/* pthread handles for the loader */ -static pthread_t consumer, producer; -static int n = 0; -static char running = 0; - -static void* produce(void* arg){ - while (1) { - sem_wait(&consumed); - n++; - sem_post(&produced); - pthread_testcancel(); - } - return NULL; -} - -static void* consume(void* arg){ - while (1) { - sem_wait(&produced); - n--; - sem_post(&consumed); - pthread_testcancel(); - } - return NULL; -} - -/* -* This function starts threads loading the CPU and creates associated semaphores. -* -* Has a guard to prevent starting again, before it was stopped. -* -* No error handling currently, only tries to report errors. -*/ -int start_thread_load(){ - if (running == 0){ - printf("Attempting to start load.\n"); - int res; - running = 1; - res = sem_init(&consumed, 0, 0); - if (res < 0){ - printf("Couldn't initialize consumed semaphore.\n"); - return 1; - } - res = sem_init(&produced, 0, 1); - if (res < 0){ - printf("Couldn't initialize produced semaphore.\n"); - return 1; - } - - res = pthread_create(&producer, NULL, produce, NULL); - if (res < 0){ - printf("Couldn't create producer thread.\n"); - return 1; - } - - res = pthread_create(&consumer, NULL, consume, NULL); - if (res < 0){ - printf("Couldn't create consumer thread.\n"); - return 1; - } - - pthread_detach(producer); - pthread_detach(consumer); - printf("Load started succesfully.\n"); - return 0; - } else { - printf("Load is already running.\n"); - return 0; - } -} - -/* -* This function stops threads loading the CPU and destroys associated semaphores. -* -* Has a guard against attempting to stop the threads if they are not running. -* -* No error handling currently, only tries to report errors. -*/ -int end_thread_load(){ - if (running == 1){ - int res; - printf("Attempting to cancel producer thread.\n"); - res = pthread_cancel(producer); - if (res != 0){ - /* This means that sending cancel signal has failed... Just returning an error should be enough. */ - /* If we killed the thread, destroying the semaphore would lead to UB. */ - printf("Failed.\n"); - return 1; - } - - printf("Attempting to cancel consumer thread.\n"); - res = pthread_cancel(consumer); - if (res != 0){ - /* Same here. */ - printf("Failed.\n"); - return 1; - } - - printf("Preparing to destroy semaphores.\n"); - /* Wait a bit so that the threads can get to a cancellation point. */ - sleep(1); - sem_destroy(&produced); - sem_destroy(&consumed); - running = 0; - printf("Finished.\n"); - return 0; - } else { - printf("Load is not running.\n"); - return 0; - } +#include + +#include +#include + +#include "load.h" + +/* Load function for threads. */ +static void* produce(void* arg); +static void* consume(void* arg); + +/* semaphores for consumer/producer pair */ +static sem_t produced, consumed; +/* pthread handles for the loader */ +static pthread_t consumer, producer; +static int n = 0; +static char running = 0; + +static void* produce(void* arg){ + while (1) { + sem_wait(&consumed); + n++; + sem_post(&produced); + pthread_testcancel(); + } + return NULL; +} + +static void* consume(void* arg){ + while (1) { + sem_wait(&produced); + n--; + sem_post(&consumed); + pthread_testcancel(); + } + return NULL; +} + +/* +* This function starts threads loading the CPU and creates associated semaphores. +* +* Has a guard to prevent starting again, before it was stopped. +* +* No error handling currently, only tries to report errors. +*/ +int start_thread_load(){ + if (running == 0){ + printf("Attempting to start load.\n"); + int res; + running = 1; + res = sem_init(&consumed, 0, 0); + if (res < 0){ + printf("Couldn't initialize consumed semaphore.\n"); + return 1; + } + res = sem_init(&produced, 0, 1); + if (res < 0){ + printf("Couldn't initialize produced semaphore.\n"); + return 1; + } + + res = pthread_create(&producer, NULL, produce, NULL); + if (res < 0){ + printf("Couldn't create producer thread.\n"); + return 1; + } + + res = pthread_create(&consumer, NULL, consume, NULL); + if (res < 0){ + printf("Couldn't create consumer thread.\n"); + return 1; + } + + pthread_detach(producer); + pthread_detach(consumer); + printf("Load started succesfully.\n"); + return 0; + } else { + printf("Load is already running.\n"); + return 0; + } +} + +/* +* This function stops threads loading the CPU and destroys associated semaphores. +* +* Has a guard against attempting to stop the threads if they are not running. +* +* No error handling currently, only tries to report errors. +*/ +int end_thread_load(){ + if (running == 1){ + int res; + printf("Attempting to cancel producer thread.\n"); + res = pthread_cancel(producer); + if (res != 0){ + /* This means that sending cancel signal has failed... Just returning an error should be enough. */ + /* If we killed the thread, destroying the semaphore would lead to UB. */ + printf("Failed.\n"); + return 1; + } + + printf("Attempting to cancel consumer thread.\n"); + res = pthread_cancel(consumer); + if (res != 0){ + /* Same here. */ + printf("Failed.\n"); + return 1; + } + + printf("Preparing to destroy semaphores.\n"); + /* Wait a bit so that the threads can get to a cancellation point. */ + sleep(1); + sem_destroy(&produced); + sem_destroy(&consumed); + running = 0; + printf("Finished.\n"); + return 0; + } else { + printf("Load is not running.\n"); + return 0; + } } \ No newline at end of file diff --git a/rtems/gw/libs/load.h b/rtems/gw/libs/load.h index 1cb6af9..b81b8c6 100644 --- a/rtems/gw/libs/load.h +++ b/rtems/gw/libs/load.h @@ -1,35 +1,35 @@ -/* -* Implements simple producer/consumer thread pair to cause load on the CPU. -* -* Used in benchmarking the CAN gateway. -* -* Co-opted from http://support.dce.felk.cvut.cz/pos/cv3/src/semaphore.html. -*/ -#ifndef __CPU_LOAD_H_ -#define __CPU_LOAD_H_ - - -/* -* This function starts threads loading the CPU and creates associated semaphores. -* -* Has a guard to prevent starting again, before it was stopped. -* -* No error handling currently, only tries to report errors. -* -* 0 if successfull, 1 otherwise. -*/ -int start_thread_load(); - -/* -* This function stops threads loading the CPU and destroys associated semaphores. -* -* Has a guard against attempting to stop the threads if they are not running. -* -* No error handling currently, only tries to report errors. -* -* 0 if successfull, 1 otherwise. -*/ -int end_thread_load(); - -#endif - +/* +* Implements simple producer/consumer thread pair to cause load on the CPU. +* +* Used in benchmarking the CAN gateway. +* +* Co-opted from http://support.dce.felk.cvut.cz/pos/cv3/src/semaphore.html. +*/ +#ifndef __CPU_LOAD_H_ +#define __CPU_LOAD_H_ + + +/* +* This function starts threads loading the CPU and creates associated semaphores. +* +* Has a guard to prevent starting again, before it was stopped. +* +* No error handling currently, only tries to report errors. +* +* 0 if successfull, 1 otherwise. +*/ +int start_thread_load(); + +/* +* This function stops threads loading the CPU and destroys associated semaphores. +* +* Has a guard against attempting to stop the threads if they are not running. +* +* No error handling currently, only tries to report errors. +* +* 0 if successfull, 1 otherwise. +*/ +int end_thread_load(); + +#endif + diff --git a/rtems/gw/system_opt/Makefile.omk b/rtems/gw/system_opt/Makefile.omk index 6ecec4c..ab28ea2 100644 --- a/rtems/gw/system_opt/Makefile.omk +++ b/rtems/gw/system_opt/Makefile.omk @@ -1,3 +1 @@ -#SUBDIRS = - include_HEADERS = system_def.h networkconfig.h app_def.h diff --git a/rtems/gw/system_opt/networkconfig.h b/rtems/gw/system_opt/networkconfig.h index a162114..abbaef1 100644 --- a/rtems/gw/system_opt/networkconfig.h +++ b/rtems/gw/system_opt/networkconfig.h @@ -4,40 +4,40 @@ #include #include -/* This was taken from existing example, driver failed to provide its own, so why not. */ -static char ethernet_address[6] = {0x00, 0x04, 0x9F, 0x00, 0x27, 0x50 }; - -extern void rtems_bsdnet_loopattach(); - -/* config for loopback device */ -static struct rtems_bsdnet_ifconfig loopback_config = { - "lo0", - rtems_bsdnet_loopattach, - NULL, - "127.0.0.1", - "255.0.0.0" - /* Rest of the struct is set to 0 (is defaulted) */ -}; - -/* config for ethernet */ -static struct rtems_bsdnet_ifconfig netdriver_config = { - RTEMS_BSP_NETWORK_DRIVER_NAME, - RTEMS_BSP_NETWORK_DRIVER_ATTACH, - &loopback_config, /* link to next interface */ - "192.168.2.3", /* IP address */ - "255.255.255.0", /* IP address net mask */ - ethernet_address /* ethernet hardware address, 0 - supplied by driver according to documentation, but it caused error when tested */ - - /* rest of the struct is set to 0 (is defaulted) */ -}; - +/* This was taken from existing example, driver failed to provide its own, so why not. */ +static char ethernet_address[6] = {0x00, 0x04, 0x9F, 0x00, 0x27, 0x50 }; + +extern void rtems_bsdnet_loopattach(); + +/* config for loopback device */ +static struct rtems_bsdnet_ifconfig loopback_config = { + "lo0", + rtems_bsdnet_loopattach, + NULL, + "127.0.0.1", + "255.0.0.0" + /* Rest of the struct is set to 0 (is defaulted) */ +}; + +/* config for ethernet */ +static struct rtems_bsdnet_ifconfig netdriver_config = { + RTEMS_BSP_NETWORK_DRIVER_NAME, + RTEMS_BSP_NETWORK_DRIVER_ATTACH, + &loopback_config, /* link to next interface */ + "192.168.2.3", /* IP address */ + "255.255.255.0", /* IP address net mask */ + ethernet_address /* ethernet hardware address, 0 - supplied by driver according to documentation, but it caused error when tested */ + + /* rest of the struct is set to 0 (is defaulted) */ +}; + /* Main config. */ struct rtems_bsdnet_config rtems_bsdnet_config = { &netdriver_config, /* This entry points to the head of the ifconfig chain. */ NULL, /* rtems_bsdnet_do_bootp if it should use bootp, null otherwise */ - /* From here on, zero means default value. */ + /* From here on, zero means default value. */ 100, /* network task priority (default 100) */ - 65792, /* mbuf bytecount (default 64kbytes) */ + 65792, /* mbuf bytecount (default 64kbytes) */ /* Using default sizes stops RTEMS from answering to pings. (Probably because it allocates too few buffer for an answer.). */ 263168, //180584, /* mbuf cluster bytecount (default 128kbytes) */ "midam_deska", /* hostname (default BOOTP) */ -- 2.39.2