The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 1 | /* //device/system/rild/rild.c |
| 2 | ** |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 3 | ** Copyright 2006 The Android Open Source Project |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 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 <telephony/ril.h> |
| 28 | #define LOG_TAG "RILD" |
Dan Willemsen | 950d830 | 2017-04-03 21:58:13 -0700 | [diff] [blame] | 29 | #include <log/log.h> |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 30 | #include <cutils/properties.h> |
| 31 | #include <cutils/sockets.h> |
Nick Kralevich | fe4e1ec | 2013-02-28 14:16:41 -0800 | [diff] [blame] | 32 | #include <sys/capability.h> |
Elliott Hughes | 5d0cb9e | 2014-07-18 17:55:52 -0700 | [diff] [blame] | 33 | #include <sys/prctl.h> |
Andreas Gampe | 8ded996 | 2015-04-06 12:20:24 -0700 | [diff] [blame] | 34 | #include <sys/stat.h> |
| 35 | #include <sys/types.h> |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 36 | #include <libril/ril_ex.h> |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 37 | |
Jaekyun Seok | 1b4e637 | 2018-02-26 13:30:06 +0900 | [diff] [blame] | 38 | #if defined(PRODUCT_COMPATIBLE_PROPERTY) |
| 39 | #define LIB_PATH_PROPERTY "vendor.rild.libpath" |
| 40 | #define LIB_ARGS_PROPERTY "vendor.rild.libargs" |
| 41 | #else |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 42 | #define LIB_PATH_PROPERTY "rild.libpath" |
| 43 | #define LIB_ARGS_PROPERTY "rild.libargs" |
Jaekyun Seok | 1b4e637 | 2018-02-26 13:30:06 +0900 | [diff] [blame] | 44 | #endif |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 45 | #define MAX_LIB_ARGS 16 |
| 46 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 47 | static void usage(const char *argv0) { |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 48 | fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0); |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 49 | exit(EXIT_FAILURE); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Amit Mahajan | 7955c43 | 2017-03-28 11:17:55 -0700 | [diff] [blame] | 52 | extern char ril_service_name_base[MAX_SERVICE_NAME_LENGTH]; |
| 53 | extern char ril_service_name[MAX_SERVICE_NAME_LENGTH]; |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 54 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 55 | extern void RIL_register (const RIL_RadioFunctions *callbacks); |
Amit Mahajan | 18fe36b | 2016-08-25 11:19:21 -0700 | [diff] [blame] | 56 | extern void rilc_thread_pool (); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 57 | |
Nathan Harold | 14e4266 | 2017-07-31 19:44:46 -0700 | [diff] [blame] | 58 | extern void RIL_register_socket (const RIL_RadioFunctions *(*rilUimInit) |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 59 | (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv); |
| 60 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 61 | extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e, |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 62 | void *response, size_t responselen); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 63 | |
Sanket Padawe | 6ff9a87 | 2016-01-27 15:09:12 -0800 | [diff] [blame] | 64 | extern void RIL_onRequestAck(RIL_Token t); |
| 65 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 66 | #if defined(ANDROID_MULTI_SIM) |
| 67 | extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data, |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 68 | size_t datalen, RIL_SOCKET_ID socket_id); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 69 | #else |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 70 | extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data, |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 71 | size_t datalen); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 72 | #endif |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 73 | |
Wink Saville | 2932f31 | 2010-10-07 16:35:15 -0700 | [diff] [blame] | 74 | extern void RIL_requestTimedCallback (RIL_TimedCallback callback, |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 75 | void *param, const struct timeval *relativeTime); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 76 | |
| 77 | |
| 78 | static struct RIL_Env s_rilEnv = { |
| 79 | RIL_onRequestComplete, |
| 80 | RIL_onUnsolicitedResponse, |
Sanket Padawe | 6ff9a87 | 2016-01-27 15:09:12 -0800 | [diff] [blame] | 81 | RIL_requestTimedCallback, |
| 82 | RIL_onRequestAck |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | extern void RIL_startEventLoop(); |
| 86 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 87 | static int make_argv(char * args, char ** argv) { |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 88 | // Note: reserve argv[0] |
| 89 | int count = 1; |
| 90 | char * tok; |
| 91 | char * s = args; |
| 92 | |
| 93 | while ((tok = strtok(s, " \0"))) { |
| 94 | argv[count] = tok; |
| 95 | s = NULL; |
| 96 | count++; |
| 97 | } |
| 98 | return count; |
| 99 | } |
| 100 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 101 | int main(int argc, char **argv) { |
Amit Mahajan | 18fe36b | 2016-08-25 11:19:21 -0700 | [diff] [blame] | 102 | // vendor ril lib path either passed in as -l parameter, or read from rild.libpath property |
Jorge Lucangeli Obes | eaa7b3c | 2016-04-18 17:58:04 -0700 | [diff] [blame] | 103 | const char *rilLibPath = NULL; |
Amit Mahajan | 18fe36b | 2016-08-25 11:19:21 -0700 | [diff] [blame] | 104 | // ril arguments either passed in as -- parameter, or read from rild.libargs property |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 105 | char **rilArgv; |
Amit Mahajan | 18fe36b | 2016-08-25 11:19:21 -0700 | [diff] [blame] | 106 | // handle for vendor ril lib |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 107 | void *dlHandle; |
Amit Mahajan | 18fe36b | 2016-08-25 11:19:21 -0700 | [diff] [blame] | 108 | // Pointer to ril init function in vendor ril |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 109 | const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **); |
Amit Mahajan | 18fe36b | 2016-08-25 11:19:21 -0700 | [diff] [blame] | 110 | // Pointer to sap init function in vendor ril |
Nathan Harold | 14e4266 | 2017-07-31 19:44:46 -0700 | [diff] [blame] | 111 | const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **); |
Jorge Lucangeli Obes | eaa7b3c | 2016-04-18 17:58:04 -0700 | [diff] [blame] | 112 | const char *err_str = NULL; |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 113 | |
Amit Mahajan | 18fe36b | 2016-08-25 11:19:21 -0700 | [diff] [blame] | 114 | // functions returned by ril init function in vendor ril |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 115 | const RIL_RadioFunctions *funcs; |
Amit Mahajan | 18fe36b | 2016-08-25 11:19:21 -0700 | [diff] [blame] | 116 | // lib path from rild.libpath property (if it's read) |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 117 | char libPath[PROPERTY_VALUE_MAX]; |
Amit Mahajan | 18fe36b | 2016-08-25 11:19:21 -0700 | [diff] [blame] | 118 | // flat to indicate if -- parameters are present |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 119 | unsigned char hasLibArgs = 0; |
| 120 | |
| 121 | int i; |
Amit Mahajan | 18fe36b | 2016-08-25 11:19:21 -0700 | [diff] [blame] | 122 | // ril/socket id received as -c parameter, otherwise set to 0 |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 123 | const char *clientId = NULL; |
Amit Mahajan | 18fe36b | 2016-08-25 11:19:21 -0700 | [diff] [blame] | 124 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 125 | RLOGD("**RIL Daemon Started**"); |
| 126 | RLOGD("**RILd param count=%d**", argc); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 127 | |
Nick Kralevich | aea7d5b | 2010-12-10 15:53:02 -0800 | [diff] [blame] | 128 | umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 129 | for (i = 1; i < argc ;) { |
| 130 | if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) { |
| 131 | rilLibPath = argv[i + 1]; |
| 132 | i += 2; |
| 133 | } else if (0 == strcmp(argv[i], "--")) { |
| 134 | i++; |
| 135 | hasLibArgs = 1; |
| 136 | break; |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 137 | } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) { |
| 138 | clientId = argv[i+1]; |
| 139 | i += 2; |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 140 | } else { |
| 141 | usage(argv[0]); |
| 142 | } |
| 143 | } |
| 144 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 145 | if (clientId == NULL) { |
| 146 | clientId = "0"; |
| 147 | } else if (atoi(clientId) >= MAX_RILDS) { |
| 148 | RLOGE("Max Number of rild's supported is: %d", MAX_RILDS); |
| 149 | exit(0); |
| 150 | } |
| 151 | if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) { |
Elliott Hughes | ef75a2a | 2018-01-19 15:53:14 -0800 | [diff] [blame] | 152 | snprintf(ril_service_name, sizeof(ril_service_name), "%s%s", ril_service_name_base, |
| 153 | clientId); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 154 | } |
| 155 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 156 | if (rilLibPath == NULL) { |
| 157 | if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) { |
| 158 | // No lib sepcified on the command line, and nothing set in props. |
| 159 | // Assume "no-ril" case. |
| 160 | goto done; |
| 161 | } else { |
| 162 | rilLibPath = libPath; |
| 163 | } |
| 164 | } |
| 165 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 166 | dlHandle = dlopen(rilLibPath, RTLD_NOW); |
| 167 | |
| 168 | if (dlHandle == NULL) { |
Wink Saville | 8eb2a12 | 2012-11-19 16:05:13 -0800 | [diff] [blame] | 169 | RLOGE("dlopen failed: %s", dlerror()); |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 170 | exit(EXIT_FAILURE); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | RIL_startEventLoop(); |
| 174 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 175 | rilInit = |
| 176 | (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **)) |
| 177 | dlsym(dlHandle, "RIL_Init"); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 178 | |
| 179 | if (rilInit == NULL) { |
Wink Saville | 8eb2a12 | 2012-11-19 16:05:13 -0800 | [diff] [blame] | 180 | RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath); |
Elliott Hughes | e2a70cf | 2013-11-20 13:51:45 -0800 | [diff] [blame] | 181 | exit(EXIT_FAILURE); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 184 | dlerror(); // Clear any previous dlerror |
| 185 | rilUimInit = |
Nathan Harold | 14e4266 | 2017-07-31 19:44:46 -0700 | [diff] [blame] | 186 | (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **)) |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 187 | dlsym(dlHandle, "RIL_SAP_Init"); |
| 188 | err_str = dlerror(); |
| 189 | if (err_str) { |
| 190 | RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str); |
| 191 | } else if (!rilUimInit) { |
| 192 | RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath); |
| 193 | } |
| 194 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 195 | if (hasLibArgs) { |
| 196 | rilArgv = argv + i - 1; |
| 197 | argc = argc -i + 1; |
| 198 | } else { |
| 199 | static char * newArgv[MAX_LIB_ARGS]; |
| 200 | static char args[PROPERTY_VALUE_MAX]; |
| 201 | rilArgv = newArgv; |
| 202 | property_get(LIB_ARGS_PROPERTY, args, ""); |
| 203 | argc = make_argv(args, rilArgv); |
| 204 | } |
| 205 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 206 | rilArgv[argc++] = "-c"; |
Jorge Lucangeli Obes | eaa7b3c | 2016-04-18 17:58:04 -0700 | [diff] [blame] | 207 | rilArgv[argc++] = (char*)clientId; |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 208 | RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]); |
| 209 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 210 | // Make sure there's a reasonable argv[0] |
| 211 | rilArgv[0] = argv[0]; |
| 212 | |
| 213 | funcs = rilInit(&s_rilEnv, argc, rilArgv); |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 214 | RLOGD("RIL_Init rilInit completed"); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 215 | |
| 216 | RIL_register(funcs); |
| 217 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 218 | RLOGD("RIL_Init RIL_register completed"); |
| 219 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 220 | if (rilUimInit) { |
| 221 | RLOGD("RIL_register_socket started"); |
| 222 | RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv); |
| 223 | } |
| 224 | |
| 225 | RLOGD("RIL_register_socket completed"); |
| 226 | |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 227 | done: |
| 228 | |
Amit Mahajan | 18fe36b | 2016-08-25 11:19:21 -0700 | [diff] [blame] | 229 | rilc_thread_pool(); |
| 230 | |
Etan Cohen | d365219 | 2014-06-20 08:28:44 -0700 | [diff] [blame] | 231 | RLOGD("RIL_Init starting sleep loop"); |
Elliott Hughes | 164d205 | 2013-11-20 14:53:08 -0800 | [diff] [blame] | 232 | while (true) { |
| 233 | sleep(UINT32_MAX); |
The Android Open Source Project | 00f06fc | 2009-03-03 19:32:15 -0800 | [diff] [blame] | 234 | } |
| 235 | } |