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/reference-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/reference-libril/ril_ex.h>
38
39 #define LIB_ARGS_PROPERTY "vendor.rild.libargs"
40 #define MAX_LIB_ARGS 16
41
usage(const char * argv0)42 static void usage(const char *argv0) {
43 fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
44 exit(EXIT_FAILURE);
45 }
46
47 extern char ril_service_name_base[MAX_SERVICE_NAME_LENGTH];
48 extern char ril_service_name[MAX_SERVICE_NAME_LENGTH];
49
50 extern void RIL_register (const RIL_RadioFunctions *callbacks);
51 extern void rilc_thread_pool ();
52
53 extern void RIL_register_socket (const RIL_RadioFunctions *(*rilUimInit)
54 (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv);
55
56 extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
57 void *response, size_t responselen);
58
59 extern void RIL_onRequestAck(RIL_Token t);
60
61 #if defined(ANDROID_MULTI_SIM)
62 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
63 size_t datalen, RIL_SOCKET_ID socket_id);
64 #else
65 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
66 size_t datalen);
67 #endif
68
69 extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
70 void *param, const struct timeval *relativeTime);
71
72
73 static struct RIL_Env s_rilEnv = {
74 RIL_onRequestComplete,
75 RIL_onUnsolicitedResponse,
76 RIL_requestTimedCallback,
77 RIL_onRequestAck
78 };
79
80 extern void RIL_startEventLoop();
81
make_argv(char * args,char ** argv)82 static int make_argv(char * args, char ** argv) {
83 // Note: reserve argv[0]
84 int count = 1;
85 char * tok;
86 char * s = args;
87
88 while ((tok = strtok(s, " \0"))) {
89 argv[count] = tok;
90 s = NULL;
91 count++;
92 }
93 return count;
94 }
95
main(int argc,char ** argv)96 int main(int argc, char **argv) {
97 // vendor ril lib path either passed in as -l parameter, or read from rild.libpath property
98 const char *rilLibPath = NULL;
99 // ril arguments either passed in as -- parameter, or read from rild.libargs property
100 char **rilArgv;
101 // handle for vendor ril lib
102 void *dlHandle;
103 // Pointer to ril init function in vendor ril
104 const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
105 // Pointer to sap init function in vendor ril
106 const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
107 const char *err_str = NULL;
108
109 // functions returned by ril init function in vendor ril
110 const RIL_RadioFunctions *funcs;
111 // flat to indicate if -- parameters are present
112 unsigned char hasLibArgs = 0;
113 char port[PROPERTY_VALUE_MAX] = {0};
114
115 int i;
116 // ril/socket id received as -c parameter, otherwise set to 0
117 const char *clientId = NULL;
118
119 RLOGD("**RIL Daemon Started - Version 1.4**");
120 RLOGD("**RILd param count=%d**", argc);
121
122 umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
123 for (i = 1; i < argc ;) {
124 if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
125 rilLibPath = argv[i + 1];
126 i += 2;
127 } else if (0 == strcmp(argv[i], "--")) {
128 i++;
129 hasLibArgs = 1;
130 break;
131 } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) {
132 clientId = argv[i+1];
133 i += 2;
134 } else {
135 usage(argv[0]);
136 }
137 }
138
139 if (clientId == NULL) {
140 clientId = "0";
141 } else if (atoi(clientId) >= MAX_RILDS) {
142 RLOGE("Max Number of rild's supported is: %d", MAX_RILDS);
143 exit(0);
144 }
145 if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) {
146 snprintf(ril_service_name, sizeof(ril_service_name), "%s%s", ril_service_name_base,
147 clientId);
148 }
149
150 property_get("ro.boot.modem_simulator_ports", port, "");
151 if (strcmp(port, "") == 0) {
152 // Assume "no-ril" case.
153 goto done;
154 } else {
155 rilLibPath = "libcuttlefish-ril-2.so";
156 }
157
158 dlHandle = dlopen(rilLibPath, RTLD_NOW);
159
160 if (dlHandle == NULL) {
161 RLOGE("dlopen failed: %s", dlerror());
162 exit(EXIT_FAILURE);
163 }
164
165 RIL_startEventLoop();
166
167 rilInit =
168 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
169 dlsym(dlHandle, "RIL_Init");
170
171 if (rilInit == NULL) {
172 RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
173 exit(EXIT_FAILURE);
174 }
175
176 dlerror(); // Clear any previous dlerror
177 rilUimInit =
178 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
179 dlsym(dlHandle, "RIL_SAP_Init");
180 err_str = dlerror();
181 if (err_str) {
182 RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
183 } else if (!rilUimInit) {
184 RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
185 }
186
187 if (hasLibArgs) {
188 rilArgv = argv + i - 1;
189 argc = argc -i + 1;
190 } else {
191 static char * newArgv[MAX_LIB_ARGS];
192 static char args[PROPERTY_VALUE_MAX];
193 rilArgv = newArgv;
194 property_get(LIB_ARGS_PROPERTY, args, "");
195 argc = make_argv(args, rilArgv);
196 }
197
198 rilArgv[argc++] = "-c";
199 rilArgv[argc++] = (char*)clientId;
200 RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
201
202 if (strcmp(port, "")) {
203 rilArgv[argc++] = "-m";
204 rilArgv[argc++] = port;
205 }
206
207 // Make sure there's a reasonable argv[0]
208 rilArgv[0] = argv[0];
209
210 funcs = rilInit(&s_rilEnv, argc, rilArgv);
211 if (funcs == NULL) {
212 RLOGE("RIL_Init rilInit failed.\n");
213 exit(EXIT_FAILURE);
214 }
215
216 RLOGD("RIL_Init rilInit completed");
217
218 RLOGD("RIL_Init callback versions = %d", funcs->version);
219
220 RIL_register(funcs);
221
222 RLOGD("RIL_Init RIL_register completed");
223
224 if (rilUimInit) {
225 RLOGD("RIL_register_socket started");
226 RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
227 }
228
229 RLOGD("RIL_register_socket completed");
230
231 done:
232
233 rilc_thread_pool();
234
235 RLOGD("RIL_Init starting sleep loop");
236 while (true) {
237 sleep(UINT32_MAX);
238 }
239 }
240