1 /* //device/system/rild/rild.c
2 **
3 ** Copyright 2006 The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <dlfcn.h>
21 #include <string.h>
22 #include <stdint.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <errno.h>
26
27 #include <guest/hals/ril/libril/ril.h>
28
29 #define LOG_TAG "RILD"
30 #include <log/log.h>
31 #include <cutils/properties.h>
32 #include <cutils/sockets.h>
33 #include <sys/capability.h>
34 #include <sys/prctl.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <guest/hals/ril/libril/ril_ex.h>
38
39 #define LIB_PATH_PROPERTY "vendor.rild.libpath"
40 #define LIB_ARGS_PROPERTY "vendor.rild.libargs"
41 #define MAX_LIB_ARGS 16
42
usage(const char * argv0)43 static void usage(const char *argv0) {
44 fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
45 exit(EXIT_FAILURE);
46 }
47
48 extern char ril_service_name_base[MAX_SERVICE_NAME_LENGTH];
49 extern char ril_service_name[MAX_SERVICE_NAME_LENGTH];
50
51 extern void RIL_register (const RIL_RadioFunctions *callbacks);
52 extern void rilc_thread_pool ();
53
54 extern void RIL_register_socket (const RIL_RadioFunctions *(*rilUimInit)
55 (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv);
56
57 extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
58 void *response, size_t responselen);
59
60 extern void RIL_onRequestAck(RIL_Token t);
61
62 #if defined(ANDROID_MULTI_SIM)
63 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
64 size_t datalen, RIL_SOCKET_ID socket_id);
65 #else
66 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
67 size_t datalen);
68 #endif
69
70 extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
71 void *param, const struct timeval *relativeTime);
72
73
74 static struct RIL_Env s_rilEnv = {
75 RIL_onRequestComplete,
76 RIL_onUnsolicitedResponse,
77 RIL_requestTimedCallback,
78 RIL_onRequestAck
79 };
80
81 extern void RIL_startEventLoop();
82
make_argv(char * args,char ** argv)83 static int make_argv(char * args, char ** argv) {
84 // Note: reserve argv[0]
85 int count = 1;
86 char * tok;
87 char * s = args;
88
89 while ((tok = strtok(s, " \0"))) {
90 argv[count] = tok;
91 s = NULL;
92 count++;
93 }
94 return count;
95 }
96
main(int argc,char ** argv)97 int main(int argc, char **argv) {
98 // vendor ril lib path either passed in as -l parameter, or read from rild.libpath property
99 const char *rilLibPath = NULL;
100 // ril arguments either passed in as -- parameter, or read from rild.libargs property
101 char **rilArgv;
102 // handle for vendor ril lib
103 void *dlHandle;
104 // Pointer to ril init function in vendor ril
105 const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
106 // Pointer to sap init function in vendor ril
107 const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
108 const char *err_str = NULL;
109
110 // functions returned by ril init function in vendor ril
111 const RIL_RadioFunctions *funcs;
112 // lib path from rild.libpath property (if it's read)
113 char libPath[PROPERTY_VALUE_MAX];
114 // flat to indicate if -- parameters are present
115 unsigned char hasLibArgs = 0;
116 char port[PROPERTY_VALUE_MAX] = {0};
117
118 int i;
119 // ril/socket id received as -c parameter, otherwise set to 0
120 const char *clientId = NULL;
121
122 RLOGD("**RIL Daemon Started - Version 1.4**");
123 RLOGD("**RILd param count=%d**", argc);
124
125 umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
126 for (i = 1; i < argc ;) {
127 if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
128 rilLibPath = argv[i + 1];
129 i += 2;
130 } else if (0 == strcmp(argv[i], "--")) {
131 i++;
132 hasLibArgs = 1;
133 break;
134 } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) {
135 clientId = argv[i+1];
136 i += 2;
137 } else {
138 usage(argv[0]);
139 }
140 }
141
142 if (clientId == NULL) {
143 clientId = "0";
144 } else if (atoi(clientId) >= MAX_RILDS) {
145 RLOGE("Max Number of rild's supported is: %d", MAX_RILDS);
146 exit(0);
147 }
148 if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) {
149 snprintf(ril_service_name, sizeof(ril_service_name), "%s%s", ril_service_name_base,
150 clientId);
151 }
152
153 if (rilLibPath == NULL) {
154 if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) {
155 // No lib sepcified on the command line, and nothing set in props.
156 // Assume "no-ril" case.
157 goto done;
158 } else {
159 rilLibPath = libPath;
160 }
161 }
162
163 property_get("ro.boot.modem_simulator_ports", port, "");
164 if (strcmp(port, "") != 0) {
165 rilLibPath = "libcuttlefish-ril-2.so";
166 }
167
168 dlHandle = dlopen(rilLibPath, RTLD_NOW);
169
170 if (dlHandle == NULL) {
171 RLOGE("dlopen failed: %s", dlerror());
172 exit(EXIT_FAILURE);
173 }
174
175 RIL_startEventLoop();
176
177 rilInit =
178 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
179 dlsym(dlHandle, "RIL_Init");
180
181 if (rilInit == NULL) {
182 RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
183 exit(EXIT_FAILURE);
184 }
185
186 dlerror(); // Clear any previous dlerror
187 rilUimInit =
188 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
189 dlsym(dlHandle, "RIL_SAP_Init");
190 err_str = dlerror();
191 if (err_str) {
192 RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
193 } else if (!rilUimInit) {
194 RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
195 }
196
197 if (hasLibArgs) {
198 rilArgv = argv + i - 1;
199 argc = argc -i + 1;
200 } else {
201 static char * newArgv[MAX_LIB_ARGS];
202 static char args[PROPERTY_VALUE_MAX];
203 rilArgv = newArgv;
204 property_get(LIB_ARGS_PROPERTY, args, "");
205 argc = make_argv(args, rilArgv);
206 }
207
208 rilArgv[argc++] = "-c";
209 rilArgv[argc++] = (char*)clientId;
210 RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
211
212 if (strcmp(port, "")) {
213 rilArgv[argc++] = "-m";
214 rilArgv[argc++] = port;
215 }
216
217 // Make sure there's a reasonable argv[0]
218 rilArgv[0] = argv[0];
219
220 funcs = rilInit(&s_rilEnv, argc, rilArgv);
221 if (funcs == NULL) {
222 RLOGE("RIL_Init rilInit failed.\n");
223 exit(EXIT_FAILURE);
224 }
225
226 RLOGD("RIL_Init rilInit completed");
227
228 RLOGD("RIL_Init callback versions = %d", funcs->version);
229
230 RIL_register(funcs);
231
232 RLOGD("RIL_Init RIL_register completed");
233
234 if (rilUimInit) {
235 RLOGD("RIL_register_socket started");
236 RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
237 }
238
239 RLOGD("RIL_register_socket completed");
240
241 done:
242
243 rilc_thread_pool();
244
245 RLOGD("RIL_Init starting sleep loop");
246 while (true) {
247 sleep(UINT32_MAX);
248 }
249 }
250