summaryrefslogtreecommitdiff
path: root/navit/plugin/j1850/j1850.c
diff options
context:
space:
mode:
Diffstat (limited to 'navit/plugin/j1850/j1850.c')
-rw-r--r--navit/plugin/j1850/j1850.c225
1 files changed, 104 insertions, 121 deletions
diff --git a/navit/plugin/j1850/j1850.c b/navit/plugin/j1850/j1850.c
index 046087fbe..20a4a25e8 100644
--- a/navit/plugin/j1850/j1850.c
+++ b/navit/plugin/j1850/j1850.c
@@ -18,7 +18,7 @@
* Boston, MA 02110-1301, USA.
*/
-/*
+/*
This plugin implements a small subset of the SAE j1850 protocal used in some cars.
So far the code assumes that it is run on Linux. It allows Navit to read the steering
wheel inputs and some metrics like RPM or the fuel tank level
@@ -105,13 +105,11 @@ void rand_str(char *dest, size_t length) {
* Write the cmd to the serial port
*
*/
-void write_to_serial_port(unsigned char *cmd, int device)
-{
- int n_written = 0;
- do {
+void write_to_serial_port(unsigned char *cmd, int device) {
+ int n_written = 0;
+ do {
n_written += write( device, &cmd[n_written], 1 );
- }
- while (cmd[n_written-1] != '\r' && n_written > 0);
+ } while (cmd[n_written-1] != '\r' && n_written > 0);
dbg(lvl_info,"sent %s to the serial port",cmd);
}
@@ -122,15 +120,14 @@ void write_to_serial_port(unsigned char *cmd, int device)
* @return nothing
*
* This is the main function of this plugin. It is called when navit is idle,
- * and performs the initialization of the obd device if needed, then reads
- * one char each time it is called, and puts this char in a buffer. When it
+ * and performs the initialization of the obd device if needed, then reads
+ * one char each time it is called, and puts this char in a buffer. When it
* reads an EOL character, the buffer is parsed, and the appropriate action is
* taken. The buffer is then cleared and we start over.
*
*/
static void
-j1850_idle(struct j1850 *j1850)
-{
+j1850_idle(struct j1850 *j1850) {
int n; // used to keep track of the numbers of char read from the device
int value; // used to convert the ascii char to an int
char buf = '\0'; // the buffer where we store the char read from the device
@@ -150,16 +147,14 @@ j1850_idle(struct j1850 *j1850)
};
// Make sure we sent all init commands before trying to read
- if ( init_string[j1850->init_string_index])
- {
+ if ( init_string[j1850->init_string_index]) {
dbg(lvl_info,"Sending next init command : %s",init_string[j1850->init_string_index]);
- if (j1850->device > 0 ){
+ if (j1850->device > 0 ) {
write_to_serial_port(init_string[j1850->init_string_index++],j1850->device);
}
// Did we reach the last init command?
- if ( !init_string[j1850->init_string_index])
- {
+ if ( !init_string[j1850->init_string_index]) {
// if yes, switch to idle read instead of timed read
event_remove_timeout(j1850->idle);
j1850->idle=event_add_idle(125, j1850->callback);
@@ -170,37 +165,37 @@ j1850_idle(struct j1850 *j1850)
navit.u.navit=j1850->nav;
// If not connected, generate random messages for debugging purpose
- if (j1850->device < 0 ){
- rand_str(j1850->message,8);
- return;
+ if (j1850->device < 0 ) {
+ rand_str(j1850->message,8);
+ return;
}
-
+
n = read( j1850->device, &buf, 1 );
if(n == -1) {
- dbg(lvl_debug,"x");
+ dbg(lvl_debug,"x");
} else if (n==0) {
- dbg(lvl_debug,".");
+ dbg(lvl_debug,".");
} else {
if( buf == 13 ) {
gettimeofday(&tv, NULL);
unsigned long long millisecondsSinceEpoch =
(unsigned long long)(tv.tv_sec) * 1000 +
(unsigned long long)(tv.tv_usec) / 1000;
-
+
j1850->message[j1850->index]='\0';
FILE *fp;
fp = fopen(j1850->filename,"a");
fprintf(fp, "%llu,%s\n", millisecondsSinceEpoch, j1850->message);
- fclose(fp);
+ fclose(fp);
strncpy(header, j1850->message, 2);
header[2]='\0';
if( strncmp(header,"10",2)==0 ) {
- char * w1 = strndup(j1850->message+2, 2);
- char * w2 = strndup(j1850->message+4, 2);
- char * w3 = strndup(j1850->message+6, 2);
- j1850->engine_rpm = ((int)strtol(w1, NULL, 16) ) / 4 ;
- j1850->trans_rpm = ((int)strtol(w2, NULL, 16) ) / 4 ;
- j1850->map = (int)strtol(w3, NULL, 16);
+ char * w1 = strndup(j1850->message+2, 2);
+ char * w2 = strndup(j1850->message+4, 2);
+ char * w3 = strndup(j1850->message+6, 2);
+ j1850->engine_rpm = ((int)strtol(w1, NULL, 16) ) / 4 ;
+ j1850->trans_rpm = ((int)strtol(w2, NULL, 16) ) / 4 ;
+ j1850->map = (int)strtol(w3, NULL, 16);
} else if( strncmp(header,"3D",2)==0 ) {
if (strcmp(j1850->message, "3D110000EE") == 0) {
// noise
@@ -226,12 +221,12 @@ j1850_idle(struct j1850 *j1850)
dbg(lvl_error,"Got button from %s", j1850->message);
}
} else if( strncmp(header,"72",2)==0 ) {
- char * data=strndup(j1850->message+2, 8);
- j1850->odo=((int)strtol(data, NULL, 16) )/8000;
+ char * data=strndup(j1850->message+2, 8);
+ j1850->odo=((int)strtol(data, NULL, 16) )/8000;
} else if( strncmp(header,"90",2)==0 ) {
} else if( strncmp(header,"A4",2)==0 ) {
- char * w1 =strndup(j1850->message+2, 4);
- j1850->tank_level = ((int)strtol(w1, NULL, 16) ) / 4 ;
+ char * w1 =strndup(j1850->message+2, 4);
+ j1850->tank_level = ((int)strtol(w1, NULL, 16) ) / 4 ;
} else {
// printf(" ascii: %i [%s] with header [%s]\n",buf, response, header);
}
@@ -240,7 +235,7 @@ j1850_idle(struct j1850 *j1850)
j1850->index=0;
} else {
value=buf-48;
- if(value==-16 || buf == 10 ){
+ if(value==-16 || buf == 10 ) {
//space and newline, discard
return;
} else if (value>16) {
@@ -257,12 +252,12 @@ j1850_idle(struct j1850 *j1850)
j1850->index++;
}
// printf("{%c:%i}", buf,value);
- }
- }
+ }
+ }
}
/**
- * @brief Draws the j1850 OSD
+ * @brief Draws the j1850 OSD
* @param[in] j1850 - the j1850 struct containing the state of the plugin
* nav - the navit object
* v - the vehicle object
@@ -274,8 +269,7 @@ j1850_idle(struct j1850 *j1850)
*/
static void
osd_j1850_draw(struct j1850 *this, struct navit *nav,
- struct vehicle *v)
-{
+ struct vehicle *v) {
osd_std_draw(&this->osd_item);
struct point p, bbox[4];
@@ -301,12 +295,11 @@ osd_j1850_draw(struct j1850 *this, struct navit *nav,
*
*/
static void
-osd_j1850_init(struct j1850 *this, struct navit *nav)
-{
+osd_j1850_init(struct j1850 *this, struct navit *nav) {
struct color c;
osd_set_std_graphic(nav, &this->osd_item, (struct osd_priv *)this);
-
+
// Used when debugging or when the device is offline
this->orange = graphics_gc_new(this->osd_item.gr);
c.r = 0xFFFF;
@@ -347,13 +340,11 @@ osd_j1850_init(struct j1850 *this, struct navit *nav)
* Sends 'cmd' and reads the reply from the device
*
*/
-void send_and_read(unsigned char *cmd, int USB)
-{
+void send_and_read(unsigned char *cmd, int USB) {
int n_written = 0;
do {
n_written += write( USB, &cmd[n_written], 1 );
- }
- while (cmd[n_written-1] != '\r' && n_written > 0);
+ } while (cmd[n_written-1] != '\r' && n_written > 0);
int n = 0;
char buf = '\0';
@@ -361,25 +352,23 @@ void send_and_read(unsigned char *cmd, int USB)
/* Whole response*/
char response[255];
- do
- {
- n = read( USB, &buf, 1 );
- if(n == -1) {
+ do {
+ n = read( USB, &buf, 1 );
+ if(n == -1) {
dbg(lvl_debug,"x");
- } else if (n==0) {
+ } else if (n==0) {
dbg(lvl_debug,".");
- } else {
+ } else {
dbg(lvl_debug,"[%s]", &buf);
- }
- }
- while( buf != '\r' && n > 0);
+ }
+ } while( buf != '\r' && n > 0);
if (n < 0) {
- dbg(lvl_error,"Read error");
+ dbg(lvl_error,"Read error");
} else if (n == 0) {
- dbg(lvl_error,"Nothing to read?");
+ dbg(lvl_error,"Nothing to read?");
} else {
- dbg(lvl_error,"Response : ");
+ dbg(lvl_error,"Response : ");
}
}
@@ -393,61 +382,57 @@ void send_and_read(unsigned char *cmd, int USB)
*
*/
void
-j1850_init_serial_port(struct j1850 *j1850)
-{
- j1850->callback=callback_new_1(callback_cast(j1850_idle), j1850);
- // Fixme : we should read the device path from the config file
- j1850->device = open( "/dev/ttyUSB0", O_RDWR| O_NOCTTY );
- if ( j1850->device < 0 )
- {
- dbg(lvl_error,"Can't open port");
- j1850->idle=event_add_timeout(100, 1, j1850->callback);
- return;
- }
-
- struct termios tty;
- struct termios tty_old;
- memset (&tty, 0, sizeof tty);
-
- /* Error Handling */
- if ( tcgetattr ( j1850->device, &tty ) != 0 )
- {
- dbg(lvl_error,"Error");
- return;
- }
-
- /* Save old tty parameters */
- tty_old = tty;
-
- /* Set Baud Rate */
- cfsetospeed (&tty, (speed_t)B115200);
- cfsetispeed (&tty, (speed_t)B115200);
-
- /* Setting other Port Stuff */
- tty.c_cflag &= ~PARENB; // Make 8n1
- tty.c_cflag &= ~CSTOPB;
- tty.c_cflag &= ~CSIZE;
- tty.c_cflag |= CS8;
-
- tty.c_cflag &= ~CRTSCTS; // no flow control
- tty.c_cc[VMIN] = 1; // read doesn't block
- tty.c_cc[VTIME] = 10; // 0.5 seconds read timeout
- tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
-
- /* Make raw */
- cfmakeraw(&tty);
-
- /* Flush Port, then applies attributes */
- tcflush( j1850->device, TCIFLUSH );
- if ( tcsetattr ( j1850->device, TCSANOW, &tty ) != 0)
- {
- dbg(lvl_error,"Flush error");
- return;
- }
-
- dbg(lvl_error,"Port init ok");
- // For the init part, we want to wait 1sec before each init string
- j1850->idle=event_add_timeout(1000, 1, j1850->callback);
+j1850_init_serial_port(struct j1850 *j1850) {
+ j1850->callback=callback_new_1(callback_cast(j1850_idle), j1850);
+ // Fixme : we should read the device path from the config file
+ j1850->device = open( "/dev/ttyUSB0", O_RDWR| O_NOCTTY );
+ if ( j1850->device < 0 ) {
+ dbg(lvl_error,"Can't open port");
+ j1850->idle=event_add_timeout(100, 1, j1850->callback);
+ return;
+ }
+
+ struct termios tty;
+ struct termios tty_old;
+ memset (&tty, 0, sizeof tty);
+
+ /* Error Handling */
+ if ( tcgetattr ( j1850->device, &tty ) != 0 ) {
+ dbg(lvl_error,"Error");
+ return;
+ }
+
+ /* Save old tty parameters */
+ tty_old = tty;
+
+ /* Set Baud Rate */
+ cfsetospeed (&tty, (speed_t)B115200);
+ cfsetispeed (&tty, (speed_t)B115200);
+
+ /* Setting other Port Stuff */
+ tty.c_cflag &= ~PARENB; // Make 8n1
+ tty.c_cflag &= ~CSTOPB;
+ tty.c_cflag &= ~CSIZE;
+ tty.c_cflag |= CS8;
+
+ tty.c_cflag &= ~CRTSCTS; // no flow control
+ tty.c_cc[VMIN] = 1; // read doesn't block
+ tty.c_cc[VTIME] = 10; // 0.5 seconds read timeout
+ tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
+
+ /* Make raw */
+ cfmakeraw(&tty);
+
+ /* Flush Port, then applies attributes */
+ tcflush( j1850->device, TCIFLUSH );
+ if ( tcsetattr ( j1850->device, TCSANOW, &tty ) != 0) {
+ dbg(lvl_error,"Flush error");
+ return;
+ }
+
+ dbg(lvl_error,"Port init ok");
+ // For the init part, we want to wait 1sec before each init string
+ j1850->idle=event_add_timeout(1000, 1, j1850->callback);
}
/**
@@ -463,8 +448,7 @@ j1850_init_serial_port(struct j1850 *j1850)
*/
static struct osd_priv *
osd_j1850_new(struct navit *nav, struct osd_methods *meth,
- struct attr **attrs)
-{
+ struct attr **attrs) {
struct j1850 *this=g_new0(struct j1850, 1);
this->nav=nav;
time_t current_time = time(NULL);
@@ -497,10 +481,9 @@ osd_j1850_new(struct navit *nav, struct osd_methods *meth,
*
*/
void
-plugin_init(void)
-{
- struct attr callback,navit;
- struct attr_iter *iter;
+plugin_init(void) {
+ struct attr callback,navit;
+ struct attr_iter *iter;
- plugin_register_category_osd("j1850", osd_j1850_new);
+ plugin_register_category_osd("j1850", osd_j1850_new);
}