| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | *(C) Copyright 2018 ASR Microelectronics |
| 3 | * All Rights Reserved |
| 4 | ******************************************************************************/ |
| 5 | /* ---------------------------------------------------------------------------- |
| 6 | * |
| 7 | * Filename: mbim_uicc.c |
| 8 | * |
| 9 | * |
| 10 | * Description: Microsoft Low-Level UICC Access |
| 11 | * |
| 12 | * |
| 13 | * Notes: |
| 14 | * |
| 15 | ******************************************************************************/ |
| 16 | |
| 17 | #define LOG_TAG "MBIM" |
| 18 | |
| 19 | /****************************************************************************** |
| 20 | * Include files |
| 21 | ******************************************************************************/ |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <stddef.h> |
| 25 | |
| 26 | #include "mbim_types.h" |
| 27 | #include "mbim_util.h" |
| 28 | #include "mbim_uicc.h" |
| 29 | #include "mbim_ril.h" |
| 30 | |
| 31 | #if defined MBIM_MTIL |
| 32 | #include "mbim_mtil.h" |
| 33 | #include "MtilAPI.h" |
| 34 | #endif |
| 35 | |
| 36 | #ifndef MBIM_UICC_UNUSEDPARAM |
| 37 | #define MBIM_UICC_UNUSEDPARAM() ((void) cid);((void) transactionId);((void) commandType);((void) infoBufLen);((void) infoBuf_p) |
| 38 | #endif |
| 39 | |
| 40 | extern MBIM_DATABASE mbimDb; |
| 41 | |
| 42 | |
| 43 | /****************************************************************************** |
| 44 | * Macros |
| 45 | ******************************************************************************/ |
| 46 | #define CREATE_UICC_CONTEXT(pContext, mBuf) \ |
| 47 | pContext = (P_MBIM_MESSAGE_CONTEXT)MBIM_MALLOC(sizeof(MBIM_MESSAGE_CONTEXT)); \ |
| 48 | if (pContext != NULL) { \ |
| 49 | pContext->hdr.cid = cid; \ |
| 50 | pContext->hdr.transId = transactionId; \ |
| 51 | pContext->hdr.uuidIndex = UUID_MS_UICC_LOW_LEVEL_INDEX; \ |
| 52 | pContext->hdr.indication= MBIM_REQUEST; \ |
| 53 | pContext->body = mBuf; \ |
| 54 | pContext->cmd = NULL; \ |
| 55 | } \ |
| 56 | else { \ |
| 57 | if (mBuf != NULL) \ |
| 58 | MBIM_FREE(mBuf); \ |
| 59 | MBIM_LOGE("UICC UUID: OUT OF MEMORY CID %d (TransactionId = %d, Cmd Type = %d)", cid, transactionId, commandType); \ |
| 60 | return MbimSendFunctionErrorMsg(transactionId, MBIM_ERROR_UNKNOWN); \ |
| 61 | } |
| 62 | |
| 63 | |
| 64 | #define ALLOCATE_RESPONSE_BUFFER(mBuf_p, mSize) \ |
| 65 | mBuf_p = (char *)MBIM_MALLOC(mSize); \ |
| 66 | if (mBuf_p == NULL) { \ |
| 67 | MBIM_LOGE("UICC UUID: OUT OF MEMORY CID %d (TransactionId = %d, Cmd Type = %d)", cid, transactionId, commandType); \ |
| 68 | return MbimSendFunctionErrorMsg(transactionId, MBIM_ERROR_UNKNOWN); \ |
| 69 | } |
| 70 | |
| 71 | |
| 72 | #define ALLOCATE_CMD_BUFFER(context_p, mBuf_p, mSize) \ |
| 73 | mBuf_p = (char *)MBIM_MALLOC(mSize); \ |
| 74 | if (mBuf_p == NULL) { \ |
| 75 | if (context_p->body != NULL) \ |
| 76 | MBIM_FREE(context_p->body); \ |
| 77 | if (context_p != NULL) \ |
| 78 | MBIM_FREE(context_p); \ |
| 79 | MBIM_LOGE("UICC UUID: OUT OF MEMORY CID %d (TransactionId = %d, Cmd Type = %d)", cid, transactionId, commandType); \ |
| 80 | return MbimSendFunctionErrorMsg(transactionId, MBIM_ERROR_UNKNOWN); \ |
| 81 | } else { \ |
| 82 | memset(mBuf_p, 0, mSize); \ |
| 83 | context_p->cmd = mBuf_p; \ |
| 84 | } |
| 85 | |
| 86 | /****************************************************************************** |
| 87 | * #defines |
| 88 | ******************************************************************************/ |
| 89 | #define DEFAULT_CONTEXT_ALLOC_SIZE (4000) |
| 90 | |
| 91 | |
| 92 | /****************************************************************************** |
| 93 | * Global Variables |
| 94 | ******************************************************************************/ |
| 95 | |
| 96 | |
| 97 | /****************************************************************************** |
| 98 | * Function prototypes |
| 99 | ******************************************************************************/ |
| 100 | int CID_UICC_NotSupported(UICC_CID_PARAMS); |
| 101 | int CID_UICC_ATR(UICC_CID_PARAMS); |
| 102 | int CID_UICC_OPENCHANNEL(UICC_CID_PARAMS); |
| 103 | int CID_UICC_CLOSECHANNEL(UICC_CID_PARAMS); |
| 104 | int CID_UICC_APDU(UICC_CID_PARAMS); |
| 105 | int CID_UICC_TERMINAL_CAPABILITY(UICC_CID_PARAMS); |
| 106 | int CID_UICC_RESET(UICC_CID_PARAMS); |
| 107 | |
| 108 | /****************************************************************************** |
| 109 | * External variables |
| 110 | ******************************************************************************/ |
| 111 | UiccUuidProcessors uiccCidProcessor[UUID_CID_MS_UICC_MAX] = { |
| 112 | CID_UICC_NotSupported, //0 |
| 113 | CID_UICC_ATR, |
| 114 | CID_UICC_OPENCHANNEL, |
| 115 | CID_UICC_CLOSECHANNEL, |
| 116 | CID_UICC_APDU, |
| 117 | CID_UICC_TERMINAL_CAPABILITY, |
| 118 | CID_UICC_RESET, |
| 119 | }; |
| 120 | |
| 121 | /****************************************************************************** |
| 122 | * Local variables |
| 123 | ******************************************************************************/ |
| 124 | const int MBIM_MAXLENGTH_APPID = 32; |
| 125 | const int MBIM_MAXLENGTH_APPNAME = 256; |
| 126 | const int MBIM_MAXNUM_PINREF = 8; |
| 127 | |
| 128 | /****************************************************************************** |
| 129 | * Code |
| 130 | ******************************************************************************/ |
| 131 | |
| 132 | |
| 133 | /*******************************************************************************\ |
| 134 | * Function: CID_UICC_NotSupported |
| 135 | * Description: This function sends Error to HOST when a unknown command is received |
| 136 | * Parameters: dataBuf - String |
| 137 | * |
| 138 | * Returns: 0=OK, <0=Error Code |
| 139 | \*******************************************************************************/ |
| 140 | int CID_UICC_NotSupported(UICC_CID_PARAMS) |
| 141 | { |
| 142 | int rc = MBIM_OK; |
| 143 | |
| 144 | //Added for compilation of unused parameters on Codeline |
| 145 | MBIM_UICC_UNUSEDPARAM(); |
| 146 | |
| 147 | MBIM_LOGE("UICC UUID: Command %d not supported (TransactionId = %d, Cmd Type = %d)", cid, transactionId, commandType); |
| 148 | rc = MbimSendCommandDone(transactionId, UUID_MS_UICC_LOW_LEVEL_INDEX, |
| 149 | cid, MBIM_STATUS_NO_DEVICE_SUPPORT, 0, NULL); |
| 150 | |
| 151 | return rc; |
| 152 | } |
| 153 | |
| 154 | int CID_UICC_ATR(UICC_CID_PARAMS) |
| 155 | { |
| 156 | char *buf = NULL; |
| 157 | P_MBIM_MESSAGE_CONTEXT mContext = NULL; |
| 158 | int rc = MBIM_STATUS_SUCCESS; |
| 159 | |
| 160 | if (commandType != MBIM_QUERY_COMMAND) |
| 161 | { |
| 162 | return CID_UICC_NotSupported(UICC_CID_PARAMS_USAGE); |
| 163 | } |
| 164 | |
| 165 | //Allocate buffer for response |
| 166 | ALLOCATE_RESPONSE_BUFFER(buf, DEFAULT_CONTEXT_ALLOC_SIZE); |
| 167 | |
| 168 | //Create the context for this command |
| 169 | CREATE_UICC_CONTEXT(mContext, buf); |
| 170 | |
| 171 | MbimTelRequestATR(mContext); |
| 172 | |
| 173 | return rc; |
| 174 | } |
| 175 | |
| 176 | int CID_UICC_RESET(UICC_CID_PARAMS) |
| 177 | { |
| 178 | char *buf = NULL; |
| 179 | P_MBIM_MESSAGE_CONTEXT mContext = NULL; |
| 180 | int rc = MBIM_STATUS_SUCCESS; |
| 181 | P_MBIM_SET_MS_UICC_RESET pSet = NULL; |
| 182 | |
| 183 | //Allocate buffer for response |
| 184 | ALLOCATE_RESPONSE_BUFFER(buf, DEFAULT_CONTEXT_ALLOC_SIZE); |
| 185 | |
| 186 | //Create the context for this command |
| 187 | CREATE_UICC_CONTEXT(mContext, buf); |
| 188 | |
| 189 | if (commandType == MBIM_SET_COMMAND) |
| 190 | { |
| 191 | pSet = (P_MBIM_SET_MS_UICC_RESET)infoBuf_p; |
| 192 | MbimTelRequestSetUICCReset(mContext, pSet->PassThroughAction); |
| 193 | } |
| 194 | else |
| 195 | MbimTelRequestQueryUICCReset(mContext); |
| 196 | |
| 197 | return rc; |
| 198 | } |
| 199 | |
| 200 | int CID_UICC_OPENCHANNEL(UICC_CID_PARAMS) |
| 201 | { |
| 202 | char *buf = NULL; |
| 203 | P_MBIM_MESSAGE_CONTEXT mContext = NULL; |
| 204 | int rc = MBIM_STATUS_SUCCESS; |
| 205 | |
| 206 | if (commandType != MBIM_SET_COMMAND) |
| 207 | { |
| 208 | return CID_UICC_NotSupported(UICC_CID_PARAMS_USAGE); |
| 209 | } |
| 210 | |
| 211 | //Allocate buffer for response |
| 212 | ALLOCATE_RESPONSE_BUFFER(buf, DEFAULT_CONTEXT_ALLOC_SIZE); |
| 213 | |
| 214 | //Create the context for this command |
| 215 | CREATE_UICC_CONTEXT(mContext, buf); |
| 216 | |
| 217 | //copy cmd |
| 218 | ALLOCATE_CMD_BUFFER(mContext, mContext->cmd, infoBufLen); |
| 219 | memcpy(mContext->cmd, (char *)infoBuf_p, infoBufLen); |
| 220 | |
| 221 | MbimTelRequestOPENCHNL(mContext); |
| 222 | |
| 223 | return rc; |
| 224 | } |
| 225 | |
| 226 | int CID_UICC_CLOSECHANNEL(UICC_CID_PARAMS) |
| 227 | { |
| 228 | char *buf = NULL; |
| 229 | P_MBIM_MESSAGE_CONTEXT mContext = NULL; |
| 230 | int rc = MBIM_STATUS_SUCCESS; |
| 231 | |
| 232 | if (commandType != MBIM_SET_COMMAND) |
| 233 | { |
| 234 | return CID_UICC_NotSupported(UICC_CID_PARAMS_USAGE); |
| 235 | } |
| 236 | |
| 237 | //Allocate buffer for response |
| 238 | ALLOCATE_RESPONSE_BUFFER(buf, DEFAULT_CONTEXT_ALLOC_SIZE); |
| 239 | |
| 240 | //Create the context for this command |
| 241 | CREATE_UICC_CONTEXT(mContext, buf); |
| 242 | |
| 243 | //copy cmd |
| 244 | ALLOCATE_CMD_BUFFER(mContext, mContext->cmd, infoBufLen); |
| 245 | memcpy(mContext->cmd, (char *)infoBuf_p, infoBufLen); |
| 246 | |
| 247 | MbimTelRequestCLOSECHNL(mContext); |
| 248 | |
| 249 | return rc; |
| 250 | } |
| 251 | |
| 252 | int CID_UICC_APDU(UICC_CID_PARAMS) |
| 253 | { |
| 254 | char *buf = NULL; |
| 255 | P_MBIM_MESSAGE_CONTEXT mContext = NULL; |
| 256 | int rc = MBIM_STATUS_SUCCESS; |
| 257 | |
| 258 | if (commandType != MBIM_SET_COMMAND) |
| 259 | { |
| 260 | return CID_UICC_NotSupported(UICC_CID_PARAMS_USAGE); |
| 261 | } |
| 262 | |
| 263 | //Allocate buffer for response |
| 264 | ALLOCATE_RESPONSE_BUFFER(buf, DEFAULT_CONTEXT_ALLOC_SIZE); |
| 265 | |
| 266 | //Create the context for this command |
| 267 | CREATE_UICC_CONTEXT(mContext, buf); |
| 268 | |
| 269 | //copy cmd |
| 270 | ALLOCATE_CMD_BUFFER(mContext, mContext->cmd, infoBufLen); |
| 271 | memcpy(mContext->cmd, (char *)infoBuf_p, infoBufLen); |
| 272 | |
| 273 | MbimTelRequestAPDU(mContext); |
| 274 | |
| 275 | return rc; |
| 276 | } |
| 277 | |
| 278 | int CID_UICC_TERMINAL_CAPABILITY(UICC_CID_PARAMS) |
| 279 | { |
| 280 | char *buf = NULL; |
| 281 | P_MBIM_MESSAGE_CONTEXT mContext = NULL; |
| 282 | int rc = MBIM_STATUS_SUCCESS; |
| 283 | |
| 284 | if (commandType == MBIM_QUERY_COMMAND) |
| 285 | { |
| 286 | //get from local |
| 287 | if (mbimDb.uiccTermCapLen) |
| 288 | { |
| 289 | MbimSendCommandDone(transactionId, UUID_MS_UICC_LOW_LEVEL_INDEX, cid, MBIM_STATUS_SUCCESS, mbimDb.uiccTermCapLen, mbimDb.uiccTermCap); |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | MBIM_MS_SET_UICC_TERMINAL_CAPABILITY cap; |
| 294 | cap.ElementCount = 0; |
| 295 | MbimSendCommandDone(transactionId, UUID_MS_UICC_LOW_LEVEL_INDEX, cid, MBIM_STATUS_SUCCESS, sizeof(MBIM_MS_SET_UICC_TERMINAL_CAPABILITY), (char *)&cap); |
| 296 | } |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | //Create the context for this command |
| 301 | CREATE_UICC_CONTEXT(mContext, buf); |
| 302 | |
| 303 | //copy cmd |
| 304 | ALLOCATE_CMD_BUFFER(mContext, mContext->cmd, infoBufLen); |
| 305 | memcpy(mContext->cmd, (char *)infoBuf_p, infoBufLen); |
| 306 | |
| 307 | int ret = MbimTelRequestTermCap(mContext); |
| 308 | |
| 309 | if (!ret) |
| 310 | { |
| 311 | //save |
| 312 | MBIM_FREE(mbimDb.uiccTermCap); |
| 313 | mbimDb.uiccTermCap = (char *)malloc(infoBufLen); |
| 314 | memcpy(mbimDb.uiccTermCap, infoBuf_p, infoBufLen); |
| 315 | mbimDb.uiccTermCapLen = infoBufLen; |
| 316 | } |
| 317 | |
| 318 | MbimSendCommandDone(transactionId, UUID_MS_UICC_LOW_LEVEL_INDEX, cid, (ret? MBIM_STATUS_FAILURE: MBIM_STATUS_SUCCESS), infoBufLen, infoBuf_p); |
| 319 | } |
| 320 | return rc; |
| 321 | } |