SampleSensors in C
authorBernhard Tittelbach <xro@realraum.at>
Wed, 24 Mar 2010 12:55:33 +0000 (12:55 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Wed, 24 Mar 2010 12:55:33 +0000 (12:55 +0000)
sensor_graph/get_sensor_light.pl [changed mode: 0644->0755]
sensor_graph/sample_sensor.lua [deleted file]
sensor_graph/sample_sensors.sh [deleted file]
serial_console/Makefile
serial_console/sample_sensors.c [new file with mode: 0644]
serial_console/sample_sensors.h [new file with mode: 0644]
serial_console/usocket.c

old mode 100644 (file)
new mode 100755 (executable)
diff --git a/sensor_graph/sample_sensor.lua b/sensor_graph/sample_sensor.lua
deleted file mode 100755 (executable)
index 31cdfc3..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-#!/usr/bin/lua
-require('os')
-require('string')
-require('io')
-
-function parse_value(str)
-  last_temp = 0.0
-  last_light = 0
-  if string.find(str,"temp0: Temp C:") then
-    last_temp = tonumber(string.sub(str,15))
-    os.execute(string.format("rrdtool update /home/sensortemp.rrd -t temp N:%f", last_temp))
-    --print(string.format("t: %f Grad Celsius",last_temp))
-  end
-  if string.find(str,"photo0: Photo:") then
-    last_light = tonumber(string.sub(str,15))
-    os.execute(string.format("rrdtool update /home/sensorlight.rrd -t light N:%d", last_light))
-    --print(string.format("p: %d",last_light))
-  end
-end
-
-while 1 do
-   local line = io.read("*line")
-    if line then 
-      parse_value(line) 
-    else
-      break
-    end
-end
diff --git a/sensor_graph/sample_sensors.sh b/sensor_graph/sample_sensors.sh
deleted file mode 100755 (executable)
index 6474ed5..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-#!/bin/sh
-sleep 2
-echo -e "listen sensor\n" | usocket /var/run/powersensordaemon/cmd.sock -n | ./sample_sensor.lua &>/dev/null &
-PID1=$!
-echo -e "listen movement\n" | usocket /var/run/powersensordaemon/cmd.sock -n >> /tmp/movement.tmp &
-PID2=$!
-trap "kill $PID1 $PID2" 0 INT QUIT
-while sleep 30; do
-  L=$(wc -l /tmp/movement.tmp | sed 's/[^0-9]*//g')
-  echo -n > /tmp/movement.tmp
-  rrdtool update /home/sensormovement.rrd -t movement N:$L
-done
index 15c62c6..f8b3413 100644 (file)
@@ -1,10 +1,11 @@
 TARGET1:=serial
 TARGET2:=usocket
+TARGET3:=sample_sensors
 CFLAGS:= -Wall
 
-all: $(TARGET1) $(TARGET2)
+all: $(TARGET1) $(TARGET2) $(TARGET3)
 
 .PHONY: clean all
 
 clean: 
-       rm -f $(TARGET1) $(TARGET2)
\ No newline at end of file
+       rm -f $(TARGET1) $(TARGET2) $(TARGET3)
diff --git a/serial_console/sample_sensors.c b/serial_console/sample_sensors.c
new file mode 100644 (file)
index 0000000..d81daa1
--- /dev/null
@@ -0,0 +1,161 @@
+#include "sample_sensors.h"
+
+unsigned int collect_data(char *buffer, unsigned int size)
+{
+  char *cmd;
+  if (size >= 8 && strncmp("movement", buffer, 8) == 0)
+    return 1;
+  
+  if (size > 16 && strncmp("temp0:", buffer, 5) == 0)
+  {
+    if (asprintf(&cmd, "rrdtool update %s -t temp N:%s", rrd_temp_, buffer + 15))
+    {
+      /*printf("%s\n",cmd);*/
+      system(cmd);
+      free(cmd);
+    }
+  }
+  
+  if (size > 16 && strncmp("photo0:", buffer, 6) == 0)
+  {
+    if (asprintf(&cmd, "rrdtool update %s -t light N:%s", rrd_light_, buffer + 15))
+    {
+      /*printf("%s\n",cmd);*/
+      system(cmd);
+      free(cmd);
+    }
+  }
+  return 0;  
+}
+
+void  sample_sensors(int fd)
+{
+  if (fd < 3)
+    return;
+  struct timespec timeout;
+  fd_set fds_r;
+  unsigned int const buffer_size=1024;
+  unsigned int buffer_offset=0;
+  char buffer[1024];
+  char *cmd;
+  unsigned int movement_count=0;
+  int num_byte=0;
+  time_t last_sample_time, curr_time;
+  
+  send(fd,"listen sensor\n",14,0);
+  send(fd,"listen movement\n",16,0);
+  
+  FD_ZERO(&fds_r);
+  FD_SET(fd,&fds_r);
+  timeout.tv_sec=1;
+  timeout.tv_nsec=0;
+  last_sample_time=time(0);
+  while (select(fd+1,&fds_r,0,0,0) > 0)
+  {
+    curr_time=time(0);
+    if (FD_ISSET(fd,&fds_r))
+    {
+      if ((num_byte = recv(fd, buffer+buffer_offset , buffer_size - buffer_offset ,0)) > 0)
+      {
+        buffer_offset+=num_byte;
+      }
+      
+      if (num_byte == 0 || (num_byte <0 && errno != EAGAIN))
+        return;
+      
+      char linebreak_found=0;
+      if (buffer_offset > 0)
+      {
+        do
+        {
+          int c=0;
+          linebreak_found=0;
+          for (c=0; c < buffer_offset; c++)
+            if (buffer[c] == '\n')
+            {
+              buffer[c]='\0';
+              linebreak_found=1;
+              break;
+            }
+            
+          if (linebreak_found)
+          {
+            movement_count += collect_data(buffer, buffer_offset+c);
+            if (c < buffer_offset)
+            {
+              memmove(buffer, buffer + c + 1, buffer_size - c - 1);
+            }
+            buffer_offset -= c + 1;
+          }
+        } while (linebreak_found);
+      }
+      
+    }
+    
+    if (curr_time - last_sample_time > sample_interval_s_)
+    {
+      last_sample_time=curr_time;
+      if (asprintf(&cmd,"rrdtool update %s -t movement N:%d", rrd_movement_, movement_count))
+      {
+        /*printf("%s\n",cmd);*/
+        system(cmd);
+        free(cmd);
+        movement_count=0;
+      }
+    }
+    
+    FD_SET(fd,&fds_r);
+    timeout.tv_sec=1;
+    timeout.tv_nsec=0;
+  }
+}
+
+int establish_socket_connection(const char* path)
+{
+  int fd = socket(AF_UNIX, SOCK_STREAM, 0);
+  if(fd < 0) {
+    fprintf(stderr, "unable to open socket: %s\n", strerror(errno));
+    return -1;
+  }
+  struct sockaddr_un local;
+  local.sun_family = AF_UNIX;
+  if(sizeof(local.sun_path) <= strlen(path)) {
+    fprintf(stderr, "socket path is to long (max %lu)\n", sizeof(local.sun_path)-1);
+    return -1;
+  }
+  strcpy(local.sun_path, path);
+  int len = SUN_LEN(&local);
+  int ret = connect(fd, (struct sockaddr*) &local, len);
+  if(ret) {
+    fprintf(stderr, "unable to connect to '%s': %s\n", local.sun_path, strerror(errno));
+    return -1;
+  }
+  return fd;
+}
+
+int main(int argc, char* argv[])
+{
+  int ret = 0;
+  int socket_fd = 0;
+  char *socket_file;
+  if (argc > 1)
+    socket_file = argv[1];
+  else
+    socket_file = default_socket_file_;
+  
+  socket_fd = establish_socket_connection(socket_file);
+  if(socket_fd)
+  {
+    sample_sensors(socket_fd);
+  }
+  else
+  {
+    fprintf(stderr, "%s error, aborting..\n", socket_file);
+    ret=2;
+  }
+
+  if(socket_fd > 0)
+    shutdown(socket_fd,SHUT_RDWR);
+  return(ret);
+}
+
diff --git a/serial_console/sample_sensors.h b/serial_console/sample_sensors.h
new file mode 100644 (file)
index 0000000..e27c32c
--- /dev/null
@@ -0,0 +1,20 @@
+#define _GNU_SOURCE
+#include <sys/select.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <termios.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/un.h>
+#include <stdlib.h>
+#include <time.h>
+
+char *default_socket_file_="/var/run/powersensordaemon/cmd.sock";
+char *rrd_temp_ = "/home/sensortemp.rrd";
+char *rrd_light_ = "/home/sensorlight.rrd";
+char *rrd_movement_ = "/home/sensormovement.rrd";
+const int sample_interval_s_ = 30;
\ No newline at end of file
index 01d5090..52817f2 100644 (file)
@@ -1,40 +1,5 @@
 #include "usocket.h"
 
-
-int set_tty_raw(int fd, struct termios *termios_prev)
-{
-  struct termios tmio;
-  
-  int ret = tcgetattr(fd, &tmio);
-  if(ret) {
-    fprintf(stderr, "Error on tcgetattr(): %s\n", strerror(errno));
-    return ret;
-  }
-  
-  memcpy(termios_prev, &tmio,sizeof(struct termios));
-  
-  cfmakeraw(&tmio);
-  
-  ret = tcsetattr(fd, TCSANOW, &tmio);
-  if(ret) {
-    fprintf(stderr, "Error on tcsetattr(): %s\n", strerror(errno));
-    return ret;
-  }  
-  
-  fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
-  
-  return 0;
-}
-
-int restore_tty(int fd, struct termios  *termios_prev)
-{
-  int ret = tcsetattr(fd, TCSANOW, termios_prev);
-  if(ret) {
-    fprintf(stderr, "Error on tcsetattr(): %s\n", strerror(errno));
-  }
-  return ret;
-}
-
 void  connect_terminal(int fd)
 {
   if (fd < 3)
@@ -104,7 +69,7 @@ int establish_socket_connection(const char* path)
   int len = SUN_LEN(&local);
   int ret = connect(fd, (struct sockaddr*) &local, len);
   if(ret) {
-    fprintf(stderr, "unable to bind to '%s': %s\n", local.sun_path, strerror(errno));
+    fprintf(stderr, "unable to connect to '%s': %s\n", local.sun_path, strerror(errno));
     return -1;
   }
   
@@ -117,7 +82,7 @@ int main(int argc, char* argv[])
   int ret = 0;
   int socket_fd = 0;
   char *socket_file;
-  //~ struct termios tmio_prev;
+  
   if (argc > 1)
     socket_file = argv[1];
   else
@@ -131,15 +96,8 @@ int main(int argc, char* argv[])
   socket_fd = establish_socket_connection(socket_file);
   if(socket_fd)
   {
-    //~ ret = set_tty_raw(STDIN_FILENO,&tmio_prev);
-    //~ if (ret)
-      //~ break;
     fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
     connect_terminal(socket_fd);
-      
-    //~ ret = restore_tty(STDIN_FILENO,&tmio_prev);
-    //~ if (ret)
-      //~ break;        
   }
   else
   {