• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 "vendor_adapter.h"
17 
18 #include <fcntl.h>
19 #include <stddef.h>
20 #include <sys/prctl.h>
21 #include <termios.h>
22 
23 #include "at_call.h"
24 #include "at_data.h"
25 #include "at_modem.h"
26 #include "at_network.h"
27 #include "at_sim.h"
28 #include "at_sms.h"
29 #include "at_support.h"
30 #include "hril_notification.h"
31 #include "hril_public_struct.h"
32 #include "hril_vendor_call_defs.h"
33 #include "hril_vendor_data_defs.h"
34 #include "hril_vendor_modem_defs.h"
35 #include "hril_vendor_network_defs.h"
36 #include "hril_vendor_sim_defs.h"
37 #include "hril_vendor_sms_defs.h"
38 #include "parameter.h"
39 #include "pthread.h"
40 #include "sched.h"
41 #include "securec.h"
42 #include "telephony_log_c.h"
43 #include "unistd.h"
44 #include "vendor_report.h"
45 #include "vendor_util.h"
46 
47 #define DEVICE_PATH "/dev/ttyUSB0"
48 #define DEVICE_PATH_DEFAULT "/dev/ttyUSB"
49 
50 #define AT_TTY_PATH "const.telephony.ril.attty.path"
51 
52 static HRilRadioState g_radioState = HRIL_RADIO_POWER_STATE_UNAVAILABLE;
53 static pthread_mutex_t g_statusMutex = PTHREAD_MUTEX_INITIALIZER;
54 static pthread_cond_t g_statusCond = PTHREAD_COND_INITIALIZER;
55 static pthread_t g_eventListeners;
56 static int32_t g_fd = -1;
57 static int32_t g_atStatus = 0;
58 
59 static const HRilCallReq g_callReqOps = {
60     .GetCallList = ReqGetCallList,
61     .Dial = ReqDial,
62     .Hangup = ReqHangup,
63     .Reject = ReqReject,
64     .Answer = ReqAnswer,
65     .GetClip = ReqGetClip,
66     .SetClip = ReqSetClip,
67     .HoldCall = ReqHoldCall,
68     .UnHoldCall = ReqUnHoldCall,
69     .SwitchCall = ReqSwitchCall,
70     .CombineConference = ReqCombineConference,
71     .SeparateConference = ReqSeparateConference,
72     .CallSupplement = ReqCallSupplement,
73     .GetCallWaiting = ReqGetCallWaiting,
74     .SetCallWaiting = ReqSetCallWaiting,
75     .GetCallTransferInfo = ReqGetCallTransferInfo,
76     .SetCallTransferInfo = ReqSetCallTransferInfo,
77     .GetCallRestriction = ReqGetCallRestriction,
78     .SetCallRestriction = ReqSetCallRestriction,
79     .GetClir = ReqGetClir,
80     .SetClir = ReqSetClir,
81     .StartDtmf = ReqStartDtmf,
82     .SendDtmf = ReqSendDtmf,
83     .StopDtmf = ReqStopDtmf,
84     .GetCallPreferenceMode = ReqGetCallPreferenceMode,
85     .SetCallPreferenceMode = ReqSetCallPreferenceMode,
86     .SetUssd = ReqSetUssd,
87     .GetUssd = ReqGetUssd,
88     .GetMute = ReqGetMute,
89     .SetMute = ReqSetMute,
90     .GetEmergencyCallList = ReqGetEmergencyCallList,
91     .GetCallFailReason = ReqGetCallFailReason,
92     .SetEmergencyCallList = ReqSetEmergencyCallList,
93     .SetBarringPassword = ReqSetBarringPassword,
94     .CloseUnFinishedUssd = ReqCloseUnFinishedUssd,
95     .SetVonrSwitch = ReqSetVonrSwitch,
96 };
97 
98 static const HRilSimReq g_simReqOps = {
99     .GetSimStatus = ReqGetSimStatus,
100     .GetSimImsi = ReqGetSimImsi,
101     .GetSimIO = ReqGetSimIO,
102     .GetSimLockStatus = ReqGetSimLockStatus,
103     .SetSimLock = ReqSetSimLock,
104     .ChangeSimPassword = ReqChangeSimPassword,
105     .UnlockPin = ReqUnlockPin,
106     .UnlockPuk = ReqUnlockPuk,
107     .UnlockPin2 = ReqUnlockPin2,
108     .UnlockPuk2 = ReqUnlockPuk2,
109     .SetActiveSim = ReqSetActiveSim,
110     .SimStkSendTerminalResponse = ReqSimStkSendTerminalResponse,
111     .SimStkSendEnvelope = ReqSimStkSendEnvelope,
112     .SimStkSendCallSetupRequestResult = ReqSimStkSendCallSetupRequestResult,
113     .SimStkIsReady = ReqSimStkIsReady,
114     .GetRadioProtocol = ReqGetRadioProtocol,
115     .SetRadioProtocol = ReqSetRadioProtocol,
116     .SimOpenLogicalChannel = ReqSimOpenLogicalChannel,
117     .SimCloseLogicalChannel = ReqSimCloseLogicalChannel,
118     .SimTransmitApduLogicalChannel = ReqSimTransmitApduLogicalChannel,
119     .SimAuthentication = ReqSimAuthentication,
120     .UnlockSimLock = ReqUnlockSimLock,
121     .SendSimMatchedOperatorInfo = ReqSendSimMatchedOperatorInfo,
122 };
123 
124 static const HRilSmsReq g_smsReqOps = {
125     .SendGsmSms = ReqSendGsmSms,
126     .SendSmsAck = ReqSendSmsAck,
127     .SendCdmaSms = ReqSendCdmaSms,
128     .SendCdmaAck = ReqSendCdmaSmsAck,
129     .AddSimMessage = ReqWriteSimMessage,
130     .DelSimMessage = ReqDelSimMessage,
131     .UpdateSimMessage = ReqWriteSimMessage,
132     .SetSmscAddr = ReqSetSmscAddr,
133     .GetSmscAddr = ReqGetSmscAddr,
134     .SetCBConfig = ReqSetCBConfig,
135     .GetCBConfig = ReqGetCBConfig,
136     .GetCdmaCBConfig = ReqGetCdmaCBConfig,
137     .SetCdmaCBConfig = ReqSetCdmaCBConfig,
138     .AddCdmaSimMessageV2 = ReqAddCdmaSimMessage,
139     .DelCdmaSimMessage = ReqDelCdmaSimMessage,
140     .UpdateCdmaSimMessage = ReqUpdateCdmaSimMessage,
141 };
142 
143 static const HRilNetworkReq g_networkReqOps = {
144     .GetSignalStrength = ReqGetSignalStrength,
145     .GetCsRegStatus = ReqGetCsRegStatus,
146     .GetPsRegStatus = ReqGetPsRegStatus,
147     .GetOperatorInfo = ReqGetOperatorInfo,
148     .GetNetworkSearchInformation = ReqGetNetworkSearchInformation,
149     .GetNetworkSelectionMode = ReqGetNetworkSelectionMode,
150     .SetNetworkSelectionMode = ReqSetNetworkSelectionMode,
151     .SetPreferredNetwork = ReqSetPreferredNetwork,
152     .GetPreferredNetwork = ReqGetPreferredNetwork,
153     .GetNeighboringCellInfoList = ReqGetNeighboringCellInfoList,
154     .GetCurrentCellInfo = ReqGetCurrentCellInfo,
155     .GetPhysicalChannelConfig = ReqGetPhysicalChannelConfig,
156     .SetLocateUpdates = ReqSetLocateUpdates,
157     .SetNotificationFilter = ReqSetNotificationFilter,
158     .SetDeviceState = ReqSetDeviceState,
159     .GetNrSsbId = ReqGetNrSsbId,
160 };
161 
162 static const HRilDataReq g_dataReqOps = {
163     .SetInitApnInfo = ReqSetInitApnInfo,
164     .ActivatePdpContext = ReqActivatePdpContext,
165     .DeactivatePdpContext = ReqDeactivatePdpContext,
166     .GetPdpContextList = ReqGetPdpContextList,
167     .GetLinkBandwidthInfo = ReqGetLinkBandwidthInfo,
168     .SetLinkBandwidthReportingRule = ReqSetLinkBandwidthReportingRule,
169     .SetDataPermitted = ReqSetDataPermitted,
170     .GetLinkCapability = ReqGetLinkCapability,
171     .CleanAllConnections = ReqCleanAllConnections,
172 };
173 
174 static const HRilModemReq g_modemReqOps = {
175     .SetRadioState = ReqSetRadioState,
176     .GetRadioState = ReqGetRadioState,
177     .GetImei = ReqGetImei,
178     .GetMeid = ReqGetMeid,
179     .GetVoiceRadioTechnology = ReqGetVoiceRadioTechnology,
180 };
181 
182 HRilOps g_hrilOps = {
183     .callOps = &g_callReqOps,
184     .simOps = &g_simReqOps,
185     .smsOps = &g_smsReqOps,
186     .networkOps = &g_networkReqOps,
187     .dataOps = &g_dataReqOps,
188     .modemOps = &g_modemReqOps,
189 };
190 
GetRadioState(void)191 HRilRadioState GetRadioState(void)
192 {
193     return g_radioState;
194 }
195 
SetRadioState(HRilRadioState newState,int32_t rst)196 int32_t SetRadioState(HRilRadioState newState, int32_t rst)
197 {
198     char cmd[MAX_CMD_LENGTH] = {0};
199     ResponseInfo *pResponse = NULL;
200     HRilRadioState oldState;
201     const int32_t timeOut = 10000;
202     (void)memset_s(&oldState, sizeof(HRilRadioState), 0, sizeof(HRilRadioState));
203     struct ReportInfo reportInfo;
204     (void)memset_s(&reportInfo, sizeof(struct ReportInfo), 0, sizeof(struct ReportInfo));
205     if (g_atStatus > 0) {
206         pthread_cond_signal(&g_statusCond);
207         return -1;
208     }
209 
210     pthread_mutex_lock(&g_statusMutex);
211     oldState = g_radioState;
212     if (oldState == newState) {
213         TELEPHONY_LOGE("now then is same state");
214         pthread_mutex_unlock(&g_statusMutex);
215         return HRIL_ERR_REPEAT_STATUS;
216     }
217     g_radioState = newState;
218     pthread_cond_broadcast(&g_statusCond);
219     pthread_mutex_unlock(&g_statusMutex);
220 
221     if (oldState != g_radioState) {
222         (void)sprintf_s(cmd, MAX_CMD_LENGTH, "AT+CFUN=%u,%d", newState, rst);
223         int32_t err = SendCommandLock(cmd, NULL, timeOut, &pResponse);
224         if (err != 0 || !pResponse->success) {
225             TELEPHONY_LOGE("AT+CFUN send failed");
226             FreeResponseInfo(pResponse);
227             return -1;
228         }
229     }
230 
231     FreeResponseInfo(pResponse);
232     reportInfo.notifyId = HNOTI_MODEM_RADIO_STATE_UPDATED;
233     reportInfo.type = HRIL_NOTIFICATION;
234     reportInfo.error = HRIL_ERR_SUCCESS;
235     OnModemReport(GetSlotId(NULL), reportInfo, (const uint8_t *)&g_radioState, sizeof(HRilRadioState));
236     return 0;
237 }
238 
AtOnUnusual(void)239 static void AtOnUnusual(void)
240 {
241     ATCloseReadLoop();
242     g_atStatus = 1;
243     g_fd = -1;
244     int32_t err = SetRadioState(HRIL_RADIO_POWER_STATE_OFF, 0);
245     if (err == -1) {
246         TELEPHONY_LOGE("RadioState set failed");
247     }
248 }
249 
WaitAtClose(void)250 static void WaitAtClose(void)
251 {
252     pthread_mutex_lock(&g_statusMutex);
253 
254     while (g_atStatus == 0) {
255         pthread_cond_wait(&g_statusCond, &g_statusMutex);
256     }
257 
258     pthread_mutex_unlock(&g_statusMutex);
259 }
260 
ModemInit(void)261 static int32_t ModemInit(void)
262 {
263     ResponseInfo *pResponse = NULL;
264     int32_t err = SendCommandLock("ATE0Q0V1", NULL, 0, &pResponse);
265     if (err != 0 || !pResponse->success) {
266         TELEPHONY_LOGE("ATE0Q0V1 send failed");
267     }
268     FreeResponseInfo(pResponse);
269     /* Network registration events */
270     err = SendCommandLock("AT+CREG=2", NULL, 0, &pResponse);
271     if (err != 0 || !pResponse->success) {
272         SendCommandLock("AT+CREG=2", NULL, 0, &pResponse);
273     }
274     FreeResponseInfo(pResponse);
275 
276     /* GPRS registration events */
277     err = SendCommandLock("AT+CGREG=2", NULL, 0, &pResponse);
278     if (err != 0 || !pResponse->success) {
279         SendCommandLock("AT+CGREG=2", NULL, 0, &pResponse);
280     }
281     FreeResponseInfo(pResponse);
282     /* Enable the extended format of incoming calls */
283     SendCommandLock("AT+CRC=1", NULL, 0, NULL);
284     /* Set the SMS service type to Phase 2+ version */
285     SendCommandLock("AT+CSMS=1", NULL, 0, NULL);
286     /* Set the new SMS reporting method to +CMTI */
287     SendCommandLock("AT+CNMI=1,2,0,1,1", NULL, 0, NULL);
288     /* Enable active reporting of (U)SIM status */
289     SendCommandLock("AT^SIMST=1", NULL, 0, NULL);
290     /* Disabled  auto-answer */
291     SendCommandLock("ATS0=0", NULL, 0, NULL);
292     /* Extended errors */
293     SendCommandLock("AT+CMEE=1", NULL, 0, NULL);
294     /* Set to signal  reporting */
295     SendCommandLock("AT^HCSQ=3,10", NULL, 0, NULL);
296     SendCommandLock("AT^CURCEX=2,F7FFFFFFFFFFFF", NULL, 0, NULL);
297     /* IMS registration events */
298     SendCommandLock("AT+CIREG=2", NULL, 0, NULL);
299     /*  Call Waiting notifications */
300     SendCommandLock("AT+CCWA=1", NULL, 0, NULL);
301     /* Disabled muted */
302     SendCommandLock("AT+CMUT=0", NULL, 0, NULL);
303     /* Enabled CSSU unsolicited supp service notifications */
304     SendCommandLock("AT+CSSN=0,1", NULL, 0, NULL);
305     /* Set SMS PDU mode */
306     SendCommandLock("AT+CMGF=0", NULL, 0, NULL);
307     /* Set UNICODE character */
308     SendCommandLock("AT+CSCS=\"IRA\"", NULL, 0, NULL);
309     /* Set sms memory */
310     SendCommandLock("AT+CPMS=\"SM\",\"SM\",\"ME\"", NULL, 0, NULL);
311     /* Set to open network time reporting */
312     SendCommandLock("AT^TIME=1", NULL, 0, NULL);
313     /* Set to open network time zone reporting */
314     SendCommandLock("AT+CTZR=1", NULL, 0, NULL);
315     /* Enabled SRVCC status to report actively: This command complies with the 3GPP TS 27.007 protocol. */
316     SendCommandLock("AT+CIREP=1", NULL, 0, NULL);
317 
318     err = SetRadioState(HRIL_RADIO_POWER_STATE_ON, 0);
319     if (err == -1) {
320         TELEPHONY_LOGE("RadioState set failed");
321         struct ReportInfo reportInfo = { 0 };
322         reportInfo.notifyId = HNOTI_MODEM_RADIO_STATE_UPDATED;
323         reportInfo.type = HRIL_NOTIFICATION;
324         reportInfo.error = HRIL_ERR_SUCCESS;
325         OnModemReport(GetSlotId(NULL), reportInfo, (const uint8_t *)&g_radioState, sizeof(HRilRadioState));
326     }
327     TELEPHONY_LOGI("ModemInit finish radioState %{public}d", g_radioState);
328     return err;
329 }
330 
EventListeners(void)331 static void EventListeners(void)
332 {
333     prctl(PR_SET_NAME, "hril_event_listeners");
334     int32_t waitNextTryTime = SLEEP_TIME;
335     const char *devicePath = DEVICE_PATH;
336     char atTtyPath[PARAMETER_SIZE] = {0};
337 
338     usleep(DELAY_WAIT_MS); // Prevent slow loading of system properties.
339     if (GetParameter(AT_TTY_PATH, "", atTtyPath, PARAMETER_SIZE) > 0) {
340         devicePath = atTtyPath;
341     }
342 
343     TELEPHONY_LOGI("opening AT interface %{public}s", devicePath);
344     AtSetOnUnusual(AtOnUnusual);
345     while (TRUE) {
346         while (g_fd < 0) {
347             if (devicePath != NULL) {
348                 g_fd = open(devicePath, O_RDWR);
349             }
350             if (g_fd < 0) {
351                 TELEPHONY_LOGE("ril vendorlib,opening AT interface. retrying...");
352                 sleep(waitNextTryTime);
353             } else if (!memcmp(devicePath, DEVICE_PATH_DEFAULT, sizeof(DEVICE_PATH_DEFAULT) - 1)) {
354                 struct termios ios;
355                 tcgetattr(g_fd, &ios);
356                 ios.c_lflag = 0;
357                 tcsetattr(g_fd, TCSANOW, &ios);
358             }
359         }
360         g_atStatus = 0;
361         int32_t ret = ATStartReadLoop(g_fd, OnNotifyOps);
362         if (ret < 0) {
363             TELEPHONY_LOGE("AtRead error %d\n", ret);
364             return;
365         }
366         ModemInit();
367         sleep(1);
368         WaitAtClose();
369     }
370 }
371 
RilInitOps(const struct HRilReport * reportOps)372 const HRilOps *RilInitOps(const struct HRilReport *reportOps)
373 {
374     pthread_attr_t attr;
375     SetReportOps(reportOps);
376     pthread_attr_init(&attr);
377     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
378     int32_t ret = pthread_create(&g_eventListeners, &attr, (void *(*)(void *))EventListeners, NULL);
379     if (ret < 0) {
380         TELEPHONY_LOGE("EventListeners create failed %d \n", ret);
381     }
382     return &g_hrilOps;
383 }
384