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

📄 sms.c

📁 在GPRS或者CDMA modem上实现发送/接收短信的C代码;支持Siemens
💻 C
📖 第 1 页 / 共 2 页
字号:
		}
	}
	*csq = atoi(szBuf);
	//bit error rate
	memset(szBuf, 0, sizeof(szBuf));
	p1 += 2;
	p2 = szBuf;
	while ( (*p1 != 0x0D) && ( *(p1 + 1) != 0x0A ) )
	{
        if((*p1 >= '0') && (*p1 <= '9'))
		{
			*p2++ = *p1++;	
		}
		else
		{
			ret = -SMS_ERR_AT_RESP_ERROR;
			goto testcsq_fail;
		}
	}
        *berr = atoi(szBuf);
	
	return 0;

testcsq_fail:
	return ret;
}

int setsmsconfig(SmsHandle hSmsHandle, const SMS_CONFIG* pSmsConfig)
{
	int ret, ttyfd;
	uchar ucSmsType, ucSmsCode;
	int baudrate, databits, stopbits, parity, crtscts;
	SMS_CONFIG* pCurSmsConfig = NULL;

	if (pSmsConfig == NULL)
	{
		WMMP_TRACE(debugType, "\r\nError: setsmsconfig invalid params");
		ret = -SMS_ERR_PARAMS;
		goto setsmsconfig_fail;
	}
	ret = searchsmshandle(&hSmsHandle);
	if (ret < 0)
	{
		WMMP_TRACE(debugType, "\r\nError: setsmsconfig invalid SmsHandle");
		ret = -SMS_ERR_HANDLE;
		goto setsmsconfig_fail;
	}
    //get the pointer to SMS_CONFIG of the current SmsHandle
    pCurSmsConfig = &(hSmsHandle->SmsConf);

    //Can is it copyed by bits???--YES!!!
	//set the current SMS_CONFIG according to pSmsConfig
	memset(pCurSmsConfig, 0, sizeof(SMS_CONFIG));
	memcpy(pCurSmsConfig, pSmsConfig, sizeof(SMS_CONFIG));

	//to be fixed....
	//executing commands according to SMS_CONFIG
	//tty configure
	ttyfd = hSmsHandle->ttyfd;
	baudrate = hSmsHandle->SmsConf.baudrate;
	databits = hSmsHandle->SmsConf.databits;
	stopbits = hSmsHandle->SmsConf.stopbits;
	parity   = hSmsHandle->SmsConf.parity;
	crtscts  = hSmsHandle->SmsConf.crtscts;
        ret = initialtty(ttyfd, baudrate, databits, stopbits, parity, crtscts);
	if (ret < 0)
	{
		ret = -SMS_ERR_INIT_TTY;
		goto setsmsconfig_fail;
	}
	//sms configure
	ucSmsType = hSmsHandle->SmsConf.smstype;
	ucSmsCode = hSmsHandle->SmsConf.smscode;
	ret = sms_init(ttyfd, ucSmsType, ucSmsCode);
	if (ret < 0)
	{
		goto setsmsconfig_fail;
	}
	//if pdu SMSC is not empty, then set it
	if (strlen(hSmsHandle->SmsConf.pdu.sc_addr) != 0) 
	{
        ret = setsmsc(ttyfd, hSmsHandle->SmsFunc.acModuleType, hSmsHandle->SmsConf.pdu.sc_addr);
		if (ret < 0)
		{
			ret = -SMS_ERR_SET_SMSC;
			goto setsmsconfig_fail;
		}
	}
    return 0;

setsmsconfig_fail:
	return ret;
}

int getsmsconfig(SmsHandle hSmsHandle, SMS_CONFIG* pSmsConfig)
{
	int ret;
	SMS_CONFIG* pOutSmsConfig;

	if (pSmsConfig == NULL)
	{
		WMMP_TRACE(debugType, "\r\nError: getsmsconfig invalid params");
		ret = -SMS_ERR_PARAMS;
		goto getsmsconfig_fail;
	}
    ret = searchsmshandle(&hSmsHandle);
	if (ret < 0)
	{
		WMMP_TRACE(debugType, "\r\nError: getsmsconfig invalid SmsHandle");
		goto getsmsconfig_fail;
	}

	//get current pointer to SMS_CONFIG
	pOutSmsConfig = &(hSmsHandle->SmsConf);

	//set values of pOutSmsConfig to pSmsConfig
    memset(pSmsConfig, 0, sizeof(SMS_CONFIG));
	memcpy(pSmsConfig, pOutSmsConfig, sizeof(SMS_CONFIG));

	return 0;

getsmsconfig_fail:
	return ret;
}


/***********************************
* debug definitions
************************************/
void WMMP_TRACE(char type, const char *format, ...)
{
	va_list argptr;

	va_start(argptr,format);
	vsprintf(buffer,format,argptr);
	va_end(argptr);

	switch (type)
	{
	case DEBUG_NONE:			
		break;
	case DEGUG_PRINT:
		printf("%s", buffer);	
		break;
	case DEGUG_SYSLOG:
#ifdef _LINUX_
		syslog(LOG_NOTICE, "%s", buffer);
#endif
		break;
	default:
		break;
	}

}


//get module type by send AT+GMI 
int getmoduletype(int fd, char* ptype)
{
    int ret, realen;
	char sendbuf[50];
	char recvbuf[200];

	//siemens SIEMENS OK
	//huawei  HUAWEI OK
	//simcom  SIMCOM_Ltd
	//bellwave BELLWAVE 
	//fidelix  FIDEKIX
	memset(sendbuf, 0, sizeof(sendbuf));
	sprintf(sendbuf, "AT+GMI\x0D");
	cleartty(fd);

	WMMP_TRACE(debugType, "\r\nWrite: %s", sendbuf);
	ret = writetty(fd, strlen(sendbuf), 1, sendbuf);
	if (ret < 0)
	{
		WMMP_TRACE(debugType, "\r\nError: write tty");
		goto getmoduletype_fail;
	}

	memset(recvbuf, 0, sizeof(recvbuf));
	ret = readtty(fd, sizeof(recvbuf), 2, recvbuf, &realen);
	if (ret < 0 || realen == 0)
	{
		WMMP_TRACE(debugType, "\r\nError: read tty");
		goto getmoduletype_fail;
	}

	if (strstr(recvbuf, "SIEMENS") != NULL)
	{
        *ptype = GPRS_SIEMENS;
	}
	else if (strstr(recvbuf, "SIMCOM") != NULL)
	{
		*ptype = GPRS_SIMCOM;
	}
	else if (strstr(recvbuf, "HUAWEI") != NULL)
	{
        *ptype = GPRS_HUAWEI;
	}
	else if (strstr(recvbuf, "BELLWAVE") != NULL)
	{
        *ptype = CDMA_BELLWAVE;
	}
	else if (strstr(recvbuf, "FIDELIX") != NULL)
	{
        *ptype = CDMA_FIDELIX;
	}
	else
	{//unknown module
        *ptype = UNKNOWN_MODULE;
	}
	
    return 0;

getmoduletype_fail:
	*ptype = UNKNOWN_MODULE;
	return ret;
}


int setwmmpdebugtype(char debType)
{
	debugType = debType;
	return 0;
}

//initialize sms configure structure
int initsmsfunc(uchar acModType, SMS_FUNC* pSmsFunc)
{
	int ret = 0;
	
	pSmsFunc->acModuleType = acModType;

	switch(acModType)
	{
		case GPRS_SIMCOM:
		{
			memset(pSmsFunc->desc, 0, sizeof(pSmsFunc->desc));
			memcpy(pSmsFunc->desc, "SIMCOM", strlen("SIMCOM"));
			pSmsFunc->psms_init = simcom_init;
			pSmsFunc->psms_send = simcom_send;
			pSmsFunc->psms_recv = simcom_recv;
			pSmsFunc->psms_ifnew = simcom_ifnew;
			pSmsFunc->psms_parse = simcom_parse;
			pSmsFunc->psms_delall = simcom_delall;
 	        break;
		}
		case GPRS_SIEMENS:
		{
			memset(pSmsFunc->desc, 0, sizeof(pSmsFunc->desc));
			memcpy(pSmsFunc->desc, "SIEMENS", strlen("SIEMENS"));
			pSmsFunc->psms_init = siemens_init;
			pSmsFunc->psms_send = siemens_send;
			pSmsFunc->psms_recv = siemens_recv;
			pSmsFunc->psms_ifnew = siemens_ifnew;
			pSmsFunc->psms_parse = siemens_parse;
			pSmsFunc->psms_delall = siemens_delall;
		    break;
		}
		case GPRS_HUAWEI:
		{
			memset(pSmsFunc->desc, 0, sizeof(pSmsFunc->desc));
			memcpy(pSmsFunc->desc, "HUAWEI", strlen("HUAWEI"));
			pSmsFunc->psms_init = huawei_init;
			pSmsFunc->psms_send = huawei_send;
			pSmsFunc->psms_recv = huawei_recv;
			pSmsFunc->psms_ifnew = huawei_ifnew;
			pSmsFunc->psms_parse = huawei_parse;
			pSmsFunc->psms_delall = huawei_delall;
		    break;
		}
		case CDMA_BELLWAVE:
		{
			memset(pSmsFunc->desc, 0, sizeof(pSmsFunc->desc));
			memcpy(pSmsFunc->desc, "BELLWAVE", strlen("BELLWAVE"));
			pSmsFunc->psms_init = bellwave_init;
			pSmsFunc->psms_send = bellwave_send;
			pSmsFunc->psms_recv = bellwave_recv;
			pSmsFunc->psms_ifnew = bellwave_ifnew;
			pSmsFunc->psms_parse = bellwave_parse;
			pSmsFunc->psms_delall = bellwave_delall;
		    break;
		}
		case CDMA_FIDELIX:
		{
			memset(pSmsFunc->desc, 0, sizeof(pSmsFunc->desc));
			memcpy(pSmsFunc->desc, "FIDELIX", strlen("FIDELIX"));
			pSmsFunc->psms_init = fidelix_init;
			pSmsFunc->psms_send = fidelix_send;
			pSmsFunc->psms_recv = fidelix_recv;
			pSmsFunc->psms_ifnew = fidelix_ifnew;
			pSmsFunc->psms_parse = fidelix_parse;
			pSmsFunc->psms_delall = fidelix_delall;
		    break;
		}
		default:
		{
		
			WMMP_TRACE(debugType, "\r\nError: unknown wireless module");
			break;
		}		
	}

	//get export functions
	sms_init = pSmsFunc->psms_init;
	sms_send = pSmsFunc->psms_send;
	sms_recv = pSmsFunc->psms_recv;
	sms_ifnew = pSmsFunc->psms_ifnew;
	sms_parse = pSmsFunc->psms_parse;
	sms_delall = pSmsFunc->psms_delall;
	
	return ret;
}

int confirmttyspeed(int fd, int* speed)
{
	int i, ret, trytime, inspeed;
	char sendBuf[10];

	static int autospeed[7] = {115200, 230400, 9600, 19200, 38400, 57600, 0};
	
	memset(sendBuf, 0, sizeof(sendBuf));
	sprintf(sendBuf, "AT\x0D");
	inspeed = 0;

        trytime = sizeof(autospeed)/sizeof(int) - 1; //modified by ljs 2007-01-25
	for (i = 0; i < trytime; i++)
	{
		//firstly, try to test 115200BPS
		//initialize tty with default values
		inspeed = autospeed[i];
		initialtty(fd, inspeed, 8, 1, NO_PARITY, FLOWCTR_NONE);
		cleartty(fd); //for clear tty recv buffer
		
		for(i = 0; i < 5; i++)
		{
			WMMP_TRACE(debugType, "\r\nWrite: %s, Length: %d\r\n", sendBuf, strlen(sendBuf));
			ret = writetty(fd, strlen(sendBuf), 2, sendBuf);
			if(ret == 0)
			{
				ret = wait_for_OK(fd, "OK", 2);
				if(ret == 0)
				{
					break;
				}
			}
		}
		if(i == 5 && ret != 0)
		{
			WMMP_TRACE(debugType, "\n\rERROR: AT does not return OK");
			//	goto confirmttyspeed_fail;
		}
		else
		{
			*speed = inspeed;
			goto confirmttyspeed_succ;
		}
	}

	if (i == trytime && ret != 0)
	{
        WMMP_TRACE(debugType, "\r\nError: conform module baudrate");
		goto confirmttyspeed_fail;
	}

confirmttyspeed_succ:
	return 0;

confirmttyspeed_fail:
	*speed = 0;
	return -1;
}

int defaultsmsconfig(SMS_CONFIG* pSmsConfig)
{
	//tty configure
	pSmsConfig->baudrate = 115200;
	pSmsConfig->databits = 8;
	pSmsConfig->stopbits = 1;
	pSmsConfig->parity   = NO_PARITY;
	pSmsConfig->crtscts  = FLOWCTR_NONE;
	//sms type and code configure
	pSmsConfig->smstype  = SMS_TEXT;
	pSmsConfig->smscode  = SMS_ENCODE_7BIT;
	//pdu configure
	//default SMSC is nothing.
	pSmsConfig->pdu.sc_addr_len  = 0;
	pSmsConfig->pdu.sc_addr_type = 0;
	memset(pSmsConfig->pdu.sc_addr, 0, sizeof(pSmsConfig->pdu.sc_addr));
	//message info.
	pSmsConfig->pdu.tp_head      = 0x11;
	pSmsConfig->pdu.tp_msg_refer = 0x00;
	pSmsConfig->pdu.tp_da_len    = 0x0D;
	pSmsConfig->pdu.tp_da_type   = 0x91;
	memset(pSmsConfig->pdu.tp_daddr, 0, sizeof(pSmsConfig->pdu.tp_daddr));
	//protocol ID
	pSmsConfig->pdu.tp_pid       = 0x00;
	//data coding scheme
	pSmsConfig->pdu.tp_dcs       = 0x00; //7-bit GSM
	//valid period
	pSmsConfig->pdu.tp_vp        = 0xA7; //12h + (VP-143)*30min

	return 0;
}

int setsmsc(int fd, uchar ucModType, const char* smsc)
{
	int ret, realen;
	char ucSetCmd[256];
	char ucRecv[100];
	char ucTempNum[100];
	int  iTempNumLen;

	if (smsc == NULL)
	{
		ret = -SMS_ERR_PARAMS;
		goto setsmsc_fail;
	}

	if ( (ucModType & 0xF0) != 0x00 )
	{
		ret = -SMS_ERR_MOD_TYPE;
		goto setsmsc_fail;
	}

	memset(ucSetCmd, 0, sizeof(ucSetCmd));
	//huawei
	//GSM or IRA or UCS2
	//AT+CSCS="+8613800591500"\r
	if (ucModType == GPRS_HUAWEI)
	{
         sprintf(ucSetCmd, "AT+CSCA=\"+86%s\"\x0D", smsc);
	}
	//simcom must do like the following examples
	//GSM: AT+CSCA="+8613800591500"\r  
	//UCS2: AT+CSCA="002b0038003600310033003800300030003500390031003500300030"\r
	//siemens is the same as simcom
	//GSM: AT+CSCA="+8613800591500"\r or +8613800591500
	//UCS2: AT+CSCA="002b0038003600310033003800300030003500390031003500300030"\r
	//   or AT+CSCA=002b0038003600310033003800300030003500390031003500300030\r
	if (ucModType == GPRS_SIEMENS || ucModType == GPRS_SIMCOM)
	{
         ret = writetty(fd, strlen("AT+CSCS?\x0D"), 2, "AT+CSCS?\x0D");
		 if (ret < 0)
		 {
			 ret = -SMS_ERR_SEND_ATCMD;
			 WMMP_TRACE(debugType, "\r\nError: send AT+CSCS?");
			 goto setsmsc_fail;
		 }
		 memset(ucRecv, 0, sizeof(ucRecv));
		 realen = 0;
		 ret = readtty(fd, sizeof(ucRecv), 1, ucRecv, &realen);
		 if (ret < 0)
		 {
			 ret = -SMS_ERR_READ_TTY;
			 WMMP_TRACE(debugType, "\r\nError: read the response of AT+CSCS");
			 goto setsmsc_fail;
		 }
		 memset(ucTempNum, 0, sizeof(ucTempNum));
		 iTempNumLen = 0;
		 if (strstr(ucRecv, "UCS2") != NULL)
		 {
             convsmsdata(smsc, strlen(smsc), ucTempNum, &iTempNumLen, TO_UNI_NUM);
			 sprintf(ucSetCmd, "AT+CSCA=\"002B00380036%s\"\x0D", ucTempNum);
		 }
		 else if (strstr(ucRecv, "GSM") != NULL)
		 {
             sprintf(ucSetCmd, "AT+CSCA=\"+86%s\"\x0D", smsc);
		 }
		 else
		 {
			 ret = -SMS_ERR_CSCS_TYPE;
			 WMMP_TRACE(debugType, "\r\nError: error CSCS");
			 goto setsmsc_fail;
		 }
	}
	//sent AT+CSCA command
	ret = writetty(fd, strlen(ucSetCmd), 2, ucSetCmd);
	if (ret < 0)
	{
		ret = -SMS_ERR_SEND_ATCMD;
		WMMP_TRACE(debugType, "\r\nError: send AT+CSCA command");
		goto setsmsc_fail;
	}
	ret = wait_for_OK(fd, "OK", 1);
	if (ret < 0)
	{
		ret = -SMS_ERR_AT_NOTOK;
		WMMP_TRACE(debugType, "\r\nError: set sms center");
		goto setsmsc_fail;
	}

	return 0;

setsmsc_fail:
	return ret;
}

⌨️ 快捷键说明

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