blob: 50710d65122f78925464de43a41d8eeda587c975 [file] [log] [blame]
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001/* //device/system/rild/rild.c
2**
Etan Cohend3652192014-06-20 08:28:44 -07003** Copyright 2006 The Android Open Source Project
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08004**
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 <telephony/ril.h>
28#define LOG_TAG "RILD"
29#include <utils/Log.h>
30#include <cutils/properties.h>
31#include <cutils/sockets.h>
Nick Kralevichfe4e1ec2013-02-28 14:16:41 -080032#include <sys/capability.h>
Elliott Hughes5d0cb9e2014-07-18 17:55:52 -070033#include <sys/prctl.h>
Andreas Gampe8ded9962015-04-06 12:20:24 -070034#include <sys/stat.h>
35#include <sys/types.h>
Dheeraj Shetty27976c42014-07-02 21:27:57 +020036#include <libril/ril_ex.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080037
38#include <private/android_filesystem_config.h>
39
40#define LIB_PATH_PROPERTY "rild.libpath"
41#define LIB_ARGS_PROPERTY "rild.libargs"
42#define MAX_LIB_ARGS 16
43
Dheeraj Shetty27976c42014-07-02 21:27:57 +020044static void usage(const char *argv0) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080045 fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
Elliott Hughese2a70cf2013-11-20 13:51:45 -080046 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080047}
48
Etan Cohend3652192014-06-20 08:28:44 -070049extern char rild[MAX_SOCKET_NAME_LENGTH];
50
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080051extern void RIL_register (const RIL_RadioFunctions *callbacks);
52
Dheeraj Shetty27976c42014-07-02 21:27:57 +020053extern void RIL_register_socket (RIL_RadioFunctions *(*rilUimInit)
54 (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv);
55
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080056extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020057 void *response, size_t responselen);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080058
Sanket Padawe6ff9a872016-01-27 15:09:12 -080059extern void RIL_onRequestAck(RIL_Token t);
60
Elliott Hughese102e952015-02-20 10:52:14 -080061extern void RIL_setRilSocketName(char *);
Etan Cohend3652192014-06-20 08:28:44 -070062
63#if defined(ANDROID_MULTI_SIM)
64extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020065 size_t datalen, RIL_SOCKET_ID socket_id);
Etan Cohend3652192014-06-20 08:28:44 -070066#else
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080067extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020068 size_t datalen);
Etan Cohend3652192014-06-20 08:28:44 -070069#endif
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080070
Wink Saville2932f312010-10-07 16:35:15 -070071extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
Dheeraj Shetty27976c42014-07-02 21:27:57 +020072 void *param, const struct timeval *relativeTime);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080073
74
75static struct RIL_Env s_rilEnv = {
76 RIL_onRequestComplete,
77 RIL_onUnsolicitedResponse,
Sanket Padawe6ff9a872016-01-27 15:09:12 -080078 RIL_requestTimedCallback,
79 RIL_onRequestAck
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080080};
81
82extern void RIL_startEventLoop();
83
Dheeraj Shetty27976c42014-07-02 21:27:57 +020084static int make_argv(char * args, char ** argv) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080085 // Note: reserve argv[0]
86 int count = 1;
87 char * tok;
88 char * s = args;
89
90 while ((tok = strtok(s, " \0"))) {
91 argv[count] = tok;
92 s = NULL;
93 count++;
94 }
95 return count;
96}
97
Dheeraj Shetty27976c42014-07-02 21:27:57 +020098int main(int argc, char **argv) {
Jorge Lucangeli Obeseaa7b3c2016-04-18 17:58:04 -070099 const char *rilLibPath = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800100 char **rilArgv;
101 void *dlHandle;
102 const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
Sanket Padawef0c8ca72016-06-30 15:01:08 -0700103 RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
Jorge Lucangeli Obeseaa7b3c2016-04-18 17:58:04 -0700104 const char *err_str = NULL;
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200105
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800106 const RIL_RadioFunctions *funcs;
107 char libPath[PROPERTY_VALUE_MAX];
108 unsigned char hasLibArgs = 0;
109
110 int i;
Etan Cohend3652192014-06-20 08:28:44 -0700111 const char *clientId = NULL;
112 RLOGD("**RIL Daemon Started**");
113 RLOGD("**RILd param count=%d**", argc);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800114
Nick Kralevichaea7d5b2010-12-10 15:53:02 -0800115 umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800116 for (i = 1; i < argc ;) {
117 if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
118 rilLibPath = argv[i + 1];
119 i += 2;
120 } else if (0 == strcmp(argv[i], "--")) {
121 i++;
122 hasLibArgs = 1;
123 break;
Etan Cohend3652192014-06-20 08:28:44 -0700124 } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) {
125 clientId = argv[i+1];
126 i += 2;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800127 } else {
128 usage(argv[0]);
129 }
130 }
131
Etan Cohend3652192014-06-20 08:28:44 -0700132 if (clientId == NULL) {
133 clientId = "0";
134 } else if (atoi(clientId) >= MAX_RILDS) {
135 RLOGE("Max Number of rild's supported is: %d", MAX_RILDS);
136 exit(0);
137 }
138 if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) {
Keun Soo Yim19736852016-04-13 17:11:20 -0700139 strlcat(rild, clientId, MAX_SOCKET_NAME_LENGTH);
Keun Soo Yim1067bb62016-04-13 19:06:26 -0700140 RIL_setRilSocketName(rild);
Etan Cohend3652192014-06-20 08:28:44 -0700141 }
142
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800143 if (rilLibPath == NULL) {
144 if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) {
145 // No lib sepcified on the command line, and nothing set in props.
146 // Assume "no-ril" case.
147 goto done;
148 } else {
149 rilLibPath = libPath;
150 }
151 }
152
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800153 dlHandle = dlopen(rilLibPath, RTLD_NOW);
154
155 if (dlHandle == NULL) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800156 RLOGE("dlopen failed: %s", dlerror());
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800157 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800158 }
159
160 RIL_startEventLoop();
161
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200162 rilInit =
163 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
164 dlsym(dlHandle, "RIL_Init");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800165
166 if (rilInit == NULL) {
Wink Saville8eb2a122012-11-19 16:05:13 -0800167 RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
Elliott Hughese2a70cf2013-11-20 13:51:45 -0800168 exit(EXIT_FAILURE);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800169 }
170
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200171 dlerror(); // Clear any previous dlerror
172 rilUimInit =
Sanket Padawef0c8ca72016-06-30 15:01:08 -0700173 (RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200174 dlsym(dlHandle, "RIL_SAP_Init");
175 err_str = dlerror();
176 if (err_str) {
177 RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
178 } else if (!rilUimInit) {
179 RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
180 }
181
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800182 if (hasLibArgs) {
183 rilArgv = argv + i - 1;
184 argc = argc -i + 1;
185 } else {
186 static char * newArgv[MAX_LIB_ARGS];
187 static char args[PROPERTY_VALUE_MAX];
188 rilArgv = newArgv;
189 property_get(LIB_ARGS_PROPERTY, args, "");
190 argc = make_argv(args, rilArgv);
191 }
192
Etan Cohend3652192014-06-20 08:28:44 -0700193 rilArgv[argc++] = "-c";
Jorge Lucangeli Obeseaa7b3c2016-04-18 17:58:04 -0700194 rilArgv[argc++] = (char*)clientId;
Etan Cohend3652192014-06-20 08:28:44 -0700195 RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
196
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800197 // Make sure there's a reasonable argv[0]
198 rilArgv[0] = argv[0];
199
200 funcs = rilInit(&s_rilEnv, argc, rilArgv);
Etan Cohend3652192014-06-20 08:28:44 -0700201 RLOGD("RIL_Init rilInit completed");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800202
203 RIL_register(funcs);
204
Etan Cohend3652192014-06-20 08:28:44 -0700205 RLOGD("RIL_Init RIL_register completed");
206
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200207 if (rilUimInit) {
208 RLOGD("RIL_register_socket started");
209 RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
210 }
211
212 RLOGD("RIL_register_socket completed");
213
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800214done:
215
Etan Cohend3652192014-06-20 08:28:44 -0700216 RLOGD("RIL_Init starting sleep loop");
Elliott Hughes164d2052013-11-20 14:53:08 -0800217 while (true) {
218 sleep(UINT32_MAX);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800219 }
220}