Code Search for Developers
 
 
  

logvehspd.c from EmStar at Krugle


Show logvehspd.c syntax highlighted

/*
 *
 * Copyright (c) 2006 The Regents of the University of California.  All 
 * rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Neither the name of the University nor the names of its
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <stdio.h>
#include <stdlib.h>

#include "libmisc/misc.h"
#include "libdev/glib_dev.h"
#include <termios.h> /* POSIX terminal control definitions */

extern struct timeval start_time;
extern FILE *syncf;

int htoi (char hex)
{
  int i;
  char *map = "0123456789ABCDEF";
  for (i=0;map[i];i++) {
    if (hex == map[i])
      return i;
  }
  elog(LOG_WARNING, "illegal hex char %c", hex);
  return 0;
}


int sfd;

void vehicle_config(int fd)
{
  struct termios options;
  sfd = fd;

  fcntl(fd, F_SETFL, 0);

  /* get the current options */
  tcgetattr(fd, &options);

  // acceptable speed is 9600 and 38400
  cfsetispeed(&options, B38400);
  cfsetospeed(&options, B38400);

  // set control options
  options.c_cflag |= CLOCAL | CREAD; /* Enable the receiver and set local mode... */
  options.c_cflag &= ~PARENB; /* Disable parity */
  options.c_cflag &= ~CSTOPB; /* Disable 2 stop bits */
  options.c_cflag &= ~CSIZE; /* Mask the character size bits */
  options.c_cflag |= CS8;    /* Select 8 data bits */

  // set local options
  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* raw input */
 
  // set input options
  options.c_iflag &= ~(IXON | IXOFF | IXANY); /* disable software flow control */

  // set output options
  options.c_oflag &= ~OPOST;  /* raw mode */

  // set control characters options
  options.c_cc[VMIN] = 0;
  options.c_cc[VTIME] = 10;  

  /* Clear the line */
  tcflush(fd,TCIFLUSH);

  /* Update the options and do it NOW */
  if (tcsetattr(fd,TCSANOW,&options)) {
    perror("Set attribute fails");
  }
}


int request_speed(void * data, int interval, g_event_t *event)
{
  int res_l;      // return values
  unsigned char msg[10] = "010D\r";
  // message sent: 01 = get current data, 0D = veh speed
  
  // send request message
  res_l = write(sfd, msg, 5);
  if (res_l < 0) {
    elog(LOG_WARNING, "Writing fails: %m");
  }

  return EVENT_RENEW;
}


int handle_input(void *data, int fd, int cond, g_event_t *event)
{
  static char resp[256];
  static int resp_len = 0;
  
  int status = read(fd, resp+resp_len, sizeof(resp) - resp_len);
  if (status < 0) {
    if (errno != EAGAIN) 
      elog(LOG_WARNING, "read failed: %m");
    return EVENT_RENEW;
  }
  resp_len += status;
  resp[resp_len] = 0;
  
  //printf("buf='%s'\n", resp);

  while (resp_len > 0) {
    if (resp[0] == 'N') {
      elog(LOG_WARNING, "please reset autotap.. %s", resp);
    }
    if (resp[0] != '4') {
      do {
	resp_len--;
	memmove(resp, resp+1, resp_len);
      } while (resp_len > 0 && resp[0] != '>');
    }
    else {
      if (resp_len >= 8) {
	int veh_spd = htoi(resp[6]) * 16 + htoi(resp[7]);
	resp_len-=8;
	memmove(resp, resp+8, resp_len);
	struct timeval ts;
	gettimeofday(&ts, NULL);
	misc_tv_sub(&ts, &start_time);
	fprintf(syncf, "%ld.%06ld 0 0 %d speed\n",
		ts.tv_sec, ts.tv_usec, veh_spd);
	fflush(syncf);
	printf("speed %d\n", veh_spd);
      }

      else
	break;
    }
  }    

  return EVENT_RENEW;
}





See more files for this project here

EmStar

EmStar is a software system for developing and deploying wireless sensor networks involving Linux-based platforms. As the wireless sensor network community has attempted to deploy more complex designs---large-scale, long-lived systems that need self-organization and adaptivity---a number of difficult software design issues have arisen. Advances in software design have not kept pace with the capabilities of hardware. This is because designing for an adaptive, efficient, and useful sensor network has turned out to be surprisingly complex and difficult. EmStar is a Linux-based software framework, whose goal is to dramatically reduce this complexity, enabling work to be shared and reused, and simplifying and speeding the design of new sensor network applications.

Project homepage: http://cvs.cens.ucla.edu/emstar/
Programming language(s): C,Shell Script
License: other

  BUILD
  logvehspd.c
  map
  motelog.c
  readfreebob.c
  snippets
  sparkfun_accel.c
  start.run
  syncfiles.c