ril-at_tok.c分析

本文详细解析了处理AT命令响应的关键步骤,包括初始化、跳过空格、跳过符号等,以及如何解析整数、布尔值和字符串。通过这些步骤,能够有效解析AT命令的响应参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

局部变量初始化为任意值。
==============================================
1.作用:找到第一个:的位置
int at_tok_start(char **p_cur)
{
*p_cur = strchr(*p_cur, ':');
}
==============================================
2.作用:跳过空格
static void skipWhiteSpace(char **p_cur)
==============================================
3.作用:跳过冒号
static void skipNextComma(char **p_cur)
==============================================
4.作用:跳到下一个参数,返回当前参数。
static char * nextTok(char **p_cur)
{
 ret = strsep(p_cur, "\"");//分割字符
 strsep返回值为分割后的开始字符串,并将函数的第一个参数指针指向分割后的
 剩余字符串。它适用于分割关键字在两个字符串之间只严格出现一次的情况
}
==============================================
5.作用:解析下一个整数在AT响应命令,输出在P_OUT。
static int at_tok_nextint_base(char **p_cur, int *p_out, int base, int  uns)
参数:p_cur输入字符串    base多少进制   uns无符号。p_out为输出整形。

==================================================
6.
int at_tok_nextbool(char **p_cur, char *p_out)和at_tok_nextint差不多,只是结果只有0或者1.更安全。
int at_tok_hasmore(char **p_cur)//判断响应参数结束没有
int at_tok_nextstr(char **p_cur, char **p_out)//将分割的字符串给p_out 此函数和 at_tok_nextint相似
======================================================================
unsigned long strtoul(const char *nptr,char **endptr,int base);
将字符串转换成无符号长整型数(str,0,16)
unsigned long strtoul(const char *nptr,char **endptr,int base);
将字符串转换成长整型数(str,0,16)
endptr是一个传出参数,函数返回时指向后面未被识别的第一个字符。
例如char *pos; strtol("123abc", &pos, 10);,strtol返回123,pos指向字符串中的字母a


=============================================================
7.int at_tok_nextint(char **p_cur, int *p_out)
  int at_tok_nexthexint(char **p_cur, int *p_out)
Purpose:Parses the next base 16 integer in the AT response line and places it in *p_out
----------------------------------------------
具体分析:
 at_tok_nextint(&line, &response[0])
 {
  ret = nextTok(line);//分割line后给line,line分割前部分给ret。
  l = strtol(ret, &end, base);
  response[0]= (int)l;
 }

================================================================
8.对命令响应的处理:

requestRegistrationState
{
err = at_send_command_singleline(cmd, prefix, &p_response);//响应在p_response
line = p_response->p_intermediates->line;//line=+CREG: 2,5, 240090, 1039
err = at_tok_start(&line);//line=2,5, 240090, 1039
commas//commas=3逗号的个数
case 3: //line=2,5, 240090, 1039
       err = at_tok_nextint(&line, &skip);//2
    err = at_tok_nextint(&line, &response[0]);//5
       err = at_tok_nexthexint(&line, &response[1]); //240090      
       err = at_tok_nexthexint(&line, &response[2]);//1039

}
-----------------

extern void bootupGetIccid(RILChannelCtx *p_channel) { 1477 int result = 0; 1478 char hashedIccid[PROPERTY_VALUE_MAX] = { 0 }; 1479 ATResponse *p_response = NULL; 1480 int err = at_send_command_singleline("AT+ICCID?", "+ICCID:", &p_response, p_channel); 1481 RIL_SOCKET_ID rid = getRILIdByChannelCtx(p_channel); 1482 if (err >= 0 && p_response != NULL) { 1483 if (at_get_cme_error(p_response) == CME_SUCCESS) { 1484 char *line; 1485 char *iccId; 1486 line = p_response->p_intermediates->line; 1487 err = at_tok_start (&line); 1488 if (err >= 0) { 1489 err = at_tok_nextstr(&line, &iccId); 1490 if (err >= 0) { 1491 char maskIccid[PROPERTY_VALUE_MAX] = {0}; 1492 givePrintableIccid(iccId, maskIccid); 1493 RLOGD("bootupGetIccid[%d] iccid is %s", rid, maskIccid); 1494 // Convert all the lower case characters in iccid to upper case 1495 stringToUpper(iccId); 1496 hash256(iccId, hashedIccid, PROPERTY_VALUE_MAX); 1497 property_set(PROPERTY_ICCID_SIM[rid], hashedIccid); 1498 RLOGD("bootupGetIccid,rid: %d, iccid: %s", rid, hashedIccid); 1499 RIL_UNSOL_RESPONSE(RIL_UNSOL_ICCID_CHANGED, iccId, strlen(iccId), rid); 1500 result = 1; 1501 } else 1502 RLOGD("bootupGetIccid[%d]: get iccid error 2", rid); 1503 } else { 1504 RLOGD("bootupGetIccid[%d]: get iccid error 1", rid); 1505 } 1506 } else { 1507 RLOGD("bootupGetIccid[%d]: Error or no SIM inserted!", rid); 1508 } 1509 } else { 1510 RLOGE("bootupGetIccid[%d] Fail", rid); 1511 } 1512 at_response_free(p_response); 1513 1514 if (!result) { 1515 RLOG
最新发布
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值