/*
*
* copyright (C) 2015 zhht all rights reserved.
*@filename:vehicle_counter_alg.c
*@author:liushaofang
*@create date:2015-10-09
*@brief:read adc data and write the data into file
*
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include "includes.h"
#include "uuid.h"
#include "vehicle_counter_alg.h"
#include "gateway_bar_ctrl.h"
#include "uart_gprs_ctrl.h"
#include "zhht_errno.h"
#define TEMP_DATA_BUF_SIZE 4
int nFdAdc, nFdDataFile, nFdVehicleLogFile, nFdUartGPRS;
unsigned char g_pchUUID[UUID_LEN] = { 0 };
int main(int argc, char * argv[], char * env[])
{
long lDelayTime = 0;
uint8_t nSampleFreqInHz = 0;
gateway_bar_status nGatewayBarStatus = GATEWAY_BAR_NOT_DETERMINED;
if(argc > 1)
{
lDelayTime = (long)atoi(argv[1]);
if(lDelayTime > 0)
lDelayTime = lDelayTime * 1000;
else
lDelayTime = DEFAULT_SAMPLE_RATE;
}
else
lDelayTime = DEFAULT_SAMPLE_RATE;
if(init_system_uuid() < 0)
{
trap_system_error("device uuid initialize failed.");
}
if(init_system() < 0)
{
trap_system_error("vehicle counter system init failed.");
}
//dynamically modify adc data threshold value
while(get_adc_data_min_max_threshold(lDelayTime) < 0);
nSampleFreqInHz = (uint8_t)(1000000l/lDelayTime);
if(init_vehicle_counter_alg(nSampleFreqInHz) < 0)
{
trap_system_error("vehicle counter system user signal install failed.");
}
while(1)
{
nGatewayBarStatus = get_gateway_bar_status();
if(nGatewayBarStatus == GATEWAY_BAR_UP)
{
process_adc_data();
}
else
{
#if ZHHT_DEBUG
printf("current gateway_bar_status=%d.\n", nGatewayBarStatus);
#endif
}
usleep(lDelayTime);
}
exit_system(0);
}
int init_system(void)
{
signal(SIGINT, exit_system);
init_gateway_bar();
nFdAdc = -1;
nFdDataFile = -1;
nFdVehicleLogFile = -1;
nFdUartGPRS = -1;
nFdAdc = open(ADC_DEV_FILE_PATH, O_RDONLY);
if(nFdAdc < 0)
{
perror("adc device file open failed.\n");
return -1;
}
nFdDataFile = open(ADC_DATA_FILE_PATH, O_RDWR | O_CREAT | O_APPEND); if(nFdDataFile < 0)
if(nFdDataFile < 0)
{
perror("adc data file open failed.\n");
return -1;
}
nFdVehicleLogFile = open(VEHICLE_PASSED_LOG_FILE_PATH, O_RDWR | O_CREAT | O_APPEND);
if(nFdVehicleLogFile < 0)
{
perror("adc data file open failed.\n");
return -1;
}
nFdUartGPRS = init_uart_gprs();
if(nFdUartGPRS < 0)
{
perror("uart gprs module initialize failed.\n");
return -1;
}
return 0;
}
int init_system_uuid(void)
{
unsigned char pchUUIDBuf[UUID_LEN] = { 0 };
int nRet = 0;
nRet = get_system_uuid(pchUUIDBuf, UUID_LEN);
if(nRet < 0)
{
nRet = generate_system_uuid(pchUUIDBuf, sizeof(pchUUIDBuf));
if(nRet < 0)
{
perror("generate system uuid failed.\n");
return -1;
}
nRet = save_system_uuid(pchUUIDBuf, sizeof(pchUUIDBuf));
if(nRet < 0)
{
perror("save system uuid failed.\n");
return -1;
}
}
memset(g_pchUUID, 0, sizeof(g_pchUUID));
memcpy(g_pchUUID, pchUUIDBuf, strlen(pchUUIDBuf));
printf("device uuid is: %s \n", g_pchUUID);
return 0;
}
void trap_system_error(char * pErrorMsg)
{
uint8_t pchErrorMsgBuf[256] = { 0 };
while(1)
{
sprintf(pchErrorMsgBuf, "%s,L%d,%s is called,%s\n", __FILE__, __LINE__, __func__, pErrorMsg);
perror(pchErrorMsgBuf);
usleep(1000000);
}
}
#define INITIAL_MIN_THRESHOLD_DATA_SAMPLE_COUNT 32
#define INITIAL_MIN_THRESHOLD_DATA_SAMPLE_COUNT_BITS 5
int get_adc_data_min_max_threshold(long lDelayTime)
{
unsigned char uchDataBuf[TEMP_DATA_BUF_SIZE] = { 0 };
int nMinThresholdBuf[INITIAL_MIN_THRESHOLD_DATA_SAMPLE_COUNT] = { 0 };
int nIndex = 0, nADCMinThresholdData = 0, nReadLimit = 128;
int32_t nMinThresholdSum = 0;
float fMinThresholdVoltageData= 0.0;
for(nIndex = 0; nIndex < INITIAL_MIN_THRESHOLD_DATA_SAMPLE_COUNT * 2; nIndex++)
{
read(nFdAdc, uchDataBuf, TEMP_DATA_BUF_SIZE);
//usleep(lDelayTime);
usleep(100);
}
nMinThresholdSum = 0;
for(nIndex = 0; nIndex < INITIAL_MIN_THRESHOLD_DATA_SAMPLE_COUNT; nIndex++)
{
memset(uchDataBuf, 0, TEMP_DATA_BUF_SIZE);
while(read(nFdAdc, uchDataBuf, TEMP_DATA_BUF_SIZE) <= 0 && nReadLimit-- >= 0);
if(nReadLimit < 0)
{
trap_system_error("Initialize min threshold data value failed.");
}
#if ZHHT_DEBUG
printf("%s,L%d,%s is called, uchDataBuf[0]=0x%02x, uchDataBuf[1]=0x%02x.\n", __FILE__, __LINE__, __func__, uchDataBuf[0], uchDataBuf[1]);
#endif
nMinThresholdBuf[nIndex] = uchDataBuf[0] + (int)(uchDataBuf[1] << 8);
nMinThresholdSum += (int)(nMinThresholdBuf[nIndex]);
#if ZHHT_DEBUG
printf("%s,L%d,%s is called, nMinThresholdBuf[%d]=%d, nMinThresholdSum=%d.\n", __FILE__, __LINE__, __func__, nIndex, nMinThresholdBuf[nIndex], nMinThresholdSum);
#endif
//usleep(lDelayTime);
usleep(100);
}
nADCMinThresholdData = (int)(nMinThresholdSum >> INITIAL_MIN_THRESHOLD_DATA_SAMPLE_COUNT_BITS);
fMinThresholdVoltageData = nADCMinThresholdData/1024.0*3.3;
#if 1 //ZHHT_DEBUG
printf("adc min threshold voltage value = %d (%.2f V) \n",nADCMinThresholdData, fMinThresholdVoltageData);
#endif
set_filter_threshold_data_value(nADCMinThresholdData, MAX_THRESHOLD_DATA_VALUE);
return nADCMinThresholdData;
}
void process_adc_data(void)
{
unsigned char uchDataBuf[TEMP_DATA_BUF_SIZE] = { 0 };
unsigned char uchWriteDataBuf[64] = { 0 };
int nDataBufSize = TEMP_DATA_BUF_SIZE, nDataLen = 0;
int nIndex = 0, nADCData = 0;
float fVoltageData = 0.0;
union sigval sigval_adc_data_prepared;
memset(uchDataBuf, 0, TEMP_DATA_BUF_SIZE);
nDataLen = read(nFdAdc, uchDataBuf, TEMP_DATA_BUF_SIZE);
if(nDataLen <= 0)
{
perror("adc data read failed.\n");
get_continue_cmd();
}
sigval_adc_data_prepared.sival_ptr = uchDataBuf;
if(sigqueue(getpid(), SIG_ADC_DATA_PREPARED, sigval_adc_data_prepared) == -1)
{
#if ZHHT_DEBUG
printf("%s,L%d,%s is called, signal %d send failed.\n", __FILE__, __LINE__, __func__, SIG_ADC_DATA_PREPARED);
#endif
}
else
{
#if 0 //ZHHT_DEBUG
printf("%s,L%d,%s is called, signal %d send succeed.\n", __FILE__, __LINE__, __func__, SIG_ADC_DATA_PREPARED);
#endif
}
nADCData = uchDataBuf[0] + (int)(uchDataBuf[1] << 8);
fVoltageData = nADCData/1024.0*3.3;
#if ZHHT_DEBUG
printf("adc voltage value = %d (%.2f V) \n",nADCData, fVoltageData);
#endif
memset(uchWriteDataBuf, 0 , sizeof(uchWriteDataBuf));
sprintf(uchWriteDataBuf, "%.2f\n",fVoltageData);
#if ZHHT_LOG_ADC_DATA
write(nFdDataFile, uchWriteDataBuf, strlen(uchWriteDataBuf));
#endif
}
void get_continue_cmd(void)
{
char chData = '\0';
do
{
printf("continue read ADC(Y/N)?\n");
scanf("%c",&chData);
getchar();
}while(chData != 'Y' && chData != 'y');
}
void signal_vehicle_passed_handler(int nSignum, siginfo_t * pSigInfo, void * pSigAction)
{
time_t pTime;
struct tm * pLocalTime;
int nVehiclePassed = pSigInfo->si_int;
uint8_t uchLogMesg[128] = { 0 };
uint8_t uchVehiclePassed[128] = { 0 };
pTime = time(NULL);
pLocalTime = localtime(&pTime);
sprintf(uchVehiclePassed, "data=\"equipId=%s&value=%d\"",g_pchUUID, (uint8_t)(nVehiclePassed));
if(nFdUartGPRS > 0)
send_uart_gprs(nFdUartGPRS, uchVehiclePassed, strlen(uchVehiclePassed));
if(nVehiclePassed == 1)
{
sprintf(uchLogMesg, "%d-%02d-%02d %02d:%02d:%02d one vehicle is passed, vehicle type = %d.\n", (pLocalTime->tm_year + 1900), (pLocalTime->tm_mon + 1), pLocalTime->tm_mday, pLocalTime->tm_hour, pLocalTime->tm_min, pLocalTime->tm_sec, nVehiclePassed);
#if ZHHT_DEBUG
printf("%s", uchLogMesg);
#endif
}
else if(nVehiclePassed == 2)
{
sprintf(uchLogMesg, "%d-%02d-%02d %02d:%02d:%02d probably a black vehicle is passed, vehicle type = %d.\n", (pLocalTime->tm_year + 1900), (pLocalTime->tm_mon + 1), pLocalTime->tm_mday, pLocalTime->tm_hour, pLocalTime->tm_min, pLocalTime->tm_sec, nVehiclePassed);
#if ZHHT_DEBUG
printf("%s", uchLogMesg);
#endif
}
#if ZHHT_LOG_VEHICLE_DATA
write(nFdVehicleLogFile, uchLogMesg, strlen(uchLogMesg));
#en