aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan McCrohan <jmccrohan@gmail.com>2011-11-01 14:46:28 +0000
committerJonathan McCrohan <jmccrohan@gmail.com>2011-11-01 14:46:28 +0000
commit78dce59594ebe24123889d0b5a4e7f224c811ac2 (patch)
tree57a51e0a74b4b95356c0a50ff51b8be51a51a475
parent8adfff8b2419a0f79e85ba6884e507e787a313da (diff)
downloadverteco-78dce59594ebe24123889d0b5a4e7f224c811ac2.tar.gz
Integrated MAC address code into modbuslog.c
-rw-r--r--modbuslog.c57
1 files changed, 52 insertions, 5 deletions
diff --git a/modbuslog.c b/modbuslog.c
index ec0acc4..e46c9a7 100644
--- a/modbuslog.c
+++ b/modbuslog.c
@@ -32,6 +32,12 @@
#include <libconfig.h>
#include <modbus.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <string.h>
+
#define SLAVE_ID 1
#define START_ADDRESS 0
#define NUMBER_REGISTERS 14
@@ -41,18 +47,35 @@ void minute_check(int signum){
alarm(60);
}
+void getmac(unsigned char* mac_address){
+ int s;
+ struct ifreq ifr;
+
+ s = socket(PF_INET, SOCK_DGRAM, 0);
+ memset(&ifr, 0x00, sizeof(ifr));
+ strcpy(ifr.ifr_name, "eth0");
+ ioctl(s, SIOCGIFHWADDR, &ifr);
+ close(s);
+ unsigned char mac_address_internal[6];
+ sprintf(mac_address_internal,"%.2X%.2X%.2X%.2X%.2X%.2X", (unsigned char) ifr.ifr_hwaddr.sa_data[0], (unsigned char) ifr.ifr_hwaddr.sa_data[1], (unsigned char) ifr.ifr_hwaddr.sa_data[2], (unsigned char) ifr.ifr_hwaddr.sa_data[3], (unsigned char) ifr.ifr_hwaddr.sa_data[4], (unsigned char) ifr.ifr_hwaddr.sa_data[5]);
+
+ strcpy(mac_address,mac_address_internal);
+}
int main(int argc, char *argv[])
{
- const char *CONFIG_FILE = "modbuslog.cfg";
+ const char *CONFIG_FILE = "modbuslog.cfg";
config_t cfg;
//config_setting_t *setting;
+ //unsigned char* mac_address;
+ unsigned char mac_address[6];
+
const char *modbus_device_address;
int modbus_baud_rate;
int modbus_data_bits;
@@ -61,6 +84,10 @@ int main(int argc, char *argv[])
config_init(&cfg);
+ getmac(mac_address);
+ if(DEBUG){
+printf("%s\n",mac_address);
+}
if(!config_read_file(&cfg, CONFIG_FILE))
{
@@ -99,9 +126,9 @@ int main(int argc, char *argv[])
select(0, NULL, NULL, NULL, NULL);
time_t t = time(NULL);
- //int unixtime_min = (((int) t)/60*60);
- int unixtime = (int) t;
- printf("%d\n", unixtime);
+ int unixtime_min = (((int) t)/60*60);
+ //int unixtime = (int) t;
+ //printf("%d\n", unixtime);
config_setting_t *registers;
@@ -114,9 +141,29 @@ int main(int argc, char *argv[])
config_setting_t *register_element = config_setting_get_elem(registers, i);
int slaveid;
config_setting_lookup_int(register_element, "slaveid", &slaveid);
+ int intervalvalue;
+ config_setting_lookup_int(register_element, "intervalvalue", &intervalvalue);
+ printf("%d", intervalvalue);
+
+ const char *intervalunit;
+ config_setting_lookup_string(register_element, "intervalunit", &intervalunit);
+
+ printf("%c", intervalunit[0]);
+
+ int intervalduration;
+
+ if(intervalunit[0]=='H')
+ intervalduration=intervalvalue*3600;
+ else
+ intervalduration=intervalvalue*60;
+
+ if(unixtime_min%intervalduration==0){
+ printf("specified minute. %d %d\n", unixtime_min, intervalduration);
+ }
+
printf("%d ", slaveid);
}
- printf("\n");
+ printf("%d\n",unixtime_min);
}
}