| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <stdio.h>
 | 
 | 2 | #include <stdlib.h>
 | 
 | 3 | #include <string.h>
 | 
 | 4 | #include <time.h>
 | 
 | 5 | #include <fcntl.h>
 | 
 | 6 | 
 | 
 | 7 | #include "cc_at.h"
 | 
 | 8 | #include "cc_main.h"
 | 
 | 9 | 
 | 
 | 10 | 
 | 
 | 11 | 
 | 
 | 12 | #define ZAT_INVALID_LEN    -1
 | 
 | 13 | #define MSG_RETRY_NUM    4
 | 
 | 14 | #define MSG_RETRY_INTERVAL    50
 | 
 | 15 | 
 | 
 | 16 | static CHAR         g_zCc_RecvBuf[ATMAIN_AT_BUF_LEN + 1] = {0};
 | 
 | 17 | 
 | 
 | 18 | 
 | 
 | 19 | /* ÔÚ×Ö·û´®ÖÐѰÕÒÆ¥ÅäµÄ×Ó´®£¬Ë«ÒýºÅÄÚµÄÄÚÈݳýÍâ */
 | 
 | 20 | static PSTR zCc_FindSubStr(const PSTR pStr, const PSTR pSubStr,const SINT32 strLen)
 | 
 | 21 | {
 | 
 | 22 |     SINT32 subStrLen    = 0;
 | 
 | 23 |     BOOL   bInSideQuote = FALSE; /* ±ê¼ÇÊÇ·ñÔÚË«ÒýºÅÄÚ */
 | 
 | 24 |     UINT16   wIndex       = 0;
 | 
 | 25 |     PSTR   pTmpStr      = pStr;
 | 
 | 26 | 
 | 
 | 27 |     if ((pStr == ZUFI_NULL) || (pSubStr == ZUFI_NULL) || (strLen <= 0))
 | 
 | 28 |     {
 | 
 | 29 |         return ZUFI_NULL;
 | 
 | 30 |     }
 | 
 | 31 | 
 | 
 | 32 |     subStrLen = (SINT32)strlen((CHAR *)pSubStr);
 | 
 | 33 | 
 | 
 | 34 |     if (strLen < subStrLen)
 | 
 | 35 |     {
 | 
 | 36 |         return ZUFI_NULL;
 | 
 | 37 |     }
 | 
 | 38 | 
 | 
 | 39 |     for (wIndex=0; wIndex<((strLen-subStrLen)+1); wIndex++)
 | 
 | 40 |     {
 | 
 | 41 |         /* ÅжÏÊÇ·ñÔÚË«ÒýºÅÄÚ */
 | 
 | 42 |         if (*pTmpStr == '"')
 | 
 | 43 |         {
 | 
 | 44 |             bInSideQuote = !bInSideQuote;
 | 
 | 45 |         }
 | 
 | 46 |         /* ×Ó´®±È½Ï */
 | 
 | 47 |         if (!bInSideQuote && *pTmpStr == *pSubStr)
 | 
 | 48 |         {
 | 
 | 49 |             if (strncmp((CHAR *)pTmpStr, (CHAR *)pSubStr, (UINT32)subStrLen) == 0)
 | 
 | 50 |             {
 | 
 | 51 |                 return pTmpStr;
 | 
 | 52 |             }
 | 
 | 53 |         }
 | 
 | 54 |         pTmpStr++;
 | 
 | 55 |     }
 | 
 | 56 | 
 | 
 | 57 |     return ZUFI_NULL;
 | 
 | 58 | }
 | 
 | 59 | 
 | 
 | 60 | 
 | 
 | 61 | 
 | 
 | 62 | /* ´Óµ±Ç°×Ö·û´®ÖлñÈ¡ATÏìÓ¦×Ó´®£¬·µ»Ø×Ó´®³¤¶È */
 | 
 | 63 | static SINT32 zCc_GetSubAtStr(const PSTR pCurAtStr, PSTR pSubAtStr, SINT32 iDataLen, PSTR atRecvBuf)
 | 
 | 64 | {
 | 
 | 65 |     PSTR   pAtHead      = ZUFI_NULL; /* ATÏìӦͷ²¿ */
 | 
 | 66 |     PSTR   pAtTail      = ZUFI_NULL; /* ATÏìӦβ²¿ */
 | 
 | 67 |     SINT32 iSubAtStrLen = ZAT_INVALID_LEN; /* ATÏìÓ¦×Ó´®³¤¶È */
 | 
 | 68 |     SINT32 iFreeDataLen = ZAT_INVALID_LEN; /* δ½âÎöµÄATÏìÓ¦Êý¾Ý³¤¶È */
 | 
 | 69 | 
 | 
 | 70 |     if ((pCurAtStr == ZUFI_NULL) || (pSubAtStr == ZUFI_NULL))
 | 
 | 71 |     {
 | 
 | 72 |         return ZAT_INVALID_LEN;
 | 
 | 73 |     }
 | 
 | 74 | 
 | 
 | 75 |     /* ÔÚATÏìÓ¦´®ÖÐËÑË÷ATÏìÓ¦¿ªÊ¼´¦µÄ"\r\n" */
 | 
 | 76 |     iFreeDataLen = iDataLen - (pCurAtStr - atRecvBuf);
 | 
 | 77 |     pAtHead = zCc_FindSubStr(pCurAtStr, (PSTR)"\r\n", iFreeDataLen);
 | 
 | 78 |     if (pAtHead == ZUFI_NULL)
 | 
 | 79 |     {
 | 
 | 80 |         return ZAT_INVALID_LEN;
 | 
 | 81 |     }
 | 
 | 82 | 
 | 
 | 83 |     /* ÔÚATÏìÓ¦´®ÖÐËÑË÷ATÏìÓ¦½áÊø´¦µÄ"\r\n" */
 | 
 | 84 |     pAtHead += 2;
 | 
 | 85 |     iFreeDataLen = iDataLen - (pAtHead - atRecvBuf);
 | 
 | 86 |     pAtTail = zCc_FindSubStr(pAtHead, (PSTR)"\r\n", iFreeDataLen);
 | 
 | 87 |     if (pAtTail == ZUFI_NULL)
 | 
 | 88 |     {
 | 
 | 89 |         if (strlen((CHAR *)pAtHead) == 0)
 | 
 | 90 |         {
 | 
 | 91 |             return ZAT_INVALID_LEN;
 | 
 | 92 |         }
 | 
 | 93 |         else
 | 
 | 94 |         {
 | 
 | 95 |             pAtTail = pAtHead + strlen((CHAR *)pAtHead);    
 | 
 | 96 |         }
 | 
 | 97 |     }
 | 
 | 98 | 
 | 
 | 99 |     /* È¡³öÁ½¸ö"\r\n"Ö®¼äµÄATÏìÓ¦ */
 | 
 | 100 |     iSubAtStrLen = pAtTail - pAtHead;
 | 
 | 101 |     if ((iSubAtStrLen > 0) && (iSubAtStrLen < AT_CMD_MAX))
 | 
 | 102 |     {
 | 
 | 103 |         memcpy(pSubAtStr, pAtHead, (UINT32)iSubAtStrLen); 
 | 
 | 104 |         return iSubAtStrLen;
 | 
 | 105 |     }
 | 
 | 106 | 
 | 
 | 107 |     return iSubAtStrLen == 0? 0 : ZAT_INVALID_LEN;
 | 
 | 108 | }
 | 
 | 109 | static void skipWhiteSpace(char **p_cur)
 | 
 | 110 | {
 | 
 | 111 |     if (*p_cur == NULL)
 | 
 | 112 |     {
 | 
 | 113 |         return;
 | 
 | 114 |     }
 | 
 | 115 | 
 | 
 | 116 |     while (**p_cur != '\0' && isspace(**p_cur))
 | 
 | 117 |     {
 | 
 | 118 |         (*p_cur)++;
 | 
 | 119 |     }
 | 
 | 120 | }
 | 
 | 121 | char *strsep(char **stringp, const char *delim)
 | 
 | 122 | {
 | 
 | 123 |         char *s;
 | 
 | 124 |         const char *spanp;
 | 
 | 125 |         int c, sc;
 | 
 | 126 |         char *tok;
 | 
 | 127 | 
 | 
 | 128 |         if ((s = *stringp) == NULL)
 | 
 | 129 |                 return (NULL);
 | 
 | 130 |         for (tok = s;;) {
 | 
 | 131 |                 c = *s++;
 | 
 | 132 |                 spanp = delim;
 | 
 | 133 |                 do {
 | 
 | 134 |                         if ((sc = *spanp++) == c) {
 | 
 | 135 |                                 if (c == 0)
 | 
 | 136 |                                         s = NULL;
 | 
 | 137 |                                 else
 | 
 | 138 |                                         s[-1] = 0;
 | 
 | 139 |                                 *stringp = s;
 | 
 | 140 |                                 return (tok);
 | 
 | 141 |                         }
 | 
 | 142 |                 } while (sc != 0);
 | 
 | 143 |         }
 | 
 | 144 |         /* NOTREACHED */
 | 
 | 145 | }
 | 
 | 146 | void skipNextComma(char **p_cur)
 | 
 | 147 | {
 | 
 | 148 |     if (*p_cur == NULL)
 | 
 | 149 |     {
 | 
 | 150 |         return;
 | 
 | 151 |     }
 | 
 | 152 | 
 | 
 | 153 |     while (**p_cur != '\0' && **p_cur != ',')
 | 
 | 154 |     {
 | 
 | 155 |         (*p_cur)++;
 | 
 | 156 |     }
 | 
 | 157 | 
 | 
 | 158 |     if (**p_cur == ',')
 | 
 | 159 |     {
 | 
 | 160 |         (*p_cur)++;
 | 
 | 161 |     }
 | 
 | 162 | }
 | 
 | 163 | static char * nextTok(char **p_cur)
 | 
 | 164 | {
 | 
 | 165 |     char    *ret = NULL;
 | 
 | 166 | 
 | 
 | 167 |     skipWhiteSpace(p_cur);
 | 
 | 168 | 
 | 
 | 169 |     if (*p_cur == NULL)
 | 
 | 170 |     {
 | 
 | 171 |         ret = NULL;
 | 
 | 172 |     }
 | 
 | 173 |     else if (**p_cur == '"')
 | 
 | 174 |     {
 | 
 | 175 |         (*p_cur)++;
 | 
 | 176 |         ret = (char*)strsep(p_cur, "\"");
 | 
 | 177 |         skipNextComma(p_cur);
 | 
 | 178 |     }
 | 
 | 179 |     else
 | 
 | 180 |     {
 | 
 | 181 |         ret = (char*)strsep(p_cur, ",");
 | 
 | 182 |     }
 | 
 | 183 | 
 | 
 | 184 |     return ret;
 | 
 | 185 | }
 | 
 | 186 | SINT32 zCc_at_tok_start(char **p_cur)
 | 
 | 187 | {
 | 
 | 188 |     if (*p_cur == NULL)
 | 
 | 189 |     {
 | 
 | 190 |         return -1;
 | 
 | 191 |     }
 | 
 | 192 | 
 | 
 | 193 |     // skip prefix
 | 
 | 194 |     // consume "^[^:]:"
 | 
 | 195 | 
 | 
 | 196 |     *p_cur = strchr(*p_cur, ':');
 | 
 | 197 | 
 | 
 | 198 |     if (*p_cur == NULL)
 | 
 | 199 |     {
 | 
 | 200 |         return -1;
 | 
 | 201 |     }
 | 
 | 202 | 
 | 
 | 203 |     (*p_cur)++;
 | 
 | 204 | 
 | 
 | 205 |     return 0;
 | 
 | 206 | }
 | 
 | 207 | /**
 | 
 | 208 |  * Parses the next integer in the AT response line and places it in *p_out
 | 
 | 209 |  * returns 0 on success and -1 on fail
 | 
 | 210 |  * updates *p_cur
 | 
 | 211 |  * "base" is the same as the base param in strtol
 | 
 | 212 |  */
 | 
 | 213 | 
 | 
 | 214 | static SINT32 zCc_at_tok_nextint_base(char **p_cur, SINT32 *p_out, SINT32 base, SINT32 uns)
 | 
 | 215 | {
 | 
 | 216 |     char    *ret;
 | 
 | 217 | 
 | 
 | 218 |     if (*p_cur == NULL)
 | 
 | 219 |     {
 | 
 | 220 |         return -1;
 | 
 | 221 |     }
 | 
 | 222 | 
 | 
 | 223 |     ret = nextTok(p_cur);
 | 
 | 224 | 
 | 
 | 225 |     if (ret == NULL)
 | 
 | 226 |     {
 | 
 | 227 |         return -1;
 | 
 | 228 |     }
 | 
 | 229 |     else
 | 
 | 230 |     {
 | 
 | 231 |         long    l;
 | 
 | 232 |         char    *end;
 | 
 | 233 | 		errno = 0;// kw CXX.ERRNO.NOT_CHECKED
 | 
 | 234 |         if (uns)
 | 
 | 235 |         {
 | 
 | 236 |             l = strtoul(ret, &end, base);
 | 
 | 237 |         }
 | 
 | 238 |         else
 | 
 | 239 |         {
 | 
 | 240 |             l = strtol(ret, &end, base);
 | 
 | 241 |         }
 | 
 | 242 | 		if (errno == ERANGE)// kw CXX.ERRNO.NOT_CHECKED
 | 
 | 243 | 		{
 | 
 | 244 | 			zte_log_append(__FILE__, __LINE__, "strtoul errno %d: %s\n", errno, strerror(errno));
 | 
 | 245 | 		}
 | 
 | 246 |         *p_out = (SINT32) l;
 | 
 | 247 | 
 | 
 | 248 |         if (end == ret)
 | 
 | 249 |         {
 | 
 | 250 |             return -1;
 | 
 | 251 |         }
 | 
 | 252 |     }
 | 
 | 253 | 
 | 
 | 254 |     return 0;
 | 
 | 255 | }
 | 
 | 256 | 
 | 
 | 257 | /**
 | 
 | 258 |  * Parses the next base 10 integer in the AT response line
 | 
 | 259 |  * and places it in *p_out
 | 
 | 260 |  * returns 0 on success and -1 on fail
 | 
 | 261 |  * updates *p_cur
 | 
 | 262 |  */
 | 
 | 263 | SINT32 zCc_at_tok_nextint(char **p_cur, SINT32 *p_out)
 | 
 | 264 | {
 | 
 | 265 |     return zCc_at_tok_nextint_base(p_cur, p_out, 10, 0);
 | 
 | 266 | }
 | 
 | 267 | 
 | 
 | 268 | int zCcApp_EncAtReqCcfc(char* ptMsgBuf)
 | 
 | 269 | {
 | 
 | 270 |         int Fd ;
 | 
 | 271 |         char AtCmdBuf[512] = {0};
 | 
 | 272 |         int AtLen;  
 | 
 | 273 | 
 | 
 | 274 |         zCcApp_SetAtChnlCmdId(CC_APP_AT_CMD_REQ_SET_CCFC);
 | 
 | 275 |         sprintf(AtCmdBuf, "%s",ptMsgBuf);
 | 
 | 276 |         AtLen = strlen(AtCmdBuf);
 | 
 | 277 |         Fd = zCcApp_GetAtCmdFd();
 | 
 | 278 |         return zCcApp_PortSend(Fd,AtCmdBuf,AtLen);
 | 
 | 279 | }
 | 
 | 280 | 
 | 
 | 281 | int zCcApp_EncAtReqClck(char* ptMsgBuf)
 | 
 | 282 | {
 | 
 | 283 |         int Fd ;
 | 
 | 284 |         char AtCmdBuf[512] = {0};
 | 
 | 285 |         int AtLen;  
 | 
 | 286 | 
 | 
 | 287 |         zCcApp_SetAtChnlCmdId(CC_APP_AT_CMD_REQ_SET_CLCK);
 | 
 | 288 |         sprintf(AtCmdBuf, "%s",ptMsgBuf);
 | 
 | 289 |         AtLen = strlen(AtCmdBuf);
 | 
 | 290 |         Fd = zCcApp_GetAtCmdFd();
 | 
 | 291 |         return zCcApp_PortSend(Fd,AtCmdBuf,AtLen);
 | 
 | 292 | }
 | 
 | 293 | 
 | 
 | 294 | int zCcApp_EncAtReqD(char* ptMsgBuf)
 | 
 | 295 | {
 | 
 | 296 | 
 | 
 | 297 |         int Fd ;
 | 
 | 298 |         char AtCmdBuf[512] = {0};
 | 
 | 299 |         int AtLen;  
 | 
 | 300 | 
 | 
 | 301 |         zCcApp_SetAtChnlCmdId(CC_APP_AT_CMD_REQ_D);
 | 
 | 302 |         sprintf(AtCmdBuf, "%s",ptMsgBuf);
 | 
 | 303 |         AtLen = strlen(AtCmdBuf);
 | 
 | 304 |         Fd = zCcApp_GetAtCmdFd();
 | 
 | 305 |     zte_log_append(__FILE__, __LINE__, "zte_ccapp.log","%s  zCcApp_EncAtReqD:AtcmdMsg = %s\n",__FUNCTION__,AtCmdBuf);
 | 
 | 306 |         return zCcApp_PortSend(Fd,AtCmdBuf,AtLen);
 | 
 | 307 | }
 | 
 | 308 | 
 | 
 | 309 | int zCcApp_EncAtReqA(VOID)
 | 
 | 310 | {
 | 
 | 311 |         int Fd ;
 | 
 | 312 |         char  AtCmdBuf[512] = {0};
 | 
 | 313 |         int AtLen;
 | 
 | 314 | 
 | 
 | 315 |         zCcApp_SetAtChnlCmdId(CC_APP_AT_CMD_REQ_A);
 | 
 | 316 | 
 | 
 | 317 |         sprintf(AtCmdBuf, "ATA\r\n");
 | 
 | 318 |         AtLen = strlen(AtCmdBuf);
 | 
 | 319 | 
 | 
 | 320 |         /*·¢ËÍ*/
 | 
 | 321 |         Fd = zCcApp_GetAtCmdFd();
 | 
 | 322 |         return zCcApp_PortSend(Fd,AtCmdBuf,AtLen);
 | 
 | 323 | }
 | 
 | 324 | 
 | 
 | 325 | int zCcApp_EncAtReqChup(void)
 | 
 | 326 | {
 | 
 | 327 |         int Fd ;
 | 
 | 328 |         char  AtCmdBuf[512] = {0};
 | 
 | 329 |         int AtLen;
 | 
 | 330 | 
 | 
 | 331 |         zCcApp_SetAtChnlCmdId(CC_APP_AT_CMD_REQ_CHUP);
 | 
 | 332 | 
 | 
 | 333 |         sprintf(AtCmdBuf, "AT+CHUP\r\n");
 | 
 | 334 |         AtLen = strlen(AtCmdBuf);
 | 
 | 335 | 
 | 
 | 336 |         Fd = zCcApp_GetAtCmdFd();
 | 
 | 337 |         return zCcApp_PortSend(Fd,AtCmdBuf,AtLen);
 | 
 | 338 | }
 | 
 | 339 | 
 | 
 | 340 | int zCcApp_EncAtReqVts(char *ptMsgBuf)
 | 
 | 341 | {
 | 
 | 342 |         int Fd ;
 | 
 | 343 |         char  AtCmdBuf[512] = {0};
 | 
 | 344 |         int AtLen;
 | 
 | 345 | 
 | 
 | 346 |         zCcApp_SetAtChnlCmdId(CC_APP_AT_CMD_REQ_VTS);
 | 
 | 347 | 
 | 
 | 348 |         sprintf(AtCmdBuf, "%s",ptMsgBuf);        
 | 
 | 349 |         AtLen = strlen(AtCmdBuf);
 | 
 | 350 | 
 | 
 | 351 |         /*·¢ËÍ*/
 | 
 | 352 |         Fd = zCcApp_GetAtCmdFd();
 | 
 | 353 |         return zCcApp_PortSend(Fd,AtCmdBuf,AtLen);
 | 
 | 354 | }
 | 
 | 355 | 
 | 
 | 356 | int zCcApp_EncAtReqImsPlus(char *ptMsgBuf)
 | 
 | 357 | {
 | 
 | 358 |         int Fd ;
 | 
 | 359 |         char  AtCmdBuf[512] = {0};
 | 
 | 360 |         int AtLen;
 | 
 | 361 | 
 | 
 | 362 |         zCcApp_SetAtChnlCmdId(CC_APP_AT_CMD_REQ_IMSPLUS);
 | 
 | 363 | 
 | 
 | 364 |         sprintf(AtCmdBuf, "%s", ptMsgBuf);        
 | 
 | 365 |         AtLen = strlen(AtCmdBuf);
 | 
 | 366 | 
 | 
 | 367 |         /*·¢ËÍ*/
 | 
 | 368 |         Fd = zCcApp_GetAtCmdFd();
 | 
 | 369 |         return zCcApp_PortSend(Fd,AtCmdBuf,AtLen);
 | 
 | 370 | }
 | 
 | 371 | 
 | 
 | 372 | int zCcApp_EncAtReqChld(char *AtcmdMsg)
 | 
 | 373 | {
 | 
 | 374 |         int Fd ;
 | 
 | 375 |         int AtLen;        
 | 
 | 376 | 
 | 
 | 377 |         zCcApp_SetAtChnlCmdId(CC_APP_AT_CMD_REQ_SET_CHLD);
 | 
 | 378 |         AtLen = strlen(AtcmdMsg);
 | 
 | 379 | 
 | 
 | 380 |         /*·¢ËÍ*/
 | 
 | 381 |         Fd = zCcApp_GetAtCmdFd();
 | 
 | 382 |         return zCcApp_PortSend(Fd,AtcmdMsg,AtLen);
 | 
 | 383 | }
 | 
 | 384 | 
 |