François Gautrais 9 éve
szülő
commit
da12607b37
15 módosított fájl, 461 hozzáadás és 66 törlés
  1. 4 4
      MakefileRpi
  2. 67 0
      backend.c
  3. 16 0
      backend.h
  4. 18 7
      bus.c
  5. 227 0
      config.c
  6. 26 0
      config.cfg
  7. 55 0
      config.h
  8. 4 1
      io.h
  9. 1 0
      mount.c
  10. 6 0
      mount.h
  11. 3 39
      recv.c
  12. 14 12
      send.c
  13. 4 1
      stub.c
  14. 11 1
      timer.c
  15. 5 1
      timer.h

+ 4 - 4
MakefileRpi

@@ -1,12 +1,12 @@
-OBJ=bus.o stub.o timer.o recv.o
+OBJ=bus.o stub.o timer.o recv.o backend.o config.o
 TARGET=recv
-CROSS_COMPILE=/home/ptitcois/Programmation/rpi/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi-
+#CROSS_COMPILE=/home/ptitcois/Programmation/rpi/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi-
 CC=$(CROSS_COMPILE)gcc
 OBJCOPY=$(CROSS_COMPILE)objcopy
 
-CFLAGS=-I/home/ptitcois/src/wiringPi/wiringPi/ -g
+CFLAGS= -g 
 
-LDFLAGS=-lwiringPi -L/home/ptitcois/src/wiringPi/wiringPi/ -lpthread -Llib
+LDFLAGS= -lpthread -Llib -lrt
 
 all: $(TARGET)
 

+ 67 - 0
backend.c

@@ -0,0 +1,67 @@
+#include "backend.h"
+
+
+#include "bus.h"
+#ifdef __linux__
+#include "config.h"
+int data[3]={0};
+#include "io.h"
+
+int do_pid_file(config_t* cfg)
+{
+	const char* file = config_get_string(cfg, "misc.pidfile");
+	FILE* f;
+	int x;
+	f=fopen(file, "w+");
+	if(!f)
+	{
+		fprintf(stderr, "Erreur unable to open pid file '%s'\n", file);
+		perror(" ");
+		return -1;
+	}
+	
+	x=getpid();
+	
+	fprintf(f, "%d\n", x);
+	fclose(f);
+	return 0;
+}
+
+
+int main_loop()
+{
+	config_t cfg;
+	bus_t bus;
+	pthread_t thread;
+	FILE* file = NULL;
+	struct timeval tv;
+	time(NULL);
+	setup_io();
+	config_init(&cfg, "config.cfg");
+	config_print(&cfg);
+	bus_init_recv(&bus, config_get_int(&cfg, "pin.data"), 
+											config_get_int(&cfg, "pin.clk"), 
+											config_get_int(&cfg, "pin.ack"));
+	init_read_pin(config_get_int(&cfg, "pin.data"), 
+								config_get_int(&cfg, "pin.clk"), 
+								config_get_int(&cfg, "pin.ack"));
+								
+	pthread_create(&thread, NULL, bus_recv_thread, &bus);
+	while(1)
+	{
+		uint32_t c;
+		double w;
+		char buffer[16];
+		bus_read(&bus, &c, 4);
+		
+		
+		printf("%ld' s  %ld ms %'d us \n", c/1000000, c%1000000/1000, c%1000);
+		w=3600.0/(((double)c)/1000000.0);
+		printf("\t%.2lf W %.2lf A\n\n", w, w/220);
+	}
+
+
+}
+
+
+#endif

+ 16 - 0
backend.h

@@ -0,0 +1,16 @@
+#ifndef BACKEND_H
+#define BACKEND_H
+
+#include "io.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+
+
+#include "bus.h"
+
+int main_loop();
+
+#endif

+ 18 - 7
bus.c

@@ -1,7 +1,6 @@
 #include "bus.h"
 #include "timer.h"
-
-
+#include "io.h"
 void file_produce(file_t* f, char x)
 {
 	f->buffer[f->t]=x;
@@ -114,11 +113,23 @@ void bus_write(bus_t* bus, const void* x, int length)
 
 void bus_read(bus_t* bus, void* x, int length)
 {
-	uint8_t *iter=(uint8_t*)x;
-	int i;
-	if(bus->ismaster) return;
-	for(i=0; i<length; i++)
-	  iter[i]=file_consume(&bus->buffer);
+	#ifdef __linux__
+		#ifdef ARM
+			uint8_t *iter=(uint8_t*)x;
+			int i;
+			if(bus->ismaster) return;
+			for(i=0; i<length; i++)
+				iter[i]=file_consume(&bus->buffer);
+		#else
+			static uint64_t old=0;
+			int i;
+			struct timeval tv;
+			sleep(1);
+			gettimeofday(&tv, NULL);
+			*(uint32_t*)x=(tv.tv_sec-old)*1000000;
+			old=tv.tv_sec;
+		#endif
+	#endif
 }
 
 

+ 227 - 0
config.c

@@ -0,0 +1,227 @@
+#include "config.h"
+
+void config_load_default(config_t *cfg)
+{
+	config_add_int(cfg, "pin.data", 2); 
+	config_add_int(cfg, "pin.clk", 3); 
+	config_add_int(cfg, "pin.ack", 4);
+	config_add_int(cfg, "cache.size", 4096);
+	
+}
+
+opt_t* _config_get(config_t* cfg, const char* name)
+{
+	int i;
+	for(i=0; i<cfg->n; i++)
+	{
+		if(!strcmp(name, cfg->opts[i].name))
+			return &cfg->opts[i];
+	}
+	return NULL;
+}
+
+int _fgetc(config_t *cfg, FILE* f)
+{
+	int com=0, c;
+	do{
+		c = fgetc(f);
+		if(c=='\n')
+		{
+			cfg->c=0;
+			cfg->line++;
+			com=0;
+		}else cfg->c++;
+		if(c=='#')
+			com=1;
+	}while(com);
+	return c;
+	
+}
+
+int _config_next_opt(config_t *cfg, FILE* f)
+{
+	char name[64];
+	int c, i;
+	while((c=_fgetc(cfg,f))==' ' || c=='\t' || c=='\r' || c=='\n');
+	if(c==EOF)
+		return 0;
+	
+	name[0]=c;
+	for(i=1; (c=_fgetc(cfg,f))!=' ' && c!='\t' && c!='\r' && c!='\n' && c!='=' && c!=EOF; i++)
+		name[i]=c;
+	name[i]=0;
+	if(c==EOF)
+	{
+		fprintf(stderr, "Erreur de syntaxe: fin de fichier inattendue l %d:%d\n", cfg->line, cfg->c);
+		return 0;
+	}
+	
+	while( c!='=' && (c=_fgetc(cfg,f))!='=' && c!=EOF);
+	if(c==EOF)
+	{
+		fprintf(stderr, "Erreur de syntaxe: fin de fichier inattendue l %d:%d\n", cfg->line, cfg->c);
+		return 0;
+	}
+	
+	while((c=_fgetc(cfg,f))==' ' || c=='\t' || c=='\r' || c=='\n');
+	if(c==EOF)
+	{
+		fprintf(stderr, "Erreur de syntaxe: fin de fichier inattendue l %d:%d\n", cfg->line, cfg->c);
+		return 0;
+	}
+	
+	if(c=='"')
+	{
+		char buffer[4096];
+		for(i=0; (c=_fgetc(cfg, f))!='"'; i++)
+			buffer[i]=c;
+		buffer[i]=0;
+		config_add_string(cfg, name, buffer);
+	}
+	else if(c>='0' && c<='9')
+	{
+		char buffer[128];
+		int isfloat=0;
+		buffer[0]=c;
+		for(i=1; (c=_fgetc(cfg, f))>='0' && c<='9' || c=='.' ; i++)
+		{
+			if(c=='.') isfloat=1;
+			buffer[i]=c;
+		}
+		buffer[i]=0;
+		if(isfloat)
+			config_add_float(cfg, name, atof(buffer));
+		else
+			config_add_int(cfg, name, atoi(buffer));
+	}
+	else
+	{
+		fprintf(stderr, "Erreur de syntaxe: attendue STRING, INT ou  FLOAT, token '%c'(%d), ligne %d:%d\n", c, c, cfg->line, cfg->c);
+		return 0;
+	}
+	while( c!=';' && (c=_fgetc(cfg, f))!=';' && c!=EOF);
+	if(c==EOF)
+	{
+		fprintf(stderr, "Erreur de syntaxe: fin de fichier inattendue l %d:%d\n", cfg->line, cfg->c);
+		return 0;
+	}
+	return 1;
+	
+}
+
+
+void config_init(config_t *cfg, const char* path)
+{
+	FILE* f;
+	cfg->n=0;
+	cfg->c=0;
+	cfg->line=0;
+	f=fopen(path, "r");
+	if(!f)
+	{
+		fprintf(stderr, "WARNING: Unable to open config file '%s'", path);
+		perror(" ");
+		fprintf(stderr, "Loading default config\n");
+		return;
+	}
+	
+	while(_config_next_opt(cfg, f));
+	
+	fclose(f);
+}
+
+void config_add_int(config_t* cfg, const char* name, int val)
+{
+	union opt_value value;
+	value._int=val;
+	config_add(cfg, name, OPT_TYPE_INT, value);
+}
+
+void config_add_float(config_t* cfg, const char* name, double val)
+{
+	union opt_value value;
+	value._float=val;
+	config_add(cfg, name, OPT_TYPE_FLOAT, value);
+}
+
+void config_add_string(config_t* cfg, const char* name, const char* val)
+{
+	union opt_value value;
+	char * x;
+	x=malloc(strlen(val)+1);
+	memcpy(x, val, strlen(val)+1);
+	value._string=x;
+	config_add(cfg, name, OPT_TYPE_STRING, value);
+}
+
+void config_add(config_t* cfg, const char* name, int type,  union opt_value val)
+{
+	opt_t opt, *o;
+	strcpy(opt.name, name);
+	opt.type=type;
+	opt.val = val;
+	
+	if(o=_config_get(cfg, name))
+		*o=opt;
+	else cfg->opts[cfg->n++]=opt;
+}
+
+void config_free(config_t *cfg)
+{
+	int i;
+	for(i=0; i<cfg->n; i++)
+		if(cfg->opts[i].type==OPT_TYPE_STRING)
+			free(cfg->opts[i].val._string);
+}
+
+
+
+
+const char* config_get_string(config_t* cfg, const char* name)
+{
+	opt_t* op = _config_get(cfg, name);
+	if(!op || op->type!=OPT_TYPE_STRING) return "(null)";
+	return op->val._string;
+}
+
+
+int config_get_int(config_t* cfg, const char* name)
+{
+	opt_t* op = _config_get(cfg, name);
+	if(!op || op->type!=OPT_TYPE_INT) return -1;
+	return op->val._int;
+}
+
+
+double config_get_float(config_t* cfg, const char* name)
+{
+	opt_t* op = _config_get(cfg, name);
+	if(!op || op->type!=OPT_TYPE_FLOAT) return 0.0;
+	return op->val._float;
+}
+
+
+
+int config_get_type(config_t* cfg, const char* name)
+{
+	opt_t* op = _config_get(cfg, name);
+	if(!op) return OPT_TYPE_ERROR;
+	return op->type;
+}
+
+
+void config_print(config_t *cfg)
+{
+	int i;
+	for(i=0; i<cfg->n; i++)
+	{
+		opt_t * o = &cfg->opts[i];
+		if(o->type==OPT_TYPE_FLOAT)
+			printf("%s=%f\n", o->name, o->val._float);
+		if(o->type==OPT_TYPE_INT)
+			printf("%s=%d\n", o->name, o->val._int);
+		if(o->type==OPT_TYPE_STRING)
+			printf("%s=\"%s\"\n", o->name, o->val._string);
+	}
+}
+

+ 26 - 0
config.cfg

@@ -0,0 +1,26 @@
+#pins
+pin.data=2;
+pin.clk = 3;   
+pin.ack= 4;
+
+#cache
+cache.size =2048;
+cache.file="/tmp/compteur_elec.cache";
+
+#Samba mount
+mount=1;
+mount.ip="192.168.0.88";
+mount.src="\\\\nas\\";
+mount.dst="/media/samba";
+mount.username="ptitcois";
+mount.password="e1010898";
+
+#storage
+storage.location="/media/samba";
+storage.data="data";
+storage.years="years";
+storage.monthes="monthes";
+storage.days="days";
+
+
+misc.pidfile="/tmp/compteur_elec.cache";

+ 55 - 0
config.h

@@ -0,0 +1,55 @@
+#ifndef CONFIG_H
+#define CONFIG_H
+
+#ifdef __linux__
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#define OPT_TYPE_ERROR 	0
+#define OPT_TYPE_INT 	  1
+#define OPT_TYPE_FLOAT  2
+#define OPT_TYPE_STRING 3
+
+union opt_value
+{
+    int _int;
+    double _float;
+    char* _string;
+};
+
+typedef struct 
+{
+	char name[64];
+	int type;
+	union opt_value val;	
+} opt_t;
+
+typedef struct 
+{
+	opt_t opts[128];
+	int n;
+	int line;
+	int c;
+} config_t;
+
+
+void config_init(config_t *cfg, const char* path);
+void config_add_int(config_t* cfg, const char* name, int val);
+void config_add_float(config_t* cfg, const char* name, double val);
+void config_add_string(config_t* cfg, const char* name, const char* val);
+void config_add(config_t* cfg, const char* name, int type,  union opt_value val);
+
+const char* config_get_string(config_t* cfg, const char* name);
+int config_get_int(config_t* cfg, const char* name);
+double config_get_float(config_t* cfg, const char* name);
+
+int config_get_type(config_t* cfg, const char* name);
+
+void config_free(config_t *cfg);
+void config_print(config_t *cfg);
+
+#endif
+
+#endif

+ 4 - 1
io.h

@@ -2,15 +2,18 @@
 #define IO_H
 
 #ifdef __linux__
+	
+
 	#include <unistd.h>
 	#include <unistd.h>
 	#include <sys/types.h>
 	#include <sys/stat.h>
 	#include <sys/types.h>
 	#include <sys/stat.h>
+	#include <time.h>
 	#include <fcntl.h>
 	#include <pthread.h>
-	#define ARM 1
+	//#define ARM 1
 	#define BCM2708_PERI_BASE        0x20000000
 	#define GPIO_BASE                (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
  

+ 1 - 0
mount.c

@@ -0,0 +1 @@
+#include "mount.h"

+ 6 - 0
mount.h

@@ -0,0 +1,6 @@
+#ifndef MOUNT_H
+#define MOUNT_H
+
+
+
+#endif

+ 3 - 39
recv.c

@@ -1,45 +1,9 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdint.h>
-
-#include "bus.h"
-#ifdef __linux__
-
-#define PIN_DATA 2
-#define PIN_CLK  3
-#define PIN_ACK  4
-int data[3]={0};
-#include "io.h"
-
+#include "backend.h"
+#include "config.h"
 
 
 
 int main()
 {
-	bus_t bus;
-	pthread_t thread;
-	FILE* file = NULL;
-	struct timeval tv;
-	setup_io();
-	bus_init_recv(&bus, PIN_DATA, PIN_CLK, PIN_ACK);
-	init_read_pin(PIN_DATA, PIN_CLK, PIN_ACK);
-	pthread_create(&thread, NULL, bus_recv_thread, &bus);
-	while(1)
-	{
-		uint32_t c;
-		double w;
-		char buffer[16];
-		bus_read(&bus, &c, 4);
-		
-		gettimeofday(&tv, NULL);
-		printf("%ld' s  %ld ms %'d us \n", c/1000000, c%1000000/1000, c%1000);
-		w=3600.0/(((double)c)/1000000.0);
-		printf("%ld : %.2lf W %.2lf\n\n", tv.tv_sec, w, w/220);
-	}
-
-
+	main_loop();
 }
-
-
-#endif

+ 14 - 12
send.c

@@ -13,19 +13,21 @@
 #include "timer.h"
 
 
+#ifndef __linux__
+	void init_interrupt()
+	{
+		MCUSR = 0;
+		wdt_disable();
+		DDRB &= ~(1<<PB4); /* Set PB0 as input */
+		PORTB |= (1<<PB4); /* Activate PULL UP resistor */ 
+
+		PCMSK |= (1 << PCINT4); /* Enable PCINT0 */
+		GIMSK |= (1 << PCIE); /* Activate interrupt on enabled PCINT7-0 */
+		sei ();
+		
+	}
+#endif
 
-void init_interrupt()
-{
-	MCUSR = 0;
-	wdt_disable();
-	DDRB &= ~(1<<PB4); /* Set PB0 as input */
-	PORTB |= (1<<PB4); /* Activate PULL UP resistor */ 
-
-	PCMSK |= (1 << PCINT4); /* Enable PCINT0 */
-	GIMSK |= (1 << PCIE); /* Activate interrupt on enabled PCINT7-0 */
-	sei ();
-	
-}
 int main()
 {
 	unsigned long last=0;

+ 4 - 1
stub.c

@@ -49,7 +49,10 @@
 	 
 	 
 	} // setup_io
-	
+#else 
+	void setup_io()
+	{
+	}
 #endif
 
 void write_pin(int pin, int value)

+ 11 - 1
timer.c

@@ -1,6 +1,8 @@
 #include "timer.h"
 #include "io.h"
 
+
+
 static uint64_t __time_us;
 static uint64_t __delta_time_us;
 
@@ -31,7 +33,15 @@ void 			timer_init()
 
 uint64_t 	get_time_us()
 {
-	return millis()*1000;
+	#ifdef __linux__
+		uint64_t ret;
+		struct timespec t;
+		clock_gettime(CLOCK_REALTIME, &t);
+		ret=(t.tv_nsec+t.tv_sec*1000000000)/1000;
+		return ret;	
+	#else
+		return 0;
+	#endif
 }
 
 

+ 5 - 1
timer.h

@@ -1,7 +1,11 @@
 #ifndef TIMER_H
 #define TIMER_H
 
-#include <stdint.h>
+#include <stdint.h>	
+
+#ifdef __linux__
+	#include <time.h>
+#endif
 
 void 			timer_init();
 uint64_t 	get_time_us();