歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android RIL源碼研究筆記 の ril (一)

Android RIL源碼研究筆記 の ril (一)

日期:2017/3/1 10:50:13   编辑:Linux編程
Android源碼目錄hardware/ril/libril中總共包含5個C/CPP文件,它們分別是ril_commands.h、ril_unsol_commands.h、ril_event.h、ril_event.cpp和ril.cpp。這篇文章主要分析ril.cpp文件。

我們可以將該文件劃分為定義部分和實現部分,先來看定義部分:

  1. #define LOG_TAG "RILC"
  2. #include <hardware_legacy/power.h>
  3. #include <telephony/ril.h>
  4. #include <telephony/ril_cdma_sms.h>
  5. #include <cutils/sockets.h>
  6. #include <cutils/jstring.h>
  7. #include <cutils/record_stream.h>
  8. #include <utils/Log.h>
  9. #include <utils/SystemClock.h>
  10. #include <pthread.h>
  11. #include <binder/Parcel.h>
  12. #include <cutils/jstring.h>
  13. #include <sys/types.h>
  14. #include <pwd.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stdarg.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <time.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <ctype.h>
  25. #include <alloca.h>
  26. #include <sys/un.h>
  27. #include <assert.h>
  28. #include <netinet/in.h>
  29. #include <cutils/properties.h>
  30. #include <ril_event.h>
  31. namespace android {
  32. #define PHONE_PROCESS "radio"
  33. #define SOCKET_NAME_RIL "rild"
  34. #define SOCKET_NAME_RIL_DEBUG "rild-debug"
  35. #define ANDROID_WAKE_LOCK_NAME "radio-interface"
  36. #define PROPERTY_RIL_IMPL "gsm.version.ril-impl"
  37. // match with constant in RIL.java
  38. #define MAX_COMMAND_BYTES (8 * 1024)
  39. // Basically: memset buffers that the client library
  40. // shouldn't be using anymore in an attempt to find
  41. // memory usage issues sooner.
  42. #define MEMSET_FREED 1
  43. // 常見的獲取數組元素個數的方法
  44. #define NUM_ELEMS(a) (sizeof (a) / sizeof (a)[0])
  45. // 返回兩數中較小者
  46. #define MIN(a,b) ((a)<(b) ? (a) : (b))
  47. /* 回復類型:經過請求的回復和未經請求的回復*/
  48. #define RESPONSE_SOLICITED 0
  49. #define RESPONSE_UNSOLICITED 1
  50. /* Negative values for private RIL errno's */
  51. #define RIL_ERRNO_INVALID_RESPONSE -1
  52. // request, response, and unsolicited msg print macro
  53. // 即打印緩沖區printBuf的大小
  54. #define PRINTBUF_SIZE 8096
  55. // Enable RILC log
  56. #define RILC_LOG 0
  57. #if RILC_LOG
  58. // 三個宏的調用順序是startRequest - printRequest - closeRequest
  59. // 這樣打印出來的請求命令將包含在()中
  60. #define startRequest sprintf(printBuf, "(")
  61. #define closeRequest sprintf(printBuf, "%s)", printBuf)
  62. #define printRequest(token, req) \
  63. LOGD("[%04d]> %s %s", token, requestToString(req), printBuf)
  64. // 三個宏的調用順序是startResponse - printResponse - closeResponse
  65. // 這樣打印出來的回復信息將包含在{}中
  66. #define startResponse sprintf(printBuf, "%s {", printBuf)
  67. #define closeResponse sprintf(printBuf, "%s}", printBuf)
  68. #define printResponse LOGD("%s", printBuf)
  69. #define clearPrintBuf printBuf[0] = 0
  70. #define removeLastChar printBuf[strlen(printBuf)-1] = 0
  71. #define appendPrintBuf(x...) sprintf(printBuf, x)
  72. #else
  73. #define startRequest
  74. #define closeRequest
  75. #define printRequest(token, req)
  76. #define startResponse
  77. #define closeResponse
  78. #define printResponse
  79. #define clearPrintBuf
  80. #define removeLastChar
  81. #define appendPrintBuf(x...)
  82. #endif
  83. // 喚醒類型:不喚醒,部分喚醒
  84. enum WakeType {DONT_WAKE, WAKE_PARTIAL};
  85. // "經過請求的回復"結構體定義:請求號,命令分發處理函數,返回結果響應函數
  86. // 該結構體的取值見ril_commands.h文件
  87. typedef struct {
  88. int requestNumber;
  89. void (*dispatchFunction) (Parcel &p, struct RequestInfo *pRI);
  90. int(*responseFunction) (Parcel &p, void *response, size_t responselen);
  91. } CommandInfo;
  92. //"未經請求的回復"結構體定義:請求號,事件響應函數,喚醒類型
  93. // 該結構體的取值見ril_unsol_commands.h文件
  94. typedef struct {
  95. int requestNumber;
  96. int (*responseFunction) (Parcel &p, void *response, size_t responselen);
  97. WakeType wakeType;
  98. } UnsolResponseInfo;
  99. // 請求信息結構體,封裝CommandInfo,串成鏈表
  100. typedef struct RequestInfo {
  101. int32_t token; //this is not RIL_Token
  102. CommandInfo *pCI;
  103. struct RequestInfo *p_next;
  104. char cancelled;
  105. char local; // responses to local commands do not go back to command process
  106. } RequestInfo;
  107. // 用戶回調信息結構體
  108. typedef struct UserCallbackInfo {
  109. RIL_TimedCallback p_callback; // 回調函數
  110. void *userParam; // 回調函數的參數
  111. struct ril_event event; // ril event
  112. struct UserCallbackInfo *p_next; // 指向下一個回調信息結構(鏈表形式)
  113. } UserCallbackInfo;
  114. /*******************************************************************/
  115. // 初始化回調結構
  116. RIL_RadioFunctions s_callbacks = {0, NULL, NULL, NULL, NULL, NULL};
  117. static int s_registerCalled = 0;
  118. static pthread_t s_tid_dispatch; // 分發處理線程ID
  119. static pthread_t s_tid_reader; // 讀者線程ID
  120. static int s_started = 0;
  121. // 文件描述符初始化
  122. static int s_fdListen = -1;
  123. static int s_fdCommand = -1;
  124. static int s_fdDebug = -1;
  125. static int s_fdWakeupRead;
  126. static int s_fdWakeupWrite;
  127. // 5個相關的事件
  128. static struct ril_event s_commands_event;
  129. static struct ril_event s_wakeupfd_event;
  130. static struct ril_event s_listen_event;
  131. static struct ril_event s_wake_timeout_event;
  132. static struct ril_event s_debug_event;
  133. static const struct timeval TIMEVAL_WAKE_TIMEOUT = {1,0};
  134. // 初始化互斥量和條件變量
  135. static pthread_mutex_t s_pendingRequestsMutex = PTHREAD_MUTEX_INITIALIZER;
  136. static pthread_mutex_t s_writeMutex = PTHREAD_MUTEX_INITIALIZER;
  137. static pthread_mutex_t s_startupMutex = PTHREAD_MUTEX_INITIALIZER;
  138. static pthread_cond_t s_startupCond = PTHREAD_COND_INITIALIZER;
  139. static pthread_mutex_t s_dispatchMutex = PTHREAD_MUTEX_INITIALIZER;
  140. static pthread_cond_t s_dispatchCond = PTHREAD_COND_INITIALIZER;
  141. static RequestInfo *s_pendingRequests = NULL;
  142. static RequestInfo *s_toDispatchHead = NULL;
  143. static RequestInfo *s_toDispatchTail = NULL;
  144. static UserCallbackInfo *s_last_wake_timeout_info = NULL;
  145. static void *s_lastNITZTimeData = NULL;
  146. static size_t s_lastNITZTimeDataSize;
  147. #if RILC_LOG
  148. static char printBuf[PRINTBUF_SIZE]; // 緩存打印信息的數組
  149. #endif
  150. /*******************************************************************/
  151. // dispatch*系列函數是基帶處理器對應用處理器請求的處理函數
  152. static void dispatchVoid (Parcel& p, RequestInfo *pRI);
  153. static void dispatchString (Parcel& p, RequestInfo *pRI);
  154. static void dispatchStrings (Parcel& p, RequestInfo *pRI);
  155. static void dispatchInts (Parcel& p, RequestInfo *pRI);
  156. static void dispatchDial (Parcel& p, RequestInfo *pRI);
  157. static void dispatchSIM_IO (Parcel& p, RequestInfo *pRI);
  158. static void dispatchCallForward(Parcel& p, RequestInfo *pRI);
  159. static void dispatchRaw(Parcel& p, RequestInfo *pRI);
  160. static void dispatchSmsWrite (Parcel &p, RequestInfo *pRI);
  161. static void dispatchCdmaSms(Parcel &p, RequestInfo *pRI);
  162. static void dispatchCdmaSmsAck(Parcel &p, RequestInfo *pRI);
  163. static void dispatchGsmBrSmsCnf(Parcel &p, RequestInfo *pRI);
  164. static void dispatchCdmaBrSmsCnf(Parcel &p, RequestInfo *pRI);
  165. static void dispatchRilCdmaSmsWriteArgs(Parcel &p, RequestInfo *pRI);
  166. // response*系列函數是應用處理器對基帶處理器消息的響應函數
  167. // 包括請求回復響應函數和事件響應函數
  168. static int responseInts(Parcel &p, void *response, size_t responselen);
  169. static int responseStrings(Parcel &p, void *response, size_t responselen);
  170. static int responseString(Parcel &p, void *response, size_t responselen);
  171. static int responseVoid(Parcel &p, void *response, size_t responselen);
  172. static int responseCallList(Parcel &p, void *response, size_t responselen);
  173. static int responseSMS(Parcel &p, void *response, size_t responselen);
  174. static int responseSIM_IO(Parcel &p, void *response, size_t responselen);
  175. static int responseCallForwards(Parcel &p, void *response, size_t responselen);
  176. static int responseDataCallList(Parcel &p, void *response, size_t responselen);
  177. static int responseRaw(Parcel &p, void *response, size_t responselen);
  178. static int responseSsn(Parcel &p, void *response, size_t responselen);
  179. static int responseSimStatus(Parcel &p, void *response, size_t responselen);
  180. static int responseGsmBrSmsCnf(Parcel &p, void *response, size_t responselen);
  181. static int responseCdmaBrSmsCnf(Parcel &p, void *response, size_t responselen);
  182. static int responseCdmaSms(Parcel &p, void *response, size_t responselen);
  183. static int responseCellList(Parcel &p, void *response, size_t responselen);
  184. static int responseCdmaInformationRecords(Parcel &p,void *response, size_t responselen);
  185. static int responseRilSignalStrength(Parcel &p,void *response, size_t responselen);
  186. static int responseCallRing(Parcel &p, void *response, size_t responselen);
  187. static int responseCdmaSignalInfoRecord(Parcel &p,void *response, size_t responselen);
  188. static int responseCdmaCallWaiting(Parcel &p,void *response, size_t responselen);
  189. // 將數據結構信息轉換成字符串輸出
  190. extern "C" const char * requestToString(int request);
  191. extern "C" const char * failCauseToString(RIL_Errno);
  192. extern "C" const char * callStateToString(RIL_CallState);
  193. extern "C" const char * radioStateToString(RIL_RadioState);
  194. #ifdef RIL_SHLIB
  195. extern "C" void RIL_onUnsolicitedResponse(int unsolResponse, void *data,
  196. size_t datalen);
  197. #endif
  198. static UserCallbackInfo * internalRequestTimedCallback
  199. (RIL_TimedCallback callback, void *param,
  200. const struct timeval *relativeTime);
  201. /** Index == requestNumber */
  202. // 很不錯的一個用法,由於數組元素太多,為了代碼的整潔清晰,
  203. // 將數組元素的定義放在一個單獨的頭文件中,並用#include進來即可
  204. static CommandInfo s_commands[] = {
  205. #include "ril_commands.h"
  206. };
  207. static UnsolResponseInfo s_unsolResponses[] = {
  208. #include "ril_unsol_commands.h"
  209. };
Copyright © Linux教程網 All Rights Reserved