blob: 1e6ad27166e03071b9486c85b71dabee1c1ccfaa [file] [log] [blame]
//#include "at_com.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/msg.h>
#include "at_api.h"
#define AT_PARAM_MAX_NUM 50
#define RESEND_TIMES_MAX 3
//µ÷Óøýӿڷ¢ËÍÖ÷¶¯Éϱ¨×Ö·û´®¸øÔ¶¶ËоƬ
int send_rsp_str_to_modem(int module_id, char *rsp_cmd,int rsp_len)
{
return at_send_msg(module_id, MODULE_ID_AT_CLIENT, MSG_CMD_SEND_RSP_TO_ZTE, rsp_len, rsp_cmd, 0);
}
//»ñÈ¡ATÃüÁîǰ׺
static void get_at_req_prefix(char *at_str, char** at_prefix)
{
char *prefix = NULL;
char *pszAtHead = NULL;
char *ptemstr = NULL;
if((*at_str == 'A'||*at_str == 'a')&&(*(at_str+1) == 'T'||*(at_str+1) == 't'))
{
if(isalpha(*(at_str+2)))
{
*at_prefix = NULL;
}
else
{
prefix = (char*)malloc(50);
if(prefix == NULL) at_assert(0);
memset(prefix, 0x00, 50);
pszAtHead = at_str;
//ÕÒµ½Ê׸ö·Ç×Öĸ»òÕßÊý×ֵIJÎÊý£¬ÀýÈç+
for(; *at_str != '\0'; at_str++)
{
if(!(isalnum(*at_str) || *at_str == '\r' || *at_str == '\n'))
{
pszAtHead = at_str;
pszAtHead++;
break;
}
}
for(ptemstr = pszAtHead; *ptemstr != '\0'; ptemstr++)
{
if(*ptemstr == '=' || *ptemstr == '?' || *ptemstr == '\r')
{
memcpy(prefix, pszAtHead, ptemstr-pszAtHead);
*at_prefix = prefix;
return;
}
}
}
}
else
{
at_assert(0);
}
}
static int is_print_str(const char *str){
while(isprint(*str)){
str++;
}
return *str == '\0';
}
static int get_fmt_param_count(const char *str){
int fmt_param_count = 0;
for(; *str != '\0'; str++){
if(*str == ','){
fmt_param_count++;
}
}
return fmt_param_count + 1;
}
/*return 0±íʾ½âÎö²ÎÊýÕý³££¬ÄÚ²¿½øÐÐÑϸñµÄ²ÎÊý¼ì²é£¬°üÀ¨²ÎÊý¸öÊýÒ»ÖÂÐÔ¼ì²é£¬¶ÔÓÚ¿ÉÑ¡²ÎÊý£¬ÐèÒªµ÷ÓÃÕß×ÔÐп¼ÂÇ*/
static int parse_param_safe(char *fmt, char *buf, void **pval)
{
char *p;
char *sepp;
char *tmp;
char sepc;
char *pstr[AT_PARAM_MAX_NUM] = {0};
int len, flag;
int n = 0;
int nparam = 0;//buf×Ö·û´®Êµ¼ÊµÄ²ÎÊý¸öÊý
int fmt_param_num = 0;//fmt¸ñʽ»¯ÖÐÒªÇóµÄ²ÎÊý×ܸöÊý£¬Óë%¸öÊýÒ»ÖÂ
int ret = AT_PARSE_OK;
int mark_flag = 0;//ÓÃÓÚ±êÊ¶ÌØÊâ·ûºÅµÄÅä¶ÔÇé¿ö£¬Ä¿Ç°ÓÃÓÚË«ÒýºÅ
while(*buf == ' ') buf++;
len = strlen(buf) + 1;
tmp = p = (char*)malloc(len);
if (p == NULL) {
return ATERR_NO_PRINT;
}
//fmt_param_num = find_char_num(fmt,"%");
memset(p, 0, len);
//ÉêÇëÐÂÄÚ´æ´æ·ÅÈë²ÎµÄATÃüÁÒÔ±ã½øÐÐÕûÐζ¨ÖÆ
memcpy(p, buf, strlen(buf));
/*È¥³ýβ²¿µÄ\r\n£¬Ä¿Ç°½öÔÚ7100ÉÏÓдËÎÊÌâ */
for(len--; len > 0; len--){
if(p[len - 1] == '\r' || p[len - 1] == '\n'){
p[len - 1] = '\0';
} else {
break;
}
}
if(!is_print_str(p)){/* ÈçATÃüÁîÖаüº¬²»¿É¼û×Ö·ûÖ±½Ó·µ»Ø´íÎó */
free(tmp);
return ATERR_NO_PRINT;
}
/*°´¶ººÅ½øÐÐÿ¸ö²ÎÊýµÄ·Ö¸îÕûÐÎ,Ë«ÒýºÅÀïµÄ¶ººÅÌø¹ý*/
do {
flag = 0;
mark_flag = 0;
if (*p == '"') {
int i, j;
for (i = j = 0, ++p; p[j] != '\0'; i++, j++) {
if (p[j] == '"') {
++j;
mark_flag = 1;
break;
}
}
if(mark_flag == 0){/* δƥÅ䵽˫ÒýºÅÖ±½Ó·µ»Ø´íÎó */
free(tmp);
return ATERR_DROP_MARK;
}
if (j == 1) flag = 1;
p[j - 1] = '\0';
sepp = p + j;
} else {
sepp = p + strcspn(p, ",");
if (p == sepp) flag = 1;
}
nparam++;
sepc = sepp[0];
sepp[0] = '\0';
if (flag == 1)
pstr[n++] = NULL;
else
pstr[n++] = p;
p = sepp + 1;
} while ((n < AT_PARAM_MAX_NUM) && (sepc == ','));
if(nparam < get_fmt_param_count(fmt)){
ret = ATWARN_DROP_RN;
}else if(nparam > get_fmt_param_count(fmt)){
ret = ATWARN_LACK_PARAM;
}
/* convert the string params to the types that fmt Appointed */
int param_count = 0; /*ÊäÈëµÄ×Ö·û´®ÖÐÓÐЧ²ÎÊý¸öÊý*/
int param_str_index = 0; /* the index of string params */
int param_val_index = 0; /* the index of param values */
//°´ÕÕÈë²Î¸ñʽ½øÐÐÿ¸ö²ÎÊýÖµµÄ·µ»Ø¸³Öµ
for (; fmt != NULL && param_str_index < nparam; param_str_index++) {
char type;
unsigned int size;
char str_param_size[10] = { 0 };
int param_size_index = 0;
while(*fmt == ' ') fmt++;
if(*fmt == ','){
fmt++;
continue; /* igmore the param that is without type */
}
if(*fmt++ != '%'){
ret = ATERR_NO_PRINT;
break;
}
for (; param_size_index < sizeof(str_param_size) - 1; fmt++, param_size_index++){
if(!isdigit(*fmt)){
break;
}
str_param_size[param_size_index] = *fmt;
}
type = *(fmt++);
size = atoi(str_param_size);
while(*fmt == ' ') fmt++; /* igmore the blanks */
fmt = (*fmt == ',' ? fmt + 1 : NULL);
if (type == 'd') {
if(size == 0 || size == 4) {
if(pstr[param_str_index] != NULL) {
*((int *)pval[param_val_index]) = (int)atoi(pstr[param_str_index]);
param_count++;
}
} else if(size == 1) {
if(pstr[param_str_index] != NULL) {
*((char *)pval[param_val_index]) = (char)atoi(pstr[param_str_index]);
param_count++;
}
} else if(size == 2) {
if(pstr[param_str_index] != NULL) {
*((short *)pval[param_val_index]) = (short)atoi(pstr[param_str_index]);
param_count++;
}
}else {
at_assert(0);
break;
}
} else if (type == 's') {
if (size == 0) {
if(pstr[param_str_index] != NULL) {
strcpy((char *)pval[param_val_index], pstr[param_str_index]);
param_count++;
}
} else {
if(size < strlen(pstr[param_str_index])){
ret = ATERR_STR_TOO_LONG;
break;
}
if(pstr[param_str_index] != NULL) {
strncpy((char *)pval[param_val_index], pstr[param_str_index], size - 1);
param_count++;
}
}
} else {
at_assert(0);
break;
}
param_val_index++;
}
free(tmp);
return ret;
}
/*%d¸ñʽʱ£¬Èë²Î±ØÐëΪintÀàÐÍ£¬·ñÔòÄÚ²¿ÓÐÔ½½çµÄ·çÏÕ*/
/*Óû§Ê¹ÓÃʱ£¬Èô²»ÏëÓɸýӿÚÄÚ²¿½øÐвÎÊýµÄ¸ñʽ»¯½âÎö£¬¿ÉÒÔ½«fmt¸³ÖµÎª"%s"£¬pval¸³ÖµÎªchar **p˫ָÕ룬ÕâÑù²ÎÊý½«×÷ΪÕûÌå×Ö·û´®·µ»Ø¸øµ÷ÓÃÕß*/
int parse_param(char *fmt, char *buf, void **pval)
{
int ret = parse_param_safe(fmt,buf,pval);
if(ret==ATWARN_LACK_PARAM||ret==ATWARN_DROP_RN)
return AT_PARSE_OK;
return ret;
}
//¸øat_client·¢ËÍÇëÇóÏûÏ¢£¬Ð¯´øatÃüÁî×Ö·û´®ºÍ³¬Ê±Ê±³¤
static void send_app_req(int module_id, char *req_at, int len, int timeout,int direct)
{
struct app_req appreq;
memset(&appreq, 0x00, sizeof(struct app_req));
memcpy(appreq.atstr, req_at, len);
appreq.str_len = len;
appreq.timeout = timeout;
if(direct == NEAR_PS)
at_send_msg(module_id, MODULE_ID_AT_CLIENT, MSG_CMD_SEND_AT_TO_ZTE,sizeof(struct app_req), &appreq, 0);
printf("send_app_req\n");
}
//·µ»Ø1 ¿É³¢ÊÔÖØ·¢ ·µ»Ø0 ²»ÔÙÖØ·¢
int at_client_err_proc(int err, int sent_cnt)
{
if(9001 == err)//ÖÐÐË΢·µ»Ø×´Ì¬»úÒì³££¬ÐèÒªÕû»úÖØÆô
{
at_assert(0);
}
if(6003 == err || 6004 == err || 3 == err || 4 == err)
{
if(sent_cnt == 1)
{
return 1;
}
else
{
printf("please check req\n");
}
}
else if(8008 == err || 6000 == err)
{
if(sent_cnt == 1)
{
return 1;
}
else
{//Á¬ÐøÁ½´Î³¬Ê±»òͨµÀ×èÈû£¬ÖÐÐË΢оƬÒì³££¬ÐèÒªÕû»úÖØÆô
at_assert(0);
}
}
return 0;
}
/**
* @brief Ó¦Ó÷¢ËͶ¯×÷ÀàATÇëÇ󣬲¢×èÈûµÈ´ý¶¯×÷½á¹û£»×¢Ò⣺¸Ã½Ó¿ÚÖ»ÄÜÓÃÓÚ²éѯºÍÉèÖúÍÖ´ÐÐÇëÇó£¬Í¨¹ýÈë²Î½øÐÐÖмä½á¹ûµÄÄÚ²¿½âÎö´¦Àí£¬²¢ÇÒ
ÓÐÖмä½á¹ûÉϱ¨Ê±£¬Ö»Ö§³ÖÒ»ÌõÖмä½á¹ûµÄÉϱ¨½âÎö£»
* @param req_at ÇëÇóATÃüÁî
* @param info_fmt ATÏìÓ¦µÄ¸ñʽ»¯·½Ê½
* @param pval ¸ñʽ»¯ºóµÄATÏìÓ¦
* @param safe_parm_check ָʾÊÇ·ñ¶Ô²ÎÊý¸öÊý½øÐÐÒ»ÖÂÐÔ¼ì²é£¬0±íʾº¬¿ÉÑ¡²ÎÊý£¬²»¶Ô²ÎÊý¸öÊý½øÐмì²é;1±íʾ¶Ô²ÎÊý¸öÊý½øÐÐÑϸñ¼ì²é
* @return 0±íʾ·µ»Ø³É¹¦£¬ÆäËûÖµ±íʾʧ°Ü´íÎóÂë
* @note 1. Óû§Ê¹ÓÃʱ£¬Èô²»ÏëÓɸýӿÚÄÚ²¿½øÐвÎÊýµÄ¸ñʽ»¯½âÎö£¬¿ÉÒÔ½«info_fmt¸³ÖµÎª"%s"£¬pval¸³ÖµÎªchar **p˫ָÕ룬ÕâÑù²ÎÊý½«×÷ΪÕûÌå×Ö·û´®·µ»Ø¸øµ÷ÓÃÕß
2. ¶ÔÓÚZMGL,CPBR, COPS=?µÈÒ»ÌõÇëÇó»áÓжàÌõÖмä½á¹ûÇé¿ö£¬»¹ÐèʹÓÃregister_inform_func×¢²áÖмä½á¹ûµÄ´¦Àíº¯Êý
* @warning
*/
static int send_req_and_wait(char *req_at,int len,char *info_fmt,void **pval, int safe_parm_check,int timeout,int direct)
{
int my_handle = 0;
char *prefix = NULL;
MSG_BUF rsp_msg = {0};
long msgSize = sizeof(MSG_BUF)-sizeof(long);
int iRet = 0;
int module_id = MODULE_ID_APP_DYNAMIC_BASE;//ÄÚ²¿¶¯Ì¬»ñȡԴģ¿éID£¬³õʼֵΪMODULE_ID_ATDYNAMIC_BASE
int err = 0;
int at_send_cnt = 0;
if(len > DATA_BUFFER_MAX-12)
at_assert(0);
at_assert(((info_fmt==NULL&&pval==NULL)||(info_fmt!=NULL&&pval!=NULL)));
//¶¯Ì¬´´½¨ÁÙʱµÄÏûÏ¢¶ÓÁйܵÀ
//msggetʹÓòÎÊýIPC_CREAT | IPC_EXCL| 0600£¬Åжϵ±Ç°module_idµÄÏûÏ¢¶ÓÁÐÊÇ·ñ
//´æÔÚ£¬Èç¹û²»´æÔÚ£¬Ö±½Ó´´½¨ÐµÄÏûÏ¢¶ÓÁУ»Èç¹û´æÔÚ£¬·µ»ØÖµÎ´-1£¬È»ºómodule_id
//Öµ¼Ó1£¬²»¶ÏÑ­»·Ö±µ½ÕÒµ½Ã»ÓÐʹÓõÄmodule_idÖµ
while((my_handle = msgget(module_id,IPC_CREAT | IPC_EXCL| 0600)) == -1)
{
module_id++;
if (module_id > MODULE_ID_APP_DYNAMIC_END)
//µ±module_id´óÓÚMODULE_ID_ATDYNAMIC_ENDֵʱ£¬Ö÷¶¯¶ÏÑÔ
at_assert(0);
}
//¸ù¾ÝÇëÇóATÃüÁîºÍ²éѯÓï·¨£¬ÕÒµ½¶ÔÓ¦µÄ²éѯǰ׺£¬¶¯Ì¬¸³Öµ¸ø³ö²Î£¬Íⲿ½øÐÐÄÚ´æµÄÊÍ·Å
get_at_req_prefix(req_at,&prefix);
RESEND:
//¸øat_ctl·¢ËÍÏûÏ¢£¬·¢Ë͵ÄÏûÏ¢ÄÚÈÝÊÇatÃüÁî×Ö·û´®
at_send_cnt++;
at_assert(at_send_cnt<=RESEND_TIMES_MAX);
//ÓÉatctlÖ÷¿ØÄÚ²¿ÉèÖó¬Ê±µÈ´ý£¬ÖØ·¢ºó»áÓÉÖØ¹¹ÖØÉè¸Ã¶¨Ê±Æ÷
send_app_req(module_id,req_at,len,timeout,direct);
printf("prefix:%s\n",prefix);
while(1)
{
iRet = 0;
memset(&rsp_msg, 0x00, sizeof(MSG_BUF));
iRet = msgrcv(my_handle, &rsp_msg, msgSize, 0, 0);
if (iRet < 0)
{
continue;
}
//ËÀµÈÓ¦´ðÏûÏ¢
if(rsp_msg.usMsgCmd == MSG_CMD_SEND_AT_MSG_RSP)//Æ¥Åäµ½ÊÇMSG_CMD_SEND_AT_MSG_RSPʱ²Å´¦Àí
{
char *buf = rsp_msg.aucDataBuf;
char *at_paras = NULL;
char *rsp_head = NULL;
char *rsp_tail = NULL;
rsp_head = buf;
printf("recv MSG_CMD_SEND_AT_MSG_RSP, buf:%s\n",buf);
//¶ÔÓÚ²éѯÇëÇ󣬲¢ÌṩÁ˲ÎÊýÄÚ´æ¿Õ¼ä£¬Ôò½øÐвÎÊýµÄ½âÎö
if(prefix!=NULL && info_fmt!=NULL && pval!=NULL && ((rsp_head=strcasestr(buf,prefix))!=NULL))
{
//Ìø¹ýÖмä½á¹ûµÄÍ·²¿£¬ÕÒµ½²ÎÊýµÄÊ×µØÖ·
rsp_head = rsp_head+strlen(prefix)+2;
if((rsp_tail = strchr(rsp_head,'\0')) == NULL)//µ±Éϱ¨µÄÖмä½á¹ûÖÐûÓÐ\r\nʱ£¬ËµÃ÷½á¹ûÓÐÎÊÌ⣬ֱ½Ó¶ÏÑÔ
at_assert(0);
if(strcmp(info_fmt,"%s") == 0)
//Èç¹û²»ÏëÓÉÄÚ²¿½øÐвÎÊýµÄ¸ñʽ»¯½âÎöʱ£¬Ö±½Ó½«È«²¿²ÎÊýÄÚÈÝÈ¡³öÀ´¸³Öµµ½pval[0]ÖУ¬²»×öÈκνâÎö
strncpy((char *)*pval, rsp_head, (int)(rsp_tail-rsp_head));
else//ÐèÒª²ÎÊý½âÎöʱ£¬½«Öмä½á¹û´ÓÉϱ¨½á¹ûÖгé³öÀ´£¨Éϱ¨µÄ½á¹ûÖпÉÄÜÓÐOK´æÔÚ£©
{
char *at_paras = malloc(strlen(rsp_head)+1);
if(at_paras == NULL) at_assert(0);
memset(at_paras, 0, strlen(rsp_head)+1);
memcpy(at_paras, rsp_head, (int)(rsp_tail-rsp_head));
if(safe_parm_check)
err = parse_param_safe(info_fmt, at_paras, pval);//°´ÕÕ¸ñʽ½âÎö²ÎÊý
else
err = parse_param(info_fmt, at_paras, pval);//°´ÕÕ¸ñʽ½âÎö²ÎÊý
printf("rsp parse_param_safe return ERR=%d!!!!!!\n",err);
printf("at_paras=%s!!!!!!\n",at_paras);
if(at_paras != NULL)
free(at_paras);
//²ÎÊý²»ºÏ·¨£¬¿ÉÄÜÊÇ´®¿ÚͨÐÅÒì³£Ôì³ÉµÄ
else if(err != AT_PARSE_OK)
goto RESEND;
}
}
//±ê×¼3GPPµÄ´íÎ󣬲»½øÐÐÖØ´«³¢ÊÔ
if((rsp_head = strstr(buf,"ERROR")) != NULL)
{
//Èç¹ûÕÒ²»µ½Ã°ºÅ£¬ËµÃ÷ERRORûÓдíÎóÂë
if(strchr(rsp_head,':') != NULL)
sscanf(rsp_head+strlen("ERROR")+2, "%d", &err);
printf("server return ERR=%d, at_send_cnt=%d!!!!!!!\n",err,at_send_cnt);
//ÖÐÐË΢оƬ״̬»úÒì³££¬Ò»°ãΪ˽×ÔÖØÆô£¬ÐèÒª½øÐÐÕû»úµÄÖØÆô²Ù×÷
if(err == ATERR_ZXIC_ERR)
at_assert(!"ZXIC err!");
//¶ÔÓÚÏòÖÐÐË΢оƬ·¢ËÍÇëÇóʱ£¬»Ø¸´´íÎóÂëΪATERR_DROP_MARK¡¢ATERR_NO_PRINT¡¢ATERR_STR_TOO_LONG¡¢ATWARN_DROP_RN¡¢
//ATWARN_LACK_PARAM¡¢ATERR_WAIT_REQ_END_TIMEOUT¡¢ATERR_WAIT_RSP_TIMEOUTºÍATERR_UNKNOWNING_CMDʱ¶¼±ØÐëÖØ·¢
else if((err>=ATERR_DROP_MARK && err<=ATWARN_LACK_PARAM) || err == ATERR_WAIT_REQ_END_TIMEOUT || err == ATERR_WAIT_RSP_TIMEOUT
|| err == ATERR_UNKNOWN_CMD)
goto RESEND;
break;
}
//¶ÔÓÚÉèÖÃÀàÃüÁîºÍһЩִÐÐÀàÃüÁֻÓÐOKÉϱ¨Ã»ÓÐÖмä½á¹ûÉϱ¨
else if((rsp_head = strstr(buf,"OK")) != NULL)
{
//ÖÐÐË΢»Ø¸´µÄ½á¹ûÈç¹û½âÎö³öµÄ²ÎÊý²»ºÏ·¨£¬Á¢¼´ÖØÐ·¢ËÍÇëÇó
if(err>=ATERR_DROP_MARK && err<=ATWARN_LACK_PARAM)
goto RESEND;
break;
}
}
}
if(prefix!=NULL)
free(prefix);
//²éѯÃüÁî½á¹û·µ»Øºó£¬Ïú»Ù֮ǰ´´½¨µÄÏûÏ¢¶ÓÁÐ
if(msgctl(my_handle,IPC_RMID,0) < 0)
printf("%d:%s",errno,strerror(errno));
return err;
}
//ÄÚ²¿¶ÔÓÚÎïÀíͨµÀÒì³£Ôì³ÉµÄÊý¾Ý´íÎó½øÐÐÖØ·¢³¢ÊÔµÄÈÝ´í£¬timeoutΪºÁÃëΪµ¥Î»µÄµÈ´ý½á¹ûʱ³¤
int get_modem_info(char *req_at,char *info_fmt,void **pval, int timeout)
{
return send_req_and_wait(req_at,strlen(req_at),info_fmt,pval,0,timeout,NEAR_PS);
}
//·¢ËÍÏûÏ¢½Ó¿Ú£¬³É¹¦·µ»Ø0£¬Ê§°Ü·µ»Ø·Ç0
int at_send_msg(int source_id, int target_id, unsigned short Msg_cmd, unsigned short us_DataLen, unsigned char *pData, int msgflag)
{
MSG_BUF stMsg;
int lRet = 0;
int lTgtMsgID = 0;
long msgSize = sizeof(MSG_BUF) - sizeof(long);
long errNo = 0;
memset(&stMsg, 0, sizeof(MSG_BUF));
lTgtMsgID = msgget(target_id, 0);
if (-1 == lTgtMsgID) {
return -1;
}
if ((us_DataLen > 0) && (NULL == pData)) {
return -1;
}
stMsg.usSourceModuleID = source_id;
stMsg.usTargetModuleID = target_id;
stMsg.usMsgCmd = Msg_cmd;
stMsg.usDataLen = us_DataLen;
if (us_DataLen > 0) {
memcpy(stMsg.aucDataBuf, pData, us_DataLen);
}
AGAIN:
lRet = msgsnd(lTgtMsgID, &stMsg, msgSize, msgflag!=0?04000:0);//IPC_NOWAIT 04000
if (lRet < 0) {
if (errno == EINTR) {
goto AGAIN;
}
if (msgflag == 0) {
assert(0);
}
return -1;
}
return 0;
}