• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "hril_hdf.h"
17 
18 #include <libudev.h>
19 
20 #include "dlfcn.h"
21 #include "hdf_base.h"
22 #include "hril_enum.h"
23 #include "modem_adapter.h"
24 #include "parameter.h"
25 #include "stdlib.h"
26 #include "telephony_log_c.h"
27 
28 #define RIL_VENDOR_LIB_PATH "persist.sys.radio.vendorlib.path"
29 #define BASE_HEX 16
30 
31 static void *g_dlHandle = NULL;
32 static struct HRilReport g_reportOps = {
33     OnCallReport,
34     OnDataReport,
35     OnModemReport,
36     OnNetworkReport,
37     OnSimReport,
38     OnSmsReport,
39     OnTimerCallback
40 };
41 
GetVendorLibPath(char * path)42 static int32_t GetVendorLibPath(char *path)
43 {
44     int32_t code = GetParameter(RIL_VENDOR_LIB_PATH, "", path, PARAMETER_SIZE);
45     if (code <= 0) {
46         TELEPHONY_LOGE("Failed to get vendor library path through system properties. err:%{public}d", code);
47         return HDF_FAILURE;
48     }
49     return HDF_SUCCESS;
50 }
51 
GetPresetInformation(const char * vId,const char * pId)52 static UsbDeviceInfo *GetPresetInformation(const char *vId, const char *pId)
53 {
54     if (vId == NULL || pId == NULL) {
55         return NULL;
56     }
57     char *out = NULL;
58     UsbDeviceInfo *uDevInfo = NULL;
59     int32_t idVendor = (int32_t)strtol(vId, &out, BASE_HEX);
60     int32_t idProduct = (int32_t)strtol(pId, &out, BASE_HEX);
61     for (uint32_t i = 0; i < sizeof(g_usbModemVendorInfo) / sizeof(UsbDeviceInfo); i++) {
62         if (g_usbModemVendorInfo[i].idVendor == idVendor && g_usbModemVendorInfo[i].idProduct == idProduct) {
63             TELEPHONY_LOGI("list index:%{public}d", i);
64             uDevInfo = &g_usbModemVendorInfo[i];
65             break;
66         }
67     }
68     return uDevInfo;
69 }
70 
GetUsbDeviceInfo(void)71 static UsbDeviceInfo *GetUsbDeviceInfo(void)
72 {
73     struct udev *udev;
74     struct udev_enumerate *enumerate;
75     struct udev_list_entry *devices, *devListEntry;
76     struct udev_device *dev;
77     UsbDeviceInfo *uDevInfo = NULL;
78 
79     udev = udev_new();
80     if (udev == NULL) {
81         TELEPHONY_LOGE("Can't create udev");
82         return uDevInfo;
83     }
84     enumerate = udev_enumerate_new(udev);
85     if (enumerate == NULL) {
86         TELEPHONY_LOGE("Can't create enumerate");
87         udev_unref(udev);
88         return uDevInfo;
89     }
90     udev_enumerate_add_match_subsystem(enumerate, "tty");
91     udev_enumerate_scan_devices(enumerate);
92     devices = udev_enumerate_get_list_entry(enumerate);
93     udev_list_entry_foreach(devListEntry, devices) {
94         const char *path = udev_list_entry_get_name(devListEntry);
95         if (path == NULL) {
96             continue;
97         }
98         dev = udev_device_new_from_syspath(udev, path);
99         if (dev == NULL) {
100             continue;
101         }
102         dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");
103         if (!dev) {
104             TELEPHONY_LOGE("Unable to find parent usb device.");
105             return uDevInfo;
106         }
107         const char *cIdVendor = udev_device_get_sysattr_value(dev, "idVendor");
108         const char *cIdProduct = udev_device_get_sysattr_value(dev, "idProduct");
109         uDevInfo = GetPresetInformation(cIdVendor, cIdProduct);
110         udev_device_unref(dev);
111         if (uDevInfo != NULL) {
112             break;
113         }
114     }
115     udev_enumerate_unref(enumerate);
116     udev_unref(udev);
117     return uDevInfo;
118 }
119 
LoadVendor(void)120 static void LoadVendor(void)
121 {
122     const char *rilLibPath = NULL;
123     char vendorLibPath[PARAMETER_SIZE] = {0};
124     // Pointer to ril init function in vendor ril
125     const HRilOps *(*rilInitOps)(const struct HRilReport *) = NULL;
126     // functions returned by ril init function in vendor ril
127     const HRilOps *ops = NULL;
128 
129     UsbDeviceInfo *uDevInfo = GetUsbDeviceInfo();
130     if (GetVendorLibPath(vendorLibPath) == HDF_SUCCESS) {
131         rilLibPath = vendorLibPath;
132     } else if (uDevInfo != NULL) {
133         rilLibPath = uDevInfo->libPath;
134     } else {
135         TELEPHONY_LOGI("use default vendor lib.");
136         rilLibPath = g_usbModemVendorInfo[DEFAULT_MODE_INDEX].libPath;
137     }
138     if (rilLibPath == NULL) {
139         TELEPHONY_LOGE("dynamic library path is empty");
140         return;
141     }
142 
143     TELEPHONY_LOGI("RilInit LoadVendor start with rilLibPath:%{public}s", rilLibPath);
144     g_dlHandle = dlopen(rilLibPath, RTLD_NOW);
145     if (g_dlHandle == NULL) {
146         TELEPHONY_LOGE("dlopen %{public}s is fail. %{public}s", rilLibPath, dlerror());
147         return;
148     }
149     rilInitOps = (const HRilOps *(*)(const struct HRilReport *))dlsym(g_dlHandle, "RilInitOps");
150     if (rilInitOps == NULL) {
151         dlclose(g_dlHandle);
152         TELEPHONY_LOGE("RilInit not defined or exported");
153         return;
154     }
155     HRilInit();
156     ops = rilInitOps(&g_reportOps);
157     HRilRegOps(ops);
158     TELEPHONY_LOGI("HRilRegOps completed");
159 }
160 
InitRilAdapter(void)161 void InitRilAdapter(void)
162 {
163     LoadVendor();
164 }
165 
ReleaseRilAdapter(void)166 void ReleaseRilAdapter(void)
167 {
168     if (g_dlHandle == NULL) {
169         TELEPHONY_LOGE("g_dlHandle has been null");
170         return;
171     }
172     dlclose(g_dlHandle);
173 }
174