📄 pubfunc.c
字号:
/*************************************************************************
* File name : pubfunc.c
* Subsystem : modem卡驱动程序
* Target env : Linux
* Author : 谢红伟
* Last modified : 2002/04/30
* Description : This file contains the public functions for utility
* Copyright : zjkj
* Note :
**************************************************************************/
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/timeb.h>
#include <errno.h>
#include <newt.h>
#include "pubfunc.h"
/************************************************************************
* DEST : 将指定地址的数据倒过来存放
* RET : 缓冲地址
************************************************************************/
char *ReversalBuf(char *in_buf,int bytes)
{
char ch_tmp;
int i,j;
for (i=0,j=bytes-1; i<bytes/2; i++,j--)
{
ch_tmp = in_buf[i];in_buf[i] = in_buf[j];in_buf[j] = ch_tmp;
}
return in_buf;
}
/*************************************************************************
*DESC: 将表示时间的BCD码一串数据转成标准显示的时间格式,如:2002-01-21 12:30:59
*RET : 转换后的字节数
*************************************************************************/
int ConvertBCDToTimeStr(char *in_buf,int len,char *out_buf)
{
int i;
char tmp_buf[32],bcd_buf[32];
memset(bcd_buf,0,sizeof(bcd_buf));
memcpy(bcd_buf,in_buf,len);
for (i=0; i<6; i++)
{
tmp_buf[i] = ConvertBCDToChar(bcd_buf[i]);
if (tmp_buf[i]<0) break;
}
if (i<6)
return sprintf(out_buf,"Time data empty or error!");
else
return sprintf(out_buf,"20%.2d-%.2d-%.2d %.2d:%.2d:%.2d",tmp_buf[0],tmp_buf[1],tmp_buf[2],
tmp_buf[3],tmp_buf[4],tmp_buf[5]);
}
/*************************************************************************
*DESC: 求一个浮点数4舍5入后的整型结果
*RET : 求得的结果
*************************************************************************/
int FloatRoundToInt(float in_fdata,float *out_float)
{
int i_res;
float f_sub;
i_res = (int)in_fdata;
f_sub = in_fdata - (float)i_res;
if (f_sub > 0.5)
{
if (out_float!=NULL) *out_float = (float)(i_res+1);
return i_res+1;
}
else
{
if (out_float!=NULL) *out_float = (float)(i_res);
return i_res;
}
}
/*************************************************************************
func: ConvertStrToBCD
type: public
desc: 将一个十进制的表示数字的字符串str转成BCD码格式,结果存入buf中。
ret : returns the length of the BCD numeric string.
**************************************************************************/
int ConvertStrToBCD(char *str, char *buf)
{
int i, j, in_size;
unsigned char ch_tmp;
in_size = strlen(str); /* 待处理缓冲区字符个数,不包括'\0' */
for (i=0, j=0; i<in_size; i+=2)
{
if ((str[i] < '0') || (str[i] > '9')) break;
ch_tmp = (str[i] - '0') << 4;
if ((str[i+1] < '0') || (str[i+1] > '9'))
{
ch_tmp |= 0xf;
buf[j++] = ch_tmp;
break;
}
ch_tmp |= (str[i+1] - '0');
buf[j++] = ch_tmp;
}
return j;
}
/*************************************************************************
func: ConvertBCDToStr
type: public
desc: 将一个BCD码格式的数字串(若数字串长度为奇数,则最后一位BCD码用F填充)
转换成字符串,结果存入buf中。
ret : returns the length of the numeric string.
**************************************************************************/
int ConvertBCDToStr(char *buf, char *bcd_str, int len)
{
int i, j;
for (i=0, j=0; i<len; i++)
{
buf[j] = '0' + (((unsigned char)bcd_str[i]) >> 4);
if ((unsigned char)(buf[j] < '0') || (unsigned char)(buf[j] > '9')) break;
j ++;
buf[j] = '0' + (unsigned char)(bcd_str[i] & 0x0f);
if ((unsigned char)(buf[j] < '0') || (unsigned char)(buf[j] > '9')) break;
j ++;
}
buf[j] = '\0';
return j;
}
/*************************************************************************
func: ConvertBCDToInt
type: public
desc: 将一个BCD码格式的字符转换成十进制的整数。
ret : returns the value of the BCD character.
**************************************************************************/
char ConvertBCDToChar(char bcd_char)
{
char t1, t2;
t1 = bcd_char >> 4;
t2 = bcd_char & 0xf;
if (t1 > 9) return 0xff;
else if (t2 > 9) return t1;
return t1 * 10 + t2;
}
/*************************************************************************
func: ConvertStrToTime
type: public
desc: 将一个表示日期的字符串(按年、月、日、时、分、秒的顺序,
如"2001-08-09 18:03:30")转成time_t格式.
arg : nosec为TRUE即将传来的秒数置0;
ret : return the time_t value.
**************************************************************************/
time_t ConvertStrToTime(char *chtime,int nosec)
{
int i, j, k;
char tmpbuf[8];
int value[6];
struct tm time1;
memset((void *)value, 0, sizeof(value));
memset((void *)&time1, 0, sizeof(time1));
for (i=0, j=0, k=0; ; i++)
{
if (chtime[i]<'0' || chtime[i]>'9') /* 非数字字符 */
{
tmpbuf[j] = '\0';
value[k++] = atoi(tmpbuf);
j = 0;
if ((k >= 6) || (chtime[i] == '\0')) break;
}
else if (j < 7) tmpbuf[j++] = chtime[i];
}
/********年(number of years since 1900)********/
time1.tm_year = value[0] - TIME_START_YEAR;
/*if (time1.tm_year < 0) time1.tm_year = 0;*/
/********月(0--11)********/
time1.tm_mon = value[1] - 1;
if (time1.tm_mon < 0 || time1.tm_mon > 11) time1.tm_mon = 0;
/********日(1--31)********/
time1.tm_mday = value[2];
if (time1.tm_mday < 1 || time1.tm_mday > 31) time1.tm_mday = 1;
/********时********/
time1.tm_hour = value[3];
if (time1.tm_hour < 0 || time1.tm_hour > 23) time1.tm_hour = 0;
/********分********/
time1.tm_min = value[4];
if (time1.tm_min < 0 || time1.tm_min > 59) time1.tm_min = 0;
/********秒********/
if (nosec)
{
time1.tm_sec = 0;
}
else
{
time1.tm_sec = value[5];
if (time1.tm_sec < 0 || time1.tm_sec > 59) time1.tm_sec = 0;
}
/* 将日期转成time_t秒 */
return mktime(&time1);
}
/*************************************************************************
func: ConvertTimeToStr
type: public
desc: 将一个time_t格式的时间值转换成相应的字符串(按年、月、日、时、分、秒
的顺序,年用四个字符,其他项用两个字符表示,flag=0则是"2001-08-09 18:03:30")。
flag=1 则是"20010809"。flag=2 则是"20010809100330"。flag=3则是"08-09 18:03:30"
ret : return the string length.
**************************************************************************/
int ConvertTimeToStr(time_t t, char *buf,int flag)
{
struct tm *tm1;
tm1 = localtime(&t);
tm1->tm_year += TIME_START_YEAR;
tm1->tm_mon += 1;
if (flag==0)
return sprintf(buf, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d", tm1->tm_year, tm1->tm_mon,
tm1->tm_mday, tm1->tm_hour, tm1->tm_min, tm1->tm_sec);
else if (flag==1)
return sprintf(buf, "%.4d%.2d%.2d", tm1->tm_year, tm1->tm_mon,tm1->tm_mday);
else if(flag == 2)
return sprintf(buf, "%.4d%.2d%.2d%.2d%.2d%.2d", tm1->tm_year, tm1->tm_mon,
tm1->tm_mday, tm1->tm_hour, tm1->tm_min, tm1->tm_sec);
else if(flag == 3)
return sprintf(buf, "%.2d-%.2d %.2d:%.2d:%.2d", tm1->tm_mon,
tm1->tm_mday, tm1->tm_hour, tm1->tm_min, tm1->tm_sec);
}
/*************************************************************************
func: CalculateCRC
type: public
desc: 生成数据的CRC校验码
ret : 无返回值
**************************************************************************/
void CalculateCRC(char *data, int len, char *crc1, char *crc2)
{
char b1, b2, b3;
char c1, c2, c3;
int i, k;
if (len <= 0) return;
/* 取两字节数据 */
b1 = data[0];
b2 = ((len>=2) ? data[1] : 0);
i = 2;
do {
/* 取后一字节数据 */
b3 = ((i<len) ? data[i] : 0);
/* 处理一字节的数据 */
for (k=0; k<8; k++)
{
/* 取高位 */
c1 = b1 & 0x80;
c2 = b2 & 0x80;
c3 = b3 & 0x80;
/* 左移一位 */
b1 <<= 1;
b2 <<= 1;
b3 <<= 1;
if (c2) b1 |= 1;
if (c3) b2 |= 1;
if (c1)
{
/* 16位的模2除法 */
b1 ^= CRC_DIVIDER1;
b2 ^= CRC_DIVIDER2;
}
}
i ++;
} while (i < (len+2));
(*crc1) = b1;
(*crc2) = b2;
}
/*************************************************************************
func: specifytty
type: public
desc: 指定modem驱动程序打印信息的输出终端
ret : 失败返回-1
**************************************************************************/
int specifytty(char tty)
{
char bytes[2] = {11,0}; /* 11 is the TIOCLINUX cmd number */
bytes[1] = tty; /* the chosen console */
if (ioctl(1,TIOCLINUX,bytes)<0)
{
fprintf(stderr,"ERROR:ioctl(stdin,TIOCLINUX): %s\r\n",strerror(errno));
return -1;
}
else
{
printf("Set drive print console to %d sucessfully!\r\n",tty);
return 0;
}
}
/*********************************************************************
* 输出日志信息
*********************************************************************/
void Log( t_TdkMdmChl *p_tdk, char *how, ...)
{
char lofile_name[32] = "mdmchl-tdk.log";
char tmp_buf[32];
FILE *fp;
va_list va;
char buf[LOG_BUFSIZE], *p;
int len;
struct timeb tp;
/* Get log filename */
if(p_tdk)
snprintf(lofile_name,31,"mdmchl-%d-%d.log",p_tdk->mdm_no,p_tdk->chl_no);
/* log current day & time, and log level */
ftime(&tp);
ConvertTimeToStr(tp.time,buf,3);
sprintf(tmp_buf,":%03d",tp.millitm);
strcat(buf,tmp_buf);
len = strlen(buf);
p = &buf[len];
if(p_tdk)
p += sprintf(p," %d-%d] : ",p_tdk->mdm_no,p_tdk->chl_no);
else
p += sprintf(p," tdk] : ");
va_start (va, how);
vsnprintf (p, LOG_BUFSIZE-strlen(buf), how, va);
len = strlen(buf);
strcpy(&buf[len], "\r\n");
len += 2;
printf("%s",buf);
fp = fopen(lofile_name,"ab+");
if (fp == NULL)
{
printf("\r\nOpen log file error!\r\n");
}
else
{
//写入磁盘
fwrite(buf,sizeof(char),len,fp);
fclose(fp);
}
return;
}
/******************************************
* 将进程间的通信标志数据写入文件
******************************************/
void PutFlagToFile(int in_flag,char *filename)
{
FILE *fp;
fp = fopen(filename,"wb");
if (fp!=NULL)
{
fwrite(&in_flag,sizeof(char),sizeof(in_flag),fp);
fclose(fp);
}
return;
}
/******************************************
* 从文件中读取标志
******************************************/
int GetFlagFromFile(char *filename)
{
FILE *fp;
int i_read;
fp = fopen(filename,"rb");
if (fp!=NULL)
{
fread(&i_read,sizeof(char),sizeof(i_read),fp);
fclose(fp);
}
return i_read;
}
int xhw_strncmp(char *ch1,char *ch2,int size)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -