• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "at_network.h"
17 
18 #include <signal.h>
19 #include <string.h>
20 
21 #include "hril_notification.h"
22 #include "vendor_report.h"
23 #include "vendor_util.h"
24 
25 #define NUM_COUNT 3
26 #define TIME_VALUE_LEN 30
27 #define PLMN_LEN 10
28 
29 static struct ReportInfo g_reportInfoForOperListToUse;
30 static pthread_mutex_t g_networkSearchInformationMutex = PTHREAD_MUTEX_INITIALIZER;
31 
32 struct ResponseAck {
33     ResponseInfo *responseInfo;
34     uint8_t *respDataPointer;
35     int32_t respDataLen;
36 };
37 
38 // RadioAccessFamily defines
39 const int32_t RAF_UNKNOWN = 1 << RADIO_TECHNOLOGY_UNKNOWN;
40 const int32_t RAF_GSM = 1 << RADIO_TECHNOLOGY_GSM;
41 const int32_t RAF_1XRTT = 1 << RADIO_TECHNOLOGY_1XRTT;
42 const int32_t RAF_WCDMA = 1 << RADIO_TECHNOLOGY_WCDMA;
43 const int32_t RAF_HSPA = 1 << RADIO_TECHNOLOGY_HSPA;
44 const int32_t RAF_HSPAP = 1 << RADIO_TECHNOLOGY_HSPAP;
45 const int32_t RAF_TD_SCDMA = 1 << RADIO_TECHNOLOGY_TD_SCDMA;
46 const int32_t RAF_EVDO = 1 << RADIO_TECHNOLOGY_EVDO;
47 const int32_t RAF_EHRPD = 1 << RADIO_TECHNOLOGY_EHRPD;
48 const int32_t RAF_LTE = 1 << RADIO_TECHNOLOGY_LTE;
49 const int32_t RAF_LTE_CA = 1 << RADIO_TECHNOLOGY_LTE_CA;
50 const int32_t RAF_NR = 1 << RADIO_TECHNOLOGY_NR;
51 
52 // Group
53 const int32_t GSM = RAF_GSM;
54 const int32_t CDMA = RAF_1XRTT;
55 const int32_t EVDO = RAF_EVDO | RAF_EHRPD;
56 const int32_t HS = RAF_WCDMA | RAF_HSPA | RAF_HSPAP;
57 const int32_t WCDMA = HS;
58 const int32_t LTE = RAF_LTE | RAF_LTE_CA;
59 const int32_t NR = RAF_NR;
60 
61 // NG
62 const int32_t RAF_2G = GSM | CDMA;
63 const int32_t RAF_3G = WCDMA | EVDO | RAF_TD_SCDMA;
64 const int32_t RAF_4G = LTE;
65 const int32_t RAF_5G = NR;
66 const int32_t RAF_AUTO = RAF_2G | RAF_3G | RAF_4G | RAF_5G;
67 const size_t TIME_VALUE_OFFSET = 2;
68 const int32_t ADD_LENGTH = 3;
69 
GetResponseErrorCode(ResponseInfo * pResponseInfo)70 static int32_t GetResponseErrorCode(ResponseInfo *pResponseInfo)
71 {
72     char *pLine = NULL;
73     int32_t ret = HRIL_ERR_GENERIC_FAILURE;
74     if (pResponseInfo && pResponseInfo->result && strlen(pResponseInfo->result) != 0) {
75         pLine = pResponseInfo->result;
76         SkipATPrefix(&pLine);
77         NextInt(&pLine, &ret);
78     }
79 
80     if (ret == -1) {
81         ret = HRIL_ERR_INVALID_RESPONSE;
82     }
83     TELEPHONY_LOGD("networks response error code: %{public}d", ret);
84     return ret;
85 }
86 
ResponseNetworkReport(int32_t slotId,const ReqDataInfo * requestInfo,int32_t err,struct ResponseAck * respDataAck)87 static int32_t ResponseNetworkReport(
88     int32_t slotId, const ReqDataInfo *requestInfo, int32_t err, struct ResponseAck *respDataAck)
89 {
90     if (requestInfo == NULL) {
91         TELEPHONY_LOGE("requestInfo is nullptr!");
92         return HRIL_ERR_GENERIC_FAILURE;
93     }
94     if (respDataAck == NULL) {
95         TELEPHONY_LOGE("respDataAck is nullptr!");
96         return HRIL_ERR_GENERIC_FAILURE;
97     }
98     if (err < HRIL_ERR_SUCCESS) {
99         err = HRIL_ERR_GENERIC_FAILURE;
100     }
101     struct ReportInfo reportInfo;
102     reportInfo = CreateReportInfo(requestInfo, err, HRIL_RESPONSE, 0);
103     OnNetworkReport(slotId, reportInfo, (const uint8_t *)(respDataAck->respDataPointer), respDataAck->respDataLen);
104     if (respDataAck->responseInfo != NULL) {
105         FreeResponseInfo(respDataAck->responseInfo);
106     }
107     return err;
108 }
109 
ConvertActToRadioTech(HRilActTech actType)110 static HRilRadioTech ConvertActToRadioTech(HRilActTech actType)
111 {
112     switch (actType) {
113         case HRIL_RADIO_GSM:
114         case HRIL_RADIO_GSM_COMPACT:
115         case HRIL_RADIO_EGPRS:
116             return RADIO_TECHNOLOGY_GSM;
117         case HRIL_RADIO_HSDPA_HSUPA:
118         case HRIL_RADIO_HSDPA:
119         case HRIL_RADIO_HSUPA:
120             return RADIO_TECHNOLOGY_HSPA;
121         case HRIL_RADIO_HSPAP:
122             return RADIO_TECHNOLOGY_HSPAP;
123         case HRIL_RADIO_UTRAN:
124             return RADIO_TECHNOLOGY_WCDMA;
125         case HRIL_RADIO_EUTRAN:
126             return RADIO_TECHNOLOGY_LTE;
127         case HRIL_RADIO_CDMA:
128         case HRIL_RADIO_CDMA_IS95A:
129         case HRIL_RADIO_CDMA_IS95B:
130             return RADIO_TECHNOLOGY_1XRTT;
131         case HRIL_RADIO_CDMA_EVDO_0:
132         case HRIL_RADIO_CDMA_EVDO_A:
133         case HRIL_RADIO_CDMA_EVDO_B:
134             return RADIO_TECHNOLOGY_EVDO;
135         case HRIL_RADIO_CDMA_EHRPD:
136             return RADIO_TECHNOLOGY_EHRPD;
137         case HRIL_RADIO_TDSCDMA:
138             return RADIO_TECHNOLOGY_TD_SCDMA;
139         case HRIL_RADIO_LTE_CA:
140             return RADIO_TECHNOLOGY_LTE_CA;
141         case HRIL_RADIO_NR:
142             return RADIO_TECHNOLOGY_NR;
143         default:
144             return RADIO_TECHNOLOGY_UNKNOWN;
145     }
146 }
147 
FindSemicolonCharNum(const char * srcStr)148 static int32_t FindSemicolonCharNum(const char *srcStr)
149 {
150     char *str = (char *)srcStr;
151     if (str == NULL) {
152         TELEPHONY_LOGE("Semicolon srcStr parameter is null.");
153         return -1;
154     }
155     if (*str == '\0') {
156         return -1;
157     }
158     int32_t charNum = 0;
159     while (*str != '\0') {
160         if (*str == ';') {
161             charNum++;
162         }
163         str++;
164     }
165     return charNum;
166 }
167 
ConvertIntToHRilRegStatus(int32_t reg)168 static HRilRegStatus ConvertIntToHRilRegStatus(int32_t reg)
169 {
170     if ((reg >= NO_REG_MT_NO_SEARCH) && (reg <= REG_MT_EMERGENCY)) {
171         return (HRilRegStatus)reg;
172     } else {
173         return REG_MT_UNKNOWN;
174     }
175 }
176 
ExtractRegStatus(const char * s,const HRilRegStatusInfo * hrilRegInfo)177 static int32_t ExtractRegStatus(const char *s, const HRilRegStatusInfo *hrilRegInfo)
178 {
179     char *str = (char *)s;
180     HRilRegStatusInfo *regStateInfo = (HRilRegStatusInfo *)hrilRegInfo;
181     int32_t info[MAX_5GREG_INFO_ITEM] = {0, 0, 0, RADIO_TECHNOLOGY_INVALID, 0, 0, 0};
182     const int32_t FORMAT_ONE_COMMA_NUM = 0;
183     const int32_t FORMAT_TWO_COMMA_NUM = 3;
184     const int32_t FORMAT_TWO_5G_COMMA_NUM = 6;
185     int32_t commaNum = FindCommaCharNum(str);
186     if (NextInt(&str, &(info[REG_STAT_POS])) != 0) {
187         return HRIL_ERR_INVALID_RESPONSE;
188     }
189     if (commaNum == FORMAT_ONE_COMMA_NUM) {
190     } else if ((commaNum == FORMAT_TWO_COMMA_NUM) || (commaNum == FORMAT_TWO_5G_COMMA_NUM)) {
191         if (NextIntFromHex(&str, &(info[REG_LAC_POS])) != 0) {
192             return HRIL_ERR_INVALID_RESPONSE;
193         }
194         if (NextIntFromHex(&str, &(info[REG_CELL_ID_POS])) != 0) {
195             return HRIL_ERR_INVALID_RESPONSE;
196         }
197         if (NextInt(&str, &(info[REG_ACT_POS])) != 0) {
198             return HRIL_ERR_INVALID_RESPONSE;
199         }
200         if (commaNum == FORMAT_TWO_5G_COMMA_NUM) {
201             if (NextInt(&str, &(info[REG_NR_AVAILABLE_POS])) != 0) {
202                 return HRIL_ERR_INVALID_RESPONSE;
203             }
204             if (NextInt(&str, &(info[REG_EN_DC_AVAILABLE_POS])) != 0) {
205                 return HRIL_ERR_INVALID_RESPONSE;
206             }
207             if (NextInt(&str, &(info[REG_DC_NR_RESTRICTED_POS])) != 0) {
208                 return HRIL_ERR_INVALID_RESPONSE;
209             }
210         }
211     } else {
212         return HRIL_ERR_INVALID_RESPONSE;
213     }
214     regStateInfo->regStatus = ConvertIntToHRilRegStatus(info[REG_STAT_POS]);
215     regStateInfo->lacCode = info[REG_LAC_POS];
216     regStateInfo->cellId = info[REG_CELL_ID_POS];
217     regStateInfo->actType = ConvertActToRadioTech((HRilActTech)info[REG_ACT_POS]);
218     regStateInfo->isNrAvailable = ((info[REG_NR_AVAILABLE_POS] == 0) ? 0 : 1);
219     regStateInfo->isEnDcAvailable = ((info[REG_EN_DC_AVAILABLE_POS] == 0) ? 0 : 1);
220     regStateInfo->isDcNrRestricted = ((info[REG_DC_NR_RESTRICTED_POS] == 0) ? 0 : 1);
221     return HRIL_ERR_SUCCESS;
222 }
223 
ParseRegStatusStr(const char * srcStr,HRilRegStatusInfo * hrilRegInfo)224 static int32_t ParseRegStatusStr(const char *srcStr, HRilRegStatusInfo *hrilRegInfo)
225 {
226     if (srcStr == NULL || hrilRegInfo == NULL) {
227         return HRIL_ERR_INVALID_PARAMETER;
228     }
229     if (ReportStrWith(srcStr, "+CREG:")) {
230         hrilRegInfo->regType = CS_REG_TYPE;
231     } else if (ReportStrWith(srcStr, "+CGREG:")) {
232         hrilRegInfo->regType = PS_REG_TYPE;
233     } else {
234         return HRIL_ERR_INVALID_RESPONSE;
235     }
236     char *str = (char *)srcStr;
237     if (SkipATPrefix(&str) < 0) {
238         TELEPHONY_LOGE("skip failed: [%{public}s]", str);
239         return HRIL_ERR_INVALID_RESPONSE;
240     }
241     const int32_t NUM_ONE = 1;
242     const int32_t NUM_THREE = 3;
243     const int32_t NUM_FOUR = 4;
244     const int32_t NUM_SIX = 6;
245     const int32_t NUM_SEVEN = 7;
246     hrilRegInfo->notifyMode = REG_NOTIFY_STAT_LAC_CELLID;
247     hrilRegInfo->regMsgType = REG_RESPONSE_TYPE;
248     int32_t commaNum = FindCommaCharNum(str);
249     if (commaNum == 0) {
250         hrilRegInfo->regMsgType = REG_NOTIFY_TYPE;
251         hrilRegInfo->notifyMode = REG_NOTIFY_STAT_ONLY;
252     } else if (commaNum == NUM_ONE) {
253         hrilRegInfo->notifyMode = REG_NOTIFY_STAT_ONLY;
254     } else if (commaNum == NUM_FOUR || commaNum == NUM_SEVEN) {
255     } else if (commaNum == NUM_THREE || commaNum == NUM_SIX) {
256         hrilRegInfo->regMsgType = REG_NOTIFY_TYPE;
257     } else {
258         hrilRegInfo->regMsgType = REG_NOTIFY_TYPE;
259         return HRIL_ERR_INVALID_RESPONSE;
260     }
261     int32_t skip = 0;
262     if (hrilRegInfo->regMsgType == REG_RESPONSE_TYPE) {
263         if ((NextInt(&str, &skip)) != 0) {
264             TELEPHONY_LOGE("skip failed: [%{public}s]", str);
265             return HRIL_ERR_INVALID_RESPONSE;
266         }
267         if ((skip != (int32_t)REG_NOTIFY_STAT_LAC_CELLID) && (skip != (int32_t)REG_NOTIFY_STAT_ONLY)) {
268             TELEPHONY_LOGE("notifyType check failed: [%{public}d]", skip);
269             return HRIL_ERR_INVALID_RESPONSE;
270         }
271     }
272     return ExtractRegStatus(str, hrilRegInfo);
273 }
274 
ProcessRegStatus(const char * s,const HRilRegStatusInfo * hrilRegStateInfo)275 int32_t ProcessRegStatus(const char *s, const HRilRegStatusInfo *hrilRegStateInfo)
276 {
277     char *str = (char *)s;
278     HRilRegStatusInfo *regStateInfo = (HRilRegStatusInfo *)hrilRegStateInfo;
279     if ((str == NULL) || (regStateInfo == NULL)) {
280         TELEPHONY_LOGE("ProcessRegStatus s or regStateInfo param is null");
281         return HRIL_ERR_NULL_POINT;
282     } else {
283         (void)memset_s(regStateInfo, sizeof(HRilRegStatusInfo), 0, sizeof(HRilRegStatusInfo));
284         return ParseRegStatusStr(str, regStateInfo);
285     }
286 }
287 
ParseGetGsmSignalStrength(const char * line,HRilRssi * hrilRssi)288 static void ParseGetGsmSignalStrength(const char *line, HRilRssi *hrilRssi)
289 {
290     char *lineStr = (char *)line;
291     if (lineStr == NULL || hrilRssi == NULL) {
292         TELEPHONY_LOGE("line or hrilRssi is null!!!");
293         return;
294     }
295     NextInt(&lineStr, &hrilRssi->gsmRssi.rxlev);
296     NextInt(&lineStr, &hrilRssi->gsmRssi.ber);
297     hrilRssi->gsmRssi.rxlev = -hrilRssi->gsmRssi.rxlev;
298 }
299 
ParseGetLteSignalStrength(const char * line,HRilRssi * hrilRssi)300 static void ParseGetLteSignalStrength(const char *line, HRilRssi *hrilRssi)
301 {
302     char *lineStr = (char *)line;
303     if (lineStr == NULL || hrilRssi == NULL) {
304         TELEPHONY_LOGE("line or hrilRssi is null!!!");
305         return;
306     }
307     NextInt(&lineStr, &hrilRssi->lteRssi.rxlev);
308     NextInt(&lineStr, &hrilRssi->lteRssi.rsrq);
309     NextInt(&lineStr, &hrilRssi->lteRssi.rsrp);
310     NextInt(&lineStr, &hrilRssi->lteRssi.snr);
311     hrilRssi->lteRssi.rxlev = -hrilRssi->lteRssi.rxlev;
312     hrilRssi->lteRssi.rsrq = -hrilRssi->lteRssi.rsrq;
313     hrilRssi->lteRssi.rsrp = -hrilRssi->lteRssi.rsrp;
314 }
315 
ParseGetWcdmaSignalStrength(const char * line,HRilRssi * hrilRssi)316 static void ParseGetWcdmaSignalStrength(const char *line, HRilRssi *hrilRssi)
317 {
318     char *lineStr = (char *)line;
319     if (lineStr == NULL || hrilRssi == NULL) {
320         TELEPHONY_LOGE("line or hrilRssi is null!!!");
321         return;
322     }
323     NextInt(&lineStr, &hrilRssi->wcdmaRssi.rxlev);
324     NextInt(&lineStr, &hrilRssi->wcdmaRssi.ecio);
325     NextInt(&lineStr, &hrilRssi->wcdmaRssi.rscp);
326     NextInt(&lineStr, &hrilRssi->wcdmaRssi.ber);
327     hrilRssi->wcdmaRssi.rxlev = -hrilRssi->wcdmaRssi.rxlev;
328     hrilRssi->wcdmaRssi.ecio = -hrilRssi->wcdmaRssi.ecio;
329     hrilRssi->wcdmaRssi.rscp = -hrilRssi->wcdmaRssi.rscp;
330 }
331 
ParseGetTdScdmaSignalStrength(const char * line,HRilRssi * hrilRssi)332 static void ParseGetTdScdmaSignalStrength(const char *line, HRilRssi *hrilRssi)
333 {
334     char *lineStr = (char *)line;
335     if (lineStr == NULL || hrilRssi == NULL) {
336         TELEPHONY_LOGE("line or hrilRssi is null!!!");
337         return;
338     }
339     NextInt(&lineStr, &hrilRssi->tdScdmaRssi.rscp);
340     hrilRssi->tdScdmaRssi.rscp = -hrilRssi->tdScdmaRssi.rscp;
341 }
342 
ParseGetCdmaSignalStrength(const char * line,HRilRssi * hrilRssi)343 static void ParseGetCdmaSignalStrength(const char *line, HRilRssi *hrilRssi)
344 {
345     char *lineStr = (char *)line;
346     if (lineStr == NULL || hrilRssi == NULL) {
347         TELEPHONY_LOGE("line or hrilRssi is null!!!");
348         return;
349     }
350     NextInt(&lineStr, &hrilRssi->cdmaRssi.absoluteRssi);
351     NextInt(&lineStr, &hrilRssi->cdmaRssi.ecno);
352     hrilRssi->cdmaRssi.absoluteRssi = -hrilRssi->cdmaRssi.absoluteRssi;
353     hrilRssi->cdmaRssi.ecno = -hrilRssi->cdmaRssi.ecno;
354 }
355 
ParseGetNrSignalStrength(const char * line,HRilRssi * hrilRssi)356 static void ParseGetNrSignalStrength(const char *line, HRilRssi *hrilRssi)
357 {
358     char *lineStr = (char *)line;
359     if (lineStr == NULL || hrilRssi == NULL) {
360         TELEPHONY_LOGE("line or hrilRssi is null!!!");
361         return;
362     }
363     NextInt(&lineStr, &hrilRssi->nrRssi.rsrp);
364     NextInt(&lineStr, &hrilRssi->nrRssi.rsrq);
365     NextInt(&lineStr, &hrilRssi->nrRssi.sinr);
366     hrilRssi->nrRssi.rsrp = -hrilRssi->nrRssi.rsrp;
367     hrilRssi->nrRssi.rsrq = -hrilRssi->nrRssi.rsrq;
368 }
369 
ProcessParamSignalStrength(const char * result,HRilRssi * hrilRssi)370 int32_t ProcessParamSignalStrength(const char *result, HRilRssi *hrilRssi)
371 {
372     char *resultStr = (char *)result;
373     char *c = NULL;
374     int32_t tmp = 0;
375     int32_t err = SkipATPrefix(&resultStr);
376     if (err < 0) {
377         TELEPHONY_LOGE("skip failed: [%{public}s]", resultStr);
378         return err;
379     }
380     err = NextInt(&resultStr, &tmp);
381     TELEPHONY_LOGD("ProcessParamSignalStrength  enter -->, result %{public}s", resultStr);
382     if (err < 0) {
383         TELEPHONY_LOGE("read failed: %{public}d", err);
384         return err;
385     }
386     err = NextInt(&resultStr, &tmp);
387     TELEPHONY_LOGD("ProcessParamSignalStrength  enter -->, result %{public}s", resultStr);
388     if (err < 0) {
389         TELEPHONY_LOGE("read failed: %{public}d", err);
390         return err;
391     }
392     err = NextStr(&resultStr, &c);
393     TELEPHONY_LOGD("ProcessParamSignalStrength  enter -->, result %{public}s", resultStr);
394     if (err < 0) {
395         TELEPHONY_LOGE("read failed: %{public}d", err);
396         return err;
397     }
398     if (!strcmp(c, "GSM")) {
399         TELEPHONY_LOGD("ProcessParamSignalStrength  enter GSM-->, result %{public}s", resultStr);
400         ParseGetGsmSignalStrength(resultStr, hrilRssi);
401     } else if (!strcmp(c, "LTE")) {
402         TELEPHONY_LOGD("ProcessParamSignalStrength  enter LTE-->, result %{public}s", resultStr);
403         ParseGetLteSignalStrength(resultStr, hrilRssi);
404     } else if (!strcmp(c, "WCDMA")) {
405         TELEPHONY_LOGD("ProcessParamSignalStrength  enter WCDMA-->, result %{public}s", resultStr);
406         ParseGetWcdmaSignalStrength(resultStr, hrilRssi);
407     } else if (!strcmp(c, "TDSCDMA")) {
408         TELEPHONY_LOGD("ProcessParamSignalStrength  enter TDSCDMA-->, result %{public}s", resultStr);
409         ParseGetTdScdmaSignalStrength(resultStr, hrilRssi);
410     } else if (!strcmp(c, "CDMA")) {
411         TELEPHONY_LOGD("ProcessParamSignalStrength  enter CDMA-->, result %{public}s", resultStr);
412         ParseGetCdmaSignalStrength(resultStr, hrilRssi);
413     } else if (!strcmp(c, "NR")) {
414         TELEPHONY_LOGD("ProcessParamSignalStrength  enter NR-->, result %{public}s", resultStr);
415         ParseGetNrSignalStrength(resultStr, hrilRssi);
416     }
417     return HRIL_ERR_SUCCESS;
418 }
419 
ProcessParamSignalStrengthNotify(const char * result,HRilRssi * hrilRssi)420 int32_t ProcessParamSignalStrengthNotify(const char *result, HRilRssi *hrilRssi)
421 {
422     char *resultStr = (char *)result;
423     char *c = NULL;
424     int32_t err = SkipATPrefix(&resultStr);
425     TELEPHONY_LOGD("ProcessParamSignalStrengthNotify  enter -->, resultStr %{public}s", resultStr);
426     if (err < 0) {
427         TELEPHONY_LOGE("skip failed: %{public}s", resultStr);
428         return err;
429     }
430 
431     err = NextStr(&resultStr, &c);
432     if (err < 0) {
433         TELEPHONY_LOGE("read failed: %{public}d", err);
434         return err;
435     }
436 
437     if (!strcmp(c, "GSM")) {
438         ParseGetGsmSignalStrength(resultStr, hrilRssi);
439     } else if (!strcmp(c, "LTE")) {
440         ParseGetLteSignalStrength(resultStr, hrilRssi);
441     } else if (!strcmp(c, "WCDMA")) {
442         ParseGetWcdmaSignalStrength(resultStr, hrilRssi);
443     } else if (!strcmp(c, "TDSCDMA")) {
444         ParseGetTdScdmaSignalStrength(resultStr, hrilRssi);
445     } else if (!strcmp(c, "CDMA")) {
446         ParseGetCdmaSignalStrength(resultStr, hrilRssi);
447     } else if (!strcmp(c, "NR")) {
448         ParseGetNrSignalStrength(resultStr, hrilRssi);
449     }
450     return HRIL_ERR_SUCCESS;
451 }
452 
ReqGetSignalStrength(const ReqDataInfo * requestInfo)453 void ReqGetSignalStrength(const ReqDataInfo *requestInfo)
454 {
455     if (requestInfo == NULL) {
456         return;
457     }
458     int32_t err = HRIL_ERR_SUCCESS;
459     struct ReportInfo reportInfo;
460     const int32_t REPORT_SIZE = 20;
461     ResponseInfo *responseInfo = NULL;
462     char *result = NULL;
463     TELEPHONY_LOGD("enter to [%{public}s]:%{public}d", __func__, __LINE__);
464     int32_t ret = SendCommandLock("AT^HCSQ?", "^HCSQ:", DEFAULT_TIMEOUT, &responseInfo);
465     if (responseInfo == NULL) {
466         TELEPHONY_LOGE("responseInfo is nullptr!");
467         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_NULL_POINT, HRIL_RESPONSE, 0);
468         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
469         return;
470     }
471 
472     if (ret != 0 || !responseInfo->success) {
473         err = GetResponseErrorCode(responseInfo);
474         TELEPHONY_LOGE("AT^HCSQ send failed");
475     }
476     if (responseInfo->head != NULL) {
477         result = responseInfo->head->data;
478     }
479     if (result == NULL) {
480         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_RESPONSE, HRIL_RESPONSE, 0);
481         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
482         FreeResponseInfo(responseInfo);
483         return;
484     }
485     HRilRssi hrilRssi = {0};
486     if (strlen(result) > REPORT_SIZE) {
487         ret = ProcessParamSignalStrength(result, &hrilRssi);
488     } else {
489         ret = ProcessParamSignalStrengthNotify(result, &hrilRssi);
490     }
491     if (ret != 0) {
492         err = HRIL_ERR_GENERIC_FAILURE;
493         TELEPHONY_LOGE("SignalStrength is null!");
494     }
495 
496     reportInfo = CreateReportInfo(requestInfo, err, HRIL_RESPONSE, 0);
497     OnNetworkReport(requestInfo->slotId, reportInfo, (const uint8_t *)&hrilRssi, sizeof(HRilRssi));
498     FreeResponseInfo(responseInfo);
499 }
500 
ReqGetCsRegStatus(const ReqDataInfo * requestInfo)501 void ReqGetCsRegStatus(const ReqDataInfo *requestInfo)
502 {
503     if (requestInfo == NULL) {
504         return;
505     }
506     int32_t err = HRIL_ERR_SUCCESS;
507     struct ReportInfo reportInfo;
508     ResponseInfo *responseInfo = NULL;
509     char *result = NULL;
510 
511     int32_t ret = SendCommandLock("AT+CREG?", "+CREG:", DEFAULT_TIMEOUT, &responseInfo);
512     if (responseInfo == NULL) {
513         TELEPHONY_LOGE("responseInfo is nullptr!");
514         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_NULL_POINT, HRIL_RESPONSE, 0);
515         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
516         return;
517     }
518     if (ret != 0 || !responseInfo->success) {
519         err = GetResponseErrorCode(responseInfo);
520         TELEPHONY_LOGE("send AT CMD failed!");
521     }
522     if (responseInfo->head != NULL) {
523         result = responseInfo->head->data;
524     }
525     HRilRegStatusInfo regStatusInfo = {0};
526     ret = ProcessRegStatus(result, &regStatusInfo);
527     if (ret != 0) {
528         TELEPHONY_LOGE("ReqGetCsRegStatus CREG format  unexpected: %{public}s", result);
529         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_RESPONSE, HRIL_RESPONSE, 0);
530         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
531         FreeResponseInfo(responseInfo);
532         return;
533     }
534     reportInfo = CreateReportInfo(requestInfo, err, HRIL_RESPONSE, 0);
535     OnNetworkReport(requestInfo->slotId, reportInfo, (const uint8_t *)&regStatusInfo, sizeof(HRilRegStatusInfo));
536     FreeResponseInfo(responseInfo);
537 }
538 
ReqGetPsRegStatus(const ReqDataInfo * requestInfo)539 void ReqGetPsRegStatus(const ReqDataInfo *requestInfo)
540 {
541     if (requestInfo == NULL) {
542         return;
543     }
544     int32_t err = HRIL_ERR_SUCCESS;
545     struct ReportInfo reportInfo;
546     ResponseInfo *responseInfo = NULL;
547     char *result = NULL;
548 
549     int32_t ret = SendCommandLock("AT+CGREG?", "+CGREG:", DEFAULT_TIMEOUT, &responseInfo);
550     if (responseInfo == NULL) {
551         TELEPHONY_LOGE("responseInfo is nullptr!");
552         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_NULL_POINT, HRIL_RESPONSE, 0);
553         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
554         return;
555     }
556     if (ret != 0 || responseInfo->success == 0) {
557         err = GetResponseErrorCode(responseInfo);
558         TELEPHONY_LOGE("send AT CMD failed!");
559     }
560     if (responseInfo->head != NULL) {
561         result = responseInfo->head->data;
562     }
563     HRilRegStatusInfo regStatusInfo = {0};
564     ret = ProcessRegStatus(result, &regStatusInfo);
565     if (ret != 0) {
566         TELEPHONY_LOGE("ReqGetPsRegStatus CGREG format  unexpected: %{public}s", result);
567         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_RESPONSE, HRIL_RESPONSE, 0);
568         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
569         FreeResponseInfo(responseInfo);
570         return;
571     }
572     reportInfo = CreateReportInfo(requestInfo, err, HRIL_RESPONSE, 0);
573     OnNetworkReport(requestInfo->slotId, reportInfo, (const uint8_t *)&regStatusInfo, sizeof(HRilRegStatusInfo));
574     FreeResponseInfo(responseInfo);
575 }
576 
ReqGetOperatorInfo(const ReqDataInfo * requestInfo)577 void ReqGetOperatorInfo(const ReqDataInfo *requestInfo)
578 {
579     if (requestInfo == NULL) {
580         return;
581     }
582     int32_t err = HRIL_ERR_SUCCESS;
583     struct ReportInfo reportInfo;
584     ResponseInfo *responseInfo = NULL;
585     char *result = NULL;
586     char *response[NUM_COUNT] = { "", "", "" };
587 
588     int32_t ret = SendCommandLock(
589         "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?", "+COPS:", DEFAULT_TIMEOUT, &responseInfo);
590     if (responseInfo == NULL) {
591         TELEPHONY_LOGE("responseInfo is nullptr!");
592         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_NULL_POINT, HRIL_RESPONSE, 0);
593         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
594         return;
595     }
596     if (ret != 0 || !responseInfo->success) {
597         err = GetResponseErrorCode(responseInfo);
598         TELEPHONY_LOGE("send AT CMD failed!");
599     }
600     Line *pLine = responseInfo->head;
601     for (int32_t i = 0; pLine != NULL; i++, pLine = pLine->next) {
602         int32_t skip = 0;
603         result = pLine->data;
604         SkipATPrefix(&result);
605         ret = NextInt(&result, &skip);
606         if (ret == -1) {
607             TELEPHONY_LOGE("read failed");
608             break;
609         }
610         ret = NextInt(&result, &skip);
611         if (ret == -1) {
612             TELEPHONY_LOGE("read failed");
613             break;
614         }
615         ret = NextStr(&result, &response[i]);
616         TELEPHONY_LOGD("result[%{public}d]: %{public}s", i, response[i]);
617         if (ret == -1) {
618             response[i] = "";
619             TELEPHONY_LOGE("read failed");
620             break;
621         }
622     }
623     reportInfo = CreateReportInfo(requestInfo, err, HRIL_RESPONSE, 0);
624     OnNetworkReport(requestInfo->slotId, reportInfo, (const uint8_t *)response, NUM_COUNT);
625     FreeResponseInfo(responseInfo);
626 }
627 
MoveLeftBracket(char ** pStr)628 static int32_t MoveLeftBracket(char **pStr)
629 {
630     if (*pStr == NULL) {
631         TELEPHONY_LOGE("pStr is nullptr");
632         return -1;
633     }
634     *pStr = strchr(*pStr, '(');
635     if (*pStr == NULL) {
636         TELEPHONY_LOGE("pStr is nullptr");
637         return -1;
638     }
639     (*pStr)++;
640     return 0;
641 }
642 
GetNetworkSearchInformationPause(void)643 void GetNetworkSearchInformationPause(void)
644 {
645     TELEPHONY_LOGD("enter to [%{public}s]:%{public}d", __func__, __LINE__);
646     pthread_mutex_lock(&g_networkSearchInformationMutex);
647     g_reportInfoForOperListToUse.error = HRIL_ERR_NETWORK_SEARCHING_INTERRUPTED;
648     if (g_reportInfoForOperListToUse.requestInfo != NULL) {
649         OnNetworkReport(g_reportInfoForOperListToUse.requestInfo->slotId, g_reportInfoForOperListToUse, NULL, 0);
650     }
651     SetAtPauseFlag(false);
652     if (g_reportInfoForOperListToUse.requestInfo != NULL) {
653         g_reportInfoForOperListToUse.requestInfo = NULL;
654     }
655     pthread_mutex_unlock(&g_networkSearchInformationMutex);
656 }
657 
PerformTimeOut(int32_t sigFlag)658 void PerformTimeOut(int32_t sigFlag)
659 {
660     if (SIGALRM == sigFlag) {
661         bool sendFlag = GetAtPauseFlag();
662         TELEPHONY_LOGD("enter to [%{public}s]:%{public}d", __func__, __LINE__);
663         if (sendFlag) {
664             g_reportInfoForOperListToUse.error = HRIL_ERR_NETWORK_SEARCH_TIMEOUT;
665             if (g_reportInfoForOperListToUse.requestInfo != NULL) {
666                 OnNetworkReport(g_reportInfoForOperListToUse.requestInfo->slotId,
667                     g_reportInfoForOperListToUse, NULL, 0);
668             }
669             SetAtPauseFlag(false);
670             if (g_reportInfoForOperListToUse.requestInfo != NULL) {
671                 g_reportInfoForOperListToUse.requestInfo = NULL;
672             }
673             TELEPHONY_LOGI("ReqGetNetworkSearchInformation response timeout!");
674         }
675     }
676     return;
677 }
678 
ReqGetNetworkSearchInformation(const ReqDataInfo * requestInfo)679 void ReqGetNetworkSearchInformation(const ReqDataInfo *requestInfo)
680 {
681     if (requestInfo == NULL) {
682         return;
683     }
684     ResponseInfo *responseInfo = NULL;
685     const int32_t SECOND = 120;
686     TELEPHONY_LOGD("enter to [%{public}s]:%{public}d", __func__, __LINE__);
687     pthread_mutex_lock(&g_networkSearchInformationMutex);
688     alarm(0);
689     if (g_reportInfoForOperListToUse.requestInfo != NULL) {
690         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_NETWORK_SEARCHING, HRIL_RESPONSE, 0);
691         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
692         pthread_mutex_unlock(&g_networkSearchInformationMutex);
693         TELEPHONY_LOGE("ReqGetNetworkSearchInformation repeat Send!");
694         return;
695     }
696     if (signal(SIGALRM, PerformTimeOut) == SIG_ERR) {
697         TELEPHONY_LOGE("ReqGetNetworkSearchInformation signal PerformTimeOut is SIG_ERR!");
698         pthread_mutex_unlock(&g_networkSearchInformationMutex);
699         return;
700     }
701     int32_t ret = SendCommandNetWorksLock("AT+COPS=?", "+COPS:", DEFAULT_TIMEOUT, &responseInfo);
702 
703     g_reportInfoForOperListToUse = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
704     if ((ret != 0 && ret != AT_ERR_WAITING) || (responseInfo != NULL && !responseInfo->success)) {
705         TELEPHONY_LOGE("send AT CMD failed!");
706         SetAtPauseFlag(false);
707         g_reportInfoForOperListToUse.error = GetResponseErrorCode(responseInfo);
708         OnNetworkReport(requestInfo->slotId, g_reportInfoForOperListToUse, NULL, 1);
709         if (responseInfo != NULL) {
710             FreeResponseInfo(responseInfo);
711         }
712         if (g_reportInfoForOperListToUse.requestInfo != NULL) {
713             g_reportInfoForOperListToUse.requestInfo = NULL;
714         }
715         pthread_mutex_unlock(&g_networkSearchInformationMutex);
716         return;
717     }
718     alarm(SECOND);
719     if (responseInfo != NULL) {
720         FreeResponseInfo(responseInfo);
721     }
722     pthread_mutex_unlock(&g_networkSearchInformationMutex);
723     SetWatchFunction(GetNetworkSearchInformationPause);
724 }
725 
ParseOperInfoItem(char ** lineInfo,char ** const longNameInfo,char ** const shortNameInfo,char ** const numericInfo)726 static int32_t ParseOperInfoItem(
727     char **lineInfo, char **const longNameInfo, char **const shortNameInfo, char **const numericInfo)
728 {
729     if (lineInfo == NULL || longNameInfo == NULL || shortNameInfo == NULL || numericInfo == NULL) {
730         return -1;
731     }
732     char *data = *lineInfo;
733     char **longNameInfoStr = (char **)longNameInfo;
734     char **shortNameInfoStr = (char **)shortNameInfo;
735     char **numericInfoStr = (char **)numericInfo;
736     int32_t ret = NextStr(&data, longNameInfoStr);
737     if (ret < 0) {
738         return -1;
739     }
740     TELEPHONY_LOGD("ok , longName:%{public}s", *longNameInfoStr);
741 
742     ret = NextStr(&data, shortNameInfoStr);
743     if (ret < 0) {
744         return -1;
745     }
746     TELEPHONY_LOGD("ok , shortName:%{public}s", *shortNameInfoStr);
747 
748     ret = NextStr(&data, numericInfoStr);
749     if (ret < 0) {
750         return -1;
751     }
752     TELEPHONY_LOGD("ok , numeric:%{public}s", *numericInfoStr);
753     *lineInfo = data;
754     return 0;
755 }
756 
ParseOperListInfo(const char * lineInfo,int32_t count,AvailableOperInfo * pOperInfo,AvailableOperInfo ** ppOperInfo)757 int32_t ParseOperListInfo(
758     const char *lineInfo, int32_t count, AvailableOperInfo *pOperInfo, AvailableOperInfo **ppOperInfo)
759 {
760     if (lineInfo == NULL || pOperInfo == NULL) {
761         return 0;
762     }
763     int32_t state = 0;
764     int32_t rat = 0;
765     int32_t operCount = 0;
766     char *line = (char *)lineInfo;
767     int32_t item = count;
768     for (int32_t i = 0; i < item && operCount < item; i++) {
769         int32_t ret = MoveLeftBracket(&line);
770         if (ret < 0) {
771             break;
772         }
773         ret = NextInt(&line, &state);
774         if (ret < 0) {
775             break;
776         }
777         char *longName = NULL;
778         char *shortName = NULL;
779         char *numeric = NULL;
780         ret = ParseOperInfoItem(&line, &longName, &shortName, &numeric);
781         if (ret < 0) {
782             break;
783         }
784         ret = NextIntByRightBracket(&line, &rat);
785         if (ret < 0) {
786             break;
787         }
788         if (state > 0) {
789             pOperInfo[operCount].status = state;
790             TELEPHONY_LOGD("pOperInfo , status:%{public}d", pOperInfo[i].status);
791             pOperInfo[operCount].longName = longName;
792             TELEPHONY_LOGD("pOperInfo , longName:%{public}s", longName);
793             pOperInfo[operCount].numeric = numeric;
794             TELEPHONY_LOGD("pOperInfo , numeric:%{public}s", numeric);
795             pOperInfo[operCount].shortName = shortName;
796             TELEPHONY_LOGD("pOperInfo , shortName:%{public}s", shortName);
797             pOperInfo[operCount].rat = rat;
798             TELEPHONY_LOGD("pOperInfo , rat:%{public}d", rat);
799             operCount++;
800         }
801     }
802     return operCount;
803 }
804 
ParseListItemNum(const char * list)805 int32_t ParseListItemNum(const char *list)
806 {
807     int32_t item = 0;
808     if (list == NULL) {
809         TELEPHONY_LOGI("ProcessOperListToUse result is null");
810     } else {
811         char *line = (char *)list;
812         while (*line != '\0') {
813             if (*line == ')') {
814                 item++;
815             }
816             line++;
817         }
818     }
819     return item;
820 }
821 
DealNetworkSearchInformation(int32_t operCount,AvailableOperInfo ** ppOperInfo,AvailableOperInfo * pOperInfo)822 static void DealNetworkSearchInformation(
823     int32_t operCount, AvailableOperInfo **ppOperInfo, AvailableOperInfo *pOperInfo)
824 {
825     if (operCount == 0) {
826         pthread_mutex_lock(&g_networkSearchInformationMutex);
827         SetAtPauseFlag(false);
828         alarm(0);
829         if (g_reportInfoForOperListToUse.requestInfo != NULL) {
830             g_reportInfoForOperListToUse.error = HRIL_ERR_INVALID_RESPONSE;
831             OnNetworkReport(g_reportInfoForOperListToUse.requestInfo->slotId, g_reportInfoForOperListToUse, NULL, 0);
832             g_reportInfoForOperListToUse.requestInfo = NULL;
833         }
834         pthread_mutex_unlock(&g_networkSearchInformationMutex);
835     } else {
836         pthread_mutex_lock(&g_networkSearchInformationMutex);
837         SetAtPauseFlag(false);
838         alarm(0);
839         if (g_reportInfoForOperListToUse.requestInfo != NULL) {
840             OnNetworkReport(g_reportInfoForOperListToUse.requestInfo->slotId, g_reportInfoForOperListToUse,
841                 (const uint8_t *)ppOperInfo, operCount);
842             g_reportInfoForOperListToUse.requestInfo = NULL;
843         }
844         pthread_mutex_unlock(&g_networkSearchInformationMutex);
845     }
846     if (ppOperInfo != NULL) {
847         free(ppOperInfo);
848     }
849     if (pOperInfo != NULL) {
850         free(pOperInfo);
851     }
852 }
853 
ErrorHandling(void)854 static int32_t ErrorHandling(void)
855 {
856     TELEPHONY_LOGE("This command resolution is not supported.");
857     return HRIL_ERR_GENERIC_FAILURE;
858 }
859 
ParseCellInfoGsm(const char * str,CellInfo * ci)860 static int32_t ParseCellInfoGsm(const char *str, CellInfo *ci)
861 {
862     if (str == NULL || ci == NULL) {
863         return -1;
864     }
865     char *pStr = (char *)str;
866     char *pat = NULL;
867     (void)memset_s(ci, sizeof(CellInfo), 0, sizeof(CellInfo));
868     TELEPHONY_LOGD("ParseCellInfoGsm %{public}s", pStr);
869     if (SkipATPrefix(&pStr) < 0) {
870         return ErrorHandling();
871     }
872     if (NextStr(&pStr, &pat) < 0) {
873         return ErrorHandling();
874     }
875     if (NextInt(&pStr, &ci->ServiceCellParas.gsm.band) < 0) {
876         return ErrorHandling();
877     }
878     if (NextInt(&pStr, &ci->ServiceCellParas.gsm.arfcn) < 0) {
879         return ErrorHandling();
880     }
881     if (NextInt(&pStr, &ci->ServiceCellParas.gsm.bsic) < 0) {
882         return ErrorHandling();
883     }
884     if (NextIntFromHex(&pStr, &ci->ServiceCellParas.gsm.cellId) < 0) {
885         return ErrorHandling();
886     }
887     if (NextIntFromHex(&pStr, &ci->ServiceCellParas.gsm.lac) < 0) {
888         return ErrorHandling();
889     }
890     if (NextInt(&pStr, &ci->ServiceCellParas.gsm.rxlev) < 0) {
891         return ErrorHandling();
892     }
893     ci->ratType = NETWORK_TYPE_GSM;
894     return HRIL_ERR_SUCCESS;
895 }
896 
ParseCellInfoLte(const char * str,CellInfo * ci)897 static int32_t ParseCellInfoLte(const char *str, CellInfo *ci)
898 {
899     if (str == NULL || ci == NULL) {
900         return -1;
901     }
902     char *pStr = (char *)str;
903     char *pat = NULL;
904     (void)memset_s(ci, sizeof(CellInfo), 0, sizeof(CellInfo));
905     TELEPHONY_LOGD("ParseCellInfoLte %{public}s", pStr);
906     if (SkipATPrefix(&pStr) < 0) {
907         return ErrorHandling();
908     }
909     if (NextStr(&pStr, &pat) < 0) {
910         return ErrorHandling();
911     }
912     if (NextInt(&pStr, &ci->ServiceCellParas.lte.arfcn) < 0) {
913         return ErrorHandling();
914     }
915     if (NextIntFromHex(&pStr, &ci->ServiceCellParas.lte.pci) < 0) {
916         return ErrorHandling();
917     }
918     if (NextInt(&pStr, &ci->ServiceCellParas.lte.rsrp) < 0) {
919         return ErrorHandling();
920     }
921     if (NextInt(&pStr, &ci->ServiceCellParas.lte.rsrq) < 0) {
922         return ErrorHandling();
923     }
924     if (NextInt(&pStr, &ci->ServiceCellParas.lte.rxlev) < 0) {
925         return ErrorHandling();
926     }
927     ci->ratType = NETWORK_TYPE_LTE;
928     return HRIL_ERR_SUCCESS;
929 }
930 
ParseCellInfoWcdma(const char * str,CellInfo * ci)931 static int32_t ParseCellInfoWcdma(const char *str, CellInfo *ci)
932 {
933     if (str == NULL || ci == NULL) {
934         return -1;
935     }
936     char *pStr = (char *)str;
937     char *pat = NULL;
938     (void)memset_s(ci, sizeof(CellInfo), 0, sizeof(CellInfo));
939     TELEPHONY_LOGD("ParseCellInfoWcdma %{public}s", pStr);
940     if (SkipATPrefix(&pStr) < 0) {
941         return ErrorHandling();
942     }
943     if (NextStr(&pStr, &pat) < 0) {
944         return ErrorHandling();
945     }
946     if (NextInt(&pStr, &ci->ServiceCellParas.wcdma.arfcn) < 0) {
947         return ErrorHandling();
948     }
949     if (NextInt(&pStr, &ci->ServiceCellParas.wcdma.psc) < 0) {
950         return ErrorHandling();
951     }
952     if (NextInt(&pStr, &ci->ServiceCellParas.wcdma.rscp) < 0) {
953         return ErrorHandling();
954     }
955     if (NextInt(&pStr, &ci->ServiceCellParas.wcdma.ecno) < 0) {
956         return ErrorHandling();
957     }
958     ci->ratType = NETWORK_TYPE_WCDMA;
959     return HRIL_ERR_SUCCESS;
960 }
961 
ParseCellInfoCdma(const char * str,CellInfo * ci)962 static int32_t ParseCellInfoCdma(const char *str, CellInfo *ci)
963 {
964     if (str == NULL || ci == NULL) {
965         return -1;
966     }
967     char *pStr = (char *)str;
968     char *pat = NULL;
969     (void)memset_s(ci, sizeof(CellInfo), 0, sizeof(CellInfo));
970     TELEPHONY_LOGD("ParseCellInfoCdma %{public}s", pStr);
971     if (SkipATPrefix(&pStr) < 0) {
972         return ErrorHandling();
973     }
974     if (NextStr(&pStr, &pat) < 0) {
975         return ErrorHandling();
976     }
977     if (NextInt(&pStr, &ci->ServiceCellParas.cdma.systemId) < 0) {
978         return ErrorHandling();
979     }
980     if (NextInt(&pStr, &ci->ServiceCellParas.cdma.networkId) < 0) {
981         return ErrorHandling();
982     }
983     if (NextInt(&pStr, &ci->ServiceCellParas.cdma.baseId) < 0) {
984         return ErrorHandling();
985     }
986     if (NextInt(&pStr, &ci->ServiceCellParas.cdma.zoneId) < 0) {
987         return ErrorHandling();
988     }
989     if (NextInt(&pStr, &ci->ServiceCellParas.cdma.pilotPn) < 0) {
990         return ErrorHandling();
991     }
992     if (NextInt(&pStr, &ci->ServiceCellParas.cdma.pilotStrength) < 0) {
993         return ErrorHandling();
994     }
995     if (NextInt(&pStr, &ci->ServiceCellParas.cdma.channel) < 0) {
996         return ErrorHandling();
997     }
998     if (NextInt(&pStr, &ci->ServiceCellParas.cdma.longitude) < 0) {
999         return ErrorHandling();
1000     }
1001     if (NextInt(&pStr, &ci->ServiceCellParas.cdma.latitude) < 0) {
1002         return ErrorHandling();
1003     }
1004     ci->ratType = NETWORK_TYPE_CDMA;
1005     return HRIL_ERR_SUCCESS;
1006 }
1007 
ParseCellInfoTdscdma(const char * str,CellInfo * ci)1008 static int32_t ParseCellInfoTdscdma(const char *str, CellInfo *ci)
1009 {
1010     if (str == NULL || ci == NULL) {
1011         return -1;
1012     }
1013     char *pStr = (char *)str;
1014     char *pat = NULL;
1015     (void)memset_s(ci, sizeof(CellInfo), 0, sizeof(CellInfo));
1016     TELEPHONY_LOGD("ParseCellInfoTdscdma %{public}s", pStr);
1017     if (SkipATPrefix(&pStr) < 0) {
1018         return ErrorHandling();
1019     }
1020     if (NextStr(&pStr, &pat) < 0) {
1021         return ErrorHandling();
1022     }
1023     if (NextInt(&pStr, &ci->ServiceCellParas.tdscdma.arfcn) < 0) {
1024         return ErrorHandling();
1025     }
1026     if (NextInt(&pStr, &ci->ServiceCellParas.tdscdma.syncId) < 0) {
1027         return ErrorHandling();
1028     }
1029     if (NextInt(&pStr, &ci->ServiceCellParas.tdscdma.sc) < 0) {
1030         return ErrorHandling();
1031     }
1032     if (NextInt(&pStr, &ci->ServiceCellParas.tdscdma.cellId) < 0) {
1033         return ErrorHandling();
1034     }
1035     if (NextInt(&pStr, &ci->ServiceCellParas.tdscdma.lac) < 0) {
1036         return ErrorHandling();
1037     }
1038     if (NextInt(&pStr, &ci->ServiceCellParas.tdscdma.rscp) < 0) {
1039         return ErrorHandling();
1040     }
1041     if (NextInt(&pStr, &ci->ServiceCellParas.tdscdma.drx) < 0) {
1042         return ErrorHandling();
1043     }
1044     if (NextInt(&pStr, &ci->ServiceCellParas.tdscdma.rac) < 0) {
1045         return ErrorHandling();
1046     }
1047     if (NextInt(&pStr, &ci->ServiceCellParas.tdscdma.cpid) < 0) {
1048         return ErrorHandling();
1049     }
1050     ci->ratType = NETWORK_TYPE_TDSCDMA;
1051     return HRIL_ERR_SUCCESS;
1052 }
1053 
ParseCellInfoNr(const char * str,CellInfo * ci)1054 static int32_t ParseCellInfoNr(const char *str, CellInfo *ci)
1055 {
1056     if (str == NULL || ci == NULL) {
1057         return -1;
1058     }
1059     char *pStr = (char *)str;
1060     char *pat = NULL;
1061     (void)memset_s(ci, sizeof(CellInfo), 0, sizeof(CellInfo));
1062     TELEPHONY_LOGD("ParseCellInfoNr %{public}s", pStr);
1063     if (SkipATPrefix(&pStr) < 0) {
1064         return ErrorHandling();
1065     }
1066     if (NextStr(&pStr, &pat) < 0) {
1067         return ErrorHandling();
1068     }
1069     if (NextInt(&pStr, &ci->ServiceCellParas.nr.nrArfcn) < 0) {
1070         return ErrorHandling();
1071     }
1072     if (NextInt(&pStr, &ci->ServiceCellParas.nr.pci) < 0) {
1073         return ErrorHandling();
1074     }
1075     if (NextInt(&pStr, &ci->ServiceCellParas.nr.tac) < 0) {
1076         return ErrorHandling();
1077     }
1078     if (NextInt64(&pStr, &ci->ServiceCellParas.nr.nci) < 0) {
1079         return ErrorHandling();
1080     }
1081     ci->ratType = NETWORK_TYPE_NR;
1082     return HRIL_ERR_SUCCESS;
1083 }
1084 
ParseCellInfos(const char * str,const CellInfo * cellInfo)1085 static int32_t ParseCellInfos(const char *str, const CellInfo *cellInfo)
1086 {
1087     CellInfo *ci = (CellInfo *)cellInfo;
1088     char *pStr = (char *)str;
1089     if (pStr == NULL) {
1090         TELEPHONY_LOGE("pStr is null.");
1091         return HRIL_ERR_GENERIC_FAILURE;
1092     }
1093     TELEPHONY_LOGD("ParseCellInfos %{public}s", pStr);
1094     if (ReportStrWith(pStr, "^MONNC: GSM")) {
1095         if (ParseCellInfoGsm(pStr, ci) != 0) {
1096             TELEPHONY_LOGE("parse gsm command failed!");
1097             return HRIL_ERR_GENERIC_FAILURE;
1098         }
1099     } else if (ReportStrWith(pStr, "^MONNC: WCDMA")) {
1100         if (ParseCellInfoWcdma(pStr, ci) != 0) {
1101             TELEPHONY_LOGE("parse wcdma command failed!");
1102             return HRIL_ERR_GENERIC_FAILURE;
1103         }
1104     } else if (ReportStrWith(pStr, "^MONNC: CDMA")) {
1105         if (ParseCellInfoCdma(pStr, ci) != 0) {
1106             TELEPHONY_LOGE("parse cdma command failed!");
1107             return HRIL_ERR_GENERIC_FAILURE;
1108         }
1109     } else if (ReportStrWith(pStr, "^MONNC: TDSCDMA")) {
1110         if (ParseCellInfoTdscdma(pStr, ci) != 0) {
1111             TELEPHONY_LOGE("parse tdscdma command failed!");
1112             return HRIL_ERR_GENERIC_FAILURE;
1113         }
1114     } else if (ReportStrWith(pStr, "^MONNC: LTE")) {
1115         if (ParseCellInfoLte(pStr, ci) != 0) {
1116             TELEPHONY_LOGE("parse lte command failed!");
1117             return HRIL_ERR_GENERIC_FAILURE;
1118         }
1119     } else if (ReportStrWith(pStr, "^MONNC: NR")) {
1120         if (ParseCellInfoNr(pStr, ci) != 0) {
1121             TELEPHONY_LOGE("parse nr command failed!");
1122             return HRIL_ERR_GENERIC_FAILURE;
1123         }
1124     } else if (ReportStrWith(pStr, "^MONNC: NONE")) {
1125         (void)memset_s(ci, sizeof(CellInfo), 0, sizeof(CellInfo));
1126         TELEPHONY_LOGD("ParseCellInfos ^MONNC: NONE branch");
1127     } else {
1128         TELEPHONY_LOGE("%{public}s This command resolution not supported.", pStr);
1129         return HRIL_ERR_GENERIC_FAILURE;
1130     }
1131     return HRIL_ERR_SUCCESS;
1132 }
1133 
ReqGetNeighboringCellInfoList(const ReqDataInfo * requestInfo)1134 void ReqGetNeighboringCellInfoList(const ReqDataInfo *requestInfo)
1135 {
1136     if (requestInfo == NULL) {
1137         return;
1138     }
1139     int32_t err = HRIL_ERR_SUCCESS;
1140     int32_t countCellInfo = 0;
1141     int32_t index = 0;
1142     ResponseInfo *responseInfo = NULL;
1143     Line *pLine = NULL;
1144     CellInfo *cellInfo = NULL;
1145     CellInfoList cellInfoList = { 0, NULL };
1146     int32_t ret = SendCommandLock("AT^MONNC", "^MONNC:", DEFAULT_TIMEOUT, &responseInfo);
1147     struct ResponseAck respDataAck = { responseInfo, (uint8_t *)&cellInfoList, sizeof(CellInfoList) };
1148     if (responseInfo == NULL) {
1149         TELEPHONY_LOGE("responseInfo is nullptr");
1150         ResponseNetworkReport(HRIL_SIM_SLOT_0, requestInfo, HRIL_ERR_INVALID_RESPONSE, &respDataAck);
1151         return;
1152     }
1153     if (ret != 0 || responseInfo->success == 0) {
1154         TELEPHONY_LOGE("send AT CMD failed!");
1155         err = (ret != 0) ? HRIL_ERR_GENERIC_FAILURE : HRIL_ERR_CMD_SEND_FAILURE;
1156         ResponseNetworkReport(requestInfo->slotId, requestInfo, err, &respDataAck);
1157         return;
1158     }
1159     for (countCellInfo = 0, pLine = responseInfo->head; pLine != NULL; pLine = pLine->next) {
1160         countCellInfo++;
1161     }
1162     if (countCellInfo == 0) {
1163         ResponseNetworkReport(requestInfo->slotId, requestInfo, HRIL_ERR_INVALID_RESPONSE, &respDataAck);
1164         return;
1165     }
1166     TELEPHONY_LOGD("countCellInfo:%{public}d", countCellInfo);
1167     cellInfo = (CellInfo *)calloc(countCellInfo, sizeof(CellInfo));
1168     if (cellInfo == NULL) {
1169         TELEPHONY_LOGE("cellInfoList alloc failed!");
1170         ResponseNetworkReport(requestInfo->slotId, requestInfo, HRIL_ERR_MEMORY_FULL, &respDataAck);
1171         return;
1172     }
1173     for (pLine = responseInfo->head; pLine != NULL; pLine = pLine->next) {
1174         if (ParseCellInfos(pLine->data, &cellInfo[index]) != 0) {
1175             continue;
1176         }
1177         index++;
1178     }
1179     cellInfoList.itemNum = index;
1180     cellInfoList.cellNearbyInfo = cellInfo;
1181     ResponseNetworkReport(requestInfo->slotId, requestInfo, err, &respDataAck);
1182     free(cellInfo);
1183     return;
1184 }
1185 
ParseGetGsmCellInfoLine(char * line,CurrentCellInfoVendor * response)1186 static void ParseGetGsmCellInfoLine(char *line, CurrentCellInfoVendor *response)
1187 {
1188     if (line == NULL || response == NULL) {
1189         return;
1190     }
1191     NextInt(&line, &response->ServiceCellParas.gsm.band);
1192     NextInt(&line, &response->ServiceCellParas.gsm.arfcn);
1193     NextInt(&line, &response->ServiceCellParas.gsm.bsic);
1194     NextIntFromHex(&line, &response->ServiceCellParas.gsm.cellId);
1195     NextIntFromHex(&line, &response->ServiceCellParas.gsm.lac);
1196     NextInt(&line, &response->ServiceCellParas.gsm.rxlev);
1197     NextInt(&line, &response->ServiceCellParas.gsm.rxQuality);
1198     NextInt(&line, &response->ServiceCellParas.gsm.ta);
1199     response->ServiceCellParas.gsm.rxlev = -response->ServiceCellParas.gsm.rxlev;
1200     TELEPHONY_LOGD("ParseGetGsmCellInfoLine band:%{private}d,arfcn:%{private}d,bsic:%{private}d,cellId:%{private}d"
1201                    "lac:%{private}d,rxlev:%{private}d,rxQuality:%{private}d,ta:%{private}d",
1202         response->ServiceCellParas.gsm.band, response->ServiceCellParas.gsm.arfcn, response->ServiceCellParas.gsm.bsic,
1203         response->ServiceCellParas.gsm.cellId, response->ServiceCellParas.gsm.lac, response->ServiceCellParas.gsm.rxlev,
1204         response->ServiceCellParas.gsm.rxQuality, response->ServiceCellParas.gsm.ta);
1205 }
1206 
ParseGetLteCellInfoLine(char * line,CurrentCellInfoVendor * response)1207 static void ParseGetLteCellInfoLine(char *line, CurrentCellInfoVendor *response)
1208 {
1209     if (line == NULL || response == NULL) {
1210         return;
1211     }
1212     NextInt(&line, &response->ServiceCellParas.lte.arfcn);
1213     NextIntFromHex(&line, &response->ServiceCellParas.lte.cellId);
1214     NextIntFromHex(&line, &response->ServiceCellParas.lte.pci);
1215     NextIntFromHex(&line, &response->ServiceCellParas.lte.tac);
1216     NextInt(&line, &response->ServiceCellParas.lte.rsrp);
1217     NextInt(&line, &response->ServiceCellParas.lte.rsrq);
1218     NextInt(&line, &response->ServiceCellParas.lte.rssi);
1219     response->ServiceCellParas.lte.rsrp = -response->ServiceCellParas.lte.rsrp;
1220     TELEPHONY_LOGD("ParseGetLteCellInfoLine arfcn:%{private}d,cellId:%{private}d,pci:%{private}d"
1221                    "tac:%{private}d,rsrp:%{private}d,rsrq:%{private}d,rssi:%{private}d",
1222         response->ServiceCellParas.lte.arfcn, response->ServiceCellParas.lte.cellId, response->ServiceCellParas.lte.pci,
1223         response->ServiceCellParas.lte.tac, response->ServiceCellParas.lte.rsrp, response->ServiceCellParas.lte.rsrq,
1224         response->ServiceCellParas.lte.rssi);
1225 }
1226 
ParseGetWcdmaCellInfoLine(char * line,CurrentCellInfoVendor * response)1227 static void ParseGetWcdmaCellInfoLine(char *line, CurrentCellInfoVendor *response)
1228 {
1229     if (line == NULL || response == NULL) {
1230         return;
1231     }
1232     NextInt(&line, &response->ServiceCellParas.wcdma.arfcn);
1233     NextInt(&line, &response->ServiceCellParas.wcdma.psc);
1234     NextIntFromHex(&line, &response->ServiceCellParas.wcdma.cellId);
1235     NextIntFromHex(&line, &response->ServiceCellParas.wcdma.lac);
1236     NextInt(&line, &response->ServiceCellParas.wcdma.rscp);
1237     NextInt(&line, &response->ServiceCellParas.wcdma.rxlev);
1238     NextInt(&line, &response->ServiceCellParas.wcdma.ecno);
1239     NextInt(&line, &response->ServiceCellParas.wcdma.drx);
1240     NextInt(&line, &response->ServiceCellParas.wcdma.ura);
1241     response->ServiceCellParas.wcdma.rscp = -response->ServiceCellParas.wcdma.rscp;
1242     TELEPHONY_LOGD("ParseGetWcdmaCellInfoLine arfcn:%{private}d,psc:%{private}d,cellId:%{private}d,lac:%{private}d"
1243                    "rscp:%{private}d,rxlev:%{private}d,ecno:%{private}d,drx:%{private}d,ura:%{private}d",
1244         response->ServiceCellParas.wcdma.arfcn, response->ServiceCellParas.wcdma.psc,
1245         response->ServiceCellParas.wcdma.cellId, response->ServiceCellParas.wcdma.lac,
1246         response->ServiceCellParas.wcdma.rscp, response->ServiceCellParas.wcdma.rxlev,
1247         response->ServiceCellParas.wcdma.ecno, response->ServiceCellParas.wcdma.drx,
1248         response->ServiceCellParas.wcdma.ura);
1249 }
1250 
ParseGetCdmaCellInfoLine(char * line,CurrentCellInfoVendor * response)1251 static void ParseGetCdmaCellInfoLine(char *line, CurrentCellInfoVendor *response)
1252 {
1253     if (line == NULL || response == NULL) {
1254         return;
1255     }
1256     NextInt(&line, &response->ServiceCellParas.cdma.systemId);
1257     NextInt(&line, &response->ServiceCellParas.cdma.networkId);
1258     NextIntFromHex(&line, &response->ServiceCellParas.cdma.baseId);
1259     NextIntFromHex(&line, &response->ServiceCellParas.cdma.zoneId);
1260     NextInt(&line, &response->ServiceCellParas.cdma.pilotPn);
1261     NextInt(&line, &response->ServiceCellParas.cdma.pilotStrength);
1262     NextInt(&line, &response->ServiceCellParas.cdma.channel);
1263     NextInt(&line, &response->ServiceCellParas.cdma.longitude);
1264     NextInt(&line, &response->ServiceCellParas.cdma.latitude);
1265     response->ServiceCellParas.cdma.pilotStrength = -response->ServiceCellParas.cdma.pilotStrength;
1266     TELEPHONY_LOGD(
1267         "ParseGetCdmaCellInfoLine systemId:%{private}d,networkId:%{private}d,baseId:%{private}d,zoneId:%{private}d"
1268         "pilotPn:%{private}d,pilotStrength:%{private}d,channel:%{private}d,longitude:%{private}d,latitude:%{private}d",
1269         response->ServiceCellParas.cdma.systemId, response->ServiceCellParas.cdma.networkId,
1270         response->ServiceCellParas.cdma.baseId, response->ServiceCellParas.cdma.zoneId,
1271         response->ServiceCellParas.cdma.pilotPn, response->ServiceCellParas.cdma.pilotStrength,
1272         response->ServiceCellParas.cdma.channel, response->ServiceCellParas.cdma.longitude,
1273         response->ServiceCellParas.cdma.latitude);
1274 }
1275 
ParseGetTdscdmaCellInfoLine(char * line,CurrentCellInfoVendor * response)1276 static void ParseGetTdscdmaCellInfoLine(char *line, CurrentCellInfoVendor *response)
1277 {
1278     if (line == NULL || response == NULL) {
1279         return;
1280     }
1281     NextInt(&line, &response->ServiceCellParas.tdscdma.arfcn);
1282     NextInt(&line, &response->ServiceCellParas.tdscdma.syncId);
1283     NextInt(&line, &response->ServiceCellParas.tdscdma.sc);
1284     NextIntFromHex(&line, &response->ServiceCellParas.tdscdma.cellId);
1285     NextIntFromHex(&line, &response->ServiceCellParas.tdscdma.lac);
1286     NextInt(&line, &response->ServiceCellParas.tdscdma.rscp);
1287     NextInt(&line, &response->ServiceCellParas.tdscdma.drx);
1288     NextIntFromHex(&line, &response->ServiceCellParas.tdscdma.rac);
1289     NextInt(&line, &response->ServiceCellParas.tdscdma.cpid);
1290     response->ServiceCellParas.tdscdma.rscp = -response->ServiceCellParas.tdscdma.rscp;
1291     TELEPHONY_LOGD("ParseGetTdscdmaCellInfoLine arfcn:%{private}d,syncId:%{private}d,sc:%{private}d,cellId:%{private}d,"
1292                    "lac:%{private}d,rscp:%{private}d,drx:%{private}d,rac:%{private}d,cpid:%{private}d,",
1293         response->ServiceCellParas.tdscdma.arfcn, response->ServiceCellParas.tdscdma.syncId,
1294         response->ServiceCellParas.tdscdma.sc, response->ServiceCellParas.tdscdma.cellId,
1295         response->ServiceCellParas.tdscdma.lac, response->ServiceCellParas.tdscdma.rscp,
1296         response->ServiceCellParas.tdscdma.drx, response->ServiceCellParas.tdscdma.rac,
1297         response->ServiceCellParas.tdscdma.cpid);
1298 }
1299 
ParseGetNrCellInfoLine(char * line,CurrentCellInfoVendor * response)1300 static void ParseGetNrCellInfoLine(char *line, CurrentCellInfoVendor *response)
1301 {
1302     if (line == NULL || response == NULL) {
1303         return;
1304     }
1305     NextInt(&line, &response->ServiceCellParas.nr.nrArfcn);
1306     NextInt(&line, &response->ServiceCellParas.nr.pci);
1307     NextInt(&line, &response->ServiceCellParas.nr.tac);
1308     NextInt64(&line, &response->ServiceCellParas.nr.nci);
1309 }
1310 
ParseGetCurrentCellInfoResponseLineSwitch(const char * str,const CurrentCellInfoVendor * hrilResponse)1311 static int32_t ParseGetCurrentCellInfoResponseLineSwitch(const char *str, const CurrentCellInfoVendor *hrilResponse)
1312 {
1313     char *line = (char *)str;
1314     CurrentCellInfoVendor *response = (CurrentCellInfoVendor *)hrilResponse;
1315     if ((line == NULL) || (response == NULL)) {
1316         TELEPHONY_LOGE("ParseGetCurrentCellInfoResponseLineSwitch line or response param is null");
1317         return HRIL_ERR_NULL_POINT;
1318     }
1319     switch (response->ratType) {
1320         case NETWORK_TYPE_GSM:
1321             ParseGetGsmCellInfoLine(line, response);
1322             break;
1323         case NETWORK_TYPE_LTE:
1324             ParseGetLteCellInfoLine(line, response);
1325             break;
1326         case NETWORK_TYPE_WCDMA:
1327             ParseGetWcdmaCellInfoLine(line, response);
1328             break;
1329         case NETWORK_TYPE_CDMA:
1330             ParseGetCdmaCellInfoLine(line, response);
1331             break;
1332         case NETWORK_TYPE_TDSCDMA:
1333             ParseGetTdscdmaCellInfoLine(line, response);
1334             break;
1335         case NETWORK_TYPE_NR:
1336             ParseGetNrCellInfoLine(line, response);
1337             break;
1338         default:
1339             TELEPHONY_LOGE("ParseGetCellInfoResponseLineSwitch is not support cell, line = %{public}s", line);
1340             break;
1341     }
1342     return HRIL_ERR_SUCCESS;
1343 }
1344 
ParseGetCurrentCellInfoResponseLine(char * line,CurrentCellInfoVendor * response)1345 static int32_t ParseGetCurrentCellInfoResponseLine(char *line, CurrentCellInfoVendor *response)
1346 {
1347     if (line == NULL || response == NULL) {
1348         return -1;
1349     }
1350     char *c = NULL;
1351     TELEPHONY_LOGD("ParseGetCurrentCellInfoResponseLine  line %{public}s", line);
1352     int32_t err = SkipATPrefix(&line);
1353     if (err < 0) {
1354         return err;
1355     }
1356     err = NextStr(&line, &c);
1357     if (err < 0) {
1358         return err;
1359     }
1360     if (!strcmp(c, "GSM")) {
1361         response->ratType = NETWORK_TYPE_GSM;
1362     } else if (!strcmp(c, "LTE")) {
1363         response->ratType = NETWORK_TYPE_LTE;
1364     } else if (!strcmp(c, "WCDMA")) {
1365         response->ratType = NETWORK_TYPE_WCDMA;
1366     } else if (!strcmp(c, "CDMA")) {
1367         response->ratType = NETWORK_TYPE_CDMA;
1368     } else if (!strcmp(c, "TDSCDMA")) {
1369         response->ratType = NETWORK_TYPE_TDSCDMA;
1370     } else if (!strcmp(c, "NR")) {
1371         response->ratType = NETWORK_TYPE_NR;
1372     } else if (!strcmp(c, "NONE")) {
1373         (void)memset_s(response, sizeof(CurrentCellInfoVendor), 0, sizeof(CurrentCellInfoVendor));
1374         response->ratType = NETWORK_TYPE_UNKNOWN;
1375         return HRIL_ERR_SUCCESS;
1376     } else {
1377         TELEPHONY_LOGI("ParseCurrentCellInfo unSupport ratType line %{public}s", line);
1378         return -1;
1379     }
1380     err = NextInt(&line, &response->mcc);
1381     if (err < 0) {
1382         return err;
1383     }
1384     err = NextInt(&line, &response->mnc);
1385     if (err < 0) {
1386         return err;
1387     }
1388     ParseGetCurrentCellInfoResponseLineSwitch(line, response);
1389     return HRIL_ERR_SUCCESS;
1390 }
1391 
ResidentNetworkUpdated(struct ReportInfo reportInfo,const char * s)1392 int32_t ResidentNetworkUpdated(struct ReportInfo reportInfo, const char *s)
1393 {
1394     char *str = (char *)s;
1395     char *mcc = NULL;
1396     char *mnc = NULL;
1397     if (str == NULL) {
1398         TELEPHONY_LOGE("str is null.");
1399         return HRIL_ERR_GENERIC_FAILURE;
1400     }
1401     int32_t err = SkipATPrefix(&str);
1402     if (err < 0) {
1403         TELEPHONY_LOGE("ResidentNetworkUpdated skip failed: [%{public}s]", str);
1404         return HRIL_ERR_GENERIC_FAILURE;
1405     }
1406     if (NextStr(&str, &mcc) < 0) {
1407         TELEPHONY_LOGE("invalid str!");
1408         return HRIL_ERR_INVALID_MODEM_PARAMETER;
1409     }
1410     if (NextStr(&str, &mnc) < 0) {
1411         TELEPHONY_LOGE("invalid str!");
1412         return HRIL_ERR_INVALID_MODEM_PARAMETER;
1413     }
1414     int len = strlen(mcc) + strlen(mnc) + 1;
1415     if (len > PLMN_LEN) {
1416         TELEPHONY_LOGE("len is invalid.");
1417         return HRIL_ERR_INVALID_MODEM_PARAMETER;
1418     }
1419     if (strcat_s(mcc, len, mnc) < 0) {
1420         TELEPHONY_LOGE("strcat_s fail.");
1421         return HRIL_ERR_GENERIC_FAILURE;
1422     }
1423     reportInfo.notifyId = HNOTI_NETWORK_RESIDENT_NETWORK_UPDATED;
1424     reportInfo.type = HRIL_NOTIFICATION;
1425     reportInfo.error = HRIL_ERR_SUCCESS;
1426     OnNetworkReport(GetSlotId(NULL), reportInfo, (const uint8_t *)mcc, len);
1427     return HRIL_ERR_SUCCESS;
1428 }
1429 
ProcessCurrentCellList(struct ReportInfo reportInfo,const char * s)1430 int32_t ProcessCurrentCellList(struct ReportInfo reportInfo, const char *s)
1431 {
1432     char *str = (char *)s;
1433     if (str == NULL) {
1434         TELEPHONY_LOGE("ProcessCurrentCellList Str  is null.");
1435         return HRIL_ERR_GENERIC_FAILURE;
1436     }
1437     int32_t err = SkipATPrefix(&str);
1438     if (err < 0) {
1439         TELEPHONY_LOGE("ProcessCurrentCellList skip failed: [%{public}s]", str);
1440         return HRIL_ERR_GENERIC_FAILURE;
1441     }
1442     int32_t semicolonNum = FindSemicolonCharNum(str);
1443     if (semicolonNum > 0) {
1444         CurrentCellInfoVendor *cciv = NULL;
1445         CurrentCellInfoList cellList = {0, NULL};
1446         cciv = (CurrentCellInfoVendor *)calloc(semicolonNum, sizeof(CurrentCellInfoVendor));
1447         if (cciv == NULL) {
1448             TELEPHONY_LOGE("^MONSC: notify CurrentCellInfoVendor alloc failed!");
1449             return HRIL_ERR_MEMORY_FULL;
1450         }
1451         for (int32_t i = 0; i < semicolonNum; i++) {
1452             char *pStr = strsep(&str, ";");
1453             if (ParseGetCurrentCellInfoResponseLine(pStr, &cciv[cellList.itemNum]) != 0) {
1454                 continue;
1455             }
1456             cellList.itemNum++;
1457         }
1458         cellList.currentCellInfo = cciv;
1459         reportInfo.error = HRIL_ERR_SUCCESS;
1460         reportInfo.type = HRIL_NOTIFICATION;
1461         reportInfo.notifyId = HNOTI_NETWORK_CURRENT_CELL_UPDATED;
1462         OnNetworkReport(GetSlotId(NULL), reportInfo, (const uint8_t *)&cellList, sizeof(CurrentCellInfoList));
1463         if (cellList.currentCellInfo != NULL) {
1464             free(cellList.currentCellInfo);
1465         }
1466         return HRIL_ERR_SUCCESS;
1467     } else {
1468         TELEPHONY_LOGW("^MONSC: notify str format  unexpected: %{public}s", str);
1469         return HRIL_ERR_GENERIC_FAILURE;
1470     }
1471 }
1472 
ReqGetCurrentCellInfo(const ReqDataInfo * requestInfo)1473 void ReqGetCurrentCellInfo(const ReqDataInfo *requestInfo)
1474 {
1475     if (requestInfo == NULL) {
1476         return;
1477     }
1478     int32_t err = HRIL_ERR_SUCCESS;
1479     int32_t countCellInfo = 0;
1480     int32_t index = 0;
1481     ResponseInfo *responseInfo = NULL;
1482     Line *pLine = NULL;
1483     CurrentCellInfoVendor *currCellInfo = NULL;
1484     CurrentCellInfoList currCellInfoList = { 0, NULL };
1485     int32_t ret = SendCommandLock("AT^MONSC", "^MONSC:", DEFAULT_TIMEOUT, &responseInfo);
1486     struct ResponseAck respDataAck = { responseInfo, (uint8_t *)&currCellInfoList, sizeof(CurrentCellInfoList) };
1487     if (responseInfo == NULL) {
1488         TELEPHONY_LOGE("AT^MONSC responseInfo is nullptr");
1489         ResponseNetworkReport(requestInfo->slotId, requestInfo, HRIL_ERR_INVALID_RESPONSE, &respDataAck);
1490         return;
1491     }
1492     if (ret != 0 || responseInfo->success == 0) {
1493         TELEPHONY_LOGE("AT^MONSC send AT CMD failed!");
1494         err = (ret != 0) ? HRIL_ERR_GENERIC_FAILURE : HRIL_ERR_CMD_SEND_FAILURE;
1495         ResponseNetworkReport(requestInfo->slotId, requestInfo, err, &respDataAck);
1496         return;
1497     }
1498     for (countCellInfo = 0, pLine = responseInfo->head; pLine != NULL; pLine = pLine->next) {
1499         countCellInfo++;
1500     }
1501     if (countCellInfo == 0) {
1502         ResponseNetworkReport(requestInfo->slotId, requestInfo, HRIL_ERR_INVALID_RESPONSE, &respDataAck);
1503         return;
1504     }
1505     currCellInfo = (CurrentCellInfoVendor *)calloc(countCellInfo, sizeof(CurrentCellInfoVendor));
1506     if (currCellInfo == NULL) {
1507         TELEPHONY_LOGE("currentCellList  countCellInfo:%{public}d,currCellInfo alloc failed!", countCellInfo);
1508         ResponseNetworkReport(requestInfo->slotId, requestInfo, HRIL_ERR_MEMORY_FULL, &respDataAck);
1509         return;
1510     }
1511     for (pLine = responseInfo->head; pLine != NULL; pLine = pLine->next) {
1512         if (ParseGetCurrentCellInfoResponseLine(pLine->data, &currCellInfo[index]) != 0) {
1513             continue;
1514         }
1515         index++;
1516     }
1517     currCellInfoList.itemNum = index;
1518     currCellInfoList.currentCellInfo = currCellInfo;
1519     ResponseNetworkReport(requestInfo->slotId, requestInfo, err, &respDataAck);
1520     free(currCellInfo);
1521     return;
1522 }
1523 
OperListErrorHandler(AvailableOperInfo ** ppOperInfo,AvailableOperInfo * pOperInfo)1524 static int32_t OperListErrorHandler(AvailableOperInfo **ppOperInfo, AvailableOperInfo *pOperInfo)
1525 {
1526     TELEPHONY_LOGE("ReqGetNetworkSearchInformation Failed");
1527     DealNetworkSearchInformation(0, ppOperInfo, pOperInfo);
1528     return HRIL_ERR_GENERIC_FAILURE;
1529 }
1530 
ProcessOperListToUse(const char * list)1531 int32_t ProcessOperListToUse(const char *list)
1532 {
1533     const int32_t UNUSED_ITEM_COUNT = 2;
1534     AvailableOperInfo **ppOperInfo = NULL;
1535     AvailableOperInfo *pOperInfo = NULL;
1536     SetAtPauseFlag(false);
1537     alarm(0);
1538     int32_t item = ParseListItemNum(list);
1539     if (item <= UNUSED_ITEM_COUNT) {
1540         return OperListErrorHandler(ppOperInfo, pOperInfo);
1541     }
1542     char *line = (char *)list;
1543     int32_t ret = SkipATPrefix(&line);
1544     if (ret < 0) {
1545         return OperListErrorHandler(ppOperInfo, pOperInfo);
1546     }
1547 
1548     item = item - UNUSED_ITEM_COUNT;
1549     ppOperInfo = (AvailableOperInfo **)malloc(item * sizeof(AvailableOperInfo *));
1550     if (!ppOperInfo) {
1551         TELEPHONY_LOGE("ppOperInfo malloc fail");
1552         return OperListErrorHandler(ppOperInfo, pOperInfo);
1553     }
1554 
1555     pOperInfo = (AvailableOperInfo *)malloc(item * sizeof(AvailableOperInfo));
1556     if (!pOperInfo) {
1557         TELEPHONY_LOGE("pOperInfo malloc fail");
1558         return OperListErrorHandler(ppOperInfo, pOperInfo);
1559     }
1560     (void)memset_s(pOperInfo, item * sizeof(AvailableOperInfo), 0, item * sizeof(AvailableOperInfo));
1561     for (int32_t j = 0; j < item; j++) {
1562         ppOperInfo[j] = &(pOperInfo[j]);
1563     }
1564 
1565     int32_t operCount = ParseOperListInfo(line, item, pOperInfo, ppOperInfo);
1566     if (operCount != 0) {
1567         DealNetworkSearchInformation(operCount, ppOperInfo, pOperInfo);
1568     } else {
1569         return OperListErrorHandler(ppOperInfo, pOperInfo);
1570     }
1571     return HRIL_ERR_SUCCESS;
1572 }
1573 
PrepareSetNetworkSelectionMode(char * cmd,const HRilSetNetworkModeInfo * setModeInfo)1574 static bool PrepareSetNetworkSelectionMode(char *cmd, const HRilSetNetworkModeInfo *setModeInfo)
1575 {
1576     if (cmd == NULL || setModeInfo == NULL) {
1577         return false;
1578     }
1579     bool ret = true;
1580     TELEPHONY_LOGD("setModeInfo, serial123 = %{public}d", setModeInfo->selectMode);
1581     if (setModeInfo->selectMode == 0) {
1582         (void)sprintf_s(cmd, MAX_CMD_LENGTH, "%s", "AT+COPS=0");
1583     } else if (setModeInfo->selectMode == 1) {
1584         if (setModeInfo->oper == NULL) {
1585             ret = false;
1586         } else {
1587             (void)sprintf_s(cmd, MAX_CMD_LENGTH, "AT+COPS=1,2,%s", setModeInfo->oper);
1588         }
1589     } else {
1590         ret = false;
1591     }
1592     return ret;
1593 }
1594 
ReqSetNetworkSelectionMode(const ReqDataInfo * requestInfo,const HRilSetNetworkModeInfo * data)1595 void ReqSetNetworkSelectionMode(const ReqDataInfo *requestInfo, const HRilSetNetworkModeInfo *data)
1596 {
1597     if (requestInfo == NULL) {
1598         return;
1599     }
1600 
1601     ResponseInfo *responseInfo = NULL;
1602     char cmd[MAX_CMD_LENGTH] = {0};
1603     char *cmdBuff = cmd;
1604     struct ReportInfo reportInfo;
1605     HRilSetNetworkModeInfo *setModeInfo = (HRilSetNetworkModeInfo *)data;
1606     if (setModeInfo == NULL) {
1607         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_NULL_POINT, HRIL_RESPONSE, 0);
1608         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 1);
1609         TELEPHONY_LOGE("SetAutomaticMode HRIL_ERR_NULL_POINT");
1610         return;
1611     }
1612     if (!PrepareSetNetworkSelectionMode(cmdBuff, setModeInfo)) {
1613         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_PARAMETER, HRIL_RESPONSE, 0);
1614         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 1);
1615         TELEPHONY_LOGE("SetAutomaticMode HRIL_ERR_INVALID_PARAMETER");
1616         return;
1617     }
1618     TELEPHONY_LOGD("requestSetAutomaticModeForNetworks, cmd = %{public}s", cmd);
1619     int32_t err = SendCommandLock(cmd, NULL, 0, &responseInfo);
1620 
1621     if (responseInfo == NULL) {
1622         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_NULL_POINT, HRIL_RESPONSE, 0);
1623         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 1);
1624         TELEPHONY_LOGE("SetAutomaticMode responseInfo == NULL");
1625         return;
1626     }
1627     TELEPHONY_LOGD("SetAutomaticModeForNetworks, responseInfo->success = %{public}d", responseInfo->success);
1628     if (err != 0 || responseInfo->success == 0) {
1629         TELEPHONY_LOGE("ret is not equal to HDF_SUCCESS!");
1630         err = GetResponseErrorCode(responseInfo);
1631         reportInfo = CreateReportInfo(requestInfo, err, HRIL_RESPONSE, 0);
1632         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
1633         TELEPHONY_LOGE("SetAutomaticMode HRIL_ERR_GENERIC_FAILURE");
1634         FreeResponseInfo(responseInfo);
1635         return;
1636     }
1637     reportInfo = CreateReportInfo(requestInfo, err, HRIL_RESPONSE, 0);
1638     OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
1639     FreeResponseInfo(responseInfo);
1640 }
1641 
ReqGetNetworkSelectionMode(const ReqDataInfo * requestInfo)1642 void ReqGetNetworkSelectionMode(const ReqDataInfo *requestInfo)
1643 {
1644     if (requestInfo == NULL) {
1645         return;
1646     }
1647     int32_t err = HRIL_ERR_SUCCESS;
1648     ResponseInfo *responseInfo = NULL;
1649     int32_t state = 0;
1650     struct ReportInfo reportInfo;
1651     int32_t ret = SendCommandLock("AT+COPS?", "+COPS:", DEFAULT_TIMEOUT, &responseInfo);
1652     struct ResponseAck respDataAck = { responseInfo, NULL, 0 };
1653     if (responseInfo == NULL) {
1654         TELEPHONY_LOGE("responseInfo is nullptr");
1655         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_NULL_POINT, HRIL_RESPONSE, 0);
1656         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
1657         return;
1658     }
1659     if (ret != 0 || responseInfo->success == 0) {
1660         err = GetResponseErrorCode(responseInfo);
1661         TELEPHONY_LOGE("send AT CMD failed!");
1662         ResponseNetworkReport(requestInfo->slotId, requestInfo, err, &respDataAck);
1663         return;
1664     }
1665     if (responseInfo->head == NULL) {
1666         TELEPHONY_LOGE("no data!");
1667         err = HRIL_ERR_NULL_POINT;
1668         ResponseNetworkReport(requestInfo->slotId, requestInfo, err, &respDataAck);
1669         return;
1670     }
1671     char *line = responseInfo->head->data;
1672     ret = SkipATPrefix(&line);
1673     if (ret < 0) {
1674         err = HRIL_ERR_INVALID_RESPONSE;
1675         TELEPHONY_LOGE("Response is Invalid!");
1676         ResponseNetworkReport(requestInfo->slotId, requestInfo, err, &respDataAck);
1677         return;
1678     }
1679     ret = NextInt(&line, &state);
1680     if (ret < 0) {
1681         err = HRIL_ERR_INVALID_RESPONSE;
1682         TELEPHONY_LOGE("Response is Invalid!");
1683         ResponseNetworkReport(requestInfo->slotId, requestInfo, err, &respDataAck);
1684         return;
1685     }
1686     int32_t mode = (int32_t)state;
1687     respDataAck.respDataPointer = (uint8_t *)&mode;
1688     respDataAck.respDataLen = sizeof(int32_t);
1689     ResponseNetworkReport(requestInfo->slotId, requestInfo, HRIL_ERR_SUCCESS, &respDataAck);
1690     NotifyNetWorkTime(requestInfo->slotId);
1691     return;
1692 }
1693 
IntToNetTypeCmd(int32_t value,char * dst,int32_t dstMaxSize)1694 static int32_t IntToNetTypeCmd(int32_t value, char *dst, int32_t dstMaxSize)
1695 {
1696     int32_t len = MAX_CMD_LENGTH - 1;
1697     const int32_t MAX_PREFERRED_NET_ENUM = 99;
1698     const int32_t MIN_CHAR_ARRAY_SIZE = 50;
1699     const int32_t CONVERT_FAIL = -1;
1700     const int32_t DECIMAL = 10;
1701     if (strcat_s(dst, len, "AT^SYSCFGEX=\"") < 0) {
1702         return CONVERT_FAIL;
1703     }
1704     if ((value > MAX_PREFERRED_NET_ENUM) || (value < 0) || (dstMaxSize < MIN_CHAR_ARRAY_SIZE)) {
1705         return CONVERT_FAIL;
1706     }
1707     int32_t pos = strlen(dst);
1708     if (pos > len - ADD_LENGTH) {
1709         return CONVERT_FAIL;
1710     }
1711     if (value < DECIMAL) {
1712         dst[pos++] = '0';
1713         dst[pos++] = (value % DECIMAL) + '0';
1714     } else {
1715         dst[pos++] = (value / DECIMAL) + '0';
1716         dst[pos++] = (value % DECIMAL) + '0';
1717     }
1718     dst[pos++] = '\"';
1719     dst[pos] = '\0';
1720     return 0;
1721 }
1722 
PrepareCommandByNetworkType(HRilPreferredNetworkType net)1723 static char *PrepareCommandByNetworkType(HRilPreferredNetworkType net)
1724 {
1725     char *cmd = (char *)calloc(1, MAX_CMD_LENGTH);
1726     if (cmd != NULL) {
1727         TELEPHONY_LOGD("SetPreferredNetwork, net = %{public}d", net);
1728         if (net == HRIL_NETWORK_AUTO) {
1729             GenerateCommand(cmd, MAX_CMD_LENGTH, "%s", "AT^SYSCFGEX=\"00\",3FFFFFFF,1,2,7FFFFFFFFFFFFFFF,0,0");
1730         } else if (net == HRIL_NETWORK_GSM) {
1731             GenerateCommand(cmd, MAX_CMD_LENGTH, "%s", "AT^SYSCFGEX=\"01\",3FFFFFFF,1,2,7FFFFFFFFFFFFFFF,0,0");
1732         } else if (net == HRIL_NETWORK_WCDMA) {
1733             GenerateCommand(cmd, MAX_CMD_LENGTH, "%s", "AT^SYSCFGEX=\"02\",3FFFFFFF,1,2,7FFFFFFFFFFFFFFF,0,0");
1734         } else if (net == HRIL_NETWORK_LTE) {
1735             GenerateCommand(cmd, MAX_CMD_LENGTH, "%s", "AT^SYSCFGEX=\"03\",3FFFFFFF,1,2,7FFFFFFFFFFFFFFF,0,0");
1736         } else if (net == HRIL_NETWORK_LTE_WCDMA_GSM) {
1737             GenerateCommand(cmd, MAX_CMD_LENGTH, "%s", "AT^SYSCFGEX=\"030201\",3FFFFFFF,1,2,7FFFFFFFFFFFFFFF,0,0");
1738         } else if (net == HRIL_NETWORK_WCDMA_GSM) {
1739             GenerateCommand(cmd, MAX_CMD_LENGTH, "%s", "AT^SYSCFGEX=\"0201\",3FFFFFFF,1,2,7FFFFFFFFFFFFFFF,0,0");
1740         } else if (net == HRIL_NETWORK_LTE_WCDMA) {
1741             GenerateCommand(cmd, MAX_CMD_LENGTH, "%s", "AT^SYSCFGEX=\"0302\",3FFFFFFF,1,2,7FFFFFFFFFFFFFFF,0,0");
1742         } else {
1743             if (((net > HRIL_NETWORK_WCDMA_GSM) && (net <= HRIL_NETWORK_LTE_TDSCDMA_WCDMA_GSM_EVDO_CDMA)) ||
1744                 ((net >= HRIL_NETWORK_NR) && (net <= HRIL_NETWORK_NR_LTE_TDSCDMA_WCDMA_GSM_EVDO_CDMA))) {
1745                 char dst[MAX_CMD_LENGTH] = {0};
1746                 IntToNetTypeCmd(net, dst, MAX_CMD_LENGTH);
1747                 GenerateCommand(cmd, MAX_CMD_LENGTH, "%s", dst);
1748             } else {
1749                 free(cmd);
1750                 cmd = NULL;
1751             }
1752         }
1753     }
1754     return cmd;
1755 }
1756 
ReqSetPreferredNetwork(const ReqDataInfo * requestInfo,const int32_t * data)1757 void ReqSetPreferredNetwork(const ReqDataInfo *requestInfo, const int32_t *data)
1758 {
1759     if (requestInfo == NULL) {
1760         return;
1761     }
1762     ResponseInfo *responseInfo = NULL;
1763     struct ReportInfo reportInfo;
1764     if (data == NULL) {
1765         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_PARAMETER, HRIL_RESPONSE, 0);
1766         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
1767         return;
1768     }
1769     int32_t net = *(int32_t *)data;
1770     int32_t err = HRIL_ERR_INVALID_PARAMETER;
1771     char *cmd = PrepareCommandByNetworkType(net);
1772     if (cmd == NULL) {
1773         TELEPHONY_LOGE("RequestReqSetPreferredNetwork  HRIL_ERR_INVALID_PARAMETER cmd NULL");
1774         reportInfo = CreateReportInfo(requestInfo, err, HRIL_RESPONSE, 0);
1775         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
1776         return;
1777     }
1778     err = SendCommandLock(cmd, NULL, 0, &responseInfo);
1779     if (cmd != NULL) {
1780         free(cmd);
1781         cmd = NULL;
1782     }
1783     if (responseInfo == NULL) {
1784         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_GENERIC_FAILURE, HRIL_RESPONSE, 0);
1785         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
1786         return;
1787     }
1788     if (err != 0 || responseInfo->success == 0) {
1789         err = GetResponseErrorCode(responseInfo);
1790     }
1791     reportInfo = CreateReportInfo(requestInfo, err, HRIL_RESPONSE, 0);
1792     OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
1793     FreeResponseInfo(responseInfo);
1794 }
1795 
ParseNetTypeStr(const char * netType)1796 static int32_t ParseNetTypeStr(const char *netType)
1797 {
1798     TELEPHONY_LOGD("enter to [%{public}s]:%{public}d", __func__, __LINE__);
1799     if (netType == NULL) {
1800         TELEPHONY_LOGE("ParseNetTypeStr netType is null");
1801         return -1;
1802     }
1803     TELEPHONY_LOGD("netType: [%{public}s]", netType);
1804     if (strcmp(netType, AUTO_TYPE) == 0) {
1805         return HRIL_NETWORK_AUTO;
1806     } else if (strcmp(netType, GSM_TYPE) == 0) {
1807         return HRIL_NETWORK_GSM;
1808     } else if (strcmp(netType, WCDMA_TYPE) == 0) {
1809         return HRIL_NETWORK_WCDMA;
1810     } else if (strcmp(netType, LTE_TYPE) == 0) {
1811         return HRIL_NETWORK_LTE;
1812     } else if (strcmp(netType, LTE_WCDMA_TYPE) == 0) {
1813         return HRIL_NETWORK_LTE_WCDMA;
1814     } else if (strcmp(netType, LTE_WCDMA_GSM_TYPE) == 0) {
1815         return HRIL_NETWORK_LTE_WCDMA_GSM;
1816     } else if (strcmp(netType, WCDMA_GSM_TYPE) == 0) {
1817         return HRIL_NETWORK_WCDMA_GSM;
1818     } else {
1819         int32_t net = ConvertCharToInt32(netType);
1820         if (((net > HRIL_NETWORK_WCDMA_GSM) && (net <= HRIL_NETWORK_LTE_TDSCDMA_WCDMA_GSM_EVDO_CDMA)) ||
1821             ((net >= HRIL_NETWORK_NR) && (net <= HRIL_NETWORK_NR_LTE_TDSCDMA_WCDMA_GSM_EVDO_CDMA))) {
1822             return net;
1823         } else {
1824             return -1;
1825         }
1826     }
1827 }
1828 
ReqGetPreferredNetwork(const ReqDataInfo * requestInfo)1829 void ReqGetPreferredNetwork(const ReqDataInfo *requestInfo)
1830 {
1831     if (requestInfo == NULL) {
1832         return;
1833     }
1834     int32_t err = HRIL_ERR_SUCCESS;
1835     ResponseInfo *responseInfo = NULL;
1836     char *netTypeStr = "";
1837     int32_t ret = SendCommandLock("AT^SYSCFGEX?", "^SYSCFGEX:", DEFAULT_TIMEOUT, &responseInfo);
1838     struct ResponseAck respDataAck = { responseInfo, NULL, 0 };
1839     if (responseInfo == NULL) {
1840         TELEPHONY_LOGE("responseInfo is null");
1841         ResponseNetworkReport(requestInfo->slotId, requestInfo, HRIL_ERR_NULL_POINT, &respDataAck);
1842         return;
1843     }
1844     respDataAck.responseInfo = responseInfo;
1845     if (ret != 0 || !responseInfo->success) {
1846         err = GetResponseErrorCode(responseInfo);
1847         TELEPHONY_LOGE("send AT CMD failed!");
1848         ResponseNetworkReport(requestInfo->slotId, requestInfo, err, &respDataAck);
1849         return;
1850     }
1851     if (responseInfo->head == NULL) {
1852         ResponseNetworkReport(requestInfo->slotId, requestInfo, HRIL_ERR_INVALID_RESPONSE, &respDataAck);
1853         return;
1854     }
1855     Line *pLine = responseInfo->head;
1856     char *line = pLine->data;
1857     if (SkipATPrefix(&line) < 0) {
1858         ResponseNetworkReport(requestInfo->slotId, requestInfo, HRIL_ERR_INVALID_RESPONSE, &respDataAck);
1859         return;
1860     }
1861     if (NextStr(&line, &netTypeStr) < 0) {
1862         ResponseNetworkReport(requestInfo->slotId, requestInfo, HRIL_ERR_INVALID_RESPONSE, &respDataAck);
1863         return;
1864     }
1865     int32_t netType = ParseNetTypeStr(netTypeStr);
1866     respDataAck.respDataPointer = (uint8_t *)&netType;
1867     respDataAck.respDataLen = sizeof(int32_t);
1868     ResponseNetworkReport(requestInfo->slotId, requestInfo, HRIL_ERR_SUCCESS, &respDataAck);
1869 }
1870 
ProcessPhyChnlCfgNotify(struct ReportInfo reportInfo,char * srcStr)1871 void ProcessPhyChnlCfgNotify(struct ReportInfo reportInfo, char *srcStr)
1872 {
1873     int32_t contextIds[] = { 7, 8, 9 };
1874     HRilPhyChannelConfig configInfo = { 1, 2, 3, 4, 5, 6, 3 };
1875     configInfo.contextIds = contextIds;
1876     HRilChannelConfigList configInfoList = { 1, &configInfo };
1877     reportInfo.error = HRIL_ERR_SUCCESS;
1878     reportInfo.type = HRIL_NOTIFICATION;
1879     reportInfo.notifyId = HNOTI_NETWORK_PHY_CHNL_CFG_UPDATED;
1880     OnNetworkReport(GetSlotId(NULL), reportInfo, (const uint8_t *)&configInfoList, sizeof(HRilChannelConfigList));
1881 }
1882 
ReqGetPhysicalChannelConfig(const ReqDataInfo * requestInfo)1883 void ReqGetPhysicalChannelConfig(const ReqDataInfo *requestInfo)
1884 {
1885     if (requestInfo == NULL) {
1886         TELEPHONY_LOGE("ReqGetPhysicalChannelConfig requestInfo is NULL");
1887         return;
1888     }
1889     int32_t contextIds[] = { 7, 8, 9 };
1890     HRilPhyChannelConfig configInfo = { HRIL_SERVING_CELL_PRIMARY, RADIO_TECHNOLOGY_GSM, 1, 2, 3, 4, 5, 6, 1 };
1891     configInfo.contextIds = contextIds;
1892     HRilChannelConfigList configInfoList = { 1, &configInfo };
1893     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1894     OnNetworkReport(requestInfo->slotId, reportInfo, (const uint8_t *)&configInfoList, sizeof(HRilChannelConfigList));
1895 }
1896 
ReqSetLocateUpdates(const ReqDataInfo * requestInfo,HRilRegNotifyMode mode)1897 void ReqSetLocateUpdates(const ReqDataInfo *requestInfo, HRilRegNotifyMode mode)
1898 {
1899     if (requestInfo == NULL) {
1900         return;
1901     }
1902     ResponseInfo *responseInfo = NULL;
1903     char *cmd = NULL;
1904     if (mode == REG_NOTIFY_STAT_LAC_CELLID) {
1905         cmd = "AT+CREG=2";
1906     } else if (mode == REG_NOTIFY_STAT_ONLY) {
1907         cmd = "AT+CREG=1";
1908     } else {
1909         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_PARAMETER, HRIL_RESPONSE, 0);
1910         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
1911         TELEPHONY_LOGE("ReqSetLocateUpdates:  locateUpdateMode > 1");
1912         return;
1913     }
1914     int32_t err = SendCommandLock(cmd, NULL, DEFAULT_TIMEOUT, &responseInfo);
1915     if (responseInfo == NULL) {
1916         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_GENERIC_FAILURE, HRIL_RESPONSE, 0);
1917         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
1918         TELEPHONY_LOGE("ReqSetLocateUpdates responseInfo == NULL");
1919         return;
1920     }
1921     TELEPHONY_LOGD("ReqSetLocateUpdates, responseInfo->success = %{public}d", responseInfo->success);
1922     if (err != 0 || responseInfo->success == 0) {
1923         err = GetResponseErrorCode(responseInfo);
1924         TELEPHONY_LOGE("ReqSetLocateUpdates errcode = %{public}d", err);
1925     }
1926     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, err, HRIL_RESPONSE, 0);
1927     OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
1928     FreeResponseInfo(responseInfo);
1929 }
1930 
ReqSetNotificationFilter(const ReqDataInfo * requestInfo,const int32_t * newFilter)1931 void ReqSetNotificationFilter(const ReqDataInfo *requestInfo, const int32_t *newFilter)
1932 {
1933     if (requestInfo == NULL) {
1934         TELEPHONY_LOGE("ReqSetNotificationFilter requestInfo is NULL");
1935         return;
1936     }
1937     if (newFilter == NULL) {
1938         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_PARAMETER, HRIL_RESPONSE, 0);
1939         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
1940         return;
1941     }
1942     TELEPHONY_LOGD("ReqSetNotificationFilter success");
1943     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1944     OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
1945 }
1946 
ReqSetDeviceState(const ReqDataInfo * requestInfo,const int32_t * deviceStateType,const int32_t * deviceStateOn)1947 void ReqSetDeviceState(const ReqDataInfo *requestInfo, const int32_t *deviceStateType, const int32_t *deviceStateOn)
1948 {
1949     if (requestInfo == NULL) {
1950         TELEPHONY_LOGE("ReqSetDeviceState requestInfo is NULL");
1951         return;
1952     }
1953 
1954     if (deviceStateType == NULL || deviceStateOn == NULL) {
1955         struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_PARAMETER, HRIL_RESPONSE, 0);
1956         OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
1957         return;
1958     }
1959     TELEPHONY_LOGD("ReqSetDeviceState success");
1960     struct ReportInfo reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_SUCCESS, HRIL_RESPONSE, 0);
1961     OnNetworkReport(requestInfo->slotId, reportInfo, NULL, 0);
1962 }
1963 
NotifyNetWorkTime(int32_t slotId)1964 void NotifyNetWorkTime(int32_t slotId)
1965 {
1966     ResponseInfo *responseInfo = NULL;
1967     char timeStr[TIME_VALUE_LEN] = { 0 };
1968     struct ReportInfo reportInfo = { 0 };
1969 
1970     int32_t ret = SendCommandLock("AT+CCLK?", "+CCLK:", DEFAULT_TIMEOUT, &responseInfo);
1971     if (ret != 0 || responseInfo->success == 0) {
1972         TELEPHONY_LOGE("send AT CMD failed!");
1973         return;
1974     }
1975     if (responseInfo->head == NULL) {
1976         TELEPHONY_LOGE("no data!");
1977         return;
1978     }
1979     Line *pLine = responseInfo->head;
1980     if (pLine->data == NULL) {
1981         TELEPHONY_LOGE("no data!");
1982         return;
1983     }
1984     char *line = pLine->data;
1985     ret = SkipATPrefix(&line);
1986     if (ret < 0) {
1987         TELEPHONY_LOGE("format error!");
1988         return;
1989     }
1990     size_t len = strlen(line) - 1;
1991     if (len <= TIME_VALUE_OFFSET) {
1992         TELEPHONY_LOGE("invalid len!");
1993         return;
1994     }
1995     for (size_t i = TIME_VALUE_OFFSET; i < len; i++) {
1996         timeStr[i - TIME_VALUE_OFFSET] = *(line + i);
1997     }
1998     TELEPHONY_LOGI("netTime:%{public}s", timeStr);
1999     reportInfo.notifyId = HNOTI_NETWORK_TIME_UPDATED;
2000     reportInfo.type = HRIL_NOTIFICATION;
2001     reportInfo.error = HRIL_ERR_SUCCESS;
2002     OnNetworkReport(GetSlotId(NULL), reportInfo, (const uint8_t *)timeStr, TIME_VALUE_LEN);
2003     FreeResponseInfo(responseInfo);
2004 }
2005 
FillNeighboringCellSsbId(const char * str,NrCellSsbIdsVendor * nrSsbIdInfo)2006 int32_t FillNeighboringCellSsbId(const char *str, NrCellSsbIdsVendor *nrSsbIdInfo)
2007 {
2008     int32_t tempData = 0;
2009     const int32_t NBCELL_SSB_LIST = 4;
2010     const int32_t MAX_NBCELL_COUNT = 4;
2011     char *tempStr = (char *)str;
2012     if (NextIntNotSkipNextComma(&tempStr, &tempData) != 0) {
2013         return HRIL_ERR_INVALID_RESPONSE;
2014     }
2015     nrSsbIdInfo->nbCellCount = tempData;
2016     if (nrSsbIdInfo->nbCellCount > MAX_NBCELL_COUNT || nrSsbIdInfo->nbCellSsbList == NULL) {
2017         TELEPHONY_LOGE("ProcessNrSsbId failed nbCellCount: [%{public}d]", nrSsbIdInfo->nbCellCount);
2018         return HRIL_ERR_INVALID_RESPONSE;
2019     }
2020     (void)memset_s(nrSsbIdInfo->nbCellSsbList->ssbIdList, sizeof(SsbIdInfoVendor) * NBCELL_SSB_LIST, 0,
2021         sizeof(SsbIdInfoVendor) * NBCELL_SSB_LIST);
2022     (void)memset_s(nrSsbIdInfo->nbCellSsbList, sizeof(NeighboringCellSsbInfoVendor) * tempData, 0,
2023         sizeof(NeighboringCellSsbInfoVendor) * tempData);
2024     for (int32_t i = 0; i < nrSsbIdInfo->nbCellCount; i++) {
2025         if (NextIntNotSkipNextComma(&tempStr, &tempData) != 0) {
2026             return HRIL_ERR_INVALID_RESPONSE;
2027         }
2028         nrSsbIdInfo->nbCellSsbList[i].pci = tempData;
2029         if (NextIntNotSkipNextComma(&tempStr, &tempData) != 0) {
2030             return HRIL_ERR_INVALID_RESPONSE;
2031         }
2032         nrSsbIdInfo->nbCellSsbList[i].arfcn = tempData;
2033         if (NextIntNotSkipNextComma(&tempStr, &tempData) != 0) {
2034             return HRIL_ERR_INVALID_RESPONSE;
2035         }
2036         nrSsbIdInfo->nbCellSsbList[i].rsrp = tempData;
2037         if (NextIntNotSkipNextComma(&tempStr, &tempData) != 0) {
2038             return HRIL_ERR_INVALID_RESPONSE;
2039         }
2040         nrSsbIdInfo->nbCellSsbList[i].sinr = tempData;
2041         for (int32_t j = 0; j < NBCELL_SSB_LIST; j++) {
2042             if (NextIntNotSkipNextComma(&tempStr, &tempData) != 0) {
2043                 return HRIL_ERR_INVALID_RESPONSE;
2044             }
2045             nrSsbIdInfo->nbCellSsbList[i].ssbIdList[j].ssbId = tempData;
2046             if (NextIntNotSkipNextComma(&tempStr, &tempData) != 0) {
2047                 return HRIL_ERR_INVALID_RESPONSE;
2048             }
2049             nrSsbIdInfo->nbCellSsbList[i].ssbIdList[j].rsrp = tempData;
2050         }
2051     }
2052     return HRIL_ERR_SUCCESS;
2053 }
2054 
FillServingCellSsbId(const char * str,NrCellSsbIdsVendor * nrSsbIdInfo)2055 int32_t FillServingCellSsbId(const char *str, NrCellSsbIdsVendor *nrSsbIdInfo)
2056 {
2057     int32_t tempData = 0;
2058     const int32_t SCELL_SSB_LIST = 8;
2059     char *tempStr = (char *)str;
2060     (void)memset_s(nrSsbIdInfo->sCellSsbList, sizeof(SsbIdInfoVendor) * SCELL_SSB_LIST, 0,
2061         sizeof(SsbIdInfoVendor) * SCELL_SSB_LIST);
2062     for (int32_t i = 0; i < SCELL_SSB_LIST; i++) {
2063         if (NextIntNotSkipNextComma(&tempStr, &tempData) != 0) {
2064             return HRIL_ERR_INVALID_RESPONSE;
2065         }
2066         nrSsbIdInfo->sCellSsbList[i].ssbId = tempData;
2067         if (NextIntNotSkipNextComma(&tempStr, &tempData) != 0) {
2068             return HRIL_ERR_INVALID_RESPONSE;
2069         }
2070         nrSsbIdInfo->sCellSsbList[i].rsrp = tempData;
2071     }
2072     return HRIL_ERR_SUCCESS;
2073 }
2074 
ProcessNrSsbId(const char * str,NrCellSsbIdsVendor * nrSsbId)2075 int32_t ProcessNrSsbId(const char *str, NrCellSsbIdsVendor *nrSsbId)
2076 {
2077     int32_t tempData = 0;
2078     int64_t tempData64 = 0;
2079     char *tempStr = (char *)str;
2080     NrCellSsbIdsVendor *nrSsbIdInfo = (NrCellSsbIdsVendor *)nrSsbId;
2081     if ((tempStr == NULL) || (nrSsbIdInfo == NULL)) {
2082         TELEPHONY_LOGE("ProcessNrSsbId s or nrSsbIdInfo param is null");
2083         return HRIL_ERR_NULL_POINT;
2084     } else {
2085         (void)memset_s(nrSsbIdInfo, sizeof(NrCellSsbIdsVendor), 0, sizeof(NrCellSsbIdsVendor));
2086         TELEPHONY_LOGD("result: %{public}s", tempStr);
2087         int32_t err = SkipATPrefix(&tempStr);
2088         if (err < 0) {
2089             TELEPHONY_LOGE("skip failed: [%{public}s]", tempStr);
2090             return HRIL_ERR_INVALID_RESPONSE;
2091         }
2092         if (NextIntNotSkipNextComma(&tempStr, &tempData) != 0) {
2093             return HRIL_ERR_INVALID_RESPONSE;
2094         }
2095         nrSsbIdInfo->arfcn = tempData;
2096         if (NextInt64(&tempStr, &tempData64) != 0) {
2097             return HRIL_ERR_INVALID_RESPONSE;
2098         }
2099         nrSsbIdInfo->cid = tempData64;
2100         if (NextIntNotSkipNextComma(&tempStr, &tempData) != 0) {
2101             return HRIL_ERR_INVALID_RESPONSE;
2102         }
2103         nrSsbIdInfo->pic = tempData;
2104         if (NextIntNotSkipNextComma(&tempStr, &tempData) != 0) {
2105             return HRIL_ERR_INVALID_RESPONSE;
2106         }
2107         nrSsbIdInfo->rsrp = tempData;
2108         if (NextIntNotSkipNextComma(&tempStr, &tempData) != 0) {
2109             return HRIL_ERR_INVALID_RESPONSE;
2110         }
2111         nrSsbIdInfo->sinr = tempData;
2112         if (NextIntNotSkipNextComma(&tempStr, &tempData) != 0) {
2113             return HRIL_ERR_INVALID_RESPONSE;
2114         }
2115         nrSsbIdInfo->timeAdvance = tempData;
2116         if ((FillServingCellSsbId(tempStr, nrSsbIdInfo) != HRIL_ERR_SUCCESS) ||
2117             (FillNeighboringCellSsbId(tempStr, nrSsbIdInfo) != HRIL_ERR_SUCCESS)) {
2118             return HRIL_ERR_INVALID_RESPONSE;
2119         }
2120         return HRIL_ERR_SUCCESS;
2121     }
2122 }
2123 
ReqGetNrSsbId(const ReqDataInfo * requestInfo)2124 void ReqGetNrSsbId(const ReqDataInfo *requestInfo)
2125 {
2126     if (requestInfo == NULL) {
2127         return;
2128     }
2129     int32_t err = HRIL_ERR_SUCCESS;
2130     struct ReportInfo reportInfo;
2131     ResponseInfo *responseInfo = NULL;
2132     char *result = NULL;
2133     int32_t ret = SendCommandLock("AT+NRSSBID?", "+NRSSBID:", DEFAULT_TIMEOUT, &responseInfo);
2134     if (responseInfo == NULL) {
2135         TELEPHONY_LOGE("NRSSBID's responseInfo is nullptr!");
2136         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_NULL_POINT, HRIL_RESPONSE, 0);
2137         OnModemReport(requestInfo->slotId, reportInfo, NULL, 0);
2138         return;
2139     }
2140     if (ret != 0 || !responseInfo->success) {
2141         err = GetResponseErrorCode(responseInfo);
2142         TELEPHONY_LOGE("send AT+NRSSBID is failed!");
2143     }
2144     if (responseInfo->head != NULL) {
2145         result = responseInfo->head->data;
2146     }
2147     NrCellSsbIdsVendor nrSsbIdInfo = {0};
2148     ret = ProcessNrSsbId(result, &nrSsbIdInfo);
2149     if (ret != 0) {
2150         if (result != NULL) {
2151             TELEPHONY_LOGE("ProcessNrSsbId format  unexpected: %{public}s", result);
2152         }
2153         reportInfo = CreateReportInfo(requestInfo, HRIL_ERR_INVALID_RESPONSE, HRIL_RESPONSE, 0);
2154         OnModemReport(requestInfo->slotId, reportInfo, NULL, 0);
2155         FreeResponseInfo(responseInfo);
2156         return;
2157     }
2158     reportInfo = CreateReportInfo(requestInfo, err, HRIL_RESPONSE, 0);
2159     OnModemReport(requestInfo->slotId, reportInfo, (const uint8_t *)&nrSsbIdInfo, sizeof(NrCellSsbIdsVendor));
2160     FreeResponseInfo(responseInfo);
2161 }
2162