⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smssend.c

📁 LINUX 平台下面 纯C的 短信和邮件发送程序
💻 C
字号:

#include "smssend.h"    
#include "com.h" 


int gsmBytes2String(const unsigned char* pSrc, char* pDst, int nSrcLength); 
int gsmString2Bytes(const char* pSrc, unsigned char* pDst, int nSrcLength);
int gsmEncode7bit(const char* pSrc, unsigned char* pDst, int nSrcLength); 
int gsmEncode8bit(const char* pSrc, unsigned char* pDst, int nSrcLength); 
int gsmEncodeUcs2(const char* pSrc, unsigned char* pDst, int nSrcLength); 
int gsmInvertNumbers(const char* pSrc, char* pDst, int nSrcLength); 
int gsmSerializeNumbers(const char* pSrc, char* pDst, int nSrcLength); 
int gsmEncodePdu(const SM_PARAM* pSrc, char* pDst); 


static SM_PARAM *sp = NULL;   

int gsmString2Bytes(const char* pSrc, unsigned char* pDst, int nSrcLength)   
{   
	int i = 0;
    for (i = 0; i < nSrcLength; i += 2)   
    {            
        if ((*pSrc >= '0') && (*pSrc <= '9'))   
        {   
            *pDst = (*pSrc - '0') << 4;   
        }   
        else  
        {   
            *pDst = (*pSrc - 'A' + 10) << 4;   
        } 
        pSrc++;
		
        if ((*pSrc>='0') && (*pSrc<='9'))   
        {   
            *pDst |= *pSrc - '0';   
        }   
        else   
        {   
            *pDst |= *pSrc - 'A' + 10;   
        }
        pSrc++; 
        pDst++;   
	  
    }
    return (nSrcLength / 2);   
}   
 
  
int gsmBytes2String(const unsigned char* pSrc, char* pDst, int nSrcLength)
{
	const char tab[]="0123456789ABCDEF";    
    int i=0;
	for( i=0; i < nSrcLength; i++)
    { 
		*pDst++ = tab[*pSrc >> 4];
		
		*pDst++ = tab[*pSrc & 0x0f]; 
	
		pSrc++; 
    }	   
    *pDst = '\0'; 
    return (nSrcLength * 2);
} 
 
int gsmEncode7bit(const char* pSrc, unsigned char* pDst, int nSrcLength)   
{   
    int nSrc;       // 源字符串的计数值    
    int nDst;       // 目标编码串的计数值    
    int nChar;      // 当前正在处理的组内字符字节的序号,范围是0-7    
    unsigned char nLeft;    // 上一字节残余的数据    
	
    // 计数值初始化    
    nSrc = 0; 
    nDst = 0; 
	
    // 将源串每8个字节分为一组,压缩成7个字节    
    // 循环该处理过程,直至源串被处理完    
    // 如果分组不到8字节,也能正确处理    
    while (nSrc < nSrcLength)         
    {   
        // 取源字符串的计数值的最低3位    
        nChar = nSrc & 7;   
		
        // 处理源串的每个字节    
        if(nChar == 0)   
        {   
            // 组内第一个字节,只是保存起来,待处理下一个字节时使用    
            nLeft = *pSrc;   
        }   
        else   
        {   
            // 组内其它字节,将其右边部分与残余数据相加,得到一个目标编码字节    
            *pDst = (*pSrc << (8-nChar)) | nLeft;   
			
            // 将该字节剩下的左边部分,作为残余数据保存起来    
            nLeft = *pSrc >> nChar;   
			
            // 修改目标串的指针和计数值    
            pDst++;
			//printf("pDst 1 == %02x\n",*(pDst-1)); 
            nDst++; 
			//printf("pDst 2 == %02x\n",*(pDst-1)); 
        }   
		
           
        pSrc++;   
        nSrc++;   
    }   
	
    // Return dst length  
    return nDst;   
}  


int gsmEncode8bit(const char* pSrc, unsigned char* pDst, int nSrcLength)   
{   
    // simple copy   
    memcpy(pDst, pSrc, nSrcLength);   
	
    return nSrcLength;   
}  
   
int gsmEncodeUcs2(const char* pSrc, unsigned char* pDst, int nSrcLength)   
{   
    int nDstLength;      
	wchar_t  wchar[128];      
	int i=0;
  	nDstLength = mbstowcs(wchar,pSrc,nSrcLength);   //For Linux
  
	for(i=0; i<nDstLength; i++)
	{
		*pDst++ = (unsigned char) wchar[i] >> 8 &0xFF;        
       	*pDst++ = (unsigned char) wchar[i] & 0xff;        
   	 }   
	*pDst='\0';      
    return nDstLength * 2;   
}   
 
int gsmInvertNumbers(const char* pSrc, char* pDst, int nSrcLength)   
{   
    int nDstLength;        
    char ch;               
	int i=0;
	char *p=pDst;
       
    nDstLength = nSrcLength; 
    for(i=0; i<nSrcLength;i+=2)
	{
		ch = *pSrc++;
		*pDst++ = *pSrc++;
		*pDst++ = ch;
	}	
		*pDst='\0';	
	if(nSrcLength%2 != 0)
	{
		*(pDst-2)='F';
		nDstLength++;
	}	
	return nDstLength;

}


int gsmEncodePdu(const SM_PARAM* pSrc, char* pDst)
{
	int nLength;			
	int nDstLength=0;			
	unsigned char buf[256];	
			
	nLength = strlen(pSrc->SCA);	
	buf[0] = (char)((nLength & 1) == 0 ? nLength : nLength + 1) / 2 + 1;        
    buf[1] = 0x91;          
    nDstLength = gsmBytes2String(buf, pDst, 2);         
    nDstLength += gsmInvertNumbers(pSrc->SCA, &pDst[nDstLength], nLength);      
	   
    nLength = strlen(pSrc->TPA);   
    buf[0] = 0x11;                      
    buf[1] = 0;                      
    buf[2] = (char)nLength;          
	if(buf[2] = 0x0B)
	buf[3] = 0x81;					  
    else buf[3] = 0x91;                  
    nDstLength += gsmBytes2String(buf, &pDst[nDstLength], 4);          
    nDstLength += gsmInvertNumbers(pSrc->TPA, &pDst[nDstLength], nLength);      
      
    nLength = strlen(pSrc->TP_UD);     
    buf[0] = pSrc->TP_PID;             
    buf[1] = pSrc->TP_DCS;  
	buf[2] = 0xAA;                       
    
	if(pSrc->TP_DCS == GSM_UCS2)   
    {   
         
        buf[3] = gsmEncodeUcs2(pSrc->TP_UD, &buf[4], nLength);             
        nLength = buf[3] + 4;           
    }else if(pSrc->TP_DCS == GSM_8BIT)
	{   
        // 8-bit    
        buf[3] = gsmEncode8bit(pSrc->TP_UD, &buf[4], nLength);       
        nLength = buf[3] + 4;    
    }else
	{
		// 7-bit编码方式    
        buf[3] = nLength;           // 编码前长度    
        nLength = gsmEncode7bit(pSrc->TP_UD, &buf[4], nLength+1) + 4;    
		// 转换TP-DA到目标PDU串 
	}
    nDstLength += gsmBytes2String(buf, &pDst[nDstLength], nLength);       
    return nDstLength;   
}

int smsinit(const char* smsc,const char* phone,const char* msg,const com_attr_t *ca)
{
	if (sp!= NULL)
	{
		free(sp);
		sp = NULL;
	}
	sp = (SM_PARAM *)malloc(sizeof(SM_PARAM));
	memset(sp->TP_SCTS,0,16);
	strcpy(sp->SCA,smsc);
	strcpy(sp->TP_UD,msg);
	strcpy(sp->TPA,phone); 
	sp->TP_PID = 0;
	//sp->TP_DCS = GSM_UCS2;
	sp->TP_DCS = GSM_7BIT;
	sp->index = (short)0;
		
	int fd;
	if((fd=openComm(ca)) < 0)
	{
		fprintf(stderr,"can Not Open device\n");
		return -1;
	}
	char ans[128]; 	 
	writeComm("AT\r", 3);
	readComm(ans, 128);
	if (strstr(ans, "OK") == NULL)
		return -1;   //return FALSE
	
	// ECHO OFF    
	writeComm("ATE0\r", 5);   
	readComm(ans, 128); 
	// PDU    
	writeComm("AT+CMGF=0\r", 10);   
	readComm(ans, 128); 
	
	return 0;  
	//return TRUE
	
}
int send_sms(const char* smsc, const char* phone, 
			 const char* msg,const com_attr_t * ca)
{
	if((smsinit(smsc,phone,msg,ca))<0 )
	{
		fprintf(stderr,"GSM init failed!\n");
		free(sp);
		return -1;
	}

	int nPduLength;         
    unsigned char nSmscLength;     
    int nLength;           
    char cmd[16];          
    char pdu[512];         
    char ans[128]; 
	printf("Ready to send SMS ,Please standby!\n");
	
    nPduLength = gsmEncodePdu(sp, pdu); 
	printf("PDU=[%s],"pdu);
	strcat(pdu, "\x01a");       
    gsmString2Bytes(pdu, &nSmscLength, 2);    
    nSmscLength++;
   	sprintf(cmd, "AT+CMGS=%d\r", nPduLength / 2 - nSmscLength); 
    writeComm(cmd, strlen(cmd));      
    nLength = readComm(ans, 128);     
	
    if(nLength == 4 && strncmp(ans, "\r\n> ", 4) == 0)   
    {   
        writeComm(pdu, strlen(pdu));
	}else
	{
		strcat(sp->TP_UD,"Send Failed!");
		fprintf(stderr,"SMS send failed!\n");
	}
	
	printf("SMS send Complete!\n");
	savelog("/var/local/sms_warning.log",sp->TPA,sp->TP_UD);
	free(sp);
	closeComm();
    return 0; 

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -